|  | @@ -142,10 +142,13 @@ local pieces = {
 | 
											
												
													
														|  |          },
 |  |          },
 | 
											
												
													
														|  |      },
 |  |      },
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  | 
 |  | +local piece = {}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  local board = {}
 |  |  local board = {}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  local game_state = {
 |  |  local game_state = {
 | 
											
												
													
														|  | 
 |  | +    board = {},
 | 
											
												
													
														|  | 
 |  | +    level = 1,
 | 
											
												
													
														|  |      piece_queue = {},
 |  |      piece_queue = {},
 | 
											
												
													
														|  |      piece_bag = {},
 |  |      piece_bag = {},
 | 
											
												
													
														|  |      update = nil,
 |  |      update = nil,
 | 
											
										
											
												
													
														|  | @@ -195,6 +198,7 @@ function init_board()
 | 
											
												
													
														|  |  end
 |  |  end
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  function spawn_piece()
 |  |  function spawn_piece()
 | 
											
												
													
														|  | 
 |  | +    trace("spawn")
 | 
											
												
													
														|  |      if #game_state.piece_bag <= 4 then
 |  |      if #game_state.piece_bag <= 4 then
 | 
											
												
													
														|  |          fill_bag()
 |  |          fill_bag()
 | 
											
												
													
														|  |      end
 |  |      end
 | 
											
										
											
												
													
														|  | @@ -207,10 +211,18 @@ function spawn_piece()
 | 
											
												
													
														|  |  end
 |  |  end
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  function hard_drop()
 |  |  function hard_drop()
 | 
											
												
													
														|  | 
 |  | +    for i=20,0,-1 do
 | 
											
												
													
														|  | 
 |  | +        local tempy = active_piece.y + i
 | 
											
												
													
														|  | 
 |  | +        if test_can_fit(active_piece.type, active_piece.x, tempy, active_piece.rotation) then
 | 
											
												
													
														|  | 
 |  | +            return i
 | 
											
												
													
														|  | 
 |  | +        end
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return 0
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      -- active_piece.rotation = 1
 |  |      -- active_piece.rotation = 1
 | 
											
												
													
														|  | -    active_piece.type = active_piece.type + 1
 |  | 
 | 
											
												
													
														|  | -    if active_piece.type > 7 then active_piece.type = 1 end
 |  | 
 | 
											
												
													
														|  | -    trace("type: "..tostring(active_piece.type)..", rot="..tostring(active_piece.rotation))
 |  | 
 | 
											
												
													
														|  | 
 |  | +    -- active_piece.type = active_piece.type + 1
 | 
											
												
													
														|  | 
 |  | +    -- if active_piece.type > 7 then active_piece.type = 1 end
 | 
											
												
													
														|  | 
 |  | +    -- trace("hard_drop: new type: "..tostring(active_piece.type)..", rot="..tostring(active_piece.rotation))
 | 
											
												
													
														|  |  end
 |  |  end
 | 
											
												
													
														|  |  function soft_drop()
 |  |  function soft_drop()
 | 
											
												
													
														|  |      active_piece.y = active_piece.y + 1
 |  |      active_piece.y = active_piece.y + 1
 | 
											
										
											
												
													
														|  | @@ -225,7 +237,8 @@ end
 | 
											
												
													
														|  |  function handle_input()
 |  |  function handle_input()
 | 
											
												
													
														|  |      is_move_pressed = false
 |  |      is_move_pressed = false
 | 
											
												
													
														|  |      if btnp(0) then
 |  |      if btnp(0) then
 | 
											
												
													
														|  | -        spawn_piece()
 |  | 
 | 
											
												
													
														|  | 
 |  | +        -- spawn_piece()
 | 
											
												
													
														|  | 
 |  | +        hard_drop()
 | 
											
												
													
														|  |  	end
 |  |  	end
 | 
											
												
													
														|  |  	if btn(1) then
 |  |  	if btn(1) then
 | 
											
												
													
														|  |          is_move_pressed = true
 |  |          is_move_pressed = true
 | 
											
										
											
												
													
														|  | @@ -328,6 +341,62 @@ function update_game()
 | 
											
												
													
														|  |  end
 |  |  end
 | 
											
												
													
														|  |  -- END state updates
 |  |  -- END state updates
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +function test_can_fit(ptype, xpos, ypos, rotation)
 | 
											
												
													
														|  | 
 |  | +	for y = 1, 4 do
 | 
											
												
													
														|  | 
 |  | +        for x = 1, 4 do
 | 
											
												
													
														|  | 
 |  | +            local block = pieces[ptype][rotation][y][x]
 | 
											
												
													
														|  | 
 |  | +            if block ~= 0 then
 | 
											
												
													
														|  | 
 |  | +                if ypos + y > 21 or ypos + y < 0 then return false end
 | 
											
												
													
														|  | 
 |  | +                trace("yooo "..tostring(ypos))
 | 
											
												
													
														|  | 
 |  | +                -- TODO: some nil reference here idk
 | 
											
												
													
														|  | 
 |  | +                local cell = game_state.board[ypos + y - 1][xpos + x - 1]
 | 
											
												
													
														|  | 
 |  | +                if cell ~= 0 then
 | 
											
												
													
														|  | 
 |  | +                    return false
 | 
											
												
													
														|  | 
 |  | +                end
 | 
											
												
													
														|  | 
 |  | +            end
 | 
											
												
													
														|  | 
 |  | +        end
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return true
 | 
											
												
													
														|  | 
 |  | +end
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +function piece.move_left()
 | 
											
												
													
														|  | 
 |  | +    local tempx = active_piece.x - 1
 | 
											
												
													
														|  | 
 |  | +    if piece.test_can_fit(active_piece.type, tempx, active_piece.y, active_piece.rotation) then
 | 
											
												
													
														|  | 
 |  | +        active_piece.x = tempx
 | 
											
												
													
														|  | 
 |  | +        return true
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return false
 | 
											
												
													
														|  | 
 |  | +end
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +function piece.move_right()
 | 
											
												
													
														|  | 
 |  | +    local tempx = active_piece.x + 1
 | 
											
												
													
														|  | 
 |  | +    if piece.test_can_fit(active_piece.type, tempx, active_piece.y, active_piece.rotation) then
 | 
											
												
													
														|  | 
 |  | +        active_piece.x = tempx
 | 
											
												
													
														|  | 
 |  | +        return true
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return false
 | 
											
												
													
														|  | 
 |  | +end
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +function piece.soft_drop()
 | 
											
												
													
														|  | 
 |  | +    local tempy = active_piece.y + 1
 | 
											
												
													
														|  | 
 |  | +    if piece.test_can_fit(active_piece.type, active_piece.x, tempy, active_piece.rotation) then
 | 
											
												
													
														|  | 
 |  | +        active_piece.y = tempy
 | 
											
												
													
														|  | 
 |  | +        return true
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return false
 | 
											
												
													
														|  | 
 |  | +end
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +-- if return value is 0, piece should lock immediately
 | 
											
												
													
														|  | 
 |  | +function piece.test_hard_drop()
 | 
											
												
													
														|  | 
 |  | +    for i=20,0,-1 do
 | 
											
												
													
														|  | 
 |  | +        local tempy = active_piece.y + i
 | 
											
												
													
														|  | 
 |  | +        if piece.test_can_fit(active_piece.type, active_piece.x, tempy, active_piece.rotation) then
 | 
											
												
													
														|  | 
 |  | +            return i
 | 
											
												
													
														|  | 
 |  | +        end
 | 
											
												
													
														|  | 
 |  | +    end
 | 
											
												
													
														|  | 
 |  | +    return 0
 | 
											
												
													
														|  | 
 |  | +end
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  game_state.update = update_intro
 |  |  game_state.update = update_intro
 | 
											
												
													
														|  |  
 |  |  
 |