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

Cleaned-up Map::cases. #37

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/AICastor.cpp
Expand Up @@ -2003,11 +2003,11 @@ void AICastor::computeObstacleUnitMap()
//int wMask=map->wMask;
//int hMask=map->hMask;
size_t size=w*h;
Case *cases=map->cases;
const auto& cases=map->cases;
Uint32 teamMask=team->me;
for (size_t i=0; i<size; i++)
{
Case c=cases[i];
const auto& c=cases[i];
if (c.building!=NOGBID)
obstacleUnitMap[i]=0;
else if (c.ressource.type!=NO_RES_TYPE)
Expand All @@ -2033,10 +2033,10 @@ void AICastor::computeObstacleBuildingMap()
//int hDec=map->hDec;
//int wDec=map->wDec;
size_t size=w*h;
Case *cases=map->cases;
const auto& cases=map->cases;
for (size_t i=0; i<size; i++)
{
Case c=cases[i];
const Case& c=cases[i];
if (c.building!=NOGBID)
obstacleBuildingMap[i]=0;
else if (c.terrain>=16) // if (!isGrass)
Expand Down Expand Up @@ -2102,7 +2102,7 @@ void AICastor::computeBuildingNeighbourMapOfBuilding(int bx, int by, int bw, int

//size_t size=w*h;
Uint8 *gradient=buildingNeighbourMap;
Case *cases=map->cases;
const auto& cases=map->cases;

//Uint8 *wheatGradient=map->ressourcesGradient[team->teamNumber][CORN][canSwim];

Expand Down Expand Up @@ -2169,7 +2169,7 @@ void AICastor::computeBuildingNeighbourMapOfBuilding(int bx, int by, int bw, int
gradient[((bx+bw)&wMask)+(((by+bh)&hMask)<<wDec)]|=1;
}

// At a range of 0 space case (neignbours), without corners,
// At a range of 0 space case (neighbours), without corners,
// we increment (bit 1 to 3), and dirty bit 0 in case:
for (int xi=bx-dw+1; xi<bx+bw; xi++)
{
Expand Down Expand Up @@ -2436,7 +2436,7 @@ fprintf(logFile, "computeHydratationMap()...\n");

Uint16 *gradient=(Uint16 *)malloc(2*size);
memset(gradient, 0, 2*size);
Case *cases=map->cases;
const auto& cases=map->cases;
static const int range=16;
for (int y=0; y<h; y++)
for (int x=0; x<w; x++)
Expand Down Expand Up @@ -2490,7 +2490,7 @@ void AICastor::computeNotGrassMap()

memset(notGrassMap, 0, size);

Case *cases=map->cases;
const auto& cases=map->cases;
for (size_t i=0; i<size; i++)
{
Uint16 t=cases[i].terrain;
Expand Down Expand Up @@ -2906,7 +2906,7 @@ void AICastor::computeRessourcesCluster()
fprintf(logFile, "computeRessourcesCluster()\n");
int w=map->w;
int h=map->h;
int wMask=map->wMask;
//int wMask=map->wMask;
int hMask=map->hMask;
size_t size=w*h;

Expand All @@ -2921,8 +2921,8 @@ void AICastor::computeRessourcesCluster()
{
for (int x=0; x<w; x++)
{
Case *c=map->cases+w*(y&hMask)+(x&wMask); // case
Ressource r=c->ressource; // ressource
const auto& c = map->cases[map->coordToIndex(x, y)]; // case
const auto& r=c.ressource; // ressource
Uint8 rt=r.type; // ressources type

int rci=x+y*w; // ressource cluster index
Expand Down
45 changes: 12 additions & 33 deletions src/Map.cpp
Expand Up @@ -89,7 +89,6 @@ Map::Map()
fogOfWarA=NULL;
fogOfWarB=NULL;
aStarPoints = NULL;
cases=NULL;
for (int t=0; t<Team::MAX_COUNT; t++)
for (int r=0; r<MAX_NB_RESSOURCES; r++)
for (int s=0; s<2; s++)
Expand Down Expand Up @@ -251,9 +250,6 @@ void Map::clear()
delete[] fogOfWarB;
fogOfWarB=NULL;

assert(cases);
delete[] cases;
cases=NULL;
for (int t=0; t<Team::MAX_COUNT; t++)
if (ressourcesGradient[t][0][0])
for (int r=0; r<MAX_RESSOURCES; r++)
Expand Down Expand Up @@ -328,7 +324,6 @@ void Map::clear()
assert(fogOfWar==NULL);
assert(fogOfWarA==NULL);
assert(fogOfWarB==NULL);
assert(cases==NULL);
for (int t=0; t<Team::MAX_COUNT; t++)
for (int r=0; r<MAX_RESSOURCES; r++)
for (int s=0; s<2; s++)
Expand Down Expand Up @@ -968,22 +963,7 @@ void Map::setSize(int wDec, int hDec, TerrainType terrainType)
localGuardAreaMap.resize(size, false);
localClearAreaMap.resize(size, false);

cases=new Case[size];
Case initCase;
initCase.terrain = 0; // default, not really meaningfull.
initCase.building = NOGBID;
initCase.ressource.clear();
initCase.groundUnit = NOGUID;
initCase.airUnit = NOGUID;
initCase.forbidden = 0;
initCase.guardArea = 0;
initCase.clearArea = 0;
initCase.scriptAreas = 0;
initCase.canRessourcesGrow = 1;
initCase.fertility = 0;

for (size_t i=0; i<size; i++)
cases[i]=initCase;
cases.assign(size, Case());

undermap=new Uint8[size];
memset(undermap, terrainType, size);
Expand Down Expand Up @@ -1085,7 +1065,7 @@ bool Map::load(GAGCore::InputStream *stream, MapHeader& header, Game *game)
localForbiddenMap.resize(size, false);
localGuardAreaMap.resize(size, false);
localClearAreaMap.resize(size, false);
cases = new Case[size];
cases.resize(size);
undermap = new Uint8[size];
listedAddr = new Uint8*[size];
aStarPoints=new AStarAlgorithmPoint[size];
Expand Down Expand Up @@ -2782,7 +2762,7 @@ template<typename Tint> void Map::updateRessourcesGradient(int teamNumber, Uint8
assert(globalContainer);
for (size_t i=0; i<size; i++)
{
Case& c=cases[i];
const Case& c=cases[i];
if (c.forbidden & teamMask)
gradient[i]=0;
else if(immobileUnits[i] != 255)
Expand Down Expand Up @@ -3615,7 +3595,7 @@ template<typename Tint> void Map::updateGlobalGradient(Building *building, bool
for (int x=0; x<w; x++)
{
int wyx=wy+x;
Case& c=cases[wyx];
const Case& c=cases[wyx];
if (c.building==NOGBID)
{
if (c.forbidden&teamMask)
Expand Down Expand Up @@ -5144,17 +5124,16 @@ Uint32 Map::checkSum(bool heavy)
Uint32 cs=size;
if (heavy)
{
const Case* end = cases + (w * h);
for (Case* c=cases; c < end; ++c)
for (const auto& c: cases)
{
cs+=
c->terrain +
c->building +
c->ressource.getUint32() +
c->groundUnit +
c->airUnit +
c->forbidden +
c->scriptAreas;
c.terrain +
c.building +
c.ressource.getUint32() +
c.groundUnit +
c.airUnit +
c.forbidden +
c.scriptAreas;
cs=(cs<<1)|(cs>>31);
}
};
Expand Down
31 changes: 16 additions & 15 deletions src/Map.h
Expand Up @@ -49,24 +49,24 @@ class MapHeader;
// a 1x1 piece of map
struct Case
{
Uint16 terrain;
Uint16 building;
Uint16 terrain = 0; // default, not really meaningful.
Uint16 building = NOGBID;

Ressource ressource;

Uint16 groundUnit;
Uint16 airUnit;
Uint16 groundUnit = NOGUID;
Uint16 airUnit = NOGUID;

Uint32 forbidden; // This is a mask, one bit by team, 1=forbidden, 0=allowed
Uint32 forbidden = 0; // This is a mask, one bit by team, 1=forbidden, 0=allowed
///The difference between forbidden zone and hidden forbidden zone is that hidden forbidden zone
///is put there by the game engine and is not draw to the screen.
Uint32 guardArea; // This is a mask, one bit by team, 1=guard area, 0=normal
Uint32 clearArea; // This is a mask, one bit by team, 1=clear area, 0=normal
Uint32 guardArea = 0; // This is a mask, one bit by team, 1=guard area, 0=normal
Uint32 clearArea = 0; // This is a mask, one bit by team, 1=clear area, 0=normal

Uint16 scriptAreas; // This is also a mask. A single bit represents an area #n, on or off for the square
Uint8 canRessourcesGrow; // This is a boolean, it represents whether ressources are allowed to grow into this location.
Uint16 scriptAreas = 0; // This is also a mask. A single bit represents an area #n, on or off for the square
Uint8 canRessourcesGrow = 1; // This is a boolean, it represents whether ressources are allowed to grow into this location.

Uint16 fertility; // This is a value that represents the fertility of this square, the chance that wheat will grow on it
Uint16 fertility = 0; // This is a value that represents the fertility of this square, the chance that wheat will grow on it
};

/// Types of areas
Expand Down Expand Up @@ -138,8 +138,9 @@ class Map
//! Return the number of sectors on y, which corresponds to the sector map height
int getSectorH(void) const { return hSector; }

/// Return an index into map arrays for a given position, wrap-safe
size_t coordToIndex(int x, int y) {
return ((y&hMask)<<wDec)+(x&wMask);
return ((y & hMask) << wDec) + (x & wMask);
}

///Returns a normalized version of the x cordinate, taking into account that x coordinates wrap around
Expand Down Expand Up @@ -174,7 +175,7 @@ class Map
//! Make the building at (x, y) visible for all teams in sharedVision (mask).
void setMapBuildingsDiscovered(int x, int y, Uint32 sharedVision, Team *teams[Team::MAX_COUNT])
{
Uint16 bgid = (cases+coordToIndex(x, y))->building;
Uint16 bgid = cases[coordToIndex(x, y)].building;
if (bgid != NOGBID)
{
int id = Building::GIDtoID(bgid);
Expand Down Expand Up @@ -340,9 +341,9 @@ class Map
return cases[coordToIndex(x, y)].ressource;
}

Ressource getRessource(unsigned pos)
Ressource getRessource(size_t pos)
{
return (cases+pos)->ressource;
return cases[pos].ressource;
}

//Returns the combined forbidden and hidden foribidden masks
Expand Down Expand Up @@ -759,7 +760,7 @@ class Map
public:
Game *game;
public:
Case *cases;
std::vector<Case> cases;
Sint32 w, h;
Sint32 wMask, hMask;
Sint32 wDec, hDec;
Expand Down
10 changes: 5 additions & 5 deletions src/Ressource.h
Expand Up @@ -30,14 +30,14 @@
//! Either a ressource type is NO_RES_TYPE and all others fields are zero, or the ressource has a valid type and amount is NOT zero. This constraint does not apply if a ressource is eternal.
struct Ressource
{
Uint8 type;
Uint8 variety;
Uint8 amount;
Uint8 animation;
Uint8 type = NO_RES_TYPE;
Uint8 variety = 0;
Uint8 amount = 0;
Uint8 animation = 0;

void clear() {type=NO_RES_TYPE; variety = 0; amount = 0; animation = 0; }
//void setUint32(Uint32 i) { animation=i&0xFF; amount=(i>>8)&0xFF; variety=(i>>16)&0xFF; type=(i>>24)&0xFF; }
Uint32 getUint32() { return animation | (amount<<8) | (variety<<16) | (type<<24); }
Uint32 getUint32() const { return animation | (amount<<8) | (variety<<16) | (type<<24); }
};

std::string getRessourceName(int type);
Expand Down