SRL Archive - SRL Snippets

Object/Item find with CTS2 TPA and Mask thing..


Narcle

2012-01-29 19:24:16

I randomly thought of this the other day. It reminds me of mask finding but it uses CTS2 and TPA as well for parameters.

What it does in sequence:
  1. Search for TPA in Area with given color and hue/sat.
  2. Splits into ATPA (with split 2 atm)
  3. Search through ATPA to be between CountLow and CountHi
  4. It then paints that ATPA onto a bitmap
  5. Then it will use bitmaps given and search to see what one matches the Accuracy. (you can use any number of bitmaps for the search)
  6. It will then return the x,y position as well as the BmpNum (for its given state?).
  7. Will Result true if the accuracy is equal or greater then given

I plan on to expand on this if it works well and make it return a record of points and its accuracy. So you could basically call it once on the whole screen and return all matches.

Finding gold bar in bank and inventory. (x, y returns the midpoint shown as red dot, TPA where it was found is the blue)
Debug of this search:
ATPA[0] Length: 309
Acc: 0.99628252788104
Object found - Inv: 6 - MidPoint(619, 261) BmpNum: 0
ATPA[1] Length: 300
Acc: 0.99628252788104
Object found - MidPoint(18, 16) BmpNum: 0


Right now I have it set to TPAItems[bmp_Bar_Gold] this is the item your searching. Just make your own or change bmp_Bar_Gold to another I have listed in the array. (note the cts2 colors aren't finished, they still need to be updated more)

And teh code:
program new;
{$i srl/srl/misc/Smart.simba}
{$i srl/srl.simba}
{$i srl/srl/misc/paintsmart.simba}


Type
TTPA2Item = Record
Tolerance, CountHi, CountLow, Color: Integer;
Bitmaps: TIntegerArray;
Hue, Sat, BmpAcc: Extended;
end;
Var
TPAItems: Array of TTPA2Item;
Mask_Ore, Mask_Bar: Integer;

Function FindTPA2Item(var x, y, BmpNum: integer; Item: TTPA2Item; Area: TBox): Boolean;
var
cts, target, C, w, h, bmp, i: Integer;
TPA, TPA2: TPointArray;
Acc: Extended;
ATPA: T2DPointArray;
B: TBox;
begin
cts := GetColorToleranceSpeed;
target := GetImageTarget;

SetColorToleranceSpeed(2);
SetColorspeed2Modifiers(Item.Hue, Item.Sat)
with Area do
FindColorsTolerance(TPA, Item.Color, X1, Y1, X2, Y2, Item.Tolerance);
SetColorToleranceSpeed(cts);
ATPA := SplitTPA(TPA, 2);

for i := 0 to High(ATPA) do
begin
if not InRange(Length(ATPA[i]), Item.CountLow, Item.CountHi) then
Continue;
Writeln('ATPA['+ToStr(i)+'] Length: '+ToStr(Length(ATPA[i])));
SMART_DrawDotsEx(False, ATPA[i], clBlue);
B := GetTPABounds(ATPA[i]);
OffsetTPA(ATPA[i], Point(-B.X1, -B.Y1));
bmp := CreateBitmap(B.X2-B.X1+2, B.Y2-B.Y1+2);
DrawTPABitmap(bmp, ATPA[i], 255);
GetbitmapSize(bmp,w,h);
SetTargetBitmap(bmp);
for i := 0 to High(Item.Bitmaps) do
begin
FindDeformedBitmapToleranceIn(Item.Bitmaps[i], x, y, 0, 0, w-1, h-1, 0, 0, True, Acc);
Writeln('Acc: '+ToStr(Acc));
if Acc >= Item.BmpAcc then
begin
Result := True;
GetbitmapSize(Item.Bitmaps[i], w, h);
x := x+B.X1+(w/2);
y := y+B.Y1+(h/2);
BmpNum := i;
Break;
end;
end;
if Result then
Break;
end;
SetImageTarget(Target);
if bmp <> 0 then
FreeBitmap(bmp);
end;

Procedure LoadColorArrays;
begin
SetLength(TPAItems, 30);

Mask_Ore := BitmapFromString(14, 18, 'meJxjYICD/9gQVkBrlfgVEATEWI' +
'fLecSrJMnLWBXjUompGI/K/+Said9TWDViqiTG2bj04gK0UwkFALE' +
'LeYc=');
Mask_Bar := BitmapFromString(28, 16, 'meJxjYMAF/lOAqG4gVjMpNBDTTM' +
'oN/E8DA/+PGkjteKFdIqfEcJIA1Q0kbDgTBSbiMJ+KgOoGMhBjIAB' +
'KeAsG');

with TPAItems[bmp_Ore_Iron] do
begin
Tolerance := 4;
Color := 1317678;
CountHi := 150;
CountLow := 130;
Hue := 0.20;
Sat := 0.37;
Bitmaps := [Mask_Ore];
BmpAcc := 0.85;
end;

with TPAItems[bmp_Ore_Coal] do
begin
Tolerance := 4;
Color := 1515295;
CountHi := 145;
CountLow := 100;
Hue := 0.00;
Sat := 0.74;
Bitmaps := [Mask_Ore];
BmpAcc := 0.85;
end;

with TPAItems[bmp_Bar_Steel] do
begin
Tolerance := 15;
Color := 6053216;
CountHi := 350;
CountLow := 150;
Hue := 0.28;
Sat := 0.03;
Bitmaps := [Mask_Bar];
BmpAcc := 0.85;
end;

with TPAItems[bmp_Ore_Tin] do
begin
Tolerance := 15;
Color := 6053216;
CountHi := 150;
CountLow := 100;
Hue := 0.28;
Sat := 0.03;
Bitmaps := [Mask_Ore];
BmpAcc := 0.85;
end;

with TPAItems[bmp_Bar_Gold] do
begin
Tolerance := 16;
Color := 1473692;
CountHi := 350;
CountLow := 150;
Hue := 0.01;
Sat := 0.09;
Bitmaps := [Mask_Bar];
BmpAcc := 0.85;
end;

end;

var
i, BmpNum: integer;
T: TPoint;
begin
Smart_Server := 1;
Smart_Members:= true;
Smart_Signed := true;
Smart_SuperDetail := false;
SetupSRL;
LoadColorArrays;

SMART_ClearCanvas;

{This looks through Inventory}
for i := 1 to 28 do
if FindTPA2Item(T.x, T.y, BmpNum, TPAItems[bmp_Bar_Gold], InvBox(i)) then
begin
Writeln('Object found - Inv: '+ToStr(i)+' - MidPoint'+ToStr(T)+' BmpNum: '+ToStr(BmpNum));
SMART_DrawDot(False, T, clRed);
end;

{This looks on MS}
if FindTPA2Item(T.x, T.y, BmpNum, TPAItems[bmp_Bar_Gold], MSBox) then
begin
Writeln('Object found - MidPoint'+ToStr(T)+' BmpNum: '+ToStr(BmpNum));
SMART_DrawDot(False, T, clRed);
end;

end.