Mana
Loading...
Searching...
No Matches
beinginfo.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana 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 "resources/beinginfo.h"
23
24#include "log.h"
25#include "configuration.h"
26
27#include "utils/gettext.h"
28
29#include <optional>
30
31static BeingInfo *createUnknownBeingInfo()
32{
33 auto info = new BeingInfo;
34 info->name = _("unnamed");
35
36 SpriteReference errorSprite { paths.getStringValue("spriteErrorFile"), 0 };
37 info->display.sprites.push_back(std::move(errorSprite));
38
39 return info;
40}
41
42BeingInfo *BeingInfo::Unknown = createUnknownBeingInfo();
43
44static std::optional<ActorSprite::TargetCursorSize> targetCursorSizeFromString(const std::string &cursor)
45{
46 if (cursor == "small") return ActorSprite::TC_SMALL;
47 if (cursor == "medium") return ActorSprite::TC_MEDIUM;
48 if (cursor == "large") return ActorSprite::TC_LARGE;
49
50 return {};
51}
52
53static std::optional<Cursor> cursorFromString(const std::string &cursor)
54{
55 if (cursor == "pointer") return Cursor::Pointer;
56 if (cursor == "attack") return Cursor::Fight;
57 if (cursor == "pickup") return Cursor::PickUp;
58 if (cursor == "talk") return Cursor::Talk;
59 if (cursor == "action") return Cursor::Action;
60 if (cursor == "left") return Cursor::Left;
61 if (cursor == "up") return Cursor::Up;
62 if (cursor == "right") return Cursor::Right;
63 if (cursor == "down") return Cursor::Down;
64
65 return {};
66}
67
68BeingInfo::BeingInfo() = default;
69BeingInfo::~BeingInfo() = default;
70
71void BeingInfo::setTargetCursorSize(const std::string &size)
72{
73 const auto cursorSize = targetCursorSizeFromString(size);
74 if (!cursorSize)
75 {
76 Log::info("Unknown targetCursor value \"%s\" for %s",
77 size.c_str(), name.c_str());
78 }
79 targetCursorSize = cursorSize.value_or(ActorSprite::TC_MEDIUM);
80}
81
82void BeingInfo::setHoverCursor(const std::string &cursorName)
83{
84 const auto cursor = cursorFromString(cursorName);
85 if (!cursor)
86 {
87 Log::info("Unknown hoverCursor value \"%s\" for %s",
88 cursorName.c_str(), name.c_str());
89 }
90 hoverCursor = cursor.value_or(Cursor::Pointer);
91}
92
93void BeingInfo::addSound(SoundEvent event, const std::string &filename)
94{
95 mSounds[event].push_back("sfx/" + filename);
96}
97
98const std::string &BeingInfo::getSound(SoundEvent event) const
99{
100 static const std::string empty;
101
102 auto i = mSounds.find(event);
103 return i == mSounds.end() ? empty :
104 i->second.at(rand() % i->second.size());
105}
106
107const Attack &BeingInfo::getAttack(int id) const
108{
109 static const Attack empty {
110 SpriteAction::ATTACK,
111 -1, // Default strike effect on monster
112 paths.getIntValue("hitEffectId"),
113 paths.getIntValue("criticalHitEffectId"),
114 std::string()
115 };
116
117 auto it = mAttacks.find(id);
118 return it == mAttacks.end() ? empty : it->second;
119}
120
121void BeingInfo::addAttack(int id, Attack attack)
122{
123 mAttacks[id] = std::move(attack);
124}
SoundEvent
Definition beinginfo.h:44
Holds information about a certain type of monster.
Definition beinginfo.h:59
static BeingInfo * Unknown
Definition beinginfo.h:61
std::map< SoundEvent, std::vector< std::string > > mSounds
Definition beinginfo.h:84
std::map< int, Attack > mAttacks
Definition beinginfo.h:85
const Attack & getAttack(int id) const
ActorSprite::TargetCursorSize targetCursorSize
Definition beinginfo.h:68
void addSound(SoundEvent event, const std::string &filename)
Definition beinginfo.cpp:93
void setHoverCursor(const std::string &cursorName)
Definition beinginfo.cpp:82
void addAttack(int id, Attack attack)
const std::string & getSound(SoundEvent event) const
Definition beinginfo.cpp:98
Cursor hoverCursor
Definition beinginfo.h:69
std::string name
Definition beinginfo.h:66
void setTargetCursorSize(const std::string &size)
Definition beinginfo.cpp:71
std::string getStringValue(const std::string &key) const
int getIntValue(const std::string &key) const
returns a value corresponding to the given key.
Configuration paths
XML default paths information reader.
Definition client.cpp:99
#define _(s)
Definition gettext.h:38
void info(const char *log_text,...) LOG_PRINTF_ATTR