namespace = fp3_story_cycle_seljuks_invasion_events

### RISE OF THE SELJUKS #############################################
## 0001 	Leader and Story Cycle Creation		by Maxence Voleau
## 0002 	The First Settlers					by Hugo Cortell
#####################################################################

###################################
# Setup: we create the leader and start the story cycle
# By Maxence Voleau
###################################
fp3_story_cycle_seljuks_invasion_events.0001 = {
	scope = none
	hidden = yes

	immediate = {
		spawn_seljuk_character_effect = yes
		scope:lead_seljuk = {
			create_story = story_seljuk_invasion
		}
	}
}

###################################
# The First Settlers
# By Hugo Cortell
###################################
fp3_story_cycle_seljuks_invasion_events.0002 = {
	type = character_event
	title = fp3_story_cycle_seljuks_invasion_events.0001.t
	desc = {
		desc = fp3_story_cycle_seljuks_invasion_events.0001.desc
		first_valid = {
			triggered_desc = {
				trigger = { highest_held_title_tier > tier_duchy }
				desc = fp3_story_cycle_seljuks_invasion_events.0001.desc_vassal
			}
			desc = fp3_story_cycle_seljuks_invasion_events.0001.desc_independent
		}
	}
	theme = realm

	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:lead_seljuk
		animation = personality_honorable
	}

	immediate = {
		save_scope_as = seljuk_victim
		random_sub_realm_county = {
			limit = {
				any_neighboring_county = {
					exists = holder
					holder.top_liege != scope:seljuk_victim
				}
				de_jure_liege = scope:fp3_seljuk_settle_target
			}
			alternative_limit = {
				any_neighboring_county = {
					exists = holder
					holder.top_liege != scope:seljuk_victim
				}
				any_neighboring_county = { de_jure_liege = scope:fp3_seljuk_settle_target }
			}
			alternative_limit = {
				any_neighboring_county = {
					exists = holder
					holder.top_liege != scope:seljuk_victim
				}
			}
			random_county_province = {
				limit = {
					this != scope:fp3_seljuk_settle_target.title_capital_county.title_province
				}
				save_scope_as = spawn_location
			}
		}
		save_scope_value_as = {
			name = victim_strength_modifier
			value = {
				value = current_military_strength
				divide = 10000
				add = 1.25
			}
		}
		scope:lead_seljuk = {
			#move to the army spawn location, so it's less confusing when you want to see where they are
			set_location = scope:spawn_location
			var:seljuk_birthplace = { save_scope_as = seljuk_birthplace }
		}
	}

	option = { # Yes - Serve me well!
		name = fp3_story_cycle_seljuks_invasion_events.0001.a_liege

		fp3_story_cycle_seljuks_invasion_events_grant_seljuks_land_effect = yes
		scope:lead_seljuk = {
			add_trait = loyal
			culture = {
				change_cultural_acceptance = {
					target = scope:seljuk_victim.culture
					value = 30
					desc = STEWARD_PROMOTE_CULTURE_ALLOWED_NOMAD_SETTLEMENT
				}
			}
		}
		create_seljuk_army_at_peace_effect = yes
		debug_log = "Seljuk got the land and were vassalized"
		ai_chance = {
			base = 50

			ai_value_modifier = {
				ai_boldness = -0.5
				ai_compassion = 0.2
				ai_greed = -0.5
				ai_energy = -0.5
				ai_rationality = 0.3
				ai_sociability = 0.3
			}
		}
	}

	option = { # No - To war with you!
		name = fp3_story_cycle_seljuks_invasion_events.0001.b

		# Start war
		scope:lead_seljuk = {
			start_war = {
				casus_belli = fp3_seljuk_invasion_cb
				target = root
				target_title = scope:fp3_seljuk_settle_target
			}
			random_character_war = { save_scope_as = war }
		}

		create_seljuk_army_effect = yes
		debug_log = "Seljuk have to win a war"
		ai_chance = {
			base = 50

			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = 0.5
				ai_energy = 0.5
				ai_vengefulness = 0.5
			}
		}
	}
}
