PDA

View Full Version : General treefinder...



Negaal
01-19-2008, 09:06 PM
Here it is, big thanks to Nielsie for explaining wizzyplugin functions and Markus for testing:)

function FindTree(var x, y: Integer; Treetype : String): Boolean;
var
TPA : TpointArray;
ATPA : Array of TPointArray;
MSCPoint : Tpoint;
i, CTS, Color : integer;
Uptext : String;
begin
x := mscx;
y := mscy;
MSCPoint.x := mscy;
MSCPoint.y := mscy;
Color := 2966339;
Case lowercase(TreeType) of
'tree', 'usual' : Uptext := 'Tree';
'oak' : UpText := 'Oak';
'willow' : UpText := 'illo';
'yew' : UpText := 'Yew';
end;
CTS := GetColorToleRanceSpeed;
ColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.2, 0.2);
FindColorsSpiralTolerance(x, y, TPA, Color, msx1, msy1, msx2, msy2, 10)
ColorToleranceSpeed(CTS);
if Length(TPA) > 4 then
begin
ATPA := SplitTPA(TPA, 15);
SortATPAFrom(ATPA, MSCPoint);
for i := 0 to High(ATPA) do
begin
MiddleTPAEx(ATPA[i], x, y);
MMouse(x, y, 3, 3);
Wait(100 + random(100));
if IsUpText(UpText) then
begin
Result := True;
Getmousepos(x, y);
Exit;
end;
end;
end;
end;


Edit:
This one should be lot better xD :D

function FindTree(var a, b: Integer; Treetype : String): Boolean;
var
TPA : TpointArray;
ATPA : Array of TPointArray;
i, C, CTS, Color : integer;
Uptext : String;
Colors : Array of integer;
SortedTPA : TPointArray;
x, y, z, h, s, l,
maxx, minx, maxy, miny, maxz, minz,
maxh, minh, maxs, mins, maxl, minl : extended;
begin
Color := 2446923;
Minx := 1;
Maxx := 22;
Miny := 0;
Maxy := 11;
Minz := 1;
Maxz := 28;
Minh := 17;
Maxh := 22;
Mins := 23;
Maxs := 62;
Minl := 7;
Maxl := 45;
Case lowercase(TreeType) of
'tree' : Uptext := 'ree';
'oak' : UpText := 'ak';
'willow' : UpText := 'llo';
'yew' : UpText := 'ew';
'all' : UpText := 'hop d';
end;
CTS := GetColorToleRanceSpeed;
ColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.15, 0.5);
FindColorsSpiralTolerance(mscx, mscy, TPA, Color, msx1, msy1, msx2, msy2, 9)
ColorToleranceSpeed(CTS);
SetColorspeed2Modifiers(0.2, 0.2);
Colors := GetColors(TPA);
for C := 0 to High(TPA) do
begin
colortoxyz(colors[i], x, y, z);
if (x > minx) then
if (x < maxx) then
if (y > miny) then
if (y < maxy) then
if (z > minz) then
if (z < maxz) then
begin
colortohsl(colors[i], h, s, l);
if (h > minh) then
if (h < maxh) then
if (s > mins) then
if (s < maxs) then
if (l > minl) then
if (l < maxl) then
begin
Setlength(SortedTPA, length(SortedTPA) + 1);
SortedTPA[high(SortedTPA)] := TPA[C];
end;
end;
end;
if Length(SortedTPA) >= 50 then
begin
ATPA := SplitTPAEx(SortedTPA, 20, 15);
SortATPAFrom(ATPA, IntToPoint(MSCX, MSCY));
for i := 0 to High(ATPA) do
if Length(ATPA[i]) >= 50 then
begin
MiddleTPAEx(ATPA[i], a, b);
MMouse(a-10, b-10, 21, 21);
Wait(50 + random(50));
if IsUpText(UpText) then
begin
Result := True;
Getmousepos(a, b);
Exit;
end;
end;
end;
end;

