diff --git a/src/game/game.cpp b/src/game/game.cpp index 238a0d0af6..ce7dec9c61 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -74,6 +74,7 @@ #include "version.h" #include "video.h" +#include #include extern void CleanGame(); @@ -99,7 +100,6 @@ bool UseHPForXp = false; /// true if gain XP by dealing damage, fal ----------------------------------------------------------------------------*/ extern gcn::Gui *Gui; -static std::vector Containers; /** ** Save game settings. @@ -120,14 +120,11 @@ void CreateGame(const fs::path &filename, CMap *map); void StartMap(const std::string &filename, bool clean) { - std::string nc, rc; - gcn::Widget *oldTop = Gui->getTop(); - gcn::Container *container = new gcn::Container(); - Containers.push_back(container); + auto container = std::make_unique(); container->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height)); container->setOpaque(false); - Gui->setTop(container); + Gui->setTop(container.get()); NetConnectRunning = 0; InterfaceState = IfaceState::Normal; @@ -137,6 +134,7 @@ void StartMap(const std::string &filename, bool clean) if (clean) { CleanPlayers(); } + std::string nc, rc; GetDefaultTextColors(nc, rc); CreateGame(filename, &Map); @@ -156,15 +154,6 @@ void StartMap(const std::string &filename, bool clean) SetDefaultTextColors(nc, rc); Gui->setTop(oldTop); - ranges::erase(Containers, container); - delete container; -} - -void FreeAllContainers() -{ - for (auto *p : Containers) { - delete p; - } } /*---------------------------------------------------------------------------- diff --git a/src/include/game.h b/src/include/game.h index 8b873653f8..088b8a46f4 100644 --- a/src/include/game.h +++ b/src/include/game.h @@ -45,8 +45,6 @@ extern void LuaRegisterModules(); /// Register lua script of each modules extern void LoadModules(); /// Load all modules extern void CleanModules(); /// Cleanup all modules -extern void FreeAllContainers(); - extern void SaveGameSettings(CFile &file); /// Save game settings extern std::string GameName; /// Name of the game diff --git a/src/include/iolib.h b/src/include/iolib.h index 0629409bb2..8387d25c12 100644 --- a/src/include/iolib.h +++ b/src/include/iolib.h @@ -107,6 +107,8 @@ class CFile public: CFile(); ~CFile(); + CFile(const CFile &) = delete; + const CFile &operator = (const CFile &) = delete; int open(const char *name, long flags); int close(); @@ -117,9 +119,6 @@ class CFile static SDL_RWops *to_SDL_RWops(std::unique_ptr file); int printf(const char *format, ...) PRINTF_VAARG_ATTRIBUTE(2, 3); // Don't forget to count this -private: - CFile(const CFile &rhs); // No implementation - const CFile &operator = (const CFile &rhs); // No implementation private: class PImpl; PImpl *pimpl; diff --git a/src/include/spells.h b/src/include/spells.h index 3695525e7c..38d92553f1 100644 --- a/src/include/spells.h +++ b/src/include/spells.h @@ -41,6 +41,7 @@ #include "unitsound.h" #include "vec2i.h" +#include #include #include @@ -237,7 +238,7 @@ class SpellType /** ** Define the names and effects of all available spells. */ -extern std::vector SpellTypeTable; +extern std::vector> SpellTypeTable; /*---------------------------------------------------------------------------- diff --git a/src/include/upgrade.h b/src/include/upgrade.h index 8c9befdd2c..af11228e96 100644 --- a/src/include/upgrade.h +++ b/src/include/upgrade.h @@ -33,6 +33,8 @@ //@{ +#include + /*---------------------------------------------------------------------------- -- Declarations ----------------------------------------------------------------------------*/ @@ -50,7 +52,7 @@ class CUpgradeModifier; /// How many upgrades modifiers supported #define UPGRADE_MODIFIERS_MAX (UpgradeMax * 4) -extern CUpgradeModifier *UpgradeModifiers[UPGRADE_MODIFIERS_MAX]; +extern std::unique_ptr UpgradeModifiers[UPGRADE_MODIFIERS_MAX]; extern int NumUpgradeModifiers; @@ -88,7 +90,7 @@ extern void UpgradeLost(CPlayer &player, int id); /// Apply researched upgrades when map is loading extern void ApplyUpgrades(); -extern void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier *um); /// Apply upgrade modifier of an individual upgrade +extern void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier &um); /// Apply upgrade modifier of an individual upgrade extern void IndividualUpgradeAcquire(CUnit &unit, const CUpgrade *upgrade); /// Make a unit acquire in individual upgrade extern void IndividualUpgradeLost(CUnit &unit, const CUpgrade *upgrade); /// Make a unit lose in individual upgrade diff --git a/src/include/upgrade_structs.h b/src/include/upgrade_structs.h index a245441014..46fda77928 100644 --- a/src/include/upgrade_structs.h +++ b/src/include/upgrade_structs.h @@ -112,7 +112,7 @@ class CUnitStats CUnitStats() = default; ~CUnitStats() = default; - CUnitStats &operator=(const CUnitStats &rhs); + CUnitStats &operator=(const CUnitStats &) = default; bool operator == (const CUnitStats &rhs) const; bool operator != (const CUnitStats &rhs) const; diff --git a/src/spell/script_spell.cpp b/src/spell/script_spell.cpp index 7f67bb963c..f3c8ca46ac 100644 --- a/src/spell/script_spell.cpp +++ b/src/spell/script_spell.cpp @@ -324,19 +324,19 @@ static int CclDefineSpell(lua_State *l) const auto it = ranges::find(SpellTypeTable, identname, &SpellType::Ident); SpellType *spell = nullptr; if (it != SpellTypeTable.end()) { - spell = *it; + spell = it->get(); DebugPrint("Redefining spell-type '%s'\n", identname.data()); } else { - spell = new SpellType(SpellTypeTable.size(), std::string{identname}); + SpellTypeTable.push_back(std::make_unique(SpellTypeTable.size(), std::string{identname})); + spell = SpellTypeTable.back().get(); for (CUnitType *unitType : UnitTypes) { // adjust array for caster already defined if (!unitType->CanCastSpell.empty()) { - unitType->CanCastSpell.resize(SpellTypeTable.size() + 1); + unitType->CanCastSpell.resize(SpellTypeTable.size()); } if (!unitType->AutoCastActive.empty()) { - unitType->AutoCastActive.resize(SpellTypeTable.size() + 1); + unitType->AutoCastActive.resize(SpellTypeTable.size()); } } - SpellTypeTable.push_back(spell); } for (int i = 1; i < args; ++i) { std::string_view value = LuaToString(l, i + 1); diff --git a/src/spell/spells.cpp b/src/spell/spells.cpp index a1bd7c4639..d64844ab46 100644 --- a/src/spell/spells.cpp +++ b/src/spell/spells.cpp @@ -61,7 +61,7 @@ /** ** Define the names and effects of all im play available spells. */ -std::vector SpellTypeTable; +std::vector> SpellTypeTable; /*---------------------------------------------------------------------------- @@ -561,9 +561,6 @@ int SpellCast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i void CleanSpells() { DebugPrint("Cleaning spells.\n"); - for (SpellType *spell : SpellTypeTable) { - delete spell; - } SpellTypeTable.clear(); } diff --git a/src/stratagus/iolib.cpp b/src/stratagus/iolib.cpp index 1ddfec5f3e..192bacf208 100644 --- a/src/stratagus/iolib.cpp +++ b/src/stratagus/iolib.cpp @@ -71,6 +71,8 @@ class CFile::PImpl public: PImpl(); ~PImpl(); + PImpl(const PImpl &) = delete; + const PImpl &operator=(const PImpl &) = delete; int open(const char *name, long flags); int close(); @@ -80,10 +82,6 @@ class CFile::PImpl long tell(); int write(const void *buf, size_t len); -private: - PImpl(const PImpl &rhs); // No implementation - const PImpl &operator = (const PImpl &rhs); // No implementation - private: ClfType cl_type; /// type of CFile FILE *cl_plain; /// standard file pointer diff --git a/src/stratagus/stratagus.cpp b/src/stratagus/stratagus.cpp index fd029a4d0f..c963c3b79a 100644 --- a/src/stratagus/stratagus.cpp +++ b/src/stratagus/stratagus.cpp @@ -409,7 +409,6 @@ void Exit(int err) FreeGraphics(); FreePlayerColors(); FreeButtonStyles(); - FreeAllContainers(); freeGuichan(); fprintf(stdout, "Frames %lu, Slow frames %ld = %ld%%\n", FrameCounter, SlowFrameCounter, diff --git a/src/ui/mouse.cpp b/src/ui/mouse.cpp index 724207bf98..dea480fbf6 100644 --- a/src/ui/mouse.cpp +++ b/src/ui/mouse.cpp @@ -1438,7 +1438,7 @@ static int SendSpellCast(const Vec2i &tilePos) continue; } // CursorValue here holds the spell type id - const SpellType *spell = SpellTypeTable[CursorValue]; + const SpellType *spell = SpellTypeTable.at(CursorValue).get(); if (!spell) { fprintf(stderr, "unknown spell-id: %d\n", CursorValue); ExitFatal(1); diff --git a/src/ui/popup.cpp b/src/ui/popup.cpp index 8cccd7393f..6a69cb031c 100644 --- a/src/ui/popup.cpp +++ b/src/ui/popup.cpp @@ -248,9 +248,9 @@ int CPopupContentTypeCosts::GetWidth(const ButtonAction &button, int *Costs) con } if (Costs[ManaResCost]) { const CGraphic *G = UI.Resources[ManaResCost].G; - const SpellType *spell = SpellTypeTable[button.Value]; + const SpellType &spell = *SpellTypeTable.at(button.Value); - if (spell->ManaCost) { + if (spell.ManaCost) { popupWidth = 10; if (UI.Resources[ManaResCost].IconWidth != -1) { popupWidth += (UI.Resources[ManaResCost].IconWidth + 5); @@ -259,8 +259,8 @@ int CPopupContentTypeCosts::GetWidth(const ButtonAction &button, int *Costs) con popupWidth += (G->Width + 5); } } - popupWidth += font.Width(spell->ManaCost); - popupWidth = std::max(popupWidth, font.Width(spell->Name) + 10); + popupWidth += font.Width(spell.ManaCost); + popupWidth = std::max(popupWidth, font.Width(spell.Name) + 10); } else { popupWidth = font.Width(button.Hint) + 10; } diff --git a/src/unit/unit.cpp b/src/unit/unit.cpp index 28a5051cf4..d18069aadb 100644 --- a/src/unit/unit.cpp +++ b/src/unit/unit.cpp @@ -1883,7 +1883,7 @@ void CUnit::ChangeOwner(CPlayer &newplayer) //apply the upgrades of the new player, if the old one doesn't have that upgrade for (int z = 0; z < NumUpgradeModifiers; ++z) { if (oldplayer->Allow.Upgrades[UpgradeModifiers[z]->UpgradeId] != 'R' && newplayer.Allow.Upgrades[UpgradeModifiers[z]->UpgradeId] == 'R' && UpgradeModifiers[z]->ApplyTo[Type->Slot] == 'X') { //if the old player doesn't have the modifier's upgrade, and the upgrade is applicable to the unit - ApplyIndividualUpgradeModifier(*this, UpgradeModifiers[z]); //apply the upgrade to this unit only + ApplyIndividualUpgradeModifier(*this, *UpgradeModifiers[z]); //apply the upgrade to this unit only } } diff --git a/src/unit/upgrade.cpp b/src/unit/upgrade.cpp index 6d17a9981c..0135bdaf56 100644 --- a/src/unit/upgrade.cpp +++ b/src/unit/upgrade.cpp @@ -68,7 +68,7 @@ static void AllowUnitId(CPlayer &player, int id, int units); std::vector AllUpgrades; /// The main user useable upgrades /// Upgrades modifiers -CUpgradeModifier *UpgradeModifiers[UPGRADE_MODIFIERS_MAX]; +std::unique_ptr UpgradeModifiers[UPGRADE_MODIFIERS_MAX]; /// Number of upgrades modifiers used int NumUpgradeModifiers; @@ -78,17 +78,6 @@ static std::map> Upgrades; -- Functions ----------------------------------------------------------------------------*/ -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]; - } - this->Variables = rhs.Variables; - return *this; -} - bool CUnitStats::operator == (const CUnitStats &rhs) const { for (int i = 0; i != MaxCosts; ++i) { @@ -102,10 +91,8 @@ bool CUnitStats::operator == (const CUnitStats &rhs) const return false; } } - for (unsigned int i = 0; i != UnitTypeVar.GetNumberVariable(); ++i) { - if (this->Variables[i] != rhs.Variables[i]) { - return false; - } + if (this->Variables != rhs.Variables) { + return false; } return true; } @@ -178,8 +165,8 @@ void CleanUpgrades() // // Free the upgrade modifiers. // - for (int i = 0; i < NumUpgradeModifiers; ++i) { - delete UpgradeModifiers[i]; + for (auto &p : UpgradeModifiers) { + p.reset(); } NumUpgradeModifiers = 0; } @@ -234,7 +221,7 @@ static int CclDefineModifier(lua_State *l) { const int args = lua_gettop(l); - CUpgradeModifier *um = new CUpgradeModifier; + auto um = std::make_unique(); memset(um->ChangeUpgrades, '?', sizeof(um->ChangeUpgrades)); memset(um->ApplyTo, '?', sizeof(um->ApplyTo)); @@ -339,7 +326,7 @@ static int CclDefineModifier(lua_State *l) } } - UpgradeModifiers[NumUpgradeModifiers++] = um; + UpgradeModifiers[NumUpgradeModifiers++] = std::move(um); return 0; } @@ -492,10 +479,8 @@ static void ConvertUnitTypeTo(CPlayer &player, const CUnitType &src, CUnitType & ** @param player Player that get all the upgrades. ** @param um Upgrade modifier that do the effects */ -static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) +static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier &um) { - Assert(um); - int pn = player.Index; for (int z = 0; z < UpgradeMax; ++z) { @@ -504,14 +489,14 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) // FIXME: check if modify is allowed if (player.Allow.Upgrades[z] != 'R') { - if (um->ChangeUpgrades[z] == 'A') { + if (um.ChangeUpgrades[z] == 'A') { player.Allow.Upgrades[z] = 'A'; } - if (um->ChangeUpgrades[z] == 'F') { + if (um.ChangeUpgrades[z] == 'F') { player.Allow.Upgrades[z] = 'F'; } // we can even have upgrade acquired w/o costs - if (um->ChangeUpgrades[z] == 'R') { + if (um.ChangeUpgrades[z] == 'R') { player.Allow.Upgrades[z] = 'R'; } } @@ -523,59 +508,59 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) // FIXME: check if modify is allowed - player.Allow.Units[z] += um->ChangeUnits[z]; + player.Allow.Units[z] += um.ChangeUnits[z]; - Assert(um->ApplyTo[z] == '?' || um->ApplyTo[z] == 'X'); + Assert(um.ApplyTo[z] == '?' || um.ApplyTo[z] == 'X'); // this modifier should be applied to unittype id == z - if (um->ApplyTo[z] == 'X') { + if (um.ApplyTo[z] == 'X') { // If Sight range is upgraded, we need to change EVERY unit // to the new range, otherwise the counters get confused. - if (um->Modifier.Variables[SIGHTRANGE_INDEX].Value) { + if (um.Modifier.Variables[SIGHTRANGE_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && !unit->Removed) { MapUnmarkUnitSight(*unit); unit->CurrentSightRange = stat.Variables[SIGHTRANGE_INDEX].Max - + um->Modifier.Variables[SIGHTRANGE_INDEX].Value; + + um.Modifier.Variables[SIGHTRANGE_INDEX].Value; MapMarkUnitSight(*unit); } } } // if a unit type's supply is changed, we need to update the player's supply accordingly - if (um->Modifier.Variables[SUPPLY_INDEX].Value) { + if (um.Modifier.Variables[SUPPLY_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && unit->IsAlive()) { - unit->Player->Supply += um->Modifier.Variables[SUPPLY_INDEX].Value; + unit->Player->Supply += um.Modifier.Variables[SUPPLY_INDEX].Value; } } } // if a unit type's demand is changed, we need to update the player's demand accordingly - if (um->Modifier.Variables[DEMAND_INDEX].Value) { + if (um.Modifier.Variables[DEMAND_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && unit->IsAlive()) { - unit->Player->Demand += um->Modifier.Variables[DEMAND_INDEX].Value; + unit->Player->Demand += um.Modifier.Variables[DEMAND_INDEX].Value; } } } // upgrade costs :) for (unsigned int j = 0; j < MaxCosts; ++j) { - stat.Costs[j] += um->Modifier.Costs[j]; - stat.Storing[j] += um->Modifier.Storing[j]; - if (um->Modifier.ImproveIncomes[j]) { + stat.Costs[j] += um.Modifier.Costs[j]; + stat.Storing[j] += um.Modifier.Storing[j]; + if (um.Modifier.ImproveIncomes[j]) { if (!stat.ImproveIncomes[j]) { - stat.ImproveIncomes[j] += DefaultIncomes[j] + um->Modifier.ImproveIncomes[j]; + stat.ImproveIncomes[j] += DefaultIncomes[j] + um.Modifier.ImproveIncomes[j]; } else { - stat.ImproveIncomes[j] += um->Modifier.ImproveIncomes[j]; + stat.ImproveIncomes[j] += um.Modifier.ImproveIncomes[j]; } //update player's income std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); @@ -587,21 +572,21 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) int varModified = 0; for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - varModified |= um->Modifier.Variables[j].Value - | um->Modifier.Variables[j].Max - | um->Modifier.Variables[j].Increase - | um->Modifier.Variables[j].IncreaseFrequency - | um->Modifier.Variables[j].Enable - | um->ModifyPercent[j]; - stat.Variables[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - stat.Variables[j].Value += stat.Variables[j].Value * um->ModifyPercent[j] / 100; - stat.Variables[j].Max += stat.Variables[j].Max * um->ModifyPercent[j] / 100; + varModified |= um.Modifier.Variables[j].Value + | um.Modifier.Variables[j].Max + | um.Modifier.Variables[j].Increase + | um.Modifier.Variables[j].IncreaseFrequency + | um.Modifier.Variables[j].Enable + | um.ModifyPercent[j]; + stat.Variables[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + stat.Variables[j].Value += stat.Variables[j].Value * um.ModifyPercent[j] / 100; + stat.Variables[j].Max += stat.Variables[j].Max * um.ModifyPercent[j] / 100; } else { - stat.Variables[j].Value += um->Modifier.Variables[j].Value; - stat.Variables[j].Max += um->Modifier.Variables[j].Max; - stat.Variables[j].Increase += um->Modifier.Variables[j].Increase; - stat.Variables[j].IncreaseFrequency += um->Modifier.Variables[j].IncreaseFrequency; + stat.Variables[j].Value += um.Modifier.Variables[j].Value; + stat.Variables[j].Max += um.Modifier.Variables[j].Max; + stat.Variables[j].Increase += um.Modifier.Variables[j].Increase; + stat.Variables[j].IncreaseFrequency += um.Modifier.Variables[j].IncreaseFrequency; } stat.Variables[j].Max = std::max(stat.Variables[j].Max, 0); @@ -619,17 +604,17 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) continue; } for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - unit.Variable[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - unit.Variable[j].Value += unit.Variable[j].Value * um->ModifyPercent[j] / 100; - unit.Variable[j].Max += unit.Variable[j].Max * um->ModifyPercent[j] / 100; + unit.Variable[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + unit.Variable[j].Value += unit.Variable[j].Value * um.ModifyPercent[j] / 100; + unit.Variable[j].Max += unit.Variable[j].Max * um.ModifyPercent[j] / 100; } else { - unit.Variable[j].Value += um->Modifier.Variables[j].Value; - unit.Variable[j].Increase += um->Modifier.Variables[j].Increase; - unit.Variable[j].IncreaseFrequency += um->Modifier.Variables[j].IncreaseFrequency; + unit.Variable[j].Value += um.Modifier.Variables[j].Value; + unit.Variable[j].Increase += um.Modifier.Variables[j].Increase; + unit.Variable[j].IncreaseFrequency += um.Modifier.Variables[j].IncreaseFrequency; } - unit.Variable[j].Max += um->Modifier.Variables[j].Max; + unit.Variable[j].Max += um.Modifier.Variables[j].Max; unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0); if (unit.Variable[j].Max > 0) { clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max); @@ -637,8 +622,8 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) } } } - if (um->ConvertTo) { - ConvertUnitTypeTo(player, *UnitTypes[z], *um->ConvertTo); + if (um.ConvertTo) { + ConvertUnitTypeTo(player, *UnitTypes[z], *um.ConvertTo); } } } @@ -653,14 +638,12 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) ** @param player Player that get all the upgrades. ** @param um Upgrade modifier that do the effects */ -static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) +static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier &um) { - Assert(um); - int pn = player.Index; - if (um->SpeedResearch != 0) { - player.SpeedResearch -= um->SpeedResearch; + if (um.SpeedResearch != 0) { + player.SpeedResearch -= um.SpeedResearch; } for (int z = 0; z < UpgradeMax; ++z) { @@ -669,14 +652,14 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) // FIXME: check if modify is allowed if (player.Allow.Upgrades[z] != 'R') { - if (um->ChangeUpgrades[z] == 'A') { + if (um.ChangeUpgrades[z] == 'A') { player.Allow.Upgrades[z] = 'F'; } - if (um->ChangeUpgrades[z] == 'F') { + if (um.ChangeUpgrades[z] == 'F') { player.Allow.Upgrades[z] = 'A'; } // we can even have upgrade acquired w/o costs - if (um->ChangeUpgrades[z] == 'R') { + if (um.ChangeUpgrades[z] == 'R') { player.Allow.Upgrades[z] = 'A'; } } @@ -688,57 +671,57 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) // FIXME: check if modify is allowed - player.Allow.Units[z] -= um->ChangeUnits[z]; + player.Allow.Units[z] -= um.ChangeUnits[z]; - Assert(um->ApplyTo[z] == '?' || um->ApplyTo[z] == 'X'); + Assert(um.ApplyTo[z] == '?' || um.ApplyTo[z] == 'X'); // this modifier should be applied to unittype id == z - if (um->ApplyTo[z] == 'X') { + if (um.ApplyTo[z] == 'X') { // If Sight range is upgraded, we need to change EVERY unit // to the new range, otherwise the counters get confused. - if (um->Modifier.Variables[SIGHTRANGE_INDEX].Value) { + if (um.Modifier.Variables[SIGHTRANGE_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && !unit->Removed) { MapUnmarkUnitSight(*unit); - unit->CurrentSightRange = stat.Variables[SIGHTRANGE_INDEX].Max - - um->Modifier.Variables[SIGHTRANGE_INDEX].Value; + unit->CurrentSightRange = stat.Variables[SIGHTRANGE_INDEX].Max + - um.Modifier.Variables[SIGHTRANGE_INDEX].Value; MapMarkUnitSight(*unit); } } } // if a unit type's supply is changed, we need to update the player's supply accordingly - if (um->Modifier.Variables[SUPPLY_INDEX].Value) { + if (um.Modifier.Variables[SUPPLY_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && unit->IsAlive()) { - unit->Player->Supply -= um->Modifier.Variables[SUPPLY_INDEX].Value; + unit->Player->Supply -= um.Modifier.Variables[SUPPLY_INDEX].Value; } } } // if a unit type's demand is changed, we need to update the player's demand accordingly - if (um->Modifier.Variables[DEMAND_INDEX].Value) { + if (um.Modifier.Variables[DEMAND_INDEX].Value) { std::vector unitupgrade = FindUnitsByType(*UnitTypes[z]); for (CUnit *unit : unitupgrade) { if (unit->Player->Index == pn && unit->IsAlive()) { - unit->Player->Demand -= um->Modifier.Variables[DEMAND_INDEX].Value; + unit->Player->Demand -= um.Modifier.Variables[DEMAND_INDEX].Value; } } } // upgrade costs :) for (unsigned int j = 0; j < MaxCosts; ++j) { - stat.Costs[j] -= um->Modifier.Costs[j]; - stat.Storing[j] -= um->Modifier.Storing[j]; - stat.ImproveIncomes[j] -= um->Modifier.ImproveIncomes[j]; + stat.Costs[j] -= um.Modifier.Costs[j]; + stat.Storing[j] -= um.Modifier.Storing[j]; + stat.ImproveIncomes[j] -= um.Modifier.ImproveIncomes[j]; //if this was the highest improve income, search for another - if (player.Incomes[j] && (stat.ImproveIncomes[j] + um->Modifier.ImproveIncomes[j]) == player.Incomes[j]) { + if (player.Incomes[j] && (stat.ImproveIncomes[j] + um.Modifier.ImproveIncomes[j]) == player.Incomes[j]) { int m = DefaultIncomes[j]; for (const CUnit* unit : player.GetUnits()) { @@ -750,19 +733,19 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) int varModified = 0; for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - varModified |= um->Modifier.Variables[j].Value - | um->Modifier.Variables[j].Max - | um->Modifier.Variables[j].Increase - | um->Modifier.Variables[j].Enable - | um->ModifyPercent[j]; - stat.Variables[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - stat.Variables[j].Value = stat.Variables[j].Value * 100 / (100 + um->ModifyPercent[j]); - stat.Variables[j].Max = stat.Variables[j].Max * 100 / (100 + um->ModifyPercent[j]); + varModified |= um.Modifier.Variables[j].Value + | um.Modifier.Variables[j].Max + | um.Modifier.Variables[j].Increase + | um.Modifier.Variables[j].Enable + | um.ModifyPercent[j]; + stat.Variables[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + stat.Variables[j].Value = stat.Variables[j].Value * 100 / (100 + um.ModifyPercent[j]); + stat.Variables[j].Max = stat.Variables[j].Max * 100 / (100 + um.ModifyPercent[j]); } else { - stat.Variables[j].Value -= um->Modifier.Variables[j].Value; - stat.Variables[j].Max -= um->Modifier.Variables[j].Max; - stat.Variables[j].Increase -= um->Modifier.Variables[j].Increase; + stat.Variables[j].Value -= um.Modifier.Variables[j].Value; + stat.Variables[j].Max -= um.Modifier.Variables[j].Max; + stat.Variables[j].Increase -= um.Modifier.Variables[j].Increase; } stat.Variables[j].Max = std::max(stat.Variables[j].Max, 0); @@ -780,24 +763,24 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) continue; } for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - unit.Variable[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - unit.Variable[j].Value = unit.Variable[j].Value * 100 / (100 + um->ModifyPercent[j]); - unit.Variable[j].Max = unit.Variable[j].Max * 100 / (100 + um->ModifyPercent[j]); + unit.Variable[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + unit.Variable[j].Value = unit.Variable[j].Value * 100 / (100 + um.ModifyPercent[j]); + unit.Variable[j].Max = unit.Variable[j].Max * 100 / (100 + um.ModifyPercent[j]); } else { - unit.Variable[j].Value -= um->Modifier.Variables[j].Value; - unit.Variable[j].Increase -= um->Modifier.Variables[j].Increase; + unit.Variable[j].Value -= um.Modifier.Variables[j].Value; + unit.Variable[j].Increase -= um.Modifier.Variables[j].Increase; } - unit.Variable[j].Max -= um->Modifier.Variables[j].Max; + unit.Variable[j].Max -= um.Modifier.Variables[j].Max; unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0); clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max); } } } - if (um->ConvertTo) { - ConvertUnitTypeTo(player, *um->ConvertTo, *UnitTypes[z]); + if (um.ConvertTo) { + ConvertUnitTypeTo(player, *um.ConvertTo, *UnitTypes[z]); } } } @@ -809,65 +792,61 @@ static void RemoveUpgradeModifier(CPlayer &player, const CUpgradeModifier *um) ** @param unit Unit that will get the modifier applied ** @param um Upgrade modifier that does the effects */ -void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier *um) +void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier &um) { - Assert(um); - - if (um->Modifier.Variables[SIGHTRANGE_INDEX].Value) { + if (um.Modifier.Variables[SIGHTRANGE_INDEX].Value) { if (!unit.Removed) { MapUnmarkUnitSight(unit); - unit.CurrentSightRange = unit.Variable[SIGHTRANGE_INDEX].Value + - um->Modifier.Variables[SIGHTRANGE_INDEX].Value; + unit.CurrentSightRange = unit.Variable[SIGHTRANGE_INDEX].Value + + um.Modifier.Variables[SIGHTRANGE_INDEX].Value; UpdateUnitSightRange(unit); MapMarkUnitSight(unit); } } for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - unit.Variable[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - unit.Variable[j].Value += unit.Variable[j].Value * um->ModifyPercent[j] / 100; - unit.Variable[j].Max += unit.Variable[j].Max * um->ModifyPercent[j] / 100; + unit.Variable[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + unit.Variable[j].Value += unit.Variable[j].Value * um.ModifyPercent[j] / 100; + unit.Variable[j].Max += unit.Variable[j].Max * um.ModifyPercent[j] / 100; } else { - unit.Variable[j].Value += um->Modifier.Variables[j].Value; - unit.Variable[j].Increase += um->Modifier.Variables[j].Increase; - unit.Variable[j].IncreaseFrequency += um->Modifier.Variables[j].IncreaseFrequency; + unit.Variable[j].Value += um.Modifier.Variables[j].Value; + unit.Variable[j].Increase += um.Modifier.Variables[j].Increase; + unit.Variable[j].IncreaseFrequency += um.Modifier.Variables[j].IncreaseFrequency; } - unit.Variable[j].Max += um->Modifier.Variables[j].Max; + unit.Variable[j].Max += um.Modifier.Variables[j].Max; unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0); if (unit.Variable[j].Max > 0) { clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max); } } - if (um->ConvertTo) { - CommandTransformIntoType(unit, *um->ConvertTo); + if (um.ConvertTo) { + CommandTransformIntoType(unit, *um.ConvertTo); } } -static void RemoveIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier *um) +static void RemoveIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier &um) { - Assert(um); - - if (um->Modifier.Variables[SIGHTRANGE_INDEX].Value) { + if (um.Modifier.Variables[SIGHTRANGE_INDEX].Value) { if (!unit.Removed) { MapUnmarkUnitSight(unit); - unit.CurrentSightRange = unit.Variable[SIGHTRANGE_INDEX].Value - - um->Modifier.Variables[SIGHTRANGE_INDEX].Value; + unit.CurrentSightRange = unit.Variable[SIGHTRANGE_INDEX].Value + - um.Modifier.Variables[SIGHTRANGE_INDEX].Value; UpdateUnitSightRange(unit); MapMarkUnitSight(unit); } } for (unsigned int j = 0; j < UnitTypeVar.GetNumberVariable(); j++) { - unit.Variable[j].Enable |= um->Modifier.Variables[j].Enable; - if (um->ModifyPercent[j]) { - unit.Variable[j].Value = unit.Variable[j].Value * 100 / (100 + um->ModifyPercent[j]); - unit.Variable[j].Max = unit.Variable[j].Max * 100 / (100 + um->ModifyPercent[j]); + unit.Variable[j].Enable |= um.Modifier.Variables[j].Enable; + if (um.ModifyPercent[j]) { + unit.Variable[j].Value = unit.Variable[j].Value * 100 / (100 + um.ModifyPercent[j]); + unit.Variable[j].Max = unit.Variable[j].Max * 100 / (100 + um.ModifyPercent[j]); } else { - unit.Variable[j].Value -= um->Modifier.Variables[j].Value; - unit.Variable[j].Increase -= um->Modifier.Variables[j].Increase; + unit.Variable[j].Value -= um.Modifier.Variables[j].Value; + unit.Variable[j].Increase -= um.Modifier.Variables[j].Increase; } - unit.Variable[j].Max -= um->Modifier.Variables[j].Max; + unit.Variable[j].Max -= um.Modifier.Variables[j].Max; unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0); if (unit.Variable[j].Max > 0) { clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max); @@ -889,7 +868,7 @@ void UpgradeAcquire(CPlayer &player, const CUpgrade *upgrade) for (int z = 0; z < NumUpgradeModifiers; ++z) { if (UpgradeModifiers[z]->UpgradeId == id) { - ApplyUpgradeModifier(player, UpgradeModifiers[z]); + ApplyUpgradeModifier(player, *UpgradeModifiers[z]); } } @@ -914,7 +893,7 @@ void UpgradeLost(CPlayer &player, int id) for (int z = 0; z < NumUpgradeModifiers; ++z) { if (UpgradeModifiers[z]->UpgradeId == id) { - RemoveUpgradeModifier(player, UpgradeModifiers[z]); + RemoveUpgradeModifier(player, *UpgradeModifiers[z]); } } @@ -944,7 +923,7 @@ void ApplyUpgrades() for (int z = 0; z < NumUpgradeModifiers; ++z) { if (UpgradeModifiers[z]->UpgradeId == id) { - ApplyUpgradeModifier(Players[p], UpgradeModifiers[z]); + ApplyUpgradeModifier(Players[p], *UpgradeModifiers[z]); } } } @@ -961,7 +940,7 @@ void IndividualUpgradeAcquire(CUnit &unit, const CUpgrade *upgrade) for (int z = 0; z < NumUpgradeModifiers; ++z) { if (UpgradeModifiers[z]->UpgradeId == id) { - ApplyIndividualUpgradeModifier(unit, UpgradeModifiers[z]); + ApplyIndividualUpgradeModifier(unit, *UpgradeModifiers[z]); } } @@ -981,7 +960,7 @@ void IndividualUpgradeLost(CUnit &unit, const CUpgrade *upgrade) for (int z = 0; z < NumUpgradeModifiers; ++z) { if (UpgradeModifiers[z]->UpgradeId == id) { - RemoveIndividualUpgradeModifier(unit, UpgradeModifiers[z]); + RemoveIndividualUpgradeModifier(unit, *UpgradeModifiers[z]); } }