#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 */
}
}
}
- mv_yx_in_dir_wrap(0, NULL, 1);
+ mv_yx_in_dir_legal(0, NULL);
free_angles(shadows);
update_map_memory(t, map_size);
}
/* 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.
+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;
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;
}
*/
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
* 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 */
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)
+++ /dev/null
-/* 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);
-}
+++ /dev/null
-/* 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