Example #1
0
    public TradeSubmission Consume(Dictionary <string, Commodity> com)
    {
        var bids = new TradeSubmission();

        //replenish depended commodities
        foreach (var stock in stockPile)
        {
            if (buildables.Contains(stock.Key))
            {
                continue;
            }

            var numBids = FindBuyCount(stock.Key);
            if (numBids > 0)
            {
                //maybe buy less if expensive?
                float buyPrice = stock.Value.GetPrice();
                if (buyPrice > 1000)
                {
                    Debug.Log(stock.Key + "buyPrice: " + buyPrice.ToString("c2") + " : " + stock.Value.minPriceBelief.ToString("n2") + "<" + stock.Value.maxPriceBelief.ToString("n2"));
                }
                if (numBids < 0)
                {
                    Debug.Log(stock.Key + " buying negative " + numBids.ToString("n2") + " for " + buyPrice.ToString("c2"));
                }
                bids.Add(stock.Key, new Trade(stock.Value.commodityName, buyPrice, numBids, this));
            }
        }
        return(bids);
    }
Example #2
0
 public void Add(TradeSubmission ts)
 {
     foreach (var entry in ts)
     {
         var commodity = entry.Key;
         var trade     = entry.Value;
         base[commodity].Add(trade);
     }
 }
Example #3
0
    public TradeSubmission Produce(Dictionary <string, Commodity> com, ref float idleTax)
    {
        var asks = new TradeSubmission();

        //TODO sort buildables by profit

        //build as many as one can TODO don't build things that won't earn a profit
        foreach (var buildable in buildables)
        {
            //get list of dependent commodities
            float  numProduced = float.MaxValue;            //amt agent can produce for commodity buildable
            string sStock      = ", has in stock";
            //find max that can be made w/ available stock
            if (!com.ContainsKey(buildable))
            {
                Debug.Log("not a commodity: " + buildable);
            }
            foreach (var dep in com[buildable].dep)
            {
                //get num commodities you can build
                var numNeeded = dep.Value;
                var numAvail  = stockPile[dep.Key].quantity;
                numProduced = Mathf.Min(numProduced, numAvail / numNeeded);
                sStock     += " " + numAvail + " " + dep.Key;
            }
            //can only build fixed rate at a time
            //can't produce more than what's in stock
            var upperBound = Mathf.Min(stockPile[buildable].productionRate, stockPile[buildable].Deficit());
            numProduced = Mathf.Clamp(numProduced, 0, upperBound);;
            //build and add to stockpile
            foreach (var dep in com[buildable].dep)
            {
                var stock   = stockPile[dep.Key].quantity;
                var numUsed = dep.Value * numProduced;
                numUsed = Mathf.Clamp(numUsed, 0, stock);
                stockPile[dep.Key].quantity -= numUsed;
            }
            numProduced *= stockPile[buildable].production;
            numProduced  = Mathf.Max(numProduced, 0);
            stockPile[buildable].quantity += numProduced;
            if (float.IsNaN(numProduced))
            {
                Debug.Log(buildable + " numproduced is nan!");
            }

            var   buildStock = stockPile[buildable];
            float sellPrice  = FindSellCount(buildable); buildStock.GetPrice();
            //HACK, so economy is always flowing somewhere
            //numProduced = Mathf.Max(1, numProduced);
            //sellPrice = Mathf.Max(1, sellPrice);

//            Debug.Log(name + " has " + cash.ToString("c2") + " made " + numProduced.ToString("n2") + " " + buildable + sellPrice.ToString("c2") + sStock);

            if (numProduced > 0 && sellPrice > 0)
            {
                asks.Add(buildable, new Trade(buildable, sellPrice, buildStock.quantity, this));
            }
            else
            {
                idleTax = Mathf.Abs(cash * .05f);
                cash   -= idleTax;
            }
        }

        //alternative build: only make what is most profitable...?
        foreach (var buildable in buildables)
        {
            //cost to make c = price of dependents + overhead (labor, other)
            //profit = price of c - cost of c

            //number to produce c = f(profit/stock) (want to maximize!)

            //if c_price < c_cost
            //just buy c, don't produce any
        }

        return(asks);
    }