﻿namespace = raiding

##################################################
#Raiding CB events

	#0001-0002	- You have the chance to sack a holding.
	#0003		- Your holding may have been sacked (liege or vassal).
	#0004		- Your vassal's holding may have been sacked.
	#0011		- Raiding notification event, received upon delivering army loot.
	#0012		- Raiding notification event, telling involved parties how much loot a defending army stole.
	#0021		- Increase raid tally.
	#0022		- Chance to fire viking/raider trait acquisition event.
	#0023		- Acquire viking/raider trait.

##################################################

##################################################
# Fire and Blood
# by Ewan Cowhig Croft
# #0001-0004
##################################################

#	You have the chance to sack a holding.
raiding.0001 = {	#Hidden event: do I get a sacking?
	hidden = yes
	scope = army

	trigger = {
		#You must be leading your own forces.
		scope:raider = { is_at_location = scope:barony.title_province }
		scope:barony.title_province = { fort_level > 0 }
		#The county must not have recently been sacked.
		NOR = {
			scope:county = { has_county_modifier = recently_sacked_modifier }
			scope:county.title_province = { has_holding_type = herder_holding }
		}
	}

	immediate = {
		save_scope_as = raid_army
		random = {
			chance = 40
			modifier = {
				add = 40
				scope:raider = { has_trait = viking }
			}
			modifier = {
				add = 60
				scope:raid_army = { raid_intent = raze_intent }
			}
			scope:raider = {
				# Grab some loc things.
				scope:county = { save_scope_as = sacked_county }
				scope:barony = { save_scope_as = sacked_barony }
				scope:raider.capital_province = { save_scope_as = raider_capital }
				trigger_event = raiding.0002
			}
		}
	}
}

