diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-10-05 09:31:02 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-10-05 09:42:39 +0200 |
| commit | 9e698917cb5b332cdbf1969ddfe3840239106c8d (patch) | |
| tree | 1125917c5d281aff8acfd94292b8a3d3e370bc2d /src | |
| parent | 99fabe8592a4022313b1cb0581881ee1bc6b1c99 (diff) | |
| download | nissy-core-9e698917cb5b332cdbf1969ddfe3840239106c8d.tar.gz nissy-core-9e698917cb5b332cdbf1969ddfe3840239106c8d.zip | |
Implemented compose; some cleanup
Diffstat (limited to 'src')
| -rw-r--r-- | src/constants.h | 71 | ||||
| -rw-r--r-- | src/cube.h | 51 | ||||
| -rw-r--r-- | src/cube_array.c | 27 | ||||
| -rw-r--r-- | src/cube_array.h | 6 |
4 files changed, 110 insertions, 45 deletions
diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000..6a7e3f9 --- /dev/null +++ b/src/constants.h | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | /* Standard moves */ | ||
| 2 | #define U 0U | ||
| 3 | #define U2 1U | ||
| 4 | #define U3 2U | ||
| 5 | #define D 3U | ||
| 6 | #define D2 4U | ||
| 7 | #define D3 5U | ||
| 8 | #define R 6U | ||
| 9 | #define R2 7U | ||
| 10 | #define R3 8U | ||
| 11 | #define L 9U | ||
| 12 | #define L2 10U | ||
| 13 | #define L3 11U | ||
| 14 | #define F 12U | ||
| 15 | #define F2 13U | ||
| 16 | #define F3 14U | ||
| 17 | #define B 15U | ||
| 18 | #define B2 16U | ||
| 19 | #define B3 17U | ||
| 20 | |||
| 21 | /* Regular transformations (rotations) */ | ||
| 22 | #define UF_i 0 | ||
| 23 | #define UL_i 1 | ||
| 24 | #define UB_i 2 | ||
| 25 | #define UR_i 3 | ||
| 26 | #define DF_i 4 | ||
| 27 | #define DL_i 5 | ||
| 28 | #define DB_i 6 | ||
| 29 | #define DR_i 7 | ||
| 30 | #define RU_i 8 | ||
| 31 | #define RF_i 9 | ||
| 32 | #define RD_i 10 | ||
| 33 | #define RB_i 11 | ||
| 34 | #define LU_i 12 | ||
| 35 | #define LF_i 13 | ||
| 36 | #define LD_i 14 | ||
| 37 | #define LB_i 15 | ||
| 38 | #define FU_i 16 | ||
| 39 | #define FR_i 16 | ||
| 40 | #define FD_i 18 | ||
| 41 | #define FL_i 19 | ||
| 42 | #define BU_i 20 | ||
| 43 | #define BR_i 21 | ||
| 44 | #define BD_i 22 | ||
| 45 | #define BL_i 23 | ||
| 46 | |||
| 47 | /* Mirrored transformations */ | ||
| 48 | #define UF_m 24 | ||
| 49 | #define UL_m 25 | ||
| 50 | #define UB_m 26 | ||
| 51 | #define UR_m 27 | ||
| 52 | #define DF_m 28 | ||
| 53 | #define DL_m 29 | ||
| 54 | #define DB_m 30 | ||
| 55 | #define DR_m 31 | ||
| 56 | #define RU_m 32 | ||
| 57 | #define RF_m 33 | ||
| 58 | #define RD_m 34 | ||
| 59 | #define RB_m 35 | ||
| 60 | #define LU_m 36 | ||
| 61 | #define LF_m 37 | ||
| 62 | #define LD_m 38 | ||
| 63 | #define LB_m 39 | ||
| 64 | #define FU_m 40 | ||
| 65 | #define FR_m 41 | ||
| 66 | #define FD_m 42 | ||
| 67 | #define FL_m 43 | ||
| 68 | #define BU_m 44 | ||
| 69 | #define BR_m 45 | ||
| 70 | #define BD_m 46 | ||
| 71 | #define BL_m 47 | ||
| @@ -1,52 +1,13 @@ | |||
| 1 | typedef enum { | 1 | /* TODO: ifdef for different implementations */ |
| 2 | U = 0, U2, U3, D, D2, D3, | 2 | #include "cube_array.h" |
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | 3 | ||
| 7 | typedef enum { | 4 | /* For moves and transformations, see constants.h */ |
| 8 | UF_i = 0, UL_i, UB_i, UR_i, DF_i, DL_i, DB_i, DR_i, | 5 | typedef uint8_t move_t; |
| 9 | UF_m, UL_m, UB_m, UR_m, DF_m, DL_m, DB_m, DR_m, | 6 | typedef uint8_t trans_t; |
| 10 | RU_i, RF_i, RD_i, RB_i, LU_i, LF_i, LD_i, LB_i, | ||
| 11 | RU_m, RF_m, RD_m, RB_m, LU_m, LF_m, LD_m, LB_m, | ||
| 12 | FU_i, FR_i, FD_i, FL_i, BU_i, BR_i, BD_i, BL_i, | ||
| 13 | FU_m, FR_m, FD_m, FL_m, BU_m, BR_m, BD_m, BL_m | ||
| 14 | } trans_t; | ||
| 15 | |||
| 16 | typedef struct { | ||
| 17 | uint8_t c[8]; | ||
| 18 | uint8_t e[12]; | ||
| 19 | } cube_t; | ||
| 20 | 7 | ||
| 21 | extern cube_t solvedcube; | 8 | extern cube_t solvedcube; |
| 22 | 9 | ||
| 23 | /* | 10 | /* For the textual representation of the cube, see utils/FORMAT.txt */ |
| 24 | The functions readcube() and writecube() use the following format. | ||
| 25 | |||
| 26 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 27 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 28 | Similarly, each corner is represented by three letters and a number | ||
| 29 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 30 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 31 | relative to the U / D axis. | ||
| 32 | |||
| 33 | The pieces are ordered such that the solved cube looks like this: | ||
| 34 | |||
| 35 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 | ||
| 36 | UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 37 | |||
| 38 | Whitespace (including newlines) between pieces is ignored when reading | ||
| 39 | the cube, and a single whitespace character is added between pieces | ||
| 40 | when writing. | ||
| 41 | |||
| 42 | The cube after the moves R'U'F looks like this: | ||
| 43 | |||
| 44 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 | ||
| 45 | UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 46 | |||
| 47 | More formats might be supported in the future. | ||
| 48 | */ | ||
| 49 | |||
| 50 | cube_t readcube(char *); | 11 | cube_t readcube(char *); |
| 51 | void writecube(cube_t, char *); | 12 | void writecube(cube_t, char *); |
| 52 | 13 | ||
diff --git a/src/cube_array.c b/src/cube_array.c index a051610..b0b5e1e 100644 --- a/src/cube_array.c +++ b/src/cube_array.c | |||
| @@ -25,6 +25,7 @@ The third bit is needed because x+y+1 can exceed 4. | |||
| 25 | #include <stdio.h> | 25 | #include <stdio.h> |
| 26 | #endif | 26 | #endif |
| 27 | 27 | ||
| 28 | #include "constants.h" | ||
| 28 | #include "cube.h" | 29 | #include "cube.h" |
| 29 | 30 | ||
| 30 | #define _c_ufr 0U | 31 | #define _c_ufr 0U |
| @@ -713,6 +714,32 @@ inverse_inconsistent: | |||
| 713 | cube_t | 714 | cube_t |
| 714 | compose(cube_t c1, cube_t c2) | 715 | compose(cube_t c1, cube_t c2) |
| 715 | { | 716 | { |
| 717 | uint8_t i, piece, orien, aux, auy; | ||
| 718 | cube_t ret = {0}; | ||
| 719 | |||
| 720 | #ifdef DEBUG | ||
| 721 | if (!isconsistent(c1) || !isconsistent(c2)) | ||
| 722 | goto compose_inconsistent; | ||
| 723 | #endif | ||
| 724 | |||
| 725 | for (i = 0; i < 12; i++) { | ||
| 726 | piece = c2.e[i] & _pbits; | ||
| 727 | orien = (c2.e[i] ^ c1.e[piece]) & _eobit; | ||
| 728 | ret.e[i] = (c1.e[piece] & _pbits) | orien; | ||
| 729 | } | ||
| 730 | |||
| 731 | for (i = 0; i < 8; i++) { | ||
| 732 | piece = c2.c[i] & _pbits; | ||
| 733 | aux = (c2.c[i] & _cobits) + (c1.c[piece] & _cobits); | ||
| 734 | auy = (aux + _ctwist_cw) >> 2U; | ||
| 735 | orien = (aux + auy) & _cobits2; | ||
| 736 | ret.c[i] = (c1.c[piece] & _pbits) | orien; | ||
| 737 | } | ||
| 738 | |||
| 739 | return ret; | ||
| 740 | |||
| 741 | compose_inconsistent: | ||
| 742 | fprintf(stderr, "compose error, inconsistent cube\n"); | ||
| 716 | return errorcube; | 743 | return errorcube; |
| 717 | } | 744 | } |
| 718 | 745 | ||
diff --git a/src/cube_array.h b/src/cube_array.h new file mode 100644 index 0000000..6d9603b --- /dev/null +++ b/src/cube_array.h | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | /* Public properties specific to the array implementation */ | ||
| 2 | |||
| 3 | typedef struct { | ||
| 4 | uint8_t c[8]; | ||
| 5 | uint8_t e[12]; | ||
| 6 | } cube_t; | ||
