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, set_val(), test_for_int(),
9 * err_line(), parse_file(),token_from_line(),
10 * finalize_by_readyflag()
12 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS,
15 * CLEANUP_MAP_OBJ_ACTS
17 #include "map_object_actions.h" /* struct MapObjAct */
18 #include "map_objects.h" /* struct MapObj, struct MapObjDef */
19 #include "world.h" /* world global */
23 /* Flags defining state of object and action entry reading ((un-)finished /
24 * ready for starting the reading of a new definition etc.)
32 LIFEPOINTS_SET = 0x10,
33 CONSUMABLE_SET = 0x20,
34 READY_ACT = NAME_SET | EFFORT_SET,
35 READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET
41 /* What MapObjDef and MapObjAct structs have in common at their top. Use this to
42 * allow same functions run over structs of both types.
47 struct EntryHead * next;
52 /* Interpret "token0" and "token1" as data to write into the MapObjAct /
55 * Individual MapObjDef / MapObjAct DB entries are put together line by line
56 * before being written. Writing only happens after all necessary members of an
57 * entry have been assembled, and when additionally a) a new entry is started by
58 * a "token0" of "ACTION" or "OBJECT"; or b) "token0" is NULL.
60 * Also check against the line parse_file() read tokens from having more tokens.
62 static void tokens_into_entries(char * token0, char * token1);
64 /* Start reading a new DB entry of "size" from tokens if "token0" matches
65 * "comparand". Set EDIT_STARTED in "flags" to mark beginning of new entry
66 * reading. Check that "token1" id of new entry has not already been used in DB
67 * starting at "entry_cmp".
69 static uint8_t new_entry(char * token0, char * token1, char * comparand,
70 uint8_t * flags, size_t size,
71 struct EntryHead ** entry,
72 struct EntryHead * entry_cmp);
74 /* Write DB entry pointed to by "entry" to its appropriate location. */
75 static void write_if_entry(struct EntryHead ** entry,
76 struct EntryHead *** entry_p_p_p);
78 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
79 * MapObjDef DB entries.
81 static void test_corpse_ids();
83 /* Try to read tokens as members for the entry currently edited, which must be
84 * either "mod" or "moa". What member of which of the two is set depends on
85 * which of "object_flags" and "action_flags" has EDIT_STARTED set and on the
86 * key name in "token0". Return 1 if interpretation succeeds, else 0.
88 * Note that MapObjAct entries' .name also determines their .func.
90 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
91 uint8_t * action_flags, struct MapObjDef * mod,
92 struct MapObjAct * moa);
94 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
95 * .func from .name for set_members().
97 static uint8_t try_func_name(struct MapObjAct * moa,
98 char * name, void (* func) (struct MapObj *));
102 static void tokens_into_entries(char * token0, char * token1)
104 char * str_act = "ACTION";
105 char * str_obj = "OBJECT";
106 static struct MapObjAct ** moa_p_p = &world.map_obj_acts;
107 static struct MapObjDef ** mod_p_p = &world.map_obj_defs;
108 static uint8_t action_flags = READY_ACT;
109 static uint8_t object_flags = READY_OBJ;
110 static struct EntryHead * moa = NULL;
111 static struct EntryHead * mod = NULL;
112 if (!token0 || !strcmp(token0,str_act) || !strcmp(token0,str_obj))
114 finalize_by_readyflag(&action_flags, READY_ACT);
115 finalize_by_readyflag(&object_flags, READY_OBJ);
116 write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
117 write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
119 err_line(token0 && NULL != token_from_line(NULL), "Too many values.");
121 && !( new_entry(token0, token1, str_act, &action_flags,
122 sizeof(struct MapObjAct), (struct EntryHead**) &moa,
123 (struct EntryHead *) world.map_obj_acts)
124 || new_entry(token0, token1, str_obj, &object_flags,
125 sizeof(struct MapObjDef), (struct EntryHead**) &mod,
126 (struct EntryHead *) world.map_obj_defs)
127 || set_members(token0, token1, &object_flags, &action_flags,
128 (struct MapObjDef *) mod, (struct MapObjAct *) moa)))
130 err_line(1, "Unknown argument.");
136 static uint8_t new_entry(char * token0, char * token1, char * comparand,
137 uint8_t * flags, size_t size,
138 struct EntryHead ** entry,struct EntryHead * entry_cmp)
140 char * f_name = "new_entry()";
141 if (!strcmp(token0, comparand))
143 char * err_uniq = "Declaration of ID already used.";
144 * flags = EDIT_STARTED;
145 * entry = try_malloc(size, f_name);
146 test_for_int(token1, '8');
147 (*entry)-> id = atoi(token1);
148 for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
150 err_line((*entry)->id == entry_cmp->id, err_uniq);
159 static void write_if_entry(struct EntryHead ** entry,
160 struct EntryHead *** entry_p_p_p)
164 (*entry)->next = NULL;
165 **entry_p_p_p = *entry;
166 *entry_p_p_p = &((*entry)->next);
167 *entry = NULL; /* So later runs of this don't re-append same entry. */
173 static void test_corpse_ids()
175 char * f_name = "test_corpse_ids()";
176 char * prefix = "In the object definitions DB, one object corpse ID does "
177 "not reference any known object in the DB. ID of "
178 "responsible object: ";
179 size_t size = strlen(prefix) + 3 + 1; /* 3: uint8_t representation strlen */
180 char * err_corpse = try_malloc(size, f_name);
181 struct MapObjDef * test_entry_0 = world.map_obj_defs;
182 for (; test_entry_0; test_entry_0 = test_entry_0->next)
184 uint8_t corpse_id_found = 0;
185 struct MapObjDef * test_entry_1 = world.map_obj_defs;
186 for (; test_entry_1; test_entry_1 = test_entry_1->next)
188 if (test_entry_0->corpse_id == test_entry_1->id)
193 int test = snprintf(err_corpse, size, "%s%d", prefix, test_entry_0->id);
194 exit_trouble(test < 0, f_name, "snprintf()");
195 exit_err(!corpse_id_found, err_corpse);
202 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
203 uint8_t * action_flags, struct MapObjDef * mod,
204 struct MapObjAct * moa)
206 if ( *action_flags & EDIT_STARTED
207 && set_val(token0, token1, "NAME", action_flags,
208 NAME_SET, 's', (char *) &moa->name))
210 if (!( try_func_name(moa, "move", actor_move)
211 || try_func_name(moa, "pick_up", actor_pick)
212 || try_func_name(moa, "drop", actor_drop)
213 || try_func_name(moa, "use", actor_use)))
215 moa->func = actor_wait;
217 *action_flags = *action_flags | NAME_SET;
220 else if ( set_val(token0, token1, "NAME", object_flags,
221 NAME_SET, 's', (char *) &mod->name)
222 || set_val(token0, token1, "SYMBOL", object_flags,
223 SYMBOL_SET, 'c', (char *) &mod->char_on_map)
224 || set_val(token0, token1, "EFFORT", action_flags,
225 EFFORT_SET, '8', (char *) &moa->effort)
226 || set_val(token0, token1, "LIFEPOINTS", object_flags,
227 LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
228 || set_val(token0, token1, "CONSUMABLE", object_flags,
229 CONSUMABLE_SET, '8', (char *) &mod->consumable)
230 || set_val(token0, token1, "CORPSE_ID", object_flags,
231 CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
240 static uint8_t try_func_name(struct MapObjAct * moa, char * name,
241 void (* func) (struct MapObj *))
243 if (0 == strcmp(moa->name, name))
253 extern void read_config_file()
255 parse_file(world.path_config, tokens_into_entries);
256 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);