﻿## ---------------------------------------------------------------
## ----------------------- YEARLY EVENTS -------------------------
## ---------------------------------------------------------------
yearly_everyone_pulse = {
	on_actions = {
		epithet_yearly_everyone_pulse
	}
}

epithet_yearly_everyone_pulse = {
	trigger = {
		OR = {
			is_valid_for_epithet_grant_playable = no
			NOT = { has_game_rule = never_epithet_frequency }
		}
	}

	random_on_action = {
		chance_to_happen = 100
		chance_of_no_event = {
			value = 0
			if = {
				limit = { has_game_rule = more_epithet_frequency }
				add = 99
			}
			else_if = {
				limit = { has_game_rule = a_lot_more_epithet_frequency }
				add = 95
			}
			else_if = {
				limit = { has_game_rule = fewer_epithet_frequency }
				add = 99.99
			}
			else = {
				add = 99.9
			}
		}
		100 = epithet_grant_unplayable
	}
}

yearly_playable_pulse = {
	on_actions = {
		epithet_yearly_playable_pulse
	}
}

epithet_yearly_playable_pulse = {
	trigger = {
		OR = {
			is_valid_for_epithet_grant_playable = yes
			NOT = { has_game_rule = never_epithet_frequency }
		}
	}

	random_on_action = {
		chance_to_happen = 100
		chance_of_no_event = {
			value = 0
			if = {
				limit = { has_game_rule = more_epithet_frequency }
				add = 99
			}
			else_if = {
				limit = { has_game_rule = a_lot_more_epithet_frequency }
				add = 99.99
			}
			else_if = {
				limit = { has_game_rule = fewer_epithet_frequency }
				add = 99
			}
			else = {
				add = 99.9
			}
		}
		100 = epithet_grant_rare
	}
}

## ---------------------------------------------------------------
## ----------------------  ON ACTIONS ----------------------------
## ---------------------------------------------------------------
on_prestige_level_gain = {
	on_actions = {
		epithet_on_prestige_level_gain
	}
}

epithet_on_prestige_level_gain = {
	trigger = {
		AND = {
			has_any_nickname = no
			prestige_level >= 4
		}
	}

	on_actions = {
		epithet_grant_rare
	}
}

on_death = {
	first_valid_on_action = {
		on_nicknamed_death
		epithet_preparation_on_death
	}
}

on_birth_child = {
	on_actions = {
		epithet_preparation_on_birth_child
	}

	random_on_action = {
		chance_to_happen = 5
		100 = epithet_random_selection_on_birth
	}
}

## ---------------------------------------------------------------

on_nicknamed_death = {
	trigger = {
		has_any_nickname = yes
	}

	effect = {
		save_scope_as = epithet_receiver
		remove_list_global_variable = {
			name = nickname_already_given
			target = scope:epithet_receiver.var:nickname_given
		}
	}

}

epithet_preparation_on_death = {
	trigger = {
		has_any_nickname = no
		is_ruler = yes
	}
	effect = {
		save_scope_as = epithet_receiver
		save_scope_value_as = { name = prestige_level value = scope:epithet_receiver.prestige_level }
		save_scope_value_as = { name = piety_level value = scope:epithet_receiver.piety_level }
		capital_county = {
			trigger_event = {
				on_action = epithet_scope_change_on_death
				days = 1
			}
		}
	}
}

epithet_scope_change_on_death = {
	effect = {
		holder = {
			trigger_event = {
				on_action = epithet_random_selection_on_death
			}
		}
	}
}

epithet_random_selection_on_death = {
	random_events = {
		100 = epithet_death.0001
		100 = epithet_death.0002
		100 = epithet_death.0003
		100 = epithet_death.0004
		100 = epithet_death.0005
		100 = epithet_death.0006
		100 = epithet_death.0007
		100 = epithet_death.0008
		1000 = 0
	}
}

epithet_preparation_on_birth_child = {
	trigger = {
		scope:mother = {
			exists = var:dead_spouse
		}
	}
	effect = {
		add_character_flag = epithet_posthumous
	}
}

epithet_random_selection_on_birth = {
	random_events = {
		100 = epithet_birth.0001
	}
}

on_title_gain = {
	on_actions = {
		on_epithet_title_gain
	}
}

on_epithet_title_gain = {
	trigger = {
		scope:title = {
			is_title_created = yes
			tier = tier_kingdom
			exists = holder
		}
	}
	
	effect = {
		save_scope_as = epithet_receiver
		remove_list_global_variable = {
			name = nickname_already_given
			target = scope:epithet_receiver.var:nickname_given
		}
	}
}