﻿# Heirs of Ērānšahr — reconquest effects. scope:actor = Sasanian, scope:recipient = house head.
# MaA note: the "old-kind" regiments use Legacy of Persia (FP3) units when the player owns that
# DLC, and fall back to base-game equivalents otherwise — so the mod stays standalone-safe.

### A house answers a petition: convert (if Zoroastrian-eligible) + a pledge + an alliance ###
he_house_accept_effect = {
	scope:recipient = {
		add_character_flag = he_house_answered
		add_opinion = { target = scope:actor modifier = he_answered_the_call_opinion }
		if = {
			limit = {
				NOT = { faith = { religion_tag = christianity_religion } }
				OR = { faith = faith:mazdayasna culture = { has_cultural_pillar = heritage_iranian } }
			}
			set_character_faith = faith:mazdayasna
		}
		# a pledge, fitting the house's fighting tradition
		if = {
			limit = { culture = { has_cultural_pillar = heritage_iranian } martial >= 8 }
			scope:actor = { add_gold = 150 }
			if = {
				limit = { culture = culture:daylamite }
				scope:actor = { spawn_army = { men_at_arms = { type = pikemen_unit men = 200 } location = capital_province inheritable = no name = he_house_levy_army_name } }
			}
			else_if = {
				limit = { OR = { culture = culture:sogdian culture = culture:tajik } }
				scope:actor = {
					if = {
						limit = { has_dlc_feature = legacy_of_persia }
						spawn_army = { men_at_arms = { type = asawira men = 180 } location = capital_province inheritable = no name = he_house_levy_army_name }
					}
					else = {
						spawn_army = { men_at_arms = { type = horse_archers men = 180 } location = capital_province inheritable = no name = he_house_levy_army_name }
					}
				}
			}
			else = {
				scope:actor = { spawn_army = { men_at_arms = { type = armored_horsemen men = 180 } location = capital_province inheritable = no name = he_house_levy_army_name } }
			}
		}
		else = {
			scope:actor = { add_gold = 250 }
		}
		if = {
			limit = { is_landed = yes scope:actor = { is_landed = yes } }
			create_alliance = { target = scope:actor allied_through_owner = scope:recipient allied_through_target = scope:actor }
		}
	}
	scope:actor = {
		add_prestige = 200
		dynasty = { add_dynasty_prestige = 100 }
	}
}

### Soft refusal: no bad blood, the door stays open ###
he_house_soft_reject_effect = {
	scope:recipient = {
		add_character_flag = { flag = he_house_declined_soft years = 10 }
	}
}

### Hard refusal: the house turns hostile and exposes the pretender ###
he_house_hard_reject_effect = {
	scope:recipient = {
		add_character_flag = he_house_answered
		set_relation_rival = { target = scope:actor reason = rival_generic }
		add_opinion = { target = scope:actor modifier = he_spurned_the_call_opinion }
	}
	scope:actor = {
		add_character_flag = he_exposed_pretender
		every_independent_ruler = {
			limit = {
				this != scope:actor
				faith = { religion_tag = islam_religion }
				any_held_title = { tier >= tier_county title:e_persia = { is_de_jure_liege_or_above_target = prev } }
			}
			add_opinion = { target = scope:actor modifier = he_exposed_pretender_opinion }
		}
	}
}

### A dormant house rises — its SURVIVING members are minted on arrival, at their true age ###
# The 867 scions are NOT defined in history (an unlanded character alive at the bookmark can die,
# be married, or be landed before the player arrives — see zz_he_seven_houses.txt). Instead each
# house's living members are minted the moment the reconquest reaches its homeland, as children of a
# dead bloodline anchor so dynasty + lineage are inherited. Every member has a FIXED lifespan: past
# his death year (the deadlines in each wrapper below) he simply is not minted, and the senior
# survivor leads in his place. If NO member of the house is still alive, the house is gone — event
# he_reconquest.0003 ("too late") fires instead. Regiments start large and decay year-by-year,
# calibrated per house (not uniform), so arriving early means arriving with an army.

# Shared: mark the camp as arrived, the risen champion pledges, and the rally event fires.
he_champion_pledge_effect = {
	scope:actor = { if = { limit = { NOT = { has_character_flag = he_reached_eranshahr } } add_character_flag = he_reached_eranshahr } }
	scope:risen_champion = {
		add_character_flag = he_house_answered
		add_opinion = { target = scope:actor modifier = he_answered_the_call_opinion }
	}
	scope:actor = { trigger_event = he_reconquest.0002 }
}

# Shared: spawn a house regiment, BASE men through 880 then declining RATE men/year down to FLOOR. Uses the
# Legacy-of-Persia unit when that DLC is active, the base-game unit otherwise (standalone-safe).
he_spawn_house_army_effect = {
	scope:actor = {
		if = {
			limit = { has_dlc_feature = legacy_of_persia }
			spawn_army = {
				men_at_arms = { type = $LOP_MAA$ men = { value = $BASE$ subtract = { value = current_year subtract = 880 multiply = $RATE$ } min = $FLOOR$ max = $BASE$ } }
				location = capital_province inheritable = no name = he_house_levy_army_name
			}
		}
		else = {
			spawn_army = {
				men_at_arms = { type = $BASE_MAA$ men = { value = $BASE$ subtract = { value = current_year subtract = 880 multiply = $RATE$ } min = $FLOOR$ max = $BASE$ } }
				location = capital_province inheritable = no name = he_house_levy_army_name
			}
		}
	}
}

# Per-house wrappers. Called both by the arrival check below and by the debug "raise all" decision,
# so each house is defined in one place. Each expects scope:actor = the Sasanian, mints the surviving
# members (senior survivor as scope:risen_champion), spawns the regiment and fires the rally event —
# or, if the whole house has died out, fires the "too late" event instead. Birth years are preserved,
# so every member appears at his true age for the year he rises. Deadlines: past them, that person is
# dead and is not minted.

### Suren — cataphracts of Carrhae. Mihr (†900) leads; then his children Gew & Abanoy (†920) ###
he_raise_suren_effect = {
	if = {
		limit = { current_year <= 900 }			# Mihr still lives and leads
		scope:actor = {
			create_character = { employer = this father = character:suren_rustam dynasty = inherit name = Mihr gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 828 } martial = 18 prowess = 18 diplomacy = 10 stewardship = 12 learning = 7 intrigue = 8 trait = education_martial_4 trait = aggressive_attacker trait = physique_good_2 trait = ambitious trait = brave trait = diligent random_traits = no save_scope_as = risen_champion }
		}
		scope:risen_champion = {
			create_character = { employer = scope:actor father = this dynasty = inherit name = Gew    gender = male   faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 849 } martial = 12 prowess = 16 diplomacy = 6 stewardship = 6 learning = 5 intrigue = 5 trait = education_martial_3 trait = just trait = trusting trait = diligent random_traits = no }
			create_character = { employer = scope:actor father = this dynasty = inherit name = Abanoy gender = female faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 857 } diplomacy = 12 intrigue = 10 learning = 8 stewardship = 8 trait = education_diplomacy_3 trait = gregarious trait = compassionate trait = honest random_traits = no }
		}
		he_spawn_house_army_effect = { LOP_MAA = tarkhan BASE_MAA = cataphract BASE = 220 RATE = 4.5 FLOOR = 40 }
		he_champion_pledge_effect = yes
	}
	else_if = {
		limit = { current_year <= 920 }			# Mihr dead — his son Gew leads, sister Abanoy with him
		dynasty:1042143 = { save_scope_as = rc_dyn }
		scope:actor = {
			create_character = { employer = this dynasty = scope:rc_dyn name = Gew    gender = male   faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 849 } martial = 16 prowess = 18 diplomacy = 7 stewardship = 8 trait = education_martial_3 trait = aggressive_attacker trait = just trait = trusting trait = diligent random_traits = no save_scope_as = risen_champion }
			create_character = { employer = this dynasty = scope:rc_dyn name = Abanoy gender = female faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 857 } diplomacy = 12 intrigue = 10 learning = 8 stewardship = 8 trait = education_diplomacy_3 trait = gregarious trait = compassionate trait = honest random_traits = no }
		}
		he_spawn_house_army_effect = { LOP_MAA = tarkhan BASE_MAA = cataphract BASE = 220 RATE = 4.5 FLOOR = 40 }
		he_champion_pledge_effect = yes
	}
	else = { scope:actor = { trigger_event = he_reconquest.0003 } }	# all of House Suren are dead
}

