GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/beinginfo.cpp Lines: 8 74 10.8 %
Date: 2017-11-29 Branches: 7 72 9.7 %

Line Branch Exec Source
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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "resources/beinginfo.h"
24
25
#include "configuration.h"
26
#include "logger.h"
27
28
#include "const/resources/spriteaction.h"
29
30
#include "enums/resources/map/blockmask.h"
31
32
#include "resources/attack.h"
33
34
#include "resources/sprite/spritereference.h"
35
36
#include "resources/db/colordb.h"
37
38
#include "utils/cast.h"
39
#include "utils/delete2.h"
40
#include "utils/dtor.h"
41
#include "utils/gettext.h"
42
43
#include "debug.h"
44
45
BeingInfo *BeingInfo::unknown = nullptr;
46
2
Attack *BeingInfo::empty = new Attack(SpriteAction::ATTACK,
47
    SpriteAction::ATTACKSKY,
48
    SpriteAction::ATTACKWATER,
49
    SpriteAction::ATTACKRIDE,
50
    -1,
51
    -1,
52
    -1,
53
    -1,
54
4
    std::string(),
55
    32.0F,
56
    7.0F,
57
    8.0F,
58

2
    500);
59
60
BeingInfo::BeingInfo() :
61
    mDisplay(),
62
    // TRANSLATORS: being info default name
63
    mName(_("unnamed")),
64
    mTargetCursorSize(TargetCursorSize::MEDIUM),
65
    mHoverCursor(Cursor::CURSOR_POINTER),
66
    mSounds(),
67
    mAttacks(),
68
    mMenu(),
69
    mStrings(),
70
    mCurrency(),
71
    mBlockWalkMask(BlockMask::WALL |
72
                   BlockMask::AIR |
73
                   BlockMask::WATER |
74
                   BlockMask::MONSTERWALL),
75
    mBlockType(BlockType::NONE),
76
    mColors(nullptr),
77
    mTargetOffsetX(0),
78
    mTargetOffsetY(0),
79
    mNameOffsetX(0),
80
    mNameOffsetY(0),
81
    mHpBarOffsetX(0),
82
    mHpBarOffsetY(0),
83
    mMaxHP(0),
84
    mSortOffsetY(0),
85
    mDeadSortOffsetY(31),
86
    mAvatarId(BeingTypeId_zero),
87
    mWidth(0),
88
    mHeight(0),
89
    mStartFollowDist(3),
90
    mFollowDist(1),
91
    mWarpDist(11),
92
    mWalkSpeed(0),
93
    mSitOffsetX(0),
94
    mSitOffsetY(0),
95
    mMoveOffsetX(0),
96
    mMoveOffsetY(0),
97
    mDeadOffsetX(0),
98
    mDeadOffsetY(0),
99
    mAttackOffsetX(0),
100
    mAttackOffsetY(0),
101
    mThinkTime(50),
102
    mDirectionType(1),
103
    mSitDirectionType(1),
104
    mDeadDirectionType(1),
105
    mAttackDirectionType(1),
106
    mQuickActionEffectId(-1),
107
    mStaticMaxHP(false),
108
    mTargetSelection(true),
109
    mAllowDelete(true),
110
    mAllowEquipment(false)
111
{
112
    SpriteDisplay display;
113
    display.sprites.push_back(SpriteReference::Empty);
114
115
    setDisplay(display);
116
}
117
118
BeingInfo::~BeingInfo()
119
{
120
    delete_all(mSounds);
121
    delete_all(mAttacks);
122
    mSounds.clear();
123
    delete_all(mDisplay.sprites);
124
}
125
126
void BeingInfo::setDisplay(const SpriteDisplay &display)
127
{
128
    mDisplay = display;
129
}
130
131
void BeingInfo::setTargetCursorSize(const std::string &size)
132
{
133
    if (size == "small")
134
    {
135
        setTargetCursorSize(TargetCursorSize::SMALL);
136
    }
137
    else if (size == "medium")
138
    {
139
        setTargetCursorSize(TargetCursorSize::MEDIUM);
140
    }
141
    else if (size == "large")
142
    {
143
        setTargetCursorSize(TargetCursorSize::LARGE);
144
    }
145
    else
146
    {
147
        logger->log("Unknown target cursor type \"%s\" for %s - using medium "
148
                    "sized one", size.c_str(), getName().c_str());
149
        setTargetCursorSize(TargetCursorSize::MEDIUM);
150
    }
151
}
152
153
void BeingInfo::addSound(const ItemSoundEvent::Type event,
154
                         const std::string &filename,
155
                         const int delay)
