• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

GlobalEvent Summon Auto Attacking Monster [1.0]

Printer

if Printer then print("LUA") end
Senator
Premium User
Joined
Dec 27, 2009
Messages
5,780
Solutions
31
Reaction score
2,299
Location
Sweden?
Hello, i found this request:
And i gave it a try, also got help from @Dalkon

One problem which i couldn't figure it out, was that how to make it not attack players, now it wont attack player but it will steer on the player. It wants to attack it, but there is a line which prevent it. But i would like to cancel it from even checking on the player, but if there is a monster on the screen it will directly switch focus to the monster instead of the player. Feel free if you have any idea.

Add it on creaturescript:
Code:
<event type="think" name="aA" script="autoAttack.lua"/>

Code:
function onThink(cid, interval)
    local monster = Monster(cid)
    if monster ~= nil then
        local targets = monster:getTargetList()
        local currentTarget = monster:getTarget()
        if not currentTarget then
            return monster:searchTarget()
        end
        if currentTarget ~= nil then
            for i = 1, #targets do
                    local target = targets[i]
                if target:isPlayer() then
                    monster:addFriend(target)
                    monster:removeTarget(target)
                end
            end
            local newTarget = monster:getTargetList()[1]
            monster:setTarget(newTarget)
            monster:setFollowCreature(newTarget)
        end
    end
    return true
end

And here is a example how to register it:
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local monster = doSummonCreature("Rattata", getPlayerPosition(cid))
    doConvinceCreature(cid, monster)
    registerCreatureEvent(monster, "aA")
    return true
end
 
Anyone have this for 0.3 or 0.4? Tried to convert it but I don't know it is possible.
 
Excuse me but where do i "register" it? I pasted the first line in under creaturescripts and also made a file named autoAttack.lua, but where do i register that last part?
 
Another thing, would it be possible to enable this to summons in general, like every monster you summon with utevo res will work this way?
 
any chance to use it on tfs 1.3 ?
get no errors but don't work :/
LUA:
function onThink(creature, interval)
    local monster = Monster(creature:getId())
    if monster then
        local targets = monster:getTargetList()
        local currentTarget = monster:getTarget()
        if not currentTarget then
            return monster:searchTarget()
        else
            for indx, target in pairs(targets) do
                if target:isPlayer() then
                    monster:addFriend(target)
                    monster:removeTarget(target)
                end
            end
            local newTarget = monster:getTargetList()[1]
            monster:setTarget(newTarget)
            monster:setFollowCreature(newTarget)
    end
    return true
end

LUA:
function onUse(player, item, fromPos, target, toPos, isHotkey)
    local monster = Game.createMonster("Rattata", player:getPosition())
    if monster then
        doConvinceCreature(monster, player)
        monster:registerEvent("aA")
    end
    return true
end
 
nothing again.
I put the first part of the code on autoattack.lua
and the second part inside the login.lua
but without success in autoattack :x

LUA:
function onThink(creature, interval)
    local monster = Monster(creature:getId())
    if monster then
        local targets = monster:getTargetList()
        local currentTarget = monster:getTarget()
        if not currentTarget then
            return monster:searchTarget()
        else
            for indx, target in pairs(targets) do
                if target:isPlayer() then
                    monster:addFriend(target)
                    monster:removeTarget(target)
                end
            end
            local newTarget = monster:getTargetList()[1]
            monster:setTarget(newTarget)
            monster:setFollowCreature(newTarget)
    end
    return true
end

LUA:
function onUse(player, item, fromPos, target, toPos, isHotkey)
    local monster = Game.createMonster("Rattata", player:getPosition())
    if monster then
        doConvinceCreature(monster, player)
        monster:registerEvent("aA")
    end
    return true
end
 
Hello Mr. @Printer I hope this message finds you well. I solved your problem, but for TSF 0.x, as @andu requested.

Like in 'brazilian' words, here is the 'jump of the cat'. As you can find these code lines in luascript.cpp, and all I did was comment these lines that check if monster isSummon(), so then, it can selectTarget(target).

LUA:
int32_t LuaScriptInterface::luaDoMonsterSetTarget(lua_State* L)
{
    //doMonsterSetTarget(cid, target)
...
...
    //if(!monster->isSummon())
        lua_pushboolean(L, monster->selectTarget(target));
    //else
    //    lua_pushboolean(L, false);


    return 1;
}

Then you compile it again... I did a tutorial long ago of 'how compile tsf 0.x'. You can see in this tutorial: link
Once you summon can target by lua code, things get possible know.

So, what I did in my project was to create a rune that summons a creature and attacks by itself, without 'mouse targeting'.

Then, in that spells >> scripts >> attack >> rune.lua I wrote, just like you suggested:

LUA:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_EARTHDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_STONES)
setCombatParam(combat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_EARTH)
function getCombatFormulas(cid, lv, maglv)
    return -50,-50
end
setCombatCallback(combat, CALLBACK_PARAM_LEVELMAGICVALUE, "getCombatFormulas")

function onCastSpell(cid, var)

creature = doCreateMonster("Serpent", var.pos)
doConvinceCreature(cid, creature)
registerCreatureEvent(creature, "aA")

    return doCombat(cid, combat, var)
end

And then, in creatureevents >> scripts >> autoAttack.lua (after registering in creatureevents >> creaturescripts, just like you did). I wrote:


LUA:
local function inTable(table, value)
for _,val in ipairs(table) do
    if val == value then
        return true
    end
end
return false
end

function onThink(cid, interval)
    local monster = isMonster(cid)
    if monster ~= nil then

    local master = getCreatureMaster(cid)
    local summonlist = getCreatureSummons(master)
    local targetlist = getMonsterTargetList(cid)

    for i = 1, #targetlist do
        for j = 1, #summonlist do
            if not inTable(summonlist, targetlist[i]) and master ~= targetlist[i] then
                doMonsterSetTarget(cid,getMonsterTargetList(cid)[i])
                return true
            end
        end
    end

    end
    return true
end

And that's it!!! =)

I know this is from 12 years ago... But dude, it's solved know.
Maybe this solution exist in other place, but I couldn't find it....
This topic helped me a lot to think in a solution, so thank you! @Printer
 
Last edited:
Back
Top