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
12 # Copyright (c) 2017 by Eric S. Raymond
13 # SPDX-License-Identifier: BSD-2-clause
15 import sys, getopt, yaml
18 "Select out loci related to the Maze All Alike"
19 return ("ALIKE" in loc) or (loc == "LOC_PITBRINK") or ("MAZEEND" in loc) or ("STALACTITE" in loc)
21 def alldifferent(loc):
22 "Select out loci related to the Maze All Alike"
23 return ("DIFFERENT" in loc) or (loc == "LOC_DEADEND13")
26 "Select out surface locations"
27 attrs = location_lookup[loc]
28 if ("ABOVE" in attrs["conditions"]) and attrs["conditions"]["ABOVE"]:
30 if ("FOREST" in attrs["conditions"]) and attrs["conditions"]["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']
42 longd = loc_descriptions["long"]
43 short = loc_descriptions["maptag"] or loc_descriptions["short"]
44 if short is None and longd is not None and len(longd) < 20:
45 short = loc_descriptions["long"]
47 if short.startswith("You're "):
49 if short.startswith("You are "):
51 if short.startswith("in ") or short.startswith("at ") or short.startswith("on "):
53 if short.startswith("the "):
55 if short[:3] in {"n/s", "e/w"}:
56 short = short[:3].upper() + short[3:]
57 elif short[:2] in {"ne", "sw", "se", "nw"}:
58 short = short[:2].upper() + short[2:]
60 short = short[0].upper() + short[1:]
61 description += "\\n" + short
64 # A forwarder is a location that you can't actually stop in - when you go there
65 # it ships some message (which is the point) then shifts you to a nexr location.
66 # A forwarder has a zero-length array of notion verbs in its travel section.
68 # Here is an example forwarder declaration:
72 # long: 'There is now one more gruesome aspect to the spectacular vista.'
75 # conditions: {DEEP: true}
77 # {verbs: [], action: [goto, LOC_NOWHERE]},
80 def is_forwarder(loc):
81 "Is a location a forwarder?"
82 travel = location_lookup[loc]['travel']
83 return len(travel) == 1 and len(travel[0]['verbs']) == 0
86 "Chase a location through forwarding links."
87 while is_forwarder(loc):
88 loc = location_lookup[loc]["travel"][0]["action"][1]
91 if __name__ == "__main__":
92 with open("adventure.yaml", "r") as f:
93 db = yaml.safe_load(f)
95 location_lookup = dict(db["locations"])
98 (options, arguments) = getopt.getopt(sys.argv[1:], "adms")
99 except getopt.GetoptError as e:
104 for (switch, val) in options:
106 subset = lambda loc: True
108 subset = alldifferent
114 sys.stderr.write(__doc__)
118 for obj in db["objects"]:
120 location = obj[1].get("locations")
121 if "OBJ" not in objname and location != "LOC_NOWHERE" and ("immovable" not in obj[1] or not obj[1]["immovable"]):
122 if location in startlocs:
123 startlocs[location].append(objname)
125 startlocs[location] = [objname]
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]
139 for (loc, attrs) in db["locations"]:
140 if is_forwarder(loc):
144 node_label = roomlabel(loc)
146 node_label += "\\n" + ",".join(startlocs[loc]).lower()
147 print(' %s [shape=box,label="%s"]' % (loc[4:], node_label))
149 for (loc, attrs) in db["locations"]:
150 travel = attrs["travel"]
153 verbs = [abbreviate(x) for x in dest["verbs"]]
156 action = dest["action"]
157 if action[0] == "goto":
158 dest = forward(action[1])
159 if not (subset(loc) or subset(dest)):
161 arc = "%s -> %s" % (loc[4:], dest[4:])
162 label=",".join(verbs).lower()
164 arc += ' [label="%s"]' % label