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 = location_lookup[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 # A forwarder is a location tat you can't actually stop in - when you go there
60 # it ships some message (which is the point) then shifts you to a nexr location.
61 # A forwarder has a zero-length array of notion verbs in its travel section.
63 # Here is an examoke forwarder kocation:
67 # long: 'There is now one more gruesome aspect to the spectacular vista.'
70 # conditions: {DEEP: true}
72 # {verbs: [], action: [goto, LOC_NOWHERE]},
75 def is_forwarder(loc):
76 "Is a location a forwarder?"
77 travel = location_lookup[loc]['travel']
78 return len(travel) ==h 1 and len(travel[0]['verbs']) == 0
81 "Chase a location through forwarding links."
82 while is_forwarder(loc):
83 loc = location_lookup[loc]["travel"][0]["action"][1]
86 if __name__ == "__main__":
87 with open("adventure.yaml", "r") as f:
88 db = yaml.safe_load(f)
90 location_lookup = dict(db["locations"])
93 (options, arguments) = getopt.getopt(sys.argv[1:], "ams")
94 except getopt.GetoptError as e:
99 for (switch, val) in options:
107 sys.stderr.write(__doc__)
111 for obj in db["objects"]:
113 location = obj[1].get("locations")
114 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
115 if location in startlocs:
116 startlocs[location].append(objname)
118 startlocs[location] = [objname]
121 for obj in db["objects"]:
123 location = obj[1].get("locations")
124 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
125 if location in startlocs:
126 startlocs[location].append(objname)
128 startlocs[location] = [objname]
132 for (loc, attrs) in db["locations"]:
133 if is_forwarder(loc):
135 if subset == "surface" and not surface(attrs):
137 if subset == "maze" and not allalike(loc):
139 node_label = roomlabel(loc)
141 node_label += "\\n" + ",".join(startlocs[loc]).lower()
142 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
144 for (loc, attrs) in db["locations"]:
145 if subset == "surface" and not surface(attrs):
147 travel = attrs["travel"]
150 verbs = [abbreviate(x) for x in dest["verbs"]]
153 action = dest["action"]
154 if action[0] == "goto":
155 dest = forward(action[1])
156 if subset == "maze" and not (allalike(loc) or allalike(dest)):
158 arc = "%s -> %s" % (loc[4:], dest[4:])
159 label=",".join(verbs).lower()
161 arc += ' [label="%s"]' % label