SRL Archive - SRL Archives

Function ItemDtm(var DTM:Integer; I:Integer):Boolean;


MylesMadness

2008-10-12 20:45:22

Function ItemDtm(I:Integer):Integer;
var
MP: TDTMPointDef;
SP: array of TDTMPointDef;
DDTM: TDTM;
TP:TPoint;
Box:TBox;
P:TpointArray;
A:Integer;
begin
Box:=InvBox(I);
If not FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0) then
Result:=-1;
If Result = -1 then exit;
TP:=ItemCoords(I);
MP.x := TP.x;
MP.y := TP.y;
MP.areasize := 1;
MP.areashape := 0;
MP.color := GetColor(TP.x, TP.y);
MP.tolerance := 10;
SetArrayLength(SP, GetArrayLength(P));
A:=high(P);
For I:=0 to A do
begin
SP[i].x := P[i].x;
SP[i].y := P[i].y;
SP[i].areasize := 1;
SP[i].areashape := 0;
SP[i].color := 65536;
SP[i].tolerance := 0;
end;
DDTM.MainPoint := MP;
DDTM.SubPoints := SP;
Result:= AddDTM(DDTM);
end;

Dtms during runtime ftw

TViYH

2008-10-12 20:53:32

Wow!

Very very nice!

HyperSecret

2008-10-13 03:09:14


Box:=InvBox(I);
If FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0) then
Result:=true;

could be


if ExistsItem(i) then Result := True;

Just to shorten it up a little. But looks good.

MylesMadness

2008-10-13 11:07:53

How would it shorten it? You would still have to do the FindColorsTolerance.

HyperSecret

2008-10-13 18:56:36

why would you? checking for the black color checks to see if an item exists in the inv slot…

which is what ExistsItem does…

Method

2008-10-13 19:01:02

From: HyperSecret
why would you? checking for the black color checks to see if an item exists in the inv slot…

which is what ExistsItem does…


You can't get an array of black points to make the DTM without doing a FindColorsTolerance.

Boreas

2008-10-13 19:19:20

Dual purpose efficiency ftw!

HyperSecret

2008-10-13 20:11:45

From: Method
You can't get an array of black points to make the DTM without doing a FindColorsTolerance.


ah sorry, I totally read it wrong :P

MylesMadness

2008-10-14 15:30:43

Update
Made it compile and added a failsafe

Nava2

2008-10-14 15:39:40

You should lower the amount of points, as well as lower the top of the box in the FindColorsTolerance, that way you don't pick up text if there is any. :)

Good function btw.

Boreas

2008-10-14 15:52:22

I found starting at 15 and checking every 4th pixel works well.

boberman

2008-10-14 16:14:10

If not result could cause problems as result is never initialized. there is nothing to guarantee that it is ever going to be false.

For a bit shorter more effecient script it would be better to say

Result := FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0);

since that is what you are basing the result on anyways, there is no need to do two boolean evaluations.

Also, it is very possible that DTM is assigned earlier. Rather then pass it in by reference it might be better to return the DTM. Heres how I might rewrite this script to try and avoid a potential memory leak, and to avoid the result problem.

Function ItemDtm(I:Integer):Integer;
var
MP: TDTMPointDef;
SP: array of TDTMPointDef;
DDTM: TDTM;
TP:TPoint;
Box:TBox;
P:TpointArray;
begin
Box:=InvBox(I);

If not FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0) then
begin
Result := 0;
exit;
end;

TP:=ItemCoords(I);
MP.x := TP.x;
MP.y := TP.y;
MP.areasize := 1;
MP.areashape := 0;
MP.color := GetColor(TP.x, TP.y);
MP.tolerance := 10;
SetArrayLength(SP, GetArrayLength(P));
For I:=0 to high(P)do
begin
SP[i].x := P[i].x;
SP[i].y := P[i].y;
SP[i].areasize := 1;
SP[i].areashape := 0;
SP[i].color := 65536;
SP[i].tolerance := 0;
end;
DDTM.MainPoint := MP;
DDTM.SubPoints := SP;
Result := AddDTM(DDTM);
end;

