Skip to content

Commit 23f4855

Browse files
committed
feat: GameTabs Keybinds added
This is a complete rewrite of my original keybindings in 1.4. Much cleaner and simpler than the original, partially due to knowledge, partially due to being built directly on the TRSGameTabs. The way it works is the same as the original in that we try random FKeys when we try to use it, probably fail, but cache the results for next uses. Once things start getting mapped it starts working as intended.
1 parent 5278845 commit 23f4855

File tree

4 files changed

+115
-45
lines changed

4 files changed

+115
-45
lines changed

osrs/interfaces/gametabs/gametabs.simba

Lines changed: 109 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
(*
22
# GameTabs
3-
Methods to interact with the gametab buttons
3+
Methods to interact with the gametab buttons.
4+
5+
By default it will attempt to use "F-Keys" to open gametabs.
6+
You can turn this off by setting `TRSGameTabs.KeybindsEnabled` to `False`.
7+
8+
The way keybindings work in SRLT is that it will try a random "F-Key" everytime
9+
you want to open a gametab. Because it uses a random "F-Key", it will probably
10+
not open the gametab you want but whatever opens, gets cached and mapped to the
11+
random "F-Key" used.
12+
13+
Next time you want to open a gametab that has already been mapped, it will use
14+
the correct "F-Key".
415
*)
516
{$DEFINE SRLT_GAMETABS_INCLUDED}
617
{$INCLUDE_ONCE SRLT/osrs.simba}
718

