Kicking, not for I pieces

This commit is contained in:
Dusk 2022-07-06 02:12:18 +02:00
parent 8d2259b6c7
commit 1d5bc310b9
6 changed files with 111 additions and 22 deletions

3
.gitignore vendored
View File

@ -88,4 +88,5 @@ tags
zig zig
zig-out zig-out
zig-cache zig-cache
zig-cache/* zig-cache/h
zig-cache/z

View File

@ -1,3 +1,4 @@
const std = @import("std");
const Grid = @import("grid.zig"); const Grid = @import("grid.zig");
const Piece = @import("piece.zig"); const Piece = @import("piece.zig");
@ -65,9 +66,12 @@ 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] and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1) or if (piece.structure[y][x]
(@intCast(i32, x) + piece.col < 0) or (@intCast(i32, y) + piece.row > Grid.nrows - 1) or and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1)
(!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free))) 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;
} }
@ -78,16 +82,65 @@ 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);
if (checkCollision(grid, new_piece)) { new_piece = kick(grid, new_piece, piece);
return piece; 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);
if (checkCollision(grid, new_piece)) { new_piece = kick(grid, new_piece, piece);
return piece; 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 {
if (piece.piece_type == Piece.Type.o) return piece;
const prev_stage = prev_piece.rot_stage;
const next_stage = piece.rot_stage;
var new_piece = piece;
if (piece.piece_type == Piece.Type.i) {
if (!checkCollision(grid, new_piece)) return new_piece;
} //TODO
if (
(prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right)
or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.right)
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 {
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;
}
return prev_piece;
}

View File

@ -11,6 +11,13 @@ pub const Rot = enum {
left, left,
}; };
pub const RotStage = enum(i8) {
init,
right,
flip,
left,
};
pub const Type = enum { pub const Type = enum {
o, o,
i, i,
@ -31,9 +38,14 @@ col: i32,
// 4x4 grid indicating piece form // 4x4 grid indicating piece form
structure: [4][4]bool, structure: [4][4]bool,
// Rotation Stage
rot_stage: RotStage = RotStage.init,
// Should this be dropped and become part of the grid (flag) // Should this be dropped and become part of the grid (flag)
dropped: bool = false, dropped: bool = false,
// Has the piece been swapped?
swapped: bool = false, swapped: bool = false,
piece_type: Type, piece_type: Type,
color: SDL.SDL_Color, color: SDL.SDL_Color,
@ -107,15 +119,28 @@ pub fn rotate(self: Self, dir: Rot) Self {
var new_piece = self; var new_piece = self;
// The "sequences" indicate square coords and are always the same when rotating, so we make all squares follow said rotation
// [Inner/Outer][The sequence (0 -> 1 -> 2 -> 3 -> 0...)][Rows, Cols]
if (self.piece_type == Type.i) { if (self.piece_type == Type.i) {
const sequences = .{ .{ .{ 1, 0 }, .{ 0, 2 }, .{ 2, 3 }, .{ 3, 1 } }, .{ .{ 2, 0 }, .{ 0, 1 }, .{ 1, 3 }, .{ 3, 2 } }, .{ .{ 1, 1 }, .{ 1, 2 }, .{ 2, 2 }, .{ 2, 1 } } }; const sequences = .{ .{ .{ 1, 0 }, .{ 0, 2 }, .{ 2, 3 }, .{ 3, 1 } }, .{ .{ 2, 0 }, .{ 0, 1 }, .{ 1, 3 }, .{ 3, 2 } }, .{ .{ 1, 1 }, .{ 1, 2 }, .{ 2, 2 }, .{ 2, 1 } } };
if (dir == Rot.right) {
// Cicle through rotation stage
new_piece.rot_stage = @intToEnum(RotStage, @mod((@enumToInt(new_piece.rot_stage) + 1), 4));
// Rotate structure CW
inline for (sequences) |seq| { inline for (sequences) |seq| {
inline for (seq) |_, i| { inline for (seq) |_, i| {
if (dir == Rot.right) {
const refi = @mod((@intCast(i32, i) - 1), 4); const refi = @mod((@intCast(i32, i) - 1), 4);
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]]; new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]];
}
}
} else { } else {
// Cicle through rotation stage
new_piece.rot_stage = @intToEnum(RotStage, @mod((@enumToInt(new_piece.rot_stage) - 1), 4));
// Rotate structure CCW
inline for (sequences) |seq| {
inline for (seq) |_, i| {
const refi = @mod((@intCast(i32, i) + 1), 4); const refi = @mod((@intCast(i32, i) + 1), 4);
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]]; new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]];
} }
@ -124,12 +149,22 @@ pub fn rotate(self: Self, dir: Rot) Self {
} else { } else {
const sequences = .{ .{ .{ 0, 0 }, .{ 0, 2 }, .{ 2, 2 }, .{ 2, 0 } }, .{ .{ 0, 1 }, .{ 1, 2 }, .{ 2, 1 }, .{ 1, 0 } } }; const sequences = .{ .{ .{ 0, 0 }, .{ 0, 2 }, .{ 2, 2 }, .{ 2, 0 } }, .{ .{ 0, 1 }, .{ 1, 2 }, .{ 2, 1 }, .{ 1, 0 } } };
if (dir == Rot.right) {
// Cicle through rotation stage
new_piece.rot_stage = @intToEnum(RotStage, @mod((@enumToInt(new_piece.rot_stage) + 1), 4));
// Rotate structure CW
inline for (sequences) |seq| { inline for (sequences) |seq| {
inline for (seq) |_, i| { inline for (seq) |_, i| {
if (dir == Rot.right) {
const refi = @mod((@intCast(i32, i) - 1), 4); const refi = @mod((@intCast(i32, i) - 1), 4);
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]]; new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]];
}
}
} else { } else {
// Cicle through rotation stage
new_piece.rot_stage = @intToEnum(RotStage, @mod((@enumToInt(new_piece.rot_stage) - 1), 4));
// Rotate structure CCW
inline for (sequences) |seq| {
inline for (seq) |_, i| {
const refi = @mod((@intCast(i32, i) + 1), 4); const refi = @mod((@intCast(i32, i) + 1), 4);
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]]; new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[refi][0]][seq[refi][1]];
} }

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 1657057884680280513 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36570080 1657065821400339134 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 1657057884680280513 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36570080 1657065821400339134 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
@ -29,7 +29,7 @@
2077 36569864 1657057506010277717 a18ae95a6705d7982e23251e180871b3 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig 2077 36569864 1657057506010277717 a18ae95a6705d7982e23251e180871b3 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig
256 36569823 1657057405170276972 a69ae39529d33ff9e7623523a1e0dbe9 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig 256 36569823 1657057405170276972 a69ae39529d33ff9e7623523a1e0dbe9 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig
1708 36569840 1657038395020136563 151ce67aaf4e36de54f56be701bc0b66 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig 1708 36569840 1657038395020136563 151ce67aaf4e36de54f56be701bc0b66 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
5393 36569846 1657057045880274318 1150e3c04cfc9dfab11e84e9393b3c7f /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig 6714 36569846 1657063070590318817 75177ff9bec84da98c4812ee52669e19 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
16192 36968506 1655270162000000000 744a2bb13e662f350a196780c9fbcad2 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand.zig 16192 36968506 1655270162000000000 744a2bb13e662f350a196780c9fbcad2 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand.zig
3105 36968346 1655270162000000000 412e5ff0bcf84571f173caa8acab5393 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand/Xoshiro256.zig 3105 36968346 1655270162000000000 412e5ff0bcf84571f173caa8acab5393 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand/Xoshiro256.zig
45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig 45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.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
2630 36569862 1657050236460224024 5dff7cfcc8018dda84a0c5cdfefabc5b /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig 4491 36569862 1657065820400339127 9a2d44189292dcdacdca08c75c5b78ec /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