Skip to content

Commit

Permalink
Clean up usage of namepace std
Browse files Browse the repository at this point in the history
It's pretty bad form to have header files do 'using namespace'
directives and pull in a lot of definitions that way.  While it
isn't too horrible in a private project it's just bad practice.

This removes all the `using namespace std;` from the header files
and declares the objects with explicit namespaces.   For the .cpp
files, if there were only a few uses of the namespace, just do the
in-place explicit naming.  If the cpp file used it in a lot of
places, those are a much more common (and expected) place to add
those sort of `using` directives.

Also removed a couple of long-commented out functions.
  • Loading branch information
jt-traub committed Sep 28, 2024
1 parent 31ee961 commit 26a787f
Show file tree
Hide file tree
Showing 44 changed files with 499 additions and 515 deletions.
28 changes: 14 additions & 14 deletions aregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class TownInfo
TownInfo();
~TownInfo();

void Readin(istream& f);
void Writeout(ostream& f);
void Readin(std::istream& f);
void Writeout(std::ostream& f);
int TownType();

AString *name;
Expand Down Expand Up @@ -187,15 +187,15 @@ class ARegion : public AListElem
ARegion();
//ARegion(int, int);
~ARegion();

void Setup();
void ManualSetup(const RegionSetup& settings);

void ZeroNeighbors();
void SetName(char const *);

void Writeout(ostream& f);
void Readin(istream& f, AList *);
void Writeout(std::ostream& f);
void Readin(std::istream& f, AList *);

int CanMakeAdv(Faction *, int);
int HasItem(Faction *, int);
Expand Down Expand Up @@ -318,7 +318,7 @@ class ARegion : public AListElem
int wages;
int maxwages;
int wealth;

/* Economy */
int habitat;
int development;
Expand All @@ -336,15 +336,15 @@ class ARegion : public AListElem
int emigrants;
// economic improvement
int improvement;

/* Potential bonuses to economy */
int clearskies;
int earthlore;
int phantasmal_entertainment;

ARegion *neighbors[NDIRS];
AList objects;
map<int,int> newfleets;
std::map<int,int> newfleets;
int fleetalias;
std::vector<Unit *>hell; /* Where dead units go */
AList farsees;
Expand Down Expand Up @@ -447,10 +447,10 @@ class GeoMap
int GetVegetation(int, int);
int GetCulture(int, int);
void ApplyGeography(ARegionArray *pArr);

int size, xscale, yscale, xoff, yoff;
map<long int,Geography> geomap;
std::map<long int, Geography> geomap;

};

class ARegionList : public AList
Expand All @@ -461,8 +461,8 @@ class ARegionList : public AList

ARegion *GetRegion(int);
ARegion *GetRegion(int, int, int);
int ReadRegions(istream &f, AList *);
void WriteRegions(ostream& f);
int ReadRegions(std::istream &f, AList *);
void WriteRegions(std::ostream& f);
Location *FindUnit(int);
Location *GetUnitId(UnitId *id, int faction, ARegion *cur);

Expand Down Expand Up @@ -516,7 +516,7 @@ class ARegionList : public AList
void UnsetRace(ARegionArray *pRegs);
void RaceAnchors(ARegionArray *pRegs);
void GrowRaces(ARegionArray *pRegs);

void TownStatistics();
void ResourcesStatistics();

Expand Down
20 changes: 11 additions & 9 deletions army.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "gameio.h"
#include "gamedata.h"

#include <assert.h>
#include <assert.h>

