﻿# THESE ARE USED IN A LOT OF PLACES; DO NOT CHANGE THE STRUCTURE, ONLY ADD TO THE APPROPRIATE LISTS
# -By Tobbzn, from The Way of Kings CK3 mod.

## In this file, you can:
# Add effects that you might want to apply to one or more of the artifacts defined in artifacts_custom.txt

## NOTE FOR MODDERS:
# To use complicated strings as arguments, you MUST call the core or custom artifact list *directly*, because strings will get resolved after being passed from one function to the next. I have not been able to find a workaround, but there might exist one if one implements arguments of the form TYPE = "$TYPE$".
# This restriction includes empty "" strings! Those are perhaps the most complicated of all!

# Add and remove individual artifacts:

add_artifact = {# ARTIFACT = X
	set_variable = {
		name = $ARTIFACT$
		value = flag:$ARTIFACT$
	}
	add_to_variable_list = {
		name = artifact_list
		target = flag:$ARTIFACT$
	}
	set_variable = {
		name = most_recent_artifact
		value = flag:$ARTIFACT$
	}
	#$ARTIFACT$_modifier = {save_scope_as = artyparty}
	#custom_description ={
		#text = key_add_artifact 
		#subject = ROOT
		#value = 15
		#modifier = artifact_ryshadium_modifier
		#object = 1003
		#character = root
		#artifact = "test"
		#value = var:stormlight
	#}
	show_as_tooltip = {add_character_modifier = $ARTIFACT$_modifier}
	#custom_tooltip = "[ROOT.Var('ARTIFACT').GetFlagName]"
#"[GetModifier(Concatenate(ROOT.Var('$ARTIFACT$').GetFlagName,'_modifier')).GetNameWithTooltip] test"
}
remove_artifact = {# ARTIFACT = X
	remove_variable = $ARTIFACT$
	remove_list_variable = {
		name = artifact_list
		target = flag:$ARTIFACT$
	}
}
select_artifact = {# ARTIFACT = X
	clear_all_artifacts = yes
	set_variable = {
		name = $ARTIFACT$_selected
		value = flag:$ARTIFACT$
	}
	set_variable = {
		name = artifact_selection
		value = flag:$ARTIFACT$
	}
}
remove_artifact_selection = {# ARTIFACT = X
	remove_variable = $ARTIFACT$_selected
	remove_variable = artifact_selection
}
add_equipped_artifact = {# ARTIFACT = X
	set_variable = {
		name = $ARTIFACT$_equipped
		value = flag:$ARTIFACT$
	}
	add_to_variable_list = {
		name = artifact_list_equipped
		target = flag:$ARTIFACT$
	}
	add_artifact_modifier = {ARTIFACT = $ARTIFACT$}
}
select_and_equip_artifact = { # ARTIFACT = X
	if = {limit = {has_variable = $ARTIFACT$}
	select_artifact = {ARTIFACT = $ARTIFACT$}
	for_all_slots = {APPLY = unequip_selected_type_artifacts}
	equip_selected_artifact = {ARTIFACT = $ARTIFACT$}
}}

remove_equipped_artifact = {# ARTIFACT = X
	remove_variable = $ARTIFACT$_equipped
	remove_list_variable = {
		name = artifact_list_equipped
		target = flag:$ARTIFACT$
	}
	remove_artifact_modifier = {ARTIFACT = $ARTIFACT$}
}

add_all_artifacts = {# USAGE {}
	for_all_artifacts_no_arguments = {APPLY = add_artifact}}

# Modifiers

add_artifact_modifier = { # ARTIFACT = X}
	add_character_modifier = $ARTIFACT$_modifier
}
remove_artifact_modifier = { # ARTIFACT = X}
	remove_character_modifier = $ARTIFACT$_modifier
}

add_all_artifact_modifiers = {# USAGE {}
	for_all_artifacts_no_arguments = {APPLY = add_artifact_modifier}}

remove_all_artifact_modifiers = {# USAGE {}
	for_all_artifacts_no_arguments = {APPLY = remove_artifact_modifier}}

# Listification from lambda calculus to global variable list of flags. artifact_ID -> flag:artifact_ID

# listify_all_typed_artifacts_as_flags and accompanying list of types can be found in artifacts.txt and should be invoked at the game_start on_action.