Edit: I made min, max, etc to vars because I had lot of messing with them...Hard to understand...remove em and put in HSL and XYZ if you want...


Did like Wizzup? said and now takes only ~250 ms and hard to notice tiny lag

function FindTree(var a, b: Integer; Treetype : String): Boolean;
var
TPA : TpointArray;
ATPA : Array of TPointArray;
i, C, CTS, Count : integer;
Uptext : String;
Colors : Array of integer;
SortedTPA : TPointArray;
x, y, z, h, s, l : extended;
begin
Case lowercase(TreeType) of
'tree' : Uptext := 'ree';
'oak' : UpText := 'ak';
'willow' : UpText := 'llo';
'yew' : UpText := 'ew';
'all' : UpText := 'hop d';
end;
CTS := GetColorToleRanceSpeed;
ColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.15, 0.5);
FindColorsSpiralTolerance(mscx, mscy, TPA, 2446923, msx1, msy1, msx2, msy2, 9);
ColorToleranceSpeed(CTS);
SetColorspeed2Modifiers(0.2, 0.2);
TPA:=ReArrangeandShortenArrayEx(TPA, 5, 5);
Colors := GetColors(TPA);
SetLength(SortedTPA, Length(TPA));
Count := 0;
for C := 0 to High(TPA) do
begin
colortoxyz(colors[i], x, y, z);
if(x > 1)and(x < 22)and(y > 0)and(y < 11)and(z > 1)and(z < 28)then
begin
colortohsl(colors[i], h, s, l);
if (h > 17)and(h < 22)and(s > 23)and(s < 62)and(l > 7)and(l < 45) then
begin
SortedTPA[Count] := TPA[C];
Count := Count + 1;
end;
end;
end;
SetLength(SortedTPA, Count);
SortedTPA := TPA;
begin
ATPA := SplitTPAEx(SortedTPA, 20, 15);
SortATPAFrom(ATPA, IntToPoint(MSCX, MSCY));
for i := 0 to High(ATPA) do
if Length(ATPA[i]) >= 50 then
begin
MiddleTPAEx(ATPA[i], a, b);
MMouse(a-10, b-10, 21, 21);
Wait(50 + random(50));
if IsUpText(UpText) then
begin
Result := True;
Getmousepos(a, b);
Exit;
end;
end;
end;
end;


Usage example
Findtree(x, y, 'yew');


You can choose between tree, oak, willow and yew and all.

n3ss3s
01-19-2008, 09:25 PM
Want to hear the sad news?


SetColorspeed2Modifiers(0.2, 0.2);

Those, my friend, are the default Hue and Saturation modifiers :p

Also, try to avoid using capitalized letters with the UpChars font.

If you wanna save a var and two lines -

SortATPAFrom(ATPA, IntToPoint(MSCX, MSCY));


Also, if you use 15 in SplitTPA, use TPAToATPA instead, 20 should do there, then, in the for loop do like

"If GetArrayLength(TPAA[i]) >= 60 Then"

and then the MiddleTPA crap.

Gj though.

Negaal
01-19-2008, 09:37 PM
Want to hear the sad news?



Those, my friend, are the default Hue and Saturation modifiers :p

Also, try to avoid using capitalized letters with the UpChars font.

If you wanna save a var and two lines -

SortATPAFrom(ATPA, IntToPoint(MSCX, MSCY));


Also, if you use 15 in SplitTPA, use TPAToATPA instead, 20 should do there, then, in the for loop do like

"If GetArrayLength(TPAA[i]) >= 60 Then"

and then the MiddleTPA crap.

Gj though.

I just wanted to be 1337 with using ColorTolerancespeedmodifiers...ok...didn't made out well

Thanks for IntToPoint, didn't knew that...

But why should I use TPATpATPA?

Edit: Nvm, now I got it why TPAToATPA

R0b0t1
01-20-2008, 04:57 AM
Aww... Its noob friendly! Why doesn't it have confusing params like RadialWalk?

nielsie95
01-20-2008, 08:42 AM
Also, if you use 15 in SplitTPA, use TPAToATPA instead, 20 should do there, then, in the for loop do like

Why? I think SplitTPA is better in this case, the distance is a bit big though. 5 would've done it.
Also the distance version of TPAtoATPA and SplitTPA are a lot slower than the Ex versions. So when you use a large tpa (the green color of the trees), it's better to use SplitTPAEx ;)

Negaal
01-20-2008, 08:55 AM
Why? I think SplitTPA is better in this case, the distance is a bit big though. 5 would've done it.
Also the distance version of TPAtoATPA and SplitTPA are a lot slower than the Ex versions. So when you use a large tpa (the green color of the trees), it's better to use SplitTPAEx ;)

Thanks teacher:D, I'll try adding some HSL or XYZ thingy today...now I know why you said I should use SplitTpaEx not SplitTpa

Edit: How could I draw it on canvas and see matching pixels( in red), So I can see how many point it found, and where...

n3ss3s
01-20-2008, 09:37 AM
Search for DebugTPA ;)


Hmm, maybe use Hue and Sat mods 0.4, and 15 Tol in the ColorsSpiralTolerance so when you do either SplitTPA(ex) or TPAToATPA(ex) you have more points in your "subTPAs".

Wizzup?
01-20-2008, 10:04 AM
Search for DebugTPA ;)


Hmm, maybe use Hue and Sat mods 0.4, and 15 Tol in the ColorsSpiralTolerance so when you do either SplitTPA(ex) or TPAToATPA(ex) you have more points in your "subTPAs".

http://www.villavu.com/forum/showthread.php?t=18636?t=20369&highlight=debugtpa

Sir R. M8gic1an
01-20-2008, 12:08 PM
wow that confused the hell outta me, especially the posts, will this really find any tree? and are there any tuts on this?

* goes search*

~RM

Negaal
01-20-2008, 02:38 PM
wow that confused the hell outta me,
~RM

I got like 3x more confusing version ready...I just have to pick colors...

n3ss3s
01-20-2008, 03:04 PM
wow that confused the hell outta me, especially the posts, will this really find any tree? and are there any tuts on this?

* goes search*

~RM

Go to tuts for advanced, my TPA tut is stickied... Should get you understand the basics.

osmm
01-20-2008, 05:41 PM
When I used it inplace of FindObjCustom in my script it was terrible, then I changed your default color to 3830377 (my reg color), and it worked prety well. I think I gotta read up on Tpoint junk before I can really mess with this. But I'll probably use it in my next version.

n3ss3s
01-20-2008, 05:59 PM
--- more shit coming ---

Negaal, instead of exit, use break, so in the end of the procedure you can do:

ColorToleranceSpeed(CTS) to return it to the original
SetColorSpeed2Modifiers(0.2, 0.2) to return them to the default.

Negaal
01-20-2008, 09:42 PM
--- more shit coming ---

Negaal, instead of exit, use break, so in the end of the procedure you can do:

ColorToleranceSpeed(CTS) to return it to the original
SetColorSpeed2Modifiers(0.2, 0.2) to return them to the default.

Why break? No difference...

And if it doesnt find enough pixels it will never reach there...and CTS and CTS2Speedmodifiers will stay what I setted them to...

Let them be ;)

And leave me alone:D

Edit: Look my first post, I updated it a bit

Edit: I wouldn't done it without nielsie:) He taught me everything about HSL's and XYZ'

osmm
01-20-2008, 10:23 PM
It is a lot nicer but now it comes with a half second lag =/.

Negaal
01-21-2008, 06:06 AM
It is a lot nicer but now it comes with a half second lag =/.

:S , yeah, it's because it looks every color trough HSL and XYZ,

And it's not half sec, like 0,1 sec :D

Killerdou
01-21-2008, 06:16 AM
try rearrangeandshortenarray, should speed up things a lot especially since trees are rather big.

