Advertisement
Guest User

Untitled

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