Hey guys! I'm back with more on the topic of walking around RS using OGL! This time I have several new functions that can utilize different techniques such as finding models during a tilewalk! I also included a maximum wait time for the waitFlag procedure in case your player happens to click in a non-walkable area.
Here are three basic functions I use:
getDistance returns the distance as an integer of the number of pixels between two points. You can use this to make sure you are near something.
EDIT: As Obscurity pointed out there is already a distance function:
Simba Code:
somePoint.distanceFrom(anotherPoint)
But I'll leave mine here just cause!
Simba Code:
function getDistance(point1, point2 : TPoint) : Integer;
begin
exit(round(sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y))));
end;
sgn returns the sign of an integer you pass in (zero will return 0). I use this for getting the distance to travel in the tile walking functions.
Simba Code:
function sgn(x : Integer) : Integer;
begin
if x = 0 then
exit(0)
else
if x > 0 then
exit(1)
else
exit(-1);
end;
waitFlag will wait until the flag has disappeared to continue. It can take in an integer in milliseconds for how long it should wait for the flag to disappear (15000 is default). This is great for making sure errors don't occur.
Simba Code:
procedure waitFlag(maxWait : Integer = 15000);
var
flagTimer : TCountDown;
begin
flagTimer.setTime(maxWait);
wait(250);
while (not ogl.getTextures(1275).isEmpty()) do
begin
if flagTimer.isFinished() then
exit();
wait(1000);
end;
wait(randomrange(250, 500));
if not ogl.getTextures(1275).isEmpty() then
waitFlag();
end;
and walkTo simply walks to a point using the minimap. This is implemented in the following functions.
Simba Code:
procedure walkTo(point : TPoint);
begin
mouse.click(minimap.getScreenPosition(minimap.getLocalPosition().adjustPosition(point.X, point.Y)), 1);
end;
Ok now that we're through that lets get into the juicy stuff!
Our first procedure is tileWalkInterval. This one walks to a "tile" you give it as a TPoint, and takes a randomness which affects the variation each point it goes to, and the step size (which you can increase/decrease in the code if you wish).
Simba Code:
procedure tileWalkInterval(point : TPoint; r : Integer);
var
stepSize, rx, ry, toGoX, toGoY : Integer;
next : TPoint;
begin
stepSize := r + 7;
toGoX := point.X;
toGoY := point.Y;
repeat
rx := random(-r, r);
ry := random(-r, r);
next := [sgn(toGoX) * (min(abs(toGoX), stepSize)) + rx, sgn(toGoY) * (min(abs(toGoY), stepSize)) + ry];
writeln('walking x: ', next.x, ' walking y: ', next.y);
walkTo(next);
waitFlag();
toGoX := toGoX - next.X;
toGoY := toGoY - next.Y;
until (abs(toGoX) <= r) and (abs(toGoY) <= r);
end;
tileWalk is the procedure used to call the non-overloaded tileWalkInterval. It needs the array of TPoints to walk, and also the randomness you wish.
Simba Code:
procedure tileWalk(points : TPointArray; r : Integer);
var
i : Integer;
begin
for i := 0 to high(points) do
begin
writeln('Walking to: ', points[i]);
tileWalkInterval(points[i], r);
end;
end;
This tileWalkInterval is an overloaded version of the previous. This time, in addition, it takes a model ID as an integer, and the offset to click from that model as a TPoint (make sure to include randomness!).
Simba Code:
procedure tileWalkInterval(point : TPoint; r, modelID : Integer; modelOffSet : TPoint); overload;
var
stepSize, rx, ry, toGoX, toGoY : Integer;
next : TPoint;
modelTimer : TCountDown;
models : glModelArray;
begin
stepSize := r + 7;
toGoX := point.X;
toGoY := point.Y;
repeat
rx := random(-r, r);
ry := random(-r, r);
next := [sgn(toGoX) * (min(abs(toGoX), stepSize)) + rx, sgn(toGoY) * (min(abs(toGoY), stepSize)) + ry];
writeln('walking x: ', next.x, ' walking y: ', next.y);
walkTo(next);
repeat
wait(randomrange(450, 950));
models := ogl.getModels(modelID);
if (not models.getVisible().isEmpty()) then
begin
if (models[0].isVisible()) then
begin
mouse.click(models[0].toPoint().adjustPosition(modelOffset.X, modelOffset.Y));
writeLn('Clicked on model ', models[0].ID, '. Coords: ', models[0].toPoint());
waitFlag();
exit();
end;
end;
if (not ogl.getTextures(1275).isEmpty()) then
modelTimer.setTime(1000);
until modelTimer.isFinished();
waitFlag();
toGoX := toGoX - next.X;
toGoY := toGoY - next.Y;
until (abs(toGoX) <= r) and (abs(toGoY) <= r);
end;
Our next method of walking to a point is iconWalk! As the name suggests, it uses an icon texture on the minimap. This time though, it takes in a maxWait time in case your icon (such as a bank) is in an unwalkable area (behind the counter for example). The default for the wait time is 15000 ms.
Simba Code:
function iconWalk(randomization: integer; icon: glTextureArray; offSetTiles: TPoint; maxWait : Integer = 15000): boolean;
var
rx, ry :integer;
begin
rx := randomrange(-(randomization), randomization);
ry := randomrange(-(randomization), randomization);
if icon.isEmpty() then
result:=false
else
begin
mouse.click(minimap.getScreenPosition(minimap.getLocalPosition(icon.closestTo(minimap.getScreenPosition(minimap.getLocalPosition()))[0]).adjustposition(offsetTiles.x+rx, offsetTiles.y+ry)), 1);
if ((offSetTiles.y > 20) or (offSetTiles.y < -20)) or ((offSetTiles.x > 15) or (offSetTiles.x < -15)) then
wait(3500)
else
wait(1000);
waitFlag(maxWait);
exit(true);
end;
exit(false);
end;
The next function can be used in almost any situation! modelWalk will find a model ID with an offset that you pass it and walk there. If it does in fact find your model it will walk to it and return true, otherwise it returns false.
Simba Code:
function modelWalk(modelID : Integer; offSet : TPoint) : boolean;
var
models : glModelArray;
modelTimer : TCountDown;
mousePoint : TPoint;
begin
modelTimer.setTime(1000);
repeat
models := ogl.getModels(modelID);
until (not models.isEmpty) or modelTimer.isFinished();
mousePoint := models[0].toPoint().adjustPosition(offSet.X + randomrange(-10, 10), offSet.Y + randomrange(-10, 10));
mouse.move(mousePoint);
if (mouse.getTooltip() = '') then
mouse.click(mousePoint)
else
mouse.rightClickOption(mousePoint, ['alk', 'here']);
waitFlag();
end;
And that's it for today! I spend a while on these so like always I absolutely love feedback! If you see any problems or possible improvements please post them, and I will work on them. Once again thank you @Ross; for the original procedures and idea, and @Clarity; and @Lucidity; for help answering questions about OGL.