From ac7521c1d40b86cd7d33cef590353692542fa0a4 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 7 Dec 2014 06:59:38 +0100
Subject: [PATCH] Server: Decouple update_map_memory() and build_fov_map(),
 thus fix bugs.

---
 SERVER_COMMANDS            |  4 ++--
 src/server/field_of_view.c | 14 ++++----------
 src/server/field_of_view.h |  9 +++++++--
 src/server/god_commands.c  | 13 +++++++++++--
 src/server/init.c          | 15 ++++-----------
 src/server/run.c           |  2 ++
 6 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/SERVER_COMMANDS b/SERVER_COMMANDS
index c43efed..cca0671 100644
--- a/SERVER_COMMANDS
+++ b/SERVER_COMMANDS
@@ -114,8 +114,8 @@ WORLD_ACTIVE [0 to 255]
 Set world activity state to argument. If 0, remove ./server/worldstate file.
 Else, if world was so far inactive, a map exists, at least one "wait" thing
 action is defined, and a thing of ID 0 (= a player character) that is not part
-of any other thing's inventory, (re-)build all animate things' fields of view and
-activate world. (Initial value: 0.)
+of any other thing's inventory, (re-)build all animate things' fields of view,
+and activate world. (Initial value: 0.)
 
 TA_ID [0 to 255]
 Select thing action to manipulate by argument as ID. If argument is 0, change it
diff --git a/src/server/field_of_view.c b/src/server/field_of_view.c
index 34bfa13..7ab5678 100644
--- a/src/server/field_of_view.c
+++ b/src/server/field_of_view.c
@@ -75,11 +75,6 @@ static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
                           struct yx_uint8 * test_pos,
                           struct shadow_angle ** shadows);
 
-/* Update "t"'s .mem_map memory with what's in its current FOV, remove from its
- * .t_mem all memorized things in FOV and add inanimiate things in FOV to it.
- */
-static void update_map_memory(struct Thing * t, uint32_t map_size);
-
 
 
 static uint32_t correct_angle(int32_t angle)
@@ -267,15 +262,15 @@ static void eval_position(uint16_t dist, uint16_t hex_i, char * fov_map,
 
 
 
-static void update_map_memory(struct Thing * t_eye, uint32_t map_size)
+extern void update_map_memory(struct Thing * t_eye)
 {
     if (!t_eye->mem_map)
     {
-        t_eye->mem_map = try_malloc(map_size, __func__);
-        memset(t_eye->mem_map, ' ', map_size);
+        t_eye->mem_map = try_malloc(world.map.length*world.map.length,__func__);
+        memset(t_eye->mem_map, ' ', world.map.length * world.map.length);
     }
     uint32_t i;
-    for (i = 0; i < map_size; i++)
+    for (i = 0; i < (uint32_t) (world.map.length * world.map.length); i++)
     {
         if (' ' == t_eye->mem_map[i] && t_eye->fov_map[i] == 'v')
         {
@@ -352,5 +347,4 @@ extern void build_fov_map(struct Thing * t)
     }
     mv_yx_in_dir_legal(0, NULL);
     free_angles(shadows);
-    update_map_memory(t, map_size);
 }
diff --git a/src/server/field_of_view.h b/src/server/field_of_view.h
index 6e4edb9..5cc9edb 100644
--- a/src/server/field_of_view.h
+++ b/src/server/field_of_view.h
@@ -10,12 +10,17 @@
 #ifndef FIELD_OF_VIEW_H
 #define FIELD_OF_VIEW_H
 
-#include <stdint.h> /* uint8_t */
+#include <stdint.h> /* uint8_t, uint32_t */
 struct Thing;
 
 
 
-/* Build "t"'s field of view and update its map memory with the result. */
+/* Update "t"'s .mem_map memory with what's in its current FOV, remove from its
+ * .t_mem all memorized things in FOV and add inanimiate things in FOV to it.
+ */
+extern void update_map_memory(struct Thing * t_eye);
+
+/* Build "t"'s field of view. */
 extern void build_fov_map(struct Thing * t);
 
 
diff --git a/src/server/god_commands.c b/src/server/god_commands.c
index 06abcfe..a190682 100644
--- a/src/server/god_commands.c
+++ b/src/server/god_commands.c
@@ -15,7 +15,7 @@
 #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() */
@@ -23,7 +23,8 @@
                             * actor_use(), actor_pickup(), actor_drop()
                             */
 #include "things.h" /* Thing, ThingType, add_thing(), get_thing(), own_thing(),
-                     * free_things(), add_thing_to_memory_map(),get_thing_type()
+                     * free_things(), add_thing_to_memory_map(),get_thing_type(),
+                     * get_player()
                      */
 #include "world.h" /* world */
 
@@ -251,6 +252,10 @@ static uint8_t parse_position(char* tok0, char * tok1, struct Thing * t)
             if (world.exists && t->lifepoints)
             {
                 build_fov_map(t);
+                if (t == get_player())
+                {
+                    update_map_memory(t);
+                }
             }
         }
         return 1;
@@ -374,6 +379,10 @@ static uint8_t parse_world_active(char * tok0, char * tok1)
                     if (ti->lifepoints)
                     {
                         build_fov_map(ti);
+                        if (ti == get_player())
+                        {
+                            update_map_memory(ti);
+                        }
                     }
                 }
                 world.exists = 1;
