home · contact · privacy
Server: Refactor yx_uint8 moving; handle actors hitting the map edge.
[plomrogue] / src / server / yx_uint8.c
1 /* src/server/yx_uint8.c */
2
3 #include "yx_uint8.h"
4 #include <stdint.h> /* uint8_t, int8_t */
5 #include <string.h> /* strchr() */
6 #include "../common/yx_uint8.h" /* yx_uint8 */
7
8
9
10 /* Move "yx" into hex direction "d". */
11 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx);
12
13
14
15 static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx)
16 {
17     if     (d == 'e')
18     {
19         yx->x = yx->x + (yx->y % 2);
20         yx->y--;
21     }
22     else if (d == 'd')
23     {
24         yx->x++;
25     }
26     else if (d == 'c')
27     {
28         yx->x = yx->x + (yx->y % 2);
29         yx->y++;
30     }
31     else if (d == 'x')
32     {
33         yx->x = yx->x - !(yx->y % 2);
34         yx->y++;
35     }
36     else if (d == 's')
37     {
38         yx->x--;
39     }
40     else if (d == 'w')
41     {
42         yx->x = yx->x - !(yx->y % 2);
43         yx->y--;
44     }
45 }
46
47
48
49 extern uint8_t yx_uint8_cmp(struct yx_uint8 * a, struct yx_uint8 * b)
50 {
51     if (a->y == b->y && a->x == b->x)
52     {
53         return 1;
54     }
55     return 0;
56 }
57
58
59
60 extern uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap)
61 {
62     static int8_t wrap_west_east   = 0;
63     static int8_t wrap_north_south = 0;
64     if (unwrap)
65     {
66         wrap_west_east = wrap_north_south = 0;
67         return 0;
68     }
69     struct yx_uint8 original;
70     original.y = yx->y;
71     original.x = yx->x;
72     mv_yx_in_hex_dir(d, yx);
73     if      (strchr("edc", d) && yx->x < original.x)
74     {
75         wrap_west_east++;
76     }
77     else if (strchr("xsw", d) && yx->x > original.x)
78     {
79         wrap_west_east--;
80     }
81     if      (strchr("we", d) && yx->y > original.y)
82     {
83         wrap_north_south--;
84     }
85     else if (strchr("xc", d) && yx->y < original.y)
86     {
87         wrap_north_south++;
88     }
89     return (wrap_west_east != 0) + (wrap_north_south != 0);
90 }