diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-21 10:33:46 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-21 10:33:46 +0200 |
| commit | 1d9b8acfeece68c4f55d2499d8aed127f98a383c (patch) | |
| tree | d520e8726a0c37af5738779847c2da942a05cf64 /src/core/moves.h | |
| parent | f1575ed335166a0897f2cfd314a0f4bade6a10fc (diff) | |
| download | nissy-core-1d9b8acfeece68c4f55d2499d8aed127f98a383c.tar.gz nissy-core-1d9b8acfeece68c4f55d2499d8aed127f98a383c.zip | |
Merged some files, renamed another
Diffstat (limited to 'src/core/moves.h')
| -rw-r--r-- | src/core/moves.h | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/core/moves.h b/src/core/moves.h index cc91706..8f91167 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -1,6 +1,12 @@ | |||
| 1 | #define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) | 1 | #define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) |
| 2 | #define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) | 2 | #define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) |
| 3 | 3 | ||
| 4 | STATIC uint8_t readmove(char); | ||
| 5 | STATIC int64_t readmoves(const char *, size_t n, uint8_t [n]); | ||
| 6 | STATIC int64_t countmoves(const char *); | ||
| 7 | STATIC uint8_t readmodifier(char); | ||
| 8 | STATIC int64_t writemoves(size_t n, const uint8_t [n], size_t m, char [m]); | ||
| 9 | |||
| 4 | STATIC_INLINE bool allowednextmove(uint8_t, uint8_t); | 10 | STATIC_INLINE bool allowednextmove(uint8_t, uint8_t); |
| 5 | STATIC bool allowedmoves(size_t n, const uint8_t [n]); | 11 | STATIC bool allowedmoves(size_t n, const uint8_t [n]); |
| 6 | 12 | ||
| @@ -17,6 +23,128 @@ STATIC bool are_lastmoves_singlecw(size_t n, const uint8_t [n]); | |||
| 17 | 23 | ||
| 18 | STATIC cube_t applymoves(cube_t, const char *); | 24 | STATIC cube_t applymoves(cube_t, const char *); |
| 19 | 25 | ||
| 26 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | ||
| 27 | RET_ERROR, ARG_ACTION) \ | ||
| 28 | const char *VAR_B; \ | ||
| 29 | uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ | ||
| 30 | for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ | ||
| 31 | while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ | ||
| 32 | VAR_B++; \ | ||
| 33 | if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ | ||
| 34 | break; \ | ||
| 35 | if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) { \ | ||
| 36 | LOG("Unknown move: %c\n", *VAR_B); \ | ||
| 37 | return RET_ERROR; \ | ||
| 38 | } \ | ||
| 39 | if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ | ||
| 40 | VAR_B++; \ | ||
| 41 | ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ | ||
| 42 | ARG_ACTION \ | ||
| 43 | } | ||
| 44 | |||
| 45 | STATIC uint8_t | ||
| 46 | readmove(char c) | ||
| 47 | { | ||
| 48 | switch (c) { | ||
| 49 | case 'U': | ||
| 50 | return MOVE_U; | ||
| 51 | case 'D': | ||
| 52 | return MOVE_D; | ||
| 53 | case 'R': | ||
| 54 | return MOVE_R; | ||
| 55 | case 'L': | ||
| 56 | return MOVE_L; | ||
| 57 | case 'F': | ||
| 58 | return MOVE_F; | ||
| 59 | case 'B': | ||
| 60 | return MOVE_B; | ||
| 61 | default: | ||
| 62 | return UINT8_ERROR; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | STATIC uint8_t | ||
| 67 | readmodifier(char c) | ||
| 68 | { | ||
| 69 | switch (c) { | ||
| 70 | case '1': /* Fallthrough */ | ||
| 71 | case '2': /* Fallthrough */ | ||
| 72 | case '3': | ||
| 73 | return c - '0' - 1; | ||
| 74 | case '\'': | ||
| 75 | return 2; | ||
| 76 | default: | ||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | STATIC int64_t | ||
| 82 | readmoves(const char *buf, size_t n, uint8_t ret[n]) | ||
| 83 | { | ||
| 84 | uint8_t m; | ||
| 85 | uint64_t c; | ||
| 86 | |||
| 87 | FOREACH_READMOVE(buf, m, c, n, NISSY_ERROR_INVALID_MOVES, | ||
| 88 | ret[c] = m; | ||
| 89 | ) | ||
| 90 | |||
| 91 | return (int64_t)c; | ||
| 92 | } | ||
| 93 | |||
| 94 | STATIC int64_t | ||
| 95 | countmoves(const char *buf) | ||
| 96 | { | ||
| 97 | uint8_t m; | ||
| 98 | uint64_t c; | ||
| 99 | |||
| 100 | FOREACH_READMOVE(buf, m, c, INT_MAX, NISSY_ERROR_INVALID_MOVES, | ||
| 101 | {} | ||
| 102 | ) | ||
| 103 | |||
| 104 | (void)m; /* Ignore "variable set but not used" warning */ | ||
| 105 | |||
| 106 | return (int64_t)c; | ||
| 107 | } | ||
| 108 | |||
| 109 | STATIC int64_t | ||
| 110 | writemoves( | ||
| 111 | size_t nmoves, | ||
| 112 | const uint8_t m[nmoves], | ||
| 113 | size_t buf_size, | ||
| 114 | char buf[buf_size] | ||
| 115 | ) | ||
| 116 | { | ||
| 117 | size_t i, len, w; | ||
| 118 | const char *s; | ||
| 119 | |||
| 120 | if (buf_size == 0) { | ||
| 121 | LOG("Error: cannot write moves to buffer of size 0.\n"); | ||
| 122 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 123 | } | ||
| 124 | |||
| 125 | for (i = 0, w = 0; i < nmoves; i++, w++) { | ||
| 126 | s = movestr[m[i]]; | ||
| 127 | len = strlen(s); | ||
| 128 | if (len + w >= buf_size) { | ||
| 129 | LOG("Error: the given buffer is too small for " | ||
| 130 | "writing the given moves.\n"); | ||
| 131 | goto writemoves_error; | ||
| 132 | } | ||
| 133 | memcpy(buf+w, s, len); | ||
| 134 | w += len; | ||
| 135 | buf[w] = ' '; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (w > 0) w--; /* Remove last space */ | ||
| 139 | buf[w] = '\0'; | ||
| 140 | |||
| 141 | return (int64_t)w; | ||
| 142 | |||
| 143 | writemoves_error: | ||
| 144 | *buf = '\0'; | ||
| 145 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 146 | } | ||
| 147 | |||
| 20 | STATIC_INLINE bool | 148 | STATIC_INLINE bool |
| 21 | allowednextmove(uint8_t m1, uint8_t m2) | 149 | allowednextmove(uint8_t m1, uint8_t m2) |
| 22 | { | 150 | { |
