This commit is contained in:
Dusk 2022-07-06 16:30:54 +02:00
parent 1d5bc310b9
commit ee893b5947
3 changed files with 42 additions and 52 deletions

View File

@ -48,7 +48,6 @@ pub fn drop(grid: Grid, piece: Piece) Grid {
for (piece.structure) |_, y| { for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| { for (piece.structure[y]) |_, x| {
if (piece.structure[y][x]) { if (piece.structure[y][x]) {
new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free = false; new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free = false;
new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].color = piece.color; new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].color = piece.color;
} }
@ -66,13 +65,7 @@ pub fn shadow(grid: Grid, piece: Piece) Piece {
fn checkCollision(grid: Grid, piece: Piece) bool { fn checkCollision(grid: Grid, piece: Piece) bool {
for (piece.structure) |_, y| { for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| { for (piece.structure[y]) |_, x| {
if (piece.structure[y][x] if (piece.structure[y][x] and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1) or (@intCast(i32, x) + piece.col < 0) or (@intCast(i32, y) + piece.row > Grid.nrows - 1) or (@intCast(i32, y) + piece.row < 0) or (!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free))) {
and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1)
or (@intCast(i32, x) + piece.col < 0)
or (@intCast(i32, y) + piece.row > Grid.nrows - 1)
or (@intCast(i32, y) + piece.row < 0)
or (!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free)))
{
return true; return true;
} }
} }
@ -83,64 +76,61 @@ fn checkCollision(grid: Grid, piece: Piece) bool {
pub fn rotateLeft(grid: Grid, piece: Piece) Piece { pub fn rotateLeft(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.left); var new_piece = piece.rotate(Piece.Rot.left);
new_piece = kick(grid, new_piece, piece); new_piece = kick(grid, new_piece, piece);
std.debug.print("{}\n",.{new_piece.rot_stage}); std.debug.print("{}\n", .{new_piece.rot_stage});
return new_piece; return new_piece;
} }
pub fn rotateRight(grid: Grid, piece: Piece) Piece { pub fn rotateRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.right); var new_piece = piece.rotate(Piece.Rot.right);
new_piece = kick(grid, new_piece, piece); new_piece = kick(grid, new_piece, piece);
std.debug.print("{}\n",.{new_piece.rot_stage}); std.debug.print("{}\n", .{new_piece.rot_stage});
return new_piece; return new_piece;
} }
pub fn kick(grid: Grid, piece: Piece, prev_piece: Piece) Piece { pub fn kick(grid: Grid, piece: Piece, prev_piece: Piece) Piece {
// O piece does not rotate
if (piece.piece_type == Piece.Type.o) return piece; if (piece.piece_type == Piece.Type.o) return piece;
// Get the rotation stage for appropiate checks
const prev_stage = prev_piece.rot_stage; const prev_stage = prev_piece.rot_stage;
const next_stage = piece.rot_stage; const next_stage = piece.rot_stage;
var new_piece = piece; var new_piece = piece;
// Test 1
if (!checkCollision(grid, new_piece)) return new_piece;
var offsets: [4][2]i8 = undefined;
if (piece.piece_type == Piece.Type.i) { if (piece.piece_type == Piece.Type.i) {
if (!checkCollision(grid, new_piece)) return new_piece; if ((prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right) or (prev_stage == Piece.RotStage.left and next_stage == Piece.RotStage.flip)) {
} //TODO offsets = .{ .{ -2, 0 }, .{ 1, 0 }, .{ -2, 1 }, .{ 1, -2 } };
} else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.init) or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.left)) {
if ( offsets = .{ .{ 2, 0 }, .{ -1, 0 }, .{ 2, -1 }, .{ -1, 2 } };
(prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right) } else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.flip) or (prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.left)) {
or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.right) offsets = .{ .{ -1, 0 }, .{ 2, 0 }, .{ -1, -2 }, .{ 2, 1 } };
or (prev_stage == Piece.RotStage.left and next_stage == Piece.RotStage.flip)
or (prev_stage == Piece.RotStage.left and next_stage == Piece.RotStage.init)
) {
// Test Positions
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col -= 1;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.row -= 1;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col += 1;
new_piece.row += 3;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col -= 1;
if (!checkCollision(grid, new_piece)) return new_piece;
} else { } else {
if (!checkCollision(grid, new_piece)) return new_piece; offsets = .{ .{ 1, 0 }, .{ -2, 0 }, .{ 1, 2 }, .{ -2, -1 } };
new_piece.col += 1;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.row += 1;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col -= 1;
new_piece.row -= 3;
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col += 1;
if (!checkCollision(grid, new_piece)) return new_piece;
} }
} else {
if ((prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right) or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.right)) {
offsets = .{ .{ -1, 0 }, .{ -1, -1 }, .{ 0, 2 }, .{ -1, 2 } };
} else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.init) or (prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.flip)) {
offsets = .{ .{ 1, 0 }, .{ 1, 1 }, .{ 0, -2 }, .{ 1, -2 } };
} else if ((prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.left) or (prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.left)) {
offsets = .{ .{ 1, 0 }, .{ 1, -1 }, .{ 0, 2 }, .{ 1, 2 } };
} else {
offsets = .{ .{ -1, 0 }, .{ -1, 1 }, .{ 0, -2 }, .{ -1, -2 } };
}
}
// Try offsets
for (offsets) |offset| {
new_piece.col += offset[0];
new_piece.row += offset[1];
if (!checkCollision(grid, new_piece)) return new_piece;
new_piece.col -= offset[0];
new_piece.row -= offset[1];
}
return prev_piece; return prev_piece;
} }

View File

@ -1,7 +1,7 @@
1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
32 36570080 1657065821400339134 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36570080 1657107000090054032 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig 29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig
@ -21,7 +21,7 @@
121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig 121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig 1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig
32 36570080 1657065821400339134 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36570080 1657107000090054032 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig 7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig
21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig 21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig
113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig 113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig
@ -40,7 +40,7 @@
9352 36968413 1655270162000000000 8c526bbafa435d2af666b490a1e838ad /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Mutex.zig 9352 36968413 1655270162000000000 8c526bbafa435d2af666b490a1e838ad /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Mutex.zig
3400 36968452 1655270162000000000 5d1d518c32f4fd700943417f56120125 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/writer.zig 3400 36968452 1655270162000000000 5d1d518c32f4fd700943417f56120125 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/writer.zig
41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig 41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig
4491 36569862 1657065820400339127 9a2d44189292dcdacdca08c75c5b78ec /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig 5108 36569824 1657106993040053980 07f1cbc47513104f7aaefa9a2df0de9b /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
1287 36569807 1657025367450040341 019ede768d3bb377ce293523ab6255bf /home/strange/Documentos/Projectes/Programació/usg/src/renderer.zig 1287 36569807 1657025367450040341 019ede768d3bb377ce293523ab6255bf /home/strange/Documentos/Projectes/Programació/usg/src/renderer.zig
102705 36968384 1655270162000000000 4896646ce9abce74b86bcbbc275a18c1 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fmt.zig 102705 36968384 1655270162000000000 4896646ce9abce74b86bcbbc275a18c1 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fmt.zig
31107 36968497 1655270162000000000 efe5272d00145b6eca03d42fcf5fcd30 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem/Allocator.zig 31107 36968497 1655270162000000000 efe5272d00145b6eca03d42fcf5fcd30 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem/Allocator.zig