SRL Archive - Test corner

FindFishingAreas


Special Ed

2009-05-01 02:22:27

Yes, I've made one before but I always get fascinated with these. Don't judge me. :p

I'm looking for any suggestions or critiques you can offer. The point of this is to find the "Exact" area of fishing spots, useful for checking if your character is fishing, where you're character is on the mainscreen (because they'd be next to the fishing spot you're fishing at right?) ect.

Thanks if you actually took a look at it.

NOTE: Just run the script if you want to see the fishing areas in relation to the RS client, or else remove the code in the mainloop after the comment if you want to see how the code works through it.


program New;
{.include SRL/SRL.scar}

function TBoxToTPA(Box: TBox): TPointArray;
begin
SetArrayLength(result, 4);
result[0]:= IntToPoint(Box.x1, Box.y1);
result[1]:= IntToPoint(Box.x2, Box.y1);
result[2]:= IntToPoint(Box.x1, Box.y2);
result[3]:= IntToPoint(Box.x2, Box.y2);
end;

function PointInBoxArray(Point: TPoint; BoxArray: TBoxArray): Boolean;
var
ArrMax, i: Integer;
begin
ArrMax:= High(BoxArray);
for i:= 0 to ArrMax do
if(PointInBox(Point, BoxArray[i]))then
begin
result:= True;
Exit;
end else result:= False;
end;

procedure DrawBox(BMP, Color: Integer; Box: TBox);
var
i: Byte;
WH: TPoint;
begin
WH.x:= Box.x2 - Box.x1;
WH.y:= Box.y2 - Box.y1;
for i:= 0 to WH.x do
begin
FastSetPixel(BMP, Box.x1 + i, Box.y1, Color);
FastSetPixel(BMP, Box.x1 + i, Box.y2, Color);
end;
for i:= 0 to WH.y do
begin
FastSetPixel(BMP, Box.x1, Box.y1 + i, Color);
FastSetPixel(BMP, Box.x2, Box.y1 + i, Color);
end;
end;

function FindFishingAreas(Passes: Byte; PassDelay: Integer): TBoxArray;
var
fTPA, fTPA_Extra, BoxArrTotal, tempTPA, BoxArr: TPointArray;
BoxArr_TPA: Array [0..3] of TPointArray;
fTPA_ArrMax, BMP, i: Integer;
BMP_Box: TBox;
BMP_Size: TPoint;
tempArr: TBoxArray;
a, f, c, GapSize: Byte;
begin
GapSize:= 10;
ColorToleranceSpeed(2);
SetColorSpeed2Modifiers(0.10, 0.24);
FindColorsTolerance(fTPA, 11972264, MSx1, MSy1, MSx2, MSy2, 13);

while(i < Passes)do
begin
Wait(PassDelay);
FindColorsTolerance(fTPA_Extra, 11972264, MSx1, MSy1, MSx2, MSy2, 13);
fTPA:= CombineTPA(fTPA, fTPA_Extra);
Inc(i);
end;

{
for i:= 1 to Passes do
begin
Wait(PassDelay);
FindColorsTolerance(fTPA_Extra, 11972264, MSx1, MSy1, MSx2, MSy2, 13);
fTPA:= CombineTPA(fTPA, fTPA_Extra);
end;
}

fTPA_ArrMax:= High(fTPA);
Writeln('fTPA_ArrMax: ' + inttostr(fTPA_ArrMax));
BMP_Box:= GetTPABounds(fTPA);
Writeln('BMP_Box: (' + inttostr(BMP_Box.x1) + ', ' + inttostr(BMP_Box.y1) + ', ' + inttostr(BMP_Box.x2) + ', ' + inttostr(BMP_Box.y2) + ')');
BMP_Size.x:= BMP_Box.x2 - BMP_Box.x1;
Writeln('BMP_Size.x: ' + inttostr(BMP_Size.x));
BMP_Size.y:= BMP_Box.y2 - BMP_Box.y1;
Writeln('BMP_Size.y: ' + inttostr(BMP_Size.y));
BMP:= BitmapFromString(BMP_Size.x, BMP_Size.y, '');
for i:= 0 to fTPA_ArrMax do
fTPA[i]:= IntToPoint(fTPA[i].x - BMP_Box.x1, fTPA[i].y - BMP_Box.y1);

