﻿######################################################################
# Government
######################################################################

building_requirement_tribal = {
	scope:holder ?= {
		government_has_flag = government_is_tribal
	}
}

building_requirement_wanua = {
	scope:holder ?= {
		government_has_flag = government_is_wanua
	}
}

building_requirement_non_tribal = {
	NOT = {
		scope:holder ?= {
			government_has_flag = government_is_tribal
		}
	}
}

building_requirement_tribal_holding_in_county = {
	NOT = {
		county = {
			any_county_province = {
				has_holding_type = tribal_holding
			}
		}
	}
}

building_requirement_nomad = {
	scope:holder ?= {
		government_has_flag = government_is_nomadic
	}
}

building_requirement_nomad_holding_in_county = {
	NOT = {
		county = {
			any_county_province = {
				has_holding_type = nomad_holding
			}
		}
	}
}

building_requirement_herder = {
	scope:holder ?= {
		government_has_flag = government_is_herder
	}
}

building_requirement_herder_holding_in_county = {
	NOT = {
		county = {
			any_county_province = {
				has_holding_type = herder_holding
			}
		}
	}
}

holding_is_tribal = {
	OR = {
		has_holding_type = tribal_holding
		has_holding_type = nomad_holding
		has_holding_type = herder_holding
	}
}

holding_is_nomad = {
	OR = {
		has_holding_type = nomad_holding
		has_holding_type = herder_holding
	}
}

holding_is_castle = {
	OR = {
		has_holding_type = castle_holding
		has_holding_type = temple_citadel_holding
	}
}
holding_is_church = {
	OR = {
		has_holding_type = church_holding
		has_holding_type = temple_citadel_holding
	}
}
holding_is_city = {
	has_holding_type = city_holding
}

holding_is_feudalized = {
	OR = {
		has_holding_type = castle_holding
		has_holding_type = city_holding
		has_holding_type = church_holding
		has_holding_type = temple_citadel_holding
	}
}

######################################################################
# Holding Level
######################################################################

building_requirement_castle_city_church = {
	trigger_if = {
		limit = { has_holding_type = castle_holding }
		has_building_or_higher = castle_$LEVEL$
	}
	trigger_else_if = {
		limit = { has_holding_type = city_holding }
		has_building_or_higher = city_$LEVEL$
	}
	trigger_else_if = {
		limit = { has_holding_type = church_holding }
		has_building_or_higher = temple_$LEVEL$
	}
	trigger_else_if = {
		limit = { has_holding_type = temple_citadel_holding }
		has_building_or_higher = temple_citadel_$LEVEL$
	}
	trigger_else = {
		custom_description = {
			text = "building_requirement_castle_city_church_failed"
			always = no
		}
	}
}

######################################################################
# Special
######################################################################

building_hall_of_heroes_requirement = {
	custom_description = {
		text = building_requirement_hall_of_heroes_failed
		has_variable = hall_of_heroes
		scope:holder = {
			OR = {
				has_religion = religion:baltic_religion
				has_religion = religion:slavic_religion
				has_religion = religion:uralic_religion
				has_religion = religion:siberian_religion
			}
		}
	}
}

building_university_requirement = {
	#has_variable = university
}

######################################################################
# Terrain
######################################################################

building_cereal_fields_requirement_terrain = {
	OR = {
		terrain = farmlands
		terrain = plains
		terrain = drylands
		AND = {
			terrain = floodplains
			NOR = {
				geographical_region = graphical_east_asia
				geographical_region = custom_viet_lands
			}
		}
		county = {
			OR = {
				has_county_modifier = fertile_desert_mountains_modifier
				has_county_modifier = dodekapolis_modifier
			}
		}
	}
}

