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