From ea70f90e92548be0130d88d11f3cd76041a5e126 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Thu, 12 Jan 2012 22:31:39 -0200 Subject: [PATCH] improve containers --- modules/core_widgets/core_widgets.otmod | 4 +- modules/core_widgets/uiitem.lua | 6 ++ modules/game/game.otmod | 3 +- modules/game/thing.lua | 40 +++++++++--- modules/game_containers/container.otui | 13 ++++ modules/game_containers/containers.lua | 79 ++++++++++++++++++++++++ modules/game_containers/containers.otmod | 7 +++ modules/game_inventory/inventory.lua | 6 -- modules/game_inventory/inventory.otui | 23 +++---- src/otclient/core/game.cpp | 14 ++++- src/otclient/core/game.h | 1 + src/otclient/luafunctions.cpp | 1 + src/otclient/net/protocolgameparse.cpp | 6 +- src/otclient/util/position.h | 2 +- 14 files changed, 169 insertions(+), 36 deletions(-) create mode 100644 modules/core_widgets/uiitem.lua create mode 100644 modules/game_containers/container.otui create mode 100644 modules/game_containers/containers.lua create mode 100644 modules/game_containers/containers.otmod diff --git a/modules/core_widgets/core_widgets.otmod b/modules/core_widgets/core_widgets.otmod index 12d9f79a..62e085d3 100644 --- a/modules/core_widgets/core_widgets.otmod +++ b/modules/core_widgets/core_widgets.otmod @@ -14,5 +14,7 @@ Module require 'uitabbar' require 'uipopupmenu' require 'uiwindow' + require 'uiitem' require 'tooltip/tooltip' - require 'messagebox/messagebox' \ No newline at end of file + require 'messagebox/messagebox' + diff --git a/modules/core_widgets/uiitem.lua b/modules/core_widgets/uiitem.lua new file mode 100644 index 00000000..5c539ada --- /dev/null +++ b/modules/core_widgets/uiitem.lua @@ -0,0 +1,6 @@ +function UIItem:onMouseRelease(mousePosition, mouseButton) + local item = self:getItem() + if not item or not self:containsPoint(mousePosition) then return false end + return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item) +end + diff --git a/modules/game/game.otmod b/modules/game/game.otmod index a21ac83e..cc5d2814 100644 --- a/modules/game/game.otmod +++ b/modules/game/game.otmod @@ -7,11 +7,12 @@ Module dependencies: - game_healthbar - game_inventory - - game_skills + //- game_skills - game_textmessage - game_viplist - game_console - game_outfit + - game_containers onLoad: | require 'game' diff --git a/modules/game/thing.lua b/modules/game/thing.lua index 7deda5c7..077c7211 100644 --- a/modules/game/thing.lua +++ b/modules/game/thing.lua @@ -1,4 +1,15 @@ +function Thing:isInsideContainer() + local pos = self:getPos() + return (pos and pos.x == 65535 and pos.y >= 64) +end + +function Thing:getContainerId() + local pos = self:getPos() + if not pos then return 0 end + return pos.y - 64 +end + -- public functions function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, useThing, creatureThing, multiUseThing) local keyboardModifiers = g_window.getKeyboardModifiers() @@ -18,7 +29,7 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, if mouseButton == MouseLeftButton and selectedThing then Game.useWith(Game.getSelectedThing(), useThing) Game.setSelectedThing(nil) - -- restore cursor + restoreCursor() return true elseif keyboardModifiers == KeyboardNoModifier and mouseButton == MouseRightButton then Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing) @@ -28,7 +39,11 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, return true elseif useThing and keyboardModifiers == KeyboardCtrlModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then if useThing:isContainer() then - print "open" + if useThing:isInsideContainer() then + Game.open(useThing, useThing:getContainerId()) + else + Game.open(useThing, Containers.getFreeContainerId()) + end elseif useThing:isMultiUse() then Game.setSelectedThing(useThing) setTargetCursor() @@ -50,12 +65,16 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, if multiUseThing:asCreature() then Game.attack(multiUseThing:asCreature()) elseif multiUseThing:isContainer() then - print "open" + if multiUseThing:isInsideContainer() then + Game.open(multiUseThing, multiUseThing:getContainerId()) + else + Game.open(multiUseThing, Containers.getFreeContainerId()) + end elseif multiUseThing:isMultiUse() then Game.setSelectedThing(multiUseThing) setTargetCursor() else - Game.use(useThing) + Game.use(multiUseThing) end return true elseif lookThing and keyboardModifiers == KeyboardShiftModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then @@ -81,15 +100,17 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing) menu:addOption('Look', function() Game.look(lookThing) end) end - -- Open or Use, depending if thing is a container if useThing then if useThing:isContainer() then - -- check for open in new window - menu:addOption('Open', function() print('open') end) + if useThing:isInsideContainer() then + menu:addOption('Open', function() Game.open(useThing, useThing:getContainerId()) end) + menu:addOption('Open in new window', function() Game.open(useThing, Containers.getFreeContainerId()) end) + else + menu:addOption('Open', function() Game.open(useThing, Containers.getFreeContainerId()) end) + end else if useThing:isMultiUse() then - setTargetCursor() - menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) end) + menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) setTargetCursor() end) else menu:addOption('Use', function() Game.use(useThing) end) end @@ -179,3 +200,4 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing) menu:display(menuPosition) end + diff --git a/modules/game_containers/container.otui b/modules/game_containers/container.otui new file mode 100644 index 00000000..a61b96b8 --- /dev/null +++ b/modules/game_containers/container.otui @@ -0,0 +1,13 @@ +MiniWindow + size: 200 186 + padding-top: 30 + padding-left: 10 + + layout: + type: grid + cell-size: 32 32 + cell-spacing: 5 + num-columns: 4 + num-lines: 5 + + diff --git a/modules/game_containers/containers.lua b/modules/game_containers/containers.lua new file mode 100644 index 00000000..1fd0b465 --- /dev/null +++ b/modules/game_containers/containers.lua @@ -0,0 +1,79 @@ +Containers = {} + +-- private variables +local m_containers = {} + +-- public functions +function Containers.clean() + m_containers = {} +end + +function Containers.getFreeContainerId() + for i=0,15 do + if not m_containers[i] then + return i + end + end + return 0 +end + +-- hooked events +function Containers.onContainerOpen(containerId, itemId, name, capacity, hasParent) + local container = m_containers[containerId] + if container then + Game.gameRightPanel:removeChild(container) + end + + container = displayUI('container.otui', { parent = Game.gameRightPanel }) + container:setText(name) + + -- set icon, itemid + -- closebutton + -- resize + if hasParent then + -- parent button + end + + container.itemCount = 0 + container.capacity = capacity + + for i=1,capacity do + local item = UIItem.create() + item:setStyle('Item') + container:addChild(item) + end + + m_containers[containerId] = container + print("opencid ".. containerId) +end + +function Containers.onContainerClose(containerId) + print("closecid ".. containerId) + local container = m_containers[containerId] + if container then + Game.gameRightPanel:removeChild(container) + end + m_containers[containerId] = nil +end + +function Containers.onContainerAddItem(containerId, item) + print("addcid ".. containerId) + local container = m_containers[containerId] + if not container or not item or container.itemCount >= container.capacity then return end + + -- maybe this has to be moved to client internal's files + local pos = item:getPos() + pos.z = container.itemCount + item:setPos(pos) + + local itemWidget = container:getChildByIndex(container.itemCount + 1) + itemWidget:setItem(item) + container.itemCount = container.itemCount + 1 + +end + +connect(Game, { onLogin = Containers.clean, + onLogout = Containers.clean, + onContainerOpen = Containers.onContainerOpen, + onContainerClose = Containers.onContainerClose, + onContainerAddItem = Containers.onContainerAddItem }) diff --git a/modules/game_containers/containers.otmod b/modules/game_containers/containers.otmod new file mode 100644 index 00000000..3f24761f --- /dev/null +++ b/modules/game_containers/containers.otmod @@ -0,0 +1,7 @@ +Module + name: game_containers + description: Manage containers + author: OTClient team + website: https://github.com/edubart/otclient + onLoad: | + require 'containers' diff --git a/modules/game_inventory/inventory.lua b/modules/game_inventory/inventory.lua index 1b77e699..0a2d1367 100644 --- a/modules/game_inventory/inventory.lua +++ b/modules/game_inventory/inventory.lua @@ -29,12 +29,6 @@ function Inventory.onSoulChange(soul) widget:setText("Soul:\n" .. soul) end -function Inventory.onInventoryItemMouseRelease(itemWidget, mousePosition, mouseButton) - local item = itemWidget:getItem() - if not item or not itemWidget:containsPoint(mousePosition) then return false end - return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item) -end - connect(Game, { onLogin = Inventory.create, onLogout = Inventory.destroy, onInventoryChange = Inventory.onInventoryChange, diff --git a/modules/game_inventory/inventory.otui b/modules/game_inventory/inventory.otui index f2e2d837..32095a9f 100644 --- a/modules/game_inventory/inventory.otui +++ b/modules/game_inventory/inventory.otui @@ -1,6 +1,3 @@ -InvetoryItem < Item - &onMouseRelease: Inventory.onInventoryItemMouseRelease - UIWindow width: 192 height: 148 @@ -9,34 +6,34 @@ UIWindow margin-right: 6 move-policy: free updated - InvetoryItem + Item // head id: slot1 anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter - InvetoryItem + Item // armor id: slot4 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // legs id: slot7 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // feet id: slot8 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // necklace id: slot2 anchors.top: parent.top @@ -44,21 +41,21 @@ UIWindow margin-top: 10 margin-right: 5 - InvetoryItem + Item // left id: slot6 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // ring id: slot9 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // backpack id: slot3 anchors.top: parent.top @@ -66,14 +63,14 @@ UIWindow margin-top: 10 margin-left: 5 - InvetoryItem + Item // right id: slot5 anchors.top: prev.bottom anchors.horizontalCenter: prev.horizontalCenter margin-top: 5 - InvetoryItem + Item // ammo id: slot10 anchors.top: prev.bottom diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 82dab455..7fce3d9e 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -125,7 +125,7 @@ void Game::processTextMessage(const std::string& type, const std::string& messag void Game::processContainerAddItem(int containerId, const ItemPtr& item) { if(item) - item->setPos(Position(65535, containerId, 0)); + item->setPos(Position(65535, containerId + 0x40, 0)); g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item); } @@ -250,6 +250,16 @@ void Game::look(const ThingPtr& thing) m_protocolGame->sendLookAt(thing->getPos(), thing->getId(), stackpos); } +void Game::open(const ThingPtr& thing, int containerId) +{ + if(!m_online || !thing || !checkBotProtection()) + return; + + int stackpos = getThingStackpos(thing); + if(stackpos != -1) + m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, containerId); +} + void Game::use(const ThingPtr& thing) { if(!m_online || !thing || !checkBotProtection()) @@ -257,7 +267,7 @@ void Game::use(const ThingPtr& thing) int stackpos = getThingStackpos(thing); if(stackpos != -1) - m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, 0);// last 0 has something to do with container + m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, 0); } void Game::useWith(const ThingPtr& fromThing, const ThingPtr& toThing) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 3a9797ad..a9334d62 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -57,6 +57,7 @@ public: void walk(Otc::Direction direction); void turn(Otc::Direction direction); void look(const ThingPtr& thing); + void open(const ThingPtr& thing, int containerId); void use(const ThingPtr& thing); void useWith(const ThingPtr& fromThing, const ThingPtr& toThing); void attack(const CreaturePtr& creature); diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 7b0d0a6d..9e5f4d8a 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -179,6 +179,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("openOutfitWindow", std::bind(&Game::openOutfitWindow, &g_game)); g_lua.bindClassStaticFunction("setOutfit", std::bind(&Game::setOutfit, &g_game, _1)); 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("useWith", std::bind(&Game::useWith, &g_game, _1, _2)); g_lua.bindClassStaticFunction("attack", std::bind(&Game::attack, &g_game, _1)); diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 68ecbc0a..cdda9b97 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -441,7 +441,7 @@ void ProtocolGame::parseOpenContainer(InputMessage& msg) for(int i = 0; i < itemCount; i++) { ItemPtr item = internalGetItem(msg); - g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item); + g_game.processContainerAddItem(containerId, item); } } @@ -455,7 +455,7 @@ void ProtocolGame::parseContainerAddItem(InputMessage& msg) { int containerId = msg.getU8(); ItemPtr item = internalGetItem(msg); - g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item); + g_game.processContainerAddItem(containerId, item); } void ProtocolGame::parseContainerUpdateItem(InputMessage& msg) @@ -470,7 +470,7 @@ void ProtocolGame::parseContainerRemoveItem(InputMessage& msg) { int containerId = msg.getU8(); int slot = msg.getU8(); - g_lua.callGlobalField("Game", "onContainerUpdateItem", containerId, slot); + g_lua.callGlobalField("Game", "onContainerRemoveItem", containerId, slot); } void ProtocolGame::parseAddInventoryItem(InputMessage& msg) diff --git a/src/otclient/util/position.h b/src/otclient/util/position.h index 41667ade..45ef871f 100644 --- a/src/otclient/util/position.h +++ b/src/otclient/util/position.h @@ -98,7 +98,7 @@ public: return Otc::InvalidDirection; } - bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x < 65536 && y < 65536 && z < 16; } + bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x <= 65535 && y <= 65535 && z <= 255; } Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z); } Position& operator+=(const Position& other) { x+=other.x; y+=other.y; z +=other.z; return *this; }