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