SRL Archive - Research & Development Lounge

CorrectCompass(MinLine:Integer; Align:String):Boolean;


caused

2009-08-25 13:20:17

Heya =).

Sometimes you need to have the Compass aligned better than MakeCompass Does. Especially for map walking it may be crucial.

So this function aligns the compass with makecompass and than corrects it at any degree you want.

Usage:

Example:
CorrectCompass(15,'N');
Makes the Compass 'N' and corrects it till it finds an 15 px long outline. The higher the number, the more accurate it will be, but the longer it will take.

Help me improving this :'D.. I know it can be shortenend ALOT. And also improved alot. It may even be done before :3.. But i'd like to finish my function anyways =).


//Usage: First Argument is the accuracy, the higher the more accurate, but //the longer it will correct the compass, Second argument is the point of the //compass (n,s,w,e)...

function CorrectCompass(MinLine:Integer; Align:String):Boolean;
var
Cols : TpointArray;
TempColor, CLine,x,y,i,j,k,tempx, tempy,Timer,last : Integer;
wline : TautocolorInfo;
Nonono : Boolean;

FirstRun : Boolean;
TempCline : Array of Integer;

movedright, movedleft,lastmove : Boolean;
begin
FirstRun := TRUE;
MakeCompass(Align);
MarkTime(Timer);
ColorToleranceSPeed(2);
//AutoColor for them white lines
with wline do
begin
MinCount := 30;
Color := 15987692;
HueMod := 35.00;
SatMod := 21.69;
LumTol := 3;
MinR := 225; MaxR := 254;
MinG := 225; MaxG := 254;
MinB := 225; MaxB := 254;
MinX := 77.57; MaxX := 85.77;
MinY := 79.55; MaxY := 90.31;
MinZ := 86.42; MaxZ := 100.90;
end;
repeat
ColorToleranceSpeed(2);
// WriteLn('START');
TempColor := FindColorRecordEx(x, y, wline, MMX1, MMY1, MMX2, MMY2, TRUE);
// WriteLn('Found Line Color: '+IntTostr(Tempcolor));
ColorToleranceSpeed(0);
FindColorsTolerance(Cols, TempColor,MMX1, MMY1, MMX2, MMY2,11);

If TempColor = 0 then
begin
WriteLn('Couldnt Find White Outline');
exit;
end;

Case Align Of
'n':
begin
If (rs_GetCompassAngleDegrees > 20) or (rs_GetCompassAngleDegrees < 340) then
begin
MakeCompass(Align);
end;
end;
's':
begin
If (rs_GetCompassAngleDegrees > 200) or (rs_GetCompassAngleDegrees < 160) then
begin
MakeCompass(Align);
end;
end;
'w':
begin
If (rs_GetCompassAngleDegrees > 290) or (rs_GetCompassAngleDegrees < 240) then
begin
MakeCompass(Align);
end;
end;
'o':
begin
If (rs_GetCompassAngleDegrees > 110) or (rs_GetCompassAngleDegrees < 70) then
begin
MakeCompass(Align);
end;
end;
end;

repeat

//First Run

SetArrayLength(TempCline, high(Cols));

If SimilarColors(GetColor(Cols[i].x,Cols[i].y),TempColor,11) then
begin
// WriteLn('Checking Next Line');
repeat
inc(j);
If SimilarColors(GetColor(Cols[i].x+j,Cols[i].y),TempColor,11) then
begin
Inc(Cline);
end else
begin
Nonono := TRUE;
end;
until(CLine = MinLine) OR Nonono;

Nonono := FALSE;
j := 0;

TempCline[i] := Cline;

If Cline < MinLine then
Cline := 0
else
MoveMouse(Cols[i].x,Cols[i].y);

end;
inc(i);
Until(CLine >=MinLine) Or (i >= high(cols)-1);

If Cline >= MinLine then
begin
WriteLn('Correcting Took Seconds:'+ IntToStr(TimeFromMark(Timer)/1000));
WriteLn('STRAIGHT');
Result := TRUE;
exit;
end;

If TimeFromMark(Timer) >= 60000 then
begin
WriteLn('Correcting Failed after 60secs');
Result := FALSE;
MakeCompass(Align);
exit;
end;

//Find biggest temp number
try
BubbleSortB(TempCline);
except
end;

//Check if the movement was good
If (CLine < MinLine) AND not FirstRun then
begin
WriteLn('TempCline[i]: '+inttostr(TempCline[i]));
WriteLn('TempCline[0]: '+inttostr(TempCline[0]));
WriteLn('last: '+inttostr(last));
If last < TempCline[0] then
lastmove := false
else
lastmove := true;
end;




If (CLine < MinLine) AND not FirstRun then
begin

wait(100);
//MakeCompass(Align);
If lastmove then
begin
WriteLn('lastmove true');
if movedleft = true then
begin
movedleft:=false;
movedright := TRUE;
SendArrowWait(1,Random(175))
end else
begin
movedleft:= True;
movedright := false;
SendArrowWait(3,Random(175));
end
end else
begin
WriteLn('last move false');
if movedleft = false then
begin
movedleft:=true;
movedright := false;
SendArrowWait(3,Random(175))
end else
begin
movedleft:= false;
movedright := true;
SendArrowWait(1,Random(175));
end;
end;
end;




