Guest User

Untitled

a guest
Jul 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.51 KB | None | 0 0
  1. -- Plant flax and harvest either flax or seeds. Version 1.1
  2. --
  3. -- Works Reliably: 2x2, 3x3, 4x4, 5x5
  4. -- May Work (depends on your computer): 6x6, 7x7
  5.  
  6. -- Global parameters set by prompt box.
  7. grid_w = 5;
  8. grid_h = 5;
  9. is_plant = true;
  10. seeds_per_pass = 5;
  11.  
  12. loadfile("luaScripts/screen_reader_common.inc")();
  13. loadfile("luaScripts/ui_utils.inc")();
  14.  
  15. imgFlax1 = "FlaxGeneric.png";
  16. imgHarvest = "HarvestThisFlax.png";
  17. imgWeedAndWater = "WeedAndWater.png";
  18. imgWeed = "WeedThisFlaxBed.png";
  19. imgSeeds = "HarvestSeeds.png";
  20. imgUseable = "UseableBy.png";
  21. imgThisIs = "ThisIs.png";
  22. imgUtility = "Utility.png";
  23. imgRipOut = "RipOut.png";
  24. imgUnpin = "UnPin.png";
  25.  
  26. -- Tweakable delay values
  27. tick_time = 10; -- Time to sleep in a polling loop
  28. delay_time = 50; -- Time to wait between non-dependent clicks
  29. refresh_time = 300; -- Time to wait for windows to update
  30. walk_time = 300;
  31.  
  32. -- Don't touch. These are set according to Jimbly's black magic.
  33. walk_px_y = 340;
  34. walk_px_x = 380;
  35. xyWindowSize = srGetWindowSize();
  36. xyCenter = {};
  37. xyFlaxMenu = {};
  38.  
  39. -- The flax bed window
  40. window_w = 174;
  41. window_h = 100;
  42.  
  43. -------------------------------------------------------------------------------
  44. -- initGlobals()
  45. --
  46. -- Set up black magic values used for trying to walk a standard grid.
  47. -------------------------------------------------------------------------------
  48.  
  49. function initGlobals()
  50.   -- Macro written with 1720 pixel wide window
  51.   local pixel_scale = xyWindowSize[0] / 1720;
  52.   lsPrintln("pixel_scale " .. pixel_scale);
  53.  
  54.   walk_px_y = math.floor(walk_px_y * pixel_scale);
  55.   walk_px_x = math.floor(walk_px_x * pixel_scale);
  56.  
  57.   local walk_x_drift = 14;
  58.   local walk_y_drift = 18;
  59.   if (lsScreenX < 1280) then
  60.     -- Have to click way off center in order to not move at high resoltuions
  61.     walk_x_drift = math.floor(walk_x_drift * pixel_scale);
  62.     walk_y_drift = math.floor(walk_y_drift * pixel_scale);
  63.   else
  64.     -- Very little drift at these resolutions, clicking dead center barely moves
  65.     walk_x_drift = 1;
  66.     walk_y_drift = 1;
  67.   end
  68.  
  69.   xyCenter[0] = xyWindowSize[0] / 2 - walk_x_drift;
  70.   xyCenter[1] = xyWindowSize[1] / 2 + walk_y_drift;
  71.   xyFlaxMenu[0] = xyCenter[0] - 43*pixel_scale;
  72.   xyFlaxMenu[1] = xyCenter[1] + 0;
  73. end
  74.  
  75. -------------------------------------------------------------------------------
  76. -- checkWindowSize()
  77. --
  78. -- Set width and height of flax window based on whether they are guilded.
  79. -------------------------------------------------------------------------------
  80.  
  81. window_check_done_once = false;
  82. function checkWindowSize(x, y)
  83.   if not window_check_done_once then
  84.     srReadScreen();
  85.     window_check_done_once = true;
  86.     local pos = srFindImageInRange(imgUseable, x-5, y-50, 150, 100)
  87.     if pos then
  88.       window_w = 166;
  89.       window_h = 116;
  90.     end
  91.   end
  92. end
  93.  
  94. -------------------------------------------------------------------------------
  95. -- setWaitSpot()
  96. --
  97. -- Initialize position and pixel value for waitForChange() or waitForStasis()
  98. -------------------------------------------------------------------------------
  99.  
  100. function setWaitSpot(x0, y0)
  101.   setWaitSpot_x = x0;
  102.   setWaitSpot_y = y0;
  103.   setWaitSpot_px = srReadPixel(x0, y0);
  104. end
  105.  
  106. -------------------------------------------------------------------------------
  107. -- waitForChange()
  108. --
  109. -- Wait for pixel previously initialized in setWaitSpot() to change.
  110. -------------------------------------------------------------------------------
  111.  
  112. function waitForChange(timeout)
  113.   local success = true;
  114.   local timestart = lsGetTimer();
  115.   local pixel = srReadPixel(setWaitSpot_x, setWaitSpot_y)
  116.   while pixel == setWaitSpot_px do
  117.     lsSleep(tick_time);
  118.     if (lsShiftHeld() and lsControlHeld()) then
  119.       error 'broke out of loop from Shift+Ctrl';
  120.     end
  121.     if timeout and (lsGetTimer() > timestart + timeout) then
  122.       lsPrintln('Timed out waiting.');
  123.       success = false;
  124.       break;
  125.     end
  126.     pixel = srReadPixel(setWaitSpot_x, setWaitSpot_y);
  127.   end
  128.   lsPrintln('Waited ' .. (lsGetTimer() - timestart) ..
  129.             'ms for pixel to change.');
  130.   return success;
  131. end
  132.  
  133. function isHomogenous(list)
  134.   local result = true;
  135.   for i=1,#list do
  136.     if list[i] ~= list[1] then
  137.       result = false;
  138.     end
  139.   end
  140.   return result;
  141. end
  142.  
  143. -------------------------------------------------------------------------------
  144. -- waitForStasis()
  145. --
  146. -- Wait for pixel previously initialized in setWaitSpot() to change.
  147. -------------------------------------------------------------------------------
  148.  
  149. function waitForStasis(timeout)
  150.   local lastPixels = {0, 1, 2, 3, 4, 5, 6};
  151.   local index = 1;
  152.   local success = true;
  153.   local timestart = lsGetTimer();
  154. --  local pixel = srReadPixel(setWaitSpot_x, setWaitSpot_y)
  155.   while not isHomogenous(lastPixels) do
  156.     if (lsShiftHeld() and lsControlHeld()) then
  157.       error 'broke out of loop from Shift+Ctrl';
  158.     end
  159.     if timeout and (lsGetTimer() > timestart + timeout) then
  160.       lsPrintln('Timed out waiting.');
  161.       success = false;
  162.       break;
  163.     end
  164.     lsSleep(tick_time);
  165.     pixel = srReadPixel(setWaitSpot_x, setWaitSpot_y);
  166.     lastPixels[index] = pixel;
  167.     index = index + 1;
  168.     if (index > #lastPixels) then
  169.       index = 1;
  170.     end
  171.   end
  172.   lsPrintln('Waited ' .. (lsGetTimer() - timestart) ..
  173.             'ms for pixel to change.');
  174.   return success;
  175. end
  176.  
  177. -------------------------------------------------------------------------------
  178. -- waitForImage(file, timeout)
  179. --
  180. -- Wait for a particular image to appear subject to a timeout in ms.
  181. -------------------------------------------------------------------------------
  182.  
  183. function waitForImage(file, timeout)
  184.   return waitForImageInRange(file, 0, 0, xyWindowSize[0], xyWindowSize[1],
  185.                              timeout);
  186. end
  187.  
  188. -------------------------------------------------------------------------------
  189. -- waitForImageInRange(file, x, y, width, height, timeout)
  190. --
  191. -- Wait for an image to appear within a box subject to a timeout in ms.
  192. -------------------------------------------------------------------------------
  193.  
  194. function waitForImageInRange(file, x, y, width, height, timeout)
  195.   local done = false;
  196.   local image = null;
  197.   local timestart = lsGetTimer();
  198.   while not done do
  199.     lsSleep(tick_time);
  200.     srReadScreen();
  201.     image = srFindImageInRange(file, x, y, width, height, 5000);
  202.     done = (image ~= null) ;
  203.     if timeout and (lsGetTimer() > timestart + timeout) then
  204.       lsPrintln('Timed out waiting.');
  205.       done = true;
  206.     end
  207.  end
  208.   return image;
  209. end
  210.  
  211. -------------------------------------------------------------------------------
  212. -- drag()
  213. --
  214. -- Drag the mouse from (sourceX, sourceY) to (destX, destY)
  215. -------------------------------------------------------------------------------
  216.  
  217. function drag(sourceX, sourceY, destX, destY)
  218.   setWaitSpot(destX, destY);
  219.   srSetMousePos(sourceX, sourceY);
  220.   srMouseDown(sourceX, sourceY, 0);
  221.   srSetMousePos(destX, destY);
  222.   local result = waitForChange(500);
  223.   srMouseUp(destX, destY, 0);
  224.   return result;
  225. end
  226.  
  227. -------------------------------------------------------------------------------
  228. -- promptFlaxNumbers()
  229. --
  230. -- Gather user-settable parameters before beginning
  231. -------------------------------------------------------------------------------
  232.  
  233. function promptFlaxNumbers()
  234.   scale = 1.0;
  235.    
  236.   local z = 0;
  237.   local is_done = nil;
  238.   local value = nil;
  239.   -- Edit box and text display
  240.   while not is_done do
  241.     -- Make sure we don't lock up with no easy way to escape!
  242.     checkBreak("disallow pause");
  243.  
  244.     lsPrint(10, 10, z, scale, scale, 0xFFFFFFff, "Choose passes and grid size");
  245.  
  246.     -- lsEditBox needs a key to uniquely name this edit box
  247.     --   let's just use the prompt!
  248.     -- lsEditBox returns two different things (a state and a value)
  249.     local y = 40;
  250.  
  251.     lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Passes:");
  252.     is_done, num_loops = lsEditBox("passes", 110, y, z, 50, 30, scale, scale,
  253.                                    0x000000ff, 5);
  254.     if not tonumber(num_loops) then
  255.       is_done = nil;
  256.       lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  257.       num_loops = 1;
  258.     end
  259.     y = y + 32;
  260.  
  261.     lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Grid size:");
  262.     is_done, grid_w = lsEditBox("grid", 110, y, z, 50, 30, scale, scale,
  263.                                 0x000000ff, grid_w);
  264.     if not tonumber(grid_w) then
  265.       is_done = nil;
  266.       lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  267.       grid_w = 1;
  268.       grid_h = 1;
  269.     end
  270.     grid_w = tonumber(grid_w);
  271.     grid_h = grid_w;
  272.     y = y + 32;
  273.  
  274.     if not is_plant then
  275.       lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "Seeds per:");
  276.       is_done, seeds_per_pass = lsEditBox("seedsper", 110, y, z, 50, 30,
  277.                                           scale, scale, 0x000000ff, 4);
  278.       seeds_per_pass = tonumber(seeds_per_pass);
  279.       if not seeds_per_pass then
  280.         is_done = nil;
  281.         lsPrint(10, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
  282.         seeds_per_pass = 1;
  283.       end
  284.       y = y + 32;
  285.     end
  286.  
  287.     is_plant = lsCheckBox(10, y, z+10, 0xFFFFFFff, "Grow Flax", is_plant);
  288.     y = y + 32;
  289.  
  290.     if lsButtonText(170, y-32, z, 100, 0xFFFFFFff, "OK") then
  291.       is_done = 1;
  292.     end
  293.  
  294.     if is_plant then
  295.       lsPrintWrapped(10, y, z+10, lsScreenX - 20, 0.7, 0.7, 0xD0D0D0ff,
  296.                      "This will plant and harvest a " .. grid_w .. "x" ..
  297.                      grid_w .. " grid of Flax " .. num_loops ..
  298.                      " times, requiring " .. (grid_w * grid_w * num_loops) ..
  299.                      " seeds, doing " .. (grid_w*grid_w*num_loops) ..
  300.                      " flax harvests.");
  301.     else
  302.       local seedTotal = grid_w * grid_h * num_loops * seeds_per_pass
  303.       lsPrintWrapped(10, y, z+10, lsScreenX - 20, 0.7, 0.7, 0xD0D0D0ff,
  304.                      "This will plant a " .. grid_w .. "x" .. grid_w ..
  305.                      " grid of Flax and harvest it " .. seeds_per_pass ..
  306.                      " times, requiring " .. (grid_w * grid_w) ..
  307.                      " seeds, and repeat this " .. num_loops ..
  308.                      " times, yielding " .. seedTotal .. " seed harvests.");
  309.     end
  310.  
  311.     if is_done and (not num_loops or not grid_w) then
  312.       error 'Canceled';
  313.     end
  314.        
  315.     if lsButtonText(lsScreenX - 110, lsScreenY - 30, z, 100, 0xFFFFFFff,
  316.                     "End script") then
  317.       error "Clicked End Script button";
  318.     end
  319.  
  320.     lsDoFrame();
  321.     lsSleep(tick_time);
  322.   end
  323. end
  324.  
  325. -------------------------------------------------------------------------------
  326. -- safeBegin()
  327. --
  328. -- Call this just before a click or a drag to make sure the user isn't
  329. -- moving the mouse or clicking it. Reduces the chances of interference.
  330. -------------------------------------------------------------------------------
  331.  
  332. function safeBegin()
  333.   local oldX = 0;
  334.   local oldY = 0;
  335.   oldX, oldY = srMousePos();
  336.   local at_rest = false;
  337.   local loopCount = 0;
  338.   while not at_rest do
  339.     lsSleep(tick_time);
  340.     local currentX = 0;
  341.     local currentY = 0;
  342.     currentX, currentY = srMousePos();
  343.     at_rest = (currentX == oldX and currentY == oldY);
  344.     oldX = currentX;
  345.     oldY = currentY;
  346.     loopCount = loopCount + 1;
  347.     if loopCount > 200 then
  348.       error "Error: The mouse keeps moving"
  349.     end
  350.   end
  351.   srMouseUp(oldX, oldY);
  352. end
  353.  
  354. -------------------------------------------------------------------------------
  355. -- safeClick()
  356. --
  357. -- Click the mouse without moving it.
  358. -------------------------------------------------------------------------------
  359.  
  360. function safeClick(x, y, rightClick)
  361.   safeBegin();
  362.   srClickMouseNoMove(x, y, rightClick);
  363. end
  364.  
  365. -------------------------------------------------------------------------------
  366. -- safeDrag()
  367. -------------------------------------------------------------------------------
  368.  
  369. function safeDrag(sourceX, sourceY, destX, destY)
  370.   safeBegin();
  371.   return drag(sourceX, sourceY, destX, destY);
  372. end
  373.  
  374. -------------------------------------------------------------------------------
  375. -- getPlantWindowPos()
  376. -------------------------------------------------------------------------------
  377.  
  378. lastPlantPos = null;
  379.  
  380. function getPlantWindowPos()
  381.   srReadScreen();
  382.   local plantPos = srFindImage(imgFlax1);
  383.   if plantPos then
  384.     plantPos[0] = plantPos[0] + 5;
  385.   else
  386.     plantPos = lastPlantPos;
  387.     if plantPos then
  388.       safeClick(plantPos[0], plantPos[1]);
  389.       lsSleep(refresh_time);
  390.     end
  391.   end
  392.   if not plantPos then
  393.     error 'Could not find plant window';
  394.   end
  395.   lastPlantPos = plantPos;
  396.   return plantPos;
  397. end
  398.  
  399. -------------------------------------------------------------------------------
  400. -- getToggle()
  401. --
  402. -- Returns 0 or 2 alternately. Used to slightly shift position of windows
  403. -- while collecting them.
  404. -------------------------------------------------------------------------------
  405.  
  406. toggleBit = 0;
  407.  
  408. function getToggle()
  409.   if toggleBit == 0 then
  410.     toggleBit = 2;
  411.   else
  412.     toggleBit = 0;
  413.   end
  414.   return toggleBit;
  415. end
  416.  
  417. -------------------------------------------------------------------------------
  418. -- doit()
  419. -------------------------------------------------------------------------------
  420.  
  421. function doit()
  422.   promptFlaxNumbers();
  423.  
  424.   askForWindow("flax_stable v1.1 -- Script by Jimbly with tweaks from Cegaiel and KasumiGhia. Revised by 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.");
  425.    
  426.   initGlobals();
  427.  
  428.   for loop_count=1, num_loops do
  429.     local finalPos = plantAndPin(loop_count);
  430.     dragWindows(loop_count);
  431.     harvestFlax(loop_count);
  432.     walkHome(loop_count, finalPos);
  433.   end
  434.   lsPlaySound("Complete.wav");
  435. end
  436.  
  437. -------------------------------------------------------------------------------
  438. -- plantAndPin()
  439. --
  440. -- Walk around in a spiral, planting flax seeds and grabbing windows.
  441. -------------------------------------------------------------------------------
  442.  
  443. function plantAndPin(loop_count)
  444.   local xyPlantFlax = getPlantWindowPos();
  445.        
  446.   -- for spiral
  447.   local dxi=1;
  448.   local dt_max=grid_w;
  449.   local dt=grid_w;
  450.   local dx={1, 0, -1, 0};
  451.   local dy={0, -1, 0, 1};
  452.   local num_at_this_length = 3;
  453.   local x_pos = 0;
  454.   local y_pos = 0;
  455.   local success = true;
  456.  
  457.   for y=1, grid_h do
  458.     for x=1, grid_w do
  459.       statusScreen("(" .. loop_count .. "/" .. num_loops .. ") Planting " ..
  460.                    x .. ", " .. y);
  461.       success = plantHere(xyPlantFlax, y);
  462.       if not success then
  463.         break;
  464.       end
  465.  
  466.       -- Move to next position
  467.       if not ((x == grid_w) and (y == grid_h)) then
  468.         lsPrintln('walking dx=' .. dx[dxi] .. ' dy=' .. dy[dxi]);
  469.         x_pos = x_pos + dx[dxi];
  470.         y_pos = y_pos + dy[dxi];
  471.         safeClick(xyCenter[0] + walk_px_x*dx[dxi],
  472.                   xyCenter[1] + walk_px_y*dy[dxi], 0);
  473.         setWaitSpot(xyFlaxMenu[0], xyFlaxMenu[1]);
  474.         lsSleep(walk_time);
  475.         waitForStasis(1000);
  476.         dt = dt - 1;
  477.         if dt == 1 then
  478.           dxi = dxi + 1;
  479.           num_at_this_length = num_at_this_length - 1;
  480.           if num_at_this_length == 0 then
  481.             dt_max = dt_max - 1;
  482.             num_at_this_length = 2;
  483.           end
  484.           if dxi == 5 then
  485.             dxi = 1;
  486.           end
  487.           dt = dt_max;
  488.         end
  489.       else
  490.         lsPrintln('skipping walking, on last leg');
  491.       end
  492.     end
  493.     checkBreak();
  494.     if not success then
  495.       break;
  496.     end
  497.   end
  498.   local finalPos = {};
  499.   finalPos[0] = x_pos;
  500.   finalPos[1] = y_pos;
  501.   return finalPos;
  502. end
  503.  
  504. -------------------------------------------------------------------------------
  505. -- plantHere(xyPlantFlax)
  506. --
  507. -- Plant a single flax bed, get the window, pin it, then stash it.
  508. -------------------------------------------------------------------------------
  509.  
  510. function plantHere(xyPlantFlax, y_pos)
  511.   -- Plant
  512.   lsPrintln('planting ' .. xyPlantFlax[0] .. ',' .. xyPlantFlax[1]);
  513.   setWaitSpot(xyFlaxMenu[0], xyFlaxMenu[1]);
  514.   safeClick(xyPlantFlax[0], xyPlantFlax[1], 0);
  515.   srSetMousePos(xyFlaxMenu[0], xyFlaxMenu[1]);
  516.   local plantSuccess = waitForChange(500);
  517.   if not plantSuccess then
  518.     --error "No plant was placed. Abort this run.";
  519.     return false;
  520.   end
  521.  
  522.   -- Bring up menu
  523.   lsPrintln('menu ' .. xyFlaxMenu[0] .. ',' .. xyFlaxMenu[1]);
  524.   safeBegin();
  525.   srClickMouse(xyFlaxMenu[0], xyFlaxMenu[1], 0);
  526.   local windowPos = waitForImageInRange(imgThisIs, xyFlaxMenu[0]-50,
  527.                                         xyFlaxMenu[1]-200, 300, 300, 500);
  528.   if not windowPos then
  529.     --error "No window came up. Abort this run.";
  530.     return false;
  531.   end
  532.  
  533.   -- Check for window size
  534.   checkWindowSize(xyFlaxMenu[0], xyFlaxMenu[1]);
  535.  
  536.   -- Pin
  537.   lsPrintln('pin ' .. windowPos[0] .. ',' .. windowPos[1]);
  538.   safeClick(windowPos[0], windowPos[1], 1);
  539.  
  540.   -- Move window into corner
  541.   srReadScreen();
  542.   local windowList = findAllImages(imgThisIs);
  543.   if #windowList >= 2 then
  544.     table.remove(windowList, 1);
  545.   end
  546.   for i=1,#windowList do
  547.     local bit = getToggle();
  548.     lsPrintln('move ' .. windowList[i][0] .. ',' .. windowList[i][1] ..
  549.               ' to ' .. 20 .. ',' .. (100+bit));
  550.     local dragged = safeDrag(windowList[i][0], windowList[i][1],
  551.                              20, 100 + bit, 0);
  552.     if not dragged then
  553.       --error "Drag failed. Abort this run.";
  554.       return false;
  555.     end
  556.     lsSleep(delay_time);
  557.   end
  558.   return true;
  559. end
  560.  
  561. -------------------------------------------------------------------------------
  562. -- dragWindows(loop_count)
  563. --
  564. -- Move flax windows into a grid on the screen.
  565. -------------------------------------------------------------------------------
  566.  
  567. function dragWindows(loop_count)
  568.   statusScreen("(" .. loop_count .. "/" .. num_loops .. ")  " ..
  569.                "Dragging Windows into Grid");
  570.   local moveX = 1;
  571.   local moveY = 1;
  572.   lsSleep(delay_time);
  573.   srReadScreen();
  574.   local corner = srFindImageInRange(imgThisIs, 10, 40, 100, 100, 5000);
  575.   while corner do
  576.     safeDrag(corner[0], corner[1],
  577.              window_w*(grid_w - moveX + 1), window_h*(grid_h - moveY + 1));
  578.     moveX = moveX + 1;
  579.     if moveX > grid_w then
  580.       moveX = 1;
  581.       moveY = moveY + 1;
  582.     end
  583.     lsSleep(delay_time);
  584.     srReadScreen();
  585.     corner =  srFindImageInRange(imgThisIs, 10, 40, 100, 100, 5000);
  586.   end
  587. end
  588.  
  589. -------------------------------------------------------------------------------
  590. -- harvestFlax(loop_count)
  591. --
  592. -- Harvest all the flax or seeds and clean up the windows afterwards.
  593. -------------------------------------------------------------------------------
  594.  
  595. function harvestFlax(loop_count)
  596.   local did_harvest=false;
  597.   local harvestLeft = 0;
  598.   local seedIndex = 1;
  599.   local seedWave = 1;
  600.   local lastTops = {};
  601.   while not did_harvest do
  602.     -- Monitor for Weed This/etc
  603.     lsSleep(refresh_time);
  604.     srReadScreen();
  605.     local tops = findAllImages(imgThisIs);
  606.     for i=1,#tops do
  607.       safeClick(tops[i][0], tops[i][1]);
  608.       lsSleep(delay_time);
  609.     end
  610.  
  611.     if is_plant then
  612.       harvestLeft = #tops;
  613.     else
  614.       harvestLeft = (#tops - seedIndex) + 1 +
  615.                     (#tops * (seeds_per_pass - seedWave));
  616.     end
  617.  
  618.     statusScreen("(" .. loop_count .. "/" .. num_loops ..
  619.                  ") Harvests Left: " .. harvestLeft);
  620.  
  621.     lsSleep(refresh_time);
  622.     srReadScreen();
  623.     if is_plant then
  624.       local weeds = findAllImages(imgWeed);
  625.       for i=1,#weeds do
  626.         safeClick(weeds[i][0], weeds[i][1]);
  627.       end
  628.  
  629.       local waters = findAllImages(imgWeedAndWater);
  630.       for i=1,#waters do
  631.         safeClick(waters[i][0], waters[i][1]);
  632.       end
  633.  
  634.       local harvests = findAllImages(imgHarvest);
  635.       for i=1,#harvests do
  636.         safeClick(harvests[i][0], harvests[i][1]);
  637.         lsSleep(refresh_time);
  638.         safeClick(harvests[i][0], harvests[i][1] - 15, 1);
  639.       end
  640.  
  641.       local seeds = findAllImages(imgSeeds);
  642.       for i=1,#seeds do
  643.         local seedTop = srFindImageInRange(imgThisIs,
  644.                                         seeds[i][0] - 10, seeds[i][1]-window_h,
  645.                                         window_w, window_h, 5000);
  646.         if seedTop then
  647.           ripOut(seedTop);
  648.         end
  649.       end
  650.     else
  651.       srReadScreen();
  652.       local tops = findAllImages(imgThisIs);
  653.       if #tops > 0 then
  654.         if seedIndex > #tops then
  655.           seedIndex = 1;
  656.           seedWave = seedWave + 1;
  657.         end
  658.         local seedPos = srFindImageInRange(imgSeeds, tops[seedIndex][0],
  659.                     tops[seedIndex][1], 160, 100);
  660.         if seedPos and seedWave <= seeds_per_pass then
  661.           safeClick(seedPos[0] + 5, seedPos[1]);
  662.           lsSleep(harvest_seeds_time);
  663.           seedIndex = seedIndex + 1;
  664.         end
  665.       end
  666.       if seedWave > seeds_per_pass then
  667.         local seeds = findAllImages(imgThisIs);
  668.         for i=1,#seeds do
  669.           ripOut(seeds[i]);
  670.         end
  671.       end
  672.     end
  673.  
  674.     if #tops <= 0 then
  675.       did_harvest = true;
  676.     end
  677.     checkBreak();
  678.   end
  679. end
  680.  
  681. -------------------------------------------------------------------------------
  682. -- walkHome(loop_count, finalPos)
  683. --
  684. -- Walk back to the origin (southwest corner) to start planting again.
  685. -------------------------------------------------------------------------------
  686.  
  687. function walkHome(loop_count, finalPos)
  688.   -- Wait for last flax bed to disappear
  689.   statusScreen("(" .. loop_count .. "/" .. num_loops ..
  690.                ") Waiting for flax beds to disappear...");
  691.   lsSleep(2500);
  692.   unpinStragglers();
  693.   statusScreen("(" .. loop_count .. "/" .. num_loops .. ") Walking...");
  694.  
  695.   -- Walk back
  696.   for x=1, finalPos[0] do
  697.     safeClick(xyCenter[0] + walk_px_x*-1, xyCenter[1], 0);
  698.     lsSleep(walk_time);
  699.     waitForStasis(1000);
  700.   end
  701.   for x=1, -(finalPos[1]) do
  702.     safeClick(xyCenter[0], xyCenter[1] + walk_px_y, 0);
  703.     lsSleep(walk_time);
  704.     waitForStasis(1000);
  705.   end
  706. end
  707.  
  708. -------------------------------------------------------------------------------
  709. -- unpinStragglers()
  710. --
  711. -- If there are any empty windows left, unpin them.
  712. -------------------------------------------------------------------------------
  713. function unpinStragglers()
  714.   local pins = findAllImages(imgUnpin);
  715.   local plant = getPlantWindowPos();
  716.   for i=1,#pins do
  717.     if pins[i][0] < plant[0] then
  718.       safeClick(pins[i][0] + 3, pins[i][1] + 3);
  719.       lsSleep(delay_time);
  720.     end
  721.   end
  722. end
  723.  
  724. -------------------------------------------------------------------------------
  725. -- ripOut(pos)
  726. --
  727. -- Use the Utility menu to rip out a flax bed that has gone to seed.
  728. -- pos should be the screen position of the 'This Is' text on the window.
  729. -------------------------------------------------------------------------------
  730.  
  731. function ripOut(pos)
  732.   statusScreen("Ripping Out");
  733.   lsSleep(refresh_time);
  734.   srReadScreen();
  735.   local util_menu = srFindImageInRange(imgUtility, pos[0] - 10, pos[1] - 50,
  736.                                        180, 200, 5000);
  737.   if util_menu then
  738.     safeClick(util_menu[0] + 5, util_menu[1], 0);
  739.     local rip_out = waitForImage(imgRipOut, 1000);
  740.     if rip_out then
  741.       safeClick(rip_out[0] + 5, rip_out[1], 0);
  742.       lsSleep(refresh_time);
  743.       safeClick(pos[0], pos[1], 1); -- unpin
  744.       lsSleep(refresh_time);
  745.     end
  746.   end
  747. end
Add Comment
Please, Sign In to add comment