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(), 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()
25 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
26 * free_things(), add_thing_to_memory_map(),get_thing_type(),
29 #include "world.h" /* world */
33 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
34 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
36 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
37 * .func from .name for set_members().
39 static uint8_t try_func_name(struct ThingAction * ta, char * name,
40 void (* func) (struct Thing *));
42 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
43 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
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);
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);
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);
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);
57 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
58 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
60 /* Performs parse_world_active()'s world activation legality tests. */
61 static uint8_t world_may_be_set_active();
63 /* Unlink worldstate file if it exists. */
64 static void remove_worldstate_file();
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.
71 static uint8_t parse_world_active(char * tok0, char * tok1);
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
76 static uint8_t set_map_length(char * tok0, char * tok1);
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;
87 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
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])))
95 return err_line(1, "No thing type defined to manipulate yet.");
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))
107 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
109 if (!get_thing_type(id))
111 return err_line(1, "Corpse ID belongs to no known thing type.");
115 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
117 tt = get_thing_type(id);
120 tt = add_thing_type(id);
132 static uint8_t try_func_name(struct ThingAction * ta, char * name,
133 void (* func) (struct Thing *))
135 if (0 == strcmp(ta->name, name))
145 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
148 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
150 return err_line(1, "No thing action defined to manipulate yet.");
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))
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)))
162 return err_line(1, "Invalid action function name.");
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]);
169 remove_worldstate_file();
173 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
175 ta = get_thing_action(id);
178 ta = add_thing_action(id);
190 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
193 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
195 struct ThingType * tt = get_thing_type(type);
196 if (!err_line(!tt, "Thing type does not exist."))
207 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
210 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
214 t->command = command;
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."))
221 t->command = command;
230 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
233 if (!strcmp(tok0, s[S_CMD_T_POSY]))
237 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
241 if (axis && !parsetest_int(tok1, '8'))
243 uint8_t length = atoi(tok1);
244 char * err = "Position is outside of map.";
245 if (!err_line(length >= world.map.length, err))
251 else if ('x' == axis)
255 if (world.exists && t->lifepoints)
258 if (t == get_player())
260 update_map_memory(t, 1);
271 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
274 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
276 if (!err_line(id == t->id, "Thing cannot carry itself."))
278 struct Thing * o = get_thing(world.things, id, 0);
279 if (!err_line(!o, "Thing not available for carrying."))
281 own_thing(&(t->owns), &world.things, id);
292 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
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])))
301 return err_line(1, "No thing defined to manipulate yet.");
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))
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))
318 t = add_thing(id, world.thing_types->id, 0, 0);
330 static uint8_t world_may_be_set_active()
332 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
334 err_line(1, "No thing action of name 'wait' found.");
339 err_line(1, "No un-owned player thing (of id=0) found.");
342 if (!world.map.cells)
344 err_line(1, "No map found.");
352 static void remove_worldstate_file()
354 if (!access(s[S_PATH_WORLDSTATE], F_OK))
356 int test = unlink(s[S_PATH_WORLDSTATE]);
357 exit_trouble(-1 == test, __func__, "unlink");
363 static uint8_t parse_world_active(char * tok0, char * tok1)
365 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
367 if (!parsetest_int(tok1, '8'))
369 uint8_t argument = atoi(tok1);
372 remove_worldstate_file();
375 else if (world.exists)
377 err_line(1, "World already active.");
379 else if (world_may_be_set_active())
382 for (ti = world.things; ti; ti = ti->next)
387 if (ti == get_player())
389 update_map_memory(ti, 0);
403 static uint8_t set_map_length(char * tok0, char * tok1)
405 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
407 uint16_t argument = atoi(tok1);
408 if (argument < 1 || argument > 256)
410 return err_line(1, "Value must be >= 1 and <= 256.");
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;
425 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
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))
440 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
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.");
455 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
457 if (!t && ( !strcmp(tok0, s[S_CMD_T_MEMMAP])
458 || !strcmp(tok0, s[S_CMD_T_MEMDEPTHMAP])))
460 return err_line(1, "No thing defined to manipulate yet.");
462 if (!strcmp(tok0,s[S_CMD_T_MEMMAP]) || !strcmp(tok0,s[S_CMD_T_MEMDEPTHMAP]))
464 uint8_t y = atoi(tok1);
465 if (parsetest_int(tok1, '8') || y >= world.map.length)
467 return err_line(1, "Illegal value for map line number.");
469 if (strlen(tok2) != world.map.length)
471 return err_line(1, "Map line length is unequal map width.");
473 if (!strcmp(tok0,s[S_CMD_T_MEMMAP]))
477 init_empty_map(&(t->mem_map));
479 memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
483 if (!t->mem_depth_map)
485 init_empty_map(&(t->mem_depth_map));
487 memcpy(t->mem_depth_map+y*world.map.length, tok2, world.map.length);
499 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
502 if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
504 return err_line(1, "No thing defined to manipulate yet.");
506 if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
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)
515 return err_line(1, "Illegal value for thing type or position.");
517 add_thing_to_memory_map(t, id, y, x);