Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #677

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,7 @@ static bool AiRequestSupply()

// Count the already made build requests.
const auto counter = AiGetBuildRequestsCount(*AiPlayer);
struct cnode cache[16];

memset(cache, 0, sizeof(cache));
struct cnode cache[16]{};

//
// Check if we can build this?
Expand Down
14 changes: 6 additions & 8 deletions src/include/ui/statusline.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class CFont;
class CStatusLine
{
public:
CStatusLine() : Width(0), TextX(0), TextY(0), Font(0) {
memset(Costs, 0, (ManaResCost + 1) * sizeof(int));
}
CStatusLine() = default;

void Draw();
void DrawCosts();
Expand All @@ -54,11 +52,11 @@ class CStatusLine
void ClearCosts();

public:
int Width;
int TextX;
int TextY;
CFont *Font;
int Costs[ManaResCost + 1];
int Width = 0;
int TextX = 0;
int TextY = 0;
CFont *Font = nullptr;
int Costs[ManaResCost + 1]{};

private:
std::string StatusLine;
Expand Down
7 changes: 4 additions & 3 deletions src/include/upgrade_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
----------------------------------------------------------------------------*/

#include "settings.h"
#include "util.h"

#include <cstring>
#include <memory>
Expand Down Expand Up @@ -198,8 +199,8 @@ class CAllow

void Clear()
{
memset(Units, 0, sizeof(Units));
memset(Upgrades, 0, sizeof(Upgrades));
ranges::fill(Units, 0);
ranges::fill(Upgrades, '\0');
}

int Units[UnitTypeMax]{}; /// maximum amount of units allowed
Expand All @@ -215,7 +216,7 @@ class CUpgradeTimers
public:
CUpgradeTimers() = default;

void Clear() { memset(Upgrades, 0, sizeof(Upgrades)); }
void Clear() { ranges::fill(Upgrades, 0); }

/**
** all 0 at the beginning, all upgrade actions do increment values in
Expand Down
8 changes: 8 additions & 0 deletions src/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
//@{

#include "filesystem.h"
#include "stratagus.h"

#include <cstdlib>
#include <cstdint>
Expand Down Expand Up @@ -196,6 +197,13 @@ namespace ranges
return std::none_of(std::begin(range), std::end(range), std::forward<Predicate>(predicate));
}

template <typename Range>
bool equal(const Range &range1, const Range &range2)
{
return std::equal(std::begin(range1), std::end(range1), std::begin(range2), std::end(range2));
}


template <typename Range, typename Predicate>
std::size_t count_if(const Range &range, Predicate &&predicate)
{
Expand Down
4 changes: 2 additions & 2 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ void CMapInfo::Clear()
this->Description.clear();
this->Filename.clear();
this->MapWidth = this->MapHeight = 0;
memset(this->PlayerSide, 0, sizeof(this->PlayerSide));
memset(this->PlayerType, 0, sizeof(this->PlayerType));
ranges::fill(this->PlayerSide, 0);
ranges::fill(this->PlayerType, PlayerTypes::PlayerUnset);
this->MapUID = 0;

this->HighgroundsEnabled = false;
Expand Down
8 changes: 4 additions & 4 deletions src/map/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ void CTileset::clear()
midOneTreeTile = 0;
botOneTreeTile = 0;
removedTreeTile = 0;
memset(woodTable, 0, sizeof(woodTable));
ranges::fill(woodTable, 0);
mixedLookupTable.clear();
topOneRockTile = 0;
midOneRockTile = 0;
botOneRockTile = 0;
removedRockTile = 0;
memset(rockTable, 0, sizeof(rockTable));
memset(humanWallTable, 0, sizeof(humanWallTable));
memset(orcWallTable, 0, sizeof(orcWallTable));
ranges::fill(rockTable, 0);
ranges::fill(humanWallTable, 0);
ranges::fill(orcWallTable, 0);
}

bool CTileset::setTileCount(const size_t newCount)
Expand Down
5 changes: 2 additions & 3 deletions src/network/mdns_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ static int service_callback(int sock, const struct sockaddr* from, size_t addrle

void MDNS::AnswerServerQueries() {
if (serviceSocket == -1) {
// When recieving, a socket can recieve data from all network interfaces
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(struct sockaddr_in));
// When receiving, a socket can receive data from all network interfaces
struct sockaddr_in sock_addr{};
sock_addr.sin_family = AF_INET;
#ifdef _WIN32
sock_addr.sin_addr = in4addr_any;
Expand Down
9 changes: 3 additions & 6 deletions src/network/net_lowlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,8 @@ Socket NetOpenUDP(unsigned long ip, int port)
}
// bind local port
if (port) {
struct sockaddr_in sock_addr;
struct sockaddr_in sock_addr{};

memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = ip;
sock_addr.sin_port = htons(port);
Expand Down Expand Up @@ -339,9 +338,8 @@ Socket NetOpenTCP(const char *addr, int port)
}
// bind local port
if (port) {
struct sockaddr_in sock_addr;
struct sockaddr_in sock_addr{};

memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
if (addr) {
sock_addr.sin_addr.s_addr = inet_addr(addr);
Expand Down Expand Up @@ -377,7 +375,6 @@ Socket NetOpenTCP(const char *addr, int port)
*/
int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
{
struct sockaddr_in sa;
#ifndef __BEOS__
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (setsockopttype)&opt, sizeof(opt));
Expand All @@ -389,7 +386,7 @@ int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
return -1;
}

