GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/inventoryrecv.cpp Lines: 4 56 7.1 %
Date: 2021-03-17 Branches: 2 34 5.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "net/ea/inventoryrecv.h"
25
26
#include "notifymanager.h"
27
28
#include "being/localplayer.h"
29
30
#include "const/net/inventory.h"
31
32
#include "enums/resources/notifytypes.h"
33
34
#include "gui/widgets/createwidget.h"
35
36
#include "net/inventoryhandler.h"
37
#include "net/messagein.h"
38
39
#include "net/ea/equipbackend.h"
40
41
#include "utils/delete2.h"
42
#include "utils/foreach.h"
43
44
#include "listeners/arrowslistener.h"
45
46
#include "debug.h"
47
48
namespace Ea
49
{
50
51
namespace InventoryRecv
52
{
53
1
    EquipBackend mEquips;
54
1
    InventoryItems mStorageItems;
55
    Inventory *mStorage = nullptr;
56
3
    PickupQueue mSentPickups;
57
    bool mDebugInventory = true;
58
}  // namespace InventoryRecv
59
60
void InventoryRecv::processItemUseResponse(Net::MessageIn &msg)
61
{
62
    BLOCK_START("InventoryRecv::processItemUseResponse")
63
    Inventory *const inventory = localPlayer != nullptr
64
        ? PlayerInfo::getInventory() : nullptr;
65
66
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
67
    const int amount = msg.readInt16("amount");
68
69
    if (msg.readUInt8("result") == 0)
70
    {
71
        NotifyManager::notify(NotifyTypes::USE_FAILED);
72
    }
73
    else
74
    {
75
        if (inventory != nullptr)
76
        {
77
            if (Item *const item = inventory->getItem(index))
78
            {
79
                if (amount != 0)
80
                    item->setQuantity(amount);
81
                else
82
                    inventory->removeItemAt(index);
83
            }
84
        }
85
    }
86
    BLOCK_END("InventoryRecv::processItemUseResponse")
87
}
88
89
void InventoryRecv::processPlayerStorageStatus(Net::MessageIn &msg)
90
{
91
    BLOCK_START("InventoryRecv::processPlayerStorageStatus")
92
    /*
93
      * This is the closest we get to an "Open Storage" packet from the
94
      * server. It always comes after the two SMSG_PLAYER_STORAGE_...
95
      * packets that update storage contents.
96
      */
97
    msg.readInt16("used count");
98
    const int size = msg.readInt16("max size");
99
100
    if (mStorage == nullptr)
101
        mStorage = new Inventory(InventoryType::Storage, size);
102
103
    FOR_EACH (Ea::InventoryItems::const_iterator, it, mStorageItems)
104
    {
105
        mStorage->setItem((*it).slot,
106
            (*it).id,
107
            (*it).type,
108
            (*it).quantity,
109
            (*it).refine,
110
            (*it).color,
111
            (*it).identified,
112
            (*it).damaged,
113
            (*it).favorite,
114
            (*it).equip,
115
            Equipped_false);
116
    }
117
    mStorageItems.clear();
118
119
    if (storageWindow == nullptr)
120
    {
121
        CREATEWIDGETV(storageWindow, InventoryWindow, mStorage);
122
    }
123
    BLOCK_END("InventoryRecv::processPlayerStorageStatus")
124
}
125
126
void InventoryRecv::processPlayerStorageClose(Net::MessageIn &msg A_UNUSED)
127
{
128
    BLOCK_START("InventoryRecv::processPlayerStorageClose")
129
    // Storage access has been closed
130
    // Storage window deletes itself
131
    if (storageWindow != nullptr)
132
    {
133
        storageWindow->unsetInventory();
134
        storageWindow->close();
135
    }
136
    storageWindow = nullptr;
137
138
    if (mStorage != nullptr)
139
        mStorage->clear();
140
141
    delete2(mStorage)
142
    BLOCK_END("InventoryRecv::processPlayerStorageClose")
143
}
144
145
void InventoryRecv::processPlayerAttackRange(Net::MessageIn &msg)
146
{
147
    BLOCK_START("InventoryRecv::processPlayerAttackRange")
148
    const int range = msg.readInt16("range");
149
    if (localPlayer != nullptr)
150
        localPlayer->setAttackRange(range);
151
    PlayerInfo::setStatBase(Attributes::PLAYER_ATTACK_RANGE,
152
        range,
153
        Notify_true);
154
    PlayerInfo::setStatMod(Attributes::PLAYER_ATTACK_RANGE,
155
        0,
156
        Notify_true);
157
    BLOCK_END("InventoryRecv::processPlayerAttackRange")
158
}
159
160
void InventoryRecv::processPlayerArrowEquip(Net::MessageIn &msg)
161
{
162
    BLOCK_START("InventoryRecv::processPlayerArrowEquip")
163
    int index = msg.readInt16("index");
164
    if (index <= 1)
165
        return;
166
167
    index -= INVENTORY_OFFSET;
168
    mEquips.setEquipment(inventoryHandler->getProjectileSlot(), index);
169
    ArrowsListener::distributeEvent();
170
    BLOCK_END("InventoryRecv::processPlayerArrowEquip")
171
}
172
173

3
}  // namespace Ea