Tidy up the source code

* Replaced push_back calls with emplace_back where applicable.
* Replaced size() == 0 and size() != 0 with empty() and !empty().
* Replaced C style loops for range for loops where applicable.
* Fixed mismatching arg names between function declarations and definitions.
* Replaced NULL and 0 (in the context of pointers) with nullptr.
* Remove unnecessary calls to string::c_str() where applicable.
* Replaced deprecated C headers with proper C++ headers.
* Removed unnecessary null pointer checks when deleting pointers
(deleting a null pointer has no effect).
* Fixed a potential memory leak in apngloader.cpp file.
* Replaced unsafe strcpy with strncpy in the demangle_name function.
This commit is contained in:
Kamil Chojnowski
2019-10-10 00:21:26 +02:00
parent caae18dbce
commit 869de6886f
59 changed files with 202 additions and 199 deletions

View File

@@ -50,7 +50,7 @@ void Animator::unserialize(int animationPhases, const FileStreamPtr& fin)
for(int i = 0; i < m_animationPhases; ++i) {
int minimum = fin->getU32();
int maximum = fin->getU32();
m_phaseDurations.push_back(std::make_tuple(minimum, maximum));
m_phaseDurations.emplace_back(minimum, maximum);
}
m_phase = getStartPhase();

View File

@@ -133,7 +133,7 @@ public:
const std::vector<CreatureTypePtr>& getCreatures() { return m_creatures; }
protected:
void internalLoadCreatureBuffer(TiXmlElement* elem, const CreatureTypePtr& m);
void internalLoadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr& m);
private:
std::vector<CreatureTypePtr> m_creatures;

View File