This makes it harder for the user to overwrite a DTM that they might have used while return 0 if no DTM is created.

Nava2

2008-10-14 22:08:09

@Boberman, why not just free the dtm? The leave the rest.

boberman

2008-10-14 22:25:59

From: Nava2
@Boberman, why not just free the dtm? The leave the rest.


Two problems, if the function frees the DTM, and no DTM is assigned then you will probably have a crash. If the function doesn't free the DTM and one is there then you have a Memory leak.

This way you just have a memory leak, if it hasn't been freed, and you place the responsibility on the user of the function to take care of their DTMs, like it should be.

Nava2

2008-10-16 07:59:59

Btw for your result thing:
Result := Integer(FindColorsToleranceSpiral(x, y, blah...));

Back to where I was, a try except would fix that?

bullzeye95

2008-10-16 16:27:35

Make it return -1 if the color isn't found. If no DTMs are currently in memory, AddDTM() will return 0.

MylesMadness

2008-10-16 17:02:29

Better?

ShowerThoughts

2008-10-16 17:24:01

 If FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0) then
Result:=-1;
If Result = -1 then exit;

Pl0X :p( I c you want a result :()

 If FindColorsTolerance(P, 65536, Box.x1, Box.y1, Box.x2, Box.Y2, 0) then
exit;
edit: wait this can't be working i think you mean IF NOT findcolorsbla then Result := -1;

MylesMadness

2008-10-16 18:33:40

fixed

Nava2

2008-10-16 21:54:12

Function ItemDtm(I:Integer):Integer;
var
MP: TDTMPointDef;
SP: array of TDTMPointDef;
DDTM: TDTM;
TP:TPoint;
Box:TBox;
P:TpointArray;
II, H : Integer;
begin
Box:=InvBox(I);
if not FindColorsTolerance(P, 65536, Box.x1, Box.y1 - 8, Box.x2, Box.Y2, 0) then
Result:=-1;
if Result = -1 then exit;
TP:=ItemCoords(I);
MP.x := TP.x;
MP.y := TP.y;
MP.areasize := 1;
MP.areashape := 0;
MP.color := GetColor(TP.x, TP.y);
MP.tolerance := 10;
H := Length(P);
SetArrayLength(SP, H);
II := Round(H / 8);
H := High(P);
for I := 0 to H do
begin
SP[i].x := P[i].x;
SP[i].y := P[i].y;
SP[i].areasize := 1;
SP[i].areashape := 0;
SP[i].color := 65536;
SP[i].tolerance := 0;
I := I + II;
if I > (H - II) then
I := H;
end;
DDTM.MainPoint := MP;
DDTM.SubPoints := SP;
Result:= AddDTM(DDTM);
end;

Added only using every 8th point, lowered the top of the box, to account for possible text later on. Removed the High in the for loop. Might speed it up, when called lots. :)

Thats all ;)

MylesMadness

2008-10-17 20:46:57

That why everyone always does
I:=High(erdf);
For II:=0 to I do

Because its faster!

Nava2

2008-10-18 01:35:39

If that was to me, I'm not sure why you said that… :) I posted why I changed it to that ;)

MylesMadness

2008-10-18 10:11:34

I was talking to my self. I always saw that and though "its shorter if he put the high in the loop"

mixster

2008-10-20 11:29:57

A couple of things:
1. "FindColorsTolerance(P, 65536, Box.x1, Box.y1 - 8, Box.x2, Box.Y2, 0)"
I think you want +8 there to avoid yellow text and doing -8 means it will search up higher rather than further down.
2. You set the length of SP to H, which is equal to Length(P), but you only use up to 8 points meaning you can have over 80 blanks points with no values.
3. You should keep the ratio (II) as an extended variable to make it more accurate and use the round when adding II to I.