HeroOfTheWinds wrote:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir) — Called by the engine when somebody punches the object. Note that you probably want to handle most punches using the automatic armor group system (See #Entity Damage).
puncher — An ObjectRef or nil.
time_from_last_punch — Can be used to calculate damage. Can be nil.
tool_capabilities — Capability table of used tool. Can be nil.
dir — Unit vector in the direction of the punch. Points from the puncher to the entity, and is always defined.
Maybe that will help a bit... There is no currently programmed callback for when entities are damaged, but perhaps you could make a feature request on the Minetest GitHub page. Until then, I would use the above function to determine when a mob is hit. It will be hard, but it's the only way. You need to find some way of re-registering all of the mobs in the minetest.registered_entities[] table with the on_punch() function overwritten with your changes.
Good luck!
EDIT--
Thank you Hero! With your post I was able to find everything I needed. What I have isn't perfect yet, but this appears to be providing the data needed at least to determine initial damage dealt, and when a monster is defeated:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.after(2.5, function()
local ents = minetest.registered_entities
local monsters = {}
for _,ent in pairs(ents) do
if ent.hp_max ~= nil and ent.hp_max > 0 and ent.damage ~= nil and ent.damage > 0 then
table.insert(monsters,ent)
print(dump("Adding " .. ent.name .. " to set of monsters."))
end
end
for _,monster in pairs(monsters) do
monster.on_punch = function(self, player)
if player and player:is_player() then
print(dump("Monster punched by: " .. player:get_player_name()))
print(dump("Monsters current hitpoints: " .. self.object:get_hp() .. " and max hp was " .. monster.hp_max))
end
end
end
end)
This punch event may override current mob mod punch event script (not certain) such as awarding drops, but I was thinking of handling that with my mod anyway.
I can now determine when a monster is defeated and calculate the total damage dealt to it, or calculate the damage dealt between when monster was at full health and 'now'. Only thing I can't figure out from this (so far) is if the monster was already partially damaged and now it has been damaged further (since I can't find out its HP before it was hit, which may not be its max_hp).
I may be able to determine the damage from the time_from_last_punch and tool_capabilities arguments, but haven't checked that yet.