﻿mpo_the_great_steppe_monthly_pulse = {
}

mpo_the_great_steppe_yearly_pulse = {
	effect = {
		if = {
			limit = {
				situation_top_has_catalyst = catalyst_situation_passing_of_time_blessing
				NOT = { has_global_variable = had_sky_blessing_var }
			}
			random = {
				chance = 5
				# 100% chance total in 1000y
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					add = 5
				}
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					years_from_game_start >= 50
					add = 5
				}
				modifier = {
					years_from_game_start >= 75
					add = 5
				}
				modifier = {
					years_from_game_start >= 100
					add = 5
				}
				modifier = {
					years_from_game_start >= 150
					add = 5
				}
				modifier = {
					years_from_game_start >= 200
					add = 5
				}
				modifier = {
					years_from_game_start >= 250
					add = 5
				}
				modifier = {
					years_from_game_start >= 300
					add = 5
				}
				modifier = {
					years_from_game_start >= 400
					add = 5
				}
				modifier = {
					years_from_game_start >= 500
					add = 10
				}
				modifier = {
					years_from_game_start >= 600
					add = 10
				}
				modifier = {
					years_from_game_start >= 700
					add = 10
				}
				modifier = {
					years_from_game_start >= 800
					add = 10
				}
				modifier = {
					years_from_game_start >= 900
					add = 10
				}
				modifier = {
					years_from_game_start >= 1000
					add = 5
				}
				trigger_situation_catalyst = catalyst_situation_passing_of_time_blessing
			}
		}
		if = {
			limit = {
				situation_top_has_catalyst = catalyst_situation_passing_of_time_havsarsan
				NOT = { has_global_variable = had_havsarsan_zud_var }
			}
			random = {
				chance = 5
				# 100% chance total in 1000y
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					add = 5
				}
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					years_from_game_start >= 50
					add = 5
				}
				modifier = {
					years_from_game_start >= 75
					add = 5
				}
				modifier = {
					years_from_game_start >= 100
					add = 5
				}
				modifier = {
					years_from_game_start >= 150
					add = 5
				}
				modifier = {
					years_from_game_start >= 200
					add = 5
				}
				modifier = {
					years_from_game_start >= 250
					add = 5
				}
				modifier = {
					years_from_game_start >= 300
					add = 5
				}
				modifier = {
					years_from_game_start >= 400
					add = 5
				}
				modifier = {
					years_from_game_start >= 500
					add = 10
				}
				modifier = {
					years_from_game_start >= 600
					add = 10
				}
				modifier = {
					years_from_game_start >= 700
					add = 10
				}
				modifier = {
					years_from_game_start >= 800
					add = 10
				}
				modifier = {
					years_from_game_start >= 900
					add = 10
				}
				modifier = {
					years_from_game_start >= 1000
					add = 5
				}
				trigger_situation_catalyst = catalyst_situation_passing_of_time_havsarsan
			}
		}
		if = {
			limit = {
				situation_top_has_catalyst = catalyst_situation_passing_of_time_lucky_chance_blessing
				NOT = { has_global_variable = had_sky_blessing_var }
			}
			random = {
				chance = 0
				# 100% chance total in 1000y
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					add = 5
				}
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					years_from_game_start >= 50
					add = 5
				}
				modifier = {
					years_from_game_start >= 75
					add = 5
				}
				modifier = {
					years_from_game_start >= 100
					add = 5
				}
				modifier = {
					years_from_game_start >= 150
					add = 5
				}
				modifier = {
					years_from_game_start >= 200
					add = 5
				}
				modifier = {
					years_from_game_start >= 250
					add = 5
				}
				modifier = {
					years_from_game_start >= 300
					add = 5
				}
				modifier = {
					years_from_game_start >= 400
					add = 5
				}
				modifier = {
					years_from_game_start >= 500
					add = 5
				}
				modifier = {
					years_from_game_start >= 600
					add = 10
				}
				modifier = {
					years_from_game_start >= 700
					add = 10
				}
				modifier = {
					years_from_game_start >= 800
					add = 10
				}
				modifier = {
					years_from_game_start >= 900
					add = 20
				}
				modifier = {
					years_from_game_start >= 1000
					add = 5
				}
				trigger_situation_catalyst = catalyst_situation_passing_of_time_lucky_chance_blessing
			}
		}
		if = {
			limit = {
				situation_top_has_catalyst = catalyst_situation_passing_of_time_lucky_chance_havsarsan
				NOT = { has_global_variable = had_havsarsan_zud_var }
			}
			random = {
				chance = 0
				# 100% chance total in 1000y
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					add = 5
				}
				modifier = {
					game_start_date > 1065
					game_start_date < 1177
					years_from_game_start >= 50
					add = 5
				}
				modifier = {
					years_from_game_start >= 75
					add = 5
				}
				modifier = {
					years_from_game_start >= 100
					add = 5
				}
				modifier = {
					years_from_game_start >= 150
					add = 5
				}
				modifier = {
					years_from_game_start >= 200
					add = 5
				}
				modifier = {
					years_from_game_start >= 250
					add = 5
				}
				modifier = {
					years_from_game_start >= 300
					add = 5
				}
				modifier = {
					years_from_game_start >= 400
					add = 5
				}
				modifier = {
					years_from_game_start >= 500
					add = 5
				}
				modifier = {
					years_from_game_start >= 600
					add = 10
				}
				modifier = {
					years_from_game_start >= 700
					add = 10
				}
				modifier = {
					years_from_game_start >= 800
					add = 10
				}
				modifier = {
					years_from_game_start >= 900
					add = 20
				}
				modifier = {
					years_from_game_start >= 1000
					add = 5
				}
				trigger_situation_catalyst = catalyst_situation_passing_of_time_lucky_chance_havsarsan
			}
		}
	}
}

