﻿# $TRAIT$: beauty, intellect, or physique
set_qol_trait_group_variable = {
	switch = {
		trigger = has_trait
		$TRAIT$_good_3 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = 3
			}
			make_trait_inactive = $TRAIT$_good_3
		}
		$TRAIT$_good_2 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = 2
			}
			make_trait_inactive = $TRAIT$_good_2
		}
		$TRAIT$_good_1 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = 1
			}
			make_trait_inactive = $TRAIT$_good_1
		}
		$TRAIT$_bad_1 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = -1
			}
			make_trait_inactive = $TRAIT$_bad_1
		}
		$TRAIT$_bad_2 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = -2
			}
			make_trait_inactive = $TRAIT$_bad_2
		}
		$TRAIT$_bad_3 = {
			set_variable = {
				name = qol_child_$TRAIT$
				value = -3
			}
			make_trait_inactive = $TRAIT$_bad_3
		}
	}
}
set_qol_trait_variable = {
	if = {
		limit = { has_trait = $TRAIT$ }
		set_variable = {
			name = qol_child_$TRAIT$
			value = yes
		}
		make_trait_inactive = $TRAIT$
	}
}
# $TRAIT$: beauty, intellect, or physique
# var:qol_child_$TRAIT$ must exists, else nothing happens
# if trait is not inactive(doesn't have the trait), adds it
make_qol_trait_group_active = {
	if = {
		limit = { exists = var:qol_child_$TRAIT$ }
		if = {
			limit = { var:qol_child_$TRAIT$ > 0 } #good
			switch = {
				trigger = has_inactive_trait
				$TRAIT$_good_1 = { make_trait_active = $TRAIT$_good_1 }
				$TRAIT$_good_2 = { make_trait_active = $TRAIT$_good_2 }
				$TRAIT$_good_3 = { make_trait_active = $TRAIT$_good_3 }
				fallback = {
					change_trait_rank = {
						trait = $TRAIT$_good
						rank = var:qol_child_$TRAIT$
						max = 3
					}
				}
			}
		}
		else = {
			switch = {
				trigger = has_inactive_trait
				$TRAIT$_bad_1 = { make_trait_active = $TRAIT$_bad_1 }
				$TRAIT$_bad_2 = { make_trait_active = $TRAIT$_bad_2 }
				$TRAIT$_bad_3 = { make_trait_active = $TRAIT$_bad_3 }
				fallback = {
					change_trait_rank = {
						trait = $TRAIT$_bad
						rank = {
							value = var:qol_child_$TRAIT$
							multiply = -1
						}
						max = 3
					}
				}
			}
		}
		remove_variable = qol_child_$TRAIT$
	}
}
make_qol_trait_active = {
	if = {
		limit = { exists = var:qol_child_$TRAIT$ }
		if = {
			limit = { has_inactive_trait = $TRAIT$ }
			make_trait_active = $TRAIT$
		}
		else = {
			add_trait = $TRAIT$
		}
		remove_variable = qol_child_$TRAIT$
	}
}


#scope:army should exist
add_army_commander_modifier = {
	scope:army ?= {
		army_commander ?= {
			add_character_modifier = {
				modifier = $MODIFIER$
				days = 30
			}
		}
	}
}

