﻿# referenced by code. Fired yearly on every rite
# root = rite
on_rite_yearly = {
	effect = {
		rite_yearly_hor_passive_learning_effect = yes
		rite_yearly_hor_divergence_drift_effect = yes
	}
}

pam_the_christian_church_yearly_on_action = {
	effect = {
		# Non Christian presence in Europe
		pam_the_christian_church_non_christian_kingdom_in_europe_catalyst_effect = yes
		# Non-Catholic ruler controls the Papal State's de jure capital
		pam_the_christian_church_catholic_hof_weak_papal_state_control_catalyst_effect = yes
		# Byzantine emperor is not Christian
		pam_the_christian_church_byzantine_emperor_non_christian_catalyst_effect = yes
	}
}
# Tier 1 is paced once from pam_cathedral_01 on_start_build (years = 2).
pam_cathedral_great_project_yearly_on_action = {
	effect = {
		every_great_project = {
			limit = {
				OR = {
					great_project_type = pam_cathedral_02
					great_project_type = pam_cathedral_03
				}
			}
			great_project_founder = {
				save_scope_as = founder
				trigger_event = {
					on_action = pam_cathedral_great_project_yearly_founder_events_on_action
					years = 5
				}
			}
			great_project_owner = {
				if = {
					limit = {
						this = scope:founder
					}
					trigger_event = {
						on_action = pam_cathedral_great_project_yearly_owner_events_on_action
						years = 10
					}
				}
				else = {
					trigger_event = {
						on_action = pam_cathedral_great_project_yearly_owner_events_on_action
						years = 5
					}
				}
			}
		}
	}
}
pam_cathedral_great_project_yearly_founder_events_on_action = {
	random_events = {
		1 = pam_great_projects_events.0007
		1 = pam_great_projects_events.0008
		1 = pam_great_projects_events.0011
		1 = pam_great_projects_events.0013
	}
}
pam_cathedral_great_project_yearly_owner_events_on_action = {
	random_events = {
		1 = pam_great_projects_events.0012
	}
}

study_faith_success = {
	on_actions = {
		pam_study_faith_personal_tenet_on_action
	}
}

on_travel_plan_movement = {
	on_actions = {
		pam_mountain_travel_personal_tenet_on_action
		sun_worship_desert_travel_personal_tenet_on_action
		pam_sanctity_of_nature_travel_on_action
	}
}

on_building_completed = {
	on_actions = {
		pam_sanctity_of_nature_building_completed_on_action
	}
}

on_faith_conversion = {
	on_actions = {
		pam_tax_nonbelievers_faith_conversion_on_action
	}
}

on_birth_child = {
	on_actions = {
		pam_auspicious_birthright_birth_on_action
	}
}

pam_study_faith_personal_tenet_on_action = {
	effect = {
		if = {
			limit = {
				scope:owner = {
					has_personal_tenet_flag = adaptive_study_rite_cultural_acceptance
					culture != scope:target.culture
				}
			}
			scope:owner = {
				culture = {
					change_cultural_acceptance = {
						target = scope:target.culture
						value = minor_cultural_acceptance_gain
						desc = pam_adaptive_study_rite_cultural_acceptance
					}
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_adaptive_study_rite_cultural_acceptance_toast
					left_icon = scope:owner
					right_icon = scope:target
				}
			}
		}
	}
}

