-with open(yaml_name, "r") as f:
- db = yaml.load(f)
-
-h = h_template.format(
- get_refs(db["arbitrary_messages"]),
- get_refs(db["class_messages"]),
- get_refs(db["locations"]),
- get_refs(db["object_descriptions"]),
-)
-
-c = c_template.format(
- h_name,
- get_arbitrary_messages(db["arbitrary_messages"]),
- get_class_messages(db["class_messages"]),
- get_turn_thresholds(db["turn_thresholds"]),
- get_locations(db["locations"]),
- get_object_descriptions(db["object_descriptions"]),
- get_obituaries(db["obituaries"]),
- len(db["class_messages"]),
- len(db["obituaries"]),
- len(db["turn_thresholds"]),
-)
-
-with open(h_name, "w") as hf:
- hf.write(h)
-
-with open(c_name, "w") as cf:
- cf.write(c)
+def get_hints(hnt, arb):
+ template = """ {{
+ .number = {},
+ .penalty = {},
+ .turns = {},
+ .question = {},
+ .hint = {},
+ }},
+"""
+ hnt_str = ""
+ md = dict(arb)
+ for member in hnt:
+ item = member["hint"]
+ number = item["number"]
+ penalty = item["penalty"]
+ turns = item["turns"]
+ question = make_c_string(item["question"])
+ hint = make_c_string(item["hint"])
+ hnt_str += template.format(number, penalty, turns, question, hint)
+ hnt_str = hnt_str[:-1] # trim trailing newline
+ return hnt_str
+
+def get_condbits(locations):
+ cnd_str = ""
+ for (name, loc) in locations:
+ conditions = loc["conditions"]
+ hints = loc.get("hints") or []
+ flaglist = []
+ for flag in conditions:
+ if conditions[flag]:
+ flaglist.append(flag)
+ line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
+ trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
+ if trail:
+ line += "|" + trail
+ if line.startswith("|"):
+ line = line[1:]
+ if not line:
+ line = "0"
+ cnd_str += " " + line + ",\t// " + name + "\n"
+ return cnd_str
+
+def recompose(type_word, value):
+ "Compose the internal code for a vocabulary word from its YAML entry"
+ parts = ("motion", "action", "object", "special")
+ try:
+ return value + 1000 * parts.index(type_word)
+ except KeyError:
+ sys.stderr.write("dungeon: %s is not a known word\n" % word)
+ sys.exit(1)
+ except IndexError:
+ sys.stderr.write("%s is not a known word classifier" % attrs["type"])
+ sys.exit(1)
+
+def get_vocabulary(vocabulary):
+ template = """ {{
+ .word = {},
+ .type = {},
+ .value = {},
+ }},
+"""
+ voc_str = ""
+ for vocab in vocabulary:
+ word = make_c_string(vocab["word"])
+ type_code = recompose(vocab["type"], vocab["value"])
+ value = vocab["value"]
+ voc_str += template.format(word, type_code, value)
+ voc_str = voc_str[:-1] # trim trailing newline
+ return voc_str
+
+if __name__ == "__main__":
+ with open(yaml_name, "r") as f:
+ db = yaml.load(f)
+
+ locnames = [x[0] for x in db["locations"]]
+
+ 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_object_descriptions(db["object_descriptions"]),
+ get_obituaries(db["obituaries"]),
+ get_hints(db["hints"], db["arbitrary_messages"]),
+ get_condbits(db["locations"]),
+ get_vocabulary(db["vocabulary"]),
+ )
+
+ h = h_template.format(
+ len(db["locations"])-1,
+ len(db["object_descriptions"])-1,
+ len(db["hints"]),
+ len(db["classes"]),
+ len(db["obituaries"]),
+ len(db["turn_thresholds"]),
+ get_refs(db["arbitrary_messages"]),
+ get_refs(db["locations"]),
+ get_refs(db["object_descriptions"]),
+ statedefines,
+ )
+
+ with open(h_name, "w") as hf:
+ hf.write(h)
+
+ with open(c_name, "w") as cf:
+ cf.write(c)
+
+# end