tiag0_bn
Well-Known Member
- Joined
- Dec 8, 2011
- Messages
- 181
- Reaction score
- 50
I'm starting to learn otcv8 and I'm having problems with the opcode
I have an environment to test the opcode that I found in some forum... could you help me?
otcv8
script server:
<event type="extendedopcode" name="ExtendedOpcode" event="script" value="opcodes.lua"/>
registerCreatureEvent(cid, "ExtendedOpcode")
return otcv8
opcodes in source
protocolgame.cpp
const.h
luascript.h
player.h
creatureevent.h
1 -
2 -
creatureevent.cpp
game.cpp
game.h
spectators.h
}
applied this fix
I have an environment to test the opcode that I found in some forum... could you help me?
otcv8
Lua:
local storeWindow = nil
local lblName = nil
storeWindow = g_ui.loadUI('testes')
local Opcodes = 180
function init()
connect(g_game, {
onGameStart = toggle,
onGameEnd = toggle,
})
ProtocolGame.registerExtendedOpcode(Opcodes, function(protocol, opcode, buffer) onReceiveName(buffer) end)
storeWindow:hide()
g_keyboard.bindKeyPress('Ctrl+X', toggle)
end
function terminate()
disconnect(g_game, {
onGameStart = naoexibir,
onGameEnd = naoexibir,
})
ProtocolGame.unregisterExtendedOpcode(Opcodes)
storeWindow:destroy()
g_keyboard.unbindKeyPress('Ctrl+X')
end
function toggle()
if storeWindow:isVisible() then
storeWindow:setFocusable(false)
storeWindow:hide()
else
storeWindow:show()
end
end
function onReceiveName(buffer)
local param = buffer:split("@")
local name = tostring(param[1])
print("Nome recebido: "..name)
end
script server:
<event type="extendedopcode" name="ExtendedOpcode" event="script" value="opcodes.lua"/>
registerCreatureEvent(cid, "ExtendedOpcode")
Code:
local OPCODES = {
GET_NAME = 180
}
function onExtendedOpcode(cid, opcode, buffer)
if opcode == OPCODES.GET_NAME then
doSendPlayerExtendedOpcode(cid, OPCODES.GET_NAME, getCreatureName(cid).."@")
end
end
return otcv8
Lua:
ERROR: protected lua call failed: /modules/gamelib/protocolgame.lua:69: Opcode is already taken.
stack traceback:
[C]: at 0x00a8a390
[C]: in function 'error'
/modules/gamelib/protocolgame.lua:69: in function 'registerExtendedOpcode'
/modules/game_testes/testes.lua:11: in function 'init'
opcodes in source
protocolgame.cpp
Code:
void ProtocolGame::parseExtendedOpcode(NetworkMessage& msg)
{
uint8_t opcode = msg.get<char>();
std::string buffer = msg.getString();
// process additional opcodes via lua script event
addGameTask(&Game::parsePlayerExtendedOpcode, player->getID(), opcode, buffer);
}
void ProtocolGame::sendExtendedOpcode(uint8_t opcode, const std::string& buffer)
{
// extended opcodes can only be send to players using otclient, cipsoft's tibia can't understand them
if(player && !player->isUsingOtclient())
return;
NetworkMessage_ptr msg = getOutputBuffer();
if(!msg)
return;
TRACK_MESSAGE(msg);
msg->put<char>(0x32);
msg->put<char>(opcode);
msg->putString(buffer);
}
const.h
Code:
GameExtendedOpcode = 80,
luascript.h
Code:
static int32_t luaDoSendPlayerExtendedOpcode(lua_State* L);
player.h
Code:
void sendExtendedOpcode(uint8_t opcode, const std::string& buffer)
{
if (client) client->sendExtendedOpcode(opcode, buffer);
}
creatureevent.h
1 -
Code:
CREATURE_EVENT_EXTENDED_OPCODE,
2 -
Code:
uint32_t executeExtendedOpcode(Creature* creature, uint8_t opcode, const std::string& buffer);
creatureevent.cpp
Code:
else if(type == "extendedopcode")
_type = CREATURE_EVENT_EXTENDED_OPCODE;
Code:
case CREATURE_EVENT_EXTENDED_OPCODE:
return "onExtendedOpcode";
Code:
case CREATURE_EVENT_EXTENDED_OPCODE:
return "cid, opcode, buffer";
Code:
uint32_t CreatureEvent::executeExtendedOpcode(Creature* creature, uint8_t opcode, const std::string& buffer)
{
//onExtendedOpcode(cid, opcode, buffer)
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;
scriptstream << "local opcode = " << (int)opcode << std::endl;
scriptstream << "local buffer = " << buffer.c_str() << std::endl;
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[35];
sprintf(desc, "%s", creature->getName().c_str());
env->setEvent(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));
lua_pushnumber(L, opcode);
lua_pushlstring(L, buffer.c_str(), buffer.length());
bool result = m_interface->callFunction(3);
m_interface->releaseEnv();
return result;
}
}
else
{
std::cout << "[Error - CreatureEvent::executeRemoved] Call stack overflow." << std::endl;
return 0;
}
}
game.cpp
Code:
void Game::parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const std::string& buffer)
{
Player* player = getPlayerByID(playerId);
if(!player || player->isRemoved())
return;
CreatureEventList extendedOpcodeEvents = player->getCreatureEvents(CREATURE_EVENT_EXTENDED_OPCODE);
for(CreatureEventList::iterator it = extendedOpcodeEvents.begin(); it != extendedOpcodeEvents.end(); ++it)
(*it)->executeExtendedOpcode(player, opcode, buffer);
}
Post automatically merged:
game.h
Code:
void parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const std::string& buffer);
spectators.h
Code:
void sendExtendedOpcode(uint8_t opcode, const std::string& buffer)
{
if(!owner)
return;
owner->sendExtendedOpcode(opcode, buffer);
applied this fix
Opcode fix · mattyx14/otxserver@b4b7e35
OTX Server from:. Contribute to mattyx14/otxserver development by creating an account on GitHub.
github.com
Last edited: