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;
25 }} object_description_t;
33 descriptions_t description;
40 const char* yes_response;
62 extern location_t locations[];
63 extern object_description_t object_descriptions[];
64 extern const char* arbitrary_messages[];
65 extern const class_t classes[];
66 extern turn_threshold_t turn_thresholds[];
67 extern obituary_t obituaries[];
68 extern hint_t hints[];
69 extern long conditions[];
70 extern const size_t CLSSES;
71 extern const int maximum_deaths;
72 extern const int turn_threshold_count;
75 enum arbitrary_messages_refs {{
79 enum locations_refs {{
83 enum object_descriptions_refs {{
87 /* State definitions */
90 #endif /* end NEWDB_H */
93 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
98 const char* arbitrary_messages[] = {{
102 const class_t classes[] = {{
106 turn_threshold_t turn_thresholds[] = {{
110 location_t locations[] = {{
114 object_description_t object_descriptions[] = {{
118 obituary_t obituaries[] = {{
126 long conditions[] = {{
130 const size_t CLSSES = {};
131 const int maximum_deaths = {};
132 const int turn_threshold_count = {};
137 def make_c_string(string):
138 """Render a Python string into C string literal format."""
141 string = string.replace("\n", "\\n")
142 string = string.replace("\t", "\\t")
143 string = string.replace('"', '\\"')
144 string = string.replace("'", "\\'")
145 string = '"' + string + '"'
149 reflist = [x[0] for x in l]
152 ref_str += " {},\n".format(ref)
153 ref_str = ref_str[:-1] # trim trailing newline
156 def get_arbitrary_messages(arb):
161 arb_str += template.format(make_c_string(item[1]))
162 arb_str = arb_str[:-1] # trim trailing newline
165 def get_class_messages(cls):
173 threshold = item["threshold"]
174 message = make_c_string(item["message"])
175 cls_str += template.format(threshold, message)
176 cls_str = cls_str[:-1] # trim trailing newline
179 def get_turn_thresholds(trn):
188 threshold = item["threshold"]
189 point_loss = item["point_loss"]
190 message = make_c_string(item["message"])
191 trn_str += template.format(threshold, point_loss, message)
192 trn_str = trn_str[:-1] # trim trailing newline
195 def get_locations(loc):
207 short_d = make_c_string(item[1]["description"]["short"])
208 long_d = make_c_string(item[1]["description"]["long"])
209 sound = item[1].get("sound", "SILENT")
210 loud = "true" if item[1].get("loud") else "false"
211 loc_str += template.format(short_d, long_d, sound, loud)
212 loc_str = loc_str[:-1] # trim trailing newline
215 def get_object_descriptions(obj):
218 .longs = (const char* []) {{
225 i_msg = make_c_string(item[1]["inventory"])
227 if item[1]["longs"] == None:
228 longs_str = " " * 12 + "NULL,"
231 for l_msg in item[1]["longs"]:
232 if not isinstance(l_msg, str):
235 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
236 longs_str = longs_str[:-1] # trim trailing newline
239 statedefines += "/* States for %s */\n" % item[0]
240 for (i, (label, message)) in enumerate(labels):
241 if len(message) >= 45:
242 message = message[:45] + "..."
243 statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
245 obj_str += template.format(i_msg, longs_str)
246 obj_str = obj_str[:-1] # trim trailing newline
249 def get_obituaries(obit):
257 query = make_c_string(o["query"])
258 yes = make_c_string(o["yes_response"])
259 obit_str += template.format(query, yes)
260 obit_str = obit_str[:-1] # trim trailing newline
263 def get_hints(hnt, arb):
275 item = member["hint"]
276 number = item["number"]
277 penalty = item["penalty"]
278 turns = item["turns"]
279 question = make_c_string(md[item["question"]])
280 hint = make_c_string(md[item["hint"]])
281 hnt_str += template.format(number, penalty, turns, question, hint)
282 hnt_str = hnt_str[:-1] # trim trailing newline
285 def get_condbits(locations):
287 for (name, loc) in locations:
288 conditions = loc["conditions"]
289 hints = loc.get("hints") or []
291 for flag in conditions:
293 flaglist.append(flag)
294 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
295 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
298 if line.startswith("|"):
302 cnd_str += " " + line + ",\t// " + name + "\n"
305 if __name__ == "__main__":
306 with open(yaml_name, "r") as f:
309 c = c_template.format(
311 get_arbitrary_messages(db["arbitrary_messages"]),
312 get_class_messages(db["classes"]),
313 get_turn_thresholds(db["turn_thresholds"]),
314 get_locations(db["locations"]),
315 get_object_descriptions(db["object_descriptions"]),
316 get_obituaries(db["obituaries"]),
317 get_hints(db["hints"], db["arbitrary_messages"]),
318 get_condbits(db["locations"]),
320 len(db["obituaries"]),
321 len(db["turn_thresholds"]),
324 h = h_template.format(
326 get_refs(db["arbitrary_messages"]),
327 get_refs(db["locations"]),
328 get_refs(db["object_descriptions"]),
332 with open(h_name, "w") as hf:
335 with open(c_name, "w") as cf: