diff --git a/modules/game/game.otmod b/modules/game/game.otmod index 240e67da..e1a954f5 100644 --- a/modules/game/game.otmod +++ b/modules/game/game.otmod @@ -15,4 +15,5 @@ Module onLoad: | require 'game' - return true \ No newline at end of file + require 'thing' + return true diff --git a/modules/game/thing.lua b/modules/game/thing.lua new file mode 100644 index 00000000..ba2eb1c6 --- /dev/null +++ b/modules/game/thing.lua @@ -0,0 +1,19 @@ + +-- public functions +function Thing:createMenu(menuPosition) + local menu = createWidget('PopupMenu') + menu:addOption('Look', function() Game.look(self) end) + + -- Open or Use, depending if thing is a container + if self:isContainer() then + menu:addOption('Open', function() print('open') end) + else + menu:addOption('Use', function() print('use') end) + end + + if self:asLocalPlayer() then + menu:addOption('Set Outfit', function() Game.openOutfitWindow() end) + end + + menu:display(menuPosition) +end diff --git a/modules/game_inventory/inventory.lua b/modules/game_inventory/inventory.lua index 99b09934..a78601bf 100644 --- a/modules/game_inventory/inventory.lua +++ b/modules/game_inventory/inventory.lua @@ -35,20 +35,7 @@ function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton) local item = itemWidget:getItem() if not item then return end - local menu = createWidget('PopupMenu') - - -- Look - local itemId = item:getId() - local slotId = tonumber(itemWidget:getId():sub(5)) - menu:addOption('Look', function() Game.lookAtInventory(itemId, slotId) end) - - -- Open or Use, depending if thing is a container - if item:isContainer() then - menu:addOption('Open', function() print('open') end) - else - menu:addOption('Use', function() print('use') end) - end - menu:display(mousePos) + item:createMenu(mousePos) end connect(Game, { onLogin = Inventory.create, diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 3d22ce27..4e728144 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -97,6 +97,9 @@ void Game::processTextMessage(int type, const std::string& message) void Game::processInventoryChange(int slot, const ItemPtr& item) { + if(item) + item->setPosition(Position(65535, slot, 0)); + g_lua.callGlobalField("Game","onInventoryChange", slot, item); } @@ -156,30 +159,30 @@ void Game::turn(Otc::Direction direction) } } -void Game::lookAtMap(const Position& position) +void Game::look(const ThingPtr& thing) { - Position tilePos = position; - TilePtr tile = nullptr; - int stackpos = -1; - - while(true) { - tile = g_map.getTile(tilePos); - stackpos = tile->getLookStackpos(); - if(stackpos != -1 || tilePos.z >= Map::MAX_Z) - break; + // thing is at map + if(thing->getPosition().x != 65535) { + Position tilePos = thing->getPosition(); + TilePtr tile = nullptr; + int stackpos = -1; - tilePos.coveredDown(); - } + while(true) { + tile = g_map.getTile(tilePos); + stackpos = tile->getLookStackpos(); + if(stackpos != -1 || tilePos.z >= Map::MAX_Z) + break; - ThingPtr thing = tile->getThing(stackpos); - if(thing) - m_protocolGame->sendLookAt(tilePos, thing->getId(), stackpos); -} + tilePos.coveredDown(); + } -void Game::lookAtInventory(int thingId, Otc::InventorySlots slot) -{ - Position pos = Position(0xffff, slot, 0); - m_protocolGame->sendLookAt(pos, thingId, 0); + ThingPtr lookThing = tile->getThing(stackpos); + if(lookThing) + m_protocolGame->sendLookAt(tilePos, lookThing->getId(), stackpos); + } + // thing is at inventory + else + m_protocolGame->sendLookAt(thing->getPosition(), thing->getId(), 0); } void Game::talkChannel(int channelType, int channelId, const std::string& message) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 0a909858..6d693305 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -48,8 +48,7 @@ public: void walk(Otc::Direction direction); void turn(Otc::Direction direction); - void lookAtMap(const Position& position); - void lookAtInventory(int thingId, Otc::InventorySlots slot); + void look(const ThingPtr& thing); void talkChannel(int channelType, int channelId, const std::string& message); void talkPrivate(int channelType, const std::string& receiver, const std::string& message); void openOutfitWindow(); diff --git a/src/otclient/core/tile.cpp b/src/otclient/core/tile.cpp index ae749c4c..789303ee 100644 --- a/src/otclient/core/tile.cpp +++ b/src/otclient/core/tile.cpp @@ -133,6 +133,14 @@ ThingPtr Tile::getThing(int stackPos) return nullptr; } +ThingPtr Tile::getTopThing() +{ + if(isEmpty()) + return nullptr; + + return m_things[m_things.size() - 1]; +} + ThingPtr Tile::removeThing(int stackPos) { ThingPtr oldObject; diff --git a/src/otclient/core/tile.h b/src/otclient/core/tile.h index a37650db..607162eb 100644 --- a/src/otclient/core/tile.h +++ b/src/otclient/core/tile.h @@ -39,6 +39,7 @@ public: ThingPtr addThing(const ThingPtr& thing, int stackPos = -1); ThingPtr getThing(int stackPos); + ThingPtr getTopThing(); ThingPtr removeThing(int stackPos); ThingPtr removeThing(const ThingPtr& thing); diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 4046f467..d4b4e2bb 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -38,6 +38,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getId", &Thing::getId); g_lua.bindClassMemberFunction("getType", &Thing::getType); g_lua.bindClassMemberFunction("isContainer", &Thing::isContainer); + g_lua.bindClassMemberFunction("asLocalPlayer", &Thing::asLocalPlayer); g_lua.registerClass(); g_lua.bindClassMemberFunction("setOutfit", &Creature::setOutfit); @@ -56,7 +57,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("isOnline", std::bind(&Game::isOnline, &g_game)); g_lua.bindClassStaticFunction("openOutfitWindow", std::bind(&Game::openOutfitWindow, &g_game)); g_lua.bindClassStaticFunction("setOutfit", std::bind(&Game::setOutfit, &g_game, _1)); - g_lua.bindClassStaticFunction("lookAtInventory", std::bind(&Game::lookAtInventory, &g_game, _1, _2)); + g_lua.bindClassStaticFunction("look", std::bind(&Game::look, &g_game, _1)); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &UIItem::create); diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index 2bdefb6f..f04fce51 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -90,9 +90,11 @@ bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button) //tile->useItem(); if(button == Fw::MouseLeftButton) { + g_game.look(tile->getThing(0)); } else if(button == Fw::MouseRightButton) { - //g_game.lookAtMap(tilePos); + + g_lua.callGlobalField("Thing","createMenu", tile->getTopThing(), mousePos); } return true;