﻿# Independent Title and Province Manager scripted GUIs.
# The GUI supplies the local player for world-management actions and the chosen
# recipient for claims/title grants. Title and province selections live in global
# lists, so no vanilla window needs to be open for these actions to work.

advcm_tm_clear_workspace = {
	scope = character
	effect = {
		advcm_tm_clear_workspace_effect = yes
	}
}

advcm_tm_clear_title_selection = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_tm_selected_titles
		clear_global_variable_list = advcm_tm_action_counties
		clear_global_variable_list = advcm_tm_province_list
		clear_global_variable_list = advcm_tm_county_header_provinces
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_selected_title_count value = 0 }
		set_global_variable = { name = advcm_tm_province_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
	}
}

advcm_tm_clear_province_selection = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
	}
}

advcm_tm_load_marked_titles = {
	scope = character
	effect = {
		advcm_tm_clear_workspace_effect = yes
		every_in_global_list = {
			variable = marked_characters
			every_held_title = {
				limit = { this.tier > tier_barony }
				advcm_tm_add_current_title_once_effect = yes
			}
		}
		advcm_tm_sort_title_list_effect = yes
	}
}

advcm_tm_load_marked_realms = {
	scope = character
	effect = {
		advcm_tm_clear_workspace_effect = yes
		every_in_global_list = {
			variable = marked_characters
			every_sub_realm_title = {
				limit = { this.tier > tier_barony }
				advcm_tm_add_current_title_once_effect = yes
			}
		}
		advcm_tm_sort_title_list_effect = yes
	}
}

advcm_tm_load_player_realm = {
	scope = character
	effect = {
		advcm_tm_clear_workspace_effect = yes
		every_sub_realm_title = {
			limit = { this.tier > tier_barony }
			advcm_tm_add_current_title_once_effect = yes
		}
		advcm_tm_sort_title_list_effect = yes
	}
}

advcm_tm_has_titles = {
	scope = character
	is_valid = { global_var:advcm_tm_title_count > 0 }
}

advcm_tm_has_selected_titles = {
	scope = character
	is_valid = { global_var:advcm_tm_selected_title_count > 0 }
}

advcm_tm_ensure_recipient = {
	scope = character
	effect = {
		if = {
			limit = { NOT = { exists = global_var:advcm_title_selected_char } }
			set_global_variable = { name = advcm_title_selected_char value = root }
		}
		else_if = {
			limit = { global_var:advcm_title_selected_char = { is_alive = no } }
			set_global_variable = { name = advcm_title_selected_char value = root }
		}
	}
}

advcm_tm_set_recipient_to_player = {
	scope = character
	effect = {
		set_global_variable = { name = advcm_title_selected_char value = root }
	}
}

advcm_tm_can_assign_selected_titles = {
	scope = character
	is_valid = {
		global_var:advcm_tm_selected_title_count > 0
		exists = global_var:advcm_title_selected_char
		global_var:advcm_title_selected_char = { is_alive = yes }
	}
}

advcm_tm_has_provinces = {
	scope = character
	is_valid = { global_var:advcm_tm_province_count > 0 }
}

advcm_tm_has_selected_provinces = {
	scope = character
	is_valid = { global_var:advcm_tm_selected_province_count > 0 }
}

advcm_tm_is_title_selected = {
	scope = character
	saved_scopes = { title }
	is_valid = {
		is_target_in_global_variable_list = { name = advcm_tm_selected_titles target = scope:title }
	}
}

advcm_tm_is_title_rank_header = {
	scope = character
	saved_scopes = { title }
	is_valid = {
		is_target_in_global_variable_list = { name = advcm_tm_title_rank_header_titles target = scope:title }
	}
}

advcm_tm_is_de_jure_liege_target = {
	scope = character
	saved_scopes = { title }
	is_valid = {
		exists = global_var:advcm_tm_de_jure_liege
		global_var:advcm_tm_de_jure_liege = { this = scope:title }
	}
}

