﻿claim_caliphate_cb = {
	group = conquest

	combine_into_one = yes

	allowed_for_character = {
		government_has_flag = government_is_caliphate
		exists = var:cal_authority
	}

	allowed_against_character = {
		has_government = caliphate_government
		scope:defender.faith = scope:attacker.faith
		scope:defender.faith.religious_head = scope:defender
	}
	allowed_against_character_display_regardless = { 
		AND = {
			exists = var:cal_authority
			scope:attacker = {
				exists = var:cal_authority
			}
			custom_description = {
				text = claim_authority_requirement
				subject = scope:attacker
				scope:attacker.var:cal_authority >= scope:defender.var:cal_authority
			}
		}
	}

	should_invalidate = {
		NOT = {
			OR = {
				scope:defender.faith = scope:attacker.faith
				scope:defender.faith.religious_head = scope:defender
			}
		}
	}

	on_invalidated_desc = msg_county_conquest_war_invalidated_message
	on_invalidated = {
	}

	cost = {
		piety = {
			value = 0
			if = {
				limit = {
					scope:defender.faith = scope:attacker.faith
				}
				add = {
					value = medium_piety_value
					desc = CB_SAME_FAITH_COST
				}
			}
			if = {
				limit = {
					scope:attacker = { has_government = tribal_government }
				}
				multiply = {
					value = 0.5
					desc = CB_TRIBAL_COST
				}
			}

			#Innovation Piety Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_reconquista
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_RECONQUISTA_COST
				}
			}

			if = {
				limit = {
					scope:attacker = {
						government_has_flag = government_is_nomadic
					}
				}
				multiply = {
					value = 0.3
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
		prestige = {
			value = 0
			if = {
				limit = {
					NOT = { scope:defender.faith = scope:attacker.faith }
				}
				add = {
					value = medium_prestige_value
					desc = CB_COST_DIFFERENT_FAITH
				}
			}
			if = {
				limit = {
					scope:attacker = { has_government = tribal_government }
				}
				multiply = {
					value = 0.5
					desc = CB_TRIBAL_COST
				}
			}

			#Innovation Prestige Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_chronicle_writing
							has_innovation = innovation_land_grants
							has_innovation = innovation_rightful_ownership
						}
					}
				}
				multiply = {
					value = 0.7
					desc = CB_ATTACKER_INNOVATIONS
				}
			}
			else_if = {
				limit = {
					scope:attacker = {
						culture = {
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_land_grants
							}
							OR = {
								has_innovation = innovation_land_grants
								has_innovation = innovation_rightful_ownership
							}
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_rightful_ownership
							}
						}
					}
				}
				multiply = {
					value = 0.8
					desc = CB_ATTACKER_INNOVATIONS
				}
			}
			else_if = {
				limit = {
					scope:attacker = {
						culture = {
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_land_grants
								has_innovation = innovation_rightful_ownership
							}
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_ATTACKER_INNOVATIONS
				}
			}

			if = {
				limit = {
					scope:attacker = {
						government_has_flag = government_is_nomadic
					}
				}
				multiply = {
					value = 0.3
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
	}

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

	on_victory = {
        #EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:defender = {
			every_held_title = {
				limit = {
					scope:defender.faith.religious_head_title = THIS
				}
				change_title_holder_include_vassals = {
					holder = scope:attacker
					change = scope:change
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		scope:attacker = {
			add_prestige_experience = 500

			change_variable = {
				name = cal_authority
				add = 10
			}
			clamp_variable = {
				name = cal_authority
				max = 0
				min = 100
			}
		}

		add_from_contribution_defenders = {
			prestige = medium_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		add_from_contribution_attackers = {
			prestige = medium_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		add_truce_attacker_victory_effect = yes
	}

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

	on_white_peace = {
        #EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }
		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
 			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = minor_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

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

		add_truce_white_peace_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = caliphate_cb_defeat_desc
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = caliphate_cb_defeat_desc
			}
			desc = caliphate_cb_defeat_desc
		}
	}

	on_defeat = {
        #EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_attacker_war_end_glory_gain_high_effect = yes }
		scope:attacker = { show_pow_release_message_effect = yes }


		scope:attacker = {
			pay_short_term_gold = {
				gold = 3
				target = scope:defender
				yearly_income = yes
			}
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
			change_variable = {
				name = cal_authority
				subtract = 10
			}
			clamp_variable = {
				name = cal_authority
				max = 0
				min = 100
			}
		}

		add_from_contribution_attackers = {
			prestige = medium_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		scope:defender = {
			add_prestige = major_prestige_value
			
		}

		add_from_contribution_defenders = {
			prestige = medium_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		add_truce_attacker_defeat_effect = yes

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

	transfer_behavior = transfer

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "CALIPHATE_WAR_NAME"
	war_name_base = "CALIPHATE_WAR_NAME_BASE"
	cb_name = "CALIPHATE_CB_NAME"

	interface_priority = 100
}

holy_jihad_cb = {
	group = religious 

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

	allowed_for_character = {
		government_has_flag = government_is_caliphate
		has_religion = religion:islam_religion
	}

	allowed_for_character_display_regardless = {
		AND = {
			exists = var:cal_authority
			custom_description = {
				text = jihad_authority_requirement
				subject = scope:attacker
				scope:attacker.var:cal_authority >= 50
			}
		}
	}

	allowed_against_character = {
		NOT = { scope:defender.religion = scope:attacker.religion }
	}
	target_titles = neighbor_land
	target_title_tier = kingdom
	ignore_effect = change_title_holder
	target_de_jure_regions_above = yes

	should_invalidate = {
		scope:defender.religion = scope:attacker.religion
	}

	on_invalidated_desc = msg_caliphate_war_invalidated_message
	on_invalidated = {
	}

	cost = {
		piety = {
			value = 500

			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}
		}
		prestige = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}

			#Innovation Prestige Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_chronicle_writing
							has_innovation = innovation_land_grants
							has_innovation = innovation_rightful_ownership
						}
					}
				}
				multiply = {
					value = 0.7
					desc = CB_ATTACKER_INNOVATIONS
				}
			}
			else_if = {
				limit = {
					scope:attacker = {
						culture = {
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_land_grants
							}
							OR = {
								has_innovation = innovation_land_grants
								has_innovation = innovation_rightful_ownership
							}
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_rightful_ownership
							}
						}
					}
				}
				multiply = {
					value = 0.8
					desc = CB_ATTACKER_INNOVATIONS
				}
			}
			else_if = {
				limit = {
					scope:attacker = {
						culture = {
							OR = {
								has_innovation = innovation_chronicle_writing
								has_innovation = innovation_land_grants
								has_innovation = innovation_rightful_ownership
							}
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_ATTACKER_INNOVATIONS
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}
			
			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:defender = {
			trigger_event = islam.0004
		}

		scope:attacker = {
			set_variable = {
				name = tmp_army_jihad
				value = 0
			}
			every_realm_county = {
				limit = {
					faith = scope:attacker.faith
				}
				scope:attacker = {
					change_variable = {
						name = tmp_army_jihad
						add = 1
					}
				}
			}
			spawn_army = {
				name = "Muslim Mujahideen"
				levies = 3000
				men_at_arms = {
					type = light_footmen
					stacks = scope:attacker.var:tmp_army_jihad
				}
				war = scope:war
				location = capital_province
			}
			remove_variable = tmp_army_jihad
		}
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = invasion_war_victory_desc_attacker
			}
			desc = invasion_war_victory_desc
		}
	}
	
	on_victory = {
        #EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }
		scope:attacker = { show_pow_release_message_effect = yes }
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		
		setup_invasion_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			change = scope:change
			take_occupied = yes
		}
		resolve_title_and_vassal_change = scope:change

		# Attacker gets Prestige Experience, Defender loses Prestige, all other participants gain Prestige based on their war contribution.
		modify_all_participants_fame_values = {
			WINNER = scope:attacker
			LOSER = scope:defender
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_invasion_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		scope:attacker.faith = {
			change_fervor = 5
		}
		scope:defender.faith = {
			change_fervor = -5
		}
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
        #EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }
		
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = major_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = major_prestige_value
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

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

		scope:attacker.faith = {
			change_fervor = -2
		}
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
        #EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_attacker_war_end_glory_gain_high_effect = yes }
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold = {
				gold = 5
				target = scope:defender
				yearly_income = yes
			}
			add_prestige = {
				value = massive_prestige_value
				multiply = -1.0
			}
		}
		
		setup_invasion_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}

		# Attacker loses Prestige, all other war participants gain Prestige (Defender gains full prestige, all allies on both sides gain based on war contribution).
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
	
		add_truce_attacker_defeat_effect = yes
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes

		scope:attacker.faith = {
			change_fervor = -5
		}
		scope:defender.faith = {
			change_fervor = 5
		}
	}

	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "JIHAD_WAR_NAME"
	war_name_base = "JIHAD_WAR_NAME_BASE"
	cb_name = "JIHAD_CB_NAME"
	
	interface_priority = 90

	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
}