﻿steal_artifact_in_siege_effect = {
	# Save scopes for localization
	$LOCATION$ = { save_scope_as = SAISE_location }
	$ATTACKER$ = { save_scope_as = SAISE_attacker }
	$DEFENDER$ = { save_scope_as = SAISE_defender }
	$ARTIFACT$ = { save_scope_as = this_artifact }

	scope:this_artifact = {
		# Suppress generic notifications, as we will send custom ones
		set_variable = {
			name = suppress_artifact_notifications
			value = yes
			days = 1
		}
		if = {
			limit = {
				NOT = { has_variable = stolen_artifact }
			}
			set_variable = {
				name = stolen_artifact
				value = 1
			}
		}
		else = {
			change_variable = {
				name = stolen_artifact
				add = 1
			}
		}

		# Transfer the artifact to its new owner
		if = {
			limit = {
				has_variable = ai_to_ai_destroy_in_sieges
				scope:SAISE_attacker = {
					is_ai = yes
				}
				scope:SAISE_defender = {
					is_ai = yes
				}
			}
			destroy_artifact = this
		}
		else = {
			set_owner = {
				target = scope:SAISE_attacker
				history = {
					location = scope:SAISE_location
					actor = scope:SAISE_defender
					recipient = scope:SAISE_attacker
					type = taken_in_siege
				}
			}
			scope:SAISE_defender = { add_personal_artifact_claim = scope:this_artifact }
			if = {
				limit = {
					scope:SAISE_attacker = {
						has_ep3_dlc_trigger = yes
						is_roman_emperor_trigger = yes
						culture = { has_cultural_parameter = holds_triumphs }
					}
				}
				scope:SAISE_attacker = {
					set_variable = {
						name = byz_valid_for_trophy
						value = scope:this_artifact
						years = 9
					}
				}
			}
			
			# Send notifications to the relevant parties
			scope:SAISE_defender = {
				send_interface_toast = {
					type = msg_artifact_lost
					title = artifact_lost_toast
					left_icon = scope:this_artifact
					right_icon = scope:SAISE_attacker
					if = {
						limit = { $IS_RAID$ = yes }
						custom_tooltip = artifact_lost_in_raid
					}
					else = {
						custom_tooltip = artifact_lost_in_siege
					}
				}
			}
			scope:SAISE_attacker = {
				send_interface_toast = {
					type = msg_artifact_gained
					right_icon = scope:this_artifact
				}
				if = {
					limit = {
						NOT = { any_claimed_artifact = { this = scope:this_artifact } }
					}
					scope:this_artifact = {
						set_variable = {
							name = last_person_to_steal
							value = scope:SAISE_attacker
						}
					} 
				}
			}
		}
	}
}

steal_artifact_in_event_effect = {
	if = {
		limit = { exists = $VICTIM$.capital_province }
		set_local_variable = {
			name = location
			value = $VICTIM$.capital_province
		}
	}
	else_if = {
		limit = { exists = $VICTIM$.location }
		set_local_variable = {
			name = location
			value = $VICTIM$.location
		}
	}

	$ARTIFACT$ = {
		save_scope_as = artifact_to_steal
		hidden_effect = {
			if = {
				limit = {
					exists = local_var:location
				}
				set_owner = {
					target = $THIEF$
					history = {
						type = stolen
						actor = $VICTIM$ #stolen from
						recipient = $THIEF$ #stealer
						location = local_var:location
					}
				}
			}
			else = { #If no location exists it is "conquered"
				set_owner = {
					target = $THIEF$
					history = {
						type = conquest
						actor = $THIEF$ #stealer
						recipient = $VICTIM$ #stolen from
					}
				}
			}
			if = {
				limit = {
					NOT = { has_variable = stolen_artifact }
				}
				set_variable = {
					name = stolen_artifact
					value = 1
				}
			}
			else = {
				change_variable = {
					name = stolen_artifact
					add = 1
				}
			}
		}
	}
	$VICTIM$ = {
		add_personal_artifact_claim = scope:artifact_to_steal
	}
}

damage_artifact_in_battle_effect = {
	# Save scopes for localization
	$OWNER$ = {
		save_scope_as = DAIBE_owner
	}
	$OPPONENT$ = { save_scope_as = DAIBE_opponent }
	$LOCATION$ = { save_scope_as = DAIBE_location }
	$ARTIFACT$ = {
		save_scope_as = DAIBE_artifact 

		# Suppress generic notifications, as we will send custom ones
		set_variable = {
			name = suppress_artifact_notifications
			value = yes
			days = 1
		}

		set_variable = {
			name = DAIBE_damage
			value = $DURABILITY_LOSS$
			days = 1
		}

		# Damage the artifact by the specified amount
		add_durability = {
			value = 0
			subtract = var:DAIBE_damage
		}
	}

	# Transformed into an if / else as I found the following note somewhere else:
	# NOTE: We must not run ANY script after we destroy an artifact, or we will cause errors
	if = {
		# If the artifact has no durability left, destroy it now.
		limit = { scope:DAIBE_artifact.artifact_durability <= 0 }
		destroy_artifact = scope:DAIBE_artifact
	}
	# Send notifications to the relevant party IF the artifact was not destroyed
	else = {
		$OWNER$ = {
			# Send the toast
			send_interface_toast = {
				title = artifact_damaged_toast
				left_icon = scope:DAIBE_artifact
				right_icon = scope:DAIBE_opponent
				if = {
					limit = {
						$IS_DUEL$ = yes
					}
					custom_tooltip = artifact_damaged_in_duel
				}
				else = {
					custom_tooltip = artifact_damaged_in_siege
				}
			}
		}
	}
}

