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
|
2019-02-22 00:09:31 +01:00
|
|
|
local random = map_random
|
|
|
|
local randomseed = map_randomseed
|
2017-12-06 11:44:17 +01:00
|
|
|
|
|
|
|
-- 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
|
2019-01-10 19:26:19 +01:00
|
|
|
local seed = get_random_seed(CURRENT_LEVEL)
|
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
|
2019-03-07 11:09:14 +01:00
|
|
|
local shopLevel = CURRENT_LEVEL % 4 == 0
|
2018-08-15 07:19:44 +02:00
|
|
|
local bossLevel = CURRENT_LEVEL % 5 == 0
|
2018-10-22 09:38:05 +02:00
|
|
|
if QUICK_MODE then
|
|
|
|
bossLevel = CURRENT_LEVEL % 3 == 0
|
2019-03-07 11:09:14 +01:00
|
|
|
shopLevel = CURRENT_LEVEL % 2 == 0
|
2018-10-22 09:38:05 +02:00
|
|
|
end
|
2018-09-09 12:43:17 +02:00
|
|
|
local coverage = 8 + CURRENT_LEVEL
|
|
|
|
if bossLevel or CURRENT_LEVEL == 1 then
|
2018-08-15 07:19:44 +02:00
|
|
|
coverage = 5
|
|
|
|
end
|
2018-10-27 13:53:29 +02:00
|
|
|
if ARCADE_MODE then
|
2018-10-26 18:37:50 +02:00
|
|
|
coverage = 40
|
2019-03-07 11:09:14 +01:00
|
|
|
shopLevel = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if shopLevel then
|
|
|
|
coverage = coverage + 1
|
2018-10-26 18:37:50 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-09 12:43:17 +02:00
|
|
|
if coridoor_count < coverage/2 then
|
2017-12-07 23:58:29 +01:00
|
|
|
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
|
2019-03-07 11:09:14 +01:00
|
|
|
local shopAdded = false
|
2018-08-15 07:19:44 +02:00
|
|
|
|
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
|
2019-03-07 20:06:04 +01:00
|
|
|
if roomCount > 5 and shopLevel and not shopAdded then
|
2019-03-07 11:09:14 +01:00
|
|
|
room.type = "shop"
|
|
|
|
shopAdded = true
|
|
|
|
end
|
2018-08-15 07:19:44 +02:00
|
|
|
roomCount = roomCount + 1
|
2019-03-07 11:09:14 +01:00
|
|
|
room_builder.build_room(room, i-1, j-1)
|
|
|
|
if room.type ~= "shop" then
|
|
|
|
monster_gen.add_monsters_to_room(room, i-1, j-1)
|
|
|
|
trap_gen.add_traps_to_room(room, i-1, j-1)
|
|
|
|
chest_gen.add_chests_to_room(room, i-1, j-1)
|
2019-03-07 20:06:04 +01:00
|
|
|
else
|
|
|
|
monster_gen.add_shopkeeper_to_room(room, i-1, j-1)
|
2019-03-07 11:09:14 +01:00
|
|
|
end
|
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
|