Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 578a52d

Browse files
authored
Add an a* pathfinder (tgstation#113)
Returns the shortest path in a static node map. That is made for TGMC wich uses manually placed nodes for pathfinding. Benchmark : image That's the average number of path computed in one second, out of 30 runs with random nodes. Size of the node map is roughly 800 On average 32 times faster than tgmc a* implementation. Another possible comparison is with TG's JPS system, and according to the benchmark done here it's 7000 times faster (tgstation/tgstation#56780). Not exactly the same use cases though
1 parent 4af771a commit 578a52d

File tree

6 files changed

+384
-0
lines changed

6 files changed

+384
-0
lines changed

Cargo.lock

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ toml-dep = { version = "0.5.8", package="toml", optional = true }
4646
aho-corasick = { version = "0.7.18", optional = true}
4747
rayon = { version = "1.5", optional = true}
4848
dbpnoise = { version = "0.1.2", optional = true}
49+
pathfinding = { version = "3.0.13", optional = true }
50+
num = { version = "0.4.0", optional = true }
4951

5052
[features]
5153
default = ["acreplace", "cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "time", "toml", "url"]
@@ -67,6 +69,7 @@ url = ["url-dep", "percent-encoding"]
6769
# additional features
6870
batchnoise = ["dbpnoise"]
6971
hash = ["base64", "const-random", "md-5", "hex", "sha-1", "sha2", "twox-hash", "serde", "serde_json"]
72+
pathfinder = [ "num", "pathfinding", "serde", "serde_json"]
7073
redis_pubsub = ["flume", "redis", "serde", "serde_json"]
7174
unzip = ["zip", "jobs"]
7275
worleynoise = ["rand","rayon"]

dmsrc/pathfinder.dm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Register a list of nodes into a rust library. This list of nodes must have been serialized in a json.
3+
* Node {// Index of this node in the list of nodes
4+
* unique_id: usize,
5+
* // Position of the node in byond
6+
* x: usize,
7+
* y: usize,
8+
* z: usize,
9+
* // Indexes of nodes connected to this one
10+
* connected_nodes_id: Vec<usize>}
11+
* It is important that the node with the unique_id 0 is the first in the json, unique_id 1 right after that, etc.
12+
* It is also important that all unique ids follow. {0, 1, 2, 4} is not a correct list and the registering will fail
13+
* Nodes should not link across z levels.
14+
* A node cannot link twice to the same node and shouldn't link itself either
15+
*/
16+
#define rustg_register_nodes_astar(json) call(RUST_G, "register_nodes_astar")(json)
17+
18+
/**
19+
* Add a new node to the static list of nodes. Same rule as registering_nodes applies.
20+
* This node unique_id must be equal to the current length of the static list of nodes
21+
*/
22+
#define rustg_add_node_astar(json) call(RUST_G, "add_node_astar")(json)
23+
24+
/*
25+
* Remove every link to the node with unique_id. Replace that node by null
26+
*/
27+
#define rustg_remove_node_astart(unique_id) call(RUST_G, "remove_node_astar")(unique_id)
28+
29+
/**
30+
* Compute the shortest path between start_node and goal_node using A*. Heuristic used is simple geometric distance
31+
*/
32+
#define rustg_generate_path_astar(start_node_id, goal_node_id) call(RUST_G, "generate_path_astar")(start_node_id, goal_node_id)

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub mod json;
3030
pub mod log;
3131
#[cfg(feature = "noise")]
3232
pub mod noise_gen;
33+
#[cfg(feature = "pathfinder")]
34+
pub mod pathfinder;
3335
#[cfg(feature = "redis_pubsub")]
3436
pub mod redis_pubsub;
3537
#[cfg(feature = "sql")]

0 commit comments

Comments
 (0)