﻿# Camp and Estate Auto-Build Effects
# Upgrades domicile buildings with priority system
# Uses custom tier cost script_values

# Automatically upgrades camp buildings
clrbnit_magic_auto_upgrade_camps_effect = {
	if = {
		limit = { has_domicile = yes }
		every_domicile = {
			save_scope_as = clrbnit_current_domicile
			if = {
				limit = { 
					AND = {
						is_domicile_type = camp
						has_domicile_building = camp_main_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_main_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_main_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = camp_main_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = camp_main_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_main_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_main_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = camp_main_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = camp_main_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_main_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_main_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = camp_main_04 }
					}
				}
			}
			
			# SUPPLY TENT - Priority 2
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = supply_tent_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = supply_tent_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = supply_tent_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = supply_tent_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = supply_tent_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = supply_tent_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = supply_tent_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = supply_tent_05 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = supply_tent_05
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_6_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_6_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = supply_tent_06 }
					}
				}
			}
			
			# BAGGAGE TRAIN - Priority 3
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = baggage_train_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = baggage_train_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = baggage_train_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = baggage_train_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = baggage_train_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = baggage_train_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = baggage_train_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = baggage_train_05 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = baggage_train_05
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_6_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_6_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = baggage_train_06 }
					}
				}
			}
			
			# MESS TENT - Priority 4
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = mess_tent_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = mess_tent_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = mess_tent_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = mess_tent_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = mess_tent_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = mess_tent_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = mess_tent_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = mess_tent_05 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = mess_tent_05
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_6_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_6_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = mess_tent_06 }
					}
				}
			}
			
			# BARBER TENT - Priority 5
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = barber_tent_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barber_tent_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = barber_tent_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barber_tent_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = barber_tent_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barber_tent_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = barber_tent_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barber_tent_05 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = camp
						has_domicile_building = barber_tent_05
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_camp_support_tier_6_cost } }
						add_gold = { value = 0 subtract = clrbnit_camp_support_tier_6_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barber_tent_06 }
					}
				}
			}
		}
	}
}

clrbnit_magic_auto_upgrade_estates_effect = {
	if = {
		limit = { has_domicile = yes }
		every_domicile = {
			save_scope_as = clrbnit_current_domicile
			if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = estate_main_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_main_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_main_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = estate_main_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = estate_main_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_main_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_main_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = estate_main_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = estate_main_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_main_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_main_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = estate_main_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = estate_main_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_main_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_main_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = estate_main_05 }
					}
				}
			}
			
			# BARRACKS - Priority 2
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = barracks_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_barracks_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_barracks_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barracks_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = barracks_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_barracks_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_barracks_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barracks_03 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = barracks_03
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_barracks_tier_4_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_barracks_tier_4_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barracks_04 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = barracks_04
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_barracks_tier_5_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_barracks_tier_5_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barracks_05 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = barracks_05
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_barracks_tier_6_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_barracks_tier_6_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = barracks_06 }
					}
				}
			}
			
			# GARDEN - Priority 3 (Tiers 01-03 only)
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = garden_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_garden_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_garden_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = garden_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = garden_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_garden_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_garden_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = garden_03 }
					}
				}
			}
			
			# WORKSHOP - Priority 4 (Tiers 01-02 only)
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = workshop_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_workshop_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_workshop_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = workshop_02 }
					}
				}
			}
			
			# STABLE - Priority 5 (Tiers 01-03 only)
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = stable_01
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_stable_tier_2_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_stable_tier_2_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = stable_02 }
					}
				}
			}
			else_if = {
				limit = {
					AND = {
						is_domicile_type = estate
						has_domicile_building = stable_02
					}
				}
				owner = {
					if = {
						limit = { gold >= { value = clrbnit_estate_stable_tier_3_cost } }
						add_gold = { value = 0 subtract = clrbnit_estate_stable_tier_3_cost }
						scope:clrbnit_current_domicile = { add_domicile_building = stable_03 }
					}
				}
			}
		}
	}
}
