container.cpp

main 144 lines · 4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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
26Container::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
40ItemPtr Container::getItem(int slot)
41{
42 if(slot < 0 || slot >= (int)m_items.size())
43 return nullptr;
44 return m_items[slot];
45}
46
47void Container::onOpen(const ContainerPtr& previousContainer)
48{
49 callLuaField("onOpen", previousContainer);
50}
51
52void Container::onClose()
53{
54 m_closed = true;
55 callLuaField("onClose");
56}
57
58void 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
79ItemPtr 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
87void Container::onAddItems(const std::vector<ItemPtr>& items)
88{
89 for(const ItemPtr& item : items)
90 m_items.push_back(item);
91 updateItemsPositions();
92}
93
94void 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
109void 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
139void Container::updateItemsPositions()
140{
141 for(int slot = 0; slot < (int)m_items.size(); ++slot)
142 m_items[slot]->setPosition(getSlotPosition(slot));
143}
144
Top