﻿
on_game_start_after_lobby = {
	on_actions = {
		add_concubine_traits_on_game_start_on_action
	}
}

yearly_global_pulse = {
	on_actions = {
		add_concubine_traits_on_game_start_on_action
	}
}

add_concubine_traits_on_game_start_on_action = {
    trigger = { 
		NOT = { has_global_variable = immersive_concubinage_traits_added_to_concubines }
	}
	effect = {
		every_living_character = {
			if = {
				limit = { 
					is_concubine = yes
					NOR = {
						has_trait = concubine
						has_trait = concubine_elevated
					}
				}
				add_trait = concubine
			}
		}
		set_global_variable = {
			name = immersive_concubinage_traits_added_to_concubines
			value = yes
		}
	}
}

# Character becomes another's concubine
# root = the concubinist
# scope:concubine = the concubine
on_concubinage = {
	on_actions = {
		depose_and_disinherit_concubine_on_action
	}
}

depose_and_disinherit_concubine_on_action = {
	effect = {
		if = {
			limit = { scope:concubine = { is_ruler = yes } }
			scope:concubine = {
				depose = yes
			}
			add_courtier = scope:concubine
		}
		scope:concubine = {
			add_trait = concubine
		}		
	}
}

# Character ceases to be another's concubine
# root = the concubinist
# scope:concubine = the concubine
# scope:reason = flag:death, flag:faith, or flag:script
on_concubinage_end = {
	on_actions = {
		end_concubinage_on_action
	}
}

on_marriage = {
	on_actions = {
		end_concubinage_on_action
	}	
}

end_concubinage_on_action = {
	effect = {
		if = {
			limit = { exists = scope:concubine } # To Prevent errors
			scope:concubine = { 
				if = {
					limit = {
						OR = {
							has_trait = concubine
							has_trait = concubine_elevated
						}
					}
					remove_trait = concubine
					remove_trait = concubine_elevated				
				}
			}
		}
		else = {
			if = {
				limit = {
					OR = {
						has_trait = concubine
						has_trait = concubine_elevated
					}
				}
				remove_trait = concubine
				remove_trait = concubine_elevated
			}
			scope:spouse = { 
				if = {
					limit = {
						OR = {
							has_trait = concubine
							has_trait = concubine_elevated
						}
					}
					remove_trait = concubine
					remove_trait = concubine_elevated				
				}
			}
		}
	}
}
