home · contact · privacy
Server: Add inanimate things to map memory, integrate into AI searches.
[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         err_line(1, "No thing type defined to manipulate yet.");
89         return 1;
90     }
91     int16_t id;
92     if (   parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
93         || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
94         || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
95         || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
96         || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
97     else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
98     {
99         if (!get_thing_type(id))
100         {
101             err_line(1, "Corpse ID belongs to no known thing type.");
102             return 1;
103         }
104         tt->corpse_id = id;
105     }
106     else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
107     {
108         tt = get_thing_type(id);
109         if (!tt)
110         {
111             tt = add_thing_type(id);
112         }
113     }
114     else
115     {
116         return 0;
117     }
118     return 1;
119 }
120
121
122
123 static uint8_t try_func_name(struct ThingAction * ta, char * name,
124                              void (* func) (struct Thing *))
125 {
126     if (0 == strcmp(ta->name, name))
127     {
128         ta->func = func;
129         return 1;
130     }
131     return 0;
132 }
133
134
135
136 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
137 {
138     if (!ta &&
139         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
140     {
141         err_line(1, "No thing action defined to manipulate yet.");
142         return 1;
143     }
144     int16_t id;
145     if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
146     else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
147     {
148         if (!(   try_func_name(ta, s[S_CMD_MOVE], actor_move)
149               || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
150               || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
151               || try_func_name(ta, s[S_CMD_DROP], actor_drop)
152               || try_func_name(ta, s[S_CMD_USE], actor_use)))
153         {
154             err_line(1, "Invalid action function name.");
155             return 1;
156         }         /* Legal worlds have at least one thing action for waiting. */
157         if (world.exists)
158         {
159             world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
160             if (!world.exists)
161             {
162                 remove_worldstate_file();
163             }
164         }
165     }
166     else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
167     {
168         ta = get_thing_action(id);
169         if (!ta)
170         {
171             ta = add_thing_action(id);
172         }
173     }
174     else
175     {
176         return 0;
177     }
178     return 1;
179 }
180
181
182
183 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
184 {
185     uint8_t type;
186     if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
187     {
188         struct ThingType * tt = get_thing_type(type);
189         if (!err_line(!tt, "Thing type does not exist."))
190         {
191             t->type = type;
192         }
193         return 1;
194     }
195     return 0;
196 }
197
198
199
200 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
201 {
202     uint8_t command;
203     if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
204     {
205         if (!command)
206         {
207             t->command = command;
208             return 1;
209         }
210         struct ThingAction * ta = world.thing_actions;
211         for (; ta && command != ta->id; ta = ta->next);
212         if (!err_line(!ta, "Thing action does not exist."))
213         {
214             t->command = command;
215         }
216         return 1;
217     }
218     return 0;
219 }
220
221
222
223 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
224 {
225     char axis = 0;
226     if      (!strcmp(tok0, s[S_CMD_T_POSY]))
227     {
228         axis = 'y';
229     }
230     else if (!strcmp(tok0, s[S_CMD_T_POSX]))
231     {
232         axis = 'x';
233     }
234     if (axis && !parsetest_int(tok1, '8'))
235     {
236         uint8_t length = atoi(tok1);
237         char * err = "Position is outside of map.";
238         if (!err_line(length >= world.map.length, err))
239         {
240             if      ('y' == axis)
241             {
242                 t->pos.y = length;
243             }
244             else if ('x' == axis)
245             {
246                 t->pos.x = length;
247             }
248             if (world.exists && t->lifepoints)
249             {
250                 build_fov_map(t);
251             }
252         }
253         return 1;
254     }
255     return 0;
256 }
257
258
259
260 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
261 {
262     uint8_t id;
263     if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
264     {
265         if (!err_line(id == t->id, "Thing cannot carry itself."))
266         {
267             struct Thing * o = get_thing(world.things, id, 0);
268             if (!err_line(!o, "Thing not available for carrying."))
269             {
270                 own_thing(&(t->owns), &world.things, id);
271                 o->pos = t->pos;
272             }
273         }
274         return 1;
275     }
276     return 0;
277 }
278
279
280
281 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
282 {
283     if (!t &&
284         (   !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
285          || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
286          || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
287          || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
288     {
289         err_line(1, "No thing defined to manipulate yet.");
290         return 1;
291     }
292     int16_t id;
293     if (   parse_thing_type(tok0, tok1, t)
294         || parse_thing_command(tok0, tok1, t)
295         || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
296         || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
297         || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
298         || parse_position(tok0, tok1, t)
299         || parse_carry(tok0, tok1, t));
300     else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
301     {
302         t = get_thing(world.things, id, 1);
303         char * err = "No thing type found to initialize new thing.";
304         if (!t && !err_line(NULL == world.thing_types, err))
305         {
306             t = add_thing(id, world.thing_types->id, 0, 0);
307             if (world.exists && t->lifepoints)
308             {
309                 build_fov_map(t);
310             }
311         }
312     }
313     else
314     {
315         return 0;
316     }
317     return 1;
318 }
319
320
321
322 static uint8_t world_may_be_set_active()
323 {
324     if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
325     {
326         err_line(1, "No thing action of name 'wait' found.");
327         return 0;
328     }
329     if (!get_player())
330     {
331         err_line(1, "No un-owned player thing (of id=0) found.");
332         return 0;
333     }
334     if (!world.map.cells)
335     {
336         err_line(1, "No map found.");
337         return 0;
338     }
339     return 1;
340 }
341
342
343
344 static void remove_worldstate_file()
345 {
346     if (!access(s[S_PATH_WORLDSTATE], F_OK))
347     {
348         int test = unlink(s[S_PATH_WORLDSTATE]);
349         exit_trouble(-1 == test, __func__, "unlink");
350     }
351 }
352
353
354
355 static uint8_t parse_world_active(char * tok0, char * tok1)
356 {
357     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
358     {
359         if (!parsetest_int(tok1, '8'))
360         {
361             uint8_t argument = atoi(tok1);
362             if (!argument)
363             {
364                 remove_worldstate_file();
365                 world.exists = 0;
366             }
367             else if (world.exists)
368             {
369                 err_line(1, "World already active.");
370             }
371             else if (world_may_be_set_active())
372             {
373                 struct Thing * ti;
374                 for (ti = world.things; ti; ti = ti->next)
375                 {
376                     if (ti->lifepoints)
377                     {
378                         build_fov_map(ti);
379                     }
380                 }
381                 world.exists = 1;
382             }
383             return 1;
384         }
385     }
386     return 0;
387 }
388
389
390
391 static uint8_t set_map_length(char * tok0, char * tok1)
392 {
393     if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
394     {
395         uint16_t argument = atoi(tok1);
396         if (argument < 1 || argument > 256)
397         {
398             err_line(1, "Value must be >= 1 and <= 256.");
399             return 1;
400         }
401         world.exists = 0;
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;
407         return 1;
408     }
409     return 0;
410 }
411
412
413
414 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
415 {
416     if (   parse_thingtype_manipulation(tok0, tok1)
417         || parse_thingaction_manipulation(tok0, tok1)
418         || parse_thing_manipulation_1arg(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))
425
426     {
427         remake_map();
428     }
429     else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
430     {
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.");
434     }
435     else
436     {
437         return 0;
438     }
439     return 1;
440 }
441
442
443
444 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
445 {
446     if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
447     {
448         err_line(1, "No thing defined to manipulate yet.");
449         return 1;
450     }
451     if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
452     {
453         uint8_t y = atoi(tok1);
454         if (parsetest_int(tok1, '8') || y >= world.map.length)
455         {
456             err_line(1, "Illegal value for map line number.");
457             return 1;
458         }
459         if (strlen(tok2) != world.map.length)
460         {
461             err_line(1, "Map line length is unequal map width.");
462             return 1;
463         }
464         if (!t->mem_map)
465         {
466             uint32_t map_size = world.map.length * world.map.length;
467             t->mem_map = try_malloc(map_size, __func__);
468             memset(t->mem_map, ' ', map_size);
469         }
470         memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
471     }
472     else
473     {
474         return 0;
475     }
476     return 1;
477 }
478
479
480
481 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
482                                       char * tok3)
483 {
484     if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
485     {
486         err_line(1, "No thing defined to manipulate yet.");
487         return 1;
488     }
489     if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
490     {
491         uint8_t id = atoi(tok1);
492         uint8_t y  = atoi(tok2);
493         uint8_t x  = atoi(tok3);
494         if (   parsetest_int(tok1, '8') || !get_thing_type(id)
495             || parsetest_int(tok2, '8') || y >= world.map.length
496             || parsetest_int(tok3, '8') || x >= world.map.length)
497         {
498             err_line(1, "Illegal value for thing type or position.");
499             return 1;
500         }
501         add_thing_to_memory_map(t, id, y, x);
502     }
503     else
504     {
505         return 0;
506     }
507     return 1;
508 }