• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Determine the hit in lua?

marek12

Available for sprite works
Joined
Apr 8, 2020
Messages
398
Solutions
4
Reaction score
394
Hi guys,.

Is there any way of determine the damage amount dealt by basic attack in lua?
I want to make a script that will take damage dealt by auto attack, divide it and turn it into lifesteal, but I have no idea if it is possible to determine damage dealt in lua.
for example we got "getCreatureStorageValue(cid)", so the question is is there a function for damage?

I hope you understand what I mean.
 
I am not familiar with events yet. at the moment trying myself in lua, so far so good, but I don't know all the functions yet.
Would you be able to tell me a little bit more about your event? How to set it up?
at the moment " onHealthChange event + ORIGIN_MELEE/ORIGIN_RANGED " is not telling me much :c
Post automatically merged:

this is c++, I am asking if it is possible to do in lua ^^
Post automatically merged:

I am asking for a hint, not for ready to use script, so if someone would be able to help, I would really appreciate it
 
Note: onStatsChange will only trigger if the health / mana of it's target is being moved up / down.
So if someone 'blocks' an attack, there is no trigger of onStatsChange.

onStatsChange(cid, attacker, type, combat, value)

cid - creature being attacked or healed.

attacker - creature attacking cid

type - type of stat change
Lua:
STATSCHANGE_HEALTHGAIN = 0
STATSCHANGE_HEALTHLOSS = 1
STATSCHANGE_MANAGAIN = 2
STATSCHANGE_MANALOSS = 3
combat - type of combat
Lua:
COMBAT_NONE = 0
COMBAT_PHYSICALDAMAGE = 1
COMBAT_ENERGYDAMAGE = 2
COMBAT_EARTHDAMAGE = 4
COMBAT_POISONDAMAGE = 4
COMBAT_FIREDAMAGE = 8
COMBAT_UNDEFINEDDAMAGE = 16
COMBAT_LIFEDRAIN = 32
COMBAT_MANADRAIN = 64
COMBAT_HEALING = 128
COMBAT_DROWNDAMAGE = 256
COMBAT_ICEDAMAGE = 512
COMBAT_HOLYDAMAGE = 1024
COMBAT_DEATHDAMAGE = 2048
value - amount of healing / damage being dealt to creature.
Note; This is the FINAL DAMAGE to the creature.

So if the creature has 14 hp, and is hit by a 8000 damage attack, this script will only show 14 as the value.
On a separate note, this also means that if the creatures armor reduces the damage being taken by 15%, and your script reduces it by 5%, you will be 'double dipping' on your damage calculations.
Same for increasing the damage.

Example: 8000 damage

Expected Result:
increase 15% by armor + 15% by script -> 30% total = 8000 * 1.3 = 10400

Actual Result:
increase 15% by armor -> 8000 * 1.15 = 9200 AND THEN increase by 15% in script -> 9200 * 1.15 = 10580
--
Which brings us to our last point;
You cannot adjust the amount of damage or healing being applied to the creature during the scripts execution. You only have the option of true(allow) / false(disallow) the damage.
While this does not mean we cannot adjust the damage/healing, it's just a bit more annoying to do so, and in rare cases can cause unforseen issues.
(Need to return false original statChange and make our own statChange, making sure it doesn't 'infinite loop')
---
So yes, onStatsChange is useful, but only in specific use-cases.

-----------
Onto your question.., there is no way to determine if it is an auto-attack, since it only checks the damage type, not the damage source.

Likely the only way to accomplish what you want is with a source edit so you have more parameters to work with.
 
In case of 0.4 there is no 'origin' (melee, ranged or SPELL):
Code:
onStatsChange(cid, attacker, type, combat, value)
YOU CANNOT DETECT, IF IT'S SPELL OR 'AUTO-ATTACK' DAMAGE.
but... if you ignore spells that deal 'COMBAT_PHYSICALDAMAGE' (exori, exori gran), you can assume that 'physical damage = auto-attack'.
Example script (not tested):
Code:
function onStatsChange(cid, attacker, type, combat, value)
  -- if player gets hit
  if type == STATSCHANGE_HEALTHLOSS or type == STATSCHANGE_MANALOSS then
    -- if it's probably 'auto-attack'
    if combat == COMBAT_PHYSICALDAMAGE then
        -- add 10% of damage as health to attacker (lifesteal)
        doCreatureAddHealth(attacker, value / 10)
        -- !! 'value' can be negative value like '-10', you may need to change it to:
        --  doCreatureAddHealth(attacker, -value / 10)
    end
  end

  -- true = do not block damage
  return true 
end
Add it to creaturescripts.xml:
Code:
<event type="statschange" name="lifesteal" script="lifesteal.lua"/>
and in login.lua add:
Code:
registerCreatureEvent(cid, "lifesteal")
It will work only in PvP fights. To make it work when player hits monster, you need to add this event to all monsters :(
This event is execute on creature (monster/player), when it gets hit. Not on player when it hits 'something'.
 
wow, thank you very much guys. I am now going to try take something out of what you just said here. Will message here when I got my script to work.
MAinly my question was if we can determine with lua any damage dealt, auto attack was just an example, so as far as I can see I am not able to do that with auto attack, but onStatsChange maybe I can make it work at least similar of what I think of.
Big thank you :)
 
Back
Top