Skip to content

Commit 4e76f46

Browse files
committed
fix(webwalking): read notes
- nodes now also have a check function to check if the node should be available during path finding, this should be functions that are very fast, what's currently on webgraph_callbacks is just a proof of concept - fixed a couple of bugs in the new webgraph logic - disabled range checks for better performance
1 parent 74290d8 commit 4e76f46

File tree

3 files changed

+84
-37
lines changed

3 files changed

+84
-37
lines changed

osrs/position/map/webgraph_callbacks.simba

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ type
1111
Position: TWalkerPositionFunction;
1212
end;
1313

14-
function TWebGraphCallbacks.TeleportGrandExchange(): Boolean;
14+
function TWebGraphCallbacks.CheckTeleportGrandExchange(): Boolean;
15+
begin
16+
if Stats.GetLevel(ERSSkill.MAGIC) < 25 then Exit;
17+
if Achievements.Diaries.GetLevel(ERSAchievementDiary.VARROCK) < 2 then Exit;
18+
Result := Magic.ContainsSpell(ERSSpell.VARROCK_TELEPORT);
19+
end;
20+
21+
function TWebGraphCallbacks.DoTeleportGrandExchange(): Boolean;
1522
begin
1623
if Magic.CastSpell(ERSSpell.VARROCK_TELEPORT, 'Grand Exchange') then
1724
Result := SleepUntil(Self.Position().InRange([8565, 36522], 60), 300, 5000);
1825
end;
1926

20-
function TWebGraphCallbacks.TeleportVarrock(): Boolean;
27+
function TWebGraphCallbacks.CheckTeleportVarrock(): Boolean;
28+
begin
29+
if Stats.GetLevel(ERSSkill.MAGIC) < 25 then Exit;
30+
Result := Magic.Open() and Magic.ContainsSpell(ERSSpell.VARROCK_TELEPORT);
31+
end;
32+
33+
34+
function TWebGraphCallbacks.DoTeleportVarrock(): Boolean;
2135
begin
2236
if Magic.CastSpell(ERSSpell.VARROCK_TELEPORT, 'Cast') then
2337
Result := SleepUntil(Self.Position().InRange([8752, 36716], 60), 300, 5000);
@@ -29,18 +43,11 @@ begin
2943
case chunk of
3044
[49,54]:
3145
begin
32-
if Stats.GetLevel(ERSSkill.MAGIC) < 25 then
33-
Exit;
34-
if Achievements.Diaries.GetLevel(ERSAchievementDiary.VARROCK) < 2 then
35-
Exit;
36-
37-
//graph^.AddTeleport([8565, 36522], @Self.TeleportGrandExchange);
46+
//graph^.AddTeleport([8565, 36522], @Self.CheckTeleportGrandExchange, @Self.DoTeleportGrandExchange);
3847
end;
3948
[50,53]:
4049
begin
41-
if Stats.GetLevel(ERSSkill.MAGIC) < 25 then
42-
Exit;
43-
graph^.AddTeleport([8752, 36716], @Self.TeleportVarrock);
50+
graph^.AddTeleport([8752, 36716], @Self.CheckTeleportVarrock, @Self.DoTeleportVarrock);
4451
end;
4552
end;
4653
end;

osrs/walker.simba

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -898,25 +898,48 @@ begin
898898

899899
Self._Index := nearest;
900900

901-
if Self._Path[Self._Index].Typ = EGraphNode.TELEPORT then
901+
while nearest < High(Self._Path) do
902902
begin
903-
index := Self._Index;
904-
Self._Node := EGraphNode.TELEPORT;
905-
Exit(nearest > previous);
906-
end;
903+
if Self._Path[nearest].Typ = EGraphNode.TELEPORT then
904+
begin
905+
index := nearest;
906+
Self._Node := EGraphNode.TELEPORT;
907+
Exit(nearest > previous);
908+
end;
907909

908-
while (nearest < High(Self._Path)) and not Self.IsWalkable(Self._Path[nearest+1].Node, me, tmp, angle) do
910+
if Self.IsWalkable(Self._Path[nearest+1].Node, me, tmp, angle) then
911+
Break;
909912
Inc(nearest);
913+
end;
910914

