some cleanup

master
Eduardo Bart 12 years ago
parent 7ed81799b4
commit e9411aa244

@ -306,19 +306,15 @@ void Game::look(const ThingPtr& thing)
if(!isOnline() || !thing || !checkBotProtection()) if(!isOnline() || !thing || !checkBotProtection())
return; return;
int stackpos = getThingStackpos(thing); m_protocolGame->sendLookAt(thing->getPosition(), thing->getId(), thing->getStackpos());
if(stackpos != -1)
m_protocolGame->sendLookAt(thing->getPosition(), thing->getId(), stackpos);
} }
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; return;
int stackpos = getThingStackpos(thing); m_protocolGame->sendUseItem(item->getPosition(), item->getId(), item->getStackpos(), containerId);
if(stackpos != -1)
m_protocolGame->sendUseItem(thing->getPosition(), thing->getId(), stackpos, containerId);
} }
void Game::use(const ThingPtr& thing) void Game::use(const ThingPtr& thing)
@ -326,40 +322,44 @@ void Game::use(const ThingPtr& thing)
if(!isOnline() || !thing || !checkBotProtection()) if(!isOnline() || !thing || !checkBotProtection())
return; 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(); m_localPlayer->lockWalk();
int stackpos = getThingStackpos(thing); m_protocolGame->sendUseItem(pos, thing->getId(), thing->getStackpos(), 0);
if(stackpos != -1)
m_protocolGame->sendUseItem(thing->getPosition(), thing->getId(), stackpos, 0);
} }
void Game::useWith(const ThingPtr& fromThing, const ThingPtr& toThing) void Game::useInventoryItem(int itemId)
{ {
if(!isOnline() || !fromThing || !toThing || !checkBotProtection()) if(!isOnline() || !checkBotProtection())
return; return;
Position pos = fromThing->getPosition(); Position pos = Position(0xFFFF, 0, 0); // means that is a item in inventory
if(!pos.isValid()) // virtual item m_localPlayer->lockWalk();
pos = Position(0xFFFF, 0, 0); // means that is a item in inventory
m_protocolGame->sendUseItem(pos, itemId, 0, 0);
}
int fromStackpos = getThingStackpos(fromThing); void Game::useWith(const ItemPtr& item, const ThingPtr& toThing)
if(fromStackpos == -1) {
if(!isOnline() || !item || !toThing || !checkBotProtection())
return; 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_localPlayer->lockWalk();
m_protocolGame->sendUseOnCreature(pos, fromThing->getId(), fromStackpos, creature->getId());
} else {
int toStackpos = getThingStackpos(toThing);
if(toStackpos == -1)
return;
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()) if(!isOnline() || !toThing || !checkBotProtection())
return; return;
@ -367,15 +367,11 @@ void Game::useInventoryItem(int itemId, const ThingPtr& toThing)
m_localPlayer->lockWalk(); m_localPlayer->lockWalk();
Position 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
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()); m_protocolGame->sendUseOnCreature(pos, itemId, 0, creature->getId());
} else { else
m_protocolGame->sendUseItemEx(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toStackpos); m_protocolGame->sendUseItemEx(pos, itemId, 0, toThing->getPosition(), toThing->getId(), toThing->getStackpos());
}
} }
void Game::move(const ThingPtr& thing, const Position& toPos, int count) 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(); m_localPlayer->lockWalk();
int stackpos = getThingStackpos(thing); m_protocolGame->sendThrow(thing->getPosition(), thing->getId(), thing->getStackpos(), toPos, count);
if(stackpos == -1)
return;
m_protocolGame->sendThrow(thing->getPosition(), thing->getId(), stackpos, toPos, count);
} }
void Game::attack(const CreaturePtr& creature) void Game::attack(const CreaturePtr& creature)
@ -440,26 +432,7 @@ void Game::rotate(const ThingPtr& thing)
if(!isOnline() || !thing || !checkBotProtection()) if(!isOnline() || !thing || !checkBotProtection())
return; return;
int stackpos = getThingStackpos(thing); m_protocolGame->sendRotateItem(thing->getPosition(), thing->getId(), thing->getStackpos());
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;
} }
void Game::talk(const std::string& message) void Game::talk(const std::string& message)

