Skip to content

Commit ff8d910

Browse files
committed
Improve search for valid spawnpoints
Instead of randomly picking a coordinate on the map and checking it against all spawnpoints, we now pick a random spawnpoint from the list and check if it is valid. Now it doesn't take more than 3 tries on average while before, it would take up to a few hundred tries on larger maps.
1 parent 272b092 commit ff8d910

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/map/Map.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,17 @@ function Map.new()
8383

8484
---
8585
-- Randomly searches for a tile on which a player could be spawned.
86-
-- @treturn Tile A tile suitable for spawning.
86+
-- @tparam string faction The faction id to spawn a character for.
87+
-- @treturn Tile A tile suitable for spawning.
8788
--
8889
function self:findSpawnPoint( faction )
8990
for _ = 1, 2000 do
90-
local x = love.math.random( 1, width )
91-
local y = love.math.random( 1, height )
91+
local index = love.math.random( #spawnpoints[faction] )
92+
local spawn = spawnpoints[faction][index]
9293

93-
local tile = self:getTileAt( x, y )
94-
for _, spawn in ipairs( spawnpoints[faction] ) do
95-
if tile == spawn and tile:isPassable() and not tile:isOccupied() then
96-
return tile
97-
end
94+
local tile = self:getTileAt( spawn.x, spawn.y )
95+
if tile:isPassable() and not tile:isOccupied() then
96+
return tile
9897
end
9998
end
10099
error( string.format( 'Can not find a valid spawnpoint at position!' ))

src/map/procedural/ProceduralMapGenerator.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function ProceduralMapGenerator.new()
268268

269269
for w = 1, PARCEL_SIZE.WIDTH do
270270
for h = 1, PARCEL_SIZE.HEIGHT do
271-
spawnpoints[target][#spawnpoints[target] + 1] = tileGrid[x+w][y+h]
271+
spawnpoints[target][#spawnpoints[target] + 1] = { x = x+w, y = y+h }
272272
end
273273
end
274274
end

0 commit comments

Comments
 (0)