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