Exemplo n.º 1
0
            public void buy(Pawn negotiator)
            {
                if (canAfford())
                {
                    IEnumerable <ThingDef> thingsToPay = ingredients.Select(p => p.Key).Concat(new ThingDef[] { ThingDefOf.Silver });
                    foreach (Pair <ThingDef, IngredientCount> p in ingredients)
                    {
                        IEnumerable <Thing> thingsInStock = resources.Where(thingInStock => thingInStock.def == p.Key).OrderBy(thingInStock => thingInStock.stackCount);
                        int countToGive = p.Value.CountRequiredOfFor(p.Key, recipe);
                        foreach (Thing t in thingsInStock)
                        {
                            int countForStack = Math.Min(t.stackCount, countToGive);
                            colony.GiveSoldThingToTrader(t, countForStack, negotiator);
                            if (countToGive <= countForStack)
                            {
                                break;
                            }
                            countToGive -= countForStack;
                        }
                    }

                    IEnumerable <Thing> silverInStock = resources.Where(thingInStock => thingInStock.def == ThingDefOf.Silver).OrderBy(thingInStock => thingInStock.stackCount);
                    int silverToGive = cost();
                    foreach (Thing t in silverInStock)
                    {
                        int countForStack = Math.Min(t.stackCount, silverToGive);
                        colony.GiveSoldThingToTrader(t, countForStack, negotiator);
                        if (silverToGive == countForStack)
                        {
                            break;
                        }
                        else
                        {
                            silverToGive -= countForStack;
                        }
                    }

                    foreach (Thing t in createProducts())
                    {
                        colony.GiveSoldThingToPlayer(t, t.stackCount, negotiator);
                    }
                }
                else
                {
                    Find.WindowStack.Add(new Dialog_MessageBox("You cannot afford the recipe, ensure all materials and silver are placed under a trade bacon in valid stockpiles.", "Ok"));
                }
            }