﻿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
				}

				E_kCAFG_county_randomized_invasion = {
					FACTOR = 0.7 # Violent rebellion with massacres
					VARIANCE = 0.2
					CULTURE = culture:east_bantu
					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
}