# Title decisions are possible vs _all_ titles and are shown in the Title Diplomacy View, not the Intrigue View. The taker is in the FROM scope.
#
# filter = [capital/primary_title/owned/vassal_owned/sub_realm_owned/realm_owned/dynasty_owned/all]
# ai_target_filter = [capital/primary_title/owned/vassal_owned/sub_realm_owned/realm_owned/dynasty_owned/all] (which titles for which the AI evaluates the decision.)
#	owned: all titles owned by the AI
#	vassal_owned: all titles owned by direct vassal rulers of the AI's employer
#	sub_realm_owned: all titles below the AI's employer
#	realm_owned: all titles in the same top realm as the AI
#	dynasty_owned: all titles owned by members of the same dynasty
#	all: all titles (Avoid if possible. VERY CPU-HEAVY!)
#

title_decisions = {

	tribal_build_city = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_feudal = yes
		}

		potential = {
			real_tier = COUNT

			location = {
				has_city = no
				num_of_empty_holdings >= 1
				has_province_modifier = new_buildings_timer
				NOT = { has_province_modifier = building_new_city }
			}
		}

		allow = {
			is_occupied = no

			location = {
				culture = FROM
				num_of_empty_holdings >= 1
			}

			FROM = {
				wealth >= 100
				war = no

				custom_tooltip = {
					text = NOT_BUILDING_CITY

					NOT = {
						any_demesne_province = {
							has_province_modifier = building_new_city
							owned_by = PREV
						}
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no
				wealth = -100
			}

			location = {
				show_scope_change = no

				custom_tooltip = {
					text = CITY_CONSTRUCTION_BEGINS

					set_province_flag = tribal_build_city

					add_province_modifier = {
						name = building_new_city
						duration = -1
					}
				}
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				FROM = {
					NOT = { has_character_modifier = ai_austerity }
				}
			}

			mult_modifier = {
				factor = 5
				location = { is_capital = yes }
			}

			mult_modifier = {
				factor = 2

				location = {
					OR = {
						has_building = tb_town_market_1
						has_building = tb_town_market_2
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				location = {
					OR = {
						has_building = tb_shipyard_1
						has_building = tb_shipyard_2
					}
				}
			}
		}
	}

	tribal_build_temple = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_feudal = yes
		}

		potential = {
			real_tier = COUNT

			location = {
				has_temple = no
				num_of_empty_holdings >= 1
				has_province_modifier = new_buildings_timer
				NOT = { has_province_modifier = building_new_temple }
			}
		}

		allow = {
			is_occupied = no

			location = {
				religion = FROM
				culture = FROM
				num_of_empty_holdings >= 1
			}

			FROM = {
				war = no
				wealth >= 100

				custom_tooltip = {
					text = NOT_BUILDING_TEMPLE

					NOT = {
						any_demesne_province = {
							has_province_modifier = building_new_temple
							owned_by = PREV
						}
					}
				}

				custom_tooltip = {
					text = HAS_POSSIBLE_TEMPLE_BUILDER

					any_courtier = {
						is_adult = yes
						religion = PREV
						culture = PREV
						health >= 4
						prisoner = no
						is_ruler = no
						is_incapable = no

						OR = {
							trait = detached_priest
							trait = dutiful_cleric
							trait = scholarly_theologian
							trait = mastermind_theologian

							AND = {
								learning >= 8
								trait = zealous
							}
						}

						trigger_if = {
							limit = { is_female = yes }
							religion_allows_female_temple_holders = yes
						}
						trigger_else = {
							religion_allows_male_temple_holders = yes
						}

						trigger_if = {
							limit = { is_lowborn = yes }

							OR = {
								trait = ambitious
								trait = zealous
							}
						}

						NOR = {
							trait = cynical
							trait = content
							trait = stressed
							trait = depressed
							trait = imbecile
							any_heir_title = { always = yes }
							has_character_modifier = planning_new_temple
							has_character_modifier = planning_claimant_adventure
							has_character_modifier = planning_duchy_adventure
							has_character_flag = claimant_adventurer
						}
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				wealth = -100
				piety = 25

				character_event = {
					id = Plus.1422
					tooltip = FIND_TEMPLE_BUILDER
				}
			}

			hidden_effect = {
				location = {
					set_province_flag = building_new_temple_check

					add_province_modifier = {
						name = building_new_temple
						duration = -1
					}
				}
			}
		}

		ai_will_do = {
			factor = 0.01

			trigger = {
				FROM = {
					wealth >= 200
					NOT = { has_character_modifier = ai_austerity }
				}
			}

			mult_modifier = {
				factor = 3
				location = { is_capital = yes }
			}
		}
	}

	establish_tribe = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_tribal = yes
		}

		potential = {
			real_tier = COUNT

			location = {
				num_of_empty_holdings >= 1
				num_of_settlements < 1

				NOR = {
					has_province_modifier = building_new_city
					has_province_modifier = building_new_temple
				}
			}
		}

		allow = {
			is_occupied = no

			location = {
				num_of_settlements < 1
			}

			FROM = {
				prestige >= 100
				war = no
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				prestige = -100
			}

			location = {
				show_scope_change = no

				build_holding = { type = tribal }

				hidden_effect = {
					any_province_holding = { make_capital_holding = yes }
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	establish_castle = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_tribal = no
			is_nomadic = no
		}

		potential = {
			real_tier = COUNT

			location = {
				num_of_empty_holdings >= 1
				num_of_settlements < 1

				NOR = {
					has_province_modifier = building_new_city
					has_province_modifier = building_new_temple
				}
			}
		}

		allow = {
			is_occupied = no

			location = {
				num_of_settlements < 1
			}

			FROM = {
				wealth >= 100
				war = no
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				wealth = -100
			}

			location = {
				show_scope_change = no

				build_holding = { type = castle }

				hidden_effect = {
					any_province_holding = { make_capital_holding = yes }
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					NOT = { has_character_modifier = ai_austerity }
				}
			}
		}
	}

	constantinople_emperor_revoke = {
		filter = vassal_owned
		ai_target_filter = vassal_owned
		ai_check_interval = 12
		is_high_prio = yes

		only_independent = yes

		from_potential = {
			has_landed_title = e_byzantium
			e_byzantium = { is_primary_holder_title = yes }
			is_orthodox_branch = yes
		}

		potential = {
			trigger_if = {
				limit = { title = b_hagiasophia }
				holder_scope = { NOT = { religion = FROM } }
			}
			trigger_else = {
				title = c_constantinopolis
				NOT = { holder = FROM }
			}
		}

		allow = {
			FROM = {
				is_not_in_regency_trigger = yes
			}
		}

		effect = {
			usurp_title = FROM
		}

		ai_will_do = {
			factor = 1
		}
	}

	raise_flag = { # Flag only the county capital
		filter = owned

		ai = no

		potential = {
			holder = FROM
			real_tier = COUNT

			holder_scope = {
				has_character_modifier = nhv_explained
				NOT = { has_character_modifier = nhv_shut_off }
			}

			location = {
				NOT = { has_province_flag = keep_it }
			}
		}

		effect = {
			location = {
				set_province_flag = keep_it
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	raise_all_flag = { # Flag everything owned in the county
		filter = owned

		ai = no

		potential = {
			holder = FROM
			real_tier = COUNT

			holder_scope = {
				has_character_modifier = nhv_explained
				NOT = { has_character_modifier = nhv_shut_off }
			}

			location = {
				NOT = { has_province_flag = keep_it }
			}
		}

		effect = {
			location = {
				set_province_flag = keep_it
				set_province_flag = keep_baronies
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	lower_flag = { # Unflag the county
		filter = owned

		ai = no

		potential = {
			holder = FROM
			real_tier = COUNT

			holder_scope = {
				has_character_modifier = nhv_explained
				NOT = { has_character_modifier = nhv_shut_off }
			}

			location = {
				has_province_flag = keep_it
			}
		}

		effect = {
			location = {
				clr_province_flag = keep_it
				clr_province_flag = keep_baronies
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	scourge_province = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_unreformed_pagan_trigger = yes
			pacifist = no
		}

		potential = {
			real_tier = COUNT
			location = {
				NOT = { religion = FROM }
			}
		}

		allow = {
			is_occupied = no

			location = {
				NOT = { has_province_modifier = peasant_unrest }
			}

			FROM = {
				prestige >= 100
			}
		}

		effect = {
			location = {
				province_event = { id = Plus.960 }
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				FROM = {
					trait = cruel
					trait = zealous
					NOT = { religion = tengri_pagan }
				}
			}
		}
	}

	reset_title_coa = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12

		only_playable = yes

		from_potential = {
			is_nomadic = no
		}

		potential = {
			holder = FROM
			real_tier = DUKE
			is_titular = no
			has_title_flag = custom_changed_coa
		}

		allow = {
			FROM = {
				war = no
				is_inaccessible_trigger = no
				is_not_in_regency_trigger = yes
			}
		}

		effect = {
			clr_title_flag = custom_changed_coa
			reset_coa = THIS

			if = {
				limit = { capital_scope = { region = custom_england } }

				if = {
					limit = { has_global_flag = england_is_saxon }

					trigger_switch = {
						on_trigger = title

						d_kent = { set_coa = d_kent_saxon }
						d_cornwall = { set_coa = d_cornwall_saxon }
						d_hwicce = { set_coa = d_hwicce_saxon }
						d_mercia = { set_coa = d_mercia_saxon }
						d_lancaster = { set_coa = d_lancaster_saxon }
						d_east_anglia = { set_coa = d_east_anglia_saxon }
						# d_oxford = { set_coa = d_oxford_saxon }
						d_wessex = { set_coa = d_wessex_saxon }
						d_york = { set_coa = d_york_saxon }
					}
				}
				else_if = {
					limit = { has_global_flag = england_is_norman }

					trigger_switch = {
						on_trigger = title

						d_kent = { set_coa = d_kent_norman }
						d_cornwall = { set_coa = d_cornwall_norman }
						d_hwicce = { set_coa = d_hwicce_norman }
						d_mercia = { set_coa = d_mercia_norman }
						d_lancaster = { set_coa = d_lancaster_norman }
						d_east_anglia = { set_coa = d_east_anglia_norman }
						# d_oxford = { set_coa = d_oxford_norman }
						d_wessex = { set_coa = d_wessex_norman }
						d_york = { set_coa = d_york_norman }
					}
				}
			}
			else_if = {
				limit = {
					title = d_finland
					has_global_flag = finland_is_satakunta
				}

				set_coa = d_satakunta
			}
		}

		ai_will_do = {
			factor = 1
			# ai should always reset
		}
	}

	take_county_coa = {
		filter = owned
		ai_target_filter = owned

		ai = no # AI should never do this

		from_potential = {
			is_nomadic = no
		}

		potential = {
			real_tier = COUNT
			dejure_liege_title = { holder = FROM }
		}

		allow = {
			is_occupied = no

			location = {
				NOT = { has_province_modifier = peasant_unrest }
			}

			FROM = {
				war = no
				has_regent = no
				is_incapable = no
				is_inaccessible_trigger = no
			}
		}

		effect = {
			dejure_liege_title = {
				set_coa = ROOT
				set_title_flag = custom_changed_coa
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	# TODO: This and other decision/events/effects can likely be replaced with 'prompt_name'-effect
	rename_character_title = {
		filter = owned
		ai_target_filter = owned

		ai = no # AI should never do this

		from_potential = {
			has_character_flag = renaming_character
		}

		potential = {
			real_tier = DUKE
			has_title_flag = rename_character_duchy
		}

		effect = {
			FROM = {
				set_character_flag = changed_name_once
				clr_character_flag = renaming_character
				set_name = "[Prev.GetName]"
			}

			unsafe_destroy_landed_title = THIS
		}

		ai_will_do = {
			factor = 0
		}
	}
}