Skip to content

Commit ff64bff

Browse files
committed
Update brogue generator algo
The Brogue algorithm is something along the lines of: Build the first room (wether it be the standard first floor room, a cave, or a rectangle.) Try 600 random wall spots on each spot try 15 times to build a corridor+room(.8 random chance) or room(forced on last 215 iterations.) That reliably takes three seconds in my code. Whether because I'm bad at this or whatever. changing that to try 1000 wall spots once each cuts the time down to about a half second. That's what this commit does. I'll keep trying to speed up the room and corridor building (those eat up most of the time), but for now this is a significant improvement and the level doesn't really suffer for it.
1 parent d3a0cf1 commit ff64bff

2 files changed

Lines changed: 53 additions & 57 deletions

File tree

rotLove/rotLove.lua

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,7 +3768,7 @@ end
37683768

37693769
function ROT.Map.Brogue:_generateRooms()
37703770
local rooms=0
3771-
for i=1,600 do
3771+
for i=1,1000 do
37723772
if rooms>self._maxrooms then break end
37733773
if self:_buildRoom(i>375) then
37743774
rooms=rooms+1
@@ -3777,41 +3777,40 @@ function ROT.Map.Brogue:_generateRooms()
37773777
end
37783778

37793779
function ROT.Map.Brogue:_buildRoom(forceNoCorridor)
3780-
local p=table.remove(self._walls,self._rng:random(1,#self._walls))
3781-
--local p=self._walls[self._rng:random(1,#self._walls)]
3780+
--local p=table.remove(self._walls,self._rng:random(1,#self._walls))
3781+
local p=self._walls[self._rng:random(1,#self._walls)]
37823782
if not p then return false end
37833783
local d=self:_getDiggingDirection(p[1], p[2])
37843784
if d then
3785-
for j=1,15 do
3786-
if self._rng:random()<self._options.corridorChance and not forceNoCorridor then
3787-
if d[1]~=0 then cd=self._options.corridorWidth
3788-
else cd=self._options.corridorHeight
3789-
end
3790-
local corridor=ROT.Map.Corridor:createRandomAt(p[1]+d[1],p[2]+d[2],d[1],d[2],{corridorLength=cd}, self._rng)
3791-
if corridor:isValid(self, self._isWallCallback, self._canBeDugCallback) then
3792-
local dx=corridor._endX
3793-
local dy=corridor._endY
3794-
3795-
local room=ROT.Map.BrogueRoom:createRandomAt(dx, dy ,d[1],d[2], self._options, self._rng)
3796-
3797-
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
3798-
corridor:create(self, self._digCallback)
3799-
room:create(self, self._digCallback)
3800-
self:_insertWalls(room._walls)
3801-
self._map[p[1]][p[2]]=0
3802-
self._map[dx][dy]=0
3803-
return true
3804-
end
3805-
end
3806-
else
3807-
local room=ROT.Map.BrogueRoom:createRandomAt(p[1],p[2],d[1],d[2], self._options, self._rng)
3785+
if self._rng:random()<self._options.corridorChance and not forceNoCorridor then
3786+
if d[1]~=0 then cd=self._options.corridorWidth
3787+
else cd=self._options.corridorHeight
3788+
end
3789+
local corridor=ROT.Map.Corridor:createRandomAt(p[1]+d[1],p[2]+d[2],d[1],d[2],{corridorLength=cd}, self._rng)
3790+
if corridor:isValid(self, self._isWallCallback, self._canBeDugCallback) then
3791+
local dx=corridor._endX
3792+
local dy=corridor._endY
3793+
3794+
local room=ROT.Map.BrogueRoom:createRandomAt(dx, dy ,d[1],d[2], self._options, self._rng)
3795+
38083796
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
3797+
corridor:create(self, self._digCallback)
38093798
room:create(self, self._digCallback)
38103799
self:_insertWalls(room._walls)
3811-
table.insert(self._doors, room._doors[1])
3800+
self._map[p[1]][p[2]]=0
3801+
self._map[dx][dy]=0
38123802
return true
38133803
end
38143804
end
3805+
else
3806+
local room=ROT.Map.BrogueRoom:createRandomAt(p[1],p[2],d[1],d[2], self._options, self._rng)
3807+
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
3808+
room:create(self, self._digCallback)
3809+
self._map[p[1]][p[2]]=0
3810+
self:_insertWalls(room._walls)
3811+
table.insert(self._doors, room._doors[1])
3812+
return true
3813+
end
38153814
end
38163815
end
38173816
return false
@@ -3853,11 +3852,11 @@ function ROT.Map.Brogue:_generateLoops()
38533852
count=count+1
38543853
end
38553854
local function pass(x,y)
3856-
return m[x][y]~=1
3855+
return m[x][y]==0
38573856
end
38583857
for i=1,300 do
38593858
if #self._walls<1 then return end
3860-
local w=table.remove(self._walls, self._rng:random(1,#self._walls))
3859+
local w=table.remove(self._walls, 1)--self._rng:random(1,#self._walls))
38613860
for j=1,2 do
38623861
local x=w[1] +dirs[j][1]
38633862
local y=w[2] +dirs[j][2]
@@ -3869,8 +3868,8 @@ function ROT.Map.Brogue:_generateLoops()
38693868
then
38703869
local path=ROT.Path.AStar(x,y,pass)
38713870
path:compute(x2, y2, cb)
3872-
if count>20 then
3873-
m[w[1]][w[2]]=3
3871+
if count>30 then
3872+
m[w[1]][w[2]]=0
38743873
end
38753874
count=0
38763875
end
@@ -3882,7 +3881,6 @@ function ROT.Map.Brogue:_closeDiagonalOpenings()
38823881
end
38833882

38843883
function ROT.Map.Brogue:_getDoors() return self._doors end
3885-
function ROT.Map.Brogue:getDoors() return self._doors end
38863884

38873885
function ROT.Map.Brogue:_digCallback(x, y, value)
38883886
self._map[x][y]=value

src/brogue.lua

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ end
166166

167167
function Brogue:_generateRooms()
168168
local rooms=0
169-
for i=1,600 do
169+
for i=1,1000 do
170170
if rooms>self._maxrooms then break end
171171
if self:_buildRoom(i>375) then
172172
rooms=rooms+1
@@ -175,41 +175,39 @@ function Brogue:_generateRooms()
175175
end
176176

177177
function Brogue:_buildRoom(forceNoCorridor)
178-
local p=table.remove(self._walls,self._rng:random(1,#self._walls))
179-
--local p=self._walls[self._rng:random(1,#self._walls)]
178+
--local p=table.remove(self._walls,self._rng:random(1,#self._walls))
179+
local p=self._walls[self._rng:random(1,#self._walls)]
180180
if not p then return false end
181181
local d=self:_getDiggingDirection(p[1], p[2])
182182
if d then
183-
for j=1,15 do
184-
if self._rng:random()<self._options.corridorChance and not forceNoCorridor then
185-
if d[1]~=0 then cd=self._options.corridorWidth
186-
else cd=self._options.corridorHeight
187-
end
188-
local corridor=ROT.Map.Corridor:createRandomAt(p[1]+d[1],p[2]+d[2],d[1],d[2],{corridorLength=cd}, self._rng)
189-
if corridor:isValid(self, self._isWallCallback, self._canBeDugCallback) then
190-
local dx=corridor._endX
191-
local dy=corridor._endY
183+
if self._rng:random()<self._options.corridorChance and not forceNoCorridor then
184+
if d[1]~=0 then cd=self._options.corridorWidth
185+
else cd=self._options.corridorHeight
186+
end
187+
local corridor=ROT.Map.Corridor:createRandomAt(p[1]+d[1],p[2]+d[2],d[1],d[2],{corridorLength=cd}, self._rng)
188+
if corridor:isValid(self, self._isWallCallback, self._canBeDugCallback) then
189+
local dx=corridor._endX
190+
local dy=corridor._endY
192191

193-
local room=ROT.Map.BrogueRoom:createRandomAt(dx, dy ,d[1],d[2], self._options, self._rng)
192+
local room=ROT.Map.BrogueRoom:createRandomAt(dx, dy ,d[1],d[2], self._options, self._rng)
194193

195-
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
196-
corridor:create(self, self._digCallback)
197-
room:create(self, self._digCallback)
198-
self:_insertWalls(room._walls)
199-
self._map[p[1]][p[2]]=0
200-
self._map[dx][dy]=0
201-
return true
202-
end
203-
end
204-
else
205-
local room=ROT.Map.BrogueRoom:createRandomAt(p[1],p[2],d[1],d[2], self._options, self._rng)
206194
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
195+
corridor:create(self, self._digCallback)
207196
room:create(self, self._digCallback)
208197
self:_insertWalls(room._walls)
209-
table.insert(self._doors, room._doors[1])
198+
self._map[p[1]][p[2]]=0
199+
self._map[dx][dy]=0
210200
return true
211201
end
212202
end
203+
else
204+
local room=ROT.Map.BrogueRoom:createRandomAt(p[1],p[2],d[1],d[2], self._options, self._rng)
205+
if room:isValid(self, self._isWallCallback, self._canBeDugCallback) then
206+
room:create(self, self._digCallback)
207+
self:_insertWalls(room._walls)
208+
table.insert(self._doors, room._doors[1])
209+
return true
210+
end
213211
end
214212
end
215213
return false

0 commit comments

Comments
 (0)