void unit_stat_control::Clear(UnitStat& us) {
us.attackStats.clear();
Expand Down Expand Up @@ -249,8 +249,8 @@ Soldier::Soldier(Unit * u,Object * o,int regtype,int r,int ass)
dskill[i] += ObjectDefs[o->type].defenceArray[i];
}
if (o->runes) {
dskill[ATTACK_ENERGY] = max(dskill[ATTACK_ENERGY], o->runes);
dskill[ATTACK_SPIRIT] = max(dskill[ATTACK_SPIRIT], o->runes);
dskill[ATTACK_ENERGY] = std::max(dskill[ATTACK_ENERGY], o->runes);
dskill[ATTACK_SPIRIT] = std::max(dskill[ATTACK_SPIRIT], o->runes);
}
o->capacity--;
}
Expand Down Expand Up @@ -699,7 +699,7 @@ Army::Army(Unit * ldr,AList * locs,int regtype,int ass)
}
}
}
// If TACTICS_NEEDS_WAR is enabled, we don't want to push leaders
// If TACTICS_NEEDS_WAR is enabled, we don't want to push leaders
// from tact-4 to tact-5! Also check that we have skills, otherwise
// we get a nasty core dump ;)
if (Globals->TACTICS_NEEDS_WAR && (tactician->skills.size() != 0)) {
Expand Down Expand Up @@ -1352,8 +1352,10 @@ int Army::DoAnAttack(Battle * b, char const *special, int numAttacks, int attack
}

if (canShield) {
auto correctShield = [&attackType](shared_ptr<Shield> sh) { return sh->shieldtype == attackType; };
auto compareShield = [](shared_ptr<Shield> s1, shared_ptr<Shield> s2) { return s1->shieldskill < s2->shieldskill; };
auto correctShield = [&attackType](std::shared_ptr<Shield> sh) { return sh->shieldtype == attackType; };
auto compareShield = [](std::shared_ptr<Shield> s1, std::shared_ptr<Shield> s2) {
return s1->shieldskill < s2->shieldskill;
};
auto validShields = shields | std::views::filter(correctShield);
auto maxShield = std::max_element(validShields.begin(), validShields.end(), compareShield);

Expand Down Expand Up @@ -1506,9 +1508,9 @@ void Army::Kill(int killed, int damage)
if (Globals->ARMY_ROUT == GameDefs::ARMY_ROUT_HITS_INDIVIDUAL)
hitsalive--;

temp->damage += min(temp->hits, damage);
temp->hits = max(0, temp->hits - damage);
temp->damage += std::min(temp->hits, damage);
temp->hits = std::max(0, temp->hits - damage);

if (temp->hits > 0) return;

temp->unit->losses++;
Expand Down
5 changes: 2 additions & 3 deletions army.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <functional>
#include <map>
#include <vector>
using namespace std;

class Soldier;
class Army;
Expand Down Expand Up @@ -154,7 +153,7 @@ class Soldier {
int amuletofi;

/* Effects */
map< char const *, int > effects;
std::map<char const *, int> effects;
};

typedef Soldier * SoldierPtr;
Expand Down Expand Up @@ -208,7 +207,7 @@ class Army

SoldierPtr * soldiers;
Unit * leader;
std::vector<shared_ptr<Shield>> shields;
std::vector<std::shared_ptr<Shield>> shields;
int round;
int tac;
int canfront;
Expand Down
8 changes: 4 additions & 4 deletions astring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,24 @@ int AString::strict_value() //this cannot handle negative numbers!
return ret;
}

ostream& operator<<(ostream &os,const AString &s)
std::ostream& operator<<(std::ostream &os,const AString &s)
{
os << s.str;
return os;
}

istream& operator>>(istream &is,AString &s)
std::istream& operator>>(std::istream &is,AString &s)
{
// We expect to read a line at a time from the file, not a string at a time since we do tokenization internally.
string buf;
std::string buf;
getline(is, buf);
s.len = strlen(buf.c_str());
s.str = new char[s.len + 1];
strcpy(s.str,buf.c_str());
return is;
}

const string plural(int count, const std::string &one, const std::string &many) {
const std::string plural(int count, const std::string &one, const std::string &many) {
return count != 1 ? many : one;
}

Expand Down
6 changes: 2 additions & 4 deletions astring.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
#include <string>
#include <vector>

using namespace std;

class AString {
friend ostream & operator <<(ostream &os, const AString &);
friend istream & operator >>(istream &is, AString &);
friend std::ostream & operator <<(std::ostream &os, const AString &);
friend std::istream & operator >>(std::istream &is, AString &);

public:

Expand Down
44 changes: 22 additions & 22 deletions basic/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ void ARegionList::CreateSurfaceLevel(int level, int xSize, int ySize, char const
int sea = Globals->OCEAN;
if (Globals->SEA_LIMIT)
sea = sea * (100 + 2 * Globals->SEA_LIMIT) / 100;

MakeLand(pRegionArrays[level], sea, Globals->CONTINENT_SIZE);

CleanUpWater(pRegionArrays[level]);

SetupAnchors(pRegionArrays[level]);

GrowTerrain(pRegionArrays[level], 0);

AssignTypes(pRegionArrays[level]);

SeverLandBridges(pRegionArrays[level]);

if (Globals->LAKES) RemoveCoastalLakes(pRegionArrays[level]);

if (Globals->GROW_RACES) GrowRaces(pRegionArrays[level]);

FinalSetup(pRegionArrays[level]);
Expand Down Expand Up @@ -281,8 +281,8 @@ void ARegionList::MakeRegions(int level, int xSize, int ySize)
// Some initial values; these will get reset
//
reg->type = -1;
reg->race = -1;
reg->wages = -1;
reg->race = -1;
reg->wages = -1;

reg->level = arr;
Add(reg);
Expand Down Expand Up @@ -376,7 +376,7 @@ void ARegionList::MakeIcosahedralRegions(int level, int xSize, int ySize)
// Some initial values; these will get reset
//
reg->type = -1;
reg->race = -1; //
reg->race = -1; //
reg->wages = -1; // initially store: name
reg->population = -1; // initially used as flag
reg->elevation = -1;
Expand Down Expand Up @@ -431,7 +431,7 @@ void ARegionList::MakeLand(ARegionArray *pRegs, int percentOcean,
if (!reg) continue;
ARegion *newreg = reg;
ARegion *seareg = reg;

// Archipelago or Continent?
if (getrandom(100) < Globals->ARCHIPELAGO) {
// Make an Archipelago:
Expand Down Expand Up @@ -511,7 +511,7 @@ void ARegionList::MakeLand(ARegionArray *pRegs, int percentOcean,
if ((reg->yloc < yoff*2) && ((dir < 2) || (dir == (NDIRS-1)))
&& (getrandom(4) < 3)) continue;
if ((reg->yloc > (yband+yoff)*2) && ((dir < 5) && (dir > 1))
&& (getrandom(4) < 3)) continue;
&& (getrandom(4) < 3)) continue;
ARegion *newreg = reg->neighbors[dir];
if (!newreg) break;
int polecheck = 0;
Expand Down Expand Up @@ -774,7 +774,7 @@ void ARegionList::GrowTerrain(ARegionArray *pArr, int growOcean)
if (!reg) continue;
if ((j > 0) && (j < 21) && (getrandom(3) < 2)) continue;
if (reg->type == R_NUM) {

// Check for Lakes
if (Globals->LAKES &&
(getrandom(100) < (Globals->LAKES/10 + 1))) {
Expand All @@ -788,7 +788,7 @@ void ARegionList::GrowTerrain(ARegionArray *pArr, int growOcean)
reg->wages = AGetName(0, reg);
break;
}


int init = getrandom(6);
for (int i=0; i<NDIRS; i++) {
Expand Down Expand Up @@ -920,13 +920,13 @@ void ARegionList::RaceAnchors(ARegionArray *pArr)
int xoff = x + 2 - getrandom(3) - getrandom(3);
ARegion *reg = pArr->GetRegion(xoff, y);
if (!reg) continue;

if ((reg->type == R_LAKE) && (!Globals->LAKESIDE_IS_COASTAL)) continue;
if (TerrainDefs[reg->type].flags & TerrainType::BARREN) continue;

reg->race = -1;
wigout = 0; // reset sanity

if (TerrainDefs[reg->type].similar_type == R_OCEAN) {
// setup near coastal race here
int d = getrandom(NDIRS);
Expand All @@ -937,7 +937,7 @@ void ARegionList::RaceAnchors(ARegionArray *pArr)
if (TerrainDefs[nreg->type].similar_type != R_OCEAN) {
int rnum = sizeof(TerrainDefs[nreg->type].coastal_races) /
sizeof(TerrainDefs[nreg->type].coastal_races[0]);

while ( reg->race == -1 || (ItemDefs[reg->race].flags & ItemType::DISABLED)) {
reg->race = TerrainDefs[nreg->type].coastal_races[getrandom(rnum)];
if (++wigout > 100) break;
Expand All @@ -952,14 +952,14 @@ void ARegionList::RaceAnchors(ARegionArray *pArr)
} else {
// setup noncoastal race here
int rnum = sizeof(TerrainDefs[reg->type].races)/sizeof(TerrainDefs[reg->type].races[0]);
while ( reg->race == -1 ||

while ( reg->race == -1 ||
(ItemDefs[reg->race].flags & ItemType::DISABLED)) {
reg->race = TerrainDefs[reg->type].races[getrandom(rnum)];
if (++wigout > 100) break;
}
}

/* leave out this sort of check for the moment
if (wigout > 100) {
// do something!
Expand All @@ -968,10 +968,10 @@ void ARegionList::RaceAnchors(ARegionArray *pArr)
Awrite(" region type");
}
*/

if (reg->race == -1) {
cout << "Hey! No race anchor got assigned to the "
<< TerrainDefs[reg->type].name
std::cout << "Hey! No race anchor got assigned to the "
<< TerrainDefs[reg->type].name
<< " at " << x << "," << y << "\n";
}
}
Expand Down
Loading

0 comments on commit 26a787f

Please sign in to comment.