private void RefreshBuyingSellingInventory() { if (TradeProps == null) { return; } TradeProps.Buying.List.Shuffle(World.Rand); int quantity = Math.Min(TradeProps.Buying.List.Length, TradeProps.Buying.MaxItems); for (int i = 0; i < quantity; i++) { ItemSlotTrade slot = Inventory.GetBuyingSlot(i); TradeItem tradeItem = TradeProps.Buying.List[i]; if (tradeItem.Name == null) { tradeItem.Name = i + ""; } slot.SetTradeItem(tradeItem.Resolve(World)); slot.MarkDirty(); } TradeProps.Selling.List.Shuffle(World.Rand); quantity = Math.Min(TradeProps.Selling.List.Length, TradeProps.Selling.MaxItems); //Console.WriteLine("=================="); //Console.WriteLine("resolving for " + EntityId + ", total items: " +TradeConf.Selling.List.Length + ", on side " + api.Side); for (int i = 0; i < quantity; i++) { ItemSlotTrade slot = Inventory.GetSellingSlot(i); TradeItem tradeItem = TradeProps.Selling.List[i]; if (tradeItem.Name == null) { tradeItem.Name = i + ""; } slot.SetTradeItem(tradeItem.Resolve(World)); slot.MarkDirty(); //Console.WriteLine("resolved to: " + slot.Itemstack); } ITreeAttribute tree = GetOrCreateTradeStore(); Inventory.ToTreeAttributes(tree); }
private void RefreshBuyingSellingInventory(float refreshChance = 1.1f) { if (TradeProps == null) { return; } TradeProps.Buying.List.Shuffle(World.Rand); int buyingQuantity = Math.Min(TradeProps.Buying.List.Length, TradeProps.Buying.MaxItems); TradeProps.Selling.List.Shuffle(World.Rand); int sellingQuantity = Math.Min(TradeProps.Selling.List.Length, TradeProps.Selling.MaxItems); // Pick quantity items from the trade list that the trader doesn't already sell // Slots 0..15: Selling slots // Slots 16..19: Buying cart // Slots 20..35: Buying slots // Slots 36..39: Selling cart // Slot 40: Money slot Stack <TradeItem> newBuyItems = new Stack <TradeItem>(); Stack <TradeItem> newsellItems = new Stack <TradeItem>(); ItemSlotTrade[] sellingSlots = Inventory.SellingSlots; ItemSlotTrade[] buyingSlots = Inventory.BuyingSlots; #region Avoid duplicate sales for (int i = 0; i < TradeProps.Selling.List.Length; i++) { if (newsellItems.Count >= sellingQuantity) { break; } TradeItem item = TradeProps.Selling.List[i]; item.Resolve(World, "tradeItem resolver"); bool alreadySelling = sellingSlots.Any((slot) => slot?.Itemstack != null && slot.TradeItem.Stock > 0 && item.ResolvedItemstack?.Equals(World, slot.Itemstack, GlobalConstants.IgnoredStackAttributes) == true); if (!alreadySelling) { newsellItems.Push(item); } } for (int i = 0; i < TradeProps.Buying.List.Length; i++) { if (newBuyItems.Count >= buyingQuantity) { break; } TradeItem item = TradeProps.Buying.List[i]; item.Resolve(World, "tradeItem resolver"); bool alreadySelling = buyingSlots.Any((slot) => slot?.Itemstack != null && slot.TradeItem.Stock > 0 && item.ResolvedItemstack?.Equals(World, slot.Itemstack, GlobalConstants.IgnoredStackAttributes) == true); if (!alreadySelling) { newBuyItems.Push(item); } } #endregion replaceTradeItems(newBuyItems, buyingSlots, buyingQuantity, refreshChance); replaceTradeItems(newsellItems, sellingSlots, sellingQuantity, refreshChance); ITreeAttribute tree = GetOrCreateTradeStore(); Inventory.ToTreeAttributes(tree); }