### Ispahbudhan — the asawaran of the northern march. Vistahm (†900) is the last of the line ###
he_raise_ispahbudhan_effect = {
	if = {
		limit = { current_year <= 900 }
		scope:actor = {
			create_character = { employer = this father = character:ispahbudhan_gen3 dynasty = inherit name = Vistahm gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 830 } martial = 18 prowess = 16 diplomacy = 14 stewardship = 10 learning = 7 intrigue = 12 trait = education_martial_4 trait = open_terrain_expert trait = ambitious trait = arrogant trait = patient random_traits = no save_scope_as = risen_champion }
		}
		he_spawn_house_army_effect = { LOP_MAA = asawira BASE_MAA = horse_archers BASE = 260 RATE = 4.5 FLOOR = 120 }
		he_champion_pledge_effect = yes
	}
	else = { scope:actor = { trigger_event = he_reconquest.0003 } }
}

### Waraz — Median champions. Godarz (†880) leads only if reached at once; then Warazdad & cousin Zamasp (†920) ###
he_raise_waraz_effect = {
	if = {
		limit = { current_year <= 880 }			# Godarz still lives and leads
		scope:actor = {
			create_character = { employer = this father = character:waraz_adurbad dynasty = inherit name = Godarz gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 816 } martial = 14 prowess = 22 diplomacy = 6 stewardship = 7 learning = 5 intrigue = 6 trait = education_martial_3 trait = lifestyle_blademaster trait = physique_good_2 trait = brave trait = wrathful trait = honest random_traits = no save_scope_as = risen_champion }
		}
		scope:risen_champion = {
			create_character = { employer = scope:actor father = this dynasty = inherit name = Warazdad gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 851 } martial = 11 prowess = 16 diplomacy = 5 stewardship = 6 trait = education_martial_3 trait = patient trait = humble trait = brave random_traits = no }
		}
		scope:actor = {
			create_character = { employer = this father = character:waraz_shapur dynasty = inherit name = Zamasp gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 848 } martial = 10 prowess = 14 stewardship = 10 learning = 8 trait = education_martial_2 trait = calm trait = temperate trait = diligent random_traits = no }
		}
		he_spawn_house_army_effect = { LOP_MAA = mubarizun BASE_MAA = mubarizun BASE = 200 RATE = 3.75 FLOOR = 50 }
		he_champion_pledge_effect = yes
	}
	else_if = {
		limit = { current_year <= 920 }			# Godarz dead — his son Warazdad leads, cousin Zamasp with him
		dynasty:1042145 = { save_scope_as = rc_dyn }
		scope:actor = {
			create_character = { employer = this dynasty = scope:rc_dyn name = Warazdad gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 851 } martial = 15 prowess = 19 diplomacy = 6 stewardship = 7 trait = education_martial_3 trait = lifestyle_blademaster trait = patient trait = humble trait = brave random_traits = no save_scope_as = risen_champion }
			create_character = { employer = this father = character:waraz_shapur dynasty = inherit name = Zamasp gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 848 } martial = 10 prowess = 14 stewardship = 10 learning = 8 trait = education_martial_2 trait = calm trait = temperate trait = diligent random_traits = no }
		}
		he_spawn_house_army_effect = { LOP_MAA = mubarizun BASE_MAA = mubarizun BASE = 200 RATE = 3.75 FLOOR = 50 }
		he_champion_pledge_effect = yes
	}
	else = { scope:actor = { trigger_event = he_reconquest.0003 } }	# all of House Waraz are dead
}