raiding.0002 = {	#Less hidden event: I do get a sacking!
	type = character_event
	title = raiding.0002.t
	desc = raiding.0002.desc
	theme = raid
	override_background = { reference = burning_building }
	left_portrait = {
		character = root
		animation = menacing
	}
	right_portrait = {
		character = scope:sacked_county_owner
		animation = fear
		triggered_animation = {
			trigger = {
				ai_vengefulness >= medium_positive_ai_value
				ai_boldness > high_negative_ai_value
			}
			animation = rage
		}
	}

	trigger = {
		NOT = { has_character_flag = had_event_raiding_0002 }
	}

	immediate = {
		add_character_flag = {
			flag = had_event_raiding_0002
			days = 365
		}
		#For loc.
		scope:sacked_county.holder = { save_scope_as = sacked_county_owner }
	}

	#Bring me slaves and scrolls!
	option = {
		name = raiding.0002.a
		trigger = {
			NOT = { government_has_flag = government_is_nomadic }
			root.capital_county = { development_level < 15 }	#If you have a certain amount of development, you can't gain anything from this.
			OR = {
				scope:county = { development_level > 15 }	#The county must actually have enough development to be worth sacking.
				scope:county.holder = {	#Or at least be a non-tribal place.
					NOR = {
						government_has_flag = government_is_tribal
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_herder
					}
				}
			}
		}
		scope:sacked_county = {
			add_county_modifier = {	#Prevent the county from being sacked again, by anyone, for quite a while.
				modifier = recently_sacked_modifier
				years = 20
			}
			change_county_control = -10
			change_development_progress = major_development_progress_loss	#Always deduct progress.
			if = {	#If they don't have enough progress made, burn a level of development.
				limit = { development_towards_level_increase < major_development_progress_gain }
				change_development_level = -1
			}
			save_scope_value_as = {
				name = sack_status
				value = flag:admin_plundered
			}
			holder = {
				trigger_event = raiding.0003
				if = {
					limit = { is_independent_ruler = no }
					liege = { trigger_event = raiding.0004 }
				}
			}
		}
		root.capital_county = {
			change_development_progress = medium_development_progress_gain
		}
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_greed = 0.5
				ai_compassion = -0.5
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = compassionate
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = forgiving
			}
		}
	}
	
	option = {
		name = raiding.0002.a_nomad
		trigger = {
			government_has_flag = government_is_nomadic
		}
		scope:sacked_county = {
			add_county_modifier = {	#Prevent the county from being sacked again, by anyone, for quite a while.
				modifier = recently_sacked_modifier
				years = 20
			}
			change_county_control = -10
			change_development_progress = major_development_progress_loss	#Always deduct progress.
			if = {	#If they don't have enough progress made, burn a level of development.
				limit = { development_towards_level_increase < major_development_progress_gain }
				change_development_level = -1
			}
			save_scope_value_as = {
				name = sack_status
				value = flag:admin_plundered
			}
			holder = {
				trigger_event = raiding.0003
				if = {
					limit = { is_independent_ruler = no }
					liege = { trigger_event = raiding.0004 }
				}
			}
		}
		root.capital_county = {
			add_county_modifier = {
				modifier = stolen_ewes_and_mares_modifier
				years = 10
			}
		}
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_greed = 0.5
				ai_compassion = -0.5
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = compassionate
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = forgiving
			}
		}
	}

	#Bring me bounteous plunder!
	option = {
		name = raiding.0002.b
		scope:sacked_county = {
			add_county_modifier = {	#Prevent the county from being sacked again for quite a while.
				modifier = recently_sacked_modifier
				years = 20
			}
			if = {	#If they have enough progress gain to take the hit, then they just lose some progress.
				limit = {
					development_towards_level_increase > medium_development_progress_loss
				}
				change_development_progress = medium_development_progress_loss
			}
			else = {	#If they don't, then they lose the progress, and a level of development is burned away.
				change_development_progress = medium_development_progress_loss
				change_development_level = -1
			}
			save_scope_value_as = {
				name = sack_status
				value = flag:gold_plundered
			}
			holder = {
				if = {
					limit = { is_independent_ruler = yes }
					trigger_event = raiding.0003
				}
				else = {
					trigger_event = raiding.0003
					liege = { trigger_event = raiding.0004 }
				}
			}
		}
		scope:raid_army = { add_loot = scope:sacked_barony.county.title_province.raid_gold_value }
		add_prestige = minor_prestige_value
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_greed = 0.75
				ai_compassion = -0.25	#It's more ethical to steal stuff than people, right?
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = compassionate
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = forgiving
			}
		}
	}
	#I want no part of these shenanigans.
	option = {
		name = raiding.0002.c
		scope:sacked_county = {
			#Opinion gain if not primary defender.
			save_scope_value_as = {
				name = sack_status
				value = flag:spared_plundered
			}
			holder = {
				if = {
					limit = { is_independent_ruler = yes }
					trigger_event = raiding.0003
				}
				else = {
					trigger_event = raiding.0003
					liege = { trigger_event = raiding.0004 }
				}
			}
		}
		if = {
			limit = {
				faith = {
					OR = {
						trait_is_sin = compassionate
						trait_is_sin = forgiving
						trait_is_sin = calm
					}
				}
			}
			add_piety = minor_piety_loss
		}
		else = { add_piety = medium_piety_gain }
		stress_impact = {
			sadistic = minor_stress_impact_gain
			vengeful = minor_stress_impact_gain
			wrathful = minor_stress_impact_gain
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_honor = 0.25
				ai_greed = -0.25
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = sadistic
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = vengeful
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = wrathful
			}
		}
	}
}

