﻿namespace = hasan_sabbah

# Ismaili Preachers
# You meet Amira Zarrab and begin the story cycle
# 1000
hasan_sabbah.1000 = {
	type = character_event
	title = hasan_sabbah.1000.t
	desc = hasan_sabbah.1000.desc
	theme = learning
	override_background = { reference = temple }
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:amira
		animation = personality_zealous
	}

	trigger = {
		has_ep3_dlc_trigger = yes
	}

	immediate = {
		hidden_effect = {
			create_character = {
				name = amira
				age = { 20 50 }
				gender = male
				trait = zealous
				trait = gregarious
				trait = diligent
				trait = loyal
				random_traits_list = {
					count = 1
					education_learning_3 = {}
					education_learning_4 = {}
				}
				location = root.location
				faith = faith:ismaili
				culture = culture:persian
				after_creation = {
					dynasty = { set_dynasty_name = dynn_zarrab }
					house = { set_house_name = dynn_zarrab }
				}
				save_scope_as = amira
			}
			location = { save_scope_as = location }
		}
	}

	# You have succeeded in wounding and breaching my belief
	option = {
		name = hasan_sabbah.1000.a
		flavor = begin_hasan_cycle_ismaili_tt
		add_courtier = scope:amira
		camp_officer_grant_effect = {
			EMPLOYER = root
			CANDIDATE = scope:amira
			POS = second
		}

		set_character_faith = faith:ismaili
		custom_tooltip = {
			text = unlock_evangelize_decision_tt
			create_story = story_hasan
			random_owned_story = {
				type = story_hasan
				set_variable = {
					name = story_phase
					value = 1
				}
			}
			every_courtier = {
				limit = {
					religion = religion:islam_religion
				}
				set_character_faith = faith:ismaili
			}
		}
		custom_tooltip = advance_to_the_assassins_tt
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Truth cannot be sought outside the pale of Islam
	option = {
		name = hasan_sabbah.1000.b
		flavor = begin_hasan_cycle_twelver_tt
		custom_tooltip = {
			text = unlock_evangelize_decision_tt
			create_story = story_hasan
			random_owned_story = {
				type = story_hasan
				set_variable = {
					name = story_phase
					value = 1
				}
			}
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# Shiite Evangelism
# You try to convert the local populace to your faith
# 1010-1019
hasan_sabbah.1010 = {
	type = character_event
	title = hasan_sabbah.1010.t
	desc = hasan_sabbah.1010.desc
	theme = learning
	override_background = {
		trigger = {
			location = {
				county.development_level >= 10
				has_holding = yes
			}
		}
		reference = market
	}
	override_background = {
		trigger = {
			location = {
				OR = {
					county.development_level < 10
					has_holding = no
				}
			}
		}
		reference = terrain_travel
	}
	left_portrait = {
		character = root
		animation = debating
	}

	# Preach the faith, diplomatically
	option = {
		name = hasan_sabbah.1010.a
		duel = {
			skills = { diplomacy learning }
			value = high_skill_rating
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					add = {
						subtract = scope:location.county.county_opinion
					}
				}
				modifier = {
					factor = 0.75
					scope:location.county.religion != root.religion
				}
				modifier = {
					factor = 10
					scope:location.county = title:c_qazwin
				}
				min = 10
				send_interface_toast = {
					type = event_toast_effect_good
					title = hasan_sabbah.1010.a.win
					left_icon = root
					ep3_hasan_missionary_effect = {
						PREACHER = root
						LOCATION = scope:location
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					add = scope:location.county.county_opinion
				}
				modifier = {
					factor = 1.25
					scope:location.county.religion != root.religion
				}
				min = 10
				send_interface_toast = {
					type = event_toast_effect_bad
					title = hasan_sabbah.1010.a.lose
					left_icon = root
					scope:location.county = {
						add_county_modifier = {
							modifier = tougher_to_convert
							years = 10
						}
					}
				}
			}
		}
		ai_chance = {
			base = 10
		}
	}

	after = {
		hidden_effect = {
			# Send Hasan to Egypt
			if = {
				limit = {
					any_owned_story = {
						type = story_hasan
						exists = var:radical_points
						var:radical_points >= 5
					}
					OR = {
						faith = faith:ismaili
						faith = faith:nizari
					}
					NOR = {
						has_character_flag = arrived_in_egypt
						has_character_flag = had_hasan_sabah_20
					}
				}
				trigger_event = {
					id = hasan_sabbah.1020
					months = { 3 5 }
				}
			}
			# Make Hasan a radical (begin forming the Assassins)
			else_if = {
				limit = {
					any_owned_story = {
						type = story_hasan
						exists = var:radical_points
						var:radical_points >= 15
						NOT = { exists = var:ultimate_foe }
					}
				}
				trigger_event = {
					id = hasan_sabbah.1031
					months = { 3 5 }
				}
			}
			else_if = {
				limit = {
					any_owned_story = {
						type = story_hasan
						exists = var:story_phase
						var:story_phase >= 2
						NOT = { exists = var:ultimate_foe }
					}
					faith:ismaili = {
						NOT = {
							religious_head = scope:location.county.holder
							religious_head = scope:location.county.holder.top_liege
							religious_head = scope:location.county.holder.liege
							religious_head = scope:location.county.holder.liege.liege
						}
					}
					faith:nizari = {
						NOT = {
							religious_head = scope:location.county.holder
							religious_head = scope:location.county.holder.top_liege
							religious_head = scope:location.county.holder.liege
							religious_head = scope:location.county.holder.liege.liege
						}
					}
				}
				trigger_event = {
					id = hasan_sabbah.1031
					months = { 3 5 }
				}
			}
		}
	}
}

# Divine Calling
# You're summoned to Egypt
# 1020-1029
hasan_sabbah.1020 = {
	type = character_event
	title = hasan_sabbah.1020.t
	desc = hasan_sabbah.1020.desc
	theme = learning
	override_background = { reference = terrain }
	left_portrait = {
		character = root
		animation = personality_zealous
	}
	lower_right_portrait = {
		character = scope:ismaili_caliph
		animation = personality_zealous
	}

	trigger = {
		is_travelling = no
		NOT = {
			has_character_flag = had_hasan_sabah_20
		}
		faith:ismaili.religious_head ?= { is_landed = yes }
	}

	immediate = {
		if = {
			limit = {
				NOT = { has_character_flag = had_hasan_sabah_20 }
			}
			add_character_flag = {
				flag = had_hasan_sabah_20
				years = 1
			}
		}
		faith:ismaili.religious_head = {
			save_scope_as = ismaili_caliph
			capital_county = { save_scope_as = cairo }
		}
	}

	# Let'sa go
	option = {
		name = hasan_sabbah.1020.a
		custom_tooltip = hasan_sabbah.1020.a.tt
		custom_tooltip = advance_to_the_assassins_tt
		start_travel_plan = {
			destination = scope:ismaili_caliph.capital_province
			players_use_planner = yes
			return_trip = no
			travel_with_domicile = yes
			on_arrival_destinations = last
			on_arrival_event = hasan_sabbah.1022
			on_travel_planner_cancel_event = hasan_sabbah.1020
		}

		ai_chance = {
			base = 1000 # Historical path
		}
	}

	# Let'sa not
	option = {
		name = hasan_sabbah.1020.b
		stress_impact = {
			zealous = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}
}

# The Ismaili Caliphate
# Arrive in Egypt, is it time to become Nizari? I think it's time to become Nizari
# 1022
hasan_sabbah.1022 = {
	type = character_event
	title = hasan_sabbah.1022.t
	desc = hasan_sabbah.1022.desc
	theme = realm
	override_background = { reference = throne_room_scope }
	left_portrait = {
		character = root
		animation = obsequious_bow
	}
	right_portrait = {
		character = scope:ismaili_caliph
		animation = personality_honorable
	}
	lower_left_portrait = {
		character = character:41706
	}
	lower_right_portrait = {
		character = character:3098
	}

	immediate = {
		add_character_flag = arrived_in_egypt
		faith:ismaili.religious_head = {
			save_scope_as = ismaili_caliph
			save_scope_as = background_throne_room_scope
		}
		character:41706 = { save_scope_as = nizar }
	}

	# Nizar knows what's up
	option = {
		name = hasan_sabbah.1022.a
		if = {
			limit = {
				character:41706 ?= { is_alive = yes }
			}
			set_relation_friend = {
				target = character:41706
				reason = friend_supported_my_claim
			}
			hidden_effect = {
				if = {
					limit = { NOT = { character:41706.faith = faith:nizari } }
					character:41706 = {
						set_character_faith = faith:nizari
					}
				}
			}
		}
		ep3_hasan_advance_radicalism = yes
		if = {
			limit = {
				NOT = {
					faith = faith:nizari
				}
			}
			set_character_faith = faith:nizari
		}
		add_piety = major_piety_gain
		ai_chance = {
			base = 1000 # Historical path
		}
	}

	# Actually, the Ismaili candidate seems like a cool guy
	option = {
		name = hasan_sabbah.1022.b
		if = {
			limit = {
				NOT = {
					faith = faith:ismaili
				}
			}
			set_character_faith = faith:ismaili
		}
		stress_impact = {
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}
}

# Death of a Caliph
# The original caliph died, it's *definitely* time to become Nizari
# 1030
hasan_sabbah.1030 = {
	type = character_event
	title = hasan_sabbah.1030.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:new_ismaili_caliph }
				desc = hasan_sabbah.1030.desc_caliph_heir
			}
			desc = hasan_sabbah.1030.desc_caliph_no_heir
		}
	}
	theme = realm
	override_background = { reference = throne_room }
	left_portrait = {
		character = root
		animation = rage
	}
	right_portrait = {
		character = scope:new_ismaili_caliph
		animation = personality_dishonorable
	}
	lower_left_portrait = {
		character = character:41706
	}

	trigger = {
		OR = {
			faith = faith:ismaili
			faith = faith:nizari
		}
		exists = faith:ismaili.religious_head
	}

	immediate = {
		faith:ismaili.religious_head = { save_scope_as = new_ismaili_caliph }
		character:41706 ?= {
			liege = { save_scope_as = liege }
			save_scope_as = nizar
			if = {
				limit = {
					is_ai = yes
					is_alive = yes
				}
				banish_effect = { BANISHER = scope:liege }
			}
			hidden_effect = {
				if = {
					limit = { NOT = { faith = faith:nizari } }
					set_character_faith = faith:nizari
				}
			}
		}
		random_owned_story = {
			type = story_hasan
			save_scope_as = story
		}
	}

	# Nizar knows what's up
	option = {
		name = hasan_sabbah.1030.a
		if = {
			limit = {
				character:41706 ?= { is_alive = yes }
			}
			set_relation_friend = {
				target = character:41706
				reason = friend_supported_my_claim
			}
			if = {
				limit = {
					character:41706 = { is_landed = no }
				}
				add_courtier = character:41706
				scope:story = {
					set_variable = {
						name = preferred_caliph
						value = character:41706
					}
				}
			}
		}
		else = {
			scope:story = {
				set_variable = {
					name = preferred_caliph
					value = root
				}
			}
		}
		custom_tooltip = {
			text = advance_to_the_assassins_tt
			scope:story = {
				set_variable = {
					name = story_phase
					value = 2
				}
			}
		}
		if = {
			limit = { NOT = { faith = faith:nizari } }
			set_character_faith = faith:nizari
		}
		add_piety = medium_piety_gain
		if = {
			limit = { is_ai = yes }
			domicile = { move_domicile = title:b_alamut.title_province }
		}
		else = {
			start_travel_plan = {
				destination = title:b_alamut.title_province
				players_use_planner = yes
				return_trip = no
				travel_with_domicile = yes
				on_arrival_destinations = last
			}
		}
		ai_chance = {
			base = 1000 # Historical path
		}
	}

	# Actually, the Ismaili candidate seems like a cool guy
	option = {
		name = hasan_sabbah.1030.b
		trigger = {
			exists = scope:new_ismaili_caliph
		}
		scope:story = {
			set_variable = {
				name = preferred_caliph
				value = scope:new_ismaili_caliph
			}
		}
		set_relation_friend = {
			target = scope:new_ismaili_caliph
			reason = friend_supported_my_claim
		}
		if = {
			limit = { NOT = { faith = faith:ismaili } }
			set_character_faith = faith:ismaili
		}
		stress_impact = {
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}

	# Actually, the Ismaili are right, but have no heir
	option = {
		name = hasan_sabbah.1030.c
		trigger = {
			NOT = { exists = scope:new_ismaili_caliph }
		}
		if = {
			limit = { NOT = { faith = faith:ismaili } }
			set_character_faith = faith:ismaili
		}
		stress_impact = {
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}
}

# The Enemy of Nizarism
# Time to decide my ultimate enemy
# 1031
hasan_sabbah.1031 = {
	type = character_event
	title = hasan_sabbah.1031.t
	desc = hasan_sabbah.1031.desc
	theme = realm
	override_background = { reference = throne_room }
	left_portrait = {
		character = root
		animation = rage
	}
	right_portrait = {
		character = scope:seljuk_equivalent
		animation = personality_dishonorable
	}

	trigger = {
		scope:location.county.holder.top_liege = {
			faith != root.faith
			realm_size >= 10
			highest_held_title_tier >= tier_kingdom
		}
		any_owned_story = {
			type = story_hasan
			NOT = {
				exists = var:ultimate_foe
			}
		}
	}

	immediate = {
		scope:location.county.holder.top_liege = { save_scope_as = seljuk_equivalent }
		random_owned_story = {
			type = story_hasan
			save_scope_as = story
		}
	}

	# It's this guy
	option = {
		name = hasan_sabbah.1031.a
		custom_tooltip = {
			text = unlock_final_hasan_decisions_1_tt
			scope:story = {
				set_variable = {
					name = ultimate_foe
					value = scope:seljuk_equivalent
				}
			}
		}
		if = {
			limit = {
				faith = faith:nizari
			}
			custom_tooltip = unlock_final_hasan_decisions_2_tt
		}
		custom_tooltip = unlock_final_hasan_decisions_3_tt
		set_relation_nemesis = {
			target = scope:seljuk_equivalent
			reason = nemesis_eternal_enemy
		}

		ai_chance = {
			base = 0
			modifier = {
				add = 1000
				scope:seljuk_equivalent.house = house:house_seljuk
			}
		}
	}

	# This is not him
	option = {
		name = hasan_sabbah.1031.b
		custom_tooltip = look_for_another_enemy_tt
		ai_chance = {
			base = 0
			modifier = {
				add = 1000
				NOT = { scope:seljuk_equivalent.house = house:house_seljuk }
			}
		}
	}
}

# A Rousing Sermon
# Agitate the populace decision event
# 1040
hasan_sabbah.1040 = {
	type = character_event
	title = hasan_sabbah.1040.t
	desc = hasan_sabbah.1040.desc
	theme = realm
	override_background = { reference = terrain }
	left_portrait = {
		character = root
		animation = rage
	}

	immediate = {
		random_owned_story = {
			type = story_hasan
			var:ultimate_foe = { save_scope_as = ultimate_foe }
		}
	}

	# Make them angy
	option = {
		name = hasan_sabbah.1040.a
		duel = {
			skills = { intrigue learning }
			value = high_skill_rating
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					add = {
						subtract = scope:location.county.county_opinion
					}
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = hasan_sabbah.1040.a.win
					left_icon = root
					scope:location.county = {
						add_county_modifier = {
							modifier = ep3_agitated_populace
							years = 15
						}
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					add = scope:location.county.county_opinion
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = hasan_sabbah.1040.a.lose
					left_icon = root
					scope:location.county = {
						add_county_modifier = {
							modifier = tougher_to_convert
							years = 10
						}
					}
				}
			}
		}
		ai_chance = {
			base = 10
		}
	}
}

# The Old Man of the Mountain
# Post-war settling down event
# 1050
hasan_sabbah.1050 = {
	type = character_event
	title = hasan_sabbah.1050.t
	desc = hasan_sabbah.1050.desc
	theme = realm
	left_portrait = {
		character = root
		animation = thinking
	}

	# Let's settle
	option = {
		name = hasan_sabbah.1050.a

		custom_tooltip = create_new_kingdom
		create_dynamic_title = {
			tier = kingdom
			name = HASAN_KINGDOM
			adj = HASAN_KINGDOM_adj
		}
		change_government = clan_government
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:hasan_home = {
			duchy = {
				every_direct_de_jure_vassal_title = {
					limit = { tier = tier_county }
					change_title_holder = {
						holder = root
						change = scope:change
					}
				}
				change_title_holder = {
					holder = root
					change = scope:change
				}
			}
		}
		scope:new_title = {
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		hidden_effect = {
			scope:new_title = {
				set_de_jure_liege_title = root.location.empire
				set_coa = title:b_alamut
				set_color_from_title = scope:hasan_home
				set_capital_county = scope:hasan_home
			}
		}
		if = {
			limit = {
				exists = scope:caliph
				scope:caliph = {
					is_landed = no
				}
				scope:caliph.faith = root.faith
				NOT = { exists = root.faith.religious_head }
			}
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change2
				add_claim_on_loss = yes
			}
			custom_description = {
				text = "new_religious_head_title"
				object = scope:caliph
				subject = scope:caliph

				set_up_dynamic_spiritual_hof_title_existing_hof_effect = {
					CREATOR = root
					RELIGIOUS_HEAD = scope:caliph
				}
			}
			scope:caliph = {
				change_liege = {
					liege = root
					change = scope:change2
				}
			}
			resolve_title_and_vassal_change = scope:change2
		}
		destroy_laamp_effect = { ADVENTURER = root }
		ep3_become_landed_cleanup_effect = yes

		ai_chance = {
			base = 1000
		}
	}

	# Let's not
	option = {
		name = hasan_sabbah.1050.b
		ai_chance = {
			base = 0
		}
	}
}
