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(),parse_val()
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
18 #include "map_object_actions.h" /* struct MapObjAct */
19 #include "map_objects.h" /* struct MapObj, struct MapObjDef */
20 #include "world.h" /* world global */
24 /* Flags defining state of object and action entry reading ((un-)finished /
25 * ready for starting the reading of a new definition etc.)
33 LIFEPOINTS_SET = 0x10,
34 CONSUMABLE_SET = 0x20,
35 READY_ACT = NAME_SET | EFFORT_SET,
36 READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET
42 /* What MapObjDef and MapObjAct structs have in common at their top. Use this to
43 * allow same functions run over structs of both types.
48 struct EntryHead * next;
53 /* Interpret "token0" and "token1" as data to write into the MapObjAct /
56 * Individual MapObjDef / MapObjAct DB entries are put together line by line
57 * before being written. Writing only happens after all necessary members of an
58 * entry have been assembled, and when additionally a) a new entry is started by
59 * a "token0" of "ACTION" or "OBJECT"; or b) "token0" is NULL.
61 * Also check against the line parse_file() read tokens from having more tokens.
63 static void tokens_into_entries(char * token0, char * token1);
65 /* Start reading a new DB entry of "size" from tokens if "token0" matches
66 * "comparand". Set EDIT_STARTED in "flags" to mark beginning of new entry
67 * reading. Check that "token1" id of new entry has not already been used in DB
68 * starting at "entry_cmp".
70 static uint8_t start_entry(char * token0, char * token1, char * comparand,
71 uint8_t * flags, size_t size,
72 struct EntryHead ** entry,
73 struct EntryHead * entry_cmp);
75 /* Write DB entry pointed to by "entry" to its appropriate location. */
76 static void write_if_entry(struct EntryHead ** entry,
77 struct EntryHead *** entry_p_p_p);
79 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
80 * MapObjDef DB entries.
82 static void test_corpse_ids();
84 /* Try to read tokens as members for the entry currently edited, which must be
85 * either "mod" or "moa". What member of which of the two is set depends on
86 * which of "object_flags" and "action_flags" has EDIT_STARTED set and on the
87 * key name in "token0". Return 1 if interpretation succeeds, else 0.
89 * Note that MapObjAct entries' .name also determines their .func.
91 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
92 uint8_t * action_flags, struct MapObjDef * mod,
93 struct MapObjAct * moa);
95 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
96 * .func from .name for set_members().
98 static uint8_t try_func_name(struct MapObjAct * moa,
99 char * name, void (* func) (struct MapObj *));
103 static void tokens_into_entries(char * token0, char * token1)
105 char * str_act = "ACTION";
106 char * str_obj = "OBJECT";
107 static struct MapObjAct ** moa_p_p = &world.map_obj_acts;
108 static struct MapObjDef ** mod_p_p = &world.map_obj_defs;
109 static uint8_t action_flags = READY_ACT;
110 static uint8_t object_flags = READY_OBJ;
111 static struct EntryHead * moa = NULL;
112 static struct EntryHead * mod = NULL;
113 if (!token0 || !strcmp(token0,str_act) || !strcmp(token0,str_obj))
115 parse_and_reduce_to_readyflag(&action_flags, READY_ACT);
116 parse_and_reduce_to_readyflag(&object_flags, READY_OBJ);
117 write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
118 write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
122 parsetest_too_many_values();
123 if (!( start_entry(token0, token1, str_act, &action_flags,
124 sizeof(struct MapObjAct),(struct EntryHead**) &moa,
125 (struct EntryHead *) world.map_obj_acts)
126 || start_entry(token0, token1, str_obj, &object_flags,
127 sizeof(struct MapObjDef),(struct EntryHead**) &mod,
128 (struct EntryHead *) world.map_obj_defs)
129 || set_members(token0, token1, &object_flags, &action_flags,
130 (struct MapObjDef *)mod,(struct MapObjAct *) moa)))
139 static uint8_t start_entry(char * token0, char * token1, char * comparand,
140 uint8_t * flags, size_t size,
141 struct EntryHead ** entry,
142 struct EntryHead * entry_cmp)
144 if (strcmp(token0, comparand))
148 *entry = (struct EntryHead *) parse_init_entry(flags, size);
149 parsetest_int(token1, '8');
150 (*entry)-> id = atoi(token1);
151 for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
153 parse_id_uniq((*entry)->id == entry_cmp->id);
160 static void write_if_entry(struct EntryHead ** entry,
161 struct EntryHead *** entry_p_p_p)
165 (*entry)->next = NULL;
166 **entry_p_p_p = *entry;
167 *entry_p_p_p = &((*entry)->next);
168 *entry = NULL; /* So later runs of this don't re-append same entry. */
174 static void test_corpse_ids()
176 char * f_name = "test_corpse_ids()";
177 char * prefix = "In the object definitions DB, one object corpse ID does "
178 "not reference any known object in the DB. ID of "
179 "responsible object: ";
180 size_t size = strlen(prefix) + 3 + 1; /* 3: uint8_t representation strlen */
181 char * err_corpse = try_malloc(size, f_name);
182 struct MapObjDef * test_entry_0 = world.map_obj_defs;
183 for (; test_entry_0; test_entry_0 = test_entry_0->next)
185 uint8_t corpse_id_found = 0;
186 struct MapObjDef * test_entry_1 = world.map_obj_defs;
187 for (; test_entry_1; test_entry_1 = test_entry_1->next)
189 if (test_entry_0->corpse_id == test_entry_1->id)
194 int test = snprintf(err_corpse, size, "%s%d", prefix, test_entry_0->id);
195 exit_trouble(test < 0, f_name, "snprintf()");
196 exit_err(!corpse_id_found, err_corpse);
203 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
204 uint8_t * action_flags, struct MapObjDef * mod,
205 struct MapObjAct * moa)
207 if ( *action_flags & EDIT_STARTED
208 && parse_val(token0, token1, "NAME", action_flags,
209 NAME_SET, 's', (char *) &moa->name))
211 if (!( try_func_name(moa, "move", actor_move)
212 || try_func_name(moa, "pick_up", actor_pick)
213 || try_func_name(moa, "drop", actor_drop)
214 || try_func_name(moa, "use", actor_use)))
216 moa->func = actor_wait;
218 *action_flags = *action_flags | NAME_SET;
221 else if ( parse_val(token0, token1, "NAME", object_flags,
222 NAME_SET, 's', (char *) &mod->name)
223 || parse_val(token0, token1, "SYMBOL", object_flags,
224 SYMBOL_SET, 'c', (char *) &mod->char_on_map)
225 || parse_val(token0, token1, "EFFORT", action_flags,
226 EFFORT_SET, '8', (char *) &moa->effort)
227 || parse_val(token0, token1, "LIFEPOINTS", object_flags,
228 LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
229 || parse_val(token0, token1, "CONSUMABLE", object_flags,
230 CONSUMABLE_SET, '8', (char *) &mod->consumable)
231 || parse_val(token0, token1, "CORPSE_ID", object_flags,
232 CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
241 static uint8_t try_func_name(struct MapObjAct * moa, char * name,
242 void (* func) (struct MapObj *))
244 if (0 == strcmp(moa->name, name))
254 extern void read_config_file()
256 parse_file(world.path_config, tokens_into_entries);
257 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);