Guest User

Untitled

a guest
Jul 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.84 KB | None | 0 0
  1. -- Edit these first 2 to adjust how much is planted in a pass
  2. -- May need to adjust walk_time in flax_common.inc if you move too slowly to keep up
  3. -- grids tested: 2x2, 3x3, 5x5, 6x6 (probably need 3+ dex and 600ms walk time)
  4. grid_w = 5;
  5. grid_h = 5;
  6. is_plant = true;
  7. seeds_per_pass = 5;
  8. rip_out_when_done = true;
  9.  
  10. loadfile("luaScripts/screen_reader_common.inc")();
  11. loadfile("luaScripts/ui_utils.inc")();
  12.  
  13. xyWindowSize = srGetWindowSize();
  14. imgFlax1 = "FlaxGeneric.png";
  15. imgHarvest = "HarvestThisFlax.png";
  16. imgWeedAndWater = "WeedAndWater.png";
  17. imgWeed = "WeedThisFlaxBed.png";
  18. imgSeeds = "HarvestSeeds.png";
  19. imgHarvestThese = "HarvestThese.png";
  20. imgWeedThese = "WeedThese.png";
  21.  
  22. delay_time = 100;
  23. screen_refresh_time = 300;
  24. walk_px_y = 340;
  25. walk_px_x = 380;
  26. walk_y_drift = 18;
  27. walk_x_drift = 14;
  28. walk_time = 570;
  29. refocus_time = 500;
  30.  
  31. function initGlobals()
  32. pixel_scale = xyWindowSize[0] / 1720; -- Macro written with 1720 pixel wide window
  33. lsPrintln("pixel_scale " .. pixel_scale);
  34.  
  35. walk_px_y = math.floor(walk_px_y * pixel_scale);
  36. walk_px_x = math.floor(walk_px_x * pixel_scale);
  37. if (lsScreenX < 1280) then
  38. -- Have to click way off center in order to not move at high resoltuions
  39. walk_y_drift = math.floor(walk_y_drift * pixel_scale);
  40. walk_x_drift = math.floor(walk_x_drift * pixel_scale);
  41. else
  42. -- Very little drift at these resolutions, clicking dead center barely moves
  43. walk_y_drift = 1;
  44. walk_x_drift = 1;
  45. end
  46. end
  47.  
  48. -- The flax bed window
  49. -- T4, guilded?: window_w = 166;
  50. -- T4, guilded?: window_h = 116;
  51. window_w = 174;
  52. window_h = 100;
  53. refresh_down_y_offs = 4;
  54. refresh_up_y_offs = 0;
  55.  
  56. -- adjust if "plant all guild owned" option is enabled
  57. window_check_done_once = false;
  58. function checkWindowSize(x, y)
  59. if not window_check_done_once then
  60. srReadScreen();
  61. window_check_done_once = true;
  62. local pos = srFindImageInRange("UseableBy.png", x-5, y-50, 150, 100)
  63. if pos then
  64. window_w = 166;
  65. window_h = 116;
  66. end
  67. end
  68. end
  69.  
  70. function getCenterPos()
  71. local ret = {};
  72. ret[0] = xyWindowSize[0] / 2 - walk_x_drift;
  73. ret[1] = xyWindowSize[1] / 2 + walk_y_drift;
  74. return ret;
  75. end
  76.  
  77. function setWaitSpot(x0, y0)
  78. setWaitSpot_x = x0;
  79. setWaitSpot_y = y0;
  80. setWaitSpot_px = srReadPixel(x0, y0);
  81. end
  82.  
  83. function waitForChange(timeout)
  84. if not timeout then
  85. timeout = 10000000;
  86. end
  87. local c=0;
  88. local timestart = lsGetTimer();
  89. while srReadPixel(setWaitSpot_x, setWaitSpot_y) == setWaitSpot_px do
  90. lsSleep(1);
  91. c = c+1;
  92. if (lsShiftHeld() and lsControlHeld()) then
  93. error 'broke out of loop from Shift+Ctrl';
  94. end
  95. if lsGetTimer() > timestart + timeout then
  96. lsPrintln('Timed out waiting.');
  97. end
  98. end
  99. lsPrintln('Waited ' .. c .. 'ms for pixel to change.');
  100. end
  101.  
  102. function drag(x0, y0, x1, y1)
  103. srSetMousePos(x0, y0);
  104. setWaitSpot(x1, y1);
  105. srMouseDown(x0, y0, 0);
  106. -- lsSleep(15);
  107. srSetMousePos(x1, y1);
  108. --lsSleep(50);
  109. waitForChange(1000);
  110. srMouseUp(x0, y0, 0);
  111. --lsSleep(50);
  112. end
  113.  
  114. function promptFlaxNumbers()
  115. scale = 1.0;
  116.  
  117. local z = 0;
  118. local is_done = nil;
  119. local value = nil;
  120. -- Edit box and text display
  121. while not is_done do
  122. -- Put these everywhere to make sure we don't lock up with no easy way to escape!
  123. checkBreak("disallow pause");
  124.  
  125. lsPrint(10, 10, z, scale, scale, 0xFFFFFFff, "Choose passes and grid size");
  126.  
  127. -- lsEditBox needs a key to uniquely name this edit box
  128. -- let's just use the prompt!
  129. -- lsEditBox returns two different things (a state and a value)
  130. local y = 40;
  131. if is_plant or rip_out_when_done then
  132. lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Passes:");
  133. is_done, num_loops = lsEditBox("passes",
  134. 110, y, z, 50, 30, scale, scale,
  135. 0x000000ff, 5);
  136. if not tonumber(num_loops) then
  137. is_done = nil;
  138. lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  139. num_loops = 1;
  140. end
  141. else
  142. lsPrint(5, y, z, scale, scale, 0x808080ff, "Passes: 1");
  143. num_loops = 1;
  144. end
  145. y = y + 32;
  146.  
  147. lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Grid size:");
  148. is_done, grid_w = lsEditBox("grid",
  149. 110, y, z, 50, 30, scale, scale,
  150. 0x000000ff, grid_w);
  151. if not tonumber(grid_w) then
  152. is_done = nil;
  153. lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  154. grid_w = 1;
  155. grid_h = 1;
  156. end
  157. grid_w = tonumber(grid_w);
  158. grid_h = grid_w;
  159. y = y + 32;
  160.  
  161. if not is_plant then
  162. lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Seeds per:");
  163. is_done, seeds_per_pass = lsEditBox("seedsper",
  164. 110, y, z, 50, 30, scale, scale,
  165. 0x000000ff, 4);
  166. seeds_per_pass = tonumber(seeds_per_pass);
  167. if not seeds_per_pass then
  168. is_done = nil;
  169. lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  170. seeds_per_pass = 1;
  171. end
  172. y = y + 32;
  173. end
  174.  
  175. is_plant = lsCheckBox(10, y, z+10, 0xFFFFFFff, "Grow Flax", is_plant);
  176. y = y + 32;
  177.  
  178. if lsButtonText(170, y-32, z, 100, 0xFFFFFFff, "OK") then
  179. is_done = 1;
  180. end
  181.  
  182. if is_plant then
  183. lsPrintWrapped(10, y, z+10, lsScreenX - 20, 0.7, 0.7, 0xD0D0D0ff, "This will plant and harvest a " .. grid_w .. "x" .. grid_w .. " grid of Flax " .. num_loops .. " times, requiring " .. (grid_w * grid_w * num_loops) .. " seeds, doing " .. (grid_w*grid_w*num_loops) .. " flax harvests.");
  184. else
  185. -- rip_out_when_done = lsCheckBox(10, y, z+10, 0xFFFFFFff, "Rip out when done", rip_out_when_done);
  186. -- y = y + 25;
  187. lsPrintWrapped(10, y, z+10, lsScreenX - 20, 0.7, 0.7, 0xD0D0D0ff, "This will plant a " .. grid_w .. "x" .. grid_w .. " grid of Flax and harvest it " .. seeds_per_pass .. " times, requiring " .. (grid_w * grid_w) .. " seeds, and repeat this " .. num_loops .. " times, yielding " .. (grid_w * grid_w * num_loops * seeds_per_pass) .. " seeds.");
  188. end
  189.  
  190. if is_done and (not num_loops or not grid_w) then
  191. error 'Canceled';
  192. end
  193.  
  194. if lsButtonText(lsScreenX - 110, lsScreenY - 30, z, 100, 0xFFFFFFff, "End script") then
  195. error "Clicked End Script button";
  196. end
  197.  
  198.  
  199. lsDoFrame();
  200. lsSleep(10); -- Sleep just so we don't eat up all the CPU for no reason
  201. end
  202. end
  203.  
  204. function safeBegin()
  205. local oldX = 0;
  206. local oldY = 0;
  207. oldX, oldY = srMousePos();
  208. local at_rest = false;
  209. local loopCount = 0;
  210. while not at_rest do
  211. lsSleep(10);
  212. local currentX = 0;
  213. local currentY = 0;
  214. currentX, currentY = srMousePos();
  215. at_rest = (currentX == oldX and currentY == oldY);
  216. oldX = currentX;
  217. oldY = currentY;
  218. loopCount = loopCount + 1;
  219. if loopCount > 200 then
  220. error "Error: The mouse keeps moving"
  221. end
  222. end
  223. srMouseUp(oldX, oldY);
  224. end
  225.  
  226. function safeClick(x, y, rightClick)
  227. safeBegin();
  228. srClickMouseNoMove(x, y, rightClick);
  229. end
  230.  
  231. function safeDrag(sourceX, sourceY, destX, destY)
  232. safeBegin();
  233. drag(sourceX, sourceY, destX, destY);
  234. end
  235.  
  236. -- Just testing spiral algorithm, ignore this...
  237. function spiraltest()
  238. -- for spiral
  239. local dxi=1;
  240. local dt_max=grid_w;
  241. local dt=grid_w;
  242. local dx={1, 0, -1, 0};
  243. local dy={0, -1, 0, 1};
  244. local num_at_this_length = 3;
  245.  
  246. -- Plant and pin
  247. for y=1, grid_h do
  248. for x=1, grid_w do
  249. lsPrintln('doing ' .. x .. ',' .. y);
  250.  
  251. -- move to next position
  252. if not ((x == grid_w) and (y == grid_h)) then
  253. lsPrintln('walking dx=' .. dx[dxi] .. ' dy=' .. dy[dxi]);
  254. dt = dt - 1;
  255. if dt == 1 then
  256. dxi = dxi + 1;
  257. num_at_this_length = num_at_this_length - 1;
  258. if num_at_this_length == 0 then
  259. dt_max = dt_max - 1;
  260. num_at_this_length = 2;
  261. end
  262. if dxi == 5 then
  263. dxi = 1;
  264. end
  265. dt = dt_max;
  266. end
  267. else
  268. lsPrintln('skipping walking, on last leg');
  269. end
  270. end
  271. end
  272. end
  273.  
  274. function doit()
  275. -- num_loops = promptNumber("How many " .. grid_w .. "x" .. grid_h .. " passes ?", 5);
  276. promptFlaxNumbers();
  277.  
  278. askForWindow("Script by Jimbly with tweaks from Cegaiel, KasumiGhia, and Tallow\n\nMake sure the plant flax window is pinned and on the RIGHT side of the screen. Your VeggieTales window should also be on the RIGHT side of the screen. You must be in F8F8 cam zoomed in. You may need to F12 at low resolutions or hide your chat window (if it starts planting and fails to move downward, it probably clicked on your chat window). Will plant grid NE of current location. 'Plant all crops where you stand' must be ON. 'Right click pins/unpins a menu' must be ON. Enable Hotkeys on flax must be OFF.");
  279.  
  280. initGlobals();
  281.  
  282. local went_to_seeds = 0; -- Don't loop if we lost one, it'll mess us up!
  283. srReadScreen();
  284. local xyPlantFlax = srFindImage(imgFlax1);
  285. if not xyPlantFlax then
  286. error 'Could not find plant window';
  287. end
  288. xyPlantFlax[0] = xyPlantFlax[0] + 5;
  289. local xyCenter = getCenterPos();
  290. local xyFlaxMenu = {};
  291. xyFlaxMenu[0] = xyCenter[0] - 43*pixel_scale;
  292. xyFlaxMenu[1] = xyCenter[1] + 0;
  293.  
  294. for loop_count=1, num_loops do
  295.  
  296. -- for spiral
  297. local dxi=1;
  298. local dt_max=grid_w;
  299. local dt=grid_w;
  300. local dx={1, 0, -1, 0};
  301. local dy={0, -1, 0, 1};
  302. local num_at_this_length = 3;
  303. local x_pos = 0;
  304. local y_pos = 0;
  305. local bit = 0;
  306.  
  307. -- Plant and pin
  308. for y=1, grid_h do
  309. for x=1, grid_w do
  310. if bit == 0 then
  311. bit = 2;
  312. else
  313. bit = 0;
  314. end
  315. lsPrintln('doing ' .. x .. ',' .. y .. ' of ' .. grid_w .. ',' .. grid_h);
  316.  
  317. statusScreen("(" .. loop_count .. "/" .. num_loops .. ") Planting " .. x .. ", " .. y);
  318.  
  319. -- Plant
  320. lsPrintln('planting ' .. xyPlantFlax[0] .. ',' .. xyPlantFlax[1]);
  321. setWaitSpot(xyFlaxMenu[0], xyFlaxMenu[1]);
  322. safeClick(xyPlantFlax[0], xyPlantFlax[1], 0);
  323. srSetMousePos(xyFlaxMenu[0], xyFlaxMenu[1]);
  324. waitForChange();
  325. -- lsSleep(delay_time);
  326.  
  327. -- Bring up menu
  328. lsPrintln('menu ' .. xyFlaxMenu[0] .. ',' .. xyFlaxMenu[1]);
  329. setWaitSpot(xyFlaxMenu[0]+5, xyFlaxMenu[1]);
  330. safeClick(xyFlaxMenu[0], xyFlaxMenu[1], 0);
  331. waitForChange();
  332. -- lsSleep(delay_time);
  333.  
  334. -- Check for window size
  335. checkWindowSize(xyFlaxMenu[0], xyFlaxMenu[1]);
  336.  
  337. -- Pin
  338. lsPrintln('pin ' .. xyFlaxMenu[0] .. ',' .. xyFlaxMenu[1]);
  339. safeClick(xyFlaxMenu[0]+5, xyFlaxMenu[1], 1);
  340.  
  341. -- Move window
  342. srReadScreen();
  343. local windowList = findAllImages("ThisIs.png");
  344. if #windowList >= 2 then
  345. table.remove(windowList, 1);
  346. end
  347. for i=1,#windowList do
  348. lsPrintln('move ' .. windowList[i][0] .. ',' .. windowList[i][1] .. ' to ' .. 20 .. ',' .. (100+bit));
  349. safeDrag(windowList[i][0], windowList[i][1],
  350. 20, 100 + bit, 0);
  351. lsSleep(50);
  352. end
  353.  
  354. -- move to next position
  355. if not ((x == grid_w) and (y == grid_h)) then
  356. lsPrintln('walking dx=' .. dx[dxi] .. ' dy=' .. dy[dxi]);
  357. x_pos = x_pos + dx[dxi];
  358. y_pos = y_pos + dy[dxi];
  359. safeClick(xyCenter[0] + walk_px_x*dx[dxi], xyCenter[1] + walk_px_y*dy[dxi], 0);
  360. lsSleep(walk_time - 50);
  361. dt = dt - 1;
  362. if dt == 1 then
  363. dxi = dxi + 1;
  364. num_at_this_length = num_at_this_length - 1;
  365. if num_at_this_length == 0 then
  366. dt_max = dt_max - 1;
  367. num_at_this_length = 2;
  368. end
  369. if dxi == 5 then
  370. dxi = 1;
  371. end
  372. dt = dt_max;
  373. end
  374. else
  375. lsPrintln('skipping walking, on last leg');
  376. end
  377. end
  378. checkBreak();
  379. end
  380.  
  381. -- Move windows into a grid
  382. local moveX = 1;
  383. local moveY = 1;
  384. --local screenWidth = srGetWindowSize()[0];
  385. lsSleep(25);
  386. srReadScreen();
  387. local corner =
  388. srFindImageInRange("ThisIs.png", 10,
  389. 40, 100, 100, 5000);
  390. while corner do
  391. safeDrag(corner[0], corner[1],
  392. window_w*(grid_w - moveX + 1),
  393. window_h*(grid_h - moveY + 1));
  394. moveX = moveX + 1;
  395. if moveX > grid_w then
  396. moveX = 1;
  397. moveY = moveY + 1;
  398. end
  399. lsSleep(25);
  400. srReadScreen();
  401. corner =
  402. srFindImageInRange("ThisIs.png", 10,
  403. 40, 100, 100, 5000);
  404. end
  405.  
  406. -- Bring windows to front
  407. -- refocusWindows();
  408.  
  409. local did_harvest=false;
  410. local harvestLeft = 0;
  411. local seedIndex = 1;
  412. local seedWave = 1;
  413. local lastTops = {};
  414. while not did_harvest do
  415. -- Monitor for Weed This/etc
  416. lsSleep(screen_refresh_time);
  417. srReadScreen();
  418. local tops = findAllImages("ThisIs.png");
  419. for i=1,#tops do
  420. safeClick(tops[i][0], tops[i][1]);
  421. lsSleep(50);
  422. end
  423.  
  424. if is_plant then
  425. harvestLeft = #tops;
  426. else
  427. harvestLeft = (#tops - seedIndex) + 1
  428. + (#tops * (seeds_per_pass - seedWave));
  429. end
  430.  
  431. statusScreen("(" .. loop_count .. "/" .. num_loops .. ") Harvests Left: " .. harvestLeft);
  432.  
  433. lsSleep(screen_refresh_time);
  434. srReadScreen();
  435. if is_plant then
  436. local weeds = findAllImages(imgWeed);
  437. for i=1,#weeds do
  438. safeClick(weeds[i][0], weeds[i][1]);
  439. --lsSleep(screen_refresh_time);
  440. end
  441.  
  442. local waters = findAllImages(imgWeedAndWater);
  443. for i=1,#waters do
  444. safeClick(waters[i][0], waters[i][1]);
  445. --lsSleep(screen_refresh_time);
  446. end
  447.  
  448. local harvests = findAllImages(imgHarvest);
  449. for i=1,#harvests do
  450. safeClick(harvests[i][0], harvests[i][1]);
  451. lsSleep(screen_refresh_time);
  452. safeClick(harvests[i][0], harvests[i][1] - 15, 1);
  453. end
  454. local seeds = findAllImages(imgSeeds);
  455. for i=1,#seeds do
  456. local seedTop = srFindImageInRange("ThisIs.png",
  457. seeds[i][0] - 10, seeds[i][1]-window_h,
  458. window_w, window_h, 5000);
  459. if seedTop then
  460. ripOut(seedTop);
  461. end
  462. end
  463. else
  464. srReadScreen();
  465. local tops = findAllImages("ThisIs.png");
  466. if #tops > 0 then
  467. if seedIndex > #tops then
  468. seedIndex = 1;
  469. seedWave = seedWave + 1;
  470. end
  471. local seedPos = srFindImageInRange(imgSeeds,
  472. tops[seedIndex][0],
  473. tops[seedIndex][1],
  474. 160, 100);
  475. if seedPos and seedWave <= seeds_per_pass then
  476. safeClick(seedPos[0] + 5, seedPos[1]);
  477. lsSleep(harvest_seeds_time);
  478. seedIndex = seedIndex + 1;
  479. end
  480. end
  481. if seedWave > seeds_per_pass then
  482. local seeds = findAllImages("ThisIs.png");
  483. for i=1,#seeds do
  484. ripOut(seeds[i]);
  485. end
  486. end
  487. end
  488. --if is_plant then
  489. --end
  490.  
  491. if #tops <= 0 then
  492. did_harvest = true;
  493. end
  494. checkBreak();
  495. end
  496.  
  497. -- Remove all windows
  498. --for i=1,#lastTops do
  499. -- safeClick(lastTops[i][0], lastTops[i][1], 1);
  500. --end
  501.  
  502. lsSleep(2500); -- Wait for last flax bed to disappear before accidentally clicking on it!
  503. statusScreen("(" .. loop_count .. "/" .. num_loops .. ") Walking...");
  504.  
  505. if went_to_seeds == 0 then
  506. -- Walk back
  507. for x=1, x_pos do
  508. safeClick(xyCenter[0] + walk_px_x*-1, xyCenter[1], 0);
  509. lsSleep(walk_time);
  510. end
  511. for x=1, -y_pos do
  512. safeClick(xyCenter[0], xyCenter[1] + walk_px_y, 0);
  513. lsSleep(walk_time);
  514. end
  515. end
  516.  
  517. if went_to_seeds and not loop_count == num_loops then
  518. error 'Some of the plants went to seeds, stopping loop'
  519. end
  520. end
  521.  
  522. lsPlaySound("Complete.wav");
  523. end
  524.  
  525. function ripOut(pos)
  526. while 1 do
  527. -- statusScreen(loop_label .. " Ripping out...");
  528. statusScreen("Ripping Out");
  529. lsSleep(screen_refresh_time);
  530. srReadScreen();
  531. local util_menu = srFindImageInRange("Utility.png", pos[0] - 10, pos[1] - 50, 180, 200);
  532. if util_menu then
  533. safeClick(util_menu[0] + 5, util_menu[1], 0);
  534. while 1 do
  535. lsSleep(screen_refresh_time);
  536. srReadScreen();
  537. local rip_out = srFindImage("RipOut.png");
  538. if rip_out then
  539. safeClick(rip_out[0] + 5, rip_out[1], 0);
  540. lsSleep(refocus_time);
  541. safeClick(pos[0], pos[1], 1); -- unpin
  542. lsSleep(screen_refresh_time);
  543. break;
  544. end
  545. checkBreak();
  546. end
  547. break;
  548. end
  549. checkBreak();
  550. end
  551. end
Add Comment
Please, Sign In to add comment