Exemple #1
0
 /// <summary>
 /// returns how much was sold de facto
 /// new version of buy-old,
 /// real deal. If not enough money to buy (including deposits) then buys some part of desired
 /// </summary>
 internal Storage buy(Consumer buyer, Storage whatWantedToBuy)
 {
     if (whatWantedToBuy.isNotZero())
     {
         Storage buying;
         if (whatWantedToBuy.getProduct().isAbstract())
         {
             buying = marketPrice.convertToCheapestExistingSubstitute(whatWantedToBuy);
             if (buying == null)//no substitution available on market
             {
                 return(new Storage(whatWantedToBuy.getProduct()));
             }
         }
         else
         {
             buying = whatWantedToBuy;
         }
         Storage howMuchCanConsume;
         Value   price = getPrice(buying.getProduct());
         Value   cost;
         if (Game.market.sentToMarket.has(buying))
         {
             cost = buying.multiplyOutside(price);
             //if (cost.isNotZero())
             //{
             if (buyer.canPay(cost))
             {
                 buyer.pay(Game.market, cost);
                 buyer.consumeFromMarket(buying);
                 if (buyer is SimpleProduction)
                 {
                     (buyer as SimpleProduction).getInputProductsReserve().add(buying);
                 }
                 howMuchCanConsume = buying;
             }
             else
             {
                 float val = buyer.cash.get() / price.get();
                 val = Mathf.Floor(val * Value.precision) / Value.precision;
                 howMuchCanConsume = new Storage(buying.getProduct(), val);
                 buyer.pay(Game.market, howMuchCanConsume.multiplyOutside(price));
                 buyer.consumeFromMarket(howMuchCanConsume);
                 if (buyer is SimpleProduction)
                 {
                     (buyer as SimpleProduction).getInputProductsReserve().add(howMuchCanConsume);
                 }
             }
             //}
             //else
             //    return new Storage(buying.getProduct(), 0f);
         }
         else
         {
             // assuming available < buying
             Storage howMuchAvailable = new Storage(Game.market.HowMuchAvailable(buying));
             if (howMuchAvailable.get() > 0f)
             {
                 cost = howMuchAvailable.multiplyOutside(price);
                 if (buyer.canPay(cost))
                 {
                     buyer.pay(Game.market, cost);
                     buyer.consumeFromMarket(howMuchAvailable);
                     if (buyer is SimpleProduction)
                     {
                         (buyer as SimpleProduction).getInputProductsReserve().add(howMuchAvailable);
                     }
                     howMuchCanConsume = howMuchAvailable;
                 }
                 else
                 {
                     howMuchCanConsume = new Storage(howMuchAvailable.getProduct(), buyer.cash.get() / price.get());
                     if (howMuchCanConsume.get() > howMuchAvailable.get())
                     {
                         howMuchCanConsume.set(howMuchAvailable.get()); // you don't buy more than there is
                     }
                     if (howMuchCanConsume.isNotZero())
                     {
                         buyer.sendAllAvailableMoney(Game.market); //pay all money cause you don't have more
                         buyer.consumeFromMarket(howMuchCanConsume);
                         if (buyer is SimpleProduction)
                         {
                             (buyer as SimpleProduction).getInputProductsReserve().add(howMuchCanConsume);
                         }
                     }
                 }
             }
             else
             {
                 howMuchCanConsume = new Storage(buying.getProduct(), 0f);
             }
         }
         return(howMuchCanConsume);
     }
     else
     {
         return(whatWantedToBuy); // assuming buying is empty here
     }
 }