﻿######################################################################
# Government
######################################################################
 ### EK EDIT: EK Terrains added, triggers modified


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

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 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 = {
		custom_description = {
			text = "building_requirement_castle_city_church_failed"
			always = no
		}
	}
}

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

### EK EDIT: Hall of Heroes are disabled
building_hall_of_heroes_requirement = {
	always = no
	# 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:finno_ugric_religion
				# has_religion = religion:siberian_religion
			# }
		# }
	# }
}

### EK EDIT: Universities might be changed into big mages colleges at one point
building_university_requirement = {
	has_variable = university
}

######################################################################
# Terrain
######################################################################
### EK TODO: See what fits for Ashlands, Volcanic, Mangrove Forests, Valen Woods, Wastelands, 

building_common_tradeport_requirement_terrain = {
	OR = {
		is_coastal = yes
		is_riverside_province = yes
	}
}

building_pastures_requirement_terrain = {
	OR = {
		terrain = farmlands
		terrain = plains
		terrain = steppe
		terrain = desert
		terrain = drylands
		terrain = oasis
		terrain = mountains
		terrain = desert_mountains
		terrain = hills
		terrain = floodplains
		#EK ADDITION: our terrain types
		terrain = desert_hills
		terrain = valenwood_hills
		terrain = ashlands
		terrain = valenwood
		#EK EDIT: We don't have such a region
		# AND = {
			# terrain = jungle
			# NOT = { geographical_region = world_innovation_elephants }
		# }
	}
}
### EK EDIT: Added EK terrains to all the buildings below where relevant
building_hunting_grounds_requirement_terrain = {
	OR = {
		terrain = plains
		terrain = drylands
		terrain = steppe
		terrain = hills
		terrain = taiga
		terrain = forest
		terrain = jungle
		terrain = wetlands
		#EK ADDITION: our terrain types
		terrain = desert_hills
		terrain = tundra
		terrain = taiga_hills
		terrain = jungle_hills
		terrain = valenwood_hills
		terrain = valenwood
		terrain = mangrove_forest
		terrain = ashlands
		terrain = wasteland
		terrain = volcanic
		terrain = black_marsh
	}
}

building_orchards_requirement_terrain = {
	OR = {
		terrain = floodplains
		terrain = oasis
		has_building_or_higher = qanats_05
		AND = {
			culture = {
				has_cultural_parameter = vegetarian_fruit_orchard_allowed
			}
		}
	}
}

building_farm_estates_requirement_terrain = {
	terrain = farmlands
}

building_military_camps_requirement_terrain = {
	#OR = { # All Terrains
	#	terrain = farmlands
	#	terrain = plains
	#	terrain = drylands
	#	terrain = desert
	#	terrain = oasis
	#	terrain = floodplains
	#	terrain = steppe
	#	terrain = mountains
	#	terrain = desert_mountains
	#	terrain = wetlands
	#	terrain = forest
	#	terrain = taiga
	#	terrain = jungle
	#	terrain = hills
	#}
}

building_regimental_grounds_requirement_terrain = {
	OR = {
		terrain = floodplains
		terrain = farmlands
	}
}

building_ramparts_requirement_terrain = {
	OR = {
		terrain = taiga
		terrain = forest
		terrain = jungle
		# EK ADDITION: our terrain types
		terrain = valenwood
	}
}

building_curtain_walls_requirement_terrain = {
	OR = {
		terrain = farmlands
		terrain = plains
		terrain = wetlands
		terrain = floodplains
		#EK ADDITION: our terrain types
		terrain = mangrove_forest
		terrain = black_marsh
	}
}

building_watchtowers_requirement_terrain = {
	OR = {
		terrain = desert
		terrain = drylands
		terrain = oasis
		terrain = steppe
		#EK ADDITION: our terrain types
		terrain = ashlands
		terrain = wasteland
		terrain = tundra
	}
}

building_cereal_fields_requirement_terrain = {
	OR = {
		terrain = farmlands
		terrain = plains
		terrain = drylands
		terrain = floodplains
		county = {
			OR = {
				has_county_modifier = fertile_desert_mountains_modifier
				#EK DISABLED: Irl stuff
				#has_county_modifier = dodekapolis_modifier
			}
		}
		#EK DISABLED: we don't use qanats
		#has_building_or_higher = qanats_05
	}
}

building_outposts_requirement_terrain = {
	OR = {
		terrain = wetlands
		terrain = taiga
		terrain = forest
		terrain = jungle
		#EK ADDITION: our terrain types
		terrain = black_marsh
		terrain = tundra
		terrain = taiga_hills
		terrain = jungle_hills
		terrain = valenwood
		terrain = valenwood_hills
		terrain = ashlands
		terrain = wasteland
		terrain = volcanic
	}
}

building_barracks_requirement_terrain = {
	#OR = {
	#	terrain = farmlands
	#	terrain = plains
	#	terrain = drylands
	#	terrain = floodplains # new
	#	terrain = hills
	#	terrain = mountains
	#	terrain = forest
	#	terrain = taiga
	#	terrain = jungle
	#	terrain = wetlands
	#	terrain = steppe
	#}
}

