Mana
Loading...
Searching...
No Matches
playerinfo.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "playerinfo.h"
22
23#include "client.h"
24#include "equipment.h"
25#include "event.h"
26#include "inventory.h"
27#include "eventlistener.h"
28#include "log.h"
29
31#include "net/net.h"
32
33#include "utils/time.h"
34
35namespace PlayerInfo {
36
37class PlayerLogic;
38
39static PlayerLogic *mListener;
40
41static PlayerInfoBackend mData;
42
43static Inventory *mInventory;
44static Equipment *mEquipment;
45
46static bool mStorageCount = false;
47
48static bool mNPCCount = false;
49static bool mNPCPostCount = false;
50
51static BuySellState mBuySellState = BUYSELL_NONE;
52
53static std::map<int, Ability> mAbilities;
54static Timer mAbilityRechargeUpdateTimer;
55
56// --- Triggers ---------------------------------------------------------------
57
58void triggerAttr(int id, int old)
59{
61 event.setInt("id", id);
62 event.setInt("oldValue", old);
63 event.setInt("newValue", mData.mAttributes.find(id)->second);
64 event.trigger(Event::AttributesChannel);
65}
66
67void triggerStat(int id, const std::string &changed, int old1, int old2 = 0)
68{
69 auto it = mData.mStats.find(id);
71 event.setInt("id", id);
72 event.setInt("base", it->second.base);
73 event.setInt("mod", it->second.mod);
74 event.setInt("exp", it->second.exp);
75 event.setInt("expNeeded", it->second.expNeed);
76 event.setString("changed", changed);
77 event.setInt("oldValue1", old1);
78 event.setInt("oldValue2", old2);
79 event.trigger(Event::AttributesChannel);
80}
81
82// --- Attributes -------------------------------------------------------------
83
84int getAttribute(int id)
85{
86 auto it = mData.mAttributes.find(id);
87 if (it != mData.mAttributes.end())
88 return it->second;
89
90 return 0;
91}
92
93void setAttribute(int id, int value, bool notify)
94{
95 int old = mData.mAttributes[id];
96 mData.mAttributes[id] = value;
97 if (notify)
98 triggerAttr(id, old);
99}
100
101// --- Stats ------------------------------------------------------------------
102
103int getStatBase(int id)
104{
105 auto it = mData.mStats.find(id);
106 if (it != mData.mStats.end())
107 return it->second.base;
108
109 return 0;
110}
111
112void setStatBase(int id, int value, bool notify)
113{
114 int old = mData.mStats[id].base;
115 mData.mStats[id].base = value;
116 if (notify)
117 triggerStat(id, "base", old);
118}
119
120int getStatMod(int id)
121{
122 auto it = mData.mStats.find(id);
123 if (it != mData.mStats.end())
124 return it->second.mod;
125
126 return 0;
127}
128
129void setStatMod(int id, int value, bool notify)
130{
131 int old = mData.mStats[id].mod;
132 mData.mStats[id].mod = value;
133 if (notify)
134 triggerStat(id, "mod", old);
135}
136
138{
139 auto it = mData.mStats.find(id);
140 if (it != mData.mStats.end())
141 return it->second.base + it->second.mod;
142
143 return 0;
144}
145
146std::pair<int, int> getStatExperience(int id)
147{
148 auto it = mData.mStats.find(id);
149 int a, b;
150 if (it != mData.mStats.end())
151 {
152 a = it->second.exp;
153 b = it->second.expNeed;
154 }
155 else
156 {
157 a = 0;
158 b = 0;
159 }
160 return { a, b };
161}
162
163void setStatExperience(int id, int have, int need, bool notify)
164{
165 int oldExp = mData.mStats[id].exp;
166 int oldExpNeed = mData.mStats[id].expNeed;
167 mData.mStats[id].exp = have;
168 mData.mStats[id].expNeed = need;
169 if (notify)
170 triggerStat(id, "exp", oldExp, oldExpNeed);
171}
172
173// --- Inventory / Equipment --------------------------------------------------
174
176{
177 return mInventory;
178}
179
181{
182 mEquipment->clear();
183 mInventory->clear();
184}
185
186void setInventoryItem(int index, int id, int amount)
187{
188 mInventory->setItem(index, id, amount);
189}
190
192{
193 return mEquipment;
194}
195
196Item *getEquipment(unsigned int slot)
197{
198 return mEquipment->getEquipment(slot);
199}
200
202{
203 return mStorageCount;
204}
205
206void setStorageCount(int count)
207{
208 int old = mStorageCount;
209 mStorageCount = count;
210
211 if (count != old)
212 {
214 event.setInt("oldCount", old);
215 event.setInt("newCount", count);
216 event.trigger(Event::StorageChannel);
217 }
218}
219
220// -- NPC ---------------------------------------------------------------------
221
223{
224 return mNPCCount;
225}
226
228{
229 int old = mNPCCount;
230 mNPCCount = count;
231
232 if (count != old)
233 {
234 Event event(Event::NpcCount);
235 event.setInt("oldCount", old);
236 event.setInt("newCount", count);
237 event.trigger(Event::NpcChannel);
238 }
239}
240
242{
243 return mNPCPostCount;
244}
245
246void setNPCPostCount(int count)
247{
248 int old = mNPCPostCount;
249 mNPCPostCount = count;
250
251 if (count != old)
252 {
253 Event event(Event::PostCount);
254 event.setInt("oldCount", old);
255 event.setInt("newCount", count);
256 event.trigger(Event::NpcChannel);
257 }
258}
259
260// -- Buy/Sell/Trade ----------------------------------------------------------
261
263{
264 return mBuySellState;
265}
266
268{
269 BuySellState old = mBuySellState;
270 mBuySellState = buySellState;
271
272 if (buySellState != old)
273 {
275 event.setInt("oldState", old);
276 event.setInt("newState", buySellState);
277 event.trigger(Event::BuySellChannel);
278 }
279}
280
281// --- Abilities --------------------------------------------------------------
282
284{
285 mAbilities.erase(id);
286}
287
288void setAbilityStatus(int id, int current, int max, int recharge)
289{
290 Log::info("AbilityUpdate Skill #%d -- (%d/%d) -> %d", id, current, max,
291 recharge);
292 mAbilities[id].currentMana = current;
293 mAbilities[id].neededMana = max;
294 mAbilities[id].recharge = recharge;
295}
296
297const std::map<int, Ability> &getAbilityStatus()
298{
299 return mAbilities;
300}
301
302// --- Misc -------------------------------------------------------------------
303
304void setBackend(const PlayerInfoBackend &backend)
305{
306 mData = backend;
307}
308
310{
313}
314
315void logic()
316{
317 if (mAbilityRechargeUpdateTimer.passed())
318 {
319 mAbilityRechargeUpdateTimer.set(100);
320
321 for (auto &[id, ability] : mAbilities)
322 {
323 ability.currentMana += ability.recharge;
324 if (ability.currentMana > ability.neededMana)
325 {
326 ability.currentMana = ability.neededMana;
327 }
328 }
329 }
330}
331
333{
334public:
340
341 void event(Event::Channel channel, const Event &event) override
342 {
343 if (channel == Event::ClientChannel)
344 {
345 if (event.getType() == Event::StateChange)
346 {
347 int newState = event.getInt("newState");
348
349 if (newState == STATE_GAME)
350 {
351 if (mInventory == nullptr)
352 {
353 mInventory = new Inventory(Inventory::INVENTORY);
354 mEquipment = new Equipment(Net::getInventoryHandler()->getEquipmentBackend());
355 }
356 }
357 }
358 }
359 else if (channel == Event::GameChannel)
360 {
361 if (event.getType() == Event::Destructed)
362 {
363 delete mInventory;
364 delete mEquipment;
365
366 mInventory = nullptr;
367 mEquipment = nullptr;
368 }
369 }
370 }
371};
372
373void init()
374{
375 if (mListener)
376 return;
377
378 mListener = new PlayerLogic();
379}
380
381} // namespace PlayerInfo
void clear()
Clears equipment.
Definition equipment.h:68
Item * getEquipment(int slotIndex) const
Get equipment at the given slot.
Definition equipment.h:53
void listen(Event::Channel channel)
Definition event.h:42
@ Destructed
Definition event.h:72
@ UpdateAttribute
Definition event.h:99
@ PostCount
Definition event.h:94
@ StateChange
Definition event.h:96
@ StorageCount
Definition event.h:97
@ UpdateStat
Definition event.h:100
@ NpcCount
Definition event.h:91
Channel
Definition event.h:45
@ StorageChannel
Definition event.h:56
@ AttributesChannel
Definition event.h:47
@ ClientChannel
Definition event.h:50
@ NpcChannel
Definition event.h:55
@ BuySellChannel
Definition event.h:48
@ GameChannel
Definition event.h:52
void clear()
Reset all item slots.
Definition inventory.cpp:96
void setItem(int index, int id, int quantity)
Sets the item at the given position.
Definition inventory.cpp:66
Represents one or more instances of a certain item type.
Definition item.h:35
void event(Event::Channel channel, const Event &event) override
Simple timer that can be used to check if a certain amount of time has passed.
Definition time.h:62
bool passed() const
Returns whether the timer has passed.
Definition time.h:88
void set(uint32_t ms=0)
Sets the timer with an optional duration in milliseconds.
Definition time.h:69
@ STATE_GAME
Definition client.h:73
void info(const char *log_text,...) LOG_PRINTF_ATTR
InventoryHandler * getInventoryHandler()
Definition net.cpp:90
A database like namespace which holds global info about the localplayer.
int getStatBase(int id)
Returns the base value of the given stat.
Inventory * getInventory()
Returns the player's inventory.
int getStatEffective(int id)
Returns the current effective value of the given stat.
int getStorageCount()
Returns the number of currently open storage windows.
void setBackend(const PlayerInfoBackend &backend)
Changes the internal PlayerInfoBackend reference;.
const std::map< int, Ability > & getAbilityStatus()
Returns the status all abilities.
void setBuySellState(BuySellState buySellState)
Sets which buy, sell, or related interaction the player is currently involved in.
void setNPCInteractionCount(int count)
Sets the number of currently open NPC interaction windows.
Equipment * getEquipment()
Returns the player's equipment.
void logic()
Does necessary updates every tick.
bool isTalking()
Returns true if the player is involved in a NPC interaction, false otherwise.
void setNPCPostCount(int count)
Sets the number of currently open NPC post windows.
void triggerAttr(int id, int old)
void init()
Initializes some internals.
void setStatExperience(int id, int have, int need, bool notify)
Changes the experience of the given stat.
void triggerStat(int id, const std::string &changed, int old1, int old2=0)
void setAbilityStatus(int id, int current, int max, int recharge)
Changes the status of the given ability.
void setStatBase(int id, int value, bool notify)
Changes the base value of the given stat.
BuySellState getBuySellState()
Returns the current buy, sell, or related interaction the player is involved in.
void setAttribute(int id, int value, bool notify)
Changes the value of the given attribute.
void clearInventory()
Clears the player's inventory and equipment.
void clearAbilityStatus(int id)
Removes the status for the given ability.
int getNPCPostCount()
Returns the number of currently open NPC post windows.
int getAttribute(int id)
Returns the value of the given attribute.
void setStorageCount(int count)
Sets the number of currently open storage windows.
int getStatMod(int id)
Returns the modifier for the given stat.
void setInventoryItem(int index, int id, int amount)
Changes the inventory item at the given slot.
std::pair< int, int > getStatExperience(int id)
Returns the experience of the given stat.
void setStatMod(int id, int value, bool notify)
Changes the modifier for the given stat.
int getNPCInteractionCount()
Returns the number of currently open NPC interaction windows.
BuySellState
Definition playerinfo.h:65
@ BUYSELL_NONE
Definition playerinfo.h:66
Backend for core player information.
Definition playerinfo.h:55
std::map< int, Stat > mStats
Definition playerinfo.h:57
std::map< int, int > mAttributes
Definition playerinfo.h:56