advcm_tm_toggle_title_selection = {
	scope = character
	saved_scopes = { title }
	effect = {
		if = {
			limit = {
				is_target_in_global_variable_list = { name = advcm_tm_selected_titles target = scope:title }
			}
			remove_list_global_variable = { name = advcm_tm_selected_titles target = scope:title }
			change_global_variable = { name = advcm_tm_selected_title_count subtract = 1 }
		}
		else = {
			add_to_global_variable_list = { name = advcm_tm_selected_titles target = scope:title }
			change_global_variable = { name = advcm_tm_selected_title_count add = 1 }
		}
		# Province results are a snapshot of the title selection.
		clear_global_variable_list = advcm_tm_action_counties
		clear_global_variable_list = advcm_tm_province_list
		clear_global_variable_list = advcm_tm_county_header_provinces
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_province_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
	}
}

advcm_tm_select_all_loaded_titles = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_tm_selected_titles
		set_global_variable = { name = advcm_tm_selected_title_count value = 0 }
		every_in_global_list = {
			variable = advcm_tm_title_list
			add_to_global_variable_list = { name = advcm_tm_selected_titles target = this }
			change_global_variable = { name = advcm_tm_selected_title_count add = 1 }
		}
		clear_global_variable_list = advcm_tm_action_counties
		clear_global_variable_list = advcm_tm_province_list
		clear_global_variable_list = advcm_tm_county_header_provinces
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_province_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
	}
}

advcm_tm_set_de_jure_liege = {
	scope = character
	saved_scopes = { title }
	effect = {
		set_global_variable = { name = advcm_tm_de_jure_liege value = scope:title }
	}
}

advcm_tm_clear_de_jure_liege = {
	scope = character
	effect = { remove_global_variable = advcm_tm_de_jure_liege }
}

advcm_tm_has_de_jure_liege = {
	scope = character
	is_valid = { exists = global_var:advcm_tm_de_jure_liege }
}

advcm_tm_apply_de_jure_liege = {
	scope = character
	is_valid = {
		exists = global_var:advcm_tm_de_jure_liege
		global_var:advcm_tm_selected_title_count > 0
	}
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_titles
			save_scope_as = advcm_tm_de_jure_child
			if = {
				limit = {
					global_var:advcm_tm_de_jure_liege = {
						this.tier > scope:advcm_tm_de_jure_child.tier
					}
				}
				set_de_jure_liege_title = global_var:advcm_tm_de_jure_liege
			}
		}
	}
}

advcm_tm_add_strong_claims = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_titles
			save_scope_as = advcm_tm_action_title
			root = { make_claim_strong = scope:advcm_tm_action_title }
		}
	}
}

advcm_tm_add_weak_claims = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_titles
			save_scope_as = advcm_tm_action_title
			root = { make_claim_weak = scope:advcm_tm_action_title }
		}
	}
}

advcm_tm_take_titles = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_titles
			save_scope_as = advcm_tm_action_title
			root = { get_title = scope:advcm_tm_action_title }
		}
	}
}

advcm_tm_destroy_titles = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_titles
			limit = { this.tier > tier_county }
			save_scope_as = advcm_tm_action_title
			root = { destroy_title = scope:advcm_tm_action_title }
		}
		# Destroyed scopes must never remain in GUI data models.
		clear_global_variable_list = advcm_tm_title_list
		clear_global_variable_list = advcm_tm_title_sort_buffer
		clear_global_variable_list = advcm_tm_title_rank_header_titles
		clear_global_variable_list = advcm_tm_selected_titles
		clear_global_variable_list = advcm_tm_action_counties
		clear_global_variable_list = advcm_tm_province_list
		clear_global_variable_list = advcm_tm_county_header_provinces
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_title_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_title_count value = 0 }
		set_global_variable = { name = advcm_tm_province_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
		remove_global_variable = advcm_tm_de_jure_liege
	}
}

advcm_tm_max_control = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_county_control = 100 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_min_control = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_county_control = -100 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_subtract_control_10 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_county_control = -10 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_add_control_10 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_county_control = 10 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_subtract_development_10 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_development_level = -10 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_subtract_development_1 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_development_level = -1 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_add_development_1 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_development_level = 1 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_add_development_10 = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties change_development_level = 10 }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_set_player_culture = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties set_county_culture = root.culture }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_set_player_faith = {
	scope = character
	effect = {
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = { variable = advcm_tm_action_counties set_county_faith = root.faith }
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_load_provinces = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_tm_province_list
		clear_global_variable_list = advcm_tm_county_header_provinces
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_province_count value = 0 }
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
		advcm_tm_collect_selected_counties_effect = yes
		every_in_global_list = {
			variable = advcm_tm_action_counties
			remove_variable = advcm_tm_collapsed
			# Keep each county contiguous and put its capital first so the GUI can
			# use that row as a stable section boundary.
			every_county_province = {
				limit = { is_county_capital = yes }
				add_to_global_variable_list = { name = advcm_tm_county_header_provinces target = this }
				advcm_tm_add_current_province_once_effect = yes
			}
			every_county_province = {
				limit = { is_county_capital = no }
				advcm_tm_add_current_province_once_effect = yes
			}
		}
		clear_global_variable_list = advcm_tm_action_counties
	}
}

