Skip to content

Commit

Permalink
Turn CUnitStats::Variables into std::vector<CVariable>.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Oct 6, 2023
1 parent 1d3f3cf commit dd7275e
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/action/action_die.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void COrder_Die::Execute(CUnit &unit) /* override */

unit.Remove(nullptr);
unit.Type = &corpseType;
unit.Stats = &corpseType.Stats[unit.Player->Index];
unit.Stats = const_cast<CUnitStats *>(&corpseType.Stats[unit.Player->Index]);
UpdateUnitSightRange(unit);
unit.Place(unit.tilePos);

Expand Down
2 changes: 1 addition & 1 deletion src/action/action_upgradeto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static int TransformUnitIntoType(CUnit &unit, const CUnitType &newtype)
}

unit.Type = const_cast<CUnitType *>(&newtype);
unit.Stats = &unit.Type->Stats[player.Index];
unit.Stats = const_cast<CUnitStats *>(&unit.Type->Stats[player.Index]);

if (!newtype.CanCastSpell.empty() && !unit.AutoCastSpell) {
unit.AutoCastSpell = new char[SpellTypeTable.size()];
Expand Down
2 changes: 1 addition & 1 deletion src/include/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class CUnit

const CUnitType *Type = nullptr; /// Pointer to unit-type (peon,...)
CPlayer *Player = nullptr; /// Owner of this unit
const CUnitStats *Stats = nullptr; /// Current unit stats
CUnitStats *Stats = nullptr; /// Current unit stats
int CurrentSightRange; /// Unit's Current Sight Range

// Pathfinding stuff:
Expand Down
4 changes: 2 additions & 2 deletions src/include/upgrade_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ class CUnitStats
{
public:
CUnitStats() = default;
~CUnitStats();
~CUnitStats() = default;

CUnitStats &operator=(const CUnitStats &rhs);

bool operator == (const CUnitStats &rhs) const;
bool operator != (const CUnitStats &rhs) const;
public:
CVariable *Variables = nullptr; /// user defined variable.
std::vector<CVariable> Variables; /// user defined variable.
int Costs[MaxCosts]{}; /// current costs of the unit
int Storing[MaxCosts]{}; /// storage increasing
int ImproveIncomes[MaxCosts]{}; /// Gives player an improved income
Expand Down
4 changes: 2 additions & 2 deletions src/unit/script_unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,8 @@ static int CclDefineUnitStats(lua_State *l)
Assert(playerId < PlayerMax);

CUnitStats *stats = &type.Stats[playerId];
if (!stats->Variables) {
stats->Variables = new CVariable[UnitTypeVar.GetNumberVariable()];
if (stats->Variables.empty()) {
stats->Variables.resize(UnitTypeVar.GetNumberVariable());
}

// Parse the list: (still everything could be changed!)
Expand Down
10 changes: 5 additions & 5 deletions src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ void CUnit::Init(const CUnitType &type)
Assert(!Variable);
const unsigned int size = UnitTypeVar.GetNumberVariable();
Variable = new CVariable[size];
std::copy(type.MapDefaultStat.Variables, type.MapDefaultStat.Variables + size, Variable);
std::copy(type.MapDefaultStat.Variables.begin(), type.MapDefaultStat.Variables.end(), Variable);
} else {
Variable = nullptr;
}
Expand Down Expand Up @@ -746,12 +746,12 @@ void CUnit::AssignToPlayer(CPlayer &player)
}
}
Player = &player;
Stats = &type.Stats[Player->Index];
Stats = const_cast<CUnitStats *>(&type.Stats[Player->Index]);
if (!SaveGameLoading) {
if (UnitTypeVar.GetNumberVariable()) {
Assert(Variable);
Assert(Stats->Variables);
memcpy(Variable, Stats->Variables, UnitTypeVar.GetNumberVariable() * sizeof(*Variable));
Assert(Stats->Variables.size() == UnitTypeVar.GetNumberVariable());
std::copy(Stats->Variables.begin(), Stats->Variables.end(), Variable);
}
}
}
Expand Down Expand Up @@ -1870,7 +1870,7 @@ void CUnit::ChangeOwner(CPlayer &newplayer)

MapUnmarkUnitSight(*this);
newplayer.AddUnit(*this);
Stats = &Type->Stats[newplayer.Index];
Stats = const_cast<CUnitStats *>(&Type->Stats[newplayer.Index]);
UpdateUnitSightRange(*this);
MapMarkUnitSight(*this);

Expand Down
6 changes: 2 additions & 4 deletions src/unit/unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,8 @@ std::pair<CUnitType *, bool> NewUnitTypeSlot(std::string_view ident)
type->Ident = ident;
type->BoolFlag.resize(new_bool_size);

type->DefaultStat.Variables = new CVariable[UnitTypeVar.GetNumberVariable()];
for (unsigned int i = 0; i < UnitTypeVar.GetNumberVariable(); ++i) {
type->DefaultStat.Variables[i] = UnitTypeVar.Variable[i];
}
type->DefaultStat.Variables = UnitTypeVar.Variable;

UnitTypes.push_back(type);
UnitTypeMap[type->Ident] = type;
return {type, false};
Expand Down
14 changes: 2 additions & 12 deletions src/unit/upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,14 @@ static std::map<std::string, CUpgrade *, std::less<>> Upgrades;
-- Functions
----------------------------------------------------------------------------*/


CUnitStats::~CUnitStats()
{
delete [] this->Variables;
}

CUnitStats &CUnitStats::operator = (const CUnitStats &rhs)
{
for (unsigned int i = 0; i < MaxCosts; ++i) {
this->Costs[i] = rhs.Costs[i];
this->Storing[i] = rhs.Storing[i];
this->ImproveIncomes[i] = rhs.ImproveIncomes[i];
}
delete [] this->Variables;
const unsigned int size = UnitTypeVar.GetNumberVariable();
this->Variables = new CVariable[size];

std::copy(rhs.Variables, rhs.Variables + size, this->Variables);
this->Variables = rhs.Variables;
return *this;
}

Expand Down Expand Up @@ -248,7 +238,7 @@ static int CclDefineModifier(lua_State *l)

memset(um->ChangeUpgrades, '?', sizeof(um->ChangeUpgrades));
memset(um->ApplyTo, '?', sizeof(um->ApplyTo));
um->Modifier.Variables = new CVariable[UnitTypeVar.GetNumberVariable()];
um->Modifier.Variables.resize(UnitTypeVar.GetNumberVariable());
um->ModifyPercent.resize(UnitTypeVar.GetNumberVariable());

std::string_view upgrade_ident = LuaToString(l, 1);
Expand Down

0 comments on commit dd7275e

Please sign in to comment.