replenish_on_going_revolt = {
	if = {
		limit = { has_variable = rebel_leader_peasants }
		var:rebel_leader_peasants.faction_war = { save_temporary_scope_as = revolt_war }
		location = { save_temporary_scope_as = local_center_of_rebellion }
		random = {
			chance = 25
			modifier = {
				has_game_rule = all_difficulty
				add = 10
			}
			modifier = { # the revolting is always attacker
				exists = scope:revolt_war
				add = {
					scope:revolt_war = {
						value = attacker_war_score # my score -100 to 100
						subtract = {
							value = defender_war_score # your score 100 to -100
							if = {
								limit = {
									OR = {
										has_game_rule = easy_difficulty
										has_game_rule = very_easy_difficulty
									}
								}
								divide = 2
							}
						}
					}
				}
			}
			# effect modified from spawn_popular_revolt_troops
			# Spawn Levies
			spawn_army = {
				levies = {
					value = 0
					joined_faction = {
						every_faction_county_member = {
							add = county_levies_to_raise
						}
					}
				}
				location = scope:local_center_of_rebellion
				war = scope:revolt_war
				name = populist_faction_event_troops
			}
			
			# Spawn MAAs - An appropriate amount of troops dependent on the terrain type they spawn in
			if = {
				limit = {
					scope:local_center_of_rebellion = {
						OR = {
							terrain = forest
							terrain = taiga
							terrain = jungle
							terrain = plains
							terrain = drylands
							terrain = oasis
							terrain = desert
							terrain = wetlands
							terrain = steppe
						}
					}
				}
				spawn_army = {
					men_at_arms = {
						type = light_footmen
						stacks = {
							value = 0
							joined_faction = {
								every_faction_county_member = {
									add = county_maa_to_raise
								}
							}
						}
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
			else_if = {
				limit = {
					scope:local_center_of_rebellion = {
						OR = {
							terrain = mountains
							terrain = desert_mountains
						}
					}
				}
				spawn_army = {
					men_at_arms = {
						type = pikemen_unit
						stacks = {
							value = 0
							joined_faction = {
								every_faction_county_member = {
									add = county_maa_to_raise
								}
							}
						}
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
			else_if = {
				limit = {
					scope:local_center_of_rebellion = {
						OR = {
							terrain = hills
							terrain = farmlands
							terrain = floodplains
						}
					}
				}
				spawn_army = {
					men_at_arms = {
						type = bowmen
						stacks = {
							value = 0
							joined_faction = {
								every_faction_county_member = {
									add = county_maa_to_raise
								}
							}
						}
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
			
			# Spawn Siege Weapons depending on discovered innovations
			if = {
				limit = {
					culture = { has_innovation = innovation_gunpowder }
				}
				spawn_army = {
					men_at_arms = {
						type = bombard
						stacks = 1
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
			else_if = {
				limit = {
					culture = { has_innovation = innovation_trebuchet }
				}
				spawn_army = {
					men_at_arms = {
						type = trebuchet
						stacks = 1
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
				
			}
			else_if = {
				limit = {
					culture = { has_innovation = innovation_mangonel }
				}
				spawn_army = {
					men_at_arms = {
						type = mangonel
						stacks = 1
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
			else_if = {
				limit = {
					culture = { has_innovation = innovation_catapult }
				}
				spawn_army = {
					men_at_arms = {
						type = onager
						stacks = 1
					}
					location = scope:local_center_of_rebellion
					war = scope:revolt_war
					name = populist_faction_event_troops
				}
			}
		}
	}
}

set_intellect_trait_xp = {
	switch = {
		trigger = has_trait
		intellect_good_1 = { set_intellect_rank_xp = { RANK = 1 } }
		intellect_good_2 = { set_intellect_rank_xp = { RANK = 2 } }
		intellect_good_3 = { set_intellect_rank_xp = { RANK = 3 } }
	}
}
set_intellect_rank_xp = {
	set_intellect_track_xp = {
		RANK = $RANK$
		TRACK = diplomacy
	}
	set_intellect_track_xp = {
		RANK = $RANK$
		TRACK = martial
	}
	set_intellect_track_xp = {
		RANK = $RANK$
		TRACK = stewardship
	}
	set_intellect_track_xp = {
		RANK = $RANK$
		TRACK = intrigue
	}
	set_intellect_track_xp = {
		RANK = $RANK$
		TRACK = learning
	}
}
set_intellect_track_xp = {
	# clear
	add_trait_xp = {
		trait = intellect_good_$RANK$
		track = $TRACK$
		value = -100
	}
	# reset
	add_trait_xp = {
		trait = intellect_good_$RANK$
		track = $TRACK$
		value = {
			value = intellect_$TRACK$_exp
			switch = {
				trigger = has_game_rule
				mastery_at_twenty = { divide = 20 }
				mastery_at_thirty = { divide = 30 }
				mastery_at_forty = { divide = 40 }
				mastery_at_sixty = { divide = 60 }
				fallback = { divide = 100 }
			}
			multiply = 100
			floor = yes
			min = 1
			max = 100
		}
	}
}

rotate_excess_lifestyle_experience = {
	random_list = {
		0 = {
			modifier = {
				perks_in_tree = { tree = family value = 9 }
				perks_in_tree = { tree = majesty value = 9 }
				perks_in_tree = { tree = foreign_affairs value = 9 }
				#perks_in_diplomacy_lifestyle >= 27
				add = diplomacy_lifestyle_perk_points
			}
			while = {
				count = diplomacy_lifestyle_perk_points
				add_diplomacy_lifestyle_perk_points = -1
				add_intrigue_lifestyle_xp = 500
				add_learning_lifestyle_xp = 500
			}
		}
		0 = {
			modifier = {
				perks_in_tree = { tree = chivalry value = 9 }
				perks_in_tree = { tree = authority value = 9 }
				perks_in_tree = { tree = strategy value = 9 }
				#perks_in_martial_lifestyle >= 27
				add = martial_lifestyle_perk_points
			}
			while = {
				count = martial_lifestyle_perk_points
				add_martial_lifestyle_perk_points = -1
				add_learning_lifestyle_xp = 1000
			}
		}
		0 = {
			modifier = {
				perks_in_tree = { tree = domain value = 9 }
				perks_in_tree = { tree = wealth value = 9 }
				perks_in_tree = { tree = duty value = 9 }
				#perks_in_stewardship_lifestyle >= 27
				add = stewardship_lifestyle_perk_points
			}
			while = {
				count = stewardship_lifestyle_perk_points
				add_stewardship_lifestyle_perk_points = -1
				add_martial_lifestyle_xp = 333
				add_intrigue_lifestyle_xp = 333
				add_learning_lifestyle_xp = 334
			}
		}
		0 = {
			modifier = {
				perks_in_tree = { tree = intimidation value = 9 }
				perks_in_tree = { tree = temptation value = 9 }
				perks_in_tree = { tree = skulduggery value = 9 }
				#perks_in_intrigue_lifestyle >= 27
				add = intrigue_lifestyle_perk_points
			}
			while = {
				count = intrigue_lifestyle_perk_points
				add_intrigue_lifestyle_perk_points = -1
				add_diplomacy_lifestyle_xp = 333
				add_stewardship_lifestyle_xp = 333
				add_learning_lifestyle_xp = 334
			}
		}
		0 = {
			modifier = {
				perks_in_tree = { tree = theology value = 9 }
				perks_in_tree = { tree = scholarship value = 9 }
				perks_in_tree = { tree = medicine value = 9 }
				#perks_in_learning_lifestyle >= 27
				add = learning_lifestyle_perk_points
			}
			while = {
				count = learning_lifestyle_perk_points
				add_learning_lifestyle_perk_points = -1
				add_diplomacy_lifestyle_xp = 250
				add_martial_lifestyle_xp = 250
				add_stewardship_lifestyle_xp = 250
				add_intrigue_lifestyle_xp = 250
			}
		}
	}
}

#save_county_faith = {
#	set_variable = {
#		name = qol_static_faith
#		value = faith
#	}
#}
#save_county_culture = {
#	set_variable = {
#		name = qol_static_culture
#		value = culture
#	}
#}
revert_county_faith = {
	if = {
		limit = {
			NOT = { exists = var:qol_revert_faith } # here to prevent potential on_action looping
			exists = scope:old_faith #var:qol_static_faith
			#var:qol_static_faith != faith
		}
		set_variable = qol_revert_faith
		set_county_faith = scope:old_faith #var:qol_static_faith
	}
	else = {
		remove_variable = qol_revert_faith
	}
}
revert_county_culture = {
	if = {
		limit = {
			NOT = { exists = var:qol_revert_culture }
			exists = scope:old_culture #var:qol_static_culture
			#var:qol_static_culture != culture
		}
		set_variable = qol_revert_culture
		set_county_culture = scope:old_culture #var:qol_static_culture
	}
	else = {
		remove_variable = qol_revert_culture
	}
}

# imported from mr_mod, note to self: be sure to sync them
#create a list of all ancestors of $GEN$ generations or up until $ANCESTOR$, inclusing the deceased
#input: $GEN$ scope:ancestor
#returns the list: all_ancestors
find_all_ancestors = {
	save_scope_as = var_root
	add_to_variable_list = { name = all_ancestors target = this }
	while = {
		count = $GEN$
		limit = {
			trigger_if = {
				limit = { exists = scope:ancestor }
				NOT = { is_target_in_variable_list = { name = all_ancestors target = scope:ancestor } }
			}
			always = yes
		}
		every_in_list = {
			variable = all_ancestors
			every_parent = {
				even_if_dead = yes
				limit = {
					NOT = {
						scope:var_root = {
							is_target_in_variable_list = { name = all_ancestors target = prev }
						}
					}
				}
				scope:var_root = { add_to_variable_list = { name = all_ancestors target = prev } }
			}
		}
	}
	remove_list_variable = { name = all_ancestors target = this }
	clear_saved_scope = var_root
}

# return scopes: scope:this_character, scope:chosen_character
find_next_player_character = {
	find_all_ancestors = { GEN = 6 } #this is exponential, keep it small
	save_scope_as = this_character
	random_ruler = {
		limit = {
			NOT = { this = scope:this_character }
			is_playable_character = yes
			NOT = { government_has_flag = government_is_republic }
			
			is_healthy = yes # dying soon again is not fun
			switch = {
				trigger = has_game_rule
				randomize_player_global = { always = yes }
				randomize_player_regional = { in_diplomatic_range = prev } # simple enough
				randomize_player_neighbor = { # now...
					# get old location
					prev = {
						# this goes: location exists, goes into location scope and save it
						switch = {
							trigger = exists
							location = { location = { save_temporary_scope_as = t_province } }
							default_location = { default_location = { save_temporary_scope_as = t_province } }
							#capital_province
							#domicile.domicile_location
						}
					}
					# get new location
					switch = {
						trigger = exists
						location = { location = { save_temporary_scope_as = t_province } }
						default_location = { default_location = { save_temporary_scope_as = t_province } }
					}
					# check that they are in neighboring realms
					scope:t_province.county.holder.top_liege = {
						any_neighboring_and_across_water_top_liege_realm_owner = {
							this = scope:n_province.county.holder.top_liege
						}
					}
				}
				randomize_player_realm = { # use location to include landless rulers
					# get old base location
					prev.default_location = { save_temporary_scope_as = t_province }
					# get new base location
					default_location = { save_temporary_scope_as = n_province }
					# check that they are in neighboring realms
					scope:t_province.county.holder.top_liege = scope:n_province.county.holder.top_liege
				}
				randomize_player_disabled = { always = no } # You're not supposed to be in here!!!
			}
			switch = {
				trigger = has_game_rule
				randomize_player_any_society = { always = yes }
				randomize_player_same_faith = { faith = prev.faith }
				randomize_player_same_culture = { culture = prev.culture }
				randomize_player_same_society = {
					faith = prev.faith
					culture = prev.culture
				}
			}
		}
		add_to_list = valid_rulers
	}
	random_in_list = {
		list = valid_rulers
		weight = {
			base = 1
			modifier = { # raise base so when the pool is small, we maintain randomness
				list_size:valid_rulers <= 1000
				add = {
					value = 5000
					divide = list_size:valid_rulers
					floor = yes
					max = 20
				}
			}
			modifier = { # raise base: there's so few of them...
				government_has_flag = government_is_landless_adventurer
				add = 20
			}
			modifier = { # preferably younger <_<
				always = yes
				add = {
					value = scope:this_character.effective_age
					subtract = effective_age
					max = 20
					min = 0
				}
			}
			# make it interesting
			modifier = { # forgotten lineage?
				OR = { # far enough
					has_game_rule = randomize_player_global
					has_game_rule = randomize_player_regional
				}
				effective_age < 30 # reduce total comparisons... you don't want to play someone old anyway
				is_lowborn = no
				NOT = { is_close_or_extended_family_of = scope:this_character } # not interesting
				## translation: one of my ancestor came from this dynasty
				save_temporary_scope_as = selected
				scope:this_character = {
					any_in_list = {
						variable = all_ancestors
						is_lowborn = no
						trigger_if = {
							limit = {
								dynasty = scope:selected.dynasty
								NOT = { exists = scope:any_found }
							}
							save_temporary_scope_as = any_found # end search
						}
					}
				}
				add = {
					value = 30 # leap-years allocated(120 years)
					#subtract = { # minus the time gap between them: we reward ones closer and more relevant to the present
					#	value = scope:this_character.birth_date
					#	subtract = scope:any_found.birth_date ## game unable to access scope:any_found :(
					#	divide = 1460 # 4*365 convert to leap-years >.< +1?
					#	floor = yes
					#}
					max = 20 # max bonus(four generations)
					min = 0
				}
			}
			modifier = { # old friend's family?
				any_close_family_member = {}
				add = {
					every_close_family_member = {
						if = {
							limit = {
								OR = {
									has_relation_rival = scope:this_character
									has_relation_nemesis = scope:this_character
								}
							}
							save_temporary_scope_as = enemy
						}
						if = {
							limit = {
								OR = {
									has_relation_oaf = scope:this_character
									has_relation_victim = scope:this_character
									has_relation_bully = scope:this_character
									has_relation_grudge = scope:this_character
									has_relation_potential_rival = scope:this_character
								}
							}
							save_temporary_scope_as = bad_blood
						}
						if = {
							limit = {
								OR = {
									has_relation_elder = scope:this_character
									has_relation_guardian = scope:this_character
									has_relation_mentor = scope:this_character
									has_relation_disciple = scope:this_character
									has_relation_ward = scope:this_character
									has_relation_student = scope:this_character
								}
							}
							save_temporary_scope_as = teacher_student
						}
						if = {
							limit = {
								OR = {
									has_relation_fellow_disciple = scope:this_character
									has_relation_friend = scope:this_character
									has_relation_potential_lover = scope:this_character
									has_relation_crush = scope:this_character
								}
							}
							save_temporary_scope_as = peer
						}
						if = {
							limit = {
								OR = {
									has_relation_blood_brother = scope:this_character
									has_relation_best_friend = scope:this_character
									has_relation_soldier_friend = scope:this_character
								}
							}
							save_temporary_scope_as = close_peer
						}
						if = {
							limit = {
								OR = {
									has_relation_soulmate = scope:this_character
									has_relation_lover = scope:this_character
									has_relation_old_flame = scope:this_character
								}
							}
							save_temporary_scope_as = intimate
						}
					}
					switch = {
						trigger = exists
						scope:enemy = { value = 20 }
						scope:bad_blood = { value = 15 }
						scope:teacher_student = { value = 15 }
						scope:peer = { value = 15 }
						scope:close_peer = { value = 20 }
						scope:intimate = { value = 20 }
					}
				}
			}
			modifier = { # cuckold? romantic rival? murderer of family? war participant?
				OR = {
					any_child = {
						even_if_dead = yes
						father = prev
						real_father = scope:this_character
					}
					any_spouse = {
						OR = {
							has_relation_soulmate = scope:this_character
							has_relation_lover = scope:this_character
							has_relation_old_flame = scope:this_character
						}
					}
					any_relation = {
						type = soulmate
						type = lover
						type = old_flame
						OR = {
							has_relation_soulmate = scope:this_character
							has_relation_lover = scope:this_character
							has_relation_old_flame = scope:this_character
						}
					}
					any_close_or_extended_family_member = {
						even_if_dead = yes
						is_alive = no
						killer = scope:this_character
					}
					any_war_enemy = { this = scope:this_character }
					any_war_ally = { this = scope:this_character }
				}
				add = 20
			}
		}
		save_scope_as = chosen_character
	}
	clear_saved_scope = this_character
	clear_variable_list = all_ancestors
}