From cf0d81ea863df7adb1bdd862708571b0e40a73cb Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 22 Aug 2014 03:00:13 +0200
Subject: [PATCH] Server: Refactor, remove yx_uint8 module.

---
 src/server/field_of_view.c |  3 +-
 src/server/map.c           | 84 ++++++++++++++++++++++++++++++++++++--
 src/server/map.h           | 15 ++++++-
 src/server/thing_actions.c |  3 +-
 src/server/yx_uint8.c      | 79 -----------------------------------
 src/server/yx_uint8.h      | 26 ------------
 6 files changed, 95 insertions(+), 115 deletions(-)
 delete mode 100644 src/server/yx_uint8.c
 delete mode 100644 src/server/yx_uint8.h

diff --git a/src/server/field_of_view.c b/src/server/field_of_view.c
index 3d86402..e279bab 100644
--- a/src/server/field_of_view.c
+++ b/src/server/field_of_view.c
@@ -9,7 +9,6 @@
 #include "../common/yx_uint8.h" /* yx_uint8 */
 #include "map.h" /* mv_yx_in_dir_legal() */
 #include "things.h" /* Thing, ThingInMemory, add_thing_to_memory_map() */
-#include "yx_uint8.h" /* mv_yx_in_dir_wrap(), */
 #include "world.h" /* world  */
 
 
@@ -345,7 +344,7 @@ extern void build_fov_map(struct Thing * t)
             }
         }
     }
-    mv_yx_in_dir_wrap(0, NULL, 1);
+    mv_yx_in_dir_legal(0, NULL);
     free_angles(shadows);
     update_map_memory(t, map_size);
 }
diff --git a/src/server/map.c b/src/server/map.c
index ae907d2..4b49fa1 100644
--- a/src/server/map.c
+++ b/src/server/map.c
@@ -1,17 +1,25 @@
 /* src/server/map.c */
 
 #include "map.h"
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
+#include <stdint.h> /* uint8_t, int8_t, uint16_t, uint32_t, UINT16_MAX */
 #include <stdlib.h> /* free() */
+#include <string.h> /* strchr() */
 #include "../common/rexit.h" /* exit_err() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "../common/yx_uint8.h" /* yx_uint8 */
 #include "rrand.h" /* rrand() */
-#include "yx_uint8.h" /* mv_yx_in_dir_wrap() */
 #include "world.h" /* global world */
 
 
 
+/* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
+static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
+
+/* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d", do wrapping
+ * logic, return 1 if "yx" ends outside of the original wrap space, else 0.
+ */
+static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx);
+
 /* Call this too often with "init" of 0 and the game exits with an error message
  * about reaching an iteration limit. An "init" of 1 sets the iteration counter
  * to 0. Iteration limit is currently 256 * UINT16_MAX.
@@ -32,6 +40,74 @@ static void make_trees();
 
 
 
+static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
+{
+    if     (d == 'e')
+    {
+        yx->x = yx->x + (yx->y % 2);
+        yx->y--;
+    }
+    else if (d == 'd')
+    {
+        yx->x++;
+    }
+    else if (d == 'c')
+    {
+        yx->x = yx->x + (yx->y % 2);
+        yx->y++;
+    }
+    else if (d == 'x')
+    {
+        yx->x = yx->x - !(yx->y % 2);
+        yx->y++;
+    }
+    else if (d == 's')
+    {
+        yx->x--;
+    }
+    else if (d == 'w')
+    {
+        yx->x = yx->x - !(yx->y % 2);
+        yx->y--;
+    }
+}
+
+
+
+static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx)
+{
+    static int8_t wrap_west_east   = 0;
+    static int8_t wrap_north_south = 0;
+    if (!yx)
+    {
+        wrap_west_east = wrap_north_south = 0;
+        return 0;
+    }
+    struct yx_uint8 original;
+    original.y = yx->y;
+    original.x = yx->x;
+    mv_yx_in_hex_dir(d, yx);
+    if      (strchr("edc", d) && yx->x < original.x)
+    {
+        wrap_west_east++;
+    }
+    else if (strchr("xsw", d) && yx->x > original.x)
+    {
+        wrap_west_east--;
+    }
+    if      (strchr("we", d) && yx->y > original.y)
+    {
+        wrap_north_south--;
+    }
+    else if (strchr("xc", d) && yx->y < original.y)
+    {
+        wrap_north_south++;
+    }
+    return (wrap_west_east != 0) + (wrap_north_south != 0);
+}
+
+
+
 static uint8_t iter_limit(uint8_t init)
 {
     static uint32_t i = 0;
@@ -154,8 +230,8 @@ extern void remake_map()
 
 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
 {
-    uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
-    if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
+    uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx);
+    if (yx && !wraptest && yx->x < world.map.length && yx->y < world.map.length)
     {
         return 1;
     }
diff --git a/src/server/map.h b/src/server/map.h
index d704bc2..1ca12d6 100644
--- a/src/server/map.h
+++ b/src/server/map.h
@@ -21,10 +21,21 @@ struct yx_uint8;
  */
 extern void remake_map();
 
