swf541 / ColdWarIronCurtain

138 stars 287 forks source link

AI constantly building farms despite massive food surpluses #141

Open baldamundo opened 2 years ago

baldamundo commented 2 years ago

I noticed in my games, the major AI nations were consistently having strong GDP growth, but little or no growth in total factory numbers. Was curious what it was they were building instead, so I tag-switched to see, and they just keep on building agricultural complexes and water infrastructure.

In my latest game, by 1984, the USA has a surplus of over 2000 food, yet they still have another 12 farms currently building - more than half of their build queue!

So I decided to try and look into the code to work out why this was happening, and I think I might have found the problem:

The AI building strategy seems to be defined in common/scripted_effects/IncomeGenerator.txt https://github.com/swf541/ColdWarIronCurtain/blob/0.3-The-Crescent%2C-the-Chakra-and-the-Tiger/Cold%20War%20Iron%20Curtain/common/scripted_effects/IncomeGenerator.txt

This section seems to deal with when to build or not build farms:

    #AI will spend the ai_agri_investment on factories on agri-industry until they reach a positive food balance
    set_variable = { ai_agri_industrial_complex_target = ROOT.farm_total }
    set_variable = { beta = ROOT.num_of_civilian_factories_available_for_projects }
    add_to_variable = { beta = 1 }
    divide_variable = { beta = 7 } #one half max factories per construction project rounded down
    multiply_variable = { beta = global.ai_agri_investment }
    multiply_variable = { ai_agri_industrial_complex_target = beta }

    round_variable = ai_fossil_fuel_powerplant_target
    round_variable = ai_fossil_fuel_powerplant_target_reduced
    round_variable = ai_renewable_powerplant_target
    round_variable = ai_nuclear_powerplant_target
    round_variable = ai_office_park_target
    round_variable = ai_agri_industrial_complex_target

    #ONLY BUILD NUCLEAR/RENEWABLES IF PAST THE AI POINT#
    if = { limit = { num_of_civilian_factories_available_for_projects > global.ai_renewable_point }
        set_variable = { ai_fossil_fuel_powerplant_target = ai_fossil_fuel_powerplant_target_reduced }
    }
    else_if = {
        limit = {
            check_variable = {
                var = num_of_civilian_factories_available_for_projects
                value = global.ai_renewable_point
                compare = less_than_or_equals
            }
        }
        set_variable = { ai_nuclear_powerplant_target = 0 }
        set_variable = { ai_renewable_powerplant_target = 0 }
    }

    #DO NOT BUILD MORE FARMS THAN YOU NEED x 1.1#
    set_variable = { food_compo = food_consumption }
    multiply_variable = { food_compo = 1.1 }
    if = { limit = { check_variable = { **foodCheck** > food_compo } }
        set_variable = { ai_agri_industrial_complex_target = ROOT.farm_total }
    }

Notice at the end, the section that's meant to stop it building too many farms, it checks a desired surplus number against a value called "foodCheck". I can't find this value defined anywhere. I've searched the files for it, and the only other reference I can see is in something in IC_Industrial_Pulse.txt, but that's been commented out https://github.com/swf541/ColdWarIronCurtain/blob/0.3-The-Crescent%2C-the-Chakra-and-the-Tiger/Cold%20War%20Iron%20Curtain/common/scripted_effects/IC_Industrial_Pulse.txt

        if = { limit = { has_resources_amount = { resource = rubber amount > 0 delivered = yes } } # counts number of Agri-Complexes that actually deliver
            add_to_variable = { PREV.agri_industrial_complex_quantity = non_damaged_building_level@agri_industrial_complex }
            #if = { limit = { is_owned_by = AST } log = "AST: [THIS.GetName] - agri complexes: [?temp_agri_industrial_complex_quantity]" }      
        }
        if = {
            limit = { has_resources_amount = { resource = rubber amount > 0 }
                is_capital = no # check for leftover food in all states except capital
            }
            set_temp_variable = { temp_agri_industrial_complex_quantity = non_damaged_building_level@agri_industrial_complex } # number of Agri-Complexes in State
            set_temp_variable = { temp_food_produced = resource@rubber } # total food production in State
            subtract_from_temp_variable = { temp_food_produced = temp_agri_industrial_complex_quantity } # deduct food from Agri-Complexes
            if = {
                limit = { NOT = { check_variable = { temp_food_produced = 0 } } }
                #log = "    [ROOT.GetTag]: removed/added [?temp_food_produced] from [THIS.GetName]"
                multiply_temp_variable = { temp_food_produced = -1 }
                add_resource = {
                    type = rubber
                    amount = temp_food_produced # remove/add leftovers from State (can happen in some cases where a country loses capital to another)
                }
            }
        }
        **# add_to_variable = { PREV.foodCheck = resource@rubber }**

Searching on the Github repository, there is a deprecated file in the backup folder which defines it:

https://github.com/swf541/ColdWarIronCurtain/blob/0cf8bbbf051af0f99e269871bfcf993994cbe0c6/CWIC%20Backup/Harain%20moved%20these/common/scripted_effects/wip/IC_Energy_Pulses.txt

    set_variable = { FoodCheck = food_consumption }
    multiply_variable = { FoodCheck = -1 }

Although even then, I'm not sure I understand how the script would work - it seems like it would be checking food consumption multiplied by negative 1 against food consumption multiplied by 1.1?? I guess that works to check whether the value is positive or negative, but shouldn't it be using FoodBalanceTotal ?

Probably I've misunderstood some of this, since the scripts are very complicated, but something's definitely not right because the AI is definitely building way too many farms in my games!

baldamundo commented 2 years ago

Have also noticed the AI building what seems like way too many powerplants - although not as bad as the farm situation - but I'm finding the script for that even harder to understand, so I can't guess what the issue is

baldamundo commented 2 years ago

no matter what i do, the AI keeps on building farms.

have tried deiting various things into the parameter under #DO NOT BUILD MORE FARMS THAN YOU NEED. but it seems to make no difference. so either i'm not understanding how the script is meant to work at all (very likely) or it's broken at a deeper level than just that section