﻿# 根据trait、perk等区分建造花费
aba_building_cost_effect = { #扣钱
    if = {
        limit = {
            prev = {
                has_trait = architect
            }
        }
        prev = {
            # remove_short_term_gold = cheap_building_tier_$LEVEL$_cost
            remove_treasury_or_gold = cheap_building_tier_$LEVEL$_cost
        }
    }
    else_if = {
        limit = {
            prev = {
                has_perk = cutting_corners_perk
            }
        }
        prev = {
            # remove_short_term_gold = abacheap_building_tier_$LEVEL$_cost
            remove_treasury_or_gold = abacheap_building_tier_$LEVEL$_cost
        }
    }
    else = {
        prev = {
            # remove_short_term_gold = abacommon_building_tier_$LEVEL$_cost
            remove_treasury_or_gold = abacommon_building_tier_$LEVEL$_cost
        }
    }
}

aba_building_upgrade_root_effect = { #直辖自动升级
    random_directly_owned_province = {
        limit = {
            province_owner = root
            barony = { is_leased_out = no }
			has_holding = yes
            aba_save_resource_trigger = yes
            NOT = { has_province_modifier = aba_recent_build_modifier }
        }
        aba_building_upgrade_scripted_effect = yes
        add_province_modifier = {
            modifier = aba_recent_build_modifier
            days = aba_building_recent
        }
    }
}

aba_building_upgrade_county_effect = { #直辖伯爵领自动升级
    random_realm_province = {
        limit = {
            OR = {
                province_owner = { 
                    is_vassal_of = root 
                    highest_held_title_tier = tier_barony
                }
                province_owner = root
            }
			has_holding = yes
            aba_save_resource_trigger = yes
            NOT = { has_province_modifier = aba_recent_build_modifier }
        }
        aba_building_upgrade_scripted_effect = yes
        add_province_modifier = {
            modifier = aba_recent_build_modifier
            days = aba_building_recent
        }
    }
}

aba_building_upgrade_vassal_effect = { #领地自动升级
    random_realm_province = {
        limit = {
            OR = {
                province_owner = { is_vassal_or_below_of = root }
                province_owner = root
            }
			has_holding = yes
            aba_save_resource_trigger = yes
            NOT = { has_province_modifier = aba_recent_build_modifier }
        }
        aba_building_upgrade_scripted_effect = yes
        add_province_modifier = {
            modifier = aba_recent_build_modifier
            days = aba_building_recent
        }
    }
}

aba_building_upgrade_scripted_effect = { # 自动升级effect
    aba_building_upgrade_effect = yes
    aba_holding_upgrade_effect = yes
    # save_scope_as = aba_building_province
    # province_owner = {
    #     send_interface_message = {
    #         type = aba_building_completed
    #         #left_icon = this
    #     }
    # }
}

aba_building_upgrade_effect = { # 自动升级 建筑
    aba_building_upgrade_effect_vanilla = yes # 原版
    aba_building_upgrade_effect_uba = yes # 奇观建筑师
    aba_building_upgrade_effect_mbrxxx = yes # More Building系列
    aba_building_upgrade_effect_oe = yes # 东方王朝
    aba_building_upgrade_effect_oeex = yes # 东方王朝文化兵种拓展（你为什么要写建筑？）
    aba_building_upgrade_effect_agot = yes # A Game of Throne 冰与火之歌
    aba_building_upgrade_effect_hm = yes # 幻萌 一年战争
    aba_building_upgrade_effect_comr = yes #共产宗教
}

aba_holding_upgrade_effect = { # 自动升级 地产
    aba_holding_upgrade_effect_vanilla = yes # 原版
    aba_holding_upgrade_effect_comr = yes # 共产宗教
}

aba_holding_upgrade_effect_vanilla = { # 原版的地产
    if = {
        limit = { has_building_or_higher = castle_01 }
        aba_vanilla_holding_upgrade_effect_castle = { BUILDING = castle }
    }
    else_if = {
        limit = { has_building_or_higher = city_01 }
        aba_vanilla_holding_upgrade_effect_city = { BUILDING = city }
    }
    else_if = {
        limit = { has_building_or_higher = temple_01 }
        aba_vanilla_holding_upgrade_effect_city = { BUILDING = temple }
    }
    else_if = {
        limit = { has_building_or_higher = temple_citadel_01 }
        aba_vanilla_holding_upgrade_effect_city = { BUILDING = temple_citadel }
    }
    else_if = {
        limit = { has_building_or_higher = tribe_01 }
        aba_vanilla_holding_upgrade_effect_tribe = { BUILDING = tribe }
    }
}

aba_vanilla_holding_upgrade_effect_castle = { # 原版地产 城堡
	if = { #castle
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_2_cost }
            has_building = $BUILDING$_01
            prev.culture = {
                has_innovation = innovation_battlements
            }
        }
		add_building = $BUILDING$_02
        prev = { remove_treasury_or_gold = main_building_tier_2_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_3_cost }
            has_building = $BUILDING$_02
            prev.culture = {
                has_innovation = innovation_hoardings
            }
        }
		add_building = $BUILDING$_03
        prev = { remove_treasury_or_gold = main_building_tier_3_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_4_cost }
            has_building = $BUILDING$_03
            prev.culture = {
                has_innovation = innovation_machicolations
            }
        }
		add_building = $BUILDING$_04
        prev = { remove_treasury_or_gold = main_building_tier_4_cost }
	}
}

aba_vanilla_holding_upgrade_effect_city = { # 原版地产 城市/神殿/寺庙城塞
	if = { #city/temple
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_2_cost }
            has_building = $BUILDING$_01
            prev.culture = {
                has_innovation = innovation_manorialism
            }
        }
		add_building = $BUILDING$_02
        prev = { remove_treasury_or_gold = main_building_tier_2_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_3_cost }
            has_building = $BUILDING$_02
            prev.culture = {
                has_innovation = innovation_windmills
            }
        }
		add_building = $BUILDING$_03
        prev = { remove_treasury_or_gold = main_building_tier_3_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > main_building_tier_4_cost }
            has_building = $BUILDING$_03
            prev.culture = {
                has_innovation = innovation_cranes
            }
        }
		add_building = $BUILDING$_04
        prev = { remove_treasury_or_gold = main_building_tier_4_cost }
	}
    if = { #tribal
		limit = {
            prev = { 
                short_term_treasury_or_gold > main_building_tier_2_cost 
                prestige > expensive_building_tier_3_cost
            }
            has_building = $BUILDING$_01
            prev.culture = {
                has_innovation = innovation_plenary_assemblies
            }
        }
		add_building = $BUILDING$_02
        prev = {
            remove_treasury_or_gold = main_building_tier_2_cost 
            add_prestige_no_experience = -500
        }
	}
}

aba_vanilla_holding_upgrade_effect_tribe = { # 原版地产 部落
    if = { #tribal
		limit = {
            prev = { 
                short_term_treasury_or_gold > main_building_tier_2_cost 
                prestige > expensive_building_tier_3_cost
            }
            has_building = $BUILDING$_01
            prev.culture = {
                has_innovation = innovation_plenary_assemblies
            }
        }
		add_building = $BUILDING$_02
        prev = {
            remove_treasury_or_gold = main_building_tier_2_cost 
            add_prestige_no_experience = -500
        }
	}
}

