From d7980dc65a6a0d917d2ad4c7643c7d9b45ed44c9 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 23 Mar 2024 00:15:08 +0100 Subject: [PATCH] Use some range algorithm. --- src/ai/ai_force.cpp | 10 ++-------- src/game/game.cpp | 9 ++++----- src/include/net_message.h | 2 +- src/include/util.h | 6 ++++++ src/network/net_lowlevel.cpp | 17 ++++------------- src/network/network.cpp | 25 ++++++++++--------------- src/network/online_service.cpp | 9 +-------- src/pathfinder/pathfinder.cpp | 1 - src/sound/sound_id.cpp | 4 +--- src/unit/script_unittype.cpp | 32 ++++++-------------------------- src/video/video.cpp | 13 +++---------- 11 files changed, 38 insertions(+), 90 deletions(-) diff --git a/src/ai/ai_force.cpp b/src/ai/ai_force.cpp index c7a9d3da71..1b37dc10ce 100644 --- a/src/ai/ai_force.cpp +++ b/src/ai/ai_force.cpp @@ -475,14 +475,8 @@ void AiForce::Attack(const Vec2i &pos) this->State = AiForceAttackingState::Attacking; } // Send all units in the force to enemy. - - CUnit *leader = nullptr; - for (CUnit *unit : this->Units) { - if (unit->IsAgressive()) { - leader = unit; - break; - } - } + const auto leaderIt = ranges::find_if(this->Units, [](const CUnit *unit) { return unit->IsAgressive(); }); + CUnit *leader = leaderIt != this->Units.end() ? *leaderIt : nullptr; for (size_t i = 0; i != this->Units.size(); ++i) { CUnit *const unit = this->Units[i]; diff --git a/src/game/game.cpp b/src/game/game.cpp index b2e671a408..16ba491021 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -838,11 +838,10 @@ void CreateGame(const fs::path &filename, CMap *map) if (!ThisPlayer) { if (!IsNetworkGame()) { // In demo or kiosk mode, pick first empty slot - for (int i = 0; i < PlayerMax; ++i) { - if (Players[i].Type == PlayerTypes::PlayerNobody) { - ThisPlayer = &Players[i]; - break; - } + if (auto it = ranges::find_if( + Players, [](const CPlayer &p) { return p.Type == PlayerTypes::PlayerNobody; }); + it != std::end(Players)) { + ThisPlayer = &*it; } } else { // this is bad - we are starting a network game, but ThisPlayer is not assigned!! diff --git a/src/include/net_message.h b/src/include/net_message.h index c580fe8550..d51482c5bb 100644 --- a/src/include/net_message.h +++ b/src/include/net_message.h @@ -62,7 +62,7 @@ class CNetworkHost void SetName(const char *name); - bool IsValid() { return (PlyNr != 0) || (PlyName[0] != '\0'); } + bool IsValid() const { return (PlyNr != 0) || (PlyName[0] != '\0'); } uint32_t Host; /// Host address uint16_t Port; /// Port on host diff --git a/src/include/util.h b/src/include/util.h index 0f990e3bb5..2e8d28fe7e 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -264,6 +264,12 @@ namespace ranges v.erase(std::remove_if(std::begin(v), std::end(v), pred), std::end(v)); } + template + auto max_element(Range &range) + { + return std::max_element(std::begin(range), std::end(range)); + } + template auto min_element(Range &range) { diff --git a/src/network/net_lowlevel.cpp b/src/network/net_lowlevel.cpp index cbf900857e..750938382a 100644 --- a/src/network/net_lowlevel.cpp +++ b/src/network/net_lowlevel.cpp @@ -642,21 +642,12 @@ void SocketSet::AddSocket(Socket socket) */ void SocketSet::DelSocket(Socket socket) { - std::vector::iterator i; - std::vector::iterator j; - - for (i = Sockets.begin(), j = SocketReady.begin(); i != Sockets.end(); ++i, ++j) { - if (*i == socket) { - Sockets.erase(i); - SocketReady.erase(j); - break; - } + if (const auto it = ranges::find(Sockets, socket); it != Sockets.end()) { + SocketReady.erase(SocketReady.begin() + std::distance(Sockets.begin(), it)); + Sockets.erase(it); } if (socket == MaxSockFD) { - MaxSockFD = 0; - for (auto &socketFd : Sockets) { - MaxSockFD = std::max(this->MaxSockFD, socketFd); - } + MaxSockFD = Sockets.empty() ? 0 : *ranges::max_element(Sockets); } } diff --git a/src/network/network.cpp b/src/network/network.cpp index 7b40d41742..62eb43c5f2 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -612,16 +612,10 @@ void NetworkSendExtendedCommand(int command, int arg1, int arg2, int arg3, void NetworkSendSelection(CUnit **units, int count) { // Check if we have any teammates to send to - bool hasteammates = false; - for (int i = 0; i < NetPlayers; ++i) { - if (ThisPlayer->Index == Hosts[i].PlyNr) { - continue; // skip self - } - if (Hosts[i].IsValid() && Players[Hosts[i].PlyNr].Team == ThisPlayer->Team) { - hasteammates = true; - break; - } - } + const bool hasteammates = std::any_of(Hosts, Hosts + NetPlayers, [](const CNetworkHost &host) { + return ThisPlayer->Index != host.PlyNr && host.IsValid() + && Players[host.PlyNr].Team == ThisPlayer->Team; + }); if (!hasteammates) { return; } @@ -667,11 +661,12 @@ void NetworkSendChatMessage(const std::string &msg) static void NetworkRemovePlayer(int player) { // Remove player from Hosts and clear NetworkIn - for (int i = 0; i < NetPlayers; ++i) { - if (Hosts[i].IsValid() && Hosts[i].PlyNr == player) { - Hosts[i].Clear(); - break; - } + if (auto it = + std::find_if(Hosts, + Hosts + NetPlayers, + [&](const auto &host) { return host.IsValid() && host.PlyNr == player; }); + it != Hosts + NetPlayers) { + it->Clear(); } for (int i = 0; i < 256; ++i) { for (int c = 0; c < MaxNetworkCommands; ++c) { diff --git a/src/network/online_service.cpp b/src/network/online_service.cpp index dbd29e2c6e..2781cde1c1 100644 --- a/src/network/online_service.cpp +++ b/src/network/online_service.cpp @@ -1071,14 +1071,7 @@ class Context : public OnlineContext void setCurrentChannel(std::string name) { this->currentChannel = name; - bool unlisted = true; - for (const auto &c : channelList) { - if (c == name) { - unlisted = false; - break; - } - } - if (unlisted) { + if (!ranges::contains(channelList, name)) { channelList.push_back(name); setChannels(channelList); } diff --git a/src/pathfinder/pathfinder.cpp b/src/pathfinder/pathfinder.cpp index 0ad154d68e..b5b9f9ad3c 100644 --- a/src/pathfinder/pathfinder.cpp +++ b/src/pathfinder/pathfinder.cpp @@ -296,7 +296,6 @@ int CalcPathLengthToUnit(const CUnit &src, const CUnit &dst, const int minrange, case PF_UNREACHABLE: case PF_WAIT: return -1; - break; case PF_REACHED: return 0; } diff --git a/src/sound/sound_id.cpp b/src/sound/sound_id.cpp index 2eed4c44ee..8b74c8722e 100644 --- a/src/sound/sound_id.cpp +++ b/src/sound/sound_id.cpp @@ -155,9 +155,7 @@ CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second) void FreeSounds() { - std::map::iterator i; - for (i = SoundMap.begin(); i != SoundMap.end(); ++i) { - CSound *sound = (*i).second; + for (auto& [_, sound] : SoundMap) { Assert(sound && sound->Mapref != 0); if (sound && !--sound->Mapref) { delete sound; diff --git a/src/unit/script_unittype.cpp b/src/unit/script_unittype.cpp index 0d935d958a..53a55b4b8f 100644 --- a/src/unit/script_unittype.cpp +++ b/src/unit/script_unittype.cpp @@ -805,12 +805,7 @@ static int CclDefineUnitType(lua_State *l) type->Impact[ANIMATIONS_DEATHTYPES + 1].MapMissile(); } } else { - int num = 0; - for (; num < ANIMATIONS_DEATHTYPES; ++num) { - if (dtype == ExtraDeathTypes[num]) { - break; - } - } + const int num = std::distance(ExtraDeathTypes, ranges::find(ExtraDeathTypes, dtype)); if (num == ANIMATIONS_DEATHTYPES) { LuaError(l, "Death type not found: %s", dtype.data()); } else { @@ -1129,18 +1124,13 @@ static int CclDefineUnitType(lua_State *l) } else if (value == "work-complete") { type->Sound.WorkComplete.Name = LuaToString(l, -1, k + 1); } else if (value == "dead") { - int death; - const std::string_view name = LuaToString(l, -1, k + 1); - for (death = 0; death < ANIMATIONS_DEATHTYPES; ++death) { - if (name == ExtraDeathTypes[death]) { - ++k; - break; - } - } + const int death = std::distance(ExtraDeathTypes, ranges::find(ExtraDeathTypes, name)); + if (death == ANIMATIONS_DEATHTYPES) { type->Sound.Dead[ANIMATIONS_DEATHTYPES].Name = name; } else { + ++k; type->Sound.Dead[death].Name = LuaToString(l, -1, k + 1); } } else { @@ -1802,14 +1792,9 @@ static int CclGetUnitTypeData(lua_State *l) lua_pushstring(l, type->MapSound.Dead[ANIMATIONS_DEATHTYPES].Name.c_str()); } } else { - int death; const std::string_view sound_subtype = LuaToString(l, 4); + const int death = std::distance(ExtraDeathTypes, ranges::find(ExtraDeathTypes, sound_subtype)); - for (death = 0; death < ANIMATIONS_DEATHTYPES; ++death) { - if (sound_subtype == ExtraDeathTypes[death]) { - break; - } - } if (death == ANIMATIONS_DEATHTYPES) { if (!GameRunning && Editor.Running != EditorEditing) { lua_pushstring(l, type->Sound.Dead[ANIMATIONS_DEATHTYPES].Name.c_str()); @@ -2450,13 +2435,8 @@ void SetMapSound(std::string ident, std::string sound, std::string sound_type, s } else if (sound_type == "help") { type.MapSound.Help.Name = sound; } else if (sound_type == "dead") { - int death; + const int death = std::distance(ExtraDeathTypes, ranges::find(ExtraDeathTypes, sound_subtype)); - for (death = 0; death < ANIMATIONS_DEATHTYPES; ++death) { - if (sound_subtype == ExtraDeathTypes[death]) { - break; - } - } if (death == ANIMATIONS_DEATHTYPES) { type.MapSound.Dead[ANIMATIONS_DEATHTYPES].Name = sound; } else { diff --git a/src/video/video.cpp b/src/video/video.cpp index c1eeaa3d6b..f9a834feb3 100644 --- a/src/video/video.cpp +++ b/src/video/video.cpp @@ -455,12 +455,10 @@ void VideoPaletteListAdd(SDL_Surface *surface) if (surface == nullptr || surface->format == nullptr || surface->format->BytesPerPixel != 1) { return; } - CColorCycling &colorCycling = CColorCycling::GetInstance(); - std::vector::iterator it = ranges::find(colorCycling.PaletteList, surface); - if (it != colorCycling.PaletteList.end()) { - return ; + if (ranges::contains(colorCycling.PaletteList, surface)) { + return; } colorCycling.PaletteList.push_back(surface); } @@ -472,12 +470,7 @@ void VideoPaletteListAdd(SDL_Surface *surface) */ void VideoPaletteListRemove(SDL_Surface *surface) { - CColorCycling &colorCycling = CColorCycling::GetInstance(); - std::vector::iterator it = ranges::find(colorCycling.PaletteList, surface); - - if (it != colorCycling.PaletteList.end()) { - colorCycling.PaletteList.erase(it); - } + ranges::erase(CColorCycling::GetInstance().PaletteList, surface); } void ClearAllColorCyclingRange()