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! */
18 const char* inventory;
20 }} object_description_t;
28 descriptions_t description;
33 const char* yes_response;
55 extern location_t locations[];
56 extern object_description_t object_descriptions[];
57 extern const char* arbitrary_messages[];
58 extern const class_t classes[];
59 extern turn_threshold_t turn_thresholds[];
60 extern obituary_t obituaries[];
61 extern hint_t hints[];
63 extern const size_t CLSSES;
64 extern const int maximum_deaths;
65 extern const int turn_threshold_count;
68 enum arbitrary_messages_refs {{
72 enum locations_refs {{
76 enum object_descriptions_refs {{
80 #endif /* end NEWDB_H */
83 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
87 const char* arbitrary_messages[] = {{
91 const class_t classes[] = {{
95 turn_threshold_t turn_thresholds[] = {{
99 location_t locations[] = {{
103 object_description_t object_descriptions[] = {{
107 obituary_t obituaries[] = {{
115 const size_t CLSSES = {};
116 const int maximum_deaths = {};
117 const int turn_threshold_count = {};
122 def make_c_string(string):
123 """Render a Python string into C string literal format."""
126 string = string.replace("\n", "\\n")
127 string = string.replace("\t", "\\t")
128 string = string.replace('"', '\\"')
129 string = string.replace("'", "\\'")
130 string = '"' + string + '"'
134 reflist = [x[0] for x in l]
137 ref_str += " {},\n".format(ref)
138 ref_str = ref_str[:-1] # trim trailing newline
141 def get_arbitrary_messages(arb):
146 arb_str += template.format(make_c_string(item[1]))
147 arb_str = arb_str[:-1] # trim trailing newline
150 def get_class_messages(cls):
158 threshold = item["threshold"]
159 message = make_c_string(item["message"])
160 cls_str += template.format(threshold, message)
161 cls_str = cls_str[:-1] # trim trailing newline
164 def get_turn_thresholds(trn):
173 threshold = item["threshold"]
174 point_loss = item["point_loss"]
175 message = make_c_string(item["message"])
176 trn_str += template.format(threshold, point_loss, message)
177 trn_str = trn_str[:-1] # trim trailing newline
180 def get_locations(loc):
190 short_d = make_c_string(item[1]["description"]["short"])
191 long_d = make_c_string(item[1]["description"]["long"])
192 loc_str += template.format(short_d, long_d)
193 loc_str = loc_str[:-1] # trim trailing newline
196 def get_object_descriptions(obj):
199 .longs = (const char* []) {{
206 i_msg = make_c_string(item[1]["inventory"])
208 if item[1]["longs"] == None:
209 longs_str = " " * 12 + "NULL,"
211 for l_msg in item[1]["longs"]:
212 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
213 longs_str = longs_str[:-1] # trim trailing newline
214 obj_str += template.format(i_msg, longs_str)
215 obj_str = obj_str[:-1] # trim trailing newline
218 def get_obituaries(obit):
226 query = make_c_string(o["query"])
227 yes = make_c_string(o["yes_response"])
228 obit_str += template.format(query, yes)
229 obit_str = obit_str[:-1] # trim trailing newline
232 def get_hints(hnt, arb):
244 number = item["number"]
245 penalty = item["penalty"]
246 turns = item["turns"]
247 question = make_c_string(md[item["question"]])
248 hint = make_c_string(md[item["hint"]])
249 hnt_str += template.format(number, penalty, turns, question, hint)
250 hnt_str = hnt_str[:-1] # trim trailing newline
254 if __name__ == "__main__":
255 with open(yaml_name, "r") as f:
258 h = h_template.format(
260 get_refs(db["arbitrary_messages"]),
261 get_refs(db["locations"]),
262 get_refs(db["object_descriptions"]),
265 c = c_template.format(
267 get_arbitrary_messages(db["arbitrary_messages"]),
268 get_class_messages(db["classes"]),
269 get_turn_thresholds(db["turn_thresholds"]),
270 get_locations(db["locations"]),
271 get_object_descriptions(db["object_descriptions"]),
272 get_obituaries(db["obituaries"]),
273 get_hints(db["hints"], db["arbitrary_messages"]),
275 len(db["obituaries"]),
276 len(db["turn_thresholds"]),
279 with open(h_name, "w") as hf:
282 with open(c_name, "w") as cf: