-#include <stdint.h> /* uint16_t, uint32_t */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT8_MIN, INT8_MAX */
+
+
+
+/* Coordinate for maps of max. 256x256 cells. */
+struct yx_uint8
+{
+ uint8_t y;
+ uint8_t x;
+};
+
+/* Storage for map_length, set by set_maplength(). */
+static uint16_t maplength = 0;
+extern void set_maplength(uint16_t maplength_input)
+{
+ maplength = maplength_input;
+}
+/* Helper to mv_yx_in_dir_legal(). Move "yx" into hex direction "d". */
+static void mv_yx_in_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--;
+ }
+}
+
+
+
+/* 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, 0 if not, and -1 when internal
+ * wrapping limits were exceeded.
+ *
+ * A move is legal if "yx" ends up within the 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.
+ */
+static int8_t mv_yx_in_dir_legal(char dir, 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;
+ }
+ if ( INT8_MIN == wrap_west_east || INT8_MIN == wrap_north_south
+ || INT8_MAX == wrap_west_east || INT8_MAX == wrap_north_south)
+ {
+ return -1;
+ }
+ struct yx_uint8 original = *yx;
+ mv_yx_in_dir(dir, yx);
+ if (('e' == dir || 'd' == dir || 'c' == dir) && yx->x < original.x)
+ {
+ wrap_west_east++;
+ }
+ else if (('x' == dir || 's' == dir || 'w' == dir) && yx->x > original.x)
+ {
+ wrap_west_east--;
+ }
+ if (('w' == dir || 'e' == dir) && yx->y > original.y)
+ {
+ wrap_north_south--;
+ }
+ else if (('x' == dir || 'c' == dir) && yx->y < original.y)
+ {
+ wrap_north_south++;
+ }
+ if ( !wrap_west_east && !wrap_north_south
+ && yx->x < maplength && yx->y < maplength)
+ {
+ return 1;
+ }
+ return 0;
+}
+
+
+
+/* Wrapper around mv_yx_in_dir_legal() that stores the new coordinate in the
+ * globals res_y, res_x.
+ */
+uint8_t res_y = 0;
+uint8_t res_x = 0;
+extern uint8_t mv_yx_in_dir_legal_wrap(char dir, uint8_t y, uint8_t x)
+{
+ struct yx_uint8 yx;
+ yx.y = y;
+ yx.x = x;
+ uint8_t result = mv_yx_in_dir_legal(dir, &yx);
+ res_y = yx.y;
+ res_x = yx.x;
+ return result;
+}
+
+
+
/* With set_seed set, set seed global to seed_input. In any case, return it. */
extern uint32_t seed_rrand(uint8_t set_seed, uint32_t seed_input)
{
libpr.seed_rrand.restype = ctypes.c_uint32
libpr.rrand.argtypes = []
libpr.rrand.restype = ctypes.c_uint16
+ libpr.set_maplength.argtypes = [ctypes.c_uint16]
+ libpr.mv_yx_in_dir_legal_wrap.argtypes = [ctypes.c_char, ctypes.c_uint8,
+ ctypes.c_uint8]
+ libpr.mv_yx_in_dir_legal_wrap.restype = ctypes.c_uint8
return libpr
if None != val:
world_db["Things"][0]["T_ARGUMENT"] = val
set_command()
- else:
- print("Ignoring: Argument must be integer >= 0 <=255.")
def set_command_and_argument_movestring(str_arg):
dirs = {"east": "d", "south-east": "c", "south-west": "x",
val = integer_test(seed_string, 0, 4294967295)
if None != val:
rand.seed = val
- else:
- print("Ignoring: Value must be integer >= 0, <= 4294967295.")
def command_seedmap(seed_string):
val = integer_test(seed_string, 0, 4294967295)
if None == val:
- print("Ignoring: Value must be integer >= 0, <= 4294967295.")
return
rand.seed = val
world_db["SEED_MAP"] = val
def command_maplength(maplength_string):
"""Redefine map length. Invalidate map, therefore lose all things on it."""
- set_world_inactive()
- world_db["Things"] = {}
- setter(None, "MAP_LENGTH", 1, 256)(maplength_string)
+ val = integer_test(val_string, 1, 256)
+ if None != val:
+ world_db["MAP_LENGTH"] = val
+ set_world_inactive()
+ world_db["Things"] = {}
+ libpr.set_maplength = val
def command_worldactive(worldactive_string):