Skip to content

Commit

Permalink
Replace some iterator usages by for-range or algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 8, 2023
1 parent eeae371 commit f7fc73f
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 122 deletions.
80 changes: 35 additions & 45 deletions src/ai/script_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ static std::vector<CUnitType *> getReparableUnits()
{
std::vector<CUnitType *> res;

for (std::vector<CUnitType *>::const_iterator i = UnitTypes.begin(); i != UnitTypes.end(); ++i) {
CUnitType &type = **i;

if (type.RepairHP > 0) {
res.push_back(&type);
for (CUnitType *type : UnitTypes) {
if (type->RepairHP > 0) {
res.push_back(type);
}
}
return res;
Expand All @@ -122,29 +120,26 @@ static std::vector<CUnitType *> getSupplyUnits()
std::vector<CUnitType *> res;
std::vector<CUnitType *> sorted_res;

for (std::vector<CUnitType *>::const_iterator i = UnitTypes.begin(); i != UnitTypes.end(); ++i) {
CUnitType &type = **i;

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);
for (CUnitType *type : UnitTypes) {
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);
}
}
// Now, sort them, best first.
while (!res.empty()) {
float bestscore = 0;
CUnitType *besttype = nullptr;

for (std::vector<CUnitType *>::const_iterator i = res.begin(); i != res.end(); ++i) {
CUnitType &type = **i;
for (CUnitType *type : res) {
unsigned int cost = 0;

for (unsigned j = 0; j < MaxCosts; ++j) {
cost += type.DefaultStat.Costs[j]; //this cannot be MapDefaultStat because this function is called when the AiHelper is defined, rather than when a game is started
cost += type->DefaultStat.Costs[j]; //this cannot be MapDefaultStat because this function is called when the AiHelper is defined, rather than when a game is started
}
const float score = ((float) type.DefaultStat.Variables[SUPPLY_INDEX].Value) / cost;
const float score = ((float) type->DefaultStat.Variables[SUPPLY_INDEX].Value) / cost;
if (score > bestscore) {
bestscore = score;
besttype = &type;
besttype = type;
}
}
sorted_res.push_back(besttype);
Expand All @@ -165,11 +160,9 @@ static std::vector<CUnitType *> getRefineryUnits()
{
std::vector<CUnitType *> res;

for (std::vector<CUnitType *>::const_iterator i = UnitTypes.begin(); i != UnitTypes.end(); ++i) {
CUnitType &type = **i;

if (type.GivesResource > 0 && type.BoolFlag[CANHARVEST_INDEX].value) {
res.push_back(&type);
for (CUnitType *type : UnitTypes) {
if (type->GivesResource > 0 && type->BoolFlag[CANHARVEST_INDEX].value) {
res.push_back(type);
}
}
#if 0
Expand All @@ -180,8 +173,7 @@ static std::vector<CUnitType *> getRefineryUnits()
CUnitType *besttype;

bestscore = 0;
for (std::vector<CUnitType *>::const_iterator i = res.begin(); i != res.end(); ++i) {
CUnitType *type = *i;
for (CUnitType *type : res) {
float score;
unsigned int cost = 0;

Expand Down Expand Up @@ -222,23 +214,21 @@ static void InitAiHelper(AiHelper &aiHelper)
std::vector<CUnitType *> supplyUnits = getSupplyUnits();
std::vector<CUnitType *> mineUnits = getRefineryUnits();

for (std::vector<CUnitType *>::const_iterator i = supplyUnits.begin(); i != supplyUnits.end(); ++i) {
AiHelperInsert(aiHelper.UnitLimit(), 0, **i);
for (CUnitType *type : supplyUnits) {
AiHelperInsert(aiHelper.UnitLimit(), 0, *type);
}

for (int i = 1; i < MaxCosts; ++i) {
for (std::vector<CUnitType *>::const_iterator j = mineUnits.begin(); j != mineUnits.end(); ++j) {
if ((*j)->GivesResource == i) {
for (CUnitType *type : mineUnits) {
if (type->GivesResource == i) {
/* HACK : we can't mine TIME then use 0 as 1 */
AiHelperInsert(aiHelper.Refinery(), i - 1, **j);
AiHelperInsert(aiHelper.Refinery(), i - 1, *type);
}
}
for (std::vector<CUnitType *>::const_iterator d = UnitTypes.begin(); d != UnitTypes.end(); ++d) {
CUnitType &type = **d;

if (type.CanStore[i] > 0) {
for (CUnitType *type : UnitTypes) {
if (type->CanStore[i] > 0) {
/* HACK : we can't store TIME then use 0 as 1 */
AiHelperInsert(aiHelper.Depots(), i - 1, type);
AiHelperInsert(aiHelper.Depots(), i - 1, *type);
}
}
}
Expand All @@ -249,46 +239,46 @@ static void InitAiHelper(AiHelper &aiHelper)

switch (button.Action) {
case ButtonRepair :
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
for (std::vector<CUnitType *>::const_iterator k = reparableUnits.begin(); k != reparableUnits.end(); ++k) {
AiHelperInsert(aiHelper.Repair(), (*k)->Slot, **j);
for (CUnitType *type : unitmask) {
for (CUnitType *reparableUnit : reparableUnits) {
AiHelperInsert(aiHelper.Repair(), reparableUnit->Slot, *type);
}
}
break;
case ButtonBuild: {
CUnitType *buildingType = UnitTypeByIdent(button.ValueStr);

for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
AiHelperInsert(aiHelper.Build(), buildingType->Slot, (**j));
for (CUnitType *type : unitmask) {
AiHelperInsert(aiHelper.Build(), buildingType->Slot, *type);
}
break;
}
case ButtonTrain : {
CUnitType *trainingType = UnitTypeByIdent(button.ValueStr);

for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
AiHelperInsert(aiHelper.Train(), trainingType->Slot, (**j));
for (CUnitType *type : unitmask) {
AiHelperInsert(aiHelper.Train(), trainingType->Slot, *type);
}
break;
}
case ButtonUpgradeTo : {
CUnitType *upgradeToType = UnitTypeByIdent(button.ValueStr);

for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
AiHelperInsert(aiHelper.Upgrade(), upgradeToType->Slot, **j);
for (CUnitType *type : unitmask) {
AiHelperInsert(aiHelper.Upgrade(), upgradeToType->Slot, *type);
}
break;
}
case ButtonResearch : {
int researchId = UpgradeIdByIdent(button.ValueStr);

if (button.Allowed == ButtonCheckSingleResearch) {
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
AiHelperInsert(aiHelper.SingleResearch(), researchId, **j);
for (CUnitType *type : unitmask) {
AiHelperInsert(aiHelper.SingleResearch(), researchId, *type);
}
} else {
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) {
AiHelperInsert(aiHelper.Research(), researchId, **j);
for (CUnitType *type : unitmask) {
AiHelperInsert(aiHelper.Research(), researchId, *type);
}
}
break;
Expand Down
4 changes: 1 addition & 3 deletions src/animation/animation_luacallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
Assert(cb);

cb->pushPreamble();
for (std::vector<std::string>::const_iterator it = cbArgs.begin(); it != cbArgs.end(); ++it) {
const std::string str = *it;

for (const std::string &str : cbArgs) {
const int arg = ParseAnimInt(unit, str.c_str());
cb->pushInteger(arg);
}
Expand Down
10 changes: 4 additions & 6 deletions src/include/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,15 @@ class CBuildRestrictionAnd : public CBuildRestriction
public:
virtual ~CBuildRestrictionAnd()
{
for (std::vector<CBuildRestriction *>::const_iterator i = _or_list.begin();
i != _or_list.end(); ++i) {
delete *i;
for (CBuildRestriction *p : _or_list) {
delete p;
}
_or_list.clear();
}
virtual void Init()
{
for (std::vector<CBuildRestriction *>::const_iterator i = _or_list.begin();
i != _or_list.end(); ++i) {
(*i)->Init();
for (CBuildRestriction *restriction : _or_list) {
restriction->Init();
}
}
virtual bool Check(const CUnit *builder, const CUnitType &type, const Vec2i &pos, CUnit *&ontoptarget) const;
Expand Down
31 changes: 12 additions & 19 deletions src/missile/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,8 @@ static void MissileHitsWall(const Missile &missile, const Vec2i &tilePos, int sp

static bool IsPiercedUnit(const Missile &missile, const CUnit &unit)
{
for (std::vector<CUnit *>::const_iterator it = missile.PiercedUnits.begin();
it != missile.PiercedUnits.end(); ++it) {
CUnit &punit = **it;
if (UnitNumber(unit) == UnitNumber(punit)) {
for (const CUnit *punit : missile.PiercedUnits) {
if (UnitNumber(unit) == UnitNumber(*punit)) {
return true;
}
}
Expand All @@ -922,9 +920,8 @@ void Missile::MissileHit(CUnit *unit)
// The impact generates a new missile.
//
if (mtype.Impact.empty() == false) {
for (std::vector<MissileConfig *>::const_iterator it = mtype.Impact.begin(); it != mtype.Impact.end(); ++it) {
const MissileConfig &mc = **it;
Missile *impact = MakeMissile(*mc.Missile, pixelPos, pixelPos);
for (MissileConfig *mc : mtype.Impact) {
Missile *impact = MakeMissile(*mc->Missile, pixelPos, pixelPos);
if (impact && impact->Type->Damage) {
impact->SourceUnit = this->SourceUnit;
}
Expand Down Expand Up @@ -1288,13 +1285,11 @@ void SaveMissiles(CFile &file)
file.printf("\n--- -----------------------------------------\n");
file.printf("--- MODULE: missiles\n\n");

std::vector<Missile *>::const_iterator i;

for (i = GlobalMissiles.begin(); i != GlobalMissiles.end(); ++i) {
(*i)->SaveMissile(file);
for (const Missile *missile : GlobalMissiles) {
missile->SaveMissile(file);
}
for (i = LocalMissiles.begin(); i != LocalMissiles.end(); ++i) {
(*i)->SaveMissile(file);
for (const Missile *missile : LocalMissiles) {
missile->SaveMissile(file);
}
}

Expand Down Expand Up @@ -1384,14 +1379,12 @@ Missile::~Missile()
*/
void CleanMissiles()
{
std::vector<Missile *>::const_iterator i;

for (i = GlobalMissiles.begin(); i != GlobalMissiles.end(); ++i) {
delete *i;
for (Missile *missile : GlobalMissiles) {
delete missile;
}
GlobalMissiles.clear();
for (i = LocalMissiles.begin(); i != LocalMissiles.end(); ++i) {
delete *i;
for (Missile *missile : LocalMissiles) {
delete missile;
}
LocalMissiles.clear();
}
Expand Down
7 changes: 3 additions & 4 deletions src/spell/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,11 @@ int SpellCast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i
PlayGameSound(spell.SoundWhenCast.Sound, CalculateVolume(false, ViewPointDistance(target ? target->tilePos : goalPos), spell.SoundWhenCast.Sound->Range));
}
}
for (std::vector<SpellActionType *>::const_iterator act = spell.Action.begin();
act != spell.Action.end(); ++act) {
if ((*act)->ModifyManaCaster) {
for (SpellActionType *act : spell.Action) {
if (act->ModifyManaCaster) {
mustSubtractMana = false;
}
cont = cont & (*act)->Cast(caster, spell, target, pos);
cont = cont & act->Cast(caster, spell, target, pos);
}
if (mustSubtractMana) {
caster.Variable[MANA_INDEX].Value -= spell.ManaCost;
Expand Down
8 changes: 3 additions & 5 deletions src/stratagus/construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ void CleanConstructions()
*/
CConstruction *ConstructionByIdent(const std::string &name)
{
for (std::vector<CConstruction *>::const_iterator it = Constructions.begin();
it != Constructions.end();
++it) {
if ((*it)->Ident == name) {
return *it;
for (CConstruction *c : Constructions) {
if (c->Ident == name) {
return c;
}
}
return nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/stratagus/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2354,11 +2354,11 @@ static bool LuaValueToString(lua_State *l, std::string &value)
if ((s.find('\n') != std::string::npos)) {
value = std::string("[[") + s + "]]";
} else {
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
if (*it == '\"') {
for (char c : s) {
if (c == '\"') {
value.push_back('\\');
}
value.push_back(*it);
value.push_back(c);
}
value = std::string("\"") + value + "\"";
}
Expand Down
11 changes: 4 additions & 7 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,8 @@ static void GetPopupSize(const CPopup &popup, const ButtonAction &button,
popupWidth = popup.MarginX;
popupHeight = popup.MarginY;

for (std::vector<CPopupContentType *>::const_iterator it = popup.Contents.begin();
it != popup.Contents.end();
++it) {
CPopupContentType &content = **it;
for (CPopupContentType *contentPtr : popup.Contents) {
CPopupContentType &content = *contentPtr;

if (CanShowPopupContent(content.Condition, button, UnitTypes[button.Value])) {
// Automatically write the calculated coordinates.
Expand Down Expand Up @@ -602,9 +600,8 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
Video.DrawRectangle(popup->BorderColor, x, y, popupWidth, popupHeight);

// Contents
for (std::vector<CPopupContentType *>::const_iterator it = popup->Contents.begin();
it != popup->Contents.end(); ++it) {
const CPopupContentType &content = **it;
for (CPopupContentType *contentPtr : popup->Contents) {
const CPopupContentType &content = *contentPtr;

if (CanShowPopupContent(content.Condition, button, UnitTypes[button.Value])) {
content.Draw(x + content.pos.x, y + content.pos.y, *popup, popupWidth, button, Costs);
Expand Down
7 changes: 3 additions & 4 deletions src/ui/mainscr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,9 @@ static void DrawUnitInfo(CUnit &unit)
UpdateUnitVariables(unit);
for (size_t i = 0; i != UI.InfoPanelContents.size(); ++i) {
if (CanShowContent(UI.InfoPanelContents[i]->Condition, unit)) {
for (std::vector<CContentType *>::const_iterator content = UI.InfoPanelContents[i]->Contents.begin();
content != UI.InfoPanelContents[i]->Contents.end(); ++content) {
if (CanShowContent((*content)->Condition, unit)) {
(*content)->Draw(unit, UI.InfoPanelContents[i]->DefaultFont);
for (CContentType *content : UI.InfoPanelContents[i]->Contents) {
if (CanShowContent(content->Condition, unit)) {
content->Draw(unit, UI.InfoPanelContents[i]->DefaultFont);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/ui/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,8 @@ void UIHandleMouseMove(const PixelPos &cursorPos)

// We now need to check if there are another build commands on this build spot
bool buildable = true;
for (std::vector<COrderPtr>::const_iterator it = unit.Orders.begin();
it != unit.Orders.end(); ++it) {
COrder &order = **it;
for (COrderPtr orderPtr : unit.Orders) {
COrder &order = *orderPtr;
if (order.Action == UnitActionBuild) {
COrder_Build &build = dynamic_cast<COrder_Build &>(order);
if (tilePos.x >= build.GetGoalPos().x
Expand Down
15 changes: 7 additions & 8 deletions src/unit/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@
CBuildRestrictionOnTop *OnTopDetails(const CUnit &unit, const CUnitType *parent)
{

for (std::vector<CBuildRestriction *>::const_iterator i = unit.Type->BuildingRules.begin();
i != unit.Type->BuildingRules.end(); ++i) {
CBuildRestrictionOnTop *ontopb = dynamic_cast<CBuildRestrictionOnTop *>(*i);
for (CBuildRestriction *p : unit.Type->BuildingRules) {
CBuildRestrictionOnTop *ontopb = dynamic_cast<CBuildRestrictionOnTop *>(p);

if (ontopb) {
if (!parent) {
Expand All @@ -76,11 +75,11 @@ CBuildRestrictionOnTop *OnTopDetails(const CUnit &unit, const CUnitType *parent)
}
continue;
}
CBuildRestrictionAnd *andb = dynamic_cast<CBuildRestrictionAnd *>(*i);
CBuildRestrictionAnd *andb = dynamic_cast<CBuildRestrictionAnd *>(p);

if (andb) {
for (std::vector<CBuildRestriction *>::iterator j = andb->_or_list.begin(); j != andb->_or_list.end(); ++j) {
CBuildRestrictionOnTop *ontopb = dynamic_cast<CBuildRestrictionOnTop *>(*j);
for (CBuildRestriction *orRestriction : andb->_or_list) {
CBuildRestrictionOnTop *ontopb = dynamic_cast<CBuildRestrictionOnTop *>(orRestriction);
if (ontopb) {
if (!parent) {
// Guess this is right
Expand All @@ -101,8 +100,8 @@ CBuildRestrictionOnTop *OnTopDetails(const CUnit &unit, const CUnitType *parent)
*/
bool CBuildRestrictionAnd::Check(const CUnit *builder, const CUnitType &type, const Vec2i &pos, CUnit *&ontoptarget) const
{
for (std::vector<CBuildRestriction *>::const_iterator i = _or_list.begin(); i != _or_list.end(); ++i) {
if (!(*i)->Check(builder, type, pos, ontoptarget)) {
for (CBuildRestriction *restriction : _or_list) {
if (!restriction->Check(builder, type, pos, ontoptarget)) {
return false;
}
}
Expand Down
Loading

0 comments on commit f7fc73f

Please sign in to comment.