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! */
17 #define SILENT -1 /* no sound */
20 const char* inventory;
22 }} object_description_t;
30 descriptions_t description;
36 const char* yes_response;
58 extern location_t locations[];
59 extern object_description_t object_descriptions[];
60 extern const char* arbitrary_messages[];
61 extern const class_t classes[];
62 extern turn_threshold_t turn_thresholds[];
63 extern obituary_t obituaries[];
64 extern hint_t hints[];
65 extern long conditions[];
67 extern const size_t CLSSES;
68 extern const int maximum_deaths;
69 extern const int turn_threshold_count;
72 enum arbitrary_messages_refs {{
76 enum locations_refs {{
80 enum object_descriptions_refs {{
84 #endif /* end NEWDB_H */
87 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
92 const char* arbitrary_messages[] = {{
96 const class_t classes[] = {{
100 turn_threshold_t turn_thresholds[] = {{
104 location_t locations[] = {{
108 object_description_t object_descriptions[] = {{
112 obituary_t obituaries[] = {{
120 long conditions[] = {{
124 const size_t CLSSES = {};
125 const int maximum_deaths = {};
126 const int turn_threshold_count = {};
131 def make_c_string(string):
132 """Render a Python string into C string literal format."""
135 string = string.replace("\n", "\\n")
136 string = string.replace("\t", "\\t")
137 string = string.replace('"', '\\"')
138 string = string.replace("'", "\\'")
139 string = '"' + string + '"'
143 reflist = [x[0] for x in l]
146 ref_str += " {},\n".format(ref)
147 ref_str = ref_str[:-1] # trim trailing newline
150 def get_arbitrary_messages(arb):
155 arb_str += template.format(make_c_string(item[1]))
156 arb_str = arb_str[:-1] # trim trailing newline
159 def get_class_messages(cls):
167 threshold = item["threshold"]
168 message = make_c_string(item["message"])
169 cls_str += template.format(threshold, message)
170 cls_str = cls_str[:-1] # trim trailing newline
173 def get_turn_thresholds(trn):
182 threshold = item["threshold"]
183 point_loss = item["point_loss"]
184 message = make_c_string(item["message"])
185 trn_str += template.format(threshold, point_loss, message)
186 trn_str = trn_str[:-1] # trim trailing newline
189 def get_locations(loc):
200 short_d = make_c_string(item[1]["description"]["short"])
201 long_d = make_c_string(item[1]["description"]["long"])
202 sound = item[1].get("sound", "SILENT")
203 loc_str += template.format(short_d, long_d, sound)
204 loc_str = loc_str[:-1] # trim trailing newline
207 def get_object_descriptions(obj):
210 .longs = (const char* []) {{
217 i_msg = make_c_string(item[1]["inventory"])
219 if item[1]["longs"] == None:
220 longs_str = " " * 12 + "NULL,"
222 for l_msg in item[1]["longs"]:
223 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
224 longs_str = longs_str[:-1] # trim trailing newline
225 obj_str += template.format(i_msg, longs_str)
226 obj_str = obj_str[:-1] # trim trailing newline
229 def get_obituaries(obit):
237 query = make_c_string(o["query"])
238 yes = make_c_string(o["yes_response"])
239 obit_str += template.format(query, yes)
240 obit_str = obit_str[:-1] # trim trailing newline
243 def get_hints(hnt, arb):
255 item = member["hint"]
256 number = item["number"]
257 penalty = item["penalty"]
258 turns = item["turns"]
259 question = make_c_string(md[item["question"]])
260 hint = make_c_string(md[item["hint"]])
261 hnt_str += template.format(number, penalty, turns, question, hint)
262 hnt_str = hnt_str[:-1] # trim trailing newline
265 def get_condbits(locations):
267 for (name, loc) in locations:
268 conditions = loc["conditions"]
269 hints = loc.get("hints") or []
271 for flag in conditions:
273 flaglist.append(flag)
274 line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
275 trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
278 if line.startswith("|"):
282 cnd_str += " " + line + ",\t// " + name + "\n"
285 if __name__ == "__main__":
286 with open(yaml_name, "r") as f:
289 h = h_template.format(
291 get_refs(db["arbitrary_messages"]),
292 get_refs(db["locations"]),
293 get_refs(db["object_descriptions"]),
296 c = c_template.format(
298 get_arbitrary_messages(db["arbitrary_messages"]),
299 get_class_messages(db["classes"]),
300 get_turn_thresholds(db["turn_thresholds"]),
301 get_locations(db["locations"]),
302 get_object_descriptions(db["object_descriptions"]),
303 get_obituaries(db["obituaries"]),
304 get_hints(db["hints"], db["arbitrary_messages"]),
305 get_condbits(db["locations"]),
307 len(db["obituaries"]),
308 len(db["turn_thresholds"]),
311 with open(h_name, "w") as hf:
314 with open(c_name, "w") as cf: