﻿# Generous Motherhood

# Gives gm_nourishing_mother from the 3rd birth and brings its XP up to gm_trait_xp_target.
# var:gm_trait_xp_given remembers how much XP this mod already gave, since script can't read trait XP.
# Root is a woman. Silent: callers that want the level-up popup compare gm_trait_level before and after.
gm_sync_trait_effect = {
	if = {
		limit = {
			is_female = yes
			gm_birth_count >= gm_births_for_trait
		}
		if = {
			limit = { NOT = { has_trait = gm_nourishing_mother } }
			add_trait = gm_nourishing_mother
			set_variable = {
				name = gm_trait_xp_given
				value = 0
			}
		}
		if = {
			limit = { NOT = { has_variable = gm_trait_xp_given } }
			set_variable = {
				name = gm_trait_xp_given
				value = 0
			}
		}
		if = {
			limit = { var:gm_trait_xp_given < gm_trait_xp_target }
			add_trait_xp = {
				trait = gm_nourishing_mother
				value = {
					value = gm_trait_xp_target
					subtract = var:gm_trait_xp_given
				}
			}
			set_variable = {
				name = gm_trait_xp_given
				value = gm_trait_xp_target
			}
		}
	}
}

# Mother's Milk for 5 years on scope:child, at root's level. Root is the mother.
gm_give_mothers_milk_effect = {
	if = {
		limit = { gm_trait_level = 1 }
		scope:child = {
			add_character_modifier = {
				modifier = gm_mothers_milk_1
				years = 5
			}
		}
	}
	else_if = {
		limit = { gm_trait_level = 2 }
		scope:child = {
			add_character_modifier = {
				modifier = gm_mothers_milk_2
				years = 5
			}
		}
	}
	else_if = {
		limit = { gm_trait_level = 3 }
		scope:child = {
			add_character_modifier = {
				modifier = gm_mothers_milk_3
				years = 5
			}
		}
	}
	else_if = {
		limit = { gm_trait_level >= 4 }
		scope:child = {
			add_character_modifier = {
				modifier = gm_mothers_milk_4
				years = 5
			}
		}
	}
}

# Nourishing Bosom for 1 year on root (the partner), at $MOTHER$'s level.
# Any older copy is removed first so that levels never stack (hidden: the interaction preview
# would otherwise list four removals).
gm_give_nourishing_bosom_effect = {
	hidden_effect = {
		remove_character_modifier = gm_nourishing_bosom_1
		remove_character_modifier = gm_nourishing_bosom_2
		remove_character_modifier = gm_nourishing_bosom_3
		remove_character_modifier = gm_nourishing_bosom_4
	}
	if = {
		limit = { $MOTHER$ = { gm_trait_level = 1 } }
		add_character_modifier = {
			modifier = gm_nourishing_bosom_1
			years = 1
		}
	}
	else_if = {
		limit = { $MOTHER$ = { gm_trait_level = 2 } }
		add_character_modifier = {
			modifier = gm_nourishing_bosom_2
			years = 1
		}
	}
	else_if = {
		limit = { $MOTHER$ = { gm_trait_level = 3 } }
		add_character_modifier = {
			modifier = gm_nourishing_bosom_3
			years = 1
		}
	}
	else = {
		add_character_modifier = {
			modifier = gm_nourishing_bosom_4
			years = 1
		}
	}
}

# Everyone who shares root's bed: spouses, concubines and concubinist, in a temporary list.
gm_list_bed_partners_effect = {
	every_spouse = {
		add_to_temporary_list = $LIST$
	}
	every_concubine = {
		limit = { NOT = { is_in_list = $LIST$ } }
		add_to_temporary_list = $LIST$
	}
	if = {
		limit = { exists = concubinist }
		concubinist = {
			if = {
				limit = { NOT = { is_in_list = $LIST$ } }
				add_to_temporary_list = $LIST$
			}
		}
	}
}

# Popup scene: saves as $SLOT$ the best of $LIST$ by $ORDER$ that is not already in the scene,
# then marks it as used. The temporary list gm_scene_used must exist (the mother is put in it first).
gm_pick_scene_slot_effect = {
	ordered_in_list = {
		list = $LIST$
		limit = { NOT = { is_in_list = gm_scene_used } }
		order_by = $ORDER$
		save_scope_as = $SLOT$
		add_to_temporary_list = gm_scene_used
	}
}

