2017-12-11 20:07:05 +01:00
|
|
|
local room_builder = require "data/maproombuilder"
|
2017-12-13 23:20:54 +01:00
|
|
|
local monster_gen = require "data/monstergen"
|
2017-12-06 16:02:50 +01:00
|
|
|
|
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
|
2017-12-07 23:58:29 +01:00
|
|
|
local UP = 1
|
|
|
|
local LEFT = 2
|
|
|
|
local RIGHT = 3
|
|
|
|
local DOWN = 4
|
2017-12-06 11:44:17 +01:00
|
|
|
|
|
|
|
-- BEGIN FUNCTIONS
|
2017-12-07 23:58:29 +01:00
|
|
|
local function matrix_coverage (matrix)
|
2017-12-06 11:44:17 +01:00
|
|
|
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-07 23:58:29 +01:00
|
|
|
local function reverse_direction(dir)
|
2017-12-06 16:02:50 +01:00
|
|
|
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
|
|
|
|
|
2017-12-07 23:58:29 +01:00
|
|
|
local function generate_path ()
|
2017-12-06 11:44:17 +01:00
|
|
|
local map_matrix = {}
|
|
|
|
for i=1,10 do
|
|
|
|
map_matrix[i] = {}
|
|
|
|
for j=1,10 do
|
2017-12-11 20:07:05 +01:00
|
|
|
map_matrix[i][j] = room_builder.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)
|
2017-12-07 23:58:29 +01:00
|
|
|
local direction = 0
|
|
|
|
local lastDirection = 0
|
|
|
|
local coridoor_count = 0
|
|
|
|
local coverage = 10
|
|
|
|
|
|
|
|
while matrix_coverage(map_matrix) < coverage do
|
2017-12-06 11:44:17 +01:00
|
|
|
local direction = random(4)
|
|
|
|
|
|
|
|
if lastDirection > 0 then
|
2017-12-07 23:58:29 +01:00
|
|
|
if random(24) <= 6 then direction = lastDirection end
|
|
|
|
end
|
|
|
|
|
|
|
|
while lastDirection == reverse_direction(direction) do
|
|
|
|
direction = random(4)
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].active = true
|
2017-12-07 23:58:29 +01:00
|
|
|
|
|
|
|
if coridoor_count < coverage/3 then
|
|
|
|
if random(3) == 1 and (cx > 1 or cy > 1) then
|
|
|
|
map_matrix[cx][cy].type = "coridoor"
|
|
|
|
coridoor_count = coridoor_count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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-07 23:58:29 +01:00
|
|
|
|
|
|
|
-- Last room rules
|
|
|
|
map_matrix[cx][cy].active = true
|
|
|
|
map_matrix[cx][cy].goal = true
|
|
|
|
map_matrix[cx][cy].type = "room"
|
2017-12-06 11:44:17 +01:00
|
|
|
|
|
|
|
return map_matrix;
|
|
|
|
end
|
2017-12-07 23:58:29 +01:00
|
|
|
|
|
|
|
local function print_matrix(matrix)
|
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
|
|
|
if not map_matrix[j][i].goal then
|
|
|
|
io.write(map_matrix[j][i].path_dir .. " ")
|
|
|
|
else
|
|
|
|
io.write("G ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
io.write("\n")
|
|
|
|
end
|
|
|
|
end
|
2017-12-06 11:44:17 +01:00
|
|
|
-- 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-11 20:07:05 +01:00
|
|
|
room_builder.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]
|
2017-12-07 23:58:29 +01:00
|
|
|
-- print_matrix(map_matrix)
|
2017-12-06 11:44:17 +01:00
|
|
|
|
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
2017-12-07 23:58:29 +01:00
|
|
|
local room = map_matrix[i][j]
|
|
|
|
if room.active then
|
2017-12-06 11:44:17 +01:00
|
|
|
set_current_room(map, i-1, j-1);
|
2017-12-07 23:58:29 +01:00
|
|
|
if room.type == "room" then
|
2017-12-11 20:07:05 +01:00
|
|
|
room_builder.build_square_room(map, room)
|
2017-12-14 09:48:49 +01:00
|
|
|
monster_gen.add_monster_to_room(map, i-1, j-1);
|
2017-12-07 23:58:29 +01:00
|
|
|
elseif room.type == "coridoor" then
|
2017-12-11 20:07:05 +01:00
|
|
|
room_builder.build_coridoor_room(map, room)
|
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
|