Fix death and drop handling of mobs

This commit is contained in:
Wuzzy 2019-09-03 19:56:47 +02:00
parent 59864a8311
commit 66e5d3230d
5 changed files with 39 additions and 40 deletions

View File

@ -7,6 +7,9 @@ local S = minetest.get_translator("mobs")
achievements.register_achievement(
"hunter",
{
-- Note: This achievement only counts animals that
-- have at least one food item in their drop table
-- (no matter how unlikely).
title = S("Hunter"),
description = S("Kill 5 animals for food."),
times = 5,

View File

@ -46,25 +46,15 @@ local function effect(pos, amount, texture, max_size)
})
end
-- on mob death drop items
local function check_for_death(self, hitter)
local hp = self.object:get_hp()
if hp > 0 then
self.health = hp
if self.sounds.damage ~= nil then
minetest.sound_play(
self.sounds.damage,
{
object = self.object,
max_hear_distance = self.sounds.distance
})
end
return false
end
local function die_handler(self, killer)
local pos = self.object:get_pos()
self.object:remove()
minetest.log("action", "[mobs] "..self.name.." dies at "..minetest.pos_to_string(vector.round(pos)))
local obj = nil
local drops_food = false
for _,drop in ipairs(self.drops) do
if minetest.get_item_group(drop.name, "food") ~= 0 then
drops_food = true
end
if math.random(1, drop.chance) == 1 then
obj = minetest.add_item(pos, ItemStack(drop.name.." "..math.random(drop.min, drop.max)))
if obj then
@ -85,10 +75,33 @@ local function check_for_death(self, hitter)
max_hear_distance = self.sounds.distance
})
end
if self.on_die then
self.on_die(self, pos, hitter)
-- Hunter achievement: If mob is a food-dropping animal, it counts.
if killer ~= nil and self.type == "animal" and drops_food then
achievements.trigger_achievement(killer, "hunter")
end
if self.on_die then
self.on_die(self, pos, killer)
end
end
-- on mob death drop items
local function check_for_death(self, hitter)
local hp = self.object:get_hp()
if hp > 0 then
self.health = hp
if self.sounds.damage ~= nil then
minetest.sound_play(
self.sounds.damage,
{
object = self.object,
max_hear_distance = self.sounds.distance
})
end
return false
else
die_handler(self, hitter)
return true
end
return true
end
-- from TNT mod
@ -1376,6 +1389,10 @@ function mobs:register_mob(name, def)
end
end
end,
on_death = function(self, killer)
die_handler(self, killer)
end,
})
end

View File

@ -53,13 +53,6 @@ mobs:register_mob(
mobs:capture_mob(self, clicker, 0, 5, 40, false, nil)
end,
on_die = function(self, pos, hitter)
if hitter == nil or (hitter ~= nil and not hitter:is_player()) then
return
end
achievements.trigger_achievement(hitter, "hunter")
end,
})
mobs:register_spawn(

View File

@ -125,13 +125,6 @@ mobs:register_mob(
mobs:capture_mob(self, clicker, 0, 5, 60, false, nil)
end,
on_die = function(self, pos, hitter)
if hitter == nil or (hitter ~= nil and not hitter:is_player()) then
return
end
achievements.trigger_achievement(hitter, "hunter")
end,
})

View File

@ -48,13 +48,6 @@ mobs:register_mob(
mobs:feed_tame(self, clicker, 6, true)
mobs:capture_mob(self, clicker, 10, 40, 20, false, nil)
end,
on_die = function(self, pos, hitter)
if hitter == nil or (hitter ~= nil and not hitter:is_player()) then
return
end
achievements.trigger_achievement(hitter, "hunter")
end,
})
mobs:register_spawn(