﻿mpo_yearly_events_nomads = {
	trigger = {
		has_mpo_dlc_trigger = yes
		government_has_flag = government_is_nomadic
	}

	random_events = {
		chance_to_happen = 100

		# EVENT CHAINS/STORY CYCLE EVENTS
		100 = nomad_events.0001 # Starts the Achmach Fanakiti event chain

		# Regular events
		150 = mpo_nomad_events.1030 # You choose a sport for your son to tryhard
		200 = mpo_nomad_events.1020 # Your kin wants to break off from your tribe
		250 = court_astrologer_events.0002 # Your Astrologer suggest a better place to migrate to
		250 = nomad_events.0100 # Two vassals have a herd dispute
		250 = nomad_events.0120 # You take an evening ride with someone
		200 = nomad_events.0130 # A merchant offers you some herd
		150 = nomad_events.0140 # A foreign merchant passes by your capital and asks to stay
		250 = nomad_events.0150 # You share a drink with a courtier
		300 = nomad_events.0160 # Your spouse tells you they admire you
		300 = nomad_events.0170 # Your spouse is scared of your high dread
		250 = nomad_events.0180 # Random courtier laughs at your child low prowess
		250 = nomad_events.0190 # Reminisce of a past memory
		250 = nomad_events.0200 # Someone tells you of a dream they had
		250 = nomad_events.0220 # Spouse scolds you for your low Prowess
		250 = nomad_events.0230 # Spouse is feeling homesick
		200 = nomad_events.0240 # Neighbouring Nomad's herd shows up in your capital
		100 = nomad_events.0250 # Enthusiastic priest asks you to convert
		100 = nomad_events.0260 # Distant priest arrives at your court
		100 = mpo_nomad_events.1050 # A neighboring herder is salty at you
		300 = mpo_nomad_events.1060 # You trade with non-nomad neighbors
		50 = mpo_nomad_events.1070 #You wake up after having ridden off alone
		200 = mpo_nomad_events.1080 #Neighbor is intruding on your land hunting a criminal
		150 = mpo_nomad_events.1100 #You adapt your herds to the local terrain
		150 = mpo_nomad_events.1110 #Your capital is by a place you can fish
		200 = mpo_events_tova.0025 # You are invited to a knucklebone shooting game
		200 = mpo_events_tova.0030 # No astrologer? Someone comes to you with a vision
		200 = mpo_events_tova.0035 # Anda comes to you with marital concerns
		200 = mpo_events_tova.0045 # A Treasured Friend
		250 = nomad_events_oltner.0001 # Hunt sighting in your lands
		250 = nomad_events_oltner.0002 # Bird sighting in your lands
		250 = nomad_events_oltner.0003 # Dangerous Hunt sighting in your lands
		150 = nomad_events_oltner.0004 # Zud - Dangerous animals eat your herd
		250 = nomad_events_oltner.0005 # Good season! Animals aplenty.
		200 = nomad_events_oltner.0006 # You are offered a skilled Master of the Chase from a tributary
		200 = nomad_events_oltner.0007 # You are offered a falcon from a tributary
		50 = mpo_bb_events.0001 # blood brother comes by for a wrestling match
		100 = mpo_events_anna.0001 #Train Them Young - teach a child how to ride a horse
		200 = mpo_events_anna.0005 #Stories from the Past - choose a story to be told
		200 = mpo_events_anna.0010 #Whistle Like an Arrow - being taught how to whistle as a child
		250 = mpo_events_ariana.0001 # Nest Sighting
		50 = mpo_events_ariana.0010 # Your Heir wants to convert to a different faith
		250 = mpo_events_ariana.0020 # A spouse is suspicious of a councillor
		250 = mpo_events_ariana.0050 # A family member risks being branded
		150 = mpo_events_ariana.0060 # A child in your family spots an eagles nest with treasure
		250 = mpo_events_ariana.0080 # Someone tells you about a magical tree
	  	150 = pet_animal.3050 # Your rival has an eagle
		250 = mpo_events_ariana.0090 # Your child wants to be a herder
		250 = mpo_events_ariana.0100 # A non-martial gender family member is very skilled
		150 = mpo_events_ariana.0110 # Your court astrologer wants you to move your capital

		# SEASON SPECIFIC EVENTS
		300 = mpo_nomads_season_events.0001 # Herders robbed during drought
		300 = mpo_nomads_season_events.0005 # A hungry neighbour asks for herd
		300 = mpo_nomads_season_events.0010 # A well-to-do neighbor offers herd
		300 = mpo_nomads_season_events.0015 # Two Kurultai members fight over food
		150 = nomad_events.0110 # A snow wolf attacks a child (weighted up during white zud/cold zud)
		300 = nomad_events.0210 # Spouse/Child is worried about low herd
		150 = mpo_nomad_events.1120 #It's winter. wolves are hungry for your herd (weighted up during white zud/cold zud)
	}
}

