From b3b9bf393f97654390e776b24c5e2582d7d78bae Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Mon, 30 Jan 2017 21:51:16 -0500 Subject: [PATCH] Trade: Added basic logic for dedicated traders offers. --- npc.lua | 10 ++++++++++ trade/trade.lua | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/npc.lua b/npc.lua index 30a1ef7..79e28fc 100755 --- a/npc.lua +++ b/npc.lua @@ -524,7 +524,17 @@ local function npc_spawn(self, pos) --npc.add_action(ent, npc.actions.set_interval, {self=ent, interval=10, freeze=true}) npc.add_action(ent, npc.actions.freeze, {freeze = false}) end + + -- Dedicated trade test + ent.trader_data.trade_list = { + "default:tree", + "default:cobble", + "default:wood" + } + local trade_offers = npc.trade.get_trade_offers_for_dedicated_trader(ent) + minetest.log("Trade offers: "..dump(trade_offers)) + -- npc.add_action(ent, npc.action.stand, {self = ent}) -- npc.add_action(ent, npc.action.stand, {self = ent}) -- npc.add_action(ent, npc.action.walk_step, {self = ent, dir = npc.direction.east}) diff --git a/trade/trade.lua b/trade/trade.lua index f3d6e50..c0e3b95 100644 --- a/trade/trade.lua +++ b/trade/trade.lua @@ -199,8 +199,47 @@ end -- be NPC inventories. In the future, it should support both NPC and chest -- inventories, function npc.trade.get_trade_offers_for_dedicated_trader(self) + local offers = { + for_sell = {}, + to_buy = {} + } - + for i = 1, #self.trader_data.trade_list do + -- For each item on the trader list, check if it is in the NPC inventory. + -- If it is, create a sell offer, else create a buy offer if possible. + local item = npc.inventory_contains(self, self.trader_data.trade_list[i]) + if item ~= nil then + -- Create sell offer for this item. Currently, traders will offer to sell only + -- of their items to allow the fine control for players to buy what they want. + -- This requires, however, that the trade offers are re-generated everytime a + -- sell is made. + table.insert(offers.for_sell, npc.trade.create_offer(npc.trade.OFFER_SELL, self.trader_data.trade_list[i], nil, nil, 1)) + else + -- Create buy offer for this item + -- Only do if the NPC can actually afford the items. + local currencies = npc.trade.get_currencies_in_inventory(self) + -- Choose a random currency + local chosen_tier = currencies[math.random(#currencies)] + -- Get items for this currency + local buyable_items = + npc.trade.prices.get_items_for_currency_count(chosen_tier.name, chosen_tier.count, 0.5) + -- Check if the item from trader list is present in the buyable items list + for buyable_item, price_info in pairs(buyable_items) do + if buyable_item == self.trader_data.trade_list[i] then + -- If item found, create a buy offer for this item + -- Again, offers are created for one item only. Buy offers should be removed + -- after the NPC has bought a certain quantity, say, 5 items. + table.insert(offers.to_buy, npc.trade.create_offer( + npc.trade.OFFER_BUY, + self.trader_data.trade_list[i], + price_info.price, + price_info.min_buyable_item_price, + 1)) + end + end + end + end + return offers end -- Creates a trade offer based on the offer type, given item and count. If