﻿## Vanilla ##
on_game_start_after_lobby = {
	#Cannot be on_actions
	first_valid_on_action = {
		calyver_game_start_all_on_actions
	}
}

## Harem Politics ##
calyver_game_start_all_on_actions = {
	on_actions = {
		calyver_set_doctrines
		calyver_consort_favor_valuation_on_action
		calyver_mulsim_add_court_eunuchs_on_action
		calyver_china_replace_court_machinations_on_action
	}
}

calyver_set_doctrines = {
	effect = {
		every_religion_global = {
			every_faith = {
				if = {
					limit = {
						has_doctrine = doctrine_monogamy
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_monogamy
					add_doctrine = doctrine_monogamy
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						add_doctrine = doctrine_concubines_0
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_first
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}
				} 
				else_if = {
					limit = {
						has_doctrine = doctrine_concubines
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_concubines
					if= {
						limit = {
							NOR = {
								has_doctrine = doctrine_monogamy
								has_doctrine = doctrine_polygamy_4
								has_doctrine = doctrine_polygamy_10
								has_doctrine = doctrine_polygamy_15
								has_doctrine = doctrine_polygamy_25
								has_doctrine = doctrine_polygamy_50
								has_doctrine = doctrine_polygamy_100
							}
						}
						if = {
							limit = {
								OR = {	
									has_game_rule = calyver_harem_concubinage
									has_game_rule = calyver_harem_concubinage_10
								}
							}
							add_doctrine = doctrine_polygamy_4
						}
						else = {
							add_doctrine = doctrine_monogamy
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						if = {
							limit = {
								OR = {	
									has_game_rule = calyver_expanded_concubinage
									has_game_rule = calyver_harem_concubinage_10
								}
							}
							add_doctrine = doctrine_concubines_10
						}
						else = {
							add_doctrine = doctrine_concubines_3
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_disallowed
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}				
				}
				else_if = {
					limit = {
						has_doctrine = doctrine_polygamy
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_polygamy
					if= {
						limit = {
							NOR = {
								has_doctrine = doctrine_monogamy
								has_doctrine = doctrine_polygamy_4
								has_doctrine = doctrine_polygamy_10
								has_doctrine = doctrine_polygamy_15
								has_doctrine = doctrine_polygamy_25
								has_doctrine = doctrine_polygamy_50
								has_doctrine = doctrine_polygamy_100
							}
						}
						if = {
							limit = {
								has_game_rule = calyver_expanded_polygamy
							}
							add_doctrine = doctrine_polygamy_10
						}
						else = {
							add_doctrine = doctrine_polygamy_4
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						if = {
							limit = {
								has_game_rule = calyver_harem_polygamy
							}
							add_doctrine = doctrine_concubines_3
						}
						else_if = {
							limit = {
								has_game_rule = calyver_harem_polygamy_10
							}
							add_doctrine = doctrine_concubines_10
						}
						else = {
							add_doctrine = doctrine_concubines_0
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_disallowed
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}	
				}
			}
		}		
	}
}

calyver_consort_favor_valuation_on_action = {
	events = {
		consort_favor_valuation.0001	#Favor system
	}
}

calyver_mulsim_add_court_eunuchs_on_action = {
	effect = {
		every_culture_global = {
			limit = {
				culture_head ?= {
					religion = religion:islam_religion
				}
				NOT = {
					has_cultural_parameter = can_appoint_chief_eunuch
				}
			}
			add_culture_tradition = tradition_magnificent_courts
		}		
	}
}

calyver_china_replace_court_machinations_on_action = {
	effect = {
		every_culture_global = {
			limit = {
				has_cultural_tradition = tradition_tgp_court_machinations
			}
			remove_culture_tradition = tradition_tgp_court_machinations
			add_culture_tradition = tradition_cosmic_order
		}
		character:han_3641 = {
			if = {
				limit = {
					is_alive = yes
					is_playable_character = yes
				}
				if = {
					limit = {
						character:han_3559 = {
							is_alive = yes
							NOT = { is_consort_of = prev }
						}
					}
					if = {
						limit = {
							character:han_3559 = {
								NOT = { is_courtier_of = prev }
							}
						}
						add_courtier = character:han_3559
					}
					if = {
						limit = {
							is_character_interaction_valid = {
								recipient = character:han_3559
								interaction = ai_make_concubine_interaction
							}
						}
						run_interaction = {
							interaction = ai_make_concubine_interaction
							actor = character:han_3641
							recipient = character:han_3559
							execute_threshold = accept
						}
					}
					if = {
						limit = {
							can_appoint_char_to_court_position = {
								CHAR = character:han_3559
								COURT_POS = grand_consort_court_position
							}
						}
						appoint_court_position = {
							recipient = character:han_3559
							court_position = grand_consort_court_position
						}
					}
				}
			}
		}
		character:han_8910 = {
			if = {
				limit = {
					is_alive = yes
					any_heir_title = {
						count >= 1
					}
				}
				if = {
					limit = {
						exists = primary_spouse
						any_child = {
							NOT = { is_child_of = prev.primary_spouse }
						}
					}
					every_child = {
						limit = {
							NOT = { is_child_of = prev.primary_spouse }
						}
						add_trait = child_of_concubine_female
					}
				}
			}
		}
		every_ruler = {
			limit = {
				is_dishu_parent_trigger = yes
			}
			save_temporary_scope_as = dishu_parent
			every_child = {
				limit = {
					is_bastard_child_trigger = no
					NOR = {
						has_trait = di_child
						has_trait = child_of_concubine
					}
					house ?= {
						any_house_member = {
							this = scope:dishu_parent
						}
					}
				}
				add_trait = di_child
			}
		}
	}
}