-
Notifications
You must be signed in to change notification settings - Fork 11
/
thistle_new.lua
354 lines (305 loc) · 8.34 KB
/
thistle_new.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
--
-- To run:
-- Copy a recipe from the output of the Thistle Mode
-- Update expected_gardens equal to the number you have opened and pinned
-- Gardens must all have their buttons visible, can overlap as long as the
-- row with "asc" is visible regardlesss of which window was clicked last
-- Update "last sun" to be what it is now (0 if night,
-- 99 if daylight and open lid, 33 if daylight and closed lid)
--
loadfile("luaScripts/screen_reader_common.inc")();
loadfile("luaScripts/ui_utils.inc")();
per_click_delay = 0;
local expected_gardens = 33;
local last_sun = 0;
instructions = {
0,0,0,1,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,1,0,0,
0,0,0,0,0,
0,0,0,1,0,
0,0,0,2,0,
0,0,0,0,0,
0,0,2,0,0,
0,0,0,0,0,
0,0,0,3,0,
0,0,0,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,0,0,0,
0,0,0,3,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,2,0,0,
0,0,1,0,0,
0,0,0,0,0,
0,0,1,1,0,
0,0,0,0,0,
0,0,0,1,0,
0,0,1,0,0,
0,0,1,1,0,
0,0,0,0,0,
0,0,1,1,0,
0,0,0,0,0,
0,0,1,1,0,
0,0,0,0,0,
0,0,3,3,0,
0,0,0,0,0,
0,0,0,1,0,
0,0,1,0,0,
0,0,1,1,0,
0,0,0,0,0,
0,0,0,1,0,
0,0,0,0,0,
0,0,0,0,0,
};
local window_locs = {};
function setWaitSpot(x0, y0)
setWaitSpot_x = x0;
setWaitSpot_y = y0;
setWaitSpot_px = srReadPixel(x0, y0);
end
function waitForChange()
local c=0;
while srReadPixel(setWaitSpot_x, setWaitSpot_y) == setWaitSpot_px do
lsSleep(1);
c = c+1;
if (lsShiftHeld() and lsControlHeld()) then
error 'broke out of loop from Shift+Ctrl';
end
end
-- lsPrintln('Waited ' .. c .. 'ms for pixel to change.');
end
function clickAll(image_name, up)
-- Find buttons and click them!
srReadScreen();
xyWindowSize = srGetWindowSize();
local buttons = findAllImages(image_name);
if #buttons == 0 then
statusScreen("Could not find specified buttons...");
lsSleep(1500);
else
statusScreen("Clicking " .. #buttons .. "button(s)...");
if up then
for i=#buttons, 1, -1 do
srClickMouseNoMove(buttons[i][0]+5, buttons[i][1]+3);
lsSleep(per_click_delay);
end
else
for i=1, #buttons do
srClickMouseNoMove(buttons[i][0]+5, buttons[i][1]+3);
lsSleep(per_click_delay);
end
end
statusScreen("Done clicking (" .. #buttons .. " clicks).");
lsSleep(100);
end
end
function clickAllComplex(image_names, message)
if not message then
message = "";
end
-- Find buttons and click them!
srReadScreen();
local dpos = {};
for i=1, #image_names do
local pos = srFindImageInRange(image_names[i],
window_locs[#window_locs][0], window_locs[#window_locs][1],
410, 312);
if not pos then
error ('Failed to find ' .. image_names[i]);
end
dpos[i] = {};
dpos[i][0] = pos[0] - window_locs[#window_locs][0];
dpos[i][1] = pos[1] - window_locs[#window_locs][1];
end
statusScreen(message .. " Clicking " .. #window_locs .. " button(s)...");
local first = 1;
for i=#window_locs, 1, -1 do
if not first then
-- focus
setWaitSpot(window_locs[i+1][0], window_locs[i+1][1]);
srClickMouseNoMove(window_locs[i][0], window_locs[i][1]);
waitForChange();
end
-- click all buttons
for j=1, #image_names do
srClickMouseNoMove(window_locs[i][0] + dpos[j][0] + 5, window_locs[i][1] + dpos[j][1] + 5);
end
first = nil;
end
lsSleep(100);
statusScreen(message .. " Refocusing...");
-- refocus
for i=2, #window_locs do
setWaitSpot(window_locs[i][0], window_locs[i][1]);
srClickMouseNoMove(window_locs[i][0], window_locs[i][1] + 310);
waitForChange();
end
lsSleep(100);
end
button_names = {"ThistleNit.png", "ThistlePot.png", "ThistleH2O.png", "ThistleOxy.png", "ThistleSun.png"};
local z = 2;
-- Initialize last_mon
local mon_w = 10;
local mon_h = 152;
local last_mon = {};
for x=1, mon_w do
last_mon[x] = {};
for y=1, mon_h do
last_mon[x][y] = 0;
end
end
function waitForMonChange(message)
if not first_nit then
first_nit = srFindImage("ThistleNit.png");
end
if not first_nit then
error "Could not find first Nit";
end
mon_x = first_nit[0] - 25;
mon_y = first_nit[1] + 13;
local different = nil;
local skip_next = nil;
local first_loop = 1;
while not different do
srReadScreen();
for x=1, mon_w do
for y=1, mon_h do
newvalue = srReadPixelFromBuffer(mon_x + x, mon_y + y);
if not (newvalue == last_mon[x][y]) then
different = 1;
end
last_mon[x][y] = newvalue;
end
end
if not different then
first_loop = nil;
end
if different then
-- Make sure the screen was done refreshing and update again
lsSleep(60);
srReadScreen();
for x=1, mon_w do
for y=1, mon_h do
last_mon[x][y] = srReadPixelFromBuffer(mon_x + x, mon_y + y);
end
end
end
if (different and skip_next) then
skip_next = nil;
different = nil;
end
lsPrintWrapped(10, 5, 0, lsScreenX - 20, 1, 1, 0xFFFFFFff, message);
lsPrintWrapped(10, 60, 0, lsScreenX - 20, 1, 1, 0xFFFFFFff, "Waiting for change...");
if lsButtonText(lsScreenX - 110, lsScreenY - 30, z, 100, 0xFFFFFFff, "End script") then
error "Clicked End Script button";
end
if lsButtonText(40, lsScreenY - 60, z, 200, 0xFFFFFFff, "Force tick") then
different = 1;
end
if lsButtonText(40, lsScreenY - 90, z, 200, 0xFFFFFFff, "Skip tick") then
skip_next = 1;
end
if lsButtonText(40, lsScreenY - 120, z, 200, 0xFFFFFFff, "Finish up") then
finish_up = 1;
end
-- display mon pixels
for x=1, mon_w do
for y=1, mon_h do
local size = 2;
lsDisplaySystemSprite(1, 10+x*size, 90+y*size, 0, size, size, last_mon[x][y]);
end
end
lsDoFrame();
lsSleep(100);
end
statusScreen("Changed, waiting a moment for other beds to catch up...");
if not first_loop then -- Don't wait, we might be behind already!
lsSleep(1500); -- Wait a moment after image changes before doing the next tick
end
end
function test()
local loop=0;
while 1 do
waitForMonChange("tick " .. loop);
statusScreen('Changed!');
lsSleep(1000);
loop = loop + 1;
end
error 'done';
end
function refillWater()
statusScreen("Refilling water...");
srClickMouseNoMove(359, 13);
lsSleep(100);
srClickMouseNoMove(srGetWindowSize()[0]/2 + 10, srGetWindowSize()[1]/2 + 20);
lsSleep(4000);
end
function doit()
num_loops = promptNumber("How many passes ?", 1);
askForWindow("Pin any number of thistle gardens, edit thistle_new with recipe.");
if not ( #instructions == 41*5) then
error 'Invalid instruction length';
end
srReadScreen();
window_locs = findAllImages("ThisIs.png");
if not (#window_locs == expected_gardens) then
error ("Did not find expected number of thistle gardens (found " .. #window_locs .. " expected " .. expected_gardens .. ")");
end
-- wl2 = {};
-- wl2[1] = window_locs[31];
-- wl2[2] = window_locs[32];
-- window_locs = wl2;
-- if not (#window_locs == 2) then
-- error 'fail';
-- end
-- test();
for loops=1, num_loops do
-- clickAll("ThisIs.png", 1);
-- lsSleep(100);
srReadScreen();
-- clickAllComplex({"Harvest.png"}, 1);
-- error 'done';
-- clickAllComplex({"ThistleAbort.png"}, 1);
-- error 'done';
-- statusScreen("(" .. loops .. "/" .. num_loops .. ") Doing initial 2s wait...");
-- lsSleep(2000);
--waitForMonChange("Getting initial image...");
for i=0, 39 do
local to_click = {};
if (i == 0) then
to_click[1] = "ThistlePlantACrop.png";
end
for j=0, 3 do
for k=1, instructions[i*5 + j + 1] do
to_click[#to_click+1] = button_names[j+1];
end
end
if not (instructions[i*5 + 5] == last_sun) then
last_sun = instructions[i*5 + 5];
to_click[#to_click+1] = button_names[5];
end
if #to_click > 0 then
clickAllComplex(to_click, ("(" .. loops .. "/" .. num_loops .. ") " .. i .. ":"));
end
waitForMonChange("(" .. loops .. "/" .. num_loops .. ") Tick " .. i .. " done.");
if (i == 0) then -- first one immediately finds a change
waitForMonChange("(" .. loops .. "/" .. num_loops .. ") Tick " .. i .. " done.");
end
if finish_up then
num_loops = loops;
end
end
lsSleep(3000);
clickAllComplex({"Harvest.png"});
lsSleep(500);
refillWater(); -- Hack - won't work unless things are arranged precisely, and does no checking
if finish_up then
break;
end
end
lsPlaySound("Complete.wav");
end