#####################################################################
# EFFECT LIST
#####################################################################
# !!! Remember to add all new effects with a short description here !!!

#add_next_building_tier_effect
#add_random_building_effect
#add_random_building_variable_effect

#####################################################################
# EFFECTS
#####################################################################

# Builds or upgrades the specified building, e.g. outposts
add_next_building_tier_effect = {
	if = {
		limit = {
			NOT = { has_building_or_higher = $BUILDING$_01 }
		}
		add_building = $BUILDING$_01
	}
	else_if = {
		limit = { has_building = $BUILDING$_01 }
		add_building = $BUILDING$_02
	}
	else_if = {
		limit = { has_building = $BUILDING$_02 }
		add_building = $BUILDING$_03
	}
	else_if = {
		limit = { has_building = $BUILDING$_03 }
		add_building = $BUILDING$_04
	}
	else_if = {
		limit = { has_building = $BUILDING$_04 }
		add_building = $BUILDING$_05
	}
	else_if = {
		limit = { has_building = $BUILDING$_05 }
		add_building = $BUILDING$_06
	}
	else_if = {
		limit = { has_building = $BUILDING$_06 }
		add_building = $BUILDING$_07
	}
	else_if = {
		limit = { has_building = $BUILDING$_07 }
		add_building = $BUILDING$_08
	}
}

#### Create Random Building - 2 Part Effect ####

# creates a random building in a holding, limited by innovations and holding level, and weighted to cheapest/lowest level, excludes tribal holdings/buildings, only for use in castle/church/city baronies

# ensure the event/script where this is used checks that a building can be built, based on innovations and holding level, as if noe can be built this effect will do absolutely nada

# 1, sets variable based on fitness of possible buildings
add_random_building_variable_effect = { # Province scope
	province_owner = { save_scope_as = build_owner }
	province_owner = { save_scope_as = holder } #Since building requirements reference scope:holder.
	random_list = { # Which building?
		# Generic/Terrain
		10 = { # outposts : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = outposts } # Generic eligiblity
				generic_recruitment_building_innovations_trigger = { BUILDING = outposts }
			}
			set_variable = {
				name = random_building_variable
				value = 0
				days = 365
			}
		}
		10 = { # logging_camps : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = logging_camps }
				generic_economic_building_innovation_trigger = { BUILDING = logging_camps }
			}
			set_variable = {
				name = random_building_variable
				value = 1
				days = 365
			}
		}
		10 = { # peat_quarries : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = peat_quarries }
				generic_economic_building_innovation_trigger = { BUILDING = peat_quarries }
			}
			set_variable = {
				name = random_building_variable
				value = 2
				days = 365
			}
		}
		10 = { # hill_forts : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = hill_forts }
				generic_fortification_building_innovations_trigger = { BUILDING = hill_forts }
			}
			set_variable = {
				name = random_building_variable
				value = 3
				days = 365
			}
		}
		10 = { # plantations : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = plantations }
				generic_economic_building_innovation_trigger = { BUILDING = plantations }
			}
			set_variable = {
				name = random_building_variable
				value = 4
				days = 365
			}
		}
		10 = { # quarries : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = quarries }
				generic_economic_building_innovation_trigger = { BUILDING = quarries }
			}
			set_variable = {
				name = random_building_variable
				value = 5
				days = 365
			}
		}
		10 = { # hunting_grounds : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = hunting_grounds }
				generic_economic_building_innovation_trigger = { BUILDING = hunting_grounds }
			}
			set_variable = {
				name = random_building_variable
				value = 6
				days = 365
			}
		}
		10 = { # military_camps : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = military_camps }
				generic_recruitment_building_innovations_trigger = { BUILDING = military_camps }
			}
			set_variable = {
				name = random_building_variable
				value = 7
				days = 365
			}
		}
		10 = { # horse_pastures : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = horse_pastures }
				generic_recruitment_building_innovations_trigger = { BUILDING = horse_pastures }
			}
			set_variable = {
				name = random_building_variable
				value = 8
				days = 365
			}
		}
		10 = { # hillside_grazing : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = hillside_grazing }
				generic_recruitment_building_innovations_trigger = { BUILDING = hillside_grazing }
			}
			set_variable = {
				name = random_building_variable
				value = 9
				days = 365
			}
		}
		10 = { # warrior_lodges : Cheap
			trigger = {
				add_random_building_trigger = { BUILDING = warrior_lodges }
				generic_recruitment_building_innovations_trigger = { BUILDING = warrior_lodges }
			}
			set_variable = {
				name = random_building_variable
				value = 10
				days = 365
			}
		}
		2 = { # pastures : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = pastures }
				generic_economic_building_innovation_trigger = { BUILDING = pastures }
			}
			set_variable = {
				name = random_building_variable
				value = 11
				days = 365
			}
		}
		2 = { # ramparts : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = ramparts }
				generic_fortification_building_innovations_trigger = { BUILDING = ramparts }
			}
			set_variable = {
				name = random_building_variable
				value = 12
				days = 365
			}
		}
		2 = { # curtain_walls : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = curtain_walls }
				generic_fortification_building_innovations_trigger = { BUILDING = curtain_walls }
			}
			set_variable = {
				name = random_building_variable
				value = 13
				days = 365
			}
		}
		2 = { # watchtowers : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = watchtowers }
				generic_fortification_building_innovations_trigger = { BUILDING = watchtowers }
			}
			set_variable = {
				name = random_building_variable
				value = 14
				days = 365
			}
		}
		2 = { # cereal_fields : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = cereal_fields }
				generic_economic_building_innovation_trigger = { BUILDING = cereal_fields }
			}
			set_variable = {
				name = random_building_variable
				value = 15
				days = 365
			}
		}
		2 = { # barracks : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = barracks }
				generic_recruitment_building_innovations_trigger = { BUILDING = barracks }
			}
			set_variable = {
				name = random_building_variable
				value = 16
				days = 365
			}
		}
		2 = { # camel_farms : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = camel_farms }
				generic_recruitment_building_innovations_trigger = { BUILDING = camel_farms }
			}
			set_variable = {
				name = random_building_variable
				value = 17
				days = 365
			}
		}
		2 = { # hill_farms : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = hill_farms }
				generic_economic_building_innovation_trigger = { BUILDING = hill_farms }
			}
			set_variable = {
				name = random_building_variable
				value = 18
				days = 365
			}
		}
		2 = { # elephant_pens : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = elephant_pens }
				generic_economic_building_innovation_trigger = { BUILDING = elephant_pens }
			}
			set_variable = {
				name = random_building_variable
				value = 19
				days = 365
			}
		}
		2 = { # common_tradeport : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = common_tradeport }
				# Innovation triggers
				trigger_if = {
					limit = { has_building_or_higher = common_tradeport_01 }
					scope:build_owner.culture = {
						OR = {
							has_innovation = innovation_crop_rotation
							has_cultural_parameter = next_level_trade_ports
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = common_tradeport_02 }
					building_requirement_castle_city_church = { LEVEL = 02 }
					scope:build_owner.culture = {
						OR = {
							has_innovation = innovation_manorialism
							AND = {
								has_innovation = innovation_crop_rotation
								has_cultural_parameter = next_level_trade_ports
							}
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = common_tradeport_04 }
					building_requirement_castle_city_church = { LEVEL = 03 }
					scope:build_owner.culture = {
						OR = {
							has_innovation = innovation_windmills
							AND = {
								has_innovation = innovation_manorialism
								has_cultural_parameter = next_level_trade_ports
							}
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = common_tradeport_06 }
					building_requirement_castle_city_church = { LEVEL = 04 }
					scope:build_owner.culture = {
						OR = {
							has_innovation = innovation_cranes
							AND = {
								has_innovation = innovation_windmills
								has_cultural_parameter = next_level_trade_ports
							}
						}
					}
				}
			}
			set_variable = {
				name = random_building_variable
				value = 20
				days = 365
			}
		}
		2 = { # hospices : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = hospices }
				generic_economic_building_innovation_trigger = { BUILDING = hospices }
			}
			set_variable = {
				name = random_building_variable
				value = 21
				days = 365
			}
		}
		2 = { # qanats : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = qanats }
				building_requirement_castle_city_church = { LEVEL = 01 }
				trigger_if = {
					limit = {
						NOT = {
							has_building_or_higher = qanats_01
						}
					}
					scope:build_owner.culture = {
						has_cultural_parameter = unlocks_qanat_building
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = qanats_02
					}
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = qanats_04
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = qanats_06
					}
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 22
				days = 365
			}
		}
		2 = { # murex_farm : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = murex_farm }
				building_requirement_castle_city_church = { LEVEL = 01 }
				county = { NOT = { has_county_modifier = backwater_county_modifier } }
				trigger_if = {
					limit = {
						has_building_or_higher = murex_farm_02
					}
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = murex_farm_04
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = murex_farm_06
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 23
				days = 365
			}
		}
		2 = { # wind_furnace : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = wind_furnace }
				building_requirement_castle_city_church = { LEVEL = 01 }
				trigger_if = {
					limit = {
						has_building_or_higher = wind_furnace_01
					}
					scope:build_owner.culture = {
						has_innovation = innovation_barracks
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = wind_furnace_02
					}
					building_requirement_castle_city_church = { LEVEL = 02 }
					scope:build_owner.culture = {
						has_innovation = innovation_burhs
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = wind_furnace_04
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
					scope:build_owner.culture = {
						has_innovation = innovation_castle_baileys
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = wind_furnace_05
					}
					scope:build_owner.culture = {
						has_innovation = innovation_royal_armory
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = wind_furnace_06
					}
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 24
				days = 365
			}
		}
		2 = { # stables : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = stables }
				generic_recruitment_building_innovations_trigger = { BUILDING = stables }
			}
			set_variable = {
				name = random_building_variable
				value = 25
				days = 365
			}
		}
		2 = { # smiths : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = smiths }
				generic_recruitment_building_innovations_trigger = { BUILDING = smiths }
			}
			set_variable = {
				name = random_building_variable
				value = 26
				days = 365
			}
		}
		1 = { # orchards : Expensive
			trigger = {
				add_random_building_trigger = { BUILDING = orchards }
				generic_economic_building_innovation_trigger = { BUILDING = orchards }
			}
			set_variable = {
				name = random_building_variable
				value = 27
				days = 365
			}
		}
		1 = { # farm_estates : Expensive
			trigger = {
				add_random_building_trigger = { BUILDING = farm_estates }
				generic_economic_building_innovation_trigger = { BUILDING = farm_estates }
			}
			set_variable = {
				name = random_building_variable
				value = 28
				days = 365
			}
		}
		1 = { # regimental_grounds : Expensive
			trigger = {
				add_random_building_trigger = { BUILDING = regimental_grounds }
				generic_recruitment_building_innovations_trigger = { BUILDING = regimental_grounds }
			}
			set_variable = {
				name = random_building_variable
				value = 29
				days = 365
			}
		}
		1 = { # caravanserai : Expensive
			trigger = {
				is_county_capital = yes
				building_caravanserai_requirement_terrain = yes
				scope:build_owner.culture = {
					has_innovation = innovation_guilds
				}
				building_requirement_castle_city_church = { LEVEL = 01 }
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = caravanserai_01
				}
				trigger_if = {
					limit = { has_building_or_higher = caravanserai_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = caravanserai }
					}
				}
				# City level triggers
				trigger_if = {
					limit = { has_building_or_higher = caravanserai_02 }
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = { has_building_or_higher = caravanserai_04 }
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = { has_building_or_higher = caravanserai_05 }
					scope:build_owner.culture = { has_innovation = innovation_cranes }
				}
				trigger_if = {
					limit = { has_building_or_higher = caravanserai_06 }
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 30
				days = 365
			}
		}
		1 = { # watermills : Expensive
			trigger = {
				is_county_capital = yes
				building_watermills_requirement_terrain = yes
				scope:build_owner.culture = {
					has_innovation = innovation_windmills
				}
				building_requirement_castle_city_church = { LEVEL = 01 }
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = watermills_01
				}
				trigger_if = {
					limit = { has_building_or_higher = watermills_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = watermills }
					}
				}
				# City level triggers
				trigger_if = {
					limit = { has_building_or_higher = watermills_02 }
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = { has_building_or_higher = watermills_04 }
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = { has_building_or_higher = watermills_05 }
					scope:build_owner.culture = { has_innovation = innovation_cranes }
				}
				trigger_if = {
					limit = { has_building_or_higher = watermills_06 }
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 31
				days = 365
			}
		}
		1 = { # windmills : Expensive
			trigger = {
				is_county_capital = yes
				building_windmills_requirement_terrain = yes
				scope:build_owner.culture = {
					has_innovation = innovation_windmills
				}
				building_requirement_castle_city_church = { LEVEL = 01 }
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = windmills_01
				}
				trigger_if = {
					limit = { has_building_or_higher = windmills_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = windmills }
					}
				}
				# City level triggers
				trigger_if = {
					limit = { has_building_or_higher = windmills_02 }
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = { has_building_or_higher = windmills_04 }
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = { has_building_or_higher = windmills_05 }
					scope:build_owner.culture = { has_innovation = innovation_cranes }
				}
				trigger_if = {
					limit = { has_building_or_higher = windmills_06 }
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 32
				days = 365
			}
		}
		1 = { # workshops : Expensive
			trigger = {
				add_random_building_trigger = { BUILDING = workshops }
				building_requirement_castle_city_church = { LEVEL = 01 }
				scope:build_owner.culture = {
					has_innovation = innovation_advanced_bowmaking
				}
				trigger_if = {
					limit = {
						has_building_or_higher = workshops_02
					}
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = workshops_04
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = workshops_05
					}
					scope:build_owner.culture = {
						has_innovation = innovation_royal_armory
					}
				}
				trigger_if = {
					limit = {
						has_building_or_higher = workshops_06
					}
					building_requirement_castle_city_church = { LEVEL = 04 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 33
				days = 365
			}
		}
		# Holding Type Specific : Normal
		10 = { # monastic_schools : Normal
			trigger = {
				has_holding_type = church_holding
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = monastic_schools_01
				}
				trigger_if = {
					limit = { has_building_or_higher = monastic_schools_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = monastic_schools }
					}
				}
				# Innovation triggers
				trigger_if = {
					limit = { has_building_or_higher = monastic_schools_01 }
					scope:build_owner.culture = { has_innovation = innovation_city_planning }
				}
				trigger_if = {
					limit = { has_building_or_higher = monastic_schools_02 }
					building_requirement_castle_city_church = { LEVEL = 02 }
					scope:build_owner.culture = { has_innovation = innovation_manorialism }
				}
				trigger_if = {
					limit = { has_building_or_higher = monastic_schools_04 }
					building_requirement_castle_city_church = { LEVEL = 03 }
					scope:build_owner.culture = { has_innovation = innovation_windmills }
				}
				trigger_if = {
					limit = { has_building_or_higher = monastic_schools_06 }
					building_requirement_castle_city_church = { LEVEL = 04 }
					scope:build_owner.culture = { has_innovation = innovation_cranes }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 34
				days = 365
			}
		}
		10 = { # guild_halls : Normal
			trigger = {
				has_holding_type = city_holding
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = guild_halls_01
				}
				trigger_if = {
					limit = { has_building_or_higher = guild_halls_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = guild_halls }
					}
				}
				# Innovation triggers
				trigger_if = {
					limit = { has_building_or_higher = guild_halls_01 }
					scope:build_owner.culture = {
						OR = {
							has_innovation = innovation_crop_rotation
							has_cultural_parameter = next_level_guild_halls
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = guild_halls_02 }
					OR = {
						AND = {
							building_requirement_castle_city_church = { LEVEL = 02 }
							scope:build_owner.culture = {
								has_innovation = innovation_manorialism
							}
						}
						scope:build_owner.culture = {
							has_cultural_parameter = next_level_guild_halls
							has_innovation = innovation_crop_rotation
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = guild_halls_04 }
					OR = {
						AND = {
							building_requirement_castle_city_church = { LEVEL = 03 }
							scope:build_owner.culture = {
								has_innovation = innovation_guilds
							}
						}
						AND = {
							building_requirement_castle_city_church = { LEVEL = 02 }
							scope:build_owner.culture = {
								has_cultural_parameter = next_level_guild_halls
								has_innovation = innovation_manorialism
							}
						}
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = guild_halls_06 }
					OR = {
						AND = {
							building_requirement_castle_city_church = { LEVEL = 04 }
							scope:build_owner.culture = {
								has_innovation = innovation_cranes
							}
						}
						AND = {
							building_requirement_castle_city_church = { LEVEL = 03 }
							scope:build_owner.culture = {
								has_cultural_parameter = next_level_guild_halls
								has_innovation = innovation_guilds
							}
						}
					}
				}
			}
			set_variable = {
				name = random_building_variable
				value = 35
				days = 365
			}
		}
		10 = { # scriptorium : Normal
			trigger = {
				has_holding_type = church_holding
				scope:build_owner = {
					has_dlc_feature = legends
				}
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = scriptorium_01
				}
				trigger_if = {
					limit = { has_building_or_higher = scriptorium_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = scriptorium }
					}
				}
				generic_economic_building_innovation_trigger = { BUILDING = scriptorium }
			}
			set_variable = {
				name = random_building_variable
				value = 36
				days = 365
			}
		}
		10 = { # megalith : Normal
			trigger = {
				has_holding_type = church_holding
				scope:build_owner.faith = {
					has_doctrine_parameter = can_build_megaliths
				}
				trigger_if = { # if all slots are full, this building type exists already
					limit = { free_building_slots = 0 }
					has_building_or_higher = megalith_01
				}
				trigger_if = {
					limit = { has_building_or_higher = megalith_01 }
					NOT = {
						has_lesser_building_trigger = { COMPARE = megalith }
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = megalith_02 }
					scope:build_owner.culture = {
						has_innovation = innovation_city_planning
					}
				}
				trigger_if = {
					limit = { has_building_or_higher = megalith_03 }
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 37
				days = 365
			}
		}
		2 = { # waterworks : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = waterworks }
				building_requirement_castle_city_church = { LEVEL = 01 }
				county = { NOT = { has_county_modifier = backwater_county_modifier } }
				trigger_if = {
					limit = {
						has_building_or_higher = waterworks_02
					}
					building_requirement_castle_city_church = { LEVEL = 02 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = waterworks_04
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
				trigger_if = {
					limit = {
						has_building_or_higher = waterworks_06
					}
					building_requirement_castle_city_church = { LEVEL = 03 }
				}
			}
			set_variable = {
				name = random_building_variable
				value = 42
				days = 365
			}
		}
		2 = { # paddy_fields : Normal
			trigger = {
				add_random_building_trigger = { BUILDING = paddy_fields }
				generic_economic_building_innovation_trigger = { BUILDING = paddy_fields }
			}
			set_variable = {
				name = random_building_variable
				value = 43
				days = 365
			}
		}
	}
}

