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;
28 }} object_description_t;
36 descriptions_t description;
43 const char* yes_response;
65 extern const location_t locations[];
66 extern const object_description_t object_descriptions[];
67 extern const const char* arbitrary_messages[];
68 extern const const class_t classes[];
69 extern const turn_threshold_t turn_thresholds[];
70 extern const obituary_t obituaries[];
71 extern const hint_t hints[];
72 extern long conditions[];
79 #define NTHRESHOLDS {}
81 enum arbitrary_messages_refs {{
85 enum locations_refs {{
89 enum object_descriptions_refs {{
93 /* State definitions */
96 #endif /* end NEWDB_H */
99 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
104 const char* arbitrary_messages[] = {{
108 const class_t classes[] = {{
112 const turn_threshold_t turn_thresholds[] = {{
116 const location_t locations[] = {{
120 const object_description_t object_descriptions[] = {{
124 const obituary_t obituaries[] = {{
128 const hint_t hints[] = {{
132 long conditions[] = {{
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):
221 .longs = (const char* []) {{
224 .sounds = (const char* []) {{
227 .texts = (const char* []) {{
234 i_msg = make_c_string(item[1]["inventory"])
236 if item[1]["longs"] == None:
237 longs_str = " " * 12 + "NULL,"
240 for l_msg in item[1]["longs"]:
241 if not isinstance(l_msg, str):
244 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
245 longs_str = longs_str[:-1] # trim trailing newline
248 statedefines += "/* States for %s */\n" % item[0]
249 for (i, (label, message)) in enumerate(labels):
250 if len(message) >= 45:
251 message = message[:45] + "..."
252 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
255 if item[1].get("sounds") == None:
256 sounds_str = " " * 12 + "NULL,"
258 for l_msg in item[1]["sounds"]:
259 sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
260 sounds_str = sounds_str[:-1] # trim trailing newline
262 if item[1].get("texts") == None:
263 texts_str = " " * 12 + "NULL,"
265 for l_msg in item[1]["texts"]:
266 texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
267 texts_str = texts_str[:-1] # trim trailing newline
268 treasure = "true" if item[1].get("treasure") else "false"
269 obj_str += template.format(i_msg, treasure, longs_str, sounds_str, texts_str)
270 obj_str = obj_str[:-1] # trim trailing newline
273 def get_obituaries(obit):
281 query = make_c_string(o["query"])
282 yes = make_c_string(o["yes_response"])
283 obit_str += template.format(query, yes)
284 obit_str = obit_str[:-1] # trim trailing newline
287 def get_hints(hnt, arb):
299 item = member["hint"]
300 number = item["number"]
301 penalty = item["penalty"]
302 turns = item["turns"]
303 question = make_c_string(md[item["question"]])
304 hint = make_c_string(md[item["hint"]])
305 hnt_str += template.format(number, penalty, turns, question, hint)
306 hnt_str = hnt_str[:-1] # trim trailing newline
309 def get_condbits(locations):
311 for (name, loc) in locations:
312 conditions = loc["conditions"]
313 hints = loc.get("hints") or []
315 for flag in conditions:
317 flaglist.append(flag)
318 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
319 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
322 if line.startswith("|"):
326 cnd_str += " " + line + ",\t// " + name + "\n"
329 if __name__ == "__main__":
330 with open(yaml_name, "r") as f:
333 c = c_template.format(
335 get_arbitrary_messages(db["arbitrary_messages"]),
336 get_class_messages(db["classes"]),
337 get_turn_thresholds(db["turn_thresholds"]),
338 get_locations(db["locations"]),
339 get_object_descriptions(db["object_descriptions"]),
340 get_obituaries(db["obituaries"]),
341 get_hints(db["hints"], db["arbitrary_messages"]),
342 get_condbits(db["locations"]),
345 h = h_template.format(
346 len(db["locations"]),
347 len(db["object_descriptions"]),
350 len(db["obituaries"]),
351 len(db["turn_thresholds"]),
352 get_refs(db["arbitrary_messages"]),
353 get_refs(db["locations"]),
354 get_refs(db["object_descriptions"]),
358 with open(h_name, "w") as hf:
361 with open(c_name, "w") as cf: