﻿# Called from code once a year for "playable" (count+) characters.
# Root is the character
yearly_playable_pulse = {
	on_actions = {
		on_insane_ai
	}
}

on_insane_ai = {
	trigger = {
		is_ai = yes
		has_game_rule = all_difficulty
	}
	effect = {
		random_list = {
			1 = {
				trigger_event = { on_action = ai_marriage_pulse }
			}
			4 = {
				trigger = { age < 25 }
				trigger_event = { on_action = ai_marriage_pulse }
			}
			5 = { #nothing happens
				modifier = {
					any_child = {}
					add = {
						value = 0
						every_child = {
							add = 1
						}
					}
				}
			}
		}
	}
	on_actions = {
		ai_succession_pulse
	}
}

ai_succession_pulse = {
	trigger = {
		can_designate_heir_trigger = yes
		OR = {
			has_character_flag = new_heir_candidate
			AND = {
				save_temporary_scope_value_as = {
					name = age_check
					value = {
						value = root.age
						modulo = 3
					}
				}
				scope:age_check = 0 #when the character's age is divisible by 3
			}
		}
	}
	effect = {
		remove_character_flag = new_heir_candidate
		random_child = {
			limit = {
				num_of_bad_genetic_traits = 0
				is_valid_designated_heir = root
				#is_heir_of = root
			}
			weight = {
				base = 5
				#opinion of heir
				modifier = {
					root = {
						save_temporary_opinion_value_as = {
							name = opinion_of_heir
							target = prev
						}
					}
					exists = scope:opinion_of_heir
					add = scope:opinion_of_heir
				}
				#preferred personality
				modifier = {
					has_trait = brave
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = brave } }
							add = 10
						}
						else_if = {
							limit = { root = { has_trait = craven } }
							add = -10
						}
					}
				}
				modifier = {
					has_trait = craven
					add = {
						value = -15
						if = {
							limit = { root = { has_trait = brave } }
							add = -10
						}
						else_if = {
							limit = { root = { has_trait = craven } }
							add = 15
						}
					}
				}
				modifier = {
					has_trait = diligent
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = diligent } }
							add = 10
						}
					}
				}
				modifier = {
					has_trait = lazy
					add = {
						value = -15
						if = {
							limit = { root = { has_trait = diligent } }
							add = -15
						}
						else_if = {
							limit = { root = { has_trait = lazy } }
							add = 15
						}
					}
				}
				modifier = {
					has_trait = temperate
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = temperate } }
							add = 10
						}
						else_if = {
							limit = { root = { has_trait = gluttonous } }
							add = -25
						}
					}
				}
				modifier = {
					has_trait = gluttonous
					add = {
						value = -15
						if = {
							limit = { root = { has_trait = temperate } }
							add = -10
						}
						else_if = {
							limit = { root = { has_trait = gluttonous } }
							add = 15
						}
					}
				}
				modifier = {
					has_trait = gregarious
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = gregarious } }
							add = 10
						}
					}
				}
				modifier = {
					has_trait = shy
					add = {
						value = -15
						if = {
							limit = { root = { has_trait = shy } }
							add = 15
						}
					}
				}
				modifier = {
					has_trait = ambitious
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = ambitious } }
							add = 10
						}
						else_if = {
							limit = { root = { has_trait = content } }
							add = -25
						}
					}
				}
				modifier = {
					has_trait = content
					add = {
						value = -15
						if = {
							limit = { root = { has_trait = ambitious } }
							add = -10
						}
						else_if = {
							limit = { root = { has_trait = content } }
							add = 25
						}
					}
				}
				#preferred traits
				modifier = {
					has_trait = lifestyle_herbalist
					add = {
						value = 5
						if = {
							limit = { root = { is_witch_trigger = yes } }
							add = 10
						}
					}
				}
				modifier = {
					has_trait = lifestyle_gardener
					add = {
						value = 10
						if = {
							limit = { root = { has_trait = lifestyle_gardener } }
							add = 5
						}
					}
				}
				modifier = {
					has_trait = lifestyle_reveler
					add = {
						value = 15
						if = {
							limit = { root = { has_trait = lifestyle_reveler } }
							add = 10
						}
					}
				}
				modifier = {
					has_trait = lifestyle_mystic
					add = 10
				}
				modifier = {
					has_trait = lifestyle_hunter
					add = 10
				}
				modifier = {
					has_trait = lifestyle_blademaster
					add = 10
				}
				modifier = {
					has_trait = shrewd
					add = 20
				}
				modifier = {
					has_trait = dull
					add = -20
				}
				modifier = {
					has_trait = strong
					add = 10
				}
				modifier = {
					has_trait = weak
					add = -5
				}
				modifier = {
					root = { ai_has_warlike_personality = yes }
					add = {
						value = number_of_commander_traits
						multiply = 15
					}
				}
				#skills
				modifier = {
					martial >= 0
					add = {
						value = martial
						if = {
							limit = { root = { ai_has_warlike_personality = yes } }
							multiply = 2
						}
						round = yes
					}
				}
				modifier = {
					prowess >= 0
					add = {
						value = prowess
						if = {
							limit = { root = { ai_has_warlike_personality = yes } }
							multiply = 1.5
						}
						else = { multiply = 0.5 }
						round = yes
					}
				}
				modifier = {
					diplomacy >= 0
					add = {
						value = diplomacy
						if = {
							limit = { root = { ai_has_cautious_personality = yes } }
							multiply = 2
						}
						round = yes
					}
				}
				modifier = {
					stewardship >= 0
					add = {
						value = stewardship
						if = {
							limit = { root = { ai_has_economical_boom_personality = yes } }
							multiply = 2
						}
						round = yes
					}
				}
				modifier = {
					learning >= 0
					add = {
						value = learning
						if = {
							limit = { root = { ai_has_economical_boom_personality = yes } }
							multiply = 2
						}
						else = { multiply = 0.5 }
						round = yes
					}
				}
				modifier = {
					intrigue >= 0
					add = {
						value = intrigue
						if = {
							limit = { root = { ai_has_cautious_personality = yes } }
							multiply = 2
						}
						else = { multiply = 0.5 }
						round = yes
					}
				}
				#other stats - health, fertility, prestige, and etc.
				modifier = {
					is_alive = yes
					add = {
						value = health
						add = -3
						multiply = 10
						max = 30
					}
				}
				modifier = {
					fertility < 0.5
					add = -5
				}
				modifier = {
					fertility < 0.1
					add = -15
				}
				modifier = {
					is_alive = yes
					add = {
						value = prestige_level
						multiply = 10
					}
				}
				modifier = {
					root = { ai_zeal > low_negative_ai_value }
					add = {
						value = piety_level
						multiply = {
							value = root.ai_zeal
							max = dominant_positive_ai_value
							divide = dominant_positive_ai_value
							multiply = 20
							floor = yes
							min = 5
						}
					}
				}
				#alignment
				modifier = {
					NOT = { has_culture = root.culture }
					factor = 0.5
				}
				modifier = {
					NOT = { has_faith = root.faith }
					factor = 0.5
				}
				#good genes
				modifier = {
					has_trait = intellect_good_1
					factor = 1.25
				}
				modifier = {
					has_trait = intellect_good_2
					factor = 1.5
				}
				modifier = {
					has_trait = intellect_good_3
					factor = 2
				}
				modifier = {
					has_trait = beauty_good_1
					factor = 1.25
				}
				modifier = {
					has_trait = beauty_good_2
					factor = 1.5
				}
				modifier = {
					has_trait = beauty_good_3
					factor = 2
				}
				modifier = {
					has_trait = physique_good_1
					factor = 1.25
				}
				modifier = {
					has_trait = physique_good_2
					factor = 1.5
				}
				modifier = {
					has_trait = physique_good_3
					factor = 2
				}
				modifier = {
					OR = {
						num_of_good_genetic_traits > 1
						any_child = { num_of_good_genetic_traits > 1 }
					}
					factor = 2
				}
				modifier = {
					OR = {
						num_of_good_genetic_traits > 2
						any_child = { num_of_good_genetic_traits > 2 }
					}
					factor = 4
				}
				modifier = {
					OR = {
						num_of_good_genetic_traits > 3
						any_child = { num_of_good_genetic_traits > 3 }
					}
					factor = 6
				}
			}
			save_scope_as = chosen_heir
		}
		if = {
			limit = { exists = scope:chosen_heir }
			set_designated_heir = scope:chosen_heir
			clear_saved_scope = chosen_heir
		}
	}
}