# Your holding may have been sacked (liege or vassal).
raiding.0003 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:sack_status = flag:spared_plundered }
				desc = raiding.0003.t_spared
			}
			desc = raiding.0003.t_attacked
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {	#If the holding was raided for skilled slaves, let us know of such.
				trigger = {
					scope:sack_status = flag:admin_plundered
					is_independent_ruler = yes
				}
				desc = raiding.0003.desc.admin_plundered_independent
			}
			triggered_desc = {	#If the holding was raided for gold, then tell us our shinies are gone.
				trigger = {
					scope:sack_status = flag:gold_plundered
					is_independent_ruler = yes
				}
				desc = raiding.0003.desc.gold_plundered_independent
			}
			triggered_desc = {	#If the holding was spared, we have a lukewarm one response.
				trigger = {
					scope:sack_status = flag:spared_plundered
					is_independent_ruler = yes
				}
				desc = 	raiding.0003.desc.spared_plundered_independent
			}
			triggered_desc = {
				trigger = { scope:sack_status = flag:admin_plundered }
				desc = raiding.0003.desc.admin_plundered_vassal
			}
			triggered_desc = {
				trigger = { scope:sack_status = flag:gold_plundered }
				desc = raiding.0003.desc.gold_plundered_vassal
			}
			desc = raiding.0003.desc.spared_plundered_vassal
		}
	}
	theme = raid
	override_background = {
		reference = burning_building
	}
	left_portrait = {
		character = scope:raider
		animation = war_over_win
	}

	#Oh no!
	option = {
		name = raiding.0003.a
		add_prestige = minor_prestige_loss
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_energy = -0.25	#I am too lazy to do much more than lament.
				ai_vengefulness = -0.25	#I will not stand for this.
			}
		}
	}

	#I will avenge this slight!
	option = {
		name = raiding.0003.b
		trigger = {
			NOT = { scope:sack_status = flag:spared_plundered }
		}
		if = {
			limit = { has_relation_rival = scope:raider }
			add_prestige = miniscule_prestige_gain
		}
		else = {
			progress_towards_rival_effect = {
				REASON = rival_raided_me
				CHARACTER = scope:raider
				OPINION = 0
			}
		}
		stress_impact = { forgiving = minor_stress_impact_gain }
		ai_chance = {
			base = 0	#Otherwise every raiding culture will have a million rivals.
			ai_value_modifier = {	#Vengeful characters may do this, though.
				ai_vengefulness = 0.2
			}
			modifier = {	#Angry characters can also struggle to get over these things.
				add = 10
				has_trait = wrathful
			}
			modifier = {	#Vengeful characters are, by definition, not going to let things go.
				add = 10
				has_trait = vengeful
			}
			modifier = {	#Dead peasants happen, y'know?
				add = -20
				has_trait = forgiving
			}
		}
	}

	#I will pay for the reconstruction.
	option = {
		name = raiding.0003.c
		trigger = {
			is_independent_ruler = yes
			NOT = { scope:sack_status = flag:spared_plundered }
			NOT = { government_has_flag = government_is_tribal }	#Tribes don't care about development.
			NOT = { government_has_flag = government_is_nomadic }
			OR = {
				is_ai = no
				short_term_gold >= scope:sacked_barony.county.title_province.raid_gold_value
			}
		}
		remove_short_term_gold = scope:sacked_barony.county.title_province.raid_gold_value
		scope:sacked_county = {
			add_county_modifier = {
				modifier = rebuilding_after_sacking_modifier
				years = 10
			}
		}
		stress_impact = { lazy = minor_stress_impact_gain }
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_greed = -0.5
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = lazy
			}
		}
	}
}

