Devlog #54 - TruckSystem refactoring, bug fixing, Global Market- further dev, and connecting to the existing structure/ truckSystem, StoreSystem - new mechanic - also fixing storeSystem bugs, order amount,

 *****TruckSystem refactoring, bug fixing (industries order) , GlobalMarket futher dev ,also connecting import to the truckSystem, StoreSystem new mechanic for overfilling warehouse, and bug fixes with orders.******



TruckSystem :

First, refactoring code, separating methods, but first it was the usage of 2 confusing and different varialbes, that were causing bugs, because I mixed them up few times while writing the code hah


    public int currentCargo; // used only to remember the value over turn

    public int truckCargo;


   if (sourceIndustry.warehouseStock > 0)
                {
                    int amountToTake = Mathf.Min(truck.orderAmount, (int)sourceIndustry.warehouseStock, maxByCapacity);
                    sourceIndustry.warehouseStock -= amountToTake;
                    sourceIndustry.soldGoods += amountToTake;
                    truck.truckCargo = amountToTake;

                    //rememember the currentcargo amount for industryInfo -so it wont reset when its delivered;
                    truck.currentCargo = amountToTake;


So here is the difference. I mixed this up so many times, and still its sometimes confusing, so i had to add explanation hahah. Thing is, truckCargo resets each turn and at the end its emptied out (when goods are delivered), while , currentCargo changes each turn but keeps the value over turn, and that value is displayed in the industry/store/hub...as the infomration to what the truck is carying.

Now the simple question is, why doesnt the truckCargo do the same, but its because truckCargo is temporary to hold while its transporting goods afterwhich is that same item removed back from it. Its to carry the goods, but , because its ina  single turn, the end value after the turn is gone as well. Like this its another way to keep the value stick to the next day.


Another bug with truckSystem,  was the reseting orders each day. And this was way bigger issue and really hard to find.

PROBLEM : State owned industries( or future private industries) function based upon the demand, if the demand is higher what they produce, the industry grows to the next tier. Here it was the issue that one industry didnt, specifically the first one. But all others were working flawlessly.. So at first, chaning orders, trying to figure out the issue, and then i uncovered even worse bug  then before. 

Problem was that the 'resetallorders' 

        GlobalMarketSystem.Tick();

        // TruckSystem.ResetIndustryInventoryOrderForTrucks();

method. Basically, this was being done before all cities in CityManager as you know from before.

      GlobalMarketSystem.Tick();

        -HERE-        // TruckSystem.ResetIndustryInventoryOrderForTrucks();
        for (int c = 0; c < WorldManager.Instance.runtimeCities.Count; c++)
        {

So this reset all industries and all orders, so then the trucck can fill up the orders. So after the turn, the orders appear as the incomingOrders in the industry. Which directly impacts the growth or not.

Of course in this case this wasn't working, because, after the last city, the trucks in the last city would write onto the industry that is in the first city for example, that order is 500 goods for example. But then, the reset happens, and the industry reads 0. But i see, and i see that there is still500 ? How?

so turn 1, truck sets the order, end of the turn - says 500. 

Then next turn, first it resets to 0, then industry reads 0, says no need to increase size. at the end of this turn, it again says 500, but the industry never reads, because industry reads it at the begining of the turn, after it has been reseted. Does that makes sense? this was really hard to pinpoint. and also caused me to find even worse issue.

PROBLEM 2: After the first issue, i thought ok, let it reset after the industry read in the first city, and then reset all, its win win, doesnt matter. But then, that was working only in case that there was a hub in the first city..and if in another, it would constantly override all the values, and none of the values would be true, constantly! It was such a messy bug, that took me so much to actually run all the possible issues in my head to actually figure out a scope of the issue. Becuase again, a reminder, each turn, the for loop goes through all cities and does all systems, industry, store , market, office.. then the next city. 
Because of this, what happens in the first, must not overwrite second, or third must not first, but if the truck is in the third city, it has to write back to the first, or fifth. So the reset, was no way to be fixed, because, for example: Industry in city 2,  the loop goes through it, and it reads order for nextDay is 1000 , but then the city loop goes on , so we are in the 4th city, then it says oh,there is more order for the trucks, assigned to the city 2,  but the industry was reading only old values. See? broken!!! 

Anyhow, the solution was way simplerer then the actual issue and bug hunting. This was really really annoying. Anyhow,

Simply adding 

    public int industryOrder;

    public int industryAccumulatedOrder;

So one turn, the trucks actually give in the industryAccumulatedOrder, then at the end of the turn, all the accumulated orders are just transfered to the industryOrder. AND THEN RESETED at the end of the turn. So next day, the industryReads the orders from the last day, while industryAccumulated orders accumulate yet again. Same it was done for store, same issue occured. Fixed! 


Also for code cleanliness the method was moved within the truckSystem, because it resets all orders for everything, universal, and related to truck. Otherwise it would be consfusing, wether industry for industry, or store or others... 


________________________________________________________________________________


GlobalMarket futerhdev:

So I think we discused the structure before, but the GlobalMarket, (WITHOUT SPOILING), is actually same structure as every other sistem, its a list of products that we already have, and it will have methods and ways to move prices around, and also affect the local industries prices etc. Just a small reminder to what it is. Technically speaking, its a list with products, that is on a GlobalProductState, a struct, which contains values similarly to MarketState, only this is global, which has some different variables, then the usual cityMarketState. Like, sellingPrice, buyingPrice  for( import/export) etc..

Anyhow, now we had to connec the truck to order from the globalMarket. sounds complicated right? But its acutally ingenial to this system that exist, and quite simple!

Since the globalMarket is a list of products, the truck just simply has to find the product in that list, by the universal productType enum,  and then do the same as it was for industry etc..only it loads the selling price, and later when it would export, then its buying price. The border is buying and selling. that is the logic.

Anyhow,


            case LocationType.Import:

                var wm = WorldManager.Instance;

                //get global product
                GlobalProductState globalProduct = wm.globalMarketProducts[(int)truck.productType];

                //calculate amount to be taken
                int amountToImport = Mathf.Min(truck.orderAmount, maxByCapacity);
                truck.truckCargo = amountToImport;
                truck.allGoodsValue = amountToImport * globalProduct.sellingPrice;


So it matches the product form the list by the productType, then takes the amount and uses the sellingPrice. Simple! 

The bug that we had, was the if you remember long time ago, the truck needs sourceLocationID ,and sourceLocationCityID ,  which is the way it finds the list and data we need for it to take away from. But, this time, we would use the truck.cityIndex, which is the city from where the truck is, so it wont break the game and show the exception if we put a random city number, that doesnt exist. On the other hand, I put 500, for the sourceLocationId, because my guess is that there wont be 500 industries, if it so happens in the future, i will increase the number. its just a placeholder now, and if one player ever reaches having 500 industries, i will change it :D Anyhow, that was how it was solved. So truck then takes the cityIndex as the sourceLocationCityId, and 500 as the sourceLocationID. Simple yet effective! 

________________________________________________________________________________




StoreSystem : new Mechanic, penalty for overfilling 

This one was straighforward addon to this system, but i thought about the way and tested out the 'punishment'. There were more ways to solve this, the trucks wouldn't deliver,or get paused, or whatnot, lots of solutions. But i think simpler, and also more effective if player doesnt pay attentiont to game, and 'emails :D" it would basically punsih overfilling the store inventory. So if the price is too high, less goods are sold, but trucks keep bringing goods, and if player even ignores the emails and warnings that its almostfull, and then overfilled. well , thats the business! haha

Anyhow, 

 private static void CheckStoreInventory(ref StoreState store, string cityName)
    {
        var wm = WorldManager.Instance;
        float inventoryFullnessRatio = (float)store.storeCurrentInventory / (float)store.storeInventoryCapacity;

        // Debug.Log($"Store Inventory ratio is : {inventoryFullnessRatio}");

        if (inventoryFullnessRatio > 1)
        {

And yes , i stopped before revealing the method. but i can say ,its exponential. So the more its overfilled the more penalty there is, which is realistic i think hahah. Anyhow, the cost is multiplied by the dailystoreUpkeep, so its also balanced, for bigger stores, bigger penalty. For smaller smaller. And it works great! 


And yes as mentioned before, here we had the same bug with orders for the next day, so again, same principle. 


Thank you!!! 



Comments

Popular posts from this blog

Devlog #47 - TaxSystem/FinanceSystem ,Update-FIx -LoanFixes-- TruckSystem Update - Upgrade/BuyNEwTruck - Costs Prices introduced / OfficeSystem CreateNewOffice, upgrade office - other Fixes

Devlog #46 - HubSystem - CreatNewHub added, HubBuild Ui + INdustrySystem Update - -INdustryTierData to be combined with the IndustryData and IndustryState struct -

Devlog #49-50 - Asset System-NEW system, *Game Design* -construction costs update - LoanSystem- fix-redesign-upgrade,Bug fixes-storeSystem,AutoAdvance Day, Market system, Truck System cost redesign, refactored, Ui bug fixes