ManaPlus
beinginfo.cpp
Go to the documentation of this file.
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-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "resources/beinginfo.h"
25 
26 #include "configuration.h"
27 #include "logger.h"
28 
30 
32 
33 #include "resources/attack.h"
34 
36 
37 #include "resources/db/colordb.h"
38 
39 #include "utils/cast.h"
40 #include "utils/delete2.h"
41 #include "utils/dtor.h"
42 #include "utils/gettext.h"
43 
44 #include "debug.h"
45 
46 BeingInfo *BeingInfo::unknown = nullptr;
51  -1,
52  -1,
53  -1,
54  -1,
55  std::string(),
56  32.0F,
57  7.0F,
58  8.0F,
59  500);
60 
62  mDisplay(),
63  // TRANSLATORS: being info default name
64  mName(_("unnamed")),
65  mTargetCursorSize(TargetCursorSize::MEDIUM),
66  mHoverCursor(Cursor::CURSOR_POINTER),
67  mSounds(),
68  mAttacks(),
69  mMenu(),
70  mStrings(),
71  mCurrency(),
72  mBlockWalkMask(BlockMask::WALL |
73  BlockMask::AIR |
74  BlockMask::WATER |
76  mBlockType(BlockType::NONE),
78  mTargetOffsetX(0),
79  mTargetOffsetY(0),
80  mNameOffsetX(0),
81  mNameOffsetY(0),
82  mHpBarOffsetX(0),
83  mHpBarOffsetY(0),
84  mMaxHP(0),
85  mSortOffsetY(0),
86  mDeadSortOffsetY(31),
87  mAvatarId(BeingTypeId_zero),
88  mWidth(0),
89  mHeight(0),
90  mStartFollowDist(3),
91  mFollowDist(1),
92  mWarpDist(11),
93  mWalkSpeed(0),
94  mSitOffsetX(0),
95  mSitOffsetY(0),
96  mMoveOffsetX(0),
97  mMoveOffsetY(0),
98  mDeadOffsetX(0),
99  mDeadOffsetY(0),
100  mAttackOffsetX(0),
101  mAttackOffsetY(0),
102  mThinkTime(50),
103  mDirectionType(1),
104  mSitDirectionType(1),
105  mDeadDirectionType(1),
106  mAttackDirectionType(1),
107  mQuickActionEffectId(-1),
108  mStaticMaxHP(false),
109  mTargetSelection(true),
110  mAllowDelete(true),
111  mAllowEquipment(false)
112 {
113  SpriteDisplay display;
114  display.sprites.push_back(SpriteReference::Empty);
115 
116  setDisplay(display);
117 }
118 
120 {
123  mSounds.clear();
125 }
126 
128 {
129  mDisplay = display;
130 }
131 
132 void BeingInfo::setTargetCursorSize(const std::string &size)
133 {
134  if (size == "small")
135  {
137  }
138  else if (size == "medium")
139  {
141  }
142  else if (size == "large")
143  {
145  }
146  else
147  {
148  logger->log("Unknown target cursor type \"%s\" for %s - using medium "
149  "sized one", size.c_str(), getName().c_str());
151  }
152 }
153 
155  const std::string &filename,
156  const int delay)
157 {
158  if (mSounds.find(event) == mSounds.end())
159  mSounds[event] = new SoundInfoVect;
160 
161  if (mSounds[event] != nullptr)
162  mSounds[event]->push_back(SoundInfo(filename, delay));
163 }
164 
166 {
167  static SoundInfo emptySound("", 0);
168 
169  const ItemSoundEvents::const_iterator i = mSounds.find(event);
170 
171  if (i == mSounds.end())
172  return emptySound;
173 
174  const SoundInfoVect *const vect = i->second;
175  if (vect == nullptr || vect->empty())
176  return emptySound;
177  return vect->at(CAST_SIZE(rand()) % vect->size());
178 }
179 
180 const Attack *BeingInfo::getAttack(const int id) const
181 {
182  const Attacks::const_iterator i = mAttacks.find(id);
183  return (i == mAttacks.end()) ? empty : (*i).second;
184 }
185 
186 void BeingInfo::addAttack(const int id,
187  const std::string &action,
188  const std::string &skyAction,
189  const std::string &waterAction,
190  const std::string &rideAction,
191  const int effectId,
192  const int hitEffectId,
193  const int criticalHitEffectId,
194  const int missEffectId,
195  const std::string &missileParticle,
196  const float missileZ,
197  const float missileSpeed,
198  const float missileDieDistance,
199  const int missileLifeTime)
200 {
201  delete mAttacks[id];
202  mAttacks[id] = new Attack(action,
203  skyAction,
204  waterAction,
205  rideAction,
206  effectId,
207  hitEffectId,
208  criticalHitEffectId,
209  missEffectId,
210  missileParticle,
211  missileZ,
212  missileSpeed,
213  missileDieDistance,
214  missileLifeTime);
215 }
216 
218 {
220  delete2(empty)
221 }
222 
224 {
225  if (empty != nullptr)
226  {
227  empty->mEffectId = paths.getIntValue("effectId");
228  empty->mHitEffectId = paths.getIntValue("hitEffectId");
229  empty->mCriticalHitEffectId = paths.getIntValue("criticalHitEffectId");
230  empty->mMissEffectId = paths.getIntValue("missEffectId");
231  }
232 }
233 
234 void BeingInfo::setColorsList(const std::string &name)
235 {
236  if (name.empty())
237  mColors = nullptr;
238  else
240 }
241 
242 std::string BeingInfo::getColor(const ItemColor idx) const
243 {
244  if (mColors == nullptr)
245  return std::string();
246 
247  const std::map <ItemColor, ItemColorData>::const_iterator
248  it = mColors->find(idx);
249  if (it == mColors->end())
250  return std::string();
251  return it->second.color;
252 }
253 
254 void BeingInfo::addMenu(const std::string &name, const std::string &command)
255 {
256  mMenu.push_back(BeingMenuItem(name, command));
257 }
258 
259 const STD_VECTOR<BeingMenuItem> &BeingInfo::getMenu() const
260 {
261  return mMenu;
262 }
263 
264 std::string BeingInfo::getString(const int idx) const
265 {
266  const std::map<int, std::string>::const_iterator it = mStrings.find(idx);
267  if (it == mStrings.end())
268  return "";
269  return (*it).second;
270 }
const BeingTypeId BeingTypeId_zero
Definition: beingtypeid.h:30
#define CAST_SIZE
Definition: cast.h:34
void addSound(const ItemSoundEvent::Type event, const std::string &filename, const int delay)
Definition: beinginfo.cpp:154
SpriteDisplay mDisplay
Definition: beinginfo.h:361
std::map< int, std::string > mStrings
Definition: beinginfo.h:368
void setColorsList(const std::string &name)
Definition: beinginfo.cpp:234
std::vector< BeingMenuItem > mMenu
Definition: beinginfo.h:367
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:127
const std::map< ItemColor, ItemColorData > * mColors
Definition: beinginfo.h:372
void addAttack(const int id, const std::string &action, const std::string &skyAttack, const std::string &waterAttack, const std::string &rideAttack, const int effectId, const int hitEffectId, const int criticalHitEffectId, const int missEffectId, const std::string &missileParticle, const float missileZ, const float missileSpeed, const float missileDieDistance, const int missileLifeTime)
Definition: beinginfo.cpp:186
const std::string & getName() const
Definition: beinginfo.h:68
std::string getString(const int idx) const
Definition: beinginfo.cpp:264
void addMenu(const std::string &name, const std::string &command)
Definition: beinginfo.cpp:254
const std::vector< BeingMenuItem > & getMenu() const A_CONST
Definition: beinginfo.cpp:259
static void init()
Definition: beinginfo.cpp:223
const SoundInfo & getSound(const ItemSoundEvent::Type event) const
Definition: beinginfo.cpp:165
static Attack * empty
Definition: beinginfo.h:57
static void clear()
Definition: beinginfo.cpp:217
static BeingInfo * unknown
Definition: beinginfo.h:56
ItemSoundEvents mSounds
Definition: beinginfo.h:365
std::string getColor(const ItemColor idx) const
Definition: beinginfo.cpp:242
const Attack * getAttack(const int id) const
Definition: beinginfo.cpp:180
Attacks mAttacks
Definition: beinginfo.h:366
void setTargetCursorSize(const std::string &size)
Definition: beinginfo.cpp:132
int getIntValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
Configuration paths
#define delete2(var)
Definition: delete2.h:25
void delete_all(Container &c)
Definition: dtor.h:56
#define _(s)
Definition: gettext.h:35
uint16_t ItemColor
Definition: itemcolor.h:30
#define nullptr
Definition: localconsts.h:45
Logger * logger
Definition: logger.cpp:89
@ MONSTERWALL
Definition: blockmask.h:37
const std::map< ItemColor, ItemColorData > * getColorsList(const std::string &name)
Definition: colordb.cpp:219
Definition: cursor.h:27
@ CURSOR_POINTER
Definition: cursor.h:29
int size()
Definition: emotedb.cpp:306
static const std::string ATTACKWATER("attackwater")
static const std::string ATTACKSKY("attacksky")
static const std::string ATTACKRIDE("attackride")
static const std::string ATTACK("attack")
std::map< std::string, DyeColor > mColors
Definition: palettedb.cpp:37
std::vector< std::string > mSounds
Definition: sounddb.cpp:37
std::vector< SoundInfo > SoundInfoVect
Definition: soundinfo.h:49
Definition: attack.h:30
int mMissEffectId
Definition: attack.h:38
int mCriticalHitEffectId
Definition: attack.h:37
int mEffectId
Definition: attack.h:35
int mHitEffectId
Definition: attack.h:36
std::vector< SpriteReference * > sprites
Definition: spritedisplay.h:47
static SpriteReference * Empty