Lua Function creature:moveTo(position)

Marcelo Druida

Active Member
Joined
Nov 17, 2014
Messages
422
Best answers
1
Likes
120
#1
Hello!!

This function makes the creature walk to the "position".
Tested on TFS 1.2
Please report bugs

Add on luascript.cpp
registerMethod("Creature", "moveTo", LuaScriptInterface::luaCreatureMoveTo);

Code:
int32_t LuaScriptInterface::luaCreatureMoveTo(lua_State* L)
{
        //creature:moveTo(pos)
    Creature* creature = getUserdata<Creature>(L, 1);
    if (!creature) {
        lua_pushnil(L);
        return 1;
    }

    const Position& position = getPosition(L, 2);

    FindPathParams fpp;
    fpp.minTargetDist = getNumber<int32_t>(L, 3, 0);
    fpp.maxTargetDist = getNumber<int32_t>(L, 4, 1);
    fpp.fullPathSearch = getBoolean(L, 5, fpp.fullPathSearch);
    fpp.clearSight = getBoolean(L, 6, fpp.clearSight);
    fpp.maxSearchDist = getNumber<int32_t>(L, 7, 150);

    std::forward_list<Direction> dirList;
    if (creature->getPathTo(position, dirList, fpp)) {   
        creature->hasFollowPath = true;
        creature->startAutoWalk(dirList);   
        pushBoolean(L, true);
    }
    else { pushBoolean(L, false); }
    return 1;
}
Add on luascript.h:
static int luaCreatureMoveTo(lua_State* L);


Replace the original function on:
creature.cpp:

Code:
bool Creature::setFollowCreature(Creature* creature)
{
    if (creature) {
        if (followCreature == creature) {
            return true;
        }

        const Position& creaturePos = creature->getPosition();
        FindPathParams fpp;
        fpp.minTargetDist = 0;
        fpp.maxTargetDist = 1;
        fpp.fullPathSearch = true;
        fpp.clearSight = true;
        fpp.maxSearchDist = 150;
        std::forward_list<Direction> dirList;
        if (creaturePos.z != getPosition().z || !canSee(creaturePos) || !getPathTo(creaturePos, dirList, fpp)) {
            followCreature = nullptr;
            return false;
        }

        if (!listWalkDir.empty()) {
            listWalkDir.clear();
            onWalkAborted();
        }

        hasFollowPath = false;
        forceUpdateFollowPath = false;
        followCreature = creature;
        isUpdatingPath = true;
    } else {
        isUpdatingPath = false;
        followCreature = nullptr;
    }

    onFollowCreature(creature);
    return true;
}
monster.cpp:
Code:
bool Monster::getNextStep(Direction& dir, uint32_t& flags)
{
   if (getHealth() <= 0) {
     //we dont have anyone watching might aswell stop walking
     eventWalk = 0;
     return false;
   }
  
   bool result = false;
   if (hasFollowPath)
     return Creature::getNextStep(dir, flags);
   else if ((!followCreature || !hasFollowPath) && !isSummon()) {
     if ((followCreature || getTimeSinceLastMove() > 1000) && !hasFollowPath && !isIdle) {
       //choose a random direction
       result = getRandomStep(getPosition(), dir);
     }
   } else if (isSummon() || followCreature) {
     result = Creature::getNextStep(dir, flags);
     if (result) {
       flags |= FLAG_PATHFINDING;
     } else {
       //target dancing
       if (attackedCreature && attackedCreature == followCreature) {
         if (isFleeing()) {
           result = getDanceStep(getPosition(), dir, false, false);
         } else if (mType->staticAttackChance < static_cast<uint32_t>(uniform_random(1, 100))) {
           result = getDanceStep(getPosition(), dir);
         }
       }
     }
   }

   if (result && (canPushItems() || canPushCreatures())) {
     const Position& pos = Spells::getCasterPosition(this, dir);
     Tile* tile = g_game.map.getTile(pos);
     if (tile) {
       if (canPushItems()) {
         Monster::pushItems(tile);
       }

       if (canPushCreatures()) {
         Monster::pushCreatures(tile);
       }
     }
   }

   return result;
}
Enjoy!
 
Last edited:

Ahilphino

Well-Known Member
Joined
Jun 5, 2013
Messages
1,236
Best answers
0
Likes
330
#2
cool stuff men
its like the old one right? u have to disable the default movement of monster etc to work?
 
Joined
Dec 14, 2010
Messages
65
Best answers
0
Likes
17
#4
Here compiles but when I open stops working and closes , has a line that was also wrong :
Add on luascript.h: static int luaMoveCreatureTo(lua_State* L);
would not be
luaCreatureMoveTo
 
Joined
Dec 14, 2010
Messages
65
Best answers
0
Likes
17
#6
Yes, I downloaded now from GitHub, the other function onSelectTarget work's here, maybe I put something in wrong place. You change only luascript.h ?


void LuaScriptInterface::registerFunctions()
{
//creature:moveTo(pos)
registerMethod("Creature", "moveTo", LuaScriptInterface::luaCreatureMoveTo);


line 2544
int32_t LuaScriptInterface::luaCreatureMoveTo(lua_State* L)
 
Last edited:
Joined
Dec 14, 2010
Messages
65
Best answers
0
Likes
17
#8
Again, compiles but when I open stops working and closes.



Obrigado pelo seu tempo.
 

Marcelo Druida

Active Member
Joined
Nov 17, 2014
Messages
422
Best answers
1
Likes
120
#13
Creature(uid):moveTo(Position(1000, 1000, 7))

remember that the path maker has a complexity limit (that can be edited, but not advisable), so dont expect that the creature will move across the whole map

works very well as part of a waypoint system
 

silveralol

Well-Known Member
Joined
Mar 16, 2010
Messages
1,465
Best answers
8
Likes
182
#15
Creature(uid):moveTo(Position(1000, 1000, 7))

remember that the path maker has a complexity limit (that can be edited, but not advisable), so dont expect that the creature will move across the whole map

works very well as part of a waypoint system
thank you for the answer...
I want to use this function to move the monsters that lost the target to own respawn, like the global, how I can use it on c++ files?
 

silveralol

Well-Known Member
Joined
Mar 16, 2010
Messages
1,465
Best answers
8
Likes
182
#19
the function already in tfs 1.2 is "teleportTo() is just teleport to the position, and don't "WALK"
 
Joined
Nov 17, 2014
Messages
422
Best answers
1
Likes
120
#20
thank you for the answer...
I want to use this function to move the monsters that lost the target to own respawn, like the global, how I can use it on c++ files?
i've already done that

void Monster::eek:nThink(uint32_t interval)

Code:
    if (!isInSpawnRange(_position)) {
        Creature* creature = this->getCreature()
        FindPathParams fpp;
        fpp.minTargetDist = 0;
        fpp.maxTargetDist = 1;
        fpp.fullPathSearch = true;
        fpp.clearSight = true;
        fpp.maxSearchDist = 150;
        std::forward_list<Direction> dirList;
            if (creature->getPathTo(masterPos, dirList, fpp)) {
                creature->hasFollowPath = true;
                creature->startAutoWalk(dirList);
            }
guess i didnt tested it

you could also create a Lua callback in this and then make your stuff inside the Lua env
cool for puzzles like "you have to pull all of these monsters from their spawns to get the key"


have you ever tested the lua function before?
i made this to a system, but I had a insight that it wasnt the best way
so I did just very basic tests and I cant assure that will work in every situation as intended

I have to mention that these monsters may be not idle after finishing the walk because its not a problem in my server
 
Last edited:
Top