﻿fp3_install_loyalist_cb = {
	group = struggle
	icon = fp3_install_loyalist_war
	interface_priority = 8
	# Consider adding white_peace_possible = no

	ai_score_mult = {
		value = 1
		scope:attacker = {
			# Major supporters are more likely to do this.
			## Zealots.
			if = {
				limit = { has_trait = zealous }
				add = 1
			}
			## Loyals.
			if = {
				limit = { has_trait = loyal }
				add = 2
			}
			## Direct powerful vassals.
			if = {
				limit = { is_powerful_vassal_of = title:d_sunni.holder }
				add = 2
			}
			# Some characters have no interest in helping out the caliph.
			## Disloyals have zero.
			if = {
				limit = { has_trait = disloyal }
				multiply = 0
			}
			## If we like the target, we have zero.
			if = {
				limit = {
					opinion = {
						target = scope:defender
						value >= 1
					}
				}
				multiply = 0
			}
			## Cynics have less.
			if = {
				limit = { has_trait = cynical }
				add = -1
			}
			## Independents have less.
			if = {
				limit = { top_liege = this }
				add = -1
			}
		}
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	allowed_for_character = {
		has_trait = fp3_struggle_supporter
		exists = title:d_sunni.holder
		OR = {
			is_ai = no
			opinion = {
				target = title:d_sunni.holder
				value >= 1
			}
		}
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	allowed_against_character = {
		is_landed = yes
		this = top_liege
		NOT = { has_trait = fp3_struggle_supporter }
	}

	allowed_against_character_display_regardless = {
		scope:defender = {
			NOT = { is_allied_to = scope:attacker }
		}
		custom_description = {
			text = generic_is_not_part_of_same_struggle_desc
			any_character_struggle = { is_struggle_type = persian_struggle }
		}
		custom_tooltip = {
			text = fp3_install_loyalist_cb.tt.loyalist_stooge
			any_vassal = {
				fp3_is_valid_loyal_puppet = yes
			}
		}
	}

	should_invalidate = {
		scope:defender = {
			any_vassal = {
				count = 0
				fp3_is_valid_loyal_puppet = yes
			}
		}
	}

	cost = {
		piety = {
			add = {
				value = major_piety_value
				desc = CB_BASE_COST
			}
			if = { # 50% discount if the defender has refused to submit to the Caliph
				limit = {
					scope:defender = { has_variable = last_explicit_defied_caliph }
				}
				multiply = {
					value = 0.5
					desc = cb_rejected_caliph_discount
				}
			}
			multiply = common_cb_piety_cost_multiplier
		}
	}

	defender_ticking_warscore = 0.1
	attacker_wargoal_percentage = 0.75
	full_occupation_by_attacker_gives_victory = yes

	on_declaration = {
		scope:defender = { # First, we find a puppet
			ordered_vassal_or_below = { # We start with important (landed) people
				limit = { fp3_is_valid_loyal_puppet = yes }
				order_by = {
					value = 0
					add = monthly_character_income
					if = {
						limit = { house = scope:attacker.house }
						add = 10
					}
				}
				save_scope_as = loyalist_puppet
			}
		}
		war ?= {
			set_variable = {
				name = loyalist_puppet
				value = scope:loyalist_puppet
			}
		}
		hidden_effect = {
			if = {
				limit = {
					scope:loyalist_puppet != scope:attacker
				}
				scope:loyalist_puppet = {
					trigger_event = {
						id = fp3_struggle.8000
						days = 1
					}
				}
			}
			
			scope:defender = {
				trigger_event = {
					id = fp3_struggle.8001
					days = 1
				}
			}
		}
	}

	on_victory = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			hidden_effect = {
				if = { # Catalyst
					limit = {
						any_character_struggle = { phase_has_catalyst = catalyst_install_loyalist_cb }
					}
					every_character_struggle = {
						activate_struggle_catalyst = {
							catalyst = catalyst_install_loyalist_cb
							character = scope:attacker
						}
						log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_install_loyalist_cb }
					}
				}
			}
		}

		scope:defender = { # First, we find a puppet
			ordered_vassal_or_below = { # We start with important (landed) people
				limit = { fp3_is_valid_loyal_puppet = yes }
				order_by = {
					value = 0
					add = monthly_character_income
					if = {
						limit = { root.war.var:loyalist_puppet ?= this }
						add = 5
					}
					if = {
						limit = { scope:attacker.house ?= house }
						add = 10
					}
				}
				save_scope_as = loyalist_puppet
			}
		}

		if = { # Autoformatter shenanigans fix
			limit = { exists = scope:loyalist_puppet }
			create_title_and_vassal_change = {
				type = conquest_claim
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:defender = {
				every_held_title = {
					limit = {
						tier = scope:defender.primary_title.tier
					}
					add_to_list = title_to_steal
				}
				every_in_list = {
					list = title_to_steal
					change_title_holder = {
						holder = scope:loyalist_puppet
						change = scope:change
					}
				}
			}
			resolve_title_and_vassal_change = scope:change

			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = change2
			}
			# then vassalize the defender
			scope:defender = {
				hidden_effect = { # Avoid clutter. message below should make it clear already
					every_vassal = {
						limit = { primary_title.tier >= tier_county}
						change_liege = {
							liege = scope:loyalist_puppet
							change = scope:change2
						}
					}

					change_liege = {
						liege = scope:loyalist_puppet
						change = scope:change2
					}
				}
			}
			resolve_title_and_vassal_change = scope:change2


			scope:loyalist_puppet = { # We set the new ruler to be a puppet
				add_character_flag = fp3_is_a_loyalist_puppet

				if = { # Creates further interaction points for the struggle
					limit = {
						OR = {
							title:e_arabia.holder ?= { this = scope:attacker }
							title:d_sunni.holder ?= { this = scope:attacker }
						}
						scope:attacker = {
							can_add_hook = {
								type = caliphal_submission_hook
								target = scope:loyalist_puppet
							}
						}
					}
					scope:attacker = {
						add_hook = {
							target = scope:loyalist_puppet
							type = caliphal_submission_hook
						}
					}
				}
				else = {
					# Basic piety rewards for supporting the caliph/furthering your influence.
					scope:attacker = { add_piety = medium_piety_gain }
				}

				hidden_effect = {
					reverse_add_opinion = {
						modifier = usurped_title
						target = scope:defender
					}
					progress_towards_rival_effect = {
						REASON = rival_lands_stolen
						CHARACTER = scope:defender
						OPINION = default_rival_opinion
					}
				}
				

				if = {
					limit = {
						scope:attacker != scope:loyalist_puppet
					}
					create_alliance = {
						target = scope:attacker
						allied_through_owner = scope:loyalist_puppet
						allied_through_target = scope:attacker
					}
					add_opinion = {
						modifier = helped_me_against_tyrant_opinion
						opinion = 65
						years = 30
						target = scope:attacker
					}
					trigger_event = {
						id = fp3_struggle.8002
						days = 1
					}					
				}

				if = { # The puppet will make moves to secure the throne
					limit = {
						opinion = {
							target = scope:defender
							value < 0
						}
					}
					imprison = {
						target = scope:defender
						type = house_arrest
					}
				}
				
			}
		}
		else = {
			scope:attacker = {
				create_alliance = {
					target = scope:defender
					allied_through_owner = scope:attacker
					allied_through_target = scope:defender
				}
			}
		}

		scope:attacker = { add_prestige = major_prestige_gain }
		scope:defender = { add_prestige = major_prestige_loss }
		add_truce_attacker_victory_effect = yes

		# FP1 memory
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace = {
		add_truce_attacker_defeat_effect = yes
		scope:attacker = { add_prestige = major_prestige_loss }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}
	
	on_defeat = {
		add_truce_attacker_defeat_effect = yes
		scope:attacker = {
			add_prestige = major_prestige_loss
			add_character_modifier = { # Penatly for failing such a risky move
				modifier = fp3_not_puppeteering_kingmaker_modifier
				years = 10
			}
		}
		scope:defender = {
			mandala_peacemaker_perk_serenity_effect = yes
			add_prestige = major_prestige_gain
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_victory_desc = { desc = fp3_install_loyalist_cb_victory_desc }
	on_defeat_desc = { desc = fp3_install_loyalist_cb_defeat_desc }
	on_white_peace_desc = { desc = fp3_install_loyalist_cb_defeat_desc }
	on_invalidated_desc = msg_invalidate_war_title

	war_name = "INSTALL_LOYALIST_PUPPET_WAR_NAME"
	war_name_base = "STRUGGLE_WAR_NAME_BASE"
	cb_name = "INSTALL_LOYALIST_PUPPET_CB_NAME"

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit
	check_defender_inheritance_validity = yes
	attacker_allies_inherit = no
	defender_allies_inherit = no
	transfer_behavior = transfer
	truce_days = 180
}

fp3_turkic_invasion_cb = {
	group = event
	icon = request_mercenary
	war_name = "fp3_turkic_invasion_cb_war_name"
	war_name_base = "INVASION_WAR_NAME_BASE"
	cb_name = "fp3_turkic_invasion_cb_name"

	interface_priority = 8
	attacker_wargoal_percentage = 0.8
	
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		NOT = {
			trigger_if = {
				scope:attacker = {
						is_ai = no
				}
			}
		}
	}

	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	allowed_for_character = { always = no }
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						target_is_same_character_or_above = scope:defender
					}
				}
			}
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes
		
		scope:attacker = {
			add_to_list = invasion_beneficiary_candidates
			every_courtier = {
				limit = {
					is_physically_able = yes
					is_adult = yes
					culture = { has_cultural_pillar = heritage_turkic }
				}
				add_to_list = invasion_beneficiary_candidates
			}
			ordered_in_list = {
				list = invasion_beneficiary_candidates
				order_by = {
					value = 1
					if = {
						limit = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:attacker } }
						add = 20
					}
					if = {
						limit = { this = scope:attacker }
						multiply = 0.5
					}
					if = {
						limit = { is_heir_of = scope:attacker }
						add = -10
					}
					if = {
						limit = { is_spouse_of = scope:attacker }
						add = -10
					}
					if = {
						limit = { has_no_particular_noble_roots_trigger = yes }
						add = 10
					}
				}
				save_scope_as = invasion_beneficiary
			}
		}

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = target_titles
			custom = fp3_turkic_invasion_beneficiary
			custom_tooltip = fp3_turkic_invasion_title
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					# Current holder must be a (sub)vassal of the defender (no poaching titles from 3rd parties!)
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
				change_title_holder = {
					holder = scope:invasion_beneficiary
					change = scope:change
					take_baronies = yes
				}
			}
		}
		show_as_tooltip = {
			every_in_list = {
				list = target_titles
				every_in_de_jure_hierarchy = {
					limit = { holder.top_liege = scope:defender }
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		scope:invasion_beneficiary = {
			if = {
				limit = { top_liege != this }
				create_title_and_vassal_change = {
					type = conquest
					save_scope_as = change_independent
					add_claim_on_loss = yes
				}
				becomes_independent = {
					change = scope:change_independent
				}
				resolve_title_and_vassal_change = scope:change_independent
			}
		}
		
		scope:defender = {
			add_character_modifier = {
				modifier = fp3_incursion_instability
				years = 20
			}
		}
		
		hidden_effect = {
			scope:invasion_beneficiary = {
				add_gold = 200
				every_sub_realm_county = {
					change_county_control = 100
				}
				every_sub_realm_barony = {
					title_province = {
						refill_garrison = yes
						refill_levy = yes
					}
				}
				if = {
					limit = {
						exists = capital_province
						has_mpo_dlc_trigger = yes
					}
					# Scopes needed for fp3_request_invasion_troop_value_bonus_troops to work
					save_scope_as = secondary_recipient
					scope:defender = {
						save_scope_as = recipient
					}
					spawn_army = {
						men_at_arms = {
							type = horse_archers
							stacks = 5
						}
						men_at_arms = {
							type = steppe_raiders
							stacks = fp3_request_invasion_troop_value_bonus_troops
						}
						location = capital_province
						uses_supply = yes
						inheritable = yes
						name = turkic_tribal_event_troops
					}
				}
				else_if = {
					limit = {
						exists = capital_province
					}
					# Scopes needed for fp3_request_invasion_troop_value_bonus_troops to work
					save_scope_as = secondary_recipient
					scope:defender = {
						save_scope_as = recipient
					}
					spawn_army = {
						men_at_arms = {
							type = horse_archers
							stacks = 5
						}
						men_at_arms = {
							type = light_horsemen
							stacks = fp3_request_invasion_troop_value_bonus_troops
						}
						location = capital_province
						uses_supply = yes
						inheritable = yes
						name = turkic_tribal_event_troops
					}
				}
				if = {
					limit = {
						faith = { has_doctrine_parameter = unreformed }
						NOT = { has_trait = zealous }
						capital_county.faith ?= {
							NOT = {
								has_doctrine_parameter = unreformed
							}
						}
					}
					random_list = {
						75 = {
							set_character_faith_with_conversion = capital_county.faith
						}
						25 = {
							set_character_faith_with_conversion = faith:alevi
						}
					}
				}
			}
		}

		# Prestige Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_prestige_experience = minor_prestige_value
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 0
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_victory_effect = yes
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 0
			LOSER_ALLY_FAME_SCALE = 1
		}

		scope:defender = {
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
		}

		add_truce_white_peace_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 2
			}
			add_prestige = {
				value = medium_prestige_value
				multiply = -1.0
			}
		}

		# Prestige for Defender
		scope:defender = {
			mandala_peacemaker_perk_serenity_effect = yes
			# Prestige for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 0
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_attacker_defeat_effect = yes
		scope:attacker = { save_temporary_scope_as = loser }
		on_lost_aggression_war_discontent_loss = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_victory_desc = {
		desc = fp3_turkic_invasion_cb_victory_desc
	}
	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_white_peace_desc_defender
			}
			desc = county_conquest_cb_white_peace_desc
		}
	}
	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = county_conquest_cb_defeat_desc_attacker
			}
			desc = county_conquest_cb_defeat_desc
		}
	}
	on_invalidated_desc = msg_invalidate_war_title

	transfer_behavior = transfer

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit
	attacker_allies_inherit = no
	defender_allies_inherit = no

	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

