Attemptive lua structuring and more mapgeneration code

This commit is contained in:
Linus Probert 2017-12-06 16:02:50 +01:00
parent e8ccda2faf
commit b950673952
2 changed files with 157 additions and 108 deletions

View File

@ -1,3 +1,5 @@
require "data/maproombuilder"
-- Setting up some functions -- Setting up some functions
local time = os.time local time = os.time
local random = math.random local random = math.random
@ -10,67 +12,22 @@ RIGHT = 3
DOWN = 4 DOWN = 4
-- BEGIN FUNCTIONS -- BEGIN FUNCTIONS
function add_tiles_to_room (map, texture)
for i=0,15 do
for j=0,11 do
if (i >= 1 and i <= 14 and j >= 1 and j <= 10) then
if (i == 1 and j == 1) then
add_tile(map, i, j, texture, 0, 48, false)
elseif (i == 1 and j == 10) then
add_tile(map, i, j, texture, 0, 80, false)
elseif (i == 14 and j == 1) then
add_tile(map, i, j, texture, 32, 48, false)
elseif (i == 14 and j == 10) then
add_tile(map, i, j, texture, 32, 80, false)
elseif (i == 1) then
add_tile(map, i, j, texture, 0, 64, false)
elseif (j == 1) then
add_tile(map, i, j, texture, 16, 48, false)
elseif (i == 14) then
add_tile(map, i, j, texture, 32, 64, false)
elseif (j == 10) then
add_tile(map, i, j, texture, 16, 80, false)
else
add_tile(map, i, j, texture, 16, 64, false)
end
end
end
end
end
function add_walls_to_room (map, texture)
for i=0,15 do
for j=0,11 do
if (i == 0 and j == 0) then
add_tile(map, i, j, texture, 0, 48, true)
elseif (i == 15 and j == 0) then
add_tile(map, i, j, texture, 32, 48, true)
elseif (i == 0 and j == 11) then
add_tile(map, i, j, texture, 0, 80, true)
elseif (i == 15 and j == 11) then
add_tile(map, i, j, texture, 32, 80, true)
elseif (i == 0 or i == 15) then
add_tile(map, i, j, texture, 0, 64, true)
elseif (j == 0 or j == 11) then
add_tile(map, i, j, texture, 16, 48, true)
end
end
end
end
function matrix_coverage (matrix) function matrix_coverage (matrix)
local cov = 0 local cov = 0
for i=1,10 do for i=1,10 do
for j=1,10 do for j=1,10 do
if matrix[i][j] > 0 then cov = cov + 1 end if matrix[i][j].active then cov = cov + 1 end
end end
end end
return cov return cov
end end
function create_room () function reverse_direction(dir)
local room = {} if dir == UP then return DOWN
room.exits = {} elseif dir == DOWN then return UP
elseif dir == LEFT then return RIGHT
elseif dir == RIGHT then return LEFT
end
end end
function generate_path () function generate_path ()
@ -78,7 +35,7 @@ function generate_path ()
for i=1,10 do for i=1,10 do
map_matrix[i] = {} map_matrix[i] = {}
for j=1,10 do for j=1,10 do
map_matrix[i][j] = 0 map_matrix[i][j] = create_room()
end end
end end
@ -88,99 +45,71 @@ function generate_path ()
randomseed(seed) randomseed(seed)
local direction = 0; local direction = 0;
local lastDirection = 0; local lastDirection = 0;
while matrix_coverage(map_matrix) < 30 do while matrix_coverage(map_matrix) < 10 do
local direction = random(4) local direction = random(4)
if lastDirection > 0 then if lastDirection > 0 then
if random(24) <= 8 then direction = lastDirection end if random(24) <= 8 then direction = lastDirection end
end end
map_matrix[cx][cy].active = true
if direction == UP and cy > 1 then -- UP if direction == UP and cy > 1 then -- UP
map_matrix[cx][cy] = direction table.insert(map_matrix[cx][cy].exits, direction)
map_matrix[cx][cy].path_dir = direction
cy = cy - 1; cy = cy - 1;
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
elseif direction == LEFT and cx > 1 then -- LEFT elseif direction == LEFT and cx > 1 then -- LEFT
map_matrix[cx][cy] = direction table.insert(map_matrix[cx][cy].exits, direction)
map_matrix[cx][cy].path_dir = direction
cx = cx - 1; cx = cx - 1;
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
elseif direction == RIGHT and cx < 10 then -- RIGHT elseif direction == RIGHT and cx < 10 then -- RIGHT
map_matrix[cx][cy] = direction table.insert(map_matrix[cx][cy].exits, direction)
map_matrix[cx][cy].path_dir = direction
cx = cx + 1; cx = cx + 1;
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
elseif direction == DOWN and cy < 10 then -- DOWN elseif direction == DOWN and cy < 10 then -- DOWN
map_matrix[cx][cy] = direction table.insert(map_matrix[cx][cy].exits, direction)
map_matrix[cx][cy].path_dir = direction
cy = cy + 1; cy = cy + 1;
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
end end
lastDirection = direction lastDirection = direction
end end
map_matrix[cx][cy] = 5 -- The final room map_matrix[cx][cy].active = true -- Last room
map_matrix[cx][cy].goal = true -- Last room
return map_matrix; return map_matrix;
end end
function add_exit(map, direction, floorTexture, wallTexture)
if direction > 4 then return end
if direction == UP then
add_tile(map, 7, 0, floorTexture, 16, 64, false)
elseif direction == LEFT then
add_tile(map, 0, 5, floorTexture, 16, 64, false)
elseif direction == RIGHT then
add_tile(map, 15, 5, floorTexture, 16, 64, false)
elseif direction == DOWN then
add_tile(map, 7, 11, floorTexture, 16, 64, false)
end
end
function add_reverse_exit(map, direction, floorTexture, wallTexture)
local reverseDirection
if direction == UP then reverseDirection = DOWN
elseif direction == DOWN then reverseDirection = UP
elseif direction == LEFT then reverseDirection = RIGHT
elseif direction == RIGHT then reverseDirection = LEFT
else reverseDirection = 5 end
add_exit(map, reverseDirection, floorTexture, wallTexture)
end
-- END FUNCTIONS -- END FUNCTIONS
-- BEGIN SCRIPT -- BEGIN SCRIPT
map = create_map() -- 'map' needs to be global map = create_map() -- 'map' needs to be global
local floorTexture = add_texture(map, "assets/Objects/Floor.png") load_textures(map)
local wallTexture = add_texture(map, "assets/Objects/Wall.png")
local map_matrix = generate_path() local map_matrix = generate_path()
-- Print path [Debug] -- Print path [Debug]
for i=1,10 do for i=1,10 do
for j=1,10 do for j=1,10 do
io.write(map_matrix[j][i] .. " ") if not map_matrix[j][i].goal then
io.write(map_matrix[j][i].path_dir .. " ")
else
io.write("G ")
end
end end
io.write("\n") io.write("\n")
end end
for i=1,10 do for i=1,10 do
for j=1,10 do for j=1,10 do
if map_matrix[i][j] > 0 then if map_matrix[i][j].active then
set_current_room(map, i-1, j-1); set_current_room(map, i-1, j-1);
add_tiles_to_room(map, floorTexture); add_tiles_to_room(map);
add_walls_to_room(map, wallTexture); add_walls_to_room(map);
for exit=1, #map_matrix[i][j].exits do
add_exit(map, map_matrix[i][j].exits[exit]);
end end
end end
end
for i=1,10 do
for j=1,10 do
if map_matrix[i][j] > 0 then
set_current_room(map, i-1, j-1);
add_exit(map, map_matrix[i][j], floorTexture, wallTexture);
if map_matrix[i][j] == UP then
set_current_room(map, i-1, j-2)
elseif map_matrix[i][j] == LEFT then
set_current_room(map, i-2, j-1)
elseif map_matrix[i][j] == RIGHT then
set_current_room(map, i, j-1)
elseif map_matrix[i][j] == DOWN then
set_current_room(map, i-1, j)
end
add_reverse_exit(map, map_matrix[i][j], floorTexture, wallTexture);
end
end end
end end
-- END SCRIPT -- END SCRIPT

