﻿begin_sicily_war_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_realm.dds"
	}
	decision_group_type = major

	ai_check_interval_by_tier = {
		barony = 0
		county = 12
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}

	desc = begin_sicily_war_decision_desc
	selection_tooltip = begin_sicily_war_decision_tooltip

	is_shown = {
		exists = global_var:is_hauteville_dynasty
		is_hauteville_sicily_ruler = yes
		should_be_invading_sicily = yes
		any_neighboring_and_across_water_top_liege_realm_owner = {
			is_appropriate_sicily_war_target = yes
		}
	}
	
	is_valid_showing_failures_only = {
		is_ai = yes
		gold > 0
		is_at_war = no
		is_available_adult = yes
	}
	
	effect = {
		every_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				is_appropriate_sicily_war_target = yes
				any_realm_county = {
					is_better_sicily_county_target = yes
					duchy = {
						any_de_jure_county = {
							OR = {
								holder = { target_is_liege_or_above = root }
								holder = root
							}
						}
					}
				}
			}
			alternative_limit = {
				is_appropriate_sicily_war_target = yes
				any_realm_county = { is_better_sicily_county_target = yes }
			}
			alternative_limit = {
				is_appropriate_sicily_war_target = yes
			}
			add_to_list = war_targets
		}
		ordered_in_list = {
			list = war_targets
			order_by = military_strength_inverted_score
			save_scope_as = target
			random_realm_county = {
				limit = {
					is_better_sicily_county_target = yes
					duchy = {
						any_de_jure_county = {
							OR = {
								holder = { target_is_liege_or_above = root }
								holder = root
							}
						}
					}
				}
				alternative_limit = {
					is_sicily_county_target = yes
					duchy = {
						any_de_jure_county = {
							OR = {
								holder = { target_is_liege_or_above = root }
								holder = root
							}
						}
					}
				}
				alternative_limit = { is_better_sicily_county_target = yes }
				alternative_limit = { is_sicily_county_target = yes }
				save_scope_as = target_county
			}
		}
		start_war = {
			casus_belli = sicily_conquest_cb
			target = scope:target
			target_title = scope:target_county
		}
		add_prestige = 500
		add_gold = 200
	}
	
	ai_potential = {
		is_landed = yes
		is_available_ai_adult = yes
	}
	
	ai_will_do = {
		base = 100
		modifier = {
			factor = 0.5
			is_married = yes
			fertility > 0.10
			primary_spouse = {
				fertility > 0.10
				NOT = { has_trait = pregnant }
			}
			NOR = {
				has_trait = pregnant
				any_child = {
					is_male = yes
					NOT = { has_trait = bastard }
					count > 1
				}
			}
		}
		modifier = {
			factor = 0
			OR = {
				days_of_continuous_peace < 90
				current_military_strength < eighty_percent_of_max_military_strength
			}
		}
		modifier = {
			factor = 0
			gold < 0
		}
		modifier = {
			factor = 0
			has_ongoing_grand_activity_trigger = yes
		}
		modifier = {
			factor = 0
			can_execute_decision = begin_conquer_sicily_decision
		}
	}	
}