fp3_seljuk_invasion_cb = {
	group = event
	icon = invasion
	interface_priority = 8
	war_name = "fp3_seljuk_invasion_war_name"
	war_name_base = "INVASION_WAR_NAME_BASE"
	cb_name = "fp3_seljuk_invasion_cb_name"

	allowed_for_character = { is_ai = yes }
	allowed_against_character = { is_landed = yes }
	valid_to_start = { always = no }
	cost = {}

	#to be able to target not created titles
	target_de_jure_regions_above = yes

	attacker_wargoal_percentage = 0.6
	full_occupation_by_attacker_gives_victory = yes
	landless_attacker_needs_armies = yes
	attacker_capital_gives_war_score = no
	full_occupation_by_defender_gives_victory = no
	allow_hostages = no

	on_declaration = {
		hidden_effect = {
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target_title
			}
		}
	}

	on_victory = {
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes
		
		ordered_in_list = {
			list = target_titles
			order_by = tier
			save_scope_as = target_title
		}
		scope:attacker = {
			show_pow_release_message_effect = yes
			add_prestige = massive_prestige_gain
			if = {
				limit = {
					scope:target_title.tier = tier_county
				}
				change_variable = {
	  				name = seljuk_invasion_nomad_strength_score
	  				add = fp3_seljuk_invasion_troop_gain_small
				}
			}
			else_if = {
				limit = {
					scope:target_title.tier = tier_duchy
				}
				change_variable = {
	  				name = seljuk_invasion_nomad_strength_score
	  				add = fp3_seljuk_invasion_troop_gain_medium
				}
			}
			else_if = {
				limit = {
					scope:target_title.tier = tier_kingdom
				}
				change_variable = {
	  				name = seljuk_invasion_nomad_strength_score
	  				add = fp3_seljuk_invasion_troop_gain_major
				}
			}
			else_if = {
				limit = {
					scope:target_title.tier = tier_empire
				}
				change_variable = {
	  				name = seljuk_invasion_nomad_strength_score
	  				add = fp3_seljuk_invasion_troop_gain_massive
				}
			}

		}
		scope:defender = { add_prestige = massive_prestige_loss }
		add_truce_attacker_victory_effect = yes

		scope:attacker = {
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
			}
			every_in_list = {
				list = target_titles
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
				}
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_county
						holder ?= {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
						target_is_de_jure_liege_or_above = scope:target_title
					}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
			resolve_title_and_vassal_change = scope:change
			if = {
				limit = {
					NOT = { has_character_flag = settled_seljuk }
				}
				add_character_flag = settled_seljuk
				change_government = clan_government
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_defeat = {
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes
		
		add_truce_attacker_defeat_effect = yes
		scope:attacker = { add_prestige = major_prestige_loss }
		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			add_prestige = major_prestige_gain
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_white_peace = {
		add_truce_attacker_defeat_effect = yes
		scope:attacker = { add_prestige = major_prestige_loss }

		scope:attacker = {
			if = {
				limit = { NOT = { exists = scope:lead_seljuk } }
				scope:attacker = { save_scope_as = lead_seljuk }
			}
			if = {
				limit = { NOT = { exists = scope:fp3_seljuk_settle_target } }
				every_in_list = {
					list = target_titles
					save_scope_as = fp3_seljuk_settle_target
				}
			}
			scope:defender = { save_scope_as = seljuk_victim }
			fp3_story_cycle_seljuks_invasion_events_grant_seljuks_land_effect = yes
			hidden_effect = {
				if = { # Spice: If the character was hurt during the war, they will (secretly) become resentful
					limit = {
						OR = {
							health < fine_health
							has_trait_rank = {
								trait = wounded
								rank > -1
							}
						}
					}
					add_trait = disloyal
				}
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = fp3_seljuk_invasion_cb_victory_desc_attacker
			}
			desc = fp3_seljuk_invasion_cb_victory_desc
		}
	}
	on_defeat_desc = { 
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = fp3_seljuk_invasion_cb_defeat_desc_attacker
			}
			desc = fp3_seljuk_invasion_cb_defeat_desc
		}
	}
	on_white_peace_desc = { 
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = fp3_seljuk_invasion_cb_white_peace_desc_attacker
			}
			desc = fp3_seljuk_invasion_cb_white_peace_desc
		}
	}
	on_invalidated_desc = msg_invalidate_war_title

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	check_defender_inheritance_validity = yes
	attacker_allies_inherit = yes
	defender_allies_inherit = no
	transfer_behavior = transfer
	should_invalidate = {}
	truce_days = 3650

	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

