2019-06-28 15:13:03 +00:00
|
|
|
|
local cavern = {}
|
|
|
|
|
local tiletypes = {unknown = 0, empty = 1, wall = 2}
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
local visibility_map = {}
|
|
|
|
|
local remembered_cavern = {}
|
|
|
|
|
|
2019-06-28 15:13:03 +00:00
|
|
|
|
local spawn_x = nil
|
|
|
|
|
local spawn_y = nil
|
|
|
|
|
|
|
|
|
|
local directions = {up = 0, upleft = 1, left = 2, downleft = 3, down = 4, downright = 5, right = 6, upright = 7}
|
|
|
|
|
local player_x = nil
|
|
|
|
|
local player_y = nil
|
|
|
|
|
local player_direction = nil
|
|
|
|
|
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
-- Cavern generation
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function generateCavern()
|
|
|
|
|
repeat
|
|
|
|
|
cavern = {}
|
|
|
|
|
|
|
|
|
|
cavern.width = 80
|
|
|
|
|
cavern.height = 60
|
|
|
|
|
|
|
|
|
|
-- Create a cavern.width × cavern.height array filled with tiletypes.unknown
|
|
|
|
|
for x = 1, cavern.width do
|
|
|
|
|
local list = {}
|
|
|
|
|
for y = 1, cavern.height do
|
|
|
|
|
table.insert(list, tiletypes.unknown)
|
|
|
|
|
end
|
|
|
|
|
table.insert(cavern, list)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Fill top and bottom edges with walls
|
|
|
|
|
for x = 1, cavern.width do
|
|
|
|
|
cavern[x][1] = tiletypes.wall
|
|
|
|
|
cavern[x][cavern.height] = tiletypes.wall
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Fill the left and right edges with walls
|
|
|
|
|
for y = 1, cavern.height do
|
|
|
|
|
cavern[1][y] = tiletypes.wall
|
|
|
|
|
cavern[cavern.width][y] = tiletypes.wall
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local queue = {}
|
|
|
|
|
local size = 1
|
|
|
|
|
|
|
|
|
|
-- Limit spawn to locations not on the edge walls
|
|
|
|
|
spawn_x = math.random(2, cavern.width - 1)
|
|
|
|
|
spawn_y = math.random(2, cavern.height - 1)
|
|
|
|
|
|
|
|
|
|
cavern[spawn_x][spawn_y] = tiletypes.empty
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = spawn_x, y = spawn_y - 1})
|
|
|
|
|
table.insert(queue, {x = spawn_x, y = spawn_y + 1})
|
|
|
|
|
table.insert(queue, {x = spawn_x - 1, y = spawn_y})
|
|
|
|
|
table.insert(queue, {x = spawn_x + 1, y = spawn_y})
|
|
|
|
|
|
|
|
|
|
repeat
|
2019-06-28 18:53:56 +00:00
|
|
|
|
queue, size = stepCavernGeneration(queue, size)
|
2019-06-28 15:13:03 +00:00
|
|
|
|
until #queue == 0
|
|
|
|
|
until size > 200
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
function stepCavernGeneration(queue, size)
|
2019-06-28 15:13:03 +00:00
|
|
|
|
-- Tuning this parameter will change how open and cavernous vs. closed and corridory the
|
|
|
|
|
-- cavern will be
|
|
|
|
|
local spread_probability = 0.6
|
|
|
|
|
|
|
|
|
|
local new_queue = {}
|
|
|
|
|
local new_size = size
|
|
|
|
|
|
|
|
|
|
for i, location in ipairs(queue) do
|
|
|
|
|
if cavern[location.x][location.y] == tiletypes.unknown then
|
|
|
|
|
-- We haven't covered this tile yet
|
|
|
|
|
|
|
|
|
|
if math.random() < spread_probability then
|
|
|
|
|
-- Empty
|
|
|
|
|
cavern[location.x][location.y] = tiletypes.empty
|
|
|
|
|
size = size + 1
|
|
|
|
|
|
|
|
|
|
-- Up
|
|
|
|
|
if location.y - 1 >= 1 then
|
|
|
|
|
table.insert(new_queue, {x = location.x, y = location.y - 1})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Down
|
|
|
|
|
if location.y + 1 <= cavern.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 <= cavern.width then
|
|
|
|
|
table.insert(new_queue, {x = location.x + 1, y = location.y})
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
-- Wall
|
|
|
|
|
cavern[location.x][location.y] = tiletypes.wall
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return new_queue, size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
-- Spawning objects
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function spawnPlayer()
|
|
|
|
|
player_x = spawn_x
|
|
|
|
|
player_y = spawn_y
|
|
|
|
|
|
|
|
|
|
-- Check what direction we can spawn the player
|
|
|
|
|
if cavern[player_x][player_y + 1] == tiletypes.empty then
|
|
|
|
|
-- There's space under, spawn facing up
|
|
|
|
|
player_direction = directions.up
|
|
|
|
|
elseif cavern[player_x + 1][player_y] == tiletypes.empty then
|
|
|
|
|
-- There's space to the right, spawn facing left
|
|
|
|
|
player_direction = directions.left
|
|
|
|
|
elseif cavern[player_x - 1][player_y] == tiletypes.empty then
|
|
|
|
|
-- There's space to the left, spawn facing right
|
|
|
|
|
player_direction = directions.right
|
|
|
|
|
elseif cavern[player_x][player_y - 1] == tiletypes.empty then
|
|
|
|
|
-- There's space above, spawn facing down
|
|
|
|
|
player_direction = directions.down
|
|
|
|
|
else
|
|
|
|
|
error("Cavern is weirdly generated and player cannot spawn")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
-- Visibility
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function generateVisibilityMap()
|
|
|
|
|
-- Start by creating a new visibility map (since we'll have to recompute everything anyways)
|
|
|
|
|
visibility_map = {}
|
|
|
|
|
for x = 1, cavern.width do
|
|
|
|
|
local list = {}
|
|
|
|
|
for y = 1, cavern.height do
|
|
|
|
|
table.insert(list, false)
|
|
|
|
|
end
|
|
|
|
|
table.insert(visibility_map, list)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Handle visibility from the head
|
|
|
|
|
local queue = {}
|
|
|
|
|
|
2019-06-28 20:35:17 +00:00
|
|
|
|
visibility_map[player_x][player_y] = true
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
-- ⌜^⌝
|
|
|
|
|
-- <o>
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y - 1, direction = directions.upleft})
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y - 1, direction = directions.up})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y - 1, direction = directions.upright})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y, direction = directions.left})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y, direction = directions.right})
|
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
-- ⌜^
|
|
|
|
|
-- <o
|
|
|
|
|
-- ⌞v
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y - 1, direction = directions.upleft})
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y - 1, direction = directions.up})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y, direction = directions.left})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y + 1, direction = directions.downleft})
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y + 1, direction = directions.down})
|
|
|
|
|
elseif player_direction == directions.down then
|
|
|
|
|
-- <o>
|
|
|
|
|
-- ⌞v⌟
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y, direction = directions.left})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y, direction = directions.right})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x - 1, y = player_y + 1, direction = directions.downleft})
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y + 1, direction = directions.down})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y + 1, direction = directions.downright})
|
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
-- ^⌝
|
|
|
|
|
-- o>
|
|
|
|
|
-- v⌟
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y - 1, direction = directions.up})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y - 1, direction = directions.upright})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y, direction = directions.right})
|
|
|
|
|
|
|
|
|
|
table.insert(queue, {x = player_x, y = player_y + 1, direction = directions.down})
|
|
|
|
|
table.insert(queue, {x = player_x + 1, y = player_y + 1, direction = directions.downright})
|
|
|
|
|
else
|
|
|
|
|
error("Player facing an impossible direction")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
repeat
|
|
|
|
|
queue = stepVisibilityMapGeneration(queue)
|
|
|
|
|
until #queue == 0
|
|
|
|
|
|
|
|
|
|
-- Handle "visibility" from the feet
|
|
|
|
|
local body_x, body_y = getBodyLocation()
|
|
|
|
|
|
2019-06-28 20:35:17 +00:00
|
|
|
|
visibility_map[body_x][body_y] = true
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
-- <x>
|
|
|
|
|
-- v
|
|
|
|
|
visibility_map[body_x - 1][body_y] = true
|
|
|
|
|
visibility_map[body_x + 1][body_y] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x][body_y + 1] = true
|
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
-- ^
|
|
|
|
|
-- x>
|
|
|
|
|
-- v
|
|
|
|
|
visibility_map[body_x][body_y - 1] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x + 1][body_y] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x][body_y + 1] = true
|
|
|
|
|
elseif player_direction == directions.down then
|
|
|
|
|
-- ^
|
|
|
|
|
-- <x>
|
|
|
|
|
visibility_map[body_x][body_y - 1] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x - 1][body_y] = true
|
|
|
|
|
visibility_map[body_x + 1][body_y] = true
|
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
-- ^
|
|
|
|
|
-- <x
|
|
|
|
|
-- v
|
|
|
|
|
visibility_map[body_x][body_y - 1] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x - 1][body_y] = true
|
|
|
|
|
|
|
|
|
|
visibility_map[body_x][body_y + 1] = true
|
|
|
|
|
else
|
|
|
|
|
error("Player facing an impossible direction")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function stepVisibilityMapGeneration(queue)
|
|
|
|
|
-- Do a modified floodfill, where each cell will only expand in its given direction, not
|
|
|
|
|
-- every direction like in a normal floodfill
|
|
|
|
|
local new_queue = {}
|
|
|
|
|
|
|
|
|
|
for i, item in ipairs(queue) do
|
|
|
|
|
if cavern[item.x][item.y] == tiletypes.empty then
|
|
|
|
|
-- Did not yet hit a wall
|
|
|
|
|
if item.direction == directions.up then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
visibility_map[item.x][item.y] = true
|
2019-06-28 18:53:56 +00:00
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y - 1, direction = directions.up})
|
|
|
|
|
elseif item.direction == directions.left then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
visibility_map[item.x][item.y] = true
|
2019-06-28 18:53:56 +00:00
|
|
|
|
table.insert(new_queue, {x = item.x - 1, y = item.y, direction = directions.left})
|
|
|
|
|
elseif item.direction == directions.down then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
visibility_map[item.x][item.y] = true
|
2019-06-28 18:53:56 +00:00
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y + 1, direction = directions.down})
|
|
|
|
|
elseif item.direction == directions.right then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
visibility_map[item.x][item.y] = true
|
2019-06-28 18:53:56 +00:00
|
|
|
|
table.insert(new_queue, {x = item.x + 1, y = item.y, direction = directions.right})
|
|
|
|
|
elseif item.direction == directions.upleft then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
-- Check that we could have gotten here also by moving only 4-ways
|
|
|
|
|
--
|
|
|
|
|
-- The possible cases are:
|
|
|
|
|
-- (o represents old position, x is new, and # is a wall)
|
|
|
|
|
--
|
|
|
|
|
-- x x# x x#
|
|
|
|
|
-- o o #o #o
|
|
|
|
|
--
|
|
|
|
|
-- In the first three, the movement is possible, but in the last one
|
|
|
|
|
-- it is not. We can therefore check whether the movement is valid
|
|
|
|
|
-- by testing if either the cell to our right or below us (the
|
|
|
|
|
-- directions opposite to the ones in an up.left movement) is empty
|
|
|
|
|
if cavern[item.x + 1][item.y] == tiletypes.empty or cavern[item.x][item.y + 1] == tiletypes.empty then
|
|
|
|
|
-- ⌜^
|
|
|
|
|
-- ⌜ <x
|
|
|
|
|
visibility_map[item.x][item.y] = true
|
|
|
|
|
table.insert(new_queue, {x = item.x - 1, y = item.y - 1, direction = directions.upleft})
|
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y - 1, direction = directions.up})
|
|
|
|
|
table.insert(new_queue, {x = item.x - 1, y = item.y, direction = directions.left})
|
|
|
|
|
end
|
2019-06-28 18:53:56 +00:00
|
|
|
|
elseif item.direction == directions.downleft then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
-- See under item.direction == directions.upleft
|
|
|
|
|
if cavern[item.x + 1][item.y] == tiletypes.empty or cavern[item.x][item.y - 1] == tiletypes.empty then
|
|
|
|
|
-- ⌞ <x
|
|
|
|
|
-- ⌞v
|
|
|
|
|
visibility_map[item.x][item.y] = true
|
|
|
|
|
table.insert(new_queue, {x = item.x - 1, y = item.y, direction = directions.left})
|
|
|
|
|
table.insert(new_queue, {x = item.x - 1, y = item.y + 1, direction = directions.downleft})
|
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y + 1, direction = directions.down})
|
|
|
|
|
end
|
2019-06-28 18:53:56 +00:00
|
|
|
|
elseif item.direction == directions.downright then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
-- See under item.direction == directions.upleft
|
|
|
|
|
if cavern[item.x - 1][item.y] == tiletypes.empty or cavern[item.x][item.y - 1] == tiletypes.empty then
|
|
|
|
|
-- ⌟ x>
|
|
|
|
|
-- v⌟
|
|
|
|
|
visibility_map[item.x][item.y] = true
|
|
|
|
|
table.insert(new_queue, {x = item.x + 1, y = item.y, direction = directions.right})
|
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y + 1, direction = directions.down})
|
|
|
|
|
table.insert(new_queue, {x = item.x + 1, y = item.y + 1, direction = directions.downright})
|
|
|
|
|
end
|
2019-06-28 18:53:56 +00:00
|
|
|
|
elseif item.direction == directions.upright then
|
2019-06-28 20:59:41 +00:00
|
|
|
|
-- See under item.direction == directions.upleft
|
|
|
|
|
if cavern[item.x - 1][item.y] == tiletypes.empty or cavern[item.x][item.y + 1] == tiletypes.empty then
|
|
|
|
|
-- ^⌝
|
|
|
|
|
-- ⌝ x>
|
|
|
|
|
visibility_map[item.x][item.y] = true
|
|
|
|
|
table.insert(new_queue, {x = item.x, y = item.y - 1, direction = directions.up})
|
|
|
|
|
table.insert(new_queue, {x = item.x + 1, y = item.y - 1, direction = directions.upright})
|
|
|
|
|
table.insert(new_queue, {x = item.x + 1, y = item.y, direction = directions.right})
|
|
|
|
|
end
|
2019-06-28 18:53:56 +00:00
|
|
|
|
else
|
|
|
|
|
error("Visibility floodfill item travelling in an impossible direction")
|
|
|
|
|
end
|
2019-06-28 20:59:41 +00:00
|
|
|
|
else
|
|
|
|
|
-- Hit a wall
|
|
|
|
|
if item.direction == directions.up or
|
|
|
|
|
item.direction == directions.left or
|
|
|
|
|
item.direction == directions.down or
|
|
|
|
|
item.direction == directions.right then
|
|
|
|
|
-- Only draw it as visible if we reached it with a non-diagonal move
|
|
|
|
|
-- because diagonals might have been non-valid moves
|
|
|
|
|
visibility_map[item.x][item.y] = true
|
|
|
|
|
end
|
2019-06-28 18:53:56 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return new_queue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function initializeRememberedCavern()
|
|
|
|
|
remembered_cavern = {}
|
|
|
|
|
for x = 1, cavern.width do
|
|
|
|
|
local list = {}
|
|
|
|
|
for y = 1, cavern.height do
|
|
|
|
|
table.insert(list, nil)
|
|
|
|
|
end
|
|
|
|
|
table.insert(remembered_cavern, list)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function rememberVisible()
|
|
|
|
|
for x, list in ipairs(visibility_map) do
|
|
|
|
|
for y, visible in ipairs(list) do
|
|
|
|
|
if visible then
|
|
|
|
|
remembered_cavern[x][y] = cavern[x][y]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-28 15:13:03 +00:00
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
-- Player helper functions
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function getBodyLocation(x, y, facing)
|
|
|
|
|
if x == nil then x = player_x end
|
|
|
|
|
if y == nil then y = player_y end
|
|
|
|
|
if facing == nil then facing = player_direction end
|
|
|
|
|
|
|
|
|
|
if facing == directions.up then
|
|
|
|
|
-- Facing up, body is down
|
|
|
|
|
return x, y + 1
|
|
|
|
|
elseif facing == directions.left then
|
|
|
|
|
-- Facing left, body is right
|
|
|
|
|
return x + 1, y
|
|
|
|
|
elseif facing == directions.down then
|
|
|
|
|
-- Facing down, body is up
|
|
|
|
|
return x, y - 1
|
|
|
|
|
elseif facing == directions.right then
|
|
|
|
|
-- Facing right, body is left
|
|
|
|
|
return x - 1, y
|
|
|
|
|
else
|
|
|
|
|
error("Player facing an impossible direction")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function movePlayer(direction)
|
|
|
|
|
local dx = 0
|
|
|
|
|
local dy = 0
|
|
|
|
|
local new_direction = direction
|
|
|
|
|
|
|
|
|
|
-- If we are moving backwards, keep the old direction of facing
|
|
|
|
|
if player_direction == directions.up and direction == directions.down or
|
|
|
|
|
player_direction == directions.down and direction == directions.up or
|
|
|
|
|
player_direction == directions.left and direction == directions.right or
|
|
|
|
|
player_direction == directions.right and direction == directions.left then
|
|
|
|
|
new_direction = player_direction
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if direction == directions.up then
|
|
|
|
|
dy = -1
|
|
|
|
|
elseif direction == directions.down then
|
|
|
|
|
dy = 1
|
|
|
|
|
elseif direction == directions.left then
|
|
|
|
|
dx = -1
|
|
|
|
|
elseif direction == directions.right then
|
|
|
|
|
dx = 1
|
|
|
|
|
elseif direction == directions.upleft then
|
2019-06-28 17:31:03 +00:00
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
dx = -1
|
|
|
|
|
new_direction = directions.left
|
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
dy = -1
|
|
|
|
|
new_direction = directions.up
|
|
|
|
|
elseif player_direction == directions.down then
|
2019-06-28 15:13:03 +00:00
|
|
|
|
-- ## ##
|
|
|
|
|
-- x# xo#
|
|
|
|
|
-- #o# # #
|
|
|
|
|
dy = -1
|
|
|
|
|
new_direction = directions.right
|
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
-- # # #x#
|
|
|
|
|
-- #xo #o
|
|
|
|
|
-- ## ##
|
|
|
|
|
dx = -1
|
|
|
|
|
new_direction = directions.down
|
|
|
|
|
else
|
2019-06-28 18:53:56 +00:00
|
|
|
|
error("Player facing an impossible direction")
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
elseif direction == directions.downleft then
|
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
-- #o# # #
|
|
|
|
|
-- x# xo#
|
|
|
|
|
-- ## ##
|
|
|
|
|
dy = 1
|
|
|
|
|
new_direction = directions.right
|
2019-06-28 17:31:03 +00:00
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
dy = 1
|
|
|
|
|
new_direction = directions.down
|
|
|
|
|
elseif player_direction == directions.down then
|
|
|
|
|
dx = -1
|
|
|
|
|
new_direction = directions.left
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
-- ## ##
|
|
|
|
|
-- #xo #o
|
|
|
|
|
-- # # #x#
|
|
|
|
|
dx = -1
|
|
|
|
|
new_direction = directions.up
|
|
|
|
|
else
|
2019-06-28 18:53:56 +00:00
|
|
|
|
error("Player facing an impossible direction")
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
elseif direction == directions.downright then
|
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
-- #o# # #
|
|
|
|
|
-- #x #ox
|
|
|
|
|
-- ## ##
|
|
|
|
|
dy = 1
|
|
|
|
|
new_direction = directions.left
|
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
-- ## ##
|
|
|
|
|
-- ox# o#
|
|
|
|
|
-- # # #x#
|
|
|
|
|
dx = 1
|
|
|
|
|
new_direction = directions.up
|
2019-06-28 17:31:03 +00:00
|
|
|
|
elseif player_direction == directions.down then
|
|
|
|
|
dx = 1
|
|
|
|
|
new_direction = directions.right
|
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
dy = 1
|
|
|
|
|
new_direction = directions.down
|
2019-06-28 15:13:03 +00:00
|
|
|
|
else
|
2019-06-28 18:53:56 +00:00
|
|
|
|
error("Player facing an impossible direction")
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
elseif direction == directions.upright then
|
2019-06-28 17:31:03 +00:00
|
|
|
|
if player_direction == directions.up then
|
|
|
|
|
dx = 1
|
|
|
|
|
new_direction = directions.right
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif player_direction == directions.left then
|
|
|
|
|
-- # # #x#
|
|
|
|
|
-- ox# o#
|
|
|
|
|
-- ## ##
|
|
|
|
|
dx = 1
|
|
|
|
|
new_direction = directions.down
|
2019-06-28 17:31:03 +00:00
|
|
|
|
elseif player_direction == directions.down then
|
|
|
|
|
-- ## ##
|
|
|
|
|
-- #x #ox
|
|
|
|
|
-- #o# # #
|
|
|
|
|
dy = -1
|
|
|
|
|
new_direction = directions.left
|
|
|
|
|
elseif player_direction == directions.right then
|
|
|
|
|
dy = -1
|
|
|
|
|
new_direction = directions.up
|
2019-06-28 15:13:03 +00:00
|
|
|
|
else
|
2019-06-28 18:53:56 +00:00
|
|
|
|
error("Player facing an impossible direction")
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
error("Player moving in an impossible direction")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local body_x, body_y = getBodyLocation(player_x + dx, player_y + dy, new_direction)
|
|
|
|
|
|
|
|
|
|
if cavern[player_x + dx][player_y + dy] == tiletypes.empty and
|
|
|
|
|
cavern[body_x][body_y] == tiletypes.empty then
|
|
|
|
|
player_x = player_x + dx
|
|
|
|
|
player_y = player_y + dy
|
|
|
|
|
player_direction = new_direction
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
-- Callbacks
|
|
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
function love.load()
|
|
|
|
|
math.randomseed(os.time())
|
|
|
|
|
|
|
|
|
|
generateCavern()
|
|
|
|
|
spawnPlayer()
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
|
|
|
|
initializeRememberedCavern()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function love.update(dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function love.keypressed(key)
|
|
|
|
|
if key == 'r' then
|
|
|
|
|
generateCavern()
|
|
|
|
|
spawnPlayer()
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
|
|
|
|
initializeRememberedCavern()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'i' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.up)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'j' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.left)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'k' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.down)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'l' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.right)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'u' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.upleft)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'm' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.downleft)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == '.' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.downright)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'o' then
|
2019-06-28 18:53:56 +00:00
|
|
|
|
rememberVisible()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
movePlayer(directions.upright)
|
2019-06-28 18:53:56 +00:00
|
|
|
|
generateVisibilityMap()
|
2019-06-28 15:13:03 +00:00
|
|
|
|
elseif key == 'q' then
|
|
|
|
|
love.event.quit()
|
|
|
|
|
else
|
|
|
|
|
display_inputinfo_counter = 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function love.draw()
|
|
|
|
|
local x_scale = love.graphics.getWidth() / cavern.width
|
|
|
|
|
local y_scale = love.graphics.getHeight() / cavern.height
|
|
|
|
|
|
|
|
|
|
-- Draw the cavern
|
2019-06-28 18:53:56 +00:00
|
|
|
|
for tile_x, list in ipairs(visibility_map) do
|
2019-06-28 15:13:03 +00:00
|
|
|
|
local x = (tile_x - 1) * x_scale
|
2019-06-28 18:53:56 +00:00
|
|
|
|
for tile_y, visible in ipairs(list) do
|
2019-06-28 15:13:03 +00:00
|
|
|
|
local y = (tile_y - 1) * y_scale
|
|
|
|
|
|
2019-06-28 18:53:56 +00:00
|
|
|
|
if visible then
|
|
|
|
|
tile = cavern[tile_x][tile_y]
|
|
|
|
|
|
|
|
|
|
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
|
2019-06-28 15:13:03 +00:00
|
|
|
|
else
|
2019-06-28 18:53:56 +00:00
|
|
|
|
tile = remembered_cavern[tile_x][tile_y]
|
|
|
|
|
if tile == tiletypes.empty then
|
|
|
|
|
love.graphics.setColor(0.2, 0.2, 0.2)
|
|
|
|
|
elseif tile == tiletypes.wall then
|
|
|
|
|
love.graphics.setColor(0.8, 0.8, 0.8)
|
|
|
|
|
else
|
|
|
|
|
love.graphics.setColor(0.3, 0.3, 0.3)
|
|
|
|
|
end
|
2019-06-28 15:13:03 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
love.graphics.rectangle('fill', x, y, x_scale, y_scale)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Draw the player
|
|
|
|
|
local player_head_center_x = (player_x - 1) * x_scale + 0.5 * x_scale
|
|
|
|
|
local player_head_center_y = (player_y - 1) * y_scale + 0.5 * y_scale
|
|
|
|
|
love.graphics.setColor(0.7, 0.3, 0.3)
|
|
|
|
|
love.graphics.ellipse('fill', player_head_center_x, player_head_center_y, x_scale/2, y_scale/2)
|
|
|
|
|
|
|
|
|
|
local player_body_x, player_body_y = getBodyLocation()
|
|
|
|
|
local player_body_x = (player_body_x - 1) * x_scale
|
|
|
|
|
local player_body_y = (player_body_y - 1) * y_scale
|
|
|
|
|
love.graphics.setColor(0, 0, 1)
|
|
|
|
|
love.graphics.rectangle('fill', player_body_x, player_body_y, x_scale, y_scale)
|
|
|
|
|
end
|