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

	# father
	create_character = {
		template = tumart_almohad_character_template
		location = scope:invader_spawn_location
		save_scope_as = invader_father
	}
	# INVADER
	create_character = {
		template = amghar_almohad_character_template
		dynasty = inherit
		father = scope:invader_father
		location = scope:invader_spawn_location
		save_scope_as = invader
		after_creation = {
			add_character_flag = spawned_$INVADER_DYNN$
		}
	}
	scope:invader = {
		add_gold = 7500
		add_prestige = 1750
		add_piety = 2500
		
		dynasty = {
			set_dynasty_name = hi_dynn_tumart
			set_coa = title:e_almohad

			add_dynasty_prestige_level = 2
			add_dynasty_prestige = 4000
			
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_tumart
		}

		add_pressed_claim = title:k_maghreb
		if = {
			limit = { title:e_almoravid = { is_title_created = yes } }
			add_pressed_claim = title:e_almoravid
		}
	}

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

almohad_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
	add_dynasty_perk = erudition_legacy_1
	add_dynasty_perk = erudition_legacy_2
	add_dynasty_perk = erudition_legacy_3
}

almohad_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	if = {
		limit = { $INVADER_CHAR$ = { has_character_flag = spawned_$INVADER_DYNN$ } }
		give_$INVADER_DYNN$_land_effect = {
			INVADER_DYNN = $INVADER_DYNN$
			INVADER_CHAR = $INVADER_CHAR$
			#STARTING_TIER = $STARTING_TIER$
		}
	}
	if = {
		limit = { game_start_date < 1121.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_DYNN$_title_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		INVADER_TITLE = $INVADER_TITLE$
		LANDLESS_YES_NO = $LANDLESS_YES_NO$
	}
}

# Give a suitable land to invader
give_almohad_land_effect = {
	set_local_variable = {
		name = almohad_max_start_counties_var
		value = 0
	}
	while = {
		limit = {
			local_var:almohad_max_start_counties_var < 7
		}
		random_county_in_region = {
			region = special_$INVADER_DYNN$_conquest_region_1 # maghreb
			limit = {
				holder = { is_ai = yes }
				NOR = {
					this = title:c_marrakesh
					is_in_list = almohad_de_facto_starting_counties_list
				}
			}
			county = { add_to_temporary_list = almohad_de_facto_starting_counties_list }
		}
		change_local_variable = {
			name = almohad_max_start_counties_var
			add = 1
		}
	}
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	every_in_list = {
		list = almohad_de_facto_starting_counties_list
		change_title_holder = {
			holder = $INVADER_CHAR$
			change = scope:title_change
		}
		set_county_faith = $INVADER_CHAR$.faith
	}
	resolve_title_and_vassal_change = scope:title_change
}

### populating the life of the invader character
spawn_almohad_family_effect = {
	create_character = {
		template = ali_al-kumi_almohad_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = $INVADER_CHAR$.father # janky and not true; so the story can be passed on correctly
		save_scope_as = ali_al-kumi_almohad
	}

	create_character = {
		age = { 24 29 }
		template = hi_random_spouse_character_template
		employer = $INVADER_CHAR$
		save_scope_as = abd_al-mumin_spouse # marriage is down below
		after_creation = {
			disease_immunity_short_effect = yes
			if = {
				limit = {
					NOT = { has_trait = lustful }
					number_of_personality_traits < 4
				}
				add_trait = lustful
			}
		}
	}

	# successor of INVADER
	create_character = {
		template = abd_al-mumin_almohad_character_template
		dynasty = inherit
		employer = $INVADER_CHAR$
		father = scope:ali_al-kumi_almohad
		save_scope_as = abd_al-mumin_almohad
	}
	scope:abd_al-mumin_almohad = {
		disease_immunity_long_effect = yes
		marry = scope:abd_al-mumin_spouse
		add_character_modifier = historicinvasions_plot_armor
		add_character_flag = recent_nation_fracturing_faction_war # makes the AI unlikely to start dissolution factions
		add_character_flag = recent_independence_faction_war # makes the AI unlikely to start independence factions
		if = { # make invader immune to viking incursions
			limit = { # trigger of fp1_scandinavian_adventurers.0001
				has_fp1_dlc_trigger = yes
				NOT = { has_game_rule = fp1_scandi_adventurers_off }
				OR = {
					culture:norse = {
						NOT = { has_cultural_era_or_later = culture_era_early_medieval }
					}
					culture:norwegian = {
						NOT = { has_cultural_era_or_later = culture_era_early_medieval }
					}
					culture:danish = {
						NOT = { has_cultural_era_or_later = culture_era_early_medieval }
					}
					culture:swedish = {
						NOT = { has_cultural_era_or_later = culture_era_early_medieval }
					}
				}
			}
			add_character_modifier = fp1_reprieve_from_sa_modifier
		}
		house = {
			set_house_name = hi_dynn_almohad
			set_coa = title:e_almohad
		}
	}

	### historic succession
	$INVADER_CHAR$ = { set_designated_heir = scope:abd_al-mumin_almohad }
}

