﻿
##################################################
# Debug Triggers

# Do we want to gather global variable tallies for the Chinese struggle?
## Toggled off for release, toggle back on to check how often we change phases & which catalysts are firing.
gather_debug_variables_for_dynastic_cycle_trigger = { debug_only = yes }

##################################################
# General Triggers
## Miscellaneous assorted triggers related to bits of neutral struggle content.

tgp_does_this_player_care_about_the_dynastic_cycle = {
	save_temporary_scope_as = char_temp
	situation:dynastic_cycle ?= {
		OR = {
			any_situation_participant = {
				this = scope:char_temp
			}
			situation_top_has_province = scope:char_temp.capital_province
		}
	}
}

tgp_is_hegemon_or_below_in_dynastic_cycle_trigger = {
	custom_description = {
		text = tgp_is_hegemon_or_below_in_dynastic_cycle_trigger_tt
		trigger_if = {
			limit = {
				any_character_situation = {
					situation_type = dynastic_cycle
				}
			}
			top_participant_group:dynastic_cycle ?= {
				NOT = {
					participant_group_type = other_rulers
				}
			}
		}
		trigger_else = {
			always = no
		}
	}
}

is_valid_celestial_dynasty = {
	culture = {
		has_cultural_pillar = heritage_chinese
	}
}

tgp_dynastic_cycle_is_the_mandate_loser = {
	NOT = { exists = title:h_china.holder }
	title:h_china.previous_holder ?= this
}

tgp_dynastic_cycle_offensive_wars_ban_trigger = {
	trigger_if = {
		limit = {
			scope:attacker ?= { has_title = title:h_china }
			exists = situation:dynastic_cycle
		}
		custom_tooltip = {
			text = dynastic_cycle_offensive_wars_ban_tt
			scope:attacker = {
				top_participant_group:dynastic_cycle ?= {
					NOT = { has_participant_group_parameter = dynastic_cycle_offensive_wars_ban }
				}
			}
		}
	}
}

tgp_war_ban_all_ministers_trigger = {
	trigger_if = {
		limit = {
			scope:attacker.liege = {
				tgp_has_access_to_ministry_trigger = yes
			}
		}
		scope:attacker = {
			NOR = {
				has_title = title:e_minister_chancellor
				has_title = title:e_minister_censor
				has_title = title:e_minister_grand_marshal
				has_title = title:e_minister_of_personnel
				has_title = title:e_minister_of_revenue
				has_title = title:e_minister_of_rites
				has_title = title:e_minister_of_war
				has_title = title:e_minister_of_justice
				has_title = title:e_minister_of_works
			}
		}
	}
}

tgp_war_ban_civilian_ministers_trigger = {
	trigger_if = {
		limit = {
			scope:attacker.liege = {
				tgp_has_access_to_ministry_trigger = yes
			}
		}
		scope:attacker = {
			NOR = {
				has_title = title:e_minister_chancellor
				has_title = title:e_minister_censor
				#has_title = title:e_minister_grand_marshal # This guys is allowed to declare wars in some cases
				has_title = title:e_minister_of_personnel
				has_title = title:e_minister_of_revenue
				has_title = title:e_minister_of_rites
				has_title = title:e_minister_of_war
				has_title = title:e_minister_of_justice
				has_title = title:e_minister_of_works
			}
		}
	}
}

tgp_dynastic_cycle_yearly_drift_blocked_trigger = {
	trigger_if = {
		limit = {
			OR = {
				situation_current_phase = situation_dynastic_cycle_phase_stability_expansion
				situation_current_phase = situation_dynastic_cycle_phase_stability_advancement
			}
		}
		title:h_china.holder.capital_county ?= { NOT = { has_county_modifier = tgp_strengthen_capital_modifier } }
	}
}

