# 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
		
		from_potential = {
			is_feudal = yes
		}
		potential = {
			tier = COUNT
			location = {
				num_of_empty_holdings = 1
				has_province_modifier = new_buildings_timer
				NOT = { has_province_modifier = building_new_city }
				NOT = { any_province_holding = { holding_type = city } }
			}
		}
		allow = {
			is_occupied = no
			location = {
				culture = FROM
				num_of_empty_holdings = 1
			}
			FROM = {
				war = no
				wealth = 100
				custom_tooltip = {
					text = NOT_BUILDING_CITY
					hidden_tooltip = {
						NOT = {
							any_demesne_province = { has_province_modifier = building_new_city }
						}
					}
				}
			}
		}
		effect = {
			FROM = {
				wealth = -100
			}
			location = {
				custom_tooltip = {
					text = CITY_CONSTRUCTION_BEGINS
					hidden_tooltip = {
						set_province_flag = tribal_build_city
						add_province_modifier = {
							name = building_new_city
							duration = -1
						}
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 5
				location = { is_capital = yes }
			}
			modifier = {
				factor = 3
				location = {
					OR = {
						has_building = tb_market_town_3
						has_building = tb_market_town_4
					}
				}
			}
			modifier = {
				factor = 2
				location = {
					OR = {
						has_building = tb_market_town_1
						has_building = tb_market_town_2
					}
				}
			}
			modifier = {
				factor = 1.5
				location = {
					OR = {
						has_building = tb_shipyard_1
						has_building = tb_shipyard_2
						has_building = tb_shipyard_3
						has_building = tb_shipyard_4
					}
				}
			}
			modifier = {
				factor = 0
				FROM = { has_character_modifier = ai_austerity }
			}
		}
	}

	tribal_build_temple = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		
		from_potential = {
			is_feudal = yes
		}
		potential = {
			tier = COUNT
			location = {
				num_of_empty_holdings = 1
				has_province_modifier = new_buildings_timer
				NOT = { has_province_modifier = building_new_temple }
				NOT = { any_province_holding = { holding_type = 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
					hidden_tooltip = {
						NOT = {
							any_demesne_province = { has_province_modifier = building_new_temple }
						}
					}
				}
				custom_tooltip = {
					text = HAS_POSSIBLE_TEMPLE_BUILDER
					hidden_tooltip = {
						any_courtier = {
							is_adult = yes
							prisoner = no
							religion = PREV
							culture = PREV
							is_ruler = no
							health = 4
							OR = {
								trait = detached_priest
								trait = martial_cleric
								trait = scholarly_theologian
								trait = mastermind_theologian
								AND = {
									learning = 8
									trait = zealous
								}
							}
							OR = {
								is_female = no
								religion_allows_female_temple_holders = yes
							}
							OR = {
								NOT = { dynasty = 0 }
								trait = ambitious
								trait = zealous
							}
							NOR = {
								trait = incapable
								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 = {
				wealth = -100
				piety = 25
				character_event = { id = Plus.1422 tooltip = FIND_TEMPLE_BUILDER }
			}
			hidden_tooltip = {
				location = {
					set_province_flag = building_new_temple_check
					add_province_modifier = {
						name = building_new_temple
						duration = -1
					}
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.01
				always = yes
			}
			modifier = {
				factor = 3
				location = { is_capital = yes }
			}
			modifier = {
				factor = 0
				NOT = { FROM = { wealth = 200 } }
			}
			modifier = {
				factor = 0
				FROM = { has_character_modifier = ai_austerity }
			}
		}
	}

	establish_tribe = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		
		from_potential = {
			is_tribal = yes
		}
		potential = {
			tier = COUNT
			location = {
				num_of_empty_holdings = 1
				NOT = { num_of_settlements = 1 }
				NOT = { has_province_modifier = building_new_city }
				NOT = { has_province_modifier = building_new_temple }
			}
		}
		allow = {
			is_occupied = no
			location = {
				NOT = { num_of_settlements = 1 }
			}
			FROM = {
				war = no
				prestige = 100
			}
		}
		effect = {
			FROM = {
				prestige = -100
			}
			location = {
				build_holding = { type = tribal }
				hidden_tooltip = {
					any_province_holding = { make_capital_holding = yes }
				}
			}
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	establish_castle = {
		filter = owned
		ai_target_filter = owned
		ai_check_interval = 12
		
		from_potential = {
			is_tribal = no
			is_nomadic = no
		}
		potential = {
			tier = COUNT
			location = {
				num_of_empty_holdings = 1
				NOT = { num_of_settlements = 1 }
				NOT = { has_province_modifier = building_new_city }
				NOT = { has_province_modifier = building_new_temple }
			}
		}
		allow = {
			is_occupied = no
			location = {
				NOT = { num_of_settlements = 1 }
			}
			FROM = {
				war = no
				wealth = 100
			}
		}
		effect = {
			FROM = {
				wealth = -100
			}
			location = {
				build_holding = { type = castle }
				hidden_tooltip = {
					any_province_holding = { make_capital_holding = yes }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0
				FROM = { has_character_modifier = ai_austerity }
			}
		}
	}
	
	constantinople_emperor_revoke = {
		filter = vassal_owned
		ai_target_filter = vassal_owned
		ai_check_interval = 12
		is_high_prio = yes
		
		from_potential = {
			has_landed_title = e_byzantium
			e_byzantium = { is_primary_holder_title = yes }
			OR = {
				religion = chalcedonian
				religion = orthodox
				religion = bogomilist
				religion = monothelite
				religion = iconoclast
			}
		}
		potential = {
			OR = {
				AND = {
					title = b_hagiasophia
					holder_scope = { NOT = { religion = FROM } }
				}
				AND = {
					title = c_byzantion
					NOT = { holder = FROM }
				}
			}
		}
		allow = {
			FROM = { has_regent = no }
		}
		effect = {
			usurp_title = FROM
		}
		ai_will_do = {
			factor = 1
		}
	}
	
    raise_flag = { # Flag only the county capital
        filter = realm_owned
       
        from_potential = {
            ai = no
        }
        potential = {
            holder_scope = {
                has_character_modifier = nhv_explained
                NOT = { has_character_modifier = nhv_shut_off }
            }
            owner = { character = FROM }
            hidden_tooltip = {
                location = {
                    NOT = { has_province_flag = keep_it }
                }
            }
            real_tier = COUNT
        }
        allow = {
            always = yes
        }
        effect = {
            location = {
                set_province_flag = keep_it
            }
        }
        ai_will_do = {
            factor = 0
        }
    }
	
	raise_all_flag = { # Flag everything owned in the county
        filter = realm_owned
       
        from_potential = {
            ai = no
        }
        potential = {
            holder_scope = {
                has_character_modifier = nhv_explained
                NOT = { has_character_modifier = nhv_shut_off }
            }
            owner = { character = FROM }
            hidden_tooltip = {
                location = {
                    NOT = { has_province_flag = keep_it }
                }
            }
            real_tier = COUNT
        }
        allow = {
            always = yes
        }
        effect = {
            location = {
                set_province_flag = keep_it
                set_province_flag = keep_baronies
            }
        }
        ai_will_do = {
            factor = 0
        }
    }	

    lower_flag = { # Unflag the county
        filter = realm_owned
       
        from_potential = {
            ai = no
        }
        potential = {
            holder_scope = {
                has_character_modifier = nhv_explained
                NOT = { has_character_modifier = nhv_shut_off }
            }
            owner = { character = FROM }
            hidden_tooltip = {
                location = {
                    has_province_flag = keep_it
                }
            }
            real_tier = COUNT
        }
        allow = {
            always = yes
        }
        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
		
		from_potential = {
			is_pagan_religion = yes
			is_reformed_pagan_religion = no
			pacifist = no
		}
		potential = {
			tier = COUNT
			location = {
				NOT = { religion = FROM }
			}
		}
		allow = {
			is_occupied = no
			location = {
				NOT = { has_province_modifier = peasant_unrest }
			}
			FROM = {
				piety = 100
			}
		}
		effect = {
			hidden_tooltip = {
				location = {
					province_event = { id = Plus.960 }
				}
			}
		}
		ai_will_do = {
			factor = 1
			modifier = {
				factor = 0.5
				always = yes
			}
			modifier = {
				factor = 0
				FROM = { religion = tengri_pagan }
			}
			modifier = {
				factor = 0
				FROM = {
					NAND = {
						trait = cruel
						trait = zealous
					}
				}
			}
		}
	}
	
	reset_title_coa = {
		filter = realm_owned
		ai_target_filter = realm_owned
		ai_check_interval = 12
		
		from_potential = {
			is_nomadic = no
		}
		potential = {
			tier = DUKE
			is_titular = no
			has_title_flag = custom_changed_coa
			owner = { character = FROM }
		}
		allow = {
			FROM = {
				war = no
				is_not_busy_trigger = yes
				has_regent = no
				is_incapable = no
			}
		}
		effect = {
			clr_title_flag = custom_changed_coa
			reset_coa = THIS
			if = {
				limit = {
					title = d_canterbury
					k_england = { has_title_flag = england_is_saxon }
				}
				d_canterbury = { set_coa = d_canterbury_saxon }
			}
			if = {
				limit = {
					title = d_cornwall
					k_england = { has_title_flag = england_is_saxon }
				}
				d_cornwall = { set_coa = d_cornwall_saxon }
			}
			if = {
				limit = {
					title = d_gloucester
					k_england = { has_title_flag = england_is_saxon }
				}
				d_gloucester = { set_coa = d_gloucester_saxon }
			}
			if = {
				limit = {
					title = d_hereford
					k_england = { has_title_flag = england_is_saxon }
				}
				d_hereford = { set_coa = d_hereford_saxon }
			}
			if = {
				limit = {
					title = d_lancaster
					k_england = { has_title_flag = england_is_saxon }
				}
				d_lancaster = { set_coa = d_lancaster_saxon }
			}
			if = {
				limit = {
					title = d_norfolk
					k_england = { has_title_flag = england_is_saxon }
				}
				d_norfolk = { set_coa = d_norfolk_saxon }
			}
			if = {
				limit = {
					title = d_oxford
					k_england = { has_title_flag = england_is_saxon }
				}
				d_oxford = { set_coa = d_oxford_saxon }
			}
			if = {
				limit = {
					title = d_somerset
					k_england = { has_title_flag = england_is_saxon }
				}
				d_somerset = { set_coa = d_somerset_saxon }
			}
			if = {
				limit = {
					title = d_york
					k_england = { has_title_flag = england_is_saxon }
				}
				d_york = { set_coa = d_york_saxon }
			}
			if = {
				limit = {
					title = d_canterbury
					k_england = { has_title_flag = england_is_norman }
				}
				d_canterbury = { set_coa = d_canterbury_norman }
			}
			if = {
				limit = {
					title = d_cornwall
					k_england = { has_title_flag = england_is_norman }
				}
				d_cornwall = { set_coa = d_cornwall_norman }
			}
			if = {
				limit = {
					title = d_gloucester
					k_england = { has_title_flag = england_is_norman }
				}
				d_gloucester = { set_coa = d_gloucester_norman }
			}
			if = {
				limit = {
					title = d_hereford
					k_england = { has_title_flag = england_is_norman }
				}
				d_hereford = { set_coa = d_hereford_norman }
			}
			if = {
				limit = {
					title = d_lancaster
					k_england = { has_title_flag = england_is_norman }
				}
				d_lancaster = { set_coa = d_lancaster_norman }
			}
			if = {
				limit = {
					title = d_norfolk
					k_england = { has_title_flag = england_is_norman }
				}
				d_norfolk = { set_coa = d_norfolk_norman }
			}
			if = {
				limit = {
					title = d_oxford
					k_england = { has_title_flag = england_is_norman }
				}
				d_oxford = { set_coa = d_oxford_norman }
			}
			if = {
				limit = {
					title = d_somerset
					k_england = { has_title_flag = england_is_norman }
				}
				d_somerset = { set_coa = d_somerset_norman }
			}
			if = {
				limit = {
					title = d_york
					k_england = { has_title_flag = england_is_norman }
				}
				d_york = { set_coa = d_york_norman }
			}
			if = {
				limit = {
					d_finland = { has_title_flag = finland_is_satakunta }
					title = d_finland
				}
				d_finland = { set_coa = d_satakunta }
			}
		}
		ai_will_do = {
			factor = 1
			# ai should always reset
		}
	}
	
	take_county_coa = {
		filter = owned
		ai_target_filter = owned
		
		from_potential = {
			is_nomadic = no
		}
		potential = {
			tier = COUNT
			dejure_liege_title = { owner = { character = FROM } }
		}
		allow = {
			is_occupied = no
			location = {
				NOT = { has_province_modifier = peasant_unrest }
			}
			FROM = {
				war = no
				is_not_busy_trigger = yes
				has_regent = no
				is_incapable = no
			}
		}
		effect = {
			dejure_liege_title = {
				set_coa = ROOT
				set_title_flag = custom_changed_coa
			}
		}
		ai_will_do = {
			factor = 0
			# ai should never do this
		}
	}
	
	rename_character_title = {
		filter = owned
		ai_target_filter = owned
		
		from_potential = {
			ai = no
			has_character_flag = renaming_character
		}
		potential = {
			tier = DUKE
			has_title_flag = rename_character_duchy
		}
		allow = {
			
		}
		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
			# ai should never do this
		}
	}
}

