-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
actions.lua
305 lines (277 loc) · 9.99 KB
/
actions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
local config = require('config');
local packets = require('packets');
local pgen = require('pgen');
local queue = {};
local actions = {
busy = false,
talkNpc = function(self, tid, tidx)
-- https://github.com/Windower/Lua/blob/422880f0e353a82bb9a11328dc4202ed76cd948a/addons/libs/packets/fields.lua#L349
local pid = packets.out.PACKET_NPC_INTERACTION;
local packet = pgen:new(pid)
:push('L', tid)
:push('H', tidx)
:push('H', 0):push('H', 0):push('H', 0)
:push('f', 0):push('f', 0):push('f', 0)
:get_packet();
AddOutgoingPacket(pid, packet);
end,
InteractNpc = function(self, tid, tidx)
local actions = self;
-- https://github.com/Windower/Lua/blob/422880f0e353a82bb9a11328dc4202ed76cd948a/addons/libs/packets/fields.lua#L349
return actions:new():next(function()
local pid = packets.out.PACKET_NPC_INTERACTION;
local packet = pgen:new(pid)
:push('L', tid)
:push('H', tidx)
:push('H', 0):push('H', 0):push('H', 0)
:push('f', 0):push('f', 0):push('f', 0)
:get_packet();
AddOutgoingPacket(pid, packet);
return 'packet_in';
end)
:next(function(self, stalled, id, size, packet)
if (stalled == true) then -- npcs get contention when talked to repeatedly (even by other players)
if (self.__count ~= nil and self.__count >= 15) then -- bail
print('I give up');
return;
end
print('trying again');
actions:talkNpc(tid, tidx);
self.stalled = false; -- try again
self.count = 0; -- backdown
self.__count = (self.__count or 0) + 1;
print(self.__count);
return false;
elseif (id == packets.inc.PACKET_NPC_INTERACTION) then
return; -- no packet to unpack below;
elseif (id ~= packets.inc.PACKET_NPC_INTERACTION_2) then
return false;
end
-- https://github.com/Windower/Lua/blob/422880f0e353a82bb9a11328dc4202ed76cd948a/addons/libs/packets/fields.lua#L1880
self._npcid = struct.unpack('L', packet, 0x04 + 1);
self._zone = struct.unpack('H', packet, 0x2A + 1);
self._menuid = struct.unpack('H', packet, 0x2C + 1);
end)
end,
new = function(self)
-- This is an action
return {
count = 0, -- how many ticks have gone since progress
waiting = nil, -- 'packet' or nil
parts = {}, -- queue of functions to run
-- monad to help with building
next = function(self, fn)
-- to wait for a packet, call this with a function that returns 'packet'
-- the next fn you queue will be passed all incoming packets for you to filter.
-- return false if the packet you get does not match what you need.
table.insert(self.parts, fn);
return self;
end
};
end,
queue = function(self, action)
action.next = nil;
table.insert(queue, action);
end,
tick = function(self)
local len = #queue;
if (len == 0) then return end
local action = queue[1];
local parts = action.parts;
if (#parts == 0) then
table.remove(queue, 1);
self:tick();
elseif (action.count >= 5 and action.stalled ~= true) then
action.stalled = true;
parts[1](action, true); -- action will be killed next tick
elseif (action.stalled == true) then
print('Action stalled, removed.');
AshitaCore:GetChatManager():QueueCommand('/l2 Action stalled, removed.', -1);
table.remove(queue, 1);
self:tick();
elseif (action.waiting == 'wait') then
action.wait = (action.wait or 0) - 1;
-- print('waiting 1 tick, ' .. action.wait);
if (action.wait <= 0) then
action.waiting = nil;
end
elseif (action.waiting == nil) then
action.waiting, action.wait = table.remove(parts, 1)(action, false);
action.count = 0;
action.stalled = false;
else
action.count = action.count + 1;
end
end,
packet = function(self, isIn, id, size, packet)
local len = #queue;
if (len == 0) then return end
local action = queue[1];
local parts = action.parts;
if (#parts == 0) then
table.remove(queue, 1);
self:packet(id, size, packet);
elseif (isIn == true and action.waiting == 'packet_in') then
local result = parts[1](action, false, id, size, packet);
if (result ~= false) then
action.waiting, action.wait = result;
table.remove(parts, 1);
action.count = 0;
action.stalled = false;
end
elseif (isIn == false and action.waiting == 'packet_out') then
local result = parts[1](action, false, id, size, packet);
if (result ~= false) then
action.waiting, action.wait = result;
table.remove(parts, 1);
action.count = 0;
action.stalled = false;
if (result == true) then
action.waiting = nil;
return true; -- this function wants to replace the packet.
end
end
end
end,
signet = function(self, tid, tidx)
local actions = self;
actions:queue(
actions:InteractNpc(tid, tidx)
:next(function(self) return 'wait', 4; end) -- wait 4 ticks
:next(function(self, stalled) -- kill the text menu from the book
self.esc = true;
AshitaCore:GetChatManager():QueueCommand('/sendkey escape down', -1);
return 'packet_out'; -- wait to cap the packet
end)
:next(function(self, stalled, id, size, packet)
if (self.esc == true) then
self.esc = false;
AshitaCore:GetChatManager():QueueCommand('/sendkey escape up', -1);
end
if (stalled == true) then return end
if (id ~= packets.out.PACKET_NPC_CHOICE) then return false end
-- https://github.com/Windower/Lua/blob/422880f0e353a82bb9a11328dc4202ed76cd948a/addons/libs/packets/fields.lua#L661
local packet = pgen:new(id)
:push('L', self._npcid) -- npcid
:push('H', 1) -- signet is slot 1
:push('H', 0x00) -- unkown (with repeat?)
:push('H', tidx)
:push('B', 0x00) -- auto
:push('B', 0x00) -- unkown-2
:push('H', self._zone)
:push('H', self._menuid)
:get_packet();
AddOutgoingPacket(id, packet);
return true; -- replace the outgoing packet
end)
:next(function(self)
AshitaCore:GetChatManager():QueueCommand('/l2 done.', 1);
end)
);
end,
warp_scroll = function(self, tid, tidx)
local actions = self;
actions:queue(
actions:InteractNpc(tid, tidx)
:next(function(self) return 'wait', 4; end) -- wait 4 ticks
:next(function(self, stalled) -- kill the text menu from the book
self.esc = true;
AshitaCore:GetChatManager():QueueCommand('/sendkey escape down', -1);
return 'packet_out'; -- wait to cap the packet
end)
:next(function(self, stalled, id, size, packet)
if (self.esc == true) then
self.esc = false;
AshitaCore:GetChatManager():QueueCommand('/sendkey escape up', -1);
end
if (stalled == true) then return end
if (id ~= packets.out.PACKET_NPC_CHOICE) then return false end
-- https://github.com/Windower/Lua/blob/422880f0e353a82bb9a11328dc4202ed76cd948a/addons/libs/packets/fields.lua#L661
local packet = pgen:new(id)
:push('L', self._npcid) -- npcid
:push('H', 32929) -- warp scroll
:push('H', 0x00) -- unkown (with repeat?)
:push('H', tidx)
:push('B', 0x00) -- auto
:push('B', 0x00) -- unkown-2
:push('H', self._zone)
:push('H', self._menuid)
:get_packet();
AddOutgoingPacket(id, packet);
return true; -- replace the outgoing packet
end)
:next(function(self)
AshitaCore:GetChatManager():QueueCommand('/l2 done.', 1);
end)
);
end,
corn = function(self, tid, tidx)
local actions = self;
local Inventory = AshitaCore:GetDataManager():GetInventory();
local idx = nil;
for i = 0, Inventory:GetContainerMax(0) - 1 do
local item_t = Inventory:GetItem(0, i);
if (item_t and item_t.Id == 629 and item_t.Count >= 3) then
idx = i;
break;
end
end
if (idx == nil) then
AshitaCore:GetChatManager():QueueCommand('/l2 LADIES AND GENTLEMEN! NO MORE CORN!', 1);
return;
end
self:queue(self:new():next(function()
local pid = packets.out.PACKET_TRADE_MENU_ITEM;
local packet = pgen:new(pid)
:push('L', tid)
:push('L', 3) -- slot 0 (or gil)
:push('L', 0):push('L', 0):push('L', 0):push('L', 0)
:push('L', 0):push('L', 0):push('L', 0):push('L', 0)
:push('L', 0) -- unk1
:push('B', idx) -- slot 0 (or gil)
:push('B', 0):push('B', 0):push('B', 0):push('B', 0)
:push('B', 0):push('B', 0):push('B', 0):push('B', 0)
:push('B', 0)
:push('H', tidx)
:push('B', 1)
:push('B', 0)
:push('B', 0)
:push('B', 0)
:get_packet();
AddOutgoingPacket(pid, packet);
return 'packet_in';
end)
:next(function(self, stalled, id, size, packet)
if (stalled or id ~= 0x1E) then
if (self.__count ~= nil and self.__count >= 15) then -- bail
print('I give up');
return;
end
local me = self;
if (not(me.keydown)) then
me.keydown = true;
AshitaCore:GetChatManager():QueueCommand('/sendkey return down', -1);
ashita.timer.once(1, function()
AshitaCore:GetChatManager():QueueCommand('/sendkey return up', -1);
ashita.timer.once(1, function()
me.keydown = false;
end);
end);
end
self.stalled = false; -- try again
self.count = 0; -- backdown
self.__count = (self.__count or 0) + 1;
return false;
else
ashita.timer.once(1, function()
actions:corn(tid, tidx);
end);
end
end));
end,
leader = function(self, leader)
config:get().leader = leader;
print(config:get().leader);
end
};
return actions;