Improve the maze graph.
[open-adventure.git] / make_graph.py
1 #!/usr/bin/env python3
2
3 # Make a DOT graph of the dungeon
4 #
5 # Copyright (c) 2017 by Eric S. Raymond
6 # SPDX-License-Identifier: BSD-2-clause
7
8 import sys, yaml
9
10 def allalike(loc, dest):
11     "Select out loci related to the Maze All Alike"
12     return ("ALIKE" in loc) or ("MAZEEND" in loc) or ("STALACTITE" in loc) or (loc == "LOC_MISTWEST" and "ALIKE" in dest) 
13
14 def abbreviate(d):
15     m = {"NORTH":"N", "EAST":"E", "SOUTH":"S", "WEST":"W", "UPWAR":"U", "DOWN":"D"}
16     return m.get(d, d)
17
18 if __name__ == "__main__":
19     with open("adventure.yaml", "r") as f:
20         db = yaml.safe_load(f)
21
22     print("digraph G {")
23     for (loc, attrs) in db["locations"]:
24         travel = attrs["travel"]
25         if len(travel) > 0:
26             for dest in travel:
27                 verbs = [abbreviate(x) for x in dest["verbs"]]
28                 if len(verbs) == 0:
29                     continue
30                 action = dest["action"]
31                 if action[0] == "goto":
32                     dest = action[1]
33                     if not allalike(loc, dest):
34                         continue;
35                     arc = "%s -> %s" % (loc[4:], dest[4:])
36                     label=",".join(verbs).lower()
37                     if len(label) > 0:
38                         arc += ' [label="%s"]' % label
39                     print("    " + arc)
40     print("}")
41
42