Skip to content

Commit 3a70ae0

Browse files
committed
feat(bank): more methods and fixes
1 parent 7ba367b commit 3a70ae0

File tree

6 files changed

+222
-32
lines changed

6 files changed

+222
-32
lines changed

osrs/interfaces/interface.simba

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ begin
7777
end;
7878

7979
function TRSInterfaceTitle.IsTitle(const text: String; const similarity: Single = 0.8): Boolean;
80+
var
81+
match: Single;
8082
begin
81-
Result := OCR.LocateInvert(Self.Bounds, text, [$344049,0], 2, RSFonts.BOLD) >= similarity;
83+
match := OCR.LocateInvert(Self.Bounds, text, [$344049,0], 2, RSFonts.BOLD);
84+
Result := match >= similarity;
8285
end;
8386

8487
function TRSInterfaceTitle.WaitTitle(const text: String; const similarity: Single = 0.8; const time: Integer = 600; interval: Integer = -1): Boolean; overload;

osrs/interfaces/interfacecontrols.simba

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ begin
2626
Exit(True);
2727
end;
2828

29-
function TRSButton.WaitEnabled(const time: Integer; interval: Integer = -1): Boolean;
29+
function TRSButton.WaitEnabled(const time: Integer = 600; interval: Integer = -1): Boolean;
3030
begin
3131
if interval < 0 then interval := RandomMode(100, 50, 1500);
3232
Result := SleepUntil(Self.Enabled(), interval, time);
@@ -41,7 +41,7 @@ function TRSButton.Enable(): Boolean;
4141
begin
4242
if Self.Enabled() then Exit(True);
4343
Self.Click();
44-
Result := Self.WaitEnabled(600);
44+
Result := Self.WaitEnabled();
4545
end;
4646

4747
function TRSButton.Disable(): Boolean;
@@ -233,7 +233,7 @@ end;
233233

234234
function TRSScrollBar.SetLevel(value: Integer): Integer;
235235
var
236-
old, new, scrolls: Integer;
236+
old, scrolls: Integer;
237237
direction: Boolean;
238238
area: TBox;
239239
begin
@@ -250,11 +250,11 @@ begin
250250
scrolls := Random(1, 3);
251251
if direction then scrolls := -scrolls;
252252
Target.MouseScroll(scrolls);
253-
new := Self.GetLevel();
253+
Result := Self.GetLevel();
254254

255255
//user probably hover them while using RI
256-
if new = old then Mouse.Move(area, True);
257-
old := new;
256+
if Result = old then Mouse.Move(area, True);
257+
old := Result;
258258
end;
259259
end;
260260

osrs/interfaces/iteminterface.simba

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ begin
4949
slot := match.Box;
5050
end;
5151

52+
function TRSItemInterface.Find(item: TRSItem; out slot: TBox): Boolean; overload;
53+
begin
54+
Result := Self.Find([item], slot);
55+
end;
56+
5257

5358
function TRSItemInterface.FindAll(items: TRSItemArray): TBoxArray;
5459
var

osrs/interfaces/mainscreen/bank.simba

Lines changed: 179 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,51 @@ end;
365365

366366

367367

368+
function TRSBank.SetArrangement(arrangement: ERSBankArrangement): Boolean;
369+
begin
370+
if Self.ArrangementButtons[arrangement].Enabled() then Exit(True);
371+
Result := Self.ArrangementButtons[arrangement].Enable();
372+
end;
373+
374+
function TRSBank.SetWithdraw(withdraw: ERSBankWithdraw): Boolean;
375+
begin
376+
if Self.WithdrawButtons[withdraw].Enabled() then Exit(True);
377+
Result := Self.WithdrawButtons[withdraw].Enable();
378+
end;
379+
380+
381+
(*
382+
## Bank.Integer2Quantity
383+
```pascal
384+
function TRSBank.Integer2Quantity(const quantity: Integer): ERSBankQuantity;
385+
```
386+
Internal helper function to convert a integer quantity into a ERSBankQuantity.
387+
388+
Example:
389+
```pascal
390+
WriteLn Bank.Integer2Quantity(5);
391+
WriteLn Bank.Integer2Quantity(7);
392+
```
393+
*)
394+
function TRSBank.Integer2Quantity(const quantity: Integer): ERSBankQuantity;
395+
begin
396+
case quantity of
397+
1: Result := ERSBankQuantity.ONE;
398+
5: Result := ERSBankQuantity.FIVE;
399+
10: Result := ERSBankQuantity.TEN;
400+
Self.QUANTITY_ALL: Result := ERSBankQuantity.ALL;
401+
else Result := ERSBankQuantity.CUSTOM;
402+
end;
403+
end;
404+
405+
function TRSBank.SetQuantity(quantity: ERSBankQuantity): Boolean;
406+
begin
407+
if Self.QuantityButtons[quantity].Enabled() then Exit(True);
408+
Exit(Self.QuantityButtons[quantity].Enable());
409+
end;
410+
411+
412+
368413
(*
369414
## Bank.IsSearchOpen
370415
```pascal
@@ -394,9 +439,9 @@ Example:
394439
WriteLn Bank.WaitSearchOpen();
395440
```
396441
*)
397-
function TRSBank.WaitSearchOpen(const time: Integer = 600): Boolean;
442+
function TRSBank.WaitSearchOpen(const time: Integer = 600; const interval: Integer = -1): Boolean;
398443
begin
399-
Result := Chat.WaitQuery('Show items', True, time);
444+
Result := Chat.WaitQuery('Show items', True, time, interval);
400445
end;
401446

402447
(*
@@ -450,14 +495,16 @@ Bank.Search('logs'); // Search for logs
450495
```
451496
*)
452497
function TRSBank.Search(item: String): Boolean;
498+
var
499+
query: String;
453500
begin
454-
if Self.Title.IsTitle('Showing items: ' + LowerCase(item)) then
455-
Exit(True);
501+
query := 'Showing items: ' + item.ToLower();
502+
if Self.Title.IsTitle(query) then Exit(True);
503+
if not Self.ClearSearch() then Exit;
504+
if not Self.OpenSearch(Random(2000, 2500)) then Exit;
456505

457-
Result := Self.ClearSearch() and
458-
Self.OpenSearch(Random(2000, 2500)) and
459-
Chat.AnswerQuery('Show items', item, Random(2000, 2500)) and
460-
Self.Title.IsTitle('Showing items: ' + LowerCase(item));
506+
if Chat.AnswerQuery('Show items', item, Random(2000, 2500)) then
507+
Result := Self.Title.WaitTitle(query);
461508
end;
462509

463510
(*
@@ -520,38 +567,146 @@ begin
520567
end;
521568

522569

570+
(*
571+
## Bank._FindTabText
572+
```pascal
573+
function TRSBank._FindTabText(const tpa: TPointArray; out bounds: TBox): Boolean;
574+
```
575+
Internal helper function help process the tabs text when you search.
576+
577+
Example:
578+
```pascal
579+
if Bank.Search('logs') then
580+
if Bank._FindTabText(Target.FindColor(RSColors.TEXT_LIGHT_YELLOW, 0, bounds), bounds) then
581+
ShowOnClient(bounds);
582+
```
583+
*)
584+
function TRSBank._FindTabText(const tpa: TPointArray; out bounds: TBox): Boolean;
585+
var
586+
atpa: T2DPointArray;
587+
begin
588+
if tpa = [] then Exit;
589+
atpa := tpa.Cluster(6).ExcludeDimensions(24, 8, 26, 8);
590+
if atpa = [] then Exit;
591+
592+
atpa := atpa.SortByY(False)[0].Cluster(1.5);
593+
bounds := atpa.SortByX(False)[0].Bounds();
594+
595+
bounds.X1 -= 1;
596+
bounds.Y1 -= 1;
597+
bounds.X2 += 1;
598+
bounds.Y2 += 3;
599+
Result := True;
600+
end;
601+
602+
603+
(*
604+
## Bank.FindItemTab
605+
```pascal
606+
function TRSBank.FindItemTab(const item: TRSItem; const openTab: Boolean = True): Integer;
607+
```
608+
Find the bank tab of `item`.
609+
610+
```{note}
611+
A known limitation of this is that if several items match the sprite of the item (for example multiple charged jewlry) the tab retrieved will be the first one found. If you have 'Games necklace(1)' in tab 1 and 'Games necklace(8)' in tab 5 and search for the latter, you will get tab 1.
612+
```
523613

524-
function TRSBank._IsCustomQuantity(quantity: Integer): Boolean;
614+
Example:
615+
```pascal
616+
WriteLn Bank.FindItemTab('Molten glass');
617+
```
618+
*)
619+
function TRSBank.FindItemTab(const item: TRSItem): Integer;
620+
var
621+
bounds: TBox;
525622
begin
526-
Result := not (quantity in [1,5,10, Self.QUANTITY_ALL]);
623+
(* TODO: Remove
624+
if Self.Items.Contains(item) then
625+
begin
626+
if not Self.DynamicButtons[ERSBankDynamicButtons.SEARCH].Enabled() then
627+
Exit(Self.GetCurrentTab());
628+
629+
if Self.WaitSearchOpen(600) then
630+
if not Self.CloseSearch() then Exit(-1); //unknown state, reset search
631+
end;
632+
*)
633+
634+
if not Self.Search(item.SimplifyName()) then Exit(-1);
635+
if not Self.Items.Find(item, bounds) then Exit(-1);
636+
637+
bounds := [Self.Bounds.X1 + 56, Self.Bounds.Y1 + 77, Self.Bounds.X1 + 84, bounds.Y1];
638+
if bounds.Height < 8 then Exit(0);
639+
640+
if Self._FindTabText(Target.FindColor(RSColors.TEXT_LIGHT_YELLOW, 0, bounds), bounds) then
641+
Result := OCR.RecognizeNumber(bounds, RSFonts.PLAIN_11, [RSColors.TEXT_LIGHT_YELLOW], 0);
642+
if Self._FindTabText(Target.FindColor(RSColors.TEXT_WHITE, 0, bounds), bounds) then
643+
Result := OCR.RecognizeNumber(bounds, RSFonts.PLAIN_11, [RSColors.TEXT_WHITE], 0);
527644
end;
528645

529646
(*
530-
## Bank._SimplifyItemName
647+
## Bank.FindItemScroll
531648
```pascal
532-
function TRSBank._SimplifyItemName(item: TRSItem): String;
649+
function TRSBank.FindItemScroll(const item: TRSItem): Integer;
650+
```
651+
Find the scroll position of the bank where `item` is visible..
652+
653+
```{note}
654+
A known limitation of this is that if several items match the sprite of the item (for example multiple charged jewlry) the tab retrieved will be the first one found. If you have 'Games necklace(1)' in tab 1 and 'Games necklace(8)' in tab 5 and search for the latter, you will get tab 1.
533655
```
534-
Internal function to get a human like search term for an item as humans
535-
don't usually type the full item name when searching for something.
536656

537657
Example:
538658
```pascal
539-
WriteLn Bank._SimplifyItemName('Amulet of glory(6)');
659+
WriteLn Bank.FindItemScroll('Molten glass');
540660
```
541661
*)
542-
function TRSBank._SimplifyItemName(item: TRSItem): String;
662+
function TRSBank.FindItemScroll(const item: TRSItem): Integer;
543663
var
544-
str: String;
545-
short, long: Integer;
664+
next: Integer;
665+
down: Boolean;
546666
begin
547-
str := item.Before('(').ToLower();
548-
long := Length(str);
549-
short := Min(0, long - Ceil(long/3));
667+
(* TODO: Remove
668+
if Self.Items.Contains(item) then
669+
begin
670+
if not Self.DynamicButtons[ERSBankDynamicButtons.SEARCH].Enabled() then
671+
Exit(Self.Scroll.GetLevel());
672+
673+
if Self.WaitSearchOpen(600) then
674+
if not Self.CloseSearch() then Exit(-1); //unknown state, reset search
675+
end;
676+
*)
677+
678+
if not Self.Scroll.CanScroll() then Exit(-1);
679+
680+
Result := Self.Scroll.GetLevel();
681+
down := (Result > 0) or RandomBoolean(0.5);
550682

551-
Result := str.CopyRange(0, Random(short, long));
683+
repeat
684+
if down then next := Result - 5
685+
else next := Result + 5;
686+
687+
Result := Self.Scroll.SetLevel(next);
688+
if Self.Items.Contains(item) then Exit;
689+
690+
if not Self.IsOpen() then Exit(-1); //failsafe
691+
until not InRange(Result, 1, 99);
692+
693+
down := not down;
694+
695+
repeat
696+
if down then next := Result - 5
697+
else next := Result + 5;
698+
699+
Result := Self.Scroll.SetLevel(next);
700+
if Self.Items.Contains(item) then Exit;
701+
702+
if not Self.IsOpen() then Exit(-1);
703+
until not InRange(Result, 1, 99);
704+
705+
Result := -1;
552706
end;
553707

554708

709+
555710
procedure TRSBank.Draw(img: TImage);
556711
var
557712
i: Integer;
@@ -572,17 +727,16 @@ begin
572727
Self.DynamicButtons[i].Draw(img);
573728
end;
574729

575-
procedure ShowOnClient(bank: TRSBank); overload;
730+
procedure TRSBank.ShowOnClient();
576731
var
577732
img: TImage;
578733
begin
579734
img := TImage.CreateFromTarget();
580-
bank.Draw(img);
735+
Self.Draw(img);
581736
img.Show();
582737
img.Free();
583738
end;
584739

585740

586-
587741
var
588742
Bank: TRSBank;

utils/item.simba

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ function TRSItem.HasAmount(): Boolean;
1111
begin
1212
Result := Self.EndsWith(')', True);
1313
end;
14+
15+
(*
16+
## TRSItem.SimplifyName
17+
```pascal
18+
function TRSItem.SimplifyName(): String;
19+
```
20+
Internal helper function to get a human like short item name, usually to search.
21+
22+
Example:
23+
```pascal
24+
item := 'Amulet of glory(6)';
25+
WriteLn item.SimplifyName();
26+
```
27+
*)
28+
function TRSItem.SimplifyName(): String;
29+
var
30+
str: String;
31+
short, long: Integer;
32+
begin
33+
Result := ToStr(Self).ToLower();
34+
if Result.EndsWith(')') then Result := Result.Before('(');
35+
36+
long := Length(Result);
37+
short := Min(0, long - Ceil(long/3));
38+
Result := Result.CopyRange(0, Random(short, long));
39+
end;

utils/misc.simba

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ type
1616
const STACK_GREEN: TColor = $80FF00;
1717
const STACK_COLORS: TColorArray = [$00FFFF, $FFFFFF, $80FF00];
1818

19+
const TEXT_WHITE: TColor = $FFFFFF;
1920
const TEXT_ORANGE: TColor = $1F98FF;
2021
const TEXT_GREY: TColor = $9F9F9F;
22+
const TEXT_LIGHT_YELLOW: TColor = $A2D8E4;
2123
end;
2224

2325
function RSColors.HasItem(const bounds: TBox): Boolean; static;

0 commit comments

Comments
 (0)