Skip to content
Snippets Groups Projects
Commit e0e39d9c authored by DarkGod's avatar DarkGod
Browse files

Cavern map generator can now accomodate rooms

parent 0b92ec8c
No related branches found
No related tags found
No related merge requests found
......@@ -19,10 +19,11 @@
require "engine.class"
local Map = require "engine.Map"
local RoomsLoader = require "engine.generator.map.RoomsLoader"
require "engine.Generator"
--- @classmod engine.generator.map.Cavern
module(..., package.seeall, class.inherit(engine.Generator))
module(..., package.seeall, class.inherit(engine.Generator, RoomsLoader))
function _M:init(zone, map, level, data)
engine.Generator.init(self, zone, map, level)
......@@ -35,6 +36,9 @@ function _M:init(zone, map, level, data)
self.door_chance = data.door_chance or nil
self.min_floor = data.min_floor or 900
self.noise = data.noise or "simplex"
self.spots = {}
RoomsLoader.init(self, data)
end
function _M:generate(lev, old_lev)
......@@ -105,11 +109,78 @@ function _M:generate(lev, old_lev)
return self:generate(lev, old_lev)
end
local nb_room = util.getval(self.data.nb_rooms or 0)
self.required_rooms = self.required_rooms or {}
local rooms = {}
-- Those we are required to have
if #self.required_rooms > 0 then
for i, rroom in ipairs(self.required_rooms) do
local ok = false
if type(rroom) == "table" and rroom.chance_room then
if rng.percent(rroom.chance_room) then rroom = rroom[1] ok = true end
else ok = true
end
if ok then
local r = self:roomAlloc(rroom, #rooms+1, lev, old_lev)
if r then rooms[#rooms+1] = r
else self.force_recreate = true return end
nb_room = nb_room - 1
end
end
end
while nb_room > 0 do
local rroom
while true do
rroom = self.rooms[rng.range(1, #self.rooms)]
if type(rroom) == "table" and rroom.chance_room then
if rng.percent(rroom.chance_room) then rroom = rroom[1] break end
else
break
end
end
local r = self:roomAlloc(rroom, #rooms+1, lev, old_lev)
if r then rooms[#rooms+1] = r end
nb_room = nb_room - 1
end
-- Did any rooms request a tunnel ?
for i, spot in ipairs(self.spots) do if spot.make_tunnel then
local es = {}
if spot.tunnel_dir then
core.fov.calc_beam(spot.x, spot.y, self.map.w, self.map.h, 100, spot.tunnel_dir, 90,
function(_, lx, ly) return false end,
function(_, lx, ly)
if (lx ~= spot.x or ly ~= spot.y) and not self.map:checkEntity(lx, ly, Map.TERRAIN, "block_move") then es[#es+1] = {x=lx, y=ly} end
end,
nil)
else
core.fov.calc_circle(spot.x, spot.y, self.map.w, self.map.h, 100,
function(_, lx, ly) return false end,
function(_, lx, ly)
if (lx ~= spot.x or ly ~= spot.y) and not self.map:checkEntity(lx, ly, Map.TERRAIN, "block_move") then es[#es+1] = {x=lx, y=ly} end
end,
nil)
end
local ex, ey = math.floor(self.map.w / 2), math.floor(self.map.h / 2)
if #es > 0 then
table.sort(es, function(a, b) return core.fov.distance(spot.x, spot.y, a.x, a.y) < core.fov.distance(spot.x, spot.y, b.x, b.y) end)
local e = es[1]
ex, ey = e.x, e.y
end
self:tunnel(spot.x, spot.y, ex, ey, -i)
end end
if self.door_chance then
self:addDoors()
end
return self:makeStairsInside(lev, old_lev, {})
return self:makeStairsInside(lev, old_lev, self.spots)
end
function _M:addDoors()
......
......@@ -153,7 +153,7 @@ end
--- Generates parse data for from an ascii def, for function room generators
function _M:roomParse(def)
local room = { w=def[1]:len(), h=#def, spots={}, special=def.special }
local room = { w=def[1]:len(), h=#def, spots={}, exits={}, special=def.special }
-- Read the room map
for j, line in ipairs(def) do
......@@ -167,6 +167,9 @@ function _M:roomParse(def)
room.spots[c][#room.spots[c]+1] = {x=i-1, y=j-1}
c = def.numbers or '.'
end
if c == '!' then
room.exits[#room.exits+1] = {x=i-1, y=j-1}
end
room[i][j] = c
......@@ -414,6 +417,7 @@ function _M:tunnel(x1, y1, x2, y2, id, virtual)
local nx, ny = t[1], t[2]
if t[3] and self.data.door then self.possible_doors[#self.possible_doors+1] = t end
if not t[4] and not virtual then
print("=======TUNN", nx, ny)
self.map(nx, ny, Map.TERRAIN, self:resolve('=') or self:resolve('.'))
end
end
......
......@@ -1669,10 +1669,7 @@ function _M:setupCommands()
print("===============")
end end,
[{"_g","ctrl"}] = function() if config.settings.cheat then
for i = 1, 10000 do
game.log("PLOP")
profile.chat:addMessage("talk", "tome", "test", "test", "test", {"test", colors.GOLD})
end
self:changeLevel(1, "orcs+steam-quarry")
do return end
local o = game.zone:makeEntity(game.level, "object", {random_object=true}, nil, true)
if o then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment