﻿# on_title_destroyed = {
# 	on_actions = {
# 		counterfactuals_byzdestroyed
# 	}
# }
on_game_start = {
	on_actions = {
		counterfactuals_start
	}
}
on_county_occupied = {
	on_actions = {
		counterfactuals_mongol_sacks
		counterfactuals_jerusalem_occupied
		counterfactuals_mecca_occupied
	}
}
on_faith_conversion = {
	on_actions = {
		counterfactuals_hre_converts
	}
}
on_title_gain = {
	on_actions = {
		counterfactuals_hre_cleanup
		counterfactuals_world_conquest_transfer
		counterfactuals_stretched_thin_transfer
	}
}

on_title_lost = {
	on_actions = {
		counterfactuals_hre_cleanup
	}
}
on_war_won_attacker = {
	on_actions = {
		counterfactuals_mongol_sacks_war
		# counterfactuals_mecca_taken_in_war
	}
}
yearly_playable_pulse = {
	on_actions = {
		counterfactuals_world_conquest
		counterfactuals_world_conquest_maintenance
		counterfactuals_hre_maintenance
	}
}
on_vassal_gained = {
	on_actions = {
		delay = {
			days = 1
		}
		counterfactuals_mongols_maintenance
	}
}




counterfactuals_start = {
	effect = {
		set_global_variable = {
            name = counterfactuals_is_loaded
            value = yes
        }
		if = {
			limit = { 
				exists = global_var:counterfactuals_is_loaded
			}
			debug_log = "### Counterfactuals Loaded."
		}
	}
}
# counterfactuals_byzdestroyed = {
# 	events = {
# 		counterfactuals.101
# 	}
# }
counterfactuals_mongol_sacks = {
	trigger = {
		# Check that it's relevant so we don't fire it *all* the time
		exists = global_var:mongol_empire_has_spawned
		NOT = { exists = global_var:mongol_story_has_ended }
		OR = {
			AND = {
				exists = global_var:mongol_empire_has_spawned
				NOT = { exists = global_var:mongol_story_has_ended }
				any_owned_story = {
					story_type = story_mongol_invasion
				}
			}
			any_owned_story = {
				story_type = story_greatest_of_khans
			}
		}
		scope:county = {
			NOT = { exists = scope:county.var:counterfactuals_mongols_sacked }
		}
	}
	effect = {
		root = {
			save_scope_as = mongol_emperor
		}
	}
	events = {
		counterfactuals.201
	}
}
counterfactuals_mongol_sacks_war = {
	trigger = {
		scope:war = {
			using_cb = mongol_realm_invasion_war
		}
	}
	effect = {
		debug_log = "#### counterfactuals_mongol_sacks_war triggered ####"
		scope:attacker = {
			save_scope_as = mongol_emperor
		}
		scope:attacker = {
					every_sub_realm_county = {
						limit = {
							duchy.kingdom.empire = {
								is_in_list = target_titles
							}
							NOT = { exists = var:counterfactuals_mongols_sacked }
						}
						save_scope_as = county
						scope:attacker = {
								trigger_event = counterfactuals.201
						}
					}
					# every_vassal_or_below = {
					# 	every_sub_realm_county = {
					# 		limit = {
					# 			duchy.kingdom.empire = {
					# 				is_in_list = target_titles
					# 			}
					# 			NOT = { exists = var:counterfactuals_mongols_sacked }
					# 		}
					# 		save_scope_as = county
					# 		scope:attacker = {
					# 				trigger_event = counterfactuals.201
					# 		}
					# 	}
					# }
				}
	}
}
counterfactuals_mongols_maintenance = {
	trigger = {
		OR = {
			AND = {
				exists = global_var:mongol_empire_has_spawned
				NOT = { exists = global_var:mongol_story_has_ended }
				any_owned_story = {
					story_type = story_mongol_invasion
				}
			}
			any_owned_story = {
				story_type = story_greatest_of_khans
			}
		}
	}
	effect = {
		every_realm_county = {
			if = {
				limit = {
					NOT = { exists = scope:county.var:counterfactuals_mongols_sacked }
				 }
				set_variable = {
					name = counterfactuals_mongols_sacked
					value = yes
				}
			}
		}
	}
}