# Your vassal's holding may have been sacked.
raiding.0004 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:sack_status = flag:spared_plundered }
				desc = raiding.0003.t_spared
			}
			desc = raiding.0003.t_attacked
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {	#If the holding was raided for skilled slaves, let us know of such.
				trigger = {	scope:sack_status = flag:admin_plundered }
				desc = raiding.0004.desc.admin_plundered
			}
			triggered_desc = {	#If the holding was raided for gold, then tell us our vassal's shinies are gone.
				trigger = {	scope:sack_status = flag:gold_plundered }
				desc = raiding.0004.desc.gold_plundered
			}
			desc = 	raiding.0004.desc.spared_plundered #If the holding was spared, we have a lukewarm one response.
		}
	}
	theme = raid
	override_background = {
		reference = burning_building
	}
	left_portrait = {
		character = scope:raider
		animation = anger
	}
	right_portrait = {
		character = scope:sacked_county_owner
		animation = fear
		triggered_animation = {
			trigger = {
				ai_vengefulness >= medium_positive_ai_value
				ai_boldness > high_negative_ai_value
			}
			animation = rage
		}
	}	

	#Oh no!
	option = {	
		name = {
			text = raiding.0004.a
			trigger = {
				NOT = { government_has_flag = government_is_tribal }
			}
		}
		name = {
			text = raiding.0004.a.tribal
			trigger = { government_has_flag = government_is_tribal }
		}
		add_prestige = miniscule_prestige_loss
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_energy = -0.25	#Too lazy to care.
				ai_compassion = -0.25	#But wouldn't care anyway.
			}
		}
	}

	#Send some cash-monies.
	option = {	
		name = raiding.0004.b
		trigger = {
			NOT = { scope:sack_status = flag:spared_plundered }
			OR = {
				is_ai = no
				short_term_gold >= medium_gold_value
			}
		}
		pay_short_term_gold = {
			target = scope:sacked_county.holder
			gold = scope:sacked_barony.county.title_province.raid_gold_value
		}
		scope:sacked_county.holder = {
			add_opinion = {
				target = liege
				modifier = grateful_opinion
				opinion = 20
			}
		}
		stress_impact = { greedy = medium_stress_impact_gain }
		ai_chance = {
			base = 20	#Reduced base chance: the AI shouldn't bankrupt itself helping vassals.
			ai_value_modifier = {
				ai_compassion = 0.25
				ai_greed = -0.25
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = greedy
			}
		}
	}

	#I will pay for the reconstruction.
	option = {
		name = raiding.0004.c
		trigger = {
			NOR = {
				scope:sack_status = flag:spared_plundered
				government_has_flag = government_is_tribal	#Tribes don't care about development.
				government_has_flag = government_is_nomadic
			}
			OR = {
				is_ai = no
				short_term_gold >= scope:sacked_barony.county.title_province.raid_gold_value
			}
		}
		remove_short_term_gold = scope:sacked_barony.county.title_province.raid_gold_value
		scope:sacked_county.holder = {
			add_opinion = {
				target = liege
				modifier = grateful_opinion
				opinion = 30
			}
		}
		scope:sacked_county = {
			add_county_modifier = {
				modifier = rebuilding_after_sacking_modifier
				years = 10
			}
		}
		if = {
			limit = {
				employs_court_position = royal_architect_court_position
				any_court_position_holder = {
					type = royal_architect_court_position
				}
			}
			random_court_position_holder = {
				type = royal_architect_court_position
				save_scope_as = architect
			}
			custom_tooltip = raiding.0004.c.architect
			scope:sacked_county.holder = {
				send_interface_message = {
					type = event_raid_good
					title = raiding.0004.c.feed.t
					desc = raiding.0004.c.feed.desc2
					right_icon = liege
				}
			}
		}
		else = {
			scope:sacked_county.holder = {
				send_interface_message = {
					type = event_raid_bad
					title = raiding.0004.c.feed.t
					desc = raiding.0004.c.feed.desc
					right_icon = liege
				}
			}
		}
		stress_impact = { greedy = medium_stress_impact_gain }
		ai_chance = {
			base = 10	#Reduced base chance: the AI shouldn't bankrupt itself helping vassals.
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_greed = -0.5
			}
			modifier = {	#Weight down for stress.
				add = -20
				has_trait = greedy
			}
		}
	}
}

##################################################
# Raiding Notification Events
# by Ewan Cowhig Croft
# #0011-0012
##################################################

### DEPRECATED ###
# Now the interface message is sent out by raid intents instead
raiding.0011 = {
	hidden = yes
	orphan = yes
	scope = army

	immediate = {
		set_variable = {
			name = raid_loot
			value = root.raid_loot
		}
		if = {
			limit = {
				scope:raider = {
					not_tribal_raid_trigger = yes
				}
			}
			scope:raider = {
				send_interface_message = {
					type = event_raid_good
					title = raiding.0011.t
					desc = raiding.0011.desc_non_tribal
					right_icon = root.army_commander
					hidden_effect = { 
						add_prestige = {
							value = root.var:raid_loot
							multiply = not_tribal_raid_prestige_multiplier
							multiply = -1
						}
					}
				}
			}
		}
		else = {
			scope:raider = {
				send_interface_message = {
					type = event_raid_good
					title = raiding.0011.t
					desc = raiding.0011.desc
					right_icon = root.army_commander
					hidden_effect = { add_prestige = root.var:raid_loot }
				}
			}
		}
	}
}