mpo_migration_finished = {
	effect = {
		if = { # We send an event if you're migrating into a Tributary's lands
			limit = {
				has_variable = migrating_into_tributary_war_var
			}
			trigger_event = {
				id = migration_events.0002
				days = 1
			}
		}
		set_variable = {
			name = recently_migrated
			years = 3
		}
		every_held_county = {
			limit = {
				exists = duchy
				duchy = root.capital_province.duchy
			}
			change_county_control = -50
		}
		every_realm_province = {
			limit = {
				has_province_modifier = recently_looted_modifier
			}
			remove_province_modifier = recently_looted_modifier
		}
		if = {
			limit = {
				highest_held_title_tier >= tier_duchy
			}
			while = {
				count = 2
				trigger_event = {
					on_action = mpo_migration_contract
					months = 1
				}
			}
		}
		else = {
			trigger_event = {
				on_action = mpo_migration_contract
				months = 1
			}
		}
		domicile ?= {
			move_domicile = root.capital_province
		}
		every_vassal = {
			domicile ?= {
				move_domicile = prev.capital_province
			}
		}
	}
}

mpo_migration_contract = {
	trigger = {
		is_landed = yes
		has_mpo_dlc_trigger = yes
		government_has_flag = government_is_nomadic
		has_variable = recently_migrated
		any_held_county = {
			is_landless_type_title = no
			exists = title_province
			county_control < 90
		}
	}

	random_events = {
		100 = mpo_contract_events.0001
		100 = mpo_contract_events.0011
		100 = mpo_contract_events.0021
		100 = mpo_contract_events.0031
		20 = mpo_contract_events.0041
		100 = mpo_contract_events.0051
		100 = mpo_contract_events.0061
		100 = mpo_contract_events.0071
		100 = mpo_contract_events.0081
		100 = mpo_contract_events.0091
	}
}


# Triggered from code on the day after a migration travel ends
#
# root - migrating ruler
# scope:actor - migration ruler (compatibility with triggers and effects used in migration interaction)
# scope:recipient - holder of the target title
# scope:target_title - higher tier title target of the migration. Can be of any tier
# scope:domain - county or duchy tier title, all land within it shall become new personal domain of the migrating ruler
#
on_migration_travel_end = {
	events = {
		migration_events.0001
	}

	effect = {
		if = { # MPO ACHIEVEMENT Steppe by Steppe
			limit = {
				is_ai = no
				exists = global_var:started_steppe_by_steppe_achievement
				situation:the_great_steppe ?= {
					situation_sub_region:steppe_west = {
						situation_sub_region_has_county = root.capital_county
					}
				}
			}
			add_achievement_global_variable_effect = {
				VARIABLE = mpo_steppe_by_steppe_achievement_unlocked
				VALUE = yes
			}
		}
		migration_title_and_vassal_change_effect = yes
		trigger_event = {
			on_action = mpo_migration_finished
		}
	}
}

# triggered from code when migration war ends
#
# root - migrating ruler
# scope:actor - migration ruler (compatibility with triggers and effects used in migration interaction)
# scope:recipient - primary defender in migration war
# scope:target_title - higher tier title target of the migration. Can be of any tier
# scope:domain - county or duchy tier title, all land within it shall become new personal domain of the migrating ruler
# scope:war - migration war. War will disappear after this onaction

