Browse Source

improve containers

Henrique Santiago 9 years ago
parent
commit
ea70f90e92

+ 3
- 1
modules/core_widgets/core_widgets.otmod View File

@@ -14,5 +14,7 @@ Module
14 14
     require 'uitabbar'
15 15
     require 'uipopupmenu'
16 16
     require 'uiwindow'
17
+    require 'uiitem'
17 18
     require 'tooltip/tooltip'
18
-    require 'messagebox/messagebox'
19
+    require 'messagebox/messagebox'
20
+    

+ 6
- 0
modules/core_widgets/uiitem.lua View File

@@ -0,0 +1,6 @@
1
+function UIItem:onMouseRelease(mousePosition, mouseButton)
2
+  local item = self:getItem()
3
+  if not item or not self:containsPoint(mousePosition) then return false end
4
+  return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item)
5
+end
6
+

+ 2
- 1
modules/game/game.otmod View File

@@ -7,11 +7,12 @@ Module
7 7
   dependencies:
8 8
     - game_healthbar
9 9
     - game_inventory
10
-    - game_skills
10
+    //- game_skills
11 11
     - game_textmessage
12 12
     - game_viplist
13 13
     - game_console
14 14
     - game_outfit
15
+    - game_containers
15 16
 
16 17
   onLoad: |
17 18
     require 'game'

+ 31
- 9
modules/game/thing.lua View File

@@ -1,4 +1,15 @@
1 1
 
2
+function Thing:isInsideContainer()
3
+  local pos = self:getPos()
4
+  return (pos and pos.x == 65535 and pos.y >= 64)
5
+end
6
+
7
+function Thing:getContainerId()
8
+  local pos = self:getPos()
9
+  if not pos then return 0 end
10
+  return pos.y - 64
11
+end
12
+
2 13
 -- public functions
3 14
 function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, useThing, creatureThing, multiUseThing)
4 15
   local keyboardModifiers = g_window.getKeyboardModifiers()
@@ -18,7 +29,7 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
18 29
     if mouseButton == MouseLeftButton and selectedThing then
19 30
       Game.useWith(Game.getSelectedThing(), useThing)
20 31
       Game.setSelectedThing(nil)
21
-      -- restore cursor
32
+      restoreCursor()
22 33
       return true
23 34
     elseif keyboardModifiers == KeyboardNoModifier and mouseButton == MouseRightButton then
24 35
       Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
@@ -28,7 +39,11 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
28 39
       return true
29 40
     elseif useThing and keyboardModifiers == KeyboardCtrlModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
30 41
       if useThing:isContainer() then
31
-        print "open"
42
+        if useThing:isInsideContainer() then
43
+          Game.open(useThing, useThing:getContainerId())
44
+        else
45
+          Game.open(useThing, Containers.getFreeContainerId())
46
+        end
32 47
       elseif useThing:isMultiUse() then
33 48
         Game.setSelectedThing(useThing)
34 49
         setTargetCursor()
@@ -50,12 +65,16 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
50 65
       if multiUseThing:asCreature() then
51 66
         Game.attack(multiUseThing:asCreature())
52 67
       elseif multiUseThing:isContainer() then
53
-        print "open"
68
+        if multiUseThing:isInsideContainer() then
69
+          Game.open(multiUseThing, multiUseThing:getContainerId())
70
+        else
71
+          Game.open(multiUseThing, Containers.getFreeContainerId())
72
+        end
54 73
       elseif multiUseThing:isMultiUse() then
55 74
         Game.setSelectedThing(multiUseThing)
56 75
         setTargetCursor()
57 76
       else
58
-        Game.use(useThing)
77
+        Game.use(multiUseThing)
59 78
       end
60 79
       return true
61 80
     elseif lookThing and keyboardModifiers == KeyboardShiftModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
@@ -81,15 +100,17 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
81 100
     menu:addOption('Look', function() Game.look(lookThing) end)
82 101
   end
83 102
 
84
-  -- Open or Use, depending if thing is a container
85 103
   if useThing then
86 104
     if useThing:isContainer() then