# 2, builds based on variable
add_random_building_construct_effect = { # Province scope
	if = {
		limit = { has_variable = random_building_variable }
		switch = {
			trigger = var:random_building_variable
			0 = {
				add_next_building_tier_effect = { BUILDING = outposts }
			}
			1 = {
				add_next_building_tier_effect = { BUILDING = logging_camps }
			}
			2 = {
				add_next_building_tier_effect = { BUILDING = peat_quarries }
			}
			3 = {
				add_next_building_tier_effect = { BUILDING = hill_forts }
			}
			4 = {
				add_next_building_tier_effect = { BUILDING = plantations }
			}
			5 = {
				add_next_building_tier_effect = { BUILDING = quarries }
			}
			6 = {
				add_next_building_tier_effect = { BUILDING = hunting_grounds }
			}
			7 = {
				add_next_building_tier_effect = { BUILDING = military_camps }
			}
			8 = {
				add_next_building_tier_effect = { BUILDING = horse_pastures }
			}
			9 = {
				add_next_building_tier_effect = { BUILDING = hillside_grazing }
			}
			10 = {
				add_next_building_tier_effect = { BUILDING = warrior_lodges }
			}
			11 = {
				add_next_building_tier_effect = { BUILDING = pastures }
			}
			12 = {
				add_next_building_tier_effect = { BUILDING = ramparts }
			}
			13 = {
				add_next_building_tier_effect = { BUILDING = curtain_walls }
			}
			14 = {
				add_next_building_tier_effect = { BUILDING = watchtowers }
			}
			15 = {
				add_next_building_tier_effect = { BUILDING = cereal_fields }
			}
			16 = {
				add_next_building_tier_effect = { BUILDING = barracks }
			}
			17 = {
				add_next_building_tier_effect = { BUILDING = camel_farms }
			}
			18 = {
				add_next_building_tier_effect = { BUILDING = hill_farms }
			}
			19 = {
				add_next_building_tier_effect = { BUILDING = elephant_pens }
			}
			20 = {
				add_next_building_tier_effect = { BUILDING = common_tradeport }
			}
			21 = {
				add_next_building_tier_effect = { BUILDING = hospices }
			}
			22 = {
				add_next_building_tier_effect = { BUILDING = qanats }
			}
			23 = {
				add_next_building_tier_effect = { BUILDING = murex_farm }
			}
			24 = {
				add_next_building_tier_effect = { BUILDING = wind_furnace }
			}
			25 = {
				add_next_building_tier_effect = { BUILDING = stables }
			}
			26 = {
				add_next_building_tier_effect = { BUILDING = smiths }
			}
			27 = {
				add_next_building_tier_effect = { BUILDING = orchards }
			}
			28 = {
				add_next_building_tier_effect = { BUILDING = farm_estates }
			}
			29 = {
				add_next_building_tier_effect = { BUILDING = regimental_grounds }
			}
			30 = {
				add_next_building_tier_effect = { BUILDING = caravanserai }
			}
			31 = {
				add_next_building_tier_effect = { BUILDING = windmills }
			}
			32 = {
				add_next_building_tier_effect = { BUILDING = watermills }
			}
			33 = {
				add_next_building_tier_effect = { BUILDING = workshops }
			}
			34 = {
				add_next_building_tier_effect = { BUILDING = monastic_schools }
			}
			35 = {
				add_next_building_tier_effect = { BUILDING = guild_halls }
			}
			36 = {
				add_next_building_tier_effect = { BUILDING = scriptorium }
			}
			37 = {
				add_next_building_tier_effect = { BUILDING = megalith }
			}
			42 = {
				add_next_building_tier_effect = { BUILDING = waterworks }
			}
			43 = {
				add_next_building_tier_effect = { BUILDING = paddy_fields }
			}
		}
		remove_variable = random_building_variable
	}
}

# For stuff that has exactly _eight_ levels of upgrades.
destroy_or_downgrade_building_effect = {
	if = {
		limit = { has_building = $BUILDING$_08 }
		remove_building = $BUILDING$_08
		hidden_effect = { add_building = $BUILDING$_07 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_07 }
		remove_building = $BUILDING$_07
		hidden_effect = { add_building = $BUILDING$_06 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_06 }
		remove_building = $BUILDING$_06
		hidden_effect = { add_building = $BUILDING$_05 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_05 }
		remove_building = $BUILDING$_05
		hidden_effect = { add_building = $BUILDING$_04 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_04 }
		remove_building = $BUILDING$_04
		hidden_effect = { add_building = $BUILDING$_03 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_03 }
		remove_building = $BUILDING$_03
		hidden_effect = { add_building = $BUILDING$_02 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_02 }
		remove_building = $BUILDING$_02
		hidden_effect = { add_building = $BUILDING$_01 }
	}
	else = { remove_building = $BUILDING$_01 }
}

# For stuff that has exactly _three_ levels of upgrades.
destroy_or_downgrade_ducal_building_effect = {
	if = {
		limit = { has_building = $BUILDING$_03 }
		remove_building = $BUILDING$_03
		hidden_effect = { add_building = $BUILDING$_02 }
	}
	else_if = {
		limit = { has_building = $BUILDING$_02 }
		remove_building = $BUILDING$_02
		hidden_effect = { add_building = $BUILDING$_01 }
	}
	else = { remove_building = $BUILDING$_01 }
}

# For stuff that has exactly _two_ levels of upgrades.
destroy_or_downgrade_tribal_building_effect = {
	if = {
		limit = { has_building = $BUILDING$_02 }
		remove_building = $BUILDING$_02
		hidden_effect = { add_building = $BUILDING$_01 }
	}
	else = { remove_building = $BUILDING$_01 }
}

destroy_random_building_variable_effect = { # Province scope
	save_scope_as = raid_province
	if = {
		limit = { has_variable = destroyed_building_variable }
		remove_variable = destroyed_building_variable
	}
	random_list = { # Which building?
		# Generic/Terrain
		10 = { # outposts : Cheap
			trigger = { has_building_or_higher = outposts_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 0
				months = 1
			}
		}
		10 = { # logging_camps : Cheap
			trigger = { has_building_or_higher = logging_camps_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 1
				months = 1
			}
		}
		10 = { # peat_quarries : Cheap
			trigger = { has_building_or_higher = peat_quarries_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 2
				months = 1
			}
		}
		10 = { # hill_forts : Cheap
			trigger = { has_building_or_higher = hill_forts_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 3
				months = 1
			}
		}
		10 = { # plantations : Cheap
			trigger = { has_building_or_higher = plantations_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 4
				months = 1
			}
		}
		10 = { # quarries : Cheap
			trigger = { has_building_or_higher = quarries_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 5
				months = 1
			}
		}
		10 = { # hunting_grounds : Cheap
			trigger = { has_building_or_higher = hunting_grounds_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 6
				months = 1
			}
		}
		10 = { # military_camps : Cheap
			trigger = { has_building_or_higher = military_camps_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 7
				months = 1
			}
		}
		10 = { # horse_pastures : Cheap
			trigger = { has_building_or_higher = horse_pastures_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 8
				months = 1
			}
		}
		10 = { # hillside_grazing : Cheap
			trigger = { has_building_or_higher = hillside_grazing_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 9
				months = 1
			}
		}
		10 = { # warrior_lodges : Cheap
			trigger = { has_building_or_higher = warrior_lodges_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 10
				months = 1
			}
		}
		2 = { # pastures : Normal
			trigger = { has_building_or_higher = pastures_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 11
				months = 1
			}
		}
		2 = { # ramparts : Normal
			trigger = { has_building_or_higher = ramparts_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 12
				months = 1
			}
		}
		2 = { # curtain_walls : Normal
			trigger = { has_building_or_higher = curtain_walls_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 13
				months = 1
			}
		}
		2 = { # watchtowers : Normal
			trigger = { has_building_or_higher = watchtowers_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 14
				months = 1
			}
		}
		2 = { # cereal_fields : Normal
			trigger = { has_building_or_higher = cereal_fields_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 15
				months = 1
			}
		}
		2 = { # barracks : Normal
			trigger = { has_building_or_higher = barracks_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 16
				months = 1
			}
		}
		2 = { # camel_farms : Normal
			trigger = { has_building_or_higher = camel_farms_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 17
				months = 1
			}
		}
		2 = { # hill_farms : Normal
			trigger = { has_building_or_higher = hill_farms_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 18
				months = 1
			}
		}
		2 = { # paddy_fields : Normal
			trigger = { has_building_or_higher = hill_farms_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 43
				months = 1
			}
		}
		2 = { # elephant_pens : Normal
			trigger = { has_building_or_higher = elephant_pens_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 19
				months = 1
			}
		}
		2 = { # common_tradeport : Normal
			trigger = { has_building_or_higher = common_tradeport_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 20
				months = 1
			}
		}
		2 = { # hospices : Normal
			trigger = { has_building_or_higher = hospices_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 21
				months = 1
			}
		}
		2 = { # qanats : Normal
			trigger = { has_building_or_higher = qanats_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 22
				months = 1
			}
		}
		2 = { # murex_farm : Normal
			trigger = { has_building_or_higher = murex_farm_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 23
				months = 1
			}
		}
		2 = { # waterworks : Normal
			trigger = { has_building_or_higher = waterworks_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 42
				months = 1
			}
		}
		2 = { # wind_furnace : Normal
			trigger = { has_building_or_higher = wind_furnace_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 24
				months = 1
			}
		}
		2 = { # stables : Normal
			trigger = { has_building_or_higher = stables_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 25
				months = 1
			}
		}
		2 = { # smiths : Normal
			trigger = { has_building_or_higher = smiths_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 26
				months = 1
			}
		}
		1 = { # orchards : Expensive
			trigger = { has_building_or_higher = orchards_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 27
				months = 1
			}
		}
		1 = { # farm_estates : Expensive
			trigger = { has_building_or_higher = farm_estates_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 28
				months = 1
			}
		}
		1 = { # regimental_grounds : Expensive
			trigger = { has_building_or_higher = regimental_grounds_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 29
				months = 1
			}
		}
		1 = { # caravanserai : Expensive
			trigger = { has_building_or_higher = caravanserai_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 30
				months = 1
			}
		}
		1 = { # watermills : Expensive
			trigger = { has_building_or_higher = watermills_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 31
				months = 1
			}
		}
		1 = { # windmills : Expensive
			trigger = { has_building_or_higher = windmills_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 32
				months = 1
			}
		}
		1 = { # workshops : Expensive
			trigger = { has_building_or_higher = workshops_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 33
				months = 1
			}
		}
		# Holding Type Specific : Normal
		10 = { # monastic_schools : Normal
			trigger = { has_building_or_higher = monastic_schools_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 21
				months = 1
			}
		}
		10 = { # guild_halls : Normal
			trigger = { has_building_or_higher = guild_halls_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 35
				months = 1
			}
		}
		10 = { # scriptorium : Normal
			trigger = { has_building_or_higher = scriptorium_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 36
				months = 1
			}
		}
		10 = { # megalith : Normal
			trigger = { has_building_or_higher = megalith_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 37
				months = 1
			}
		}
		10 = { # Waterworks
			trigger = { has_building_or_higher = waterworks_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 42
				months = 1
			}
		}
		10 = { # Paddy Fields
			trigger = { has_building_or_higher = paddy_fields_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 43
				months = 1
			}
		}
		# Tribal
		10 = { # Market Villages
			trigger = { has_building_or_higher = market_villages_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 38
				months = 1
			}
		}
		10 = { # Palisades
			trigger = { has_building_or_higher = palisades_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 39
				months = 1
			}
		}
		10 = { # War Camps
			trigger = { has_building_or_higher = war_camps_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 40
				months = 1
			}
		}
		10 = { # Longhouses
			trigger = { has_building_or_higher = longhouses_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 41
				months = 1
			}
		}
	}
}

destroy_random_building_effect = {
	if = {
		limit = { has_variable = destroyed_building_variable }
		switch = {
			trigger = var:destroyed_building_variable
			0 = { # outposts : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = outposts }
			}
			1 = { # logging_camps : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = logging_camps }
			}
			2 = { # peat_quarries : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = peat_quarries }
			}
			3 = { # hill_forts : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = hill_forts }
			}
			4 = { # plantations : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = plantations }
			}
			5 = { # quarries : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = quarries }
			}
			6 = { # hunting_grounds : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = hunting_grounds }
			}
			7 = { # military_camps : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = military_camps }
			}
			8 = { # horse_pastures : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = horse_pastures }
			}
			9 = { # hillside_grazing : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = hillside_grazing }
			}
			10 = { # warrior_lodges : Cheap
				destroy_or_downgrade_building_effect = { BUILDING = warrior_lodges }
			}
			11 = { # pastures : Normal
				destroy_or_downgrade_building_effect = { BUILDING = pastures }
			}
			12 = { # ramparts : Normal
				destroy_or_downgrade_building_effect = { BUILDING = ramparts }
			}
			13 = { # curtain_walls : Normal
				destroy_or_downgrade_building_effect = { BUILDING = curtain_walls }
			}
			14 = { # watchtowers : Normal
				destroy_or_downgrade_building_effect = { BUILDING = watchtowers }
			}
			15 = { # cereal_fields : Normal
				destroy_or_downgrade_building_effect = { BUILDING = cereal_fields }
			}
			16 = { # barracks : Normal
				destroy_or_downgrade_building_effect = { BUILDING = barracks }
			}
			17 = { # camel_farms : Normal
				destroy_or_downgrade_building_effect = { BUILDING = camel_farms }
			}
			18 = { # hill_farms : Normal
				destroy_or_downgrade_building_effect = { BUILDING = hill_farms }
			}
			19 = { # elephant_pens : Normal
				destroy_or_downgrade_building_effect = { BUILDING = elephant_pens }
			}
			20 = { # common_tradeport : Normal
				destroy_or_downgrade_building_effect = { BUILDING = common_tradeport }
			}
			21 = { # hospices : Normal
				destroy_or_downgrade_building_effect = { BUILDING = hospices }
			}
			22 = { # qanats : Normal
				destroy_or_downgrade_building_effect = { BUILDING = qanats }
			}
			23 = { # murex_farm : Normal
				destroy_or_downgrade_building_effect = { BUILDING = murex_farm }
			}
			24 = { # wind_furnace : Normal
				destroy_or_downgrade_building_effect = { BUILDING = wind_furnace }
			}
			25 = { # stables : Normal
				destroy_or_downgrade_building_effect = { BUILDING = stables }
			}
			26 = { # smiths : Normal
				destroy_or_downgrade_building_effect = { BUILDING = smiths }
			}
			27 = { # orchards : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = orchards }
			}
			28 = { # farm_estates : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = farm_estates }
			}
			29 = { # regimental_grounds : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = regimental_grounds }
			}
			30 = { # caravanserai : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = caravanserai }
			}
			31 = { # watermills : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = watermills }
			}
			32 = { # windmills : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = windmills }
			}
			33 = { # workshops : Expensive
				destroy_or_downgrade_building_effect = { BUILDING = workshops }
			}
			# Holding Type Specific : Normal
			34 = { # monastic_schools : Normal
				destroy_or_downgrade_building_effect = { BUILDING = monastic_schools }
			}
			35 = { # guild_halls : Normal
				destroy_or_downgrade_building_effect = { BUILDING = guild_halls }
			}
			36 = { # scriptorium : Normal
				destroy_or_downgrade_building_effect = { BUILDING = scriptorium }
			}
			37 = { # megalith : Normal
				destroy_or_downgrade_building_effect = { BUILDING = megalith }
			}
			42 = { # waterworks : Normal
				destroy_or_downgrade_building_effect = { BUILDING = waterworks }
			}
			43 = { # paddy_fields : Normal
				destroy_or_downgrade_building_effect = { BUILDING = paddy_fields }
			}
			# Tribal
			38 = { # megalith : Normal
				destroy_or_downgrade_tribal_building_effect = { BUILDING = market_villages }
			}
			39 = { # megalith : Normal
				destroy_or_downgrade_tribal_building_effect = { BUILDING = palisades }
			}
			40 = { # megalith : Normal
				destroy_or_downgrade_tribal_building_effect = { BUILDING = war_camps }
			}
			41 = { # megalith : Normal
				destroy_or_downgrade_tribal_building_effect = { BUILDING = longhouses }
			}
		}
		remove_variable = destroyed_building_variable
	}
}

destroy_or_downgrade_estate_building_2_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_02 }
		lower_domicile_building_no_refund = $BUILDING$_02
	}
	else = { remove_domicile_building_no_refund = $BUILDING$_01 }
}

destroy_or_downgrade_estate_building_3_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_03 }
		lower_domicile_building_no_refund = $BUILDING$_03
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_02 }
		lower_domicile_building_no_refund = $BUILDING$_02
	}
	else = { remove_domicile_building_no_refund = $BUILDING$_01 }
}

destroy_or_downgrade_estate_building_4_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_04 }
		lower_domicile_building_no_refund = $BUILDING$_04
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_03 }
		lower_domicile_building_no_refund = $BUILDING$_03
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_02 }
		lower_domicile_building_no_refund = $BUILDING$_02
	}
	else = { remove_domicile_building_no_refund = $BUILDING$_01 }
}

destroy_or_downgrade_estate_building_5_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_05 }
		lower_domicile_building_no_refund = $BUILDING$_05
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_04 }
		lower_domicile_building_no_refund = $BUILDING$_04
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_03 }
		lower_domicile_building_no_refund = $BUILDING$_03
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_02 }
		lower_domicile_building_no_refund = $BUILDING$_02
	}
	else = { remove_domicile_building_no_refund = $BUILDING$_01 }
}

destroy_or_downgrade_estate_building_6_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_06 }
		lower_domicile_building_no_refund = $BUILDING$_06
	}
	if = {
		limit = { has_domicile_building = $BUILDING$_05 }
		lower_domicile_building_no_refund = $BUILDING$_05
	}
	if = {
		limit = { has_domicile_building = $BUILDING$_04 }
		lower_domicile_building_no_refund = $BUILDING$_04
	}
	if = {
		limit = { has_domicile_building = $BUILDING$_03 }
		lower_domicile_building_no_refund = $BUILDING$_03
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_02 }
		lower_domicile_building_no_refund = $BUILDING$_02
	}
	else = { remove_domicile_building_no_refund = $BUILDING$_01 }
}

destroy_or_downgrade_estate_building_2_4_replacement_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_04 }
		lower_domicile_building_no_refund = $BUILDING$_04
	}
	else = {
		lower_domicile_building_no_refund = $BUILDING$_03
	}
}

destroy_or_downgrade_estate_building_2_6_replacement_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_06 }
		lower_domicile_building_no_refund = $BUILDING$_06
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_05 }
		lower_domicile_building_no_refund = $BUILDING$_05
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_04 }
		lower_domicile_building_no_refund = $BUILDING$_04
	}
	else = {
		lower_domicile_building_no_refund = $BUILDING$_03
	}
}

destroy_or_downgrade_estate_building_3_6_replacement_effect = {
	if = {
		limit = { has_domicile_building = $BUILDING$_06 }
		lower_domicile_building_no_refund = $BUILDING$_06
	}
	else_if = {
		limit = { has_domicile_building = $BUILDING$_05 }
		lower_domicile_building_no_refund = $BUILDING$_05
	}
	else = {
		lower_domicile_building_no_refund = $BUILDING$_04
	}
}