aba_building_upgrade_effect_vanilla = { # 原版的建筑
    # economy
    if = {
        limit = { has_building_or_higher = caravanserai_01 }
        aba_economy_building_upgrade_effect = { BUILDING = caravanserai }
    }
    if = {
        limit = { has_building_or_higher = watermills_01 }
        aba_economy_building_upgrade_effect = { BUILDING = watermills }
    }
    if = {
        limit = { has_building_or_higher = windmills_01 }
        aba_economy_building_upgrade_effect = { BUILDING = windmills }
    }
    if = {
        limit = { has_building_or_higher = farm_estates_01 }
        aba_economy_building_upgrade_effect = { BUILDING = farm_estates }
    }
    if = {
        limit = { has_building_or_higher = cereal_fields_01 }
        aba_economy_building_upgrade_effect = { BUILDING = cereal_fields }
    }
    if = {
        limit = { has_building_or_higher = orchards_01 }
        aba_economy_building_upgrade_effect = { BUILDING = orchards }
    }
    if = {
        limit = { has_building_or_higher = qanats_01 }
        aba_economy_building_upgrade_effect = { BUILDING = qanats }
    }
    if = {
        limit = { has_building_or_higher = common_tradeport_01 }
        aba_economy_building_upgrade_effect = { BUILDING = common_tradeport }
    }
    if = {
        limit = { has_building_or_higher = pastures_01 }
        aba_economy_building_upgrade_effect = { BUILDING = pastures }
    }
    if = {
        limit = { has_building_or_higher = hunting_grounds_01 }
        aba_economy_building_upgrade_effect = { BUILDING = hunting_grounds }
    }
    if = {
        limit = { has_building_or_higher = logging_camps_01 }
        aba_economy_building_upgrade_effect = { BUILDING = logging_camps }
    }
    if = {
        limit = { has_building_or_higher = peat_quarries_01 }
        aba_economy_building_upgrade_effect = { BUILDING = peat_quarries }
    }
    if = {
        limit = { has_building_or_higher = hill_farms_01 }
        aba_economy_building_upgrade_effect = { BUILDING = hill_farms }
    }
    if = {
        limit = { has_building_or_higher = elephant_pens_01 }
        aba_economy_building_upgrade_effect = { BUILDING = elephant_pens }
    }
    if = {
        limit = { has_building_or_higher = plantations_01 }
        aba_economy_building_upgrade_effect = { BUILDING = plantations }
    }
    if = {
        limit = { has_building_or_higher = quarries_01 }
        aba_economy_building_upgrade_effect = { BUILDING = quarries }
    }
    if = {
        limit = { has_building_or_higher = murex_farm_01 }
        aba_economy_building_upgrade_effect = { BUILDING = murex_farm }
    }
    if = {
        limit = { has_building_or_higher = capital_bureau_01 }
        aba_economy_building_upgrade_effect = { BUILDING = capital_bureau }
    }
    if = {
        limit = { has_building_or_higher = paddy_fields_01 }
        aba_economy_building_upgrade_effect = { BUILDING = paddy_fields }
    }
    # military
    if = {
        limit = { has_building_or_higher = wind_furnace_01 }
        aba_military_building_upgrade_effect = { BUILDING = wind_furnace }
    }
    if = {
        limit = { has_building_or_higher = regimental_grounds_01 }
        aba_military_building_upgrade_effect = { BUILDING = regimental_grounds }
    }
    if = {
        limit = { has_building_or_higher = smiths_01 }
        aba_military_building_upgrade_effect = { BUILDING = smiths }
    }
    if = {
        limit = { has_building_or_higher = workshops_01 }
        aba_military_building_upgrade_effect = { BUILDING = workshops }
    }
    if = {
        limit = { has_building_or_higher = horse_pastures_01 }
        aba_military_building_upgrade_effect = { BUILDING = horse_pastures }
    }
    if = {
        limit = { has_building_or_higher = hillside_grazing_01 }
        aba_military_building_upgrade_effect = { BUILDING = hillside_grazing }
    }
    if = {
        limit = { has_building_or_higher = warrior_lodges_01 }
        aba_military_building_upgrade_effect = { BUILDING = warrior_lodges }
    }
    if = {
        limit = { has_building_or_higher = stables_01 }
        aba_military_building_upgrade_effect = { BUILDING = stables }
    }
    if = {
        limit = { has_building_or_higher = military_camps_01 }
        aba_military_building_upgrade_effect_2 = { BUILDING = military_camps }
    }
    if = {
        limit = { has_building_or_higher = barracks_01 }
        aba_military_building_upgrade_effect_2 = { BUILDING = barracks }
    }
    if = {
        limit = { has_building_or_higher = camel_farms_01 }
        aba_military_building_upgrade_effect_2 = { BUILDING = camel_farms }
    }
    if = {
        limit = { has_building_or_higher = outposts_01 }
        aba_military_building_upgrade_effect_2 = { BUILDING = outposts }
    }
    # fortification
    if = {
        limit = { has_building_or_higher = ramparts_01 }
        aba_fortification_building_upgrade_effect = { BUILDING = ramparts }
    }
    if = {
        limit = { has_building_or_higher = curtain_walls_01 }
        aba_fortification_building_upgrade_effect = { BUILDING = curtain_walls }
    }
    if = {
        limit = { has_building_or_higher = watchtowers_01 }
        aba_fortification_building_upgrade_effect = { BUILDING = watchtowers }
    }
    if = {
        limit = { has_building_or_higher = hill_forts_01 }
        aba_fortification_building_upgrade_effect = { BUILDING = hill_forts }
    }
    # city/temple/common
    if = {
        limit = { has_building_or_higher = hospices_01 }
        aba_common_building_upgrade_effect = { BUILDING = hospices }
    }
    if = {
        limit = { has_building_or_higher = guild_halls_01 }
        aba_common_building_upgrade_effect = { BUILDING = guild_halls }
    }
    if = {
        limit = { has_building_or_higher = scriptorium_01 }
        aba_common_building_upgrade_effect = { BUILDING = scriptorium }
    }
    if = {
        limit = { has_building_or_higher = monastic_schools_01 }
        aba_common_building_upgrade_effect = { BUILDING = monastic_schools }
    }
    if = {
        limit = { has_building_or_higher = breweries_01 }
        aba_common_building_upgrade_effect = { BUILDING = breweries }
    }
    if = { # 巨石阵建造要求不同，但是单独写一个有点麻烦，就这样了
        limit = { has_building_or_higher = megalith_01 }
        aba_common_building_upgrade_effect = { BUILDING = megalith }
    }
    # 寺庙城塞
    if = {
        limit = { has_building_or_higher = citadel_shrine_01 }
        aba_common_building_upgrade_effect = { BUILDING = citadel_shrine }
    }
    if = {
        limit = { has_building_or_higher = sacred_pool_01 }
        aba_common_building_upgrade_effect = { BUILDING = sacred_pool }
    }
    if = {
        limit = { has_building_or_higher = vihara_halls_01 }
        aba_common_building_upgrade_effect = { BUILDING = vihara_halls }
    }
    # default
    if = {
        limit = { has_building_or_higher = waterworks_01 }
        aba_default_building_upgrade_effect = { BUILDING = waterworks }
    }
    if = {
        limit = { has_building_or_higher = spice_plantation_01 }
        aba_default_building_upgrade_effect = { BUILDING = spice_plantation }
    }
    if = {
        limit = { has_building_or_higher = powder_magazine_01 }
        aba_default_building_upgrade_effect = { BUILDING = powder_magazine }
    }
    # tribal
    if = {
        limit = { has_building_or_higher = longhouses_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = longhouses }
    }
    if = {
        limit = { has_building_or_higher = war_camps_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = war_camps }
    }
    if = {
        limit = { has_building_or_higher = palisades_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = palisades }
    }
    if = {
        limit = { has_building_or_higher = market_villages_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = market_villages }
    }
    if = {
        limit = { has_building_or_higher = idjang_forts_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = idjang_forts }
    }
    if = {
        limit = { has_building_or_higher = wantilan_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = wantilan }
    }
    if = {
        limit = { has_building_or_higher = kora_kora_yards_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = kora_kora_yards }
    }
    if = {
        limit = { has_building_or_higher = water_temples_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = water_temples }
    }
    if = {
        limit = { has_building_or_higher = stilted_granaries_01 }
        aba_tribal_building_upgrade_effect = { BUILDING = stilted_granaries }
    }

}