156
{
157
    if (mSounds.find(event) == mSounds.end())
158
        mSounds[event] = new SoundInfoVect;
159
160
    if (mSounds[event] != nullptr)
161
        mSounds[event]->push_back(SoundInfo(filename, delay));
162
}
163
164
const SoundInfo &BeingInfo::getSound(const ItemSoundEvent::Type event) const
165
{
166
    static SoundInfo emptySound("", 0);
167
168
    const ItemSoundEvents::const_iterator i = mSounds.find(event);
169
170
    if (i == mSounds.end())
171
        return emptySound;
172
173
    const SoundInfoVect *const vect = i->second;
174
    if (vect == nullptr || vect->empty())
175
        return emptySound;
176
    return vect->at(CAST_SIZE(rand()) % vect->size());
177
}
178
179
const Attack *BeingInfo::getAttack(const int id) const
180
{
181
    const Attacks::const_iterator i = mAttacks.find(id);
182
    return (i == mAttacks.end()) ? empty : (*i).second;
183
}
184
185
void BeingInfo::addAttack(const int id,
186
                          const std::string &action,
187
                          const std::string &skyAction,
188
                          const std::string &waterAction,
189
                          const std::string &rideAction,
190
                          const int effectId,
191
                          const int hitEffectId,
192
                          const int criticalHitEffectId,
193
                          const int missEffectId,
194
                          const std::string &missileParticle,
195
                          const float missileZ,
196
                          const float missileSpeed,
197
                          const float missileDieDistance,
198
                          const int missileLifeTime)
199
{
200
    delete mAttacks[id];
201
    mAttacks[id] = new Attack(action,
202
        skyAction,
203
        waterAction,
204
        rideAction,
205
        effectId,
206
        hitEffectId,
207
        criticalHitEffectId,
208
        missEffectId,
209
        missileParticle,
210
        missileZ,
211
        missileSpeed,
212
        missileDieDistance,
213
        missileLifeTime);
214
}
215
216
384
void BeingInfo::clear()
217
{
218
384
    delete2(unknown);
219
384
    delete2(empty);
220
384
}
221
222
void BeingInfo::init()
223
{
224
    if (empty != nullptr)
225
    {
226
        empty->mEffectId = paths.getIntValue("effectId");
227
        empty->mHitEffectId = paths.getIntValue("hitEffectId");
228
        empty->mCriticalHitEffectId = paths.getIntValue("criticalHitEffectId");
229
        empty->mMissEffectId = paths.getIntValue("missEffectId");
230
    }
231
}
232
233
void BeingInfo::setColorsList(const std::string &name)
234
{
235
    if (name.empty())
236
        mColors = nullptr;
237
    else
238
        mColors = ColorDB::getColorsList(name);
239
}
240
241
std::string BeingInfo::getColor(const ItemColor idx) const
242
{
243
    if (mColors == nullptr)
244
        return std::string();
245
246
    const std::map <ItemColor, ItemColorData>::const_iterator
247
        it = mColors->find(idx);
248
    if (it == mColors->end())
249
        return std::string();
250
    return it->second.color;
251
}
252
253
void BeingInfo::addMenu(const std::string &name, const std::string &command)
254
{
255
    mMenu.push_back(BeingMenuItem(name, command));
256
}
257
258
const STD_VECTOR<BeingMenuItem> &BeingInfo::getMenu() const
259
{
260
    return mMenu;
261
}
262
263
std::string BeingInfo::getString(const int idx) const
264
{
265
    const std::map<int, std::string>::const_iterator it = mStrings.find(idx);
266
    if (it == mStrings.end())
267
        return "";
268
    return (*it).second;
269

6
}