﻿# Story for the bassenheim Invasion
# originally made by Petter Vilberg
# adjusted by me

story_bassenheim_invasion = {
	on_setup = {
		historicinvasions_story_setup_effect = { # global start variable, story variables, conditional dissolution protection
			INVADER_DYNN = bassenheim # for story start global variable
			INVADER_TITLE = title:k_bassenheim # title the invader spawns with
		}
		scope:story.var:invader_title_var = { set_capital_county = title:c_naumburg } # troop spawn location, is reset on becoming landed / on story end
		story_owner = {
			apply_invader_modifiers_effect = {
				INVADER_DYNN = bassenheim
			}
			form_the_invader_title_effect = {
				INVADER_DYNN = bassenheim
				INVADER_CHAR = scope:story.story_owner
				INVADER_TITLE = scope:story.var:invader_title_var
				LANDLESS_YES_NO = yes
			}
			unlanded_war_target_evaluation_and_declaration_effect = {
				INVADER_DYNN = bassenheim
				INVADER_REGION = start
				EVERY_RANDOM = random # declare on just one ruler / title (random) or on a large area (every)?
				INVADER_TARGET_TITLE_TIER = duchy # currently only to choose target lieges
				#INVADER_TITLE_TARGETS = "target_title = title:d_prussia"
				INVADER_TITLE_TARGETS = "target_title = scope:random_start_invasion_target_title"
				INVADER_LIEGE_RESTRICTIONS = ""
			}
		}
		# notification events for both holy orders
		every_player = {
			limit = {
				has_religion = religion:christianity_religion
			}
			trigger_event = {
				id = bassenheim_invasion.1101
				days = { 1 7 }
			}
		}
		# de jure stuff
		scope:story.var:invader_title_var = { set_de_jure_liege_title = title:e_wendish_empire }
	}

	on_end = {
		invader_story_on_end_effect = { INVADER_DYNN = bassenheim }
		# clean up: capital county
		if = {
			limit = { scope:story.var:invader_title_var.title_capital_county = title:c_naumburg }
			scope:story.var:invader_title_var = { set_capital_county = title:c_malbork }
		}
		# clean up: landless-type title is reverted and title is destroyed if ruler is still landless
		if = {
			limit = { scope:story.var:invader_title_var = { is_landless_type_title = yes } }
			scope:story.var:invader_title_var = { set_landless_title = no }
			if = {
				limit = {
					scope:story.var:invader_title_var = { is_title_created = yes }
					scope:story.var:invader_title_var.holder = { is_landless_ruler = yes } # holds any title, but no on-map land
				}
				scope:story.var:invader_title_var.holder = { destroy_title = scope:story.var:invader_title_var }
			}
		}
		# secularization
		scope:story.var:invader_title_var = {
			if = {
				limit = {
					is_title_created = yes
					holder = {
						is_ai = yes
						title:k_papal_state.holder = faith.religious_head
						highest_held_title_tier = tier_kingdom
						has_government = feudal_government
					}
				}
				holder = {
					change_government = theocracy_government
					every_vassal_or_below = {
						limit = {
							is_ai = yes
							highest_held_title_tier >= tier_county
							has_government = feudal_government
							#culture = prev.culture
							faith = prev.faith
						}
						change_government = theocracy_government
					}
				}
			}
		}
	}

	on_owner_death = {
		# random chance to set variable to end story after ruler N dies, depending on how many story owners there have been in total
		random = {
			chance = 0
			modifier = {
				add = 0
				var:story_owner_counter >= 1 # end story after death of ruler N?
			}
			modifier = {
				add = 10
				var:story_owner_counter >= 2
			}
			modifier = {
				add = 90
				var:story_owner_counter >= 3
			}
			modifier = {
				add = 100
				scope:story.var:invader_title_var = { is_landless_type_title = yes }
				scope:story.var:invader_title_var.holder = {
					is_landless_ruler = yes # holds any title, but no on-map land
					is_at_war = no # perhaps the initial landess war is still ongoing
				}
			}
			set_variable = {
				name = end_of_invasion_story
				value = yes
			}
		}
		invader_story_on_death_effect = { INVADER_DYNN = bassenheim }
	}

	# misc maintenance
	effect_group = {
		days = historicinvasions_story_maintenance_value

		trigger = { always = yes }

		# make sure they have money on hand
		triggered_effect = {
			trigger = { story_owner = { gold < 0 } }
			effect = { story_owner = { add_gold = 350 } }
		}

		# end the story prematurely, because their conquests have been completed
		triggered_effect = {
			trigger = {
				story_owner = { completely_controls_region = special_bassenheim_conquest_region_total }
			}
			effect = {
				debug_log = "ending bassenheim story because the conquest goal has been completed"
				end_story = yes
			}
		}

		# end the story prematurely, because the invader has lost their #th offensive war
		triggered_effect = {
			trigger = {
				NOT = { has_game_rule = historicinvasions_invasion_ends_after_defeat_disabled }
			}
			effect = {
				historicinvasions_invasion_ends_after_defeat_effect = yes
			}
		}
	}

	#Start a new war if none is ongoing
	effect_group = {
		days = { 365 730 }

		trigger = { story_owner = { is_ai = yes } }

		first_valid = {
			triggered_effect = {
				trigger = {
					story_owner = { has_character_flag = invasion_ends_after_player_win }
				}
				effect = {
					debug_log = "ending bassenheim story because a player was able to defend against the invader"
					end_story = yes
				}
			}
			triggered_effect = {
				trigger = { story_owner = { should_invader_declare_war_trigger = { INVADER_TITLE = scope:story.var:invader_title_var } } }
				effect = {
					story_owner = {
						war_target_evaluation_and_declaration_effect = {
							INVADER_DYNN = bassenheim
							INVADER_CHAR = scope:story.story_owner
							INVADER_REGION = total
							INVADER_TARGET_TITLE_TIER = duchy
							INVADER_RANGE = neighboring
							INVADER_LIEGE_RESTRICTIONS = "NOT = { this = title:e_hre.holder }"
						}
					}
				}
			}
		}
	}

	# Story handling in case of succession weirdness
	effect_group = {
		days = historicinvasions_succession_weirdness_value

		trigger = { NOT = { story_owner = { has_title = scope:story.var:invader_title_var } } }

		# either transfer story ownership or end story if the title is not created
		triggered_effect = {
			trigger = { always = yes }
			effect = {
				hi_check_for_valid_story_owner_effect = {
					INVADER_TITLE = scope:story.var:invader_title_var
					STORY_COUNTER_INCREASE = 0
				}
			}
		}
	}

	# gradual de jure updates & notification event & family spawning
	effect_group = {
		days = 120
		trigger = {
			always = yes
		}
		triggered_effect = {
			trigger = {
				scope:story.var:invader_title_var = { is_landless_type_title = yes }
				scope:story.var:invader_title_var.holder = { is_landless_ruler = no }
			}
			effect = {
				scope:story.var:invader_title_var = { set_landless_title = no }
				if = { # clean up: capital county
					limit = { scope:story.var:invader_title_var.title_capital_county = title:c_naumburg }
					scope:story.var:invader_title_var = { set_capital_county = title:c_malbork }
				}
				story_owner = {
					spawn_bassenheim_family_effect = {
						INVADER_CHAR = scope:story.story_owner
					}
					spawn_invader_servants_effect = {
						INVADER_CHAR = scope:story.story_owner
					}
					add_legitimacy_effect = { LEGITIMACY = 1000 } # only works when landed
					save_scope_as = invader
				}
				scope:story.var:invader_title_var.title_capital_county = {
					add_county_modifier = {
						modifier = historicinvasions_county_tier_one_modifier
						years = 25
					}
				}
				every_player = {
					trigger_event = bassenheim_invasion.1001 # Notification: Start of invasion
				}
			}
		}
		triggered_effect = {
			trigger = {
				exists = scope:story.var:invader_title_var
			}
			effect = {
				historicinvasions_de_jure_change = {
					INVADER_DYNN = bassenheim
					INVADER_CHAR = scope:story.story_owner
					INVADER_TITLE = scope:story.var:invader_title_var
					INVADER_TARGET_TITLE_TIER = duchy
					DE_JURE_PERCENT = 0.7
				}
			}
		}
	}
}