building_camel_farms_requirement_terrain = {
	geographical_region = world_innovation_camels #EK EDIT: Hammerfell, Yokuda
	OR = {
		terrain = desert
		terrain = desert_mountains
		terrain = oasis
		terrain = floodplains
		terrain = drylands # new
		#EK ADDITION: our terrain types
		terrain = desert_hills
	}
}

building_stables_requirement_terrain = {
	# All terrains
}

building_smiths_requirement_terrain = {
	# All terrains
}

building_workshops_requirement_terrain = {
	# All terrains
}

building_logging_camps_requirement_terrain = {
	OR = {
		terrain = taiga
		terrain = forest
		terrain = jungle
		#EK ADDITION: our terrain types
		terrain = taiga_hills
		terrain = jungle_hills
		terrain = valenwood
		terrain = valenwood_hills
	}
}

building_peat_quarries_requirement_terrain = {
	OR = {
		terrain = wetlands
		#EK ADDITION: our terrain types
		terrain = black_marsh
		terrain = mangrove_forest
	}
}

building_hill_farms_requirement_terrain = {
	OR = {
		terrain = hills
		#EK ADDITION: our terrain types
		terrain = desert_hills
		terrain = taiga_hills
		terrain = jungle_hills
		terrain = valenwood_hills
	}
}

### EK EDIT: No such region
building_elephant_pens_requirement_terrain = {
	#terrain = jungle
	#OR = {
	#	geographical_region = world_innovation_elephants
	#	culture = { has_cultural_parameter = elephant_pens_building_bonuses }
	#}
	always = no #disabled for now, maybe refactor for mammoths later?
}

building_horse_pastures_requirement_terrain = {
	OR = {
		terrain = steppe
		#geographical_region = world_steppe_east #EK EDIT: no such region
		culture = { has_cultural_parameter = unlock_horse_pastures_building }
	}
}

building_plantations_requirement_terrain = {
	OR = {
		terrain = drylands
		terrain = desert
		terrain = oasis
		#EK ADDITION: our terrain types
		terrain = ashlands
	}
}

building_quarries_requirement_terrain = {
	OR = {
		terrain = mountains
		terrain = desert_mountains
		culture = { has_cultural_parameter = can_build_quarries_everywhere }
	}
}

building_warrior_lodges_requirement_terrain = {
	OR = {
		AND = {
			OR = {
				terrain = mountains
				terrain = desert_mountains
				terrain = hills
				#EK ADDITION: our terrain types
				terrain = desert_hills
				terrain = taiga_hills
				terrain = valenwood_hills
				terrain = jungle_hills
				terrain = volcanic
			}
			NOT = { geographical_region = world_horse_buildings_in_hills_and_mountains }
		}
		culture = { has_cultural_parameter = warrior_lodges_lift_restrictions }
	}
}

building_hillside_grazing_requirement_terrain = {
	OR = {
		terrain = mountains
		terrain = desert_mountains
		terrain = hills
		#EK ADDITION: our terrain types
		terrain = desert_hills
		terrain = taiga_hills
		terrain = valenwood_hills
		terrain = jungle_hills
		terrain = volcanic
	}
	geographical_region = world_horse_buildings_in_hills_and_mountains
}

building_wind_furnace_requirement_terrain = { #EK DISABLED: doesn't exist in TES
	always = no
# 	geographical_region = world_india
# 	OR = {
# 		is_coastal = yes
# 		AND = {
# 			scope:holder.top_liege = {
# 				any_realm_county = {
# 					any_county_province = {
# 						has_building_or_higher = konasamudram_mines_01
# 					}
# 				}
# 			}
# 			duchy = {
# 				any_de_jure_county = {
# 					any_county_province = {
# 						has_building_or_higher = konasamudram_mines_01
# 					}
# 				}
# 			}
# 		}
		
# 	}
	
#	scope:holder.culture = { has_innovation = innovation_wootz_steel }
}

building_hill_forts_requirement_terrain = {
	OR = {
		terrain = mountains
		terrain = desert_mountains
		terrain = hills
		#EK ADDITION: our terrain types
		terrain = desert_hills
		terrain = taiga_hills
		terrain = valenwood_hills
		terrain = jungle_hills
		terrain = volcanic
	}
}

building_windmills_requirement_terrain = {
	county ?= {
		any_county_province = {
			OR = {
				terrain = farmlands
				terrain = plains
				terrain = hills
				is_coastal = yes
			}
		}
	}
}

building_watermills_requirement_terrain = {
	county ?= {
		any_county_province = {
			OR = {
				terrain = mountains
				terrain = wetlands
				terrain = forest
				terrain = taiga
				terrain = jungle
				is_riverside_province = yes
			}
		}
	}
}

building_caravanserai_requirement_terrain = {
	county ?= {
		any_county_province = {
			OR = {
				terrain = drylands
				terrain = desert
				terrain = oasis
				terrain = floodplains
				terrain = steppe
				terrain = desert_mountains
				#AND = { #EK DISABLED: no such thing
					#	geographical_region = world_europe_west_iberia
					#	has_global_variable = fp2_struggle_conciliation_ending
				#}
			}
		}
	}
}

