2017-12-06 16:02:50 +01:00
|
|
|
require "data/maproombuilder"
|
|
|
|
|
2017-12-06 11:44:17 +01:00
|
|
|
-- Setting up some functions
|
|
|
|
local time = os.time
|
|
|
|
local random = math.random
|
|
|
|
local randomseed = math.randomseed
|
|
|
|
|
|
|
|
-- CONSTANTS
|
|
|
|
UP = 1
|
|
|
|
LEFT = 2
|
|
|
|
RIGHT = 3
|
|
|
|
DOWN = 4
|
|
|
|
|
|
|
|
-- BEGIN FUNCTIONS
|
|
|
|
function matrix_coverage (matrix)
|
|
|
|
local cov = 0
|
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
2017-12-06 16:02:50 +01:00
|
|
|
if matrix[i][j].active then cov = cov + 1 end
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return cov
|
|
|
|
end
|
|
|
|
|
2017-12-06 16:02:50 +01:00
|
|
|
function reverse_direction(dir)
|
|
|
|
if dir == UP then return DOWN
|
|
|
|
elseif dir == DOWN then return UP
|
|
|
|
elseif dir == LEFT then return RIGHT
|
|
|
|
elseif dir == RIGHT then return LEFT
|
|
|
|
end
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function generate_path ()
|
|
|
|
local map_matrix = {}
|
|
|
|
for i=1,10 do
|
|
|
|
map_matrix[i] = {}
|
|
|
|
for j=1,10 do
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[i][j] = create_room()
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local cx, cy = 1, 1
|
|
|
|
local seed = time();
|
|
|
|
print("[**] Map generation seed: " .. seed)
|
|
|
|
randomseed(seed)
|
|
|
|
local direction = 0;
|
|
|
|
local lastDirection = 0;
|
2017-12-06 16:02:50 +01:00
|
|
|
while matrix_coverage(map_matrix) < 10 do
|
2017-12-06 11:44:17 +01:00
|
|
|
local direction = random(4)
|
|
|
|
|
|
|
|
if lastDirection > 0 then
|
|
|
|
if random(24) <= 8 then direction = lastDirection end
|
|
|
|
end
|
|
|
|
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].active = true
|
2017-12-06 11:44:17 +01:00
|
|
|
if direction == UP and cy > 1 then -- UP
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, direction)
|
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2017-12-06 11:44:17 +01:00
|
|
|
cy = cy - 1;
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == LEFT and cx > 1 then -- LEFT
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, direction)
|
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2017-12-06 11:44:17 +01:00
|
|
|
cx = cx - 1;
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == RIGHT and cx < 10 then -- RIGHT
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, direction)
|
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2017-12-06 11:44:17 +01:00
|
|
|
cx = cx + 1;
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == DOWN and cy < 10 then -- DOWN
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, direction)
|
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2017-12-06 11:44:17 +01:00
|
|
|
cy = cy + 1;
|
2017-12-06 16:02:50 +01:00
|
|
|
table.insert(map_matrix[cx][cy].exits, reverse_direction(direction))
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
lastDirection = direction
|
|
|
|
end
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].active = true -- Last room
|
|
|
|
map_matrix[cx][cy].goal = true -- Last room
|
2017-12-06 11:44:17 +01:00
|
|
|
|
|
|
|
return map_matrix;
|
|
|
|
end
|
|
|
|
-- END FUNCTIONS
|
2017-12-02 23:32:40 +01:00
|
|
|
|
2017-12-06 11:44:17 +01:00
|
|
|
-- BEGIN SCRIPT
|
|
|
|
map = create_map() -- 'map' needs to be global
|
2017-12-06 16:02:50 +01:00
|
|
|
load_textures(map)
|
2017-12-06 11:44:17 +01:00
|
|
|
local map_matrix = generate_path()
|
2017-12-02 23:32:40 +01:00
|
|
|
|
2017-12-06 11:44:17 +01:00
|
|
|
-- Print path [Debug]
|
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
2017-12-06 16:02:50 +01:00
|
|
|
if not map_matrix[j][i].goal then
|
|
|
|
io.write(map_matrix[j][i].path_dir .. " ")
|
|
|
|
else
|
|
|
|
io.write("G ")
|
|
|
|
end
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
io.write("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
2017-12-06 16:02:50 +01:00
|
|
|
if map_matrix[i][j].active then
|
2017-12-06 11:44:17 +01:00
|
|
|
set_current_room(map, i-1, j-1);
|
2017-12-06 16:02:50 +01:00
|
|
|
add_tiles_to_room(map);
|
|
|
|
add_walls_to_room(map);
|
|
|
|
for exit=1, #map_matrix[i][j].exits do
|
|
|
|
add_exit(map, map_matrix[i][j].exits[exit]);
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-02 23:32:40 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-06 11:44:17 +01:00
|
|
|
-- END SCRIPT
|