home · contact · privacy
Add basic food clock (but no consumables yet to re-set it).
[plomrogue] / src / server / god_commands.c
index efb607d9083adb3767c28fbd05bf684959682a76..d785d8cb0068663d29d606ef6f8cbdeb2d31a71f 100644 (file)
@@ -1,23 +1,30 @@
-/* src/server/god_commands.c */
+/* src/server/god_commands.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #include "god_commands.h"
 #include <stddef.h> /* NULL */
 #include <stdint.h> /* uint8_t */
 #include <stdlib.h> /* atoi(), free() */
-#include <string.h> /* strcmp() */
+#include <string.h> /* strcmp(), memset(), memcpy() */
 #include <unistd.h> /* F_OK, access(), unlink() */
 #include "../common/parse_file.h" /* err_line(), parse_val(), parsetest_int() */
 #include "../common/rexit.h" /* exit_trouble() */
+#include "../common/try_malloc.h" /* try_malloc() */
 #include "cleanup.h" /* unset_cleanup_flag() */
-#include "field_of_view.h" /* build_fov_map() */
+#include "field_of_view.h" /* build_fov_map(), update_map_memory() */
 #include "hardcoded_strings.h" /* s */
 #include "init.h" /* remake_world() */
-#include "map.h" /* remake_map() */
+#include "map.h" /* init_empty_map(), remake_map() */
 #include "thing_actions.h" /* ThingAction, actor_wait(), actor_move(),
                             * actor_use(), actor_pickup(), actor_drop()
                             */
 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
-                     * free_things()
+                     * free_things(), add_thing_to_memory_map(),get_thing_type(),
+                     * get_player()
                      */
 #include "world.h" /* world */
 
@@ -48,11 +55,14 @@ static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t);
 static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t);
 
 /* Parse/apply god command in "tok0"/"tok1" to manipulate a Thing. */
-static uint8_t parse_thing_manipulation(char * tok0, char * tok1);
+static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1);
 
 /* Performs parse_world_active()'s world activation legality tests. */
 static uint8_t world_may_be_set_active();
 
+/* Unlink worldstate file if it exists. */
+static void remove_worldstate_file();
+
 /* Parse/apply god command in "tok0"/"tok1" on toggling world.exists. Unset if
  * argument is 0 and unlink worldstate file, but only set it on positive
  * argument if it is not already set and a thing action of name S_CMD_WAIT, a
@@ -67,33 +77,43 @@ static uint8_t set_map_length(char * tok0, char * tok1);
 
 
 
+/* Thing, ThingType or ThingAction selected to be manipulated. */
+static struct Thing * t = NULL;
+static struct ThingType * tt = NULL;
+static struct ThingAction * ta = NULL;
+
+
+
 static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
 {
-    static struct ThingType * tt = NULL;
     if (!tt &&
         (   !strcmp(tok0, s[S_CMD_TT_CONSUM]) || !strcmp(tok0, s[S_CMD_TT_SYMB])
          || !strcmp(tok0, s[S_CMD_TT_STARTN]) || !strcmp(tok0, s[S_CMD_TT_NAME])
-         || !strcmp(tok0, s[S_CMD_TT_CORPS]) || !strcmp(tok0, s[S_CMD_TT_HP])))
+         || !strcmp(tok0, s[S_CMD_TT_CORPS])  || !strcmp(tok0, s[S_CMD_TT_HP])
+         || !strcmp(tok0, s[S_CMD_TT_PROL])))
     {
-        err_line(1, "No thing type defined to manipulate yet.");
-        return 1;
+        return err_line(1, "No thing type defined to manipulate yet.");
     }
-    uint8_t id;
+    int16_t id;
     if (   parse_val(tok0,tok1,s[S_CMD_TT_CONSUM],'8',(char *) &tt->consumable)
         || parse_val(tok0,tok1,s[S_CMD_TT_HP],'8',(char *) &tt->lifepoints)
         || parse_val(tok0,tok1,s[S_CMD_TT_STARTN],'8',(char *) &tt->start_n)
         || parse_val(tok0,tok1,s[S_CMD_TT_SYMB],'c',(char *) &tt->char_on_map)
-        || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name));
+        || parse_val(tok0,tok1,s[S_CMD_TT_PROL],'8',(char *) &tt->proliferate)
+        || parse_val(tok0,tok1,s[S_CMD_TT_STOMACH], 'u', (char *) &tt->stomach)
+        || parse_val(tok0,tok1,s[S_CMD_TT_NAME],'s',(char *) &tt->name))
+    {
+        ;
+    }
     else if (parse_val(tok0, tok1, s[S_CMD_TT_CORPS],'8',(char *)&id))
     {
         if (!get_thing_type(id))
         {
-            err_line(1, "Corpse ID belongs to no known thing type.");
-            return 1;
+            return err_line(1, "Corpse ID belongs to no known thing type.");
         }
         tt->corpse_id = id;
     }
