3 usage: make-graph.py [-a] [-m] [-s]
5 Make a DOT graph of Colossal Cave
7 -a = emit graph of entire dungeon
8 -m = emit graph of maze all alike
9 -s = emit graph of surface locations
11 # Copyright (c) 2017 by Eric S. Raymond
12 # SPDX-License-Identifier: BSD-2-clause
14 import sys, yaml, getopt
17 "Select out loci related to the Maze All Alike"
18 return ("ALIKE" in loc) or (loc == "LOC_PITBRINK") or ("MAZEEND" in loc) or ("STALACTITE" in loc)
21 "Select out surface locations"
22 if ("ABOVE" in attrs["conditions"]) and attrs["conditions"]["ABOVE"]:
24 if ("FOREST" in attrs["conditions"]) and attrs["conditions"]["FOREST"]:
29 m = {"NORTH":"N", "EAST":"E", "SOUTH":"S", "WEST":"W", "UPWAR":"U", "DOWN":"D"}
33 "Generate a room label from the description, if possible"
34 loc_descriptions = dict(db["locations"])[loc]['description']
36 short = loc_descriptions["short"]
37 maptag = loc_descriptions["maptag"]
39 if short.startswith("You're "):
41 if short.startswith("You are "):
43 if short.startswith("in ") or short.startswith("at ") or short.startswith("on "):
45 if short[:3] in {"n/s", "e/w"}:
46 short = short[:3].upper() + short[3:]
47 elif short[:2] in {"ne", "sw", "se", "nw"}:
48 short = short[:2].upper() + short[2:]
50 short = short[0].upper() + short[1:]
51 elif loc_descriptions["maptag"] is not None:
52 short = loc_descriptions["maptag"]
53 elif loc_descriptions["long"] is not None and len(loc_descriptions["long"]) < 20:
54 short = loc_descriptions["long"]
56 description += "\\n" + short
59 if __name__ == "__main__":
60 with open("adventure.yaml", "r") as f:
61 db = yaml.safe_load(f)
64 (options, arguments) = getopt.getopt(sys.argv[1:], "ams")
65 except getopt.GetoptError as e:
70 for (switch, val) in options:
78 sys.stderr.write(__doc__)
82 for obj in db["objects"]:
84 location = obj[1].get("locations")
85 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
86 if location in startlocs:
87 startlocs[location].append(objname)
89 startlocs[location] = [objname]
92 for obj in db["objects"]:
94 location = obj[1].get("locations")
95 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
96 if location in startlocs:
97 startlocs[location].append(objname)
99 startlocs[location] = [objname]
103 for (loc, attrs) in db["locations"]:
104 if subset == "surface" and not surface(attrs):
106 if subset == "maze" and not allalike(loc):
108 node_label = roomlabel(loc)
110 node_label += "\\n" + ",".join(startlocs[loc]).lower()
111 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
113 for (loc, attrs) in db["locations"]:
114 if subset == "surface" and not surface(attrs):
116 travel = attrs["travel"]
119 verbs = [abbreviate(x) for x in dest["verbs"]]
122 action = dest["action"]
123 if action[0] == "goto":
125 if subset == "maze" and not (allalike(loc) or allalike(dest)):
127 arc = "%s -> %s" % (loc[4:], dest[4:])
128 label=",".join(verbs).lower()
130 arc += ' [label="%s"]' % label