NerdanelVampire: I've been trying to make a quest in which the player is supposed to kill everything on the level except himself/herself and pets/companions (if any). The following piece of code (which takes after for_all_monsters()) is supposed to return false if there are monsters left and true otherwise, but the problem is that it doesn't work.
level_is_empty = function()
for i = 1, monst_list.size do
if (monst_list.node[i].flags & FLAG_FLAG_USED) ~= 0 then
local idx = monst_list.node[i].key
local monst = monster(idx)
if monst and monst.r_idx ~= 0 and monst.faction ~= FACTION_PLAYER then
return false
end
end
end
return true
endAnyone?
NerdanelVampire: Hmmm... It looks like the code above is actually correct, but at HOOK_MONSTER_DEATH the dying monster isn't actually removed, so calling this code at that time doesn't work as expected...
NerdanelVampire: I solved my own problem again. Here is the fixed code.
-- A helpful function for checking if all the monster on the level are dead
-- Call from HOOK_MONSTER_DEATH
level_is_empty = function(m_idx)
for i = 1, monst_list.size do
if (monst_list.node[i].flags & FLAG_FLAG_USED) ~= 0 then
local idx = monst_list.node[i].key
local monst = monster(idx)
if monst and monst.r_idx ~= 0 and idx ~= m_idx and monst.faction ~= FACTION_PLAYER then
return false
end
end
end
return true
endI had to add an extra check if a monster was the monster we are killing, since there isn't a better hook for the purpose than HOOK_MONSTER_DEATH.
ToME Wiki