fp3_free_house_member_cb = {
	group = de_jure
	icon = fp3_free_house_member
	interface_priority = 16

	war_name = "fp3_free_house_member_war_name"
	war_name_base = "DEJURE_WAR_NAME_BASE"
	cb_name = "fp3_free_house_member_cb_name"

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit
	transfer_behavior = transfer
	attacker_allies_inherit = no
	defender_allies_inherit = no
	should_show_war_goal_subview = no
	max_ai_diplo_distance_to_title = 500
	attacker_ticking_warscore = 0
	defender_ticking_warscore = 0.1
	attacker_wargoal_percentage = 0.75
	defender_wargoal_percentage = 1
	max_attacker_score_from_battles = 150
	max_defender_score_from_battles = 150
	max_attacker_score_from_occupation = 150
	max_defender_score_from_occupation = 150
	full_occupation_by_defender_gives_victory = yes
	full_occupation_by_attacker_gives_victory = yes
	attacker_capital_gives_war_score = yes
	defender_capital_gives_war_score = yes
	imprisonment_by_attacker_give_war_score = yes
	imprisonment_by_defender_give_war_score = yes
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	allowed_for_character = {
		is_house_head = yes
		OR = {
			government_has_flag = government_is_clan
			has_perk = thicker_than_water_perk
		}
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}
	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				NOR = {
					has_perk = thicker_than_water_perk
					house = { has_house_unity_parameter = unity_has_liberate_house_members_cb }
				}
			}
			house = { has_house_unity_parameter = unity_has_liberate_house_members_cb }
		}
	}
	allowed_against_character = {
		any_prisoner = {
			house ?= { any_house_unity_member = { this = scope:attacker } }
		}
	}

	on_victory_desc	= { 
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_free_house_member_cb_victory_desc_defender
			}
			desc = fp3_free_house_member_cb_victory_desc
		}
	}
	on_defeat_desc = { 
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_free_house_member_cb_defeat_desc_defender
			}
			desc = fp3_free_house_member_cb_defeat_desc
		} 
	}
	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_free_house_member_cb_defeat_desc_defender
			}
			desc = fp3_free_house_member_cb_defeat_desc
		}
	}

	on_invalidated_desc = msg_free_house_member_war_invalidated_message

	on_victory = {
		scope:attacker = {
			if = {
				limit = {
					government_has_flag = government_is_clan
				}
				house = {
					add_unity_value = {
						value = rescue_house_members_cb_house_unity_gain
						character = scope:attacker
						desc = clan_unity_generic_unified.notarget # Used to use clan_unity_released_from_prison.desc
					}
				}
			}
		}
		scope:defender = {
			every_prisoner = {
				limit = { house ?= scope:attacker.house }
				release_from_prison = yes
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_defeat = {
		scope:attacker = { add_prestige = major_prestige_loss }
		scope:defender = { mandala_peacemaker_perk_serenity_effect = yes }
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	cost = {
		prestige = {
			value = 0
			add = {
				value = minor_prestige_value
				desc = CB_BASE_COST
			}

			multiply = common_cb_prestige_cost_multiplier
		}
	}

	should_invalidate = {
		NOT = {
			scope:defender = {
				any_prisoner = {
					house ?= scope:attacker.house
				}
			}
		}
	}
}

fp3_unify_house_cb = {
	group = claim
	icon = fp3_house_unification_cb
	interface_priority = 2

	war_name = "fp3_unify_house_war_name"
	war_name_base = "STRUGGLE_WAR_NAME_BASE"
	cb_name = "fp3_unify_house_cb_name"

	attacker_wargoal_percentage = 0.8
	combine_into_one = no
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150
	
	ai_only_against_neighbors = yes

	allowed_for_character = {
		is_house_head = yes
		OR = {
			government_has_flag = government_is_clan
			has_perk = thicker_than_water_perk
		}
		is_independent_ruler = yes
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}
	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				NOR = {
					has_perk = thicker_than_water_perk
					house = { has_house_unity_parameter = unity_has_unify_house_cb }
				}
			}
			house = { has_house_unity_parameter = unity_has_unify_house_cb }
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		any_vassal_or_below = {
			house ?= scope:attacker.house
			primary_title.tier >= tier_county
		}
	}
	cost = {
		prestige = {
			value = 0
			add = {
				scope:defender = {
						every_vassal_or_below = {
							limit = {
								house ?= scope:attacker.house
								primary_title.tier >= tier_county
							}
							add = {
								value = medium_prestige_value
							}
	
					}
				}
				desc = CB_BASE_COST
			}
			multiply = common_cb_prestige_cost_multiplier
		}
	}

	on_invalidated_desc = msg_unify_house_war_invalidated_message
	on_invalidated = {}
	should_invalidate = {
		NOT = { scope:defender = { any_vassal_or_below = { house ?= scope:attacker.house } } }
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = fp3_unify_house_cb_victory_desc_attacker
			}
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_unify_house_cb_victory_desc_defender
			}
			desc = fp3_unify_house_cb_victory_desc
		}
	}
	on_victory = {
		create_title_and_vassal_change = { # Not at all cursed title transfer script
			type = conquest_claim
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:defender = {
			every_vassal_or_below = {
				limit = {
					house = scope:attacker.house
					primary_title.tier >= tier_county
				}
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
				house = {
					add_unity_value = {
						value = medium_unity_gain
						character = scope:attacker
						desc = clan_unity_generic_unified.notarget
					}
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		add_truce_attacker_victory_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_unify_house_cb_defeat_desc_defender
			}
			desc = fp3_unify_house_cb_defeat_desc
		}
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = fp3_unify_house_cb_defeat_desc_defender
			}
			desc = fp3_unify_house_cb_defeat_desc
		}
	}
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:defender = {
			mandala_peacemaker_perk_serenity_effect = yes
			add_prestige_war_defender_effect = { PRESTIGE_VALUE = medium_prestige_value }
			every_vassal_or_below = {
				limit = { house = scope:attacker.house }
				house = {
					add_unity_value = {
						value = medium_unity_loss
						character = scope:defender
						desc = clan_unity_generic_disunified.notarget
					}
				}
			}
		}
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_attacker_defeat_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_primary_attacker_death = invalidate
	on_primary_defender_death = invalidate
	attacker_allies_inherit = no
	defender_allies_inherit = no

	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