911915
furthest := nearest;
912916
if Self._Path[furthest].Typ = EGraphNode.NORMAL then
913917
begin
914-
while (furthest < High(Self._Path)) and Self.IsWalkable(Self._Path[furthest+1].Node, me, tmp, angle) do
918+
while furthest < High(Self._Path) do
919+
begin
920+
if Self._Path[furthest].Typ = EGraphNode.TELEPORT then
921+
begin
922+
index := furthest;
923+
Self._Node := EGraphNode.TELEPORT;
924+
Exit(nearest > previous);
925+
end;
926+
927+
if Self._Path[furthest+1].Typ = EGraphNode.TELEPORT then
928+
begin
929+
index := furthest+1;
930+
Self._Node := EGraphNode.TELEPORT;
931+
Exit(nearest > previous);
932+
end;
933+
934+
if not Self.IsWalkable(Self._Path[furthest+1].Node, me, tmp, angle) then
935+
Break;
936+
915937
if (Self._Path[Inc(furthest)].Typ <> EGraphNode.NORMAL) then
916938
begin
917939
Self._Node := Self._Path[furthest].Typ;
918940
Break;
919941
end;
942+
end;
920943
end
921944
else
922945
Self._Node := Self._Path[furthest].Typ;
@@ -991,7 +1014,7 @@ Walker.WalkPath([[100,100],[120,120],[140,140],[160,160],[180,180]]);
9911014
function TRSWalker.WalkPath(path: TGraphNodeArray; minDist: Integer = 0; debug: Boolean = False): Boolean;
9921015
var
9931016
me: TPoint;
994-
i, idx, fails: Integer;
1017+
idx, fails: Integer;
9951018
radians: Double;
9961019
begin
9971020
Self._WalkPathSetup(path);

utils/webgraph.simba

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ but it was written from scratch by Torwent.
99
{$DEFINE WL_WEBGRAPH_INCLUDED}
1010
{$INCLUDE_ONCE WaspLib/utils.simba}
1111

12-
{$R+}
12+
{$R-}
1313
type
1414
(*
1515
## EGraphNode
@@ -26,18 +26,19 @@ Enum representing the type of nodes in a `TWebGraph`.
2626
BLOCKED
2727
);
2828

29-
TGraphNodeHandler = function (): Boolean of object;
29+
TGraphNodeFunction = function (): Boolean of object;
3030

3131
TGraphNode = record
3232
Node: TPoint;
3333
Typ: EGraphNode;
34-
Handle: TGraphNodeHandler;
34+
Check, Handle: TGraphNodeFunction;
3535
end;
3636

37-
function TGraphNode.Create(pt: TPoint; typ: EGraphNode = EGraphNode.NORMAL; handle: TGraphNodeHandler = nil): TGraphNode; static;
37+
function TGraphNode.Create(pt: TPoint; typ: EGraphNode = EGraphNode.NORMAL; check, handle: TGraphNodeFunction = nil): TGraphNode; static;
3838
begin
3939
Result.Node := pt;
4040
Result.Typ := typ;
41+
Result.Check := @check;
4142
Result.Handle := @handle;
4243
end;
4344

@@ -165,7 +166,7 @@ begin
165166
Result.Sort(weights, True);
166167
end;
167168

168-
{$R+}
169+
{.$R+}
169170

170171
function TWebGraph.FindPath(start, finish: Integer; rnd: Double = 0): TIntegerArray;
171172
type TNode = record Indices: TIntegerArray; Score: Double; end;
@@ -212,19 +213,36 @@ begin
212213
p := Self.Nodes[cIdx].Node;
213214
for pathIdx in Self.Paths[cIdx] do
214215
begin
215-
if not visited[pathIdx] then
216-
begin
217-
q := Self.Nodes[pathIdx].Node;
218-
node.Indices := current.Indices + pathIdx;
216+
if visited[pathIdx] then
217+
Continue;
219218

