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
end

Anyone?

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
end

I 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.

Module Developers Discussion/Checking if all the monsters are dead (last edited 2008-02-21 22:07:43 by NerdanelVampire)