# Form and give titles
form_the_almohad_title_effect = {
	if = { # amghar spawns as a vassal
		limit = {
			title:e_maghreb = { is_title_created = yes }
			NOT = { exists = global_var:almohad_coup }
		}

		create_dynamic_title = {
			tier = kingdom
			name = $INVADER_DYNN$_COUP_TITLE
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change_one
		}
		scope:new_title = {
			change_title_holder = {
				holder = $INVADER_CHAR$
				change = scope:change_one
			}
			set_delete_on_destroy = yes
			set_no_automatic_claims = yes
			set_can_be_named_after_dynasty = no
			set_destroy_on_gain_same_tier = yes
		}
		resolve_title_and_vassal_change = scope:change_one
		scope:new_title = { generate_coa = yes }

		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change_two
		}
		$INVADER_CHAR$ = {
			change_liege = {
				liege = title:e_maghreb.holder
				change = scope:change_two
			}
		}
		resolve_title_and_vassal_change = scope:change_two

		spawn_army = {
			uses_supply = no
			inheritable = yes
			name = almoravid_event_troops
			levies = {
				value = 2400
			}
			men_at_arms = {
				type = camel_rider
				stacks = 9
			}
			men_at_arms = {
				type = camel_rider
				stacks = 9
			}
			men_at_arms = {
				type = sahel_horsemen
				stacks = 9
			}
			men_at_arms = {
				type = sahel_horsemen
				stacks = 9
			}
			men_at_arms = {
				type = light_footmen
				stacks = 7
			}
			men_at_arms = {
				type = mangonel
				stacks = 9
			}
			location = $INVADER_CHAR$.capital_province
		}

		set_global_variable = { # stops story effect "succession weirdness" from triggering and ending the story, clears automatically as fallback
			name = $INVADER_DYNN$_coup
			value = yes
			days = 1825 # 5 years
		}
	}
	else_if = { # amghar spawns as a vassal, v1.35.2 compatibility with old de jure titles
		limit = {
			title:e_almoravid = { is_title_created = yes }
			NOT = { exists = global_var:almohad_coup }
		}

		create_dynamic_title = {
			tier = kingdom
			name = $INVADER_DYNN$_COUP_TITLE
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change_one
		}
		scope:new_title = {
			change_title_holder = {
				holder = $INVADER_CHAR$
				change = scope:change_one
			}
			set_delete_on_destroy = yes
			set_no_automatic_claims = yes
			set_can_be_named_after_dynasty = no
			set_destroy_on_gain_same_tier = yes
		}
		resolve_title_and_vassal_change = scope:change_one
		scope:new_title = { generate_coa = yes }

		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change_two
		}
		$INVADER_CHAR$ = {
			change_liege = {
				liege = title:e_almoravid.holder
				change = scope:change_two
			}
		}
		resolve_title_and_vassal_change = scope:change_two

		spawn_army = {
			uses_supply = no
			inheritable = yes
			name = almoravid_event_troops
			levies = {
				value = 2400
			}
			men_at_arms = {
				type = camel_rider
				stacks = 9
			}
			men_at_arms = {
				type = camel_rider
				stacks = 9
			}
			men_at_arms = {
				type = sahel_horsemen
				stacks = 9
			}
			men_at_arms = {
				type = sahel_horsemen
				stacks = 9
			}
			men_at_arms = {
				type = light_footmen
				stacks = 7
			}
			men_at_arms = {
				type = mangonel
				stacks = 9
			}
			location = $INVADER_CHAR$.capital_province
		}

		set_global_variable = { # stops story effect "succession weirdness" from triggering and ending the story, clears automatically as fallback
			name = $INVADER_DYNN$_coup
			value = yes
			days = 1825 # 5 years
		}
	}
	else = { # amghar spawns independent
		# Create the title
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:d_masmudi = {
			change_title_holder = {
				holder = $INVADER_CHAR$
				change = scope:title_change
			}
		}
		$INVADER_TITLE$ = {
			change_title_holder = {
				holder = $INVADER_CHAR$
				change = scope:title_change
			}
			set_landless_title = $LANDLESS_YES_NO$
			copy_title_history = title:e_almoravid
		}
		becomes_independent = { change = scope:title_change }
		resolve_title_and_vassal_change = scope:title_change

		assert_if = {
			limit = { NOT = { exists = $INVADER_TITLE$ } }
			text = "almohad Empire title was not created!"
		}

		set_primary_title_to = $INVADER_TITLE$

		add_dread = high_dread

		add_to_global_variable_list = {
			name = historicinvasions_titles
			target = flag:almohad_title_spawned
		}

		if = {
			limit = { exists = global_var:almohad_coup }
			remove_global_variable = almohad_coup
		}
	}

	apply_invader_government_type_effect = { INVADER_CHAR = $INVADER_CHAR$ }

	if = {
		limit = {
			NOT = { exists = global_var:almohad_reinforcements }
		}
		set_global_variable = {
			name = almohad_reinforcements
			value = yes
			days = 3650 # 10 years
		}
		trigger_event = { # delayed to prevent error.log entry
			id = $INVADER_DYNN$_invasion.0100 # spawn troops
			days = 1
		}
	}	
}

# Make sure invader has troops
spawn_almohad_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_almohad_army_effect = yes
	}
	spawn_almohad_army_effect = yes
	spawn_almohad_army_effect = yes
}
spawn_almohad_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 = 7
		}
		men_at_arms = {
			type = sahel_horsemen
			stacks = 9
		}
		men_at_arms = {
			type = light_footmen
			stacks = 5
		}
		men_at_arms = {
			type = bowmen
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 7
		}
	}
}

almohad_create_artifacts_effect = {
	create_artifact_almohad_book_effect = { OWNER = this } # character artifact
}