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 graoh of mazw 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 import sys, getopt, yaml
20 "Select out loci related to the Maze All Alike"
21 return location_lookup[loc]["conditions"].get("ALLALIKE")
23 def alldifferent(loc):
24 "Select out loci related to the Maze All Alike"
25 return location_lookup[loc]["conditions"].get("ALLDIFFERENT")
28 "Select out surface locations"
29 return location_lookup[loc]["conditions"].get("ABOVE")
32 return location_lookup[loc]["conditions"].get("FOREST")
35 m = {"NORTH":"N", "EAST":"E", "SOUTH":"S", "WEST":"W", "UPWAR":"U", "DOWN":"D"}
39 "Generate a room label from the description, if possible"
40 loc_descriptions = location_lookup[loc]['description']
44 longd = loc_descriptions["long"]
45 short = loc_descriptions["maptag"] or loc_descriptions["short"]
46 if short is None and longd is not None and len(longd) < 20:
47 short = loc_descriptions["long"]
49 if short.startswith("You're "):
51 if short.startswith("You are "):
53 if short.startswith("in ") or short.startswith("at ") or short.startswith("on "):
55 if short.startswith("the "):
57 if short[:3] in {"n/s", "e/w"}:
58 short = short[:3].upper() + short[3:]
59 elif short[:2] in {"ne", "sw", "se", "nw"}:
60 short = short[:2].upper() + short[2:]
62 short = short[0].upper() + short[1:]
67 description += "\\n(" + ",".join(startlocs[loc]).lower() + ")"
70 # A forwarder is a location that you can't actually stop in - when you go there
71 # it ships some message (which is the point) then shifts you to a nexr location.
72 # A forwarder has a zero-length array of notion verbs in its travel section.
74 # Here is an example forwarder declaration:
78 # long: 'There is now one more gruesome aspect to the spectacular vista.'
81 # conditions: {DEEP: true}
83 # {verbs: [], action: [goto, LOC_NOWHERE]},
86 def is_forwarder(loc):
87 "Is a location a forwarder?"
88 travel = location_lookup[loc]['travel']
89 return len(travel) == 1 and len(travel[0]['verbs']) == 0
92 "Chase a location through forwarding links."
93 while is_forwarder(loc):
94 loc = location_lookup[loc]["travel"][0]["action"][1]
98 "Should this object be revealed when mappinmg?"
101 if objname == "VEND":
103 obj = object_lookup[objname]
104 return not obj.get("immovable")
106 if __name__ == "__main__":
107 with open("adventure.yaml", "r") as f:
108 db = yaml.safe_load(f)
110 location_lookup = dict(db["locations"])
111 object_lookup = dict(db["objects"])
114 (options, arguments) = getopt.getopt(sys.argv[1:], "adfmsv")
115 except getopt.GetoptError as e:
121 for (switch, val) in options:
123 subset = lambda loc: True
125 subset = alldifferent
135 sys.stderr.write(__doc__)
139 for obj in db["objects"]:
141 location = obj[1].get("locations")
142 if location != "LOC_NOWHERE" and reveal(objname):
143 if location in startlocs:
144 startlocs[location].append(objname)
146 startlocs[location] = [objname]
148 # Compute reachability, using forwards.
149 # Dictionary ke6y is (from, to) iff its a valid link,
150 # value is correspoinding motion verbs.
153 for (loc, attrs) in db["locations"]:
155 travel = attrs["travel"]
158 verbs = [abbreviate(x) for x in dest["verbs"]]
161 action = dest["action"]
162 if action[0] == "goto":
163 dest = forward(action[1])
164 if not (subset(loc) or subset(dest)):
166 links[(loc, dest)] = verbs
171 if f == 'LOC_NOWHERE' or t == 'LOC_NOWHERE':
173 if (f == loc and subset(t)) or (t == loc and subset(f)):
174 if loc not in neighbors:
180 if not is_forwarder(loc):
181 node_label = roomlabel(loc)
183 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
184 elif loc in neighbors:
185 print(' %s [label="%s"]' % (loc[4:], node_label))
189 arc = "%s -> %s" % (f[4:], t[4:])
190 label=",".join(links[(f, t)]).lower()
192 arc += ' [label="%s"]' % label