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