@ -67,10 +67,11 @@ public:
// item related // item related
void look(const ThingPtr& thing); 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 use(const ThingPtr& thing);
void useWith(const ThingPtr& fromThing, const ThingPtr& toThing); void useWith(const ItemPtr& fromThing, const ThingPtr& toThing);
void useInventoryItem(int itemId, const ThingPtr& toThing); void useInventoryItem(int itemId);
void useInventoryItemWith(int itemId, const ThingPtr& toThing);
void move(const ThingPtr &thing, const Position& toPos, int count); void move(const ThingPtr &thing, const Position& toPos, int count);
// attack/follow related // attack/follow related
@ -106,8 +107,6 @@ public:
void addVip(const std::string& name); void addVip(const std::string& name);
void removeVip(int playerId); void removeVip(int playerId);
int getThingStackpos(const ThingPtr& thing);
bool checkBotProtection(); bool checkBotProtection();
bool isOnline() { return !!m_localPlayer; } bool isOnline() { return !!m_localPlayer; }

@ -55,10 +55,14 @@ const TilePtr& Thing::getTile()
int Thing::getStackpos() int Thing::getStackpos()
{ {
const TilePtr& tile = getTile(); if(m_position.x == 65535 && asItem()) // is inside a container
if(tile) return 0;
else if(const TilePtr& tile = getTile())
return tile->getThingStackpos(asThing()); 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) void Thing::internalDraw(const Point& dest, float scaleFactor, int w, int h, int xPattern, int yPattern, int zPattern, int layer, int animationPhase)

@ -197,9 +197,10 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassStaticFunction<Game>("look", std::bind(&Game::look, &g_game, _1)); g_lua.bindClassStaticFunction<Game>("look", std::bind(&Game::look, &g_game, _1));
g_lua.bindClassStaticFunction<Game>("open", std::bind(&Game::open, &g_game, _1, _2)); g_lua.bindClassStaticFunction<Game>("open", std::bind(&Game::open, &g_game, _1, _2));
g_lua.bindClassStaticFunction<Game>("use", std::bind(&Game::use, &g_game, _1)); g_lua.bindClassStaticFunction<Game>("use", std::bind(&Game::use, &g_game, _1));
g_lua.bindClassStaticFunction<Game>("useInventoryItem", std::bind(&Game::useInventoryItem, &g_game, _1));
g_lua.bindClassStaticFunction<Game>("useWith", std::bind(&Game::useWith, &g_game, _1, _2)); g_lua.bindClassStaticFunction<Game>("useWith", std::bind(&Game::useWith, &g_game, _1, _2));
g_lua.bindClassStaticFunction<Game>("useInventoryItemWith", std::bind(&Game::useInventoryItemWith, &g_game, _1, _2));
g_lua.bindClassStaticFunction<Game>("move", std::bind(&Game::move, &g_game, _1, _2, _3)); g_lua.bindClassStaticFunction<Game>("move", std::bind(&Game::move, &g_game, _1, _2, _3));
g_lua.bindClassStaticFunction<Game>("useInventoryItem", std::bind(&Game::useInventoryItem, &g_game, _1, _2));
g_lua.bindClassStaticFunction<Game>("turn", std::bind(&Game::turn, &g_game, _1)); g_lua.bindClassStaticFunction<Game>("turn", std::bind(&Game::turn, &g_game, _1));
g_lua.bindClassStaticFunction<Game>("walk", std::bind(&Game::walk, &g_game, _1)); g_lua.bindClassStaticFunction<Game>("walk", std::bind(&Game::walk, &g_game, _1));
g_lua.bindClassStaticFunction<Game>("forceWalk", std::bind(&Game::forceWalk, &g_game, _1)); g_lua.bindClassStaticFunction<Game>("forceWalk", std::bind(&Game::forceWalk, &g_game, _1));

@ -65,7 +65,7 @@ public:
void sendAcceptTrade(); void sendAcceptTrade();
void sendRejectTrade(); void sendRejectTrade();
void sendUseItem(const Position& position, int itemId, int stackpos, int index); 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 sendUseOnCreature(const Position& pos, int thingId, int stackpos, uint creatureId);
void sendRotateItem(const Position& pos, int thingId, int stackpos); void sendRotateItem(const Position& pos, int thingId, int stackpos);
void sendCloseContainer(int containerId); void sendCloseContainer(int containerId);

@ -262,12 +262,12 @@ void ProtocolGame::sendUseItem(const Position& position, int itemId, int stackpo
send(oMsg); 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; OutputMessage oMsg;
oMsg.addU8(Proto::ClientUseTwoObjects); oMsg.addU8(Proto::ClientUseTwoObjects);
addPosition(oMsg, fromPos); addPosition(oMsg, fromPos);
oMsg.addU16(fromThingId); oMsg.addU16(itemId);
oMsg.addU8(fromStackpos); oMsg.addU8(fromStackpos);
addPosition(oMsg, toPos); addPosition(oMsg, toPos);
oMsg.addU16(toThingId); oMsg.addU16(toThingId);

Loading…
Cancel
Save