From 733039e50e6fc359d0c21783dbb2ccf00861c0a6 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Thu, 19 Jan 2012 23:12:26 -0200 Subject: [PATCH] init move items --- modules/core_widgets/uiitem.lua | 26 +++++++++++++++++++++++ modules/game/map.lua | 29 ++++++++++++++++++++++++++ modules/game_containers/containers.lua | 1 + modules/game_inventory/inventory.otui | 9 ++++++++ src/otclient/core/game.cpp | 14 +++++++++++++ src/otclient/core/game.h | 1 + src/otclient/core/tile.cpp | 17 +++++++++++++++ src/otclient/core/tile.h | 1 + src/otclient/luafunctions.cpp | 2 ++ src/otclient/ui/uimap.cpp | 13 ++++++++++-- src/otclient/ui/uimap.h | 1 + 11 files changed, 112 insertions(+), 2 deletions(-) diff --git a/modules/core_widgets/uiitem.lua b/modules/core_widgets/uiitem.lua index 5c539ada..292d7118 100644 --- a/modules/core_widgets/uiitem.lua +++ b/modules/core_widgets/uiitem.lua @@ -1,3 +1,29 @@ +function UIItem:onDragEnter(mousePos) + local item = self:getItem() + if not item then return false end + + self.currentDragThing = item + setTargetCursor() + return true +end + +function UIItem:onDragLeave(widget, mousePos) + self.currentDragThing = nil + restoreCursor() + return true +end + +function UIItem:onDrop(widget, mousePos) + if not widget or not widget.currentDragThing then return false end + + local pos = self.position + local count = 1 -- todo make a window for it + + Game.move(widget.currentDragThing, pos, count) + return true +end + + function UIItem:onMouseRelease(mousePosition, mouseButton) local item = self:getItem() if not item or not self:containsPoint(mousePosition) then return false end diff --git a/modules/game/map.lua b/modules/game/map.lua index 24ba4c08..477bb9d7 100644 --- a/modules/game/map.lua +++ b/modules/game/map.lua @@ -1,5 +1,34 @@ +function UIMap:onDragEnter(mousePos) + local tile = self:getTile(mousePosition) + if not tile then return false end + + local thing = tile:getTopMoveThing() + if not thing then return false end + + self.currentDragThing = thing + setTargetCursor() + return true +end + +function UIMap:onDragLeave(widget, mousePos) + self.currentDragThing = nil + restoreCursor() + return true +end + +function UIMap:onDrop(widget, mousePos) + if not widget or not widget.currentDragThing then return false end + + local pos = self:getPosition(mousePos) + local count = 1 -- todo make a window for it + + Game.move(widget.currentDragThing, pos, count) + return true +end + function UIMap:onMouseRelease(mousePosition, mouseButton) local tile = self:getTile(mousePosition) if tile and Game.processMouseAction(mousePosition, mouseButton, nil, tile:getTopLookThing(), tile:getTopUseThing(), tile:getTopCreature(), tile:getTopMultiUseThing()) then return true end return false end + diff --git a/modules/game_containers/containers.lua b/modules/game_containers/containers.lua index 59796293..a2794c18 100644 --- a/modules/game_containers/containers.lua +++ b/modules/game_containers/containers.lua @@ -42,6 +42,7 @@ function Containers.onContainerOpen(containerId, itemId, name, capacity, hasPare local item = UIItem.create() item:setStyle('Item') container:addChild(item) + item.position = {x=65535, y=containerId+64, z=i-1} end m_containers[containerId] = container diff --git a/modules/game_inventory/inventory.otui b/modules/game_inventory/inventory.otui index aae4daae..33a6771a 100644 --- a/modules/game_inventory/inventory.otui +++ b/modules/game_inventory/inventory.otui @@ -11,6 +11,7 @@ UIWindow id: slot1 anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter + &position: {x=65535, y=1, z=0} Item // armor @@ -18,6 +19,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=4, z=0} Item // legs @@ -25,6 +27,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=7, z=0} Item // feet @@ -32,6 +35,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=8, z=0} Item // necklace @@ -40,6 +44,7 @@ UIWindow anchors.right: slot1.left margin-top: 10 margin-right: 5 + &position: {x=65535, y=2, z=0} Item // left @@ -47,6 +52,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=6, z=0} Item // ring @@ -54,6 +60,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=9, z=0} Item // backpack @@ -62,6 +69,7 @@ UIWindow anchors.left: slot1.right margin-top: 10 margin-left: 5 + &position: {x=65535, y=3, z=0} Item // right @@ -76,6 +84,7 @@ UIWindow anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 + &position: {x=65535, y=10, z=0} GameLabel id: soul diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 2395249c..cbc6d166 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -299,6 +299,20 @@ void Game::useInventoryItem(int itemId, const ThingPtr& toThing) } } +void Game::move(const ThingPtr& thing, const Position& toPos, int count) +{ + if(!isOnline() || !thing || !checkBotProtection() || thing->getPos() == toPos || count <= 0) + return; + + m_localPlayer->lockWalk(); + + int stackpos = getThingStackpos(thing); + if(stackpos == -1) + return; + + m_protocolGame->sendThrow(thing->getPos(), thing->getId(), stackpos, toPos, count); +} + void Game::attack(const CreaturePtr& creature) { if(!isOnline() || !creature || !checkBotProtection()) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 6fafe365..9b6f1f05 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -65,6 +65,7 @@ public: void use(const ThingPtr& thing); void useWith(const ThingPtr& fromThing, const ThingPtr& toThing); void useInventoryItem(int itemId, const ThingPtr& toThing); + void move(const ThingPtr &thing, const Position& toPos, int count); // attack/follow related void attack(const CreaturePtr& creature); diff --git a/src/otclient/core/tile.cpp b/src/otclient/core/tile.cpp index 45b245f6..cfa3c446 100644 --- a/src/otclient/core/tile.cpp +++ b/src/otclient/core/tile.cpp @@ -247,6 +247,23 @@ CreaturePtr Tile::getTopCreature() return creature; } +ThingPtr Tile::getTopMoveThing() +{ + if(isEmpty()) + return nullptr; + + for(uint i = 0; i < m_things.size(); ++i) { + ThingPtr thing = m_things[i]; + if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->asCreature()) { + if(i > 0 && thing->isNotMoveable()) + return m_things[i-1]; + return thing; + } + } + + return m_things[0]; +} + ThingPtr Tile::getTopMultiUseThing() { // this is related to classic controls, getting top item, forceuse or creature diff --git a/src/otclient/core/tile.h b/src/otclient/core/tile.h index c8a478a0..4aec1107 100644 --- a/src/otclient/core/tile.h +++ b/src/otclient/core/tile.h @@ -48,6 +48,7 @@ public: ThingPtr getTopLookThing(); ThingPtr getTopUseThing(); CreaturePtr getTopCreature(); + ThingPtr getTopMoveThing(); ThingPtr getTopMultiUseThing(); const Position& getPos() { return m_pos; } diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index dfda580b..5b738d0f 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -169,6 +169,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getTopLookThing", &Tile::getTopLookThing); g_lua.bindClassMemberFunction("getTopUseThing", &Tile::getTopUseThing); g_lua.bindClassMemberFunction("getTopCreature", &Tile::getTopCreature); + g_lua.bindClassMemberFunction("getTopMoveThing", &Tile::getTopMoveThing); g_lua.bindClassMemberFunction("getTopMultiUseThing", &Tile::getTopMultiUseThing); g_lua.bindClassMemberFunction("getPos", &Tile::getPos); g_lua.bindClassMemberFunction("getDrawElevation", &Tile::getDrawElevation); @@ -237,6 +238,7 @@ void OTClient::registerLuaFunctions() g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return UIMapPtr(new UIMap); } ); g_lua.bindClassMemberFunction("getTile", &UIMap::getTile); + g_lua.bindClassMemberFunction("getPosition", &UIMap::getPosition); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return UIGamePtr(new UIGame); } ); diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index 8c620961..21cdb664 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -38,10 +38,10 @@ void UIMap::draw() drawChildren(); } -TilePtr UIMap::getTile(const Point& mousePos) +Position UIMap::getPosition(const Point& mousePos) { if(!m_mapRect.contains(mousePos)) - return nullptr; + return Position(); // Get tile position Point relativeStretchMousePos = mousePos - m_mapRect.topLeft(); @@ -58,6 +58,15 @@ TilePtr UIMap::getTile(const Point& mousePos) PointF tilePosF = relativeMousePos / Map::NUM_TILE_PIXELS; Position tilePos = Position(1 + (int)tilePosF.x - g_map.getCentralOffset().x, 1 + (int)tilePosF.y - g_map.getCentralOffset().y, 0) + g_map.getCentralPosition(); + return tilePos; +} + +TilePtr UIMap::getTile(const Point& mousePos) +{ + Position tilePos = getPosition(mousePos); + if(!tilePos.isValid()) + return nullptr; + // Get tile TilePtr tile = nullptr; diff --git a/src/otclient/ui/uimap.h b/src/otclient/ui/uimap.h index 13020238..87e14465 100644 --- a/src/otclient/ui/uimap.h +++ b/src/otclient/ui/uimap.h @@ -32,6 +32,7 @@ class UIMap : public UIWidget public: void draw(); + Position getPosition(const Point& mousePos); TilePtr getTile(const Point& mousePos); protected: