Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.82 KB | None | 0 0
  1. program new;
  2. {$I SRL/OSR.simba}
  3.  
  4. var
  5.   ITEM_FINDER_SIMILARITY = 0.95;
  6.   IGNORE_COLORS_L = [0, $FFFFFF, $00FFFF, $80FF00];
  7.   IGNORE_COLORS_R = [0, $FFFFFF, $00FFFF, $80FF00];
  8.  
  9. type
  10.   TRSItemImage = type TMufasaBitmap;
  11.   TRSItemFinder = array of TRSItemImage;
  12.  
  13.   TRSItemMatch = record
  14.     Item, Edge: Double;
  15.   end;
  16.  
  17. var
  18.   ItemFinder: TRSItemFinder;
  19.  
  20. operator >= (Left: TRSItemMatch; Right: Double): Boolean;
  21. begin
  22.   Result := (Left.Item >= Right) and (Left.Edge >= Right);
  23. end;
  24.  
  25. procedure TRSItemImage.FromData(Data: String);
  26. var
  27.   Stream: TStringStream;
  28.   Picture: TPicture;
  29. begin
  30.   Stream.Init(Data);
  31.   Picture.Init();
  32.  
  33.   try
  34.     Picture.LoadFromStream(Stream);
  35.     LoadFromTBitmap(Picture.GetBitmap());
  36.   finally
  37.     Picture.Free();
  38.     Stream.Free();
  39.   end;
  40. end;
  41.  
  42. procedure TRSItemImage.FromFile(FilePath: String);
  43. begin
  44.   if (Self = nil) then
  45.     Self.Init(Client.GetMBitmaps());
  46.  
  47.   LoadFromFile(FilePath);
  48. end;
  49.  
  50. procedure TRSItemImage.FromURL(URL: String);
  51. begin
  52.   if (Self = nil) then
  53.     Self.Init(Client.GetMBitmaps());
  54.  
  55.   FromData(GetPage(URL));
  56. end;
  57.  
  58. procedure TRSItemImage.FromGE(Identifer: String);
  59.  
  60.   function FindID: Int32;
  61.   const
  62.     URL = 'http://services.runescape.com/m=itemdb_oldschool/results';
  63.   var
  64.     Data: String;
  65.   begin
  66.     Data := GetPage(URL + '?query=' + LowerCase(Identifer));
  67.     Data := Between('class=''table-item-link''', '</a>', Data);
  68.  
  69.     Result := StrToIntDef(Between('obj=', '"', Data), -1);
  70.   end;
  71.  
  72.   function GetVersion: UInt64;
  73.   begin
  74.     Result := StrToUInt64Def(Between('<img src=''http://services.runescape.com/m=itemdb_oldschool/', '_obj_sprite.gif',
  75.                              GetPage('http://services.runescape.com/m=itemdb_oldschool/top100?list=1')), -1);
  76.     if (Result = -1) then
  77.       raise 'Unknown database version';
  78.   end;
  79.  
  80. var
  81.   ID: Int32;
  82. begin
  83.   ID := StrToIntDef(Identifer, -1);
  84.   if (ID = -1) then
  85.     ID := FindID();
  86.   if (ID = -1) then
  87.     raise 'Unknown item ID for "' + Identifer + '"';
  88.  
  89.   Self.FromURL(Format('http://services.runescape.com/m=itemdb_oldschool/%d_obj_sprite.gif?id=%d', [GetVersion(), ID]));
  90.   Self.ReplaceColor(16711935, 0);
  91. end;
  92.  
  93. procedure TRSItemImage.Align(Other: TRSItemImage);
  94. var
  95.   TPA1, TPA2: TPointArray;
  96.   P1, P2: TPoint;
  97.   BMP: TMufasaBitmap;
  98. begin
  99.   if Self.FindColors(TPA1, $10000) and Other.FindColors(TPA2, $10000) then
  100.   begin
  101.     TPA1.SortByRow(True);
  102.     TPA2.SortByRow(True);
  103.     P1 := TPA1[0];
  104.     P2 := TPA2[0];
  105.  
  106.     BMP := Self.Copy();
  107.     BMP.DrawTransparent(Max(0, P2.X - P1.X), Max(0, P2.Y - P1.Y), Self);
  108.     BMP.Free();
  109.  
  110.     Self.Crop(Abs(Min(0, P2.X - P1.X)), Abs(Min(0, P2.Y - P1.Y)), 31, 31);
  111.     Self.SetSize(32, 32);
  112.   end;
  113. end;
  114.  
  115. function TRSItemFinder.Get(Identifer: String): TRSItemImage;
  116. const
  117.   IMAGES_PATH = IncludePath + 'SRL/utils/items/';
  118. var
  119.   i: Int32;
  120. begin
  121.   for i := 0 to High(Self) do
  122.     if (Self[i].GetName() = Identifer) then
  123.       Exit(Self[i]);
  124.  
  125.   if FileExists(Identifer) then
  126.     Result.FromFile(Identifer)
  127.   else
  128.   if FileExists(IMAGES_PATH + Identifer + '.png') then
  129.     Result.FromFile(IMAGES_PATH + Identifer + '.png')
  130.   else
  131.   begin
  132.     Result.FromGE(Identifer);
  133.     Result.SaveToFile(IMAGES_PATH + Identifer + '.png');
  134.   end;
  135.  
  136.   Result.SetName(Identifer);
  137.  
  138.   if (Result.GetWidth() <> 32) or (Result.GetHeight() <> 32) then
  139.     raise 'Invaild image dimensions';
  140.  
  141.   Self += Result;
  142. end;
  143.  
  144. procedure TRSItemFinder.Free;
  145. var
  146.   i: Int32;
  147. begin
  148.   for i := 0 to High(Self) do
  149.     Self[i].Free();
  150. end;
  151.  
  152. function TRSItemFinder.Match(Img1, Img2: TMufasaBitmap;
  153.     Ignore1: TIntArray = IGNORE_COLORS_L;
  154.     Ignore2: TIntArray = IGNORE_COLORS_R;
  155.     HuePower: Double = 5.0): TRSItemMatch;
  156. var
  157.   i, off, Color1, Color2, Ignore, EdgeCount: Int32;
  158.   deltaH, H1, S1, L1, H2, S2, L2, Diff, Sum, EdgeDiff: Extended;
  159.   Data1, Data2: PRGB32;
  160.   DoIgnore: Boolean;
  161.   FACT := 1 / Sqrt(Sqr(100) + Sqr(100) + Sqr(50 * HuePower));
  162. begin
  163.   Data1 := Img1.GetData();
  164.   Data2 := Img2.GetData();
  165.   EdgeCount := 1;
  166.  
  167.   for i := 0 to Img1.GetWidth() * Img1.GetHeight() - 1 do
  168.   begin
  169.     Color1 := RGBToColor(Data1^.R, Data1^.G, Data1^.B);
  170.     Color2 := RGBToColor(Data2^.R, Data2^.G, Data2^.B);
  171.  
  172.     if (Ignore1.Find(Color1) = -1) and (Ignore2.Find(Color2) = -1) then
  173.     begin
  174.       RGBToHSL(Data1^.R, Data1^.G, Data1^.B, H1, S1, L1);
  175.       RGBToHSL(Data2^.R, Data2^.G, Data2^.B, H2, S2, L2);
  176.  
  177.       deltaH := Abs(H1 - H2);
  178.       if deltaH >= 50 then deltaH := 100 - deltaH;
  179.       deltaH *= HuePower;
  180.  
  181.       Diff := Sqrt(Sqr(deltaH) + Sqr(S1-S2) + Sqr(L1-L2)) * FACT;
  182.       Sum  := Sum + Diff;
  183.       if (Color1 = $10000) then
  184.       begin
  185.         if (Color2 = $10000) then
  186.           EdgeDiff := EdgeDiff + 1;
  187.         Inc(EdgeCount);
  188.       end;
  189.     end else
  190.       Inc(off);
  191.  
  192.     Inc(Data1); Inc(Data2);
  193.   end;
  194.  
  195.   if (Img1.GetWidth() * Img1.GetHeight() - off) = 0 then
  196.     Exit; // bad image
  197.   Result := [1 - sum / (Img1.GetWidth() * Img1.GetHeight() - off), EdgeDiff / EdgeCount];
  198. end;
  199.  
  200. function TRSItemFinder.FindAll(Identifer: String; Arr: TBoxArray; Similarity: Double = ITEM_FINDER_SIMILARITY): TIntegerArray;
  201. var
  202.   i, X, Y: Int32;
  203.   imgDB, imgRS: TRSItemImage;
  204.   Matches: array of TRSItemMatch;
  205.   Match, Best: TRSItemMatch;
  206. begin
  207.   SetLength(Matches, Length(Arr));
  208.  
  209.   for i := 0 to High(Arr) do
  210.     if FindColor(X, Y, $10000, Arr[i]) then // Has item
  211.     begin
  212.       imgDB := Self.Get(Identifer).Copy();
  213.       imgRS.FromClient(Arr[i].ExpandFunc(2));
  214.       imgRS.Align(imgDB);
  215.  
  216.       Match := Self.Match(imgDB, imgRS);
  217.       Matches[i] := Match;
  218.       if (Match.Item >= Best.Item) then
  219.         Best := Match;
  220.  
  221.       imgDB.Free();
  222.       imgRS.Free();
  223.     end;
  224.  
  225.   for i := 0 to High(Arr) do
  226.     if (Matches[i] >= Similarity) and (Matches[i].Item >= Best.Item - 0.01) then
  227.       Result += i;
  228. end;
  229.  
  230. // ------------------------------ Inventory --------------------------------- \\
  231.  
  232. // Indices of all matches
  233. function TRSInventory.FindAll(Identifers: TStringArray; Similarity: Double = ITEM_FINDER_SIMILARITY): TIntegerArray; overload;
  234. var
  235.   Identifer: String;
  236. begin
  237.   if Self.Open() then
  238.     for Identifer in Identifers do
  239.       Result += ItemFinder.FindAll(Identifer, Self.FSlots, Similarity);
  240. end;
  241.  
  242. function TRSInventory.FindAll(Identifer: String; Similarity: Double = ITEM_FINDER_SIMILARITY): TIntegerArray; overload;
  243. begin
  244.   Result := Self.FindAll([Identifer]);
  245. end;
  246.  
  247. // Index of first match
  248. function TRSInventory.Find(Identifers: TStringArray; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  249. var
  250.   Identifer: String;
  251.   Matches: TIntegerArray;
  252. begin
  253.   for Identifer in Identifers do
  254.   begin
  255.     Matches := Self.FindAll(Identifer, Similarity);
  256.     if Length(Matches) > 0 then
  257.       Exit(Matches[0]);
  258.   end;
  259.  
  260.   Exit(-1);
  261. end;
  262.  
  263. function TRSInventory.Find(Identifer: String; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  264. begin
  265.   Result := Self.Find([Identifer]);
  266. end;
  267.  
  268. // Counts all items
  269. function TRSInventory.Count(Identifers: TStringArray; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  270. var
  271.   Identifer: String;
  272. begin
  273.   for Identifer in Identifers do
  274.     Result += Length(Self.FindAll(Identifer, Similarity));
  275. end;
  276.  
  277. // Counts a item
  278. function TRSInventory.Count(Identifer: String; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  279. begin
  280.   Result := Self.Count([Identifer], Similarity);
  281. end;
  282.  
  283. // Counts a item stack
  284. function TRSInventory.CountStack(Identifer: String; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32;
  285. var
  286.   Slot: Int32;
  287. begin
  288.   Slot := Self.Find(Identifer, Similarity);
  289.   if (Slot > -1) then
  290.     Result := srl.GetItemAmount(Self.GetSlotBox(Slot));
  291. end;
  292.  
  293. // Clicks first item match
  294. function TRSInventory.Click(Identifers: TStringArray; Option: String = ''; Similarity: Double = ITEM_FINDER_SIMILARITY): Boolean; overload;
  295. var
  296.   Slot: Int32;
  297. begin
  298.   Slot := Self.Find(Identifers, Similarity);
  299.  
  300.   if (Slot > -1) then
  301.   begin
  302.     Self.MouseSlot(Slot, mouse_Move);
  303.  
  304.     if (Option = '') then
  305.       Mouse.Click(mouse_Left)
  306.     else
  307.     if (not ChooseOption.Open()) or (not ChooseOption.Select(Option)) then
  308.       Exit(False);
  309.  
  310.     Exit(True);
  311.   end;
  312. end;
  313.  
  314. function TRSInventory.Click(Identifer: String; Option: String = ''; Similarity: Double = ITEM_FINDER_SIMILARITY): Boolean; overload;
  315. begin
  316.   Result := Self.Click([Identifer], Option, Similarity);
  317. end;
  318.  
  319. // -------------------------------- Bank ------------------------------------ \\
  320.  
  321. // Index of first match
  322. function TRSBankScreen.Find(Identifers: TStringArray; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  323. var
  324.   Identifer: String;
  325.   Matches: TIntegerArray;
  326. begin
  327.   Self.FixSlots();
  328.  
  329.   for Identifer in Identifers do
  330.   begin
  331.     Matches := ItemFinder.FindAll(Identifer, Self.FSlots, Similarity);
  332.     if Length(Matches) > 0 then
  333.       Exit(Matches[0]);
  334.   end;
  335.  
  336.   Exit(-1);
  337. end;
  338.  
  339. function TRSBankScreen.Find(Identifer: String; Similarity: Double = ITEM_FINDER_SIMILARITY): Int32; overload;
  340. begin
  341.   Result := Self.Find([Identifer]);
  342. end;
  343.  
  344. // Withdraw first match
  345. function TRSBankScreen.Withdraw(Identifier: String; Amount: Int32; Mode: EBankButton = bbItem): Boolean; overload;
  346. var
  347.   Slot: Int32;
  348. begin
  349.   Slot := Self.Find(Identifier);
  350.   if (Slot > -1) then
  351.     Result := Self.Withdraw(Slot, Amount, TStringArray([]), Mode);
  352. end;
  353.  
  354. // Contains
  355. function TRSBankScreen.Contains(Identifer: String): Boolean;
  356. begin
  357.   Result := Self.Find(Identifer) >= 0;
  358. end;
  359.  
  360. // Counts a item stack
  361. function TRSBankScreen.CountStack(Identifer: String): Int32;
  362. var
  363.   Slot: Int32;
  364. begin
  365.   Slot := Self.Find(Identifer);
  366.   if (Slot > -1) then
  367.     Result := srl.GetItemAmount(Self.GetSlotBox(Slot));
  368. end;
  369.  
  370. begin
  371.   AddOnTerminate(@ItemFinder.Free);
  372.   (*
  373.   SetTargetBitmap(LoadBitmap('Images/client.png'));
  374.   WriteLn Bankscreen.Find('mind rune');
  375.   *)
  376. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement