﻿# invader appears, before story
spawn_fatimid_invader_effect = {
	random_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_start
		title_province = { save_scope_as = invader_spawn_location }
	}

	# father
	character:33920 = { save_scope_as = abdullah_father } # id 7259, hist-id 33920
	# invader
	create_character = { # ID: 9810, history-ID: 32900
		template = abdullah_fatimid_character_template
		dynasty = inherit
		father = scope:abdullah_father
		location = scope:invader_spawn_location
		save_scope_as = invader
	}

	scope:invader = {
		add_gold = 3500
		add_prestige = 1750
		add_piety = 3000

		dynasty = {
			add_dynasty_prestige_level = 3
			add_dynasty_prestige = 3500
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}

		# rivals
		if = {
			limit = { exists = title:d_sunni.holder }
			set_relation_rival = {
				target = title:d_sunni.holder
				reason = rival_competing_caliph_reason
			}
		}
	}

	every_player = {
		trigger_event = $INVADER_DYNN$_invasion.1001 # Notification: Start of invasion
	}
}

fatimid_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
	add_dynasty_perk = warfare_legacy_2
	add_dynasty_perk = warfare_legacy_3
}

fatimid_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	give_invader_land_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		STARTING_TIER = $STARTING_TIER$
	}
	spawn_$INVADER_DYNN$_family_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
	}
	spawn_invader_servants_effect = {
		INVADER_CHAR = $INVADER_CHAR$
	}
	form_the_invader_title_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		INVADER_TITLE = $INVADER_TITLE$
		LANDLESS_YES_NO = $LANDLESS_YES_NO$
	}
	fatimid_grant_historical_artifacts_effect = yes
}