destroy_random_estate_building_variable_effect = { # Domicile scope
	save_scope_as = raid_domicile
	if = {
		limit = { has_variable = destroyed_building_variable }
		remove_variable = destroyed_building_variable
	}
	random_list = { # Which building?
		# Generic/Terrain
		10 = { # barracks : Normal
			trigger = { has_domicile_building_or_higher = barracks_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 0
				months = 1
			}
		}
		10 = { # watchtower : Normal
			trigger = { has_domicile_building_or_higher = watchtower_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 1
				months = 1
			}
		}
		10 = { # guardhouse : Normal
			trigger = { has_domicile_building_or_higher = guardhouse_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 2
				months = 1
			}
		}
		10 = { # garden : Normal
			trigger = { has_domicile_building_or_higher = garden_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 3
				months = 1
			}
		}
		10 = { # stable : Normal
			trigger = { has_domicile_building_or_higher = stable_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 4
				months = 1
			}
		}
		10 = { # workshop : Normal
			trigger = { has_domicile_building_or_higher = workshop_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 5
				months = 1
			}
		}
		10 = { # storage : Normal
			trigger = { has_domicile_building_or_higher = storage_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 6
				months = 1
			}
		}
		10 = { # market : Normal
			trigger = { has_domicile_building_or_higher = market_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 7
				months = 1
			}
		}
		10 = { # grazing_land : Normal
			trigger = { has_domicile_building_or_higher = grazing_land_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 8
				months = 1
			}
		}
		10 = { # grain_field : Normal
			trigger = { has_domicile_building_or_higher = grain_field_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 9
				months = 1
			}
		}
		10 = { # living_quarters : Normal
			trigger = { has_domicile_building_or_higher = living_quarters_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 10
				months = 1
			}
		}
		10 = { # vineyard : Normal
			trigger = { has_domicile_building_or_higher = vineyard_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 11
				months = 1
			}
		}
		10 = { # olive : Normal
			trigger = { has_domicile_building_or_higher = olive_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 12
				months = 1
			}
		}
		10 = { # temple : Normal
			trigger = { has_domicile_building_or_higher = temple_small_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 13
				months = 1
			}
		}
		10 = { # grand_solar : Normal
			trigger = { has_domicile_building_or_higher = grand_solar_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 14
				months = 1
			}
		}
		10 = { # cabinet_of_curiosities : Normal
			trigger = { has_domicile_building_or_higher = cabinet_of_curiosities_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 15
				months = 1
			}
		}
		10 = { # reception_hall : Normal
			trigger = { has_domicile_building_or_higher = reception_hall_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 16
				months = 1
			}
		}
		10 = { # prison : Normal
			trigger = { has_domicile_building_or_higher = prison_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 17
				months = 1
			}
		}
		10 = { # courtyard : Normal
			trigger = { has_domicile_building_or_higher = courtyard_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 18
				months = 1
			}
		}
		10 = { # wine_cellar : Normal
			trigger = { has_domicile_building_or_higher = wine_cellar_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 19
				months = 1
			}
		}
		10 = { # guest_room : Normal
			trigger = { has_domicile_building_or_higher = guest_room_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 20
				months = 1
			}
		}
		10 = { # bath : Normal
			trigger = { has_domicile_building_or_higher = bath_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 21
				months = 1
			}
		}
		10 = { # library : Normal
			trigger = { has_domicile_building_or_higher = library_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 22
				months = 1
			}
		}
		10 = { # servants_quarters : Normal
			trigger = { has_domicile_building_or_higher = servants_quarters_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 23
				months = 1
			}
		}
		10 = { # office : Normal
			trigger = { has_domicile_building_or_higher = office_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 24
				months = 1
			}
		}
		10 = { # trophy_room : Normal
			trigger = { has_domicile_building_or_higher = trophy_room_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 25
				months = 1
			}
		}
		10 = { # silk : Normal
			trigger = { has_domicile_building_or_higher = silk_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 26
				months = 1
			}
		}
	}
}

destroy_random_estate_building_effect = {
	if = {
		limit = { has_variable = destroyed_building_variable }
		switch = {
			trigger = var:destroyed_building_variable
			0 = { # barracks : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = barracks }
			}
			1 = { # watchtower : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = watchtower }
			}
			2 = { # guardhouse : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = guardhouse }
			}
			3 = { # garden : Cheap
				if = {
					limit = { has_domicile_building_or_higher = garden_leisure_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = garden_leisure }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = garden_fruit_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = garden_fruit }
				}
				else = {
					destroy_or_downgrade_estate_building_3_effect = { BUILDING = garden }
				}
			}
			4 = { # stable : Cheap
				if = {
					limit = { has_domicile_building_or_higher = stable_grand_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = stable_grand }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = stable_kennel_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = stable_kennel }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = stable_chariot_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = stable_chariot }
				}
				else = {
					destroy_or_downgrade_estate_building_3_effect = { BUILDING = stable }
				}
			}
			5 = { # workshop : Cheap
				if = {
					limit = { has_domicile_building_or_higher = workshop_carpenter_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = workshop_carpenter }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = workshop_mason_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = workshop_mason }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = workshop_textile_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = workshop_textile }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = workshop }
				}
			}
			6 = { # storage : Cheap
				if = {
					limit = { has_domicile_building_or_higher = storage_warehouse_03 }
					destroy_or_downgrade_estate_building_2_4_replacement_effect = { BUILDING = storage_warehouse }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = storage_granary_03 }
					destroy_or_downgrade_estate_building_2_4_replacement_effect = { BUILDING = storage_granary }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = storage }
				}
			}
			7 = { # market : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = market }
			}
			8 = { # grazing_land : Cheap
				if = {
					limit = { has_domicile_building_or_higher = horse_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = horse_pasture }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = camel_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = camel_pasture }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = elephant_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = elephant_pasture }
				}
				else = {
					destroy_or_downgrade_estate_building_6_effect = { BUILDING = grazing_land }
				}
			}
			9 = { # grain_field : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = grain_field }
			}
			10 = { # living_quarters : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = living_quarters }
			}
			11 = { # vineyard : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = vineyard }
			}
			12 = { # olive : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = olive }
			}
			13 = { # temple : Cheap
				if = {
					limit = { has_domicile_building_or_higher = temple_crypt_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = temple_crypt }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = temple_large_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = temple_large }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = temple_monastery_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = temple_monastery }
				}
				else = {
					destroy_or_downgrade_estate_building_3_effect = { BUILDING = temple_small }
				}
			}
			14 = { # grand_solar : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = grand_solar }
			}
			15 = { # cabinet_of_curiosities : Cheap
				destroy_or_downgrade_estate_building_3_effect = { BUILDING = cabinet_of_curiosities }
			}
			16 = { # reception_hall : Cheap
				destroy_or_downgrade_estate_building_5_effect = { BUILDING = reception_hall }
			}
			17 = { # prison : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = prison }
			}
			18 = { # courtyard : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = courtyard }
			}
			19 = { # wine_cellar : Cheap
				destroy_or_downgrade_estate_building_3_effect = { BUILDING = wine_cellar }
			}
			20 = { # guest_room : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = guest_room }
			}
			21 = { # bath : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = bath }
			}
			22 = { # library : Cheap
				if = {
					limit = { has_domicile_building_or_higher = library_observatory_03 }
					destroy_or_downgrade_estate_building_2_4_replacement_effect = { BUILDING = library_observatory }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = library_education_03 }
					destroy_or_downgrade_estate_building_2_4_replacement_effect = { BUILDING = library_education }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = library }
				}
			}
			23 = { # servants_quarters : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = servants_quarters }
			}
			24 = { # office : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = office }
			}
			25 = { # trophy_room : Cheap
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = trophy_room }
			}
			26 = { # silk : Cheap
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = silk }
			}
		}
		remove_variable = destroyed_building_variable
	}
}