@@ -686,7 +686,7 @@ void Game::autoWalk(std::vector<Otc::Direction> dirs)
return;
}
if(dirs.size() == 0)
if(dirs.empty())
return;
// must cancel follow before any new walk
@@ -1715,10 +1715,10 @@ std::string Game::formatCreatureName(const std::string& name)
std::string formatedName = name;
if(getFeature(Otc::GameFormatCreatureName) && name.length() > 0) {
bool upnext = true;
for(uint i=0;i<formatedName.length();++i) {
char ch = formatedName[i];
for(char &i: formatedName) {
char ch = i;
if(upnext) {
formatedName[i] = stdext::upchar(ch);
i = stdext::upchar(ch);
upnext = false;
}
if(ch == ' ')

View File

@@ -70,7 +70,7 @@ private:
void resetGameStates();
protected:
void processConnectionError(const boost::system::error_code& error);
void processConnectionError(const boost::system::error_code& ec);
void processDisconnect();
void processPing();
void processPingBack();
@@ -178,7 +178,7 @@ public:
void moveToParentContainer(const ThingPtr& thing, int count);
void rotate(const ThingPtr& thing);
void use(const ThingPtr& thing);
void useWith(const ItemPtr& fromThing, const ThingPtr& toThing);
void useWith(const ItemPtr& item, const ThingPtr& toThing);
void useInventoryItem(int itemId);
void useInventoryItemWith(int itemId, const ThingPtr& toThing);
ItemPtr findItemInContainers(uint itemId, int subType);
@@ -241,7 +241,7 @@ public:
// pvp related
void setUnjustifiedPoints(UnjustifiedPoints unjustifiedPoints);
UnjustifiedPoints getUnjustifiedPoints() { return m_unjustifiedPoints; };
void setOpenPvpSituations(int openPvpSitations);
void setOpenPvpSituations(int openPvpSituations);
int getOpenPvpSituations() { return m_openPvpSituations; }
// npc trade related
@@ -345,7 +345,7 @@ public:
std::string getCharacterName() { return m_characterName; }
std::string getWorldName() { return m_worldName; }
std::vector<uint8> getGMActions() { return m_gmActions; }
bool isGM() { return m_gmActions.size() > 0; }
bool isGM() { return !m_gmActions.empty(); }
Otc::Direction getLastWalkDir() { return m_lastWalkDir; }
std::string formatCreatureName(const std::string &name);

View File

@@ -88,7 +88,7 @@ void LightView::addLightSource(const Point& center, float scaleFactor, const Lig
color.setGreen(color.gF() * brightness);
color.setBlue(color.bF() * brightness);
if(m_blendEquation == Painter::BlendEquation_Add && m_lightMap.size() > 0) {
if(m_blendEquation == Painter::BlendEquation_Add && !m_lightMap.empty()) {
LightSource prevSource = m_lightMap.back();
if(prevSource.center == center && prevSource.color == color && prevSource.radius == radius)
return;

View File

@@ -192,7 +192,7 @@ public:
// tile zone related
void setShowZone(tileflags_t zone, bool show);
void setShowZones(bool show);
void setZoneColor(tileflags_t flag, const Color& color);
void setZoneColor(tileflags_t zone, const Color& color);
void setZoneOpacity(float opacity) { m_zoneOpacity = opacity; }
float getZoneOpacity() { return m_zoneOpacity; }

View File

@@ -378,11 +378,11 @@ void MapView::updateVisibleTilesCache(int start)
Rect(tpx, tpy + 1, 1, qs),
};
for(int i=0;i<4;++i) {
int sx = std::max<int>(lines[i].left(), area.left());
int ex = std::min<int>(lines[i].right(), area.right());
int sy = std::max<int>(lines[i].top(), area.top());
int ey = std::min<int>(lines[i].bottom(), area.bottom());
for(auto &line: lines) {
int sx = std::max<int>(line.left(), area.left());
int ex = std::min<int>(line.right(), area.right());
int sy = std::max<int>(line.top(), area.top());
int ey = std::min<int>(line.bottom(), area.bottom());
for(int qx=sx;qx<=ex;++qx)
for(int qy=sy;qy<=ey;++qy)
m_spiral[count++] = Point(qx, qy);

View File

@@ -55,12 +55,12 @@ public:
void sendTurnSouth();
void sendTurnWest();
void sendEquipItem(int itemId, int countOrSubType);
void sendMove(const Position& fromPos, int itemId, int stackpos, const Position& toPos, int count);
void sendMove(const Position& fromPos, int thingId, int stackpos, const Position& toPos, int count);
void sendInspectNpcTrade(int itemId, int count);
void sendBuyItem(int itemId, int subType, int amount, bool ignoreCapacity, bool buyWithBackpack);
void sendSellItem(int itemId, int subType, int amount, bool ignoreEquipped);
void sendCloseNpcTrade();
void sendRequestTrade(const Position& pos, int thingId, int stackpos, uint playerId);
void sendRequestTrade(const Position& pos, int thingId, int stackpos, uint creatureId);
void sendInspectTrade(bool counterOffer, int index);
void sendAcceptTrade();
void sendRejectTrade();
@@ -118,7 +118,7 @@ public:
void sendRequestStoreOffers(const std::string& categoryName, int serviceType);
void sendOpenStore(int serviceType, const std::string &category);
void sendTransferCoins(const std::string& recipient, int amount);
void sendOpenTransactionHistory(int entiresPerPage);
void sendOpenTransactionHistory(int entriesPerPage);
// otclient only
void sendChangeMapAwareRange(int xrange, int yrange);

View File

@@ -1000,7 +1000,7 @@ void ProtocolGame::parseOpenNpcTrade(const InputMessagePtr& msg)
int weight = msg->getU32();
int buyPrice = msg->getU32();
int sellPrice = msg->getU32();
items.push_back(std::make_tuple(item, name, weight, buyPrice, sellPrice));
items.emplace_back(item, name, weight, buyPrice, sellPrice);
}
g_game.processOpenNpcTrade(items);
@@ -1026,7 +1026,7 @@ void ProtocolGame::parsePlayerGoods(const InputMessagePtr& msg)
else
amount = msg->getU8();
goods.push_back(std::make_tuple(Item::create(itemId), amount));
goods.emplace_back(Item::create(itemId), amount);
}
g_game.processPlayerGoods(money, goods);
@@ -1571,7 +1571,7 @@ void ProtocolGame::parseChannelList(const InputMessagePtr& msg)
for(int i = 0; i < count; i++) {
int id = msg->getU16();
std::string name = msg->getString();
channelList.push_back(std::make_tuple(id, name));
channelList.emplace_back(id, name);
}
g_game.processChannelList(channelList);
@@ -1785,7 +1785,7 @@ void ProtocolGame::parseOpenOutfitWindow(const InputMessagePtr& msg)
std::string outfitName = msg->getString();
int outfitAddons = msg->getU8();
outfitList.push_back(std::make_tuple(outfitId, outfitName, outfitAddons));
outfitList.emplace_back(outfitId, outfitName, outfitAddons);
}
} else {
int outfitStart, outfitEnd;
@@ -1798,7 +1798,7 @@ void ProtocolGame::parseOpenOutfitWindow(const InputMessagePtr& msg)
}
for(int i = outfitStart; i <= outfitEnd; i++)
outfitList.push_back(std::make_tuple(i, "", 0));
outfitList.emplace_back(i, "", 0);
}
std::vector<std::tuple<int, std::string> > mountList;
@@ -1808,7 +1808,7 @@ void ProtocolGame::parseOpenOutfitWindow(const InputMessagePtr& msg)
int mountId = msg->getU16(); // mount type
std::string mountName = msg->getString(); // mount name
mountList.push_back(std::make_tuple(mountId, mountName));
mountList.emplace_back(mountId, mountName);
}
}
@@ -1881,7 +1881,7 @@ void ProtocolGame::parseQuestLog(const InputMessagePtr& msg)
int id = msg->getU16();
std::string name = msg->getString();
bool completed = msg->getU8();
questList.push_back(std::make_tuple(id, name, completed));
questList.emplace_back(id, name, completed);
}
g_game.processQuestLog(questList);
@@ -1895,7 +1895,7 @@ void ProtocolGame::parseQuestLine(const InputMessagePtr& msg)
for(int i = 0; i < missionCount; i++) {
std::string missionName = msg->getString();
std::string missionDescrition = msg->getString();
questMissions.push_back(std::make_tuple(missionName, missionDescrition));
questMissions.emplace_back(missionName, missionDescrition);
}
g_game.processQuestLine(questId, questMissions);
@@ -1920,7 +1920,7 @@ void ProtocolGame::parseItemInfo(const InputMessagePtr& msg)
item->setCountOrSubType(msg->getU8());
std::string desc = msg->getString();
list.push_back(std::make_tuple(item, desc));
list.emplace_back(item, desc);
}
g_lua.callGlobalField("g_game", "onItemInfo", list);
@@ -1947,7 +1947,7 @@ void ProtocolGame::parseModalDialog(const InputMessagePtr& msg)
for(int i = 0; i < sizeButtons; ++i) {
std::string value = msg->getString();
int id = msg->getU8();
buttonList.push_back(std::make_tuple(id, value));
buttonList.emplace_back(id, value);
}
int sizeChoices = msg->getU8();
@@ -1955,7 +1955,7 @@ void ProtocolGame::parseModalDialog(const InputMessagePtr& msg)
for(int i = 0; i < sizeChoices; ++i) {
std::string value = msg->getString();
int id = msg->getU8();
choiceList.push_back(std::make_tuple(id, value));
choiceList.emplace_back(id, value);
}
int enterButton, escapeButton;

View File

@@ -63,7 +63,7 @@ bool StaticText::addMessage(const std::string& name, Otc::MessageMode mode, cons
{
//TODO: this could be moved to lua
// first message
if(m_messages.size() == 0) {
if(m_messages.empty()) {
m_name = name;
m_mode = mode;
}
@@ -82,7 +82,7 @@ bool StaticText::addMessage(const std::string& name, Otc::MessageMode mode, cons
if(isYell())
delay *= 2;
m_messages.push_back(std::make_pair(text, g_clock.millis() + delay));
m_messages.emplace_back(text, g_clock.millis() + delay);
compose();
if(!m_updateEvent)

View File

@@ -123,11 +123,11 @@ void ThingType::serialize(const FileStreamPtr& fin)
}
}
for(uint i = 0; i < m_spritesIndex.size(); i++) {
for(int i: m_spritesIndex) {
if(g_game.getFeature(Otc::GameSpritesU32))
fin->addU32(m_spritesIndex[i]);
fin->addU32(i);
else
fin->addU16(m_spritesIndex[i]);
fin->addU16(i);
}
}
@@ -328,7 +328,7 @@ void ThingType::exportImage(std::string fileName)
if(m_null)
stdext::throw_exception("cannot export null thingtype");
if(m_spritesIndex.size() == 0)
if(m_spritesIndex.empty())
stdext::throw_exception("cannot export thingtype without sprites");
ImagePtr image(new Image(Size(32 * m_size.width() * m_layers * m_numPatternX, 32 * m_size.height() * m_animationPhases * m_numPatternY * m_numPatternZ)));
@@ -571,4 +571,4 @@ void ThingType::setPathable(bool var)
m_attribs.remove(ThingAttrNotPathable);
else
m_attribs.set(ThingAttrNotPathable, true);
}
}

View File

@@ -48,15 +48,15 @@ void ThingTypeManager::init()
m_datLoaded = false;
m_xmlLoaded = false;
m_otbLoaded = false;
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].resize(1, m_nullThingType);
for(auto &m_thingType: m_thingTypes)
m_thingType.resize(1, m_nullThingType);
m_itemTypes.resize(1, m_nullItemType);
}
void ThingTypeManager::terminate()
{
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].clear();
for(auto &m_thingType: m_thingTypes)
m_thingType.clear();
m_itemTypes.clear();
m_reverseItemTypes.clear();
m_nullThingType = nullptr;
@@ -77,8 +77,8 @@ void ThingTypeManager::saveDat(std::string fileName)
fin->addU32(m_datSignature);
for(int category = 0; category < ThingLastCategory; ++category)
fin->addU16(m_thingTypes[category].size() - 1);
for(auto &m_thingType: m_thingTypes)
fin->addU16(m_thingType.size() - 1);
for(int category = 0; category < ThingLastCategory; ++category) {
uint16 firstId = 1;
@@ -110,10 +110,10 @@ bool ThingTypeManager::loadDat(std::string file)
m_datSignature = fin->getU32();
m_contentRevision = static_cast<uint16_t>(m_datSignature);
for(int category = 0; category < ThingLastCategory; ++category) {
for(auto &m_thingType: m_thingTypes) {
int count = fin->getU16() + 1;
m_thingTypes[category].clear();
m_thingTypes[category].resize(count, m_nullThingType);
m_thingType.clear();
m_thingType.resize(count, m_nullThingType);
}
for(int category = 0; category < ThingLastCategory; ++category) {

View File

@@ -47,7 +47,7 @@ public:
const ItemTypePtr& findItemTypeByClientId(uint16 id);
const ItemTypePtr& findItemTypeByName(std::string name);
ItemTypeList findItemTypesByName(std::string name);
ItemTypeList findItemTypesByString(std::string str);
ItemTypeList findItemTypesByString(std::string name);
const ThingTypePtr& getNullThingType() { return m_nullThingType; }
const ItemTypePtr& getNullItemType() { return m_nullItemType; }

View File

@@ -63,8 +63,7 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags, LightView *
bool restore = false;
if(g_map.showZones() && thing->isGround()) {
for(unsigned int i = 0; i < sizeof(flags) / sizeof(tileflags_t); ++i) {
tileflags_t flag = flags[i];
for(auto flag: flags) {
if(hasFlag(flag) && g_map.showZone(flag)) {
g_painter->setOpacity(g_map.getZoneOpacity());
g_painter->setColor(g_map.getZoneColor(flag));
@@ -386,8 +385,7 @@ ThingPtr Tile::getTopLookThing()
if(isEmpty())
return nullptr;
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if(!thing->isIgnoreLook() && (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()))
return thing;
}
@@ -400,14 +398,12 @@ ThingPtr Tile::getTopUseThing()
if(isEmpty())
return nullptr;
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if (thing->isForceUse() || (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature() && !thing->isSplash()))
return thing;
}
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if (!thing->isGround() && !thing->isGroundBorder() && !thing->isCreature() && !thing->isSplash())
return thing;
}
@@ -418,8 +414,7 @@ ThingPtr Tile::getTopUseThing()
CreaturePtr Tile::getTopCreature()
{
CreaturePtr creature;
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if(thing->isLocalPlayer()) // return local player if there is no other creature
creature = thing->static_self_cast<Creature>();
else if(thing->isCreature() && !thing->isLocalPlayer())
@@ -480,8 +475,7 @@ ThingPtr Tile::getTopMultiUseThing()
if(CreaturePtr topCreature = getTopCreature())
return topCreature;
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if(thing->isForceUse())
return thing;
}
@@ -495,8 +489,7 @@ ThingPtr Tile::getTopMultiUseThing()
}
}
for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i];
for(auto thing: m_things) {
if(!thing->isGround() && !thing->isOnTop())
return thing;
}