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(), memset(), memcpy() */
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 "../common/try_malloc.h" /* try_malloc() */
12 #include "cleanup.h" /* unset_cleanup_flag() */
13 #include "field_of_view.h" /* build_fov_map() */
14 #include "hardcoded_strings.h" /* s */
15 #include "init.h" /* remake_world() */
16 #include "map.h" /* remake_map() */
17 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
18 * actor_use(), actor_pickup(), actor_drop()
20 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
21 * free_things(), add_thing_to_memory_map(),get_thing_type()
23 #include "world.h" /* world */
27 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
28 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
30 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
31 * .func from .name for set_members().
33 static uint8_t try_func_name(struct ThingAction * ta, char * name,
34 void (* func) (struct Thing *));
36 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
37 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
39 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
40 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
42 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
43 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
45 /* Parse/apply god command in "tok0"/"tok1" on positioning a thing "t". */
46 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
48 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
49 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
51 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
52 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
54 /* Performs parse_world_active()'s world activation legality tests. */
55 static uint8_t world_may_be_set_active();
57 /* Unlink worldstate file if it exists. */
58 static void remove_worldstate_file();
60 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
61 * argument is 0 and unlink worldstate file, but only set it on positive
62 * argument if it is not already set and a thing action of name S_CMD_WAIT, a
63 * player thing and a map are defined. On setting it, rebuild all FOVs.
65 static uint8_t parse_world_active(char * tok0, char * tok1);
67 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
68 * re-set, set world.exists to 0, remove all things and free world.map.cells
70 static uint8_t set_map_length(char * tok0, char * tok1);
74 /* Thing, ThingType or ThingAction selected to be manipulated. */
75 static struct Thing * t = NULL;
76 static struct ThingType * tt = NULL;
77 static struct ThingAction * ta = NULL;
81 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
84 ( !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
85 || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
86 || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
88 return err_line(1, "No thing type defined to manipulate yet.");
91 if ( parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
92 || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
93 || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
94 || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
95 || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
96 else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
98 if (!get_thing_type(id))
100 return err_line(1, "Corpse ID belongs to no known thing type.");
104 else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
106 tt = get_thing_type(id);
109 tt = add_thing_type(id);
121 static uint8_t try_func_name(struct ThingAction * ta, char * name,
122 void (* func) (struct Thing *))
124 if (0 == strcmp(ta->name, name))
134 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
137 (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
139 return err_line(1, "No thing action defined to manipulate yet.");
142 if (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
143 else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
145 if (!( try_func_name(ta, s[S_CMD_MOVE], actor_move)
146 || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
147 || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
148 || try_func_name(ta, s[S_CMD_DROP], actor_drop)
149 || try_func_name(ta, s[S_CMD_USE], actor_use)))
151 return err_line(1, "Invalid action function name.");
154 { /* Legal worlds have at least one thing action for waiting. */
155 world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
158 remove_worldstate_file();
162 else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
164 ta = get_thing_action(id);
167 ta = add_thing_action(id);
179 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
182 if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
184 struct ThingType * tt = get_thing_type(type);
185 if (!err_line(!tt, "Thing type does not exist."))
196 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
199 if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
203 t->command = command;
206 struct ThingAction * ta = world.thing_actions;
207 for (; ta && command != ta->id; ta = ta->next);
208 if (!err_line(!ta, "Thing action does not exist."))
210 t->command = command;
219 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
222 if (!strcmp(tok0, s[S_CMD_T_POSY]))
226 else if (!strcmp(tok0, s[S_CMD_T_POSX]))
230 if (axis && !parsetest_int(tok1, '8'))
232 uint8_t length = atoi(tok1);
233 char * err = "Position is outside of map.";
234 if (!err_line(length >= world.map.length, err))
240 else if ('x' == axis)
244 if (world.exists && t->lifepoints)
256 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
259 if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
261 if (!err_line(id == t->id, "Thing cannot carry itself."))
263 struct Thing * o = get_thing(world.things, id, 0);
264 if (!err_line(!o, "Thing not available for carrying."))
266 own_thing(&(t->owns), &world.things, id);
277 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
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 return err_line(1, "No thing defined to manipulate yet.");
288 if ( parse_thing_type(tok0, tok1, t)
289 || parse_thing_command(tok0, tok1, t)
290 || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
291 || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
292 || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
293 || parse_position(tok0, tok1, t)
294 || parse_carry(tok0, tok1, t));
295 else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
297 t = get_thing(world.things, id, 1);
298 char * err = "No thing type found to initialize new thing.";
299 if (!t && !err_line(!world.thing_types, err))
301 t = add_thing(id, world.thing_types->id, 0, 0);
302 if (world.exists && t->lifepoints)
317 static uint8_t world_may_be_set_active()
319 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
321 err_line(1, "No thing action of name 'wait' found.");
326 err_line(1, "No un-owned player thing (of id=0) found.");
329 if (!world.map.cells)
331 err_line(1, "No map found.");
339 static void remove_worldstate_file()
341 if (!access(s[S_PATH_WORLDSTATE], F_OK))
343 int test = unlink(s[S_PATH_WORLDSTATE]);
344 exit_trouble(-1 == test, __func__, "unlink");
350 static uint8_t parse_world_active(char * tok0, char * tok1)
352 if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
354 if (!parsetest_int(tok1, '8'))
356 uint8_t argument = atoi(tok1);
359 remove_worldstate_file();
362 else if (world.exists)
364 err_line(1, "World already active.");
366 else if (world_may_be_set_active())
369 for (ti = world.things; ti; ti = ti->next)
386 static uint8_t set_map_length(char * tok0, char * tok1)
388 if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
390 uint16_t argument = atoi(tok1);
391 if (argument < 1 || argument > 256)
393 return err_line(1, "Value must be >= 1 and <= 256.");
396 remove_worldstate_file();
397 free_things(world.things);
398 free(world.map.cells);
399 world.map.cells = NULL; /* Since remake_map() runs free() on this. */
400 world.map.length = argument;
408 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
410 if ( parse_thingtype_manipulation(tok0, tok1)
411 || parse_thingaction_manipulation(tok0, tok1)
412 || parse_thing_manipulation_1arg(tok0, tok1)
413 || set_map_length(tok0,tok1)
414 || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
415 || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
416 || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
417 || parse_world_active(tok0, tok1));
418 else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
423 else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
425 uint8_t test = remake_world();
426 err_line(1 == test, "No player type with start number of >0 defined.");
427 err_line(2 == test, "No thing action with name 'wait' defined.");
438 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
440 if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
442 return err_line(1, "No thing defined to manipulate yet.");
444 if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
446 uint8_t y = atoi(tok1);
447 if (parsetest_int(tok1, '8') || y >= world.map.length)
449 return err_line(1, "Illegal value for map line number.");
451 if (strlen(tok2) != world.map.length)
453 return err_line(1, "Map line length is unequal map width.");
457 uint32_t map_size = world.map.length * world.map.length;
458 t->mem_map = try_malloc(map_size, __func__);
459 memset(t->mem_map, ' ', map_size);
461 memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
472 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
475 if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
477 return err_line(1, "No thing defined to manipulate yet.");
479 if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
481 uint8_t id = atoi(tok1);
482 uint8_t y = atoi(tok2);
483 uint8_t x = atoi(tok3);
484 if ( parsetest_int(tok1, '8') || !get_thing_type(id)
485 || parsetest_int(tok2, '8') || y >= world.map.length
486 || parsetest_int(tok3, '8') || x >= world.map.length)
488 return err_line(1, "Illegal value for thing type or position.");
490 add_thing_to_memory_map(t, id, y, x);