/// <summary>
    /// return true if buying is zero
    /// </summary>
    internal bool Buy(Producer buyer, PrimitiveStorageSet buying, Procent buyInTime, PrimitiveStorageSet ofWhat)
    {
        bool buyingIsEmpty = true;

        foreach (Storage what in ofWhat)
        {
            Storage consumeOnThisEteration = new Storage(what.getProduct(), what.get() * buyInTime.get());
            if (consumeOnThisEteration.get() == 0)
            {
                return(true);
            }
            // check if buying still have enoth to subtract consumeOnThisEteration
            if (!buying.has(consumeOnThisEteration))
            {
                consumeOnThisEteration = buying.findStorage(what.getProduct());
            }
            consumeOnThisEteration.multipleInside(Consume(buyer, consumeOnThisEteration, null));

            buying.subtract(consumeOnThisEteration);

            if (buying.findStorage(what.getProduct()).get() > 0)
            {
                buyingIsEmpty = false;
            }
        }
        return(buyingIsEmpty);
    }
    /// <summary>
    /// Only goods sent to market
    /// Based  on last turn data
    /// </summary>
    internal float getSupply(Product pro, bool takeThisTurnData)
    {
        float result = 0f;

        if (takeThisTurnData)
        {
            foreach (Country country in Country.allCountries)
            {
                foreach (Province province in country.ownedProvinces)
                {
                    foreach (Producer producer in province)
                    {
                        if (producer.sentToMarket.getProduct() == pro) //sup.getProduct()
                        {
                            result += producer.sentToMarket.get();
                        }
                    }
                }
            }
            return(result);
        }
        if (dateOfgetSupply != Game.date)
        {
            //recalculate supplybuffer
            foreach (Storage sup in marketPrice)
            {
                result = 0;
                foreach (Country country in Country.allCountries)
                {
                    foreach (Province province in country.ownedProvinces)
                    {
                        foreach (Producer producer in province)
                        {
                            if (producer.sentToMarket.getProduct() == sup.getProduct()) //sup.getProduct()
                            {
                                result += producer.sentToMarket.get();
                            }
                        }
                    }
                }

                getSupplyBuffer.Set(new Storage(sup.getProduct(), result));
            }
            dateOfgetSupply = Game.date;
        }
        Storage tmp = getSupplyBuffer.findStorage(pro);

        if (tmp == null)
        {
            return(0);
        }
        else
        {
            return(getSupplyBuffer.findStorage(pro).get());
        }
        //return result;
    }
    /// <summary>
    /// Result > 1 mean demand is hier, price should go up   Result feuwer 1 mean supply is hier, price should go down
    /// based on last turn data
    internal float getDemandSupplyBalance(Product pro)
    {
        float balance;

        if (dateOfDSB != Game.date)
        // recalculate DSBbuffer
        {
            foreach (Storage stor in marketPrice)
            {
                getProductionTotal(pro, false);  // for pre-turn initialization
                getTotalConsumption(pro, false); // for pre-turn initialization
                float supply = getSupply(stor.getProduct(), false);
                float demand = getBouth(stor.getProduct(), false);
                //if (demand == 0) getTotalConsumption(stor.getProduct());
                //else
                ////if (demand == 0) balance = 1f;
                ////else
                //if (supply == 0) balance = 2f;
                //else

                balance = demand / supply;

                //if (balance > 1f) balance = 1f;
                //&& supply == 0
                if (demand == 0)
                {
                    balance = 0f;              // overwise - furniture bag
                }
                // else
                if (supply == 0)
                {
                    balance = float.MaxValue;
                }
                //if (float.IsInfinity(balance)) // if divided by zero, s = zero
                //    balance = float.NaN;
                DSBbuffer.Set(new Storage(stor.getProduct(), balance));
            }
            dateOfDSB = Game.date;
        }
        Storage tmp = DSBbuffer.findStorage(pro);

        if (tmp == null)
        {
            return(float.NaN);
        }
        else
        {
            return(tmp.get());
        }
    }
    /// <summary>
    /// Based on DSB, shows how much you can get assuming you have enough money
    /// </summary>
    internal Storage HowMuchAvailable(Storage need)
    {
        //float BuyingAmountAvailable = 0;
        Storage result = tmpMarketStorage.findStorage(need.getProduct());

        if (result == null)
        {
            return(new Storage(need.getProduct(), 0f));
        }
        else
        {
            return(new Storage(result));
        }

        //BuyingAmountAvailable = need.get() / DSB;

        //float DSB = getDemandSupplyBalance(need.getProduct());
        //float BuyingAmountAvailable = 0;

        //if (DSB < 1f) DSB = 1f;
        //BuyingAmountAvailable = need.get() / DSB;

        //return new Storage(need.getProduct(), BuyingAmountAvailable);
    }
