Otclient  14/8/2020
container.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "container.h"
24 #include "item.h"
25 
26 Container::Container(int id, int capacity, const std::string& name, const ItemPtr& containerItem, bool hasParent, bool isUnlocked, bool hasPages, int containerSize, int firstIndex)
27 {
28  m_id = id;
29  m_capacity = capacity;
30  m_name = name;
31  m_containerItem = containerItem;
32  m_hasParent = hasParent;
33  m_closed = false;
34  m_unlocked = isUnlocked;
35  m_hasPages = hasPages;
36  m_size = containerSize;
37  m_firstIndex = firstIndex;
38 }
39 
41 {
42  if(slot < 0 || slot >= (int)m_items.size())
43  return nullptr;
44  return m_items[slot];
45 }
46 
47 void Container::onOpen(const ContainerPtr& previousContainer)
48 {
49  callLuaField("onOpen", previousContainer);
50 }
51 
53 {
54  m_closed = true;
55  callLuaField("onClose");
56 }
57 
58 void Container::onAddItem(const ItemPtr& item, int slot)
59 {
60  slot -= m_firstIndex;
61 
62  m_size++;
63  // indicates that there is a new item on next page
64  if(m_hasPages && slot > m_capacity) {
65  callLuaField("onSizeChange", m_size);
66  return;
67  }
68 
69  if(slot == 0)
70  m_items.push_front(item);
71  else
72  m_items.push_back(item);
73  updateItemsPositions();
74 
75  callLuaField("onSizeChange", m_size);
76  callLuaField("onAddItem", slot, item);
77 }
78 
79 ItemPtr Container::findItemById(uint itemId, int subType)
80 {
81  for(const ItemPtr item : m_items)
82  if(item->getId() == itemId && (subType == -1 || item->getSubType() == subType))
83  return item;
84  return nullptr;
85 }
86 
87 void Container::onAddItems(const std::vector<ItemPtr>& items)
88 {
89  for(const ItemPtr& item : items)
90  m_items.push_back(item);
91  updateItemsPositions();
92 }
93 
94 void Container::onUpdateItem(int slot, const ItemPtr& item)
95 {
96  slot -= m_firstIndex;
97  if(slot < 0 || slot >= (int)m_items.size()) {
98  g_logger.traceError("slot not found");
99  return;
100  }
101 
102  ItemPtr oldItem = m_items[slot];
103  m_items[slot] = item;
104  item->setPosition(getSlotPosition(slot));
105 
106  callLuaField("onUpdateItem", slot, item, oldItem);
107 }
108 
109 void Container::onRemoveItem(int slot, const ItemPtr& lastItem)
110 {
111  slot -= m_firstIndex;
112  if(m_hasPages && slot >= (int)m_items.size()) {
113  m_size--;
114  callLuaField("onSizeChange", m_size);
115  return;
116  }
117 
118  if(slot < 0 || slot >= (int)m_items.size()) {
119  g_logger.traceError("slot not found");
120  return;
121  }
122 
123  ItemPtr item = m_items[slot];
124  m_items.erase(m_items.begin() + slot);
125 
126 
127  if(lastItem) {
128  onAddItem(lastItem, m_firstIndex + m_capacity - 1);
129  m_size--;
130  }
131  m_size--;
132 
133  updateItemsPositions();
134 
135  callLuaField("onSizeChange", m_size);
136  callLuaField("onRemoveItem", slot, item);
137 }
138 
139 void Container::updateItemsPositions()
140 {
141  for(int slot = 0; slot < (int)m_items.size(); ++slot)
142  m_items[slot]->setPosition(getSlotPosition(slot));
143 }
Container::onClose
void onClose()
Definition: container.cpp:52
Container::onRemoveItem
void onRemoveItem(int slot, const ItemPtr &lastItem)
Definition: container.cpp:109
Container::Container
Container(int id, int capacity, const std::string &name, const ItemPtr &containerItem, bool hasParent, bool isUnlocked, bool hasPages, int containerSize, int firstIndex)
Definition: container.cpp:26
Container::onOpen
void onOpen(const ContainerPtr &previousContainer)
Definition: container.cpp:47
Container::findItemById
ItemPtr findItemById(uint itemId, int subType)
Definition: container.cpp:79
Container::onAddItems
void onAddItems(const std::vector< ItemPtr > &items)
Definition: container.cpp:87
uint
unsigned int uint
Definition: types.h:31
LuaObject::callLuaField
R callLuaField(const std::string &field, const T &... args)
Definition: luaobject.h:172
g_logger
Logger g_logger
Definition: logger.cpp:35
container.h
Container::getItem
ItemPtr getItem(int slot)
Definition: container.cpp:40
Container::isUnlocked
bool isUnlocked()
Definition: container.h:48
Container::hasPages
bool hasPages()
Definition: container.h:49
Container::getSlotPosition
Position getSlotPosition(int slot)
Definition: container.h:41
Container::onUpdateItem
void onUpdateItem(int slot, const ItemPtr &item)
Definition: container.cpp:94
stdext::shared_object_ptr< Item >
Thing::setPosition
void setPosition(const Position &position)
Definition: thing.cpp:36
Container::onAddItem
void onAddItem(const ItemPtr &item, int slot)
Definition: container.cpp:58
Container::hasParent
bool hasParent()
Definition: container.h:46
item.h