on_migration_war_end = {
	effect = {
		if = { # MPO ACHIEVEMENT Steppe by Steppe
			limit = {
				is_ai = no
				exists = global_var:started_steppe_by_steppe_achievement
				location = {
					geographical_region = world_steppe_west
				}
			}
			add_achievement_global_variable_effect = {
				VARIABLE = mpo_steppe_by_steppe_achievement_unlocked
				VALUE = yes
			}
		}
		if = {
			limit = {
				scope:war = { has_variable = migration_victory }
			}
			migration_title_and_vassal_change_effect = yes
			trigger_event = {
				on_action = mpo_migration_finished
			}
			every_character_situation = {
				limit = {
					any_participant_group = {
						participant_group_type = nomad_rulers_capital
						participant_group_has_character = root
					}				
				}
				every_participant_group = {
					limit = {
						participant_group_type = nomad_rulers_capital
						participant_group_has_character = root
					}
					every_situation_group_participant = {
						limit = {
							save_temporary_scope_as = temp_scope
							NOT = { this = root }
							is_migrating = yes
							exists = current_travel_plan
							OR = {
								current_travel_plan.final_destination_province.county = scope:target_title
								scope:domain = {
									any_in_de_jure_hierarchy = {
										tier = tier_county
										this = scope:temp_scope.current_travel_plan.final_destination_province.county
									}
								}
							}
						}
						save_scope_as = invalidated_ruler
						scope:invalidated_ruler = {
							send_interface_message = {
								type = msg_migration_into_realm
								title = war_invalidated_migration
								desc = war_invalidated_migration_desc

								left_icon = root
								right_icon = scope:invalidated_ruler
							}
						}
					}
				}
			}
		}
	}
}

# triggered from code right after kurultai succession has been resolved
#
# root - new heir (character)
# scope:was_kurultai - (bool) if new heir was part of the predecessor's Kurultai Council
# scope:kurultai_members - list all characters who were part of the predecessor's Kurultai Council
# scope:obedient_kurultai - list all obedient characters who were part of the Kurultai Election (both Council and Children)
# scope:disobedient_kurultai - list all disobedient characters who were part of the Kurultai Election (both Council and Children)
#
on_kurultai_succession_stable = {
	trigger = {
		has_realm_law = single_heir_succession_kurultai_law
	}
	effect = {
		root = {
			if = {
				limit = {
					exists = this
					"list_size(obedient_kurultai)" > 0
				}
				every_in_list = {
					list = obedient_kurultai
					limit = {
						exists = this
						NOT = { this = root }
					}
					add_opinion = {
						target = root
						modifier = obedience_opinion
						years = 5
					}
				}
				create_character_memory = {
					type = had_stable_kurultai_succession
				}
			}
		}
	}
}

