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