home · contact · privacy
Add auto-mapping / map memory.
[plomrogue] / src / server / god_commands.c
index eee8b1105e22a5c5cfe22c9bdb6b1f6a867eaacc..755acd5881b4ff8bf4ba604c08d89c0f8d83e986 100644 (file)
@@ -4,10 +4,11 @@
 #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 "hardcoded_strings.h" /* s */
@@ -48,7 +49,7 @@ 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();
@@ -70,9 +71,15 @@ 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])
@@ -81,7 +88,7 @@ static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
         err_line(1, "No thing type defined to manipulate yet.");
         return 1;
     }
-    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)
@@ -96,7 +103,7 @@ static uint8_t parse_thingtype_manipulation(char * tok0, char * tok1)
         }
         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)
@@ -128,14 +135,13 @@ 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;
     }
-    uint8_t id;
+    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))
     {
@@ -157,7 +163,7 @@ static uint8_t parse_thingaction_manipulation(char * tok0, char * tok1)
             }
         }
     }
-    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)
@@ -239,10 +245,9 @@ 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);
             }
         }
         return 1;
@@ -273,9 +278,8 @@ 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])
@@ -285,15 +289,15 @@ static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
         err_line(1, "No thing defined to manipulate yet.");
         return 1;
     }
-    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_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.";
@@ -302,7 +306,7 @@ static uint8_t parse_thing_manipulation(char * tok0, char * tok1)
             t = add_thing(id, world.thing_types->id, 0, 0);
             if (world.exists && t->lifepoints)
             {
-                t->fov_map = build_fov_map(t);
+                build_fov_map(t);
             }
         }
     }
@@ -371,11 +375,7 @@ static uint8_t parse_world_active(char * tok0, char * tok1)
                 {
                     if (ti->lifepoints)
                     {
-                        if (ti->fov_map)
-                        {
-                            free(ti->fov_map);
-                        }
-                        ti->fov_map = build_fov_map(ti);
+                        build_fov_map(ti);
                     }
                 }
                 world.exists = 1;
@@ -415,7 +415,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)
@@ -438,3 +438,40 @@ 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]))
+    {
+        err_line(1, "No thing defined to manipulate yet.");
+        return 1;
+    }
+    if (!strcmp(tok0, s[S_CMD_T_MEMMAP]))
+    {
+        uint8_t y = atoi(tok1);
+        if (parsetest_int(tok1, '8') || y >= world.map.length)
+        {
+            err_line(1, "Illegal value for map line number.");
+            return 1;
+        }
+        if (strlen(tok2) != world.map.length)
+        {
+            err_line(1, "Map line length is unequal map width.");
+            return 1;
+        }
+        if (!t->mem_map)
+        {
+            uint32_t map_size = world.map.length * world.map.length;
+            t->mem_map = try_malloc(map_size, __func__);
+            memset(t->mem_map, ' ', map_size);
+        }
+        memcpy(t->mem_map + y * world.map.length, tok2, world.map.length);
+    }
+    else
+    {
+        return 0;
+    }
+    return 1;
+}