SRL Archive - SRL Archives

RadialRoadWalk with FindMiddle


nielsie95

2007-04-28 14:40:29

Hello all,

Sometimes when I use Radial(Road)Walk it doesn't click on the road.
It clicks at the edge of the Road.. And when there's a fence at the side of the road, it will click on the fence which results in clicking near the fence and not on the road.
Well, here's my solution: RadialRoadWalkMiddle!


FindMiddle: Needed in FindMiddleOfTheRoad.

{*******************************************************************************
function FindMiddle(var x, y: integer; Color, x1, y1, x2, y2: integer): Boolean;
By: Nielsie95
Description: Finds the middle of a color in the given box.
*******************************************************************************}

function FindMiddle(var x, y: integer; Color, x1, y1, x2, y2: integer): Boolean;
var MiddlePoints: TPointArray; TotalLength: integer;
begin
FindColorsTolerance(MiddlePoints, color, x1, y1, x2, y2, 0);
TotalLength := GetArrayLength(MiddlePoints);
if TotalLength > 0 then
begin
x := round((MiddlePoints[0].x + MiddlePoints[(TotalLength -1)].x) div 2);
y := round((MiddlePoints[0].y + MiddlePoints[(TotalLength -1)].y) div 2);
Result := True;
end else Result := false;
end;




The procedure :)

{*******************************************************************************
function FindMiddleOfTheRoad(var x, y: Integer; Radial, cx, cy, TheColor, Tol: integer): Boolean;
By: Nielsie95
Description: Finds the middle the road.
*******************************************************************************}

function FindMiddleOfTheRoad(var x, y: Integer; Radial, cx, cy, TheColor, Tol: integer): Boolean;
var Temp, Temp1, Temp2, Temp3, Temp4, Temp5: integer;
x1, y1, x2, y2: integer;
Direction: String;
begin
Temp1 := 90 - Radial if Temp1 < 0 then Temp1 := Radial - 90;
Temp2 := 180 - Radial if Temp2 < 0 then Temp2 := Radial - 180;
Temp3 := 270 - Radial if Temp3 < 0 then Temp3 :=Radial - 270;
Temp4 := Radial;
Temp5 := 360 - Radial;
Temp := Min(Min(Min(Temp1, Temp2), Min(Temp3, Temp4)), Temp5);

case Temp of
Temp1 : Direction := 'East'
Temp2 : Direction := 'South'
Temp3 : Direction := 'West'
Temp4 : Direction := 'North'
Temp5 : Direction := 'North'
end;

case Direction of
'East' : begin x1 := cx -3; y1 := cy -8; x2 := cx; y2 := cy +8; end;
'West' : begin x1 := cx; y1 := cy -8; x2 := cx +3; y2 := cy +8; end;
'South' : begin x1 := cx -8; y1 := cy -3; x2 := cx +8; y2 := cy; end;
'North' : begin x1 := cx -8; y1 := cy; x2 := cx +8; y2 := cy +3; end;
end;

//WriteLn(Direction);
//WriteLn('x1 :'+IntToStr(x1)+' y1 :'+IntToStr(y1)+' x2 :'+IntToStr(x2)+' y2 :'+IntToStr(y2))
Result := FindMiddle(x, y, TheColor, x1, y1, x2, y2);
if not Result then
begin
x := (x1 + x2) div 2;
y := (y1 + y2) div 2;
Result := True;
end
end;



Adjusted RadialWalkEx, now it looks for the middle of the Road.
{*******************************************************************************
function MyRadialWalkEx(var x, y: Integer; cx, cy, TheColor, Tol, StartRadial, EndRadial, Radius: Integer): Boolean;
By: WT-Fakawi & Wizzup? --> Adjusted by Nielsie95
Description: Same as RadialWalkEx, but returns Coords of the Middle of the Road.
*******************************************************************************}