ZephyrsFury
01-21-2008, 06:25 AM
Why break? No difference...

And if it doesnt find enough pixels it will never reach there...and CTS and CTS2Speedmodifiers will stay what I setted them to...



You should change them back to the default.

Negaal
01-21-2008, 09:28 AM
You should change them back to the default.

?


ColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.15, 0.5);
FindColorsSpiralTolerance(mscx, mscy, TPA, Color, msx1, msy1, msx2, msy2, 9)
ColorToleranceSpeed(CTS);
SetColorspeed2Modifiers(0.2, 0.2);


It does...


try rearrangeandshortenarray, should speed up things a lot especially since trees are rather big.

Thx for the tip...:)

Peace?

Wizzup?
01-21-2008, 09:57 AM
:S , yeah, it's because it looks every color trough HSL and XYZ,

And it's not half sec, like 0,1 sec :D

A word of advice to speed it up. :)

Don't use variables for minx, maxx etc. Either just use numbers, or constants (you can make constants that stay within a funtion). Using ordinairy numbers or constants is much faster, if you use a variable, scar has to check the memory each time you want to access the variable's value. A constant is replaced with it's intended number when the script is being compiled. Speedness++.

If you intent to use different color spaces, always start off with rgb.
I think XYZ is faster than HSL, so after RGB use the XYZ check.
Like this:

rgbtocolor()
if r check and b check and g check then
begin
xyztocolor()
if x check and y check and g check then
begin
hsltocolor()
if h check if s check if l check
end;
end;

Also, maybe you do not have to use all the value's? (just use X and Y instead of X Y and Z, just find out whatever ones really determine the color)


About the arrays.
Set it to the max value it can take, before you enter the loop.

SetLength(SortedPoints, Length(TPA));
Right?

Then, add another variable (not C). Set it to zero before you enter the loop. I'll call it 'counter'.

SetLength(SortedPoints, Length(TPA));
counter := 0;

Now, you go through the loop.

Instead of this:

Setlength(SortedTPA, length(SortedTPA) + 1);
SortedTPA[high(SortedTPA)] := TPA[C];

You simply add to the array with index counter, and then do Counter++.
So:
SortedTPA[counter] := TPA[C];
Counter := Counter + 1;

After the loop, you Set the array (SortedTPA)'s length to Counter.


SetLength(SortedTPA, C);


That should definately help.
Calling SetLength every time required the entire array to be copied to a different point in the memory, and have 1 value added. (Am I right Ben/Ray/Nielsie/Freddy?)
If you intent to add alot of points to an array, just set it to it's maximum length first. When you finish it, set it to it's 'counter' length.

Also, I don't think you need this check:
if Length(SortedTPA) >= 50 then


Because you also check the ATPA length, and Doing TPAToATPA with a tpointarray which length < 50 is less than 16ms. :p

Great, now I just have to add all these speedups to my own VEM procedures.
:garfield:

Negaal
01-21-2008, 10:32 AM
Thx for tips, I can fix it after few hours:p, atm I'm in computer lesson...

Woah, I had to read it like 3 or 4 times before I understood it...

Edit: Did like wizzy said and Killerdou's ReArrangeandshortenArray helped a lot!:)

Now it's pretty fast:D

Try it:p

Edit: It's on front page

Edit:
http://img161.imageshack.us/img161/5676/pointsve3.jpg (http://imageshack.us)
:D

Negaal
01-22-2008, 07:38 PM
Sorry for doublepost but I wanted to get it bumped, and some criticism...

Anyone? What you think about "Wizzup's" fixed version?

PriSoner
02-14-2008, 02:23 PM
Looks very good.. I'm going to try in in my script.. will let you know how it goes..

Edit
Would be usefull to have a level check in there so that all only detects trees that a user is capable of chopping.
Obviously needs to support all tree's.. :)
All in all appears to be very good.. It's also boosted my understanding of the whole TPAToATPAEx thing.. :D
I'll have a play and get back to you..