Skip to content

Commit

Permalink
Merge pull request #645 from Wargus/range
Browse files Browse the repository at this point in the history
Range
  • Loading branch information
Jarod42 committed Mar 24, 2024
2 parents 6ac60c6 + d7980dc commit 97c5f51
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 94 deletions.
10 changes: 2 additions & 8 deletions src/ai/ai_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
9 changes: 4 additions & 5 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
Expand Down
2 changes: 1 addition & 1 deletion src/include/net_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ namespace ranges
v.erase(std::remove_if(std::begin(v), std::end(v), pred), std::end(v));
}

template <typename Range>
auto max_element(Range &range)
{
return std::max_element(std::begin(range), std::end(range));
}

template <typename Range>
auto min_element(Range &range)
{
Expand Down
17 changes: 4 additions & 13 deletions src/network/net_lowlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,21 +642,12 @@ void SocketSet::AddSocket(Socket socket)
*/
void SocketSet::DelSocket(Socket socket)
{
std::vector<Socket>::iterator i;
std::vector<int>::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);
}
}

Expand Down
25 changes: 10 additions & 15 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 1 addition & 8 deletions src/network/online_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/pathfinder/pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions src/sound/sound_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second)

void FreeSounds()
{
std::map<std::string, CSound *>::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;
Expand Down
6 changes: 2 additions & 4 deletions src/stratagus/construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,8 @@ static int CclDefineConstruction(lua_State *l)
LuaError(
l, "First percent of Construction '%s' is not 0", construction->Ident.c_str());
}
if (!std::is_sorted(
construction->Frames.begin(),
construction->Frames.end(),
[](const auto &lhs, const auto &rhs) { return lhs.Percent < rhs.Percent; })) {
if (!ranges::is_sorted(
construction->Frames, std::less<>{}, &CConstructionFrame::Percent)) {
LuaError(
l, "Percents of Construction '%s' not sorted", construction->Ident.c_str());
}
Expand Down
32 changes: 6 additions & 26 deletions src/unit/script_unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions src/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SDL_Surface *>::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);
}
Expand All @@ -472,12 +470,7 @@ void VideoPaletteListAdd(SDL_Surface *surface)
*/
void VideoPaletteListRemove(SDL_Surface *surface)
{
CColorCycling &colorCycling = CColorCycling::GetInstance();
std::vector<SDL_Surface *>::iterator it = ranges::find(colorCycling.PaletteList, surface);

if (it != colorCycling.PaletteList.end()) {
colorCycling.PaletteList.erase(it);
}
ranges::erase(CColorCycling::GetInstance().PaletteList, surface);
}

void ClearAllColorCyclingRange()
Expand Down

0 comments on commit 97c5f51

Please sign in to comment.