ai_marriage_pulse = {
	trigger = {
		has_game_rule = all_difficulty
		fertility > low_fertility
		is_married = no
		is_betrothed = no
		can_marry_trigger = yes
	}
	effect = {
		every_opposite_sex_spouse_candidate = {
			limit = {
				is_married = no
				in_diplomatic_range = root
				OR ={
					has_trait = beauty_good
					has_trait = intellect_good
					has_trait = physique_good
					has_trait = pure_blooded
					has_trait = fecund
				}
				trigger_if = {
					limit = { is_ruler = yes }
					highest_held_title_tier <= prev.highest_held_title_tier
					opinion = {
						target = prev
						value >= medium_positive_opinion
					}
				}
				NAND = {
					is_landless_ruler = yes
					faith.religious_head = this
				}
				NOR = {
					is_in_list = ai_spouses
					trigger_if = {
						limit = { is_lowborn = no }
						any_player = {
							house = prev.house
						}
					}
				}
				age > { # root is older
					value = root.age
					if = {
						limit = { is_female = yes } # root is male
						subtract = 15
					}
					else = {
						subtract = 5
					}
				}
				age < { # root is younger
					value = root.age
					if = {
						limit = { is_female = no } # root is female
						add = 15
					}
					else = {
						add = 5
					}
				}
			}
			add_to_list = ai_spouses
		}
		random_in_list = {
			list = ai_spouses
			limit = { any_liege_or_above = { this = root } }
			weight = {
				base = 1
				#status matters
				modifier = {
					any_parent = { is_ruler = yes }
					add = {
						value = 2
						if = {
							limit = { any_parent = { highest_held_title_tier = tier_kingdom } }
							add = 6
						}
						else_if = {
							limit = { any_parent = { highest_held_title_tier = tier_duchy } }
							add = 2
						}
					}
				}
				#culture
				modifier = {
					culture = root.culture
					add = 1
				}
				modifier = {
					has_trait = beauty_good_2
					add = 10
				}
				modifier = {
					has_trait = beauty_good_3
					add = 100
				}
				modifier = {
					has_trait = intellect_good_2
					add = 10
				}
				modifier = {
					has_trait = intellect_good_3
					add = 100
				}
				modifier = {
					has_trait = physique_good_2
					add = 10
				}
				modifier = {
					has_trait = physique_good_3
					add = 100
				}
				# good genes
				modifier = {
					has_trait = pure_blooded
					factor = 1.5
				}
				modifier = {
					has_trait = inbred
					factor = 0
				}
				modifier = {
					has_trait = celibate
					factor = 0
				}
			}
			save_scope_as = target_spouse
		}
		if = {
			limit = { exists = scope:target_spouse }
			if = {
				limit = {
					is_adult = yes
					scope:target_spouse = { is_adult = yes }
				}
				if = {
					limit = { is_female = yes }
					marry_matrilineal = scope:target_spouse
				}
				else = { marry = scope:target_spouse }
			}
			else_if = {
				limit = { is_female = yes }
				create_betrothal_matrilineal = scope:target_spouse
			}
			else = { create_betrothal = scope:target_spouse }
			clear_saved_scope = target_spouse
		}
		every_in_list = {
			list = ai_spouses
			remove_from_list = ai_spouses
		}
	}
}

# Called from code once a year for all characters, at a random point that year (individually for each character)
# Root is the character
random_yearly_everyone_pulse = {
	on_actions = {
		on_decline_of_health
	}
}