# Loot lost to defending army.
raiding.0012 = {
	hidden = yes
	scope = army

	immediate = {
		set_variable = {
			name = raid_loot
			value = root.raid_loot
		}
		set_variable = {
			name = raid_prestige
			value = {
				value = root.raid_loot
				multiply = 0.5
				ceiling = yes
			}
		}
		if = {
			limit = {
				OR = {
					var:raid_loot >= 1
					var:raid_prestige >= 1
				}
			}
			scope:raider = {
				send_interface_message = {
					type = event_raid_bad_text
					title = raiding.0012.a.t
					desc = raiding.0012.a.desc
					left_icon = root.army_commander
					right_icon = scope:recipient
				}
			}
			scope:recipient = {
				send_interface_message = {
					type = event_raid_good_text
					title = raiding.0012.b.t
					desc = raiding.0012.b.desc
					right_icon = root.army_commander
					hidden_effect = { add_prestige = root.var:raid_prestige }
				}
			}
		}
	}
}


# Prisoner-taking script beyond this point. Much of this is transcribed from work done by Sean Hughes & Mathilda Bjarnehed for siege events.
scripted_trigger raiding_0012_can_be_captured = {
	# Captured characters must be located in the raided barony itself.
	location = scope:barony.title_province

	# Reasons to exclude characters from the capture pool:
	NOR = {
		# If the raiders have defeated the holder's army, but the army has not yet retreated to a different province, shield them from capture by the raid.
		exists = commanding_army
		exists = knight_army

		# Is imprisoned in the barony stay prisoners (special case, any courtiers/vassals of the raider will get released elsewhere).
		#is_imprisoned = yes
	}
}