87
-      -- check for open in new window
88
-      menu:addOption('Open', function() print('open') end)
105
+      if useThing:isInsideContainer() then
106
+        menu:addOption('Open', function() Game.open(useThing, useThing:getContainerId()) end)
107
+        menu:addOption('Open in new window', function() Game.open(useThing, Containers.getFreeContainerId()) end)
108
+      else
109
+        menu:addOption('Open', function() Game.open(useThing, Containers.getFreeContainerId()) end)
110
+      end
89 111
     else
90 112
       if useThing:isMultiUse() then
91
-        setTargetCursor()
92
-        menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) end)
113
+        menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) setTargetCursor() end)
93 114
       else
94 115
         menu:addOption('Use', function() Game.use(useThing) end)
95 116
       end
@@ -179,3 +200,4 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
179 200
 
180 201
   menu:display(menuPosition)
181 202
 end
203
+

+ 13
- 0
modules/game_containers/container.otui View File

@@ -0,0 +1,13 @@
1
+MiniWindow
2
+  size: 200 186
3
+  padding-top: 30
4
+  padding-left: 10
5
+
6
+  layout:
7
+    type: grid
8
+    cell-size: 32 32
9
+    cell-spacing: 5
10
+    num-columns: 4
11
+    num-lines: 5
12
+
13
+

+ 79
- 0
modules/game_containers/containers.lua View File

@@ -0,0 +1,79 @@
1
+Containers = {}
2
+
3
+-- private variables
4
+local m_containers = {}
5
+
6
+-- public functions
7
+function Containers.clean()
8
+  m_containers = {}
9
+end
10
+
11
+function Containers.getFreeContainerId()
12
+  for i=0,15 do
13
+    if not m_containers[i] then
14
+      return i
15
+    end
16
+  end
17
+  return 0
18
+end
19
+
20
+-- hooked events
21
+function Containers.onContainerOpen(containerId, itemId, name, capacity, hasParent)
22
+  local container = m_containers[containerId]
23
+  if container then
24
+    Game.gameRightPanel:removeChild(container)
25
+  end
26
+  
27
+  container = displayUI('container.otui', { parent = Game.gameRightPanel })
28
+  container:setText(name)
29
+  
30
+  -- set icon, itemid
31
+  -- closebutton
32
+  -- resize
33
+  if hasParent then
34
+    -- parent button
35
+  end
36
+  
37
+  container.itemCount = 0
38
+  container.capacity = capacity
39
+  
40
+  for i=1,capacity do
41
+    local item = UIItem.create()
42
+    item:setStyle('Item')
43
+    container:addChild(item)
44
+  end
45
+
46
+  m_containers[containerId] = container
47
+  print("opencid ".. containerId)
48
+end
49
+
50
+function Containers.onContainerClose(containerId)
51
+  print("closecid ".. containerId)
52
+  local container = m_containers[containerId]
53
+  if container then
54
+    Game.gameRightPanel:removeChild(container)
55
+  end
56
+  m_containers[containerId] = nil
57
+end
58
+
59
+function Containers.onContainerAddItem(containerId, item)
60
+  print("addcid ".. containerId)
61
+  local container = m_containers[containerId]
62
+  if not container or not item or container.itemCount >= container.capacity then return end
63
+
64
+  -- maybe this has to be moved to client internal's files
65
+  local pos = item:getPos()
66
+  pos.z = container.itemCount
67
+  item:setPos(pos)
68
+  
69
+  local itemWidget = container:getChildByIndex(container.itemCount + 1)
70
+  itemWidget:setItem(item)
71
+  container.itemCount = container.itemCount + 1
72
+
73
+end
74
+
75
+connect(Game, { onLogin = Containers.clean,
76
+                onLogout = Containers.clean,
77
+                onContainerOpen = Containers.onContainerOpen,
78
+                onContainerClose = Containers.onContainerClose,
79
+                onContainerAddItem = Containers.onContainerAddItem })

+ 7
- 0
modules/game_containers/containers.otmod View File

@@ -0,0 +1,7 @@
1
+Module
2
+  name: game_containers
3
+  description: Manage containers
4
+  author: OTClient team
5
+  website: https://github.com/edubart/otclient
6
+  onLoad: |
7
+    require 'containers'

+ 0
- 6
modules/game_inventory/inventory.lua View File

@@ -29,12 +29,6 @@ function Inventory.onSoulChange(soul)
29 29
   widget:setText("Soul:\n" .. soul)
30 30
 end
31 31
 
