2018-02-27 19:59:51 +01:00
|
|
|
local room_builder = require "maproombuilder"
|
|
|
|
local monster_gen = require "monstergen"
|
2018-08-06 00:28:23 +02:00
|
|
|
local trap_gen = require "trapgen"
|
2018-08-10 20:09:56 +02:00
|
|
|
local chest_gen = require "chestgen"
|
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
|
2018-07-31 14:25:29 +02:00
|
|
|
if matrix[i][j] 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
|
2018-07-31 14:25:29 +02:00
|
|
|
map_matrix[i][j] = nil
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local cx, cy = 1, 1
|
|
|
|
local seed = time();
|
2018-02-01 09:55:12 +01:00
|
|
|
info("Map generation seed: " .. seed)
|
2017-12-06 11:44:17 +01:00
|
|
|
randomseed(seed)
|
2017-12-07 23:58:29 +01:00
|
|
|
local direction = 0
|
|
|
|
local lastDirection = 0
|
|
|
|
local coridoor_count = 0
|
2018-08-15 07:19:44 +02:00
|
|
|
local bossLevel = CURRENT_LEVEL % 5 == 0
|
2018-07-31 14:25:29 +02:00
|
|
|
local coverage = 9 + CURRENT_LEVEL
|
2018-08-15 07:19:44 +02:00
|
|
|
if bossLevel then
|
|
|
|
coverage = 5
|
|
|
|
end
|
2018-07-31 14:25:29 +02:00
|
|
|
|
|
|
|
-- Create the first room
|
|
|
|
map_matrix[cx][cy] = room_builder.create_empty_room()
|
2017-12-07 23:58:29 +01:00
|
|
|
|
|
|
|
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-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
|
|
|
|
|
2018-07-31 14:25:29 +02:00
|
|
|
valid_direction = false
|
2017-12-06 11:44:17 +01:00
|
|
|
if direction == UP and cy > 1 then -- UP
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.add_exit(map_matrix[cx][cy], direction)
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2018-07-31 14:25:29 +02:00
|
|
|
cy = cy - 1
|
|
|
|
valid_direction = true
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == LEFT and cx > 1 then -- LEFT
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.add_exit(map_matrix[cx][cy], direction)
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2018-07-31 14:25:29 +02:00
|
|
|
cx = cx - 1
|
|
|
|
valid_direction = true
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == RIGHT and cx < 10 then -- RIGHT
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.add_exit(map_matrix[cx][cy], direction)
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2018-07-31 14:25:29 +02:00
|
|
|
cx = cx + 1
|
|
|
|
valid_direction = true
|
2017-12-06 11:44:17 +01:00
|
|
|
elseif direction == DOWN and cy < 10 then -- DOWN
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.add_exit(map_matrix[cx][cy], direction)
|
2017-12-06 16:02:50 +01:00
|
|
|
map_matrix[cx][cy].path_dir = direction
|
2018-07-31 14:25:29 +02:00
|
|
|
cy = cy + 1
|
|
|
|
valid_direction = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create the next room and add the reverse exit
|
|
|
|
-- if a valid direction was found
|
|
|
|
if valid_direction then
|
|
|
|
if not map_matrix[cx][cy] then
|
|
|
|
map_matrix[cx][cy] = room_builder.create_empty_room()
|
|
|
|
end
|
|
|
|
room_builder.add_exit(map_matrix[cx][cy], 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].goal = true
|
|
|
|
map_matrix[cx][cy].type = "room"
|
2017-12-06 11:44:17 +01:00
|
|
|
|
2018-08-15 07:19:44 +02:00
|
|
|
local roomCount = 0
|
|
|
|
local bossAdded = false
|
|
|
|
|
2018-07-31 14:25:29 +02:00
|
|
|
-- Build all the rooms
|
2017-12-07 23:58:29 +01:00
|
|
|
for i=1,10 do
|
|
|
|
for j=1,10 do
|
2018-07-31 14:25:29 +02:00
|
|
|
room = map_matrix[i][j]
|
|
|
|
if room then
|
2018-08-15 07:19:44 +02:00
|
|
|
roomCount = roomCount + 1
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.build_room(room)
|
|
|
|
monster_gen.add_monsters_to_room(room, i-1, j-1)
|
2018-08-06 00:28:23 +02:00
|
|
|
trap_gen.add_traps_to_room(room, i-1, j-1)
|
2018-08-10 20:09:56 +02:00
|
|
|
chest_gen.add_chests_to_room(room, i-1, j-1)
|
2018-08-15 07:19:44 +02:00
|
|
|
if roomCount > 3 and bossLevel and not bossAdded then
|
|
|
|
bossAdded = true
|
|
|
|
monster_gen.add_boss_to_room(room, i-1, j-1)
|
|
|
|
end
|
2017-12-07 23:58:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-31 14:25:29 +02:00
|
|
|
|
|
|
|
return map_matrix;
|
2017-12-07 23:58:29 +01:00
|
|
|
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
|
2018-01-30 15:16:14 +01:00
|
|
|
map = create_map(CURRENT_LEVEL) -- '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]
|
2018-07-31 14:25:29 +02:00
|
|
|
if room then
|
2017-12-06 11:44:17 +01:00
|
|
|
set_current_room(map, i-1, j-1);
|
2018-07-31 14:25:29 +02:00
|
|
|
room_builder.load_room(map, room)
|
|
|
|
monster_gen.load_monsters(map, room.monsters)
|
2018-08-06 00:28:23 +02:00
|
|
|
trap_gen.load_traps(map, room.traps)
|
2018-08-10 20:09:56 +02:00
|
|
|
chest_gen.load_chests(map, room.chests)
|
2017-12-06 11:44:17 +01:00
|
|
|
end
|
2017-12-02 23:32:40 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-06 11:44:17 +01:00
|
|
|
-- END SCRIPT
|