[English] Tuning building production

  • It's quite a work to do it for all buisnesses/productions and you need to do it by hand. You have to use the building-scripts, e.g. Scripts/Buildings/Joiner.lua.



    Add "joiner_SetupAI("")" on top of the functions PingHour(), Setup() and LevelUp().



    This means that the SetupAI-function will be called on build, on levelup and every ingamehour. This is important because we need to adjust the production as often as possible to make it intelligent.



    Now add a function SetupAI(Alias)



    this function has to contain the buildings inventory:



    -- Tune the production
    if not GetInventory(Alias, INVENTORY_STD, "Inv") then
    return
    end



    We also need the City and the local market (I chose the local market because you can set your AI management to local market, the AI will sell to all cities so the production decision will not necessarily the best but still do the trick)



    if not GetSettlement(Alias, "City") then
    GetNearestSettlement(Alias, "City")
    end
    -- get the local market for stock-dependent production
    CityGetLocalMarket("City","Market")
    if not AliasExists("Market") then
    return
    end



    Now begins the fun part. Via properties we can influence the production decision.



    SetProperty("Inv", "Need_79", 14) -- OakwoodRing



    This will boost the production value by 14 for item 79 (DB/items) which is OakwoodRing



    You could do this with every item with higher values for higher value-items. This would be okay, but what if the current prices are bad? Also if you set it this static way the building basicly would always produce the same item, unless you level up/ add new upgrades.



    This is why I make it dependent on the local market stock:



    if GetItemCount("Market", "Mace", INVENTORY_STD)<15 then
    SetProperty("Inv", "Need_83", 12) -- Mace
    else
    SetProperty("Inv", "Need_83", 5)
    end



    And to keep this clean and only add properties to buildings that can actually produce an item, i will add this:



    -- level 2
    if BuildingHasUpgrade(Alias, "StrainingMachine") then
    if GetItemCount("Market", "Mace", INVENTORY_STD)<15 then
    SetProperty("Inv", "Need_83", 12) -- Mace
    else
    SetProperty("Inv", "Need_83", 5)
    end
    end



    Sometimes it can also be a good idea to boost the production for intermediate products we need for further production. So instead of looking at the market stock we simply check our own inventory



    -- **********We need some products in stock to keep the production going***********

    if GetItemCount(Alias, "Holzzapfen", INVENTORY_STD)<=10 then
    SetProperty("Inv", "Need_903", 20) -- fitting
    else
    SetProperty("Inv", "Need_903", 1)
    end



    if you like to, you could also boost the sell-stock. Again I make it dependent on actual stock instead of a static value



    -- sell stock

    if not GetInventory(Alias, INVENTORY_SELL, "InvSell") then
    return
    end

    if GetItemCount(Alias, "GrainPap", INVENTORY_SELL)<5 then
    SetProperty("InvSell", "Need_41", 20) -- GrainPap
    else
    SetProperty("InvSell", "Need_41", 1)
    end

    if GetItemCount(Alias, "SmallBeer", INVENTORY_SELL)<5 then
    SetProperty("InvSell", "Need_42", 20) -- SmallBeer
    else
    SetProperty("InvSell", "Need_42", 1)
    end



    That's it. Quite a work but the AI will produce FAR better than before. Ofc this is probably not the best solution - you could find better conditions to tune the production. For example I also tried making it dependent of the raw material you have currently in your workshop (if you have iron, make tools, for example). But this was rather complicated and didn't had the result i hoped for.



    You also need to tune the number the market has on stock, because this number (15) is chosen because of the megamod-market balance (which is, normal artifacts will sell for higher than base price as long as there are less than 15 items on stock).



    If you have any questions go ahead