From e9411aa244ca60fe41cd365a219137876f86dce7 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 7 Feb 2012 01:33:36 -0200 Subject: [PATCH] some cleanup --- src/otclient/core/game.cpp | 93 ++++++++++----------------- src/otclient/core/game.h | 9 ++- src/otclient/core/thing.cpp | 10 ++- src/otclient/luafunctions.cpp | 3 +- src/otclient/net/protocolgame.h | 2 +- src/otclient/net/protocolgamesend.cpp | 4 +- 6 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 2970db00..3a9fd83d 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -306,19 +306,15 @@ void Game::look(const ThingPtr& thing) if(!isOnline() || !thing || !checkBotProtection()) return; - int stackpos = getThingStackpos(thing); - if(stackpos != -1) - m_protocolGame->sendLookAt(thing->getPosition(), thing->getId(), stackpos); + m_protocolGame->sendLookAt(thing->getPosition(), thing->getId(), thing->getStackpos()); } -void Game::open(const ThingPtr& thing, int containerId) +void Game::open(const ItemPtr& item, int containerId) { - if(!isOnline() || !thing || !checkBotProtection()) + if(!isOnline() || !item || !checkBotProtection()) return; - int stackpos = getThingStackpos(thing); - if(stackpos != -1) - m_protocolGame->sendUseItem(thing->getPosition(), thing->getId(), stackpos, containerId); + m_protocolGame->sendUseItem(item->getPosition(), item->getId(), item->getStackpos(), containerId); } void Game::use(const ThingPtr& thing) @@ -326,40 +322,44 @@ void Game::use(const ThingPtr& thing) if(!isOnline() || !thing || !checkBotProtection()) return; + Position pos = thing->getPosition(); + if(!pos.isValid()) // virtual item + pos = Position(0xFFFF, 0, 0); // means that is a item in inventory + m_localPlayer->lockWalk(); - int stackpos = getThingStackpos(thing); - if(stackpos != -1) - m_protocolGame->sendUseItem(thing->getPosition(), thing->getId(), stackpos, 0); + m_protocolGame->sendUseItem(pos, thing->getId(), thing->getStackpos(), 0); } -void Game::useWith(const ThingPtr& fromThing, const ThingPtr& toThing) +void Game::useInventoryItem(int itemId) { - if(!isOnline() || !fromThing || !toThing || !checkBotProtection()) + if(!isOnline() || !checkBotProtection()) return; - Position pos = fromThing->getPosition(); - if(!pos.isValid()) // virtual item - pos = Position(0xFFFF, 0, 0); // means that is a item in inventory + Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory + m_localPlayer->lockWalk(); + + m_protocolGame->sendUseItem(pos, itemId, 0, 0); +} - int fromStackpos = getThingStackpos(fromThing); - if(fromStackpos == -1) +void Game::useWith(const ItemPtr& item, const ThingPtr& toThing) +{ + if(!isOnline() || !item || !toThing || !checkBotProtection()) return; - m_localPlayer->lockWalk(); + Position pos = item->getPosition(); + if(!pos.isValid()) // virtual item + pos = Position(0xFFFF, 0, 0); // means that is a item in inventory - if(CreaturePtr creature = toThing->asCreature()) { - m_protocolGame->sendUseOnCreature(pos, fromThing->getId(), fromStackpos, creature->getId()); - } else { - int toStackpos = getThingStackpos(toThing); - if(toStackpos == -1) - return; + m_localPlayer->lockWalk(); - m_protocolGame->sendUseItemEx(pos, fromThing->getId(), fromStackpos, toThing->getPosition(), toThing->getId(), toStackpos); - } + if(CreaturePtr creature = toThing->asCreature()) + m_protocolGame->sendUseOnCreature(pos, item->getId(), item->getStackpos(), creature->getId()); + else + m_protocolGame->sendUseItemEx(pos, item->getId(), item->getStackpos(), toThing->getPosition(), toThing->getId(), toThing->getStackpos()); } -void Game::useInventoryItem(int itemId, const ThingPtr& toThing) +void Game::useInventoryItemWith(int itemId, const ThingPtr& toThing) { if(!isOnline() || !toThing || !checkBotProtection()) return; @@ -367,15 +367,11 @@ void Game::useInventoryItem(int itemId, const ThingPtr& toThing) m_localPlayer->lockWalk(); Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory - int toStackpos = getThingStackpos(toThing); - if(toStackpos == -1) - return; - if(CreaturePtr creature = toThing->asCreature()) { + if(CreaturePtr creature = toThing->asCreature()) m_protocolGame->sendUseOnCreature(pos, itemId, 0, creature->getId()); - } else { - m_protocolGame->sendUseItemEx(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toStackpos); - } + else + m_protocolGame->sendUseItemEx(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toThing->getStackpos()); } void Game::move(const ThingPtr& thing, const Position& toPos, int count) @@ -385,11 +381,7 @@ void Game::move(const ThingPtr& thing, const Position& toPos, int count) m_localPlayer->lockWalk(); - int stackpos = getThingStackpos(thing); - if(stackpos == -1) - return; - - m_protocolGame->sendThrow(thing->getPosition(), thing->getId(), stackpos, toPos, count); + m_protocolGame->sendThrow(thing->getPosition(), thing->getId(), thing->getStackpos(), toPos, count); } void Game::attack(const CreaturePtr& creature) @@ -440,26 +432,7 @@ void Game::rotate(const ThingPtr& thing) if(!isOnline() || !thing || !checkBotProtection()) return; - int stackpos = getThingStackpos(thing); - if(stackpos != -1) - m_protocolGame->sendRotateItem(thing->getPosition(), thing->getId(), stackpos); -} - -//TODO: move this to Thing class -int Game::getThingStackpos(const ThingPtr& thing) -{ - // thing is at map - if(thing->getPosition().x != 65535) { - if(TilePtr tile = g_map.getTile(thing->getPosition())) - return tile->getThingStackpos(thing); - else { - logError("could not get tile"); - return -1; - } - } - - // thing is at container or inventory - return 0; + m_protocolGame->sendRotateItem(thing->getPosition(), thing->getId(), thing->getStackpos()); } void Game::talk(const std::string& message) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 9d83c81f..48b3fe23 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -67,10 +67,11 @@ public: // item related void look(const ThingPtr& thing); - void open(const ThingPtr& thing, int containerId); + void open(const ItemPtr& item, int containerId); void use(const ThingPtr& thing); - void useWith(const ThingPtr& fromThing, const ThingPtr& toThing); - void useInventoryItem(int itemId, const ThingPtr& toThing); + void useWith(const ItemPtr& fromThing, const ThingPtr& toThing); + void useInventoryItem(int itemId); + void useInventoryItemWith(int itemId, const ThingPtr& toThing); void move(const ThingPtr &thing, const Position& toPos, int count); // attack/follow related @@ -106,8 +107,6 @@ public: void addVip(const std::string& name); void removeVip(int playerId); - int getThingStackpos(const ThingPtr& thing); - bool checkBotProtection(); bool isOnline() { return !!m_localPlayer; } diff --git a/src/otclient/core/thing.cpp b/src/otclient/core/thing.cpp index c886806d..66aa5151 100644 --- a/src/otclient/core/thing.cpp +++ b/src/otclient/core/thing.cpp @@ -55,10 +55,14 @@ const TilePtr& Thing::getTile() int Thing::getStackpos() { - const TilePtr& tile = getTile(); - if(tile) + if(m_position.x == 65535 && asItem()) // is inside a container + return 0; + else if(const TilePtr& tile = getTile()) return tile->getThingStackpos(asThing()); - return -1; + else { + logTraceError("got a thing with invalid stackpos"); + return -1; + } } void Thing::internalDraw(const Point& dest, float scaleFactor, int w, int h, int xPattern, int yPattern, int zPattern, int layer, int animationPhase) diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 1a0bfa3a..0549ed0b 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -197,9 +197,10 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("look", std::bind(&Game::look, &g_game, _1)); g_lua.bindClassStaticFunction("open", std::bind(&Game::open, &g_game, _1, _2)); g_lua.bindClassStaticFunction("use", std::bind(&Game::use, &g_game, _1)); + g_lua.bindClassStaticFunction("useInventoryItem", std::bind(&Game::useInventoryItem, &g_game, _1)); g_lua.bindClassStaticFunction("useWith", std::bind(&Game::useWith, &g_game, _1, _2)); + g_lua.bindClassStaticFunction("useInventoryItemWith", std::bind(&Game::useInventoryItemWith, &g_game, _1, _2)); g_lua.bindClassStaticFunction("move", std::bind(&Game::move, &g_game, _1, _2, _3)); - g_lua.bindClassStaticFunction("useInventoryItem", std::bind(&Game::useInventoryItem, &g_game, _1, _2)); g_lua.bindClassStaticFunction("turn", std::bind(&Game::turn, &g_game, _1)); g_lua.bindClassStaticFunction("walk", std::bind(&Game::walk, &g_game, _1)); g_lua.bindClassStaticFunction("forceWalk", std::bind(&Game::forceWalk, &g_game, _1)); diff --git a/src/otclient/net/protocolgame.h b/src/otclient/net/protocolgame.h index e97b6d13..b8eaa158 100644 --- a/src/otclient/net/protocolgame.h +++ b/src/otclient/net/protocolgame.h @@ -65,7 +65,7 @@ public: void sendAcceptTrade(); void sendRejectTrade(); void sendUseItem(const Position& position, int itemId, int stackpos, int index); - void sendUseItemEx(const Position& fromPos, int fromThingId, int fromStackpos, const Position& toPos, int toThingId, int toStackpos); + void sendUseItemEx(const Position& fromPos, int itemId, int fromStackpos, const Position& toPos, int toThingId, int toStackpos); void sendUseOnCreature(const Position& pos, int thingId, int stackpos, uint creatureId); void sendRotateItem(const Position& pos, int thingId, int stackpos); void sendCloseContainer(int containerId); diff --git a/src/otclient/net/protocolgamesend.cpp b/src/otclient/net/protocolgamesend.cpp index 3ef16737..ea570d4b 100644 --- a/src/otclient/net/protocolgamesend.cpp +++ b/src/otclient/net/protocolgamesend.cpp @@ -262,12 +262,12 @@ void ProtocolGame::sendUseItem(const Position& position, int itemId, int stackpo send(oMsg); } -void ProtocolGame::sendUseItemEx(const Position& fromPos, int fromThingId, int fromStackpos, const Position& toPos, int toThingId, int toStackpos) +void ProtocolGame::sendUseItemEx(const Position& fromPos, int itemId, int fromStackpos, const Position& toPos, int toThingId, int toStackpos) { OutputMessage oMsg; oMsg.addU8(Proto::ClientUseTwoObjects); addPosition(oMsg, fromPos); - oMsg.addU16(fromThingId); + oMsg.addU16(itemId); oMsg.addU8(fromStackpos); addPosition(oMsg, toPos); oMsg.addU16(toThingId);