-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.c
211 lines (175 loc) · 6.16 KB
/
move.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "move.h"
#include "utils.h"
uint8_t coord_from(uint8_t x, uint8_t y) {
return (y * SIZE_X) + x;
}
//-----------------------------------------------------------------------------
uint8_t coord_x(uint8_t coord) {
return coord % SIZE_X;
}
//-----------------------------------------------------------------------------
uint8_t coord_y(uint8_t coord) {
return coord / SIZE_X;
}
//-----------------------------------------------------------------------------
uint8_t movable_dir(MovabilityTab* mv, uint8_t currentMovable) {
return (currentMovable != MOVABLE_NOT_FOUND) ?
(*mv)[coord_y(currentMovable)][coord_x(currentMovable)] :
MOVABLE_NOT;
}
//-----------------------------------------------------------------------------
void map_movability(PlayGround* pg, PlayGround* mv) {
uint8_t tile, x, y, movable;
for(y = 0; y < SIZE_Y; y++) {
for(x = 0; x < SIZE_X; x++) {
movable = MOVABLE_NOT;
tile = (*pg)[y][x];
if(is_block(tile)) {
if(x > 0 && ((*pg)[y][x - 1] == EMPTY_TILE)) movable += MOVABLE_LEFT;
if((x < (SIZE_X - 1)) && ((*pg)[y][x + 1] == EMPTY_TILE)) movable += MOVABLE_RIGHT;
}
(*mv)[y][x] = movable;
}
}
}
//-----------------------------------------------------------------------------
uint8_t find_movable(PlayGround* mv) {
uint8_t x, y;
for(y = 0; y < SIZE_Y; y++) {
for(x = 0; x < SIZE_X; x++) {
if((*mv)[y][x] != MOVABLE_NOT) return coord_from(x, y);
}
}
return MOVABLE_NOT_FOUND;
}
//-----------------------------------------------------------------------------
uint8_t find_movable_rev(PlayGround* mv) {
uint8_t x, y;
for(y = SIZE_Y - 1; y > 0; y--) {
for(x = SIZE_X - 1; x > 0; x--) {
if((*mv)[y][x] != MOVABLE_NOT) return coord_from(x, y);
}
}
return MOVABLE_NOT_FOUND;
}
//-----------------------------------------------------------------------------
void find_movable_left(PlayGround* mv, uint8_t* currentMovable) {
const uint8_t sx = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_x(*currentMovable) :
SIZE_X / 2;
const uint8_t sy = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_y(*currentMovable) : 0;
uint8_t px = sx;
// to the left, same line
while(px > 0) {
px--;
if((*mv)[sy][px] != MOVABLE_NOT) {
*currentMovable = coord_from(px, sy);
return;
}
}
uint8_t x, y;
for(y = sy - 1; y > 0; y--) {
for(x = SIZE_X - 1; x > 0; x--) {
if((*mv)[y][x] != MOVABLE_NOT) {
*currentMovable = coord_from(x, y);
return;
}
}
}
uint8_t last = find_movable_rev(mv);
if(last != MOVABLE_NOT_FOUND) {
*currentMovable = last;
}
}
//-----------------------------------------------------------------------------
void find_movable_right(PlayGround* mv, uint8_t* currentMovable) {
const uint8_t sx = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_x(*currentMovable) :
SIZE_X / 2;
const uint8_t sy = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_y(*currentMovable) : 0;
uint8_t px = sx;
uint8_t py = sy;
// search right
while(px < SIZE_X - 1) {
px++;
if((*mv)[sy][px] != MOVABLE_NOT) {
*currentMovable = coord_from(px, sy);
return;
}
}
uint8_t x, y;
for(y = py + 1; y < SIZE_Y; y++) {
for(x = 0; x < SIZE_X; x++) {
if((*mv)[y][x] != MOVABLE_NOT) {
*currentMovable = coord_from(x, y);
return;
}
}
}
uint8_t first = find_movable(mv);
if(first != MOVABLE_NOT_FOUND) {
*currentMovable = first;
}
}
//-----------------------------------------------------------------------------
void find_movable_down(PlayGround* mv, uint8_t* currentMovable) {
uint8_t sx = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_x(*currentMovable) : SIZE_X / 2;
uint8_t sy = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_y(*currentMovable) : 0;
uint8_t py = sy;
uint8_t delta, px_l, px_r;
while(py < SIZE_Y - 1) {
py++;
if((*mv)[py][sx] != MOVABLE_NOT) {
*currentMovable = coord_from(sx, py);
return;
}
}
py = sy;
while(py < SIZE_Y - 1) {
py++;
delta = 0;
while(delta < SIZE_X) {
delta++;
px_r = MIN(sx + delta, SIZE_X - 1);
if((*mv)[py][px_r] != MOVABLE_NOT) {
*currentMovable = coord_from(px_r, py);
return;
}
px_l = MAX(sx - delta, 0);
if((*mv)[py][px_l] != MOVABLE_NOT) {
*currentMovable = coord_from(px_l, py);
return;
}
}
}
}
//-----------------------------------------------------------------------------
void find_movable_up(PlayGround* mv, uint8_t* currentMovable) {
uint8_t sx = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_x(*currentMovable) : SIZE_X / 2;
uint8_t sy = (*currentMovable != MOVABLE_NOT_FOUND) ? coord_y(*currentMovable) : 0;
uint8_t py = sy;
uint8_t delta, px_l, px_r;
while(py > 0) {
py--;
if((*mv)[py][sx] != MOVABLE_NOT) {
*currentMovable = coord_from(sx, py);
return;
}
}
py = sy;
while(py > 0) {
py--;
delta = 0;
while(delta < SIZE_X) {
delta++;
px_r = MIN(sx + delta, SIZE_X - 1);
if((*mv)[py][px_r] != MOVABLE_NOT) {
*currentMovable = coord_from(px_r, py);
return;
}
px_l = MAX(sx - delta, 0);
if((*mv)[py][px_l] != MOVABLE_NOT) {
*currentMovable = coord_from(px_l, py);
return;
}
}
}
}