Skip to content

Commit

Permalink
add num invisible lines, simplify line clear delay state struct, simp…
Browse files Browse the repository at this point in the history
…lify matrix access, add "spawn shift"
  • Loading branch information
mgerhold committed Jul 27, 2024
1 parent 9a045b7 commit 87cb137
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
9 changes: 3 additions & 6 deletions src/obpf/include/obpf/simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ extern "C" {

typedef struct {
uint8_t count;
uint8_t first;
uint8_t second;
uint8_t third;
uint8_t fourth;
uint8_t lines[4];
uint64_t countdown;
uint64_t delay;
} ObpfLineClearDelayState;
Expand Down Expand Up @@ -65,12 +62,12 @@ extern "C" {
OBPF_EXPORT uint64_t obpf_tetrion_get_next_frame(struct ObpfTetrion const* tetrion);
OBPF_EXPORT void obpf_tetrion_simulate_next_frame(struct ObpfTetrion* tetrion, ObpfKeyState key_state);
OBPF_EXPORT void obpf_destroy_tetrion(struct ObpfTetrion const* tetrion);
OBPF_EXPORT struct ObpfMatrix const* obpf_tetrion_matrix(struct ObpfTetrion const* tetrion);
OBPF_EXPORT uint8_t obpf_tetrion_width(void);
OBPF_EXPORT uint8_t obpf_tetrion_height(void);
OBPF_EXPORT uint8_t obpf_tetrion_num_invisible_lines(void);

// Matrix
OBPF_EXPORT ObpfTetrominoType obpf_matrix_get(struct ObpfMatrix const* matrix, ObpfVec2 position);
OBPF_EXPORT ObpfTetrominoType obpf_tetrion_matrix_get(const struct ObpfTetrion* tetrion, ObpfVec2 position);

// clang-format on

Expand Down
28 changes: 15 additions & 13 deletions src/obpf/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ ObpfLineClearDelayState obpf_tetrion_get_line_clear_delay_state(ObpfTetrion cons
}
return ObpfLineClearDelayState{
.count = gsl::narrow<u8>(original_size),
.first = lines.at(0),
.second = lines.at(1),
.third = lines.at(2),
.fourth = lines.at(3),
.lines{
lines.at(0),
lines.at(1),
lines.at(2),
lines.at(3),
},
.countdown = countdown,
.delay = LineClearDelay::delay,
};
Expand All @@ -77,15 +79,6 @@ void obpf_destroy_tetrion(ObpfTetrion const* const tetrion) {
delete tetrion;
}

ObpfMatrix const* obpf_tetrion_matrix(ObpfTetrion const* const tetrion) {
return reinterpret_cast<ObpfMatrix const*>(std::addressof(tetrion->matrix()));
}

ObpfTetrominoType obpf_matrix_get(ObpfMatrix const* const matrix, ObpfVec2 const position) {
auto const pos = Vec2{ position.x, position.y };
return static_cast<ObpfTetrominoType>((*reinterpret_cast<Matrix const*>(matrix))[pos]);
}

uint8_t obpf_tetrion_width() {
return uint8_t{ Matrix::width };
}
Expand All @@ -94,6 +87,15 @@ uint8_t obpf_tetrion_height() {
return uint8_t{ Matrix::height };
}

uint8_t obpf_tetrion_num_invisible_lines() {
return uint8_t{ Matrix::num_invisible_lines };
}

ObpfTetrominoType obpf_tetrion_matrix_get(ObpfTetrion const* const tetrion, ObpfVec2 const position) {
auto const pos = Vec2{ position.x, position.y };
return static_cast<ObpfTetrominoType>(tetrion->matrix()[pos]);
}

ObpfPreviewPieces obpf_tetrion_get_preview_pieces(ObpfTetrion const* tetrion) {
auto const preview_tetrominos = tetrion->get_preview_tetrominos();
ObpfPreviewPieces result{};
Expand Down
1 change: 1 addition & 0 deletions src/simulator/include/simulator/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Matrix final {
public:
static constexpr auto width = std::size_t{ 10 };
static constexpr auto height = std::size_t{ 22 };
static constexpr auto num_invisible_lines = std::size_t{ 2 };

private:
std::array<TetrominoType, width * height> m_minos{};
Expand Down
1 change: 1 addition & 0 deletions src/simulator/include/simulator/tetrion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct ObpfTetrion final {

private:
void freeze_and_destroy_active_tetromino();
[[nodiscard]] bool is_tetromino_completely_visible(Tetromino const& tetromino) const;
[[nodiscard]] bool is_tetromino_position_valid(Tetromino const& tetromino) const;
[[nodiscard]] bool is_active_tetromino_position_valid() const;
void spawn_next_tetromino();
Expand Down
29 changes: 29 additions & 0 deletions src/simulator/tetrion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ void ObpfTetrion::freeze_and_destroy_active_tetromino() {
m_active_tetromino = std::nullopt;
}

[[nodiscard]] bool ObpfTetrion::is_tetromino_completely_visible(Tetromino const& tetromino) const {
if (not is_tetromino_position_valid(tetromino)) {
return false;
}

for (auto const& position : get_mino_positions(tetromino)) {
if (position.y < gsl::narrow<i32>(Matrix::num_invisible_lines)) {
return false;
}
}

return true;
}

[[nodiscard]] bool ObpfTetrion::is_tetromino_position_valid(Tetromino const& tetromino) const {
auto const mino_positions = get_mino_positions(tetromino);
for (auto const position : mino_positions) {
Expand Down Expand Up @@ -170,6 +184,21 @@ void ObpfTetrion::spawn_next_tetromino() {
m_is_hold_possible = true;
}

// todo: check game over state here

// clang-format off
for (
auto i = std::size_t{ 0 };
not is_tetromino_completely_visible(active_tetromino().value()) and i < Matrix::num_invisible_lines;
++i
) { // clang-format on
m_active_tetromino->position.y += 1;
if (not is_active_tetromino_position_valid()) {
m_active_tetromino->position.y -= 1;
break;
}
}

m_is_soft_dropping = false;
m_next_gravity_frame = m_next_frame + gravity_delay_by_level(level());
}
Expand Down

0 comments on commit 87cb137

Please sign in to comment.