counterfactuals_hre_converts = {
	trigger = {
		root = title:e_hre.holder
		NOT = {
			faith = faith:catholic
		}
		exists = faith:catholic.religious_head
	}
	effect = {
		root = {
			save_scope_as = hre_emperor
		}
		faith:catholic = {
			religious_head = {
				save_scope_as = pope
			}
		}
	}
	events = {
		delay = { days = 10 }
		counterfactuals.301
	}
}
counterfactuals_hre_cleanup = {
	trigger = {
		scope:title = title:e_hre
	}
	effect = {
		if = {
			#Deposed but still alive
			limit = { 
				root = {
					is_alive = yes
				}
			}
			remove_character_modifier = counterfactuals_false_emperor_modifier
		}
		if = {
			limit = {
				OR = {
					#Dismantled Papacy
					NOT = {
						exists = faith:catholic.religious_head
					}
					#Taken by a Catholic
					scope:new_holder = {
						faith = faith:catholic
					}
				} 
			}
			#Restore legitimate HRE
			scope:title = {
				reset_title_name = yes
			}
			scope:new_holder = {
				remove_character_modifier = counterfactuals_false_emperor_modifier
			}
			#Restoration notification event
		}

		if = {
			limit = { 
				#Still an apostate?
				scope:new_holder = {
					NOT = {
						faith = faith:catholic
					}
				}
				exists = faith:catholic.religious_head
			}
			#Ensure the title is apostate version + modifiers apply to new holder
			scope:title = {
				set_title_name = e_german_apostate
			}
			scope:new_holder = {
				add_character_modifier = {
					modifier = counterfactuals_false_emperor_modifier
				}
				every_vassal_or_below = {
					limit = {
						faith.religious_head = faith:catholic.religious_head
						NOT = {
							has_trait = cynical
						}
					}
					add_opinion = {
						target = title:e_hre.holder
						modifier = counterfactuals_false_emperor_opinion
						opinion = -50
					}
				}
				faith:catholic.religious_head = {
					add_opinion = {
						target = prev
						modifier = counterfactuals_false_emperor_opinion
						opinion = -100
					}
				}
			}
		}
	}
}
counterfactuals_hre_maintenance = {
	trigger = {
		has_character_modifier = counterfactuals_false_emperor_modifier
	}
	effect = {
		if = {
			limit = {
				OR = {
					#Dismantled Papacy
					NOT = {
						exists = faith:catholic.religious_head
					}
					#Taken by a Catholic
					root = {
						faith = faith:catholic
					}
					#Lost HRE
					NOT = {
						root = title:e_hre.holder
					}
				} 
			}
			#Restore legitimate HRE
			title:e_hre = {
				reset_title_name = yes
			}
			root = {
				remove_character_modifier = counterfactuals_false_emperor_modifier
			}
		}
	}
}
counterfactuals_world_conquest = {
	trigger = {
		is_ai = no
	}
	events = {
		counterfactuals.401
		counterfactuals.402
	}
}

counterfactuals_world_conquest_transfer = {
	trigger = {
		scope:previous_holder ?= {
			OR = {
				has_character_flag = counterfactuals_world_conquest
				has_character_modifier = counterfactuals_world_conquest_modifier
			}
		}
	}
	effect = {
		root = {
			add_character_flag = counterfactuals_world_conquest
			add_character_modifier = {
				modifier = counterfactuals_world_conquest_modifier
			}
		}
	}
}

