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 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
57 * argument is 0 and unlink worldstate file, but only set it on positive
58 * argument if it is not already set and a thing action of name S_CMD_WAIT, a
59 * player thing and a map are defined. On setting it, rebuild all FOVs.
61 static uint8_t parse_world_active(char * tok0, char * tok1);
63 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
64 * re-set, set world.exists to 0, remove all things and free world.map.cells
66 static uint8_t set_map_length(char * tok0, char * tok1);
70 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
72 static struct ThingType * tt = NULL;
74 ( !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
75 || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
76 || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
78 err_line(1, "No thing type defined to manipulate yet.");
82 if ( parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
83 || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
84 || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
85 || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
86 || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
87 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
89 if (!get_thing_type(id))
91 err_line(1, "Corpse ID belongs to no known thing type.");
96 else if (parse_val(tok0, tok1, s[S_CMD_THINGTYPE], '8', (char *) &id))
98 tt = get_thing_type(id);
101 tt = add_thing_type(id);
113 static uint8_t try_func_name(struct ThingAction * ta, char * name,
114 void (* func) (struct Thing *))
116 if (0 == strcmp(ta->name, name))
126 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
128 static struct ThingAction * ta = NULL;
130 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
132 err_line(1, "No thing action defined to manipulate yet.");
136 if (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
137 else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
139 if (!( try_func_name(ta, s[S_CMD_MOVE], actor_move)
140 || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
141 || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
142 || try_func_name(ta, s[S_CMD_DROP], actor_drop)
143 || try_func_name(ta, s[S_CMD_USE], actor_use)))
145 err_line(1, "Invalid action function name.");
147 } /* Legal worlds have at least one thing action for waiting. */
150 world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
153 else if (parse_val(tok0, tok1, s[S_CMD_THINGACTION], '8', (char *) &id))
155 ta = get_thing_action(id);
158 ta = add_thing_action(id);
170 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
173 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
175 struct ThingType * tt = get_thing_type(type);
176 if (!err_line(!tt, "Thing type does not exist."))
187 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
190 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
194 t->command = command;
197 struct ThingAction * ta = world.thing_actions;
198 for (; ta && command != ta->id; ta = ta->next);
199 if (!err_line(!ta, "Thing action does not exist."))
201 t->command = command;
210 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
213 if (!strcmp(tok0, s[S_CMD_T_POSY]))
217 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
221 if (axis && !parsetest_int(tok1, '8'))
223 uint8_t length = atoi(tok1);
224 char * err = "Position is outside of map.";
225 if (!err_line(length >= world.map.length, err))
231 else if ('x' == axis)
236 if (world.exists && t->lifepoints)
238 t->fov_map = build_fov_map(t);
248 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
251 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
253 if (!err_line(id == t->id, "Thing cannot carry itself."))
255 struct Thing * o = get_thing(world.things, id, 0);
256 if (!err_line(!o, "Thing not available for carrying."))
258 own_thing(&(t->owns), &world.things, id);
269 static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
271 static struct Thing * t = NULL;
273 ( !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
274 || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
275 || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
276 || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
278 err_line(1, "No thing defined to manipulate yet.");
282 if ( parse_thing_type(tok0, tok1, t)
283 || parse_thing_command(tok0, tok1, t)
284 || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
285 || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
286 || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
287 || parse_position(tok0, tok1, t)
288 || parse_carry(tok0, tok1, t));
289 else if (parse_val(tok0, tok1, s[S_CMD_THING], 'i', (char *) &id))
291 t = get_thing(world.things, id, 1);
292 char * err = "No thing type found to initialize new thing.";
293 if (!t && !err_line(NULL == world.thing_types, err))
295 t = add_thing(id, world.thing_types->id, 0, 0);
296 if (world.exists && t->lifepoints)
298 t->fov_map = build_fov_map(t);
311 static uint8_t world_may_be_set_active()
313 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
315 err_line(1, "No thing action of name 'wait' found.");
320 err_line(1, "No un-owned player thing (of id=0) found.");
323 if (!world.map.cells)
325 err_line(1, "No map found.");
333 static uint8_t parse_world_active(char * tok0, char * tok1)
335 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
337 if (!parsetest_int(tok1, '8'))
339 uint8_t argument = atoi(tok1);
342 if (!access(s[S_PATH_WORLDSTATE], F_OK))
344 int test = unlink(s[S_PATH_WORLDSTATE]);
345 exit_trouble(-1 == test, __func__, "unlink");
349 else if (world.exists)
351 err_line(1, "World already active.");
353 else if (world_may_be_set_active())
356 for (ti = world.things; ti; ti = ti->next)
364 ti->fov_map = build_fov_map(ti);
377 static uint8_t set_map_length(char * tok0, char * tok1)
379 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
381 uint16_t argument = atoi(tok1);
382 if (argument < 1 || argument > 256)
384 err_line(1, "Value must be >= 1 and <= 256.");
388 free_things(world.things);
389 free(world.map.cells);
390 world.map.cells = NULL; /* Since remake_map() runs free() on this. */
391 world.map.length = argument;
399 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
401 if ( parse_thingtype_manipulation(tok0, tok1)
402 || parse_thingaction_manipulation(tok0, tok1)
403 || parse_thing_manipulation(tok0, tok1)
404 || set_map_length(tok0,tok1)
405 || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
406 || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
407 || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
408 || parse_world_active(tok0, tok1));
409 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
414 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
416 uint8_t test = remake_world();
417 err_line(1 == test, "No player type with start number of >0 defined.");
418 err_line(2 == test, "No thing action with name 'wait' defined.");