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_THINGTYPE], '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_THINGACTION], '8', (char *) &id))
164 err_line(1, "Value must be >= 1 and <= 255.");
167 ta = get_thing_action(id);
170 ta = add_thing_action(id);
182 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
185 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
187 struct ThingType * tt = get_thing_type(type);
188 if (!err_line(!tt, "Thing type does not exist."))
199 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
202 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
206 t->command = command;
209 struct ThingAction * ta = world.thing_actions;
210 for (; ta && command != ta->id; ta = ta->next);
211 if (!err_line(!ta, "Thing action does not exist."))
213 t->command = command;
222 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
225 if (!strcmp(tok0, s[S_CMD_T_POSY]))
229 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
233 if (axis && !parsetest_int(tok1, '8'))
235 uint8_t length = atoi(tok1);
236 char * err = "Position is outside of map.";
237 if (!err_line(length >= world.map.length, err))
243 else if ('x' == axis)
248 if (world.exists && t->lifepoints)
250 t->fov_map = build_fov_map(t);
260 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
263 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
265 if (!err_line(id == t->id, "Thing cannot carry itself."))
267 struct Thing * o = get_thing(world.things, id, 0);
268 if (!err_line(!o, "Thing not available for carrying."))
270 own_thing(&(t->owns), &world.things, id);
281 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
283 static struct Thing * t = NULL;
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 err_line(1, "No thing defined to manipulate yet.");
294 if ( parse_thing_type(tok0, tok1, t)
295 || parse_thing_command(tok0, tok1, t)
296 || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
297 || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
298 || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
299 || parse_position(tok0, tok1, t)
300 || parse_carry(tok0, tok1, t));
301 else if (parse_val(tok0, tok1, s[S_CMD_THING], 'i', (char *) &id))
303 t = get_thing(world.things, id, 1);
304 char * err = "No thing type found to initialize new thing.";
305 if (!t && !err_line(NULL == world.thing_types, err))
307 t = add_thing(id, world.thing_types->id, 0, 0);
308 if (world.exists && t->lifepoints)
310 t->fov_map = build_fov_map(t);
323 static uint8_t world_may_be_set_active()
325 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
327 err_line(1, "No thing action of name 'wait' found.");
332 err_line(1, "No un-owned player thing (of id=0) found.");
335 if (!world.map.cells)
337 err_line(1, "No map found.");
345 static void remove_worldstate_file()
347 if (!access(s[S_PATH_WORLDSTATE], F_OK))
349 int test = unlink(s[S_PATH_WORLDSTATE]);
350 exit_trouble(-1 == test, __func__, "unlink");
356 static uint8_t parse_world_active(char * tok0, char * tok1)
358 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
360 if (!parsetest_int(tok1, '8'))
362 uint8_t argument = atoi(tok1);
365 remove_worldstate_file();
368 else if (world.exists)
370 err_line(1, "World already active.");
372 else if (world_may_be_set_active())
375 for (ti = world.things; ti; ti = ti->next)
383 ti->fov_map = build_fov_map(ti);
396 static uint8_t set_map_length(char * tok0, char * tok1)
398 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
400 uint16_t argument = atoi(tok1);
401 if (argument < 1 || argument > 256)
403 err_line(1, "Value must be >= 1 and <= 256.");
407 remove_worldstate_file();
408 free_things(world.things);
409 free(world.map.cells);
410 world.map.cells = NULL; /* Since remake_map() runs free() on this. */
411 world.map.length = argument;
419 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
421 if ( parse_thingtype_manipulation(tok0, tok1)
422 || parse_thingaction_manipulation(tok0, tok1)
423 || parse_thing_manipulation(tok0, tok1)
424 || set_map_length(tok0,tok1)
425 || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
426 || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
427 || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
428 || parse_world_active(tok0, tok1));
429 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
434 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
436 uint8_t test = remake_world();
437 err_line(1 == test, "No player type with start number of >0 defined.");
438 err_line(2 == test, "No thing action with name 'wait' defined.");