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

	# father
	character:189032 = { save_scope_as = atitta }

	if = {
		limit = { NOT = { exists = scope:invader } }
		# INVADER
		create_character = {
			template = parantaka_chola_character_template
			dynasty = inherit
			father = scope:atitta
			location = scope:invader_spawn_location
			save_scope_as = invader
			after_creation = {
				add_character_flag = spawned_$INVADER_DYNN$
			}
		}
	}

	scope:invader = {
		add_gold = 2000
		add_prestige = 2000
		add_piety = 150
		
		dynasty = {
			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 500
		
			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
	}

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

chola_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
}

chola_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 < 920.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_chola_family_effect = {
	# Family
	create_character = {
		template = kantaratitta_chola_character_template
		dynasty = inherit
		father = $INVADER_CHAR$
		employer = $INVADER_CHAR$
		save_scope_as = kantaratitta_chola
	}
	create_character = {
		template = uttama_chola_character_template
		dynasty = inherit
		father = scope:kantaratitta_chola
		employer = $INVADER_CHAR$
	}
	create_character = {
		template = arincaya_chola_character_template
		dynasty = inherit
		father = $INVADER_CHAR$
		employer = $INVADER_CHAR$
		save_scope_as = arincaya_chola
	}
	create_character = {
		template = cuntara_chola_character_template
		dynasty = inherit
		father = scope:arincaya_chola
		employer = $INVADER_CHAR$
	}
}

# Make sure invader has troops
spawn_chola_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_chola_army_effect = yes
	}
	spawn_chola_army_effect = yes
}
spawn_chola_army_effect = {
	spawn_army = {
		name = chola_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = war_elephant
			stacks = 3
		}
		men_at_arms = {
			type = armored_footmen
			stacks = 3
		}
		men_at_arms = {
			type = pikemen_unit
			stacks = 3
		}
		men_at_arms = {
			type = paiks
			stacks = 9
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}

create_chola_successor_kingdoms_effect = {
	# Tamilakam
	if = {
		limit = {
			title:k_tamilakam = { is_title_created = no }
			any_county_in_region = {
				region = special_chola_conquest_region_1
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_tamilakam = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	# Andhra
	if = {
		limit = {
			title:k_andhra = { is_title_created = no }
			any_county_in_region = {
				region = special_chola_conquest_region_2
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_andhra = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	# Lanka
	if = {
		limit = {
			title:k_lanka = { is_title_created = no }
			any_county_in_region = {
				region = special_chola_conquest_region_3
				percent >= 0.4
				OR = {
					holder = scope:story.story_owner
					holder = { target_is_liege_or_above = scope:story.story_owner }
				}
			}
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_lanka = {
			change_title_holder = {
				holder = scope:story.story_owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
}

chola_conquer_kedah_addon_effect = {} # placeholder for addon