# use only with Dynastic Cycle, to determine if root is in the same Movement as the target character, including pro_hegemon and hegemon_ruler as the same group
is_in_same_movement_as = {
	save_temporary_scope_as = temp_char
	exists = $TARGET$.top_participant_group:dynastic_cycle
	OR = {
		top_participant_group:dynastic_cycle ?= $TARGET$.top_participant_group:dynastic_cycle
		AND = {
			is_in_pro_hegemon_or_hegemon_movement = yes
			$TARGET$ = {
				is_in_pro_hegemon_or_hegemon_movement = yes
			}
		}
	}
}
# used to check if eligible for the pro_hegemon boost in ai chances
is_in_pro_hegemon_or_hegemon_movement = {
	save_temporary_scope_as = temp_char
	custom_tooltip = {
		top_participant_group:dynastic_cycle ?= {
			OR = {
				participant_group_type = hegemon_ruler
				participant_group_type = pro_hegemon_movement
			}
		}
		text = is_in_pro_hegemon_or_hegemon_movement_tt
	}
}

# To determine if situation_participant_group is favored
# Use e.g. "MOVEMENT = expansion_movement" to check a specific movement
is_movement_in_power = {
	situation:dynastic_cycle ?= {
		situation_participant_group:$MOVEMENT$ = {
			save_temporary_scope_as = my_movement
		}
	}
	custom_tooltip = {
		scope:my_movement = {
			exists = var:movement_favored
		}
		text = my_movement_is_in_power
	}
}

# To check if a character is a leader of any movement
is_any_movement_leader = {
	save_temporary_scope_as = leader
	custom_description = {
		text = is_any_movement_leader_tt
		top_participant_group:dynastic_cycle.var:movement_leader ?= scope:leader
	}
}

# To check if a character is part of any movement that's NOT Unaligned
is_any_movement_member_not_undecided = {
	top_participant_group:dynastic_cycle ?= {
		OR = {
			participant_group_type = expansion_movement
			participant_group_type = advancement_movement
			participant_group_type = pro_hegemon_movement
			participant_group_type = conservative_movement
		}
	}
}

is_celestial_governor_in_need_of_successor_trigger = {
	government_has_flag = government_is_celestial
	is_governor = yes
	primary_title.tier >= tier_duchy
	has_realm_law = celestial_appointment_succession_law # we don't want to make bureaucratic appointments for military provinces
	primary_title = {
		any_title_heir = {
			dynasty != root.dynasty # nepotism very bad!
			merit_level >= root.expected_primary_title_merit
			root.liege != this
			count = 0
		}
	}
}

is_province_valid_confucian_education_trigger = {
	OR = {
		#holder has confucian religion or culture
		county.holder = {
			faith = {
				religion = religion:confucianism_religion
			}
			culture = {
				has_cultural_pillar = heritage_chinese
			}
		}
		#location is in China or has confucian religion or culture
		geographical_region = world_asia_china
		faith = {
			religion = religion:confucianism_religion
		}
		culture = {
			has_cultural_pillar = heritage_chinese
		}
		#location is part of the Dynastic Cycle region
		county = {
			any_county_situation_sub_region = {
				this = situation:dynastic_cycle.situation_sub_region:core
			}
		}
	}
}

grand_campaign_valid_contributor_trigger = {
	top_liege = scope:owner
	custom_description = {
		text = grand_campaign_target_border_tt
		object = scope:great_project.var:attack_$ID$.holder
		this != scope:owner
		this != scope:founder
		scope:great_project.var:attack_$ID$ ?= {
			trigger_if = {
				limit = {
					is_title_created = yes
				}
				holder.top_overlord != scope:owner
			}
			any_title_to_title_neighboring_county = {
				holder.top_overlord = scope:owner
			}
		}
	}
	custom_tooltip = {
		text = grand_campaign_main_target_tt
		scope:great_project = {
			any_contribution = {
				contribution_id = attack_1
				contribution_is_funded = yes
			}
		}
	}
}

hegemon_favors_advancement_trigger = {
	title:h_china.holder ?= {
		OR = {
			var:favor_era ?= flag:advancement
			AND = {
				is_ai = yes
				hegemon_favored_stability_phase_value >= 0
			}
		}
	}
}

hegemon_favors_expansion_trigger = {
	title:h_china.holder ?= {
		OR = {
			var:favor_era ?= flag:expansion
			AND = {
				is_ai = yes
				hegemon_favored_stability_phase_value < 0
			}
		}
	}
}


### Exam triggers - check if the character in scope is considered to have passed (or failed) a specific examination tier