pam_mountain_travel_personal_tenet_on_action = {
	effect = {
		if = {
			limit = {
				has_personal_tenet_flag = cthonic_redoubts_mountain_travel_fulfillment
				current_travel_plan.current_location = {
					OR = {
						terrain = mountains
						terrain = desert_mountains
					}
				}
				current_travel_plan = {
					NOT = { has_variable = pam_mountain_travel_fulfillment_awarded }
				}
			}
			current_travel_plan = {
				set_variable = pam_mountain_travel_fulfillment_awarded
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_cthonic_redoubts_mountain_travel_toast
				left_icon = root
				add_piety = minor_piety_gain
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
		}
	}
}

sun_worship_desert_travel_personal_tenet_on_action = {
	effect = {
		if = {
			limit = {
				has_personal_tenet_flag = tenet_sun_worship_desert_pilgrim
				current_travel_plan.current_location = {
					OR = {
						terrain = desert
						terrain = desert_mountains
						terrain = oasis
					}
				}
				current_travel_plan = {
					NOT = { has_variable = sun_worship_desert_fulfillment_awarded }
				}
			}
			current_travel_plan = {
				set_variable = sun_worship_desert_fulfillment_awarded
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = sun_worship_desert_travel_toast
				left_icon = root
				add_piety = minor_piety_gain
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
		}
	}
}

pam_sanctity_of_nature_travel_on_action = {
	effect = {
		if = {
			limit = {
				current_travel_plan.current_location = {
					has_province_modifier = preserved_wilderness_modifier
				}
			}
			current_travel_plan = {
				if = {
					limit = {
						NOT = { has_travel_plan_modifier = preserved_wilderness_travel_modifier }
					}
					add_travel_plan_modifier = preserved_wilderness_travel_modifier
				}
			}
		}
		else_if = {
			limit = {
				current_travel_plan = {
					has_travel_plan_modifier = preserved_wilderness_travel_modifier
				}
			}
			current_travel_plan = {
				remove_travel_plan_modifier = preserved_wilderness_travel_modifier
			}
		}

		if = {
			limit = {
				has_personal_tenet_flag = tenet_sanctity_of_nature_wilderness_fulfillment
				current_travel_plan.current_location = {
					has_holding = no
					is_sea_province = no
				}
				current_travel_plan = {
					OR = {
						NOT = { has_variable_list = pam_sanctity_of_nature_visited_wilderness }
						AND = {
							has_variable_list = pam_sanctity_of_nature_visited_wilderness
							NOT = {
								is_target_in_variable_list = {
									name = pam_sanctity_of_nature_visited_wilderness
									target = root.current_travel_plan.current_location
								}
							}
						}
					}
				}
			}
			change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
			current_travel_plan = {
				add_to_variable_list = {
					name = pam_sanctity_of_nature_visited_wilderness
					target = root.current_travel_plan.current_location
				}
			}
		}
	}
}

pam_sanctity_of_nature_building_completed_on_action = {
	# add_great_building fires on_building_completed during history setup.
	trigger = {
		has_global_variable = game_has_started
	}

	effect = {
		if = {
			limit = {
				has_holding = yes
				has_province_modifier = preserved_wilderness_modifier
			}
			remove_province_modifier = preserved_wilderness_modifier
		}
	}
}

pam_tax_nonbelievers_faith_conversion_on_action = {
	trigger = {
		exists = scope:actor
		root != scope:actor
		scope:actor = {
			has_personal_tenet_flag = tax_nonbelievers_fulfillment_from_conversion
		}
	}
	effect = {
		scope:actor = {
			pam_tax_nonbelievers_conversion_fulfillment_effect = yes
		}
	}
}

pam_auspicious_birthright_birth_on_action = {
	effect = {
		if = {
			limit = {
				OR = {
					scope:mother = {
						has_personal_tenet_flag = mystical_birthright_spiritual_leader
					}
					scope:real_father ?= {
						has_personal_tenet_flag = mystical_birthright_spiritual_leader
					}
				}
			}
			# Base 5% chance for later children; the first-child modifiers shift the chance to 20%.
			random_list = {
				95 = {
					modifier = {
						add = -15
						OR = {
							scope:mother = {
								has_personal_tenet_flag = mystical_birthright_spiritual_leader
								NOR = {
									any_child = {
										even_if_dead = yes
										this != scope:child
									}
								}
							}
							scope:real_father ?= {
								has_personal_tenet_flag = mystical_birthright_spiritual_leader
								NOR = {
									any_child = {
										even_if_dead = yes
										this != scope:child
									}
								}
							}
						}
					}
				}
				5 = {
					modifier = {
						add = 15
						OR = {
							scope:mother = {
								has_personal_tenet_flag = mystical_birthright_spiritual_leader
								NOR = {
									any_child = {
										even_if_dead = yes
										this != scope:child
									}
								}
							}
							scope:real_father ?= {
								has_personal_tenet_flag = mystical_birthright_spiritual_leader
								NOR = {
									any_child = {
										even_if_dead = yes
										this != scope:child
									}
								}
							}
						}
					}
					random_list = {
						1 = {
							trigger = {
								NOR = {
									has_trait = dwarf
									has_trait = giant
								}
							}
							add_trait = dwarf
						}
						1 = {
							trigger = {
								NOR = {
									has_trait = giant
									has_trait = dwarf
								}
							}
							add_trait = giant
						}
						1 = {
							trigger = {
								NOT = { has_trait = albino }
							}
							add_trait = albino
						}
						1 = {
							trigger = {
								NOT = { has_trait = clubfooted }
							}
							add_trait = clubfooted
						}
						1 = {
							trigger = {
								NOT = { has_trait = bleeder }
							}
							add_trait = bleeder
						}
						1 = {
							trigger = {
								NOT = { has_trait = hunchbacked }
							}
							add_trait = hunchbacked
						}
						1 = {
							trigger = {
								NOT = { has_trait = lisping }
							}
							add_trait = lisping
						}
						1 = {
							trigger = {
								NOT = { has_trait = stuttering }
							}
							add_trait = stuttering
						}
						1 = {
							trigger = {
								NOR = {
									has_trait = spindly
									has_trait = physique_good
								}
							}
							add_trait = spindly
						}
						1 = {
							trigger = {
								NOT = { has_trait = scaly }
							}
							add_trait = scaly
						}
						1 = {
							trigger = {
								NOT = { has_trait = wheezing }
							}
							add_trait = wheezing
						}
						1 = {
							trigger = {
								NOR = {
									has_trait = beauty_bad
									has_trait = beauty_good
								}
							}
							add_trait = beauty_bad_2
						}
					}
				}
			}
		}
		if = {
			limit = {
				scope:child = {
					OR = {
						has_trait = dwarf
						has_trait = albino
						has_trait = clubfooted
						has_trait = bleeder
						has_trait = scaly
						has_trait = giant
						has_trait = hunchbacked
						has_trait = spindly
						has_trait = wheezing
						has_trait = lisping
						has_trait = stuttering
						has_trait = beauty_bad
					}
				}
				OR = {
					scope:mother = {
						has_personal_tenet_flag = mystical_birthright_spiritual_leader
					}
					scope:real_father ?= {
						has_personal_tenet_flag = mystical_birthright_spiritual_leader
					}
				}
			}
			scope:child = {
				add_character_modifier = {
					modifier = pam_mystical_birthright_spiritual_leader_modifier
				}
			}
			if = {
				limit = {
					scope:mother = {
						is_ai = no
					}
				}
				scope:mother = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = pam_mystical_birthright_spiritual_leader_toast
						left_icon = scope:child
						custom_tooltip = {
							text = pam_mystical_birthright_spiritual_leader_toast_tt
						}
					}
				}
			}
			if = {
				limit = {
					scope:real_father ?= {
						is_ai = no
					}
				}
				scope:real_father = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = pam_mystical_birthright_spiritual_leader_toast
						left_icon = scope:child
						custom_tooltip = {
							text = pam_mystical_birthright_spiritual_leader_toast_tt
						}
					}
				}
			}
		}
	}
}

# used by code, do not rename or remove.
# root = clerical region holder
# scope:title = clerical region title
# scope:county = capital county
# scope:reason = reason that triggered this mismatch. flag can be:
#	- county_converted (county is now of different faith, holder stayed old faith)
#	- character_converted (character converted away from county faith)
#	- changed_tenets_or_doctrines (county and character are still matching faith but the faith doesn't support clerical regions anymore)
on_clerical_region_faith_mismatch = {
	# TIT-75963 player choice when capital converts; TIT-75964 other succession cases
	events = {
		# Capital converted; both Faiths have Clerical Regions (player)
		pam_clergy_events.0051
		# AI / other mismatch reasons / new Faith lacks Clerical Regions
		pam_clergy_events.0052
	}
}

# used by code, do not rename or remove.
# root = challenger character
# scope:sponsor = former sponsor title
# scope:title = challenger title
on_challenger_removed = {
	effect = {
		if = {
			limit = {
				is_alive = yes
				has_character_flag = antipope_flag
			}
			remove_character_flag = antipope_flag
		}
		# TODO_PAM move antipope removal cleanup effects here
		#if = {
		#	limit = { root = scope:title.holder }
		#	destroy_title = scope:title
		#}
	}

}
