Skip to content

Commit

Permalink
Merge branch 'main' into feature/title-system
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel authored May 9, 2024
2 parents 2457825 + 09765f8 commit cbf5aa7
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 22 deletions.
54 changes: 51 additions & 3 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3933,16 +3933,22 @@ bool Player::removeItemCountById(uint16_t itemId, uint32_t itemAmount, bool remo
return false;
}

ItemsTierCountList Player::getInventoryItemsId() const {
ItemsTierCountList Player::getInventoryItemsId(bool ignoreStoreInbox /* false */) const {
ItemsTierCountList itemMap;
for (int32_t i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; i++) {
std::shared_ptr<Item> item = inventory[i];
if (!item) {
continue;
}

(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
if (std::shared_ptr<Container> container = item->getContainer()) {
const bool isStoreInbox = item->getID() == ITEM_STORE_INBOX;

if (!isStoreInbox) {
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}

const auto &container = item->getContainer();
if (container && (!isStoreInbox || !ignoreStoreInbox)) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
auto containerItem = *it;
(itemMap[containerItem->getID()])[containerItem->getTier()] += Item::countByType(containerItem, -1);
Expand Down Expand Up @@ -4041,6 +4047,48 @@ double_t Player::calculateDamageReduction(double_t currentTotal, int16_t resista
return (100 - currentTotal) / 100.0 * resistance + currentTotal;
}

ItemsTierCountList Player::getStoreInboxItemsId() const {
ItemsTierCountList itemMap;
const auto &container = getStoreInbox();
if (container) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
std::shared_ptr<Item> item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

ItemsTierCountList Player::getDepotChestItemsId() const {
ItemsTierCountList itemMap;

for (const auto &[index, depot] : depotChests) {
const std::shared_ptr<Container> &container = depot->getContainer();
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
std::shared_ptr<Item> item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

ItemsTierCountList Player::getDepotInboxItemsId() const {
ItemsTierCountList itemMap;

const std::shared_ptr<Inbox> &inbox = getInbox();
const std::shared_ptr<Container> &container = inbox->getContainer();
if (container) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
const auto &item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

std::vector<std::shared_ptr<Item>> Player::getAllInventoryItems(bool ignoreEquiped /*= false*/, bool ignoreItemWithTier /* false*/) const {
std::vector<std::shared_ptr<Item>> itemVector;
for (int i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; ++i) {
Expand Down
14 changes: 10 additions & 4 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,9 +1645,9 @@ class Player final : public Creature, public Cylinder, public Bankable {
}
}
void sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked, std::vector<std::pair<Achievement, uint32_t>> achievementsUnlocked);
void sendCyclopediaCharacterItemSummary() {
void sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems) {
if (client) {
client->sendCyclopediaCharacterItemSummary();
client->sendCyclopediaCharacterItemSummary(inventoryItems, storeInboxItems, supplyStashItems, depotBoxItems, inboxItems);
}
}
void sendCyclopediaCharacterOutfitsMounts() {
Expand Down Expand Up @@ -2585,6 +2585,13 @@ class Player final : public Creature, public Cylinder, public Bankable {
// Get specific inventory item from itemid
std::vector<std::shared_ptr<Item>> getInventoryItemsFromId(uint16_t itemId, bool ignore = true) const;

// this get all player store inbox items and return as ItemsTierCountList
ItemsTierCountList getStoreInboxItemsId() const;
// this get all player depot chest items and return as ItemsTierCountList
ItemsTierCountList getDepotChestItemsId() const;
// this get all player depot inbox items and return as ItemsTierCountList
ItemsTierCountList getDepotInboxItemsId() const;

// This get all player inventory items
std::vector<std::shared_ptr<Item>> getAllInventoryItems(bool ignoreEquiped = false, bool ignoreItemWithTier = false) const;

Expand Down Expand Up @@ -2685,8 +2692,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
size_t getLastIndex() const override;
uint32_t getItemTypeCount(uint16_t itemId, int32_t subType = -1) const override;
void stashContainer(StashContainerList itemDict);
ItemsTierCountList getInventoryItemsId() const;
// todo ItemsTierCountList getStoreInboxItemsId() const;
ItemsTierCountList getInventoryItemsId(bool ignoreStoreInbox = false) const;

// This function is a override function of base class
std::map<uint32_t, uint32_t> &getAllItemTypeCount(std::map<uint32_t, uint32_t> &countMap) const override;
Expand Down
11 changes: 9 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8390,9 +8390,16 @@ void Game::playerCyclopediaCharacterInfo(std::shared_ptr<Player> player, uint32_
case CYCLOPEDIA_CHARACTERINFO_ACHIEVEMENTS:
player->achiev()->sendUnlockedSecretAchievements();
break;
case CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY:
player->sendCyclopediaCharacterItemSummary();
case CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY: {
const ItemsTierCountList &inventoryItems = player->getInventoryItemsId(true);
const ItemsTierCountList &storeInboxItems = player->getStoreInboxItemsId();
const StashItemList &supplyStashItems = player->getStashItems();
const ItemsTierCountList &depotBoxItems = player->getDepotChestItemsId();
const ItemsTierCountList &inboxItems = player->getDepotInboxItemsId();

player->sendCyclopediaCharacterItemSummary(inventoryItems, storeInboxItems, supplyStashItems, depotBoxItems, inboxItems);
break;
}
case CYCLOPEDIA_CHARACTERINFO_OUTFITSMOUNTS:
player->sendCyclopediaCharacterOutfitsMounts();
break;
Expand Down
24 changes: 18 additions & 6 deletions src/items/bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ bool BedItem::canUse(std::shared_ptr<Player> player) {
return false;
}

auto partName = itemType.name;
auto nextPartname = nextBedItem->getName();
auto firstPart = keepFirstWordOnly(partName);
auto nextPartOf = keepFirstWordOnly(nextPartname);
g_logger().debug("First bed part name {}, second part name {}", firstPart, nextPartOf);
if (!isMovable() || !nextBedItem->isMovable() || firstPart != nextPartOf) {
if (!isMovable() || !nextBedItem->isMovable() || !isBedComplete(nextBedItem)) {
return false;
}

Expand All @@ -122,6 +117,23 @@ bool BedItem::canUse(std::shared_ptr<Player> player) {
return true;
}

bool BedItem::isBedComplete(std::shared_ptr<BedItem> nextBedItem) {
const ItemType &it = Item::items[id];

if (nextBedItem == nullptr) {
return false;
}

auto partName = it.name;
auto nextPartname = nextBedItem->getName();
auto firstPart = keepFirstWordOnly(partName);
auto nextPartOf = keepFirstWordOnly(nextPartname);

g_logger().debug("First bed part id {} name {}, second part id {} name {}", it.id, firstPart, nextBedItem->getID(), nextPartOf);

return it.bedPartOf == nextBedItem->getID();
}

bool BedItem::trySleep(std::shared_ptr<Player> player) {
if (!house || player->isRemoved()) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/items/bed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class BedItem final : public Item {

bool canUse(std::shared_ptr<Player> player);

bool isBedComplete(std::shared_ptr<BedItem> nextBedItem);

bool trySleep(std::shared_ptr<Player> player);
bool sleep(std::shared_ptr<Player> player);
void wakeUp(std::shared_ptr<Player> player);
Expand Down
105 changes: 99 additions & 6 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3698,7 +3698,7 @@ void ProtocolGame::sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked,
writeToOutputBuffer(msg);
}

void ProtocolGame::sendCyclopediaCharacterItemSummary() {
void ProtocolGame::sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems) {
if (!player || oldProtocol) {
return;
}
Expand All @@ -3708,11 +3708,104 @@ void ProtocolGame::sendCyclopediaCharacterItemSummary() {
msg.addByte(CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY);
msg.addByte(0x00); // 0x00 Here means 'no error'

msg.add<uint16_t>(0); // inventoryItems.size()
msg.add<uint16_t>(0); // storeInboxItems.size()
msg.add<uint16_t>(0); // supplyStashItems.size()
msg.add<uint16_t>(0); // depotBoxItems.size()
msg.add<uint16_t>(0); // inboxItems.size()
uint16_t inventoryItemsCount = 0;
const auto startInventory = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &inventoryItems_it : inventoryItems) {
for (const auto &[itemTier, itemCount] : inventoryItems_it.second) {
const ItemType &it = Item::items[inventoryItems_it.first];
msg.add<uint16_t>(inventoryItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++inventoryItemsCount;
}
}

const auto endInventory = msg.getBufferPosition();

msg.setBufferPosition(startInventory);
msg.add<uint16_t>(inventoryItemsCount);

msg.setBufferPosition(endInventory);

uint16_t storeInboxItemsCount = 0;
const auto startStoreInbox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &storeInboxItems_it : storeInboxItems) {
for (const auto &[itemTier, itemCount] : storeInboxItems_it.second) {
const ItemType &it = Item::items[storeInboxItems_it.first];
msg.add<uint16_t>(storeInboxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++storeInboxItemsCount;
}
}

const auto endStoreInbox = msg.getBufferPosition();

msg.setBufferPosition(startStoreInbox);
msg.add<uint16_t>(storeInboxItemsCount);

msg.setBufferPosition(endStoreInbox);

msg.add<uint16_t>(supplyStashItems.size());

for (const auto &[itemId, itemCount] : supplyStashItems) {
msg.add<uint16_t>(itemId);
msg.add<uint32_t>(itemCount);
}

uint16_t depotBoxItemsCount = 0;
const auto startDepotBox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &depotBoxItems_it : depotBoxItems) {
for (const auto &[itemTier, itemCount] : depotBoxItems_it.second) {
const ItemType &it = Item::items[depotBoxItems_it.first];
msg.add<uint16_t>(depotBoxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++depotBoxItemsCount;
}
}

const auto endDepotBox = msg.getBufferPosition();

msg.setBufferPosition(startDepotBox);
msg.add<uint16_t>(depotBoxItemsCount);

msg.setBufferPosition(endDepotBox);

uint16_t inboxItemsCount = 0;
const auto startInbox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &inboxItems_it : inboxItems) {
for (const auto &[itemTier, itemCount] : inboxItems_it.second) {
const ItemType &it = Item::items[inboxItems_it.first];
msg.add<uint16_t>(inboxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++inboxItemsCount;
}
}

msg.setBufferPosition(startInbox);
msg.add<uint16_t>(inboxItemsCount);

writeToOutputBuffer(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class ProtocolGame final : public Protocol {
void sendCyclopediaCharacterRecentDeaths(uint16_t page, uint16_t pages, const std::vector<RecentDeathEntry> &entries);
void sendCyclopediaCharacterRecentPvPKills(uint16_t page, uint16_t pages, const std::vector<RecentPvPKillEntry> &entries);
void sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked, std::vector<std::pair<Achievement, uint32_t>> achievementsUnlocked);
void sendCyclopediaCharacterItemSummary();
void sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems);
void sendCyclopediaCharacterOutfitsMounts();
void sendCyclopediaCharacterStoreSummary();
void sendCyclopediaCharacterInspection();
Expand Down

0 comments on commit cbf5aa7

Please sign in to comment.