mpo_generic_nomadic_region_yearly_pulse = {
	effect = {
		# Spread this pulse out
		every_participant_group = {
			limit = {
				participant_group_type = nomad_rulers_capital
			}
			every_situation_group_participant = {
				trigger_event = {
					on_action = mpo_the_great_steppe_yearly_pulse_random
					days = { 1 250 }
				}
			}
		}
	}
}

mpo_the_great_steppe_yearly_pulse_random = {
	effect = {
		# Nomads should hunt a lot
		if = {
			limit = {
				is_ai = yes
				is_at_war = no
				government_has_flag = government_is_nomadic
			}
			save_scope_as = nomad_capital_ruler
			if = {
				limit = {
					primary_title.tier = tier_county
					domain_size > domain_limit
					this = top_liege
					OR = { #Unop: Unify this in an OR since it was an alternative_limit but it's not valid in this context
						any_held_title = {
							tier = tier_county
							is_landless_type_title = no
							this != scope:nomad_capital_ruler.capital_county
							OR = {
								any_neighboring_county = {
									holder.top_liege != scope:nomad_capital_ruler.top_liege
								}
								is_coastal_county = yes
							}
							NOT = {
								any_neighboring_county = {
									this = scope:nomad_capital_ruler.capital_county
								}
							}
						}
						any_held_title = {
							tier = tier_county
							is_landless_type_title = no
							this != scope:nomad_capital_ruler.capital_county
							OR = {
								any_neighboring_county = {
									holder.top_liege != scope:nomad_capital_ruler.top_liege
								}
								is_coastal_county = yes
							}
						}
					}
				}
				random_held_title = {
					limit = {
						tier = tier_county
						is_landless_type_title = no
						this != scope:nomad_capital_ruler.capital_county
						OR = {
							any_neighboring_county = {
								holder.top_liege != scope:nomad_capital_ruler.top_liege
							}
							is_coastal_county = yes
						}
						NOT = {
							any_neighboring_county = {
								this = scope:nomad_capital_ruler.capital_county
							}
						}
					}
					alternative_limit = {
						tier = tier_county
						is_landless_type_title = no
						this != scope:nomad_capital_ruler.capital_county
						OR = {
							any_neighboring_county = {
								holder.top_liege != scope:nomad_capital_ruler.top_liege
							}
							is_coastal_county = yes
						}
					}
					weight = {
						base = 100
						modifier = { #Less likely to pick someone you actively dislike
							add = {
								value = county_fertility
								multiply = -1
							}
							uses_county_fertility = yes
						}
					}
					save_scope_as = title_to_abandon
				}
				create_character = {
					template = herder_character
					dynasty = none
					location = scope:nomad_capital_ruler.location
					save_scope_as = new_herder
				}
				scope:title_to_abandon = {
					if = {
						limit = {
							title_province = {
								has_holding_type = nomad_holding
							}
						}
						title_province = {
							set_holding_type = herder_holding
						}
					}
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_title_holder_include_vassals = {
						holder = scope:new_herder
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
				}
				scope:new_herder = {
					change_government = herder_government
					every_held_title = {
						limit = {
							is_landless_type_title = yes
						}
						scope:new_herder = { destroy_title = prev }
					}
				}
				start_tributary_interaction_effect = {
					TRIBUTARY = scope:new_herder
					SUZERAIN = scope:nomad_capital_ruler
				}
				scope:nomad_capital_ruler = {
					add_prestige = minor_prestige_loss
				}
			}
			else = {
				random = {
					chance = {
						value = 5
						if = {
							limit = {
								prestige <= 600
							}
							add = 30
						}
						if = {
							limit = {
								prestige <= 100
							}
							add = 70
						}
					}
					ai_attempt_to_host_activity = activity_hunt
				}
				random = {
					chance = {
						value = 0
						if = {
							limit = {
								has_character_modifier = mpo_si_slaughtered_animals
							}
							add = 30
						}
						if = {
							limit = {
								gold > prestige
								gold > 200
								prestige < 600
							}
							add = 30
						}
					}
					ai_attempt_to_host_activity = activity_feast
				}
				# Start Overrun Kingdom Wars
				if = {
					limit = {
						exists = scope:nomad_capital_ruler
						mpo_nomad_invasion_war_ai_trigger = { NOMAD = scope:nomad_capital_ruler }
					}
					ai_start_best_war = {
						cb = {
							mpo_nomad_invasion_cb
						}
						is_valid = {
							scope:target_character = {
								in_diplomatic_range = root
								current_military_strength < root.eighty_percent_of_current_military_strength
								OR = {
									gold < root.fifty_percent_of_current_gold
									current_military_strength <= root.fifty_percent_of_current_military_strength
									any_character_war = {
										OR = {
											is_attacker = scope:target_character
											attacker_war_score < 0
										}
										OR = {
											is_defender = scope:target_character
											defender_war_score < 0
										}
										#Not too close to being finished
										attacker_war_score <= 80
										defender_war_score <= 80
										NOR = {
											using_cb = mpo_nomad_duchy_invasion_cb
											using_cb = mpo_nomad_invasion_cb
										}
									}
									any_character_war = {
										count >= 3
										#Not too close to being finished
										attacker_war_score <= 80
										defender_war_score <= 80
										NOR = {
											using_cb = mpo_nomad_duchy_invasion_cb
											using_cb = mpo_nomad_invasion_cb
										}
									}
								}
								any_sub_realm_county = {
									title_province = {
										squared_distance = {
											target = root.capital_province
											value <= squared_distance_almost_massive
										}
									}
								}
							}
						}
					}
				}
				#Start Overrun Duchy Wars
				else_if = {
					limit = {
						exists = scope:nomad_capital_ruler
						mpo_nomad_duchy_invasion_war_ai_trigger = { NOMAD = scope:nomad_capital_ruler }
					}
					random = {
						chance = 50
						add_character_flag = nomad_duchy_invasion_all_target_override
						ai_start_best_war = {
							cb = {
								mpo_nomad_duchy_invasion_cb
							}
							is_valid = {
								scope:target_character = {
									in_diplomatic_range = root
									current_military_strength < root.seventy_percent_of_current_military_strength
									OR = {
										gold <= root.fifty_percent_of_current_gold
										current_military_strength <= root.forty_percent_of_current_military_strength
										any_character_war = {
											OR = {
												is_attacker = scope:target_character
												attacker_war_score < 0
											}
											OR = {
												is_defender = scope:target_character
												defender_war_score < 0
											}
											#Not too close to being finished
											attacker_war_score <= 80
											defender_war_score <= 80
											NOR = {
												using_cb = mpo_nomad_duchy_invasion_cb
												using_cb = mpo_nomad_invasion_cb
											}
										}
										any_character_war = {
											count >= 3
											#Not too close to being finished
											attacker_war_score <= 80
											defender_war_score <= 80
											NOR = {
												using_cb = mpo_nomad_duchy_invasion_cb
												using_cb = mpo_nomad_invasion_cb
											}
										}
									}
									any_sub_realm_county = {
										title_province = {
											squared_distance = {
												target = root.capital_province
												value <= squared_distance_huge
											}
										}
									}
								}
							}
						}
						remove_character_flag = nomad_duchy_invasion_all_target_override
					}
				}
				# Big nomads should expand a lot
				if = {
					limit = {
						highest_held_title_tier >= tier_kingdom
						NOT = {
							has_realm_law = uncrowned
						}
						war_chest_gold >= war_chest_gold_maximum
						NOT = { # But maybe not the Samí
							capital_county ?= {
								any_county_situation = { this = situation:game_rule_extra_nomads_sami }
							}
						}
					}
					random = {
						chance = 50
						ai_start_best_war = {
							cb = {
								de_jure_cb_nomadic
								retaliation_cb
								humiliation_cb
								make_tributary_cb
								sovereignty_cb
								nomadic_conquest_duchy_cb
							}
							is_valid = {
								scope:target_character = {
									current_military_strength < root.current_military_strength
									OR = {
										gold < root.gold
										current_military_strength <= root.fifty_percent_of_current_military_strength
									}
								}
							}
						}
					}
				}
			}
			# Clean-up Landless AI Nomads who have lost
			clean_up_ai_nomad_effect = yes
		}
	}
}
