ManaPlus
playerinfo.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2010 The Mana Developers
4  * Copyright (C) 2011-2018 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 "configuration.h"
25 #include "itemsoundmanager.h"
26 
27 #include "being/localplayer.h"
28 
29 #include "being/homunculusinfo.h"
30 #include "being/mercenaryinfo.h"
31 #include "being/petinfo.h"
32 
34 #include "gui/windows/npcdialog.h"
35 
36 #include "listeners/statlistener.h"
37 
38 #include "net/homunculushandler.h"
39 #include "net/inventoryhandler.h"
40 #include "net/mercenaryhandler.h"
41 #include "net/playerhandler.h"
42 
43 #include "resources/item/item.h"
44 
45 #include "utils/delete2.h"
46 
48 
49 #include "debug.h"
50 
51 namespace PlayerInfo
52 {
53 
55 int mCharId = 0;
56 
57 Inventory *mInventory = nullptr;
61 PetInfo *mPet = nullptr;
62 std::string mRoomName;
63 Equipment *mEquipment = nullptr;
67 
69 bool mVending = false;
71 int mServerLanguage = -1;
72 std::set<int> mProtectedItems;
73 
74 // --- Triggers ---------------------------------------------------------------
75 
76 void triggerAttr(const AttributesT id,
77  const int64_t old)
78 {
80  mData.mAttributes.find(id)->second);
81 }
82 
83 void triggerStat(const AttributesT id,
84  const int old1,
85  const int old2)
86 {
87  StatListener::distributeEvent(id, old1, old2);
88 }
89 
90 // --- Attributes -------------------------------------------------------------
91 
92 int64_t getAttribute64(const AttributesT id)
93 {
94  const AtrIntMap::const_iterator it = mData.mAttributes.find(id);
95  if (it != mData.mAttributes.end())
96  return it->second;
97  return 0;
98 }
99 
100 int32_t getAttribute(const AttributesT id)
101 {
102  const AtrIntMap::const_iterator it = mData.mAttributes.find(id);
103  if (it != mData.mAttributes.end())
104  return CAST_S32(it->second);
105  return 0;
106 }
107 
108 void setAttribute(const AttributesT id,
109  const int64_t value,
110  const Notify notify)
111 {
112  const int64_t old = mData.mAttributes[id];
113  mData.mAttributes[id] = value;
114  if (notify == Notify_true)
115  triggerAttr(id, old);
116 }
117 
118 int getSkillLevel(const int id)
119 {
120  const IntMap::const_iterator it = mData.mSkills.find(id);
121  if (it != mData.mSkills.end())
122  return it->second;
123  return 0;
124 }
125 
126 void setSkillLevel(const int id, const int value)
127 {
128  mData.mSkills[id] = value;
129 }
130 
131 // --- Stats ------------------------------------------------------------------
132 
134 {
135  const StatMap::const_iterator it = mData.mStats.find(id);
136  if (it != mData.mStats.end())
137  return it->second.base;
138  return 0;
139 }
140 
141 void setStatBase(const AttributesT id, const int value, const Notify notify)
142 {
143  const int old = mData.mStats[id].base;
144  mData.mStats[id].base = value;
145  if (notify == Notify_true)
146  triggerStat(id, old, 0);
147 }
148 
149 int getStatMod(const AttributesT id)
150 {
151  const StatMap::const_iterator it = mData.mStats.find(id);
152  if (it != mData.mStats.end())
153  return it->second.mod;
154  return 0;
155 }
156 
157 void setStatMod(const AttributesT id, const int value, const Notify notify)
158 {
159  const int old = mData.mStats[id].mod;
160  mData.mStats[id].mod = value;
161  if (notify == Notify_true)
162  triggerStat(id, old, 0);
163 }
164 
166 {
167  const StatMap::const_iterator it = mData.mStats.find(id);
168  if (it != mData.mStats.end())
169  return it->second.base + it->second.mod;
170  return 0;
171 }
172 
173 const std::pair<int, int> getStatExperience(const AttributesT id)
174 {
175  const StatMap::const_iterator it = mData.mStats.find(id);
176  int a;
177  int b;
178  if (it != mData.mStats.end())
179  {
180  a = it->second.exp;
181  b = it->second.expNeed;
182  }
183  else
184  {
185  a = 0;
186  b = 0;
187  }
188  return std::pair<int, int>(a, b);
189 }
190 
191 // --- Inventory / Equipment --------------------------------------------------
192 
194 {
195  return mInventory;
196 }
197 
199 {
200  if (inventoryHandler != nullptr)
201  return inventoryHandler->getStorage();
202  return nullptr;
203 }
204 
206 {
207  return mCartInventory;
208 }
209 
211 {
212  if (mEquipment != nullptr)
213  mEquipment->clear();
214  if (mInventory != nullptr)
215  mInventory->clear();
216 }
217 
219 {
220  return mEquipment;
221 }
222 
223 const Item *getEquipment(const unsigned int slot)
224 {
225  if (mEquipment != nullptr)
226  return mEquipment->getEquipment(slot);
227  return nullptr;
228 }
229 
231 {
232  if (mEquipment != nullptr)
233  mEquipment->setBackend(backend);
234 }
235 
236 void equipItem(const Item *const item, const Sfx sfx)
237 {
238  if (sfx == Sfx_true)
240  if (inventoryHandler != nullptr)
242 }
243 
244 void unequipItem(const Item *const item, const Sfx sfx)
245 {
246  if (sfx == Sfx_true)
248  if (inventoryHandler != nullptr)
250 }
251 
252 void useItem(const Item *const item, const Sfx sfx)
253 {
254  if (sfx == Sfx_true)
256  if (inventoryHandler != nullptr)
257  inventoryHandler->useItem(item);
258 }
259 
260 void useEquipItem(const Item *const item,
261  const int16_t useType,
262  const Sfx sfx)
263 {
264  if (item != nullptr)
265  {
266  if (item->getType() == ItemType::Card)
267  {
268  if (mProtectedItems.find(item->getId()) == mProtectedItems.end())
269  {
270  if (inventoryHandler != nullptr)
271  inventoryHandler->useCard(item);
272  if (sfx == Sfx_true)
274  }
275  }
276  else if (item->isEquipment() == Equipm_true)
277  {
278  if (item->isEquipped() == Equipped_true)
279  {
280  if (sfx == Sfx_true)
282  if (inventoryHandler != nullptr)
284  }
285  else
286  {
287  if (sfx == Sfx_true)
289  if (inventoryHandler != nullptr)
291  }
292  }
293  else
294  {
295  if (mProtectedItems.find(item->getId()) == mProtectedItems.end())
296  {
297  if (inventoryHandler != nullptr)
298  {
299  if (useType == 0)
300  inventoryHandler->useItem(item);
301  else
302  inventoryHandler->useItem(item, useType);
303  }
304  if (sfx == Sfx_true)
306  }
307  }
308  }
309 }
310 
311 void useEquipItem2(const Item *const item,
312  const int16_t useType,
313  const Sfx sfx)
314 {
315  if (item != nullptr)
316  {
317  if (item->isEquipment() == Equipm_false)
318  {
319  if (item->isEquipped() == Equipped_true)
320  {
321  if (sfx == Sfx_true)
323  if (inventoryHandler != nullptr)
325  }
326  else
327  {
328  if (sfx == Sfx_true)
330  if (inventoryHandler != nullptr)
332  }
333  }
334  else
335  {
336  if (mProtectedItems.find(item->getId()) == mProtectedItems.end())
337  {
338  if (sfx == Sfx_true)
340  if (inventoryHandler != nullptr)
341  {
342  if (useType == 0)
343  inventoryHandler->useItem(item);
344  else
345  inventoryHandler->useItem(item, useType);
346  }
347  }
348  }
349  }
350 }
351 
352 void dropItem(const Item *const item, const int amount, const Sfx sfx)
353 {
354  if (item != nullptr &&
355  mProtectedItems.find(item->getId()) == mProtectedItems.end())
356  {
357  if (sfx == Sfx_true)
359  if (inventoryHandler != nullptr)
360  inventoryHandler->dropItem(item, amount);
361  }
362 }
363 
364 void pickUpItem(const FloorItem *const item, const Sfx sfx)
365 {
366  if (sfx == Sfx_true)
368  if (playerHandler != nullptr)
369  playerHandler->pickUp(item);
370 }
371 
372 // --- Misc -------------------------------------------------------------------
373 
374 void setBackend(const PlayerInfoBackend &backend)
375 {
376  mData = backend;
377 }
378 
379 void setCharId(const int charId)
380 {
381  mCharId = charId;
382 }
383 
385 {
386  return mCharId;
387 }
388 
390 {
391  return mTrading;
392 }
393 
394 void setTrading(const Trading trading)
395 {
396  mTrading = trading;
397 }
398 
399 #define updateAttackStat(atk, delay, speed) \
400  attackDelay = getStatBase(delay); \
401  if (attackDelay != 0) \
402  { \
403  setStatBase(speed, \
404  getStatBase(atk) * 1000 / attackDelay, \
405  Notify_false); \
406  setStatMod(speed, \
407  getStatMod(atk) * 1000 / attackDelay, \
408  Notify_true); \
409  } \
410  else \
411  { \
412  setStatBase(speed, 0, \
413  Notify_false); \
414  setStatMod(speed, 0, \
415  Notify_true); \
416  }
417 
419 {
420  int attackDelay;
430 }
431 
432 void init()
433 {
434 }
435 
436 void deinit()
437 {
438  clearInventory();
439  delete2(mMercenary);
440  mPetBeingId = BeingId_zero;
441 }
442 
443 void loadData()
444 {
445  mProtectedItems.clear();
446  splitToIntSet(mProtectedItems,
447  serverConfig.getStringValue("protectedItems"), ',');
448 }
449 
450 void clear()
451 {
452  mData.mSkills.clear();
453  mPetBeingId = BeingId_zero;
454 }
455 
456 bool isTalking()
457 {
459 }
460 
462 {
463  delete2(mInventory);
464  delete2(mEquipment);
465  delete2(mCartInventory);
466 }
467 
468 void stateChange(const StateT state)
469 {
470  if (state == State::GAME)
471  {
472  if (mInventory == nullptr)
473  {
474  mInventory = new Inventory(InventoryType::Inventory, -1);
475  mEquipment = new Equipment;
476  mCartInventory = new Inventory(InventoryType::Cart, -1);
477  }
478  }
479 }
480 
481 static void saveProtectedItems()
482 {
483  std::string str;
484  std::set<int>::const_iterator it = mProtectedItems.begin();
485  std::set<int>::const_iterator it_end = mProtectedItems.end();
486  if (it != it_end)
487  {
488  str.append(toString(*it));
489  ++ it;
490  }
491  while (it != it_end)
492  {
493  str.append(",").append(toString(*it));
494  ++ it;
495  }
496  serverConfig.setValue("protectedItems", str);
498 }
499 
500 void protectItem(const int id)
501 {
502  mProtectedItems.insert(id);
504 }
505 
506 void unprotectItem(const int id)
507 {
508  mProtectedItems.erase(id);
510 }
511 
512 bool isItemProtected(const int id)
513 {
514  return mProtectedItems.find(id) != mProtectedItems.end();
515 }
516 
518 {
519  delete mMercenary;
520  mMercenary = info;
521 }
522 
523 void setMercenaryBeing(Being *const being)
524 {
525  if (being == nullptr ||
526  mMercenary == nullptr)
527  {
528  return;
529  }
530  being->setName(mMercenary->name);
531  being->setOwner(localPlayer);
532  being->setLevel(mMercenary->level);
533  being->setAttackRange(mMercenary->range);
534 }
535 
536 void setElemental(const BeingId id)
537 {
538  mElementalId = id;
539 }
540 
542 {
543  return mElementalId;
544 }
545 
547 {
548  return mMercenary;
549 }
550 
551 void setPet(PetInfo *const info)
552 {
553  delete mPet;
554  mPet = info;
555 }
556 
557 void setPetBeing(Being *const being)
558 {
559  if (being != nullptr)
560  mPetBeingId = being->getId();
561  else
562  mPetBeingId = BeingId_zero;
563  if (being == nullptr ||
564  mPet == nullptr)
565  {
566  return;
567  }
568  being->setName(mPet->name);
569  being->setOwner(localPlayer);
570  being->setLevel(mPet->level);
571 }
572 
574 {
575  return mPet;
576 }
577 
579 {
580  return mPetBeingId;
581 }
582 
584 {
585  delete mHomunculus;
586  mHomunculus = info;
587 }
588 
589 void setHomunculusBeing(Being *const being)
590 {
591  if (being == nullptr ||
592  mHomunculus == nullptr)
593  {
594  return;
595  }
596  being->setName(mHomunculus->name);
597  being->setOwner(localPlayer);
598 }
599 
601 {
602  return mHomunculus;
603 }
604 
606 {
607  return mHomunculus != nullptr ? mHomunculus->id : BeingId_zero;
608 }
609 
611 {
612  return mMercenary != nullptr ? mMercenary->id : BeingId_zero;
613 }
614 
615 void updateAttackAi(const BeingId targetId,
616  const Keep keep)
617 {
618  if (mMercenary != nullptr &&
619  mercenaryHandler != nullptr)
620  {
621  mercenaryHandler->attack(targetId, keep);
622  }
623  if (mHomunculus != nullptr &&
624  homunculusHandler != nullptr)
625  {
626  homunculusHandler->attack(targetId, keep);
627  }
628 }
629 
630 std::string getRoomName()
631 {
632  return mRoomName;
633 }
634 
635 void setRoomName(const std::string &name)
636 {
637  mRoomName = name;
638 }
639 
640 bool isInRoom()
641 {
642  return !mRoomName.empty();
643 }
644 
646 {
647  mGuildPositionFlags = pos;
648 }
649 
651 {
652  return mGuildPositionFlags;
653 }
654 
655 void enableVending(const bool b)
656 {
657  mVending = b;
658 }
659 
660 bool isVending()
661 {
662  return mVending;
663 }
664 
665 void setServerLanguage(const int lang)
666 {
667  if (lang != mServerLanguage)
668  {
669  mServerLanguage = lang;
671  }
672 }
673 
675 {
676  return mServerLanguage;
677 }
678 
679 } // namespace PlayerInfo
Net::PlayerHandler * playerHandler
Definition: net.cpp:93
void setGuildPositionFlags(const GuildPositionFlags::Type pos)
Definition: playerinfo.cpp:645
static bool isActive()
Definition: npcdialog.h:190
void gameDestroyed()
Definition: playerinfo.cpp:461
static bool isStorageActive()
virtual void attack(const BeingId targetId, const Keep keep) const =0
void deinit()
Definition: playerinfo.cpp:436
std::string getStringValue(const std::string &key) const
std::string mRoomName
Definition: playerinfo.cpp:62
void setStatMod(const AttributesT id, const int value, const Notify notify)
Definition: playerinfo.cpp:157
static void distributeEvent(const AttributesT id, const int64_t oldVal, const int64_t newVal)
virtual Inventory * getStorage() const =0
void setPetBeing(Being *const being)
Definition: playerinfo.cpp:557
void setAttackRange(const int range)
Definition: being.h:842
BeingId mElementalId
Definition: playerinfo.cpp:66
void splitToIntSet(std::set< int > &tokens, const std::string &text, const char separator)
void equipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:236
const bool Trading_false
Definition: trading.h:29
bool isTalking()
Definition: playerinfo.cpp:456
void setStatBase(const AttributesT id, const int value, const Notify notify)
Definition: playerinfo.cpp:141
Equipment * getEquipment()
Definition: playerinfo.cpp:218
void init()
Definition: playerinfo.cpp:432
std::string name
Equipped isEquipped() const
Definition: item.h:128
GuildPositionFlags::Type getGuildPositionFlags()
Definition: playerinfo.cpp:650
BeingId getId() const
Definition: actorsprite.h:63
std::string name
Definition: mercenaryinfo.h:40
virtual void useItem(const Item *const item) const =0
#define updateAttackStat(atk, delay, speed)
Definition: playerinfo.cpp:399
Inventory * getInventory()
Definition: playerinfo.cpp:193
Trading mTrading
Definition: playerinfo.cpp:68
const std::pair< int, int > getStatExperience(const AttributesT id)
Definition: playerinfo.cpp:173
void setHomunculus(HomunculusInfo *const info)
Definition: playerinfo.cpp:583
void clearInventory()
Definition: playerinfo.cpp:210
bool Trading
Definition: trading.h:29
const bool Sfx_true
Definition: sfx.h:29
int BeingId
Definition: beingid.h:29
void clear()
Definition: playerinfo.cpp:450
BeingId mPetBeingId
Definition: playerinfo.cpp:64
bool isInRoom()
Definition: playerinfo.cpp:640
State ::T StateT
Definition: state.h:70
std::string name
Definition: petinfo.h:46
PetInfo * mPet
Definition: playerinfo.cpp:61
void triggerAttr(const AttributesT id, const int64_t old)
Definition: playerinfo.cpp:76
void pickUpItem(const FloorItem *const item, const Sfx sfx)
Definition: playerinfo.cpp:364
void setEquipmentBackend(Equipment::Backend *const backend)
Definition: playerinfo.cpp:230
void notify(const unsigned int message)
#define delete2(var)
Definition: delete2.h:24
Configuration serverConfig
const bool Equipped_true
Definition: equipped.h:29
virtual void pickUp(const FloorItem *const floorItem) const =0
void setCharId(const int charId)
Definition: playerinfo.cpp:379
void updateAttrs()
Definition: playerinfo.cpp:418
int getStatEffective(const AttributesT id)
Definition: playerinfo.cpp:165
void clear()
Definition: inventory.cpp:237
void setHomunculusBeing(Being *const being)
Definition: playerinfo.cpp:589
const Item * getEquipment(const int index) const
Definition: equipment.h:68
MercenaryInfo * getMercenary()
Definition: playerinfo.cpp:546
int64_t getAttribute64(const AttributesT id)
Definition: playerinfo.cpp:92
void setElemental(const BeingId id)
Definition: playerinfo.cpp:536
virtual void unequipItem(const Item *const item) const =0
void triggerStat(const AttributesT id, const int old1, const int old2)
Definition: playerinfo.cpp:83
bool Keep
Definition: keep.h:29
virtual void equipItem(const Item *const item) const =0
std::string getRoomName()
Definition: playerinfo.cpp:630
bool Notify
Definition: notify.h:29
void useEquipItem(const Item *const item, const int16_t useType, const Sfx sfx)
Definition: playerinfo.cpp:260
Inventory * getCartInventory()
Definition: playerinfo.cpp:205
#define CAST_S32
Definition: cast.h:29
static void distributeEvent(const AttributesT id, const int oldVal1, const int oldVal2)
void clear()
Definition: equipment.h:77
int mServerLanguage
Definition: playerinfo.cpp:71
const bool Equipm_true
Definition: equipm.h:29
bool info(InputEvent &event)
Definition: commands.cpp:56
void useEquipItem2(const Item *const item, const int16_t useType, const Sfx sfx)
Definition: playerinfo.cpp:311
void setOwner(Being *const owner)
Definition: being.h:910
void updateAttackAi(const BeingId targetId, const Keep keep)
Definition: playerinfo.cpp:615
void setBackend(Backend *const backend)
Definition: equipment.h:87
LocalPlayer * localPlayer
void setAttribute(const AttributesT id, const int64_t value, const Notify notify)
Definition: playerinfo.cpp:108
void protectItem(const int id)
Definition: playerinfo.cpp:500
AtrIntMap mAttributes
Definition: playerinfo.h:72
Definition: item.h:48
Inventory * getStorageInventory()
Definition: playerinfo.cpp:198
Net::MercenaryHandler * mercenaryHandler
Definition: net.cpp:114
BeingId getHomunculusId()
Definition: playerinfo.cpp:605
static void saveProtectedItems()
Definition: playerinfo.cpp:481
PlayerInfoBackend mData
Definition: playerinfo.cpp:54
MercenaryInfo * mMercenary
Definition: playerinfo.cpp:59
int getStatBase(const AttributesT id)
Definition: playerinfo.cpp:133
void useItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:252
void setMercenary(MercenaryInfo *const info)
Definition: playerinfo.cpp:517
BeingId getMercenaryId()
Definition: playerinfo.cpp:610
void setSkillLevel(const int id, const int value)
Definition: playerinfo.cpp:126
int getCharId()
Definition: playerinfo.cpp:384
Net::InventoryHandler * inventoryHandler
Definition: net.cpp:86
BeingId getElementalId()
Definition: playerinfo.cpp:541
virtual void attack(const BeingId targetId, const Keep keep) const =0
bool isVending()
Definition: playerinfo.cpp:660
Inventory * mInventory
Definition: playerinfo.cpp:57
static void playSfx(const int item, const ItemSoundEvent::Type sound)
void enableVending(const bool b)
Definition: playerinfo.cpp:655
bool isItemProtected(const int id)
Definition: playerinfo.cpp:512
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
int getId() const
Definition: item.h:80
void setRoomName(const std::string &name)
Definition: playerinfo.cpp:635
void unequipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:244
Net::HomunculusHandler * homunculusHandler
Definition: net.cpp:109
void loadData()
Definition: playerinfo.cpp:443
int getSkillLevel(const int id)
Definition: playerinfo.cpp:118
HomunculusInfo * mHomunculus
Definition: playerinfo.cpp:60
Equipment * mEquipment
Definition: playerinfo.cpp:63
bool Sfx
Definition: sfx.h:29
void setName(const std::string &name)
Definition: being.cpp:1135
HomunculusInfo * getHomunculus()
Definition: playerinfo.cpp:600
Attributes ::T AttributesT
Definition: attributes.h:117
ItemTypeT getType() const
Definition: item.h:224
Equipm isEquipment() const
Definition: item.h:116
void setMercenaryBeing(Being *const being)
Definition: playerinfo.cpp:523
void stateChange(const StateT state)
Definition: playerinfo.cpp:468
int level
Definition: petinfo.h:48
BeingId getPetBeingId()
Definition: playerinfo.cpp:578
const BeingId BeingId_zero
Definition: beingid.h:29
void setBackend(const PlayerInfoBackend &backend)
Definition: playerinfo.cpp:374
bool mVending
Definition: playerinfo.cpp:69
static void loadDictionaryLang()
void setServerLanguage(const int lang)
Definition: playerinfo.cpp:665
std::set< int > mProtectedItems
Definition: playerinfo.cpp:72
PetInfo * getPet()
Definition: playerinfo.cpp:573
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:100
int mLevelProgress
Definition: playerinfo.cpp:70
void unprotectItem(const int id)
Definition: playerinfo.cpp:506
int getStatMod(const AttributesT id)
Definition: playerinfo.cpp:149
int getServerLanguage()
Definition: playerinfo.cpp:674
Definition: being.h:93
void setValue(const std::string &key, const std::string &value)
GuildPositionFlags::Type mGuildPositionFlags
Definition: playerinfo.cpp:65
virtual void dropItem(const Item *const item, const int amount) const =0
virtual void useCard(const Item *const item)=0
const bool Notify_true
Definition: notify.h:29
void dropItem(const Item *const item, const int amount, const Sfx sfx)
Definition: playerinfo.cpp:352
void setPet(PetInfo *const info)
Definition: playerinfo.cpp:551
Inventory * mCartInventory
Definition: playerinfo.cpp:58
const bool Equipm_false
Definition: equipm.h:29
void setLevel(const int n)
Definition: being.h:600
Trading isTrading()
Definition: playerinfo.cpp:389
void setTrading(const Trading trading)
Definition: playerinfo.cpp:394