﻿# make Timur loot occupied provinces; copied from the Mongol section by Paradox

# called when a county is (fully) occupied. Does not fire on reoccupation by province owner
# root is the occupant
# scope:county is the county title
# scope:barony is the barony title that got occupied
# scope:previous_controller is the previous controller of the county
# scope:war is the war causing the previous controller and the occupant to be hostile

# if you write directly into an existing on_action, it doesn't properly combine triggers or effects
# solution: use the desired on_action to trigger your own on_action
# looks ugly but doesn't break
on_county_occupied = {
	on_actions = {
		historicinvasions_on_county_occupied
		qarmatians_on_county_occupied
	}
}

historicinvasions_on_county_occupied = {
	trigger = {
		# story is still ongoing
		has_character_modifier = historicinvasions_plot_armor
		# occupant has the relevant title
		OR = {
			has_title = title:e_timurid
			has_title = title:e_mongol_empire # base game checks for: any_owned_story = { story_type = story_mongol_invasion }
		}
		# county has not been looted previously
		NOT = { scope:county = { has_variable = county_looted_by_invader } }
		# error.log suppression if development is too low
		scope:county = { development_level > 0 }
	}
	effect = {
		save_scope_as = occupant
		# can only happen once
		scope:county = {
			set_variable = {
				name = county_looted_by_invader
				value = yes
			}
		}
		# lower development, send gold
		invader_loots_county_effect = yes
		### messages
		# notify occupant
		send_interface_message = {
			type = invader_occupation_good
			title = invader_looted_county.t
			left_icon = scope:previous_controller
			right_icon = scope:county
			show_as_tooltip = {
				scope:county = { invader_loots_county_effect = yes }
			}
		}
		# notify county owner
		scope:county.holder = {
			send_interface_message = {
				type = invader_occupation_bad
				title = invader_looted_county.t
				left_icon = root
				right_icon = scope:county
				show_as_tooltip = {
					scope:county = { invader_loots_county_effect = yes }
				}
			}
		}
		# notify liege (if different character)
		if = {
			limit = { NOT = { scope:county.holder = scope:county.holder.top_liege } }
			scope:county.holder.top_liege = {
				send_interface_message = {
					type = invader_occupation_bad
					title = invader_looted_county.t
					left_icon = root
					right_icon = scope:county
					show_as_tooltip = {
						scope:county = { invader_loots_county_effect = yes }
					}
				}
			}
		}
	}
}

qarmatians_on_county_occupied = {
	trigger = {
		# is it Mecca?
		scope:county = { this = title:c_mecca }
		# occupant is of the relevant faith
		faith = faith:qarmatian
		# county has not been looted previously
		NOT = { scope:county = { has_variable = county_looted_by_invader } }
		# error.log suppression if development is too low
		scope:county = { development_level > 0 }
	}
	effect = {
		debug_log = "Mecca was sacked by the Qarmatians"
		debug_log_date = yes
		
		save_scope_as = occupant
		# can only happen once
		scope:county = {
			set_variable = {
				name = county_looted_by_invader
				value = yes
			}
		}
		# lower development, send gold
		invader_loots_county_effect = yes
		add_character_modifier = qarmatian_looted_mecca_modifier
		### messages
		# notify occupant
		send_interface_message = {
			type = invader_occupation_good
			title = invader_looted_county.t
			left_icon = scope:previous_controller
			right_icon = scope:county
			show_as_tooltip = {
				scope:county = { invader_loots_county_effect = yes }
			}
		}
		# notify county owner
		scope:county.holder = {
			send_interface_message = {
				type = invader_occupation_bad
				title = invader_looted_county.t
				left_icon = root
				right_icon = scope:county
				show_as_tooltip = {
					scope:county = { invader_loots_county_effect = yes }
				}
			}
		}
		# notify liege (if different character)
		if = {
			limit = { NOT = { scope:county.holder = scope:county.holder.top_liege } }
			scope:county.holder.top_liege = {
				send_interface_message = {
					type = invader_occupation_bad
					title = invader_looted_county.t
					left_icon = root
					right_icon = scope:county
					show_as_tooltip = {
						scope:county = { invader_loots_county_effect = yes }
					}
				}
			}
		}
		### notification event
		every_player = {
			trigger_event = {
				id = jannabid_invasion.1201
				days = 1
			}
		}
	}
}