2018-06-21 01:32:55 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2018-06-21 01:32:55 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2018-06-21 01:32:55 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2018-06-21 01:32:55 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2018-06-21 01:32:55 +02:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "Inventory.hpp"
|
2019-01-26 20:29:13 +01:00
|
|
|
#include "Network.hpp"
|
2018-06-21 01:32:55 +02:00
|
|
|
|
2020-02-17 15:54:19 +09:00
|
|
|
void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount) {
|
|
|
|
m_items.at(x + y * m_width) = ItemStack(stringID, amount);
|
2019-01-26 20:29:13 +01:00
|
|
|
m_hasChanged = true;
|
2018-06-21 01:32:55 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 12:28:26 +01:00
|
|
|
bool Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size) {
|
|
|
|
for (std::size_t i = offset ; i < (size ? offset + size : m_items.size()) ; ++i) {
|
2018-06-24 01:42:06 +02:00
|
|
|
if (m_items[i].item().id() == 0) {
|
2020-02-17 15:54:19 +09:00
|
|
|
m_items[i] = ItemStack(stringID, amount);
|
2019-01-26 20:29:13 +01:00
|
|
|
m_hasChanged = true;
|
2020-03-01 00:15:49 +01:00
|
|
|
return true;
|
2018-06-23 03:50:02 +02:00
|
|
|
}
|
2020-02-17 15:54:19 +09:00
|
|
|
else if (m_items[i].item().stringID() == stringID) {
|
|
|
|
m_items[i] = ItemStack(stringID, m_items[i].amount() + amount);
|
2019-01-26 20:29:13 +01:00
|
|
|
m_hasChanged = true;
|
2020-03-01 00:15:49 +01:00
|
|
|
return true;
|
2018-06-24 21:01:12 +02:00
|
|
|
}
|
2018-06-23 03:50:02 +02:00
|
|
|
}
|
2020-03-01 00:15:49 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-01 12:28:26 +01:00
|
|
|
// NOTE: This fonction is only used by Lua since default parameters don't work properly
|
|
|
|
bool Inventory::addStack2(const std::string &stringID, u16 amount) {
|
|
|
|
return addStack(stringID, amount, 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-03-01 00:15:49 +01:00
|
|
|
void Inventory::clearStack(u16 x, u16 y) {
|
|
|
|
setStack(x, y, "_:air", 0);
|
2018-06-23 03:50:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
void Inventory::serialize(sf::Packet &packet) const {
|
2020-02-29 18:07:01 +01:00
|
|
|
packet << m_width << m_height << m_name << u8(m_inBlock)
|
|
|
|
<< s32(m_blockPos.x) << s32(m_blockPos.y) << s32(m_blockPos.z)
|
|
|
|
<< m_isUnlimited;
|
|
|
|
|
|
|
|
packet << u16(m_items.size());
|
2019-01-26 20:29:13 +01:00
|
|
|
|
|
|
|
int i = 0;
|
2020-01-31 15:04:04 +09:00
|
|
|
for (auto &it : m_items) {
|
2020-02-17 15:54:19 +09:00
|
|
|
packet << it.item().stringID() << it.amount()
|
2020-01-31 15:04:04 +09:00
|
|
|
<< u8(i % m_width) << u8(i / m_width);
|
2019-01-26 20:29:13 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
void Inventory::deserialize(sf::Packet &packet) {
|
2019-01-26 20:29:13 +01:00
|
|
|
u8 inBlock;
|
|
|
|
s32 bx, by, bz;
|
2020-02-29 18:07:01 +01:00
|
|
|
packet >> m_width >> m_height >> m_name >> inBlock >> bx >> by >> bz >> m_isUnlimited;
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
m_inBlock = inBlock;
|
|
|
|
m_blockPos = gk::Vector3i{bx, by, bz};
|
2019-01-26 20:29:13 +01:00
|
|
|
|
2020-02-07 23:15:44 +09:00
|
|
|
if (m_items.size() != m_width * m_height)
|
|
|
|
m_items.resize(m_width * m_height);
|
|
|
|
|
2020-02-29 18:07:01 +01:00
|
|
|
u16 itemListSize, i = 0;
|
|
|
|
packet >> itemListSize;
|
|
|
|
|
2019-01-26 20:29:13 +01:00
|
|
|
std::string name;
|
|
|
|
u16 amount;
|
|
|
|
u8 x, y;
|
2020-02-29 18:07:01 +01:00
|
|
|
while (i < itemListSize) {
|
2019-01-26 20:29:13 +01:00
|
|
|
packet >> name >> amount >> x >> y;
|
2020-01-31 15:04:04 +09:00
|
|
|
setStack(x, y, name, amount);
|
2020-02-29 18:07:01 +01:00
|
|
|
++i;
|
2019-01-26 20:29:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|