building_qanats_requirement_terrain = {
 	county ?= {
		any_county_province = {
			OR = {
				terrain = hills
				terrain = mountains
				terrain = desert_mountains
				terrain = desert
				terrain = drylands
			}
			culture = { has_cultural_parameter = unlocks_qanat_building }
		}
	}
}

building_hospices_requirement_terrain = {
	
}

has_any_special_mine_trigger = {
	has_building_with_flag = mining_building #EK EDIT
}

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

### EK TODO: Maybe use it for Alik'r, some wastelands??
barony_cannot_construct_holding = {
	always = no
	#barony.title_province = {
	#	OR = {
	#		terrain = desert
	#		terrain = desert_mountains
	#	}
	#	geographical_region = world_africa_sahara
	#}
}

######################################################################
# 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$
		has_building = monastic_schools_$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
}

# can this building be added?
add_random_building_trigger = { 
	building_$BUILDING$_requirement_terrain = yes # terrain fits building type
	trigger_if = { # if all slots are full, this building type exists already
		limit = { free_building_slots = 0 }
		has_building_or_higher = $BUILDING$_01
		NOT = {
			has_lesser_building_trigger = { COMPARE = $BUILDING$ }
		}
	}
	trigger_else = {
		NOT = { has_building_or_higher = $BUILDING$_01 }
	}
}

# Innovation and holding level triggers for most economic buildings
generic_economic_building_innovation_trigger = {
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_01 }
		scope:build_owner.culture = { has_innovation = innovation_crop_rotation }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_02 }
		building_requirement_castle_city_church = { LEVEL = 02 }
		scope:build_owner.culture = { has_innovation = innovation_manorialism }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_04 }
		building_requirement_castle_city_church = { LEVEL = 03 }
		scope:build_owner.culture = { has_innovation = innovation_guilds }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_06 }
		building_requirement_castle_city_church = { LEVEL = 04 }
		scope:build_owner.culture = { has_innovation = innovation_cranes }
	}
}

# Innovation and holding level triggers for most fortification buildings
generic_fortification_building_innovations_trigger = {
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_01 }
		scope:build_owner.culture = { has_innovation = innovation_motte }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_02 }
		building_requirement_castle_city_church = { LEVEL = 02 }
		scope:build_owner.culture = { has_innovation = innovation_battlements }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_04 }
		building_requirement_castle_city_church = { LEVEL = 03 }
		scope:build_owner.culture = { has_innovation = innovation_hoardings }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_06 }
		building_requirement_castle_city_church = { LEVEL = 04 }
		scope:build_owner.culture = { has_innovation = innovation_machicolations }
	}
}

# Innovation and holding level triggers for most recruitment buildings
generic_recruitment_building_innovations_trigger = {
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_01 }
		scope:build_owner.culture = { has_innovation = innovation_barracks }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_02 }
		building_requirement_castle_city_church = { LEVEL = 02 }
		scope:build_owner.culture = { has_innovation = innovation_burhs }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_04 }
		building_requirement_castle_city_church = { LEVEL = 03 }
		scope:build_owner.culture = { has_innovation = innovation_castle_baileys }
	}
	trigger_if = {
		limit = { has_building_or_higher = $BUILDING$_06 }
		building_requirement_castle_city_church = { LEVEL = 04 }
		scope:build_owner.culture = { has_innovation = innovation_royal_armory }
	}
}

### EK DISABLED: Removed references to vanilla buildings and religions
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
	# }
	always = no
}

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
	# }
	always = no
}

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
	# }
	always = no
}

### EK EDIT: Plugged our buildings and holy sites
# Religious special building triggers
religious_special_building_exclusion_trigger = { # checks if any special buildings present do not fit your religion
	OR = {
		# Unique holy sites
		has_building = temple_of_the_one_01
		has_building = solitude_holy_walls_02
		has_building = leki_blade_01
		has_building = sancre_tor_02
		has_building = cath_bedraud_01
		
		has_building = grand_cathedral_01
		has_building = daedric_shrine_01
		has_building = great_wyrd_tree_01
		has_building = dragon_temple_01
		has_building = holy_site_other_grand_temple_01
	}
}

#EK DISABLED: IRL stuff
building_murex_farm_requirement_terrain = {
	always = no
	#is_county_capital = yes
	#is_coastal = yes
	#geographical_region = mediteranean_coast
	#NOR = {
	#	this.barony = title:b_salmydessus
	#	this.barony = title:b_chelai
	#}
}

building_breweries_requirement = {
	OR = {
		AND = {
			has_building_or_higher = temple_$NUMBER$
			OR = {
				#EK EDIT: Disabling real-world religion.
				#faith = faith:catholic
				#EK EDIT END
				faith = { has_doctrine_parameter = allows_brewery }
			}
		}
		AND = {
			has_building_or_higher = city_$NUMBER$
			culture = { has_cultural_parameter = allows_brewery }
		}
	}
}
