﻿claim_cb = {
	icon = 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 = {
					OR = {
						culture = {
							has_innovation = innovation_chronicle_writing
						}
						AND = {
							government_has_flag = government_is_landless_adventurer
							has_realm_law = camp_purpose_legitimists
							var:legitimist_claimed_title ?= {
								OR = {
									holder = scope:defender
									de_jure_liege.holder = scope:defender
								}
							}
						}
					}
				}
			}
			# 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_divine_right
					}
				}
			}
		}
	}

	# This whole section was already commented out, I just commented it out more to prevent console errors
	#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 = {
		#Titular titles like the Italian communes have no de jure vassals, so the CB would show but the war could not start
		is_titular = no
		trigger_if = {
			limit = {
				scope:attacker = {
					is_ai = yes
					has_variable = conqueror
				}
			}
			tier >= tier_duchy
		}
		custom_description = {
			text = "claimant_titles_held_by_you_or_vassal"
			NOR = {
				holder = scope:attacker
				holder = {
					target_is_liege_or_above = scope:attacker
				}
			}
		}
		scope:claimant = {
			is_incapable = no
			is_hostage = no
			custom_description = {
				text = is_not_a_minister_desc
				tgp_is_any_minister = no
			}
			trigger_if = {
				limit = { scope:claimant != scope:attacker } # You can always press your own claims
				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 = {
					root.holder = {
						target_is_liege_or_above = scope:attacker
					}
				}
			}
		}
		trigger_if = {
			limit = {
				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 = { RITE = scope:attacker.rite 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 = { RITE = scope:attacker.rite CHARACTER = root.holder }
					}
				}
			}
		}
		#Under Byzantine law, a ruler must be unblemished
		trigger_if = {
			limit = {
				scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_disfigured"
					object = scope:claimant
					valid_for_byz_succession_trigger = yes
				}
			}
		}
		#Under Byzantine law, a ruler must be able to produce children.
		trigger_if = {
			limit = {
				scope:defender.culture = { has_cultural_parameter = physical_disfigurement_blocks_inheritance }
			}
			scope:claimant = {
				custom_description = {
					text = "byzantine_claimant_is_celibate"
					object = scope:defender
					is_eunuch_trigger = no
					NOT = {
						has_trait = celibate
					}
				}
			}
		}
	}

	allowed_for_character_display_regardless = {
		#If you're landless, you need the Legitimist camp purpose
		trigger_if = {
			limit = {
				scope:attacker = {
					government_has_flag = government_is_landless_adventurer
				}
			}
			scope:attacker = {
				has_realm_law = camp_purpose_legitimists
			}
		}
		#tgp_japan_offensive_wars_ban_trigger = yes
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			value = 0
			add = claim_cb_prestige_cost
			multiply = common_cb_prestige_cost_multiplier

			if = {
				limit = {
					scope:attacker = {
						has_character_flag = temp_no_claim_war_cost
					}
				}
				multiply = {
					value = 0
					desc = "free_cost" # Says 'free'
				}
			}

			# LEGITIMACY AFFECTS COST
			multiply = claim_cb_cost_multiplier_legitimacy
		}
	}

	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_effect = { FLAG = achievement_bad_blood_flag }
			}
		}
	}

	should_invalidate = {
		OR = {
			war_should_invalidate_defender_lost_target_titles_trigger = yes
			AND = {
				scope:claimant != scope:attacker
				scope:claimant = {
					OR = {
						is_alive = no
						NOR = {
							is_vassal_or_below_of = scope:attacker
							is_courtier_of = scope:attacker
						}
					}
				}
			}
			#scope:claimant = {
			#	tgp_is_any_minister = yes
			#}
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:claimant != scope:attacker
					scope:claimant = { is_alive = no }
				}
				desc = msg_claim_war_invalidated_claimant_unavailable_dead_message
			}
			triggered_desc = {
				trigger = {
					scope:claimant = {
						NOR = {
							this = scope:attacker
							is_vassal_or_below_of = scope:attacker
							is_courtier_of = scope:attacker
							#tgp_is_any_minister = yes
						}
					}
				}
				desc = msg_claim_war_invalidated_claimant_unavailable_not_vassal_message
			}
			#triggered_desc = {
			#	trigger = {
			#		scope:claimant = {
			#			tgp_is_any_minister = yes
			#		}
			#	}
			#	desc = msg_claim_war_invalidated_claimant_is_now_a_minister_message
			#}
			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 = {
		on_victory_aggressive_war_effect = yes

		pam_notify_claim_war_instigator_effect_win = yes
		#gg_claim_unites_pope_and_emperor_tooltip_effect = yes

		#Grab the higher title tier
		ordered_in_list = {
			list = target_titles
			order_by = tier
			save_scope_as = target
		}

		scope:attacker = {
			if = {
				limit = {
					is_ai = no
					has_realm_law = camp_purpose_legitimists
					any_in_list = {
						list = target_titles
						tier >= tier_kingdom
					}
				}
				add_achievement_global_variable_effect = {
					VARIABLE = finished_birthright_achievement
					VALUE = yes
				}
			}
			if = {
				limit = {
					is_landless_adventurer = yes
					NOT = { government_has_flag = government_is_nomadic }
				}
				hidden_effect = {
					if = {
						limit = {
							NOT = { has_trait = adventurer }
						}
						add_trait = adventurer
					}
					save_scope_as = the_adventurer
					every_courtier = {
						set_variable = {
							name = former_camp_leader
							value = scope:the_adventurer
						}
						add_trait = adventurer_follower
					}
				}
			}
			if = {
				limit = { is_landless_merchant = yes }
				save_scope_as = the_merchant
				hidden_effect = {
					save_scope_as = the_merchant
					every_courtier = {
						gdp_merchant_courtier_becomes_associate_effect = yes
					}
					# Now we reforge any Domicile Artifacts
					if = {
						limit = {
							any_character_artifact = { artifact_type = library_item }
						}
						every_character_artifact = {
							limit = { artifact_type = library_item }
							reforge_artifact = { type = book }
						}
					}
					if = {
						limit = {
							any_character_artifact = { artifact_type = curio }
						}
						every_character_artifact = {
							limit = { artifact_type = curio }
							reforge_artifact = { type = pedestal }
						}
					}
					if = {
						limit = {
							any_character_artifact = { artifact_type = creature }
						}
						every_character_artifact = {
							limit = { artifact_type = creature }
							set_variable = {
								name = og_owner
								value = artifact_owner
							}
							reforge_creature_artifact_effect = yes
						}
					}
				}
			}
			if = {
				limit = {
					has_variable = install_claimants_oath
					has_variable = install_claimants_oath_counter
				}
				add_to_variable_list = {
					name = installed_claimants_oath
					target = scope:claimant
				}
				change_variable = {
					name = install_claimants_oath_counter
					add = 1
				}
			}

			#EP2 Accolade glory gain from winning against higher ranked enemy
			accolade_attacker_war_end_glory_gain_med_effect = yes
		}

		on_victory_claim_cb_effect = yes

		# Landless Ceremonial Liege
		#if = {
		#	limit = {
		#		scope:claimant.top_liege != scope:defender.top_liege
		#		scope:claimant.primary_title.tier < scope:defender.primary_title.tier
		#		scope:claimant = { is_landed = no }
		#		any_in_list = {
		#			list = target_titles
		#			exists = var:ceremonial_title
		#		}
		#		NOT = {
		#			any_in_list = {
		#				list = target_titles
		#				OR = {
		#					is_landless_type_title = no
		#					is_noble_family_title = yes
		#				}
		#			}
		#		}
		#	}
		#	scope:defender = {
		#		faction_demand_regent_transfer_effect = {
		#			NEW_REGENT = scope:claimant
		#			REASON = conquest_claim
		#		}
		#	}
		#	create_title_and_vassal_change = {
		#		type = conquest_claim
		#		save_scope_as = change_three
		#		add_claim_on_loss = no
		#	}
		#	# Landless Ceremonial Liege must be house head
		#	scope:claimant = {
		#		if = {
		#			limit = { is_house_head = no }
		#			house = {
		#				house_head = {
		#					if = {
		#						limit = {
		#							is_landed = no
		#							is_ai = no
		#						}
		#						found_cadet_house_decision_effect = {
		#							CHARACTER = this
		#							PRESTIGE = 0
		#						}
		#					}
		#				}
		#				set_house_head = scope:claimant
		#			}
		#		}
		#	}
		#	resolve_title_and_vassal_change = scope:change_three
		#}
		#else_if = {
		#	limit = {
		#		scope:claimant != scope:attacker
		#		scope:claimant.primary_title.tier < scope:attacker.primary_title.tier
		#		scope:claimant = {
		#			NOT = { target_is_liege_or_above = scope:attacker }
		#		}
		#	}
		#	create_title_and_vassal_change = {
		#		type = conquest_claim
		#		save_scope_as = change_two
		#		add_claim_on_loss = yes
		#	}
		#	scope:claimant = {
		#		change_liege = {
		#			liege = scope:attacker
		#			change = scope:change_two
		#		}
		#	}
		#	resolve_title_and_vassal_change = scope:change_two
		#}

		#if = {
			#limit = {
				#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
		}
	}

	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 = {
		pam_notify_claim_war_instigator_effect_draw = yes

		on_white_peace_effect = yes
		on_white_peace_claim_cb_effect = yes

		#EP2 accolade glory gain for defender doing alright
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

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

	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 = {
		pam_notify_claim_war_instigator_effect_loss = yes

		on_defeat_aggressive_war_effect = yes
		on_defeat_claim_cb_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = {
			accolade_defender_war_end_glory_gain_med_effect = yes
		}

		# Attacker pays gold to the defender as reparations.
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 3
			}
		}

		# 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 = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
	}

	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_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 1000

	ai_score_mult = {
		value = 1

		add = common_cb_ai_score_mult_additive

		#There is no fighting in the admin room!
		if = {
			limit = {
				scope:attacker = {
					government_has_flag = government_is_feudal
					liege ?= {
						government_has_mechanic = administrative
					}
				}
				scope:defender = {
					government_has_mechanic = administrative
					liege ?= scope:attacker.liege
				}
			}
			multiply = 0.4
		}
		multiply = pam_peace_of_god_coreligionist_war_ai_score_value
		multiply = zealous_war_on_head_of_faith_ai_score_value
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}
