Replace magic MINTRS/MAXTRS with a treasure attribute in YAML.
[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   bool is_treasure;
25   const char** longs;
26   const char** sounds;
27   const char** texts;
28 }} object_description_t;
29
30 typedef struct {{
31   const char* small;
32   const char* big;
33 }} descriptions_t;
34
35 typedef struct {{
36   descriptions_t description;
37   const long sound;
38   const bool loud;
39 }} location_t;
40
41 typedef struct {{
42   const char* query;
43   const char* yes_response;
44 }} obituary_t;
45
46 typedef struct {{
47   const int threshold;
48   const int point_loss;
49   const char* message;
50 }} turn_threshold_t;
51
52 typedef struct {{
53   const int threshold;
54   const char* message;
55 }} class_t;
56
57 typedef struct {{
58   const int number;
59   const int turns;
60   const int penalty;
61   const char* question;
62   const char* hint;
63 }} hint_t;
64
65 extern location_t locations[];
66 extern object_description_t object_descriptions[];
67 extern const char* arbitrary_messages[];
68 extern const class_t classes[];
69 extern turn_threshold_t turn_thresholds[];
70 extern obituary_t obituaries[];
71 extern hint_t hints[];
72 extern long conditions[];
73
74 #define NLOCATIONS              {}
75 #define NOBJECTS        {}
76 #define NHINTS          {}
77 #define NCLASSES        {}
78 #define NDEATHS         {}
79 #define NTHRESHOLDS     {}
80
81 enum arbitrary_messages_refs {{
82 {}
83 }};
84
85 enum locations_refs {{
86 {}
87 }};
88
89 enum object_descriptions_refs {{
90 {}
91 }};
92
93 /* State definitions */
94
95 {}
96 #endif /* end NEWDB_H */
97 """
98
99 c_template = """/* Generated from adventure.yaml - do not hand-hack! */
100
101 #include "common.h"
102 #include "{}"
103
104 const char* arbitrary_messages[] = {{
105 {}
106 }};
107
108 const class_t classes[] = {{
109 {}
110 }};
111
112 turn_threshold_t turn_thresholds[] = {{
113 {}
114 }};
115
116 location_t locations[] = {{
117 {}
118 }};
119
120 object_description_t object_descriptions[] = {{
121 {}
122 }};
123
124 obituary_t obituaries[] = {{
125 {}
126 }};
127
128 hint_t hints[] = {{
129 {}
130 }};
131
132 long conditions[] = {{
133 {}
134 }};
135
136 /* end */
137 """
138
139 def make_c_string(string):
140     """Render a Python string into C string literal format."""
141     if string == None:
142         return "NULL"
143     string = string.replace("\n", "\\n")
144     string = string.replace("\t", "\\t")
145     string = string.replace('"', '\\"')
146     string = string.replace("'", "\\'")
147     string = '"' + string + '"'
148     return string
149
150 def get_refs(l):
151     reflist = [x[0] for x in l]
152     ref_str = ""
153     for ref in reflist:
154         ref_str += "    {},\n".format(ref)
155     ref_str = ref_str[:-1] # trim trailing newline
156     return ref_str
157
158 def get_arbitrary_messages(arb):
159     template = """    {},
160 """
161     arb_str = ""
162     for item in arb:
163         arb_str += template.format(make_c_string(item[1]))
164     arb_str = arb_str[:-1] # trim trailing newline
165     return arb_str
166
167 def get_class_messages(cls):
168     template = """    {{
169         .threshold = {},
170         .message = {},
171     }},
172 """
173     cls_str = ""
174     for item in cls:
175         threshold = item["threshold"]
176         message = make_c_string(item["message"])
177         cls_str += template.format(threshold, message)
178     cls_str = cls_str[:-1] # trim trailing newline
179     return cls_str
180
181 def get_turn_thresholds(trn):
182     template = """    {{
183         .threshold = {},
184         .point_loss = {},
185         .message = {},
186     }},
187 """
188     trn_str = ""
189     for item in trn:
190         threshold = item["threshold"]
191         point_loss = item["point_loss"]
192         message = make_c_string(item["message"])
193         trn_str += template.format(threshold, point_loss, message)
194     trn_str = trn_str[:-1] # trim trailing newline
195     return trn_str
196
197 def get_locations(loc):
198     template = """    {{
199         .description = {{
200             .small = {},
201             .big = {},
202         }},
203         .sound = {},
204         .loud = {},
205     }},
206 """
207     loc_str = ""
208     for item in loc:
209         short_d = make_c_string(item[1]["description"]["short"])
210         long_d = make_c_string(item[1]["description"]["long"])
211         sound = item[1].get("sound", "SILENT")
212         loud = "true" if item[1].get("loud") else "false"
213         loc_str += template.format(short_d, long_d, sound, loud)
214     loc_str = loc_str[:-1] # trim trailing newline
215     return loc_str
216
217 def get_object_descriptions(obj):
218     template = """    {{
219         .inventory = {},
220         .is_treasure = {},
221         .longs = (const char* []) {{
222 {}
223         }},
224         .sounds = (const char* []) {{
225 {}
226         }},
227         .texts = (const char* []) {{
228 {}
229         }},
230     }},
231 """
232     obj_str = ""
233     for item in obj:
234         i_msg = make_c_string(item[1]["inventory"])
235         longs_str = ""
236         if item[1]["longs"] == None:
237             longs_str = " " * 12 + "NULL,"
238         else:
239             labels = []
240             for l_msg in item[1]["longs"]:
241                 if not isinstance(l_msg, str):
242                     labels.append(l_msg)
243                     l_msg = l_msg[1]
244                 longs_str += " " * 12 + make_c_string(l_msg) + ",\n"
245             longs_str = longs_str[:-1] # trim trailing newline
246             if labels:
247                 global statedefines
248                 statedefines += "/* States for %s */\n" % item[0]
249                 for (i, (label, message)) in enumerate(labels):
250                     if len(message) >= 45:
251                         message = message[:45] + "..."
252                     statedefines += "#define %s\t%d /* %s */\n" % (label, i, message)
253                 statedefines += "\n"
254         sounds_str = ""
255         if item[1].get("sounds") == None:
256             sounds_str = " " * 12 + "NULL,"
257         else:
258              for l_msg in item[1]["sounds"]:
259                  sounds_str += " " * 12 + make_c_string(l_msg) + ",\n"
260              sounds_str = sounds_str[:-1] # trim trailing newline
261         texts_str = ""
262         if item[1].get("texts") == None:
263             texts_str = " " * 12 + "NULL,"
264         else:
265              for l_msg in item[1]["texts"]:
266                  texts_str += " " * 12 + make_c_string(l_msg) + ",\n"
267              texts_str = texts_str[:-1] # trim trailing newline
268         treasure = "true" if item[1].get("treasure") else "false"
269         obj_str += template.format(i_msg, treasure, longs_str, sounds_str, texts_str)
270     obj_str = obj_str[:-1] # trim trailing newline
271     return obj_str
272
273 def get_obituaries(obit):
274     template = """    {{
275         .query = {},
276         .yes_response = {},
277     }},
278 """
279     obit_str = ""
280     for o in obit:
281         query = make_c_string(o["query"])
282         yes = make_c_string(o["yes_response"])
283         obit_str += template.format(query, yes)
284     obit_str = obit_str[:-1] # trim trailing newline
285     return obit_str
286
287 def get_hints(hnt, arb):
288     template = """    {{
289         .number = {},
290         .penalty = {},
291         .turns = {},
292         .question = {},
293         .hint = {},
294     }},
295 """
296     hnt_str = ""
297     md = dict(arb)
298     for member in hnt:
299         item = member["hint"]
300         number = item["number"]
301         penalty = item["penalty"]
302         turns = item["turns"]
303         question = make_c_string(md[item["question"]])
304         hint = make_c_string(md[item["hint"]])
305         hnt_str += template.format(number, penalty, turns, question, hint)
306     hnt_str = hnt_str[:-1] # trim trailing newline
307     return hnt_str
308
309 def get_condbits(locations):
310     cnd_str = ""
311     for (name, loc) in locations:
312         conditions = loc["conditions"]
313         hints = loc.get("hints") or []
314         flaglist = []
315         for flag in conditions:
316             if conditions[flag]:
317                 flaglist.append(flag)
318         line = "|".join([("(1<<COND_%s)" % f) for f in flaglist])
319         trail = "|".join([("(1<<COND_H%s)" % f['name']) for f in hints])
320         if trail:
321             line += "|" + trail
322         if line.startswith("|"):
323             line = line[1:]
324         if not line:
325             line = "0"
326         cnd_str += "    " + line + ",\t// " + name + "\n"
327     return cnd_str
328
329 if __name__ == "__main__":
330     with open(yaml_name, "r") as f:
331         db = yaml.load(f)
332
333     c = c_template.format(
334         h_name,
335         get_arbitrary_messages(db["arbitrary_messages"]),
336         get_class_messages(db["classes"]),
337         get_turn_thresholds(db["turn_thresholds"]),
338         get_locations(db["locations"]),
339         get_object_descriptions(db["object_descriptions"]),
340         get_obituaries(db["obituaries"]),
341         get_hints(db["hints"], db["arbitrary_messages"]),
342         get_condbits(db["locations"]),
343     )
344
345     h = h_template.format(
346         len(db["locations"]),
347         len(db["object_descriptions"]),
348         len(db["hints"]),
349         len(db["classes"]),
350         len(db["obituaries"]),
351         len(db["turn_thresholds"]),
352         get_refs(db["arbitrary_messages"]),
353         get_refs(db["locations"]),
354         get_refs(db["object_descriptions"]),
355         statedefines,
356     )
357
358     with open(h_name, "w") as hf:
359         hf.write(h)
360
361     with open(c_name, "w") as cf:
362         cf.write(c)
363
364 # end