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

Feature [CreatureEvent] function onMove(cid, fromPosition, toPosition) [TFS 0.3.6]

Joined
Apr 17, 2008
Messages
1,922
Solutions
1
Reaction score
188
Location
Venezuela
Hello, i've compiled this creatures-script function.

Can be used in players, monsters or npcs, work when a creature walks.

creatureevent.h:
After:
Lua:
	CREATURE_EVENT_DIRECTION,

Put:
Lua:
	CREATURE_EVENT_MOVE,

After:
Lua:
		uint32_t executeDirection(Creature* creature, Direction old, Direction current);

Put:
Lua:
		uint32_t executeMove(Creature* creature, const Position& fromPosition, const Position& toPosition);

creatureevent.cpp:

After:
Lua:
	else if(tmpStr == "direction")
		m_type = CREATURE_EVENT_DIRECTION;

Put:
Lua:
	else if(tmpStr == "move")
		m_type = CREATURE_EVENT_MOVE;

After:
Lua:
		case CREATURE_EVENT_DIRECTION:
			return "onDirection";

Put:
Lua:
		case CREATURE_EVENT_MOVE:
			return "onMove";

After:
Lua:
		case CREATURE_EVENT_OUTFIT:
			return "cid, old, current";

Paste:
Lua:
		case CREATURE_EVENT_MOVE:
			return "cid, fromPosition, toPosition";

Now, under function:
Lua:
uint32_t CreatureEvent::executeDirection(Creature* creature, Direction old, Direction current)

Paste:
Lua:
uint32_t CreatureEvent::executeMove(Creature* creature, const Position& fromPosition, const Position& toPosition)
{
	//onMove(cid, old, current)
	if(m_interface->reserveEnv())
	{
		ScriptEnviroment* env = m_interface->getEnv();
		if(m_scripted == EVENT_SCRIPT_BUFFER)
		{
			env->setRealPos(creature->getPosition());
			std::stringstream scriptstream;
			scriptstream << "local cid = " << env->addThing(creature) << std::endl;

                        env->streamPosition(scriptstream, "fromPosition", fromPosition, 0);
                        env->streamPosition(scriptstream, "toPosition", toPosition, 0);

			scriptstream << m_scriptData;
			bool result = true;
			if(m_interface->loadBuffer(scriptstream.str()))
			{
				lua_State* L = m_interface->getState();
				result = m_interface->getGlobalBool(L, "_result", true);
			}

			m_interface->releaseEnv();
			return result;
		}
		else
		{
			#ifdef __DEBUG_LUASCRIPTS__
			char desc[30];
			sprintf(desc, "%s", creature->getName().c_str());
			env->setEventDesc(desc);
			#endif

			env->setScriptId(m_scriptId, m_interface);
			env->setRealPos(creature->getPosition());

			lua_State* L = m_interface->getState();
			m_interface->pushFunction(m_scriptId);

			lua_pushnumber(L, env->addThing(creature));
			LuaScriptInterface::pushPosition(L, fromPosition);
			LuaScriptInterface::pushPosition(L, toPosition);

			bool result = m_interface->callFunction(3);
			m_interface->releaseEnv();
			return result;
		}
	}
	else
	{
		std::cout << "[Error - CreatureEvent::executeMove] Call stack overflow." << std::endl;
		return 0;
	}
}

game.cpp:
After:
Lua:
		else if(currentPos.z != 7 && (!(tmpTile = map->getTile(destPos)) || (!tmpTile->ground &&
			!tmpTile->hasProperty(BLOCKSOLID))) && (tmpTile = map->getTile(Position(
			destPos.x, destPos.y, destPos.z + 1))) && tmpTile->hasHeight(3)) //try go down
		{
			flags = flags | FLAG_IGNOREBLOCKITEM | FLAG_IGNOREBLOCKCREATURE;
			destPos.z++;
		}
	}

Paste:
Lua:
	bool deny = false;
	CreatureEventList moveEvents = creature->getCreatureEvents(CREATURE_EVENT_MOVE);
	for(CreatureEventList::iterator it = moveEvents.begin(); it != moveEvents.end(); ++it)
		if(!(*it)->executeMove(creature, currentPos, destPos))
		deny = true;

	if(deny)
		return RET_NOTPOSSIBLE;

Now compile the server:

There is the way to use:

Lua:
function onMove(cid, fromPosition, toPosition)
	if(toPosition.x == 100 and toPosition.y == 100 and toPosition.z == 7) then
		doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
	end
	return true
end

And this in creaturescripts.xml
Lua:
	<event type="move" name="Move" event="script" value="move.lua"/>

Bugs!

  • You can't use
Lua:
if(toPosition == {x = 100, y = 100, z = 7}) then
	--execute script

I don't know why :S

  • You can't use "return false" to prevent moving, i get the following error:
If i put this in game.cpp:
Lua:
if(deny)
	return false;

I get this error:
Code:
 J:\Users\xxx\Documents\Documents\OT\0.3.6pl1\game.cpp In member function `ReturnValue Game::internalMoveCreature(Creature*, Direction, uint32_t)': 
1174
J:\Users\xxx\Documents\Documents\OT\0.3.6pl1\game.cpp cannot convert `bool' to `ReturnValue' in return 
J:\Users\xxx\Documents\Documents\OT\My Compiling\Makefile.win [Build Error]  [../0.3.6pl1/game.o] Error 1
 
Last edited:
Code:
        bool deny = false;
        CreatureEventList moveEvents = creature->getCreatureEvents(CREATURE_EVENT_MOVE);
        for(CreatureEventList::iterator it = moveEvents.begin(); it != moveEvents.end(); ++it)
                if(!(*it)->executeMove(creature, Position(currentPos), Position(destPos)))
                deny = true;

->

Code:
        bool deny = false;
        CreatureEventList moveEvents = creature->getCreatureEvents(CREATURE_EVENT_MOVE);
        for(CreatureEventList::iterator it = moveEvents.begin(); it != moveEvents.end(); ++it)
        {
                if(!(*it)->executeMove(creature, Position(currentPos), Position(destPos)))
                        deny = true;
        }

and:

Code:
if(deny)
        return RET_NOTPOSSIBLE;


Code:
                        scriptstream << "local fromPosition = " << fromPosition << std::endl;
                        scriptstream << "local toPosition = " << toPosition << std::endl;

is streamed unproperly:

Code:
                        env->streamPosition(scriptstream, "fromPosition", fromPosition, 0);
                        env->streamPosition(scriptstream, "toPosition", toPosition, 0);


and I guess you dont have to do that:

Code:
Position(currentPos), Position(destPos)

"Position(...)"
 
Last edited:
not working at 0.4 errors when running tfs :p
 
I was thinking about it from ages. It's the only thing that movements yet need. Great idea!:)
 
Could someone upload the '.exe' with this compiled, since I don't seem to have any of the things that I have to search for.
 
elf: do not understand your fix
please could you put in a more explanatory label?

topic: when I add this function on my server, onthik stops working
 
Last edited:
Back
Top