• 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!

[Function] doDamageCreature

AGS

DeathSouls Owner
Joined
Oct 29, 2007
Messages
400
Reaction score
10
Location
Mexico
I made a simple function that checks if the player has manashield or not to remove mana or health.
Add this to global.lua
PHP:
function doDamageCreature(cid, damage, color, effect)
    damage = math.abs(damage)
    if getCreatureCondition(cid, CONDITION_MANASHIELD) == 1 then
        doSendMagicEffect(getThingPos(cid),CONST_ME_LOSEENERGY)
        doPlayerAddMana(cid, -math.min(damage, getPlayerMana(cid)))
        damage = damage - getPlayerMana(cid)
    end
    if damage > 0 then
        doCreatureAddHealth(cid, -damage)
        doSendAnimatedText(getThingPos(cid), damage, color or TEXTCOLOR_RED)
    end
    if effect then
        doSendMagicEffect(getThingPos(cid),effect)
    end
    return 1
end
cid - Who will recieve the damage
damage - Amount of damage the creature will receive
colour - The colour of the animated text(if you lose mana, the text will always be blue)
effect - Effect that will be shown


This is what it does:
If the monster/player has Manashield(of course, only players can have manashield) then:
if the player's mana is higher than the damage, it will simply lose mana.
if the damage is higher, then the player will lose all his mana and part of his hp, for example, the damage is 300, the player has mana shield, and 255 mana, the player will lose 255 mana and 45 hp.
if the player/monster doesn't have manashield, it will simply lose hp.

Hope you like it.
(You know how to thank me if you like it *cough* rep *cough*:rolleyes:)
 
Last edited:
Nice... Try doing something like this:
PHP:
function doDamageCreature(cid, damage, colour)
	if getCreatureCondition(cid, CONDITION_MANASHIELD) == 1 then
		if getPlayerMana(cid) > damage then
			doPlayerAddMana(cid,-math.min(damage, getPlayerMana(cid)))
			damage = damage - getPlayerMana(cid)
			if damage > 0 then
				doCreatureAddHealth(cid, -removeHealth)
			end	
		else
			doSendAnimatedText(getThingPos(cid),damage-getPlayerMana(cid),colour)			
			doCreatureAddHealth(cid,getPlayerMana(cid)-damage)
			doPlayerAddMana(cid,-getPlayerMana(cid))
		end
	else
		doCreatureAddHealth(cid,-damage)
		doSendAnimatedText(getThingPos(cid),damage,colour)
	end
	return 1
end

Removes hp too, if mana drains out completely... Idk if math.min is necessary, but on few servers mana can go below 0 with doPlayerAddMana(cid, -78787) ^^
 
Last edited:
I don't understand why...
In this part:
Code:
if getPlayerMana(cid) > damage then 
            doPlayerAddMana(cid,-math.min(damage, getPlayerMana(cid))) 
            damage = damage - getPlayerMana(cid) 
            if damage > 0 then 
                doCreatureAddHealth(cid, -removeHealth) 
            end
The mana will always be higher than damage, because its checking it first...so that part only requires the doPlayerAddMana function...

If the damage is higher, then it will drain the mana completely, and then remove the remaining damage from health.
 
PHP:
function doDamageCreature(cid, damage, colour) 
    if getCreatureCondition(cid, CONDITION_MANASHIELD) == 1 then 
        if getPlayerMana(cid) > damage then 
            damage = damage - getPlayerMana(cid) 
            doPlayerAddMana(cid,-math.min(damage, getPlayerMana(cid))) 
            if damage > 0 then 
                doCreatureAddHealth(cid, -removeHealth) 
            end     
        else 
            doSendAnimatedText(getThingPos(cid),damage-getPlayerMana(cid),colour)             
            doCreatureAddHealth(cid,getPlayerMana(cid)-damage) 
            doPlayerAddMana(cid,-getPlayerMana(cid)) 
        end 
    else 
        doCreatureAddHealth(cid,-damage) 
        doSendAnimatedText(getThingPos(cid),damage,colour) 
    end 
    return 1 
end

The "damage" thing should be over the mana thing :p

And what it does? Well, your script:
Colandus: 155 mana, 500 health.
Colandus gets attacked with 'doDamageCreature', with 'damage' as 375.
Colandus has manashield, so it will remove his mana, from 155 mana to 0, but still, it was going to damage 375 health, what happens to the remaining? Well... Nothing?

Got it?! But my script, it will remove the mana, and if you got no mana left but damage isn't over yet, it'll remove health too.
 
Look at the first else of my script, that else is when the damage is higher than the player's mana, so it removes full mana and the remaining damage.

Also, you added:
Code:
if damage > 0 then  
                doCreatureAddHealth(cid, -removeHealth)  
            end
Inside the "if getPlayerMana(cid) > damage then"...
 
Ohh i see the else now ^^
PHP:
function doDamageCreature(cid, damage, colour)
    damage = math.abs(damage) -- remove negative ;)
    if getCreatureCondition(cid, CONDITION_MANASHIELD) == 1 then 
        doPlayerAddMana(cid,-math.min(damage, getPlayerMana(cid))) 
        damage = damage - getPlayerMana(cid) 
        if damage > 0 then 
            doCreatureAddHealth(cid, -damage) 
        end     
    else 
        doCreatureAddHealth(cid,-damage) 
        doSendAnimatedText(getThingPos(cid),damage,colour) 
    end 
    return 1 
end
 
Last edited:
Thanks, I updated the first post, I also added effect and fixed the animation text when loosing mana.
 
For TFS 1.x
Lua:
local infight = Condition(CONDITION_INFIGHT)
infight:setParameter(CONDITION_PARAM_TICKS, configManager.getNumber(configKeys.PZ_LOCKED))

function Creature.damage(self, damage, effect)
    damage = math.abs(damage)
    if damage == 0 then
      return
    end

    self:addCondition(infight)

    local position = self:getPosition()
    if self:getCondition(CONDITION_MANASHIELD) then
        local loseMana = math.min(damage, self:getMana())
        self:addMana(-loseMana)
        self:sendTextMessage(MESSAGE_DAMAGE_RECEIVED, ("You lose %d mana."):format(loseMana), position, loseMana, TEXTCOLOR_BLUE)
 
        position:sendMagicEffect(CONST_ME_LOSEENERGY)

        local name = self:getName()
        for _, player in ipairs(Game.getSpectators(position, false, true)) do
          if player ~= self then
            player:sendTextMessage(MESSAGE_DAMAGE_RECEIVED, ("%s loses %d mana."):format(name, loseMana), position, loseMana, TEXTCOLOR_BLUE)
          end
        end

        damage = damage - self:getMana()
    end

    if damage > 0 then
        self:addHealth(-damage)
    end

    if effect then
        position:sendMagicEffect(effect)
    end
end
 
Last edited:
Back
Top