flag_as_trash_artifact = {
	set_variable = {
		name = ai_to_ai_destroy_in_sieges
		value = yes
	}
}

flag_as_unwanted_artifact = {
	set_variable = {
		name = unwanted_artifact
		value = yes
	}
}

flag_as_cursed_artifact = {
	set_variable = {
		name = cursed_artifact
		value = yes
	}
}

#For use with trinket looks / call this effect (set_up_trinket_adjective_effect = yes) in the line before creating the trinket
#Use this in the trinket's name/description in the loc-file [ROOT.Char.Custom('TrinketLookAdjective')] /and/ [ROOT.Char.Custom('TrinketLookAdverbFollowup')]
#Remember to clear the var:trinket_adjective_followup in your after = { ... }
set_up_trinket_adjective_effect = {
	random_list = {
		50 = {
			set_variable = {
				name = trinket_adjective_followup
				value = flag:adorned
			}
		}
		50 = {
			set_variable = {
				name = trinket_adjective_followup
				value = flag:decorated
			}
		}
		50 = {
			set_variable = {
				name = trinket_adjective_followup
				value = flag:embellished
			}
		}
		50 = {
			set_variable = {
				name = trinket_adjective_followup
				value = flag:ornamented
			}
		}

	}
}

# Grant claims on familial banners
grant_banner_house_claim_effect = {
	save_scope_as = familial_banner
	hidden_effect = {
		if = { # Dynasty banner not owned or claimed by all child house's already
			limit = {
				exists = var:banner_dynasty
				var:banner_dynasty = {
					any_dynasty_member = {
						this = house.house_head
						house = { artifact_house_not_owns_or_claims_trigger = yes } # House not owns or claims already
					}
				}
			}
			var:banner_dynasty = {
				every_dynasty_member = {
					limit = {
						this = house.house_head
						house = { artifact_house_not_owns_or_claims_trigger = yes } # House not owns or claims already
					}
					house = { add_house_artifact_claim = scope:familial_banner }
				}
			}
		}
		else_if = { # House banner not owned or claimed by creating house already
			limit = {
				exists = var:banner_house
				var:banner_house = { artifact_house_not_owns_or_claims_trigger = yes } # House not owns or claims already
			}
			var:banner_house = { add_house_artifact_claim = scope:familial_banner }
		}
	}
}

#Determine what type of artifact a court artifact is, for defacement
determine_artifact_damage_type_effect = {
	$ARTIFACT$ = { save_scope_as = artifact_to_determine }
	scope:artifact_to_determine = {
		random_list = {
			10 = {
				trigger = { 
					OR = {
						artifact_type = chest
						artifact_type = box
						artifact_type = cabinet
						artifact_type = pedestal
						artifact_type = tapestry
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big
						artifact_type = wall_shield
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
					}
				}
				set_variable = artifact_burned
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = sculpture
						artifact_type = box
						artifact_type = goblet
						artifact_type = pedestal
						artifact_type = book
						artifact_type = elixir
						artifact_type = panacea
						artifact_type = philosophers_stone
					}
				}
				set_variable = artifact_knocked_over
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = sculpture
						artifact_type = chest
						artifact_type = box
						artifact_type = cabinet
						artifact_type = goblet
						artifact_type = pedestal
						artifact_type = tapestry #cloth
						artifact_type = animal_skull
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big #shields, banners
						artifact_type = wall_small #animal trophy
						artifact_type = wall_shield
						artifact_type = wall_shield_special #kite shields
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
						artifact_type = elixir
						artifact_type = panacea
						artifact_type = philosophers_stone #box
					}
				}
				set_variable = artifact_smeared_food
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = chest
						artifact_type = box
						artifact_type = cabinet
						artifact_type = pedestal
						artifact_type = tapestry
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big
						artifact_type = wall_small
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
					}
				}
				set_variable = artifact_spilled_drink
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = tapestry
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = book
					}
				}
				set_variable = artifact_tear
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = tapestry
						artifact_type = animal_skull
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big
						artifact_type = wall_small
						artifact_type = wall_shield
						artifact_type = wall_shield_special
					}
				}
				set_variable = artifact_tear_down
			}
			10 = {
				trigger = { 
					OR = {
						artifact_type = chest
						artifact_type = box
						artifact_type = pedestal
						artifact_type = tapestry
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big
						artifact_type = wall_small
						artifact_type = wall_shield
						artifact_type = wall_shield_special
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
					}
				}
				set_variable = artifact_threw_up
			}
			10 = {
				trigger = {
					OR = {
						artifact_type = sculpture
						artifact_type = chest
						artifact_type = box
						artifact_type = cabinet
						artifact_type = goblet
						artifact_type = pedestal
						artifact_type = tapestry #cloth
						artifact_type = animal_skull
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big #shields, banners
						artifact_type = wall_small #animal trophy
						artifact_type = wall_shield
						artifact_type = wall_shield_special #kite shields
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
						artifact_type = elixir
						artifact_type = panacea
						artifact_type = philosophers_stone #box
					}
				}
				set_variable = artifact_unspeakable
			}
			10 = {
				trigger = {
					NOR = {
						artifact_type = sculpture
						artifact_type = chest
						artifact_type = box
						artifact_type = cabinet
						artifact_type = goblet
						artifact_type = pedestal
						artifact_type = tapestry #cloth
						artifact_type = animal_skull
						artifact_type = animal_hide
						artifact_type = animal_hide_big
						artifact_type = wall_big #shields, banners
						artifact_type = wall_small #animal trophy
						artifact_type = wall_shield
						artifact_type = wall_shield_special #kite shields
						artifact_type = throne
						artifact_type = throne_special
						artifact_type = book
						artifact_type = elixir
						artifact_type = panacea
						artifact_type = philosophers_stone #box
					}
				}
				set_variable = artifact_damage_fallback
			}
		}
	}
}