for i:= 0 to fTPA_ArrMax do
begin
Writeln('Pixel[' + inttostr(i) + ']: (' + inttostr(fTPA[i].x) + ', ' + inttostr(fTPA[i].y) + ')');
FastSetPixel(BMP, fTPA[i].x, fTPA[i].y, 255);
end;
DisplayDebugImgWindow(BMP_Size.x, BMP_Size.y);
CopyCanvas(GetBitmapCanvas(BMP), GetDebugCanvas, 0, 0, BMP_Size.x, BMP_Size.y, 0, 0, BMP_Size.x, BMP_Size.y);

RAaSTPA(fTPA, GapSize);
fTPA_ArrMax:= High(fTPA);
Writeln('fTPA_ArrMax (Check 2): ' + inttostr(fTPA_ArrMax));
SetTargetDc(GetBitmapDC(BMP));
Writeln('');
Writeln('** Starting Sort Loop **');
Writeln('');
for i:= 0 to fTPA_ArrMax do
begin
if not(PointInBoxArray(fTPA[i], tempArr))then
begin
Writeln('Point[' + inttostr(i) + '] is Valid: (' + inttostr(fTPA[i].x) + ', ' + inttostr(fTPA[i].y) + ')');
SetArrayLength(tempArr, f + 1);
Writeln('tempArr Length: ' + inttostr(GetArrayLength(tempArr)));
tempArr[f]:= IntToBox(fTPA[i].x - GapSize, fTPA[i].y - GapSize, fTPA[i].x + GapSize, fTPA[i].y + GapSize);
Writeln('tempArr[' + inttostr(f) + ']: (' + inttostr(tempArr[f].x1) + ', ' + inttostr(tempArr[f].y1) + ', ' + inttostr(tempArr[f].x2) + ', ' + inttostr(tempArr[f].y2) + ')');
repeat
FindColorsTolerance(tempTPA, 255, tempArr[f].x1, tempArr[f].y1, tempArr[f].x2, tempArr[f].y2, 0);
BoxArr:= TBoxtoTPA(GetTPABounds(tempTPA));
for a:= 0 to 3 do
begin
case a of
0 : FindColorsTolerance(BoxArr_TPA[a], 255, BoxArr[a].x - GapSize, BoxArr[a].y - GapSize, BoxArr[a].x + GapSize, BoxArr[a].y , 0);
1 : FindColorsTolerance(BoxArr_TPA[a], 255, BoxArr[a].x , BoxArr[a].y - GapSize, BoxArr[a].x + GapSize, BoxArr[a].y + GapSize, 0);
2 : FindColorsTolerance(BoxArr_TPA[a], 255, BoxArr[a].x - GapSize, BoxArr[a].y - GapSize, BoxArr[a].x , BoxArr[a].y + GapSize, 0);
3 : FindColorsTolerance(BoxArr_TPA[a], 255, BoxArr[a].x - GapSize, BoxArr[a].y , BoxArr[a].x + GapSize, BoxArr[a].y + GapSize, 0);
end;
BoxArrTotal:= CombineTPA(BoxArrTotal, BoxArr_TPA[a]);
Writeln('BoxArrTotal Length: ' + inttostr(GetArrayLength(BoxArrTotal)));
end;
tempArr[f]:= GetTPABounds(BoxArrTotal);

DrawBox(BMP, 16777215, tempArr[f]);
DisplayDebugImgWindow(BMP_Size.x, BMP_Size.y);
CopyCanvas(GetBitmapCanvas(BMP), GetDebugCanvas, 0, 0, BMP_Size.x, BMP_Size.y, 0, 0, BMP_Size.x, BMP_Size.y);

Inc(c);
Writeln('c: ' + inttostr(c));
until(c = 2);
SetArrayLength(BoxArrTotal, 0);
c:= 0;
Inc(f);
Writeln('f: ' + inttostr(f));
end;
end;
ResetDC;
SetArrayLength(result, f);

for i:= 0 to High(result) do
DrawBox(BMP, 8053399, tempArr[i]);

DisplayDebugImgWindow(BMP_Size.x, BMP_Size.y);
CopyCanvas(GetBitmapCanvas(BMP), GetDebugCanvas, 0, 0, BMP_Size.x, BMP_Size.y, 0, 0, BMP_Size.x, BMP_Size.y);

