• 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+ Fix Spell tfs 1.4.2

gabrielsaintz

New Member
Joined
Jul 19, 2022
Messages
6
Reaction score
2
GitHub
gabrielsaintz
What's up guys! I have a Spell but I wanted it to follow my character, can someone tell me a way to do that?

Code:

Lua:
function spellCallback(cid, position, count)
    if Creature(cid) then
        if count > 0 or math.random(0, 1) == 1 then
            position:sendMagicEffect(CONST_ME_HITBYFIRE)
            doAreaCombat(cid, COMBAT_FIREDAMAGE, position, 0, -100, -100, CONST_ME_EXPLOSIONHIT)
        end

        if count < 5 then
            count = count + 1
            addEvent(spellCallback, math.random(1000, 4000), cid, position, count)
        end
    end
end

function onTargetTile(creature, position)
    spellCallback(creature:getId(), position, 0)
end

local combat = Combat()
combat:setArea(createCombatArea(AREA_CIRCLE5X5))
combat:setCallback(CALLBACK_PARAM_TARGETTILE, "onTargetTile")

function onCastSpell(creature, variant, isHotkey)
    return combat:execute(creature, variant)
end

In Game:

giphy-downsized-large.gif


When I walk to the left she continues in the position where I cast her. My goal is to keep this randomness in the hits but I want it to follow my character.
 
You must only take the player's position each time the spell function is executed.

Something like this:
Lua:
function ExampleRepeat(player)
    local pos = player:getPosition()
    print(pos)
    addEvent(ExampleRepeat, 1000, player)
end
 
You must only take the player's position each time the spell function is executed.

Something like this:
Lua:
function ExampleRepeat(player)
    local pos = player:getPosition()
    print(pos)
    addEvent(ExampleRepeat, 1000, player)
end
result:
giphy-downsized-large.gif

it went wrong because the "position" that I pass to the spellCallback() function is the position where the hit goes and not the position of the player.
 
Last edited:
I managed to find a solution!!
After a long time testing I came up with this result:


Lua:
local combat = Combat()
combat:setArea(createCombatArea(AREA_CIRCLE5X5))

function getRandomPositions(cid)
    local player = Player(cid)
    local playerPos = player:getPosition() 

    local areaCircle5x5 = {} -- creates a new AREA_CIRCLE5X5 based on my character's position
    for i = -5, 5 do
        for j = -5, 5 do
            local dist = math.sqrt(i^2 + j^2)
            if dist <= 5 then
            table.insert(areaCircle5x5, {x=playerPos.x+i, y=playerPos.y+j, z=playerPos.z})
            end
        end
    end

    local randomPosition = {}
    for i = 1, 1 do
        local index = math.random(1, #areaCircle5x5)
        table.insert(randomPosition, areaCircle5x5[index])
    end
    return randomPosition
end

function spellCallback(cid, position, count, maxCount)
    if Creature(cid) and count < maxCount then
        if count == 0 and math.random(0, 1) == 1 or math.random(0, 15) == 1 then
            position:sendMagicEffect(CONST_ME_MORTAREA)
            doAreaCombat(cid, COMBAT_DEATHDAMAGE, position, 0, -100, -100, CONST_ME_EXPLOSIONHIT)
        end

        count = count + 1
      
        local area = getRandomPositions(cid)
        local positionAreaCircle = {x= area[1].x, y= area[1].y, z= area[1].z}
        local position1 = Position(positionAreaCircle)

        if count > 1 and count < 81 or count > 81 and count < 145 then
            addEvent(spellCallback,50, cid, position1, count, maxCount)         
        else 
            addEvent(spellCallback,math.random(1000, 2000) , cid, position1, count, maxCount)     
        end

        if count > 141 then           
            addEvent(spellCallback,math.random(600, 3000) , cid, position1, count, maxCount) 
        end
    end
end

function onTargetTile(creature, position)
    local maxCount = 145 
    spellCallback(creature:getId(), position, 0, maxCount)
end

combat:setCallback(CALLBACK_PARAM_TARGETTILE, "onTargetTile")

function onCastSpell(creature, variant, isHotkey)
    return combat:execute(creature, variant)
end

In game:

giphy-downsized-large.gif


thanks anyway @Drucken
 
Back
Top