home · contact · privacy
a76f058cf3f18fa5d7dadc04a146ddf1882df8d1
[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_NAME],'s',(char *) &tt->name));
104     else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
105     {
106         if (!get_thing_type(id))
107         {
108             return err_line(1, "Corpse ID belongs to no known thing type.");
109         }
110         tt->corpse_id = id;
111     }
112     else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
113     {
114         tt = get_thing_type(id);
115         if (!tt)
116         {
117             tt = add_thing_type(id);
118         }
119     }
120     else
121     {
122         return 0;
123     }
124     return 1;
125 }
126
127
128
129 static uint8_t try_func_name(struct ThingAction * ta, char * name,
130                              void (* func) (struct Thing *))
131 {
132     if (0 == strcmp(ta->name, name))
133     {
134         ta->func = func;
135         return 1;
136     }
137     return 0;
138 }
139
140
141
142 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
143 {
144     if (!ta &&
145         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
146     {
147         return err_line(1, "No thing action defined to manipulate yet.");
148     }
149     int16_t id;
150     if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
151     else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
152     {
153         if (!(   try_func_name(ta, s[S_CMD_MOVE], actor_move)
154               || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
155               || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
156               || try_func_name(ta, s[S_CMD_DROP], actor_drop)
157               || try_func_name(ta, s[S_CMD_USE], actor_use)))
158         {
159             return err_line(1, "Invalid action function name.");
160         }
161         if (world.exists)
162         {         /* Legal worlds have at least one thing action for waiting. */
163             world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
164             if (!world.exists)
165             {
166                 remove_worldstate_file();
167             }
168         }
169     }
170     else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
171     {
172         ta = get_thing_action(id);
173         if (!ta)
174         {
175             ta = add_thing_action(id);
176         }
177     }
178     else
179     {
180         return 0;
181     }
182     return 1;
183 }
184
185
186
187 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
188 {
189     uint8_t type;
190     if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
191     {
192         struct ThingType * tt = get_thing_type(type);
193         if (!err_line(!tt, "Thing type does not exist."))
194         {
195             t->type = type;
196         }
197         return 1;
198     }
199     return 0;
200 }
201
202
203
204 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
205 {
206     uint8_t command;
207     if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
208     {
209         if (!command)
210         {
211             t->command = command;
212             return 1;
213         }
214         struct ThingAction * ta = world.thing_actions;
215         for (; ta && command != ta->id; ta = ta->next);
216         if (!err_line(!ta, "Thing action does not exist."))
217         {
218             t->command = command;
219         }
220         return 1;
221     }
222     return 0;
223 }
224
225
226
227 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
228 {
229     char axis = 0;
230     if      (!strcmp(tok0, s[S_CMD_T_POSY]))
231     {
232         axis = 'y';
233     }
234     else if (!strcmp(tok0, s[S_CMD_T_POSX]))
235     {
236         axis = 'x';
237     }
238     if (axis && !parsetest_int(tok1, '8'))
239     {
240         uint8_t length = atoi(tok1);
241         char * err = "Position is outside of map.";
242         if (!err_line(length >= world.map.length, err))
243         {
244             if      ('y' == axis)
245             {
246                 t->pos.y = length;
247             }
248             else if ('x' == axis)
249             {
250                 t->pos.x = length;
251             }
252             if (world.exists && t->lifepoints)
253             {
254                 build_fov_map(t);
255                 if (t == get_player())
256                 {
257                     update_map_memory(t);
258                 }
259             }
260         }
261         return 1;
262     }
263     return 0;
264 }
265
266
267
268 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
269 {
270     uint8_t id;
271     if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
272     {
273         if (!err_line(id == t->id, "Thing cannot carry itself."))
274         {
275             struct Thing * o = get_thing(world.things, id, 0);
276             if (!err_line(!o, "Thing not available for carrying."))
277             {
278                 own_thing(&(t->owns), &world.things, id);
279                 o->pos = t->pos;
280             }
281         }
282         return 1;
283     }
284     return 0;
285 }
286
287
288
289 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
290 {
291     if (!t &&
292         (   !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
293          || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
294          || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
295          || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
296     {
297         return err_line(1, "No thing defined to manipulate yet.");
298     }
299     int16_t id;
300     if (   parse_thing_type(tok0, tok1, t)
301         || parse_thing_command(tok0, tok1, t)
302         || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
303         || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
304         || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
305         || parse_position(tok0, tok1, t)
306         || parse_carry(tok0, tok1, t));
307     else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
308     {
309         t = get_thing(world.things, id, 1);
310         char * err = "No thing type found to initialize new thing.";
311         if (!t && !err_line(!world.thing_types, err))
312         {
313             t = add_thing(id, world.thing_types->id, 0, 0);
314         }
315     }
316     else
317     {
318         return 0;
319     }
320     return 1;
321 }
322
323
324
325 static uint8_t world_may_be_set_active()
326 {
327     if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
328     {
329         err_line(1, "No thing action of name 'wait' found.");
330         return 0;
331     }
332     if (!get_player())
333     {
334         err_line(1, "No un-owned player thing (of id=0) found.");
335         return 0;
336     }
337     if (!world.map.cells)
338     {
339         err_line(1, "No map found.");
340         return 0;
341     }
342     return 1;
343 }
344
345
346
347 static void remove_worldstate_file()
348 {
349     if (!access(s[S_PATH_WORLDSTATE], F_OK))
350     {
351         int test = unlink(s[S_PATH_WORLDSTATE]);
352         exit_trouble(-1 == test, __func__, "unlink");
353     }
354 }
355
356
357
358 static uint8_t parse_world_active(char * tok0, char * tok1)
359 {
360     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
361     {
362         if (!parsetest_int(tok1, '8'))
363         {
364             uint8_t argument = atoi(tok1);
365             if (!argument)
366             {
367                 remove_worldstate_file();
368                 world.exists = 0;
369             }
370             else if (world.exists)
371             {
372                 err_line(1, "World already active.");
373             }
374             else if (world_may_be_set_active())
375             {
376                 struct Thing * ti;
377                 for (ti = world.things; ti; ti = ti->next)
378                 {
379                     if (ti->lifepoints)
380                     {
381                         build_fov_map(ti);
382                         if (ti == get_player())
383                         {
384                             update_map_memory(ti);
385                         }
386                     }
387                 }
388                 world.exists = 1;
389             }
390             return 1;
391         }
392     }
393     return 0;
394 }
395
396
397
398 static uint8_t set_map_length(char * tok0, char * tok1)
399 {
400     if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
401     {
402         uint16_t argument = atoi(tok1);
403         if (argument < 1 || argument > 256)
404         {
405             return err_line(1, "Value must be >= 1 and <= 256.");
406         }
407         world.exists = 0;
408         remove_worldstate_file();
409         free_things(world.things);
410         free(world.map.cells);
411         world.map.cells = NULL;    /* Since remake_map() runs free() on this. */
412         world.map.length = argument;
413         return 1;
414     }
415     return 0;
416 }
417
418
419
420 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
421 {
422     if (   parse_thingtype_manipulation(tok0, tok1)
423         || parse_thingaction_manipulation(tok0, tok1)
424         || parse_thing_manipulation_1arg(tok0, tok1)
425         || set_map_length(tok0,tok1)
426         || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
427         || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
428         || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
429         || parse_world_active(tok0, tok1));
430     else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
431
432     {
433         remake_map();
434     }
435     else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
436     {
437         uint8_t test = remake_world();
438         err_line(1 == test, "No player type with start number of >0 defined.");
439         err_line(2 == test, "No thing action with name 'wait' defined.");
440     }
441     else
442     {
443         return 0;
444     }
445     return 1;
446 }
447
448
449
450 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
451 {
452     if (!t && (   !strcmp(tok0, s[S_CMD_T_MEMMAP])
453                || !strcmp(tok0, s[S_CMD_T_MEMDEPTHMAP])))
454     {
455         return err_line(1, "No thing defined to manipulate yet.");
456     }
457     if (!strcmp(tok0,s[S_CMD_T_MEMMAP]) || !strcmp(tok0,s[S_CMD_T_MEMDEPTHMAP]))
458     {
459         uint8_t y = atoi(tok1);
460         if (parsetest_int(tok1, '8') || y >= world.map.length)
461         {
462             return err_line(1, "Illegal value for map line number.");
463         }
464         if (strlen(tok2) != world.map.length)
465         {
466             return err_line(1, "Map line length is unequal map width.");
467         }
468         if (!strcmp(tok0,s[S_CMD_T_MEMMAP]))
469         {
470             if (!t->mem_map)
471             {
472                 init_empty_map(&(t->mem_map));
473             }
474             memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
475         }
476         else
477         {
478             if (!t->mem_depth_map)
479             {
480                 init_empty_map(&(t->mem_depth_map));
481             }
482             memcpy(t->mem_depth_map+y*world.map.length, tok2, world.map.length);
483         }
484     }
485     else
486     {
487         return 0;
488     }
489     return 1;
490 }
491
492
493
494 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
495                                       char * tok3)
496 {
497     if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
498     {
499         return err_line(1, "No thing defined to manipulate yet.");
500     }
501     if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
502     {
503         uint8_t id = atoi(tok1);
504         uint8_t y  = atoi(tok2);
505         uint8_t x  = atoi(tok3);
506         if (   parsetest_int(tok1, '8') || !get_thing_type(id)
507             || parsetest_int(tok2, '8') || y >= world.map.length
508             || parsetest_int(tok3, '8') || x >= world.map.length)
509         {
510             return err_line(1, "Illegal value for thing type or position.");
511         }
512         add_thing_to_memory_map(t, id, y, x);
513     }
514     else
515     {
516         return 0;
517     }
518     return 1;
519 }