220-
hyp := Hypot(p.X-q.X, p.Y-q.Y);
219+
q := Self.Nodes[pathIdx].Node;
220+
node.Indices := current.Indices + pathIdx;
221+
222+
hyp := Hypot(p.X-q.X, p.Y-q.Y);
223+
224+
if @Self.Nodes[pathIdx].Check = nil then
225+
begin
221226
case Self.Nodes[pathIdx].Typ of
222227
EGraphNode.NORMAL: node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 1;
228+
EGraphNode.DOOR: node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 10;
229+
else node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 100;
230+
end;
231+
queue += node;
232+
Continue;
233+
end;
234+
235+
if not Self.Nodes[pathIdx].Check() then
236+
node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 100
237+
else
238+
begin
239+
case Self.Nodes[pathIdx].Typ of
240+
EGraphNode.DOOR: node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 10;
223241
EGraphNode.TELEPORT, EGraphNode.FAIRYRING: node.Score := current.Score + hyp*0.9 + 5;
224242
else node.Score := current.Score + hyp + (hyp*Random()*rnd-rnd/2) + 10;
225243
end;
226-
queue += node;
227244
end;
245+
queue += node;
228246
end;
229247
end;
230248
end;
@@ -245,7 +263,6 @@ begin
245263
if nS[0] = nG[0] then
246264
Exit([nodeA, nodeB]);
247265

248-
249266
if (Length(nG) = 1) and (Self.Paths[nG[0]] = []) then
250267
raise GetDebugLn('WebGraph', 'Points ' + ToStr(a) + ' and ' + ToStr(b) + ' don''t connect! No paths available.');
251268

@@ -413,7 +430,7 @@ begin
413430
end;
414431

415432

416-
procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handle: TGraphNodeHandler);
433+
procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handle: TGraphNodeFunction);
417434
var
418435
i, j, n: Integer;
419436
tpa: TPointArray;
@@ -451,7 +468,7 @@ begin
451468
end;
452469
end;
453470

454-
procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handleA, handleB: TGraphNodeHandler); overload;
471+
procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handleA, handleB: TGraphNodeFunction); overload;
455472
var
456473
i, j, n: Integer;
457474
tpa: TPointArray;
@@ -489,7 +506,7 @@ begin
489506
end;
490507
end;
491508

492-
procedure TWebGraph.AddLink(pt, a, b: TPoint; typ: EGraphNode; handle: TGraphNodeHandler); overload;
509+
procedure TWebGraph.AddLink(pt, a, b: TPoint; typ: EGraphNode; handle: TGraphNodeFunction); overload;
493510
var
494511
i, j, n: Integer;
495512
tpa: TPointArray;
@@ -525,13 +542,13 @@ begin
525542
Self.Nodes[i] := TGraphNode.Create(pt, typ, @handle);
526543
end;
527544

528-
procedure TWebGraph.AddTeleport(pt: TPoint; handle: TGraphNodeHandler);
545+
procedure TWebGraph.AddTeleport(pt: TPoint; check, handle: TGraphNodeFunction);
529546
var
530547
i, j, n: Integer;
531548
tpa: TPointArray;
532549
begin
533550
i := Length(Self.Nodes);
534-
Self.Nodes += [pt, EGraphNode.TELEPORT, @handle];
551+
Self.Nodes += TGraphNode.Create(pt, EGraphNode.TELEPORT, @check, @handle);
535552
SetLength(Self.Paths, Length(Self.Nodes));
536553

537554
tpa := Self.Tree.KNearest(pt, 12);
@@ -560,7 +577,7 @@ var
560577
tpa: TPointArray;
561578
begin
562579
i := Length(Self.Nodes);
563-
Self.Nodes += [pt, EGraphNode.FAIRYRING, nil];
580+
Self.Nodes += TGraphNode.Create(pt, EGraphNode.FAIRYRING, nil);
564581
SetLength(Self.Paths, Length(Self.Nodes));
565582

566583
tpa := Self.Tree.KNearest(pt, 8);

0 commit comments

Comments
 (0)