diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 9b96adef..47a7327b 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -105,7 +105,7 @@ void Game::processTextMessage(int type, const std::string& message) void Game::processInventoryChange(int slot, const ItemPtr& item) { - g_dispatcher.addEvent([=] { + g_dispatcher.addEvent([slot, item] { g_lua.callGlobalField("Game","onInventoryChange", slot, item); }); } diff --git a/src/otclient/core/map.h b/src/otclient/core/map.h index 07032cbe..a2533a47 100644 --- a/src/otclient/core/map.h +++ b/src/otclient/core/map.h @@ -28,6 +28,7 @@ class Map { +public: enum { PLAYER_OFFSET_X = 8, PLAYER_OFFSET_Y = 6, @@ -40,7 +41,6 @@ class Map NUM_TILE_PIXELS = 32 }; -public: void draw(const Rect& rect); void clean(); diff --git a/src/otclient/core/tile.cpp b/src/otclient/core/tile.cpp index 32353b8a..e9a5bff0 100644 --- a/src/otclient/core/tile.cpp +++ b/src/otclient/core/tile.cpp @@ -27,6 +27,7 @@ #include "game.h" #include "localplayer.h" #include "effect.h" +#include #include Tile::Tile(const Position& position) @@ -228,3 +229,31 @@ bool Tile::isLookPossible() } return true; } + +// TODO: +/* + Get menu options + + if invited to party + if creature, attack and follow + if item, use or use with +*/ + +void Tile::useItem() +{ + // Get top item of stack priority 2 (do a function to do this later) + ThingPtr thing; + int lastStackpos = -1; + for(int stackPos = 0; stackPos < (int)m_things.size(); ++stackPos) { + int otherPriority = m_things[stackPos]->getStackPriority(); + if(otherPriority == 2) { + thing = m_things[stackPos]; + lastStackpos = stackPos; + } + } + + if(lastStackpos != -1) { + // use this + g_game.getProtocolGame()->sendUseItem(m_position, thing->getId(), lastStackpos, 0); // 0 has something to do with container + } +} diff --git a/src/otclient/core/tile.h b/src/otclient/core/tile.h index ba90c663..d1b9d76a 100644 --- a/src/otclient/core/tile.h +++ b/src/otclient/core/tile.h @@ -53,6 +53,8 @@ public: bool isFullyOpaque(); bool isLookPossible(); + void useItem(); + TilePtr asTile() { return std::static_pointer_cast(shared_from_this()); } private: diff --git a/src/otclient/net/protocolgame.h b/src/otclient/net/protocolgame.h index 3d9567e6..55d5c1e3 100644 --- a/src/otclient/net/protocolgame.h +++ b/src/otclient/net/protocolgame.h @@ -54,6 +54,7 @@ public: void sendTurnEast(); void sendTurnSouth(); void sendTurnWest(); + void sendUseItem(const Position& position, int itemId, int stackpos, int index); void sendTalk(int channelType, int channelId, const std::string& receiver, const std::string& message); void sendAddVip(const std::string& name); void sendRemoveVip(int id); @@ -140,6 +141,7 @@ private: ThingPtr internalGetThing(InputMessage& msg); ItemPtr internalGetItem(InputMessage& msg, uint16 id); + void addPosition(OutputMessage& msg, const Position& position); Position parsePosition(InputMessage& msg); private: diff --git a/src/otclient/net/protocolgamesend.cpp b/src/otclient/net/protocolgamesend.cpp index a74c4616..c4122f9f 100644 --- a/src/otclient/net/protocolgamesend.cpp +++ b/src/otclient/net/protocolgamesend.cpp @@ -158,6 +158,17 @@ void ProtocolGame::sendTurnWest() send(oMsg); } +void ProtocolGame::sendUseItem(const Position& position, int itemId, int stackpos, int index) +{ + OutputMessage oMsg; + oMsg.addU8(Otc::ClientUseObject); + addPosition(oMsg, position); + oMsg.addU16(itemId); + oMsg.addU8(stackpos); + oMsg.addU8(index); + send(oMsg); +} + void ProtocolGame::sendTalk(int channelType, int channelId, const std::string& receiver, const std::string& message) { if(message.length() > 255 || message.length() <= 0) @@ -199,3 +210,10 @@ void ProtocolGame::sendRemoveVip(int id) oMsg.addU32(id); send(oMsg); } + +void ProtocolGame::addPosition(OutputMessage& msg, const Position& position) +{ + msg.addU16(position.x); + msg.addU16(position.y); + msg.addU8(position.z); +} diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index 99f0906f..2f198d1c 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -26,6 +26,8 @@ #include #include #include +#include +#include void UIMap::setup() { @@ -141,13 +143,27 @@ void UIMap::onStyleApply(const OTMLNodePtr& styleNode) bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button) { + if(m_mapRect.contains(mousePos)) { + Point relativeStretchMousePos = mousePos - m_mapRect.topLeft(); + Size mapSize(Map::MAP_VISIBLE_WIDTH * Map::NUM_TILE_PIXELS, Map::MAP_VISIBLE_HEIGHT * Map::NUM_TILE_PIXELS); + + PointF stretchFactor(m_mapRect.width() / (float)mapSize.width(), m_mapRect.height() / (float)mapSize.height()); + PointF relativeMousePos = PointF(relativeStretchMousePos.x, relativeStretchMousePos.y) / stretchFactor; + + PointF tilePosF = relativeMousePos / Map::NUM_TILE_PIXELS; + Position tilePos = Position(1 + (int)tilePosF.x - Map::PLAYER_OFFSET_X, 1 + (int)tilePosF.y - Map::PLAYER_OFFSET_Y, 0) + g_game.getLocalPlayer()->getPosition(); + + TilePtr tile = g_map.getTile(tilePos); + tile->useItem(); + } + return UIWidget::onMousePress(mousePos, button); } void UIMap::onGeometryUpdate(const Rect& oldRect, const Rect& newRect) { Rect mapRect = newRect.expanded(-m_mapMargin-1); - Size mapSize(15*32, 11*32); + Size mapSize(Map::MAP_VISIBLE_WIDTH * Map::NUM_TILE_PIXELS, Map::MAP_VISIBLE_HEIGHT * Map::NUM_TILE_PIXELS); mapSize.scale(mapRect.size(), Fw::KeepAspectRatio); m_mapRect.setSize(mapSize); m_mapRect.moveCenter(newRect.center());