# children examination
tgp_passed_children_examination = {
	has_character_flag = passed_child_exam
}
tgp_failed_children_examination = {
	not = { has_character_flag = passed_child_exam }
	any_memory = { has_memory_type = failed_child_exam_memory }
}

# provincial examination
tgp_passed_provincial_examination = {
	has_character_flag = passed_provincial_exam
}
tgp_failed_provincial_examination = {
	not = { has_character_flag = passed_provincial_exam }
	any_memory = { has_memory_type = failed_provincial_exam_memory }
}

# metropolitan examination
tgp_passed_metropolitan_examination = {
	has_character_flag = passed_metropolitan_exam
}
tgp_failed_metropolitan_examination = {
	not = { has_character_flag = passed_metropolitan_exam }
	any_memory = { has_memory_type = failed_metropolitan_exam_memory }
}

# palace examination
tgp_passed_palace_examination = {
	has_character_flag = passed_palace_exam
}

can_pass_candidate_score_prestige_law_trigger = {
	top_liege = {
		custom_description = {
			text = conservative_ministers_tt
			value = conservative_ministers_value
			any_councillor = {
				count >= needed_conservative_ministers_value
				top_participant_group:dynastic_cycle ?= {
					participant_group_type = conservative_movement
				}
			}
		}
		custom_description = {
			text = conservative_powerful_families_tt
			value = conservative_powerful_families_value
			any_powerful_family = {
				count >= needed_conservative_powerful_families_value
				house_head = {
					top_participant_group:dynastic_cycle ?= {
						participant_group_type = conservative_movement
					}
				}
			}
		}
	}
}

can_pass_candidate_score_merit_law_trigger = {
	top_liege = {
		custom_description = {
			text = not_conservative_ministers_tt
			value = not_conservative_ministers_value
			any_councillor = {
				count >= needed_conservative_ministers_value
				top_participant_group:dynastic_cycle ?= {
					NOT = { participant_group_type = conservative_movement }
				}
			}
		}
		custom_description = {
			text = not_conservative_powerful_families_tt
			value = not_conservative_powerful_families_value
			any_powerful_family = {
				count >= needed_conservative_powerful_families_value
				house_head = {
					top_participant_group:dynastic_cycle ?= {
						NOT = { participant_group_type = conservative_movement }
					}
				}
			}
		}
	}
}

#dynastic cycle sort to movement triggers
should_pick_pro_hegemon_movement_trigger = {
	title:h_china.holder.legitimacy_level >= 2
	NOR = {
		has_relation_victim = title:h_china.holder
		has_relation_bully = title:h_china.holder
		has_relation_rival = title:h_china.holder
		is_at_war_with = title:h_china.holder
		is_imprisoned_by = title:h_china.holder
		house_has_feud_relation_with_trigger = { TARGET = title:h_china.holder }
	}
	OR = {
		has_relation_lover = title:h_china.holder
		has_relation_friend = title:h_china.holder
		has_relation_crush = title:h_china.holder
		has_relation_potential_friend = title:h_china.holder
		has_relation_elder = title:h_china.holder
		has_relation_disciple = title:h_china.holder
		has_trait = loyal
		house ?= title:h_china.holder.house
		has_dread_level_towards = {
			target = title:h_china.holder
			level >= 1
		}
		AND = {
			title:h_china.holder.hegemon_favored_stability_phase_value >= 0
			ai_has_builder_or_pious_builder_personality = yes
		}
		AND = {
			title:h_china.holder.hegemon_favored_stability_phase_value < 0
			ai_has_warlike_personality = yes
		}
	}
}

should_pick_expansion_movement_trigger = {
	OR = {
		has_trait = strategist
		has_trait = overseer
		has_trait = gallant
		ai_has_warlike_personality = yes
		has_education_martial_trigger = yes
	}
}

should_pick_advancement_movement_trigger = {
	OR = {
		has_trait = scholar
		has_trait = theologian
		has_trait = architect
		has_trait = administrator
		ai_has_builder_or_pious_builder_personality = yes
	}
}

should_pick_conservative_movement_trigger = {
	OR = {
		has_trait = family_first
		has_trait = august
		has_trait = avaricious
		is_powerful_vassal = yes
		house ?= {
			OR = {
				is_powerful_family = yes
				is_dominant_family = yes
			}
		}
	}
}
