1 /* src/server/configfile.c */
3 #include <stddef.h> /* size_t, NULL */
4 #include <stdio.h> /* sprintf() */
5 #include <stdint.h> /* uint8_t */
6 #include <stdlib.h> /* atoi(), free() */
7 #include <string.h> /* strcmp() */
8 #include "../common/err_try_fgets.h" /* err_line() */
9 #include "../common/parse_file.h" /* Context, EDIT_STARTED, set_val(),
10 * set_uint8(), parse_file()
12 #include "../common/rexit.h" /* exit_err() */
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. Used to
42 * have common functions run over structs of both types.
47 struct EntryHead * next;
52 /* Get tokens from "context" and, by their order (in the individual context and
53 * in subsequent calls of this function), interpret them as data to write into
54 * the MapObjAct / MapObjDef DB.
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 context->token0 of "ACTION" or "OBJECT"; or b) context->token0 is NULL.
61 static void tokens_into_entries(struct Context * context);
63 /* Start reading a new DB entry of "size" from tokens in "context" if ->token0
64 * matches "comparand". Set EDIT_STARTED in "flags" to mark beginning of new
65 * entry reading. Check that id of new entry in ->token1 has not already been
66 * used in DB starting at "entry_cmp".
68 static uint8_t new_entry(struct Context * context, char * comparand,
69 uint8_t * flags, size_t size,
70 struct EntryHead ** entry,
71 struct EntryHead * entry_cmp);
73 /* Write DB entry pointed to by "entry" to its appropriate location. */
74 static void write_if_entry(struct EntryHead ** entry,
75 struct EntryHead *** entry_p_p_p);
77 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
78 * MapObjDef DB entries.
80 static void test_corpse_ids();
82 /* Try to read tokens in "context" as members for the entry currently edited,
83 * which must be either "mod" or "moa". What member of which of the two is set
84 * depends on which of "object_flags" and "action_flags" has EDIT_STARTED set
85 * and on the key name of ->token0. Return 1 if interpretation succeeds, else 0.
87 * Note that MapObjAct entries' .name also determines their .func.
89 static uint8_t set_members(struct Context * context, uint8_t * object_flags,
90 uint8_t * action_flags, struct MapObjDef * mod,
91 struct MapObjAct * moa);
93 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
94 * .func from .name for set_members().
96 static uint8_t try_func_name(struct MapObjAct * moa,
97 char * name, void (* func) (struct MapObj *));
101 static void tokens_into_entries(struct Context * context)
103 char * str_act = "ACTION";
104 char * str_obj = "OBJECT";
105 static struct MapObjAct ** moa_p_p = &world.map_obj_acts;
106 static struct MapObjDef ** mod_p_p = &world.map_obj_defs;
107 static uint8_t action_flags = READY_ACT;
108 static uint8_t object_flags = READY_OBJ;
109 static struct EntryHead * moa = NULL;
110 static struct EntryHead * mod = NULL;
111 if ( !context->token0
112 || !strcmp(context->token0,str_act) || !strcmp(context->token0,str_obj))
114 char * err_fin = "Last definition block not finished yet.";
115 err_line((action_flags & READY_ACT) ^ READY_ACT,
116 context->line, context->err_pre, err_fin);
117 err_line((object_flags & READY_OBJ) ^ READY_OBJ,
118 context->line, context->err_pre, err_fin);
119 write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
120 write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
121 object_flags = action_flags = READY_OBJ;
124 && !( new_entry(context, str_act, &action_flags,
125 sizeof(struct MapObjAct), (struct EntryHead**) &moa,
126 (struct EntryHead *) world.map_obj_acts)
127 || new_entry(context, str_obj, &object_flags,
128 sizeof(struct MapObjDef), (struct EntryHead**) &mod,
129 (struct EntryHead *) world.map_obj_defs)
130 || set_members(context, &object_flags, &action_flags,
131 (struct MapObjDef *) mod, (struct MapObjAct *) moa)))
133 char * err_unknown = "Unknown argument.";
134 err_line(1, context->line, context->err_pre, err_unknown);
140 static uint8_t new_entry(struct Context * context, char * comparand,
141 uint8_t * flags, size_t size,
142 struct EntryHead ** entry,struct EntryHead * entry_cmp)
144 char * f_name = "new_entry()";
145 char * err_uni = "Declaration of ID already used.";
146 if (!strcmp(context->token0, comparand))
148 * flags = EDIT_STARTED;
149 * entry = try_malloc(size, f_name);
150 set_uint8(context, &((*entry)->id));
151 for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
153 err_line((*entry)->id == entry_cmp->id,
154 context->line, context->err_pre, err_uni);
163 static void write_if_entry(struct EntryHead ** entry,
164 struct EntryHead *** entry_p_p_p)
168 (* entry)->next = NULL;
169 ** entry_p_p_p = *entry;
170 * entry_p_p_p = &((*entry)->next);
171 * entry = NULL; /* So later runs of this don't re-append same entry. */
177 static void test_corpse_ids()
179 char * f_name = "test_corpse_ids()";
180 char * err_corpse_prefix = "In the object definition DB, one object corpse "
181 "ID does not reference any known object in the "
182 "DB. ID of responsible object: ";
183 char * err_corpse = try_malloc(strlen(err_corpse_prefix) + 3 + 1, f_name);
184 struct MapObjDef * test_entry_0 = world.map_obj_defs;
185 for (; test_entry_0; test_entry_0 = test_entry_0->next)
187 uint8_t corpse_id_found = 0;
188 struct MapObjDef * test_entry_1 = world.map_obj_defs;
189 for (; test_entry_1; test_entry_1 = test_entry_1->next)
191 if (test_entry_0->corpse_id == test_entry_1->id)
196 sprintf(err_corpse, "%s%d", err_corpse_prefix, test_entry_0->id);
197 exit_err(!corpse_id_found, err_corpse);
204 static uint8_t set_members(struct Context * context, uint8_t * object_flags,
205 uint8_t * action_flags, struct MapObjDef * mod,
206 struct MapObjAct * moa)
208 if ( * action_flags & EDIT_STARTED
209 && set_val(context, "NAME", action_flags,
210 NAME_SET, 's', (char *) &moa->name))
212 if (!( try_func_name(moa, "move", actor_move)
213 || try_func_name(moa, "pick_up", actor_pick)
214 || try_func_name(moa, "drop", actor_drop)
215 || try_func_name(moa, "use", actor_use)))
217 moa->func = actor_wait;
219 *action_flags = *action_flags | NAME_SET;
222 else if ( set_val(context, "NAME", object_flags,
223 NAME_SET, 's', (char *) &mod->name)
224 || set_val(context, "SYMBOL", object_flags,
225 SYMBOL_SET, 'c', (char *) &mod->char_on_map)
226 || set_val(context, "EFFORT", action_flags,
227 EFFORT_SET, '8', (char *) &moa->effort)
228 || set_val(context, "LIFEPOINTS", object_flags,
229 LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
230 || set_val(context, "CONSUMABLE", object_flags,
231 CONSUMABLE_SET, '8', (char *) &mod->consumable)
232 || set_val(context, "CORPSE_ID", object_flags,
233 CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
242 static uint8_t try_func_name(struct MapObjAct * moa,
243 char * name, void (* func) (struct MapObj *))
245 if (0 == strcmp(moa->name, name))
255 extern void read_config_file()
257 parse_file(world.path_config, tokens_into_entries);
258 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);