120
data/maproombuilder.lua Normal file
View File

@ -0,0 +1,120 @@
-- Textures
local floorTexture = nil
local wallTexture = nil
local floor_center
local floor_top
local floor_bottom
local floor_left
local floor_right
local floor_topleft
local floor_topright
local floor_bottomleft
local floor_bottomright
local wall_topleft
local wall_topright
local wall_bottomleft
local wall_bottomright
local wall_vertical
local wall_horizontal
function load_textures(map)
floorTexture = add_texture(map, "assets/Objects/Floor.png")
wallTexture = add_texture(map, "assets/Objects/Wall.png")
local xo = 0
local yo = 48
floor_center = { floorTexture, xo + 16, yo + 16, false }
floor_top = { floorTexture, xo + 16, yo + 0, false }
floor_bottom = { floorTexture, xo + 16, yo + 32, false }
floor_left = { floorTexture, xo + 0, yo + 16, false }
floor_right = { floorTexture, xo + 32, yo + 16, false }
floor_topleft = { floorTexture, xo + 0, yo + 0, false }
floor_topright = { floorTexture, xo + 32, yo + 0, false }
floor_bottomleft = { floorTexture, xo + 0, yo + 32, false }
floor_bottomright = { floorTexture, xo + 32, yo + 32, false }
wall_topleft = { wallTexture, xo + 0, yo + 0, false }
wall_topright = { wallTexture, xo + 32, yo + 0, false }
wall_bottomleft = { wallTexture, xo + 0, yo + 32, false }
wall_bottomright = { wallTexture, xo + 32, yo + 32, false }
wall_vertical = { wallTexture, xo + 0, yo + 16, false }
wall_horizontal = { wallTexture, xo + 16, yo + 0, false }
end
function create_room ()
local room = {}
room.exits = {}
room.active = false
room.goal = false
room.path_dir = 0
return room;
end
function add_tiles_to_room (map)
for i=0,15 do
for j=0,11 do
if (i >= 1 and i <= 14 and j >= 1 and j <= 10) then
if (i == 1 and j == 1) then
add_tile(map, i, j, unpack(floor_topleft))
elseif (i == 14 and j == 1) then
add_tile(map, i, j, unpack(floor_topright))
elseif (i == 1 and j == 10) then
add_tile(map, i, j, unpack(floor_bottomleft))
elseif (i == 14 and j == 10) then
add_tile(map, i, j, unpack(floor_bottomright))
elseif (i == 1) then
add_tile(map, i, j, unpack(floor_left))
elseif (i == 14) then
add_tile(map, i, j, unpack(floor_right))
elseif (j == 1) then
add_tile(map, i, j, unpack(floor_top))
elseif (j == 10) then
add_tile(map, i, j, unpack(floor_bottom))
else
add_tile(map, i, j, unpack(floor_center))
end
end
end
end
end
function add_walls_to_room (map)
for i=0,15 do
for j=0,11 do
if (i == 0 and j == 0) then
add_tile(map, i, j, unpack(wall_topleft))
elseif (i == 15 and j == 0) then
add_tile(map, i, j, unpack(wall_topright))
elseif (i == 0 and j == 11) then
add_tile(map, i, j, unpack(wall_bottomleft))
elseif (i == 15 and j == 11) then
add_tile(map, i, j, unpack(wall_bottomright))
elseif (i == 0 or i == 15) then
add_tile(map, i, j, unpack(wall_vertical))
elseif (j == 0 or j == 11) then
add_tile(map, i, j, unpack(wall_horizontal))
end
end
end
end
function add_exit(map, direction)
if direction > 4 then return end
if direction == UP then
add_tile(map, 7, 0, unpack(floor_center))
add_tile(map, 8, 0, unpack(floor_center))
elseif direction == LEFT then
add_tile(map, 0, 5, unpack(floor_center))
add_tile(map, 0, 6, unpack(floor_center))
elseif direction == RIGHT then
add_tile(map, 15, 5, unpack(floor_center))
add_tile(map, 15, 6, unpack(floor_center))
elseif direction == DOWN then
add_tile(map, 7, 11, unpack(floor_center))
add_tile(map, 8, 11, unpack(floor_center))
end
end