### populating the life of the invader character
spawn_fatimid_family_effect = {
	# Create wife
	create_character = {
		template = hi_random_spouse_character_template
		age = { 32 38 }
		employer = $INVADER_CHAR$
		save_scope_as = abdullah_fatimid_spouse
	}
	scope:abdullah_fatimid_spouse = {
		marry = $INVADER_CHAR$
		disease_immunity_short_effect = yes
	}

	$INVADER_CHAR$ = { save_scope_as = $INVADER_DYNN$_family_father }
	scope:abdullah_fatimid_spouse = { save_scope_as = $INVADER_DYNN$_family_mother }

	# three extra spouses
	create_character = {
		age = { 20 24 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = spouse_2
		after_creation = {
			disease_immunity_short_effect = yes
		}
	}
	create_character = {
		age = { 20 24 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = spouse_3
		after_creation = {
			disease_immunity_short_effect = yes
		}
	}
	create_character = {
		age = { 20 24 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = spouse_4
		after_creation = {
			disease_immunity_short_effect = yes
		}
	}
	$INVADER_CHAR$ = {
		marry = scope:spouse_2
		marry = scope:spouse_3
		marry = scope:spouse_4
	}

	# al-qa'im (son)...
	create_character = { # ID: 10659, history-ID: 32901
		template = al-qaim_fatimid_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		mother = scope:$INVADER_DYNN$_family_mother
		father = scope:$INVADER_DYNN$_family_father
		save_scope_as = al-qaim_fatimid
	}
	scope:al-qaim_fatimid = {
		#Make it more likely that he gets a good education
		hidden_effect = { # causes errors...
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}

	# spouse of al-qa'im
	create_character = { # ID: 10659, history-ID: 32901
		template = al-qaim_fatimid_spouse_character_template
		employer = $INVADER_CHAR$
		save_scope_as = al-qaim_fatimid_spouse
	}
	scope:al-qaim_fatimid_spouse = {
		create_betrothal = scope:al-qaim_fatimid
		#Make it more likely that he gets a good education
		hidden_effect = { # causes errors...
			#education_point_acquisition_effect = yes
			#education_point_acquisition_effect = yes
		}
	}
}

# Make sure invader has troops
spawn_fatimid_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_fatimid_army_effect = yes
	}
	spawn_fatimid_army_effect = yes
	spawn_fatimid_army_effect = yes
}
spawn_fatimid_army_effect = {
	spawn_army = {
		name = fatimid_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
	
		levies = 250
		men_at_arms = {
			type = light_footmen
			stacks = 5
		}
		men_at_arms = {
			type = bowmen
			stacks = 5
		}
		men_at_arms = {
			type = camel_rider
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}

fatimid_grant_historical_artifacts_effect = {
	# see historical_artifacts.0023 for DLC restrictions
	title:d_shiite.holder = {
		create_artifact_al_taj_crown_effect = { OWNER = this } # character artifact
		create_artifact_qadib_al_mulk_effect = { OWNER = this } # character artifact
		if = {
			limit = {
				#has_ep1_dlc_trigger = yes
				has_dlc_feature = royal_court
			}
			create_artifact_al_sayf_al_khass_effect = { OWNER = this } # court artifact
			create_artifact_wall_banner_thankfulness_effect = { OWNER = this } # court artifact
			create_artifact_al_dawat_effect = { OWNER = this } # court artifact
			create_artifact_al_hafir_effect = { OWNER = this } # court artifact
		}
	}
}

fatimid_grant_mecca_to_hashimids_decision_effect = {
	character:163007 = { save_scope_as = hashimid_dynasty_root }

	# attempt to find a house member
	scope:hashimid_dynasty_root.house = {
		random_house_member = {
			limit = {
				is_alive = yes
				is_ai = yes
				is_ruler = no
				is_landed = no
				is_adult = yes
				is_healthy = yes
				trigger_if = {
					limit = { faith:zayidi = { has_doctrine_parameter = male_dominated_law } }
					is_male = yes
				}
				trigger_else_if = {
					limit = { faith:zayidi = { has_doctrine_parameter = female_dominated_law } }
					is_female = yes
				}
				trigger_else = {
					OR = {
						is_male = yes
						is_female = yes
					}
				}
				age >= 17
				age < 47
			}
			save_scope_as = hashimid_receiver
		}
	}

	# attempt to find a dynasty member
	if = {
		limit = { NOT = { exists = scope:hashimid_receiver } }
		scope:hashimid_dynasty_root.dynasty = {
			random_dynasty_member = {
				limit = {
					is_alive = yes
					is_ai = yes
					is_ruler = no
					is_landed = no
					is_adult = yes
					is_healthy = yes
					trigger_if = {
						limit = { faith:zayidi = { has_doctrine_parameter = male_dominated_law } }
						is_male = yes
					}
					trigger_else_if = {
						limit = { faith:zayidi = { has_doctrine_parameter = female_dominated_law } }
						is_female = yes
					}
					trigger_else = {
						OR = {
							is_male = yes
							is_female = yes
						}
					}
					age >= 17
					age < 47
				}
				save_scope_as = hashimid_receiver
			}
		}
	}

	# adjust picked character
	if = {
		limit = { exists = scope:hashimid_receiver }
		scope:hashimid_receiver = {
			set_character_faith_with_conversion = faith:zayidi
		}
	}

	if = {
		limit = { NOT = { exists = scope:hashimid_receiver } }
		# pick fake father
		character:45108 = { save_scope_as = hashimid_father }
		# create receiver
		create_character = {
			template = hashimid_mecca_character_template
			father = scope:hashimid_father
			dynasty = inherit
			employer = root
			save_scope_as = hashimid_receiver
			after_creation = {}
		}
	}

	# grant titles
	custom_tooltip = {
		text = fatimid_grant_mecca_to_hashimids_decision_title_change_tt
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:d_mecca = {
			change_title_holder = {
				holder = scope:hashimid_receiver
				change = scope:title_change
			}
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
				}
				change_title_holder = {
					holder = scope:hashimid_receiver
					change = scope:title_change
				}
			}
		}
		title:d_medina = {
			change_title_holder = {
				holder = scope:hashimid_receiver
				change = scope:title_change
			}
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
				}
				change_title_holder = {
					holder = scope:hashimid_receiver
					change = scope:title_change
				}
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}

	# vassalage
	create_title_and_vassal_change = {
		type = swear_fealty
		save_scope_as = swealty_change
	}
	scope:hashimid_receiver = {
		change_liege = {
			liege = root
			change = scope:swealty_change
		}
	}
	resolve_title_and_vassal_change = scope:swealty_change
}

fatimid_grant_mecca_to_hashimids_decision_after_effect = {
	scope:hashimid_receiver = {
		# contract
		vassal_contract_set_obligation_level = { type = jizya_special_rights level = 1 }
		vassal_contract_set_obligation_level = { type = marriage_favor_rights level = 1 }
		set_vassal_contract_modification_blocked = yes
		# county faith
		every_sub_realm_county = {
			limit = { NOT = { faith = faith:zayidi } }
			set_county_faith = faith:zayidi
		}
		# family
		trigger_event = game_rule.1001
		# other
		disease_immunity_long_effect = yes
	}

	# children
	if = {
		limit = { scope:hashimid_receiver = { age >= 22 } }
		create_character = {
			template = hi_random_child_character_template
			gender = female
			mother = scope:hashimid_receiver.primary_spouse
			father = scope:hashimid_receiver
			dynasty = inherit
			employer = scope:hashimid_receiver
			after_creation = { disease_immunity_long_effect = yes }
		}
		create_character = {
			template = hi_random_child_character_template
			gender = male
			mother = scope:hashimid_receiver.primary_spouse
			father = scope:hashimid_receiver
			dynasty = inherit
			employer = scope:hashimid_receiver
			after_creation = { disease_immunity_long_effect = yes }
		}
		create_character = {
			template = hi_random_child_character_template
			gender = female
			mother = scope:hashimid_receiver.primary_spouse
			father = scope:hashimid_receiver
			dynasty = inherit
			employer = scope:hashimid_receiver
			after_creation = { disease_immunity_long_effect = yes }
		}
		create_character = {
			template = hi_random_child_character_template
			gender = male
			mother = scope:hashimid_receiver.primary_spouse
			father = scope:hashimid_receiver
			dynasty = inherit
			employer = scope:hashimid_receiver
			after_creation = { disease_immunity_long_effect = yes }
		}
	}
}