home · contact · privacy
6eec5722d6bddcf729f9264d104a1490e9d9de5c
[plomrogue] / src / server / god_commands.c
1 /* src/server/god_commands.c */
2
3 #include "god_commands.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t */
6 #include <stdlib.h> /* atoi(), free() */
7 #include <string.h> /* strcmp() */
8 #include <unistd.h> /* F_OK, access(), unlink() */
9 #include "../common/parse_file.h" /* err_line(), parse_val(), parsetest_int() */
10 #include "../common/rexit.h" /* exit_trouble() */
11 #include "cleanup.h" /* unset_cleanup_flag() */
12 #include "field_of_view.h" /* build_fov_map() */
13 #include "hardcoded_strings.h" /* s */
14 #include "init.h" /* remake_world() */
15 #include "map.h" /* remake_map() */
16 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
17                             * actor_use(), actor_pickup(), actor_drop()
18                             */
19 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
20                      * free_things()
21                      */
22 #include "world.h" /* world */
23
24
25
26 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
27 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
28
29 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
30  * .func from .name for set_members().
31  */
32 static uint8_t try_func_name(struct ThingAction * ta, char * name,
33                              void (* func) (struct Thing *));
34
35 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
36 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
37
38 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
39 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
40
41 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
42 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
43
44 /* Parse/apply god command in "tok0"/"tok1" on positioning a thing "t". */
45 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
46
47 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
48 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
49
50 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
51 static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
52
53 /* Performs parse_world_active()'s world activation legality tests. */
54 static uint8_t world_may_be_set_active();
55
56 /* Unlink worldstate file if it exists. */
57 static void remove_worldstate_file();
58
59 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
60  * argument is 0 and unlink worldstate file, but only set it on positive
61  * argument if it is not already set and a thing action of name S_CMD_WAIT, a
62  * player thing and a map are defined. On setting it, rebuild all FOVs.
63  */
64 static uint8_t parse_world_active(char * tok0, char * tok1);
65
66 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
67  * re-set, set world.exists to 0, remove all things and free world.map.cells
68  */
69 static uint8_t set_map_length(char * tok0, char * tok1);
70
71
72
73 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
74 {
75     static struct ThingType * tt = NULL;
76     if (!tt &&
77         (   !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
78          || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
79          || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
80     {
81         err_line(1, "No thing type defined to manipulate yet.");
82         return 1;
83     }
84     int16_t id;
85     if (   parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
86         || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
87         || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
88         || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
89         || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
90     else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
91     {
92         if (!get_thing_type(id))
93         {
94             err_line(1, "Corpse ID belongs to no known thing type.");
95             return 1;
96         }
97         tt->corpse_id = id;
98     }
99     else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
100     {
101         tt = get_thing_type(id);
102         if (!tt)
103         {
104             tt = add_thing_type(id);
105         }
106     }
107     else
108     {
109         return 0;
110     }
111     return 1;
112 }
113
114
115
116 static uint8_t try_func_name(struct ThingAction * ta, char * name,
117                              void (* func) (struct Thing *))
118 {
119     if (0 == strcmp(ta->name, name))
120     {
121         ta->func = func;
122         return 1;
123     }
124     return 0;
125 }
126
127
128
129 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
130 {
131     static struct ThingAction * ta = NULL;
132     if (!ta &&
133         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
134     {
135         err_line(1, "No thing action defined to manipulate yet.");
136         return 1;
137     }
138     int16_t id;
139     if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
140     else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
141     {
142         if (!(   try_func_name(ta, s[S_CMD_MOVE], actor_move)
143               || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
144               || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
145               || try_func_name(ta, s[S_CMD_DROP], actor_drop)
146               || try_func_name(ta, s[S_CMD_USE], actor_use)))
147         {
148             err_line(1, "Invalid action function name.");
149             return 1;
150         }         /* Legal worlds have at least one thing action for waiting. */
151         if (world.exists)
152         {
153             world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
154             if (!world.exists)
155             {
156                 remove_worldstate_file();
157             }
158         }
159     }
160     else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
161     {
162         ta = get_thing_action(id);
163         if (!ta)
164         {
165             ta = add_thing_action(id);
166         }
167     }
168     else
169     {
170         return 0;
171     }
172     return 1;
173 }
174
175
176
177 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
178 {
179     uint8_t type;
180     if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
181     {
182         struct ThingType * tt = get_thing_type(type);
183         if (!err_line(!tt, "Thing type does not exist."))
184         {
185             t->type = type;
186         }
187         return 1;
188     }
189     return 0;
190 }
191
192
193
194 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
195 {
196     uint8_t command;
197     if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
198     {
199         if (!command)
200         {
201             t->command = command;
202             return 1;
203         }
204         struct ThingAction * ta = world.thing_actions;
205         for (; ta && command != ta->id; ta = ta->next);
206         if (!err_line(!ta, "Thing action does not exist."))
207         {
208             t->command = command;
209         }
210         return 1;
211     }
212     return 0;
213 }
214
215
216
217 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
218 {
219     char axis = 0;
220     if      (!strcmp(tok0, s[S_CMD_T_POSY]))
221     {
222         axis = 'y';
223     }
224     else if (!strcmp(tok0, s[S_CMD_T_POSX]))
225     {
226         axis = 'x';
227     }
228     if (axis && !parsetest_int(tok1, '8'))
229     {
230         uint8_t length = atoi(tok1);
231         char * err = "Position is outside of map.";
232         if (!err_line(length >= world.map.length, err))
233         {
234             if      ('y' == axis)
235             {
236                 t->pos.y = length;
237             }
238             else if ('x' == axis)
239             {
240                 t->pos.x = length;
241             }
242             if (world.exists && t->lifepoints)
243             {
244                 build_fov_map(t);
245             }
246         }
247         return 1;
248     }
249     return 0;
250 }
251
252
253
254 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
255 {
256     uint8_t id;
257     if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
258     {
259         if (!err_line(id == t->id, "Thing cannot carry itself."))
260         {
261             struct Thing * o = get_thing(world.things, id, 0);
262             if (!err_line(!o, "Thing not available for carrying."))
263             {
264                 own_thing(&(t->owns), &world.things, id);
265                 o->pos = t->pos;
266             }
267         }
268         return 1;
269     }
270     return 0;
271 }
272
273
274
275 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
276 {
277     static struct Thing * t = NULL;
278     if (!t &&
279         (   !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
280          || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
281          || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
282          || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
283     {
284         err_line(1, "No thing defined to manipulate yet.");
285         return 1;
286     }
287     int16_t id;
288     if (   parse_thing_type(tok0, tok1, t)
289         || parse_thing_command(tok0, tok1, t)
290         || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
291         || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
292         || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
293         || parse_position(tok0, tok1, t)
294         || parse_carry(tok0, tok1, t));
295     else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
296     {
297         t = get_thing(world.things, id, 1);
298         char * err = "No thing type found to initialize new thing.";
299         if (!t && !err_line(NULL == world.thing_types, err))
300         {
301             t = add_thing(id, world.thing_types->id, 0, 0);
302             if (world.exists && t->lifepoints)
303             {
304                 build_fov_map(t);
305             }
306         }
307     }
308     else
309     {
310         return 0;
311     }
312     return 1;
313 }
314
315
316
317 static uint8_t world_may_be_set_active()
318 {
319     if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
320     {
321         err_line(1, "No thing action of name 'wait' found.");
322         return 0;
323     }
324     if (!get_player())
325     {
326         err_line(1, "No un-owned player thing (of id=0) found.");
327         return 0;
328     }
329     if (!world.map.cells)
330     {
331         err_line(1, "No map found.");
332         return 0;
333     }
334     return 1;
335 }
336
337
338
339 static void remove_worldstate_file()
340 {
341     if (!access(s[S_PATH_WORLDSTATE], F_OK))
342     {
343         int test = unlink(s[S_PATH_WORLDSTATE]);
344         exit_trouble(-1 == test, __func__, "unlink");
345     }
346 }
347
348
349
350 static uint8_t parse_world_active(char * tok0, char * tok1)
351 {
352     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
353     {
354         if (!parsetest_int(tok1, '8'))
355         {
356             uint8_t argument = atoi(tok1);
357             if (!argument)
358             {
359                 remove_worldstate_file();
360                 world.exists = 0;
361             }
362             else if (world.exists)
363             {
364                 err_line(1, "World already active.");
365             }
366             else if (world_may_be_set_active())
367             {
368                 struct Thing * ti;
369                 for (ti = world.things; ti; ti = ti->next)
370                 {
371                     if (ti->lifepoints)
372                     {
373                         build_fov_map(ti);
374                     }
375                 }
376                 world.exists = 1;
377             }
378             return 1;
379         }
380     }
381     return 0;
382 }
383
384
385
386 static uint8_t set_map_length(char * tok0, char * tok1)
387 {
388     if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
389     {
390         uint16_t argument = atoi(tok1);
391         if (argument < 1 || argument > 256)
392         {
393             err_line(1, "Value must be >= 1 and <= 256.");
394             return 1;
395         }
396         world.exists = 0;
397         remove_worldstate_file();
398         free_things(world.things);
399         free(world.map.cells);
400         world.map.cells = NULL;    /* Since remake_map() runs free() on this. */
401         world.map.length = argument;
402         return 1;
403     }
404     return 0;
405 }
406
407
408
409 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
410 {
411     if (   parse_thingtype_manipulation(tok0, tok1)
412         || parse_thingaction_manipulation(tok0, tok1)
413         || parse_thing_manipulation(tok0, tok1)
414         || set_map_length(tok0,tok1)
415         || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
416         || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
417         || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
418         || parse_world_active(tok0, tok1));
419     else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
420
421     {
422         remake_map();
423     }
424     else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
425     {
426         uint8_t test = remake_world();
427         err_line(1 == test, "No player type with start number of >0 defined.");
428         err_line(2 == test, "No thing action with name 'wait' defined.");
429     }
430     else
431     {
432         return 0;
433     }
434     return 1;
435 }