1local context = G.botContext
2
3context.NPC = {}
4
5context.NPC.talk = function(text)
6 if g_game.getClientVersion() >= 810 then
7 g_game.talkChannel(11, 0, text)
8 else
9 return context.say(text)
10 end
11end
12context.NPC.say = context.NPC.talk
13
14context.NPC.isTrading = function()
15 return modules.game_npctrade.npcWindow and modules.game_npctrade.npcWindow:isVisible()
16end
17context.NPC.hasTrade = context.NPC.isTrading
18context.NPC.hasTradeWindow = context.NPC.isTrading
19context.NPC.isTradeOpen = context.NPC.isTrading
20
21context.NPC.getSellItems = function()
22 if not context.NPC.isTrading() then return {} end
23 local items = {}
24 for i, item in ipairs(modules.game_npctrade.tradeItems[modules.game_npctrade.SELL]) do
25 table.insert(items, {
26 item = item.ptr,
27 id = item.ptr:getId(),
28 count = item.ptr:getCount(),
29 name = item.name,
30 subType = item.ptr:getSubType(),
31 weight = item.weight / 100,
32 price = item.price
33 })
34 end
35 return items
36end
37
38context.NPC.getBuyItems = function()
39 if not context.NPC.isTrading() then return {} end
40 local items = {}
41 for i, item in ipairs(modules.game_npctrade.tradeItems[modules.game_npctrade.BUY]) do
42 table.insert(items, {
43 item = item.ptr,
44 id = item.ptr:getId(),
45 count = item.ptr:getCount(),
46 name = item.name,
47 subType = item.ptr:getSubType(),
48 weight = item.weight / 100,
49 price = item.price
50 })
51 end
52 return items
53end
54
55context.NPC.getSellQuantity = function(item)
56 if not context.NPC.isTrading() then return 0 end
57 if type(item) == 'number' then
58 item = Item.create(item)
59 end
60 return modules.game_npctrade.getSellQuantity(item)
61end
62
63context.NPC.canTradeItem = function(item)
64 if not context.NPC.isTrading() then return false end
65 if type(item) == 'number' then
66 item = Item.create(item)
67 end
68 return modules.game_npctrade.canTradeItem(item)
69end
70
71context.NPC.sell = function(item, count, ignoreEquipped)
72 if type(item) == 'number' then
73 for i, entry in ipairs(context.NPC.getSellItems()) do
74 if entry.id == item then
75 item = entry.item
76 break
77 end
78 end
79 if type(item) == 'number' then
80 item = Item.create(item)
81 end
82 end
83 if count == 0 then
84 count = 1
85 end
86 if count == nil or count == -1 then
87 count = context.NPC.getSellQuantity(item)
88 end
89 if ignoreEquipped == nil then
90 ignoreEquipped = true
91 end
92 g_game.sellItem(item, count, ignoreEquipped)
93end
94
95context.NPC.buy = function(item, count, ignoreCapacity, withBackpack)
96 if type(item) == 'number' then
97 for i, entry in ipairs(context.NPC.getBuyItems()) do
98 if entry.id == item then
99 item = entry.item
100 break
101 end
102 end
103 if type(item) == 'number' then
104 item = Item.create(item)
105 end
106 end
107 if count == nil or count <= 0 then
108 count = 1
109 end
110 if ignoreCapacity == nil then
111 ignoreCapacity = false
112 end
113 if withBackpack == nil then
114 withBackpack = false
115 end
116 g_game.buyItem(item, count, ignoreCapacity, withBackpack)
117end
118
119context.NPC.sellAll = function()
120 if not context.NPC.isTrading() then return false end
121 modules.game_npctrade.sellAll()
122end
123
124context.NPC.closeTrade = function()
125 modules.game_npctrade.closeNpcTrade()
126end
127context.NPC.close = context.NPC.closeTrade
128context.NPC.finish = context.NPC.closeTrade
129context.NPC.endTrade = context.NPC.closeTrade
130context.NPC.finishTrade = context.NPC.closeTrade