﻿### Character Spawner Effects ###
#################################

# No Random Traits #
DI_no_random_traits_effect = {
	set_variable = {
		name = DI_cs_random_traits
		value = no
	}
	set_variable = {
		name = DI_cs_martial
		value = 10
	}
	set_variable = {
		name = DI_cs_prowess
		value = 10
	}
	set_variable = {
		name = DI_cs_diplomacy
		value = 10
	}
	set_variable = {
		name = DI_cs_stewardship
		value = 10
	}
	set_variable = {
		name = DI_cs_intrigue
		value = 10
	}
	set_variable = {
		name = DI_cs_learning
		value = 10
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
}

# Random Traits #
DI_random_traits_effect = {
	set_variable = {
		name = DI_cs_random_traits
		value = yes
	}
	set_variable = {
		name = DI_cs_martial
		value = { 5 12 }
	}
	set_variable = {
		name = DI_cs_prowess
		value = { 5 12 }
	}
	set_variable = {
		name = DI_cs_diplomacy
		value = { 5 12 }
	}
	set_variable = {
		name = DI_cs_stewardship
		value = { 5 12 }
	}
	set_variable = {
		name = DI_cs_intrigue
		value = { 5 12 }
	}
	set_variable = {
		name = DI_cs_learning
		value = { 5 12 }
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
}

DI_martial_traits_effect = {
	set_variable = {
		name = DI_cs_martial
		value = {12 20}
	}
	set_variable = {
		name = DI_cs_prowess
		value = {12 20}
	}
	set_variable = {
		name = DI_cs_diplomacy
		value = {6 12}
	}
	set_variable = {
		name = DI_cs_stewardship
		value = {6 12}
	}
	set_variable = {
		name = DI_cs_intrigue
		value = {6 12}
	}
	set_variable = {
		name = DI_cs_learning
		value = {6 12}
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_martial_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:content
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:strong
	}
}

DI_diplomacy_traits_effect = {
	set_variable = {
		name = DI_cs_diplomacy
		value = {14 20}
	}
	set_variable = {
		name = DI_cs_stewardship
		value = {14 20}
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_diplomacy_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:content
	}
}

DI_stewardship_traits_effect = {
	set_variable = {
		name = DI_cs_diplomacy
		value = {10 18}
	}
	set_variable = {
		name = DI_cs_stewardship
		value = {14 20}
	}
	set_variable = {
		name = DI_cs_learning
		value = {10 18}
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_stewardship_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:content
	}
}

DI_intrigue_traits_effect = {
	set_variable = {
		name = DI_cs_diplomacy
		value = {10 18}
	}
	set_variable = {
		name = DI_cs_stewardship
		value = {10 18}
	}
	set_variable = {
		name = DI_cs_intrigue
		value = {14 20}
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_intrigue_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:content
	}
}

DI_learning_traits_effect = {
	set_variable = {
		name = DI_cs_diplomacy
		value = {10 18}
	}
	set_variable = {
		name = DI_cs_stewardship
		value = {10 18}
	}
	set_variable = {
		name = DI_cs_learning
		value = {14 20}
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_learning_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:content
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:trusting
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:calm
	}
}

DI_knight_traits_effect = {
	set_variable = {
		name = DI_cs_martial
		value = {14 20}
	}
	set_variable = {
		name = DI_cs_prowess
		value = {14 20}
	}
	if = {
		limit = {
			has_variable_list = DI_cs_trait_list
		}
		clear_variable_list = DI_cs_trait_list
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:education_martial_4
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:lifestyle_blademaster
	}
	add_to_variable_list = {
		name = DI_cs_trait_list
		target = trait:strong
	}
}

# Set the parents for spawning children #
DI_set_parents_of_child_effect = {
	if = {
		limit = {
			is_male = yes
		}
		scope:root_target = {
			set_variable = {
				name = DI_cs_father
				value = scope:target
			}
		}
		if = {
			limit = {
				scope:target.primary_spouse ?= { is_female = yes }
			}
			scope:root_target = {
				set_variable = {
					name = DI_cs_mother
					value = scope:target.primary_spouse
				}
			}
		}
		else = {
			scope:root_target = {
				if = {
					limit = {
						has_variable = DI_cs_mother
					}
					remove_variable = DI_cs_mother
				}
			}
		}
	}
	else = {
		scope:root_target = {
			set_variable = {
				name = DI_cs_mother
				value = scope:target
			}
		}
		if ={
			limit = {
				scope:target.primary_spouse ?= { is_male = yes }
			}
			scope:root_target = {
				set_variable = {
					name = DI_cs_father
					value = scope:target.primary_spouse
				}
			}
		}
		else = {
			scope:root_target = {
				if = {
					limit = {
						has_variable = DI_cs_father
					}
					remove_variable = DI_cs_father
				}
			}
		}
	}
}

# Set the parents for spawning siblings #
DI_set_parents_of_sibling_effect = {
	scope:target.father ?= { 
		scope:root_target = {
			set_variable = {
				name = DI_cs_father
				value = scope:target.father
			}
		} 
	}
	scope:target.mother ?= { 
		scope:root_target = {
			set_variable = {
				name = DI_cs_mother
				value = scope:target.mother
			}
		} 
	}
}

# Set the father of the character #
DI_set_father_effect = {
	scope:new_courtier = {
		set_father = scope:root_target.var:DI_cs_father
	}
}

# Set the mother of the character #
DI_set_mother_effect = {
	scope:new_courtier = {
		set_mother = scope:root_target.var:DI_cs_mother
	}
}

# Set spouse effect #
DI_set_spouse_effect = {
	scope:new_courtier = {
		marry = scope:target
	}
}

DI_char_spawner_resolve_origin_effect = {
	scope:target.culture = { save_scope_as = DI_cs_spawn_culture }
	scope:target.faith = { save_scope_as = DI_cs_spawn_faith }

	if = {
		limit = { scope:DI_story.var:DI_char_spawner_culture_mode = flag:selected }
		scope:DI_story.var:DI_char_spawner_selected_culture = {
			save_scope_as = DI_cs_spawn_culture
		}
	}
	else_if = {
		limit = { scope:DI_story.var:DI_char_spawner_culture_mode = flag:random }
		switch = {
			trigger = scope:DI_story.var:DI_char_spawner_random_culture_mode
			flag:heritage = {
				random_culture_global = {
					limit = { has_same_culture_heritage = scope:target.culture }
					save_scope_as = DI_cs_spawn_culture
				}
			}
			flag:language = {
				random_culture_global = {
					limit = { has_same_culture_language = scope:target.culture }
					save_scope_as = DI_cs_spawn_culture
				}
			}
			flag:low_acceptance = {
				random_culture_global = {
					limit = {
						this != scope:target.culture
						cultural_acceptance = { target = scope:target.culture value < 30 }
					}
					save_scope_as = DI_cs_spawn_culture
				}
			}
			flag:moderate_acceptance = {
				random_culture_global = {
					limit = {
						this != scope:target.culture
						cultural_acceptance = { target = scope:target.culture value >= 30 }
						cultural_acceptance = { target = scope:target.culture value < 70 }
					}
					save_scope_as = DI_cs_spawn_culture
				}
			}
			flag:high_acceptance = {
				random_culture_global = {
					limit = {
						this != scope:target.culture
						cultural_acceptance = { target = scope:target.culture value >= 70 }
					}
					save_scope_as = DI_cs_spawn_culture
				}
			}
			flag:realm = {
				scope:target = {
					random_realm_county = {
						culture = { save_scope_as = DI_cs_spawn_culture }
					}
				}
			}
			fallback = {
				random_culture_global = { save_scope_as = DI_cs_spawn_culture }
			}
		}
	}

	if = {
		limit = { scope:DI_story.var:DI_char_spawner_faith_mode = flag:selected }
		scope:DI_story.var:DI_char_spawner_selected_faith = {
			save_scope_as = DI_cs_spawn_faith
		}
	}
	else_if = {
		limit = { scope:DI_story.var:DI_char_spawner_faith_mode = flag:random }
		switch = {
			trigger = scope:DI_story.var:DI_char_spawner_random_faith_mode
			flag:religion = {
				scope:target.faith.religion = {
					random_faith = { save_scope_as = DI_cs_spawn_faith }
				}
			}
			flag:family = {
				scope:target.faith.religion = {
					switch = {
						trigger = is_in_family
						rf_abrahamic = {
							random_religion_global = {
								limit = { is_in_family = rf_abrahamic }
								random_faith = { save_scope_as = DI_cs_spawn_faith }
							}
						}
						rf_eastern = {
							random_religion_global = {
								limit = { is_in_family = rf_eastern }
								random_faith = { save_scope_as = DI_cs_spawn_faith }
							}
						}
						rf_sinitic = {
							random_religion_global = {
								limit = { is_in_family = rf_sinitic }
								random_faith = { save_scope_as = DI_cs_spawn_faith }
							}
						}
						rf_pagan = {
							random_religion_global = {
								limit = { is_in_family = rf_pagan }
								random_faith = { save_scope_as = DI_cs_spawn_faith }
							}
						}
					}
				}
			}
			flag:accepted = {
				random_religion_global = {
					limit = { any_faith = { DI_random_faith_accepted_by_target = yes } }
					random_faith = {
						limit = { DI_random_faith_accepted_by_target = yes }
						save_scope_as = DI_cs_spawn_faith
					}
				}
			}
			flag:astray = {
				random_religion_global = {
					limit = { any_faith = { DI_random_faith_astray_from_target = yes } }
					random_faith = {
						limit = { DI_random_faith_astray_from_target = yes }
						save_scope_as = DI_cs_spawn_faith
					}
				}
			}
			flag:hostile = {
				random_religion_global = {
					limit = { any_faith = { DI_random_faith_hostile_from_target = yes } }
					random_faith = {
						limit = { DI_random_faith_hostile_from_target = yes }
						save_scope_as = DI_cs_spawn_faith
					}
				}
			}
			flag:realm = {
				scope:target = {
					random_realm_county = {
						faith = { save_scope_as = DI_cs_spawn_faith }
					}
				}
			}
			fallback = {
				random_religion_global = {
					random_faith = { save_scope_as = DI_cs_spawn_faith }
				}
			}
		}
	}

	# Preserve the selected faith pools, but supply the exact rite when selected
	# or copied from the target. Faith-only random pools use their main rite.
	scope:DI_cs_spawn_faith.main_rite = { save_scope_as = DI_cs_spawn_rite }
	if = {
		limit = { scope:DI_story.var:DI_char_spawner_faith_mode = flag:selected }
		scope:DI_story.var:DI_char_spawner_selected_rite ?= {
			save_scope_as = DI_cs_spawn_rite
		}
	}
	else_if = {
		limit = { NOT = { scope:DI_story.var:DI_char_spawner_faith_mode = flag:random } }
		scope:target.rite = { save_scope_as = DI_cs_spawn_rite }
	}
}

DI_spawn_family_character_effect = {
	scope:DI_story = {
		save_scope_value_as = {
			name = DI_char_spawner_age_increment_value
			value = var:DI_char_spawner_age_increment_value
		}
		save_scope_value_as = {
			name = DI_female_chance
			value = var:DI_female_chance
		}
	}

	scope:target = {
		if = {
			limit = {
				scope:DI_story = {
					NOT = { has_variable = DI_cs_spouse }
				}
			}
			scope:target.house = {
				save_scope_as = target_house
			}
		}
		else = {
			save_scope_as = target_house
		}
	}

	DI_char_spawner_resolve_origin_effect = yes

	create_character = {
		age = scope:DI_char_spawner_age_increment_value
		gender_female_chance = scope:DI_female_chance
		health = 5
		fertility = 0.5
		employer = scope:target
		faith = scope:DI_cs_spawn_faith
		rite = scope:DI_cs_spawn_rite
		culture = scope:DI_cs_spawn_culture
		dynasty_house = scope:target_house
		martial = scope:root_target.var:DI_cs_martial
		prowess = scope:root_target.var:DI_cs_prowess
		diplomacy = scope:root_target.var:DI_cs_diplomacy
		stewardship = scope:root_target.var:DI_cs_stewardship
		intrigue = scope:root_target.var:DI_cs_intrigue
		learning = scope:root_target.var:DI_cs_learning
		random_traits = scope:root_target.var:DI_cs_random_traits
		save_scope_as = new_courtier
	}
	scope:DI_story = {
		if = {
			limit = {
				has_variable = DI_char_spawner_immortal 
			}
			scope:new_courtier = {
				add_trait = DI_immortal
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_100_stat_man
			}
			scope:new_courtier = {
				add_trait = 100_stat_man
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_good_traits 
			}
			scope:new_courtier = {
				add_trait = loyal
				add_trait = beauty_good_3
				add_trait = intellect_good_3
				add_trait = physique_good_3
			}
		}
	}
}

DI_spawn_court_character_effect = {
	scope:DI_story = {
		save_scope_value_as = {
			name = DI_char_spawner_age_increment_value
			value = var:DI_char_spawner_age_increment_value
		}
		save_scope_value_as = {
			name = DI_female_chance
			value = var:DI_female_chance
		}
	}

	DI_char_spawner_resolve_origin_effect = yes

	create_character = {
		age = scope:DI_story.var:DI_char_spawner_age_increment_value
		gender_female_chance = scope:DI_story.var:DI_female_chance
		health = 6
		fertility = 6
		employer = scope:target
		faith = scope:DI_cs_spawn_faith
		rite = scope:DI_cs_spawn_rite
		culture = scope:DI_cs_spawn_culture
		dynasty = generate
		martial = scope:root_target.var:DI_cs_martial
		prowess = scope:root_target.var:DI_cs_prowess
		diplomacy = scope:root_target.var:DI_cs_diplomacy
		stewardship = scope:root_target.var:DI_cs_stewardship
		intrigue = scope:root_target.var:DI_cs_intrigue
		learning = scope:root_target.var:DI_cs_learning
		random_traits = scope:root_target.var:DI_cs_random_traits
		save_scope_as = new_courtier
	}
	scope:DI_story = {
		if = {
			limit = {
				has_variable = DI_char_spawner_immortal 
			}
			scope:new_courtier = {
				add_trait = DI_immortal
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_100_stat_man
			}
			scope:new_courtier = {
				add_trait = 100_stat_man
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_good_traits 
			}
			scope:new_courtier = {
				add_trait = loyal
				add_trait = beauty_good_3
				add_trait = intellect_good_3
				add_trait = physique_good_3
			}
		}
	}
	scope:root_target = {
		if = {
			limit = {
				has_variable_list = DI_cs_trait_list
			}
			
			scope:new_courtier = {
				remove_any_education_traits_effect = yes
			}

			every_in_list = {
				variable = DI_cs_trait_list
				scope:new_courtier = {
					add_trait = prev
				}
			}
		}
	}
}

DI_spawn_low_court_character_effect = {
	scope:DI_story = {
		save_scope_value_as = {
			name = DI_char_spawner_age_increment_value
			value = var:DI_char_spawner_age_increment_value
		}
		save_scope_value_as = {
			name = DI_female_chance
			value = var:DI_female_chance
		}
	}

	DI_char_spawner_resolve_origin_effect = yes

	create_character = {
		age = scope:DI_char_spawner_age_increment_value
		gender_female_chance = scope:DI_female_chance
		health = 5
		fertility = 0.5
		employer = scope:target
		faith = scope:DI_cs_spawn_faith
		rite = scope:DI_cs_spawn_rite
		culture = scope:DI_cs_spawn_culture
		dynasty = none
		martial = scope:root_target.var:DI_cs_martial
		prowess = scope:root_target.var:DI_cs_prowess
		diplomacy = scope:root_target.var:DI_cs_diplomacy
		stewardship = scope:root_target.var:DI_cs_stewardship
		intrigue = scope:root_target.var:DI_cs_intrigue
		learning = scope:root_target.var:DI_cs_learning
		random_traits = scope:root_target.var:DI_cs_random_traits
		save_scope_as = new_courtier
	}
	scope:DI_story = {
		if = {
			limit = {
				has_variable = DI_char_spawner_immortal 
			}
			scope:new_courtier = {
				add_trait = DI_immortal
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_100_stat_man
			}
			scope:new_courtier = {
				add_trait = 100_stat_man
			}
		}
		if = {
			limit = {
				has_variable = DI_char_spawner_good_traits 
			}
			scope:new_courtier = {
				add_trait = loyal
				add_trait = beauty_good_3
				add_trait = intellect_good_3
				add_trait = physique_good_3
			}
		}
	}
	scope:root_target = {
		if = {
			limit = {
				has_variable_list = DI_cs_trait_list
			}
			
			scope:new_courtier = {
				remove_any_education_traits_effect = yes
			}

			every_in_list = {
				variable = DI_cs_trait_list
				scope:new_courtier = {
					add_trait = prev
				}
			}
		}
	}
}