diff --git a/src/server/init.c b/src/server/init.c
index 093f1f5..43a4d68 100644
--- a/src/server/init.c
+++ b/src/server/init.c
@@ -25,11 +25,11 @@
 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "cleanup.h" /* set_cleanup_flag() */
-#include "field_of_view.h" /* build_fov_map() */
+#include "field_of_view.h" /* update_map_memory() */
 #include "hardcoded_strings.h" /* s */
 #include "map.h" /* remake_map() */
 #include "things.h" /* Thing, ThingType, free_things(), add_things(),
-                     * get_thing_id_action_id_by_name()
+                     * get_thing_id_action_id_by_name(), get_player()
                      */
 #include "run.h" /* obey_msg(), io_loop(), record(), send_to_outfile() */
 #include "world.h" /* global world */
@@ -199,6 +199,7 @@ extern uint8_t remake_world()
     world.seed_map = world.seed;
     free_things(world.things);
     remake_map();
+    world.exists = 1;
     struct ThingType * tt;
     for (tt = world.thing_types; tt; tt = tt->next)
     {
@@ -208,6 +209,7 @@ extern uint8_t remake_world()
             break;
         }
     }
+    update_map_memory(get_player());
     for (tt = world.thing_types; tt; tt = tt->next)
     {
         if (world.player_type != tt->id)
@@ -215,16 +217,7 @@ extern uint8_t remake_world()
             add_things(tt->id, tt->start_n);
         }
     }
-    struct Thing * t;
-    for (t = world.things; t; t = t->next)
-    {
-        if (t->lifepoints)
-        {
-            build_fov_map(t);
-        }
-    }
     world.turn = 1;
-    world.exists = 1;
     send_to_outfile("NEW_WORLD\n", 1);
     return 0;
 }
diff --git a/src/server/run.c b/src/server/run.c
index 6f24f46..38dc5a0 100644
--- a/src/server/run.c
+++ b/src/server/run.c
@@ -26,6 +26,7 @@
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "ai.h" /* ai() */
 #include "cleanup.h" /* unset_cleanup_flag() */
+#include "field_of_view.h" /* update_map_memory() */
 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
 #include "hardcoded_strings.h" /* s */
 #include "io.h" /* io_round(), save_world() */
@@ -325,6 +326,7 @@ static void turn_over()
             {
                 if (0 == thing->command)
                 {
+                    update_map_memory(thing);
                     if (thing == player)
                     {
                         break;
-- 
2.30.2