32
-function Inventory.onInventoryItemMouseRelease(itemWidget, mousePosition, mouseButton)
33
-  local item = itemWidget:getItem()
34
-  if not item or not itemWidget:containsPoint(mousePosition) then return false end
35
-  return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item)
36
-end
37
-
38 32
 connect(Game, { onLogin = Inventory.create,
39 33
                 onLogout = Inventory.destroy,
40 34
                 onInventoryChange = Inventory.onInventoryChange,

+ 10
- 13
modules/game_inventory/inventory.otui View File

@@ -1,6 +1,3 @@
1
-InvetoryItem < Item
2
-  &onMouseRelease: Inventory.onInventoryItemMouseRelease
3
-
4 1
 UIWindow
5 2
   width: 192
6 3
   height: 148
@@ -9,34 +6,34 @@ UIWindow
9 6
   margin-right: 6
10 7
   move-policy: free updated
11 8
 
12
-  InvetoryItem
9
+  Item
13 10
     // head
14 11
     id: slot1
15 12
     anchors.top: parent.top
16 13
     anchors.horizontalCenter: parent.horizontalCenter
17 14
 
18
-  InvetoryItem
15
+  Item
19 16
     // armor
20 17
     id: slot4
21 18
     anchors.top: prev.bottom
22 19
     anchors.horizontalCenter: prev.horizontalCenter
23 20
     margin-top: 5
24 21
 
25
-  InvetoryItem
22
+  Item
26 23
     // legs
27 24
     id: slot7
28 25
     anchors.top: prev.bottom
29 26
     anchors.horizontalCenter: prev.horizontalCenter
30 27
     margin-top: 5
31 28
 
32
-  InvetoryItem
29
+  Item
33 30
     // feet
34 31
     id: slot8
35 32
     anchors.top: prev.bottom
36 33
     anchors.horizontalCenter: prev.horizontalCenter
37 34
     margin-top: 5
38 35
 
39
-  InvetoryItem
36
+  Item
40 37
     // necklace
41 38
     id: slot2
42 39
     anchors.top: parent.top
@@ -44,21 +41,21 @@ UIWindow
44 41
     margin-top: 10
45 42
     margin-right: 5
46 43
 
47
-  InvetoryItem
44
+  Item
48 45
     // left
49 46
     id: slot6
50 47
     anchors.top: prev.bottom
51 48
     anchors.horizontalCenter: prev.horizontalCenter
52 49
     margin-top: 5
53 50
 
54
-  InvetoryItem
51
+  Item
55 52
     // ring
56 53
     id: slot9
57 54
     anchors.top: prev.bottom
58 55
     anchors.horizontalCenter: prev.horizontalCenter
59 56
     margin-top: 5
60 57
 
61
-  InvetoryItem
58
+  Item
62 59
     // backpack
63 60
     id: slot3
64 61
     anchors.top: parent.top
@@ -66,14 +63,14 @@ UIWindow
66 63
     margin-top: 10
67 64
     margin-left: 5
68 65
 
69
-  InvetoryItem
66
+  Item
70 67
     // right
71 68
     id: slot5
72 69
     anchors.top: prev.bottom
73 70
     anchors.horizontalCenter: prev.horizontalCenter
74 71
     margin-top: 5
75 72
 
76
-  InvetoryItem
73
+  Item
77 74
     // ammo
78 75
     id: slot10
79 76
     anchors.top: prev.bottom

+ 12
- 2
src/otclient/core/game.cpp View File

@@ -125,7 +125,7 @@ void Game::processTextMessage(const std::string& type, const std::string& messag
125 125
 void Game::processContainerAddItem(int containerId, const ItemPtr& item)
126 126
 {
127 127
     if(item)
128
-        item->setPos(Position(65535, containerId, 0));
128
+        item->setPos(Position(65535, containerId + 0x40, 0));
129 129
 
130 130
     g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item);
131 131
 }
@@ -250,6 +250,16 @@ void Game::look(const ThingPtr& thing)
250 250
         m_protocolGame->sendLookAt(thing->getPos(), thing->getId(), stackpos);
251 251
 }
252 252
 
253
+void Game::open(const ThingPtr& thing, int containerId)
254
+{
255
+    if(!m_online || !thing || !checkBotProtection())
256
+        return;
257
+
258
+    int stackpos = getThingStackpos(thing);
259
+    if(stackpos != -1)
260
+        m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, containerId);
261
+}
262
+
253 263
 void Game::use(const ThingPtr& thing)
254 264
 {
255 265
     if(!m_online || !thing || !checkBotProtection())
@@ -257,7 +267,7 @@ void Game::use(const ThingPtr& thing)
257 267
 
258 268
     int stackpos = getThingStackpos(thing);
259 269
     if(stackpos != -1)
260
-        m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, 0);// last 0 has something to do with container
270
+        m_protocolGame->sendUseItem(thing->getPos(), thing->getId(), stackpos, 0);
261 271
 }
262 272
 
263 273
 void Game::useWith(const ThingPtr& fromThing, const ThingPtr& toThing)

+ 1
- 0
src/otclient/core/game.h View File

@@ -57,6 +57,7 @@ public:
57 57
     void walk(Otc::Direction direction);
58 58
     void turn(Otc::Direction direction);
59 59
     void look(const ThingPtr& thing);
60
+    void open(const ThingPtr& thing, int containerId);
60 61
     void use(const ThingPtr& thing);
61 62
     void useWith(const ThingPtr& fromThing, const ThingPtr& toThing);
62 63
     void attack(const CreaturePtr& creature);

+ 1
- 0
src/otclient/luafunctions.cpp View File

@@ -179,6 +179,7 @@ void OTClient::registerLuaFunctions()
179 179
     g_lua.bindClassStaticFunction<Game>("openOutfitWindow", std::bind(&Game::openOutfitWindow, &g_game));
180 180
     g_lua.bindClassStaticFunction<Game>("setOutfit", std::bind(&Game::setOutfit, &g_game, _1));
181 181
     g_lua.bindClassStaticFunction<Game>("look", std::bind(&Game::look, &g_game, _1));
182
+    g_lua.bindClassStaticFunction<Game>("open", std::bind(&Game::open, &g_game, _1, _2));
182 183
     g_lua.bindClassStaticFunction<Game>("use", std::bind(&Game::use, &g_game, _1));
183 184
     g_lua.bindClassStaticFunction<Game>("useWith", std::bind(&Game::useWith, &g_game, _1, _2));
184 185
     g_lua.bindClassStaticFunction<Game>("attack", std::bind(&Game::attack, &g_game, _1));

+ 3
- 3
src/otclient/net/protocolgameparse.cpp View File

@@ -441,7 +441,7 @@ void ProtocolGame::parseOpenContainer(InputMessage& msg)
441 441
 
442 442
     for(int i = 0; i < itemCount; i++) {
443 443
         ItemPtr item = internalGetItem(msg);
444
-        g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item);
444
+        g_game.processContainerAddItem(containerId, item);
445 445
     }
446 446
 }
447 447
 
@@ -455,7 +455,7 @@ void ProtocolGame::parseContainerAddItem(InputMessage& msg)
455 455
 {
456 456
     int containerId = msg.getU8();
457 457
     ItemPtr item = internalGetItem(msg);
458
-    g_lua.callGlobalField("Game", "onContainerAddItem", containerId, item);
458
+    g_game.processContainerAddItem(containerId, item);
459 459
 }
460 460
 
461 461
 void ProtocolGame::parseContainerUpdateItem(InputMessage& msg)
@@ -470,7 +470,7 @@ void ProtocolGame::parseContainerRemoveItem(InputMessage& msg)
470 470
 {
471 471
     int containerId = msg.getU8();
472 472
     int slot = msg.getU8();
473
-    g_lua.callGlobalField("Game", "onContainerUpdateItem", containerId, slot);
473
+    g_lua.callGlobalField("Game", "onContainerRemoveItem", containerId, slot);
474 474
 }
475 475
 
476 476
 void ProtocolGame::parseAddInventoryItem(InputMessage& msg)

+ 1
- 1
src/otclient/util/position.h View File

@@ -98,7 +98,7 @@ public:
98 98
         return Otc::InvalidDirection;
99 99
     }
100 100
 
101
-    bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x < 65536 && y < 65536 && z < 16; }
101
+    bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x <= 65535 && y <= 65535 && z <= 255; }
102 102
 
103 103
     Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z);   }
104 104
     Position& operator+=(const Position& other) { x+=other.x; y+=other.y; z +=other.z; return *this; }

Loading…
Cancel
Save