147 lines
3.7 KiB
Lua
147 lines
3.7 KiB
Lua
local spread_probability = 0.6
|
|
|
|
local display_inputinfo_counter = 0
|
|
local display_probability_counter = 1
|
|
|
|
local tiles = {}
|
|
local tiletypes = {unknown = 0, empty = 1, wall = 2}
|
|
|
|
local queue = {}
|
|
|
|
function initializeTiles()
|
|
tiles = {}
|
|
queue = {}
|
|
|
|
tiles.width = math.floor(love.graphics.getWidth() / 10)
|
|
tiles.height = math.floor(love.graphics.getHeight() / 10)
|
|
|
|
for x = 1, tiles.width do
|
|
local list = {}
|
|
for y = 1, tiles.height do
|
|
table.insert(list, tiletypes.unknown)
|
|
end
|
|
table.insert(tiles, list)
|
|
end
|
|
|
|
local first_tile_x = math.random(2, tiles.width - 1)
|
|
local first_tile_y = math.random(2, tiles.height - 1)
|
|
|
|
tiles[first_tile_x][first_tile_y] = tiletypes.empty
|
|
|
|
table.insert(queue, {x = first_tile_x, y = first_tile_y - 1})
|
|
table.insert(queue, {x = first_tile_x, y = first_tile_y + 1})
|
|
table.insert(queue, {x = first_tile_x - 1, y = first_tile_y})
|
|
table.insert(queue, {x = first_tile_x + 1, y = first_tile_y})
|
|
end
|
|
|
|
function love.load()
|
|
math.randomseed(os.time())
|
|
|
|
initializeTiles()
|
|
end
|
|
|
|
function love.update(dt)
|
|
-- Update counters
|
|
display_probability_counter = display_probability_counter - dt
|
|
if display_probability_counter < 0 then
|
|
display_probability_counter = 0
|
|
end
|
|
display_inputinfo_counter = display_inputinfo_counter - dt
|
|
if display_inputinfo_counter < 0 then
|
|
display_inputinfo_counter = 0
|
|
end
|
|
|
|
local new_queue = {}
|
|
|
|
for i, location in ipairs(queue) do
|
|
if tiles[location.x][location.y] == tiletypes.unknown then
|
|
-- We haven't covered this tile yet
|
|
|
|
if math.random() < spread_probability then
|
|
-- Empty
|
|
tiles[location.x][location.y] = tiletypes.empty
|
|
|
|
-- Up
|
|
if location.y - 1 >= 1 then
|
|
table.insert(new_queue, {x = location.x, y = location.y - 1})
|
|
end
|
|
|
|
-- Down
|
|
if location.y + 1 <= tiles.height then
|
|
table.insert(new_queue, {x = location.x, y = location.y + 1})
|
|
end
|
|
|
|
-- Left
|
|
if location.x - 1 >= 1 then
|
|
table.insert(new_queue, {x = location.x - 1, y = location.y})
|
|
end
|
|
|
|
-- Right
|
|
if location.x + 1 <= tiles.width then
|
|
table.insert(new_queue, {x = location.x + 1, y = location.y})
|
|
end
|
|
else
|
|
-- Wall
|
|
tiles[location.x][location.y] = tiletypes.wall
|
|
end
|
|
end
|
|
end
|
|
|
|
queue = new_queue
|
|
end
|
|
|
|
function love.keypressed(key)
|
|
if key == 'r' then
|
|
initializeTiles()
|
|
elseif key == 'u' then
|
|
spread_probability = spread_probability + 0.01
|
|
if spread_probability > 1 then
|
|
spread_probability = 1
|
|
end
|
|
display_probability_counter = 1
|
|
elseif key == 'd' then
|
|
spread_probability = spread_probability - 0.01
|
|
if spread_probability < 0 then
|
|
spread_probability = 0
|
|
end
|
|
display_probability_counter = 1
|
|
elseif key == 'q' then
|
|
love.event.quit()
|
|
else
|
|
display_inputinfo_counter = 1
|
|
end
|
|
end
|
|
|
|
function love.draw()
|
|
local x_scale = love.graphics.getWidth() / tiles.width
|
|
local y_scale = love.graphics.getHeight() / tiles.height
|
|
for tile_x, list in ipairs(tiles) do
|
|
local x = (tile_x - 1) * x_scale
|
|
for tile_y, tile in ipairs(list) do
|
|
local y = (tile_y - 1) * y_scale
|
|
|
|
if tile == tiletypes.empty then
|
|
love.graphics.setColor(0, 0, 0)
|
|
elseif tile == tiletypes.wall then
|
|
love.graphics.setColor(1, 1, 1)
|
|
else
|
|
love.graphics.setColor(1, 0.5, 0.5)
|
|
end
|
|
|
|
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
|
|
end
|
|
end
|
|
|
|
if display_probability_counter > 0 then
|
|
love.graphics.setColor(0.1, 0.1, 0.1)
|
|
love.graphics.rectangle('fill', 10, 10, 32, 16)
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.print(spread_probability, 10, 10)
|
|
end
|
|
if display_inputinfo_counter > 0 then
|
|
love.graphics.setColor(0.1, 0.1, 0.1)
|
|
love.graphics.rectangle('fill', 10, 30, 500, 16)
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.print("r - restart, u - increase spread probability, d - decrease spread probability, q - quit", 10, 30)
|
|
end
|
|
end
|