Twine Version: 2.8.1
Hello, I’m developing an open world text based game where travel is mainly done between settlements (nodes) on a map. I’m trying to figure out how to make it so I can choose a destination node and calculate the shortest path from the current node to there. I think without this feature travel will be a huge hassle, so it’s high on the priority list.
The problem is I’m not much of a math guy, or a coder, so I’m really struggling to understand how to implement this. I only learned about the Dijkstra algorithm (which is what games use to accomplish this) while trying to implement this, but I don’t really understand it well enough to code it. I’ve tried ChatGPT help or studying the algorithm step by step, but I just don’t get it. Also, I don’t know javascript, so I’ve been trying to do it solely with TwineScript.
So, it’s a bit of an ask, but can someone help me implement this? It seems to me to be the most ‘real code’ part of my game.
Here’s where I’m at right now. I’m initializing the map, starting small with example A-E names until I can get this working. This includes connections between nodes and distances.
<<set $currentPlace = "Aavros">>
<<set $destination = "Evaris">>
<<set $shortestPath = []>>
<<set $places to {
"Aavros": { "neighbors": {"Bental": 1, "Centos": 4}},
"Bental": { "neighbors": {"Aavros": 1, "Centos": 2, "Dijkstra": 5}},
"Centos": { "neighbors": {"Aavros": 4, "Bental": 2, "Dijkstra": 1, "Evaris": 3}},
"Dijkstra": { "neighbors": {"Bental": 5, "Centos": 1, "Evaris": 1}},
"Evaris": { "neighbors": {"Centos": 3, "Dijkstra": 1}}
}>>
And that’s really all I’ve got. I tried multiple ways to implement the Dijkstra algorithm into a widget according to what ChatGPT would say, but of course it was riddled with errors and I didn’t have enough of a grasp of what was going on to fix them.
Here’s a graph I put together while trying to figure it out, if that might help someone put this together.
I’d appreciate any assistance.