# Chance to kidnap prisoners from the holding holder.
raiding.0013 = {
	hidden = yes
	scope = army

	trigger = { # Only take prisoners if this barony is the holder's capital (where their family/courtiers live), or there is an estate
		OR = {
			scope:barony.holder.capital_barony = scope:barony
			location = {
				any_province_domicile = {
					is_domicile_type = estate
				}
			}
		}
	}

	immediate = {
		# For localization.
		scope:raider = { save_scope_as = occupant }
		scope:barony.holder = { save_scope_as = holder }
		location = {
			random_province_domicile = {
				limit = {
					is_domicile_type = estate
				}
				owner ?= { save_scope_as = estate_holder }
			}
		}

		#BUILD CAPTURE ATTEMPT LIST
		scope:barony.holder = {
			if = {
				limit = { raiding_0012_can_be_captured = yes }
				add_to_list = capture_attempt_targets
			}
			every_courtier_or_guest = {
				limit = { raiding_0012_can_be_captured = yes }
				add_to_list = capture_attempt_targets
			}
		}
		scope:estate_holder ?= {
			if = {
				limit = {
					raiding_0012_can_be_captured = yes
					is_landed = no # they are at the estate
				}
				add_to_list = capture_attempt_targets
			}
			every_courtier_or_guest = {
				limit = {
					raiding_0012_can_be_captured = yes
					location = scope:barony.title_province
				}
				add_to_list = capture_attempt_targets
			}
		}

		#CALCULATE CAPTURE CHANCE
		every_in_list = {
			list = capture_attempt_targets
			random = {	
				# Base chance handled in a modifier due to the chance parameter tending to quietly fail to parse scripted values.
				chance = 0

				# Base chance starts at 20%, before value modifiers & target modifiers.
				modifier = { add = raid_base_capture_chance }

				# Add -0.5 chance per 1pt of Intrigue, averaging out at -5% chance for 10pts of Intrigue.
				modifier = {
					add = {
						value = this.intrigue
						multiply = -0.5
						min = -10
					}
					# Requiring a certain base level to be at all effective.
					intrigue > low_skill_rating
				}
				# Add -0.5 chance per 1pt of Prowess, averaging out at -5% chance for 10pts of Prowess.
				modifier = {
					add = {
						value = this.prowess
						multiply = -0.5
						min = -10
					}
					# Requiring a certain base level to be at all effective.
					prowess > low_skill_rating
				}
				# Less likely to capture the ruler themselves in a raid
				modifier = {
					is_landed = yes
					add = -10
				}
				# Less likely to capture the estate owner themselves
				modifier = {
					scope:estate_holder ?= this
					add = -10
				}
				# If there is an estate, Watchtowers and Guardhouses make captures less likely
				modifier = {
					location = {
						any_province_domicile = {
							is_domicile_type = estate
							has_domicile_parameter = raids_less_likely_to_make_prisoners_1
						}
					}
					add = -10
				}
				modifier = {
					location = {
						any_province_domicile = {
							is_domicile_type = estate
							has_domicile_parameter = raids_less_likely_to_make_prisoners_2
						}
					}
					add = -15
				}
				modifier = {
					location = {
						any_province_domicile = {
							is_domicile_type = estate
							has_domicile_parameter = raids_less_likely_to_make_prisoners_3
						}
					}
					add = -20
				}

				modifier = {
					root = { raid_intent = capture_intent_nomadic }
					has_raid_intent_immunity = no
					add = {
						value = 30
						if = {
							limit = {
								has_raid_intent_protection = yes
							}
							subtract = 20
						}
						if = {
							limit = {
								scope:raider = { has_perk = kidnapper_perk }
							}
							add = 20
						}
						if = {
							limit = {
								scope:raider.culture ?= { has_innovation = innovation_motte }
							}
							add = 10
						}
					}
				}

				# Capture Effects
				if = {
					limit = { desirable_for_capture_trigger = yes }
					add_to_list = captured_targets_effects
				}
				else = { add_to_list = killed_targets }
			}
		}

		#NOTIFY CONCERNED PARTIES
		#Inform about captures.
		if = {
			limit = {
				any_in_list = {
					list = captured_targets_effects
					count >= 1
				}
			}
			#Inform raider.
			scope:raider = {
				trigger_event = siege.0002
			}
			#Inform affected holder about captures.
			scope:barony.holder = {
				if = {
					limit = {
						save_temporary_scope_as = notification_target
						any_in_list = {
							list = captured_targets_effects
							NOT = { this = scope:notification_target}
						}
					}
					trigger_event = siege.0003
				}
			}
			#Inform concerned players about captures.
			every_player = {
				limit = {
					NOR = {
						this = scope:raider
						this = scope:barony.holder
					}
					save_temporary_scope_as = notification_target
					any_in_list = {
						list = captured_targets_effects
						has_any_relation_trigger = { CHARACTER = scope:notification_target }
					}
				}
				trigger_event = siege.0004
			}
		}
		#Inform about kills.
		if = {
			limit = {
				any_in_list = {
					list = killed_targets
					count >= 1
				}
			}
			# Inform affected holder about kills.
			scope:barony.holder = {
				trigger_event = siege.0013
			}
			#Inform concerned players about kills.
			every_player = {
				limit = {
					NOR = {
						this = scope:raider
						this = scope:barony.holder
					}
					save_temporary_scope_as = notification_target
					any_in_list = {
						list = killed_targets
						has_any_relation_trigger = { CHARACTER = scope:notification_target }
					}
				}
				trigger_event = siege.0014
			}
		}


		#IMPRISON & KILL EFFECTS
		every_in_list = {
			list = captured_targets_effects

			hidden_effect = {
				add_character_flag = {
					flag = block_imprisonment_event
					days = 1
				}
			}

			prisoner_of_war_capture_effect = {
				TARGET = this
				IMPRISONER = scope:raider
			}
		}
		every_in_list = {
			list = killed_targets
			limit = { is_alive = yes }
			death = {
				death_reason = death_raid
			}
		}
	}
}

scripted_trigger raiding_0014_artifact_can_be_stolen = {
	exists = this
	OR = {
		scope:barony.holder = { # If the character is in the province and not leading an army, their stuff is probably valid to steal
			location = scope:barony.title_province
			NOR = {
				exists = commanding_army
				exists = knight_army
				is_imprisoned = yes
			}
		}
		is_equipped = no
		ep1_artifact_is_court_artifact_trigger = yes
	}
}

