2 # SPDX-FileCopyrightText: (C) Eric S. Raymond <esr@thyrsus.com>
3 # SPDX-License-Identifier: BSD-2-Clause
5 usage: make_graph.py [-a] [-d] [-m] [-s] [-v]
7 Make a DOT graph of Colossal Cave.
9 -a = emit graph of entire dungeon
10 -d = emit graph of maze all different
11 -f = emit graph of forest locations
12 -m = emit graph of maze all alike
13 -s = emit graph of non-forest surface locations
14 -v = include internal symbols in room labels
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
23 "Select out loci related to the Maze All Alike"
24 return location_lookup[loc]["conditions"].get("ALLALIKE")
27 def alldifferent(loc):
28 "Select out loci related to the Maze All Alike"
29 return location_lookup[loc]["conditions"].get("ALLDIFFERENT")
33 "Select out surface locations"
34 return location_lookup[loc]["conditions"].get("ABOVE")
38 return location_lookup[loc]["conditions"].get("FOREST")
54 "Generate a room label from the description, if possible"
55 loc_descriptions = location_lookup[loc]["description"]
59 longd = loc_descriptions["long"]
60 short = loc_descriptions["maptag"] or loc_descriptions["short"]
61 if short is None and longd is not None and len(longd) < 20:
62 short = loc_descriptions["long"]
64 if short.startswith("You're "):
66 if short.startswith("You are "):
69 short.startswith("in ")
70 or short.startswith("at ")
71 or short.startswith("on ")
74 if short.startswith("the "):
76 if short[:3] in {"n/s", "e/w"}:
77 short = short[:3].upper() + short[3:]
78 elif short[:2] in {"ne", "sw", "se", "nw"}:
79 short = short[:2].upper() + short[2:]
81 short = short[0].upper() + short[1:]
86 description += "\\n(" + ",".join(startlocs[loc]).lower() + ")"
90 # A forwarder is a location that you can't actually stop in - when you go there
91 # it ships some message (which is the point) then shifts you to a next location.
92 # A forwarder has a zero-length array of notion verbs in its travel section.
94 # Here is an example forwarder declaration:
98 # long: 'There is now one more gruesome aspect to the spectacular vista.'
101 # conditions: {DEEP: true}
103 # {verbs: [], action: [goto, LOC_NOWHERE]},
107 def is_forwarder(loc):
108 "Is a location a forwarder?"
109 travel = location_lookup[loc]["travel"]
110 return len(travel) == 1 and len(travel[0]["verbs"]) == 0
114 "Chase a location through forwarding links."
115 while is_forwarder(loc):
116 loc = location_lookup[loc]["travel"][0]["action"][1]
121 "Should this object be revealed when mapping?"
122 if "OBJ_" in objname:
124 if objname == "VEND":
126 obj = object_lookup[objname]
127 return not obj.get("immovable")
130 if __name__ == "__main__":
131 with open("adventure.yaml", "r", encoding="ascii", errors="surrogateescape") as f:
132 db = yaml.safe_load(f)
134 location_lookup = dict(db["locations"])
135 object_lookup = dict(db["objects"])
138 (options, arguments) = getopt.getopt(sys.argv[1:], "adfmsv")
139 except getopt.GetoptError as e:
145 for (switch, val) in options:
147 # pylint: disable=unnecessary-lambda-assignment
148 subset = lambda loc: True
150 subset = alldifferent
160 sys.stderr.write(__doc__)
164 for obj in db["objects"]:
166 location = obj[1].get("locations")
167 if location != "LOC_NOWHERE" and reveal(objname):
168 if location in startlocs:
169 startlocs[location].append(objname)
171 startlocs[location] = [objname]
173 # Compute reachability, using forwards.
174 # Dictionary key is (from, to) iff its a valid link,
175 # value is corresponding motion verbs.
178 for (loc, attrs) in db["locations"]:
180 travel = attrs["travel"]
183 verbs = [abbreviate(x) for x in dest["verbs"]]
186 action = dest["action"]
187 if action[0] == "goto":
188 dest = forward(action[1])
189 if not (subset(loc) or subset(dest)):
191 links[(loc, dest)] = verbs
196 if f == "LOC_NOWHERE" or t == "LOC_NOWHERE":
198 if (f == loc and subset(t)) or (t == loc and subset(f)):
199 if loc not in neighbors:
205 if not is_forwarder(loc):
206 node_label = roomlabel(loc)
208 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
209 elif loc in neighbors:
210 print(' %s [label="%s"]' % (loc[4:], node_label))
214 arc = "%s -> %s" % (f[4:], t[4:])
215 label = ",".join(links[(f, t)]).lower()
217 arc += ' [label="%s"]' % label