~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/being/playerinfo.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  The ManaPlus Client
 
3
 *  Copyright (C) 2010  The Mana Developers
 
4
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 
5
 *
 
6
 *  This file is part of The ManaPlus Client.
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "being/playerinfo.h"
 
23
 
 
24
#include "client.h"
 
25
#include "configuration.h"
 
26
#include "depricatedevent.h"
 
27
#include "inventory.h"
 
28
 
 
29
#include "gui/inventorywindow.h"
 
30
#include "gui/npcdialog.h"
 
31
#include "gui/npcpostdialog.h"
 
32
 
 
33
#include "resources/iteminfo.h"
 
34
 
 
35
#include "net/inventoryhandler.h"
 
36
#include "net/net.h"
 
37
#include "net/playerhandler.h"
 
38
 
 
39
#include "debug.h"
 
40
 
 
41
namespace PlayerInfo
 
42
{
 
43
 
 
44
PlayerInfoBackend mData;
 
45
int mCharId = 0;
 
46
 
 
47
Inventory *mInventory = nullptr;
 
48
Equipment *mEquipment = nullptr;
 
49
 
 
50
#ifdef MANASERV_SUPPORT
 
51
std::map<int, Special> mSpecials;
 
52
signed char mSpecialRechargeUpdateNeeded = 0;
 
53
#endif
 
54
 
 
55
bool mTrading = false;
 
56
int mLevelProgress = 0;
 
57
std::set<int> mProtectedItems;
 
58
 
 
59
// --- Triggers ---------------------------------------------------------------
 
60
 
 
61
void triggerAttr(const int id, const int old)
 
62
{
 
63
    DepricatedEvent event(EVENT_UPDATEATTRIBUTE);
 
64
    event.setInt("id", id);
 
65
    event.setInt("oldValue", old);
 
66
    event.setInt("newValue", mData.mAttributes.find(id)->second);
 
67
    DepricatedEvent::trigger(CHANNEL_ATTRIBUTES, event);
 
68
}
 
69
 
 
70
void triggerStat(const int id, const std::string &changed,
 
71
                 const int old1, const int old2)
 
72
{
 
73
    const StatMap::const_iterator it = mData.mStats.find(id);
 
74
    if (it == mData.mStats.end())
 
75
        return;
 
76
 
 
77
    DepricatedEvent event(EVENT_UPDATESTAT);
 
78
    event.setInt("id", id);
 
79
    const Stat &stat = it->second;
 
80
    event.setInt("base", stat.base);
 
81
    event.setInt("mod", stat.mod);
 
82
    event.setInt("exp", stat.exp);
 
83
    event.setInt("expNeeded", stat.expNeed);
 
84
    event.setString("changed", changed);
 
85
    event.setInt("oldValue1", old1);
 
86
    event.setInt("oldValue2", old2);
 
87
    DepricatedEvent::trigger(CHANNEL_ATTRIBUTES, event);
 
88
}
 
89
 
 
90
// --- Attributes -------------------------------------------------------------
 
91
 
 
92
int getAttribute(const int id)
 
93
{
 
94
    const IntMap::const_iterator it = mData.mAttributes.find(id);
 
95
    if (it != mData.mAttributes.end())
 
96
        return it->second;
 
97
    else
 
98
        return 0;
 
99
}
 
100
 
 
101
void setAttribute(const int id, const int value, const bool notify)
 
102
{
 
103
    const int old = mData.mAttributes[id];
 
104
    mData.mAttributes[id] = value;
 
105
    if (notify)
 
106
        triggerAttr(id, old);
 
107
}
 
108
 
 
109
int getSkillLevel(const int id)
 
110
{
 
111
    const IntMap::const_iterator it = mData.mSkills.find(id);
 
112
    if (it != mData.mSkills.end())
 
113
        return it->second;
 
114
    else
 
115
        return 0;
 
116
}
 
117
 
 
118
void setSkillLevel(const int id, const int value)
 
119
{
 
120
    mData.mSkills[id] = value;
 
121
}
 
122
 
 
123
// --- Stats ------------------------------------------------------------------
 
124
 
 
125
int getStatBase(const int id)
 
126
{
 
127
    const StatMap::const_iterator it = mData.mStats.find(id);
 
128
    if (it != mData.mStats.end())
 
129
        return it->second.base;
 
130
    else
 
131
        return 0;
 
132
}
 
133
 
 
134
void setStatBase(const int id, const int value, const bool notify)
 
135
{
 
136
    const int old = mData.mStats[id].base;
 
137
    mData.mStats[id].base = value;
 
138
    if (notify)
 
139
        triggerStat(id, "base", old);
 
140
}
 
141
 
 
142
int getStatMod(const int id)
 
143
{
 
144
    const StatMap::const_iterator it = mData.mStats.find(id);
 
145
    if (it != mData.mStats.end())
 
146
        return it->second.mod;
 
147
    else
 
148
        return 0;
 
149
}
 
150
 
 
151
void setStatMod(const int id, const int value, const bool notify)
 
152
{
 
153
    const int old = mData.mStats[id].mod;
 
154
    mData.mStats[id].mod = value;
 
155
    if (notify)
 
156
        triggerStat(id, "mod", old);
 
157
}
 
158
 
 
159
int getStatEffective(const int id)
 
160
{
 
161
    const StatMap::const_iterator it = mData.mStats.find(id);
 
162
    if (it != mData.mStats.end())
 
163
        return it->second.base + it->second.mod;
 
164
    else
 
165
        return 0;
 
166
}
 
167
 
 
168
const std::pair<int, int> getStatExperience(const int id)
 
169
{
 
170
    const StatMap::const_iterator it = mData.mStats.find(id);
 
171
    int a, b;
 
172
    if (it != mData.mStats.end())
 
173
    {
 
174
        a = it->second.exp;
 
175
        b = it->second.expNeed;
 
176
    }
 
177
    else
 
178
    {
 
179
        a = 0;
 
180
        b = 0;
 
181
    }
 
182
    return std::pair<int, int>(a, b);
 
183
}
 
184
 
 
185
void setStatExperience(const int id, const int have,
 
186
                       const int need, const bool notify)
 
187
{
 
188
    Stat &stat = mData.mStats[id];
 
189
 
 
190
    const int oldExp = stat.exp;
 
191
    const int oldExpNeed = stat.expNeed;
 
192
    stat.exp = have;
 
193
    stat.expNeed = need;
 
194
    if (notify)
 
195
        triggerStat(id, "exp", oldExp, oldExpNeed);
 
196
}
 
197
 
 
198
// --- Inventory / Equipment --------------------------------------------------
 
199
 
 
200
Inventory *getInventory()
 
201
{
 
202
    return mInventory;
 
203
}
 
204
 
 
205
Inventory *getStorageInventory()
 
206
{
 
207
    return Net::getInventoryHandler()->getStorage();
 
208
}
 
209
 
 
210
void clearInventory()
 
211
{
 
212
    if (mEquipment)
 
213
        mEquipment->clear();
 
214
    if (mInventory)
 
215
        mInventory->clear();
 
216
}
 
217
 
 
218
void setInventoryItem(const int index, const int id,
 
219
                      const int amount, const int refine)
 
220
{
 
221
    bool equipment = false;
 
222
    const int itemType = ItemDB::get(id).getType();
 
223
    if (itemType != ITEM_UNUSABLE && itemType != ITEM_USABLE)
 
224
        equipment = true;
 
225
    if (mInventory)
 
226
        mInventory->setItem(index, id, amount, refine, equipment);
 
227
}
 
228
 
 
229
Equipment *getEquipment()
 
230
{
 
231
    return mEquipment;
 
232
}
 
233
 
 
234
Item *getEquipment(const unsigned int slot)
 
235
{
 
236
    if (mEquipment)
 
237
        return mEquipment->getEquipment(slot);
 
238
    else
 
239
        return nullptr;
 
240
}
 
241
 
 
242
void setEquipmentBackend(Equipment::Backend *const backend)
 
243
{
 
244
    if (mEquipment)
 
245
        mEquipment->setBackend(backend);
 
246
}
 
247
 
 
248
// --- Misc -------------------------------------------------------------------
 
249
 
 
250
void setBackend(const PlayerInfoBackend &backend)
 
251
{
 
252
    mData = backend;
 
253
}
 
254
 
 
255
void setCharId(const int charId)
 
256
{
 
257
    mCharId = charId;
 
258
}
 
259
 
 
260
int getCharId()
 
261
{
 
262
    return mCharId;
 
263
}
 
264
 
 
265
void logic()
 
266
{
 
267
#ifdef MANASERV_SUPPORT
 
268
    if ((mSpecialRechargeUpdateNeeded % 11) == 0)
 
269
    {
 
270
        mSpecialRechargeUpdateNeeded = 0;
 
271
        FOR_EACH (SpecialsMap::iterator, it, mSpecials)
 
272
        {
 
273
            Special &special = it->second;
 
274
            special.currentMana += special.recharge;
 
275
            if (special.currentMana > special.neededMana)
 
276
                special.currentMana = special.neededMana;
 
277
        }
 
278
    }
 
279
    mSpecialRechargeUpdateNeeded++;
 
280
#endif
 
281
}
 
282
 
 
283
bool isTrading()
 
284
{
 
285
    return mTrading;
 
286
}
 
287
 
 
288
void setTrading(const bool trading)
 
289
{
 
290
    mTrading = trading;
 
291
}
 
292
 
 
293
void updateAttrs()
 
294
{
 
295
    const Net::PlayerHandler *const handler = Net::getPlayerHandler();
 
296
    if (!handler)
 
297
        return;
 
298
    const int attr = handler->getAttackLocation();
 
299
    const int attackDelay = getStatBase(ATTACK_DELAY);
 
300
    if (attr != -1 && attackDelay)
 
301
    {
 
302
        setStatBase(ATTACK_SPEED, getStatBase(attr) * 1000
 
303
            / attackDelay, false);
 
304
        setStatMod(ATTACK_SPEED, getStatMod(attr) * 1000
 
305
            / attackDelay, true);
 
306
    }
 
307
    else
 
308
    {
 
309
        setStatBase(ATTACK_SPEED, 0, false);
 
310
        setStatMod(ATTACK_SPEED, 0, true);
 
311
    }
 
312
}
 
313
 
 
314
void init()
 
315
{
 
316
}
 
317
 
 
318
void deinit()
 
319
{
 
320
    clearInventory();
 
321
}
 
322
 
 
323
void loadData()
 
324
{
 
325
    mProtectedItems.clear();
 
326
    splitToIntSet(mProtectedItems,
 
327
        serverConfig.getStringValue("protectedItems"), ',');
 
328
}
 
329
 
 
330
void clear()
 
331
{
 
332
    mData.mSkills.clear();
 
333
}
 
334
 
 
335
bool isTalking()
 
336
{
 
337
    return NpcDialog::isActive() || NpcPostDialog::isActive()
 
338
        || InventoryWindow::isStorageActive();
 
339
}
 
340
 
 
341
void gameDestroyed()
 
342
{
 
343
    delete mInventory;
 
344
    mInventory = nullptr;
 
345
    delete mEquipment;
 
346
    mEquipment = nullptr;
 
347
}
 
348
 
 
349
void stateChange(const int state)
 
350
{
 
351
    if (state == STATE_GAME)
 
352
    {
 
353
        if (!mInventory)
 
354
        {
 
355
            mInventory = new Inventory(Inventory::INVENTORY);
 
356
            mEquipment = new Equipment();
 
357
        }
 
358
    }
 
359
}
 
360
 
 
361
static void saveProtectedItems()
 
362
{
 
363
    std::string str;
 
364
    std::set<int>::const_iterator it = mProtectedItems.begin();
 
365
    std::set<int>::const_iterator it_end = mProtectedItems.end();
 
366
    if (it != it_end)
 
367
    {
 
368
        str.append(toString(*it));
 
369
        ++ it;
 
370
    }
 
371
    while (it != it_end)
 
372
    {
 
373
        str.append(",").append(toString(*it));
 
374
        ++ it;
 
375
    }
 
376
    serverConfig.setValue("protectedItems", str);
 
377
    serverConfig.write();
 
378
}
 
379
 
 
380
void protectItem(const int id)
 
381
{
 
382
    mProtectedItems.insert(id);
 
383
    saveProtectedItems();
 
384
}
 
385
 
 
386
void unprotectItem(const int id)
 
387
{
 
388
    mProtectedItems.erase(id);
 
389
    saveProtectedItems();
 
390
}
 
391
 
 
392
bool isItemProtected(const int id)
 
393
{
 
394
    return mProtectedItems.find(id) != mProtectedItems.end();
 
395
}
 
396
 
 
397
}  // namespace PlayerInfo