# Chance to steal an artifact from the holding holder.
raiding.0014 = {
	hidden = yes
	scope = army

	trigger = { 
		scope:barony.holder = {
			# Can only steal an artifact if this barony is the holder's capital (where they have their treasury/royal court.
			capital_barony = scope:barony

			# Holder also needs to actually possess an artifact to steal.
			any_character_artifact = {
				raiding_0014_artifact_can_be_stolen = yes
			}
		}
	}

	immediate = {
		scope:barony.holder = {
			random_character_artifact = {
				limit = { raiding_0014_artifact_can_be_stolen = yes }
				weight = {
					base = 1
					modifier = {
						add = 4
						is_equipped = no
					}
					modifier = {
						add = 4
						scope:raider = {
							can_equip_artifact = prev
						}
					}
					modifier = { # Nudge towards taking non-Court artifacts if you don't have a court yourself
						add = 2
						scope:raider = {
							has_royal_court = no
						}
						ep1_artifact_is_court_artifact_trigger = no
					}
				}
				save_scope_as = stolen_artifact
			}
		}
		
		random = {
			chance = {
				value = 5
				scope:barony.holder = {
					every_character_artifact = {
						limit = {
							OR = {
								is_equipped = no
								ep1_artifact_is_court_artifact_trigger = yes
							}
						}
						add = 1
					}
				}
				# Lower chances based on Antiquarian aptitude
				multiply = {
					value = 1
					scope:barony.holder = {
						every_court_position_holder = {
							type = antiquarian_court_position
							limit = {
								is_available_ai_adult = yes
								aptitude:antiquarian_court_position > 0
							}
							divide = aptitude:antiquarian_court_position
						}
					}
				}
			}
			steal_artifact_in_siege_effect = {
				LOCATION = root.location
				ATTACKER = scope:raider
				DEFENDER = scope:barony.holder
				ARTIFACT = scope:stolen_artifact
				IS_RAID = yes
			}
		}
	}
}


##################################################
# Becoming a Raider/Viking
# by Ewan Cowhig Croft
# #0021-0022
##################################################

# Increase raid tally
raiding.0021 = {
	hidden = yes
	scope = army

	trigger = {
		exists = army_commander
	}

	immediate = {
		root.army_commander = {
			if = {
				limit = {
					NOT = { has_variable = raid_counter }
				}
				set_variable = {
					name = raid_counter
					value = 1
				}
			}
			else = {
				change_variable = {
					name = raid_counter
					add = 1
				}
			}
		}
	}
}

# Chance to fire viking/raider trait acquisition event
raiding.0022 = {
	hidden = yes
	scope = army

	trigger = {
		exists = army_commander
		army_commander = {
			has_variable = raid_counter
			var:raid_counter >= 20
			NOT = { has_trait = viking }
		}
	}

	immediate = {
		root.army_commander = { save_scope_as = raid_leader }
		scope:raid_leader = {
			random = {
				chance = 20
				trigger_event = {
					id = raiding.0023
					days = { 180 545 }
				}
				modifier = {
					add = 10
					var:raid_counter >= 30
				}
				modifier = {
					add = 10
					var:raid_counter >= 40
				}
				modifier = {
					add = 10
					var:raid_counter >= 50
				}
			}
		}
	}
}

# Acquire viking/raider trait
raiding.0023 = {
	type = character_event
	title = raiding.0023.t
	desc = raiding.0023.desc
	theme = raid
	override_background = {
		reference = burning_building
	}
	left_portrait = {
		character = root
		animation = war_over_win
	}

	trigger = {
		scope:raid_leader = {
			is_alive = yes
			NOT = { has_trait = viking }
		}
	}

	immediate = {
		if = {
			limit = { is_landed = no }
			liege.capital_province = { save_scope_as = capital_province }
		}
		else = {
			capital_province = { save_scope_as = capital_province }
		}
	}

	#Accept the trait.
	option = {
		name = raiding.0023.a
		add_trait = viking
	}
}