advcm_tm_is_county_header_province = {
	scope = character
	saved_scopes = { province }
	is_valid = {
		is_target_in_global_variable_list = { name = advcm_tm_county_header_provinces target = scope:province }
	}
}

advcm_tm_is_province_county_expanded = {
	scope = character
	saved_scopes = { province }
	is_valid = {
		scope:province.county = {
			NOT = { has_variable = advcm_tm_collapsed }
		}
	}
}

advcm_tm_toggle_province_county = {
	scope = character
	saved_scopes = { province }
	effect = {
		scope:province.county = {
			if = {
				limit = { has_variable = advcm_tm_collapsed }
				remove_variable = advcm_tm_collapsed
			}
			else = {
				set_variable = { name = advcm_tm_collapsed value = 1 }
			}
		}
	}
}

advcm_tm_is_province_county_fully_selected = {
	scope = character
	saved_scopes = { province }
	is_valid = {
		scope:province.county = {
			NOT = {
				any_county_province = {
					NOT = {
						is_target_in_global_variable_list = { name = advcm_tm_selected_provinces target = this }
					}
				}
			}
		}
	}
}

advcm_tm_toggle_province_county_selection = {
	scope = character
	saved_scopes = { province }
	effect = {
		scope:province.county = {
			if = {
				limit = {
					NOT = {
						any_county_province = {
							NOT = {
								is_target_in_global_variable_list = { name = advcm_tm_selected_provinces target = this }
							}
						}
					}
				}
				every_county_province = {
					remove_list_global_variable = { name = advcm_tm_selected_provinces target = this }
					change_global_variable = { name = advcm_tm_selected_province_count subtract = 1 }
				}
			}
			else = {
				every_county_province = {
					if = {
						limit = {
							NOT = {
								is_target_in_global_variable_list = { name = advcm_tm_selected_provinces target = this }
							}
						}
						add_to_global_variable_list = { name = advcm_tm_selected_provinces target = this }
						change_global_variable = { name = advcm_tm_selected_province_count add = 1 }
					}
				}
			}
		}
	}
}

advcm_tm_is_province_selected = {
	scope = character
	saved_scopes = { province }
	is_valid = {
		is_target_in_global_variable_list = { name = advcm_tm_selected_provinces target = scope:province }
	}
}

advcm_tm_toggle_province_selection = {
	scope = character
	saved_scopes = { province }
	effect = {
		if = {
			limit = {
				is_target_in_global_variable_list = { name = advcm_tm_selected_provinces target = scope:province }
			}
			remove_list_global_variable = { name = advcm_tm_selected_provinces target = scope:province }
			change_global_variable = { name = advcm_tm_selected_province_count subtract = 1 }
		}
		else = {
			add_to_global_variable_list = { name = advcm_tm_selected_provinces target = scope:province }
			change_global_variable = { name = advcm_tm_selected_province_count add = 1 }
		}
	}
}

advcm_tm_select_all_loaded_provinces = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_tm_selected_provinces
		set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
		every_in_global_list = {
			variable = advcm_tm_province_list
			add_to_global_variable_list = { name = advcm_tm_selected_provinces target = this }
			change_global_variable = { name = advcm_tm_selected_province_count add = 1 }
		}
	}
}

advcm_tm_set_county_capital = {
	scope = character
	saved_scopes = { province }
	is_valid = {
		scope:province = { is_county_capital = no has_holding = yes }
	}
	effect = { scope:province.barony = { set_capital_barony = yes } }
}

advcm_tm_set_holding_castle = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = castle_holding }
	}
}

advcm_tm_set_holding_city = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = city_holding }
	}
}

advcm_tm_set_holding_church = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = church_holding }
	}
}

advcm_tm_set_holding_tribal = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = tribal_holding }
	}
}

advcm_tm_set_holding_nomad = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = nomad_holding }
	}
}