Writeln('result Length: ' + inttostr(GetArrayLength(result)));
if(GetArrayLength(Result) > 0)then
for i:= 0 to f - 1 do
result[i]:= IntToBox(tempArr[i].x1 + BMP_Box.x1, tempArr[i].y1 + BMP_Box.y1, tempArr[i].x2 + BMP_Box.x1, tempArr[i].y2 + BMP_Box.y1);
end;

var
fSpot: TBoxArray;
Client, w, h: Integer;
y: Byte;
begin
SetupSRL;
fSpot:= FindFishingAreas(4, 200);

//Comment out everything (except "end.") below here to see what the code "sees".

GetClientDimensions(w, h);
Client:= BitmapFromString(w, h, '');
CopyClientToBitmap(Client, 0, 0, w, h);
for y:= 0 to High(fSpot) do
begin
DrawBox(Client, 255, fSpot[y]);
DisplayDebugImgWindow(w, h);
CopyCanvas(GetBitmapCanvas(Client), GetDebugCanvas, 0, 0, w, h, 0, 0, w, h);
end;

end.

Thanks again. This may be used in the MSI Karaproject, we haven't decided yet. If you want to use it feel free, just give credit and let me know.

Nava2

2009-05-01 04:22:32

Why so complex? o.O

Out of curiosity.

Seems like a lot for a relatively simple thing.. Looks kinda slow too :eek:

E: I like the idea, the scanning of the spots to look for a bulk colour.. Hmm, I think I'm going to be a copy cat and use a similar strategy in my Karajama fisher.. :D

E2: I wrote one up, let me know if you want to see it…

Yours is good, man! :D.

Special Ed

2009-05-01 06:34:14

It's actually very very fast, it's in the unnoticeable range. The point behind the complexity is mainly because of two reasons.

1) Because I've been out of scripting doing things simply is harder for me (go figure), because I don't remember all the functions and procedures to use to make my code simpler.

2) Because I'm trying to find the edges of each fishing spot rather than just the center of the fishing spot there is a lot more checking that needs to be done to make sure that it gets the edges of only the one fishing spot and doesn't spill over into others.

Like looking at yours, I've been out of it so what does SplitTPA do? I get that it splits a TPA into arrays of points that have max distance 5? But distance from what exactly? I think it just come down to me not having scripted in a while.

ALSO - Why are you making your own karamja fisher, why not help with the MSI one? *Or did I miss the sarcasm I'm tired*

Nava2

2009-05-01 07:07:44

I'm writing my own just for a challange. Anyways, SplitTPA creates a 2D TPoint array based on grouping while each point must be within the inputted distance of at least one other point in the array.

In essence it groups the TPointArray. Mine could get the bounds quite easily as well, just adding a TBoxArray and using GetTPABounds would give those same boxes ;)

By the way, I applaud the scanning idea. That's just brilliant.

Oh and I don't like team projects usually because I'm too anal retentive about things being perfect that I take over. It's not enjoyable to me and I always feel badly. :(

Special Ed

2009-05-01 07:22:58

So in reality you could change your SplitTPA(5) to SplitTPA(10 or 9) really, because the closest two fishing spots ever get to each other is either 9 or 10 (I can't remember which).

So basically if you take my code and take out the really complex stuff that I thought I had a great idea for and replace it with SplitTPA you get the same thing. Awesome. :) Thanks!

See like I said It's just my unfamiliarity with function due to my absence that is giving me a hard time.

Sir R. M8gic1an

2009-05-01 10:18:29



function PointInBoxArray(Point: TPoint; BoxArray: TBoxArray): Boolean;
var
ArrMax, i: Integer;
begin
ArrMax:= High(BoxArray);
for i:= 0 to ArrMax do
if(PointInBox(Point, BoxArray[i]))then
begin
result:= True;
Exit;
end;
end;

more efficient, because you don't have to set the result as false each time. You actually don't need to set it at all because Result := false is the default value.

I didn't look through the whole thing cus i just got home from clubbing :/

~RM

Special Ed

2009-05-01 15:30:10

Ahh yes, I guess I do remember that. Thank you Rasta.

marpis

2009-05-01 19:56:01

i tried this and it works very well :) it doesnt get confused of ducks either

Special Ed

2009-05-03 06:42:36

Thank you very much marpis. =)

Home

2009-05-04 13:15:09

Well for me it worked very well and yes it ignores, all ducks and so on (: Well done mate!


~Home