has_any_special_mine_trigger = {
	has_building_with_flag = special_mine
	#OR = {
	#	has_building_or_higher = rammelsberg_mines_01
	#	has_building_or_higher = kutna_hora_mines_01
	#	has_building_or_higher = kremnica_mines_01
	#	has_building_or_higher = falun_mines_01
	#	has_building_or_higher = schwaz_mines_01
	#	has_building_or_higher = argentiera_mines_01
	#	has_building_or_higher = kollur_mines_01
	#	has_building_or_higher = siderokausia_mines_01
	#	has_building_or_higher = mali_mines_01
	#	has_building_or_higher = konasamudram_mines_01
	#	has_building_or_higher = dean_mines_01
	#	has_building_or_higher = phnom_dek_mines_01
	#	has_building_or_higher = zawar_mines_01
	#	has_building_or_higher = takkeda_mines_01
	#	has_building_or_higher = khetri_mines_01
	#	has_building_or_higher = taghaza_mines_01
	#	has_building_or_higher = ijil_mines_01
	#	has_building_or_higher = turda_mines_01
	#	has_building_or_higher = phocaea_mines_01
	#	has_building_or_higher = pansjhir_mines_01
	#	has_building_or_higher = trepca_mines_01
	#	has_building_or_higher = rudnik_mines_01
	#	has_building_or_higher = cevennes_mines_01
	#	has_building_or_higher = allaq_mines_01
	#	has_building_or_higher = verespatak_mines_01
	#	has_building_or_higher = nishapur_mines_01
	#	has_building_or_higher = srebrenica_mines_01
	#	has_building_or_higher = ratnapura_mines_01	
	#	has_building_or_higher = mogok_mines_01
	#}
}

# County scope. Pearl banks where fisheries can build Pearl Fisheries.
pearl_fishing_banks_county_trigger = {
	has_county_modifier = pearl_fishing_banks_modifier
}

# County scope. Walrus grounds where fish and furs districts can build Walrus Hunting Camps.
walrus_ivory_county_trigger = {
	has_county_modifier = walrus_ivory_modifier
}

# County scope. Naphta springs where resource districts can build Naphta Pits.
naphta_county_trigger = {
	has_county_modifier = naphta_modifier
}

######################################################################
# Baronies that cannot have a holding (intended for Sahara)
######################################################################

barony_cannot_construct_holding = {
	barony.title_province = {
		OR = {
			terrain = desert
			terrain = desert_mountains
		}
		# the corridors
		trigger_if = {
			limit = { geographical_region = world_africa_sahara }
			OR = {
				this = province:6650
				this = province:6651
				this = province:6652
				this = province:6653
				this = province:6654
				this = province:6655
				this = province:6656
				this = province:6657
				this = province:6658
				this = province:6659
				this = province:6660
				this = province:6661
				this = province:6662
				this = province:6663
				this = province:6664
				this = province:6665
				this = province:6666
				this = province:6667
				this = province:6668
			}
		}
		trigger_else = {
			always = no
		}
	}
}

######################################################################
# Triggers for add_random_building_effect script effect
######################################################################

# is any existing building of the checked tier? (e.g. 02)
has_any_building_of_level_trigger = {
	OR = {
		has_building = outposts_$BUILDING_LEVEL$
		has_building = logging_camps_$BUILDING_LEVEL$
		has_building = peat_quarries_$BUILDING_LEVEL$
		has_building = hill_forts_$BUILDING_LEVEL$
		has_building = plantations_$BUILDING_LEVEL$
		has_building = quarries_$BUILDING_LEVEL$
		has_building = hunting_grounds_$BUILDING_LEVEL$
		has_building = military_camps_$BUILDING_LEVEL$
		has_building = pastures_$BUILDING_LEVEL$
		has_building = ramparts_$BUILDING_LEVEL$
		has_building = curtain_walls_$BUILDING_LEVEL$
		has_building = watchtowers_$BUILDING_LEVEL$
		has_building = cereal_fields_$BUILDING_LEVEL$
		has_building = barracks_$BUILDING_LEVEL$
		has_building = camel_farms_$BUILDING_LEVEL$
		has_building = hill_farms_$BUILDING_LEVEL$
		has_building = elephant_pens_$BUILDING_LEVEL$
		has_building = common_tradeport_$BUILDING_LEVEL$
		has_building = orchards_$BUILDING_LEVEL$
		has_building = farm_estates_$BUILDING_LEVEL$
		has_building = regimental_grounds_$BUILDING_LEVEL$
	}
}