advcm_tm_set_holding_herder = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = herder_holding }
	}
}

advcm_tm_set_holding_temple_citadel = {
	scope = character
	effect = {
		every_in_global_list = { variable = advcm_tm_selected_provinces set_holding_type = temple_citadel_holding }
	}
}

advcm_tm_enable_extra_slot = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_provinces
			if = {
				limit = { NOT = { has_province_modifier = advcm_tm_extra_building_slot_modifier } }
				add_province_modifier = advcm_tm_extra_building_slot_modifier
			}
		}
	}
}

advcm_tm_disable_extra_slot = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_provinces
			if = {
				limit = { has_province_modifier = advcm_tm_extra_building_slot_modifier }
				remove_province_modifier = advcm_tm_extra_building_slot_modifier
			}
		}
	}
}

advcm_tm_fill_building_slots = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_provinces
			while = {
				count = 12
				limit = { has_holding = yes free_building_slots > 0 }
				generate_building = yes
			}
		}
	}
}

advcm_tm_remove_holdings = {
	scope = character
	effect = {
		every_in_global_list = {
			variable = advcm_tm_selected_provinces
			if = {
				limit = { has_holding = yes is_county_capital = no }
				remove_holding = yes
			}
		}
	}
}

# Backward-compatible single-title entry points retained for extensions that
# already call the old ACM scripted GUIs. The new window does not depend on them.
advcm_get_title_list = {
	scope = character
	effect = {
		clear_global_variable_list = advcm_title_list
		every_in_global_list = {
			variable = marked_characters
			every_held_title = {
				limit = { this.tier > tier_barony }
				add_to_global_variable_list = { name = advcm_title_list target = this }
			}
		}
		ordered_in_global_list = {
			variable = advcm_title_list
			order_by = tier
			min = 0
			max = 500
			remove_list_global_variable = { name = advcm_title_list target = this }
			add_to_global_variable_list = { name = advcm_title_list target = this }
		}
	}
}

advcm_clear_title_list = {
	scope = character
	effect = { clear_global_variable_list = advcm_title_list }
}

advcm_title_select_char = {
	scope = character
	effect = { set_global_variable = { name = advcm_title_selected_char value = this } }
}

advcm_strong_claim = {
	scope = character
	saved_scopes = { claim_title }
	effect = { make_claim_strong = scope:claim_title }
}

advcm_weak_claim = {
	scope = character
	saved_scopes = { claim_title }
	effect = { make_claim_weak = scope:claim_title }
}

advcm_get_title = {
	scope = character
	saved_scopes = { claim_title }
	effect = { get_title = scope:claim_title }
}

advcm_destroy_title = {
	scope = character
	saved_scopes = { claim_title }
	effect = { destroy_title = scope:claim_title }
}

advcm_change_title_development = {
	scope = character
	saved_scopes = { claim_title direction mode }
	effect = {
		scope:claim_title = {
			if = {
				limit = { tier = tier_county }
				if = {
					limit = { scope:direction = flag:add }
					if = {
						limit = { scope:mode = flag:ctrl }
						change_development_level = {
							value = 100
							subtract = development_level
						}
					}
					else_if = {
						limit = { scope:mode = flag:shift }
						change_development_level = 10
					}
					else = { change_development_level = 1 }
				}
				else = {
					if = {
						limit = { scope:mode = flag:ctrl }
						change_development_level = { subtract = development_level }
					}
					else_if = {
						limit = { scope:mode = flag:shift }
						change_development_level = -10
					}
					else = { change_development_level = -1 }
				}
			}
		}
	}
}

advcm_change_title_control = {
	scope = character
	saved_scopes = { claim_title direction mode }
	effect = {
		scope:claim_title = {
			if = {
				limit = { tier = tier_county }
				if = {
					limit = { scope:direction = flag:add }
					if = {
						limit = { scope:mode = flag:ctrl }
						change_county_control = {
							value = 100
							subtract = county_control
						}
					}
					else_if = {
						limit = { scope:mode = flag:shift }
						change_county_control = 10
					}
					else = { change_county_control = 1 }
				}
				else = {
					if = {
						limit = { scope:mode = flag:ctrl }
						change_county_control = { subtract = county_control }
					}
					else_if = {
						limit = { scope:mode = flag:shift }
						change_county_control = -10
					}
					else = { change_county_control = -1 }
				}
			}
		}
	}
}