19+
{.$DEFINE SRLT_KEYBINDS_DEBUG}
20+
821
type
922
(*
1023
(ERSGameTab)=
@@ -14,9 +27,8 @@ ERSGameTab = enum(NONE, COMBAT, STATS, ACHIEVEMENTS, INVENTORY, EQUIPMENT, PRAYE
1427
```
1528
*)
1629
ERSGameTab = enum(
17-
NONE,
1830
COMBAT, STATS, ACHIEVEMENTS, INVENTORY, EQUIPMENT, PRAYER, MAGIC,
19-
CLAN, FRIENDS, ACCOUNT, LOGOUT, OPTIONS, EMOTES, MUSIC
31+
CLAN, FRIENDS, ACCOUNT, LOGOUT, OPTIONS, EMOTES, MUSIC, NONE
2032
);
2133

2234
(*
@@ -26,9 +38,10 @@ Main record responsible for handling the gametabs.
2638
*)
2739
TRSGameTabs = record
2840
Bounds: TBox;
29-
{%codetools off}
3041
Tabs: TBoxArray;
31-
{%codetools on}
42+
Keybinds: array [ERSGameTab] of EKeyCode;
43+
KeybindsEnabled: Boolean;
44+
AvailableKeys: EKeycodeArray;
3245
const ActiveColor: TColorTolerance = [$14194A, 1.235, EColorSpace.HSL, [1.631, 1.084, 0.286]];
3346
end;
3447

@@ -37,7 +50,9 @@ Main record responsible for handling the gametabs.
3750
```pascal
3851
procedure TRSGameTabs.SetupInterface();
3952
```
40-
Internal method automatically called for you responsible for setting up coordinates.
53+
Internal method responsible for setting up coordinates for the {ref}`TRSGameTabs`.
54+
55+
This is automatically called for you on the {ref}`GameTabs variable`.
4156
*)
4257
procedure TRSGameTabs.SetupInterface();
4358
var
@@ -89,8 +104,8 @@ begin
89104
if Target.Width >= 948 then
90105
begin
91106
Self.Tabs := TBoxArray.Create(Self.Bounds.BottomLeft.Offset(0,-35), 14, 1, 30, 33, [3, 0]);
92-
for tab := ERSGameTab.MUSIC downto ERSGameTab.FRIENDS - 1 do
93-
Self.Tabs[tab-1] := Self.Tabs[tab-2];
107+
for tab := ERSGameTab.MUSIC downto ERSGameTab.FRIENDS do
108+
Self.Tabs[tab] := Self.Tabs[tab-1];
94109
end
95110
else // Two rows
96111
Self.Tabs := TBoxArray.Create(Self.Bounds.BottomLeft.Offset(0,-70), 7, 2, 30, 33, [3, 3]);
@@ -105,24 +120,6 @@ begin
105120
end;
106121
end;
107122

108-
109-
(*
110-
## GameTabs.Get
111-
```pascal
112-
function TRSGameTabs.Get(tab: ERSGameTab): TBox;
113-
```
114-
Get a GameTab button bounds.
115-
116-
Example:
117-
```pascal
118-
ShowOnClient(GameTabs.Get(ERSGameTab.INVENTORY));
119-
```
120-
*)
121-
function TRSGameTabs.Get(tab: ERSGameTab): TBox;
122-
begin
123-
Result := Self.Tabs[tab-1];
124-
end;
125-
126123
(*
127124
## GameTabs.GetCurrent
128125
```pascal
@@ -142,9 +139,10 @@ begin
142139
if (RSClient.Mode = ERSMode.RESIZABLE_MODERN) and (Target.CountColor(8639715, 0, Self.Tabs[ERSGameTab.LOGOUT-1]) > 0) then
143140
Exit(ERSGameTab.LOGOUT);
144141

145-
for tab := ERSGameTab.COMBAT to High(ERSGameTab) do
146-
if (Target.CountColor(Self.ActiveColor, Self.Tabs[tab-1]) > 50) then
142+
for tab := ERSGameTab.COMBAT to ERSGameTab.MUSIC do
143+
if (Target.CountColor(Self.ActiveColor, Self.Tabs[tab]) > 50) then
147144
Exit(tab);
145+
Result := ERSGameTab.NONE;
148146
end;
149147

150148

@@ -184,25 +182,90 @@ begin
184182
Result := SleepUntil(Self.IsOpen(tab), interval, time);
185183
end;
186184

185+
186+
(*
187+
## GameTabs.FKeyOpen
188+
```pascal
189+
function TRSGameTabs.FKeyOpen(tab: ERSGameTab): Boolean;
190+
```
191+
Attempts to open the specified `tab` gametab with an FKey.
192+
This works by randomly using FKeys that haven't been tried yet and slowly
193+
caching which gametabs they open. At first most times this is used it won't
194+
open the `tab` specified, but as more EKeyCode/ERSGameTab pairs get mapped
195+
this starts getting them right.
196+
197+
Example:
198+
```pascal
199+
WriteLn GameTabs.FKeyOpen(ERSGameTab.INVENTORY);
200+
```
201+
*)
202+
function TRSGameTabs.FKeyOpen(tab: ERSGameTab): Boolean;
203+
var
204+
current, new: ERSGameTab;
205+
i: Integer;
206+
switched: Boolean;
207+
begin
208+
{$IFDEF SRLT_KEYBINDS_DEBUG}
209+
for current := ERSGameTab.COMBAT to ERSGameTab.MUSIC do
210+
WriteLn GetDebugLn('GameTabs', 'Keybinds -> ' + ToStr(current) + ' -> ' ToStr(Self.Keybinds[current]));
211+
{$ENDIF}
212+
213+
if Self.Keybinds[tab] <> EKeyCode.UNKNOWN then
214+
begin
215+
Keyboard.PressKey(Self.Keybinds[tab]);
216+
Exit(Self.WaitOpen(tab, 2000));
217+
end;
218+
219+
if Self.AvailableKeys = [] then Exit; //all keys are already setup and there's no match
220+
i := Random(0, High(Self.AvailableKeys));
221+
222+
current := Self.GetCurrent();
223+
Keyboard.PressKey(Self.AvailableKeys[i]);
224+
225+
switched := SleepUntil(current <> (new := Self.GetCurrent()), RandomMode(100, 50, 1500), 600);
226+
227+
if switched then //if tab switched we are sure of this key-gametab pair
228+
begin
229+
Self.Keybinds[new] := Self.AvailableKeys[i];
230+
Delete(Self.AvailableKeys, i, 1);
231+
end
232+
else if Self.Keybinds[new] <> EKeyCode.UNKNOWN then
233+
Delete(Self.AvailableKeys, i, 1); //This key has no pair
234+
235+
Result := new = Tab;
236+
end;
237+
238+
187239
(*
188240
## GameTabs.Open
189241
```pascal
190-
function TRSGameTabs.Open(tab: ERSGameTab): Boolean;
242+
function TRSGameTabs.Open(tab: ERSGameTab; fkeyProbability: Single = -1): Boolean;
191243
```
192244
Attempts to open the specified `tab` gametab.
245+
If `TRSGameTabs.KeybindsEnabled` is true, we might attempt to open the tab
246+
with {ref}`TRSGameTabs.FKeyOpen`, subject to `fkeyProbability`.
193247

194248
Example:
195249
```pascal
196250
WriteLn GameTabs.Open(ERSGameTab.INVENTORY);
197251
```
198252
*)
199-
function TRSGameTabs.Open(tab: ERSGameTab): Boolean;
253+
function TRSGameTabs.Open(tab: ERSGameTab; fkeyProbability: Single = -1): Boolean;
200254
begin
201-
if Self.IsOpen(tab) then Exit(True);
255+
if Self.IsOpen(tab) then
256+
Exit(True);
257+
258+
if Self.KeybindsEnabled then
259+
begin
260+
if fkeyProbability = -1 then fkeyProbability := Biometrics.RandomDouble(0.8);
261+
262+
if RandomBoolean(fkeyProbability) and Self.FKeyOpen(tab) then
263+
Exit(True);
264+
end;
202265

203266
for 1 to 3 do
204267
begin
205-
Mouse.Click(Self.Tabs[tab-1], EMouseButton.LEFT);
268+
Mouse.Click(Self.Tabs[tab], EMouseButton.LEFT);
206269
if Self.WaitOpen(tab, 2000) then Exit(True);
207270
end;
208271
end;
@@ -215,4 +278,18 @@ begin
215278
end;
216279

217280
var
281+
(*
282+
(GameTabs variable)=
283+
## var GameTabs
284+
Global {ref}`TRSGameTabs` variable.
285+
*)
218286
GameTabs: TRSGameTabs;
287+
288+
begin
289+
GameTabs.KeybindsEnabled := True;
290+
GameTabs.AvailableKeys := [
291+
EKeyCode.ESCAPE, EKeyCode.F1, EKeyCode.F2, EKeyCode.F3, EKeyCode.F4,
292+
EKeyCode.F5, EKeyCode.F6, EKeyCode.F7, EKeyCode.F8, EKeyCode.F9,
293+
EKeyCode.F10, EKeyCode.F11, EKeyCode.F12
294+
];
295+
end;

osrs/interfaces/gametabs/magic.simba

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ WriteLn Magic.IsSpellBook(ERSSpellBook.LUNAR);
142142
*)
143143
function TRSMagic.IsSpellBook(book: ERSSpellBook): Boolean;
144144
begin
145-
Result := Self._IsSpellBook(book, GameTabs.Get(ERSGameTab.MAGIC).Expand(-5));
145+
Result := Self._IsSpellBook(book, GameTabs.Tabs[ERSGameTab.MAGIC].Expand(-5));
146146
end;
147147

148148
(*
@@ -162,7 +162,7 @@ var
162162
bounds: TBox;
163163
book: ERSSpellBook;
164164
begin
165-
bounds := GameTabs.Get(ERSGameTab.MAGIC).Expand(-5);
165+
bounds := GameTabs.Tabs[ERSGameTab.MAGIC].Expand(-5);
166166
for book := ERSSpellBook.STANDARD to High(ERSSpellBook) do
167167
if Self._IsSpellBook(book, bounds) then
168168
Exit(book);

osrs/overrides.simba

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function TRSGameTabs.GetCurrent(): ERSGameTab; override;
1111
begin
1212
Result := inherited;
1313
if Result <> ERSGameTab.NONE then Exit;
14+
15+
if Bank.IsOpen() then Exit(ERSGameTab.INVENTORY);
16+
1417
if not Options.IsHouseOptionsOpen() then Exit;
1518
Result := ERSGameTab.OPTIONS;
1619
if RSMouseZoom.SettingChecked then Exit;
@@ -25,14 +28,3 @@ begin
2528
Options.ZoomLevel := -1;
2629
end;
2730
end;
28-
29-
function TRSGameTabs.Open(tab: ERSGameTab): Boolean; override;
30-
begin
31-
if Bank.IsOpen() then
32-
begin
33-
if tab = ERSGameTab.INVENTORY then Exit(True);
34-
Bank.Close();
35-
end;
36-
37-
Result := inherited();
38-
end;

tests/003.simba

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ begin
4040

4141
Assert(BankPin.IsOpen() = False);
4242
Assert(Bank.IsOpen());
43+
Assert(Inventory.IsOpen());
4344
Assert(Bank.CountTabs() = 9);
4445
Assert(Bank.GetCurrentTab() = 5);
4546
Assert(Bank.ClearSearch());

0 commit comments

Comments
 (0)