Skip to content

Commit

Permalink
Merge pull request #533 from Wargus/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 9, 2023
2 parents 2aa3af2 + 71f5fe6 commit dc4c915
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 246 deletions.
4 changes: 2 additions & 2 deletions src/action/action_still.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static bool MoveRandomly(CUnit &unit)
*/
bool AutoCast(CUnit &unit)
{
if (unit.AutoCastSpell && !unit.Removed) { // Removed units can't cast any spells, from bunker)
if (!unit.AutoCastSpell.empty() && !unit.Removed) { // Removed units can't cast any spells, from bunker)
for (unsigned int i = 0; i < SpellTypeTable.size(); ++i) {
if (unit.AutoCastSpell[i]
&& (SpellTypeTable[i]->AutoCast || SpellTypeTable[i]->AICast)
Expand Down Expand Up @@ -414,7 +414,7 @@ void COrder_Still::Execute(CUnit &unit) /* override */
this->Sleep = CYCLES_PER_SECOND / 2;

if (this->Action == UnitAction::StandGround || unit.Removed || unit.CanMove() == false) {
if (unit.AutoCastSpell) {
if (!unit.AutoCastSpell.empty()) {
this->AutoCastStand(unit);
}
if (unit.IsAgressive()) {
Expand Down
8 changes: 3 additions & 5 deletions src/action/action_upgradeto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ static int TransformUnitIntoType(CUnit &unit, const CUnitType &newtype)
unit.Type = const_cast<CUnitType *>(&newtype);
unit.Stats = const_cast<CUnitStats *>(&unit.Type->Stats[player.Index]);

if (!newtype.CanCastSpell.empty() && !unit.AutoCastSpell) {
unit.AutoCastSpell = new char[SpellTypeTable.size()];
unit.SpellCoolDownTimers = new int[SpellTypeTable.size()];
memset(unit.AutoCastSpell, 0, SpellTypeTable.size() * sizeof(char));
memset(unit.SpellCoolDownTimers, 0, SpellTypeTable.size() * sizeof(int));
if (!newtype.CanCastSpell.empty() && unit.AutoCastSpell.empty()) {
unit.AutoCastSpell.resize(SpellTypeTable.size());
unit.SpellCoolDownTimers.resize(SpellTypeTable.size());
}

UpdateForNewUnit(unit, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,7 @@ void CEditor::Init()
}
}

Map.Fields = new CMapField[Map.Info.MapWidth * Map.Info.MapHeight];
Map.Fields.resize(Map.Info.MapWidth * Map.Info.MapHeight);

const int defaultTile = Map.Tileset->getDefaultTileIndex();

Expand Down
56 changes: 20 additions & 36 deletions src/include/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ class CMapInfo
return (x >= 0 && y >= 0 && x < MapWidth && y < MapHeight);
}

bool IsPointOnMap(const Vec2i &pos) const
{
return IsPointOnMap(pos.x, pos.y);
}
bool IsPointOnMap(const Vec2i &pos) const { return IsPointOnMap(pos.x, pos.y); }

void Clear();

Expand All @@ -154,38 +151,25 @@ class CMapInfo
class CMap
{
public:
CMap();
CMap() = default;
~CMap();

void AllocateTileset();

unsigned int getIndex(int x, int y) const
{
return x + y * this->Info.MapWidth;
}
unsigned int getIndex(const Vec2i &pos) const
{
return getIndex(pos.x, pos.y);
}
unsigned int getIndex(int x, int y) const { return x + y * this->Info.MapWidth; }
unsigned int getIndex(const Vec2i &pos) const { return getIndex(pos.x, pos.y); }

CMapField *Field(unsigned int index) const
{
return &this->Fields[index];
}
const CMapField *Field(unsigned int index) const { return &this->Fields[index]; }
/// Get the MapField at location x,y
CMapField *Field(int x, int y) const
{
return &this->Fields[x + y * this->Info.MapWidth];
}
CMapField *Field(const Vec2i &pos) const
{
return Field(pos.x, pos.y);
}
const CMapField *Field(int x, int y) const { return &this->Fields[getIndex(x, y)]; }
const CMapField *Field(const Vec2i &pos) const { return Field(getIndex(pos)); }

bool isInitialized() const
{
return this->isMapInitialized;
}
CMapField *Field(unsigned int index) { return &this->Fields[index]; }
/// Get the MapField at location x,y
CMapField *Field(int x, int y) { return &this->Fields[getIndex(x, y)]; }
CMapField *Field(const Vec2i &pos) { return Field(getIndex(pos)); }

bool isInitialized() const { return this->isMapInitialized; }

/// Alocate and initialise map table.
void Create();
Expand Down Expand Up @@ -265,15 +249,15 @@ class CMap
void RegenerateForestTile(const Vec2i &pos);

public:
CMapField *Fields; /// fields on map
bool NoFogOfWar; /// fog of war disabled
std::vector<CMapField> Fields; /// fields on map
bool NoFogOfWar = false; /// fog of war disabled

CTileset *Tileset; /// tileset data
fs::path TileModelsFileName; /// lua filename that loads all tilemodels
CGraphic *TileGraphic; /// graphic for all the tiles
bool isMapInitialized { false };
CTileset *Tileset = nullptr; /// tileset data
fs::path TileModelsFileName; /// lua filename that loads all tilemodels
CGraphic *TileGraphic = nullptr; /// graphic for all the tiles
bool isMapInitialized = false ;

CMapInfo Info; /// descriptive information
CMapInfo Info; /// descriptive information
};


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 @@ -316,7 +316,7 @@ class CInitMessage_MapFileFragment
{
public:
CInitMessage_MapFileFragment() {}
CInitMessage_MapFileFragment(const char *path, const char *data, uint32_t dataSize, uint32_t Fragment);
CInitMessage_MapFileFragment(const char *path, const std::vector<char> &data, uint32_t Fragment);
CInitMessage_MapFileFragment(uint32_t Fragment);
const CInitMessage_Header &GetHeader() const { return header; }
std::vector<unsigned char> Serialize() const;
Expand Down
13 changes: 6 additions & 7 deletions src/include/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ enum _directions_ {
class CUnit
{
public:
CUnit() : tilePos(-1, -1), pathFinderData(nullptr), SavedOrder(nullptr), NewOrder(nullptr), CriticalOrder(nullptr), Colors(-1),
AutoCastSpell(nullptr), SpellCoolDownTimers(nullptr), Variable(nullptr) { Init(); }
CUnit() { Init(); }
~CUnit();

void Init();
Expand Down Expand Up @@ -336,7 +335,7 @@ class CUnit
int Active; /// how many units are harvesting from the resource.
} Resource; /// Resource still

Vec2i tilePos; /// Map position
Vec2i tilePos{-1, -1}; /// Map position

unsigned int Offset;/// Map position as flat index offset (x + y * w)

Expand All @@ -349,8 +348,8 @@ class CUnit
PathFinderData *pathFinderData = nullptr;

// DISPLAY:
int Frame; /// Image frame: <0 is mirrored
int Colors; /// custom colors
int Frame; /// Image frame: <0 is mirrored
int Colors = -1; /// custom colors
bool IndividualUpgrades[UpgradeMax]{}; /// individual upgrades which the unit has

signed char IX; /// X image displacement to map position
Expand Down Expand Up @@ -428,8 +427,8 @@ class CUnit
std::unique_ptr<COrder> NewOrder; /// order for new trained units
std::unique_ptr<COrder> CriticalOrder; /// order to do as possible in breakable animation.

char *AutoCastSpell = nullptr; /// spells to auto cast
int *SpellCoolDownTimers = nullptr; /// how much time unit need to wait before spell will be ready
std::vector<bool> AutoCastSpell; /// spells to auto cast
std::vector<int> SpellCoolDownTimers; /// how much time unit need to wait before spell will be ready

CUnit *Goal = nullptr; /// Generic/Teleporter goal pointer
};
Expand Down
2 changes: 1 addition & 1 deletion src/include/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ class CUnitType
int RepairRange = 0; /// Units repair range.
#define InfiniteRepairRange INT_MAX
std::vector<char> CanCastSpell; /// Unit is able to use spells.
std::vector<char> AutoCastActive; /// Default value for autocast.
std::vector<bool> AutoCastActive; /// Default value for autocast.
int AutoBuildRate = 0; /// The rate at which the building builds itself
int RandomMovementProbability = 0; /// Probability to move randomly.
int RandomMovementDistance = 1; /// Quantity of tiles to move randomly.
Expand Down
43 changes: 20 additions & 23 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void CMap::MarkSeenTile(CMapField &mf)

#ifdef MINIMAP_UPDATE
//rb - GRRRRRRRRRRRR
const unsigned int index = &mf - Map.Fields;
const unsigned int index = &mf - Map.Fields.data();
const int y = index / Info.MapWidth;
const int x = index - (y * Info.MapWidth);
const Vec2i pos = {x, y}
Expand All @@ -90,7 +90,7 @@ void CMap::MarkSeenTile(CMapField &mf)
if (this->Tileset->TileTypeTable.empty() == false) {
#ifndef MINIMAP_UPDATE
//rb - GRRRRRRRRRRRR
const unsigned int index = &mf - Map.Fields;
const unsigned int index = &mf - Map.Fields.data();
const int y = index / Info.MapWidth;
const int x = index - (y * Info.MapWidth);
const Vec2i pos(x, y);
Expand Down Expand Up @@ -318,10 +318,6 @@ void CMapInfo::Clear()
this->MapUID = 0;
}

CMap::CMap() : Fields(nullptr), NoFogOfWar(false), TileGraphic(nullptr), Tileset(nullptr)
{
}

CMap::~CMap()
{
delete Tileset;
Expand All @@ -338,9 +334,7 @@ void CMap::AllocateTileset()
*/
void CMap::Create()
{
Assert(!this->Fields);

this->Fields = new CMapField[this->Info.MapWidth * this->Info.MapHeight];
this->Fields.resize(this->Info.MapWidth * this->Info.MapHeight);
}

/**
Expand All @@ -358,12 +352,11 @@ void CMap::Init()
*/
void CMap::Clean(const bool isHardClean /* = false*/)
{
delete[] this->Fields;
this->Fields.clear();

// Tileset freed by Tileset?

this->Info.Clear();
this->Fields = nullptr;
this->NoFogOfWar = false;
this->Tileset->clear();
this->TileModelsFileName.clear();
Expand Down Expand Up @@ -532,12 +525,17 @@ void CMap::FixTile(tile_flags type, int seen, const Vec2i &pos)
*/
void CMap::FixNeighbors(tile_flags type, int seen, const Vec2i &pos)
{
const Vec2i offset[] = {Vec2i(1, 0), Vec2i(-1, 0), Vec2i(0, 1), Vec2i(0, -1),
Vec2i(-1, -1), Vec2i(-1, 1), Vec2i(1, -1), Vec2i(1, 1)
};

for (unsigned int i = 0; i < sizeof(offset) / sizeof(*offset); ++i) {
FixTile(type, seen, pos + offset[i]);
const Vec2i offsets[] = {Vec2i(1, 0),
Vec2i(-1, 0),
Vec2i(0, 1),
Vec2i(0, -1),
Vec2i(-1, -1),
Vec2i(-1, 1),
Vec2i(1, -1),
Vec2i(1, 1)};

for (const auto &offset : offsets) {
FixTile(type, seen, pos + offset);
}
}

Expand Down Expand Up @@ -588,6 +586,7 @@ void CMap::ClearWoodTile(const Vec2i &pos)
MarkSeenTile(mf);
}
}

/// Remove rock from the map.
void CMap::ClearRockTile(const Vec2i &pos)
{
Expand All @@ -607,8 +606,6 @@ void CMap::ClearRockTile(const Vec2i &pos)
}
}



/**
** Regenerate forest.
**
Expand Down Expand Up @@ -700,11 +697,11 @@ void CMap::Insert(CUnit &unit)
unsigned int index = unit.Offset;
const int w = unit.Type->TileWidth;
const int h = unit.Type->TileHeight;
int j, i = h;
int i = h;

do {
CMapField *mf = Field(index);
j = w;
int j = w;
do {
mf->UnitCache.push_back(&unit);
++mf;
Expand All @@ -724,11 +721,11 @@ void CMap::Remove(CUnit &unit)
unsigned int index = unit.Offset;
const int w = unit.Type->TileWidth;
const int h = unit.Type->TileHeight;
int j, i = h;
int i = h;

do {
CMapField *mf = Field(index);
j = w;
int j = w;
do {
ranges::erase(mf->UnitCache, &unit);
++mf;
Expand Down
27 changes: 10 additions & 17 deletions src/map/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
-- Includes
----------------------------------------------------------------------------*/

#include <string.h>

#include "stratagus.h"

#include "minimap.h"
Expand All @@ -49,6 +47,8 @@
#include "unittype.h"
#include "video.h"

#include <vector>

/*----------------------------------------------------------------------------
-- Defines
----------------------------------------------------------------------------*/
Expand All @@ -62,19 +62,16 @@ static constexpr int ATTACK_BLINK_DURATION {7 * CYCLES_PER_SECOND};

static constexpr int SCALE_PRECISION {100};




/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/

SDL_Surface *MinimapSurface{nullptr}; /// generated minimap
SDL_Surface *MinimapSurface{nullptr}; /// generated minimap
static SDL_Surface *MinimapTerrainSurface{nullptr}; /// generated minimap terrain
static SDL_Surface *MinimapFogSurface{nullptr}; /// generated minimap fog of war
static SDL_Surface *MinimapFogSurface{nullptr}; /// generated minimap fog of war

static int *Minimap2MapX; /// fast conversion table
static int *Minimap2MapY; /// fast conversion table
static std::vector<int> Minimap2MapX; /// fast conversion table
static std::vector<int> Minimap2MapY; /// fast conversion table
static int Map2MinimapX[MaxMapWidth]; /// fast conversion table
static int Map2MinimapY[MaxMapHeight]; /// fast conversion table

Expand Down Expand Up @@ -120,10 +117,8 @@ void CMinimap::Create()
//
// Calculate minimap fast lookup tables.
//
Minimap2MapX = new int[W * H];
memset(Minimap2MapX, 0, W * H * sizeof(int));
Minimap2MapY = new int[W * H];
memset(Minimap2MapY, 0, W * H * sizeof(int));
Minimap2MapX.resize(W * H);
Minimap2MapY.resize(W * H);
for (int i = XOffset; i < W - XOffset; ++i) {
Minimap2MapX[i] = ((i - XOffset) * MINIMAP_FAC) / MinimapScaleX;
}
Expand Down Expand Up @@ -500,10 +495,8 @@ void CMinimap::Destroy()
SDL_FreeSurface(MinimapFogSurface);
MinimapFogSurface = nullptr;
}
delete[] Minimap2MapX;
Minimap2MapX = nullptr;
delete[] Minimap2MapY;
Minimap2MapY = nullptr;
Minimap2MapX.clear();
Minimap2MapY.clear();
}

/**
Expand Down
Loading

0 comments on commit dc4c915

Please sign in to comment.