Beispiel #5
0
    internal float getInputFactor()
    {
        float          inputFactor = 1;
        List <Storage> realInput   = new List <Storage>();

        //Storage available;

        // how much we really want
        foreach (Storage input in type.resourceInput)
        {
            realInput.Add(new Storage(input.getProduct(), input.get() * getWorkForceFullFilling()));
        }

        // checking if there is enough in market
        //old DSB
        //foreach (Storage input in realInput)
        //{
        //    available = Game.market.HowMuchAvailable(input);
        //    if (available.get() < input.get())
        //        input.set(available);
        //}
        foreach (Storage input in realInput)
        {
            if (!inputReservs.has(input))
            {
                Storage found = inputReservs.findStorage(input.getProduct());
                if (found == null)
                {
                    input.set(0f);
                }
                else
                {
                    input.set(found);
                }
            }
        }
        //old last turn consumption checking thing
        //foreach (Storage input in realInput)
        //{

        //    //if (Game.market.getDemandSupplyBalance(input.getProduct()) >= 1f)
        //    //available = input

        //    available = consumedLastTurn.findStorage(input.getProduct());
        //    if (available == null)
        //        ;// do nothing - pretend there is 100%, it fires only on shownFactory start
        //    else
        //    if (!justHiredPeople && available.get() < input.get())
        //        input.set(available);
        //}
        // checking if there is enough money to pay for
        // doesn't have sense with inputReserv
        //foreach (Storage input in realInput)
        //{
        //    Storage howMuchCan = wallet.HowMuchCanAfford(input);
        //    input.set(howMuchCan.get());
        //}
        // searching lowest factor
        foreach (Storage rInput in realInput)//todo optimize - convert into for i
        {
            float newFactor = rInput.get() / (type.resourceInput.findStorage(rInput.getProduct()).get() * getWorkForceFullFilling());
            if (newFactor < inputFactor)
            {
                inputFactor = newFactor;
            }
        }

        return(inputFactor);
    }
    /// <summary>
    /// Meaning demander actyally can pay for item in current prices
    /// Basing on current prices and needs
    /// Not encounting ConsumedInMarket
    /// </summary>
    internal float getBouth(Product pro, bool takeThisTurnData)
    {
        float result = 0f;

        if (takeThisTurnData)
        {
            foreach (Country country in Country.allCountries)
            {
                foreach (Province province in country.ownedProvinces)
                {
                    foreach (Producer producer in province)
                    {
                        //if (any.c.getProduct() == sup.getProduct()) //sup.getProduct()
                        {
                            Storage re = producer.consumedInMarket.findStorage(pro);
                            if (re != null)
                            {
                                result += re.get();
                            }
                        }
                    }
                }
            }
            return(result);
        }
        if (dateOfgetBouth != Game.date)
        {
            //recalculate supplybuffer
            foreach (Storage sup in marketPrice)
            {
                result = 0;
                foreach (Country country in Country.allCountries)
                {
                    foreach (Province province in country.ownedProvinces)
                    {
                        foreach (Producer producer in province)
                        {
                            //if (any.c.getProduct() == sup.getProduct()) //sup.getProduct()
                            {
                                Storage re = producer.consumedInMarket.findStorage(sup.getProduct());
                                if (re != null)
                                {
                                    result += re.get();
                                }
                            }
                        }
                    }
                }
                getBouthBuffer.Set(new Storage(sup.getProduct(), result));
            }
            dateOfgetBouth = Game.date;
        }
        Storage tmp = getBouthBuffer.findStorage(pro);

        if (tmp == null)
        {
            return(0);
        }
        else
        {
            return(getBouthBuffer.findStorage(pro).get());
        }
        //float result = 0f;
        //foreach (Country country in Country.allCountries)
        //    foreach (Province province in country.ownedProvinces)
        //    {
        //        foreach (Producer shownFactory in province)
        //            //result += shownFactory.getLocalEffectiveDemand(pro);
        //            if (shownFactory.consumedInMarket.findStorage(pro) != null)
        //                result += shownFactory.consumedInMarket.findStorage(pro).get();


        //        //foreach (PopUnit pop in province.allPopUnits)
        //        //    //result += pop.getLocalEffectiveDemand(pro);
        //        //    if (pop.consumedInMarket.findStorage(pro) != null)
        //        //        result += pop.consumedInMarket.findStorage(pro).get();
        //        // todo add same for country and any demander
        //    }
        //return result;
    }
 internal Storage findPrice(Product whom)
 {
     return(marketPrice.findStorage(whom));
 }