fp3_zanj_rebellion_war = {
	icon = peasant_war
	group = event

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	allowed_for_character = {
		is_ai = yes
	}

	allowed_against_character = {
		always = yes
	}
	target_titles = all
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		always = no
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						target_is_same_character_or_above = scope:defender
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_religious_war_invalidation_region_message
	
	on_invalidated = {
		scope:attacker = {
			every_held_title = {
				limit = { has_variable = temporary_title }
				save_scope_as = destroyable_title
				scope:attacker = { destroy_title = scope:destroyable_title }
			}
		}
	}

	on_declaration = {
		on_declared_war = yes

	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = county_conquest_cb_victory_desc_attacker
			}
			desc = county_conquest_cb_victory_desc
		}
	}

	on_victory = {
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes
		
		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equally or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			every_in_de_jure_hierarchy = {
				continue = {
					OR = {
						NOT = {	exists = holder }
						NOT = {
							holder = { target_is_liege_or_above = scope:defender }
						}
						AND = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = { prev.holder.faith <= scope:defender.faith }
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
					}
					tier > tier_county
				}
				limit = {
					tier <= tier_duchy
					holder ?= {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}

				if = {
					limit = {
						OR = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith >= scope:defender.faith
								}
							}
							holder.primary_title.tier >= scope:attacker.primary_title.tier
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						add_to_temporary_list = vassals_taken
					}
				}
			}
		}

		every_in_list = {
			list = titles_taken
			if = {
				limit = { this = title:c_basra }
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
					take_baronies = yes
				}
				set_county_culture = culture:east_bantu
				set_county_faith = faith:azariqa
			}
			else_if = {
				limit = { this = title:d_basra }
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
					take_baronies = yes
				}
			}
			else = {
				create_zanj_vassal_effect = {
					LOCATION = scope:attacker.location
				}
				change_title_holder = {
					holder = scope:new_zanj_vassal
					change = scope:change
					take_baronies = yes
				}
				scope:new_zanj_vassal = {
					change_liege = {
						liege = scope:attacker
						change = scope:change
					}
				}
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}

		resolve_title_and_vassal_change = scope:change

		# Prestige Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_prestige_experience = medium_prestige_value
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		#Clean-up zanj leader
		hidden_effect = {
			scope:attacker = {
				every_held_title = {
					limit = { has_variable = temporary_title }
					save_scope_as = destroyable_title
					scope:attacker = { destroy_title = scope:destroyable_title }
				}
			}
		}
		#Give the Zanj Leader some Zanj Courtiers
		hidden_effect = {
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:attacker
				LOCATION = scope:attacker.location
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	white_peace_possible = no
	on_white_peace_desc = { desc = county_conquest_cb_white_peace_desc }

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = county_conquest_cb_defeat_desc_attacker
			}
			desc = county_conquest_cb_defeat_desc
		}
	}

	on_defeat = {
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes
		
		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
			# Prestige for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}

		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}

		on_lost_aggression_war_discontent_loss = yes

		#Clean-up zanj leader
		hidden_effect = {
			scope:attacker = {
				every_held_title = {
					limit = { has_variable = temporary_title }
					save_scope_as = destroyable_title
					scope:attacker = { destroy_title = scope:destroyable_title }
				}
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	transfer_behavior = transfer

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = fp3_zanj_rebellion_name
	war_name_base = fp3_zanj_rebellion_name
	cb_name = fp3_zanj_rebellion_name

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}
