﻿# Called every January 1st
# Root does not exist
yearly_global_pulse = {
	on_actions = {
		timurid_invasion_on_action # 1370
		fatimid_invasion_on_action # 909
		ghaznavid_invasion_on_action # 977
		seljuk_persia_invasion_on_action # 1037
		seljuk_rum_invasion_on_action # 1077-ish?
		ziyarid_invasion_on_action # 931
		ayyubid_invasion_on_action # 1171
		balgarsko_invasion_on_action # 912
		almoravid_invasion_on_action # 1050
		almohad_invasion_on_action # 1121
		ghurid_invasion_on_action # 1152
		ludolfinger_invasion_on_action # 911
		hauteville_invasion_on_action # 1038
		jannabid_uprising_on_action # 899
		#vlaanderen_invasion_on_action # 1204; disabled since mod version 1.36.10 or game version 1.13.*
		knights_invasion_on_action # 1147
		terteroba_invasion_on_action # ~950 // 10th century
		rurikid_invasion_on_action # 879
		chola_invasion_on_action # 920
		anushtiginid_invasion_on_action # 1172
		yelu_invasion_on_action # 1124
		ottoman_invasion_on_action # 1299
		keita_invasion_on_action # 1230
		#al-qays_invasion_on_action # 869; disabled since mod version 1.34.0 or game version 1.11.*
		knytling_invasion_on_action # 1016
		gediminid_invasion_on_action # 1316
		yngling_invasion_on_action # 870
		#sabbah_invasion_on_action # 1090; disabled since mod version 1.36.13 or game version 1.13.*
		#jabalid_invasion_on_action # 1162; disabled since mod version 1.36.13 or game version 1.13.*
		qarghuyid_invasion_on_action # 968; disabled by default since mod version 1.36.13
		#vivar_invasion_on_action # 1094; disabled since mod version 1.36.10 or game version 1.13.*
		normandie_invasion_on_action # 1066
		piast_invasion_on_action # 960
		sangama_invasion_on_action # 1336
		mamluk_invasion_arabia_on_action # 1250
		mamluk_invasion_rajastan_on_action # 1206
		#bearn_invasion_on_action # 1087; disabled since mod version 1.36.12 or game version 1.13.*
		eldiguzid_invasion_on_action # 1136; disabled by default since mod version 1.36.13
		borjigin_invasion_on_action # 1206
		borjigin_events_on_action # notification events; 1162 & 1295
		qara_qoyunlu_invasion_on_action # 1405
		aq_qoyunlu_invasion_on_action # 1467
		wanggiyan_invasion_on_action # 1115
		rajasa_invasion_on_action # 1293
		liao_invasion_on_action # 916
		ahom_invasion_on_action # 1228

		turkish_migrations_on_action # 1080

		attila_invasion_on_action # 370 or random
		argead_invasion_on_action # random
		maurya_invasion_on_action # random

		song_invasion_on_action # 1279
		zunbil_invasion_on_action # 893
		komnenos_invasion_on_action # 1091

		historicinvasions_base_game_compatibility_on_action
	}
}

