Advertisement
WarPie90

ItemFinder.IsItem [False positive elimination]

May 10th, 2023 (edited)
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.32 KB | None | 0 0
  1. {$I SRL/osr.simba}
  2.  
  3. // this whole function is a hack, a stable working hack, but still a hack.
  4. procedure TRSItemFinder.PrepareCache();
  5. var
  6.   i: Int32;
  7.   ItemID, FileName: string;
  8.   list: array of string;
  9. begin
  10.   // Check if unzip is needed:
  11.   for i:=0 to 30000 do
  12.     if (i < Self.ItemNames.GetCount()) then
  13.     begin
  14.       ItemID := Self.ItemNames.GetStrings(i).After('=');
  15.       if ItemID <> '' then
  16.       begin
  17.         FileName := ExpandFileName(ITEM_FINDER_IMAGES_PATH + ItemID + '.png');
  18.         if not FileExists(FileName) then
  19.           list += FileName;
  20.       end;
  21.     end;
  22.  
  23.   // if we are missing > 500 files unzip it all, otherwise just do that one by one
  24.   // with the call later on to GetItemImages
  25.   if Length(list) > 500 then begin
  26.     WriteLn('Note: Unzipping item folder. This may take minutes');
  27.     UnZipFile(ITEM_FINDER_IMAGES_ZIP, ITEM_FINDER_IMAGES_PATH)
  28.   end;
  29.  
  30.   // load spirits to memory
  31.   if Length(Self.ItemImages) <= 8000 then //cant compare to Self.ItemNames.GetCount() idk why
  32.   begin
  33.     WriteLn('Note: Caching ',Self.ItemNames.GetCount(),' item spirits. This may take a minute');
  34.     for i:=0 to 30000 do
  35.     begin
  36.       if (i < Self.ItemNames.GetCount()) then
  37.       begin
  38.         name := Self.ItemNames.GetStrings(i).Before('=');
  39.         if name <> '' then Self.GetItemImages(name);
  40.       end;
  41.     end;
  42.   end;
  43. end;
  44.  
  45. // This method will check if there are any other item that match
  46. // our item better. If this is true, we can consider the match a false positive.
  47. function TRSItemFinder.IsItem(Item: String; Slot: TBox): Boolean;
  48. var
  49.   CustomFilter: TRSItemFinderCustomFilter;
  50.  
  51.   function CurrentMatch(constref Item: string; Slot: TBox): single;
  52.   var
  53.     i: Int32;
  54.     templ: TMufasaBitmap;
  55.     images: array of TMufasaBitmap;
  56.   begin
  57.     images := Self.GetItemImages(Item);
  58.  
  59.     for i:=0 to High(Self.ClearSparkleItems) do
  60.       if (SameText(Self.ClearSparkleItems[I], Item)) then
  61.         CustomFilter := @Self.ClearSparkles;
  62.  
  63.     templ.FromClient(Slot);
  64.     for i:=0 to High(images) do
  65.       Result := Max(Self.CompareItems(images[i], templ, @CustomFilter), Result);
  66.     templ.Free();
  67.   end;
  68. var
  69.   templ: TMufasaBitmap;
  70.   i,j: Int32;
  71.   limit: Single;
  72. begin
  73.   Result := True;
  74.   limit := CurrentMatch(Item, slot)+0.00001;
  75.  
  76.   templ.FromClient(Slot);
  77.   for j:=0 to High(Self.ItemImages) do
  78.     for i:=0 to High(Self.ItemImages[j].Images) do
  79.       if (Self.CompareItems(Self.ItemImages[j].Images[i], templ, @CustomFilter) > limit) then
  80.       begin
  81.         templ.Free();
  82.         Exit(False);
  83.       end;
  84.   templ.Free();
  85. end;
  86.  
  87.  
  88. // This method will check if there are any other item that match
  89. // our item better. If this is true, we can consider the match a false positive.
  90. function TRSItemFinder.ItemAt(Slot: TBox): string;
  91. var
  92.   CustomFilter: TRSItemFinderCustomFilter;
  93. var
  94.   templ: TMufasaBitmap;
  95.   i,j: Int32;
  96.   best, match: Single;
  97. begin
  98.   Result := '';
  99.   best := 0;
  100.  
  101.   templ.FromClient(Slot);
  102.   for j:=0 to High(Self.ItemImages) do
  103.     for i:=0 to High(Self.ItemImages[j].Images) do
  104.     begin
  105.       match := Self.CompareItems(Self.ItemImages[j].Images[i], templ, @CustomFilter);
  106.       if (match > best) then
  107.       begin
  108.         best := match;
  109.         Result := Self.ItemImages[j].Name;
  110.       end;
  111.     end;
  112.   templ.Free();
  113. end;
  114.  
  115.  
  116. var
  117.   s: TIntegerArray;
  118.   t: Double;
  119. begin
  120.   //loading 16K images to memory, possibly unzipping as well [brace yourself]
  121.   ItemFinder.PrepareCache();
  122.  
  123.   // now we can do other shit
  124.   Inventory.FindItem('Adamant scimitar', s);
  125.   if Length(s) > 0 then
  126.   begin
  127.     t := PerformanceTimer();
  128.     WriteLn ItemFinder.IsItem('Adamant scimitar', Inventory.GetSlotBox(s[0]));
  129.     WriteLn(PerformanceTimer() - t, 'ms');
  130.  
  131.     t := PerformanceTimer();
  132.     WriteLn ItemFinder.IsItem('Iron scimitar', Inventory.GetSlotBox(s[0]));
  133.     WriteLn(PerformanceTimer() - t, 'ms');
  134.  
  135.     t := PerformanceTimer();
  136.     WriteLn ItemFinder.IsItem('Steel scimitar', Inventory.GetSlotBox(s[0]));
  137.     WriteLn(PerformanceTimer() - t, 'ms');
  138.  
  139.     t := PerformanceTimer();
  140.     WriteLn ItemFinder.IsItem('Rune scimitar', Inventory.GetSlotBox(s[0]));
  141.     WriteLn(PerformanceTimer() - t, 'ms');
  142.  
  143.     t := PerformanceTimer();
  144.     WriteLn ItemFinder.IsItem('Monkfish', Inventory.GetSlotBox(s[0]));
  145.     WriteLn(PerformanceTimer() - t, 'ms');
  146.   end;
  147. end;
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement