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_STOMACH], 'u', (char *) &tt->stomach)
104 || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name))
108 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
110 if (!get_thing_type(id))
112 return err_line(1, "Corpse ID belongs to no known thing type.");
116 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
118 tt = get_thing_type(id);
121 tt = add_thing_type(id);
133 static uint8_t try_func_name(struct ThingAction * ta, char * name,
134 void (* func) (struct Thing *))
136 if (0 == strcmp(ta->name, name))
146 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
149 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
151 return err_line(1, "No thing action defined to manipulate yet.");
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))
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)))
163 return err_line(1, "Invalid action function name.");
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]);
170 remove_worldstate_file();
174 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
176 ta = get_thing_action(id);
179 ta = add_thing_action(id);
191 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
194 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
196 struct ThingType * tt = get_thing_type(type);
197 if (!err_line(!tt, "Thing type does not exist."))
208 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
211 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
215 t->command = command;
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."))
222 t->command = command;
231 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
234 if (!strcmp(tok0, s[S_CMD_T_POSY]))
238 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
242 if (axis && !parsetest_int(tok1, '8'))
244 uint8_t length = atoi(tok1);
245 char * err = "Position is outside of map.";
246 if (!err_line(length >= world.map.length, err))
252 else if ('x' == axis)
256 if (world.exists && t->lifepoints)
259 if (t == get_player())
261 update_map_memory(t);
272 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
275 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
277 if (!err_line(id == t->id, "Thing cannot carry itself."))
279 struct Thing * o = get_thing(world.things, id, 0);
280 if (!err_line(!o, "Thing not available for carrying."))
282 own_thing(&(t->owns), &world.things, id);
293 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
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])))
302 return err_line(1, "No thing defined to manipulate yet.");
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))
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))
319 t = add_thing(id, world.thing_types->id, 0, 0);
331 static uint8_t world_may_be_set_active()
333 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
335 err_line(1, "No thing action of name 'wait' found.");
340 err_line(1, "No un-owned player thing (of id=0) found.");
343 if (!world.map.cells)
345 err_line(1, "No map found.");
353 static void remove_worldstate_file()
355 if (!access(s[S_PATH_WORLDSTATE], F_OK))
357 int test = unlink(s[S_PATH_WORLDSTATE]);
358 exit_trouble(-1 == test, __func__, "unlink");
364 static uint8_t parse_world_active(char * tok0, char * tok1)
366 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
368 if (!parsetest_int(tok1, '8'))
370 uint8_t argument = atoi(tok1);
373 remove_worldstate_file();
376 else if (world.exists)
378 err_line(1, "World already active.");
380 else if (world_may_be_set_active())
383 for (ti = world.things; ti; ti = ti->next)
388 if (ti == get_player())
390 update_map_memory(ti);
404 static uint8_t set_map_length(char * tok0, char * tok1)
406 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
408 uint16_t argument = atoi(tok1);
409 if (argument < 1 || argument > 256)
411 return err_line(1, "Value must be >= 1 and <= 256.");
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;
426 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
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))
441 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
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.");
456 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
458 if (!t && ( !strcmp(tok0, s[S_CMD_T_MEMMAP])
459 || !strcmp(tok0, s[S_CMD_T_MEMDEPTHMAP])))
461 return err_line(1, "No thing defined to manipulate yet.");
463 if (!strcmp(tok0,s[S_CMD_T_MEMMAP]) || !strcmp(tok0,s[S_CMD_T_MEMDEPTHMAP]))
465 uint8_t y = atoi(tok1);
466 if (parsetest_int(tok1, '8') || y >= world.map.length)
468 return err_line(1, "Illegal value for map line number.");
470 if (strlen(tok2) != world.map.length)
472 return err_line(1, "Map line length is unequal map width.");
474 if (!strcmp(tok0,s[S_CMD_T_MEMMAP]))
478 init_empty_map(&(t->mem_map));
480 memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
484 if (!t->mem_depth_map)
486 init_empty_map(&(t->mem_depth_map));
488 memcpy(t->mem_depth_map+y*world.map.length, tok2, world.map.length);
500 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
503 if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
505 return err_line(1, "No thing defined to manipulate yet.");
507 if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
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)
516 return err_line(1, "Illegal value for thing type or position.");
518 add_thing_to_memory_map(t, id, y, x);