timurid_invasion_on_action = { # 1370
	trigger = {
		current_date > 1355.1.1
		current_date < 1455.1.1
		game_start_date < 1370.1.1
		NOT = { has_game_rule = never_timurid_invasion }
		OR = {
			has_game_rule = never_mongol_invasion
			has_global_variable = mongol_story_has_ended
			game_start_date >= 1259.1.1
		}
		exists = title:e_timurid # update compatibility - new titles in old saves return false
		title:e_timurids = { is_title_created = no } # base game title
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:timurid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1355.1.1
			}
			modifier = {
				add = 3
				current_date > 1365.1.1
			}
			modifier = {
				add = 7
				current_date > 1370.1.1
			}
			modifier = {
				add = 9
				current_date > 1380.1.1
			}
			debug_log = "attempting to trigger timurid story soon"
			trigger_event = {
				id = timurid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

fatimid_invasion_on_action = { # 909
	trigger = {
		current_date > 890.1.1
		current_date < 990.1.1
		game_start_date < 900.1.1
		NOT = { has_game_rule = never_fatimid_invasion }
		exists = title:e_fatimid # update compatibility - new titles in old saves return false
		title:d_shiite = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:fatimid_africa_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 890.1.1
			}
			modifier = {
				add = 3
				current_date > 895.1.1
			}
			modifier = {
				add = 5
				current_date > 900.1.1
			}
			modifier = {
				add = 15
				current_date > 910.1.1
			}
			modifier = {
				add = 30
				current_date > 920.1.1
			}
			debug_log = "attempting to trigger fatimid story 1 soon"
			trigger_event = {
				id = fatimid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

ghaznavid_invasion_on_action = { # 977
	trigger = {
		current_date > 950.1.1
		current_date < 1050.1.1
		game_start_date < 977.1.1
		NOT = { has_game_rule = never_ghaznavid_invasion }
		exists = title:e_ghaznavid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ghaznavid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 950.1.1
			}
			modifier = {
				add = 5
				current_date > 960.1.1
			}
			modifier = {
				add = 15
				current_date > 970.1.1
			}
			modifier = {
				add = 30
				current_date > 975.1.1
			}
			modifier = {
				add = 35
				current_date > 980.1.1
			}
			debug_log = "attempting to trigger ghaznavid story soon"
			trigger_event = {
				id = ghaznavid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

# as of 1.11, this is also part of the base game
seljuk_persia_invasion_on_action = { # 1037
	trigger = {
		current_date > 945.1.1
		current_date < 1100.1.1
		game_start_date < 1035.1.1
		NOT = { has_game_rule = never_seljuk_invasion }
		exists = title:e_seljuk # update compatibility - new titles in old saves return false
		NAND = { # seljuks
			has_global_variable = seljuk_invasion_happened # always set; blocks base game on_action from triggering
			is_target_in_global_variable_list = { # is set in story or when base game invasion is triggered
				name = historicinvasions_story_started
				target = flag:seljuk_story_started
			}
		}
		NOT = {
			exists = struggle:persian_struggle # don't allow it to happen during the struggle
		}
	}
	effect = {
		### modded mechanics
		if = {
			limit = { has_game_rule = default_seljuk_invasion }
			random = {
				chance = 0
				modifier = {
					add = 1
					current_date > 1020.1.1
				}
				modifier = {
					add = 2
					current_date > 1025.1.1
				}
				modifier = {
					add = 15
					current_date > 1030.1.1
				}
				modifier = {
					add = 20
					current_date > 1035.1.1
				}
				modifier = {
					add = 25
					current_date > 1037.1.1
				}
				debug_log = "attempting to trigger seljuk story soon"
				trigger_event = {
					id = seljuk_invasion.0001
					days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
				}
			}
		}
		### base game mechanics
		else_if = {
			limit = { has_game_rule = random_seljuk_invasion }
			if = {
				limit = {
					current_date < 1066.1.1
					current_date >= 945.1.1
				}
				random = {
					chance = fp3_seljuk_chance_of_appearance
					modifier = {
						add = 5
						current_date > 965.1.1
					}
					modifier = {
						add = 5
						current_date > 985.1.1
					}
					modifier = {
						add = 5
						current_date > 1005.1.1
					}
					modifier = {
						factor = 0.25
						OR = {
							culture:turkish = { culture_number_of_counties = 0 }
							faith:ashari = { has_followers_trigger = no }
						}
					}

					# I usually set the variables within the story cycle, but the base game set them immediately
					add_to_global_variable_list = { # set modded variable
						name = historicinvasions_story_started
						target = flag:seljuk_story_started
					}
					add_to_global_variable_list = { # set modded variable
						name = historicinvasions_story_ended
						target = flag:seljuk_story_ended
					}
					set_global_variable = { # set base game variable
						name = seljuk_invasion_happened
						value = yes
					}

					debug_log = "Attempting to spawn seljuk invasion within 240 days"
					trigger_event = {
						id = fp3_story_cycle_seljuks_invasion_events.0001
						days = { 2 240 }
					}
				}
			}
		}
	}
}

seljuk_rum_invasion_on_action = { # 1077-ish?
	trigger = {
		current_date > 1070.1.1
		current_date < 1120.1.1
		OR = {
			AND = {
				game_start_date < 1035.1.1
				has_global_variable = seljuk_invasion_happened
				is_target_in_global_variable_list = {
					name = historicinvasions_story_started
					target = flag:seljuk_story_started
				}
			}
			AND = {
				game_start_date >= 1035.1.1
				title:e_persia = { is_title_created = yes }
				title:e_persia.holder.house = house:house_seljuk
			}
		}
		OR = { # Muslim Enhancements compatibility check
			game_start_date < 1066.9.15 # use my invasion
			AND = { # use invasion by ME
				game_start_date >= 1066.9.15
				ME_is_loaded = no
			}
		}
		NOT = { has_game_rule = never_seljuk_rum_invasion }
		exists = title:k_rum
		title:k_rum = { is_title_created = no }
		NOT = {
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:seljuk_rum_have_appeared
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 5
				current_date > 1070.1.1
			}
			modifier = {
				add = 15
				current_date > 1075.1.1
			}
			modifier = {
				add = 25
				current_date > 1080.1.1
			}
			modifier = {
				add = 30
				current_date > 1085.1.1
			}
			modifier = {
				add = 25
				current_date > 1090.1.1
			}
			debug_log = "attempting to trigger seljuk story RUM invasion soon"
			trigger_event = {
				id = seljuk_invasion.0011
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0002
			}
		}
	}
}

ziyarid_invasion_on_action = { # 931
	trigger = {
		current_date > 920.1.1
		current_date < 1020.1.1
		game_start_date < 931.1.1
		NOT = { has_game_rule = never_ziyarid_invasion }
		exists = title:k_ziyarid # update compatibility - new titles in old saves return false
		NOT = {
			is_target_in_global_variable_list = { # is set in story
				name = historicinvasions_story_started
				target = flag:ziyarid_story_started
			}
		}
		NAND = { # seljuks
			has_global_variable = seljuk_invasion_happened
			is_target_in_global_variable_list = { # is set in story
				name = historicinvasions_story_started
				target = flag:seljuk_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 920.1.1
			}
			modifier = {
				add = 4
				current_date > 925.1.1
			}
			modifier = {
				add = 15
				current_date > 930.1.1
			}
			modifier = {
				add = 20
				current_date > 935.1.1
			}
			modifier = {
				add = 25
				current_date > 940.1.1
			}
			debug_log = "attempting to trigger ziyarid story soon"
			trigger_event = {
				id = ziyarid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

ayyubid_invasion_on_action = { # 1171
	trigger = {
		current_date > 1137.1.1
		current_date < 1250.1.1
		game_start_date < 1171.1.1
		NOT = { has_game_rule = never_ayyubid_invasion }
		exists = title:e_ayyubid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ayyubid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1137.1.1
			}
			modifier = {
				add = 5
				current_date > 1160.1.1
			}
			modifier = {
				add = 10
				current_date > 1170.1.1
			}
			modifier = {
				add = 30
				current_date > 1180.1.1
			}
			debug_log = "attempting to trigger ayyubid story soon"
			trigger_event = {
				id = ayyubid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

balgarsko_invasion_on_action = { # 912
	trigger = {
		current_date > 910.1.1 # start of further conquests in 912
		current_date < 1017.1.1 # subjugation by ERE
		#game_start_date > 899.1.1 # loses Hungary
		NOT = { has_game_rule = never_balgarsko_invasion }
		exists = title:e_bulgaria # update compatibility - new titles in old saves return false
		title:e_bulgaria = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:balgarsko_story_started
			}
		}
	}
	effect = {
		if = {
			limit = {
				is_target_in_global_variable_list = {
					name = unavailable_unique_decisions
					target = flag:flag_launched_hungarian_migration
				}
			}
			random = {
				chance = 0
				modifier = {
					add = 5
					current_date > 910.1.1
				}
				modifier = {
					add = 10
					current_date > 920.1.1
				}
				modifier = {
					add = 15
					current_date > 930.1.1
				}
				modifier = {
					add = 20
					current_date > 940.1.1
				}
				debug_log = "attempting to trigger balgarsko story / bulgaria story soon"
				trigger_event = {
					id = balgarsko_invasion.0001
					days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
				}
			}
		}
	}
}

almoravid_invasion_on_action = { # 1050
	trigger = {
		current_date > 1040.1.1
		current_date < 1140.1.1
		game_start_date < 1050.1.1
		NOT = { has_game_rule = never_almoravid_invasion }
		exists = title:e_almoravid # update compatibility - new titles in old saves return false
		OR = { # make sure that fatimids and almoravid don't bother each other
			has_game_rule = never_fatimid_invasion
			game_start_date >= 900.1.1
			AND = {
				has_game_rule = default_fatimid_invasion
				OR = {
					is_target_in_global_variable_list = {
						name = historicinvasions_story_ended
						target = flag:fatimid_africa_story_ended
					}
					is_target_in_global_variable_list = {
						name = historicinvasions_migrations
						target = flag:fatimid_migration_complete
					}
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:almoravid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1040.1.1
			}
			modifier = {
				add = 5
				current_date > 1045.1.1
			}
			modifier = {
				add = 10
				current_date > 1050.1.1
			}
			modifier = {
				add = 30
				current_date > 1060.1.1
			}
			debug_log = "attempting to trigger almoravid story soon"
			trigger_event = {
				id = almoravid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

almohad_invasion_on_action = { # 1121
	trigger = {
		current_date > 1100.1.1
		current_date < 1200.1.1
		game_start_date < 1121.1.1
		NOT = { has_game_rule = never_almohad_invasion }
		exists = title:e_almohad # update compatibility - new titles in old saves return false
		OR = { # make sure that almoravids and almohads don't bother each other
			has_game_rule = never_almoravid_invasion
			AND = {
				has_game_rule = default_almoravid_invasion
				OR = {
					game_start_date >= 1050.1.1
					is_target_in_global_variable_list = {
						name = historicinvasions_story_ended
						target = flag:almoravid_story_ended
					}
				}
			}
		}
		NAND = {
			has_global_variable = spawned_almohads
			is_target_in_global_variable_list = { # is set in story
				name = historicinvasions_story_started
				target = flag:almohad_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1100.1.1
			}
			modifier = {
				add = 5
				current_date > 1110.1.1
			}
			modifier = {
				add = 10
				current_date > 1120.1.1
			}
			modifier = {
				add = 30
				current_date > 1130.1.1
			}
			debug_log = "attempting to trigger almohad story soon"
			trigger_event = {
				id = almohad_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

ghurid_invasion_on_action = { # 1152
	trigger = {
		current_date > 1140.1.1
		current_date < 1240.1.1
		game_start_date < 1152.1.1
		NOT = { has_game_rule = never_ghurid_invasion }
		exists = title:e_ghurid # update compatibility - new titles in old saves return false
		OR = { # ghaznavid
					has_game_rule = never_ghaznavid_invasion
					AND = {
						has_game_rule = default_ghaznavid_invasion
						OR = {
							game_start_date >= 977.1.1
							is_target_in_global_variable_list = {
								name = historicinvasions_story_ended
								target = flag:ghaznavid_story_ended
							}
						}
					}
				}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ghurid_story_started
			}
		}	
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1140.1.1
			}
			modifier = {
				add = 5
				current_date > 1150.1.1
			}
			modifier = {
				add = 10
				current_date > 1160.1.1
			}
			modifier = {
				add = 30
				current_date > 1170.1.1
			}
			debug_log = "attempting to trigger ghurid story soon"
			trigger_event = {
				id = ghurid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

# event skips invasions if HRE is created or if Carolingian borders were restored
ludolfinger_invasion_on_action = { # 911ish - 962ish
	trigger = {
		current_date > 910.1.1
		current_date < 1050.1.1
		game_start_date < 962.2.2 # coronation of Otto as roman emperor
		NOT = { has_game_rule = never_ludolfinger_invasion }
		exists = title:e_hre
		title:e_germany = { is_title_created = no }
		NOT = { # is set in story; flagged as complete if the HRE is created
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ludolfinger_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 910.1.1 # 911 first non-carolingian ruler is elected
			}
			modifier = {
				add = 3
				current_date > 920.1.1
			}
			modifier = {
				add = 5
				current_date > 940.1.1
			}
			modifier = {
				add = 7
				current_date > 960.1.1
			}
			modifier = {
				add = 10
				current_date > 980.1.1
			}
			modifier = {
				add = 15
				current_date > 1000.1.1
			}
			debug_log = "attempting to trigger ludolfinger story soon"
			trigger_event = {
				id = ludolfinger_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

hauteville_invasion_on_action = { # 1038
	trigger = {
		current_date > 1025.1.1
		current_date < 1125.1.1
		game_start_date < 1038.1.1
		NOT = { has_game_rule = never_hauteville_invasion }
		exists = title:k_hauteville # update compatibility - new titles in old saves return false
		OR = { # make sure that fatimids and normans don't bother each other
			has_game_rule = never_fatimid_invasion
			AND = {
				has_game_rule = default_fatimid_invasion
				OR = {
					is_target_in_global_variable_list = {
						name = historicinvasions_story_ended
						target = flag:fatimid_africa_story_ended
					}
					is_target_in_global_variable_list = {
						name = historicinvasions_migrations
						target = flag:fatimid_migration_complete
					}
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:hauteville_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1025.1.1
			}
			modifier = {
				add = 3
				current_date > 1040.1.1
			}
			modifier = {
				add = 5
				current_date > 1050.1.1
			}
			modifier = {
				add = 7
				current_date > 1060.1.1
			}
			modifier = {
				add = 10
				current_date > 1080.1.1
			}
			debug_log = "attempting to trigger hauteville story soon"
			trigger_event = {
				id = hauteville_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

jannabid_uprising_on_action = { # 899
	trigger = {
		current_date > 895.1.1
		current_date < 1020.1.1
		game_start_date < 899.1.1
		NOT = { has_game_rule = never_jannabid_invasion }
		exists = title:k_jannabid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:jannabid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 895.1.1
			}
			modifier = {
				add = 7
				current_date > 900.1.1
			}
			modifier = {
				add = 15
				current_date > 910.1.1
			}
			debug_log = "attempting to trigger jannabid story soon"
			trigger_event = {
				id = jannabid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

vlaanderen_invasion_on_action = { # 1204
	trigger = {
		current_date > 1190.1.1
		current_date < 1290.1.1
		game_start_date < 1204.1.1
		NOT = { has_game_rule = never_vlaanderen_invasion }
		faith:catholic = {
			has_doctrine = doctrine_spiritual_head # papacy has not been dismantled
			has_doctrine = special_doctrine_ecumenical_christian # schism has not been mended
			religion = { exists = var:variable_ghw_unlocked } # age of crusades
		}
		exists = title:e_nikaea # update compatibility - new titles in old saves return false
		exists = title:e_vlaanderen # latin empire
		title:e_latin_empire = { is_title_created = no } # base game title
		title:h_roman_empire = { is_title_created = no }
		title:e_byzantium = { is_title_created = yes }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:vlaanderen_story_started
			}
		}
		
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1190.1.1
			}
			modifier = {
				add = 5
				current_date > 1195.1.1
			}
			modifier = {
				add = 7
				current_date > 1200.1.1
			}
			modifier = {
				add = 10
				current_date > 1205.1.1
			}
			modifier = {
				add = 15
				current_date > 1210.1.1
			}
			modifier = {
				add = 20
				current_date > 1220.1.1
			}
			debug_log = "attempting to trigger vlaanderen story soon"
			trigger_event = {
				id = vlaanderen_invasion.0001
				days = { 120 180 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

knights_invasion_on_action = { # 1230 (1191+)
	trigger = {
		current_date > 1191.1.1
		current_date < 1330.1.1
		game_start_date < 1230.1.1
		NOT = { has_game_rule = never_knights_invasion }
		faith:catholic = {
			has_doctrine = doctrine_spiritual_head # papacy has not been dismantled
			has_doctrine = special_doctrine_ecumenical_christian # schism has not been mended
			religion = { exists = var:variable_ghw_unlocked } # age of crusades
		}
		exists = title:e_teutonic_state # update compatibility - new titles in old saves return false
		exists = title:k_bassenheim # update compatibility - new titles in old saves return false; Teutonic Order
		exists = title:k_balk # update compatibility - new titles in old saves return false; Livonian Order
		NOR = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:bassenheim_story_started
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:balk_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1191.1.1 # hospice is established in Acre
			}
			modifier = {
				add = 1
				current_date > 1198.1.1 # hospice is turned into a military order
			}
			modifier = {
				add = 3
				current_date > 1220.1.1
			}
			modifier = {
				add = 10
				current_date > 1225.1.1
			}
			modifier = {
				add = 30
				current_date > 1230.1.1
			}
			debug_log = "attempting to trigger knights story soon (bassenheim / teutonic & balk / livonian)"
			trigger_event = { # teutonic order
				id = bassenheim_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
			trigger_event = { # livonian order
				id = balk_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

terteroba_invasion_on_action = { # 10th century
	trigger = {
		current_date > 900.1.1
		current_date < 1050.1.1
		game_start_date < 1000.1.1
		NOT = { has_game_rule = never_terteroba_invasion }
		exists = title:e_terteroba # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:terteroba_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 900.1.1
			}
			modifier = {
				add = 3
				current_date > 910.1.1
			}
			modifier = {
				add = 5
				current_date > 920.1.1
			}
			modifier = {
				add = 7
				current_date > 930.1.1
			}
			modifier = {
				add = 9
				current_date > 940.1.1
			}
			modifier = {
				add = 11
				current_date > 940.1.1
			}
			modifier = {
				add = 13
				current_date > 950.1.1
			}
			modifier = {
				add = 15
				current_date > 960.1.1
			}
			modifier = {
				add = 30
				current_date > 975.1.1
			}
			debug_log = "attempting to trigger terteroba story soon"
			trigger_event = {
				id = terteroba_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

rurikid_invasion_on_action = { # 879
	trigger = {
		current_date > 860.1.1
		current_date < 960.1.1
		game_start_date < 879.1.1
		NOT = { has_game_rule = never_rurikid_invasion }
		exists = title:e_rurikid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:rurikid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 860.1.1
			}
			modifier = {
				add = 7
				current_date > 870.1.1
			}
			modifier = {
				add = 15
				current_date > 880.1.1
			}
			modifier = {
				add = 30
				current_date > 890.1.1
			}
			debug_log = "attempting to trigger rurikid story soon"
			trigger_event = {
				id = rurikid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

chola_invasion_on_action = { # 920
	trigger = {
		current_date > 900.1.1
		current_date < 1000.1.1
		game_start_date < 920.1.1
		NOT = { has_game_rule = never_chola_invasion }
		exists = title:e_chola # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:chola_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 900.1.1
			}
			modifier = {
				add = 7
				current_date > 910.1.1
			}
			modifier = {
				add = 15
				current_date > 920.1.1
			}
			modifier = {
				add = 30
				current_date > 930.1.1
			}
			debug_log = "attempting to trigger chola story soon"
			trigger_event = {
				id = chola_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

anushtiginid_invasion_on_action = { # 1172
	trigger = {
		current_date > 1140.1.1
		current_date < 1240.1.1
		game_start_date < 1172.1.1
		NOT = { has_game_rule = never_anushtiginid_invasion }
		exists = title:e_anushtiginid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:anushtiginid_story_started
			}
		}
		NAND = { # mongols
			has_global_variable = mongols_have_appeared
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:borjigin_story_started
			}
		}
	}
	effect = {
		random = { # kind of tight because the Mongols will arrive soon
			chance = 0
			modifier = {
				add = 1
				current_date > 1140.1.1
			}
			modifier = {
				add = 4
				current_date > 1160.1.1
			}
			modifier = {
				add = 15
				current_date > 1165.1.1
			}
			modifier = {
				add = 30
				current_date > 1170.1.1
			}
			modifier = {
				add = 50
				current_date > 1175.1.1
			}
			debug_log = "attempting to trigger anushtiginid story soon"
			trigger_event = {
				id = anushtiginid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

yelu_invasion_on_action = { # 1124
	trigger = {
		current_date > 1100.1.1
		current_date < 1200.1.1
		game_start_date < 1124.1.1
		NOT = { has_game_rule = never_yelu_invasion }
		exists = title:e_yelu # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:yelu_story_started
			}
		}
		NAND = { # mongols
			has_global_variable = mongols_have_appeared
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:borjigin_story_started
			}
		}	
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1100.1.1
			}
			modifier = {
				add = 7
				current_date > 1110.1.1
			}
			modifier = {
				add = 15
				current_date > 1120.1.1
			}
			modifier = {
				add = 30
				current_date > 1130.1.1
			}
			debug_log = "attempting to trigger yelu story soon"
			trigger_event = {
				id = yelu_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

ottoman_invasion_on_action = { # 1299
	trigger = {
		current_date > 1250.1.1
		current_date < 1350.1.1
		game_start_date < 1299.1.1
		NOT = { has_game_rule = never_ottoman_invasion }
		exists = title:e_ottoman # update compatibility - new titles in old saves return false
		title:k_ottoman = { is_title_created = no } # base game title
		OR = {
			has_game_rule = never_mongol_invasion
			has_global_variable = mongol_story_has_ended
			game_start_date >= 1259.1.1
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ottoman_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1250.1.1
			}
			modifier = {
				add = 7
				current_date > 1280.1.1
			}
			modifier = {
				add = 15
				current_date > 1300.1.1
			}
			modifier = {
				add = 30
				current_date > 1310.1.1
			}
			debug_log = "attempting to trigger ottoman story soon"
			trigger_event = {
				id = ottoman_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

keita_invasion_on_action = { # 1230
	trigger = {
		current_date > 1200.1.1
		current_date < 1300.1.1
		game_start_date < 1230.1.1
		NOT = { has_game_rule = never_keita_invasion }
		exists = title:e_keita # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:keita_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1200.1.1
			}
			modifier = {
				add = 7
				current_date > 1220.1.1
			}
			modifier = {
				add = 15
				current_date > 1230.1.1
			}
			modifier = {
				add = 30
				current_date > 1260.1.1
			}
			debug_log = "attempting to trigger keita story soon"
			trigger_event = {
				id = keita_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

# as of 1.11, this is also part of the base game
# al-qays_invasion_on_action = { # 869
# 	trigger = {
# 		current_date > 850.1.1
# 		current_date < 1000.1.1
# 		game_start_date < 869.1.1
# 		NOT = { has_game_rule = never_al-qays_invasion }
# 		exists = title:d_al-qays # update compatibility - new titles in old saves return false
# 		NOT = { # is set in story
# 			is_target_in_global_variable_list = {
# 				name = historicinvasions_story_started
# 				target = flag:al-qays_story_started
# 			}
# 		}		
# 	}
# 	effect = {
# 		random = {
# 			chance = 0
# 			modifier = {
# 				add = 1
# 				current_date > 850.1.1
# 			}
# 			modifier = {
# 				add = 24
# 				current_date > 860.1.1
# 			}
# 			modifier = {
# 				add = 30
# 				current_date > 870.1.1
# 			}
# 			modifier = {
# 				add = 35
# 				current_date > 875.1.1
# 			}
# 			debug_log = "attempting to trigger zanj / al-Qays story soon"
# 			trigger_event = {
# 				id = al-qays_invasion.0001
# 				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
# 			}
# 		}
# 	}
# }

knytling_invasion_on_action = { #1016
	trigger = {
		current_date > 990.1.1
		current_date < 1090.1.1
		game_start_date < 1016.1.1
		NOT = { has_game_rule = never_knytling_invasion }
		exists = title:e_knytling # update compatibility - new titles in old saves return false
		title:e_north_sea = { is_title_created = no } # base game title, FP1
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:knytling_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 990.1.1
			}
			modifier = {
				add = 5
				current_date > 1000.1.1
			}
			modifier = {
				add = 15
				current_date > 1010.1.1
			}
			modifier = {
				add = 35
				current_date > 1015.1.1
			}
			debug_log = "attempting to trigger knytling story soon"
			trigger_event = {
				id = knytling_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

gediminid_invasion_on_action = { # 1316
	trigger = {
		current_date > 1300.1.1
		current_date < 1400.1.1
		game_start_date < 1316.1.1
		NOT = { has_game_rule = never_gediminid_invasion }
		exists = title:e_gediminid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:gediminid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 1300.1.1
			}
			modifier = {
				add = 7
				current_date > 1310.1.1
			}
			modifier = {
				add = 30
				current_date > 1325.1.1
			}
			debug_log = "attempting to trigger gediminid story soon"
			trigger_event = {
				id = gediminid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

yngling_invasion_on_action = { # 870
	trigger = {
		current_date > 860.1.1
		current_date < 960.1.1
		game_start_date < 872.1.1
		NOT = { has_game_rule = never_yngling_invasion }
		exists = title:d_yngling # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:yngling_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 7
				current_date > 860.1.1
			}
			modifier = {
				add = 15
				current_date > 870.1.1
			}
			modifier = {
				add = 30
				current_date > 880.1.1
			}
			debug_log = "attempting to trigger yngling story soon"
			trigger_event = {
				id = yngling_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

sabbah_invasion_on_action = { # 1090
	trigger = {
		current_date > 1080.1.1
		current_date < 1180.1.1
		game_start_date < 1090.1.1
		NOT = { has_game_rule = never_assassins_invasion }
		exists = title:k_sabbah # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:sabbah_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 7
				current_date > 1080.1.1
			}
			modifier = {
				add = 15
				current_date > 1090.1.1
			}
			modifier = {
				add = 30
				current_date > 1100.1.1
			}
			debug_log = "attempting to trigger sabbah story soon"
			trigger_event = {
				id = sabbah_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

jabalid_invasion_on_action = { # 1162
	trigger = {
		current_date > 1130.1.1
		current_date < 1230.1.1
		game_start_date < 1162.1.1
		NOT = { has_game_rule = never_assassins_invasion }
		exists = title:k_jabalid # update compatibility - new titles in old saves return false		
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:jabalid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 7
				current_date > 1130.1.1
			}
			modifier = {
				add = 15
				current_date > 1160.1.1
			}
			modifier = {
				add = 30
				current_date > 1180.1.1
			}
			debug_log = "attempting to trigger jabalid story soon"
			trigger_event = {
				id = jabalid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

qarghuyid_invasion_on_action = { # 968
	trigger = {
		current_date > 950.1.1
		current_date < 1050.1.1
		game_start_date < 968.1.1
		NOT = { has_game_rule = never_qarghuyid_invasion }
		exists = title:d_qarghuyid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:qarghuyid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 7
				current_date > 950.1.1
			}
			modifier = {
				add = 15
				current_date > 960.1.1
			}
			modifier = {
				add = 30
				current_date > 990.1.1
			}
			debug_log = "attempting to trigger qarghuyid story soon"
			trigger_event = {
				id = qarghuyid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

vivar_invasion_on_action = { # 1094
	trigger = {
		current_date > 1070.1.1
		current_date < 1170.1.1
		game_start_date < 1094.1.1
		NOT = { has_game_rule = never_vivar_invasion }
		exists = title:d_vivar # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:vivar_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 7
				current_date > 1070.1.1
			}
			modifier = {
				add = 15
				current_date > 1080.1.1
			}
			modifier = {
				add = 30
				current_date > 1095.1.1
			}
			debug_log = "attempting to trigger vivar story soon"
			trigger_event = {
				id = vivar_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

normandie_invasion_on_action = { # 1066
	trigger = {
		current_date > 1040.1.1
		current_date < 1140.1.1
		game_start_date < 1066.1.1
		NOT = { has_game_rule = never_normandie_invasion }
		exists = title:k_england
		exists = title:k_normandie # update compatibility - new titles in old saves return false
		OR = { # make sure that normandie doesn't bother knytling
			has_game_rule = never_knytling_invasion
			AND = {
				has_game_rule = default_knytling_invasion
				title:e_knytling = { is_title_created = no }
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:knytling_story_ended
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:normandie_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1040.1.1
			}
			modifier = {
				add = 7
				current_date > 1050.1.1
			}
			modifier = {
				add = 15
				current_date > 1060.1.1
			}
			modifier = {
				add = 30
				current_date > 1070.1.1
			}
			debug_log = "attempting to trigger normandie story soon"
			trigger_event = {
				id = normandie_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

piast_invasion_on_action = { # 920
	trigger = {
		current_date > 930.1.1
		current_date < 1030.1.1
		game_start_date < 960.1.1
		NOT = { has_game_rule = never_piast_invasion }
		exists = title:k_piast # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:piast_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 930.1.1
			}
			modifier = {
				add = 7
				current_date > 940.1.1
			}
			modifier = {
				add = 15
				current_date > 950.1.1
			}
			modifier = {
				add = 30
				current_date > 960.1.1
			}
			debug_log = "attempting to trigger piast story soon"
			trigger_event = {
				id = piast_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

sangama_invasion_on_action = { # 1336
	trigger = {
		current_date > 1300.1.1
		current_date < 1400.1.1
		game_start_date < 1336.1.1
		NOT = { has_game_rule = never_sangama_invasion }
		exists = title:e_sangama # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:sangama_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1300.1.1
			}
			modifier = {
				add = 7
				current_date > 1315.1.1
			}
			modifier = {
				add = 15
				current_date > 1330.1.1
			}
			modifier = {
				add = 30
				current_date > 1350.1.1
			}
			debug_log = "attempting to trigger sangama story soon"
			trigger_event = {
				id = sangama_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

mamluk_invasion_arabia_on_action = { # 1250
	trigger = {
		current_date >= 1250.1.1
		#current_date < 1350.1.1
		NOT = { has_game_rule = never_mamluk_invasion }
		exists = title:e_arabia # update compatibility - new titles in old saves return false
		title:e_arabia = {
			is_title_created = yes
			is_titular = no
			NOT = { has_variable = had_mamluk_coup_var }
			has_variable = mamluk_recruitment_var
		}
		trigger_if = {
			limit = {
				NOT = { has_game_rule = never_ayyubid_invasion }
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_ended
				target = flag:ayyubid_story_ended
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = { add = title:e_arabia.var:mamluk_recruitment_var }
			modifier = { add = -18 }
			modifier = {
				factor = 0
				current_date < 1250.1.1
			}
			debug_log = "attempting to trigger mamluk story arabia soon"
			mamluks_seize_power_effect = { COUP_TITLE = title:e_arabia }
		}
	}
}

mamluk_invasion_rajastan_on_action = { # 1206
	trigger = {
		current_date >= 1206.1.1
		#current_date < 1306.1.1
		NOT = { has_game_rule = never_mamluk_invasion }
		exists = title:e_rajastan # update compatibility - new titles in old saves return false
		title:e_rajastan = {
			is_title_created = yes
			is_titular = no
			NOT = { has_variable = had_mamluk_coup_var }
			has_variable = mamluk_recruitment_var
		}
		trigger_if = {
			limit = {
				NOT = { has_game_rule = never_ghurid_invasion }
			}
			is_target_in_global_variable_list = {
				name = historicinvasions_story_ended
				target = flag:ghurid_story_ended
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = { add = title:e_rajastan.var:mamluk_recruitment_var }
			modifier = { add = -18 }
			modifier = {
				factor = 0
				current_date < 1206.1.1
			}
			debug_log = "attempting to trigger mamluk story rajastan soon"
			mamluks_seize_power_effect = { COUP_TITLE = title:e_rajastan }
		}
	}
}

bearn_invasion_on_action = { # 1087
	trigger = {
		current_date > 1070.1.1
		current_date < 1170.1.1
		game_start_date < 1087.1.1
		NOT = { has_game_rule = never_bearn_invasion }
		exists = title:d_bearn # update compatibility - new titles in old saves return false
		OR = { # only after Norman conquest of England
			game_start_date >= 1066.1.1
			has_game_rule = never_normandie_invasion
			AND = {
				has_game_rule = default_normandie_invasion
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:normandie_story_ended
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:bearn_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 5
				current_date > 1070.1.1
			}
			modifier = {
				add = 15
				current_date > 1080.1.1
			}
			modifier = {
				add = 30
				current_date > 1095.1.1
			}
			debug_log = "attempting to trigger bearn story soon"
			trigger_event = {
				id = bearn_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

eldiguzid_invasion_on_action = { # 1136
	trigger = {
		current_date > 1120.1.1
		current_date < 1220.1.1
		game_start_date < 1136.1.1
		NOT = { has_game_rule = never_eldiguzid_invasion }
		exists = title:k_eldiguzid # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:eldiguzid_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 5
				current_date > 1120.1.1
			}
			modifier = {
				add = 15
				current_date > 1130.1.1
			}
			modifier = {
				add = 25
				current_date > 1140.1.1
			}
			modifier = {
				add = 40
				current_date > 1145.1.1
			}
			debug_log = "attempting to trigger eldiguzid story soon"
			trigger_event = {
				id = eldiguzid_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

borjigin_invasion_on_action = { # 1206
	trigger = {
		current_date > 1170.1.1
		current_date < 1270.1.1
		game_start_date < 1206.1.1
		NOT = { has_game_rule = never_mongol_invasion }
		exists = title:e_mongol_empire
		title:e_mongol_empire = { is_title_created = no } # base game title
		title:e_ilkhanate = { is_title_created = no } # base game title
		title:e_golden_horde = { is_title_created = no } # base game title
		title:e_chagatai = { is_title_created = no } # base game title
		NAND = { # mongols
			has_global_variable = mongols_have_appeared # always set; blocks base game on_action from triggering
			is_target_in_global_variable_list = { # is set in story or immediately in the case of the base game version
				name = historicinvasions_story_started
				target = flag:borjigin_story_started
			}
		}
	}
	effect = {
		### modded mechanics
		if = {
			limit = {
				has_game_rule = default_mongol_invasion
				exists = title:e_blue_horde # update compatibility - new titles in old saves return false
			}
			random = {
				chance = 0
				modifier = {
					add = 1
					current_date > 1190.1.1
				}
				modifier = {
					add = 4
					current_date > 1195.1.1
				}
				modifier = {
					add = 15
					current_date > 1200.1.1
				}
				modifier = {
					add = 20
					current_date > 1210.1.1
				}
				modifier = {
					add = 25
					current_date > 1215.1.1
				}
				modifier = {
					add = 15
					current_date > 1220.1.1
				}
				debug_log = "attempting to trigger borjigin story soon"
				trigger_event = {
					id = borjigin_invasion.0001
					days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
				}
			}
		}
		### base game mechanics
		else_if = {
			limit = {
				has_game_rule = random_mongol_invasion # actually just the base game invasion, random setting is functionally removed
			}
			random = {
				chance = temujin_appearance_base_chance
				modifier = {
					factor = 0.25
					current_date < 1180.1.1
				}
				modifier = {
					add = 3
					current_date > 1200.1.1
				}
				modifier = {
					add = 5
					current_date > 1220.1.1
				}
				modifier = {
					add = 5
					current_date > 1235.1.1
				}

				# I usually set variables within the story itself, but the base game sets them immediately
				add_to_global_variable_list = { # set modded variable
					name = historicinvasions_story_started
					target = flag:borjigin_story_started
				}
				add_to_global_variable_list = { # set modded variable
					name = historicinvasions_story_ended
					target = flag:borjigin_story_ended
				}
				set_global_variable = { # set base game variable
					name = mongols_have_appeared
					value = yes
				}

				debug_log = "Attempting to spawn mongol invasion within 240 days"
				trigger_event = {
					id = mongol_invasion.0001
					# id = borjigin_invasion.0002 # base game mongol_invasion.0001 edited
					days = { 2 240 }
				}
			}
		}
	}
}

borjigin_events_on_action = { # 1162, 1271 - 1295
	trigger = {
		current_date > 1161.1.1
		current_date < 1296.1.1
		NOT = { has_game_rule = never_mongol_invasion }
	}
	effect = {
		### birth of Temüjin
		if = {
			limit = { # current_date = 1162.1.2 also works, but I am not sure whether that's consistent
				current_date > 1161.12.31
				current_date < 1162.2.1
			}
			every_player = {
				trigger_event = {
					id = borjigin_invasion.1000
					days = { 7 340 }
				}
			}
		}
		### travels of Marco Polo, 1271 - 1295
		if = {
			limit = {
				current_date > 1294.12.31
				current_date < 1295.2.1
				is_target_in_global_variable_list = {
					name = historicinvasions_story_started
					target = flag:borjigin_story_started
				}
			}
			debug_log = "marco polo"
			every_player = {
				trigger_event = {
					id = borjigin_invasion.1003
					days = { 7 340 }
				}
			}
		}
	}
}

qara_qoyunlu_invasion_on_action = { # 1405
	trigger = {
		current_date > 1390.1.1
		current_date < 1505.1.1
		game_start_date < 1405.1.1
		NOT = { has_game_rule = never_qara_qoyunlu_invasion }
		exists = title:e_qara_qoyunlu # update compatibility - new titles in old saves return false
		OR = { # make sure that timurids and qara qoyunlu don't bother each other
			has_game_rule = never_timurid_invasion
			AND = {
				has_game_rule = default_timurid_invasion
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:timurid_story_ended
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:qara_qoyunlu_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1390.1.1
			}
			modifier = {
				add = 5
				current_date > 1395.1.1
			}
			modifier = {
				add = 10
				current_date > 1400.1.1
			}
			modifier = {
				add = 30
				current_date > 1405.1.1
			}
			debug_log = "attempting to trigger qara qoyunlu story soon"
			trigger_event = {
				id = qara_qoyunlu_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

aq_qoyunlu_invasion_on_action = { # 1467; past end date
	trigger = {
		current_date > 1420.1.1
		current_date < 1567.1.1
		game_start_date < 1467.1.1
		NOT = { has_game_rule = never_aq_qoyunlu_invasion }
		exists = title:e_aq_qoyunlu # update compatibility - new titles in old saves return false
		OR = { # make sure that qara qoyunlu and aq qoyunlu don't bother each other
			has_game_rule = never_qara_qoyunlu_invasion
			AND = {
				has_game_rule = default_qara_qoyunlu_invasion
				is_target_in_global_variable_list = {
					name = historicinvasions_story_ended
					target = flag:qara_qoyunlu_story_ended
				}
			}
		}
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:aq_qoyunlu_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1420.1.1
			}
			modifier = {
				add = 5
				current_date > 1430.1.1
			}
			modifier = {
				add = 10
				current_date > 1440.1.1
			}
			modifier = {
				add = 30
				current_date > 1460.1.1
			}
			debug_log = "attempting to trigger aq qoyunlu story soon"
			trigger_event = {
				id = aq_qoyunlu_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

wanggiyan_invasion_on_action = { # 1115
	trigger = {
		current_date > 1100.1.1
		current_date < 1200.1.1
		game_start_date < 1115.1.1
		NOT = { has_game_rule = never_wanggiyan_invasion }
		exists = title:e_wanggiyan # update compatibility - new titles in old saves return false
		title:e_wanggiyan = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:wanggiyan_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 1100.1.1
			}
			modifier = {
				add = 5
				current_date > 1110.1.1
			}
			modifier = {
				add = 15
				current_date > 1115.1.1
			}
			modifier = {
				add = 30
				current_date > 1125.1.1
			}
			debug_log = "attempting to trigger wanggiyan story soon"
			trigger_event = {
				id = wanggiyan_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

rajasa_invasion_on_action = { # 1293
	trigger = {
		current_date > 1275.1.1
		current_date < 1375.1.1
		game_start_date < 1293.1.1
		NOT = { has_game_rule = never_rajasa_invasion }
		exists = title:e_rajasa # update compatibility - new titles in old saves return false
		title:e_rajasa = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:rajasa_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 1275.1.1
			}
			modifier = {
				add = 5
				current_date > 1280.1.1
			}
			modifier = {
				add = 15
				current_date > 1290.1.1
			}
			modifier = {
				add = 30
				current_date > 1310.1.1
			}
			debug_log = "attempting to trigger rajasa story soon"
			trigger_event = {
				id = rajasa_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

liao_invasion_on_action = { # 916
	trigger = {
		current_date > 895.1.1
		current_date < 995.1.1
		game_start_date < 916.1.1
		NOT = { has_game_rule = never_liao_invasion }
		exists = title:e_liao # update compatibility - new titles in old saves return false
		title:e_liao = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:liao_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 895.1.1
			}
			modifier = {
				add = 5
				current_date > 905.1.1
			}
			modifier = {
				add = 15
				current_date > 915.1.1
			}
			modifier = {
				add = 30
				current_date > 925.1.1
			}
			debug_log = "attempting to trigger liao story soon"
			trigger_event = {
				id = liao_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

ahom_invasion_on_action = { # 1228
	trigger = {
		current_date > 1200.1.1
		current_date < 1300.1.1
		game_start_date < 1228.1.1
		NOT = { has_game_rule = never_ahom_invasion }
		exists = title:d_ahom # update compatibility - new titles in old saves return false
		title:d_ahom = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:ahom_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 3
				current_date > 1200.1.1
			}
			modifier = {
				add = 5
				current_date > 1210.1.1
			}
			modifier = {
				add = 15
				current_date > 1225.1.1
			}
			modifier = {
				add = 30
				current_date > 1245.1.1
			}
			debug_log = "attempting to trigger ahom story soon"
			trigger_event = {
				id = ahom_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

####### ANCIENT HISTORY

attila_invasion_on_action = { # 370 or random
	trigger = {
		current_date > 360.1.1
		# game start date irrelevant
		NOT = { has_game_rule = never_attila_invasion }
		exists = title:e_attila # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:attila_story_started
			}
		}
	}
	effect = {
		if = {
			limit = { game_start_date < 370.1.1 }
			random = {
				chance = 0
				modifier = {
					add = 5
					current_date > 360.1.1
				}
				modifier = {
					add = 15
					current_date > 370.1.1
				}
				modifier = {
					add = 30
					current_date > 380.1.1
				}
				debug_log = "attempting to trigger attila story soon"
				trigger_event = {
					id = attila_invasion.0001
					days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
				}
			}
		}
		else = {
			random = {
				chance = 0
				modifier = {
					add = 1
					years_from_game_start >= 50
				}
				modifier = {
					add = 1
					years_from_game_start >= 100
				}
				modifier = {
					add = 3
					years_from_game_start >= 150
				}
				debug_log = "attempting to trigger attila story soon"
				trigger_event = {
					id = attila_invasion.0001
					days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
				}
			}
		}
	}
}

argead_invasion_on_action = { # random
	trigger = {
		# game start date irrelevant
		NOT = { has_game_rule = never_argead_invasion }
		exists = title:e_argead # update compatibility - new titles in old saves return false
		title:e_byzantium = { is_title_created = no }
		title:h_roman_empire = { is_title_created = no }
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:argead_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				years_from_game_start >= 50
			}
			modifier = {
				add = 1
				years_from_game_start >= 100
			}
			modifier = {
				add = 3
				years_from_game_start >= 150
			}
			debug_log = "attempting to trigger argead story soon"
			trigger_event = {
				id = argead_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

maurya_invasion_on_action = { # random
	trigger = {
		# game start date irrelevant
		NOT = { has_game_rule = never_maurya_invasion }
		exists = title:e_maurya # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:maurya_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				years_from_game_start >= 50
			}
			modifier = {
				add = 1
				years_from_game_start >= 100
			}
			modifier = {
				add = 3
				years_from_game_start >= 150
			}
			debug_log = "attempting to trigger maurya story soon"
			trigger_event = {
				id = maurya_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

####### FANTASY

song_invasion_on_action = { # 1279
	trigger = {
		current_date > 1250.1.1
		current_date < 1350.1.1
		game_start_date < 1279.1.1
		NOT = { has_game_rule = never_song_invasion }
		exists = title:e_song # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:song_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1250.1.1
			}
			modifier = {
				add = 7
				current_date > 1270.1.1
			}
			modifier = {
				add = 15
				current_date > 1290.1.1
			}
			modifier = {
				add = 30
				current_date > 1320.1.1
			}
			debug_log = "attempting to trigger song story soon"
			trigger_event = {
				id = song_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

zunbil_invasion_on_action = { # 893
	trigger = {
		current_date > 880.1.1
		current_date < 980.1.1
		game_start_date < 893.1.1
		NOT = { has_game_rule = never_zunbil_invasion }
		exists = title:k_zunbil # update compatibility - new titles in old saves return false
		NOT = { # is set in story
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:zunbil_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 880.1.1
			}
			modifier = {
				add = 7
				current_date > 890.1.1
			}
			modifier = {
				add = 15
				current_date > 900.1.1
			}
			modifier = {
				add = 30
				current_date > 910.1.1
			}
			debug_log = "attempting to trigger zunbil story soon"
			trigger_event = {
				id = zunbil_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

komnenos_invasion_on_action = { # 1091
	trigger = {
		current_date > 1075.1.1
		current_date < 1175.1.1
		NOT = { has_game_rule = never_komnenos_invasion }
		exists = title:e_byzantium # update compatibility - new titles in old saves return false
		title:e_byzantium = { is_title_created = yes }
		title:e_byzantium.holder.religion = religion:christianity_religion
		trigger_if = {
			limit = { has_game_rule = default_seljuk_rum_invasion }
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:seljuk_rum_have_appeared
			}
			NOT = { exists = global_var:historicinvasions_rum_tmp_blocks_komnenos_story }
		}
		NOT = { # is set in story / confirmation event
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:komnenos_story_started
			}
		}
	}
	effect = {
		random = {
			chance = 0
			modifier = {
				add = 1
				current_date > 1070.1.1
			}
			modifier = {
				add = 15
				current_date > 1085.1.1
			}
			modifier = {
				add = 30
				current_date > 1090.1.1
			}
			debug_log = "attempting to trigger komnenos story soon"
			trigger_event = {
				id = komnenos_invasion.0001
				days = { 2 240 } # keep this smaller than 1 year if the global variable is set in event .0001
			}
		}
	}
}

####### GLOBAL EVENTS

turkish_migrations_on_action = { # 1080
	trigger = {
		current_date >= 1080.1.1
		has_game_rule = default_turkish_migrations # 3 rules available
		NOT = { # is set in event
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:turkish_migrations_started
			}
		}
	}
	events = {
		delay = { days = { 1 300 } }
		turkish_migrations.0001
	}
}

####### OTHER
# compatibility for ongoing games
# goal: always set the global variables blocking the base game on_action from triggering
historicinvasions_base_game_compatibility_on_action = {
	trigger = {
		# at least one of the two invasions is enabled
		NAND = {
			has_game_rule = never_seljuk_invasion
			has_game_rule = never_mongol_invasion
		}
	}
	effect = {
		### Seljuk
		# always block base game invasion
		if = {
			limit = {
				NOT = { has_global_variable = seljuk_invasion_happened }
			}
			# 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'"
		}
		# set modded variables if base game story has already started or ended
		else_if = {
			limit = {
				has_game_rule = random_seljuk_invasion # base game
				has_global_variable = seljuk_invasion_happened
				NOT = {
					is_target_in_global_variable_list = { # is set in story or when base game invasion is triggered
						name = historicinvasions_story_started
						target = flag:seljuk_story_started
					}
				}
				# there is no base game variable signaling the running / end of the invasion, so we have to guess
				OR = {
					current_date >= 1066.1.1 # base game invasion can not trigger past this point
					AND = {
						title:e_persia = { is_title_created = yes }
						title:e_persia.holder.culture = culture:turkish
					}
				}
			}
			# block modded on_action
			add_to_global_variable_list = { # set modded variable
				name = historicinvasions_story_started
				target = flag:seljuk_story_started
			}
			add_to_global_variable_list = { # set modded variable
				name = historicinvasions_story_ended
				target = flag:seljuk_story_ended
			}
			debug_log = "set global variables 'seljuk_story_started' and 'seljuk_story_ended'"
		}

		### Mongols
		# always block base game invasion
		if = {
			limit = {
				NOT = { has_global_variable = mongols_have_appeared }
			}
			# 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'"
		}
		# set modded variables if base game story has already started or ended
		else_if = {
			limit = {
				has_game_rule = random_mongol_invasion # base game
				has_global_variable = mongols_have_appeared
				NOT = {
					is_target_in_global_variable_list = { # is set in story or when base game invasion is triggered
						name = historicinvasions_story_started
						target = flag:borjigin_story_started
					}
				}
				OR = {
					exists = global_var:mongol_empire_has_spawned
					exists = global_var:mongol_empire_was_broken_up
					has_global_variable = mongol_story_has_ended
					title:e_mongol_empire = { is_title_created = yes }
				}
			}
			# block modded on_action
			add_to_global_variable_list = { # set modded variable
				name = historicinvasions_story_started
				target = flag:borjigin_story_started
			}
			add_to_global_variable_list = { # set modded variable
				name = historicinvasions_story_ended
				target = flag:borjigin_story_ended
			}
			debug_log = "set global variables 'borjigin_story_started' and 'borjigin_story_ended'"
		}
	}
}