1 /* src/server/god_commands.c
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.
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() */
19 #include "hardcoded_strings.h" /* s */
20 #include "init.h" /* remake_world() */
21 #include "map.h" /* remake_map() */
22 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
23 * actor_use(), actor_pickup(), actor_drop()
25 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
26 * free_things(), add_thing_to_memory_map(),get_thing_type()
28 #include "world.h" /* world */
32 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
33 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
35 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
36 * .func from .name for set_members().
38 static uint8_t try_func_name(struct ThingAction * ta, char * name,
39 void (* func) (struct Thing *));
41 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
42 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
44 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
45 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
47 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
48 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
50 /* Parse/apply god command in "tok0"/"tok1" on positioning a thing "t". */
51 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
53 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
54 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
56 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
57 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
59 /* Performs parse_world_active()'s world activation legality tests. */
60 static uint8_t world_may_be_set_active();
62 /* Unlink worldstate file if it exists. */
63 static void remove_worldstate_file();
65 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
66 * argument is 0 and unlink worldstate file, but only set it on positive
67 * argument if it is not already set and a thing action of name S_CMD_WAIT, a
68 * player thing and a map are defined. On setting it, rebuild all FOVs.
70 static uint8_t parse_world_active(char * tok0, char * tok1);
72 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
73 * re-set, set world.exists to 0, remove all things and free world.map.cells
75 static uint8_t set_map_length(char * tok0, char * tok1);
79 /* Thing, ThingType or ThingAction selected to be manipulated. */
80 static struct Thing * t = NULL;
81 static struct ThingType * tt = NULL;
82 static struct ThingAction * ta = NULL;
86 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
89 ( !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
90 || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
91 || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
93 return err_line(1, "No thing type defined to manipulate yet.");
96 if ( parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
97 || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
98 || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
99 || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
100 || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
101 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
103 if (!get_thing_type(id))
105 return err_line(1, "Corpse ID belongs to no known thing type.");
109 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
111 tt = get_thing_type(id);
114 tt = add_thing_type(id);
126 static uint8_t try_func_name(struct ThingAction * ta, char * name,
127 void (* func) (struct Thing *))
129 if (0 == strcmp(ta->name, name))
139 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
142 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
144 return err_line(1, "No thing action defined to manipulate yet.");
147 if (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
148 else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
150 if (!( try_func_name(ta, s[S_CMD_MOVE], actor_move)
151 || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
152 || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
153 || try_func_name(ta, s[S_CMD_DROP], actor_drop)
154 || try_func_name(ta, s[S_CMD_USE], actor_use)))
156 return err_line(1, "Invalid action function name.");
159 { /* Legal worlds have at least one thing action for waiting. */
160 world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
163 remove_worldstate_file();
167 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
169 ta = get_thing_action(id);
172 ta = add_thing_action(id);
184 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
187 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
189 struct ThingType * tt = get_thing_type(type);
190 if (!err_line(!tt, "Thing type does not exist."))
201 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
204 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
208 t->command = command;
211 struct ThingAction * ta = world.thing_actions;
212 for (; ta && command != ta->id; ta = ta->next);
213 if (!err_line(!ta, "Thing action does not exist."))
215 t->command = command;
224 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
227 if (!strcmp(tok0, s[S_CMD_T_POSY]))
231 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
235 if (axis && !parsetest_int(tok1, '8'))
237 uint8_t length = atoi(tok1);
238 char * err = "Position is outside of map.";
239 if (!err_line(length >= world.map.length, err))
245 else if ('x' == axis)
249 if (world.exists && t->lifepoints)
261 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
264 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
266 if (!err_line(id == t->id, "Thing cannot carry itself."))
268 struct Thing * o = get_thing(world.things, id, 0);
269 if (!err_line(!o, "Thing not available for carrying."))
271 own_thing(&(t->owns), &world.things, id);
282 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
285 ( !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
286 || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
287 || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
288 || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
290 return err_line(1, "No thing defined to manipulate yet.");
293 if ( parse_thing_type(tok0, tok1, t)
294 || parse_thing_command(tok0, tok1, t)
295 || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
296 || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
297 || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
298 || parse_position(tok0, tok1, t)
299 || parse_carry(tok0, tok1, t));
300 else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
302 t = get_thing(world.things, id, 1);
303 char * err = "No thing type found to initialize new thing.";
304 if (!t && !err_line(!world.thing_types, err))
306 t = add_thing(id, world.thing_types->id, 0, 0);
307 if (world.exists && t->lifepoints)
322 static uint8_t world_may_be_set_active()
324 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
326 err_line(1, "No thing action of name 'wait' found.");
331 err_line(1, "No un-owned player thing (of id=0) found.");
334 if (!world.map.cells)
336 err_line(1, "No map found.");
344 static void remove_worldstate_file()
346 if (!access(s[S_PATH_WORLDSTATE], F_OK))
348 int test = unlink(s[S_PATH_WORLDSTATE]);
349 exit_trouble(-1 == test, __func__, "unlink");
355 static uint8_t parse_world_active(char * tok0, char * tok1)
357 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
359 if (!parsetest_int(tok1, '8'))
361 uint8_t argument = atoi(tok1);
364 remove_worldstate_file();
367 else if (world.exists)
369 err_line(1, "World already active.");
371 else if (world_may_be_set_active())
374 for (ti = world.things; ti; ti = ti->next)
391 static uint8_t set_map_length(char * tok0, char * tok1)
393 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
395 uint16_t argument = atoi(tok1);
396 if (argument < 1 || argument > 256)
398 return err_line(1, "Value must be >= 1 and <= 256.");
401 remove_worldstate_file();
402 free_things(world.things);
403 free(world.map.cells);
404 world.map.cells = NULL; /* Since remake_map() runs free() on this. */
405 world.map.length = argument;
413 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
415 if ( parse_thingtype_manipulation(tok0, tok1)
416 || parse_thingaction_manipulation(tok0, tok1)
417 || parse_thing_manipulation_1arg(tok0, tok1)
418 || set_map_length(tok0,tok1)
419 || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
420 || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
421 || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
422 || parse_world_active(tok0, tok1));
423 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
428 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
430 uint8_t test = remake_world();
431 err_line(1 == test, "No player type with start number of >0 defined.");
432 err_line(2 == test, "No thing action with name 'wait' defined.");
443 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
445 if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
447 return err_line(1, "No thing defined to manipulate yet.");
449 if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
451 uint8_t y = atoi(tok1);
452 if (parsetest_int(tok1, '8') || y >= world.map.length)
454 return err_line(1, "Illegal value for map line number.");
456 if (strlen(tok2) != world.map.length)
458 return err_line(1, "Map line length is unequal map width.");
462 uint32_t map_size = world.map.length * world.map.length;
463 t->mem_map = try_malloc(map_size, __func__);
464 memset(t->mem_map, ' ', map_size);
466 memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
477 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
480 if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
482 return err_line(1, "No thing defined to manipulate yet.");
484 if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
486 uint8_t id = atoi(tok1);
487 uint8_t y = atoi(tok2);
488 uint8_t x = atoi(tok3);
489 if ( parsetest_int(tok1, '8') || !get_thing_type(id)
490 || parsetest_int(tok2, '8') || y >= world.map.length
491 || parsetest_int(tok3, '8') || x >= world.map.length)
493 return err_line(1, "Illegal value for thing type or position.");
495 add_thing_to_memory_map(t, id, y, x);