GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/inventoryrecv.cpp Lines: 4 69 5.8 %
Date: 2017-11-29 Branches: 2 42 4.8 %

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

6
}  // namespace Ea