-    else if (parse_val(tok0, tok1, s[S_CMD_THINGTYPE], '8', (char *) &id))
+    else if (parse_val(tok0, tok1, s[S_CMD_TT_ID], 'i', (char *) &id))
     {
         tt = get_thing_type(id);
         if (!tt)
@@ -125,16 +145,14 @@ static uint8_t try_func_name(struct ThingAction * ta, char * name,
 
 static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
 {
-    static struct ThingAction * ta = NULL;
     if (!ta &&
         (!strcmp(tok0, s[S_CMD_TA_EFFORT]) || !strcmp(tok0, s[S_CMD_TA_NAME])))
     {
-        err_line(1, "No thing action defined to manipulate yet.");
-        return 1;
+        return err_line(1, "No thing action defined to manipulate yet.");
     }
-    uint8_t id;
-    if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *) &ta->effort));
-    else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *) &ta->name))
+    int16_t id;
+    if      (parse_val(tok0, tok1, s[S_CMD_TA_EFFORT],'8',(char *)&ta->effort));
+    else if (parse_val(tok0, tok1, s[S_CMD_TA_NAME], 's', (char *)&ta->name))
     {
         if (!(   try_func_name(ta, s[S_CMD_MOVE], actor_move)
               || try_func_name(ta, s[S_CMD_PICKUP], actor_pick)
@@ -142,15 +160,18 @@ static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
               || try_func_name(ta, s[S_CMD_DROP], actor_drop)
               || try_func_name(ta, s[S_CMD_USE], actor_use)))
         {
-            err_line(1, "Invalid action function name.");
-            return 1;
-        }         /* Legal worlds have at least one thing action for waiting. */
+            return err_line(1, "Invalid action function name.");
+        }
         if (world.exists)
-        {
+        {         /* Legal worlds have at least one thing action for waiting. */
             world.exists = 0 != get_thing_action_id_by_name(s[S_CMD_WAIT]);
+            if (!world.exists)
+            {
+                remove_worldstate_file();
+            }
         }
     }
-    else if (parse_val(tok0, tok1, s[S_CMD_THINGACTION], '8', (char *) &id))
+    else if (parse_val(tok0, tok1, s[S_CMD_TA_ID], '8', (char *) &id))
     {
         ta = get_thing_action(id);
         if (!ta)
@@ -232,10 +253,13 @@ static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
             {
                 t->pos.x = length;
             }
-            free(t->fov_map);
             if (world.exists && t->lifepoints)
             {
-                t->fov_map = build_fov_map(t);
+                build_fov_map(t);
+                if (t == get_player())
+                {
+                    update_map_memory(t);
+                }
             }
         }
         return 1;
@@ -266,37 +290,33 @@ static uint8_t parse_carry(char * tok0, char * tok1, struct Thing * t)
 
 
 
-static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
+static uint8_t parse_thing_manipulation_1arg(char * tok0, char * tok1)
 {
-    static struct Thing * t = NULL;
     if (!t &&
         (   !strcmp(tok0, s[S_CMD_T_PROGRESS]) || !strcmp(tok0, s[S_CMD_T_TYPE])
          || !strcmp(tok0, s[S_CMD_T_CARRIES]) || !strcmp(tok0, s[S_CMD_T_POSY])
          || !strcmp(tok0, s[S_CMD_T_POSY]) || !strcmp(tok0, s[S_CMD_T_ARGUMENT])
-         || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])))
+         || !strcmp(tok0, s[S_CMD_T_HP]) || !strcmp(tok0, s[S_CMD_T_COMMAND])
+         || !strcmp(tok0, s[S_CMD_T_SATIATION])))
     {
-        err_line(1, "No thing defined to manipulate yet.");
-        return 1;
+        return err_line(1, "No thing defined to manipulate yet.");
     }
