home · contact · privacy
Client: Fit interface_conf to new config file style. Also, refactorings.
[plomrogue] / src / server / configfile.c
1 /* src/server/configfile.c */
2
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                                    */
11 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS,
14                       * CLEANUP_MAP_OBJ_ACTS
15                       */
16 #include "map_object_actions.h" /* struct MapObjAct */
17 #include "map_objects.h" /* struct MapObj, struct MapObjDef */
18 #include "world.h" /* world global */
19
20
21
22 /* Flags defining state of object and action entry reading ((un-)finished /
23  * ready for starting the reading of a new definition etc.)
24  */
25 enum flag
26 {
27     NAME_SET       = 0x02,
28     EFFORT_SET     = 0x04,
29     CORPSE_ID_SET  = 0x04,
30     SYMBOL_SET     = 0x08,
31     LIFEPOINTS_SET = 0x10,
32     CONSUMABLE_SET = 0x20,
33     READY_ACT = NAME_SET | EFFORT_SET,
34     READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET
35                 | CONSUMABLE_SET
36 };
37
38
39
40 /* What MapObjDef and MapObjAct structs have in common at their top. Use this to
41  * allow same functions run over structs of both types.
42  */
43 struct EntryHead
44 {
45     uint8_t id;
46     struct EntryHead * next;
47 };
48
49
50
51 /* Interpret "token0" and "token1" as data to write into the MapObjAct /
52  * MapObjDef DB.
53  *
54  * Individual MapObjDef / MapObjAct DB entries are put together line by line
55  * before being written. Writing only happens after all necessary members of an
56  * entry have been assembled, and when additionally a) a new entry is started by
57  * a "token0" of "ACTION" or "OBJECT"; or b) "token0" is NULL.
58  *
59  * Also check against the line parse_file() read tokens from having more tokens.
60  */
61 static void tokens_into_entries(char * token0, char * token1);
62
63 /* Start reading a new DB entry of "size" from tokens if "token0" matches
64  * "comparand". Set EDIT_STARTED in "flags" to mark beginning of new entry
65  * reading. Check that "token1" id of new entry has not already been used in DB
66  * starting at "entry_cmp".
67  */
68 static uint8_t new_entry(char * token0, char * token1, char * comparand,
69                          uint8_t * flags, size_t size,
70                          struct EntryHead ** entry,
71                          struct EntryHead * entry_cmp);
72
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);
76
77 /* Ensure that all .corpse_id members in the MapObjDef DB fit .id members of
78  * MapObjDef DB entries.
79  */
80 static void test_corpse_ids();
81
82 /* Try to read tokens as members for the entry currently edited, which must be
83  * either "mod" or "moa". What member of which of the two is set depends on
84  * which of "object_flags" and "action_flags" has EDIT_STARTED set and on the
85  * key name in "token0". Return 1 if interpretation succeeds, else 0.
86  *
87  * Note that MapObjAct entries' .name also determines their .func.
88  */
89 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
90                            uint8_t * action_flags, struct MapObjDef * mod,
91                            struct MapObjAct * moa);
92
93 /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct
94  * .func from .name for set_members().
95  */
96 static uint8_t try_func_name(struct MapObjAct * moa,
97                              char * name, void (* func) (struct MapObj *));
98
99
100
101 static void tokens_into_entries(char * token0, char * token1)
102 {
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 (!token0 || !strcmp(token0,str_act) || !strcmp(token0,str_obj))
112     {
113         err_line(   ((action_flags & READY_ACT) ^ READY_ACT)
114                  || ((object_flags & READY_OBJ) ^ READY_OBJ),
115                  "Last definitino block not finished yet.");
116         write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
117         write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
118         action_flags = READY_ACT;
119         object_flags = READY_OBJ;
120     }
121     err_line(token0 && NULL != token_from_line(NULL), "Too many values.");
122     if (   token0
123         && !(   new_entry(token0, token1, str_act, &action_flags,
124                           sizeof(struct MapObjAct), (struct EntryHead**) &moa,
125                           (struct EntryHead *) world.map_obj_acts)
126              || new_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)))
131     {
132         err_line(1, "Unknown argument.");
133     }
134 }
135
136
137
138 static uint8_t new_entry(char * token0, char * token1, char * comparand,
139                          uint8_t * flags, size_t size,
140                          struct EntryHead ** entry,struct EntryHead * entry_cmp)
141 {
142     char * f_name = "new_entry()";
143     if (!strcmp(token0, comparand))
144     {
145         char * err_uniq = "Declaration of ID already used.";
146         * flags = EDIT_STARTED;
147         * entry = try_malloc(size, f_name);
148         test_for_int(token1, '8');
149         (*entry)-> id = atoi(token1);
150         for (; NULL != entry_cmp; entry_cmp = entry_cmp->next)
151         {
152             err_line((*entry)->id == entry_cmp->id, err_uniq);
153         }
154         return 1;
155     }
156     return 0;
157 }
158
159
160
161 static void write_if_entry(struct EntryHead ** entry,
162                            struct EntryHead *** entry_p_p_p)
163 {
164     if (*entry)
165     {
166         (*entry)->next = NULL;
167         **entry_p_p_p = *entry;
168         *entry_p_p_p = &((*entry)->next);
169         *entry = NULL;   /* So later runs of this don't re-append same entry. */
170     }
171 }
172
173
174
175 static void test_corpse_ids()
176 {
177     char * f_name = "test_corpse_ids()";
178     char * prefix = "In the object definitions DB, one object corpse ID does "
179                     "not reference any known object in the DB. ID of "
180                     "responsible object: ";
181     size_t size = strlen(prefix) + 3 + 1; /* 3: uint8_t representation strlen */
182     char * err_corpse = try_malloc(size, f_name);
183     struct MapObjDef * test_entry_0 = world.map_obj_defs;
184     for (; test_entry_0; test_entry_0 = test_entry_0->next)
185     {
186         uint8_t corpse_id_found = 0;
187         struct MapObjDef * test_entry_1 = world.map_obj_defs;
188         for (; test_entry_1; test_entry_1 = test_entry_1->next)
189         {
190             if (test_entry_0->corpse_id == test_entry_1->id)
191             {
192                 corpse_id_found = 1;
193             }
194         }
195         int test = snprintf(err_corpse, size, "%s%d", prefix, test_entry_0->id);
196         exit_trouble(test < 0, f_name, "snprintf()");
197         exit_err(!corpse_id_found, err_corpse);
198     }
199     free(err_corpse);
200 }
201
202
203
204 static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags,
205                            uint8_t * action_flags, struct MapObjDef * mod,
206                            struct MapObjAct * moa)
207 {
208     if (   *action_flags & EDIT_STARTED
209         && set_val(token0, token1, "NAME", action_flags,
210                    NAME_SET, 's', (char *) &moa->name))
211     {
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)))
216         {
217             moa->func = actor_wait;
218         }
219         *action_flags = *action_flags | NAME_SET;
220         return 1;
221     }
222     else if (   set_val(token0, token1, "NAME", object_flags,
223                         NAME_SET, 's', (char *) &mod->name)
224              || set_val(token0, token1, "SYMBOL", object_flags,
225                         SYMBOL_SET, 'c', (char *) &mod->char_on_map)
226              || set_val(token0, token1, "EFFORT", action_flags,
227                         EFFORT_SET, '8', (char *) &moa->effort)
228              || set_val(token0, token1, "LIFEPOINTS", object_flags,
229                         LIFEPOINTS_SET, '8', (char *) &mod->lifepoints)
230              || set_val(token0, token1, "CONSUMABLE", object_flags,
231                         CONSUMABLE_SET, '8', (char *) &mod->consumable)
232              || set_val(token0, token1, "CORPSE_ID", object_flags,
233                         CORPSE_ID_SET, '8', (char *) &mod->corpse_id))
234     {
235         return 1;
236     }
237     return 0;
238 }
239
240
241
242 static uint8_t try_func_name(struct MapObjAct * moa, char * name,
243                              void (* func) (struct MapObj *))
244 {
245     if (0 == strcmp(moa->name, name))
246     {
247         moa->func = func;
248         return 1;
249     }
250     return 0;
251 }
252
253
254
255 extern void read_config_file()
256 {
257     parse_file(world.path_config, tokens_into_entries);
258     set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS);
259     test_corpse_ids();
260 }