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]
98 if __name__ == "__main__":
99 with open("adventure.yaml", "r") as f:
100 db = yaml.safe_load(f)
102 location_lookup = dict(db["locations"])
105 (options, arguments) = getopt.getopt(sys.argv[1:], "admsv")
106 except getopt.GetoptError as e:
112 for (switch, val) in options:
114 subset = lambda loc: True
116 subset = alldifferent
124 sys.stderr.write(__doc__)
128 for obj in db["objects"]:
130 location = obj[1].get("locations")
131 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
132 if location in startlocs:
133 startlocs[location].append(objname)
135 startlocs[location] = [objname]
137 # Compute reachability, using forwards.
138 # Dictionary ke6y is (from, to) iff its a valid link,
139 # value is correspoinding motion verbs.
142 for (loc, attrs) in db["locations"]:
144 travel = attrs["travel"]
147 verbs = [abbreviate(x) for x in dest["verbs"]]
150 action = dest["action"]
151 if action[0] == "goto":
152 dest = forward(action[1])
153 if not (subset(loc) or subset(dest)):
155 links[(loc, dest)] = verbs
160 if f == 'LOC_NOWHERE' or t == 'LOC_NOWHERE':
162 if (f == loc and subset(t)) or (t == loc and subset(f)):
163 if loc not in neighbors:
169 if is_forwarder(loc):
171 node_label = roomlabel(loc)
173 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
174 elif loc in neighbors:
175 print(' %s [label="%s"]' % (loc[4:], node_label))
179 arc = "%s -> %s" % (f[4:], t[4:])
180 label=",".join(links[(f, t)]).lower()
182 arc += ' [label="%s"]' % label