atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8516 Location: 127.0.0.1
|
Posted: Sat Jan 15, 2011 3:01 pm Post subject: Minesweeper Board Solver Lua Example |
|
|
This example shows how to call functions inside an application. For this example, this calls the left and right-click handlers that happen when Minesweeper is told the user has either left-clicked a square to "activate" it, or that they have right-clicked it to make it with a flag meaning its a bomb.
Please note because of limitations on how Lua interacts with Cheat Engine calling functions requires the use of threads. Since this script shares memory locations, there can be race conditions if a thread starts slow. This can cause the solver to mess up here and there.
This script is intended to be tested with "Beginner" mode on Minesweeper.
Code: |
--[[
Minesweeper Solver via Lua Example
by atom0s
]]--
-- ## DO NOT EDIT BELOW THIS LINE!! ##
local Minesweeper_Example = { }
----------------------------------------------------------------------------------
-- func: Minesweeper_Example.CallStepSquare( .. )
-- desc: Calls our StepSquare cave to internally call left-click function.
----------------------------------------------------------------------------------
function Minesweeper_Example.CallStepSquare( x, y )
-- Write current X and Y to memory.
autoAssemble(
"alloc(tempFunc, 1024)\n" ..
"CreateThread(tempFunc)\n" ..
"tempFunc:\n" ..
"mov [currXPos], " .. tostring( x ) .. "\n" ..
"mov [currYPos], " .. tostring( y ) .. "\n" ..
"ret\n"
);
-- Create thread calling our StepSquare function.
autoAssemble( "CreateThread(StepSquareCave)\n" );
-- Cleanup temp function.
autoAssemble( "dealloc(tempFunc)\n" );
end
----------------------------------------------------------------------------------
-- func: Minesweeper_Example.CallMakeGuess( .. )
-- desc: Calls our MakeGuess cave to internally call right-click function.
----------------------------------------------------------------------------------
function Minesweeper_Example.CallMakeGuess( x, y )
-- Write current X and Y to memory.
autoAssemble(
"alloc(tempFunc, 1024)\n" ..
"CreateThread(tempFunc)\n" ..
"tempFunc:\n" ..
"mov [currXPos], " .. tostring( x ) .. "\n" ..
"mov [currYPos], " .. tostring( y ) .. "\n" ..
"ret\n"
);
-- Create thread calling our MakeGuess function.
autoAssemble( "CreateThread(MakeGuessCave)\n" );
-- Cleanup temp function.
autoAssemble( "dealloc(tempFunc)\n" );
end
----------------------------------------------------------------------------------
-- func: Minesweeper_Example.Main( .. )
-- desc: Main script function that handles everything of the script.
----------------------------------------------------------------------------------
function Minesweeper_Example.Main ( )
-- Obtain current board sizes.
Minesweeper_Example.BoardWidth = readInteger( "10056AC" );
Minesweeper_Example.BoardHeight = readInteger( "10056A8" );
-- Board layout starting position.
Minesweeper_Example.BoardStart = 16798528;
-- Allocate memory locations for x and y positions.
autoAssemble(
"globalalloc(currXPos, 4)\n" ..
"globalalloc(currYPos, 4)\n"
);
-- Allocate cave for StepSquare function.
autoAssemble(
"globalalloc(StepSquareCave, 1024)\n" ..
"StepSquareCave:\n" ..
"push [currYPos]\n" ..
"push [currXPos]\n" ..
"mov eax, 01003512\n" ..
"call eax\n" ..
"ret\n"
);
-- Allocate cave for MakeGuess function.
autoAssemble(
"globalalloc(MakeGuessCave, 1024)\n" ..
"MakeGuessCave:\n" ..
"push [currYPos]\n" ..
"push [currXPos]\n" ..
"mov eax, 0100374F\n" ..
"call eax\n" ..
"ret\n"
);
-- Attempt to solve the board.
local boardX = 1;
local boardY = 1;
for boardX = 1, Minesweeper_Example.BoardWidth do
for boardY = 1, Minesweeper_Example.BoardHeight do
local bCurrentPos = Minesweeper_Example.BoardStart + ( boardX + ( boardY * 32 ) );
local bValue = readBytes( bCurrentPos, 1 );
if( bValue ~= 0x8F ) and ( bValue == 0x0F ) then
Minesweeper_Example.CallStepSquare( boardX, boardY );
else
Minesweeper_Example.CallMakeGuess( boardX, boardY );
end
end
end
-- Cleanup allocations.
autoAssemble(
"dealloc(currXPos)\n" ..
"dealloc(currYPos)\n" ..
"dealloc(MakeGuessCave)\n" ..
"dealloc(StepSquareCave)\n"
);
return true;
end
-- Execute our script.
Minesweeper_Example.Main();
|
_________________
- Retired. |
|