For such a large number of tiles, allEntities() will usually be faster than entitiesAt():
Code: Select all
onRest = function(self)
local go = self.go
local px = go.x
local py = go.y
local abs = math.abs
local max = math.max
for e in go.map:allEntities() do
if e.monster and e.monster:isAlive() and max(abs(e.x-px), abs(e.y-py)) <= 4 then
for i = 1 , 4 do
if self:getChampion(i):isAlive() then
playSound("polymorph_bear")
local name = self:getChampion(i):getName()
hudPrint(name .. " says: 'We can't rest here. There are monsters in the surroundings.'")
return false
end
end
end
end
end
untested so i probably made a typo somewhere
Warning: If your dungeon has a DiggingToolComponent (like a shovel) or RopeToolComponent (like a rope) anywhere in it, and you
ever return false from the onRest hook, you must also write code to prevent the DiggingToolComponent or RopeToolComponent from being used when the onRest hook will return false. Otherwise, you will break people's saved games. See mod_assets/core/scripts/reduced_standard_assets/items/misc.lua in
ORRR3's source code.
Also, you asked for (and I provided) a Chebyshev distance test, but Grimrock uses Taxicab geometry for party and monster movement, not Chebyshev. So it would make more sense to use a Taxicab distance check:
abs(e.x-px)+abs(e.y-py) <= [distance] instead of
max(abs(e.x-px), abs(e.y-py)) <= [distance]. Consider this.