-    uint8_t id;
-    if (    parse_thing_type(tok0, tok1, t)
+    int16_t id;
+    if (   parse_thing_type(tok0, tok1, t)
         || parse_thing_command(tok0, tok1, t)
         || parse_val(tok0,tok1, s[S_CMD_T_ARGUMENT], '8', (char *)&t->arg)
         || parse_val(tok0,tok1, s[S_CMD_T_PROGRESS], '8', (char *)&t->progress)
         || parse_val(tok0,tok1, s[S_CMD_T_HP], '8', (char *) &t->lifepoints)
+        || parse_val(tok0,tok1, s[S_CMD_T_SATIATION], 'i',(char *)&t->satiation)
         || parse_position(tok0, tok1, t)
         || parse_carry(tok0, tok1, t));
-    else if (parse_val(tok0, tok1, s[S_CMD_THING], 'i', (char *) &id))
+    else if (parse_val(tok0, tok1, s[S_CMD_T_ID], 'i', (char *) &id))
     {
         t = get_thing(world.things, id, 1);
         char * err = "No thing type found to initialize new thing.";
-        if (!t && !err_line(NULL == world.thing_types, err))
+        if (!t && !err_line(!world.thing_types, err))
         {
             t = add_thing(id, world.thing_types->id, 0, 0);
-            if (world.exists && t->lifepoints)
-            {
-                t->fov_map = build_fov_map(t);
-            }
         }
     }
     else
@@ -330,9 +350,19 @@ static uint8_t world_may_be_set_active()
 
 
 
+static void remove_worldstate_file()
+{
+    if (!access(s[S_PATH_WORLDSTATE], F_OK))
+    {
+        int test = unlink(s[S_PATH_WORLDSTATE]);
+        exit_trouble(-1 == test, __func__, "unlink");
+    }
+}
+
+
+
 static uint8_t parse_world_active(char * tok0, char * tok1)
 {
-    char * f_name = "parse_world_active()";
     if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8'))
     {
         if (!parsetest_int(tok1, '8'))
@@ -340,11 +370,7 @@ static uint8_t parse_world_active(char * tok0, char * tok1)
             uint8_t argument = atoi(tok1);
             if (!argument)
             {
-                if (!access(s[S_PATH_WORLDSTATE], F_OK))
-                {
-                    int test = unlink(s[S_PATH_WORLDSTATE]);
-                    exit_trouble(-1 == test, f_name, "unlink()");
-                }
+                remove_worldstate_file();
                 world.exists = 0;
             }
             else if (world.exists)
@@ -358,11 +384,11 @@ static uint8_t parse_world_active(char * tok0, char * tok1)
                 {
                     if (ti->lifepoints)
                     {
-                        if (ti->fov_map)
+                        build_fov_map(ti);
+                        if (ti == get_player())
                         {
-                            free(ti->fov_map);
+                            update_map_memory(ti);
                         }
-                        ti->fov_map = build_fov_map(ti);
                     }
                 }
                 world.exists = 1;
@@ -382,10 +408,10 @@ static uint8_t set_map_length(char * tok0, char * tok1)
         uint16_t argument = atoi(tok1);
         if (argument < 1 || argument > 256)
         {
-            err_line(1, "Value must be >= 1 and <= 256.");
-            return 1;
+            return err_line(1, "Value must be >= 1 and <= 256.");
         }
         world.exists = 0;
+        remove_worldstate_file();
         free_things(world.things);
         free(world.map.cells);
         world.map.cells = NULL;    /* Since remake_map() runs free() on this. */
@@ -401,7 +427,7 @@ extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
 {
     if (   parse_thingtype_manipulation(tok0, tok1)
         || parse_thingaction_manipulation(tok0, tok1)
-        || parse_thing_manipulation(tok0, tok1)
+        || parse_thing_manipulation_1arg(tok0, tok1)
         || set_map_length(tok0,tok1)
         || parse_val(tok0,tok1,s[S_CMD_SEED_RAND],'U', (char *)&world.seed)
         || parse_val(tok0,tok1,s[S_CMD_TURN],'u',(char *)&world.turn)
@@ -424,3 +450,76 @@ extern uint8_t parse_god_command_1arg(char * tok0, char * tok1)
     }
     return 1;
 }
+
+
+
+extern uint8_t parse_god_command_2arg(char * tok0, char * tok1, char * tok2)
+{
+    if (!t && (   !strcmp(tok0, s[S_CMD_T_MEMMAP])
+               || !strcmp(tok0, s[S_CMD_T_MEMDEPTHMAP])))
+    {
+        return err_line(1, "No thing defined to manipulate yet.");
+    }
+    if (!strcmp(tok0,s[S_CMD_T_MEMMAP]) || !strcmp(tok0,s[S_CMD_T_MEMDEPTHMAP]))
+    {
+        uint8_t y = atoi(tok1);
+        if (parsetest_int(tok1, '8') || y >= world.map.length)
+        {
+            return err_line(1, "Illegal value for map line number.");
+        }
+        if (strlen(tok2) != world.map.length)
+        {
+            return err_line(1, "Map line length is unequal map width.");
+        }
+        if (!strcmp(tok0,s[S_CMD_T_MEMMAP]))
+        {
+            if (!t->mem_map)
+            {
+                init_empty_map(&(t->mem_map));
+            }
+            memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
+        }
+        else
+        {
+            if (!t->mem_depth_map)
+            {
+                init_empty_map(&(t->mem_depth_map));
+            }
+            memcpy(t->mem_depth_map+y*world.map.length, tok2, world.map.length);
+        }
+    }
+    else
+    {
+        return 0;
+    }
+    return 1;
+}
+
+
+
+extern uint8_t parse_god_command_3arg(char * tok0, char * tok1, char * tok2,
+                                      char * tok3)
+{
+    if (!t && !strcmp(tok0, s[S_CMD_T_MEMTHING]))
+    {
+        return err_line(1, "No thing defined to manipulate yet.");
+    }
+    if (!strcmp(tok0, s[S_CMD_T_MEMTHING]))
+    {
+        uint8_t id = atoi(tok1);
+        uint8_t y  = atoi(tok2);
+        uint8_t x  = atoi(tok3);
+        if (   parsetest_int(tok1, '8') || !get_thing_type(id)
+            || parsetest_int(tok2, '8') || y >= world.map.length
+            || parsetest_int(tok3, '8') || x >= world.map.length)
+        {
+            return err_line(1, "Illegal value for thing type or position.");
+        }
+        add_thing_to_memory_map(t, id, y, x);
+    }
+    else
+    {
+        return 0;
+    }
+    return 1;
+}