~sircmpwn/tetrominoes

tetrominoes/board.ha -rw-r--r-- 6.9 KiB
7209df15Drew DeVault Update for integer changes 3 months ago
                                                                                
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use math::random;
use sdl2;
use types;

def BOARD_WIDTH: int = 10;
def BOARD_HEIGHT: int = 20;
def BOARD_SCALE: int = 4;
def SCENE_SCALE: int = 2;

type color = enum u8 {
	EMPTY,
	RED,
	BLUE,
	GREEN,
	PURPLE,
	YELLOW,
	CYAN,
	MAX = CYAN,
};

const color_map: [_](u8, u8, u8) = [
	(0, 0, 0),
	(255, 0, 0),
	(0, 255, 0),
	(0, 0, 255),
	(255, 0, 255),
	(255, 255, 0),
	(0, 255, 255),
];

type board = struct {
	cells: [BOARD_WIDTH * BOARD_HEIGHT]color,
	offs_x: int,
	offs_y: int,

	// Queue of tetrominoes to spawn, shuffled from [[tetrominoes]]
	tetrominoes: []int,
	next: size,

	active: [][TETROMINO_LEN]color,
	active_i: int,
	active_x: int,
	active_y: int,

	ghost_x: int,
	ghost_y: int,
};

fn draw_board(state: *state, board: *board) (void | sdl2::error) = {
	const offs = (board.offs_x * SCENE_SCALE, board.offs_y * SCENE_SCALE);
	const tex = &state.piece;
	for (let y = 0; y < BOARD_HEIGHT; y += 1)
	for (let x = 0; x < BOARD_WIDTH; x += 1) {
		let cell = board.cells[y * BOARD_WIDTH + x];
		if (cell == color::EMPTY) {
			continue;
		};
		let color = &color_map[cell];
		sdl2::set_texture_alpha_mod(tex.tex, 255)?;
		sdl2::set_texture_color_mod(tex.tex, color.0, color.1, color.2)?;
		sdl2::render_copy(state.render, tex.tex, null, &sdl2::rect {
			x = offs.0 + x * BOARD_SCALE * tex.width,
			y = offs.1 + y * BOARD_SCALE * tex.height,
			w = tex.width * BOARD_SCALE,
			h = tex.height * BOARD_SCALE,
		})?;
	};

	switch (state.state) {
	case gamestate::CLEAR =>
		return;
	case => void;
	};

	draw_active(state, board.ghost_x, board.ghost_y, 128)?;

	const now = sdl2::get_ticks();
	switch (state.state) {
	case gamestate::INTRO =>
		// XXX: This is hacky as fuck.
		// https://todo.sr.ht/~sircmpwn/hare/54
		let alpha = 255u8;
		const duration = (state.fadein_finish - state.fadein_start): int;
		const diff = (now - state.fadein_start): int;
		let workaround = 1.0 - (diff: f64 / duration: f64);
		if (diff > duration) {
			workaround = 0.0;
		};
		if (now >= state.fadein_start) {
			alpha = (workaround * 255.0): int: u8;
		};
		sdl2::set_render_draw_color(state.render, 0, 0, 0, alpha)?;
		sdl2::render_fill_rect(state.render, null)?;
	case => void;
	};

	draw_active(state, board.active_x, board.active_y, 255)?;
};

fn draw_active(
	state: *state,
	x: int, y: int,
	alpha: u8,
) (void | sdl2::error) = {
	const active = (x, y);
	const board = &state.board, tex = &state.piece;
	const offs = (board.offs_x * SCENE_SCALE, board.offs_y * SCENE_SCALE);
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		let cell = board.active[board.active_i][y * TETROMINO_WIDTH + x];
		if (cell == color::EMPTY) {
			continue;
		};
		const x = x + active.0, y = y + active.1;
		if (y < 0 && state.state != gamestate::INTRO) {
			continue;
		};
		let color = &color_map[cell];
		sdl2::set_texture_color_mod(tex.tex, color.0, color.1, color.2)?;
		sdl2::set_texture_alpha_mod(tex.tex, alpha)?;
		sdl2::render_copy(state.render, tex.tex, null, &sdl2::rect {
			x = offs.0 + x * BOARD_SCALE * tex.width,
			y = offs.1 + y * BOARD_SCALE * tex.height,
			w = tex.width * BOARD_SCALE,
			h = tex.height * BOARD_SCALE,
		})?;
	};
};

fn canmove(
	state: *state,
	active: *[TETROMINO_LEN]color,
	dx: int, dy: int,
) bool = {
	const board = &state.board.cells;
	const active_x = state.board.active_x;
	const active_y = state.board.active_y;
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		const active_ix = y * TETROMINO_WIDTH + x;
		if (active[active_ix] == 0) {
			continue;
		};
		if (x + dx + active_x < 0 || x + dx + active_x >= BOARD_WIDTH) {
			return false;
		};
		if (y + dy + active_y < 0) {
			continue;
		};
		const x = x + dx, y = y + dy;
		const board_ix = (y + active_y) * BOARD_WIDTH + (x + active_x);
		if (board_ix: size >= len(board) || board[board_ix] != 0) {
			return false;
		};
	};
	return true;
};

fn shuffle_tetrominoes(state: *state) void = {
	static let items = [0, 1, 2, 3, 4, 5, 6];
	const board = &state.board;
	for (let i = len(items) - 1; i > 0; i -= 1) {
		let j = random::next(&state.rand): size % i;
		let x = items[i];
		items[i] = items[j];
		items[j] = x;
	};
	board.tetrominoes = items[..];
	board.next = 0;
};

fn update_ghost(state: *state) void = {
	const board = &state.board;
	const cells = &board.cells;
	const active = &board.active[board.active_i];

	let y = 1;
	for (canmove(state, active, 0, y); y += 1) void;

	board.ghost_x = board.active_x;
	board.ghost_y = board.active_y + y - 1;
};

fn commit(state: *state) void = {
	if (state.board.active_y < 0) {
		state.state = gamestate::GAMEOVER;
		return;
	};

	let board = &state.board.cells;
	let active = &state.board.active[state.board.active_i];
	const active_x = state.board.active_x;
	const active_y = state.board.active_y;
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		const board_ix = (y + active_y) * BOARD_WIDTH + (x + active_x);
		const active_ix = y * TETROMINO_WIDTH + x;
		if (board_ix: size >= len(board)) {
			continue;
		};
		if (active[active_ix] != 0) {
			board[board_ix] = active[active_ix];
		};
	};

	static let clearbuf: [TETROMINO_HEIGHT]int = [0...];
	let clear = clearbuf[..0];
	state.state = gamestate::SPAWN;
	for (let y = active_y; y < BOARD_HEIGHT; y += 1)
	for (let x = 0, sum = 0; x < BOARD_WIDTH; x += 1) {
		const board_ix = y * BOARD_WIDTH + x;
		const cell = state.board.cells[board_ix];
		if (cell != 0) {
			sum += 1;
		};
		if (sum == BOARD_WIDTH) {
			state.state = gamestate::CLEAR;
			static append(clear, y);
		};
	};
	state.clearlines = clear;
	state.clearframe = -1;
};

fn shiftrows(state: *state) void = {
	let j = 0;
	for (let i = len(state.clearlines): int - 1; i >= 0; i -= 1) {
		shiftrow(state, state.clearlines[i] + j);
		j += 1;
	};
};

fn shiftrow(state: *state, row: int) void = {
	const cells = &state.board.cells;
	for (let y = row; y > 0; y -= 1) {
		for (let x = 0; x < BOARD_WIDTH; x += 1) {
			const src = &cells[(y - 1) * BOARD_WIDTH + x];
			cells[y * BOARD_WIDTH + x] = *src;
		};
	};
};

fn activesize(
	state: *state,
	width: nullable *int,
	height: nullable *int,
) void = {
	let min_x = types::INT_MAX, min_y = types::INT_MAX;
	let max_x = 0, max_y = 0;
	const active = &state.board.active[state.board.active_i];
	for (let y = 0; y < TETROMINO_HEIGHT; y += 1)
	for (let x = 0; x < TETROMINO_WIDTH; x += 1) {
		const active = active[y * TETROMINO_WIDTH + x] != 0;
		if (active && x > max_x) {
			max_x = x;
		};
		if (active && y > max_y) {
			max_y = y;
		};
		if (active && x < min_x) {
			min_x = x;
		};
		if (active && y < min_y) {
			min_y = y;
		};
	};
	match (width) {
	case let ptr: *int =>
		*ptr = max_x - min_x + 1;
	case null => void;
	};
	match (height) {
	case let ptr: *int =>
		*ptr = max_y - min_y + 1;
	case null => void;
	};
};

fn clear_lines(state: *state) void = {
	for (let i = 0z; i < len(state.clearlines); i += 1)
	for (let x = 0; x < BOARD_WIDTH; x += 1) {
		const y = state.clearlines[i];
		state.board.cells[y * BOARD_WIDTH + x] = 0;
	};
};