﻿# Like on_game_start, except it is called once the host (or player, in single player) exits the lobby. Good for anything where you need to know who the players are, or what the game rules are
on_game_start_after_lobby = {
	on_actions = {
		historicinvasions_on_game_start_after_lobby
		historicinvasions_immediate_invasions_on_game_start_after_lobby
	}
}

historicinvasions_on_game_start_after_lobby = {
	trigger = { always = yes }
	effect = {
		# compatibility information
		every_player = {
			if = {
				limit = { always = no }
				trigger_event = historicinvasions_event.9500
			}
		}

		# teutonic order
		if = {
			limit = { has_game_rule = default_knights_invasion }
			# Pomerania border
			title:c_slupsk = { set_de_jure_liege_title = title:d_pommerania }
			title:c_miastko = { set_de_jure_liege_title = title:d_pommerania }
			title:d_pomerelia = { set_de_jure_liege_title = title:k_lithuania }
		}

		### More German Kingdoms compatibility; suppress error logs, variable blocks MGK decision from appearing before the Ludolfinger story is completed
		if = {
			limit = {
				has_game_rule = default_ludolfinger_invasion
			}
			set_global_variable = {
				name = historicinvasions_ludolfinger_enabled
				value = yes
			}
		}

		# prevent base game on_action to trigger Seljuk invasion
		if = {
			limit = { NOT = { has_game_rule = never_seljuk_invasion } }
			# block base game invasion
			set_global_variable = { # set base game variable
				name = seljuk_invasion_happened
				value = yes
			}
			debug_log = "set global variable 'seljuk_invasion_happened'"
		}
		# prevent base game on_action to trigger Almohad invasion
		if = {
			limit = { has_game_rule = default_almohad_invasion }
			# block base game invasion
			set_global_variable = { # set base game variable
				name = spawned_almohads
				value = yes
			}
			debug_log = "set global variable 'spawned_almohads'"
		}
		# prevent base game on_action to trigger Mongol / Borjigin invasion
		if = {
			limit = { NOT = { has_game_rule = never_mongol_invasion } }
			# block base game invasion
			set_global_variable = { # set base game variable
				name = mongols_have_appeared
				value = yes
			}
			debug_log = "set global variable 'mongols_have_appeared'"
		}

		# 04 Seljuk Rûm
		if = {
			limit = {
				NOT = { has_game_rule = never_seljuk_rum_invasion }
			}
			title:k_rum = { set_title_name = hi_k_rum }
		}

		# 21 ottoman
		if = {
			limit = {
				NOT = { has_game_rule = never_ottoman_invasion }
			}
			title:k_ottoman = { set_coa = hi_k_ottoman }
		}

		### de jure changes
		# Arabia
		if = {
			limit = {
				NOT = { has_game_rule = disable_divide_arabia }
				exists = title:k_hejaz
				exists = title:k_bahrain
			}
			divide_arabia_effect = yes
		}

		# misc. historic changes
		if = {
			limit = { has_game_rule = enable_bookmark_de_jure_changes }
			bookmark_de_jure_changes_effect = yes
			# cuman empire // terteroba
			if = {
				limit = { has_game_rule = default_terteroba_invasion }
				bookmark_cuman-kipchak_empire_effect = yes
			}
		}

		# More Bookmarks
		#has_global_variable = mongol_story_has_ended
		#game_start_date >= 1270.1.1
	}
}

historicinvasions_immediate_invasions_on_game_start_after_lobby = {
	trigger = {
		always = yes
	}
	effect = {
		### 06 Ayyubid
		# if = {
		# 	limit = {
		# 		game_start_date >= 1171.1.1 # yearly trigger only checks if game start date < 1171.1.1
		# 		game_start_date < 1260.1.1 # end of Ayyubid dynasty
		# 		NOT = { has_game_rule = never_ayyubid_invasion }
		# 	}
		# 	debug_log = "attempting to trigger ayyubid story on game start"
		# 	trigger_event = {
		# 		id = ayyubid_invasion.0001
		# 		days = 0
		# 	}
		# }
		### 08 Almoravid
		if = {
			limit = {
				game_start_date >= 1050.1.1 # yearly trigger only checks if game start date < 1050.1.1
				game_start_date < 1100.1.1 # almohads (follow-up invasion) can spawn past 1121
				NOT = { has_game_rule = never_almoravid_invasion }
			}
			debug_log = "attempting to trigger almoravid story on game start"
			trigger_event = {
				id = almoravid_invasion.0001
				days = 0
			}
		}
		### 09 Almohad
		# if = {
		# 	limit = {
		# 		game_start_date >= 1121.1.1 # yearly trigger only checks if game start date < 1121.1.1
		# 		game_start_date < 1269.1.1 # end of Almohad dynasty
		# 		NOT = { has_game_rule = never_almohad_invasion }
		# 	}
		# 	debug_log = "attempting to trigger almohad story on game start"
		# 	trigger_event = {
		# 		id = almohad_invasion.0001
		# 		days = 0
		# 	}
		# }
		### 10 Ghurid
		if = {
			limit = {
				game_start_date >= 1152.1.1 # yearly trigger only checks if game start date < 1152.1.1
				game_start_date < 1215.1.1 # end of Ghurid dynasty
				NOT = { has_game_rule = never_ghurid_invasion }
			}
			debug_log = "attempting to trigger ghurid story on game start"
			trigger_event = {
				id = ghurid_invasion.0001
				days = 0
			}
		}
		### 19 Anushtiginid
		if = {
			limit = {
				game_start_date >= 1172.1.1 # yearly trigger only checks if game start date < 1172.1.1
				game_start_date < 1206.1.1 # Mongol invasion; 1231 end of Anushtiginid dynasty
				NOT = { has_game_rule = never_anushtiginid_invasion }
			}
			debug_log = "attempting to trigger anushtiginid story on game start"
			trigger_event = {
				id = anushtiginid_invasion.0001
				days = 0
			}
		}
	}
}