### Spandiyadh — the bow of Isfandiyar. Isfandiyar (†890) is the last of the line ###
he_raise_spandiyadh_effect = {
	if = {
		limit = { current_year <= 890 }
		scope:actor = {
			create_character = { employer = this father = character:spandiyadh_peroz dynasty = inherit name = Isfandiyar gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 826 } martial = 14 prowess = 21 diplomacy = 7 stewardship = 6 learning = 12 intrigue = 5 trait = education_martial_prowess_4 trait = unyielding_defender trait = zealous trait = brave trait = stubborn random_traits = no save_scope_as = risen_champion }
		}
		he_spawn_house_army_effect = { LOP_MAA = bowmen BASE_MAA = bowmen BASE = 340 RATE = 7 FLOOR = 200 }
		he_champion_pledge_effect = yes
	}
	else = { scope:actor = { trigger_event = he_reconquest.0003 } }
}

### Zik — riders of Adurbadagan. Aturpat (†890) leads; then his son Ardeshir (†920) ###
he_raise_zik_effect = {
	if = {
		limit = { current_year <= 890 }			# Aturpat still lives and leads
		scope:actor = {
			create_character = { employer = this father = character:zik_burzin dynasty = inherit name = Aturpat gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 825 } martial = 17 prowess = 15 diplomacy = 8 stewardship = 10 learning = 14 intrigue = 7 trait = education_martial_4 trait = rough_terrain_expert trait = just trait = temperate trait = zealous random_traits = no save_scope_as = risen_champion }
		}
		scope:risen_champion = {
			create_character = { employer = scope:actor father = this dynasty = inherit name = Ardeshir gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 853 } martial = 10 prowess = 14 diplomacy = 5 learning = 6 trait = education_martial_3 trait = gregarious trait = impatient trait = ambitious random_traits = no }
		}
		he_spawn_house_army_effect = { LOP_MAA = tawashi BASE_MAA = light_horsemen BASE = 300 RATE = 6 FLOOR = 50 }
		he_champion_pledge_effect = yes
	}
	else_if = {
		limit = { current_year <= 920 }			# Aturpat dead — his son Ardeshir leads
		dynasty:dyn_zik = { save_scope_as = rc_dyn }
		scope:actor = {
			create_character = { employer = this dynasty = scope:rc_dyn name = Ardeshir gender = male faith = faith:mazdayasna culture = culture:persian age = { value = current_year subtract = 853 } martial = 16 prowess = 17 diplomacy = 7 stewardship = 8 learning = 10 trait = education_martial_3 trait = rough_terrain_expert trait = gregarious trait = impatient trait = ambitious random_traits = no save_scope_as = risen_champion }
		}
		he_spawn_house_army_effect = { LOP_MAA = tawashi BASE_MAA = light_horsemen BASE = 300 RATE = 6 FLOOR = 50 }
		he_champion_pledge_effect = yes
	}
	else = { scope:actor = { trigger_event = he_reconquest.0003 } }	# all of House Zik are dead
}

### Checked as the heir moves: raise whichever house's homeland he has reached ###
he_check_house_arrivals_effect = {
	save_scope_as = actor
	if = {
		limit = { he_reached_suren_lands_trigger = yes NOT = { has_character_flag = he_house_suren_risen } }
		add_character_flag = he_house_suren_risen
		he_raise_suren_effect = yes
	}
	if = {
		limit = { he_reached_ispahbudhan_lands_trigger = yes NOT = { has_character_flag = he_house_ispahbudhan_risen } }
		add_character_flag = he_house_ispahbudhan_risen
		he_raise_ispahbudhan_effect = yes
	}
	if = {
		limit = { he_reached_waraz_lands_trigger = yes NOT = { has_character_flag = he_house_waraz_risen } }
		add_character_flag = he_house_waraz_risen
		he_raise_waraz_effect = yes
	}
	if = {
		limit = { he_reached_spandiyadh_lands_trigger = yes NOT = { has_character_flag = he_house_spandiyadh_risen } }
		add_character_flag = he_house_spandiyadh_risen
		he_raise_spandiyadh_effect = yes
	}
	if = {
		limit = { he_reached_zik_lands_trigger = yes NOT = { has_character_flag = he_house_zik_risen } }
		add_character_flag = he_house_zik_risen
		he_raise_zik_effect = yes
	}
}