# is any existing building a lower tier than compare building?
has_lesser_building_trigger = {
	trigger_if = {
		limit = { has_building_or_higher = $COMPARE$_02 }
		has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
	}
	trigger_else_if = {
		limit = { has_building_or_higher = $COMPARE$_03 }
		OR = {
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 02 }
		}
	}
	trigger_else_if = {
		limit = { has_building_or_higher = $COMPARE$_04 }
		OR = {
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 02 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 03 }
		}
	}
	trigger_else_if = {
		limit = { has_building_or_higher = $COMPARE$_05 }
		OR = {
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 02 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 03 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 04 }
		}
	}
	trigger_else_if = {
		limit = { has_building_or_higher = $COMPARE$_06 }
		OR = {
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 02 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 03 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 04 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 05 }
		}
	}
	trigger_else_if = {
		limit = { has_building_or_higher = $COMPARE$_07 }
		OR = {
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 01 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 02 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 03 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 04 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 05 }
			has_any_building_of_level_trigger = { BUILDING_LEVEL = 06 }
		}
	}
	trigger_else = { always = no } # Must already be highest tier possible
}

######################################################################
# Mint unlock trigger. Province scope.
######################################################################
mint_building_unlock_early_medieval_trigger = {
	custom_description = {
		text = mint_building_unlock_early_medieval_trigger_desc
		culture = { has_innovation = innovation_currency_02 }
	}
}

islamic_special_building_trigger = {
	OR = {
		has_building = holy_site_great_mosque_of_djenne_01
		has_building = holy_site_great_mosque_of_samarra_01
		has_building = holy_site_prophetic_mosque_01
		has_building = hagia_sophia_02
		has_building = the_friday_mosque_01 # shared zoroastrian
		has_building = holy_site_great_mosque_of_cordoba_01 # shared christian
		has_building_or_higher = damascus_mosque_01
		has_building_or_higher = yazd_mosque_01
	}
}

christian_special_building_trigger = {
	OR = {
		has_building = holy_site_cologne_cathedral_01
		has_building = holy_site_canterbury_cathedral_01
		has_building = holy_site_canterbury_cathedral_02
		has_building = holy_site_canterbury_cathedral_03
		has_building = temple_of_uppsala_01
		has_building = temple_of_uppsala_02
		has_building = lund_cathedral_01
		has_building = holy_site_great_mosque_of_cordoba_01 # shared muslim
		has_building_or_higher = cluny_abbey_01
		has_building_or_higher = beta_giyorgis_01
		has_building_or_higher = holy_wisdom_01
		has_building_or_higher = duomo_florence_01
		has_building_or_higher = wawel_cathedral_01
		has_building_or_higher = holy_site_st_peters_basilica_01
		has_building_or_higher = mont_st_michel_01
	}
}

eastern_special_building_trigger = {
	OR = {
		has_building = buddhas_of_bamian_01
		has_building = brihadeeswarar_temple_01
		has_building = shwedagon_pagoda_01
		has_building = ananda_temple_01
		has_building = khajuraho_01
		has_building = angkor_wat_01
		has_building = my_son_sanctuary_01
		has_building = stone_pagoda_01
		has_building = muara_takus_01
		has_building = three_pagodas_dali_01
		has_building = pyramid_lingapura_01
		has_building = buddha_kamakura_01
		has_building_or_higher = jokhang_01
		has_building_or_higher = konark_temple_01
		has_building_or_higher = vatapi_caves_01
		has_building_or_higher = sanchi_stupa_01
		has_building_or_higher = boudhanath_01
		has_building_or_higher = temples_angkor_01
	}
}

# Religious special building triggers
religious_special_building_exclusion_trigger = { # checks if any special buildings present do not fit your religion
	OR = {
		AND = { # Generic
			NOT = {
				barony = { is_holy_site_of = holder.faith }
			}
			OR = {
				has_building = holy_site_cathedral_01
				has_building = holy_site_mosque_01
				has_building = holy_site_pagan_grand_temple_01
				has_building = holy_site_indian_grand_temple_01
				has_building = holy_site_other_grand_temple_01
				has_building = hagia_sophia_01
				has_building = holy_site_mahabodhi_temple_01
				has_building = holy_site_imam_ali_mosque_01
				has_building = holy_site_great_mosque_of_mecca_01
				islamic_special_building_trigger = yes
				christian_special_building_trigger = yes
				has_building = holy_site_buddhist_grand_temple_01
				has_building = holy_site_japanese_temple_01
				has_building = holy_site_chinese_temple_01
				has_building = holy_site_se_asia_pagan_temple_01
				has_building = izumo_taisha_01
			}
		}
		AND = { # Islamic
			NOT = { barony.holder.religion = religion:islam_religion }
			islamic_special_building_trigger = yes
		}
		AND = { # Christian
			NOR = { 
				barony.holder.religion = religion:eastern_orthodox_religion 
				barony.holder.religion = religion:catholic_religion
				barony.holder.religion = religion:protestant_religion
				barony.holder.religion = religion:oriental_orthodox_religion 
			}
			christian_special_building_trigger = yes
		}
		AND = { # Islamic/Zoroastrian
			NOT = { barony.holder.religion = religion:zoroastrianism_religion }
			has_building = the_friday_mosque_01 # shared muslim
		}
		AND = { # Eastern family
			NOR = {
				barony.holder.religion = religion:hinduism_religion
				barony.holder.religion = religion:buddhism_religion
				barony.holder.religion = religion:jainism_religion
				barony.holder.religion = religion:taoism_religion
				barony.holder.religion = religion:confucianism_religion
				barony.holder.religion = religion:zoroastrianism_religion
			}
			eastern_special_building_trigger = yes
		}
		# Judaism
		AND = {
			barony.holder.religion = religion:judaism_religion
			has_building = dome_of_the_rock_01
		}
		AND = {
			NOT = { barony.holder.religion = religion:judaism_religion }
			has_building = temple_in_jerusalem_01
		}
	}
}

