home · contact · privacy
14464396dc7e93ff6af93396d8a0faee0e2f78bf
[plomrogue] / src / server / configfile.c
1 /* src/server/configfile.c */
2
3 #include <stddef.h> /* size_t, NULL */
4 #include <stdio.h> /* snprintf() */
5 #include <stdint.h> /* uint8_t */
6 #include <stdlib.h> /* atoi(), free() */
7 #include <string.h> /* strcmp() */
8 #include "../common/parse_file.h" /* EDIT_STARTED, parsetest_int(),parse_file(),
9                                    * parsetest_too_many_values(),parse_id_uniq()
10                                    * parse_unknown_arg(), parse_init_entry(),
11                                    * parse_and_reduce_to_readyflag(),parse_val()
12                                    */
13 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS,
16                       * CLEANUP_MAP_OBJ_ACTS
17                       */
18 #include "map_object_actions.h" /* MapObjAct */
19 #include "map_objects.h" /* MapObj, MapObjDef, struct MapObjDef */
20 #include "world.h" /* world global */
21
22
23
24 /* Flags defining state of object and action entry reading ((un-)finished /
25  * ready for starting the reading of a new definition etc.)
26  */
27 enum flag
28 {
29     HEIGHT_SET     = 0x02,
30     WIDTH_SET      = 0x04,
31     NAME_SET       = 0x02,
32     EFFORT_SET     = 0x04,
33     CORPSE_ID_SET  = 0x04,
34     SYMBOL_SET     = 0x08,
35     LIFEPOINTS_SET = 0x10,
36     CONSUMABLE_SET = 0x20,
37     START_N_SET    = 0x40,
38     READY_ACT = NAME_SET | EFFORT_SET,
39     READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET
40                 | CONSUMABLE_SET | START_N_SET,
41     READY_MAP = HEIGHT_SET | WIDTH_SET
42 };
43
44
45
46 /* What MapObjDef and MapObjAct structs have in common at their top. Use this to
47  * allow same functions run over structs of both types.
48  */
49 struct EntryHead
50 {
51     uint8_t id;
52     struct EntryHead * next;
53 };
54
55
56
57 /* Interpret "token0" and "token1" as data to write into the MapObjAct /
58  * MapObjDef DB.
59  *
60  * Individual MapObjDef / MapObjAct DB entries are put together line by line
61  * before being written. Writing only happens after all necessary members of an
62  * entry have been assembled, and when additionally a) a new entry is started by
63  * a "token0" of "ACTION" or "OBJECT"; or b) "token0" is NULL.
64  *
65  * Also check against the line parse_file() read tokens from having more tokens.
66  */
67 static void tokens_into_entries(char * token0, char * token1);
68
69 /* Start reading a new DB entry of "size" from tokens if "token0" matches
70  * "comparand". Set EDIT_STARTED in "flags" to mark beginning of new entry
71  * reading. Check that "token1" id of new entry has not already been used in DB
72  * starting at "entry_cmp".
73  */
74 static uint8_t start_entry(char * token0, char * token1, char * comparand,
75                          uint8_t * flags, size_t size,
76                          struct EntryHead ** entry,
77                          struct EntryHead * entry_cmp);
78
79 /* Start reading map definition if "token0" matches "comparand". Set "map_flags"
80  * to EDIT_STARTED to mark beginning of map definition reading.
81  */
82 static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags);
83
84 /* Write DB entry pointed to by "entry" to its appropriate location. */
85 static void write_if_entry(struct EntryHead ** entry,
86                            struct EntryHead *** entry_p_p_p);
87
88 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
89  * MapObjDef DB entries.
90  */
91 static void test_corpse_ids();
92
93 /* set_members() helper specifically for editing world.map members. */
94 static uint8_t set_map_members(char * token0,char * token1,uint8_t * map_flags);
95
96 /* If "token0" matches "comparand", set world.enemy_fov to "token1". */
97 static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1);
98
99 /* If "token0" matches "comparand", set world.player_type to int in "token1". */
100 static uint8_t set_player_type(char * token0, char * comparand, char * token1);
101
102 /* Try to read tokens as members for the definition currently edited, which may
103  * be "mod" or "moa" or that of world.map. What member of which of the three is
104  * set depends on which of the flags has EDIT_STARTED set and on the key name in
105  * "token0". Return 1 if interpretation succeeds, else 0.
106  *
107  * Note that MapObjAct entries' .name also determines their .func.
108  */
109 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
110                            uint8_t * action_flags, uint8_t * map_flags,
111                            struct MapObjDef * mod, struct MapObjAct * moa);
112
113 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
114  * .func from .name for set_members().
115  */
116 static uint8_t try_func_name(struct MapObjAct * moa,
117                              char * name, void (* func) (struct MapObj *));
118
119
120
121 static void tokens_into_entries(char * token0, char * token1)
122 {
123     char * str_act = "ACTION";
124     char * str_obj = "OBJECT";
125     char * str_map = "MAP_TYPE";
126     char * str_player = "PLAYER_TYPE";
127     char * str_enemyfov = "ENEMY_FOV";
128     static struct MapObjAct ** moa_p_p = &world.map_obj_acts;
129     static struct MapObjDef ** mod_p_p = &world.map_obj_defs;
130     static uint8_t action_flags = READY_ACT;
131     static uint8_t object_flags = READY_OBJ;
132     static uint8_t map_flags = READY_MAP;
133     static struct EntryHead * moa = NULL;
134     static struct EntryHead * mod = NULL;
135     if (!token0 || !strcmp(token0, str_act) || !strcmp(token0, str_obj)
136                 || !strcmp(token0, str_map) || !strcmp(token0, str_player)
137                 || !strcmp(token0, str_enemyfov))
138     {
139         parse_and_reduce_to_readyflag(&action_flags, READY_ACT);
140         parse_and_reduce_to_readyflag(&object_flags, READY_OBJ);
141         parse_and_reduce_to_readyflag(&map_flags, READY_MAP);
142         write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
143         write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
144     }
145     if (token0)
146     {
147         parsetest_too_many_values();
148         if (start_entry(token0, token1, str_act, &action_flags,
149                         sizeof(struct MapObjAct), (struct EntryHead**) &moa,
150                         (struct EntryHead *) world.map_obj_acts))
151         {
152             err_line(0 == atoi(token1), "Value must not be 0.");
153         }
154         else if (!(   start_entry(token0, token1, str_obj, &object_flags,
155                                   sizeof(struct MapObjDef),
156                                   (struct EntryHead**) &mod,
157                                   (struct EntryHead *) world.map_obj_defs)
158                    || start_map(token0, str_map, &map_flags)
159                    || set_player_type(token0, str_player, token1)
160                    || set_enemy_fov(token0, str_enemyfov, token1)
161                    || set_members(token0, token1, &object_flags, &action_flags,
162                                   &map_flags, (struct MapObjDef *)mod,
163                                   (struct MapObjAct *) moa)))
164         {
165             parse_unknown_arg();
166         }
167     }
168 }
169
170
171
172 static uint8_t start_entry(char * token0, char * token1, char * comparand,
173                            uint8_t * flags, size_t size,
174                            struct EntryHead ** entry,
175                            struct EntryHead * entry_cmp)
176 {
177     if (strcmp(token0, comparand))
178     {
179         return 0;
180     }
181     *entry = (struct EntryHead *) parse_init_entry(flags, size);
182     parsetest_int(token1, '8');
183     (*entry)-> id = atoi(token1);
184     for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
185     {
186         parse_id_uniq((*entry)->id == entry_cmp->id);
187     }
188     return 1;
189 }
190
191
192
193 static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags)
194 {
195     if (strcmp(token0, comparand))
196     {
197         return 0;
198     }
199     *map_flags = EDIT_STARTED;
200     return 1;
201 }
202
203
204
205 static void write_if_entry(struct EntryHead ** entry,
206                            struct EntryHead *** entry_p_p_p)
207 {
208     if (*entry)
209     {
210         (*entry)->next = NULL;
211         **entry_p_p_p = *entry;
212         *entry_p_p_p = &((*entry)->next);
213         *entry = NULL;   /* So later runs of this don't re-append same entry. */
214     }
215 }
216
217
218
219 static void test_corpse_ids()
220 {
221     char * f_name = "test_corpse_ids()";
222     char * prefix = "In the object definitions DB, one object corpse ID does "
223                     "not reference any known object in the DB. ID of "
224                     "responsible object: ";
225     size_t size = strlen(prefix) + 3 + 1; /* 3: uint8_t representation strlen */
226     char * err_corpse = try_malloc(size, f_name);
227     struct MapObjDef * test_entry_0 = world.map_obj_defs;
228     for (; test_entry_0; test_entry_0 = test_entry_0->next)
229     {
230         uint8_t corpse_id_found = 0;
231         struct MapObjDef * test_entry_1 = world.map_obj_defs;
232         for (; test_entry_1; test_entry_1 = test_entry_1->next)
233         {
234             if (test_entry_0->corpse_id == test_entry_1->id)
235             {
236                 corpse_id_found = 1;
237             }
238         }
239         int test = snprintf(err_corpse, size, "%s%d", prefix, test_entry_0->id);
240         exit_trouble(test < 0, f_name, "snprintf()");
241         exit_err(!corpse_id_found, err_corpse);
242     }
243     free(err_corpse);
244 }
245
246
247
248 static uint8_t set_map_members(char * token0, char * token1,uint8_t * map_flags)
249 {
250     if      (   parse_val(token0, token1, "HEIGHT", map_flags,
251                           HEIGHT_SET, 'i', (char *) &world.map.size.y)
252              || parse_val(token0, token1, "WIDTH", map_flags,
253                           WIDTH_SET, 'i', (char *) &world.map.size.x))
254     {
255         int test = atoi(token1) > 256 || atoi(token1) < 1;
256         err_line(test, "Value must be >= 1 and <= 256.");
257         return 1;
258     }
259     return 0;
260 }
261
262
263
264 static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1)
265 {
266     if (strcmp(token0, comparand))
267     {
268         return 0;
269     }
270     parsetest_int(token1, '8');
271     int test = atoi(token1) > 1;
272     err_line(test, "Value must be 0 or 1.");
273     world.enemy_fov = atoi(token1);
274     return 1;
275 }
276
277
278
279 static uint8_t set_player_type(char * token0, char * comparand, char * token1)
280 {
281     if (strcmp(token0, comparand))
282     {
283         return 0;
284     }
285     parsetest_int(token1, '8');
286     world.player_type = atoi(token1);
287     return 1;
288 }
289
290
291
292 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
293                            uint8_t * action_flags, uint8_t * map_flags,
294                            struct MapObjDef * mod, struct MapObjAct * moa)
295 {
296     if (   *action_flags & EDIT_STARTED
297         && parse_val(token0, token1, "NAME", action_flags,
298                      NAME_SET, 's', (char *) &moa->name))
299     {
300         if (!(   try_func_name(moa, "move", actor_move)
301               || try_func_name(moa, "pick_up", actor_pick)
302               || try_func_name(moa, "drop", actor_drop)
303               || try_func_name(moa, "use", actor_use)))
304         {
305             moa->func = actor_wait;
306         }
307         *action_flags = *action_flags | NAME_SET;
308         return 1;
309     }
310     else if (   set_map_members(token0, token1, map_flags)
311              || parse_val(token0, token1, "NAME", object_flags,
312                           NAME_SET, 's', (char *) &mod->name)
313              || parse_val(token0, token1, "SYMBOL", object_flags,
314                           SYMBOL_SET, 'c', (char *) &mod->char_on_map)
315              || parse_val(token0, token1, "EFFORT", action_flags,
316                           EFFORT_SET, '8', (char *) &moa->effort)
317              || parse_val(token0, token1, "START_NUMBER", object_flags,
318                           START_N_SET, '8', (char *) &mod->start_n)
319              || parse_val(token0, token1, "LIFEPOINTS", object_flags,
320                           LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
321              || parse_val(token0, token1, "CONSUMABLE", object_flags,
322                           CONSUMABLE_SET, '8', (char *) &mod->consumable)
323              || parse_val(token0, token1, "CORPSE_ID", object_flags,
324                           CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
325     {
326         return 1;
327     }
328     return 0;
329 }
330
331
332
333 static uint8_t try_func_name(struct MapObjAct * moa, char * name,
334                              void (* func) (struct MapObj *))
335 {
336     if (0 == strcmp(moa->name, name))
337     {
338         moa->func = func;
339         return 1;
340     }
341     return 0;
342 }
343
344
345
346 extern void read_config_file()
347 {
348     parse_file(world.path_config, tokens_into_entries);
349     exit_err(!world.map.size.y, "Map not defined in config file.");
350     uint8_t player_type_is_valid = 0;
351     struct MapObjDef * mod;
352     for (mod = world.map_obj_defs; NULL != mod; mod = mod->next)
353     {
354         if (world.player_type == mod->id)
355         {
356             player_type_is_valid = 1;
357             break;
358         }
359     }
360     exit_err(!player_type_is_valid, "No valid map object type set for player.");
361     set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);
362     test_corpse_ids();
363 }