Skip to content

Commit

Permalink
Merge pull request #31 from duerig/master
Browse files Browse the repository at this point in the history
Important fixes to thistle.lua, new cc_Auto.lua script
  • Loading branch information
Vaevictusnet committed Apr 24, 2012
2 parents cf81991 + fee7037 commit 257be62
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 61 deletions.
Binary file added 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.
17 changes: 12 additions & 5 deletions VeggieTales/luaScripts/cc_Assist.lua
Expand Up @@ -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

178 changes: 178 additions & 0 deletions 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
29 changes: 23 additions & 6 deletions VeggieTales/luaScripts/common_find.inc
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions VeggieTales/luaScripts/common_wait.inc
Expand Up @@ -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
Expand Down
49 changes: 37 additions & 12 deletions VeggieTales/luaScripts/common_window.inc
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -317,6 +333,9 @@ function windowManager(title, message, overlap)
lsDoFrame();
lsSleep(tick_delay);
end
if pauseFocus then
askForWindow(focusMessage);
end
end

-------------------------------------------------------------------------------
Expand All @@ -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;
Expand Down

0 comments on commit 257be62

Please sign in to comment.