aboutsummaryrefslogtreecommitdiff
path: root/src/movesets.c
blob: d8f5bc1442678264a20fbbefc1b11857ad486328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#define MOVESETS_C

#include "movesets.h"

static bool allowed_HTM(Move m);
static bool allowed_URF(Move m);
static bool allowed_eofb(Move m);
static bool allowed_drud(Move m);
static bool allowed_htr(Move m);
static bool can_append_HTM(Move l2, Move l1, Move m);
static bool can_append_HTM_cached(Alg *alg, Move m, bool inverse);
static bool cancel_niss_HTM_cached(Alg *alg);
static void init_can_append_HTM();

Moveset
moveset_HTM = {
	.name        = "HTM",
	.allowed     = allowed_HTM,
	.can_append  = can_append_HTM_cached,
	.cancel_niss = cancel_niss_HTM_cached,
};

Moveset
moveset_URF = {
	.name        = "URF",
	.allowed     = allowed_URF,
	.can_append  = can_append_HTM_cached,
	.cancel_niss = cancel_niss_HTM_cached,
};

Moveset
moveset_eofb = {
	.name        = "eofb",
	.allowed     = allowed_eofb,
	.can_append  = can_append_HTM_cached,
	.cancel_niss = cancel_niss_HTM_cached,
};

Moveset
moveset_drud = {
	.name        = "drud",
	.allowed     = allowed_drud,
	.can_append  = can_append_HTM_cached,
	.cancel_niss = cancel_niss_HTM_cached,
};

Moveset
moveset_htr = {
	.name        = "htr",
	.allowed     = allowed_htr,
	.can_append  = can_append_HTM_cached,
	.cancel_niss = cancel_niss_HTM_cached,
};

Moveset *
all_movesets[] = {
	&moveset_HTM,
	&moveset_URF,
	&moveset_eofb,
	&moveset_drud,
	&moveset_htr,
	NULL
};

static uint64_t can_append_HTM_mask[NMOVES][NMOVES];

static bool
allowed_HTM(Move m)
{
	return m >= U && m <= B3;
}

static bool
allowed_URF(Move m)
{
	Move b = base_move(m);

	return b == U || b == R || b == F;
}

static bool
allowed_eofb(Move m)
{
	Move b = base_move(m);

	return b == U || b == D || b == R || b == L ||
	       ((b == F || b == B) && m == b+1);
}

static bool
allowed_drud(Move m)
{
	Move b = base_move(m);

	return b == U || b == D ||
	       ((b == R || b == L || b == F || b == B) && m == b + 1);
}

static bool
allowed_htr(Move m)
{
	Move b = base_move(m);

	return moveset_HTM.allowed(m) && m == b + 1;
}

static bool
can_append_HTM(Move l2, Move l1, Move m)
{
	bool cancel, cancel_last, cancel_swap;

	cancel_last = l1 != NULLMOVE && base_move(l1) == base_move(m);
	cancel_swap = l2 != NULLMOVE && base_move(l2) == base_move(m);
	cancel = cancel_last || (commute(l1, l2) && cancel_swap);

	return !cancel;
}

static bool
can_append_HTM_cached(Alg *alg, Move m, bool inverse)
{
	Move *moves, l1, l2;
	uint64_t mbit;
	int n;

	if (inverse) {
		moves = alg->move_inverse;
		n = alg->len_inverse;
	} else {
		moves = alg->move_normal;
		n = alg->len_normal;
	}

	l1 = n > 0 ? moves[n-1] : NULLMOVE;
	l2 = n > 1 ? moves[n-2] : NULLMOVE;

	mbit = ((uint64_t)1) << m;

	return can_append_HTM_mask[l2][l1] & mbit;
}

static bool
cancel_niss_HTM_cached(Alg *alg)
{
	Move i1, i2;
	int n;
	bool can_first, can_swap;

	n = alg->len_inverse;
	i1 = n > 0 ? alg->move_inverse[n-1] : NULLMOVE;
	i2 = n > 1 ? alg->move_inverse[n-2] : NULLMOVE;

	can_first = can_append_HTM_cached(alg, inverse_move(i1), false);
	can_swap  = can_append_HTM_cached(alg, inverse_move(i2), false);

	return can_first && (!commute(i1, i2) || can_swap);
}

static void
init_can_append_HTM()
{
	Move l2, l1, m;

	for (l1 = 0; l1 < NMOVES; l1++)
		for (l2 = 0; l2 < NMOVES; l2++)
			for (m = 0; m < NMOVES; m++)
				if (can_append_HTM(l2, l1, m))
					can_append_HTM_mask[l2][l1]
					    |= (((uint64_t)1) << m);
}

void
init_moveset(Moveset *ms)
{
	int j;
	Move m;

	for (j = 0, m = U; m < NMOVES; m++)
		if (ms->allowed(m))
			ms->sorted_moves[j++] = m;
	ms->sorted_moves[j] = NULLMOVE;

/* TODO: should be here? maybe just init all movesets together anyway... */
	init_can_append_HTM();
}

void
init_movesets()
{
	int i;

	for (i = 0; all_movesets[i] != NULL; i++)
		init_moveset(all_movesets[i]);
}

Generated with cgit - Back to sebastiano.tronto.net