building_murex_fisheries_requirement_trigger = {
	is_coastal = yes
	geographical_region = mediteranean_coast
}

building_examination_hall_requirement = {
	is_county_capital = yes

	scope:holder = {
		government_has_flag = government_has_merit
	}
}

building_manor_tower_requirement = {
	OR = {
		has_holding_type = castle_holding
		is_county_capital = yes
	}
}

# Keeps the Governance Quarter slot free for the Construct Great Barracks great project
building_requirement_no_great_barracks_project = {
	custom_tooltip = {
		text = great_project_type_construct_great_barracks_reserves_slot
		NOT = {
			any_great_project_in_province = {
				great_project_type = construct_great_barracks
				OR = {
					great_project_construction_phase_is = planned
					great_project_construction_phase_is = in_progress
				}
			}
		}
	}
}

# Gunpowder buildings; either innovation unlocks them
building_gunpowder_trigger = {
	culture = {
		OR = {
			has_innovation = innovation_gunpowder
			has_innovation = innovation_fire_medicine
		}
	}
}

######################################################################
# Duchy capital building trigger
######################################################################
can_have_duchy_capital_building_trigger = {
	county.holder = {
		trigger_if = {
			limit = {
				government_has_flag = government_is_republic
			}
			capital_county = prev.county
			highest_held_title_tier >= tier_duchy
		}
		trigger_else = {
			has_title = prev.duchy
		}
	}
}

building_requirement_meat_allowed = {
	NOT = { culture = { has_cultural_parameter = vegetarianism_no_meat_buildings } }
	NOT = { rite = { rite_has_parameter = meat_forbidden } }
}

building_requirement_winter_region = {
	NOR = {
		terrain = desert
		terrain = oasis
		terrain = drylands
		terrain = desert_mountains
	}
	OR = {
		geographical_region = world_europe_north
		geographical_region = world_europe_west
		geographical_region = world_europe_east
		geographical_region = world_steppe
		geographical_region = world_tibet
		geographical_region = world_asia_china
		geographical_region = world_asia_japan
		geographical_region = world_asia_sakhalin_hokkaido
		geographical_region = world_asia_korea
		geographical_region = world_asia_north_east
	}
}

building_requirement_pasta_region = {
	OR = {
		geographical_region = world_africa_north
		geographical_region = world_europe_south_italy
	}
}

######################################################################
# Per-domain building unlock triggers. Province scope, no era fallback.
######################################################################

building_farmed_tribal_trigger = {
	culture = { has_innovation = innovation_crop_rotation }
}
building_farmed_early_medieval_trigger = {
	culture = { has_innovation = innovation_heavy_plow }
}
building_farmed_high_medieval_trigger = {
	culture = { has_innovation = innovation_windmills }
}
building_farmed_late_medieval_trigger = {
	culture = { has_innovation = innovation_putting_out_system }
}

building_artisanal_early_medieval_trigger = {
	culture = { has_innovation = innovation_craft_ordinances }
}
building_artisanal_high_medieval_trigger = {
	culture = { has_innovation = innovation_windmills }
}
building_artisanal_late_medieval_trigger = {
	culture = { has_innovation = innovation_putting_out_system }
}