# triggered from code right after kurultai succession has been resolved
#
# root - new heir (character)
# scope:was_kurultai - (bool) if new heir was part of the predecessor's Kurultai Council
# scope:kurultai_members - list all characters who were part of the predecessor's Kurultai Council
# scope:obedient_kurultai - list all obedient characters who were part of the Kurultai Election (both Council and Children)
# scope:disobedient_kurultai - list all disobedient characters who were part of the Kurultai Election (both Council and Children)
#
on_kurultai_succession_chaotic = {
	trigger = {
		has_realm_law = single_heir_succession_kurultai_law
		primary_title = {
			title_held_years < 1
		}
	}
	effect = {
		root = {
			if = {
				limit = {
					exists = this
					"list_size(obedient_kurultai)" > 0
				}
				every_in_list = {
					list = obedient_kurultai
					limit = {
						exists = this
						NOT = { this = root }
					}
					add_opinion = {
						target = root
						modifier = obedience_opinion
						years = 5
					}
				}
			}
			# Siblings gain claims on all your titles
			every_held_title = {
				limit = {
					tier >= tier_county
				}
				save_scope_as = title_claim
				root = {
					every_sibling = {
						limit = {
							OR = {
								AND = {
									is_male = yes
									faith_dominant_gender_male_or_equal = yes
								}
								AND = {
									is_female = yes
									faith_dominant_gender_female_or_equal = yes
								}
							}
							NOT = {
								has_claim_on = scope:title_claim
							}
						}
						add_pressed_claim = scope:title_claim
						add_to_list = ai_claimant_faction_preference
					}
					every_in_list = {
						list = disobedient_kurultai
						limit = {
							OR = {
								AND = {
									is_male = yes
									faith_dominant_gender_male_or_equal = yes
								}
								AND = {
									is_female = yes
									faith_dominant_gender_female_or_equal = yes
								}
							}
							NOT = {
								has_claim_on = scope:title_claim
							}
						}
						add_pressed_claim = scope:title_claim
						add_to_list = ai_claimant_faction_preference
					}
				}
			}
			random_in_list = {
				list = ai_claimant_faction_preference
				limit = {
					is_physically_able = yes
				}
				weight = {
					base = 1
					modifier = {
						add = martial
					}
					modifier = {
						add = prowess
					}
					modifier = {
						add = dread
					}
					modifier = {
						add = -250
						is_adult = no
					}
				}
				add_character_flag = nomad_preferred_claimant
			}
			create_character_memory = {
				type = had_chaotic_kurultai_succession
			}
		}
	}
	random_on_action = {
		10 = kurultai_succession_chaotic_minor
		20 = kurultai_succession_chaotic_medium
		90 = kurultai_succession_chaotic_major
	}
}

kurultai_succession_chaotic_minor = {
	trigger = {
		# No trigger for Minor penalties
		NOT = {
			has_character_flag = had_mongolia_split
		}
	}
	weight_multiplier = {
		base = 1
		modifier = { # be a bit kinder to the AI
			factor = 2
			is_ai = yes
		}
	}
	random_events = {
		100 = mpo_chaotic_kurultai_succession.0001 # Siblings become your rivals
		100 = mpo_chaotic_kurultai_succession.0002 # Brother seizes the lands of a Tributary and becomes independent
		100 = mpo_chaotic_kurultai_succession.0003 # Tributary leaves
		100 = mpo_chaotic_kurultai_succession.0004 # County Control loss
		100 = mpo_chaotic_kurultai_succession.0005 # Loss of Prestige Level
		100 = mpo_chaotic_kurultai_succession.0006 # 10% of Herd defects to Neighboring realm
		100 = mpo_chaotic_kurultai_succession.0007 # Good Courtiers defect to Neighbor
		100 = mpo_chaotic_kurultai_succession.0008 # MaA Regiment defects to neighbor
	}
}

kurultai_succession_chaotic_medium = {
	trigger = {
		OR = {
			"list_size(disobedient_kurultai)" >= 2
			"list_size(obedient_kurultai)" <= 0
		}
		OR = {
			highest_held_title_tier >= tier_kingdom
			any_tributary = {
				count >= 10
			}
		}
		NOT = {
			has_character_flag = had_mongolia_split
		}
	}
	random_events = {
		100 = mpo_chaotic_kurultai_succession.0009 # Gold divided by Siblings
		100 = mpo_chaotic_kurultai_succession.0010 # Legitimacy -500
		100 = mpo_chaotic_kurultai_succession.0011 # 20% of Herd defects to Neighboring realm
		300 = mpo_chaotic_kurultai_succession.0013 # 20% Realm Split
		100 = mpo_chaotic_kurultai_succession.0016 # 3 Tributaries leave
	}
}

kurultai_succession_chaotic_major = {
	trigger = {
		OR = {
			"list_size(disobedient_kurultai)" >= "list_size(obedient_kurultai)"
			"list_size(obedient_kurultai)" <= 0
		}
		OR = {
			highest_held_title_tier >= tier_kingdom
			any_tributary = {
				count >= 10
			}
		}
		NOT = {
			has_character_flag = had_mongolia_split
		}
	}
	weight_multiplier = {
		base = 1
		modifier = { # be a bit kinder to the AI
			factor = 0.5
			is_ai = yes
		}
	}
	random_events = {
		300 = mpo_chaotic_kurultai_succession.0012 # 50% Split realm De Jure with Sibling
		50 = mpo_chaotic_kurultai_succession.0014 # Extreme kurultai disapproval: shatter realm
		100 = mpo_chaotic_kurultai_succession.0015 # 40% of Herd defects to Neighboring realm
		100 = mpo_chaotic_kurultai_succession.0017 # All Tributaries leave
	}
}