# Destroy Artifact effect
# DESTROYER = Character destroying the artifact
# ARTIFACT = The artifact to destroy
destroy_artifact_aniconist_effect = {
	$ARTIFACT$ = {
		save_temporary_scope_as = artifact
	}

	$DESTROYER$ = {
		if = {
			limit = {
				OR = {
					$DESTROYER$.faith = { has_doctrine_parameter = destroying_artifacts_is_pious }
					$PIETY_BY_DEFAULT$ = yes
				}
			}
			give_destroyed_artifact_piety = yes
		}
		if = {
			limit = {
				$GIVE_GOLD$ = yes
			}
			add_gold = {
				if = {
					limit = {
						scope:artifact = { rarity = illustrious }
					}
					add = {
						value = 25
					}
				}
				else_if = {
					limit = {
						scope:artifact = { rarity = famed }
					}
					add = {
						value = 15
					}
				}
				else_if = {
					limit = {
						scope:artifact = { rarity = masterwork }
					}
					add = {
						value = 8
					}
				}
				else = {
					add = {
						value = 4
					}
				}

				# Boost the value up to something meaningful
				multiply = {
					value = 10
				}

				# multiply by durability percentage
				multiply = {
					add = scope:artifact.artifact_durability
					divide = scope:artifact.artifact_max_durability	
					divide = 2
					add = 0.5
				}

				#Nerf it to bits for LAAMPs
				if = {
					limit = { 
						$DESTROYER$ = { has_government = landless_adventurer_government }
					}
					multiply = {
						value = 0.1
						round = yes
					}
				}
				if = {
					limit = {
						exists = scope:artifact.var:ai_to_ai_destroy_in_sieges # Trash artifact
					}
					max = {
						value = $DESTROYER$.tiny_gold_value
						multiply = 0.5
					}
				}
				
				min = 1
			}
		}
		destroy_owned_artifact = scope:artifact
	}
}

give_destroyed_artifact_piety = {
	add_piety = {
		# Base value
		value = 5
		# Add the artifact's quality
		if = {
			limit = { exists = scope:artifact.var:quality }
			add = scope:artifact.var:quality
		}
		# If none can be found, add its wealth
		else_if = {
			limit = { exists = scope:artifact.var:wealth }
			add = scope:artifact.var:wealth
		}
		# Artifact has no quality or wealth (typical for court artifacts), add something based on rarity
		else_if = {
			limit = {
				scope:artifact = { rarity = illustrious }
			}
			add = {
				value = 20
			}
		}
		else_if = {
			limit = {
				scope:artifact = { rarity = famed }
			}
			add = {
				value = 12
			}
		}
		else_if = {
			limit = {
				scope:artifact = { rarity = masterwork }
			}
			add = {
				value = 8
			}
		}
		else = {
			add = {
				value = 4
			}
		}
		# Boost the value up to something meaningful
		multiply = 20
		# Relics are way cooler to burn, double the final value
		if = {
			limit = {
				scope:artifact = { exists = var:relic }
			}
			multiply = {
				value = 2
				desc = aniconist_destroying_relic
			}
		}
	}
}
