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"
13 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
20 #define SILENT -1 /* no sound */
23 const char* inventory;
27 }} object_description_t;
35 descriptions_t description;
42 const char* yes_response;
64 extern location_t locations[];
65 extern object_description_t object_descriptions[];
66 extern const char* arbitrary_messages[];
67 extern const class_t classes[];
68 extern turn_threshold_t turn_thresholds[];
69 extern obituary_t obituaries[];
70 extern hint_t hints[];
71 extern long conditions[];
72 extern const size_t CLSSES;
73 extern const int maximum_deaths;
74 extern const int turn_threshold_count;
77 enum arbitrary_messages_refs {{
81 enum locations_refs {{
85 enum object_descriptions_refs {{
89 /* State definitions */
92 #endif /* end NEWDB_H */
95 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
100 const char* arbitrary_messages[] = {{
104 const class_t classes[] = {{
108 turn_threshold_t turn_thresholds[] = {{
112 location_t locations[] = {{
116 object_description_t object_descriptions[] = {{
120 obituary_t obituaries[] = {{
128 long conditions[] = {{
132 const size_t CLSSES = {};
133 const int maximum_deaths = {};
134 const int turn_threshold_count = {};
139 def make_c_string(string):
140 """Render a Python string into C string literal format."""
143 string = string.replace("\n", "\\n")
144 string = string.replace("\t", "\\t")
145 string = string.replace('"', '\\"')
146 string = string.replace("'", "\\'")
147 string = '"' + string + '"'
151 reflist = [x[0] for x in l]
154 ref_str += " {},\n".format(ref)
155 ref_str = ref_str[:-1] # trim trailing newline
158 def get_arbitrary_messages(arb):
163 arb_str += template.format(make_c_string(item[1]))
164 arb_str = arb_str[:-1] # trim trailing newline
167 def get_class_messages(cls):
175 threshold = item["threshold"]
176 message = make_c_string(item["message"])
177 cls_str += template.format(threshold, message)
178 cls_str = cls_str[:-1] # trim trailing newline
181 def get_turn_thresholds(trn):
190 threshold = item["threshold"]
191 point_loss = item["point_loss"]
192 message = make_c_string(item["message"])
193 trn_str += template.format(threshold, point_loss, message)
194 trn_str = trn_str[:-1] # trim trailing newline
197 def get_locations(loc):
209 short_d = make_c_string(item[1]["description"]["short"])
210 long_d = make_c_string(item[1]["description"]["long"])
211 sound = item[1].get("sound", "SILENT")
212 loud = "true" if item[1].get("loud") else "false"
213 loc_str += template.format(short_d, long_d, sound, loud)
214 loc_str = loc_str[:-1] # trim trailing newline
217 def get_object_descriptions(obj):
220 .longs = (const char* []) {{
223 .sounds = (const char* []) {{
226 .texts = (const char* []) {{
233 i_msg = make_c_string(item[1]["inventory"])
235 if item[1]["longs"] == None:
236 longs_str = " " * 12 + "NULL,"
239 for l_msg in item[1]["longs"]:
240 if not isinstance(l_msg, str):
243 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
244 longs_str = longs_str[:-1] # trim trailing newline
247 statedefines += "/* States for %s */\n" % item[0]
248 for (i, (label, message)) in enumerate(labels):
249 if len(message) >= 45:
250 message = message[:45] + "..."
251 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
254 if item[1].get("sounds") == None:
255 sounds_str = " " * 12 + "NULL,"
257 for l_msg in item[1]["sounds"]:
258 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
259 sounds_str = sounds_str[:-1] # trim trailing newline
261 if item[1].get("texts") == None:
262 texts_str = " " * 12 + "NULL,"
264 for l_msg in item[1]["texts"]:
265 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
266 texts_str = texts_str[:-1] # trim trailing newline
267 obj_str += template.format(i_msg, longs_str, sounds_str, texts_str)
268 obj_str = obj_str[:-1] # trim trailing newline
271 def get_obituaries(obit):
279 query = make_c_string(o["query"])
280 yes = make_c_string(o["yes_response"])
281 obit_str += template.format(query, yes)
282 obit_str = obit_str[:-1] # trim trailing newline
285 def get_hints(hnt, arb):
297 item = member["hint"]
298 number = item["number"]
299 penalty = item["penalty"]
300 turns = item["turns"]
301 question = make_c_string(md[item["question"]])
302 hint = make_c_string(md[item["hint"]])
303 hnt_str += template.format(number, penalty, turns, question, hint)
304 hnt_str = hnt_str[:-1] # trim trailing newline
307 def get_condbits(locations):
309 for (name, loc) in locations:
310 conditions = loc["conditions"]
311 hints = loc.get("hints") or []
313 for flag in conditions:
315 flaglist.append(flag)
316 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
317 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
320 if line.startswith("|"):
324 cnd_str += " " + line + ",\t// " + name + "\n"
327 if __name__ == "__main__":
328 with open(yaml_name, "r") as f:
331 c = c_template.format(
333 get_arbitrary_messages(db["arbitrary_messages"]),
334 get_class_messages(db["classes"]),
335 get_turn_thresholds(db["turn_thresholds"]),
336 get_locations(db["locations"]),
337 get_object_descriptions(db["object_descriptions"]),
338 get_obituaries(db["obituaries"]),
339 get_hints(db["hints"], db["arbitrary_messages"]),
340 get_condbits(db["locations"]),
342 len(db["obituaries"]),
343 len(db["turn_thresholds"]),
346 h = h_template.format(
348 get_refs(db["arbitrary_messages"]),
349 get_refs(db["locations"]),
350 get_refs(db["object_descriptions"]),
354 with open(h_name, "w") as hf:
357 with open(c_name, "w") as cf: