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

	character:273081 = { save_scope_as = dynasty_root }

	# spawned invader
	if = {
		limit = { NOT = { exists = scope:invader } }
		### Lamptuna
		character:273081 = { save_scope_as = talakakin_lamtuna } # founder of dynasty lamtuna
		if = {
			limit = { game_start_date < 870.1.1 }
			character:273073 = { save_scope_as = tamim_lamptuna }
			create_character = {
				template = addu_lamptuna_character_template
				dynasty = inherit
				location = scope:invader_spawn_location
				father = scope:tamim_lamptuna
				save_scope_as = addu_lamptuna
			}
			create_character = {
				template = talakakin_ii_lamptuna_character_template
				dynasty = inherit
				location = scope:invader_spawn_location
				father = scope:addu_lamptuna
				save_scope_as = talakakin_ii_lamptuna
			}
			create_character = {
				template = tifawt_lamptuna_character_template
				dynasty = inherit
				location = scope:invader_spawn_location
				father = scope:talakakin_ii_lamptuna
				save_scope_as = tifawt_lamptuna
			}
		}
		

		### Almoravid: add direct line to invader before they spawn themselves so the heritage line can work properly
		# GEN A

		# fallback for dynasty link
		if = {
			limit = { NOT = { exists = scope:tifawt_lamptuna } }
			if = {
				limit = { game_start_date >= 950.1.1 }
				character:273077 = {  save_scope_as = tifawt_lamptuna  }
			}
			else = {
				scope:dynasty_root = { save_scope_as = tifawt_lamptuna }
			}
		}

		create_character = { #todo at later start dates, this might be the only house member, since his father is dead -> failed house creation
			template = turgurt_almoravid_a_character_template
			dynasty = inherit
			location = scope:invader_spawn_location
			father = scope:tifawt_lamptuna
			save_scope_as = turgurt_almoravid_a
			after_creation = {
				create_cadet_branch = {}
				father = { death = natural }
			}
		}

		# GEN B

		create_character = {
			template = talagagin_almoravid_b_character_template
			dynasty = inherit
			location = scope:invader_spawn_location
			father = scope:turgurt_almoravid_a
			save_scope_as = talagagin_almoravid_b
		}

		# GEN C

		create_character = {
			template = umar_almoravid_c_character_template
			dynasty = inherit
			location = scope:invader_spawn_location
			father = scope:talagagin_almoravid_b
			save_scope_as = umar_almoravid_c
		}

		# GEN D

		create_character = { # INVADER
			template = yahya_almoravid_character_template
			dynasty = inherit
			location = scope:invader_spawn_location
			father = scope:umar_almoravid_c
			save_scope_as = invader
			after_creation = {
				add_character_flag = spawned_$INVADER_DYNN$
			}
		}
	}
	# existing invader
	else = {}

	scope:invader = {
		if = {
			limit = { game_start_date < 1050.1.1 }
			add_gold = 3500
			add_prestige = 2000
			add_piety = 350
		}
		else = {
			add_gold = 375
			add_prestige = 150
			add_piety = 250
		}
		
		dynasty = {
			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 1000
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			if = {
				limit = {
					NOT = { # prevent the main house from being changed
						this = scope:dynasty_root.house
					}
				}
				set_coa = title:e_almoravid
				set_house_name = hi_dynn_almoravid
			}
		}
	}

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

almoravid_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
	add_dynasty_perk = erudition_legacy_1
}

almoravid_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	if = {
		limit = { $INVADER_CHAR$ = { has_character_flag = spawned_$INVADER_DYNN$ } }
		give_invader_land_effect = {
			INVADER_DYNN = $INVADER_DYNN$
			INVADER_CHAR = $INVADER_CHAR$
			STARTING_TIER = $STARTING_TIER$
		}
	}
	if = {
		limit = { game_start_date < 1050.1.1 }
		spawn_$INVADER_DYNN$_family_effect = {
			INVADER_CHAR = $INVADER_CHAR$
		}
	}
	else = { $INVADER_CHAR$ = { remove_character_flag = is_first_invader_flag } }
	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$
	}
}

### populating the life of the invader character
spawn_almoravid_family_effect = {
	# GEN B

	create_character = {
		template = muhammad_almoravid_b_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father.father.father
		save_scope_as = muhammad_almoravid_b
	}

	create_character = {
		template = hamid_almoravid_b_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father.father.father
		save_scope_as = hamid_almoravid_b
	}

	# GEN C

	create_character = {
		template = tashfin_almoravid_c_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father.father
		save_scope_as = tashfin_almoravid_c
	}

	create_character = {
		template = ali_almoravid_c_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father.father
	}

	create_character = {
		template = al-hajj_almoravid_c_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:muhammad_almoravid_b
		save_scope_as = al-hajj_almoravid_c
	}

	create_character = {
		template = tilankan_almoravid_c_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:hamid_almoravid_b
		save_scope_as = tilankan_almoravid_c
	}

	# GEN D

	create_character = {
		template = yusuf_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:tashfin_almoravid_c
	}

	create_character = {
		template = ibrahim_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:tashfin_almoravid_c
	}

	create_character = {
		template = abu_bakr_tashfin_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:tashfin_almoravid_c
	}

	create_character = {
		template = abu_bakr_umar_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father
	}

	create_character = {
		template = ali_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:al-hajj_almoravid_c
	}

	create_character = {
		template = muhammad_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:al-hajj_almoravid_c
	}

	create_character = {
		template = mazdali_almoravid_d_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:tilankan_almoravid_c
	}
}

# Make sure invader has troops
spawn_almoravid_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_almoravid_army_effect = yes
	}
	if = {
		limit = { game_start_date < 1050.1.1 }
		spawn_almoravid_army_effect = yes
	}
	spawn_almoravid_army_effect = yes
}
spawn_almoravid_army_effect = {
	spawn_army = {
		name = almoravid_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = camel_rider
			stacks = 5
		}
		men_at_arms = {
			type = sahel_horsemen
			stacks = 9
		}
		men_at_arms = {
			type = light_footmen
			stacks = 5
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}