• 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 Change on critical script

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,713
Solutions
31
Reaction score
965
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi guys! I have an alternative critical system, and I wonder if someone can help me adding a little enhancement into it.

LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)

    if (not attacker or not creature) then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if primaryType == COMBAT_HEALING then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if (attacker:isPlayer() and (attacker:getCriticalLevel() * 3) >= math.random (0, 1000)) then
        primaryDamage = primaryDamage + math.ceil(primaryDamage * CRITICAL.PERCENT)
        creature:getPosition():sendMagicEffect(CONST_ME_WATERCREATURE)
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

I don't understand it really well. So what I need is to first, know why it is only sending the effect and the CRITICAL.PERCENT calculation when player hit other players. I would like it to trigger if the player is hitting monsters too. As a second request, would be nice if it could only be triggered if the player damage is higher than 150 (for example), so it doesn't have a chance to send this critical if player hit is lower than 150. Just for clarifying, my WATERCREATURE effect is the critical effect of my server.

Thanks in advance!
Regards :)
 
Solution
X
onHealthChange triggers for creatures (players or monsters) that are being damaged.

So in order for it to work for monsters, all monsters need to have this script registered to them.

The easiest way to do this is via onSpawn. (make sure onSpawn in enabled in data/events/events.xml)
LUA:
local eventCallback = EventCallback

function eventCallback.onSpawn(creature, position, startup, artificial)
    creature:registerEvent("onHealthChange_whatever_you_named_this_event")
    return true
end

eventCallback:register(-666)

Your secondary request.. I can't tell if you want the critical to fail if less then 150.. or if you just don't want the effect to happen..
But basically you just wrap an if statement around that section.

this will...
onHealthChange triggers for creatures (players or monsters) that are being damaged.

So in order for it to work for monsters, all monsters need to have this script registered to them.

The easiest way to do this is via onSpawn. (make sure onSpawn in enabled in data/events/events.xml)
LUA:
local eventCallback = EventCallback

function eventCallback.onSpawn(creature, position, startup, artificial)
    creature:registerEvent("onHealthChange_whatever_you_named_this_event")
    return true
end

eventCallback:register(-666)

Your secondary request.. I can't tell if you want the critical to fail if less then 150.. or if you just don't want the effect to happen..
But basically you just wrap an if statement around that section.

this will effectively stop the critical hit from happening, if the initial damage was not 150+
LUA:
if primaryDamage >= 150 then
    primaryDamage = primaryDamage + math.ceil(primaryDamage * CRITICAL.PERCENT)
    creature:getPosition():sendMagicEffect(CONST_ME_WATERCREATURE)
end
 
Solution
onHealthChange triggers for creatures (players or monsters) that are being damaged.

So in order for it to work for monsters, all monsters need to have this script registered to them.

The easiest way to do this is via onSpawn. (make sure onSpawn in enabled in data/events/events.xml)
LUA:
local eventCallback = EventCallback

function eventCallback.onSpawn(creature, position, startup, artificial)
    creature:registerEvent("onHealthChange_whatever_you_named_this_event")
    return true
end

eventCallback:register(-666)

Your secondary request.. I can't tell if you want the critical to fail if less then 150.. or if you just don't want the effect to happen..
But basically you just wrap an if statement around that section.

this will effectively stop the critical hit from happening, if the initial damage was not 150+
LUA:
if primaryDamage >= 150 then
    primaryDamage = primaryDamage + math.ceil(primaryDamage * CRITICAL.PERCENT)
    creature:getPosition():sendMagicEffect(CONST_ME_WATERCREATURE)
end
Little question, for the eventCallback:register(-666) I should keep -666 or it could be -555 for example? For some reason I remember that I already registered an script with that number, can't remember where but i'm quite sure I saw that already. Also I think that this eventcallback goes at scripts/eventcallbacks/monster right? Already enabled the onSpawn event at events.xml.

Thanks in advance! Really appreciate your help, as always @Xikini :)
 
CPP Version about register for all monsters
and for every monster,

monster.cpp

Under:
C++:
    // register creature events
    for(StringVec::iterator it = mType->scriptList.begin(); it != mType->scriptList.end(); ++it)
    {
        if(!registerCreatureEvent(*it))
            std::cout << "[Warning - Monster::Monster] Unknown event name - " << *it << std::endl;
    }

add

Code:
registerCreatureEvent("onHealthChange_whatever_you_named_this_even");
 
onHealthChange triggers for creatures (players or monsters) that are being damaged.

So in order for it to work for monsters, all monsters need to have this script registered to them.

The easiest way to do this is via onSpawn. (make sure onSpawn in enabled in data/events/events.xml)
LUA:
local eventCallback = EventCallback

function eventCallback.onSpawn(creature, position, startup, artificial)
    creature:registerEvent("onHealthChange_whatever_you_named_this_event")
    return true
end

eventCallback:register(-666)

Your secondary request.. I can't tell if you want the critical to fail if less then 150.. or if you just don't want the effect to happen..
But basically you just wrap an if statement around that section.

this will effectively stop the critical hit from happening, if the initial damage was not 150+
LUA:
if primaryDamage >= 150 then
    primaryDamage = primaryDamage + math.ceil(primaryDamage * CRITICAL.PERCENT)
    creature:getPosition():sendMagicEffect(CONST_ME_WATERCREATURE)
end
Just came back to say it works perfectly! So much thanks :)
Thread solved
 
Back
Top