@@ -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- }
1313type
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;
3838begin
3939 Result.Node := pt;
4040 Result.Typ := typ;
41+ Result.Check := @check;
4142 Result.Handle := @handle;
4243end;
4344
@@ -165,7 +166,7 @@ begin
165166 Result.Sort(weights, True);
166167end;
167168
168- {$R+}
169+ {. $R+}
169170
170171function TWebGraph.FindPath(start, finish: Integer; rnd: Double = 0): TIntegerArray;
171172type 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;
230248end;
@@ -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
413430end;
414431
415432
416- procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handle: TGraphNodeHandler );
433+ procedure TWebGraph.AddLink(a, b: TPoint; typ: EGraphNode; handle: TGraphNodeFunction );
417434var
418435 i, j, n: Integer;
419436 tpa: TPointArray;
@@ -451,7 +468,7 @@ begin
451468 end;
452469end;
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;
455472var
456473 i, j, n: Integer;
457474 tpa: TPointArray;
@@ -489,7 +506,7 @@ begin
489506 end;
490507end;
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;
493510var
494511 i, j, n: Integer;
495512 tpa: TPointArray;
@@ -525,13 +542,13 @@ begin
525542 Self.Nodes[i] := TGraphNode.Create(pt, typ, @handle);
526543end;
527544
528- procedure TWebGraph.AddTeleport(pt: TPoint; handle: TGraphNodeHandler );
545+ procedure TWebGraph.AddTeleport(pt: TPoint; check, handle: TGraphNodeFunction );
529546var
530547 i, j, n: Integer;
531548 tpa: TPointArray;
532549begin
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);
560577 tpa: TPointArray;
561578begin
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