diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-03-02 12:55:32 +0000 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-03-02 12:55:32 +0000 |
commit | 5efaa5125fe92a5438b3cc2949f4d720bced5a7a (patch) | |
tree | 87f4da1382fb6179610182ca3e502e5365e66276 /src/resources | |
parent | 2e60491ceb0548b0bea93207c13b974d6a6cf5cc (diff) | |
download | mana-5efaa5125fe92a5438b3cc2949f4d720bced5a7a.tar.gz mana-5efaa5125fe92a5438b3cc2949f4d720bced5a7a.tar.bz2 mana-5efaa5125fe92a5438b3cc2949f4d720bced5a7a.tar.xz mana-5efaa5125fe92a5438b3cc2949f4d720bced5a7a.zip |
General code cleanups
* Don't needlessly store or return raw pointers in BeingInfo
* Less copying, more moving
* Less else after return
* Make AddDEF a template instead of a macro
* Removed some unused includes
* Use range-based for loops
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/action.cpp | 4 | ||||
-rw-r--r-- | src/resources/action.h | 4 | ||||
-rw-r--r-- | src/resources/animation.cpp | 9 | ||||
-rw-r--r-- | src/resources/animation.h | 4 | ||||
-rw-r--r-- | src/resources/beinginfo.cpp | 54 | ||||
-rw-r--r-- | src/resources/beinginfo.h | 41 | ||||
-rw-r--r-- | src/resources/itemdb.cpp | 40 | ||||
-rw-r--r-- | src/resources/itemdb.h | 2 | ||||
-rw-r--r-- | src/resources/monsterdb.cpp | 22 | ||||
-rw-r--r-- | src/resources/npcdb.cpp | 8 | ||||
-rw-r--r-- | src/resources/specialdb.cpp | 6 |
11 files changed, 71 insertions, 123 deletions
diff --git a/src/resources/action.cpp b/src/resources/action.cpp index c35498a4..42a8f748 100644 --- a/src/resources/action.cpp +++ b/src/resources/action.cpp @@ -25,9 +25,7 @@ #include "utils/dtor.h" -Action::Action() -{ -} +Action::Action() = default; Action::~Action() { diff --git a/src/resources/action.h b/src/resources/action.h index bf229abd..8a9a76f2 100644 --- a/src/resources/action.h +++ b/src/resources/action.h @@ -43,9 +43,7 @@ class Action Animation *getAnimation(int direction) const; protected: - using Animations = std::map<int, Animation *>; - using AnimationIterator = Animations::iterator; - Animations mAnimations; + std::map<int, Animation *> mAnimations; }; #endif diff --git a/src/resources/animation.cpp b/src/resources/animation.cpp index 8ab7cc44..1be27c2c 100644 --- a/src/resources/animation.cpp +++ b/src/resources/animation.cpp @@ -21,13 +21,6 @@ #include "resources/animation.h" -#include "utils/dtor.h" - -Animation::Animation(): - mDuration(0) -{ -} - void Animation::addFrame(Image *image, int delay, int offsetX, int offsetY) { Frame frame = { image, delay, offsetX, offsetY }; @@ -42,5 +35,5 @@ void Animation::addTerminator() bool Animation::isTerminator(const Frame &candidate) { - return (candidate.image == nullptr); + return candidate.image == nullptr; } diff --git a/src/resources/animation.h b/src/resources/animation.h index 6363e49f..59601dc4 100644 --- a/src/resources/animation.h +++ b/src/resources/animation.h @@ -46,7 +46,7 @@ struct Frame class Animation { public: - Animation(); + Animation() = default; /** * Appends a new animation at the end of the sequence. @@ -81,7 +81,7 @@ class Animation protected: std::vector<Frame> mFrames; - int mDuration; + int mDuration = 0; }; #endif diff --git a/src/resources/beinginfo.cpp b/src/resources/beinginfo.cpp index 539254ac..da65355a 100644 --- a/src/resources/beinginfo.cpp +++ b/src/resources/beinginfo.cpp @@ -24,17 +24,14 @@ #include "log.h" #include "configuration.h" -#include "utils/dtor.h" #include "utils/gettext.h" BeingInfo *BeingInfo::Unknown = new BeingInfo; BeingInfo::BeingInfo(): - mName(_("unnamed")), - mTargetCursorSize(ActorSprite::TC_MEDIUM), - mWalkMask(Map::BLOCKMASK_WALL | Map::BLOCKMASK_CHARACTER - | Map::BLOCKMASK_MONSTER), - mBlockType(Map::BLOCKTYPE_CHARACTER) + mName(_("unnamed")), + mWalkMask(Map::BLOCKMASK_WALL | Map::BLOCKMASK_CHARACTER + | Map::BLOCKMASK_MONSTER) { SpriteDisplay display; @@ -44,12 +41,7 @@ BeingInfo::BeingInfo(): setDisplay(display); } -BeingInfo::~BeingInfo() -{ - delete_all(mSounds); - delete_all(mAttacks); - mSounds.clear(); -} +BeingInfo::~BeingInfo() = default; void BeingInfo::setDisplay(SpriteDisplay display) { @@ -74,12 +66,7 @@ void BeingInfo::setTargetCursorSize(const std::string &size) void BeingInfo::addSound(SoundEvent event, const std::string &filename) { - if (mSounds.find(event) == mSounds.end()) - { - mSounds[event] = new std::vector<std::string>; - } - - mSounds[event]->push_back("sfx/" + filename); + mSounds[event].push_back("sfx/" + filename); } const std::string &BeingInfo::getSound(SoundEvent event) const @@ -87,30 +74,25 @@ const std::string &BeingInfo::getSound(SoundEvent event) const static const std::string empty; auto i = mSounds.find(event); - return (i == mSounds.end()) ? empty : - i->second->at(rand() % i->second->size()); + return i == mSounds.end() ? empty : + i->second.at(rand() % i->second.size()); } -const Attack *BeingInfo::getAttack(int id) const +const Attack &BeingInfo::getAttack(int id) const { - static auto *empty = new Attack(SpriteAction::ATTACK, - -1, // Default strike effect on monster - paths.getIntValue("hitEffectId"), - paths.getIntValue("criticalHitEffectId"), - std::string()); + static const Attack empty { + SpriteAction::ATTACK, + -1, // Default strike effect on monster + paths.getIntValue("hitEffectId"), + paths.getIntValue("criticalHitEffectId"), + std::string() + }; auto it = mAttacks.find(id); - return (it == mAttacks.end()) ? empty : it->second; + return it == mAttacks.end() ? empty : it->second; } -void BeingInfo::addAttack(int id, std::string action, int effectId, - int hitEffectId, int criticalHitEffectId, - const std::string &missileParticleFilename) +void BeingInfo::addAttack(int id, Attack attack) { - auto it = mAttacks.find(id); - if (it != mAttacks.end()) - delete it->second; - - mAttacks[id] = new Attack(action, effectId, hitEffectId, - criticalHitEffectId, missileParticleFilename); + mAttacks[id] = std::move(attack); } diff --git a/src/resources/beinginfo.h b/src/resources/beinginfo.h index 3fa5b8b6..e2d11c93 100644 --- a/src/resources/beinginfo.h +++ b/src/resources/beinginfo.h @@ -26,29 +26,19 @@ #include "resources/spritedef.h" -#include <list> #include <map> #include <string> #include <vector> -struct Attack { - std::string mAction; - int mEffectId, mHitEffectId, mCriticalHitEffectId; - std::string mMissileParticleFilename; - - Attack(std::string action, int effectId, int hitEffectId, - int criticalHitEffectId, std::string missileParticleFilename) - { - mAction = action; - mEffectId = effectId; - mHitEffectId = hitEffectId; - mCriticalHitEffectId = criticalHitEffectId; - mMissileParticleFilename = missileParticleFilename; - } +struct Attack +{ + std::string mAction = SpriteAction::ATTACK; + int mEffectId = 0; + int mHitEffectId = 0; + int mCriticalHitEffectId = 0; + std::string mMissileParticleFilename = std::string(); }; -using Attacks = std::map<int, Attack *>; - enum SoundEvent { SOUND_EVENT_HIT, @@ -57,8 +47,6 @@ enum SoundEvent SOUND_EVENT_DIE }; -using SoundEvents = std::map<SoundEvent, std::vector<std::string> *>; - /** * Holds information about a certain type of monster. This includes the name * of the monster, the sprite to display and the sounds the monster makes. @@ -97,11 +85,9 @@ class BeingInfo const std::string &getSound(SoundEvent event) const; - void addAttack(int id, std::string action, int effectId, - int hitEffectId, int criticalHitEffectId, - const std::string &missileParticleFilename); + void addAttack(int id, Attack attack); - const Attack *getAttack(int id) const; + const Attack &getAttack(int id) const; void setWalkMask(unsigned char mask) { mWalkMask = mask; } @@ -121,14 +107,13 @@ class BeingInfo private: SpriteDisplay mDisplay; std::string mName; - ActorSprite::TargetCursorSize mTargetCursorSize; - SoundEvents mSounds; - Attacks mAttacks; + ActorSprite::TargetCursorSize mTargetCursorSize = ActorSprite::TC_MEDIUM; + std::map<SoundEvent, std::vector<std::string>> mSounds; + std::map<int, Attack> mAttacks; unsigned char mWalkMask; - Map::BlockType mBlockType; + Map::BlockType mBlockType = Map::BLOCKTYPE_CHARACTER; }; using BeingInfos = std::map<int, BeingInfo *>; -using BeingInfoIterator = BeingInfos::iterator; #endif // BEINGINFO_H diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index cd027763..b910c639 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -42,23 +42,23 @@ void setStatsList(const std::list<ItemStat> &stats) static ItemType itemTypeFromString(const std::string &name, int id = 0) { - if (name=="generic") return ITEM_UNUSABLE; - else if (name=="usable") return ITEM_USABLE; - else if (name=="equip-1hand") return ITEM_EQUIPMENT_ONE_HAND_WEAPON; - else if (name=="equip-2hand") return ITEM_EQUIPMENT_TWO_HANDS_WEAPON; - else if (name=="equip-torso") return ITEM_EQUIPMENT_TORSO; - else if (name=="equip-arms") return ITEM_EQUIPMENT_ARMS; - else if (name=="equip-head") return ITEM_EQUIPMENT_HEAD; - else if (name=="equip-legs") return ITEM_EQUIPMENT_LEGS; - else if (name=="equip-shield") return ITEM_EQUIPMENT_SHIELD; - else if (name=="equip-ring") return ITEM_EQUIPMENT_RING; - else if (name=="equip-charm") return ITEM_EQUIPMENT_CHARM; - else if (name=="equip-necklace") return ITEM_EQUIPMENT_NECKLACE; - else if (name=="equip-feet") return ITEM_EQUIPMENT_FEET; - else if (name=="equip-ammo") return ITEM_EQUIPMENT_AMMO; - else if (name=="racesprite") return ITEM_SPRITE_RACE; - else if (name=="hairsprite") return ITEM_SPRITE_HAIR; - else return ITEM_UNUSABLE; + if (name == "generic") return ITEM_UNUSABLE; + if (name == "usable") return ITEM_USABLE; + if (name == "equip-1hand") return ITEM_EQUIPMENT_ONE_HAND_WEAPON; + if (name == "equip-2hand") return ITEM_EQUIPMENT_TWO_HANDS_WEAPON; + if (name == "equip-torso") return ITEM_EQUIPMENT_TORSO; + if (name == "equip-arms") return ITEM_EQUIPMENT_ARMS; + if (name == "equip-head") return ITEM_EQUIPMENT_HEAD; + if (name == "equip-legs") return ITEM_EQUIPMENT_LEGS; + if (name == "equip-shield") return ITEM_EQUIPMENT_SHIELD; + if (name == "equip-ring") return ITEM_EQUIPMENT_RING; + if (name == "equip-charm") return ITEM_EQUIPMENT_CHARM; + if (name == "equip-necklace") return ITEM_EQUIPMENT_NECKLACE; + if (name == "equip-feet") return ITEM_EQUIPMENT_FEET; + if (name == "equip-ammo") return ITEM_EQUIPMENT_AMMO; + if (name == "racesprite") return ITEM_SPRITE_RACE; + if (name == "hairsprite") return ITEM_SPRITE_HAIR; + return ITEM_UNUSABLE; } void ItemDB::loadEmptyItemDefinition() @@ -76,13 +76,11 @@ void ItemDB::loadEmptyItemDefinition() * Common itemDB functions */ -bool ItemDB::exists(int id) +bool ItemDB::exists(int id) const { assert(mLoaded); - ItemInfos::const_iterator i = mItemInfos.find(id); - - return i != mItemInfos.end(); + return mItemInfos.find(id) != mItemInfos.end(); } const ItemInfo &ItemDB::get(int id) diff --git a/src/resources/itemdb.h b/src/resources/itemdb.h index 412e64ce..6a9988d0 100644 --- a/src/resources/itemdb.h +++ b/src/resources/itemdb.h @@ -89,7 +89,7 @@ class ItemDB bool isLoaded() const { return mLoaded; } - bool exists(int id); + bool exists(int id) const; const ItemInfo &get(int id); const ItemInfo &get(const std::string &name); diff --git a/src/resources/monsterdb.cpp b/src/resources/monsterdb.cpp index 5f62f418..fa1ac5af 100644 --- a/src/resources/monsterdb.cpp +++ b/src/resources/monsterdb.cpp @@ -124,36 +124,32 @@ void MonsterDB::readMonsterNode(xmlNodePtr node, const std::string &filename) } else if (xmlStrEqual(spriteNode->name, BAD_CAST "attack")) { + Attack attack; const int id = XML::getProperty(spriteNode, "id", 0); - int effectId = XML::getProperty(spriteNode, "effect-id", -1); - int hitEffectId = + + attack.mEffectId = XML::getProperty(spriteNode, "effect-id", -1); + attack.mHitEffectId = XML::getProperty(spriteNode, "hit-effect-id", paths.getIntValue("hitEffectId")); - int criticalHitEffectId = + attack.mCriticalHitEffectId = XML::getProperty(spriteNode, "critical-hit-effect-id", paths.getIntValue("criticalHitEffectId")); - const std::string missileParticleFilename = + attack.mMissileParticleFilename = XML::getProperty(spriteNode, "missile-particle", ""); - const std::string spriteAction = XML::getProperty(spriteNode, - "action", - "attack"); + attack.mAction = XML::getProperty(spriteNode, "action", "attack"); - currentInfo->addAttack(id, spriteAction, effectId, - hitEffectId, criticalHitEffectId, - missileParticleFilename); + currentInfo->addAttack(id, std::move(attack)); } else if (xmlStrEqual(spriteNode->name, BAD_CAST "particlefx")) { - display.particles.push_back( + display.particles.emplace_back( (const char*) spriteNode->xmlChildrenNode->content); } } currentInfo->setDisplay(display); mMonsterInfos[XML::getProperty(node, "id", 0) + mMonsterIdOffset] = currentInfo; - - } /** diff --git a/src/resources/npcdb.cpp b/src/resources/npcdb.cpp index fa12eced..6a0685d2 100644 --- a/src/resources/npcdb.cpp +++ b/src/resources/npcdb.cpp @@ -27,7 +27,6 @@ #include "utils/dtor.h" #include "utils/xml.h" -#include "configuration.h" namespace { @@ -40,7 +39,6 @@ void NPCDB::init() { if (mLoaded) unload(); - } void NPCDB::readNPCNode(xmlNodePtr node, const std::string &filename) @@ -101,8 +99,6 @@ BeingInfo *NPCDB::get(int id) logger->log("NPCDB: Warning, unknown NPC ID %d requested", id); return BeingInfo::Unknown; } - else - { - return i->second; - } + + return i->second; } diff --git a/src/resources/specialdb.cpp b/src/resources/specialdb.cpp index b72c059f..89bd4d8d 100644 --- a/src/resources/specialdb.cpp +++ b/src/resources/specialdb.cpp @@ -33,8 +33,10 @@ namespace SpecialInfo::TargetMode SpecialDB::targetModeFromString(const std::string& str) { - if (str=="being") return SpecialInfo::TARGET_BEING; - else if (str=="point") return SpecialInfo::TARGET_POINT; + if (str == "being") + return SpecialInfo::TARGET_BEING; + if (str == "point") + return SpecialInfo::TARGET_POINT; logger->log("SpecialDB: Warning, unknown target mode \"%s\"", str.c_str() ); return SpecialInfo::TARGET_BEING; |