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 -m = emit graph of maze all alike
10 -s = emit graph of surface locations
11 -v = include internal sy,no;s in room labels
13 # Copyright (c) 2017 by Eric S. Raymond
14 # SPDX-License-Identifier: BSD-2-clause
16 import sys, getopt, yaml
19 "Select out loci related to the Maze All Alike"
20 return ("ALIKE" in loc) or (loc == "LOC_PITBRINK") or ("MAZEEND" in loc) or ("STALACTITE" in loc)
22 def alldifferent(loc):
23 "Select out loci related to the Maze All Alike"
24 return ("DIFFERENT" in loc) or (loc == "LOC_DEADEND13")
27 "Select out surface locations"
28 attrs = location_lookup[loc]
29 if ("ABOVE" in attrs["conditions"]) and attrs["conditions"]["ABOVE"]:
31 if ("FOREST" in attrs["conditions"]) and attrs["conditions"]["FOREST"]:
36 m = {"NORTH":"N", "EAST":"E", "SOUTH":"S", "WEST":"W", "UPWAR":"U", "DOWN":"D"}
40 "Generate a room label from the description, if possible"
41 loc_descriptions = location_lookup[loc]['description']
45 longd = loc_descriptions["long"]
46 short = loc_descriptions["maptag"] or loc_descriptions["short"]
47 if short is None and longd is not None and len(longd) < 20:
48 short = loc_descriptions["long"]
50 if short.startswith("You're "):
52 if short.startswith("You are "):
54 if short.startswith("in ") or short.startswith("at ") or short.startswith("on "):
56 if short.startswith("the "):
58 if short[:3] in {"n/s", "e/w"}:
59 short = short[:3].upper() + short[3:]
60 elif short[:2] in {"ne", "sw", "se", "nw"}:
61 short = short[:2].upper() + short[2:]
63 short = short[0].upper() + short[1:]
68 description += "\\n(" + ",".join(startlocs[loc]).lower() + ")"
71 # A forwarder is a location that you can't actually stop in - when you go there
72 # it ships some message (which is the point) then shifts you to a nexr location.
73 # A forwarder has a zero-length array of notion verbs in its travel section.
75 # Here is an example forwarder declaration:
79 # long: 'There is now one more gruesome aspect to the spectacular vista.'
82 # conditions: {DEEP: true}
84 # {verbs: [], action: [goto, LOC_NOWHERE]},
87 def is_forwarder(loc):
88 "Is a location a forwarder?"
89 travel = location_lookup[loc]['travel']
90 return len(travel) == 1 and len(travel[0]['verbs']) == 0
93 "Chase a location through forwarding links."
94 while is_forwarder(loc):
95 loc = location_lookup[loc]["travel"][0]["action"][1]
99 "Should this object be revealed when mappinmg?"
100 if "OBJ_" in objname:
102 if objname == "VEND":
104 obj = object_lookup[objname]
105 return not obj.get("immovable")
107 if __name__ == "__main__":
108 with open("adventure.yaml", "r") as f:
109 db = yaml.safe_load(f)
111 location_lookup = dict(db["locations"])
112 object_lookup = dict(db["objects"])
115 (options, arguments) = getopt.getopt(sys.argv[1:], "admsv")
116 except getopt.GetoptError as e:
122 for (switch, val) in options:
124 subset = lambda loc: True
126 subset = alldifferent
134 sys.stderr.write(__doc__)
138 for obj in db["objects"]:
140 location = obj[1].get("locations")
141 if location != "LOC_NOWHERE" and reveal(objname):
142 if location in startlocs:
143 startlocs[location].append(objname)
145 startlocs[location] = [objname]
147 # Compute reachability, using forwards.
148 # Dictionary ke6y is (from, to) iff its a valid link,
149 # value is correspoinding motion verbs.
152 for (loc, attrs) in db["locations"]:
154 travel = attrs["travel"]
157 verbs = [abbreviate(x) for x in dest["verbs"]]
160 action = dest["action"]
161 if action[0] == "goto":
162 dest = forward(action[1])
163 if not (subset(loc) or subset(dest)):
165 links[(loc, dest)] = verbs
170 if f == 'LOC_NOWHERE' or t == 'LOC_NOWHERE':
172 if (f == loc and subset(t)) or (t == loc and subset(f)):
173 if loc not in neighbors:
179 if not is_forwarder(loc):
180 node_label = roomlabel(loc)
182 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
183 elif loc in neighbors:
184 print(' %s [label="%s"]' % (loc[4:], node_label))
188 arc = "%s -> %s" % (f[4:], t[4:])
189 label=",".join(links[(f, t)]).lower()
191 arc += ' [label="%s"]' % label