memset(&sa, 0, sizeof(sa));
struct sockaddr_in sa{};
memcpy(&sa.sin_addr, &addr, sizeof(addr));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
Expand Down
7 changes: 3 additions & 4 deletions src/network/net_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void CNetworkHost::Clear()
this->Host = 0;
this->Port = 0;
this->PlyNr = 0;
memset(this->PlyName, 0, sizeof(this->PlyName));
ranges::fill(this->PlyName, '\0');
}

void CNetworkHost::SetName(const char *name)
Expand Down Expand Up @@ -348,9 +348,8 @@ void CServerSetup::Save(const std::function <void (std::string)>& f) {

bool CServerSetup::operator == (const CServerSetup &rhs) const
{
return (ServerGameSettings == rhs.ServerGameSettings
&& memcmp(CompOpt, rhs.CompOpt, sizeof(CompOpt)) == 0
&& memcmp(Ready, rhs.Ready, sizeof(Ready)) == 0);
return (ServerGameSettings == rhs.ServerGameSettings && ranges::equal(CompOpt, rhs.CompOpt)
&& ranges::equal(Ready, rhs.Ready));
}

//
Expand Down
3 changes: 1 addition & 2 deletions src/network/netsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ class CTCPSocket_Impl

bool CTCPSocket_Impl::Open(const CHost &host)
{
char ip[24]; // 127.255.255.255:65555
memset(&ip, 0, sizeof(ip));
char ip[24]{}; // 127.255.255.255:65555
sprintf(ip, "%d.%d.%d.%d", NIPQUAD(ntohl(host.getIp())));
this->socket = NetOpenTCP(ip, host.getPort());
return this->socket != INVALID_SOCKET;
Expand Down
10 changes: 5 additions & 5 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,11 @@ void NetworkOnStartGame()
ncqs[1].Type = MessageNone;
}
}
memset(NetworkSyncSeeds, 0, sizeof(NetworkSyncSeeds));
memset(NetworkSyncHashs, 0, sizeof(NetworkSyncHashs));
memset(PlayerQuit, 0, sizeof(PlayerQuit));
memset(NetworkLastFrame, 0, sizeof(NetworkLastFrame));
memset(NetworkLastCycle, 0, sizeof(NetworkLastCycle));
ranges::fill(NetworkSyncSeeds, 0);
ranges::fill(NetworkSyncHashs, 0);
ranges::fill(PlayerQuit, 0);
ranges::fill(NetworkLastFrame, 0);
ranges::fill(NetworkLastCycle, 0);
}

//----------------------------------------------------------------------------
Expand Down
22 changes: 11 additions & 11 deletions src/stratagus/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,8 @@ void CPlayer::Init(PlayerTypes type)
this->MaxResources[i] = DefaultResourceMaxAmounts[i];
}

memset(this->UnitTypesCount, 0, sizeof(this->UnitTypesCount));
memset(this->UnitTypesAiActiveCount, 0, sizeof(this->UnitTypesAiActiveCount));
ranges::fill(this->UnitTypesCount, 0);
ranges::fill(this->UnitTypesAiActiveCount, 0);

this->Supply = 0;
this->Demand = 0;
Expand Down Expand Up @@ -801,14 +801,14 @@ void CPlayer::Clear()
GaveVisionTo.clear();
StartPos.x = 0;
StartPos.y = 0;
memset(Resources, 0, sizeof(Resources));
memset(StoredResources, 0, sizeof(StoredResources));
memset(MaxResources, 0, sizeof(MaxResources));
memset(LastResources, 0, sizeof(LastResources));
memset(Incomes, 0, sizeof(Incomes));
memset(Revenue, 0, sizeof(Revenue));
memset(UnitTypesCount, 0, sizeof(UnitTypesCount));
memset(UnitTypesAiActiveCount, 0, sizeof(UnitTypesAiActiveCount));
ranges::fill(Resources, 0);
ranges::fill(StoredResources, 0);
ranges::fill(MaxResources, 0);
ranges::fill(LastResources, 0);
ranges::fill(Incomes, 0);
ranges::fill(Revenue, 0);
ranges::fill(UnitTypesCount, 0);
ranges::fill(UnitTypesAiActiveCount, 0);
AiEnabled = false;
Ai = nullptr;
this->Units.resize(0);
Expand All @@ -823,7 +823,7 @@ void CPlayer::Clear()
Score = 0;
TotalUnits = 0;
TotalBuildings = 0;
memset(TotalResources, 0, sizeof(TotalResources));
ranges::fill(TotalResources, 0);
TotalRazings = 0;
TotalKills = 0;
this->LostMainFacilityTimer = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/stratagus/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,7 @@ void aligned_free(void *block)
fs::path GetExecutablePath()
{
#ifdef WIN32
TCHAR executable_path[MAX_PATH];
memset(executable_path, 0, sizeof(executable_path));
TCHAR executable_path[MAX_PATH]{};
GetModuleFileName(nullptr, executable_path, sizeof(executable_path)-1);
#else
const auto& executable_path = OriginalArgv[0];
Expand Down
15 changes: 6 additions & 9 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,24 +528,20 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
LastDrawnButtonPopup = const_cast<ButtonAction *>(&button);
}

int popupWidth, popupHeight;
int Costs[ManaResCost + 1];
memset(Costs, 0, sizeof(Costs));

int Costs[ManaResCost + 1]{};
switch (button.Action) {
case ButtonCmd::Research:
memcpy(Costs, AllUpgrades[button.Value]->Costs, sizeof(AllUpgrades[button.Value]->Costs));
ranges::copy(AllUpgrades[button.Value]->Costs, std::begin(Costs));
break;
case ButtonCmd::SpellCast:
memcpy(Costs, SpellTypeTable[button.Value]->Costs, sizeof(SpellTypeTable[button.Value]->Costs));
ranges::copy(SpellTypeTable[button.Value]->Costs, std::begin(Costs));
Costs[ManaResCost] = SpellTypeTable[button.Value]->ManaCost;
break;
case ButtonCmd::Build:
case ButtonCmd::Train:
case ButtonCmd::UpgradeTo:
memcpy(Costs,
getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs,
sizeof(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs));
ranges::copy(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs,
std::begin(Costs));
Costs[FoodCost] = getUnitTypes()[button.Value]
->Stats[ThisPlayer->Index]
.Variables[DEMAND_INDEX]
Expand All @@ -555,6 +551,7 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
break;
}

int popupWidth, popupHeight;
if (useCache) {
popupWidth = popupCache.popupWidth;
popupHeight = popupCache.popupHeight;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void ShowInput()
static void UiBeginInput()
{
KeyState = EKeyState::Input;
memset(Input, 0, sizeof(Input));
ranges::fill(Input, '\0');
InputIndex = 0;
addCursorToInput();
UI.StatusLine.ClearCosts();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/statusline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void CStatusLine::SetCosts(int mana, int food, const int *costs)
if (costs) {
memcpy(Costs, costs, MaxCosts * sizeof(*costs));
} else {
memset(Costs, 0, sizeof(Costs));
ranges::fill(Costs, 0);
}
Costs[ManaResCost] = mana;
Costs[FoodCost] = food;
Expand Down
14 changes: 8 additions & 6 deletions src/unit/script_unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,9 +1239,9 @@ static int CclCopyUnitType(lua_State *l)
to->Portrait.Files = from.Portrait.Files;
to->Portrait.Mngs.resize(to->Portrait.Files.size());
#endif
memcpy(to->DefaultStat.Costs, from.DefaultStat.Costs, sizeof(from.DefaultStat.Costs));
memcpy(to->DefaultStat.Storing, from.DefaultStat.Storing, sizeof(from.DefaultStat.Storing));
memcpy(to->DefaultStat.ImproveIncomes, from.DefaultStat.ImproveIncomes, sizeof(from.DefaultStat.ImproveIncomes));
ranges::copy(from.DefaultStat.Costs, std::begin(to->DefaultStat.Costs));
ranges::copy(from.DefaultStat.Storing, std::begin(to->DefaultStat.Storing));
ranges::copy(from.DefaultStat.ImproveIncomes, std::begin(to->DefaultStat.ImproveIncomes));
to->Construction = from.Construction;
to->DrawLevel = from.DrawLevel;
to->MaxOnBoard = from.MaxOnBoard;
Expand Down Expand Up @@ -1310,7 +1310,7 @@ static int CclCopyUnitType(lua_State *l)
to->CanAttack = from.CanAttack;
to->RepairRange = from.RepairRange;
to->RepairHP = from.RepairHP;
memcpy(to->RepairCosts, from.RepairCosts, sizeof(from.RepairCosts));
ranges::copy(from.RepairCosts, std::begin(to->RepairCosts));
to->CanTarget = from.CanTarget;
to->Building = from.Building;
to->BuildingRules.clear();
Expand Down Expand Up @@ -1343,9 +1343,11 @@ static int CclCopyUnitType(lua_State *l)
to->BoolFlag[i].CanTargetFlag = from.BoolFlag[i].CanTargetFlag;
to->BoolFlag[i].AiPriorityTarget = from.BoolFlag[i].AiPriorityTarget;
}
memcpy(to->ResInfo, from.ResInfo, sizeof(from.ResInfo));
for (std::size_t i = 0; i != std::size(to->ResInfo); ++i) {
from.ResInfo[i] = to->ResInfo[i] ? std::make_unique<ResourceInfo>(*to->ResInfo[i]) : nullptr;
}
to->GivesResource = from.GivesResource;
memcpy(to->CanStore, from.CanStore, sizeof(from.CanStore));
ranges::copy(from.CanStore, std::begin(to->CanStore));
to->CanCastSpell = from.CanCastSpell;
to->AutoCastActive = from.AutoCastActive;
to->Sound.Selected.Name = from.Sound.Selected.Name;
Expand Down
4 changes: 2 additions & 2 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void CUnit::Init()

Frame = 0;
Colors = -1;
memset(IndividualUpgrades, 0, sizeof(IndividualUpgrades));
ranges::fill(IndividualUpgrades, false);
IX = 0;
IY = 0;
Direction = 0;
Expand All @@ -453,7 +453,7 @@ void CUnit::Init()
JustMoved = 0;
TeamSelected = 0;
RescuedFrom = nullptr;
memset(VisCount, 0, sizeof(VisCount));
ranges::fill(VisCount, 0);
memset(&Seen, 0, sizeof(Seen));
Variable.clear();
TTL = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/unit/upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ static int CclDefineModifier(lua_State *l)

auto um = std::make_unique<CUpgradeModifier>();

memset(um->ChangeUpgrades, '?', sizeof(um->ChangeUpgrades));
memset(um->ApplyTo, '?', sizeof(um->ApplyTo));
ranges::fill(um->ChangeUpgrades, '?');
ranges::fill(um->ApplyTo, '?');
um->Modifier.Variables.resize(UnitTypeVar.GetNumberVariable());
um->ModifyPercent.resize(UnitTypeVar.GetNumberVariable());

Expand Down
Loading
Loading