listify_artifact_as_flag = {# USAGE {ARTIFACT = X A = listname B = subtype}
	add_to_global_variable_list = {
		name = $A$_$B$
		target = flag:$ARTIFACT$
	}
	add_to_global_variable_list = {
		name = $A$_$B$_selected
		target = flag:$ARTIFACT$
	}
	add_to_global_variable_list = {
		name = $A$_$B$_equipped
		target = flag:$ARTIFACT$
	}
}
listify_typed_artifacts_as_flags = { #TYPE = string 
	for_$TYPE$_artifacts_nofix = {
		APPLY = "listify_artifact_as_flag" 
		A = global_artifact_list
		B = $TYPE$
	}
}
# Include the line
# 		initialize_artifacts = yes
# In order to initialize the artifact rarities, types, most triggers, and the AI's ability to equip artifacts.
initialize_artifacts = {
	# Make global lists; required for rarities, types, triggers, AI.
	listify_all_typed_artifacts_as_flags = {} 
	trigger_event = { # Call any other custom artifact setup - note that this will trigger *after* unpausing the game!
		on_action = on_artifact_initialization
	}
	trigger_event = { # Starts the monthly AI artifact equipper - this repeats every month!
		on_action = on_monthly_artifacts
	}
}
# Destruction effect for selected artifact
destroy_artifact_if_selected = {# Usage: {ARTIFACT = X} 
	if = {
		limit = {
			has_variable = $ARTIFACT$_selected
			NOT = {
				is_selection_indestructible = yes
			}
		}
		remove_artifact = {ARTIFACT = $ARTIFACT$}
		remove_equipped_artifact = {ARTIFACT = $ARTIFACT$}
		remove_artifact_selection = {ARTIFACT = $ARTIFACT$}
		#remove_character_modifier = $ARTIFACT$_modifier
	}
	#$A$ = $B$
}

equip_selected_artifact = {# Usage: {ARTIFACT = X}
	if = {
		limit = {
			has_variable = $ARTIFACT$_selected
		}
		add_equipped_artifact = {ARTIFACT = $ARTIFACT$}
		#add_character_modifier = $ARTIFACT$_modifier
	}
	#$A$ = $B$
}

unequip_selected_artifact = {# Usage: {ARTIFACT = X}
	if = {
		limit = {
			has_variable = $ARTIFACT$_selected
		}
		remove_equipped_artifact = {ARTIFACT = $ARTIFACT$}
		#remove_character_modifier = $ARTIFACT$_modifier
	}
	#$A$ = $B$
}


# Transfer effect 
# Note: Cannot get modifier from flag outside of gui unless we demand another manual 1-to-1 listing, so this cannot be directly adapted to take flag arguments.
transfer_artifact = {# Usage: transfer_artifact = {A = From B = To ARTIFACT = X} 
#where X and Y are characters and Z is an artifact name in standard form.
	if = {
		limit = {
			$A$ = { has_variable = $ARTIFACT$ }
		}
		if = {
			limit = {
				$B$ = { has_variable = $ARTIFACT$ }
			}
			debug_log = "Error:  $B$ has $ARTIFACT$ already so it cannot be transfered"
			debug_log_scopes = yes
		}
		else = {
			$A$ = { 
				remove_artifact = {ARTIFACT = $ARTIFACT$}
				remove_equipped_artifact = {ARTIFACT = $ARTIFACT$}
				remove_artifact_selection = {ARTIFACT = $ARTIFACT$}
			}
			$B$ = { 
				add_artifact = {ARTIFACT = $ARTIFACT$}
			}
		}
	}
}
transfer_all_artifacts = { # Usage: transfer_all_artifacts = {A = X B = Y}
	for_all_artifacts_nofix = {
		APPLY = transfer_artifact
		A = $A$
		B = $B$
	}
}

# Equipping effect
# Note: Cannot get modifier from flag outside of gui unless we demand another manual 1-to-1 listing, so this cannot be directly adapted to take flag arguments.
equip_artifact = { # Usage: equip_artifact = {ARTIFACT = X} 
	if = {
		limit = {
				has_variable = $ARTIFACT$
		}
		add_equipped_artifact = {ARTIFACT = $ARTIFACT$}
	}
}
equip_all_owned_artifacts = { 
	for_all_artifacts_no_arguments = {
		APPLY = select_and_equip_artifact
	}
}

# Clearing effects
clear_all_artifacts = { # Clears selected artifacts
	for_all_artifacts_no_arguments = {APPLY = remove_artifact_selection }
}

# Uncertain use:
artifact_changing_hands_calculator = {
	if = {
		limit = { $OCCUPIED$ = { has_variable = $ARTIFACT$ } }
		random = {
			chance = $CHANCE$
			$OCCUPIED$ = { remove_variable = $ARTIFACT$ }
			$OCCUPIED$ = { trigger_event = artifact_siege.0003 }
			$OCCUPIER$ = { set_variable = $ARTIFACT$ }
			$OCCUPIER$ = { trigger_event = artifact_siege.0002 }
		}
	}
}

# Unequipment effects
unequip_all_artifacts = {
	for_all_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	remove_all_artifact_modifiers = yes
}
unequip_handheld_artifacts = {
	for_handheld_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	for_handheld_artifacts_no_arguments = {APPLY = remove_artifact_modifier}
}
unequip_armor_artifacts = {
	for_armor_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	for_armor_artifacts_no_arguments = {APPLY = remove_artifact_modifier}
}
unequip_headgear_artifacts = {
	for_headgear_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	for_headgear_artifacts_no_arguments = {APPLY = remove_artifact_modifier}
}
unequip_jewelry_artifacts = {
	for_jewelry_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	for_jewelry_artifacts_no_arguments = {APPLY = remove_artifact_modifier}
}
unequip_mount_artifacts = {
	for_mount_artifacts_no_arguments = {APPLY = remove_equipped_artifact }
	for_mount_artifacts_no_arguments = {APPLY = remove_artifact_modifier}
}