# Destroy building effects for chinese estates - In domicile scope
destroy_random_east_asian_estate_building_variable_effect = {
	save_scope_as = raid_domicile
	if = {
		limit = { has_variable = destroyed_building_variable }
		remove_variable = destroyed_building_variable
	}
	random_list = { # Which building?
		# Upgrades/internal buildings
		10 = { # living_quarters
			trigger = { has_domicile_building_or_higher = east_asian_estate_living_quarters_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 1
				months = 1
			}
		}
		10 = { # commander's study
			trigger = { has_domicile_building_or_higher = east_asian_estate_commander_study_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 2
				months = 1
			}
		}
		10 = { # office
			trigger = { has_domicile_building_or_higher = east_asian_estate_office_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 3
				months = 1
			}
		}
		10 = { # servants_quarters
			trigger = { has_domicile_building_or_higher = east_asian_estate_servants_quarters_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 4
				months = 1
			}
		}
		10 = { # library
			trigger = { has_domicile_building_or_higher = east_asian_estate_library_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 5
				months = 1
			}
		}
		10 = { # bath
			trigger = { has_domicile_building_or_higher = east_asian_estate_bath_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 6
				months = 1
			}
		}
		10 = { # guest_room
			trigger = { has_domicile_building_or_higher = east_asian_estate_guest_room_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 7
				months = 1
			}
		}
		10 = { # wine_cellar
			trigger = { has_domicile_building_or_higher = east_asian_estate_wine_cellar_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 8
				months = 1
			}
		}
		10 = { # courtyard
			trigger = { has_domicile_building_or_higher = east_asian_estate_courtyard_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 9
				months = 1
			}
		}
		10 = { # prison
			trigger = { has_domicile_building_or_higher = east_asian_estate_prison_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 10
				months = 1
			}
		}
		10 = { # reception_hall
			trigger = { has_domicile_building_or_higher = east_asian_estate_reception_hall_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 11
				months = 1
			}
		}
		10 = { # cabinet_of_curiosities
			trigger = { has_domicile_building_or_higher = east_asian_estate_cabinet_of_curiosities_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 12
				months = 1
			}
		}
		# External buildings
		10 = { # ancestral shrine
			trigger = { has_domicile_building_or_higher = east_asian_estate_ancestral_shrine_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 13
				months = 1
			}
		}
		10 = { # pagoda
			trigger = { has_domicile_building_or_higher = east_asian_estate_temple_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 14
				months = 1
			}
		}
		10 = { # barracks
			trigger = { has_domicile_building_or_higher = east_asian_estate_barracks_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 15
				months = 1
			}
		}
		10 = { # watchtower
			trigger = { has_domicile_building_or_higher = east_asian_estate_watchtower_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 16
				months = 1
			}
		}
		10 = { # garden
			trigger = { has_domicile_building_or_higher = east_asian_estate_garden_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 18
				months = 1
			}
		}
		10 = { # stable
			trigger = { has_domicile_building_or_higher = east_asian_estate_stable_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 19
				months = 1
			}
		}
		10 = { # workshop
			trigger = { has_domicile_building_or_higher = east_asian_estate_workshop_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 20
				months = 1
			}
		}
		10 = { # storage
			trigger = { has_domicile_building_or_higher = east_asian_estate_storage_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 21
				months = 1
			}
		}
		10 = { # market
			trigger = { has_domicile_building_or_higher = east_asian_estate_market_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 22
				months = 1
			}
		}
		10 = { # grazing_land
			trigger = { has_domicile_building_or_higher = east_asian_estate_grazing_land_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 23
				months = 1
			}
		}
		10 = { # rice_field
			trigger = { has_domicile_building_or_higher = east_asian_estate_rice_field_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 25
				months = 1
			}
		}
		10 = { # tea plantation
			trigger = { has_domicile_building_or_higher = east_asian_estate_tea_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 27
				months = 1
			}
		}
		10 = { # physician's office
			trigger = { has_domicile_building_or_higher = east_asian_estate_health_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 28
				months = 1
			}
		}
		10 = { # silk
			trigger = { has_domicile_building_or_higher = east_asian_estate_silk_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 29
				months = 1
			}
		}
		10 = { # study
			trigger = { has_domicile_building_or_higher = east_asian_estate_study_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 30
				months = 1
			}
		}
		10 = { # minister's office
			trigger = { has_domicile_building_or_higher = east_asian_estate_minister_office_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 31
				months = 1
			}
		}
		10 = { # dynastic pavilion/movement leader building
			trigger = { has_domicile_building_or_higher = east_asian_estate_movement_study_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 32
				months = 1
			}
		}
		10 = { # archive
			trigger = { has_domicile_building_or_higher = east_asian_estate_archive_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 33
				months = 1
			}
		}
		10 = { # gate
			trigger = { has_domicile_building_or_higher = east_asian_estate_gate_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 34
				months = 1
			}
		}
		10 = { # taxation office
			trigger = { has_domicile_building_or_higher = east_asian_estate_taxation_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 35
				months = 1
			}
		}
		10 = { # yamen office
			trigger = { has_domicile_building_or_higher = east_asian_peasant_quarters_01 }
			set_variable = {
				name = destroyed_building_variable
				value = 36
				months = 1
			}
		}
	}
}

destroy_random_east_asian_estate_building_effect = {
	if = {
		limit = { has_variable = destroyed_building_variable }
		switch = {
			trigger = var:destroyed_building_variable
			1 = { # living_quarters
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_living_quarters }
			}
			2 = { # commander's study
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_commander_study }
			}
			3 = { # office
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_office }
			}
			4 = { # servants_quarters
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_servants_quarters }
			}
			5 = { # library
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_library_education_03 }
					destroy_or_downgrade_estate_building_2_4_replacement_effect = { BUILDING = east_asian_estate_library_education }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = east_asian_estate_library }
				}
			}
			6 = { # bath
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_bath }
			}
			7 = { # guest_room
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_guest_room }
			}
			8 = { # wine_cellar
				destroy_or_downgrade_estate_building_3_effect = { BUILDING = east_asian_estate_wine_cellar }
			}
			9 = { # courtyard
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_courtyard }
			}
			10 = { # prison
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_prison }
			}
			11 = { # reception_hall
				destroy_or_downgrade_estate_building_5_effect = { BUILDING = east_asian_estate_reception_hall }
			}
			12 = { # cabinet_of_curiosities
				destroy_or_downgrade_estate_building_3_effect = { BUILDING = east_asian_estate_cabinet_of_curiosities }
			}
			13 = { # ancestral shrine
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_ancestral_shrine }
			}
			14 = { # pagoda
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_temple }
			}
			15 = { # barracks
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_barracks }
			}
			16 = { # watchtower
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_watchtower }
			}
			18 = { # garden
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_garden_leisure_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_garden_leisure }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_garden_fruit_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_garden_fruit }
				}
				else = {
					destroy_or_downgrade_estate_building_3_effect = { BUILDING = east_asian_estate_garden }
				}
			}
			19 = { # stable
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_stable_grand_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_stable_grand }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_stable_kennel_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_stable_kennel }
				}
				else = {
					destroy_or_downgrade_estate_building_3_effect = { BUILDING = east_asian_estate_stable }
				}
			}
			20 = { # workshop
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_workshop_carpenter_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = east_asian_estate_workshop_carpenter }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_workshop_mason_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = east_asian_estate_workshop_mason }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_workshop_textile_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = east_asian_estate_workshop_textile }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = east_asian_estate_workshop }
				}
			}
			21 = { # storage
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_storage_warehouse_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = east_asian_estate_storage_warehouse }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_storage_granary_03 }
					destroy_or_downgrade_estate_building_2_6_replacement_effect = { BUILDING = east_asian_estate_storage_granary }
				}
				else = {
					destroy_or_downgrade_estate_building_2_effect = { BUILDING = east_asian_estate_storage }
				}
			}
			22 = { # market
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_market }
			}
			23 = { # grazing_land
				if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_horse_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_horse_pasture }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_camel_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_camel_pasture }
				}
				else_if = {
					limit = { has_domicile_building_or_higher = east_asian_estate_elephant_pasture_04 }
					destroy_or_downgrade_estate_building_3_6_replacement_effect = { BUILDING = east_asian_estate_elephant_pasture }
				}
				else = {
					destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_grazing_land }
				}
			}
			25 = { # rice_field
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_rice_field }
			}
			27 = { # tea
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_tea }
			}
			28 = { # health
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_health }
			}
			29 = { # silk
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_silk }
			}
			30 = { # study
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_study }
			}
			31 = { # minister's office
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_minister_office }
			}
			32 = { # dynastic pavilion/movement leader building
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_movement_study }
			}
			33 = { # archive
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_archive }
			}
			34 = { # gate
				destroy_or_downgrade_estate_building_6_effect = { BUILDING = east_asian_estate_gate }
			}
			35 = { # tax office
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_estate_taxation }
			}
			36 = { # peasants quarters
				destroy_or_downgrade_estate_building_4_effect = { BUILDING = east_asian_peasant_quarters }
			}
		}
		remove_variable = destroyed_building_variable
	}
}

