a981a1db4e0f2f23caa7cf866bee61f622afad70
[open-adventure.git] / newdungeon.py
1 #!/usr/bin/python3
2
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.
4
5 import yaml
6
7 yaml_name = "adventure.yaml"
8 h_name = "newdb.h"
9 c_name = "newdb.c"
10
11 statedefines = ""
12
13 h_template = """/* Generated from adventure.yaml - do not hand-hack! */
14 #ifndef NEWDB_H
15 #define NEWDB_H
16
17 #include <stdio.h>
18 #include <stdbool.h>
19
20 #define SILENT  -1      /* no sound */
21
22 typedef struct {{
23   const char* inventory;
24   const char** longs;
25 }} object_description_t;
26
27 typedef struct {{
28   const char* small;
29   const char* big;
30 }} descriptions_t;
31
32 typedef struct {{
33   descriptions_t description;
34   const long sound;
35   const bool loud;
36 }} location_t;
37
38 typedef struct {{
39   const char* query;
40   const char* yes_response;
41 }} obituary_t;
42
43 typedef struct {{
44   const int threshold;
45   const int point_loss;
46   const char* message;
47 }} turn_threshold_t;
48
49 typedef struct {{
50   const int threshold;
51   const char* message;
52 }} class_t;
53
54 typedef struct {{
55   const int number;
56   const int turns;
57   const int penalty;
58   const char* question;
59   const char* hint;
60 }} hint_t;
61
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;
73 #define HINT_COUNT {}
74
75 enum arbitrary_messages_refs {{
76 {}
77 }};
78
79 enum locations_refs {{
80 {}
81 }};
82
83 enum object_descriptions_refs {{
84 {}
85 }};
86
87 /* State definitions */
88
89 {}
90 #endif /* end NEWDB_H */
91 """
92
93 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
94
95 #include "common.h"
96 #include "{}"
97
98 const char* arbitrary_messages[] = {{
99 {}
100 }};
101
102 const class_t classes[] = {{
103 {}
104 }};
105
106 turn_threshold_t turn_thresholds[] = {{
107 {}
108 }};
109
110 location_t locations[] = {{
111 {}
112 }};
113
114 object_description_t object_descriptions[] = {{
115 {}
116 }};
117
118 obituary_t obituaries[] = {{
119 {}
120 }};
121
122 hint_t hints[] = {{
123 {}
124 }};
125
126 long conditions[] = {{
127 {}
128 }};
129
130 const size_t CLSSES = {};
131 const int maximum_deaths = {};
132 const int turn_threshold_count = {};
133
134 /* end */
135 """
136
137 def make_c_string(string):
138     """Render a Python string into C string literal format."""
139     if string == None:
140         return "NULL"
141     string = string.replace("\n", "\\n")
142     string = string.replace("\t", "\\t")
143     string = string.replace('"', '\\"')
144     string = string.replace("'", "\\'")
145     string = '"' + string + '"'
146     return string
147
148 def get_refs(l):
149     reflist = [x[0] for x in l]
150     ref_str = ""
151     for ref in reflist:
152         ref_str += "    {},\n".format(ref)
153     ref_str = ref_str[:-1] # trim trailing newline
154     return ref_str
155
156 def get_arbitrary_messages(arb):
157     template = """    {},
158 """
159     arb_str = ""
160     for item in arb:
161         arb_str += template.format(make_c_string(item[1]))
162     arb_str = arb_str[:-1] # trim trailing newline
163     return arb_str
164
165 def get_class_messages(cls):
166     template = """    {{
167         .threshold = {},
168         .message = {},
169     }},
170 """
171     cls_str = ""
172     for item in 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
177     return cls_str
178
179 def get_turn_thresholds(trn):
180     template = """    {{
181         .threshold = {},
182         .point_loss = {},
183         .message = {},
184     }},
185 """
186     trn_str = ""
187     for item in 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
193     return trn_str
194
195 def get_locations(loc):
196     template = """    {{
197         .description = {{
198             .small = {},
199             .big = {},
200         }},
201         .sound = {},
202         .loud = {},
203     }},
204 """
205     loc_str = ""
206     for item in 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
213     return loc_str
214
215 def get_object_descriptions(obj):
216     template = """    {{
217         .inventory = {},
218         .longs = (const char* []) {{
219 {}
220         }},
221     }},
222 """
223     obj_str = ""
224     for item in obj:
225         i_msg = make_c_string(item[1]["inventory"])
226         longs_str = ""
227         if item[1]["longs"] == None:
228             longs_str = " " * 12 + "NULL,"
229         else:
230             labels = []
231             for l_msg in item[1]["longs"]:
232                 if not isinstance(l_msg, str):
233                     labels.append(l_msg)
234                     l_msg = l_msg[1]
235                 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
236             longs_str = longs_str[:-1] # trim trailing newline
237             if labels:
238                 global statedefines
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)
244                 statedefines += "\n"
245         obj_str += template.format(i_msg, longs_str)
246     obj_str = obj_str[:-1] # trim trailing newline
247     return obj_str
248
249 def get_obituaries(obit):
250     template = """    {{
251         .query = {},
252         .yes_response = {},
253     }},
254 """
255     obit_str = ""
256     for o in 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
261     return obit_str
262
263 def get_hints(hnt, arb):
264     template = """    {{
265         .number = {},
266         .penalty = {},
267         .turns = {},
268         .question = {},
269         .hint = {},
270     }},
271 """
272     hnt_str = ""
273     md = dict(arb)
274     for member in hnt:
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
283     return hnt_str
284
285 def get_condbits(locations):
286     cnd_str = ""
287     for (name, loc) in locations:
288         conditions = loc["conditions"]
289         hints = loc.get("hints") or []
290         flaglist = []
291         for flag in conditions:
292             if conditions[flag]:
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])
296         if trail:
297             line += "|" + trail
298         if line.startswith("|"):
299             line = line[1:]
300         if not line:
301             line = "0"
302         cnd_str += "    " + line + ",\t// " + name + "\n"
303     return cnd_str
304
305 if __name__ == "__main__":
306     with open(yaml_name, "r") as f:
307         db = yaml.load(f)
308
309     c = c_template.format(
310         h_name,
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"]),
319         len(db["classes"]),
320         len(db["obituaries"]),
321         len(db["turn_thresholds"]),
322     )
323
324     h = h_template.format(
325         len(db["hints"]),
326         get_refs(db["arbitrary_messages"]),
327         get_refs(db["locations"]),
328         get_refs(db["object_descriptions"]),
329         statedefines,
330     )
331
332     with open(h_name, "w") as hf:
333         hf.write(h)
334
335     with open(c_name, "w") as cf:
336         cf.write(c)
337
338 # end