decisions = {

	summon_pirate_conscripts = {
		only_independent = yes
		is_high_prio = yes
		ai_check_interval = 12
		potential = {
			independent = yes
			government = pirate_republic_government
			NOR = {
				has_character_modifier = pirate_conscription_small
				has_character_modifier = pirate_conscription_medium
				has_character_modifier = pirate_conscription_large
			}
		}
		allow = {
			is_adult = yes
			war = yes
			prisoner = no
			is_incapable = no
			prestige = 200
			wealth = 30
		}
		effect = { character_event = { id = piraterep.1 } }
		ai_will_do = { factor = 1 }
	}

	upgrade_missionary_guard = {
		only_independent = yes
		ai = no
		is_high_prio = yes
		ai_check_interval = 12
		potential = {
			independent = yes
			is_playable = yes
			has_game_rule = { name = sco_rule_hoextra value = on }
			any_demesne_title = {
				has_flag = force_missionary_government
				higher_tier_than = DUKE
			}
			any_vassal = {
				NOT = { primary_title = { title = c_benedictine_guard } }
				government = missionary_guard_government
				tier = COUNT
			}
		}
		allow = {
			is_adult = yes
			war = no
			prisoner = no
			is_incapable = no
			prestige = 5000
			piety = 300
			wealth = 200
		}
		effect = {
			custom_tooltip = {
				text = missionary_guard_upgraded
				any_vassal = {
					limit = { government = missionary_guard_government }
					primary_title = {
						create_title = {
							tier = DUKE
							name = DYNAMIC_MISSIONARY_GUARD
							landless = yes
							temporary = no
							custom_created = yes
							culture = ROOT
							holder = PREV
							base_title = THIS
							mercenary = yes
							army_template = default_missionary_guard_composition
							replace_captain_on_death = no
							siphons_income_to_creator = 0.2
							ruler = "CAPTAIN"
							ruler_female = "CAPTAIN"
							foa = "CAPTAIN_FOA"
						}
						new_title = {
							religion = ROOT
							set_flag = force_missionary_guard_government
							make_primary_title = yes
							de_jure_liege = ROOT
						}
						#Add all existing upgrades to the new guard duchy
						while = {
							limit = { check_variable = { which = upgrade_light1 value >= 1 } }
							subtract_variable = { which = upgrade_light1 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_infantry
									change = 40
								}
								change_mercenary_composition = {
									type = archers
									change = 20
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_light2 value >= 1 } }
							subtract_variable = { which = upgrade_light2 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_infantry
									change = 60
								}
								change_mercenary_composition = {
									type = archers
									change = 40
								}
								change_mercenary_composition = {
									type = light_cavalry
									change = 20
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_light3 value >= 1 } }
							subtract_variable = { which = upgrade_light3 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_infantry
									change = 80
								}
								change_mercenary_composition = {
									type = archers
									change = 60
								}
								change_mercenary_composition = {
									type = light_cavalry
									change = 40
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_heavy1 value >= 1 } }
							subtract_variable = { which = upgrade_heavy1 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = pikemen
									change = 25
								}
								change_mercenary_composition = {
									type = heavy_infantry
									change = 15
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_heavy2 value >= 1 } }
							subtract_variable = { which = upgrade_heavy2 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = pikemen
									change = 50
								}
								change_mercenary_composition = {
									type = heavy_infantry
									change = 30
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_heavy3 value >= 1 } }
							subtract_variable = { which = upgrade_heavy3 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = pikemen
									change = 60
								}
								change_mercenary_composition = {
									type = heavy_infantry
									change = 40
								}
								change_mercenary_composition = {
									type = knights
									change = 20
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_cav1 value >= 1 } }
							subtract_variable = { which = upgrade_cav1 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_cavalry
									change = 40
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_cav2 value >= 1 } }
							subtract_variable = { which = upgrade_cav2 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_cavalry
									change = 60
								}
								change_mercenary_composition = {
									type = knights
									change = 20
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_cav3 value >= 1 } }
							subtract_variable = { which = upgrade_cav3 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = light_cavalry
									change = 80
								}
								change_mercenary_composition = {
									type = knights
									change = 40
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_boats1 value >= 1 } }
							subtract_variable = { which = upgrade_boats1 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = galleys
									change = 20
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_boats2 value >= 1 } }
							subtract_variable = { which = upgrade_boats2 value = 1 }
							new_title = {
								change_mercenary_composition = {
									type = galleys
									change = 40
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_ltoh value >= 1 } }
							subtract_variable = { which = upgrade_ltoh value = 1 }
							new_title = {
								change_mercenary_composition = {
									from = light_infantry
									type = heavy_infantry
									change = 50
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_ltop value >= 1 } }
							subtract_variable = { which = upgrade_ltop value = 1 }
							new_title = {
								change_mercenary_composition = {
									from = light_infantry
									type = pikemen
									change = 50
								}
							}
						}
						while = {
							limit = { check_variable = { which = upgrade_ctok value >= 1 } }
							subtract_variable = { which = upgrade_ctok value = 1 }
							new_title = {
								change_mercenary_composition = {
									from = light_cavalry
									type = knights
									change = 50
								}
							}
						}
						#An hackfix to destroy titular county titles, works well enough and is only noticeable for 1 day
						set_title_landless = { title = THIS status = no }
						create_random_soldier = {
							random_traits = yes
							health = 6
							fertility = 0.8
						}
						new_character = { gain_title = PREV }
						unsafe_destroy_landed_title = THIS
						activate_title = { title = THIS status = no }
					}
					set_defacto_liege = ROOT #if done here vassal is shown in menus immediately
				}
			}
			wealth = -200
		}
	}

}

targeted_decisions = {

	find_suitable_spouse_other = {
		is_high_prio = no
		ai = no
		filter = realm
		diplomacy_icon = GFX_sco_diplomarriage
		from_potential = {
			OR = {
				government = sisterhood_government
				AND = {
					government = order_government
					any_liege = {
						government = sisterhood_government
					}
				}
				AND = {
					OR = {
						government = order_government
						government = nunorder_government
						government = warorder_government
					}
					has_game_rule = { name = sco_rule_homarriage value = all }
				}
			}
		}
		potential = { is_marriage_adult = yes }
		allow = {
			prisoner = no
			is_incapable = no
			is_betrothed = no
			is_married = no
			is_consort = no
		}
		effect = {
			if = {
				limit = { is_female = yes }
				random_list = {
					70 = {
						create_random_soldier = {
							random_traits = yes
							dynasty = culture
							female = no
							health = 6
							fertility = 0.8
							add_trait = zealous
							add_trait = lustful
						}
					}
					30 = {
						create_random_priest = {
							random_traits = yes
							dynasty = culture
							female = no
							health = 6
							fertility = 0.8
							add_trait = zealous
							add_trait = lustful
						}
					}
				}
				new_character = {
					remove_trait = cynical
					remove_trait = chaste
					add_spouse_matrilineal = ROOT
				}
			}
			else = {
				random_list = {
					70 = {
						create_random_soldier = {
							random_traits = yes
							dynasty = culture
							female = yes
							health = 6
							fertility = 0.8
							add_trait = zealous
							add_trait = lustful
						}
					}
					30 = {
						create_random_priest = {
							random_traits = yes
							dynasty = culture
							female = yes
							health = 6
							fertility = 0.8
							add_trait = zealous
							add_trait = lustful
						}
					}
				}
				new_character = {
					remove_trait = cynical
					remove_trait = chaste
					add_spouse = ROOT
				}
			}
		}
	}
	
	find_suitable_spouse_self = {
		is_high_prio = no
		only_rulers = yes
		ai_check_interval = 4
		filter = dynasty_including_me
		ai_target_filter = self
		diplomacy_icon = GFX_sco_diplomarriage
		potential = {
			OR = {
				government = sisterhood_government
				AND = { #Added to target landless dynasty members in the decision taker's court, player-only for now
					is_ruler = no
					employer = { character = FROM }
					OR = {
						employer = { government = sisterhood_government }
						AND = {
							OR = {
								employer = { government = order_government }
								employer = { government = nunorder_government }
							}
							has_game_rule = { name = sco_rule_homarriage value = all }
						}
					}
				}
				AND = {
					government = order_government
					any_liege = { government = sisterhood_government }
				}
				AND = {
					OR = {
						government = order_government
						government = nunorder_government
						government = warorder_government
					}
					has_game_rule = { name = sco_rule_homarriage value = all }
				}
			}
			is_marriage_adult = yes
		}
		allow = {
			prisoner = no
			is_incapable = no
			is_betrothed = no
			is_married = no
		}
		third_party_filter = home_court
		ai_third_party_filter = home_court
		show_third_party_potential = yes
		third_party_potential = {
			FROMFROM  = {
				is_marriage_adult = yes
				custom_tooltip = { 
					text = find_suitable_spouse_opposite
					hidden_tooltip = { is_opposite_sex = ROOT }
				}
				prisoner = no
				is_incapable = no
				is_betrothed = no
				is_married = no
				is_consort = no
			}
		}
		third_party_score = {
			factor = 1
			additive_modifier = {
				value = 5
				FROMFROM = { is_ruler = yes }
			}
			modifier = {
				factor = 4
				FROMFROM = { age < 20 }
			}
			modifier = {
				factor = 2
				FROMFROM = { age < 30 }
			}
			modifier = {
				factor = 3
				FROMFROM = { fertility >= 0.50 }
			}
			modifier = {
				factor = 2
				FROMFROM = { martial >= 12 }
			}
			modifier = {
				factor = 4
				FROMFROM = { stewardship >= 12 }
			}
			modifier = {
				factor = 0.01
				FROMFROM = { is_female = yes age >= 35 }
			}
		}
		effect = {
			if = {
				limit = { is_female = yes }
				FROMFROM = {
					add_spouse_matrilineal = ROOT
				}
			}
			else = {
				FROMFROM = {
					add_spouse = ROOT
				}
			}
		}
		ai_will_do = { factor = 1 }
		ai_acceptance = { factor = 10 }
	}

	edit_missionary_guard = {
		only_playable = yes
		ai = no #for now
		filter = vassals
		diplomacy_icon = GFX_edit_missionary_guard
		ai_target_filter = vassals
		ai_check_interval = 36
		from_potential = { government = missionary_government }
		potential = { government = missionary_guard_government }
		allow = { FROM = { wealth = 50 } }
		effect = { FROM = { character_event = { id = missionaryguard.2 } } }
		ai_will_do = { factor = 0.2 } # Scale factor by wealth later (and add ai factor stuff to the event chain)
	}

	sco_play_as = {
		ai = no
		potential = {
			has_game_rule = { name = sco_rule_playas value = on }
			is_theocracy = no
			mercenary = no
			OR = {
				is_republic = no
				is_patrician = yes
			}
			OR = {
				is_patrician = yes
				higher_tier_than = BARON
			}
		}
		effect = { FROM = { set_player_character = ROOT } }
		ai_will_do = { factor = 0 }
	}

}