3 usage: make-graph.py [-a] -d] [-m] [-s]
5 Make a DOT graph of Colossal Cave.
7 -a = emit graph of entire dungeon
8 -d = emit graph of maze all different
9 -f = emit graph of forest locations
10 -m = emit graph of maze all alike
11 -s = emit graph of non-forest surface locations
12 -v = include internal symbols in room labels
14 # Copyright (c) 2017 by Eric S. Raymond
15 # SPDX-License-Identifier: BSD-2-clause
17 # pylint: disable=consider-using-f-string,line-too-long,invalid-name,missing-function-docstring,multiple-imports,redefined-outer-name
19 import sys, getopt, yaml
22 "Select out loci related to the Maze All Alike"
23 return location_lookup[loc]["conditions"].get("ALLALIKE")
25 def alldifferent(loc):
26 "Select out loci related to the Maze All Alike"
27 return location_lookup[loc]["conditions"].get("ALLDIFFERENT")
30 "Select out surface locations"
31 return location_lookup[loc]["conditions"].get("ABOVE")
34 return location_lookup[loc]["conditions"].get("FOREST")
37 m = {"NORTH":"N", "EAST":"E", "SOUTH":"S", "WEST":"W", "UPWAR":"U", "DOWN":"D"}
41 "Generate a room label from the description, if possible"
42 loc_descriptions = location_lookup[loc]['description']
46 longd = loc_descriptions["long"]
47 short = loc_descriptions["maptag"] or loc_descriptions["short"]
48 if short is None and longd is not None and len(longd) < 20:
49 short = loc_descriptions["long"]
51 if short.startswith("You're "):
53 if short.startswith("You are "):
55 if short.startswith("in ") or short.startswith("at ") or short.startswith("on "):
57 if short.startswith("the "):
59 if short[:3] in {"n/s", "e/w"}:
60 short = short[:3].upper() + short[3:]
61 elif short[:2] in {"ne", "sw", "se", "nw"}:
62 short = short[:2].upper() + short[2:]
64 short = short[0].upper() + short[1:]
69 description += "\\n(" + ",".join(startlocs[loc]).lower() + ")"
72 # A forwarder is a location that you can't actually stop in - when you go there
73 # it ships some message (which is the point) then shifts you to a next location.
74 # A forwarder has a zero-length array of notion verbs in its travel section.
76 # Here is an example forwarder declaration:
80 # long: 'There is now one more gruesome aspect to the spectacular vista.'
83 # conditions: {DEEP: true}
85 # {verbs: [], action: [goto, LOC_NOWHERE]},
88 def is_forwarder(loc):
89 "Is a location a forwarder?"
90 travel = location_lookup[loc]['travel']
91 return len(travel) == 1 and len(travel[0]['verbs']) == 0
94 "Chase a location through forwarding links."
95 while is_forwarder(loc):
96 loc = location_lookup[loc]["travel"][0]["action"][1]
100 "Should this object be revealed when mapping?"
101 if "OBJ_" in objname:
103 if objname == "VEND":
105 obj = object_lookup[objname]
106 return not obj.get("immovable")
108 if __name__ == "__main__":
109 with open("adventure.yaml", "r", encoding='ascii', errors='surrogateescape') as f:
110 db = yaml.safe_load(f)
112 location_lookup = dict(db["locations"])
113 object_lookup = dict(db["objects"])
116 (options, arguments) = getopt.getopt(sys.argv[1:], "adfmsv")
117 except getopt.GetoptError as e:
123 for (switch, val) in options:
125 subset = lambda loc: True
127 subset = alldifferent
137 sys.stderr.write(__doc__)
141 for obj in db["objects"]:
143 location = obj[1].get("locations")
144 if location != "LOC_NOWHERE" and reveal(objname):
145 if location in startlocs:
146 startlocs[location].append(objname)
148 startlocs[location] = [objname]
150 # Compute reachability, using forwards.
151 # Dictionary key is (from, to) iff its a valid link,
152 # value is corresponding motion verbs.
155 for (loc, attrs) in db["locations"]:
157 travel = attrs["travel"]
160 verbs = [abbreviate(x) for x in dest["verbs"]]
163 action = dest["action"]
164 if action[0] == "goto":
165 dest = forward(action[1])
166 if not (subset(loc) or subset(dest)):
168 links[(loc, dest)] = verbs
173 if f == 'LOC_NOWHERE' or t == 'LOC_NOWHERE':
175 if (f == loc and subset(t)) or (t == loc and subset(f)):
176 if loc not in neighbors:
182 if not is_forwarder(loc):
183 node_label = roomlabel(loc)
185 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
186 elif loc in neighbors:
187 print(' %s [label="%s"]' % (loc[4:], node_label))
191 arc = "%s -> %s" % (f[4:], t[4:])
192 label=",".join(links[(f, t)]).lower()
194 arc += ' [label="%s"]' % label