-/* Wrapper to mv_yx_in_dir_wrap(), returns 1 if the wrapped function moved "yx"
- * within the wrap borders and the map size, else 0.
+/* Move "yx" into hex direction "dir". Available hex directions are: 'e'
+ * (north-east), 'd' (east), 'c' (south-east), 'x' (south-west), 's' (west), 'w'
+ * (north-west). Returns 1 if the move was legal, else 0.
+ *
+ * A move is legal if "yx" ends up in the confines of the map and the original
+ * wrap space. The latter is left to a neighbor wrap space if "yx" moves beyond
+ * the minimal (0) or maximal (UINT8_MAX) column or row of possible map space –
+ * in which case "yx".y or "yx".x will snap to the respective opposite side. The
+ * current wrapping state is kept between successive calls until a "yx" of NULL
+ * is passed, in which case the function does nothing but zero the wrap state.
+ * Successive wrapping may move "yx" several wrap spaces into either direction,
+ * or return it into the original wrap space.
  */
 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx);
 
 
+
 #endif
diff --git a/src/server/thing_actions.c b/src/server/thing_actions.c
index df516e4..6f72544 100644
--- a/src/server/thing_actions.c
+++ b/src/server/thing_actions.c
@@ -16,7 +16,6 @@
                      * free_things_in_memory()
                      */
 #include "map.h" /* mv_yx_in_dir_legal() */
-#include "yx_uint8.h" /* mv_yx_in_dir_wrap() */
 #include "world.h" /* global world */
 
 
@@ -262,7 +261,7 @@ extern void actor_move(struct Thing * t)
     struct Thing * other_t;
     struct yx_uint8 target = t->pos;
     uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
-    mv_yx_in_dir_wrap(0, NULL, 1);
+    mv_yx_in_dir_legal(0, NULL);
     if (legal_move)
     {
         for (other_t = world.things; other_t != 0; other_t = other_t->next)
diff --git a/src/server/yx_uint8.c b/src/server/yx_uint8.c
deleted file mode 100644
index 18355a2..0000000
--- a/src/server/yx_uint8.c
+++ /dev/null
@@ -1,79 +0,0 @@
-/* src/server/yx_uint8.c */
-
-#include "yx_uint8.h"
-#include <stdint.h> /* uint8_t, int8_t */
-#include <string.h> /* strchr() */
-#include "../common/yx_uint8.h" /* yx_uint8 */
-
-
-
-/* Move "yx" into hex direction "d". */
-static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
-
-
-
-static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
-{
-    if     (d == 'e')
-    {
-        yx->x = yx->x + (yx->y % 2);
-        yx->y--;
-    }
-    else if (d == 'd')
-    {
-        yx->x++;
-    }
-    else if (d == 'c')
-    {
-        yx->x = yx->x + (yx->y % 2);
-        yx->y++;
-    }
-    else if (d == 'x')
-    {
-        yx->x = yx->x - !(yx->y % 2);
-        yx->y++;
-    }
-    else if (d == 's')
-    {
-        yx->x--;
-    }
-    else if (d == 'w')
-    {
-        yx->x = yx->x - !(yx->y % 2);
-        yx->y--;
-    }
-}
-
-
-
-extern uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
-{
-    static int8_t wrap_west_east   = 0;
-    static int8_t wrap_north_south = 0;
-    if (unwrap)
-    {
-        wrap_west_east = wrap_north_south = 0;
-        return 0;
-    }
-    struct yx_uint8 original;
-    original.y = yx->y;
-    original.x = yx->x;
-    mv_yx_in_hex_dir(d, yx);
-    if      (strchr("edc", d) && yx->x < original.x)
-    {
-        wrap_west_east++;
-    }
-    else if (strchr("xsw", d) && yx->x > original.x)
-    {
-        wrap_west_east--;
-    }
-    if      (strchr("we", d) && yx->y > original.y)
-    {
-        wrap_north_south--;
-    }
-    else if (strchr("xc", d) && yx->y < original.y)
-    {
-        wrap_north_south++;
-    }
-    return (wrap_west_east != 0) + (wrap_north_south != 0);
-}
diff --git a/src/server/yx_uint8.h b/src/server/yx_uint8.h
deleted file mode 100644
index 54246fd..0000000
--- a/src/server/yx_uint8.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/* src/server/yx_uint8.h
- *
- * Routines for comparison and movement with yx_uint8 structs.
- */
-
-#ifndef YX_UINT8_H_SERVER
-#define YX_UINT8_H_SERVER
-
-#include <stdint.h> /* uint8_t */
-struct yx_uint8;
-
-
-
-/* Move "yx" into hex direction "d". If this moves "yx" beyond the minimal (0)
- * or maximal (UINT8_MAX) column or row, it wraps to the opposite side. Such
- * wrapping is returned as a wraps enum value and stored, so that further calls
- * to move "yx" back into the opposite direction may unwrap it again. Pass an
- * "unwrap" of !0 to re-set the internal wrap memory to 0.
- * Hex direction values for "d": 'e' (north-east), 'd' (east), 'c' (south-east),
- * 'x' (south-west), 's' (west), 'w' (north-west)
- */
-extern uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap);
-
-
-
-#endif
-- 
2.30.2