add_random_economic_building_effect = {
	save_scope_as = building_scope
	custom_tooltip = random_economic_building_tt
	hidden_effect = {
		random_list = {
			10 = { # Caravanserai
				trigger = {
					NOT = { has_building_or_higher = caravanserai_01 }
					building_caravanserai_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
					culture = {
						has_innovation = innovation_guilds
					}
				}
				add_building = caravanserai_01
			}
			10 = { # Watermills
				trigger = {
					NOT = { has_building_or_higher = watermills_01 }
					building_watermills_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
					culture = {
						has_innovation = innovation_windmills
					}
				}
				add_building = watermills_01
			}
			10 = { # Windmills
				trigger = {
					NOT = { has_building_or_higher = windmills_01 }
					building_windmills_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
					culture = {
						has_innovation = innovation_windmills
					}
				}
				add_building = windmills_01
			}
			10 = { # Trade Port
				trigger = {
					NOT = { has_building_or_higher = common_tradeport_01 }
					building_common_tradeport_requirement_terrain = yes
					OR = {
						AND = {
							culture = {
								has_cultural_parameter = trade_ports_enabled_for_tribals
							}
							has_building_or_higher = tribe_01
						}
						building_requirement_castle_city_church = { LEVEL = 01 }
					}
				}
				add_building = common_tradeport_01
			}
			10 = { # Pastures
				trigger = {
					NOT = { has_building_or_higher = pastures_01 }
					building_pastures_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = pastures_01
			}
			10 = { # Hunting Grounds
				trigger = {
					NOT = { has_building_or_higher = hunting_grounds_01 }
					building_hunting_grounds_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = hunting_grounds_01
			}
			10 = { # Orchards
				trigger = {
					NOT = { has_building_or_higher = orchards_01 }
					building_orchards_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = orchards_01
			}
			10 = { # Farm Estates
				trigger = {
					NOT = { has_building_or_higher = farm_estates_01 }
					building_farm_estates_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = farm_estates_01
			}
			10 = { # Cereal Fields
				trigger = {
					NOT = { has_building_or_higher = cereal_fields_01 }
					building_cereal_fields_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = cereal_fields_01
			}
			10 = { # Logging Camps
				trigger = {
					NOT = { has_building_or_higher = logging_camps_01 }
					building_logging_camps_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = logging_camps_01
			}
			10 = { # Peat Quarries
				trigger = {
					NOT = { has_building_or_higher = peat_quarries_01 }
					building_peat_quarries_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = peat_quarries_01
			}
			10 = { # Hill Farms
				trigger = {
					NOT = { has_building_or_higher = hill_farms_01 }
					building_hill_farms_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = hill_farms_01
			}
			10 = { # Elephant Pens
				trigger = {
					NOT = { has_building_or_higher = elephant_pens_01 }
					building_elephant_pens_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = elephant_pens_01
			}
			10 = { # Plantations
				trigger = {
					NOT = { has_building_or_higher = plantations_01 }
					building_plantations_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = plantations_01
			}
			10 = { # Quarries
				trigger = {
					NOT = { has_building_or_higher = quarries_01 }
					building_quarries_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = quarries_01
			}
			10 = { # Qanats
				trigger = {
					NOT = { has_building_or_higher = qanats_01 }
					building_qanats_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
					culture = {
						has_cultural_parameter = unlocks_qanat_building
					}
				}
				add_building = qanats_01
			}
			10 = { # Murex Farms
				trigger = {
					NOT = { has_building_or_higher = murex_farm_01 }
					building_murex_farm_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = murex_farm_01
			}
			10 = { # Guild Halls - City Specific
				trigger = {
					NOT = { has_building_or_higher = guild_halls_01 }
					has_building_or_higher = city_01
				}
				add_building = guild_halls_01
			}
			10 = { # Monastic Schools - Temple Specific
				trigger = {
					NOT = { has_building_or_higher = monastic_schools_01 }
					has_building_or_higher = temple_01
				}
				add_building = monastic_schools_01
			}
		}
	}
}

add_random_fortification_building_effect = {
	save_scope_as = building_scope
	custom_tooltip = random_fortification_building_tt
	hidden_effect = {
		random_list = {
			10 = { # Ramparts
				trigger = {
					NOT = { has_building_or_higher = ramparts_01 }
					building_ramparts_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = ramparts_01
			}
			10 = { # Curtain Walls
				trigger = {
					NOT = { has_building_or_higher = curtain_walls_01 }
					building_curtain_walls_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = curtain_walls_01
			}
			10 = { # Watchtowers
				trigger = {
					NOT = { has_building_or_higher = watchtowers_01 }
					building_watchtowers_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = watchtowers_01
			}
			10 = { # Hill Forts
				trigger = {
					NOT = { has_building_or_higher = hill_forts_01 }
					building_hill_forts_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = hill_forts_01
			}
		}
	}
}

add_random_military_building_effect = {
	save_scope_as = building_scope
	custom_tooltip = random_military_building_tt
	hidden_effect = {
		random_list = {
			10 = { # Wind Furnace
				trigger = {
					NOT = { has_building_or_higher = wind_furnace_01 }
					building_wind_furnace_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = wind_furnace_01
			}
			10 = { # Workshops
				trigger = {
					NOT = { has_building_or_higher = workshops_01 }
					building_workshops_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
					scope:building_scope.county.holder.culture = {
						has_innovation = innovation_advanced_bowmaking
					}
				}
				add_building = workshops_01
			}
			10 = { # Horse Pastures
				trigger = {
					NOT = { has_building_or_higher = windmills_01 }
					building_horse_pastures_requirement_terrain = yes
				}
				add_building = windmills_01
			}
			10 = { # Hillside Grazing
				trigger = {
					NOT = { has_building_or_higher = hillside_grazing_01 }
					building_hillside_grazing_requirement_terrain = yes
				}
				add_building = hillside_grazing_01
			}
			10 = { # Warrior Lodges
				trigger = {
					NOT = { has_building_or_higher = warrior_lodges_01 }
					building_warrior_lodges_requirement_terrain = yes
				}
				add_building = warrior_lodges_01
			}
			10 = { # Military Camps
				trigger = {
					NOT = { has_building_or_higher = military_camps_01 }
					building_military_camps_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = military_camps_01
			}
			10 = { # Regimental Grounds
				trigger = {
					NOT = { has_building_or_higher = regimental_grounds_01 }
					building_regimental_grounds_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = regimental_grounds_01
			}
			10 = { # Outposts
				trigger = {
					NOT = { has_building_or_higher = outposts_01 }
					building_outposts_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = outposts_01
			}
			10 = { # Barracks
				trigger = {
					NOT = { has_building_or_higher = barracks_01 }
					building_barracks_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = barracks_01
			}
			10 = { # Camel Farms
				trigger = {
					NOT = { has_building_or_higher = camel_farms_01 }
					building_camel_farms_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = camel_farms_01
			}
			10 = { # Stables
				trigger = {
					NOT = { has_building_or_higher = stables_01 }
					building_stables_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = stables_01
			}
			10 = { # Smiths
				trigger = {
					NOT = { has_building_or_higher = smiths_01 }
					building_smiths_requirement_terrain = yes
					building_requirement_castle_city_church = { LEVEL = 01 }
				}
				add_building = smiths_01
			}
		}
	}
}

destroy_or_downgrade_temple_effect = {
	if = {
		limit = { has_building = temple_04 }
		remove_building = temple_04
		hidden_effect = { add_building = temple_03 }
	}
	else_if = {
		limit = { has_building = temple_03 }
		remove_building = temple_03
		hidden_effect = { add_building = temple_02 }
	}
	else_if = {
		limit = { has_building = temple_02 }
		remove_building = temple_02
		hidden_effect = { add_building = temple_01 }
	}
	else = { remove_building = temple_01 }
}

add_next_temple_tier_effect = {
	if = {
		limit = {
			NOT = { has_building_or_higher = temple_01 }
		}
		add_building = temple_01
	}
	else_if = {
		limit = { has_building = temple_01 }
		add_building = temple_02
	}
	else_if = {
		limit = { has_building = temple_02 }
		add_building = temple_03
	}
	else_if = {
		limit = { has_building = temple_03 }
		add_building = temple_04
	}
}