unequip_selected_type_artifacts = {# TYPE = Y
	if = {
		limit = {
			is_artifact_of_type = {ARTIFACT = var:artifact_selection TYPE = $TYPE$}
		}
		unequip_$TYPE$_artifacts = yes
	}
}

#Has Ai equip artifact
#Will try to equip from higher rarity groups first
#Unequips equipped artifact first
artifact_ai_equip_effect = {
	unequip_all_artifacts = yes
}
artifact_shardblade_ai_equip_effect = {
	unequip_handheld_artifacts = yes
	if = {
		limit = {
			has_any_shard_artifacts = yes
		}
		random_list = {
			20 = {
				trigger = {has_variable = artifact_mayalaran}
				add_equipped_artifact = {ARTIFACT = artifact_mayalaran}
				add_character_modifier = {
					modifier = artifact_mayalaran_modifier
				}
			}
			20 = {
				trigger = {has_variable = artifact_oathbringer}
				add_equipped_artifact = {ARTIFACT = artifact_oathbringer}
				add_character_modifier = {
					modifier = artifact_oathbringer_modifier
				}
			}
			20 = {
				trigger = {has_variable = artifact_sunraiser}
				add_equipped_artifact = {ARTIFACT = artifact_sunraiser}
				add_character_modifier = {
					modifier = artifact_sunraiser_modifier
				}
			}
			20 = {
				trigger = {has_variable = artifact_shardblade}
				add_equipped_artifact = {ARTIFACT = artifact_shardblade}
				add_character_modifier = {
					modifier = artifact_shardblade_modifier
				}
			}
			60 = {
				trigger = {has_variable = artifact_jezrien_honorblade}
				add_equipped_artifact = {ARTIFACT = artifact_jezrien_honorblade}
				add_character_modifier = {
					modifier = artifact_jezrien_honorblade_modifier
				}
			}
			70 = {
				trigger = {has_variable = artifact_nightblood}
				add_equipped_artifact = {ARTIFACT = artifact_nightblood}
				add_character_modifier = {
					modifier = artifact_nightblood_modifier
				}
				trigger_event = nightblood.1
			}
			50 = {
				trigger = {has_variable = artifact_type_four_biochromatic_entity}
				add_equipped_artifact = {ARTIFACT = artifact_type_four_biochromatic_entity}
				add_character_modifier = {
					modifier = artifact_type_four_biochromatic_entity_modifier
				}
				trigger_event = type_four_biochromatic_entity.1
			}
		}
	}
}
artifact_shardplate_ai_equip_effect = {
	unequip_armor_artifacts = yes
	if = {
		limit = {
			OR = {
				has_variable = artifact_shardplate
			}
		}
		add_equipped_artifact = {ARTIFACT = artifact_shardplate}
		add_character_modifier = {
			modifier = artifact_shardplate_modifier
		}
	}
}

fulfill_the_vengeance_pact_effects = {
	add_character_modifier = {
		modifier = artifact_nightblood_modifier
	}
}

select_artifact_to_give = {# ARTIFACT = X
	if = {
		limit = { NOT = { has_variable = $ARTIFACT$_selected_give } }
		set_variable = {
			name = $ARTIFACT$_selected_give
			value = flag:$ARTIFACT$
		}
	}
	else = {
		remove_variable = $ARTIFACT$_selected_give
	}
}

transfer_selected_gift_artifacts = { # Usage: transfer_all_artifacts = {FROM = X TO = Y}
	for_all_artifacts_nofix = {
		APPLY = gift_artifact
		A = $FROM$
		B = $TO$
	}
}
gift_artifact = {# Usage: transfer_artifact = {A = From B = To ARTIFACT = X} 
#where X and Y are characters and Z is an artifact name in standard form.
	if = {
		limit = {
			$A$ = { has_variable = $ARTIFACT$_selected_give }
		}
		if = {
			limit = {
				$B$ = { has_variable = $ARTIFACT$ }
			}
			debug_log = "Error:  $B$ has $ARTIFACT$ already so it cannot be transfered"
			debug_log_scopes = yes
		}
		else = {
			$A$ = { 
				remove_artifact = {ARTIFACT = $ARTIFACT$}
				remove_equipped_artifact = {ARTIFACT = $ARTIFACT$}
				remove_artifact_selection = {ARTIFACT = $ARTIFACT$}
				remove_variable = $ARTIFACT$_selected_give
				remove_character_modifier = $ARTIFACT$_modifier
			}
			$B$ = { 
				add_artifact = {ARTIFACT = $ARTIFACT$}
			}
		}
	}
}

