• 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 Monsters ranged spell only be casted if target is far away, possible?

Icaraii

Well-Known Member
Joined
Jan 5, 2020
Messages
469
Solutions
1
Reaction score
58
TFS 1.3
Hi guys,

I think the tittle says it all. Is it possible for me to make monster to only cast ranged spells when the target isn't in close combat?

Ranged combat would be 1 sqm away from the target.

Ranged combat is too overpower on my server, the player doesn't receive a single damage if it keeps far enough from the target. If I put ranged attacks in all monsters, them close combat player get too much hits, with close combat and ranged combat.
 
Last edited:
Solution
yeah i copied your script forgot to add that, hmm hard to say i'm not familiar with tfs 1.x+ functions maybe something like this will work
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "getDmg_Brush_2")

function onCastSpell(creature, variant)
local target = getCreatureTarget(creature)
if isCreature(target) and getDistanceBetween(getCreaturePosition(creature),getCreaturePosition(target)) > 2 then
    combat:execute(creature, variant)
    end
    return true
end
You can make custom spell with CALLBACK_PARAM_TARGETCREATURE

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, 63)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 111)
setCombatFormula(combat,  COMBAT_FORMULA_LEVELMAGIC, -0.2, 0, -0.2, 0)

local combat5 = createCombatObject()

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

setCombatCallback(combat5, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(cid, var)
return doCombat(cid, combat5, var)
end

For target spells something like this should work depends on what TFS you are using.

If you want to make it on area spells too you will have to edit source a little, in monsters.cpp you have already "range" variable on attacks so it will be max now create extra minRange and add it to canUseSpell function near already existing range.
 
You can make custom spell with CALLBACK_PARAM_TARGETCREATURE

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, 63)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 111)
setCombatFormula(combat,  COMBAT_FORMULA_LEVELMAGIC, -0.2, 0, -0.2, 0)

local combat5 = createCombatObject()

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

setCombatCallback(combat5, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(cid, var)
return doCombat(cid, combat5, var)
end

For target spells something like this should work depends on what TFS you are using.

If you want to make it on area spells too you will have to edit source a little, in monsters.cpp you have already "range" variable on attacks so it will be max now create extra minRange and add it to canUseSpell function near already existing range.
My TFS is 1.3, I always forget to put that on.
I would like it to not edit source, because I'm terrible at it, so I will try the way you show and let you know if I got any errors, thank you very much.
Post automatically merged:

You can make custom spell with CALLBACK_PARAM_TARGETCREATURE

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_FIREDAMAGE)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, 63)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 111)
setCombatFormula(combat,  COMBAT_FORMULA_LEVELMAGIC, -0.2, 0, -0.2, 0)

local combat5 = createCombatObject()

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

setCombatCallback(combat5, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(cid, var)
return doCombat(cid, combat5, var)
end

For target spells something like this should work depends on what TFS you are using.

If you want to make it on area spells too you will have to edit source a little, in monsters.cpp you have already "range" variable on attacks so it will be max now create extra minRange and add it to canUseSpell function near already existing range.
Why did you put "combat" on some lines and "combat5" in others?

It's kinda working, it send the distance effect when the player is in range combat, but it doesn't appear the effect neither deal damage. When I try to put all "combat5", it just do pof effect on the target (player).

I also try to edit it a little bit and the spell works, but not how I wanted. The spell sends distance effect, the effect appear on the target and the target receives damage, but this happen when the player is ranged combat and also when the player ir close combat. What did I do wrong?

Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

combat:setCallback(combat, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat:execute(creature, variant)
end
 
Last edited:
combat5 is used only to get the target, you clarify that target in onTargetCreature and then if possitive it will cast real damage combat something like this
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "getDmg_Brush_2")

local combat5 = Combat()

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

combat:setCallback(combat5, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat5:execute(creature, variant)
end
 
combat5 is used only to get the target, you clarify that target in onTargetCreature and then if possitive it will cast real damage combat something like this
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "getDmg_Brush_2")

local combat5 = Combat()

function onTargetCreature(cid, target)
if isCreature(target) and getDistanceBetween(getCreaturePosition(cid),getCreaturePosition(target)) > 2 then
doCombat(cid, combat, numberToVariant(target))
    end
    return true
end

combat:setCallback(combat5, CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat5:execute(creature, variant)
end
I did that and this error appear:
Lua Script Error: [Spell Interface]
data/spells/scripts/monster/range spell test.lua:eek:nCastSpell
data/spells/scripts/monster/range spell test.lua:21: attempt to index global 'combat5' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/monster/range spell test.lua:21: in function <data/spells/scripts/monster/range spell test.lua:20>

I try to solve it by adding this line:
local combat5 = createCombatObject()
But then, the monster didn't cast the spell and no erros appeared on the TFS.

Any idea on how to solve it?
 
yeah i copied your script forgot to add that, hmm hard to say i'm not familiar with tfs 1.x+ functions maybe something like this will work
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "getDmg_Brush_2")

function onCastSpell(creature, variant)
local target = getCreatureTarget(creature)
if isCreature(target) and getDistanceBetween(getCreaturePosition(creature),getCreaturePosition(target)) > 2 then
    combat:execute(creature, variant)
    end
    return true
end
 
Solution
yeah i copied your script forgot to add that, hmm hard to say i'm not familiar with tfs 1.x+ functions maybe something like this will work
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 79)

function getDmg_Brush_2(cid, level, maglevel)
    return (10)*-1,(40)*-1
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "getDmg_Brush_2")

function onCastSpell(creature, variant)
local target = getCreatureTarget(creature)
if isCreature(target) and getDistanceBetween(getCreaturePosition(creature),getCreaturePosition(target)) > 2 then
    combat:execute(creature, variant)
    end
    return true
end
It still giving the same error as before:
Lua Script Error: [Spell Interface]
data/spells/scripts/monster/range spell test.lua:eek:nCastSpell
data/spells/scripts/monster/range spell test.lua:14: attempt to index global 'combat5' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/monster/range spell test.lua:14: in function <data/spells/scripts/monster/range spell test.lua:11>
Lets see if anyone that understand TFS 1.x can help out. Thanks anyway, you point me to the right direction.
 
Last edited:
Back
Top