begin_sicily_adventurer_war_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_realm.dds"
	}
	decision_group_type = major
	
	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	
	desc = begin_sicily_adventurer_war_decision_desc
	selection_tooltip = begin_sicily_adventurer_war_decision_tooltip
	
	is_shown = {
		ep3_adventurer_invasion_allowed_for_character_trigger = yes
		exists = global_var:is_hauteville_dynasty
		dynasty ?= global_var:is_hauteville_dynasty
		should_be_invading_sicily = yes
		any_county_in_region = {
			region = world_europe_south_italy
			is_contested = no
			duchy = { is_contested = no }
			holder.top_liege = {
				is_ai = yes
				has_no_war_preventing_relation = yes
				save_temporary_scope_as = target
				root.strength_with_relevant_allies_vs_target_value > scope:target.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
				NOR = {
					dynasty = root.dynasty
					any_ally = {
						is_ruler = yes
						dynasty = root.dynasty
					}
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		is_ai = yes
		is_at_war = no
		is_available_adult = yes
		has_raised_armies = no
		prestige > 250
		gold > -200
		trigger_if = {
			limit = { culture = { has_cultural_parameter = cb_lower_prestige_level_requirement } }
			prestige_level >= 1
		}
		trigger_else = {
			prestige_level >= 2
		}
	}
	
	effect = {
		every_county_in_region = {
			region = world_europe_south_italy
			limit = {
				is_contested = no
				duchy = { is_contested = no }
				holder.top_liege = {
					is_ai = yes
					has_no_war_preventing_relation = yes
					save_temporary_scope_as = target
					root.strength_with_relevant_allies_vs_target_value > scope:target.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
					NOR = {
						dynasty = root.dynasty
						any_ally = {
							is_ruler = yes
							dynasty = root.dynasty
						}
					}
				}
			}
			if = {
				limit = {
					holder.top_liege = {
						NOT = { is_in_list = war_targets }
					}
				}
				holder.top_liege = { add_to_list = war_targets }
			}
		}
		ordered_in_list = {
			list = war_targets
			order_by = military_strength_inverted_score
			save_scope_as = target
			random_realm_county = {
				limit = {
					kingdom = title:k_sicily
					is_contested = no
					duchy = { is_contested = no }
					is_coastal_county = yes
				}
				alternative_limit = {
					title_province = { geographical_region = world_europe_south_italy }
					is_contested = no
					duchy = { is_contested = no }
					is_coastal_county = yes
				}
				alternative_limit = {
					kingdom = title:k_sicily
					is_contested = no
					duchy = { is_contested = no }
				}
				alternative_limit = {
					title_province = { geographical_region = world_europe_south_italy }
					is_contested = no
					duchy = { is_contested = no }
				}
				save_scope_as = target_county
			}
		}
		
		# move the domicile somewhere beyond the target
		random_county_in_region = {
			region = world_europe_south
			limit = {
				title_province = { geographical_region = world_europe_south_italy }
				NOR = {
					holder.top_liege = scope:target
					kingdom = title:k_sardinia
					this = title:c_malta
				}
			}
			alternative_limit = {
				NOR = {
					holder.top_liege = scope:target
					kingdom = title:k_sardinia
					this = title:c_malta
				}
			}
			title_province = { save_temporary_scope_as = move_location }
		}
		if = {
			limit = { exists = scope:move_location }
			domicile = { move_domicile = scope:move_location }
		}
		
		add_gold = 400
		if = {
			limit = {
				prestige > 1000
				scope:target = { highest_held_title_tier >= tier_duchy }
				scope:target_county.duchy = {
					any_de_jure_county = {
						holder.top_liege = scope:target
						count > 1
					}
				}
				trigger_if = {
					limit = { culture = { has_cultural_parameter = cb_lower_prestige_level_requirement } }
					prestige_level >= 2
				}
				trigger_else = {
					prestige_level >= 3
				}
			}
			add_prestige = 1000
			start_war = {
				casus_belli = ep3_laamp_adventurer_duchy_invasion
				target = scope:target
				target_title = scope:target_county.duchy
			}
		}
		else = {
			add_prestige = 250
			start_war = {
				casus_belli = ep3_laamp_adventurer_county_invasion
				target = scope:target
				target_title = scope:target_county
			}
		}
	}
	
	ai_potential = {
		has_government = landless_adventurer_government
		is_available_ai_adult = yes
	}
	
	ai_will_do = {
		base = 100
		modifier = {
			factor = 0
			OR = {
				days_of_continuous_peace < 90
				current_military_strength < eighty_percent_of_max_military_strength
			}
		}
	}
}

form_sicily_together_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_realm.dds"
	}
	decision_group_type = major
	
	ai_check_interval_by_tier = {
		barony = 0
		county = 12
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	
	desc = form_sicily_together_decision_desc
	selection_tooltip = form_sicily_together_decision_tooltip
	
	is_shown = {
		exists = global_var:is_hauteville_dynasty
		exists = capital_county
		capital_county.kingdom = title:k_sicily
		NOT = { exists = title:k_sicily.holder }
		highest_held_title_tier > tier_county
		government_has_flag = government_is_feudal
		dynasty = {
			this = global_var:is_hauteville_dynasty
			any_dynasty_member = {
				is_landed = yes
				highest_held_title_tier < tier_kingdom
				NOT = { this = root }
				exists = capital_county
				capital_county.kingdom = title:k_sicily
				sub_realm_size < root.sub_realm_size
			}
		}
	}
	
	is_valid_showing_failures_only = {
		is_at_war = no
		is_available_adult = yes
		top_liege = this
		OR = {
			highest_held_title_tier > tier_duchy
			custom_description = {
				text = have_two_duchy_titles
				any_held_title = { title_tier = duchy count > 1 }
			}
		}
		custom_description = {
			text = have_enough_hauteville_counties
			title:k_italy = {
				any_de_jure_county = {
					percent > 0.50
					holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
							AND = {
								dynasty = root.dynasty
								is_ai = yes
								is_at_war = no
								OR = {
									top_liege = this
									NOT = { any_liege_or_above = { is_ai = no } }
								}
								sub_realm_size < root.sub_realm_size
								capital_county.kingdom = title:k_sicily
								highest_held_title_tier < tier_kingdom
								NOT = { has_relation_rival = root }
							}
						}
					}
				}
			}
		}
	}
	
	cost = {
		prestige = {
			value = 500
		}
	}
	
	effect = {
		# first gain the kingdom title
		histmod_simple_gain_title_effect = { TITLE = k_sicily CHARACTER = root }
		
		# then vassalize the dynasty members
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change
		}
		dynasty = {
			every_dynasty_member = {
				limit = {
					is_ai = yes
					is_at_war = no
					OR = {
						top_liege = this
						NOT = { any_liege_or_above = { is_ai = no } }
					}
					sub_realm_size < root.sub_realm_size
					capital_county.kingdom = title:k_sicily
					highest_held_title_tier < tier_kingdom
					NOT = { has_relation_rival = root }
				}
				change_liege = {
					liege = root
					change = scope:change
				}
				save_temporary_scope_as = family
				if = {
					limit = {
						NOT = { is_allied_to = root }
					}
					hidden_effect = {
						add_opinion = { target = root modifier = event_negotiated_alliance_opinion }
						reverse_add_opinion = { target = root modifier = event_negotiated_alliance_opinion }
					}
					create_alliance = {
						target = scope:family
						allied_through_owner = root
						allied_through_target = scope:family
					}
				}
				progress_towards_friend_effect = {
					CHARACTER = root
					OPINION = 50
					REASON = friend_after_subjugation
				}
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
	
	ai_potential = {
		is_landed = yes
		is_available_ai_adult = yes
		highest_held_title_tier > tier_county
	}
	
	ai_will_do = {
		base = 100
	}
}

begin_conquer_sicily_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_realm.dds"
	}
	decision_group_type = major
	
	ai_check_interval_by_tier = {
		barony = 0
		county = 12
		duchy = 12
		kingdom = 0
		empire = 0
		hegemony = 0
	}
	
	desc = begin_conquer_sicily_decision_desc
	selection_tooltip = begin_conquer_sicily_decision_tooltip
	
	is_shown = {
		is_ai = yes
		should_be_invading_sicily = yes
		exists = global_var:is_hauteville_dynasty
		highest_held_title_tier < tier_kingdom
		NOR = {
			has_game_rule = no_conquerors_frequency
			exists = title:k_sicily.holder
			# can only ever be done once
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:begin_conquer_sicily_decision
			}
		}
		is_hauteville_sicily_ruler = yes
		can_be_histmod_conqueror = yes
		# no other Sicilian rulers who are players, conquerors, or conqueror candidates who have larger realms
		NOT = {
			any_independent_ruler = {
				NOT = { this = root }
				capital_province = { geographical_region = world_europe_south_italy }
				OR = {
					has_trait = conqueror
					has_character_modifier = ai_weak_conqueror_modifier
					is_ai = no
					AND = {
						is_hauteville_sicily_ruler = yes
						can_be_histmod_conqueror = yes
						sub_realm_size > root.sub_realm_size
					}
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		is_at_war = no
		is_available_ai_adult = yes
	}
	
	effect = {
		add_gold = 500
		add_prestige = 800
		add_character_flag = histmod_seeking_sicily
		create_story = histmod_conqueror_story_cycle
		every_vassal = {
			limit = { has_vassal_stance = glory_hound }
			add_opinion = {
				modifier = approving_opinion
				target = root
				opinion = 20
			}
		}
		every_vassal = {
			limit = { has_vassal_stance = parochial }
			add_opinion = {
				modifier = disapproving_opinion
				opinion = -20
				target = root
			}
		}
		add_to_global_variable_list = {
			name = unavailable_unique_decisions
			target = flag:begin_conquer_sicily_decision
		}
	}
	
	ai_potential = {
		is_landed = yes
	}

	ai_will_do = {
		base = 100
	}
}

