Skip to content

Commit

Permalink
[Clean up] Use unique_ptr for CUnitType and use function to get all…
Browse files Browse the repository at this point in the history
… unittypes.
  • Loading branch information
Jarod42 committed May 12, 2024
1 parent 81dab36 commit 09d44d7
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ static void AiRemoveFromBuilt(PlayerAi &pai, const CUnitType &type)
// This could happen if an upgrade is ready, look for equivalent units.
const auto equivalents = AiFindUnitTypeEquiv(type);
for (int typeIndex : equivalents) {
if (AiRemoveFromBuilt2(pai, *UnitTypes[typeIndex])) {
if (AiRemoveFromBuilt2(pai, *getUnitTypes()[typeIndex])) {
return;
}
}
Expand Down Expand Up @@ -598,7 +598,7 @@ void AiReduceMadeInBuilt(PlayerAi &pai, const CUnitType &type)
const auto equivs = AiFindUnitTypeEquiv(type);

for (int typeIndex : equivs) {
if (AiReduceMadeInBuilt2(pai, *UnitTypes[typeIndex])) {
if (AiReduceMadeInBuilt2(pai, *getUnitTypes()[typeIndex])) {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ai/ai_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ std::vector<int> AiFindAvailableUnitTypeEquiv(const CUnitType &unittype)
auto usableTypes = AiFindUnitTypeEquiv(unittype);
// 2 - Remove unavailable unittypes
ranges::erase_if(usableTypes, [&](int typeIndex) {
return !CheckDependByIdent(*AiPlayer->Player, UnitTypes[typeIndex]->Ident);
return !CheckDependByIdent(*AiPlayer->Player, getUnitTypes()[typeIndex]->Ident);
});
// 3 - Sort by level
ranges::sort(usableTypes, std::greater<>(), [](int index) {
return UnitTypes[index]->MapDefaultStat.Variables[PRIORITY_INDEX].Value;
return getUnitTypes()[index]->MapDefaultStat.Variables[PRIORITY_INDEX].Value;
});
return usableTypes;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ai/ai_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ static bool AiMakeUnit(CUnitType &typeToMake, const Vec2i &nearPos)

// Iterate them
for (int typeIndex : usableTypes) {
CUnitType &type = *UnitTypes[typeIndex];
CUnitType &type = *getUnitTypes()[typeIndex];
//
// Check if we have a place for building or a unit to build.
//
Expand Down Expand Up @@ -932,7 +932,7 @@ static bool AiAssignHarvesterFromUnit(CUnit &unit, int resource)

int exploremask = 0;

for (const CUnitType *type : UnitTypes) {
for (const CUnitType *type : getUnitTypes()) {
if (type && type->GivesResource == resource) {
switch (type->MoveType) {
case EMovement::Land: exploremask |= MapFieldLandUnit; break;
Expand Down
12 changes: 6 additions & 6 deletions src/ai/script_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static std::vector<CUnitType *> getUnitTypeFromString(std::string_view list)
std::vector<CUnitType *> res;

if (list == "*") {
return UnitTypes;
return getUnitTypes();
}
size_t begin = 1;
size_t end = list.find(",", begin);
Expand All @@ -101,7 +101,7 @@ static std::vector<CUnitType *> getReparableUnits()
{
std::vector<CUnitType *> res;

for (CUnitType *type : UnitTypes) {
for (CUnitType *type : getUnitTypes()) {
if (type->RepairHP > 0) {
res.push_back(type);
}
Expand All @@ -119,7 +119,7 @@ static std::vector<CUnitType *> getSupplyUnits()
std::vector<CUnitType *> res;
std::vector<CUnitType *> sorted_res;

for (CUnitType *type : UnitTypes) {
for (CUnitType *type : getUnitTypes()) {
if (type->DefaultStat.Variables[SUPPLY_INDEX].Value > 0) { //supply units are identified as being those with a default stat supply of 1 or more; so if a unit has a supply default stat of 0, but through an upgrade ends up having 1 or more supply, it won't be included here
res.push_back(type);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ static std::vector<CUnitType *> getRefineryUnits()
{
std::vector<CUnitType *> res;

for (CUnitType *type : UnitTypes) {
for (CUnitType *type : getUnitTypes()) {
if (type->GivesResource > 0 && type->BoolFlag[CANHARVEST_INDEX].value) {
res.push_back(type);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ static void InitAiHelper(AiHelper &aiHelper)
AiHelperInsert(aiHelper.Refinery(), i - 1, *type);
}
}
for (CUnitType *type : UnitTypes) {
for (CUnitType *type : getUnitTypes()) {
if (type->CanStore[i] > 0) {
/* HACK : we can't store TIME then use 0 as 1 */
AiHelperInsert(aiHelper.Depots(), i - 1, *type);
Expand Down Expand Up @@ -872,7 +872,7 @@ static int CclAiForce(lua_State *l)
}

// Use the equivalent unittype.
type = UnitTypes[UnitTypeEquivs[type->Slot]];
type = getUnitTypes()[UnitTypeEquivs[type->Slot]];

if (resetForce) {
// Append it.
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ static bool WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain,
}

f->printf("\n-- set map default stat and map sound for unit types\n");
for (const CUnitType *typePtr : UnitTypes) {
for (const CUnitType *typePtr : getUnitTypes()) {
const CUnitType &type = *typePtr;
for (unsigned int j = 0; j < MaxCosts; ++j) {
if (type.MapDefaultStat.Costs[j] != type.DefaultStat.Costs[j]) {
Expand Down
7 changes: 4 additions & 3 deletions src/include/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@
#include "util.h"
#include "vec2i.h"

#include <climits>
#include <vector>
#include <algorithm>
#include <climits>
#include <map>
#include <memory>
#include <vector>

/*----------------------------------------------------------------------------
-- Declarations
Expand Down Expand Up @@ -720,7 +721,7 @@ class CUnitType
-- Variables
----------------------------------------------------------------------------*/

extern std::vector<CUnitType *> UnitTypes; /// All unit-types
const std::vector<CUnitType *> &getUnitTypes(); /// All unit-types

/// @todo this hardcoded unit-types must be removed!!
extern CUnitType *UnitTypeHumanWall; /// Human wall
Expand Down
26 changes: 18 additions & 8 deletions src/network/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,15 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
break;
}
case MessageCommandBuild:
CommandLog("build", &unit, flush, pos.x, pos.y, nullptr, UnitTypes[dstnr]->Ident.c_str(), -1);
CommandBuildBuilding(unit, pos, *UnitTypes[dstnr], flush);
CommandLog("build",
&unit,
flush,
pos.x,
pos.y,
nullptr,
getUnitTypes()[dstnr]->Ident.c_str(),
-1);
CommandBuildBuilding(unit, pos, *getUnitTypes()[dstnr], flush);
break;
case MessageCommandExplore:
CommandLog("explore", &unit, flush, -1, -1, nullptr, nullptr, -1);
Expand Down Expand Up @@ -703,24 +710,27 @@ void ExecCommand(unsigned char msgnr, UnitRef unum,
break;
}
case MessageCommandTrain:
CommandLog("train", &unit, flush, -1, -1, nullptr, UnitTypes[dstnr]->Ident.c_str(), -1);
CommandTrainUnit(unit, *UnitTypes[dstnr], flush);
CommandLog(
"train", &unit, flush, -1, -1, nullptr, getUnitTypes()[dstnr]->Ident.c_str(), -1);
CommandTrainUnit(unit, *getUnitTypes()[dstnr], flush);
break;
case MessageCommandCancelTrain:
// We need (short)x for the last slot -1
if (dstnr != (unsigned short)0xFFFF) {
CommandLog("cancel-train", &unit, EFlushMode::On, -1, -1, nullptr,
UnitTypes[dstnr]->Ident.c_str(), (short)x);
CommandCancelTraining(unit, (short)x, UnitTypes[dstnr]);
getUnitTypes()[dstnr]->Ident.c_str(),
(short) x);
CommandCancelTraining(unit, (short) x, getUnitTypes()[dstnr]);
} else {
CommandLog("cancel-train", &unit, EFlushMode::On, -1, -1, nullptr, nullptr, (short)x);
CommandCancelTraining(unit, (short)x, nullptr);
}
break;
case MessageCommandUpgrade:
CommandLog("upgrade-to", &unit, flush, -1, -1, nullptr,
UnitTypes[dstnr]->Ident.c_str(), -1);
CommandUpgradeTo(unit, *UnitTypes[dstnr], flush);
getUnitTypes()[dstnr]->Ident.c_str(),
-1);
CommandUpgradeTo(unit, *getUnitTypes()[dstnr], flush);
break;
case MessageCommandCancelUpgrade:
CommandLog("cancel-upgrade-to", &unit, EFlushMode::On, -1, -1, nullptr, nullptr, -1);
Expand Down
2 changes: 1 addition & 1 deletion src/sound/unitsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void MapUnitSounds()
return;
}
// Parse all units sounds.
for (CUnitType *typePtr : UnitTypes) {
for (CUnitType *typePtr : getUnitTypes()) {
CUnitType &type = *typePtr;

MapAnimSounds(type);
Expand Down
2 changes: 1 addition & 1 deletion src/spell/script_spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ static int CclDefineSpell(lua_State *l)
} else {
SpellTypeTable.push_back(std::make_unique<SpellType>(SpellTypeTable.size(), std::string{identname}));
spell = SpellTypeTable.back().get();
for (CUnitType *unitType : UnitTypes) { // adjust array for caster already defined
for (CUnitType *unitType : getUnitTypes()) { // adjust array for caster already defined
if (!unitType->CanCastSpell.empty()) {
unitType->CanCastSpell.resize(SpellTypeTable.size());
}
Expand Down
22 changes: 13 additions & 9 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ static void GetPopupSize(const CPopup &popup, const ButtonAction &button,
for (auto &contentPtr : popup.Contents) {
CPopupContentType &content = *contentPtr;

if (CanShowPopupContent(content.Condition.get(), button, UnitTypes[button.Value])) {
if (CanShowPopupContent(content.Condition.get(), button, getUnitTypes()[button.Value])) {
// Automatically write the calculated coordinates.
content.pos.x = contentWidth + content.MarginX;
content.pos.y = popupHeight + content.MarginY;
Expand Down Expand Up @@ -543,9 +543,13 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
case ButtonCmd::Build:
case ButtonCmd::Train:
case ButtonCmd::UpgradeTo:
memcpy(Costs, UnitTypes[button.Value]->Stats[ThisPlayer->Index].Costs,
sizeof(UnitTypes[button.Value]->Stats[ThisPlayer->Index].Costs));
Costs[FoodCost] = UnitTypes[button.Value]->Stats[ThisPlayer->Index].Variables[DEMAND_INDEX].Value;
memcpy(Costs,
getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs,
sizeof(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs));
Costs[FoodCost] = getUnitTypes()[button.Value]
->Stats[ThisPlayer->Index]
.Variables[DEMAND_INDEX]
.Value;
break;
default:
break;
Expand Down Expand Up @@ -575,7 +579,7 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
for (auto &contentPtr : popup.Contents) {
const CPopupContentType &content = *contentPtr;

if (CanShowPopupContent(content.Condition.get(), button, UnitTypes[button.Value])) {
if (CanShowPopupContent(content.Condition.get(), button, getUnitTypes()[button.Value])) {
content.Draw(x + content.pos.x, y + content.pos.y, popup, popupWidth, button, Costs);
}
}
Expand Down Expand Up @@ -801,7 +805,7 @@ void UpdateStatusLineForButton(const ButtonAction &button)
case ButtonCmd::Train:
case ButtonCmd::UpgradeTo: {
// FIXME: store pointer in button table!
const CUnitStats &stats = UnitTypes[button.Value]->Stats[ThisPlayer->Index];
const CUnitStats &stats = getUnitTypes()[button.Value]->Stats[ThisPlayer->Index];
UI.StatusLine.SetCosts(0, stats.Variables[DEMAND_INDEX].Value, stats.Costs);
break;
}
Expand Down Expand Up @@ -1236,7 +1240,7 @@ void CButtonPanel::DoClicked_CancelBuild()
void CButtonPanel::DoClicked_Build(int button)
{
// FIXME: store pointer in button table!
CUnitType &type = *UnitTypes[CurrentButtons[button].Value];
CUnitType &type = *getUnitTypes()[CurrentButtons[button].Value];
if (!Selected[0]->Player->CheckUnitType(type)) {
UI.StatusLine.Set(_("Select Location"));
UI.StatusLine.ClearCosts();
Expand All @@ -1250,7 +1254,7 @@ void CButtonPanel::DoClicked_Build(int button)
void CButtonPanel::DoClicked_Train(int button)
{
// NEW CODE FOR CButtonPanel::DoClicked_Train(int button)
CUnitType &type = *UnitTypes[CurrentButtons[button].Value];
CUnitType &type = *getUnitTypes()[CurrentButtons[button].Value];
int best_training_place = 0;
int lowest_queue = Selected[0]->Orders.size();

Expand Down Expand Up @@ -1285,7 +1289,7 @@ void CButtonPanel::DoClicked_Train(int button)
void CButtonPanel::DoClicked_UpgradeTo(int button)
{
// FIXME: store pointer in button table!
CUnitType &type = *UnitTypes[CurrentButtons[button].Value];
CUnitType &type = *getUnitTypes()[CurrentButtons[button].Value];
const EFlushMode flush = !(KeyModifiers & ModifierShift) ? EFlushMode::On : EFlushMode::Off;
for (CUnit *unit : Selected) {
if (Selected[0]->Player->CheckLimits(type) != ECheckLimit::SpecificUnitLimitReached
Expand Down
6 changes: 3 additions & 3 deletions src/ui/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void CPopupContentTypeLine::Parse(lua_State *l) /* override */
int CPopupContentTypeVariable::GetWidth(const ButtonAction &button, int *) const /* override */
{
CFont &font = this->Font ? *this->Font : GetSmallFont();
TriggerData.Type = UnitTypes[button.Value];
TriggerData.Type = getUnitTypes()[button.Value];
std::string text = EvalString(*this->Text);
TriggerData.Type = nullptr;
return font.getWidth(text);
Expand All @@ -398,7 +398,7 @@ void CPopupContentTypeVariable::Draw(int x,
CLabel label(font, this->TextColor, this->HighlightColor);

if (this->Text) {
TriggerData.Type = UnitTypes[button.Value];
TriggerData.Type = getUnitTypes()[button.Value];
text = EvalString(*this->Text);
TriggerData.Type = nullptr;
if (this->Centered) {
Expand All @@ -409,7 +409,7 @@ void CPopupContentTypeVariable::Draw(int x,
}

if (this->Index != -1) {
CUnitType &type = *UnitTypes[button.Value];
CUnitType &type = *getUnitTypes()[button.Value];
int value = type.DefaultStat.Variables[this->Index].Value;
int diff = type.Stats[ThisPlayer->Index].Variables[this->Index].Value - value;

Expand Down
9 changes: 5 additions & 4 deletions src/unit/script_unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,11 +1532,12 @@ static int CclUnitTypeArray(lua_State *l)

lua_newtable(l);

for (std::vector<CUnitType *>::size_type i = 0; i < UnitTypes.size(); ++i) {
std::size_t i = 1;
for (auto type : getUnitTypes()) {
LuaUserData *data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
data->Type = LuaUnitType;
data->Data = UnitTypes[i];
lua_rawseti(l, 1, i + 1);
data->Data = type;
lua_rawseti(l, 1, i++);
}
return 1;
}
Expand Down Expand Up @@ -1908,7 +1909,7 @@ static int CclDefineBoolFlags(lua_State *l)

if (0 < old && old != UnitTypeVar.GetNumberBoolFlag()) {
size_t new_size = UnitTypeVar.GetNumberBoolFlag();
for (CUnitType *unitType : UnitTypes) { // adjust array for unit already defined
for (CUnitType *unitType : getUnitTypes()) { // adjust array for unit already defined
unitType->BoolFlag.resize(new_size);
}
}
Expand Down
Loading

0 comments on commit 09d44d7

Please sign in to comment.