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" /* 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],'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))
106 if (!get_thing_type(id))
108 return err_line(1, "Corpse ID belongs to no known thing type.");
112 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
114 tt = get_thing_type(id);
117 tt = add_thing_type(id);
129 static uint8_t try_func_name(struct ThingAction * ta, char * name,
130 void (* func) (struct Thing *))
132 if (0 == strcmp(ta->name, name))
142 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
145 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
147 return err_line(1, "No thing action defined to manipulate yet.");
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))
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)))
159 return err_line(1, "Invalid action function name.");
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]);
166 remove_worldstate_file();
170 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
172 ta = get_thing_action(id);
175 ta = add_thing_action(id);
187 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
190 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
192 struct ThingType * tt = get_thing_type(type);
193 if (!err_line(!tt, "Thing type does not exist."))
204 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
207 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
211 t->command = command;
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."))
218 t->command = command;
227 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
230 if (!strcmp(tok0, s[S_CMD_T_POSY]))
234 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
238 if (axis && !parsetest_int(tok1, '8'))
240 uint8_t length = atoi(tok1);
241 char * err = "Position is outside of map.";
242 if (!err_line(length >= world.map.length, err))
248 else if ('x' == axis)
252 if (world.exists && t->lifepoints)
255 if (t == get_player())
257 update_map_memory(t);
268 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
271 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
273 if (!err_line(id == t->id, "Thing cannot carry itself."))
275 struct Thing * o = get_thing(world.things, id, 0);
276 if (!err_line(!o, "Thing not available for carrying."))
278 own_thing(&(t->owns), &world.things, id);
289 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
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])))
297 return err_line(1, "No thing defined to manipulate yet.");
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))
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))
313 t = add_thing(id, world.thing_types->id, 0, 0);
325 static uint8_t world_may_be_set_active()
327 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
329 err_line(1, "No thing action of name 'wait' found.");
334 err_line(1, "No un-owned player thing (of id=0) found.");
337 if (!world.map.cells)
339 err_line(1, "No map found.");
347 static void remove_worldstate_file()
349 if (!access(s[S_PATH_WORLDSTATE], F_OK))
351 int test = unlink(s[S_PATH_WORLDSTATE]);
352 exit_trouble(-1 == test, __func__, "unlink");
358 static uint8_t parse_world_active(char * tok0, char * tok1)
360 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
362 if (!parsetest_int(tok1, '8'))
364 uint8_t argument = atoi(tok1);
367 remove_worldstate_file();
370 else if (world.exists)
372 err_line(1, "World already active.");
374 else if (world_may_be_set_active())
377 for (ti = world.things; ti; ti = ti->next)
382 if (ti == get_player())
384 update_map_memory(ti);
398 static uint8_t set_map_length(char * tok0, char * tok1)
400 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
402 uint16_t argument = atoi(tok1);
403 if (argument < 1 || argument > 256)
405 return err_line(1, "Value must be >= 1 and <= 256.");
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;
420 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
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))
435 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
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.");
450 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
452 if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
454 return err_line(1, "No thing defined to manipulate yet.");
456 if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
458 uint8_t y = atoi(tok1);
459 if (parsetest_int(tok1, '8') || y >= world.map.length)
461 return err_line(1, "Illegal value for map line number.");
463 if (strlen(tok2) != world.map.length)
465 return err_line(1, "Map line length is unequal map width.");
469 uint32_t map_size = world.map.length * world.map.length;
470 t->mem_map = try_malloc(map_size, __func__);
471 memset(t->mem_map, ' ', map_size);
473 memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
484 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
487 if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
489 return err_line(1, "No thing defined to manipulate yet.");
491 if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
493 uint8_t id = atoi(tok1);
494 uint8_t y = atoi(tok2);
495 uint8_t x = atoi(tok3);
496 if ( parsetest_int(tok1, '8') || !get_thing_type(id)
497 || parsetest_int(tok2, '8') || y >= world.map.length
498 || parsetest_int(tok3, '8') || x >= world.map.length)
500 return err_line(1, "Illegal value for thing type or position.");
502 add_thing_to_memory_map(t, id, y, x);