# Level 3 and 4 popups: the slots her bed partners left empty go to the musician of the court she lives at
# (saved as scope:gm_musician, who plays the lute), its chaplain (scope:gm_chaplain), then her
# siblings. Call it after the partners have been picked.
gm_fill_scene_with_court_effect = {
	scope:gm_mother = {
		add_to_temporary_list = gm_scene_extras # seed, already used
		if = {
			limit = { exists = host }
			host = { save_scope_as = gm_court }
		}
		else = {
			save_scope_as = gm_court
		}
	}
	scope:gm_court = {
		if = {
			limit = { exists = court_position:court_musician_court_position }
			court_position:court_musician_court_position = {
				save_scope_as = gm_musician
				add_to_temporary_list = gm_scene_extras
			}
		}
		if = {
			limit = { exists = cp:councillor_court_chaplain }
			cp:councillor_court_chaplain = {
				save_scope_as = gm_chaplain
				if = {
					limit = { NOT = { is_in_list = gm_scene_extras } }
					add_to_temporary_list = gm_scene_extras
				}
			}
		}
	}
	scope:gm_mother = {
		every_sibling = {
			limit = {
				is_alive = yes
				NOT = { is_in_list = gm_scene_extras }
			}
			add_to_temporary_list = gm_scene_extras
		}
	}
	if = {
		limit = { NOT = { exists = scope:gm_slot_a } }
		gm_pick_scene_slot_effect = { LIST = gm_scene_extras ORDER = gm_scene_extra_order SLOT = gm_slot_a }
	}
	if = {
		limit = { NOT = { exists = scope:gm_slot_b } }
		gm_pick_scene_slot_effect = { LIST = gm_scene_extras ORDER = gm_scene_extra_order SLOT = gm_slot_b }
	}
	if = {
		limit = { NOT = { exists = scope:gm_slot_c } }
		gm_pick_scene_slot_effect = { LIST = gm_scene_extras ORDER = gm_scene_extra_order SLOT = gm_slot_c }
	}
	if = {
		limit = { NOT = { exists = scope:gm_slot_d } }
		gm_pick_scene_slot_effect = { LIST = gm_scene_extras ORDER = gm_scene_extra_order SLOT = gm_slot_d }
	}
}

# Popup framing: var:gm_scene_camera = $CAMERA$ on scope:$SLOT$ makes the gui use another camera
# (gfx/portraits/cameras/gm_cameras.txt):
#   side portraits  1 = gm_camera_event_reversed (seen from the other side, image flipped by the gui)
#                   2 = gm_camera_event_low (further away and aimed lower: smaller and higher)
#                   3 = gm_camera_event_far (further away: smaller)
#                   4 = gm_camera_event_far, and the outer widgets move nearer the centre
#                   5 = gm_camera_event_children (smaller, lower and nearer the centre)
#   the mother      1 = gm_camera_event_center_low (aimed lower)
#                   2 = gm_camera_event_center_low_far (aimed lower, a little further away)
# The lute-playing musician always keeps the window's camera. Cleared by the event's after block;
# the 30 days are only a safety net.
gm_set_scene_camera_effect = {
	if = {
		limit = {
			exists = scope:$SLOT$
			scope:$SLOT$ = {
				NOT = { scope:gm_musician ?= this }
			}
		}
		scope:$SLOT$ = {
			set_variable = {
				name = gm_scene_camera
				value = $CAMERA$
				days = 30
			}
		}
	}
}

gm_clear_scene_camera_effect = {
	if = {
		limit = {
			exists = scope:$SLOT$
			scope:$SLOT$ = { has_variable = gm_scene_camera }
		}
		scope:$SLOT$ = { remove_variable = gm_scene_camera }
	}
}

# Level-up popup (gm_motherhood.0100) for root if she is a player, and for every player who
# shares her bed. Root is the mother.
gm_announce_level_effect = {
	save_scope_as = gm_mother
	if = {
		limit = { is_ai = no }
		trigger_event = {
			id = gm_motherhood.0100
			days = 2
		}
	}
	gm_list_bed_partners_effect = { LIST = gm_announce_partners }
	every_in_list = {
		list = gm_announce_partners
		limit = { is_ai = no }
		trigger_event = {
			id = gm_motherhood.0100
			days = 2
		}
	}
}
