GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/inventoryrecv.cpp Lines: 4 56 7.1 %
Date: 2018-11-12 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-2018  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
1
    EquipBackend mEquips;
53
1
    InventoryItems mInventoryItems;
54
    Inventory *mStorage = nullptr;
55
3
    PickupQueue mSentPickups;
56
    bool mDebugInventory = true;
57
}  // namespace InventoryRecv
58
59
void InventoryRecv::processItemUseResponse(Net::MessageIn &msg)
60
{
61
    BLOCK_START("InventoryRecv::processItemUseResponse")
62
    Inventory *const inventory = localPlayer != nullptr
63
        ? PlayerInfo::getInventory() : nullptr;
64
65
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
66
    const int amount = msg.readInt16("amount");
67
68
    if (msg.readUInt8("result") == 0)
69
    {
70
        NotifyManager::notify(NotifyTypes::USE_FAILED);
71
    }
72
    else
73
    {
74
        if (inventory != nullptr)
75
        {
76
            if (Item *const item = inventory->getItem(index))
77
            {
78
                if (amount != 0)
79
                    item->setQuantity(amount);
80
                else
81
                    inventory->removeItemAt(index);
82
            }
83
        }
84
    }
85
    BLOCK_END("InventoryRecv::processItemUseResponse")
86
}
87
88
void InventoryRecv::processPlayerStorageStatus(Net::MessageIn &msg)
89
{
90
    BLOCK_START("InventoryRecv::processPlayerStorageStatus")
91
    /*
92
      * This is the closest we get to an "Open Storage" packet from the
93
      * server. It always comes after the two SMSG_PLAYER_STORAGE_...
94
      * packets that update storage contents.
95
      */
96
    msg.readInt16("used count");
97
    const int size = msg.readInt16("max size");
98
99
    if (mStorage == nullptr)
100
        mStorage = new Inventory(InventoryType::Storage, size);
101
102
    FOR_EACH (Ea::InventoryItems::const_iterator, it, mInventoryItems)
103
    {
104
        mStorage->setItem((*it).slot,
105
            (*it).id,
106
            (*it).type,
107
            (*it).quantity,
108
            (*it).refine,
109
            (*it).color,
110
            (*it).identified,
111
            (*it).damaged,
112
            (*it).favorite,
113
            (*it).equip,
114
            Equipped_false);
115
    }
116
    mInventoryItems.clear();
117
118
    if (storageWindow == nullptr)
119
    {
120
        CREATEWIDGETV(storageWindow, InventoryWindow, mStorage);
121
    }
122
    BLOCK_END("InventoryRecv::processPlayerStorageStatus")
123
}
124
125
void InventoryRecv::processPlayerStorageClose(Net::MessageIn &msg A_UNUSED)
126
{
127
    BLOCK_START("InventoryRecv::processPlayerStorageClose")
128
    // Storage access has been closed
129
    // Storage window deletes itself
130
    if (storageWindow != nullptr)
131
    {
132
        storageWindow->unsetInventory();
133
        storageWindow->close();
134
    }
135
    storageWindow = nullptr;
136
137
    if (mStorage != nullptr)
138
        mStorage->clear();
139
140
    delete2(mStorage);
141
    BLOCK_END("InventoryRecv::processPlayerStorageClose")
142
}
143
144
void InventoryRecv::processPlayerAttackRange(Net::MessageIn &msg)
145
{
146
    BLOCK_START("InventoryRecv::processPlayerAttackRange")
147
    const int range = msg.readInt16("range");
148
    if (localPlayer != nullptr)
149
        localPlayer->setAttackRange(range);
150
    PlayerInfo::setStatBase(Attributes::PLAYER_ATTACK_RANGE,
151
        range,
152
        Notify_true);
153
    PlayerInfo::setStatMod(Attributes::PLAYER_ATTACK_RANGE,
154
        0,
155
        Notify_true);
156
    BLOCK_END("InventoryRecv::processPlayerAttackRange")
157
}
158
159
void InventoryRecv::processPlayerArrowEquip(Net::MessageIn &msg)
160
{
161
    BLOCK_START("InventoryRecv::processPlayerArrowEquip")
162
    int index = msg.readInt16("index");
163
    if (index <= 1)
164
        return;
165
166
    index -= INVENTORY_OFFSET;
167
    mEquips.setEquipment(inventoryHandler->getProjectileSlot(), index);
168
    ArrowsListener::distributeEvent();
169
    BLOCK_END("InventoryRecv::processPlayerArrowEquip")
170
}
171
172

3
}  // namespace Ea