-h += "enum object_descriptions_refs {\n"
-c += "object_description_t object_descriptions[] = {\n"
-for key, data in dungeon["object_descriptions"]:
- try:
- data["inventory"] = "\"{}\"".format(c_escape(data["inventory"]))
- except AttributeError:
- data["inventory"] = "NULL"
- h += " {},\n".format(key)
- c += " {\n"
- c += " .inventory = {},\n".format(data["inventory"])
- try:
- data["longs"][0]
- c += " .longs = (char* []) {\n"
- for l in data["longs"]:
- l = c_escape(l)
- c += " \"{}\",\n".format(l)
- c += " },\n"
- except (TypeError, IndexError):
- c += " .longs = NULL,\n"
- c += " },\n"
-h += "};"
-c += "};"
-
-c += """
-size_t CLSSES = {};
-""".format(len(dungeon["class_messages"]))
-
-# finally, write out the files
-d = {
- h_name: h,
- c_name: c,
-}
-for filename, string in d.items():
- with open(filename, "w") as f:
- f.write(string)
+ for (i, (name, loc)) in enumerate(locs):
+ if "travel" in loc:
+ for rule in loc["travel"]:
+ tt = [i]
+ dest = dencode(rule["action"], name) + 1000 * cencode(rule.get("cond"), name)
+ tt.append(dest)
+ tt += [verbmap[e] for e in rule["verbs"]]
+ if not rule["verbs"]:
+ tt.append(1)
+ ltravel.append(tuple(tt))
+
+ # At this point the ltravel data is in the Section 3
+ # representation from the FORTRAN version. Next we perform the
+ # same mapping into the runtime format. This was the C translation
+ # of the FORTRAN code:
+ # long loc;
+ # while ((loc = GETNUM(database)) != -1) {
+ # long newloc = GETNUM(NULL);
+ # long L;
+ # if (TKEY[loc] == 0) {
+ # TKEY[loc] = TRVS;
+ # } else {
+ # TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
+ # }
+ # while ((L = GETNUM(NULL)) != 0) {
+ # TRAVEL[TRVS] = newloc * 1000 + L;
+ # TRVS = TRVS + 1;
+ # if (TRVS == TRVSIZ)
+ # BUG(TOO_MANY_TRAVEL_OPTIONS);
+ # }
+ # TRAVEL[TRVS - 1] = -TRAVEL[TRVS - 1];
+ # }
+ #
+ # In order to de-crypticize the runtime code, we're going to break these
+ # magic numbers up into a struct.
+ travel = [[0, 0, False]]
+ tkey = [0]
+ oldloc = 0
+ while ltravel:
+ rule = list(ltravel.pop(0))
+ loc = rule.pop(0)
+ newloc = rule.pop(0)
+ if loc != oldloc:
+ tkey.append(len(travel))
+ oldloc = loc
+ elif travel:
+ travel[-1][2] = not travel[-1][2]
+ while rule:
+ travel.append([rule.pop(0), newloc, False])
+ travel[-1][2] = True
+ return (travel, tkey)
+
+def get_travel(travel):
+ template = """ {{
+ .motion = {},
+ .dest = {},
+ .stop = {},
+ }},
+"""
+ out = ""
+ for entry in travel:
+ out += template.format(entry[0], entry[1], entry[2]).lower()
+ out = out[:-1] # trim trailing newline
+ return out
+
+if __name__ == "__main__":
+ with open(yaml_name, "r") as f:
+ db = yaml.load(f)
+
+ locnames = [x[0] for x in db["locations"]]
+ msgnames = [el[0] for el in db["arbitrary_messages"]]
+ objnames = [el[0] for el in db["objects"]]
+
+ (travel, tkey) = buildtravel(db["locations"],
+ db["objects"],
+ db["vocabulary"])
+
+ c = c_template.format(
+ h_name,
+ get_arbitrary_messages(db["arbitrary_messages"]),
+ get_class_messages(db["classes"]),
+ get_turn_thresholds(db["turn_thresholds"]),
+ get_locations(db["locations"]),
+ get_objects(db["objects"]),
+ get_obituaries(db["obituaries"]),
+ get_hints(db["hints"], db["arbitrary_messages"]),
+ get_condbits(db["locations"]),
+ get_motions(db["motions"]),
+ get_actions(db["actions"]),
+ "const long tkey[] = {%s};" % bigdump(tkey),
+ get_travel(travel),
+ )
+
+ h = h_template.format(
+ len(db["locations"])-1,
+ len(db["objects"])-1,
+ len(db["hints"]),
+ len(db["classes"])-1,
+ len(db["obituaries"]),
+ len(db["turn_thresholds"]),
+ len(db["actions"]),
+ len(travel),
+ len(tkey),
+ get_refs(db["arbitrary_messages"]),
+ get_refs(db["locations"]),
+ get_refs(db["objects"]),
+ get_refs(db["motions"]),
+ get_refs(db["actions"]),
+ statedefines,
+ )
+
+ with open(h_name, "w") as hf:
+ hf.write(h)
+
+ with open(c_name, "w") as cf:
+ cf.write(c)