summaryrefslogtreecommitdiff
path: root/src/net/ea/inventoryrecv.cpp
blob: 9cc43e42a99e277263a249d2524c5ef4937130aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2019  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "net/ea/inventoryrecv.h"

#include "notifymanager.h"

#include "being/localplayer.h"

#include "const/net/inventory.h"

#include "enums/resources/notifytypes.h"

#include "gui/widgets/createwidget.h"

#include "net/inventoryhandler.h"
#include "net/messagein.h"

#include "net/ea/equipbackend.h"

#include "utils/delete2.h"
#include "utils/foreach.h"

#include "listeners/arrowslistener.h"

#include "debug.h"

namespace Ea
{

namespace InventoryRecv
{
    EquipBackend mEquips;
    InventoryItems mInventoryItems;
    Inventory *mStorage = nullptr;
    PickupQueue mSentPickups;
    bool mDebugInventory = true;
}  // namespace InventoryRecv

void InventoryRecv::processItemUseResponse(Net::MessageIn &msg)
{
    BLOCK_START("InventoryRecv::processItemUseResponse")
    Inventory *const inventory = localPlayer != nullptr
        ? PlayerInfo::getInventory() : nullptr;

    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
    const int amount = msg.readInt16("amount");

    if (msg.readUInt8("result") == 0)
    {
        NotifyManager::notify(NotifyTypes::USE_FAILED);
    }
    else
    {
        if (inventory != nullptr)
        {
            if (Item *const item = inventory->getItem(index))
            {
                if (amount != 0)
                    item->setQuantity(amount);
                else
                    inventory->removeItemAt(index);
            }
        }
    }
    BLOCK_END("InventoryRecv::processItemUseResponse")
}

void InventoryRecv::processPlayerStorageStatus(Net::MessageIn &msg)
{
    BLOCK_START("InventoryRecv::processPlayerStorageStatus")
    /*
      * This is the closest we get to an "Open Storage" packet from the
      * server. It always comes after the two SMSG_PLAYER_STORAGE_...
      * packets that update storage contents.
      */
    msg.readInt16("used count");
    const int size = msg.readInt16("max size");

    if (mStorage == nullptr)
        mStorage = new Inventory(InventoryType::Storage, size);

    FOR_EACH (Ea::InventoryItems::const_iterator, it, mInventoryItems)
    {
        mStorage->setItem((*it).slot,
            (*it).id,
            (*it).type,
            (*it).quantity,
            (*it).refine,
            (*it).color,
            (*it).identified,
            (*it).damaged,
            (*it).favorite,
            (*it).equip,
            Equipped_false);
    }
    mInventoryItems.clear();

    if (storageWindow == nullptr)
    {
        CREATEWIDGETV(storageWindow, InventoryWindow, mStorage);
    }
    BLOCK_END("InventoryRecv::processPlayerStorageStatus")
}

void InventoryRecv::processPlayerStorageClose(Net::MessageIn &msg A_UNUSED)
{
    BLOCK_START("InventoryRecv::processPlayerStorageClose")
    // Storage access has been closed
    // Storage window deletes itself
    if (storageWindow != nullptr)
    {
        storageWindow->unsetInventory();
        storageWindow->close();
    }
    storageWindow = nullptr;

    if (mStorage != nullptr)
        mStorage->clear();

    delete2(mStorage)
    BLOCK_END("InventoryRecv::processPlayerStorageClose")
}

void InventoryRecv::processPlayerAttackRange(Net::MessageIn &msg)
{
    BLOCK_START("InventoryRecv::processPlayerAttackRange")
    const int range = msg.readInt16("range");
    if (localPlayer != nullptr)
        localPlayer->setAttackRange(range);
    PlayerInfo::setStatBase(Attributes::PLAYER_ATTACK_RANGE,
        range,
        Notify_true);
    PlayerInfo::setStatMod(Attributes::PLAYER_ATTACK_RANGE,
        0,
        Notify_true);
    BLOCK_END("InventoryRecv::processPlayerAttackRange")
}

void InventoryRecv::processPlayerArrowEquip(Net::MessageIn &msg)
{
    BLOCK_START("InventoryRecv::processPlayerArrowEquip")
    int index = msg.readInt16("index");
    if (index <= 1)
        return;

    index -= INVENTORY_OFFSET;
    mEquips.setEquipment(inventoryHandler->getProjectileSlot(), index);
    ArrowsListener::distributeEvent();
    BLOCK_END("InventoryRecv::processPlayerArrowEquip")
}

}  // namespace Ea