WriteLn('Compass angle');
WriteLn(rs_GetCompassAngleDegrees);




i:=0; //Reset i


If (CLine < MinLine) AND FirstRun then
begin
MakeCompass(Align);
Case Random(2) of
0 :
begin
movedleft:=false;
SendArrowWait(1,Random(200));
movedright := TRUE;
end;
1 :
begin
movedright:=false;
SendArrowWait(3,Random(200));
movedleft := TRUE;
end;
end;
end;

try last := TempCline[0]; except end;
WriteLn('LAST');
WRiteLn(last);
FirstRun := FALSE;
wait(200);
Until(CLine >=MinLine);

If CLine >=MinLine then
Result := TRUE
else
Result := FALSE;

end;

Naum

2009-08-25 14:47:29

Nice work :). However, you should have used GetWallAngle in SRL, It's used in Maze.Scar as well to perfectly align the compass.

caused

2009-08-25 22:49:13

:) Thanks

GetWallAngle ?. I'll take a look at that =).

And I'll add a perfected TAutoColorInfo for the outlines later!

It's working great for me already, gotta refine it anywas =).

~caused

//EDIT: I improved the performance ALOT, it only takes about 1 second to perfectly align the compass now.

I would like to know if this had a chance to get into SRL(?), cause i'm already using it all the time :'D…

Sabzi

2009-08-30 19:07:29

I don't know if you have already thought about it or not but I say it anyway.
Getting the offset of the minimap(is that the right word?, the degree from exact 90° for example) and then fine tuning for example the radialwalk function with that might be easier.
That #?>°$ minimap rotates always too much.
There are some other threads with written functions like I said above, that's why I think about it. But I have just heard about these threads in other threads so no way I can find them :(
Just an idea, though.

caused

2009-08-30 19:17:49

Yeah, the problem is, you cant always get the angle while walking.

It's because there are not always walls(while walking), when you just start walking, you usually start anywhere, where walls on the minimap are. And also the walking usually shouldnt be thaat long that the compass totally changes alot while walking.

I refined the function already btw. It's now even faster and more efficient.

Gotta upload it, when i find the time to fix the standards :'D…

~caused

XcanadamanX

2009-08-31 06:53:10

and this works with smart? for some reason i cant get smart to work with sendarrowwait. it does nothing for me. im just curious

marpis

2009-08-31 09:23:02

MoveMouse(Cols.x,Cols.y);
why are you moving mouse?
if you want to move mouse, use MMouse(x, y, rx, ry), movemouse and clickmouse are detectable.

BubbleSortB(TempCline);
QuickSort is in SCAR, too

Naum

2009-08-31 14:27:31

From: marpis
MoveMouse(Cols.x,Cols.y);
why are you moving mouse?
if you want to move mouse, use MMouse(x, y, rx, ry), movemouse and clickmouse are detectable.

BubbleSortB(TempCline);
QuickSort is in SCAR, too


Further to your point:

http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/sortcontest/sortcontest.htm

caused

2009-08-31 15:50:25

XcanadamanX: I didnt test it with smart :O. Im not the biggest fan of smart anyways :'D.. If it doesnt work with smart, just replace those functions with the ones from "MakeCompass" or so.

Marpis: Yeh, theres still alot of debugging in there :'D.. THe MoveMouse thing is just there for debugging purposes, feel free to comment it out :'D…

Fancy Grahpics, Nauman :'D … Well BubbleSort is the only algorithm i already programmed myself, so i though I'd use that :). What excactly does Quicksort do though :O?.

~caused

Naum

2009-08-31 16:24:41

Well BubbleB is the same as doing:

QuickSort(TempCline);
InvertTIA(TempCline);

The only difference is that the above is much faster.

caused

2009-09-01 03:34:27

Update!
It now basicly uses rs_GetCompassAngleDegrees to improve the performance. And i fixed a stupid bug.

Now maybe improving the autocolor and its done : D..

~caused

Nadeem

2009-09-03 10:47:14

Quick sort is a recursion based sorting method (http://delphi.about.com/od/objectpascalide/a/quicksort.htm), I edited the one from the link above and included scar version of quicksort without recursion. (I didn't test it, just did it up real quick ;))


procedure QuickSortSample(var Values: TIntegerArray);
var iLo, iHi, Lo, Hi, Pivot, T: Integer;
label Start;
begin
iLo = 0;
iHi = High(Values);

Start:
Lo := iLo;
Hi := iHi;
Pivot := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Pivot do Inc(Lo);
while A[Hi] > Pivot do Dec(Hi);
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Lo = Lo + 1;
Hi = Hi - 1;
end;
until Lo > Hi;

if (Hi > iLo) then
begin
iHi = Hi;
goto Start;
end;
if (Lo < iHi) then
begin
iLo = Lo;
goto Start;
end;
end;



~NS