﻿# $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
		}
	}
}

#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 = {
			exists = scope:old_faith #var:qol_static_faith
			#var:qol_static_faith != faith
		}
		set_county_faith = scope:old_faith #var:qol_static_faith
	}
}
revert_county_culture = {
	if = {
		limit = {
			exists = scope:old_culture #var:qol_static_culture
			#var:qol_static_culture != culture
		}
		set_county_culture = scope:old_culture #var:qol_static_culture
	}
}