﻿on_game_start_after_lobby = {
	on_actions = { DI_on_game_start }
}

# A title is lost by a character
# root = the old holder
# scope:title = the title that changes hands
# scope:new_holder = new holder.
on_title_lost = {
	on_actions = { DI_on_title_lost }
}

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

DI_on_game_start = {
	effect = {
		set_global_variable = DI_loaded
		
		every_living_character = {
			limit = {
				is_ai = no
			}
			create_story = DI_traits_story
			create_story = DI_misc_variables_story
		}
	}
}

DI_on_title_lost = {
	effect = {	
		if = {
			# if root is a player and is giving up their last title
			limit = {
				is_ai = no
				NOT = {
					any_held_title = {
						NOT = { this = scope:title }
					}
				}
				AND = {
					any_owned_story = {
						story_type = DI_traits_story
					}
					any_owned_story = {
						story_type = DI_misc_variables_story
					}
				}
			}

			debug_log = "DI_debug: Player lost their LAST title"

			scope:new_holder = {
				if = {
					limit = {
						any_owned_story = {
							story_type = DI_misc_variables_story
						}
					}
					debug_log = "DI_debug: Player heir already has misc variables story"
					every_owned_story = {
						limit = {
							story_type = DI_misc_variables_story
						}
						end_story = yes
					}
				}
				if = {
					limit = {
						has_variable = DI_misc_variables_story
					}
					remove_variable = DI_misc_variables_story
				}
			}

			# then transfer variable stories
			debug_log = "DI_debug: Transferring variable stories"
			every_owned_story = {
				limit = {
					OR = {
						story_type = DI_traits_story
						story_type = DI_misc_variables_story
					}
				}
				make_story_owner = scope:new_holder
			}

			remove_variable = DI_misc_variables_story
			clear_variable_list = DI_pinned_traits

			scope:new_holder = {
				DI_save_misc_variables_to_character_effect = yes
			}
		}
	}
}

### Yearly Pulse, Handle Character Flags ###
DI_yearly_playable_pulse = {
	effect = {
		# Check for character flag - DI_perma_health
		if = {
			limit = {
				has_character_flag = DI_perma_health
			}
			# If the character has the flag, call eliminate health problems
			DI_eliminated_health_problem_effect = yes
		}
	}
}