﻿# Unlimited Holy Wars - Player Only
# CK3 1.19.* test version
# Overrides only major_religious_war to remove the once-per-lifetime kingdom holy war limit for the player.
# No empire-tier holy war CB is added in this version.

major_religious_war = {
	icon = major_religious_war
	group = religious

	defender_faith_can_join = yes

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

	# Additional requirements to the defines in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine_parameter = unreformed }
		}
		NOR = { 
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_celestial
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				is_alive = yes
				has_variable_list = humsacd_hofs
				is_target_in_variable_list = {
					name = humsacd_hofs
					target = scope:attacker.faith
				}
			}
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							house ?= {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= 1
			}
			trigger_else = {
				piety_level >= 2
			}
		}
		trigger_else = {
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							house ?= {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						} 
					}
				}
				piety_level >= 2
			}
			trigger_else = {
				piety_level >= 3
			}
		}
		trigger_if = {
			limit = {
				# Player-only change: AI keeps the vanilla once-per-lifetime limit.
				is_ai = yes
				culture = {
					NOT = {
						has_cultural_parameter = no_limit_to_kingdom_level_holy_wars
					}
				}
			}
			custom_description = {
				text = declared_major_religious_war_flag_tt
				OR = {
					has_character_flag = bonus_major_holy_war
					NOT = { has_character_flag = declared_major_religious_war_flag }
				}
			}
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		scope:attacker = {
			faith = {
				faith_hostility_level = {
					target = scope:defender.faith
					value >= religious_cb_enabled_hostility_level
				}
			}
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		scope:target = {
			tier = tier_kingdom
			is_landless_type_title = no
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:attacker = {
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
					}
				}
				scope:defender = {
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
					}
				}
			}
			are_holy_wars_disabled_in_struggle_phase_trigger = yes
		}
		are_holy_wars_disabled_by_struggle_conciliation_trigger = no # Disabled against other involved faiths in FP2 Conciliation ending

		scope:target = {
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 5
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = {
		OR = {
			NOT = {
				any_in_list = {
					list = target_titles
					any_in_de_jure_hierarchy = {
						tier = tier_county
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
				}
			}
			#Faith change does not immediately invalidate, it's run through event war_event.3100
			scope:war = { exists = var:invalidate_defender_faith_change }
			scope:war = { exists = var:invalidate_attacker_faith_change }
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = {
						any_in_list = {
							list = target_titles
							any_in_de_jure_hierarchy = {
								tier = tier_county
								holder = {
									OR = {
										this = scope:defender
										target_is_liege_or_above = scope:defender
									}
								}
							}
						}
					}
				}
				desc = msg_religious_war_invalidation_region_message
			}
			triggered_desc = {
				trigger = {
					scope:war = {
						OR = {
							exists = var:invalidate_defender_faith_change
							exists = var:invalidate_attacker_faith_change
						}
					}
				}
				desc = msg_religious_war_invalidated_hostility_level_message
			}
		}
	}
	
	on_invalidated = {
		scope:attacker = { remove_character_flag = declared_major_religious_war_flag }
	}

	cost = {
		piety = {
			add = {
				value = 750
				desc = CB_BASE_COST
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				multiply = accolade_piety_reduction_value
				min = 90
			}
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				multiply = accolade_piety_reduction_value_high
				min = 90
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			if = {
				limit = {
					has_character_flag = bonus_major_holy_war
				}
				remove_character_flag = bonus_major_holy_war
			}
			else_if = {
				limit = { is_ai = yes }
				add_character_flag = declared_major_religious_war_flag
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker.faith = {
						has_doctrine = doctrine_pluralism_pluralistic
					}
				}
				desc = religious_war_victory_desc_doctrine_pluralism
			}
			desc = religious_war_victory_desc
		}
	}

	on_victory = {
		scope:attacker = {
			if = {
				limit = {
					has_variable = k_holy_war_crusader
				}
				add_trait = faith_warrior
			}
		}
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# 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_high_effect = yes }

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

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = RELIGIOUS_CB_TITLE

			conquest_cb_title_transfer = {
				RELIGIOUS_WAR = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}
		
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		resolve_title_and_vassal_change = scope:change

		# 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 = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Piety Progress for the Attacker
		every_in_list = {
			list = target_titles
				scope:attacker = {
				add_piety_experience = {
					add = religious_cb_piety_gain_kingdom
					if = {
						limit = {
							scope:attacker.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}
		}

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}

		scope:attacker.faith = {
			change_fervor = {
				value = -5
				desc = fervor_loss_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 10
				desc = fervor_gain_religious_war
			}
		}

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

		# 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 = {
		desc = religious_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

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

		# 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 = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

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

		# Truce
		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_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# 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
		}

		# 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 = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# piety change
		every_in_list = {
			list = target_titles

			scope:defender = {
				add_piety = {
					add = religious_cb_piety_gain_kingdom
					if = {
						limit = {
							scope:defender.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}

			scope:attacker = {
				if = {
					limit = {
						monthly_character_income > 0
					}
					pay_short_term_income = {
						years = 4
						target = scope:defender
					}
				}
				else = {
					pay_short_term_gold = {
						target = scope:defender
						gold = medium_gold_value
					}
				}
			}
		}

		scope:attacker = {
			every_vassal = {
				limit = { faith = scope:attacker.faith }
				custom = all_attackers_vassals_same_faith
				add_opinion = {
					modifier = liege_lost_declared_religious_war
					target = scope:attacker
				}
			}
		}

		# Truce
		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_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "RELIGIOUS_WAR_NAME"
	cb_name = "HOLY_WAR_KINGDOM_NAME"

	is_holy_war = yes

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						top_liege != this
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}

		# As a feudal gov, do not attack owner of tribal holdings  if you have a tribal holding to convert already
		add = {
			every_in_list = {
				list = target_titles
				if = {
					limit = { feudal_clan_tribal_conquest_constraints = yes }
					add = -1000
				}
			}
		}
		
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}