mpo_tributary_check_pulse = {
	trigger = {
		is_landed = no
		is_migrating = no
		is_at_war = no
		number_of_tributaries > 0
	}

	effect = {
		every_tributary = {
			root = {
				send_interface_message = {
					type = msg_tributaries_breaking_free_bad
					title = tributaries_breaking_free_message_title
					left_icon = root
					right_icon = prev
					show_as_tooltip = {
						prev = { end_tributary = yes }
					}
				}
			}
			send_interface_message = {
				type = msg_tributaries_breaking_free_good
				title = tributaries_breaking_free_message_player_title
				left_icon = this
				right_icon = root
				end_tributary = yes
			}
		}
	}
}

mpo_de_jure_shift = {
	trigger = {
		government_has_flag = government_is_nomadic
		highest_held_title_tier >= tier_kingdom
	}
	effect = {
		switch = {
			trigger = root.highest_held_title_tier
			tier_empire = {
				de_jure_shifting_effect = {
					UPPER_TIER = empire
					LOWER_TIER = kingdom
				}
			}
			tier_kingdom = {
				de_jure_shifting_effect = {
					UPPER_TIER = kingdom
					LOWER_TIER = duchy
				}
			}
		}
	}
}

# root - Character who changed government
# scope:old_government - Character's old government type
on_government_change = {
	effect = {
		if = {
			limit = {
				scope:old_government = government_type:nomad_government
				NOT = { government_has_flag = government_allows_nomad_domicile_titles }
			}
			
			every_held_title = {
				limit = { is_nomad_title = yes }
				root = {
					destroy_title = prev
				}
			}
		}
		if = {
			limit = {
				is_confederation_member = yes
				NAND = {
					government_is_japanese_trigger = yes
					exists = house.house_confederation
				}
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_tribal
				}				
			}
			confederation = {
				remove_confederation_member = root
			}
		}
		if = {
			limit = {
				has_variable = crowned_emperor_var
				NOR = {
					has_game_rule = coronation_laws_off
					has_realm_law = crowned_emperor
				}
			}
			add_realm_law = crowned_emperor
		}
		if = {
			limit = {
				has_variable = crowned_king_var
				NOR = {
					has_game_rule = coronation_laws_off
					has_realm_law = crowned_king
				}
			}
			add_realm_law = crowned_king
		}
		if = {
			limit = { scope:old_government = government_type:mandala_government }
			#Remove Aspect modifiers/variables
			if = {
				limit = { has_character_modifier = mandala_increments_of_peace_modifier }
				remove_character_modifier = mandala_increments_of_peace_modifier
			}
			if = {
				limit = { has_character_modifier = mandala_creator_modifier }
				remove_character_modifier = mandala_creator_modifier
			}
			remove_variable = mandala_house_power_accumulated_increments_of_peace
			remove_variable = mandala_house_power_accumulated_creator
			#Remove max piety cap modifiers/variables
			if = {
				limit = { has_character_modifier = budding_divinity_modifier }
				remove_character_modifier = budding_divinity_modifier
			}
			if = {
				limit = { has_character_modifier = budding_deity_modifier }
				remove_character_modifier = budding_deity_modifier
			}
			if = {
				limit = { has_character_modifier = budding_godhood_modifier }
				remove_character_modifier = budding_godhood_modifier
			}
			remove_variable = not_subject_to_succession_trials
			remove_variable = tribute_mission_grace
		}
		if = {
			limit = { 
				government_has_flag = government_is_mandala
				is_house_head = yes
			}
			#Set the house aspect to default, please
			house = {
				set_house_aspiration = { type = no_aspect }
			}
		}
	}
}
