Skip to content
Browse files

Merge pull request #31 from duerig/master

Important fixes to thistle.lua, new cc_Auto.lua script
  • Loading branch information...
2 parents cf81991 + fee7037 commit 257be62c754a861877b5cfc4b08a6ecc327a192e @Vaevictusnet committed Apr 23, 2012
View
BIN VeggieTales/images/mm-corner.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
17 VeggieTales/luaScripts/cc_Assist.lua
@@ -71,13 +71,20 @@ end
function ccMenu()
while 1 do
for i=1, #buttons do
- if lsButtonText(buttons[i].buttonPos[0], buttons[i].buttonPos[1],
- 0, buttons[i].buttonSize, 0xFFFFFFff, buttons[i].name)
- then
- clickAllImages(buttons[i].image,
- buttons[i].offset[0], buttons[i].offset[1]);
+ if showButton(buttons[i]) then
+ runCommand(buttons[i]);
end
end
statusScreen("CC Control Center", 0x00d000ff);
end
end
+
+function showButton(button)
+ return lsButtonText(button.buttonPos[0], button.buttonPos[1],
+ 0, button.buttonSize, 0xFFFFFFff, button.name)
+end
+
+function runCommand(button)
+ clickAllImages(button.image, button.offset[0], button.offset[1]);
+end
+
View
178 VeggieTales/luaScripts/cc_Auto.lua
@@ -0,0 +1,178 @@
+-- cc-auto.lua v1.0 -- by Tallow, based on Tak's cc program
+--
+-- Automatically runs many charcoal hearths or ovens simultaneously.
+--
+
+assert(loadfile("luaScripts/cc_Assist.lua"))();
+
+askText = singleLine([[
+ CC Auto v1.0 (by Tallow, based on Tak's cc program) --
+ Automatically runs many charcoal hearths or ovens
+ simultaneously. Make sure the VT window is in the TOP-RIGHT corner
+ of the screen.
+]]);
+
+BEGIN = 1;
+WOOD = 2;
+WATER = 3;
+CLOSE = 4;
+OPEN = 5;
+FULL = 6;
+
+function ccMenu()
+ local passCount = promptNumber("How many passes?", 1, 1.0);
+ askForWindow(focusMessage);
+ for i=1,passCount do
+ runCommand(buttons[1]);
+ ccRun(i, passCount);
+ end
+end
+
+function findOvens()
+ local result = findAllImages("ThisIs.png");
+ for i=1,#result do
+ local corner = findImageInWindow("mm-corner.png",
+ result[i][0], result[i][1]);
+ if not corner then
+ error("Failed to find corner of cc window.");
+ end
+ result[i][1] = corner[1] - 102;
+ end
+ return result;
+end
+
+function setupVents(ovens)
+ local result = {};
+ for i=1,#ovens do
+ result[i] = 0;
+ end
+ return result;
+end
+
+function findButton(pos, index)
+ return findImageInWindow(buttons[index].image, pos[0], pos[1]);
+end
+
+function clickButton(pos, index)
+ local buttonPos = findButton(pos, index);
+ if buttonPos then
+ safeClick(buttonPos[0] + buttons[index].offset[0],
+ buttonPos[1] + buttons[index].offset[1]);
+ end
+end
+
+function getPixelDiffs(left, right)
+ local result = {};
+ for i=1,3 do
+ result[i] = math.abs((left % 256) - (right % 256));
+ left = math.floor(left / 256);
+ right = math.floor(right / 256);
+ end
+ return result;
+end
+
+function pixelCheck(oven, offset, color, tolerance)
+ local result = true;
+ local screenColor = srReadPixel(oven[0] + offset[0], oven[1] + offset[1]);
+ local diffs = getPixelDiffs(color, math.floor(screenColor/256));
+ for i=1,#diffs do
+ if diffs[i] > tolerance then
+ result = false;
+ break;
+ end
+ end
+ return result;
+end
+
+function ccRun(pass, passCount)
+ local ovens = findOvens();
+ local vents = setupVents(ovens);
+ local done = false;
+ while not done do
+ done = true;
+ for i=1,#ovens do
+ if not findButton(ovens[i], BEGIN) then
+ vents[i] = processOven(ovens[i], vents[i]);
+ done = false;
+ end
+ end
+ sleepWithStatusPause(2000, "(" .. pass .. " / " .. passCount .. ")\n" ..
+ "Waiting for next tick");
+ end
+end
+
+--px05 = 70
+--px25 = 93
+--px30 = 98
+--px35 = 104
+--px45 = 116
+--px50 = 121
+--px60 = 133
+--px65 = 139
+--px70 = 144
+--px85 = 162
+--px90 = 168
+
+progressGreen = makePoint(70, 174);
+maxDangerGreen = makePoint(150, 162);
+minHeat = makePoint(144, 115);
+minWood = makePoint(104, 139);
+minOxy = makePoint(80, 126);
+maxOxy = makePoint(100, 126);
+maxDanger = makePoint(158, 162);
+minWater = makePoint(67, 150);
+
+greenColor = 0x07FE05;
+barColor = 0x0706FD;
+
+function processOven(oven, vent)
+ local newVent = vent;
+ if pixelCheck(oven, progressGreen, greenColor, 4) then
+ -- Progress is green
+ if pixelCheck(oven, maxDangerGreen, barColor, 4) then
+ -- Danger too high
+ clickButton(oven, WATER);
+ elseif vent ~= 3 then
+ newVent = 3;
+ clickButton(oven, FULL);
+ end
+ else
+ -- Progress is not green
+ if not pixelCheck(oven, minHeat, barColor, 8) then
+ -- Heat too low
+ if not pixelCheck(oven, minWood, barColor, 8) then
+ -- Wood too low
+ clickButton(oven, WOOD);
+ end
+ end
+
+ if not pixelCheck(oven, minOxy, barColor , 8) then
+ -- Oxygen too low
+ if vent ~= 3 then
+ newVent = 3;
+ clickButton(oven, FULL);
+ end
+ elseif pixelCheck(oven, maxOxy, barColor, 8) then
+ -- Oxygen too high
+ if vent ~= 1 then
+ newVent = 1;
+ clickButton(oven, CLOSE);
+ end
+ else
+ -- Oxygen OK
+ if vent ~= 2 then
+ newVent = 2;
+ clickButton(oven, OPEN);
+ end
+ end
+
+ if pixelCheck(oven, maxDanger, barColor, 8) then
+ -- Danger > 90%
+ if not pixelCheck(oven, minWater, barColor, 8) then
+ -- Water < 2.6%
+ clickButton(oven, WATER);
+ end
+ end
+ end
+ return newVent;
+end
View
29 VeggieTales/luaScripts/common_find.inc
@@ -15,19 +15,35 @@
-------------------------------------------------------------------------------
function findAllImages(image_name, tol)
- if not image_name then
+ local size = srGetWindowSize();
+ return findAllImagesInRange(image_name, 0, 0, size[0], size[1], tol);
+end
+
+-------------------------------------------------------------------------------
+-- findAllImagesInRange(image_name, startX, startY, width, height, tol)
+--
+-- Returns an array of all positions matching the image.
+--
+-- image_name -- path and filename used to fetch the image
+-- startX, startY -- origin of search region
+-- width, height -- size of search region
+-- tol (optional) -- tolerance value used for comparison
+-------------------------------------------------------------------------------
+
+function findAllImagesInRange(image_name, startX, startY, width, height, tol)
+ if not image_name or not startX or not startY or not width or not height then
error("Incorrect number of arguments for findAllImages()");
end
if not tol then
tol = 5000;
end
+ local endX = startX + width;
+ local endY = startY + height;
local xyImageSize = srImageSize(image_name);
- local xyWindowSize = srGetWindowSize();
- local y = 0;
+ local y = startY;
local buttons = {};
while 1 do
- local pos = srFindImageInRange(image_name, 0, y,
- xyWindowSize[0], xyWindowSize[1]-y, tol);
+ local pos = srFindImageInRange(image_name, startX, y, width, endY-y, tol);
if not pos then
break;
end
@@ -38,7 +54,8 @@ function findAllImages(image_name, tol)
while 1 do
local x = pos[0]+1;
pos = srFindImageInRange(image_name, x, y,
- xyWindowSize[0]-x, xyImageSize[1], tol);
+ endX - x, math.min(xyImageSize[1], endY - y),
+ tol);
if not pos then
break;
end
View
4 VeggieTales/luaScripts/common_wait.inc
@@ -28,13 +28,13 @@ end
-- Wait for pixel at spot to change subject to a timeout.
--
-- spot -- 3-tuple of x, y, and the original color at that location
--- timeout -- Time in milliseconds to wait before giving up
+-- timeout (optional) -- Time in milliseconds to wait before giving up
--
-- returns true on change and false on timeout.
-------------------------------------------------------------------------------
function waitForChange(spot, timeout)
- if not spot or not timeout then
+ if not spot then
error("Incorrect number of arguments for waitForChange()");
end
if #spot ~= 3 then
View
49 VeggieTales/luaScripts/common_window.inc
@@ -168,6 +168,10 @@ function arrangeStashed(cascade)
screen[0] = screen[0] - lsGetWindowSize()[0];
local currentX = 0;
local currentY = 0;
+ if cascade then
+ currentX = 10;
+ currentY = 50;
+ end
local lastX = 0;
local lastY = 0;
local xMax = 0;
@@ -204,7 +208,7 @@ function arrangeStashed(cascade)
lastY = window[1] - bounds[1] + currentY;
safeDrag(window[0], window[1], lastX, lastY);
if cascade then
- currentX = currentX + 24;
+ currentX = currentX + 8;
currentY = currentY + 24;
else
currentX = currentX + width;
@@ -241,21 +245,21 @@ function closeAllWindows(x, y, width, height)
height = srGetWindowSize()[1];
end
srReadScreen();
- local current = srFindImageInRange("ThisIs.png", x, y, width, height);
- while current do
- safeClick(current[0], current[1], 1);
+ local images = findAllImagesInRange("ThisIs.png", x, y, width, height);
+ while #images >= 1 do
+ safeClick(images[#images][0], images[#images][1], 1);
sleepWithStatus(200, "Closing Windows");
srReadScreen();
- current = srFindImageInRange("ThisIs.png", x, y, width, height);
+ images = findAllImagesInRange("ThisIs.png", x, y, width, height);
end
srReadScreen();
current = srFindImageInRange("UnPin.png", x, y, width, height);
- while current do
- safeClick(current[0], current[1], 1);
+ while #images >= 1 do
+ safeClick(images[#images][0], images[#images][1], 1);
sleepWithStatus(click_delay, "Closing Windows");
srReadScreen();
- current = srFindImageInRange("UnPin.png", x, y, width, height);
+ images = findAllImagesInRange("UnPin.png", x, y, width, height);
end
end
@@ -277,22 +281,34 @@ function windowManager(title, message, overlap)
if not message then
message = "Tap shift to open and pin a window."
end
+ local pauseFocus = false;
local shift = false;
local done = false;
while not done do
lsPrint(10, 10, 0, 1.0, 1.0, 0xFFFFFFff, title);
if lsButtonText(30, 60, 0, 250, 0x80D080ff, "Arrange Windows") then
lsDoFrame();
+ if pauseFocus then
+ askForWindow(focusMessage);
+ end
arrangeInGrid(overlap);
end
- if lsButtonText(30, 100, 0, 250, 0x80D080ff, "Stash Windows") then
+ if lsButtonText(30, 90, 0, 250, 0x80D080ff, "Stash Windows") then
lsDoFrame();
+ if pauseFocus then
+ askForWindow(focusMessage);
+ end
stashAllWindows(BOTTOM_RIGHT);
end
- if lsButtonText(30, 140, 0, 250, 0x80D080ff, "Un-Pin Windows") then
+ if lsButtonText(30, 120, 0, 250, 0x80D080ff, "Un-Pin Windows") then
lsDoFrame();
+ if pauseFocus then
+ askForWindow(focusMessage);
+ end
closeAllWindows();
end
+ pauseFocus = lsCheckBox(30, 160, 10, 0xffffffff, "Pause for Focus",
+ pauseFocus);
lsPrintWrapped(10, 200, 0, 250, 0.7, 0.7, 0xd0d0d0ff,
message);
if lsButtonText(lsScreenX - 110, lsScreenY - 30, 0, 100,
@@ -317,6 +333,9 @@ function windowManager(title, message, overlap)
lsDoFrame();
lsSleep(tick_delay);
end
+ if pauseFocus then
+ askForWindow(focusMessage);
+ end
end
-------------------------------------------------------------------------------
@@ -341,15 +360,21 @@ function unpinManager(title, message)
lsSleep(tick_delay);
lsDoFrame();
end
+ local pauseFocus = false;
local done = false;
while not done do
lsPrint(10, 10, 0, 1.0, 1.0, 0xFFFFFFff, title);
lsPrintWrapped(10, 60, 0, lsScreenX - 20, 0.7, 0.7,
0xd0d0d0ff, message);
- if lsButtonText(30, 160, 0, 250, 0x80D080ff,
- "Un-Pin Windows") then
+ if lsButtonText(30, 160, 0, 250, 0x80D080ff, "Un-Pin Windows") then
+ lsDoFrame();
+ if pauseFocus then
+ askForWindow(focusMessage);
+ end
closeAllWindows();
end
+ pauseFocus = lsCheckBox(30, 200, 10, 0xffffffff, "Pause for Focus",
+ pauseFocus);
if lsButtonText(lsScreenX - 110, lsScreenY - 30, 0, 100,
0xffffffff, "Done") then
done = true;
View
73 VeggieTales/luaScripts/thistle.lua
@@ -10,51 +10,53 @@
--
instructions = {
-0,0,0,1,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,1,1,33,
+0,0,0,0,99,
+0,0,0,0,99,
+0,0,0,0,99,
+0,0,3,0,99,
+0,0,1,0,99,
+0,0,0,0,99,
+0,0,0,1,99,
+0,0,1,0,99,
+0,0,1,0,99,
+0,0,0,1,99,
0,0,1,1,33,
-0,0,0,2,33,
-0,0,1,1,99,
-0,0,2,0,99,
-0,0,1,1,99,
+0,0,0,1,33,
+0,0,0,3,99,
0,0,1,0,99,
+0,0,0,1,99,
0,0,0,0,99,
+0,0,0,0,99,
+0,0,1,1,99,
0,0,1,0,99,
+0,0,0,1,99,
0,0,0,0,99,
0,0,1,0,99,
+0,0,0,0,99,
+0,0,0,0,33,
+0,0,0,0,99,
0,0,1,0,99,
-0,0,0,4,99,
+0,0,0,0,99,
0,0,1,0,33,
0,0,0,0,33,
-0,0,0,0,33,
0,0,1,1,99,
-0,0,0,0,33,
-0,0,0,1,33,
-0,0,0,0,99,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,0,33,
-0,0,0,1,99,
0,0,0,2,99,
-0,0,4,0,99,
+0,0,2,1,33,
0,0,1,1,33,
+0,0,1,0,33,
0,0,0,1,33,
-0,0,1,0,99,
-0,0,0,1,33,
+0,0,0,0,33,
0,0,0,0,99,
0,0,0,0,99,
-0,0,0,2,33,
+0,0,1,2,99,
+0,0,2,0,33,
0,0,0,0,33,
};
assert(loadfile("luaScripts/common.inc"))();
+tick_delay = 1;
+
askText = singleLine([[
Thistle v1.1 (Revised by Tallow) --
Automatically run thistle gardens according to a recipe. Making
@@ -104,7 +106,6 @@ function doit()
promptThistles();
--promptRecipe();
windowManager("Thistle Window Setup", wmText, overlap);
- askForWindow(focusMessage);
unpinOnExit(runThistles);
end
@@ -197,11 +198,11 @@ function runThistles()
waitForMonChange(message .. " done.");
end
-- Wait a moment after image changes before doing the next tick
- sleepWithStatus(1500,
+ sleepWithStatus(500,
"Waiting a moment for other beds to catch up...");
end
- lsSleep(3000);
+ sleepWithStatus(6000, "Waiting for all beds to finish");
forAllWindows(clickHarvest, {"Harvest.png"}, "Harvesting");
lsSleep(500);
@@ -276,7 +277,7 @@ function waitForMonChange(message)
done = true;
end
- local force, skip = monInterface(message);
+ local skip, force = monInterface(message);
done = done or force;
skip_next = skip_next or skip;
@@ -363,11 +364,10 @@ function forAllWindows(f, image_names, message)
for i=#window_locs, 1, -1 do
if not first and overlap then
-- focus
- local spot = getWaitSpot(window_locs[i+1][0] + 24,
- window_locs[i+1][1] + 24);
+ local spot = getWaitSpot(window_locs[i + 1][0] - 9,
+ window_locs[i + 1][1] - 8);
safeClick(window_locs[i][0], window_locs[i][1]);
- waitForChange(spot, 500);
- lsSleep(50);
+ waitForChange(spot);
end
f(i, image_names);
first = false;
@@ -377,10 +377,10 @@ function forAllWindows(f, image_names, message)
if overlap then
statusScreen(message .. " Refocusing...");
for i=2, #window_locs do
- local spot = getWaitSpot(window_locs[i][0] + 24,
- window_locs[i][1] + 24);
+ local spot = getWaitSpot(window_locs[i][0] - 9,
+ window_locs[i][1] - 8);
safeClick(window_locs[i][0], window_locs[i][1] + 310);
- waitForChange(spot, 500);
+ waitForChange(spot);
end
lsSleep(100);
end
@@ -423,6 +423,7 @@ end
function clickHarvest(anchorIndex, image_names)
local anchor = window_locs[anchorIndex];
for i=1, #image_names do
+ lsSleep(200);
srReadScreen();
local pos = srFindImageInRange(image_names[i],
anchor[0], anchor[1],

0 comments on commit 257be62

Please sign in to comment.
Something went wrong with that request. Please try again.