Skip to content

Commit 035c38c

Browse files
committed
fix(TRSWalker): GetClosestPoint improved logic
1 parent 9b667e2 commit 035c38c

File tree

5 files changed

+118
-66
lines changed

5 files changed

+118
-66
lines changed

osrs/interfaces/interfacecontrols.simba

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ WriteLn Chat.Scroll.CanScroll();
374374
*)
375375
function TRSScrollBar.CanScroll(): Boolean;
376376
begin
377-
Result := Self.IsVisible() and (Self.Slider <> Self.Bounds);
377+
if not Self.IsVisible() then
378+
Exit;
379+
380+
Result := (Abs(Self.Slider.Y1 - Self.Bounds.Y1) > 0) or (Abs(Self.Slider.Y2 - Self.Bounds.Y2) > 0);
378381
end;
379382

380383

osrs/position/map/entities.simba

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ these do not have a rotation field.
1818
{$INCLUDE_ONCE WaspLib/osrs.simba}
1919

2020
type
21+
ENPCData = enum(ID, NAME, LEVEL, CATEGORY, MINIMAPDOT, ACTIONS, SIZE, COORDINATES, COLORS);
22+
2123
(*
2224
## TRSEntity
2325
Main type to handle {ref}`RSEntity`.
@@ -44,7 +46,7 @@ Array of {ref}`TRSEntity`.
4446
## RSEntity.Create
4547
```pascal
4648
function TRSEntity.Create(walker: PRSWalker; size: TVector3; coordinates: TPointArray; uptext: TStringArray = []; dots: ERSMinimapDots = []): TRSEntity; static;
47-
function TRSEntity.Create(json: TJSONItem): TRSEntity; static; overload;
49+
function TRSEntity.Create(json: TJSONObject): TRSEntity; static; overload;
4850
```
4951
Createors to create your {ref}`TRSEntity`.
5052

@@ -95,7 +97,7 @@ begin
9597
Result.Filter += TRSDotFilter.Create([], TCircle.Create(pt.X, pt.Y, radius), True);
9698
end;
9799

98-
function TRSEntity.Create(json: TJSONItem): TRSEntity; static; overload;
100+
function TRSEntity.Create(json: TJSONObject): TRSEntity; static; overload;
99101
var
100102
i: Integer;
101103
colors: TColorArray;
@@ -105,29 +107,34 @@ begin
105107
if json.Typ <> EJSONType.OBJ then
106108
raise GetDebugLn('TRSEntity', 'JSON Object expected, got ' + ToStr(json.Typ) + '.');
107109

108-
if json.Item[1].AsString <> 'null' then
109-
Result.UpText := [json.Item[1].AsString];
110-
if json.Item[4].AsBool then //4 is minimapdot key in TRSMap jsons
110+
with json.Item[Ord(ENPCData.NAME)] do
111+
if AsString <> 'null' then
112+
Result.UpText := [AsString];
113+
114+
if json.Item[Ord(ENPCData.MINIMAPDOT)].AsBool then
111115
Result.MinimapDots := [ERSMinimapDot.NPC];
112116

113-
//6 is size key in TRSMap jsons
114-
Result.Size.X := Round(json.Item[6].Item[0].AsInt * 0.8, 2);
115-
Result.Size.Y := Round(json.Item[6].Item[1].AsInt * 0.8, 2);
116-
Result.Size.Z := Round(json.Item[6].Item[2].AsInt / 40, 2);
117+
with json.Item[Ord(ENPCData.SIZE)] do
118+
begin
119+
Result.Size.X := Item[0].AsInt * 0.8;
120+
Result.Size.Y := Item[1].AsInt * 0.8;
121+
Result.Size.Z := Item[2].AsInt / 40;
122+
end;
117123

118-
if Result.Size.Z = 0.0 then
124+
if Abs(Result.Size.Z) < 0.05 then
119125
Result.Size.Z := 3.0;
120126

121-
//7 is coordinates key in TRSMap jsons
122-
for i := 0 to json.Item[7].Count-1 do
123-
begin
124-
coord := json.Item[7].Item[i];
125-
Result.Coordinates += [coord.Item[0].AsInt, coord.Item[1].AsInt];
126-
Result.Filter += TRSDotFilter.Create([], TCircle.Create(coord.Item[0].AsInt, coord.Item[1].AsInt, 40), True);
127-
end;
128-
//8 is colors key in TRSMap jsons
129-
for i := 0 to json.Item[8].Count-1 do
130-
colors += json.Item[8].Item[i].AsInt;
127+
with json.Item[Ord(ENPCData.COORDINATES)] do
128+
for i := 0 to Count-1 do
129+
begin
130+
coord := Item[i];
131+
Result.Coordinates += [coord.Item[0].AsInt, coord.Item[1].AsInt];
132+
Result.Filter += TRSDotFilter.Create([], TCircle.Create(coord.Item[0].AsInt, coord.Item[1].AsInt, 40), True);
133+
end;
134+
135+
with json.Item[Ord(ENPCData.COLORS)] do
136+
for i := 0 to Count-1 do
137+
colors += Item[i].AsInt;
131138

132139
if colors <> [] then
133140
begin
@@ -138,7 +145,29 @@ begin
138145
Result.Walker := @Map.Walker;
139146
end;
140147

141-
function TRSEntityArray.Create(json: TJSONItem): TRSEntityArray; static;
148+
(*
149+
## RSEntityArray.Create
150+
```pascal
151+
function TRSEntityArray.Create(json: TJSONArray): TRSEntityArray; static;
152+
```
153+
Create function to build your {ref}`TRSObjectArray`.
154+
155+
This only accepts a `json` array and it expects a specific json structure which is
156+
the one that {ref}`Map JSONs` provide.
157+
158+
Example:
159+
```pascal
160+
{$I WaspLib/osrs.simba}
161+
162+
var
163+
objs: TRSObjectArray;
164+
begin
165+
Map.Setup([ERSChunk.VARROCK]);
166+
objs := TRSObject.Create(ObjectsJSON.GetByName('bank'));
167+
end;
168+
```
169+
*)
170+
function TRSEntityArray.Create(json: TJSONArray): TRSEntityArray; static;
142171
var
143172
i: Integer;
144173
begin

osrs/position/map/mapjson.simba

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,17 @@ end;
109109

110110

111111
(*
112-
## ObjectsJSON.Get
112+
## ObjectsJSON.GetByName
113113
```pascal
114-
function TMapObjectJSON.Get(name: String; amount: Integer = 0): TJSONItem;
114+
function TMapObjectJSON.GetByName(name: String; amount: Integer = 0): TJSONArray;
115115
```
116-
Returns a `TJSONItem` JSON array of a map object that matches the specified `name`.
117-
By default you get all objects that match the `name`, however,
118-
if you want you can specify how many you want through `amount`.
116+
Returns a `TJSONArray` of JSON Objects of a map object that matches the
117+
specified `name`.
118+
119+
By default you get all objects that match the `name`, however, if you want you
120+
can specify how many you want through `amount`.
119121
*)
120-
function TMapObjectJSON.GetByName(name: String; amount: Integer = 0): TJSONItem;
122+
function TMapObjectJSON.GetByName(name: String; amount: Integer = 0): TJSONArray;
121123
var
122124
i, found: Integer;
123125
begin
@@ -138,11 +140,11 @@ end;
138140
(*
139141
## ObjectsJSON.GetByID
140142
```pascal
141-
function TMapObjectJSON.GetByID(id: Integer): TJSONItem;
143+
function TMapObjectJSON.GetByID(id: Integer): TJSONObject;
142144
```
143-
Returns a `TJSONItem` JSON object of a map object that matches the specified `id`.
145+
Returns a `TJSONObject` of a map object that matches the specified `id`.
144146
*)
145-
function TMapObjectJSON.GetByID(id: Integer): TJSONItem;
147+
function TMapObjectJSON.GetByID(id: Integer): TJSONObject;
146148
var
147149
i: Integer;
148150
begin
@@ -157,14 +159,14 @@ end;
157159
(*
158160
## ObjectsJSON.GetByAction
159161
```pascal
160-
function TMapObjectJSON.GetByAction(action: String; amount: Integer = 0): TJSONItem;
162+
function TMapObjectJSON.GetByAction(action: String; amount: Integer = 0): TJSONArray;
161163
```
162-
Returns a `TJSONItem` JSON array of a map object that has an action that matches
163-
the specified `action`.
164+
Returns a `TJSONArray` of JSON Objects of a map object that has an action that
165+
matches the specified `action`.
164166
By default you get all objects that match the `action`, however, if you want
165167
you can specify how many you want through `amount`.
166168
*)
167-
function TMapObjectJSON.GetByAction(action: String; amount: Integer = 0): TJSONItem;
169+
function TMapObjectJSON.GetByAction(action: String; amount: Integer = 0): TJSONArray;
168170
var
169171
i, j, found: Integer;
170172
begin
@@ -176,7 +178,8 @@ begin
176178
if Self.Data.Item[i].Item[4].Item[j].AsString = action then
177179
begin
178180
Result.AddObject('', Self.Data.Item[i].Clone());
179-
if Inc(found) = amount then Exit;
181+
if Inc(found) = amount then
182+
Exit;
180183
Continue(2);
181184
end;
182185

osrs/position/map/objects.simba

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ game objects.
88
{$INCLUDE_ONCE WaspLib/osrs.simba}
99

1010
type
11+
EObjectData = enum(ID, NAME, TYP, CATEGORY, ACTIONS, COORDINATES, SIZE, ROTATIONS, COLORS);
12+
1113
(*
1214
## TRSObject
1315
Main type to handle {ref}`RSObjects`.
@@ -33,7 +35,7 @@ Array of {ref}`TRSObject`.
3335
## RSObject.Create
3436
```pascal
3537
function TRSObject.Create(walker: PRSWalker; size: TVector3; coordinates: TPointArray; uptext: TStringArray = []): TRSObject; static;
36-
function TRSObject.Create(json: TJSONItem): TRSObject; static; overload;
38+
function TRSObject.Create(json: TJSONObject): TRSObject; static; overload;
3739
```
3840
Createors to create your {ref}`TRSObject`.
3941

@@ -75,7 +77,7 @@ begin
7577
Result.Walker := walker;
7678
end;
7779

78-
function TRSObject.Create(json: TJSONItem): TRSObject; static; overload;
80+
function TRSObject.Create(json: TJSONObject): TRSObject; static; overload;
7981
var
8082
i: Integer;
8183
colors: TColorArray;
@@ -84,31 +86,33 @@ begin
8486
if json.Typ <> EJSONType.OBJ then
8587
raise GetDebugLn('TRSObject', 'JSON Object expected, got ' + ToStr(json.Typ) + '.');
8688

87-
if json.Item[1].AsString <> 'null' then
88-
Result.UpText := [json.Item[1].AsString];
89+
with json.Item[Ord(EObjectData.NAME)] do
90+
if AsString <> 'null' then
91+
Result.UpText := [AsString];
8992

90-
//5 is coordinates key in trsmap jsons
91-
for i := 0 to json.Item[5].Count-1 do
92-
Result.Coordinates += [json.Item[5].Item[i].Item[0].AsInt, json.Item[5].Item[i].Item[1].AsInt];
93+
with json.Item[Ord(EObjectData.COORDINATES)] do
94+
for i := 0 to Count-1 do
95+
Result.Coordinates += [Item[i].Item[0].AsInt, Item[i].Item[1].AsInt];
9396

94-
//6 is size key in trsmap jsons
95-
Result.Size.X := Round(json.Item[6].Item[0].AsInt * 0.8, 2);
96-
Result.Size.Y := Round(json.Item[6].Item[1].AsInt * 0.8, 2);
97-
Result.Size.Z := Round(json.Item[6].Item[2].AsInt / 40, 2);
97+
with json.Item[Ord(EObjectData.SIZE)] do
98+
begin
99+
Result.Size.X := Item[0].AsInt * 0.8;
100+
Result.Size.Y := Item[1].AsInt * 0.8;
101+
Result.Size.Z := Item[2].AsInt / 40;
102+
end;
98103

99-
if Result.Size.Z = 0.0 then
104+
if Abs(Result.Size.Z) < 0.05 then
100105
Result.Size.Z := 3.0;
101106

102-
//7 is rotations key in trsmap jsons
103-
for i := 0 to json.Item[7].Count-1 do
104-
Result.Rotations += json.Item[7].Item[i].AsInt;
107+
with json.Item[Ord(EObjectData.ROTATIONS)] do
108+
for i := 0 to Count-1 do
109+
Result.Rotations += Item[i].AsInt;
105110

106-
while Length(Result.Rotations) < Length(Result.Coordinates) do
107-
Result.Rotations += Result.Rotations[High(Result.Rotations)];
111+
SetLength(Result.Rotations, Length(Result.Coordinates));
108112

109-
//8 is colors key in trsmap jsons
110-
for i := 0 to json.Item[8].Count-1 do
111-
colors += json.Item[8].Item[i].AsInt;
113+
with json.Item[Ord(EObjectData.ROTATIONS)] do
114+
for i := 0 to Count-1 do
115+
colors += Item[i].AsInt;
112116

113117
if colors <> [] then
114118
begin
@@ -122,9 +126,9 @@ end;
122126
(*
123127
## RSObjectArray.Create
124128
```pascal
125-
function TRSObjectArray.Create(json: TJSONItem): TRSObjectArray; static; overload;
129+
function TRSObjectArray.Create(json: TJSONArray): TRSObjectArray; static; overload;
126130
```
127-
Createor function to build your {ref}`TRSObjectArray`.
131+
Create function to build your {ref}`TRSObjectArray`.
128132

129133
This only accepts a `json` array and it expects a specific json structure which is
130134
the one that {ref}`Map JSONs` provide.
@@ -141,7 +145,7 @@ begin
141145
end;
142146
```
143147
*)
144-
function TRSObjectArray.Create(json: TJSONItem): TRSObjectArray; static;
148+
function TRSObjectArray.Create(json: TJSONArray): TRSObjectArray; static;
145149
var
146150
i: Integer;
147151
begin

osrs/walker.simba

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,31 +1054,44 @@ end;
10541054
(*
10551055
## TRSWalker.GetClosestPoint
10561056
```pascal
1057-
function TRSWalker.GetClosestPointEx(me: TPoint; destinations: TPointArray): TPoint;
1058-
function TRSWalker.GetClosestPoint(destinations: TPointArray): TPoint;
1057+
function TRSWalker.GetClosestPoint(me: TPoint; destinations: TPointArray; out path: TGraphNodeArray): TPoint;
10591058
```
1060-
Method used to get the closest Point to the Player out of a TPA.
1059+
Method used to get the closest `destination` point to `me`.
10611060
*)
1062-
function TRSWalker.GetClosestPoint(me: TPoint; destinations: TPointArray; out path: TGraphNodeArray): TPoint; overload;
1061+
function TRSWalker.GetClosestPoint(me: TPoint; destinations: TPointArray; out path: TGraphNodeArray): TPoint;
10631062
var
10641063
shortPaths: array of TGraphNodeArray;
1065-
best, dist: Integer;
1064+
best, dist, i: Integer;
10661065
destination: TPoint;
1066+
pathDist: Double;
10671067
begin
10681068
me := Self.Position();
10691069
best := $FFFFFF;
10701070

10711071
for destination in destinations do
10721072
begin
1073+
if not me.InRange(destination, best) then
1074+
Continue;
1075+
10731076
try
10741077
path := Self.WebGraph^.PathBetween(me, destination, 0, 4);
10751078
except
10761079
Continue;
10771080
end;
10781081

1079-
if path = [] then Continue;
1082+
if path = [] then
1083+
Continue;
1084+
1085+
pathDist := 0;
1086+
for i := 0 to High(path)-1 do
1087+
begin
1088+
pathDist += path[i].Node.DistanceTo(path[i+1].Node);
1089+
if pathDist > best then
1090+
Continue;
1091+
end;
1092+
1093+
dist := Floor(pathDist);
10801094

1081-
dist := Floor(path.PolylineLength());
10821095
if dist < best then
10831096
begin
10841097
shortPaths := [path];

0 commit comments

Comments
 (0)