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) a NULL context->token0 is
60 * passed. This is interpreted as the end of the MapObjDef / MapObjAct DB read,
61 * so the appropriate cleanup flags are set and test_corpse_ids() is called.
63 static void tokens_into_entries(struct Context * context);
65 /* Start reading a new DB entry of "size" from tokens in "context" if ->token0
66 * matches "comparand". Set EDIT_STARTED in "flags" to mark beginning of new
67 * entry reading. Check that id of new entry in ->token1 has not already been
68 * used in DB starting at "entry_cmp".
70 static uint8_t new_entry(struct Context * context, 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 in "context" as members for the entry currently edited,
85 * which must be either "mod" or "moa". What member of which of the two is set
86 * depends on which of "object_flags" and "action_flags" has EDIT_STARTED set
87 * and on the key name of ->token0. Return 1 if interpretation succeeds, else 0.
89 * Note that MapObjAct entries' .name also determines their .func.
91 static uint8_t set_members(struct Context * context, 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(struct Context * context)
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 ( !context->token0
114 || !strcmp(context->token0,str_act) || !strcmp(context->token0,str_obj))
116 char * err_fin = "Last definition block not finished yet.";
117 err_line((action_flags & READY_ACT) ^ READY_ACT,
118 context->line, context->err_pre, err_fin);
119 err_line((object_flags & READY_OBJ) ^ READY_OBJ,
120 context->line, context->err_pre, err_fin);
121 write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
122 write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
123 object_flags = action_flags = READY_OBJ;
125 if (!context->token0)
127 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);
131 if (!( new_entry(context, str_act, &action_flags,
132 sizeof(struct MapObjAct), (struct EntryHead**) &moa,
133 (struct EntryHead *) world.map_obj_acts)
134 || new_entry(context, str_obj, &object_flags,
135 sizeof(struct MapObjDef), (struct EntryHead**) &mod,
136 (struct EntryHead *) world.map_obj_defs)
137 || set_members(context, &object_flags, &action_flags,
138 (struct MapObjDef *) mod, (struct MapObjAct *) moa)))
140 err_line(1, context->line, context->err_pre, "Unknown argument");
146 static uint8_t new_entry(struct Context * context, char * comparand,
147 uint8_t * flags, size_t size,
148 struct EntryHead ** entry,struct EntryHead * entry_cmp)
150 char * f_name = "new_entry()";
151 char * err_uni = "Declaration of ID already used.";
152 if (!strcmp(context->token0, comparand))
154 * flags = EDIT_STARTED;
155 * entry = try_malloc(size, f_name);
156 set_uint8(context, &((*entry)->id));
157 for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
159 err_line((*entry)->id == entry_cmp->id,
160 context->line, context->err_pre, err_uni);
169 static void write_if_entry(struct EntryHead ** entry,
170 struct EntryHead *** entry_p_p_p)
174 (* entry)->next = NULL;
175 ** entry_p_p_p = *entry;
176 * entry_p_p_p = &((*entry)->next);
177 * entry = NULL; /* So later runs of this don't re-append same entry. */
183 static void test_corpse_ids()
185 char * f_name = "test_corpse_ids()";
186 char * err_corpse_prefix = "In the object definition DB, one object corpse "
187 "ID does not reference any known object in the "
188 "DB. ID of responsible object: ";
189 char * err_corpse = try_malloc(strlen(err_corpse_prefix) + 3 + 1, f_name);
190 struct MapObjDef * test_entry_0 = world.map_obj_defs;
191 for (; test_entry_0; test_entry_0 = test_entry_0->next)
193 uint8_t corpse_id_found = 0;
194 struct MapObjDef * test_entry_1 = world.map_obj_defs;
195 for (; test_entry_1; test_entry_1 = test_entry_1->next)
197 if (test_entry_0->corpse_id == test_entry_1->id)
202 sprintf(err_corpse, "%s%d", err_corpse_prefix, test_entry_0->id);
203 exit_err(!corpse_id_found, err_corpse);
210 static uint8_t set_members(struct Context * context, uint8_t * object_flags,
211 uint8_t * action_flags, struct MapObjDef * mod,
212 struct MapObjAct * moa)
214 if ( * action_flags & EDIT_STARTED
215 && set_val(context, "NAME", action_flags,
216 NAME_SET, 's', (char *) &moa->name))
218 if (!( try_func_name(moa, "move", actor_move)
219 || try_func_name(moa, "pick_up", actor_pick)
220 || try_func_name(moa, "drop", actor_drop)
221 || try_func_name(moa, "use", actor_use)))
223 moa->func = actor_wait;
225 *action_flags = *action_flags | NAME_SET;
228 else if ( set_val(context, "NAME", object_flags,
229 NAME_SET, 's', (char *) &mod->name)
230 || set_val(context, "SYMBOL", object_flags,
231 SYMBOL_SET, 'c', (char *) &mod->char_on_map)
232 || set_val(context, "EFFORT", action_flags,
233 EFFORT_SET, '8', (char *) &moa->effort)
234 || set_val(context, "LIFEPOINTS", object_flags,
235 LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
236 || set_val(context, "CONSUMABLE", object_flags,
237 CONSUMABLE_SET, '8', (char *) &mod->consumable)
238 || set_val(context, "CORPSE_ID", object_flags,
239 CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
248 static uint8_t try_func_name(struct MapObjAct * moa,
249 char * name, void (* func) (struct MapObj *))
251 if (0 == strcmp(moa->name, name))
261 extern void read_config_file()
263 parse_file(world.path_config, tokens_into_entries);