﻿# Holy Conquest - Sanctify and Exile Holy Sites 1.1.0
# Prefix: hcq. Every key and file of this mod starts with hcq / zz_hcq_.

# Character scope (the actor). $SITE$ = a holy site scope.
# The holy site becomes a holy site of the actor's faith.
hcq_sanctify_site_effect = {
	$SITE$ = { save_scope_as = hcq_sanctified_site }
	faith = {
		add_holy_site = {
			target = scope:hcq_sanctified_site
			actor = root
		}
		change_fervor = {
			value = 5
			desc = hcq_fervor_sanctify
		}
	}
	remove_character_modifier = hcq_sanctifier_modifier
	add_character_modifier = {
		modifier = hcq_sanctifier_modifier
		years = 10
	}
	# A great holy site (eminent for another faith): special event and consequences
	if = {
		limit = {
			scope:hcq_sanctified_site = { hcq_is_major_site_trigger = yes }
		}
		hcq_major_site_sanctified_effect = yes
	}
	# Game rule: sanctifying also exiles the other faiths
	if = {
		limit = {
			has_game_rule = hcq_sanctify_exiles
			scope:hcq_sanctified_site = {
				any_holy_site_faith = {
					NOT = { this = root.faith }
					hcq_faith_can_lose_site_trigger = { SITE = scope:hcq_sanctified_site }
				}
			}
		}
		hcq_exile_rivals_effect = { SITE = scope:hcq_sanctified_site }
	}
}

# Character scope (the actor). $SITE$ = a holy site scope.
# Every other faith that venerates the site loses it (when its minimum allows it).
hcq_exile_rivals_effect = {
	$SITE$ = { save_scope_as = hcq_exile_site }
	faith = { save_scope_as = hcq_exiler_faith }
	scope:hcq_exile_site = {
		every_holy_site_faith = {
			limit = {
				NOT = { this = scope:hcq_exiler_faith }
				hcq_faith_can_lose_site_trigger = { SITE = scope:hcq_exile_site }
			}
			add_to_temporary_list = hcq_exiled_faiths
		}
	}
	save_scope_as = hcq_actor
	every_in_list = {
		list = hcq_exiled_faiths
		if = {
			limit = { is_faith_eminent_holy_site = scope:hcq_exile_site }
			# Losing a great holy site hurts more
			change_fervor = {
				value = -10
				desc = hcq_fervor_lost_great_site
			}
			religious_head ?= {
				remove_character_modifier = hcq_holy_city_lost_modifier
				add_character_modifier = {
					modifier = hcq_holy_city_lost_modifier
					years = 10
				}
			}
			make_holy_site_not_eminent = {
				target = scope:hcq_exile_site
				actor = root
			}
		}
		remove_holy_site = {
			target = scope:hcq_exile_site
			actor = root
		}
		change_fervor = {
			value = -10
			desc = hcq_fervor_exiled
		}
		religious_head ?= {
			add_opinion = {
				modifier = hcq_exiled_our_holy_site_opinion
				target = root
			}
		}
	}
	scope:hcq_exiler_faith = {
		change_fervor = {
			value = 5
			desc = hcq_fervor_exiler
		}
	}
	# Players of the exiled faiths learn the news
	every_player = {
		limit = {
			NOT = { this = scope:hcq_actor }
			faith ?= { is_in_list = hcq_exiled_faiths }
		}
		trigger_event = hcq.0021
	}
	remove_character_modifier = hcq_exiler_modifier
	add_character_modifier = {
		modifier = hcq_exiler_modifier
		years = 10
	}
}

# AI: sanctify one holy site of the realm, or (more rarely) exile the other faiths.
hcq_ai_act_effect = {
	random_realm_county = {
		limit = {
			any_county_holy_site = {
				save_temporary_scope_as = hcq_ai_check_site
				root = { hcq_can_sanctify_site_trigger = { SITE = scope:hcq_ai_check_site } }
			}
		}
		random_county_holy_site = {
			limit = {
				save_temporary_scope_as = hcq_ai_check_site
				root = { hcq_can_sanctify_site_trigger = { SITE = scope:hcq_ai_check_site } }
			}
			save_scope_as = hcq_ai_site
		}
	}
	if = {
		limit = { exists = scope:hcq_ai_site }
		add_piety = {
			value = hcq_sanctify_piety_cost
			multiply = -1
		}
		hcq_sanctify_site_effect = { SITE = scope:hcq_ai_site }
		add_character_flag = {
			flag = hcq_ai_cooldown
			years = 10
		}
	}
	else_if = {
		limit = {
			has_game_rule = hcq_ai_normal
			piety >= hcq_exile_piety_cost
			prestige >= hcq_exile_prestige_cost
			any_realm_county = {
				any_county_holy_site = {
					save_temporary_scope_as = hcq_ai_check_site
					root = { hcq_can_exile_site_trigger = { SITE = scope:hcq_ai_check_site } }
				}
			}
		}
		random_realm_county = {
			limit = {
				any_county_holy_site = {
					save_temporary_scope_as = hcq_ai_check_site
					root = { hcq_can_exile_site_trigger = { SITE = scope:hcq_ai_check_site } }
				}
			}
			random_county_holy_site = {
				limit = {
					save_temporary_scope_as = hcq_ai_check_site
					root = { hcq_can_exile_site_trigger = { SITE = scope:hcq_ai_check_site } }
				}
				save_scope_as = hcq_ai_exile_site
			}
		}
		if = {
			limit = { exists = scope:hcq_ai_exile_site }
			add_piety = {
				value = hcq_exile_piety_cost
				multiply = -1
			}
			add_prestige = {
				value = hcq_exile_prestige_cost
				multiply = -1
			}
			hcq_exile_rivals_effect = { SITE = scope:hcq_ai_exile_site }
			add_character_flag = {
				flag = hcq_ai_cooldown
				years = 10
			}
		}
	}
}

# Character scope (the actor). scope:hcq_sanctified_site = a great holy site.
# The faiths that hold it as an eminent site are shaken.
hcq_major_site_sanctified_effect = {
	save_scope_as = hcq_actor
	add_prestige = 300
	remove_character_modifier = hcq_holy_city_sanctifier_modifier
	add_character_modifier = {
		modifier = hcq_holy_city_sanctifier_modifier
		years = 20
	}
	scope:hcq_sanctified_site = {
		every_holy_site_faith = {
			limit = {
				NOT = { this = root.faith }
				is_faith_eminent_holy_site = scope:hcq_sanctified_site
			}
			add_to_temporary_list = hcq_shaken_faiths
		}
	}
	every_in_list = {
		list = hcq_shaken_faiths
		change_fervor = {
			value = -10
			desc = hcq_fervor_holy_city_claimed
		}
		religious_head ?= {
			add_opinion = {
				modifier = hcq_claimed_our_holy_city_opinion
				target = root
			}
			remove_character_modifier = hcq_holy_city_lost_modifier
			add_character_modifier = {
				modifier = hcq_holy_city_lost_modifier
				years = 10
			}
		}
	}
	if = {
		limit = { is_ai = no }
		trigger_event = hcq.0010
	}
	every_player = {
		limit = {
			NOT = { this = scope:hcq_actor }
			faith ?= { is_in_list = hcq_shaken_faiths }
		}
		trigger_event = hcq.0020
	}
}
