# WebGraph Responsible for handling webwalking or any other graph logic you may need. It's partially based on the original webgraph logic by [slacky](https://slacky.one/) 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. - - - (TWebGraph)= ## type TWebGraph Type representing a webgraph, for more information on what a graph is in this context visit this [wikipedia page](https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)) - - - ## TWebGraph.CRC ```pascal property TWebGraph.CRC: UInt32; ``` Returns a CRC signature of the `TWebGraph.Nodes` and `TWebGraph.WalkableClusters`. Nothing else is used for the CRC signature, this is just used so we know if we should generate as KDTree for this graph or load at an already cached one instead. - - - ## TWebGraph.GetKDItems ```pascal function TWebGraph.GetKDItems(offset: Itneger = 0): TKDItems; ``` Returns `TKDItems` to be used on a {ref}`TKDTree` which can be used to compare distances between nodes very fast. - - - ## TWebGraph.Setup ```pascal procedure TWebGraph.Setup(const kditems: TKDItems = []); ``` It sets up `TWebGraph.Tree`, a {ref}`TKDTree` used to compare distances between nodes very fast. - - - ## TWebGraph.Copy ```pascal 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 {ref}`Map Debugger`. - - - ## TWebGraph.BlockArea ```pascal 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: ```pascal Map.Setup([EChunk.WINTERTODT]); Map.Loader.Graph.BlockArea([6478, 34358, 6565, 34445]); ``` - - - ## TWebGraph.NearNodesIndices ```pascal 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 ```pascal 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](https://en.wikipedia.org/wiki/Breadth-first_search). - - - ## TWebGraph.PathBetween ```pascal function TWebGraph.PathBetween(a, b: TPoint; rnd: Double = 0; attempts: Integer = 3): TPointArray; ``` 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 `TPointArray`. - - - ## TWebGraph.NearestPathable ```pascal function TWebGraph.NearestPathable(me: TPoint; destinations: TPointArray): TPoint; ``` Returns the nearest point in `destinations` to `me` that has a valid path in between. - - - ## TWebGraph.AddNode ```pascal function TWebGraph.AddNode(pt: TPoint; fromNode: Integer): Boolean; ``` Adds a new node to the `TWebGraph` with a connection to `fromNode`. You can specify it's coordinate through `pt`. - - - ## TWebGraph.ConnectNodes ```pascal 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 ```pascal procedure TWebGraph.ConnectNodesEx(a,b: TPoint; notEqual: Boolean = False); ``` Connects the closest node to `a` to the closest node to `b`. This is similar to {ref}`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 ```pascal function TWebGraph.DeleteNode(node: Integer): Integer; ``` Deletes a `Node` from a `TWebGraph` along with all the connections to it. - - - ## TWebGraph.DeleteNodes ```pascal procedure TWebGraph.DeleteNodes(b: TBox); ``` It's very similar to {ref}`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: ```pascal Map.Setup([EChunk.WINTERTODT]); Map.Loader.Graph.DeleteNodes([6478, 34358, 6565, 34445]); ``` - - - ## TWebGraph.Merge ```pascal procedure TWebGraph.Merge(constref graph: TWebGraph); ``` Merges `graph` into the current `TWebGraph`. - - - ## TWebGraph.Offset ```pascal function TWebGraph.Offset(pt: TPoint): TWebGraph; ``` Returns an offset `TWebGraph` by the specified `pt`. - - - ## TWebGraph.MaxEdgeLength ```pascal function TWebGraph.MaxEdgeLength(): Integer; ``` Returns the length of the longest connection in the graph, measured as the largest of its horizontal and vertical spans. {ref}`TWebGraph.Draw` needs this to know how far outside of the region it's drawing a node can sit and still have a connection reaching into it. - - - ## TWebGraph.Draw ```pascal procedure TWebGraph.Draw(img: TImage); procedure TWebGraph.Draw(canvas: TImageBoxCanvas); overload; procedure TWebGraph.Draw(canvas: TImageBoxCanvas; view: TBox; margin: Integer); overload; ``` Used for `TWebGraph` debugging. The overload taking a `view` only draws what's inside it, which is how the {ref}`Map Debugger` keeps repaints cheap on large maps. `margin` must be at least {ref}`TWebGraph.MaxEdgeLength` or connections reaching into `view` from off screen nodes will be missing.