function MyRadialWalkEx(var x, y: Integer; cx, cy, TheColor, Tol, StartRadial, EndRadial, Radius: Integer): Boolean;
var
i, X1, Y1, tx, ty: Integer;
begin
Result := False;
if (StartRadial < EndRadial) then
begin
repeat
for i := StartRadial to EndRadial do
begin
x1 := Round(Radius * Sine(i)) + cx;
y1 := Round(-Radius * Cose(i)) + cy;
if FindColorTolerance(tx, ty, TheColor, X1, Y1, X1 + 1, Y1 + 1, tol) then
begin
//WriteLn('Radius: '+IntToStr(i));
//WriteLn('tx: '+IntToStr(tx)+' ty: '+IntToStr(ty))
Result := FindMiddleOfTheRoad(x, y, i, tx, ty, TheColor, Tol);
//WriteLn('x: '+IntToStr(x)+' y: '+IntToStr(y))
//WriteLn('Adjusted x: '+IntToStr(tx - x)+' + y: '+IntToStr(ty - y));
Exit;
end;
end;
Radius := Radius - 4;
until Radius <= 1;
end;
if (StartRadial > EndRadial) then
begin
repeat
for i := StartRadial downto EndRadial do
begin
x1 := Round(Radius * Sine(i)) + cx;
y1 := Round(-Radius * Cose(i)) + cy;
if (FindColorTolerance(tx, ty, TheColor, X1, Y1, X1 + 1, Y1 + 1, tol)) then
begin
Result := FindMiddleOfTheRoad(x, y, i, tx, ty, TheColor, Tol);
Exit;
end;
end;
Radius := Radius - 4;
until Radius <= 1;
end;
end;



Adjusted RadialRoadWalk, it will click on the middle of the road.
{*******************************************************************************
function RadialRoadWalkMiddle(TheColor, StartRadial, EndRadial, Radius, Xmod, Ymod, FlagDist: integer): Boolean;
By: WT-Fakawi & Wizzup? --> Adjusted by Nielsie95
Description: Same as RadialRoadWalk, but clicks on the Middle of the Road.
*******************************************************************************}

function RadialRoadWalkMiddle(TheColor, StartRadial, EndRadial, Radius, Xmod, Ymod, FlagDist: integer): Boolean;
var mx, my: integer;
begin
if (RoadColorChecker) then if (DebugRadialRoad) then WriteLn(' THROUGH RADIALROADWALK= ---> ' + IntToStr(RoadColor));
if (MyRadialWalkEx(mx, my, MMCX, MMCY, TheColor, 0, StartRadial, EndRadial, Radius)) then
begin
MouseFindNoFlag(mx, my, Xmod, Ymod);
Result := True;
FFlag(FlagDist);
end;
end;





If you want to try this at home: I attached a little script. Set StartRadial, EndRadial, Radius and watch it walk 3 times on the middle of the road. :)

Smartzkid

2007-04-28 14:54:58

Very cool.

Do you have to have all the procedures, or are they independent?

nielsie95

2007-04-28 15:02:42

You can use FindMiddle alone, but the others need eachother. :)

Lorax

2007-05-03 09:20:07

I wonder, how much have you tried this? I can think of several cases where it would fuck up.. what about crossroads?

nielsie95

2007-05-03 09:28:35

I didn't try it everywhere, but I tried it enough (I think) to post it here :).
I never tried it with crossroads, but I think it still should click on the road. Where would you like me to test it? :)

rkroxpunk

2007-05-03 10:44:33

wow that's awesome! Question does it click middle of road or middle of radial?

nielsie95

2007-05-03 10:50:07

It just works like Radial(Road)Walk, but it should click on the middle of the road ;)

Lorax

2007-05-05 09:22:50

I would like you to test it between Falador and Port Sarim..
you know the cross road there?.. try it with every 20th angle / 360..
I am pretty sure it will click somewhere else on of the times..

nielsie95

2007-05-06 09:29:34

Tried it where you said. It works for me. Though sometimes it doesn't click exactly on the middle of the road, but it never clicked next to the road. If you (or someone else) want to try it out yourself, download the attachment. :)
If you try it out and it screws up, do you want to make a screenshot of it? :D

Thanks,
Nielsie95

Lorax

2007-05-06 10:25:03

From: nielsie95
Tried it where you said. It works for me. Though sometimes it doesn't click exactly on the middle of the road, but it never clicked next to the road. If you (or someone else) want to try it out yourself, download the attachment. :)
If you try it out and it screws up, do you want to make a screenshot of it? :D

Thanks,
Nielsie95


Sure I might try it some time :)