aba_economy_building_upgrade_effect = { #第二级要求是轮作
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
            prev.culture = {
                has_innovation = innovation_crop_rotation
            }
        }
		add_building = $BUILDING$_02
        aba_building_cost_effect = { LEVEL = 2 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_manorialism
            }
        }
		add_building = $BUILDING$_03
        aba_building_cost_effect = { LEVEL = 3 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_manorialism
            }
        }
		add_building = $BUILDING$_04
        aba_building_cost_effect = { LEVEL = 4 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_windmills
            }
        }
		add_building = $BUILDING$_05
        aba_building_cost_effect = { LEVEL = 5 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_windmills
            }
        }
		add_building = $BUILDING$_06
        aba_building_cost_effect = { LEVEL = 6 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_cranes
            }
        }
		add_building = $BUILDING$_07
        aba_building_cost_effect = { LEVEL = 7 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_cranes
            }
        }
		add_building = $BUILDING$_08
        aba_building_cost_effect = { LEVEL = 8 }
	}
}

aba_military_building_upgrade_effect = {
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
            prev.culture = {
                has_innovation = innovation_barracks
            }
        }
		add_building = $BUILDING$_02
        aba_building_cost_effect = { LEVEL = 2 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_burhs
            }
        }
		add_building = $BUILDING$_03
        aba_building_cost_effect = { LEVEL = 3 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_burhs
            }
        }
		add_building = $BUILDING$_04
        aba_building_cost_effect = { LEVEL = 4 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_castle_baileys
            }
        }
		add_building = $BUILDING$_05
        aba_building_cost_effect = { LEVEL = 5 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_royal_armory
            }
        }
		add_building = $BUILDING$_06
        aba_building_cost_effect = { LEVEL = 6 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_royal_armory
            }
        }
		add_building = $BUILDING$_07
        aba_building_cost_effect = { LEVEL = 7 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_royal_armory
            }
        }
		add_building = $BUILDING$_08
        aba_building_cost_effect = { LEVEL = 8 }
	}
}

aba_military_building_upgrade_effect_2 = { #6级不需要君主军械库的
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
            prev.culture = {
                has_innovation = innovation_barracks
            }
        }
		add_building = $BUILDING$_02
        aba_building_cost_effect = { LEVEL = 2 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_burhs
            }
        }
		add_building = $BUILDING$_03
        aba_building_cost_effect = { LEVEL = 3 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_burhs
            }
        }
		add_building = $BUILDING$_04
        aba_building_cost_effect = { LEVEL = 4 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_castle_baileys
            }
        }
		add_building = $BUILDING$_05
        aba_building_cost_effect = { LEVEL = 5 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_castle_baileys
            }
        }
		add_building = $BUILDING$_06
        aba_building_cost_effect = { LEVEL = 6 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_royal_armory
            }
        }
		add_building = $BUILDING$_07
        aba_building_cost_effect = { LEVEL = 7 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_royal_armory
            }
        }
		add_building = $BUILDING$_08
        aba_building_cost_effect = { LEVEL = 8 }
	}
}

aba_fortification_building_upgrade_effect = {
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
            prev.culture = {
                has_innovation = innovation_motte
            }
        }
		add_building = $BUILDING$_02
        prev = { remove_treasury_or_gold = cheap_building_tier_2_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_battlements
            }
        }
		add_building = $BUILDING$_03
        prev = { remove_treasury_or_gold = cheap_building_tier_3_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_battlements
            }
        }
		add_building = $BUILDING$_04
        prev = { remove_treasury_or_gold = cheap_building_tier_4_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_hoardings
            }
        }
		add_building = $BUILDING$_05
        prev = { remove_treasury_or_gold = cheap_building_tier_5_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_hoardings
            }
        }
		add_building = $BUILDING$_06
        prev = { remove_treasury_or_gold = cheap_building_tier_6_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_machicolations
            }
        }
		add_building = $BUILDING$_07
        prev = { remove_treasury_or_gold = cheap_building_tier_7_cost }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > cheap_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_machicolations
            }
        }
		add_building = $BUILDING$_08
        prev = { remove_treasury_or_gold = cheap_building_tier_8_cost }
	}
}