#################################
# Hereward special raiding event
# by Nick Meredith
#################################

#	You have the chance to sack a holding.
raiding.0031 = {
	hidden = yes
	scope = army

	trigger = {
		#You need to be Hereward, plus leading your own forces.
		exists = army_commander
		army_commander = {
			has_trait = the_wake
		}

		#The province must be ruled by a Norman, or Norman culture itself
		OR = {
			scope:barony.culture = culture:norman
			scope:barony.holder.culture = culture:norman
		}
	}

	immediate = {
		root.army_commander = { save_scope_as = raid_leader }
		scope:raid_leader = {
			add_trait_xp = {
				trait = the_wake
				value = { 1 3 }
			}
		}
	}
}

# Raid Intent Effects
raiding.0040 = {
	hidden = yes
	scope = army

	immediate = {
		if = {
			limit = {
				raid_intent = raze_intent
				has_raid_intent_immunity = no
			}
			scope:barony.title_province ?= {
				random_list = {
					10 = {
						modifier = {
							has_raid_intent_protection = yes
							add = 10
						}
					} # Nothing happens
					5 = {
						modifier = {
							scope:raider.culture ?= { has_innovation = innovation_motte }
							add = 4
						}
						scope:raider = {
							send_interface_message = {
								type = event_raid_good
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								show_as_tooltip = {
									scope:barony.title_province = {
										add_province_modifier = {
											modifier = shattered_defenses_modifier
											years = 5
										}
									}
								}
							}
						}
						scope:barony.holder = {
							send_interface_message = {
								type = event_raid_bad
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								scope:barony.title_province = {
									add_province_modifier = {
										modifier = shattered_defenses_modifier
										years = 5
									}
								}
							}
						}
					}
					5 = {
						trigger = {
							county = { development_level > 0 }
						}
						modifier = {
							scope:raider.culture ?= { has_innovation = innovation_motte }
							add = 4
						}
						scope:raider = {
							send_interface_message = {
								type = event_raid_good
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								show_as_tooltip = {
									scope:county = { change_development_level = -1 }
								}
							}
						}
						scope:barony.holder = {
							send_interface_message = {
								type = event_raid_bad
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								scope:county = { change_development_level = -1 }
							}
						}
					}
					5 = {
						trigger = {
							num_buildings > 1
						}
						modifier = {
							scope:raider.culture ?= { has_innovation = innovation_motte }
							add = 4
						}
						destroy_random_building_variable_effect = yes
						scope:raider = {
							send_interface_message = {
								type = event_raid_good
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								show_as_tooltip = {
									scope:barony.title_province = {
										destroy_random_building_effect = yes
									}
								}
							}
						}
						scope:barony.holder = {
							send_interface_message = {
								type = event_raid_bad
								title = raze_intent_message

								left_icon = scope:barony.holder
								right_icon = root.army_commander
								scope:barony.title_province = {
									destroy_random_building_effect = yes
								}
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				raid_intent = plunder_intent_nomadic
				scope:raider.culture = { can_get_innovation_from = scope:county.culture }
				has_raid_intent_immunity = no
			}
			random = {
				chance = {
					value = 2
					if = {
						limit = {
							has_raid_intent_protection = yes
						}
						divide = 2
					}
					if = {
						limit = {
							scope:raider.culture ?= { has_innovation = innovation_motte }
						}
						add = 1
					}
				}
				scope:raider = {
					send_interface_message = {
						type = event_raid_good
						title = raze_intent_message

						left_icon = scope:barony.holder
						right_icon = root.army_commander
						scope:raider.culture = {
							get_random_innovation_from = scope:county.culture
						}
					}
				}	
			}
		}
		scope:barony.holder ?= {
			if = {
				limit = {
					government_has_flag = government_is_nomadic
				}
				send_interface_message = {
					type = event_raid_bad
					title = raid_herd_message

					left_icon = scope:barony.holder
					right_icon = root.army_commander
					scope:barony.holder.domicile ?= {
						change_herd = -100
					}
				}
			}
		}
	}
}