counterfactuals_stretched_thin_transfer = {
	trigger = {
		scope:previous_holder ?= {
			OR = {
				has_character_modifier = counterfactuals_vast_empire_modifier
				has_character_flag = counterfactuals_stretched_thin
			}
		}
	}
	effect = {
		root = {
			add_character_flag = counterfactuals_stretched_thin
			add_character_modifier = {
				modifier = counterfactuals_vast_empire_modifier
			}
		}
	}
}

#Periodically check that the modifiers are still appropiate
counterfactuals_world_conquest_maintenance = {
	trigger = {
		OR = {
			has_character_flag = counterfactuals_stretched_thin
			has_character_flag = counterfactuals_world_conquest
		}
	}
	effect = {
		#Realm shrank? downgrade modifier
		if = {
			limit = { 
				has_character_modifier = counterfactuals_world_conquest_modifier
				realm_size < 2500
			}
			remove_character_modifier = counterfactuals_world_conquest_modifier
		}
		#Realm shrank even more? remove modifier
		if = {
			limit = { 
				has_character_modifier = counterfactuals_vast_empire_modifier
				realm_size < 1400
			}
			remove_character_modifier = counterfactuals_vast_empire_modifier
		}
		#Realm returned to 2500? re-add modifier
		if = {
			limit = { 
				NOT = {
					has_character_modifier = counterfactuals_world_conquest_modifier
				}
				realm_size > 2500
			}
			remove_character_modifier = counterfactuals_vast_empire_modifier
			add_character_modifier = {
				modifier = counterfactuals_world_conquest_modifier
			}
		}
		#Realm returned to 1500? re-add modifier
		if = {
			limit = { 
				NOR = {
					has_character_modifier = counterfactuals_vast_empire_modifier
					has_character_modifier = counterfactuals_world_conquest_modifier
				}
				realm_size > 1500
			}
			add_character_modifier = {
				modifier = counterfactuals_vast_empire_modifier
			}
		}
		#Somehow ended up a vassal or something? clear modifiers.
		if = {
			limit = { 
				is_independent_ruler = no
			}
			remove_character_modifier = counterfactuals_vast_empire_modifier
			remove_character_modifier = counterfactuals_world_conquest_modifier
			remove_character_flag = counterfactuals_stretched_thin
			remove_character_flag = counterfactuals_world_conquest
		}
	}
}

counterfactuals_jerusalem_occupied = {
	trigger = {
		NOT = {
			has_global_variable = counterfactuals_jerusalem_taken
		}
		scope:county = title:c_jerusalem
		faith = {
			religion_tag = christianity_religion
		}
		scope:previous_controller = {
			NOT = {
				faith = {
					religion_tag = christianity_religion
				}
			}
		}
		exists = faith.great_holy_war
		is_in_ongoing_great_holy_war = yes
		is_pledged_ghw_attacker = yes
	}
	effect = {
		trigger_event = {
			id = counterfactuals.501
			days = 1
		}
	}
}
counterfactuals_mecca_occupied = {
	trigger = {
		NOT = {
			has_global_variable = counterfactuals_mecca_taken
		}
		scope:county = {
			this = title:c_mecca
			faith = {
				religion_tag = islam_religion
			}
		}
		faith = {
			religion_tag = christianity_religion
		}
		scope:previous_controller = {
			NOT = {
				faith = {
					religion_tag = christianity_religion
				}
			}
		}
	}
	effect = {
		trigger_event = {
			id = counterfactuals.510
			days = 1
		}
	}
}

counterfactuals_mecca_taken_in_war = {
	trigger = {
		NOT = {
			has_global_variable = counterfactuals_mecca_taken
		}
		faith = {
			religion_tag = christianity_religion
		}
		any_realm_county = {
			this = title:c_mecca
			faith = {
				religion_tag = islam_religion
			}
		}
	}
	effect = {
		trigger_event = {
			id = counterfactuals.510
			days = 1
		}
	}
}

#HRT compatibility
HRT_CF_action = { }