aba_common_building_upgrade_effect = { # 第二级要求是城市规划
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
            prev.culture = {
			    has_innovation = innovation_city_planning
            }
        }
		add_building = $BUILDING$_02
        aba_building_cost_effect = { LEVEL = 2 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_manorialism
            }
        }
		add_building = $BUILDING$_03
        aba_building_cost_effect = { LEVEL = 3 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
            prev.culture = {
                has_innovation = innovation_manorialism
            }
        }
		add_building = $BUILDING$_04
        aba_building_cost_effect = { LEVEL = 4 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_windmills
            }
        }
		add_building = $BUILDING$_05
        aba_building_cost_effect = { LEVEL = 5 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
            prev.culture = {
                has_innovation = innovation_windmills
            }
        }
		add_building = $BUILDING$_06
        aba_building_cost_effect = { LEVEL = 6 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_cranes
            }
        }
		add_building = $BUILDING$_07
        aba_building_cost_effect = { LEVEL = 7 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
            prev.culture = {
                has_innovation = innovation_cranes
            }
        }
		add_building = $BUILDING$_08
        aba_building_cost_effect = { LEVEL = 8 }
	}
}

aba_default_building_upgrade_effect = { # 不需要革新就能升级的
	if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_2_cost }
            has_building = $BUILDING$_01
            building_requirement_castle_city_church = { LEVEL = 01 }
        }
		add_building = $BUILDING$_02
        aba_building_cost_effect = { LEVEL = 2 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_3_cost }
            has_building = $BUILDING$_02
            building_requirement_castle_city_church = { LEVEL = 02 }
        }
		add_building = $BUILDING$_03
        aba_building_cost_effect = { LEVEL = 3 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_4_cost }
            has_building = $BUILDING$_03
            building_requirement_castle_city_church = { LEVEL = 02 }
        }
		add_building = $BUILDING$_04
        aba_building_cost_effect = { LEVEL = 4 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_5_cost }
            has_building = $BUILDING$_04
            building_requirement_castle_city_church = { LEVEL = 03 }
        }
		add_building = $BUILDING$_05
        aba_building_cost_effect = { LEVEL = 5 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_6_cost }
            has_building = $BUILDING$_05
            building_requirement_castle_city_church = { LEVEL = 03 }
        }
		add_building = $BUILDING$_06
        aba_building_cost_effect = { LEVEL = 6 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_7_cost }
            has_building = $BUILDING$_06
            building_requirement_castle_city_church = { LEVEL = 04 }
        }
		add_building = $BUILDING$_07
        aba_building_cost_effect = { LEVEL = 7 }
	}
	else_if = {
		limit = {
            prev = { short_term_treasury_or_gold > abacommon_building_tier_8_cost }
            has_building = $BUILDING$_07
            building_requirement_castle_city_church = { LEVEL = 04 }
        }
		add_building = $BUILDING$_08
        aba_building_cost_effect = { LEVEL = 8 }
	}
}

aba_tribal_building_upgrade_effect = {
	if = {
		limit = {
            prev = {
                short_term_treasury_or_gold > tribal_building_tier_2_cost 
                prestige > expensive_building_tier_2_cost
            }
            has_building = $BUILDING$_01
            prev.culture = {
                has_innovation = innovation_barracks
                has_innovation = innovation_motte
                has_innovation = innovation_city_planning
            }
        }
		add_building = $BUILDING$_02
        prev = {
            remove_treasury_or_gold = tribal_building_tier_2_cost
            add_prestige_no_experience = -350
        }
	}
}