# Forest goods resource districts
building_forest_early_medieval_trigger = {
	culture = { has_innovation = innovation_coppicing }
}
building_forest_high_medieval_trigger = {
	culture = { has_innovation = innovation_windmills }
}
building_forest_late_medieval_trigger = {
	culture = { has_innovation = innovation_putting_out_system }
}

# Mineral resource districts
building_mineral_early_medieval_trigger = {
	culture = { has_innovation = innovation_prospecting }
}
building_mineral_high_medieval_trigger = {
	culture = { has_innovation = innovation_windmills }
}
building_mineral_late_medieval_trigger = {
	culture = { has_innovation = innovation_deep_mining }
}

# Maritime: the Fishery resource district and the Port district
building_maritime_early_medieval_trigger = {
	culture = { has_innovation = innovation_armilary_sphere }
}
building_maritime_high_medieval_trigger = {
	culture = { has_innovation = innovation_merchant_law }
}
building_maritime_late_medieval_trigger = {
	culture = { has_innovation = innovation_pilot_books }
}

building_rural_early_medieval_trigger = {
	culture = { has_innovation = innovation_manorialism }
}
building_rural_high_medieval_trigger = {
	culture = { has_innovation = innovation_land_grants }
}
building_rural_late_medieval_trigger = {
	culture = { has_innovation = innovation_agronomy }
}

building_communal_early_medieval_trigger = {
	culture = { has_innovation = innovation_prosperity_02 }
}
building_urban_high_medieval_trigger = {
	culture = { has_innovation = innovation_prosperity_03 }
}
building_masonry_late_medieval_trigger = {
	culture = { has_innovation = innovation_bricks }
}

building_commerce_early_medieval_trigger = {
	culture = { has_innovation = innovation_currency_02 }
}
building_commerce_high_medieval_trigger = {
	culture = { has_innovation = innovation_merchant_law }
}
building_commerce_late_medieval_trigger = {
	culture = { has_innovation = innovation_currency_04 }
}

building_administrative_early_medieval_trigger = {
	culture = { has_innovation = innovation_baliffs }
}
building_administrative_high_medieval_trigger = {
	culture = { has_innovation = innovation_divine_right }
}
building_administrative_late_medieval_trigger = {
	culture = { has_innovation = innovation_court_officials }
}

building_religious_early_medieval_trigger = {
	culture = { has_innovation = innovation_monasticism }
}
building_stonework_high_medieval_trigger = {
	culture = { has_innovation = innovation_vaulting }
}

# Fortification districts and castle holdings
building_fortification_tribal_trigger = {
	culture = { has_innovation = innovation_motte }
}
building_fortification_early_medieval_trigger = {
	culture = { has_innovation = innovation_battlements }
}
building_fortification_high_medieval_trigger = {
	culture = { has_innovation = innovation_hoardings }
}
building_fortification_late_medieval_trigger = {
	culture = { has_innovation = innovation_machicolations }
}

# Recruitment: military districts and knights
building_recruitment_tribal_trigger = {
	culture = { has_innovation = innovation_barracks }
}
building_recruitment_early_medieval_trigger = {
	culture = { has_innovation = innovation_house_soldiers }
}
building_recruitment_late_medieval_trigger = {
	culture = { has_innovation = innovation_standing_armies }
}

building_courtly_tribal_trigger = {
	culture = { has_innovation = innovation_plenary_assemblies }
}
building_courtly_early_medieval_trigger = {
	culture = { has_innovation = innovation_royal_prerogative }
}
building_courtly_late_medieval_trigger = {
	culture = { has_innovation = innovation_ermine_cloaks }
}

# Settlement primaries
building_settlement_tribal_trigger = {
	culture = { has_innovation = innovation_city_planning }
}

# Holding district primaries, unlocked by culture era instead of innovation
building_holding_early_medieval_trigger = {
	culture = { has_cultural_era_or_later = culture_era_early_medieval }
}
building_holding_high_medieval_trigger = {
	culture = { has_cultural_era_or_later = culture_era_high_medieval }
}
building_holding_late_medieval_trigger = {
	culture = { has_cultural_era_or_later = culture_era_late_medieval }
}

# Historical special buildings, for the first tier of anything not already standing in 867
building_monument_tribal_trigger = {
	culture = { has_innovation = innovation_prosperity_01 }
}
