Code:
Code: Select all
function countEntitiesOfTypesInLevel(spawnedEntityNames, level)
local count = 0
for e in allEntities(level) do
for _, t in ipairs(spawnedEntityNames) do
if (e.name == t) then
count = count + 1
end
end
end
return count
end
function partyDist(x, y)
return math.abs(party.x - x) + math.abs(party.y - y)
end
function spawnChecked(level, spawners, spawnedEntityNames, maxEntities, minDistance)
if countEntitiesOfTypesInLevel(spawnedEntityNames, level) >= maxEntities then
return
end
local spawner = spawners[math.random(1, #spawners)]
local dist = partyDist(spawner.x, spawner.y)
if dist < minDistance then
return
end
spawner:activate()
end
- Put it into a script entity in one (and only one) level in the dungeon, doesn't matter which, called "smartSpawning"
- Call it with smartSpawning.spawnChecked(<level>, <table-of-spawners>, <table-of-monsters>, <max-monsters>, <min-distance>) from a timer, pressure-plate or other activating device
Code: Select all
function spawnGo()
local spawners =
{
lvl5spawner1,
lvl5spawner2,
lvl5spawner3,
lvl5spawner4,
lvl5spawner5,
lvl5spawner6
}
smartSpawning.spawnChecked(5, spawners, { "spider" }, 16, 7)
end
- Spawns choosing a random spawner from the list
- Doesn't spawn if there are already <max-monsters> spawned entities in the level
- Doesn't spawn if the picked spawner is under <min-dist> distance from the party (to avoid seeing monsters appearing out of nowhere)
Let me know of any issue - I'll post to the wiki soon.