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

TFS 1.X+ split dmg monsters

Crystals

Member
Joined
Sep 28, 2024
Messages
58
Reaction score
10
TFS 1.4.2

Hi, maybe someone can help me with another script :D

I want to make a monster with "split damage"

Based on the principle:

* Boss + 3 mobs. If the boss receives e.g. 100 damage, it distributes 25% to all. With 2 mobs ~33%, with 1 mob - 50% between them.

But if it's one of the mobs that receives damage - it will receive 100% dmg.

Only hitting the boss distributes the damage between it and the mobs

I tried to make a script in different ways but I think I'm going in the wrong direction (or just .lua for the boss script is not enough)

Last thing I ended up with:


LUA:
function onGetDamage(cid, attacker, damage, combatType)
    if isMonster(cid) and getCreatureName(cid) == "Boss name" then

        if getCreatureStorage(cid, 10000) == 1 then
            return damage
        end
        doCreatureSetStorageValue(cid, 10000, 1)
        
        local pos = getThingPos(cid)
        local spectators = getSpectators(pos, 20, 20, false)
        local monsters = {}
        for i, creature in ipairs(spectators) do
            if isMonster(creature) then
                table.insert(monsters, creature)
            end
        end
        
        local count = #monsters
        if count > 0 then
            local sharedDamage
            if damage < 0 then
                sharedDamage = math.ceil(damage / count)
            else
                sharedDamage = math.floor(damage / count)
            end
        
            for i, creature in ipairs(monsters) do
                addEvent(function(target, dmg)
                    if isCreature(target) then
                        doCreatureAddHealth(target, dmg)
                    end
                end, 1, creature, sharedDamage)
            end
        end

        doCreatureSetStorageValue(cid, 10000, 0)
        return 0
    end

    return damage
end
 
Solution
You could start with somethis like this:

LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  local summons = getCreatureSummons(creature)
  if summons == nil or #summons == 0 then
    return primaryDamage, primaryType, secondaryDamage, secondaryType
  end

  local damageEach = primaryDamage / (#summons + 1)

  local creatureTable = {}
  for _, summonID in pairs(summons) do
    creatureTable[summonID] = true
  end

  for _, summonID in pairs(summons) do
    if not creatureTable[attacker] then
      doTargetCombatHealth(attacker, summonID, primaryType, -damageEach, -damageEach, CONST_ME_DRAWBLOOD)
    end
  end

  return damageEach, primaryType, secondaryDamage, secondaryType
end
...
You could start with somethis like this:

LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  local summons = getCreatureSummons(creature)
  if summons == nil or #summons == 0 then
    return primaryDamage, primaryType, secondaryDamage, secondaryType
  end

  local damageEach = primaryDamage / (#summons + 1)

  local creatureTable = {}
  for _, summonID in pairs(summons) do
    creatureTable[summonID] = true
  end

  for _, summonID in pairs(summons) do
    if not creatureTable[attacker] then
      doTargetCombatHealth(attacker, summonID, primaryType, -damageEach, -damageEach, CONST_ME_DRAWBLOOD)
    end
  end

  return damageEach, primaryType, secondaryDamage, secondaryType
end

You probably want to also alter secondaryDamage and chosse a better effect.

Edit: I added an image, it works.
lol.webp
 
Last edited:
Solution
You could start with somethis like this:

LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  local summons = getCreatureSummons(creature)
  if summons == nil or #summons == 0 then
    return primaryDamage, primaryType, secondaryDamage, secondaryType
  end

  local damageEach = primaryDamage / (#summons + 1)

  local creatureTable = {}
  for _, summonID in pairs(summons) do
    creatureTable[summonID] = true
  end

  for _, summonID in pairs(summons) do
    if not creatureTable[attacker] then
      doTargetCombatHealth(attacker, summonID, primaryType, -damageEach, -damageEach, CONST_ME_DRAWBLOOD)
    end
  end

  return damageEach, primaryType, secondaryDamage, secondaryType
end

You probably want to also alter secondaryDamage and chosse a better effect.

Edit: I added an image, it works.
View attachment 91223
If I have a boss, for it to work I have to implement it in boss.xml as script="boss.lua"> ?
Should I add it as a spell / event for it to work?
 
Last edited:
creaturescripts>scripts>boss_damage_split.lua
LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local summons = getCreatureSummons(creature)
    if summons == nil or #summons == 0 then
      return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
 
    local damageEach = primaryDamage / (#summons + 1)
 
    local creatureTable = {}
    for _, summonID in pairs(summons) do
      creatureTable[summonID] = true
    end
 
    for _, summonID in pairs(summons) do
      if not creatureTable[attacker] then
        doTargetCombatHealth(attacker, summonID, primaryType, -damageEach, -damageEach, CONST_ME_DRAWBLOOD)
      end
    end
 
    return damageEach, primaryType, secondaryDamage, secondaryType
  end

creaturescrpts.xml:
Code:
<event type="healthchange" name="BossDamageSplit" script="boss_damage_split.lua" />

and i added to my boss.xml:


Code:
    <script>
        <event name="BossDamageSplit"/>
    </script>


However, when I summon monsters (it doesn't matter if 2 or 4) and deal damage to the boss, the rest don't take damage.

Is something implemented incorrectly?
 
creaturescripts>scripts>boss_damage_split.lua
LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local summons = getCreatureSummons(creature)
    if summons == nil or #summons == 0 then
      return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
 
    local damageEach = primaryDamage / (#summons + 1)
 
    local creatureTable = {}
    for _, summonID in pairs(summons) do
      creatureTable[summonID] = true
    end
 
    for _, summonID in pairs(summons) do
      if not creatureTable[attacker] then
        doTargetCombatHealth(attacker, summonID, primaryType, -damageEach, -damageEach, CONST_ME_DRAWBLOOD)
      end
    end
 
    return damageEach, primaryType, secondaryDamage, secondaryType
  end

creaturescrpts.xml:
Code:
<event type="healthchange" name="BossDamageSplit" script="boss_damage_split.lua" />

and i added to my boss.xml:


Code:
    <script>
        <event name="BossDamageSplit"/>
    </script>


However, when I summon monsters (it doesn't matter if 2 or 4) and deal damage to the boss, the rest don't take damage.

Is something implemented incorrectly?
what is your tfs version?
OP's was 1.4.2.
 
what is your tfs version?
OP's was 1.4.2.
Yes, I did Error. Crystal helped me with that :) Thanks for the script :D ❤️
Can you do the one from the topic that Ciosny wrote? (TFS 1.X+ - Script for daily boss (https://otland.net/threads/script-for-daily-boss.291365/))

I asked him but he is still working on it in his free time because something is not working as it should

I'm still weak in scripts. I'm just learning and I look at people like who did what and how it works so that I can do it myself in the future :D
 
Back
Top