Skip to content

Commit

Permalink
fix: prevent single-cell piece splitting
Browse files Browse the repository at this point in the history
The algorithm should have skipped these already, but there's a bug that
seems to show otherwise, so here's another guard.
  • Loading branch information
russmatney committed Jun 23, 2024
1 parent 123a759 commit 5fb23e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/BloxGrid.gd
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func bottom_up_pieces(_dir: Vector2i) -> Array[BloxPiece]:
var ps: Array[BloxPiece] = []
ps.assign(pieces)
ps.sort_custom(func(pa, pb):
return pa.get_max_y() > pb.get_max_y())
return pa.get_max_y() >= pb.get_max_y())
return ps

func apply_step_tetris(dir=Vector2i.DOWN) -> bool:
Expand Down Expand Up @@ -247,6 +247,10 @@ func clear_rows() -> Array:

func split_piece_coord(piece: BloxPiece, grid_coord: Vector2i) -> void:
var cell = piece.remove_coord(grid_coord)

if piece.is_empty():
Log.warn("erasing/creating single-cell piece!?", piece)
pieces.erase(piece)
# maintain the same cell object!
var new_p = BloxPiece.new({grid_cells=[cell]})
add_piece(new_p)
Expand Down Expand Up @@ -298,7 +302,9 @@ func apply_split_puyo(dir=Vector2i.DOWN) -> bool:
if not p:
Log.error("Missing expected piece in apply_split_puyo", crd)
continue
split_piece_coord(p, crd)
# should have already filtered on this?!
if p.cell_count() > 1:
split_piece_coord(p, crd)

var did_split = not to_split.is_empty()
return did_split
Expand Down
6 changes: 6 additions & 0 deletions src/BloxPiece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ func get_max_y():
return 0
return grid_cells.map(func(c): return c.coord.y).max()

func get_max_x():
if grid_cells.is_empty():
Log.warn("Piece with no grid_cells?", self)
return 0
return grid_cells.map(func(c): return c.coord.x).max()

## coords ####################################

func grid_coords() -> Array[Vector2i]:
Expand Down

0 comments on commit 5fb23e9

Please sign in to comment.