Page 1 of 1

Damage Dealt Event

PostPosted: Sat Feb 14, 2015 16:55
by STP
I'm creating a mod in which I'd like to know when a player deals damage to a LuaEntitySAO. It's my understanding there is a new damage system in use that might facilitate this, plus I can see the output dumped to the log when damage is dealt (output through ACTION[ServerThread]) so I know the engine has this information.

Is there an event I can use to access this? I want my mod to be "monster-mod independent", so that it won't matter what type of monster mod the user has installed (I intent to make my mod public of course) - as long as the monster mod uses the LuaEntitySAO & engine damage system my mod should detect damage dealt.

Thanks!

Re: Damage Dealt Event

PostPosted: Mon Feb 16, 2015 15:24
by STP
Judging by the lack of replies it's looking like I may not be able to do what I wanted to.

So if I can't detect when damage is dealt, what about an event for when a mob is killed/defeated/destroyed? Is that possible?

Or failing that, when a mob has been hit at all?

Re: Damage Dealt Event

PostPosted: Mon Feb 16, 2015 22:34
by HeroOfTheWinds
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!

Re: Damage Dealt Event

PostPosted: Tue Feb 17, 2015 03:40
by STP
HeroOfTheWinds wrote:
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:

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.

Re: Damage Dealt Event

PostPosted: Thu Feb 19, 2015 06:23
by HeroOfTheWinds
Glad to hear that I could be of help. Let us know if you need more help, and good luck with your mod!