home · contact · privacy
4969b7e1f9e50504252e42dd6a85e9c563716af0
[plomrogue] / src / server / god_commands.c
1 /* src/server/god_commands.c */
2
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()
19                             */
20 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
21                      * free_things(), add_thing_to_memory_map(),get_thing_type()
22                      */
23 #include "world.h" /* world */
24
25
26
27 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
28 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
29
30 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
31  * .func from .name for set_members().
32  */
33 static uint8_t try_func_name(struct ThingAction * ta, char * name,
34                              void (* func) (struct Thing *));
35
36 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
37 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
38
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);
41
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);
44
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);
47
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);
50
51 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
52 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
53
54 /* Performs parse_world_active()'s world activation legality tests. */
55 static uint8_t world_may_be_set_active();
56
57 /* Unlink worldstate file if it exists. */
58 static void remove_worldstate_file();
59
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.
64  */
65 static uint8_t parse_world_active(char * tok0, char * tok1);
66
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
69  */
70 static uint8_t set_map_length(char * tok0, char * tok1);
71
72
73
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;
78
79
80
81 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
82 {
83     if (!tt &&
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])))
87     {
88         return err_line(1, "No thing type defined to manipulate yet.");
89     }
90     int16_t id;
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))
97     {
98         if (!get_thing_type(id))
99         {
100             return err_line(1, "Corpse ID belongs to no known thing type.");
101         }
102         tt->corpse_id = id;
103     }
104     else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
105     {
106         tt = get_thing_type(id);
107         if (!tt)
108         {
109             tt = add_thing_type(id);
110         }
111     }
112     else
113     {
114         return 0;
115     }
116     return 1;
117 }
118
119
120
121 static uint8_t try_func_name(struct ThingAction * ta, char * name,
122                              void (* func) (struct Thing *))
123 {
124     if (0 == strcmp(ta->name, name))
125     {
126         ta->func = func;
127         return 1;
128     }
129     return 0;
130 }
131
132
133
134 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
135 {
136     if (!ta &&
137         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
138     {
139         return err_line(1, "No thing action defined to manipulate yet.");
140     }
141     int16_t id;
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))
144     {
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)))
150         {
151             return err_line(1, "Invalid action function name.");
152         }
153         if (world.exists)
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]);
156             if (!world.exists)
157             {
158                 remove_worldstate_file();
159             }
160         }
161     }
162     else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
163     {
164         ta = get_thing_action(id);
165         if (!ta)
166         {
167             ta = add_thing_action(id);
168         }
169     }
170     else
171     {
172         return 0;
173     }
174     return 1;
175 }
176
177
178
179 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
180 {
181     uint8_t type;
182     if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
183     {
184         struct ThingType * tt = get_thing_type(type);
185         if (!err_line(!tt, "Thing type does not exist."))
186         {
187             t->type = type;
188         }
189         return 1;
190     }
191     return 0;
192 }
193
194
195
196 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
197 {
198     uint8_t command;
199     if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
200     {
201         if (!command)
202         {
203             t->command = command;
204             return 1;
205         }
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."))
209         {
210             t->command = command;
211         }
212         return 1;
213     }
214     return 0;
215 }
216
217
218
219 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
220 {
221     char axis = 0;
222     if      (!strcmp(tok0, s[S_CMD_T_POSY]))
223     {
224         axis = 'y';
225     }
226     else if (!strcmp(tok0, s[S_CMD_T_POSX]))
227     {
228         axis = 'x';
229     }
230     if (axis && !parsetest_int(tok1, '8'))
231     {
232         uint8_t length = atoi(tok1);
233         char * err = "Position is outside of map.";
234         if (!err_line(length >= world.map.length, err))
235         {
236             if      ('y' == axis)
237             {
238                 t->pos.y = length;
239             }
240             else if ('x' == axis)
241             {
242                 t->pos.x = length;
243             }
244             if (world.exists && t->lifepoints)
245             {
246                 build_fov_map(t);
247             }
248         }
249         return 1;
250     }
251     return 0;
252 }
253
254
255
256 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
257 {
258     uint8_t id;
259     if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
260     {
261         if (!err_line(id == t->id, "Thing cannot carry itself."))
262         {
263             struct Thing * o = get_thing(world.things, id, 0);
264             if (!err_line(!o, "Thing not available for carrying."))
265             {
266                 own_thing(&(t->owns), &world.things, id);
267                 o->pos = t->pos;
268             }
269         }
270         return 1;
271     }
272     return 0;
273 }
274
275
276
277 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
278 {
279     if (!t &&
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])))
284     {
285         return err_line(1, "No thing defined to manipulate yet.");
286     }
287     int16_t id;
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))
296     {
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))
300         {
301             t = add_thing(id, world.thing_types->id, 0, 0);
302             if (world.exists && t->lifepoints)
303             {
304                 build_fov_map(t);
305             }
306         }
307     }
308     else
309     {
310         return 0;
311     }
312     return 1;
313 }
314
315
316
317 static uint8_t world_may_be_set_active()
318 {
319     if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
320     {
321         err_line(1, "No thing action of name 'wait' found.");
322         return 0;
323     }
324     if (!get_player())
325     {
326         err_line(1, "No un-owned player thing (of id=0) found.");
327         return 0;
328     }
329     if (!world.map.cells)
330     {
331         err_line(1, "No map found.");
332         return 0;
333     }
334     return 1;
335 }
336
337
338
339 static void remove_worldstate_file()
340 {
341     if (!access(s[S_PATH_WORLDSTATE], F_OK))
342     {
343         int test = unlink(s[S_PATH_WORLDSTATE]);
344         exit_trouble(-1 == test, __func__, "unlink");
345     }
346 }
347
348
349
350 static uint8_t parse_world_active(char * tok0, char * tok1)
351 {
352     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
353     {
354         if (!parsetest_int(tok1, '8'))
355         {
356             uint8_t argument = atoi(tok1);
357             if (!argument)
358             {
359                 remove_worldstate_file();
360                 world.exists = 0;
361             }
362             else if (world.exists)
363             {
364                 err_line(1, "World already active.");
365             }
366             else if (world_may_be_set_active())
367             {
368                 struct Thing * ti;
369                 for (ti = world.things; ti; ti = ti->next)
370                 {
371                     if (ti->lifepoints)
372                     {
373                         build_fov_map(ti);
374                     }
375                 }
376                 world.exists = 1;
377             }
378             return 1;
379         }
380     }
381     return 0;
382 }
383
384
385
386 static uint8_t set_map_length(char * tok0, char * tok1)
387 {
388     if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
389     {
390         uint16_t argument = atoi(tok1);
391         if (argument < 1 || argument > 256)
392         {
393             return err_line(1, "Value must be >= 1 and <= 256.");
394         }
395         world.exists = 0;
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;
401         return 1;
402     }
403     return 0;
404 }
405
406
407
408 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
409 {
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))
419
420     {
421         remake_map();
422     }
423     else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
424     {
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.");
428     }
429     else
430     {
431         return 0;
432     }
433     return 1;
434 }
435
436
437
438 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
439 {
440     if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
441     {
442         return err_line(1, "No thing defined to manipulate yet.");
443     }
444     if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
445     {
446         uint8_t y = atoi(tok1);
447         if (parsetest_int(tok1, '8') || y >= world.map.length)
448         {
449             return err_line(1, "Illegal value for map line number.");
450         }
451         if (strlen(tok2) != world.map.length)
452         {
453             return err_line(1, "Map line length is unequal map width.");
454         }
455         if (!t->mem_map)
456         {
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);
460         }
461         memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
462     }
463     else
464     {
465         return 0;
466     }
467     return 1;
468 }
469
470
471
472 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
473                                       char * tok3)
474 {
475     if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
476     {
477         return err_line(1, "No thing defined to manipulate yet.");
478     }
479     if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
480     {
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)
487         {
488             return err_line(1, "Illegal value for thing type or position.");
489         }
490         add_thing_to_memory_map(t, id, y, x);
491     }
492     else
493     {
494         return 0;
495     }
496     return 1;
497 }