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 = """#include <stdio.h>
14 const char* inventory;
16 }} object_description_t;
24 descriptions_t description;
29 const char* yes_response;
51 extern location_t locations[];
52 extern object_description_t object_descriptions[];
53 extern const char* arbitrary_messages[];
54 extern const class_t classes[];
55 extern turn_threshold_t turn_thresholds[];
56 extern obituary_t obituaries[];
57 extern hint_t hints[];
60 extern int maximum_deaths;
61 extern int turn_threshold_count;
63 enum arbitrary_messages_refs {{
67 enum locations_refs {{
71 enum object_descriptions_refs {{
76 c_template = """#include "{}"
78 const char* arbitrary_messages[] = {{
82 const class_t classes[] = {{
86 turn_threshold_t turn_thresholds[] = {{
90 location_t locations[] = {{
94 object_description_t object_descriptions[] = {{
98 obituary_t obituaries[] = {{
107 int maximum_deaths = {};
108 int turn_threshold_count = {};
111 def make_c_string(string):
112 """Render a Python string into C string literal format."""
115 string = string.replace("\n", "\\n")
116 string = string.replace("\t", "\\t")
117 string = string.replace('"', '\\"')
118 string = string.replace("'", "\\'")
119 string = '"' + string + '"'
123 reflist = [x[0] for x in l]
126 ref_str += " {},\n".format(ref)
127 ref_str = ref_str[:-1] # trim trailing newline
130 def get_arbitrary_messages(arb):
135 arb_str += template.format(make_c_string(item[1]))
136 arb_str = arb_str[:-1] # trim trailing newline
139 def get_class_messages(cls):
147 threshold = item["threshold"]
148 message = make_c_string(item["message"])
149 cls_str += template.format(threshold, message)
150 cls_str = cls_str[:-1] # trim trailing newline
153 def get_turn_thresholds(trn):
162 threshold = item["threshold"]
163 point_loss = item["point_loss"]
164 message = make_c_string(item["message"])
165 trn_str += template.format(threshold, point_loss, message)
166 trn_str = trn_str[:-1] # trim trailing newline
169 def get_locations(loc):
179 short_d = make_c_string(item[1]["description"]["short"])
180 long_d = make_c_string(item[1]["description"]["long"])
181 loc_str += template.format(short_d, long_d)
182 loc_str = loc_str[:-1] # trim trailing newline
185 def get_object_descriptions(obj):
188 .longs = (const char* []) {{
195 i_msg = make_c_string(item[1]["inventory"])
197 if item[1]["longs"] == None:
198 longs_str = " " * 12 + "NULL,"
200 for l_msg in item[1]["longs"]:
201 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
202 longs_str = longs_str[:-1] # trim trailing newline
203 obj_str += template.format(i_msg, longs_str)
204 obj_str = obj_str[:-1] # trim trailing newline
207 def get_obituaries(obit):
215 query = make_c_string(o["query"])
216 yes = make_c_string(o["yes_response"])
217 obit_str += template.format(query, yes)
218 obit_str = obit_str[:-1] # trim trailing newline
221 def get_hints(hnt, arb):
233 number = item["number"]
234 penalty = item["penalty"]
235 turns = item["turns"]
236 question = make_c_string(md[item["question"]])
237 hint = make_c_string(md[item["hint"]])
238 hnt_str += template.format(number, penalty, turns, question, hint)
239 hnt_str = hnt_str[:-1] # trim trailing newline
243 if __name__ == "__main__":
244 with open(yaml_name, "r") as f:
247 h = h_template.format(
248 get_refs(db["arbitrary_messages"]),
249 get_refs(db["locations"]),
250 get_refs(db["object_descriptions"]),
253 c = c_template.format(
255 get_arbitrary_messages(db["arbitrary_messages"]),
256 get_class_messages(db["classes"]),
257 get_turn_thresholds(db["turn_thresholds"]),
258 get_locations(db["locations"]),
259 get_object_descriptions(db["object_descriptions"]),
260 get_obituaries(db["obituaries"]),
261 get_hints(db["hints"], db["arbitrary_messages"]),
263 len(db["obituaries"]),
264 len(db["turn_thresholds"]),
267 with open(h_name, "w") as hf:
270 with open(c_name, "w") as cf: