home · contact · privacy
Server: New command TT_PROLIFERATE sets chance for thing proliferation.
[plomrogue] / src / server / io.c
index 70a010780c9a97ba866c3c88f8f296134e0fc7b0..0f7c3a8bd60367e5a338db65b6e2648227309b7e 100644 (file)
@@ -1,6 +1,11 @@
-/* src/server/io.c */
+/* src/server/io.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.
+ */
 
-#define _POSIX_C_SOURCE 199309L
+#define _POSIX_C_SOURCE 200112L /* snrpintf() */
 #include "io.h"
 #include <errno.h> /* global errno */
 #include <limits.h> /* PIPE_BUF */
 #include "../common/rexit.h" /* exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "cleanup.h" /* set_cleanup_flag() */
-#include "field_of_view.h" /* VISIBLE */
 #include "hardcoded_strings.h" /* s */
-#include "things.h" /* Thing, ThingType, ThingAction, get_thing_type(),
-                     * get_player()
+#include "things.h" /* Thing, ThingType, ThingInMemory, ThingAction,
+                     * get_thing_type(), get_player()
                      */
 #include "world.h" /* global world  */
 
@@ -60,13 +64,15 @@ static void write_value_as_line(uint32_t value, FILE * file);
 static void write_inventory(struct Thing * player, FILE * file);
 
 /* Return map cells sequence as visible to the "player", with invisible cells as
- * whitespace. Super-impose over visible map cells things positioned there.
+ * whitespace. Super-impose over visible map cells things positioned there,
+ * with animate things overwriting inanimate things, and inanimate consumable
+ * things overwriting inanimate non-consumable things.
  */
 static char * build_visible_map(struct Thing * player);
 
 /* Write to "file" game map as visible to "player" right now, as drawn by
  * build_visible_map(), and thereafter game map as memorized by player in its
- * .mem_map. Write one row per \n-delimited line.
+ * .mem_map and .t_mem. Write one row per \n-delimited line.
  */
 static void write_map(struct Thing * player, FILE * file);
 
@@ -164,6 +170,17 @@ static void write_thing(FILE * file, struct Thing * t)
             try_fputc('\n', file, __func__);
         }
         free(mem_map_copy);
+        struct ThingInMemory * tm = t->t_mem;
+        for (; tm; tm = tm->next)
+        {
+            write_key_space(file, s[S_CMD_T_MEMTHING]);
+            write_value(file, tm->type);
+            try_fputc(' ', file, __func__);
+            write_value(file, tm->pos.y);
+            try_fputc(' ', file, __func__);
+            write_value(file, tm->pos.x);
+            try_fputc('\n', file, __func__);
+        }
     }
     try_fputc('\n', file, __func__);
 }
@@ -277,7 +294,7 @@ static void write_value_as_line(uint32_t value, FILE * file)
 static void write_inventory(struct Thing * player, FILE * file)
 {
     struct Thing * owned = player->owns;
-    if (NULL == owned)
+    if (!owned)
     {
         char * empty = "(none)\n";
         try_fwrite(empty, strlen(empty), 1, file, __func__);
@@ -285,7 +302,7 @@ static void write_inventory(struct Thing * player, FILE * file)
     else
     {
         uint8_t q;
-        for (q = 0; NULL != owned; q++)
+        for (q = 0; owned; q++)
         {
             struct ThingType * tt = get_thing_type(owned->type);
             try_fwrite(tt->name, strlen(tt->name), 1, file, __func__);
@@ -309,27 +326,28 @@ static char * build_visible_map(struct Thing * player)
         uint32_t pos_i;
         for (pos_i = 0; pos_i < map_size; pos_i++)
         {
-            if (player->fov_map[pos_i] & VISIBLE)
+            if (player->fov_map[pos_i] == 'v')
             {
                 visible_map[pos_i] = world.map.cells[pos_i];
             }
         }
         struct Thing * t;
-        struct ThingType * tt;
         char c;
         uint8_t i;
-        for (i = 0; i < 2; i++)
+        for (i = 0; i < 3; i++)
         {
             for (t = world.things; t != 0; t = t->next)
             {
-                if (   (  player->fov_map[t->pos.y * world.map.length +t->pos.x]
-                        & VISIBLE)
-                    && (   (0 == i && 0 == t->lifepoints)
-                        || (1 == i && 0 < t->lifepoints)))
+                if ('v' == player->fov_map[t->pos.y*world.map.length+t->pos.x])
                 {
-                    tt = get_thing_type(t->type);
-                    c = tt->char_on_map;
-                    visible_map[t->pos.y * world.map.length + t->pos.x] = c;
+                    struct ThingType * tt = get_thing_type(t->type);
+                    if (   (0 == i && !t->lifepoints && !tt->consumable)
+                        || (1 == i && !t->lifepoints &&  tt->consumable)
+                        || (2 == i &&  t->lifepoints))
+                    {
+                        c = tt->char_on_map;
+                        visible_map[t->pos.y * world.map.length + t->pos.x] = c;
+                    }
                 }
             }
         }
@@ -352,14 +370,36 @@ static void write_map(struct Thing * player, FILE * file)
         try_fputc('\n', file, __func__);
     }
     free(visible_map);
+    uint32_t map_size = world.map.length * world.map.length;
+    char * mem_map = try_malloc(map_size, __func__);
+    memcpy(mem_map, player->mem_map, map_size);
+    uint8_t i;
+    struct ThingInMemory * tm;
+    for (i = 0; i < 2; i++)
+    {
+        for (tm = player->t_mem; tm; tm = tm->next)
+        {
+            if (' ' != player->mem_map[tm->pos.y*world.map.length+tm->pos.x])
+            {
+                struct ThingType * tt = get_thing_type(tm->type);
+                if (   (0 == i && !tt->consumable)
+                    || (1 == i &&  tt->consumable))
+                {
+                    char c = tt->char_on_map;
+                    mem_map[tm->pos.y * world.map.length + tm->pos.x] = c;
+                }
+            }
+        }
+    }
     for (y = 0; y < world.map.length; y++)
     {
         for (x = 0; x < world.map.length; x++)
         {
-            try_fputc(player->mem_map[y * world.map.length + x], file, __func__);
+            try_fputc(mem_map[y * world.map.length + x], file, __func__);
         }
         try_fputc('\n', file, __func__);
     }
+    free(mem_map);
 }
 
 
@@ -415,6 +455,7 @@ extern void save_world()
         exit_trouble(test < 0, __func__, "fprintf");
         write_key_space_string(file, s[S_CMD_TT_NAME], tt->name);
         write_key_space_value(file, s[S_CMD_TT_CONSUM], tt->consumable);
+        write_key_space_value(file, s[S_CMD_TT_PROL], tt->proliferate);
         try_fputc('\n', file, __func__);
     }
     for (tt = world.thing_types; tt; tt = tt->next)