﻿claim_cb = {
	group = claim
	mutually_exclusive_titles = {
		NOT = {
			# If pushing personal claims, you can press multiple claims with the Divine Right innovation (High Medieval Era).
			trigger_if = {
				limit = {
					scope:attacker = scope:claimant
				}
				scope:attacker = {
					culture = {
						has_innovation = innovation_divine_right
					}
				}
			}
			# If pushing claims for someone else, you need the Rightful Ownership innovation to press multiple claims (Late Medieval Era).
			trigger_else = {
				scope:attacker = {
					culture = {
						has_innovation = innovation_rightful_ownership
					}
				}
			}
		}
	}

	#allowed_for_character = {}

	allowed_against_character = {
		#faith.religion = scope:attacker.faith.religion
	}

	# Root is the title
	# scope:claimant is the claimant
	# scope:attacker is the attacker
	# scope:defender is the defender
	is_allowed_claim_title = {
		custom_description = {
			text = "claimant_titles_held_by_you_or_vassal"
			NOR = {
				holder = scope:attacker
				holder = {
					target_is_liege_or_above = scope:attacker
				}
			}
		}
		scope:claimant = {
			NOT = { has_trait = incapable }
			is_imprisoned = no
			#religion = scope:attacker.religion
			# claimant must not be at war against defender for this claim already
			custom_description = {
				text = "claimant_already_pressing_their_claim"
				object = root
				NAND = {
					liege = scope:attacker
					any_character_war = {
						casus_belli = {
							any_target_title = {
								this = root
							}
						}
					}
				}
			}
		}
		scope:attacker = {
			#Cannot press claims against your own vassals, as these must be revoked.
			custom_description = {
				text = "claimant_titles_held_by_attacker_vassals"
				NOT = {
					any_vassal_or_below = {
						has_title = root
					}
				}
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:claimant = scope:attacker } # You can always press your own claims (though the prestige cost might be high)
			}
			trigger_if = {
				limit = {
					is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = scope:claimant }
				}
				trigger_if = {
					limit = {
						scope:claimant = { is_adult = yes }
					}
					save_temporary_scope_as = treat_claim_as_weak
				}
				trigger_else = {
					always = no
				}
			}
			trigger_else_if = {
				limit = {
					scope:claimant = { is_adult = no }
				}
				save_temporary_scope_as = treat_claim_as_weak
			}
			# making the default behavior explicit if no other trigger_(else_)ifs are valid
			trigger_else = {
				always = yes
			}
		}
		trigger_if = {
			limit = { exists = scope:treat_claim_as_weak }
			holder = {
				custom_description = {
					text = "claim_target_position_is_weak"
					object = scope:attacker
					OR = {
						has_trait = incapable
						is_adult = no
						is_weak_claimant_due_to_gender_trigger = { FAITH = scope:attacker.faith CHARACTER = root.holder }
					}
				}
			}
		}
		#The Byzantine Emperor must be unblemished
		trigger_if = {
			limit = {
				OR = {
					root = title:e_byzantium
					root = title:e_roman_empire
				}
				scope:defender = { has_culture_group = culture_group:byzantine_group }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_disfigured"
					object = scope:claimant
					NOR = {
						has_trait = incapable
						has_trait = blind
						has_trait = disfigured
						has_trait = eunuch
						has_trait = maimed
						has_trait = one_eyed
						has_trait = one_legged
						has_trait = clubfooted
						has_trait = hunchbacked
						has_trait = dwarf
					}
				}
			}
		}
		#Under Byzantine law, a ruler must be able to produce children.
		trigger_if = {
			limit = {
				scope:defender = { has_culture_group = culture_group:byzantine_group }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_celibate"
					object = scope:claimant
					NOR = {
						has_trait = eunuch
						has_trait = devoted
						has_trait = celibate
					}
				}
			}
		}
	}

	cost = {
		piety = {
			value = 0
			if = { # Attacking your HoF is impious
				limit = {
					exists = scope:attacker.faith.religious_head
					scope:attacker.faith.religious_head = scope:defender
				}
				if = {
					limit = {
						scope:attacker.faith = {
							has_doctrine = doctrine_spiritual_head
						}
					}
					add = {
						value = massive_piety_value
						desc = CB_ATTACKER_HOF
					}
				}
				else = {
					add = {
						value = medium_piety_value
						desc = CB_ATTACKER_HOF
					}
				}
			}
			if = {
				limit = {
					scope:attacker.faith = scope:defender.faith
					scope:defender = {
						has_government = theocracy_government
					}
				}
				add = {
					value = medium_piety_value
					desc = CB_ATTACKER_THEOCRACY
				}
			}
		}
		prestige = {
			value = 0
			if = {
				# Prestige cost can vary significantly for personal claims, based on the situation(s) the claimant/defender are in.
				limit = { scope:claimant = scope:attacker }

				add = {
					every_in_list = {
						list = target_titles
						limit = { tier = tier_county }
						add = {
							value = 100
							if = {
								# 25% cheaper to take Pressed claims.
								limit = { scope:attacker = { has_strong_claim_on = prev } }
								add = -25
							}
						}
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_duchy }
						add = {
							value = 200
							if = {
								# 25% cheaper to take Pressed claims.
								limit = { scope:attacker = { has_strong_claim_on = prev } }
								add = -50
							}
						}
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_kingdom }
						add = {
							value = 400
							if = {
								# 25% cheaper to take Pressed claims.
								limit = { scope:attacker = { has_strong_claim_on = prev } }
								add = -100
							}
						}
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_empire }
						add = {
							value = 800
							if = {
								# 25% cheaper to take Pressed claims.
								limit = { scope:attacker = { has_strong_claim_on = prev } }
								add = -200
							}
						}
					}

					desc = "CB_CLAIM_COST_TARGET_TITLES"
				}
				if = { # 50% cheaper for adults to take titles from child rulers.
					limit = {
						scope:attacker = { is_adult = yes }
						scope:defender = { is_adult = no }
					}
					multiply = {
						value = 0.5
						desc = "CB_COST_DEFENDER_IS_CHILD"
					}
				}
				else_if = { # 50% more expensive for children to take titles from adult rulers.
					limit = {
						scope:attacker = { is_adult = no }
						scope:defender = { is_adult = yes }
					}
					multiply = {
						value = 1.5
						desc = "CB_COST_ATTACKER_IS_CHILD"
					}
				}
				if = { # 50% cheaper to take titles from a dynasty member.
					limit = {
						scope:attacker.dynasty = scope:defender.dynasty
					}
					multiply = {
						value = 0.5
						desc = "CB_COST_SAME_DYNASTY"
					}
				}

				if = {
					limit = { # If claimant & target share a faith, cost is modified by gender preferences.
						scope:attacker.faith = scope:defender.faith
						scope:attacker = {
							sex_opposite_of = scope:defender
						}
						OR = {
							scope:attacker.faith = { has_doctrine = doctrine_gender_male_dominated }
							scope:attacker.faith = { has_doctrine = doctrine_gender_female_dominated }
						}
					}
					multiply = {
						if = {
							limit = {
								scope:attacker.faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							if = {
								limit = { # 25% cheaper for men to press claims on female rulers in male-dominated societies.
									scope:attacker = { is_male = yes }
									scope:defender = { is_male = no }
								}
								value = 0.75
							}
							else_if = {
								limit = { # 25% more expensive for women to press claims on male rulers in male-dominated societies.
									scope:attacker = { is_male = no }
									scope:defender = { is_male = yes }
								}
								value = 1.25
							}
							else = {
								value = 1
							}
						}
						else_if = {
							limit = {
								scope:attacker.faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							if = {
								limit = { # 25% cheaper for women to press claims on male rulers in female-dominated societies.
									scope:attacker = { is_male = no }
									scope:defender = { is_male = yes }
								}
								value = 0.75
							}
							else_if = {
								limit = { # 25% more expensive for men to press claims on female rulers in female-dominated societies.
									scope:attacker = { is_male = yes }
									scope:defender = { is_male = no }
								}
								value = 1.25
							}
							else = {
								value = 1
							}
						}
						else = {
							value = 1
						}
						desc = "CB_CLAIM_COST_GENDER_PREFERENCES"
					}
				}
				else_if = {
					# Taking titles from someone of another faith costs 25% more, regardless of gender preferences.
					limit = {
						NOT = {
							scope:attacker.faith = scope:defender.faith
						}
					}
					multiply = {
						value = 1.25
						desc = "CB_COST_DIFFERENT_FAITH"
					}
				}

				if = { # 25% more expensive to take titles from lower-tier rulers (pick on someone your own size!)
					limit = {
						scope:attacker.primary_title.tier > scope:defender.primary_title.tier
					}
					multiply = {
						value = 1.25
						desc = "CB_DEFNDER_LOWER_TIER"
					}
				}
			}
			else = {
				# When pressing a 3rd party's claims, the cost is mostly static.
				add = {
					every_in_list = {
						list = target_titles
						limit = { tier = tier_county }
						add = 250
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_duchy }
						add = 500
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_kingdom }
						add = 1000
					}
					every_in_list = {
						list = target_titles
						limit = { tier = tier_empire }
						add = 3000
					}
					desc = "CB_CLAIM_COST_TARGET_TITLES"
				}
				if = {
					limit = { # 100% more expensive to press claims for someone not of your Religion
						NOT = { scope:attacker.faith.religion = scope:claimant.faith.religion }
					}
					multiply = {
						value = 2
						desc = "CB_CLAIM_COST_CLAIMANT_DIFFERENT_RELIGION"
					}
				}
				if = {
					limit = { # 50% more expensive to press claims for someone not in your Dynasty
						NOT = { scope:attacker.dynasty = scope:claimant.dynasty }
					}
					multiply = {
						value = 1.5
						desc = "CB_CLAIM_COST_CLAIMANT_DIFFERENT_DYNASTY"
					}
				}
				else_if = {
					limit = { # 25% more expensive to press claims for someone not in your House
						NOT = { scope:attacker.house = scope:claimant.house }
					}
					multiply = {
						value = 1.25
						desc = "CB_CLAIM_COST_CLAIMANT_DIFFERENT_HOUSE"
					}
				}
			}

			#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"
				}
			}
		}
	}

	target_titles = claim
	#target_title_tier = all

	on_declaration = {
		on_declared_war = yes
		if = {
			limit = {
				scope:defender = { is_sibling_of = scope:attacker }
			}
			scope:attacker = {
				add_achievement_flag = { FLAG = achievement_bad_blood_flag }
			}
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier >= tier_county
					exists = holder
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_claim_war_invalidated_message
	
	on_invalidated = {
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:claimant = { is_local_player = yes }
				}
				desc = claim_cb_victory_desc_attacker_claimant
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = claim_cb_victory_desc_attacker
			}
			desc = claim_cb_victory_desc
		}
	}

	on_victory = {
		scope:claimant = { add_character_flag = prevent_military_command } #prevents defaulting to military command so pushing foreign claims results in feudal realm
		
		create_title_and_vassal_change = change

		scope:change = {
			set_title_and_vassal_change_type = conquest_claim
			set_add_claim_on_loss = yes
		}

		# Create a claim CB which is executed to handle title/vassal changes, in addition to being used to calculate Prestige awards for war participants.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			change = scope:change
		}

		resolve_title_and_vassal_change = scope:change

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:claimant.primary_title.tier < scope:attacker.primary_title.tier
				trigger_if = {
					limit = {
						exists = scope:claimant.liege
					}
					NOT = { scope:claimant.liege = scope:attacker }
				}
				trigger_else = {
					always = yes
				}
			}

			create_title_and_vassal_change = change_two
			
			scope:claimant = {
				change_liege = {
					liege = scope:attacker
					change = scope:change_two
				}
				
			}
			resolve_title_and_vassal_change = scope:change_two
			
		}
		scope:claimant = { ##Imperial mod##
			#Also give feudal vassal contract in imperial government
			if = {
				limit = {
					liege = scope:attacker
					NOT = { this = scope:attacker }
					scope:attacker = {
						OR = {
							has_government = imperial_government
							has_government = military_command
						}
					}
					NOR = { 
						has_government = military_command
						has_government = patrician_government
						has_government = republic_government
						has_government = theocracy_government
					}
				}
				add_character_flag = imperial_autonomy_override
				vassal_contract_set_obligation_level = { type = imperial_autonomy level = 1 }
			}
			remove_character_flag = prevent_military_command
		}
		
		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# 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_claim_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

	}

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

	on_white_peace = {

		# press claims
		every_in_list = {
			list = target_titles
			if = {
				limit = { scope:claimant = { has_weak_claim_on = prev } }
				scope:claimant = {
					make_claim_strong = prev
				}
			}
		}

		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
		setup_claim_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = scope:cb_prestige_factor # Set by 'setup_claim_cb'
				multiply = -5.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:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
		# Defender neither gains nor loses any prestige.

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# Truce
		add_truce_white_peace_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = { scope:claimant = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_attacker_claimant
			}
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = claim_cb_defeat_desc_attacker
			}
			desc = claim_cb_defeat_desc
		}
	}

	on_defeat = {

		# lose claims
		every_in_list = {
			list = target_titles
			scope:claimant = {
				remove_claim = prev
			}
		}

		if = {
			limit = {
				NOT = { scope:claimant = scope:attacker }
				scope:attacker = {
					can_add_hook = {
					  	type = favor_hook
					  	target = scope:claimant
					}
				}
			}
			scope:attacker = {
				add_hook = {
		  			target = scope:claimant
		  			type = favor_hook
		 		}
			}
		}

		# Attacker pays gold to the defender as reparations.
		scope:attacker = {
			pay_short_term_gold = {
				gold = 3
				target = scope:defender
				yearly_income = yes
			}
		}
		# Create an 'helper' claim CB which we won't execute, but can use to determine how much Prestige should be given to war participants.
		setup_claim_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
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

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

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "CLAIM_WAR_NAME"
	my_war_name = "CLAIM_WAR_NAME_MY"
	war_name_base = "CLAIM_WAR_NAME_BASE"
	cb_name = "CLAIM_CB_NAME"
	interface_priority = 60

	should_show_war_goal_subview = yes

	attacker_wargoal_percentage = 0.8

	max_ai_diplo_distance_to_title = 1000
	min_ai_score = 100
}
