1 /* src/server/god_commands.c */
3 #include "god_commands.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t */
6 #include <stdlib.h> /* atoi(), free() */
7 #include <string.h> /* strcmp() */
8 #include <unistd.h> /* F_OK, access(), unlink() */
9 #include "../common/parse_file.h" /* err_line(), parse_val(), parsetest_int() */
10 #include "../common/rexit.h" /* exit_trouble() */
11 #include "cleanup.h" /* unset_cleanup_flag() */
12 #include "field_of_view.h" /* build_fov_map() */
13 #include "hardcoded_strings.h" /* s */
14 #include "init.h" /* remake_world() */
15 #include "map.h" /* remake_map() */
16 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
17 * actor_use(), actor_pickup(), actor_drop()
19 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
22 #include "world.h" /* world */
26 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
27 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
29 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
30 * .func from .name for set_members().
32 static uint8_t try_func_name(struct ThingAction * ta, char * name,
33 void (* func) (struct Thing *));
35 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
36 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
38 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
39 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
41 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
42 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
44 /* Parse/apply god command in "tok0"/"tok1" on positioning a thing "t". */
45 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
47 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
48 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
50 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
51 static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
53 /* Performs parse_world_active()'s world activation legality tests. */
54 static uint8_t world_may_be_set_active();
56 /* Unlink worldstate file if it exists. */
57 static void remove_worldstate_file();
59 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
60 * argument is 0 and unlink worldstate file, but only set it on positive
61 * argument if it is not already set and a thing action of name S_CMD_WAIT, a
62 * player thing and a map are defined. On setting it, rebuild all FOVs.
64 static uint8_t parse_world_active(char * tok0, char * tok1);
66 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
67 * re-set, set world.exists to 0, remove all things and free world.map.cells
69 static uint8_t set_map_length(char * tok0, char * tok1);
73 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
75 static struct ThingType * tt = NULL;
77 ( !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
78 || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
79 || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
81 err_line(1, "No thing type defined to manipulate yet.");
85 if ( parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
86 || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
87 || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
88 || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
89 || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
90 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
92 if (!get_thing_type(id))
94 err_line(1, "Corpse ID belongs to no known thing type.");
99 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], '8', (char *) &id))
101 tt = get_thing_type(id);
104 tt = add_thing_type(id);
116 static uint8_t try_func_name(struct ThingAction * ta, char * name,
117 void (* func) (struct Thing *))
119 if (0 == strcmp(ta->name, name))
129 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
131 static struct ThingAction * ta = NULL;
133 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
135 err_line(1, "No thing action defined to manipulate yet.");
139 if (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
140 else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
142 if (!( try_func_name(ta, s[S_CMD_MOVE], actor_move)
143 || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
144 || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
145 || try_func_name(ta, s[S_CMD_DROP], actor_drop)
146 || try_func_name(ta, s[S_CMD_USE], actor_use)))
148 err_line(1, "Invalid action function name.");
150 } /* Legal worlds have at least one thing action for waiting. */
153 world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
156 remove_worldstate_file();
160 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
162 ta = get_thing_action(id);
165 ta = add_thing_action(id);
177 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
180 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
182 struct ThingType * tt = get_thing_type(type);
183 if (!err_line(!tt, "Thing type does not exist."))
194 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
197 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
201 t->command = command;
204 struct ThingAction * ta = world.thing_actions;
205 for (; ta && command != ta->id; ta = ta->next);
206 if (!err_line(!ta, "Thing action does not exist."))
208 t->command = command;
217 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
220 if (!strcmp(tok0, s[S_CMD_T_POSY]))
224 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
228 if (axis && !parsetest_int(tok1, '8'))
230 uint8_t length = atoi(tok1);
231 char * err = "Position is outside of map.";
232 if (!err_line(length >= world.map.length, err))
238 else if ('x' == axis)
243 if (world.exists && t->lifepoints)
245 t->fov_map = build_fov_map(t);
255 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
258 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
260 if (!err_line(id == t->id, "Thing cannot carry itself."))
262 struct Thing * o = get_thing(world.things, id, 0);
263 if (!err_line(!o, "Thing not available for carrying."))
265 own_thing(&(t->owns), &world.things, id);
276 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
278 static struct Thing * t = NULL;
280 ( !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
281 || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
282 || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
283 || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
285 err_line(1, "No thing defined to manipulate yet.");
289 if ( parse_thing_type(tok0, tok1, t)
290 || parse_thing_command(tok0, tok1, t)
291 || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
292 || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
293 || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
294 || parse_position(tok0, tok1, t)
295 || parse_carry(tok0, tok1, t));
296 else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
298 t = get_thing(world.things, id, 1);
299 char * err = "No thing type found to initialize new thing.";
300 if (!t && !err_line(NULL == world.thing_types, err))
302 t = add_thing(id, world.thing_types->id, 0, 0);
303 if (world.exists && t->lifepoints)
305 t->fov_map = build_fov_map(t);
318 static uint8_t world_may_be_set_active()
320 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
322 err_line(1, "No thing action of name 'wait' found.");
327 err_line(1, "No un-owned player thing (of id=0) found.");
330 if (!world.map.cells)
332 err_line(1, "No map found.");
340 static void remove_worldstate_file()
342 if (!access(s[S_PATH_WORLDSTATE], F_OK))
344 int test = unlink(s[S_PATH_WORLDSTATE]);
345 exit_trouble(-1 == test, __func__, "unlink");
351 static uint8_t parse_world_active(char * tok0, char * tok1)
353 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
355 if (!parsetest_int(tok1, '8'))
357 uint8_t argument = atoi(tok1);
360 remove_worldstate_file();
363 else if (world.exists)
365 err_line(1, "World already active.");
367 else if (world_may_be_set_active())
370 for (ti = world.things; ti; ti = ti->next)
378 ti->fov_map = build_fov_map(ti);
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 err_line(1, "Value must be >= 1 and <= 256.");
402 remove_worldstate_file();
403 free_things(world.things);
404 free(world.map.cells);
405 world.map.cells = NULL; /* Since remake_map() runs free() on this. */
406 world.map.length = argument;
414 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
416 if ( parse_thingtype_manipulation(tok0, tok1)
417 || parse_thingaction_manipulation(tok0, tok1)
418 || parse_thing_manipulation(tok0, tok1)
419 || set_map_length(tok0,tok1)
420 || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
421 || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
422 || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
423 || parse_world_active(tok0, tok1));
424 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
429 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
431 uint8_t test = remake_world();
432 err_line(1 == test, "No player type with start number of >0 defined.");
433 err_line(2 == test, "No thing action with name 'wait' defined.");