﻿holy_war_cb_cooldown_effect = {
	if = {
		limit = { has_game_rule = holy_war_cb_3 }
		add_character_modifier = {
			modifier = mgr_recent_holy_war
			years = 3
		}
	}
	else_if = {
		limit = { has_game_rule = holy_war_cb_5 }
		add_character_modifier = {
			modifier = mgr_recent_holy_war
			years = 5
		}
	}
	else_if = {
		limit = { has_game_rule = holy_war_cb_10 }
		add_character_modifier = {
			modifier = mgr_recent_holy_war
			years = 10
		}
	}
	else_if = {
		limit = { has_game_rule = holy_war_cb_20 }
		add_character_modifier = {
			modifier = mgr_recent_holy_war
			years = 20
		}
	}
}

conquest_war_cb_cooldown_effect = {
	if = {
		limit = { has_game_rule = conquest_war_cb_3 }
		add_character_modifier = {
			modifier = mgr_recent_conquest_war
			years = 3
		}
	}
	else_if = {
		limit = { has_game_rule = conquest_war_cb_5 }
		add_character_modifier = {
			modifier = mgr_recent_conquest_war
			years = 5
		}
	}
	else_if = {
		limit = { has_game_rule = conquest_war_cb_10 }
		add_character_modifier = {
			modifier = mgr_recent_conquest_war
			years = 10
		}
	}
	else_if = {
		limit = { has_game_rule = conquest_war_cb_20 }
		add_character_modifier = {
			modifier = mgr_recent_conquest_war
			years = 20
		}
	}
}

#Temp removed pending rework
#mgr_empires_effect = {
#	every_empire = {
#		limit = { 
#			OR = {
#				AND = {
#					mgr_is_historical_empire = no 
#					has_game_rule = historical_empires
#				}
#				AND = {
#					mgr_is_expanded_historical_empire = no 
#					has_game_rule = ex_historical_empires
#				}
#				has_game_rule = no_dejure_empires
#			}
#		}
#		every_in_de_jure_hierarchy = {
#			limit = {
#				tier = tier_kingdom
#			}
#			set_de_jure_liege_title = title:e_mgr_null #sadly setting this to 0, none, etc. doesn't work.
#		}
#	}
#	#Expand de jure HRE after Otto I is Emperor
#	if = {
#		limit = { #Year condition isn't working here, so we use this check to see if HRE should exist or not
#			title:e_hre = { any_in_de_jure_hierarchy = { this = title:k_east_francia } }
#			has_game_rule = historical_empires
#		}
#		title:k_italy = { set_de_jure_liege_title = title:e_hre }
#		title:k_romagna = { set_de_jure_liege_title = title:e_hre }
#		title:k_burgundy = { set_de_jure_liege_title = title:e_hre }
#	}
#}

