3 # This is the new open-adventure dungeon generator. It'll eventually replace the existing dungeon.c It currently outputs a .h and .c pair for C code.
7 yaml_name = "adventure.yaml"
11 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
18 const char* inventory;
20 }} object_description_t;
28 descriptions_t description;
33 const char* yes_response;
55 extern location_t locations[];
56 extern object_description_t object_descriptions[];
57 extern const char* arbitrary_messages[];
58 extern const class_t classes[];
59 extern turn_threshold_t turn_thresholds[];
60 extern obituary_t obituaries[];
61 extern hint_t hints[];
62 extern long conditions[];
64 extern const size_t CLSSES;
65 extern const int maximum_deaths;
66 extern const int turn_threshold_count;
69 enum arbitrary_messages_refs {{
73 enum locations_refs {{
77 enum object_descriptions_refs {{
81 #endif /* end NEWDB_H */
84 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
89 const char* arbitrary_messages[] = {{
93 const class_t classes[] = {{
97 turn_threshold_t turn_thresholds[] = {{
101 location_t locations[] = {{
105 object_description_t object_descriptions[] = {{
109 obituary_t obituaries[] = {{
117 long conditions[] = {{
121 const size_t CLSSES = {};
122 const int maximum_deaths = {};
123 const int turn_threshold_count = {};
128 def make_c_string(string):
129 """Render a Python string into C string literal format."""
132 string = string.replace("\n", "\\n")
133 string = string.replace("\t", "\\t")
134 string = string.replace('"', '\\"')
135 string = string.replace("'", "\\'")
136 string = '"' + string + '"'
140 reflist = [x[0] for x in l]
143 ref_str += " {},\n".format(ref)
144 ref_str = ref_str[:-1] # trim trailing newline
147 def get_arbitrary_messages(arb):
152 arb_str += template.format(make_c_string(item[1]))
153 arb_str = arb_str[:-1] # trim trailing newline
156 def get_class_messages(cls):
164 threshold = item["threshold"]
165 message = make_c_string(item["message"])
166 cls_str += template.format(threshold, message)
167 cls_str = cls_str[:-1] # trim trailing newline
170 def get_turn_thresholds(trn):
179 threshold = item["threshold"]
180 point_loss = item["point_loss"]
181 message = make_c_string(item["message"])
182 trn_str += template.format(threshold, point_loss, message)
183 trn_str = trn_str[:-1] # trim trailing newline
186 def get_locations(loc):
196 short_d = make_c_string(item[1]["description"]["short"])
197 long_d = make_c_string(item[1]["description"]["long"])
198 loc_str += template.format(short_d, long_d)
199 loc_str = loc_str[:-1] # trim trailing newline
202 def get_object_descriptions(obj):
205 .longs = (const char* []) {{
212 i_msg = make_c_string(item[1]["inventory"])
214 if item[1]["longs"] == None:
215 longs_str = " " * 12 + "NULL,"
217 for l_msg in item[1]["longs"]:
218 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
219 longs_str = longs_str[:-1] # trim trailing newline
220 obj_str += template.format(i_msg, longs_str)
221 obj_str = obj_str[:-1] # trim trailing newline
224 def get_obituaries(obit):
232 query = make_c_string(o["query"])
233 yes = make_c_string(o["yes_response"])
234 obit_str += template.format(query, yes)
235 obit_str = obit_str[:-1] # trim trailing newline
238 def get_hints(hnt, arb):
250 item = member["hint"]
251 number = item["number"]
252 penalty = item["penalty"]
253 turns = item["turns"]
254 question = make_c_string(md[item["question"]])
255 hint = make_c_string(md[item["hint"]])
256 hnt_str += template.format(number, penalty, turns, question, hint)
257 hnt_str = hnt_str[:-1] # trim trailing newline
260 def get_condbits(locations):
262 for (name, loc) in locations:
263 conditions = loc["conditions"]
264 hints = loc.get("hints") or []
266 for flag in conditions:
268 flaglist.append(flag)
269 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
270 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
273 if line.startswith("|"):
277 cnd_str += " " + line + ",\t// " + name + "\n"
280 if __name__ == "__main__":
281 with open(yaml_name, "r") as f:
284 h = h_template.format(
286 get_refs(db["arbitrary_messages"]),
287 get_refs(db["locations"]),
288 get_refs(db["object_descriptions"]),
291 c = c_template.format(
293 get_arbitrary_messages(db["arbitrary_messages"]),
294 get_class_messages(db["classes"]),
295 get_turn_thresholds(db["turn_thresholds"]),
296 get_locations(db["locations"]),
297 get_object_descriptions(db["object_descriptions"]),
298 get_obituaries(db["obituaries"]),
299 get_hints(db["hints"], db["arbitrary_messages"]),
300 get_condbits(db["locations"]),
302 len(db["obituaries"]),
303 len(db["turn_thresholds"]),
306 with open(h_name, "w") as hf:
309 with open(c_name, "w") as cf: