WebGraph

Responsible for handling webwalking or any other graph logic you may need.

It’s partially based on the original webgraph logic by slacky but it was written from scratch by Torwent.

It’s important to note that most types and methods are for internal usage and you shouldn’t really have to interact with it.


type EGraphNode

EGraphNode = enum(NORMAL, DOOR, STAIRS, SHORTCUT, OBSTACLE, TRANSPORT, TELEPORT, FAIRYRING, BLOCKED);

Enum representing the type of nodes in a TWebGraph.


type TGraphNodeFunction

Boolean method type for a type TGraphNode Check and Handle functions.

These are functions a type TGraphNode can have to:

  • Check to check if we can go through the node, e.g. check a level requirement

  • Handle to handle going through the node, e.g. cast a teleport.

Here is a simple example of a Check and a Handle functions for a falador teleport:

function TTransporter.CheckTeleportFalador(): Boolean;
begin
  if Stats.GetLevel(ERSSkill.MAGIC) < 31 then Exit;
  Result := Magic.Open() and Magic.ContainsSpell(ERSSpell.FALADOR_TELEPORT);
end;

function TTransporter.DoTeleportFalador(): Boolean;
begin
  if Magic.CastSpell(ERSSpell.FALADOR_TELEPORT, 'Cast') then
    Result := SleepUntil(Self.Position().InRange([8752, 36716], 60), 300, 5000);
end;

type TGraphNode

Type representing a type TWebGraph node.


TGraphNode.Create

function TGraphNode.Create(pt: TPoint; typ: EGraphNode = EGraphNode.NORMAL; check, handle: TGraphNodeFunction = nil): TGraphNode; static;

Function to create a type TGraphNode.


type TWebGraph

Type representing a webgraph, for more information on what a graph is in this context visit this wikipedia page


TWebGraph.Setup

procedure TWebGraph.Setup();

It sets up TWebGraph.Tree, a TKDPointTree used to compare distances between nodes very fast.


TWebGraph.Copy

function TWebGraph.Copy(): TWebGraph;

Performs a unique copy of the TWebGraph which you can modify without modifying the original. Useful for debugging graphs when you need to keep the original unchanged, e.g. with the Map Debugger.


TWebGraph.BlockArea

procedure TWebGraph.BlockArea(area: TBox; inside: Boolean = True);

Blocks an area of the TWebGraph so no nodes in the specified area are returned when finding paths.

For example, this is how you could block the space where wintertodt is so you don’t get paths through the chasm:

Map.Setup([ERSChunk.WINTERTODT]);
Map.Loader.Graph.BlockArea([6478, 34358, 6565, 34445]);

TWebGraph.NearNodesIndices

function TWebGraph.NearNodesIndices(pt: TPoint; amount: Integer): TIntegerArray;

Returns the indices of the amount of nearest nodes to pt on the webgraph that can be walked to to and from pt.

The result returned is sorted by shortest distance to pt.


TWebGraph.FindPath

function TWebGraph.FindPath(start, finish: Integer; rnd: Double = 0): TIntegerArray;

Finds a path between start and finish with the ability to introduce some randomness through rnd.

This is basically a weighted BFS algorithm.

For more information on the algorithm read the wikipedia page on breadth-first search.


TWebGraph.PathBetween

function TWebGraph.PathBetween(a, b: TPoint; rnd: Double = 0; attempts: Integer = 3): TGraphNodeArray;

Returns a path between a and b with the ability to introduce some randomness through rnd.

The returned path comes in the form of a TGraphNodeArray.


TWebGraph.PathToNear

function TWebGraph.PathToNear(a: TPoint; out b: TPoint; rnd: Double = 0): TGraphNodeArray;

Returns a path between a and and whatever the nearest node to b is with the ability to introduce some randomness through rnd.

The nearest node to b is also returned through b.

The returned path comes in the form of a TGraphNodeArray.


TWebGraph.AddNode

function TWebGraph.AddNode(pt: TPoint; typ: EGraphNode; fromNode: Integer): Boolean;

Adds a new node to the TWebGraph with a connection to fromNode.

You can specify it’s coordinate through pt and it’s type through typ.


TWebGraph.ConnectNodes

procedure TWebGraph.ConnectNodes(a,b: Integer);
procedure TWebGraph.ConnectNodes(a,b: TPoint); overload;
procedure TWebGraph.ConnectNodes(a: Integer; b: TPoint); overload;

Connects nodes a and b.

If either a or b are TPoints, they must match exactly the coordinates of a node in the graph.

Using indices is always better for performance but overtime they may change if the shape of the graph changes.


TWebGraph.ConnectNodesEx

procedure TWebGraph.ConnectNodesEx(a,b: TPoint);

Connects the closest node to a to the closest node to b.

This is similar to TWebGraph.ConnectNodes when using TPoints but unlike it, your points don’t have to be the exact same as the nodes in the graph.

Overall, this is the most flexible and easiest method to use but it might not connect exactly the nodes you want at times.


TWebGraph.DeleteNode

function TWebGraph.DeleteNode(node: Integer): Integer;

Deletes a TGraphNode from a TWebGraph along with all the connections to it.


TWebGraph.DeleteNodes

procedure TWebGraph.DeleteNodes(b: TBox);

It’s very similar to TWebGraph.BlockNodes but unlike it, what this does is permanent. Blocking nodes could be used to selectively toggle nodes on/off, this permanently removes them.

For example, this is how you could block the space where wintertodt is so you don’t get paths through the chasm:

Map.Setup([ERSChunk.WINTERTODT]);
Map.Loader.Graph.DeleteNodes([6478, 34358, 6565, 34445]);

TWebGraph.AddLinkEx

procedure TWebGraph.AddLinkEx(a, b: TPoint; typeA, typeB: EGraphNode; checkA, checkB, handleA, handleB: TGraphNodeFunction; connectA, connectB: Boolean);

Adds 2 nodes and a connection between them to the TWebGraph.

This differs from TWebGraph.ConnectNodes in that you can make the nodes have Check and Handle type TGraphNodeFunctions.



TWebGraph.AddUniLink

procedure TWebGraph.AddDualLink(a, b: TPoint; typ: EGraphNode; check, handle: TGraphNodeFunction);
procedure TWebGraph.AddDualLink(a, b: TPoint; typ: EGraphNode; checkA, checkB, handleA, handleB: TGraphNodeFunction); overload;

Wrapper around TWebGraph.AddLinkEx to add 2 nodes with a two way connection between a and b like an agility shortcut or a door for example.


TWebGraph.AddTeleport

procedure TWebGraph.AddTeleport(pt: TPoint; check, handle: TGraphNodeFunction);

Similar to TWebGraph.AddUniLink but made specifically for teleports.


TWebGraph.AddFairyRing

procedure TWebGraph.AddFairyRing(pt: TPoint);

Similar to TWebGraph.AddDualLink but made specifically for fairy rings.


TWebGraph.Merge

procedure TWebGraph.Merge(graph: TWebGraph);

Merges graph into the current TWebGraph.


TWebGraph.Draw

procedure TWebGraph.Draw(img: TImage);
procedure TWebGraph.Draw(canvas: TImageBoxCanvas); overload;

Used for TWebGraph debugging.