home · contact · privacy
Server: New command TT_PROLIFERATE sets chance for thing proliferation.
[plomrogue] / src / server / god_commands.c
1 /* src/server/god_commands.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "god_commands.h"
9 #include <stddef.h> /* NULL */
10 #include <stdint.h> /* uint8_t */
11 #include <stdlib.h> /* atoi(), free() */
12 #include <string.h> /* strcmp(), memset(), memcpy() */
13 #include <unistd.h> /* F_OK, access(), unlink() */
14 #include "../common/parse_file.h" /* err_line(), parse_val(), parsetest_int() */
15 #include "../common/rexit.h" /* exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "cleanup.h" /* unset_cleanup_flag() */
18 #include "field_of_view.h" /* build_fov_map() */
19 #include "hardcoded_strings.h" /* s */
20 #include "init.h" /* remake_world() */
21 #include "map.h" /* remake_map() */
22 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
23                             * actor_use(), actor_pickup(), actor_drop()
24                             */
25 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
26                      * free_things(), add_thing_to_memory_map(),get_thing_type()
27                      */
28 #include "world.h" /* world */
29
30
31
32 /* Parse/apply god command in "tok0"/tok1" to manipulate a ThingType*/
33 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1);
34
35 /* If "name" fits "ta"->name, set "ta"->func to "func". (Derives ThingAction
36  * .func from .name for set_members().
37  */
38 static uint8_t try_func_name(struct ThingAction * ta, char * name,
39                              void (* func) (struct Thing *));
40
41 /* Parse/apply god command in "tok0"/"tok1" to manipulate a ThingAction. */
42 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1);
43
44 /* Parse/apply god command in "tok0"/"tok1" oo setting "t"'s thing type. */
45 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t);
46
47 /* Parse/apply god command in "tok0"/"tok1" on setting up thing "t". */
48 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t);
49
50 /* Parse/apply god command in "tok0"/"tok1" on positioning a thing "t". */
51 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
52
53 /* Parse/apply god command in "tok0"/"tok1" on "t" owning another thing. */
54 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
55
56 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
57 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
58
59 /* Performs parse_world_active()'s world activation legality tests. */
60 static uint8_t world_may_be_set_active();
61
62 /* Unlink worldstate file if it exists. */
63 static void remove_worldstate_file();
64
65 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
66  * argument is 0 and unlink worldstate file, but only set it on positive
67  * argument if it is not already set and a thing action of name S_CMD_WAIT, a
68  * player thing and a map are defined. On setting it, rebuild all FOVs.
69  */
70 static uint8_t parse_world_active(char * tok0, char * tok1);
71
72 /* Parse/apply god command in "tok0"/"tok1" to reset world.map.length. On
73  * re-set, set world.exists to 0, remove all things and free world.map.cells
74  */
75 static uint8_t set_map_length(char * tok0, char * tok1);
76
77
78
79 /* Thing, ThingType or ThingAction selected to be manipulated. */
80 static struct Thing * t = NULL;
81 static struct ThingType * tt = NULL;
82 static struct ThingAction * ta = NULL;
83
84
85
86 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
87 {
88     if (!tt &&
89         (   !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
90          || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
91          || !strcmp(tok0, s[S_CMD_TT_CORPS])  || !strcmp(tok0, s[S_CMD_TT_HP])
92          || !strcmp(tok0, s[S_CMD_TT_PROL])))
93     {
94         return err_line(1, "No thing type defined to manipulate yet.");
95     }
96     int16_t id;
97     if (   parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
98         || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
99         || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
100         || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
101         || parse_val(tok0,tok1,s[S_CMD_TT_PROL],'8',(char *) &tt->proliferate)
102         || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
103     else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
104     {
105         if (!get_thing_type(id))
106         {
107             return err_line(1, "Corpse ID belongs to no known thing type.");
108         }
109         tt->corpse_id = id;
110     }
111     else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
112     {
113         tt = get_thing_type(id);
114         if (!tt)
115         {
116             tt = add_thing_type(id);
117         }
118     }
119     else
120     {
121         return 0;
122     }
123     return 1;
124 }
125
126
127
128 static uint8_t try_func_name(struct ThingAction * ta, char * name,
129                              void (* func) (struct Thing *))
130 {
131     if (0 == strcmp(ta->name, name))
132     {
133         ta->func = func;
134         return 1;
135     }
136     return 0;
137 }
138
139
140
141 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
142 {
143     if (!ta &&
144         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
145     {
146         return err_line(1, "No thing action defined to manipulate yet.");
147     }
148     int16_t id;
149     if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
150     else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
151     {
152         if (!(   try_func_name(ta, s[S_CMD_MOVE], actor_move)
153               || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
154               || try_func_name(ta, s[S_CMD_WAIT], actor_wait)
155               || try_func_name(ta, s[S_CMD_DROP], actor_drop)
156               || try_func_name(ta, s[S_CMD_USE], actor_use)))
157         {
158             return err_line(1, "Invalid action function name.");
159         }
160         if (world.exists)
161         {         /* Legal worlds have at least one thing action for waiting. */
162             world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
163             if (!world.exists)
164             {
165                 remove_worldstate_file();
166             }
167         }
168     }
169     else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
170     {
171         ta = get_thing_action(id);
172         if (!ta)
173         {
174             ta = add_thing_action(id);
175         }
176     }
177     else
178     {
179         return 0;
180     }
181     return 1;
182 }
183
184
185
186 static uint8_t parse_thing_type(char * tok0, char * tok1, struct Thing * t)
187 {
188     uint8_t type;
189     if (parse_val(tok0, tok1, s[S_CMD_T_TYPE], '8', (char *) &type))
190     {
191         struct ThingType * tt = get_thing_type(type);
192         if (!err_line(!tt, "Thing type does not exist."))
193         {
194             t->type = type;
195         }
196         return 1;
197     }
198     return 0;
199 }
200
201
202
203 static uint8_t parse_thing_command(char * tok0, char * tok1, struct Thing * t)
204 {
205     uint8_t command;
206     if (parse_val(tok0, tok1, s[S_CMD_T_COMMAND], '8', (char *) &command))
207     {
208         if (!command)
209         {
210             t->command = command;
211             return 1;
212         }
213         struct ThingAction * ta = world.thing_actions;
214         for (; ta && command != ta->id; ta = ta->next);
215         if (!err_line(!ta, "Thing action does not exist."))
216         {
217             t->command = command;
218         }
219         return 1;
220     }
221     return 0;
222 }
223
224
225
226 static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
227 {
228     char axis = 0;
229     if      (!strcmp(tok0, s[S_CMD_T_POSY]))
230     {
231         axis = 'y';
232     }
233     else if (!strcmp(tok0, s[S_CMD_T_POSX]))
234     {
235         axis = 'x';
236     }
237     if (axis && !parsetest_int(tok1, '8'))
238     {
239         uint8_t length = atoi(tok1);
240         char * err = "Position is outside of map.";
241         if (!err_line(length >= world.map.length, err))
242         {
243             if      ('y' == axis)
244             {
245                 t->pos.y = length;
246             }
247             else if ('x' == axis)
248             {
249                 t->pos.x = length;
250             }
251             if (world.exists && t->lifepoints)
252             {
253                 build_fov_map(t);
254             }
255         }
256         return 1;
257     }
258     return 0;
259 }
260
261
262
263 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
264 {
265     uint8_t id;
266     if (parse_val(tok0, tok1, s[S_CMD_T_CARRIES], '8', (char *) &id))
267     {
268         if (!err_line(id == t->id, "Thing cannot carry itself."))
269         {
270             struct Thing * o = get_thing(world.things, id, 0);
271             if (!err_line(!o, "Thing not available for carrying."))
272             {
273                 own_thing(&(t->owns), &world.things, id);
274                 o->pos = t->pos;
275             }
276         }
277         return 1;
278     }
279     return 0;
280 }
281
282
283
284 static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
285 {
286     if (!t &&
287         (   !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
288          || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
289          || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
290          || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
291     {
292         return err_line(1, "No thing defined to manipulate yet.");
293     }
294     int16_t id;
295     if (   parse_thing_type(tok0, tok1, t)
296         || parse_thing_command(tok0, tok1, t)
297         || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
298         || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
299         || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
300         || parse_position(tok0, tok1, t)
301         || parse_carry(tok0, tok1, t));
302     else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
303     {
304         t = get_thing(world.things, id, 1);
305         char * err = "No thing type found to initialize new thing.";
306         if (!t && !err_line(!world.thing_types, err))
307         {
308             t = add_thing(id, world.thing_types->id, 0, 0);
309         }
310     }
311     else
312     {
313         return 0;
314     }
315     return 1;
316 }
317
318
319
320 static uint8_t world_may_be_set_active()
321 {
322     if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
323     {
324         err_line(1, "No thing action of name 'wait' found.");
325         return 0;
326     }
327     if (!get_player())
328     {
329         err_line(1, "No un-owned player thing (of id=0) found.");
330         return 0;
331     }
332     if (!world.map.cells)
333     {
334         err_line(1, "No map found.");
335         return 0;
336     }
337     return 1;
338 }
339
340
341
342 static void remove_worldstate_file()
343 {
344     if (!access(s[S_PATH_WORLDSTATE], F_OK))
345     {
346         int test = unlink(s[S_PATH_WORLDSTATE]);
347         exit_trouble(-1 == test, __func__, "unlink");
348     }
349 }
350
351
352
353 static uint8_t parse_world_active(char * tok0, char * tok1)
354 {
355     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
356     {
357         if (!parsetest_int(tok1, '8'))
358         {
359             uint8_t argument = atoi(tok1);
360             if (!argument)
361             {
362                 remove_worldstate_file();
363                 world.exists = 0;
364             }
365             else if (world.exists)
366             {
367                 err_line(1, "World already active.");
368             }
369             else if (world_may_be_set_active())
370             {
371                 struct Thing * ti;
372                 for (ti = world.things; ti; ti = ti->next)
373                 {
374                     if (ti->lifepoints)
375                     {
376                         build_fov_map(ti);
377                     }
378                 }
379                 world.exists = 1;
380             }
381             return 1;
382         }
383     }
384     return 0;
385 }
386
387
388
389 static uint8_t set_map_length(char * tok0, char * tok1)
390 {
391     if (!strcmp(tok0, s[S_CMD_MAPLENGTH]) && !parsetest_int(tok1, 'u'))
392     {
393         uint16_t argument = atoi(tok1);
394         if (argument < 1 || argument > 256)
395         {
396             return err_line(1, "Value must be >= 1 and <= 256.");
397         }
398         world.exists = 0;
399         remove_worldstate_file();
400         free_things(world.things);
401         free(world.map.cells);
402         world.map.cells = NULL;    /* Since remake_map() runs free() on this. */
403         world.map.length = argument;
404         return 1;
405     }
406     return 0;
407 }
408
409
410
411 extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
412 {
413     if (   parse_thingtype_manipulation(tok0, tok1)
414         || parse_thingaction_manipulation(tok0, tok1)
415         || parse_thing_manipulation_1arg(tok0, tok1)
416         || set_map_length(tok0,tok1)
417         || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
418         || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
419         || parse_val(tok0,tok1,s[S_CMD_PLAYTYPE],'8',(char *)&world.player_type)
420         || parse_world_active(tok0, tok1));
421     else if (parse_val(tok0,tok1,s[S_CMD_SEED_MAP],'U',(char *)&world.seed_map))
422
423     {
424         remake_map();
425     }
426     else if (parse_val(tok0, tok1, s[S_CMD_MAKE_WORLD],'U',(char *)&world.seed))
427     {
428         uint8_t test = remake_world();
429         err_line(1 == test, "No player type with start number of >0 defined.");
430         err_line(2 == test, "No thing action with name 'wait' defined.");
431     }
432     else
433     {
434         return 0;
435     }
436     return 1;
437 }
438
439
440
441 extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
442 {
443     if (!t && !strcmp(tok0, s[S_CMD_T_MEMMAP]))
444     {
445         return err_line(1, "No thing defined to manipulate yet.");
446     }
447     if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
448     {
449         uint8_t y = atoi(tok1);
450         if (parsetest_int(tok1, '8') || y >= world.map.length)
451         {
452             return err_line(1, "Illegal value for map line number.");
453         }
454         if (strlen(tok2) != world.map.length)
455         {
456             return err_line(1, "Map line length is unequal map width.");
457         }
458         if (!t->mem_map)
459         {
460             uint32_t map_size = world.map.length * world.map.length;
461             t->mem_map = try_malloc(map_size, __func__);
462             memset(t->mem_map, ' ', map_size);
463         }
464         memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
465     }
466     else
467     {
468         return 0;
469     }
470     return 1;
471 }
472
473
474
475 extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
476                                       char * tok3)
477 {
478     if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
479     {
480         return err_line(1, "No thing defined to manipulate yet.");
481     }
482     if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
483     {
484         uint8_t id = atoi(tok1);
485         uint8_t y  = atoi(tok2);
486         uint8_t x  = atoi(tok3);
487         if (   parsetest_int(tok1, '8') || !get_thing_type(id)
488             || parsetest_int(tok2, '8') || y >= world.map.length
489             || parsetest_int(tok3, '8') || x >= world.map.length)
490         {
491             return err_line(1, "Illegal value for thing type or position.");
492         }
493         add_thing_to_memory_map(t, id, y, x);
494     }
495     else
496     {
497         return 0;
498     }
499     return 1;
500 }