confed_partition_succ_shatter_effect = {
	#Destroy empire titles after heir receives it.
	if = {
		limit = {
			OR = {
				government_has_flag = government_is_tribal
				faith = {
					has_doctrine_parameter = unreformed
				}
			}
			OR = {
				has_game_rule = confed_partition_shatter 
				has_game_rule = confed_partition_combo
			}
			#Not if we've reformed to elective succession
			OR = {
				government_has_flag = government_is_tribal
				NOT = {
					primary_title = {
						OR = {
							has_title_law_flag = advanced_succession_law
							has_title_law_flag = elective_succession_law
						}
					}
				}
			}
			any_held_title = { 
				tier = tier_empire 
				NOT = { this = title:e_mongol_empire } #Already handled elsewhere...
				NOT = { this = title:e_byzantium } #Avoid collapse in 867 start
				OR = { #HRE must be sanctioned by the pope unless it's reformed to a different succession law - or maybe change to Germany in this case? but that should be handled elsewhere...
					NOT = { this = title:e_hre }
					NOT = { root.faith = faith:catholic } 
				}
			}
		}
		every_held_title = {
			limit = { 
				tier = tier_empire 
				NOT = { this = title:e_mongol_empire } 
				NOT = { this = title:e_byzantium }
				OR = { 
					NOT = { this = title:e_hre }
					NOT = { root.faith = faith:catholic } 
				}
			}
			root = { 
				destroy_title = prev 
				add_pressed_claim = prev
			}
		}
	}
}
confed_partition_succ_vassal_effect = {
	#Release any vassals if primary title is not de jure - better as an event?
	if = {
		limit = {
			OR = {
				has_game_rule = confed_partition_release
				has_game_rule = confed_partition_combo
			}
			#NOT = { father = { this = character:3040 } } #See above
			#Not if we've reformed to elective succession
			OR = {
				government_has_flag = government_is_tribal
				NOT = {
					primary_title = {
						OR = {
							has_title_law_flag = advanced_succession_law
							has_title_law_flag = elective_succession_law
						}
					}
				}
			}
		}
		if = {
			limit = {
				is_independent_ruler = yes 
			}
			every_vassal = {
				limit = {
					#NOT = { root = { has_de_jure_claim_on = prev } }
					is_de_jure_vassal_of_ROOT_top_liege_trigger = no
					
					OR = {
						NOT = { root = { any_held_title = { this = title:e_hre } } }
						is_protected_hre_vassal = no
					}
				}
				every_held_title = {
					root.player_heir = { add_unpressed_claim = prev }
				}
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
				}
				change_liege_or_become_independent = {
					CHANGE = scope:change
					VASSAL = this
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
		else = {
			every_vassal = {
				limit = {
					#NOT = { root = { has_de_jure_claim_on = prev } }
					is_de_jure_vassal_of_ROOT_top_liege_trigger = no
				}
				every_held_title = {
					root.player_heir = { add_unpressed_claim = prev }
				}
				create_title_and_vassal_change = {
					type = conquest #surely there's a better option?
					save_scope_as = title_change
				}
				change_liege = {
					liege = root.liege
					change = scope:title_change
				}
				resolve_title_and_vassal_change = scope:title_change
			}
		}
	}
}

mgr_culture_religion_setup_effect = {
	###########
	###ROMAN###
	###########
	if = {
		limit = { 
			NOT = { has_game_rule = default_roman_culture }
		}
		culture:roman = {
			add_innovation = innovation_legionnaires
			if = {
				limit = { has_ep3_dlc_trigger = yes }
				remove_culture_tradition = tradition_noble_adoption #cultivated sophistication is this but more
				#High Roman
				remove_culture_tradition = tradition_hereditary_hierarchy #why is this even in vanilla?
				add_culture_tradition = tradition_ep3_roman_ceremonies #It's in the NAME Paradox...
				add_culture_tradition = tradition_ep3_cultivated_sophistication
				#Late Roman
				if = {
					limit = { has_game_rule = mgr_late_roman_culture }
					remove_culture_tradition = tradition_poetry #Classical Roman Lit was moribund in the sixth century. Also it was either this or Legalism...
					remove_culture_tradition = tradition_roman_legacy #Imperial Tagmata is more representative
					#
					add_culture_tradition = tradition_ep3_palace_politics
					add_culture_tradition = tradition_ep3_imperial_tagmata
				}
			}
		}
	}
	###############
	###HELLENISM###
	###############
	if = {
		limit = { 
			has_game_rule = roman_hellenism
		}
		faith:hellenic_pagan = {
			remove_doctrine = unreformed_faith_doctrine
			remove_doctrine = tenet_communal_identity
			remove_doctrine = doctrine_no_head
			remove_doctrine = doctrine_theocracy_temporal
			add_doctrine = doctrine_theocracy_lay_clergy
			add_doctrine = tenet_household_gods
			add_doctrine = doctrine_temporal_head
		}
		set_global_variable = household_gods_tenet_unlocked
	}
	###############
	###SHINTO###
	###############
	if = {
		limit = { 
			has_game_rule = yamato_shinto
		}
		faith:shinto = {
			remove_doctrine = doctrine_no_head
			remove_doctrine = doctrine_clerical_succession_spiritual_fixed_appointment
			remove_doctrine = doctrine_theocracy_temporal
			add_doctrine = doctrine_temporal_head
			add_doctrine = doctrine_theocracy_lay_clergy
			add_doctrine = doctrine_clerical_succession_temporal_fixed_appointment

			set_religious_head_title = title:k_chrysanthemum_throne
		}
		title:k_chrysanthemum_throne = {
			holder = {
				set_character_faith = faith:shinto
				every_child = { 
					limit = { is_female = no } #to prevent children of other clans from automatically converting
					set_character_faith = faith:shinto
					every_child = { set_character_faith = faith:shinto }
				}
				father? = {
					set_character_faith = faith:shinto #OK to convert sisters
					every_child = { 
						set_character_faith = faith:shinto
						every_child = {
							limit = { is_female = no }
							set_character_faith = faith:shinto
						}
					}
				}
			}
		}
	}
}

mgr_create_custom_hegemony_effect = {
	custom_tooltip = create_new_hegemony
	show_as_tooltip = {
		every_held_title = {
			custom = create_title_every_held_empire
			title_tier = empire
			custom_tooltip = create_custom_hegemony_de_jure_changes
		}
	}

	hidden_effect = {
		save_scope_as = founder
		primary_title = {
			save_scope_as = old_title
		}

		create_dynamic_title = {
			tier = hegemony
			name = NEW_CREATED_TITLE_NAME
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}

		scope:new_title = {
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}

		resolve_title_and_vassal_change = scope:change

  		every_held_title = {
			title_tier = empire
			if = {
				limit = {
					#Check if you need to notify a player
					empire ?= {
						holder ?= {
							this != root
							is_ai = no
						}
					}
				}
				add_to_temporary_list = empires_for_notification
				root = {
					save_temporary_scope_value_as = {
						name = send_notifications
						value = yes
					}
				}
			}
			set_de_jure_liege_title = scope:new_title
		}

		every_sub_realm_county = {
			limit = {
				exists = empire
				NOT = { exists = empire.holder }
				holder.top_liege = root
				empire = {
					save_temporary_scope_as = test_empire
				}
				holder.top_liege = {
					completely_controls = scope:test_empire
				}
			}
			if = {
				limit = {
					NOT = {
						empire = {
							is_in_list = additional_de_jure_empires
						}
					}
				}
				empire = {
					set_de_jure_liege_title = scope:new_title
					add_to_list = additional_de_jure_empires
				}
			}
		}

		scope:new_title = {
			set_coa = scope:old_title
			set_color_from_title = scope:old_title
			set_capital_county = scope:old_title.title_capital_county
		}
		set_primary_title_to = scope:new_title

		trigger_event = major_decisions.1103 #TO DO

		every_player = {
			if = {
				limit = {
					top_liege = scope:founder
					this != root
				}
				trigger_event = major_decisions.1104
			}
			else_if = {
				#Notify any players who lose de jure territory
				limit = {
					exists = scope:send_notifications
					this != root
					top_liege != scope:founder
					any_held_title = {
						any_in_de_jure_hierarchy = {
							continue = {
								tier > tier_empire
							}
							tier = tier_empire
							is_in_list = empires_for_notification
						}
					}
				}
				every_held_title = {
					every_in_de_jure_hierarchy = {
						continue = {
							tier > tier_empire
						}
						limit = {
							tier = tier_empire
							is_in_list = empires_for_notification
						}
						add_to_list = notification_titles
					}
				}
				if = {
					limit = {
						any_in_list = {
							list = notification_titles
							count > 0
						}
					}
					trigger_event = major_decisions.1105 #TO DO
				}
			}
		}
	}
}