1 /* src/server/configfile.c */
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(),
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
19 #include "hardcoded_strings.h" /* s */
20 #include "thing_actions.h" /* ThingAction */
21 #include "things.h" /* Thing, ThingType */
22 #include "world.h" /* world global */
26 /* Flags defining state of thing type and action entry reading ((un-)finished /
27 * ready for starting the reading of a new definition etc.)
37 LIFEPOINTS_SET = 0x10,
38 CONSUMABLE_SET = 0x20,
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
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.
53 struct EntryHead * next;
58 /* Interpret "token0" and "token1" as data to write into the ThingAction /
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.
66 * Also check against the line parse_file() read tokens from having more tokens.
68 static void tokens_into_entries(char * token0, char * token1);
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".
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);
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);
84 /* Ensure that all .corpse_id members in the ThingType DB fit .id members of
85 * ThingType DB entries.
87 static void test_corpse_ids();
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);
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);
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.
100 * Note that ThingAction entries' .name also determines their .func.
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);
106 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
107 * .func from .name for set_members().
109 static uint8_t try_func_name(struct ThingAction * ta,
110 char * name, void (* func) (struct Thing *));
114 static void tokens_into_entries(char * token0, char * token1)
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))
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);
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))
141 err_line(0 == atoi(token1), "Value must not be 0.");
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)))
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)
165 if (strcmp(token0, comparand))
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)
174 parse_id_uniq((*entry)->id == entry_cmp->id);
181 static void write_if_entry(struct EntryHead ** entry,
182 struct EntryHead *** entry_p_p_p)
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. */
195 static void test_corpse_ids()
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)
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)
210 if (test_entry_0->corpse_id == test_entry_1->id)
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);
224 static uint8_t set_player_type(char * token0, char * comparand, char * token1)
226 if (strcmp(token0, comparand))
230 parsetest_int(token1, '8');
231 world.player_type = atoi(token1);
237 static uint8_t set_map_length(char * token0, char * comparand, char * token1)
239 if (strcmp(token0, comparand))
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);
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)
256 if ( *action_flags & EDIT_STARTED
257 && parse_flagval(token0, token1, "NAME", action_flags,
258 NAME_SET, 's', (char *) &ta->name))
260 if (!( try_func_name(ta, s[S_CMD_MOVE], actor_move)
261 || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
262 || try_func_name(ta, s[S_CMD_DROP], actor_drop)
263 || try_func_name(ta, s[S_CMD_USE], actor_use)))
265 ta->func = actor_wait;
267 *action_flags = *action_flags | NAME_SET;
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))
292 static uint8_t try_func_name(struct ThingAction * ta, char * name,
293 void (* func) (struct Thing *))
295 if (0 == strcmp(ta->name, name))
305 extern void read_config_file()
307 parse_file(s[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)
313 if (world.player_type == tt->id)
315 player_type_is_valid = 1;
319 exit_err(!player_type_is_valid, "No valid thing type set for player.");
320 set_cleanup_flag(CLEANUP_THING_ACTIONS | CLEANUP_THING_TYPES);