Skip to content

Commit fc8f962

Browse files
author
rt
committed
fix #5825 (addendum to ab61e51)
reformat and refactor (some of) the pile of garbage that is MobileCAI
1 parent b69ca09 commit fc8f962

File tree

5 files changed

+501
-440
lines changed

5 files changed

+501
-440
lines changed

rts/Sim/Units/CommandAI/CommandAI.cpp

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ CR_REG_METADATA(CCommandAI, (
7373
CR_MEMBER(inCommand),
7474
CR_MEMBER(repeatOrders),
7575
CR_MEMBER(lastSelectedCommandPage),
76-
CR_MEMBER(unimportantMove),
7776
CR_MEMBER(commandDeathDependences),
7877
CR_MEMBER(targetLostTimer)
7978
))
@@ -89,7 +88,6 @@ CCommandAI::CCommandAI():
8988
inCommand(false),
9089
repeatOrders(false),
9190
lastSelectedCommandPage(0),
92-
unimportantMove(false),
9391
targetLostTimer(TARGET_LOST_TIMER)
9492
{}
9593

@@ -104,7 +102,6 @@ CCommandAI::CCommandAI(CUnit* owner):
104102
inCommand(false),
105103
repeatOrders(false),
106104
lastSelectedCommandPage(0),
107-
unimportantMove(false),
108105
targetLostTimer(TARGET_LOST_TIMER)
109106
{
110107
{
@@ -1029,7 +1026,7 @@ void CCommandAI::GiveWaitCommand(const Command& c)
10291026
StopMove();
10301027
inCommand = false;
10311028
targetDied = false;
1032-
unimportantMove = false;
1029+
10331030
commandQue.push_front(c);
10341031
return;
10351032
}
@@ -1131,8 +1128,8 @@ void CCommandAI::ExecuteInsert(const Command& c, bool fromSynced)
11311128
if (!queue->empty() && (insertIt == queue->begin())) {
11321129
inCommand = false;
11331130
targetDied = false;
1134-
unimportantMove = false;
1135-
SetOrderTarget(NULL);
1131+
1132+
SetOrderTarget(nullptr);
11361133
const Command& cmd = queue->front();
11371134
eoh->CommandFinished(*owner, cmd);
11381135
eventHandler.UnitCmdDone(owner, cmd);
@@ -1581,39 +1578,40 @@ void CCommandAI::FinishCommand()
15811578
assert(!commandQue.empty());
15821579

15831580
const Command cmd = commandQue.front(); //cppcheck false positive, copy is needed here
1581+
15841582
const bool dontRepeat = (cmd.options & INTERNAL_ORDER);
1583+
const bool pushCommand = (cmd.GetID() != CMD_STOP && cmd.GetID() != CMD_PATROL && cmd.GetID() != CMD_SET_WANTED_MAX_SPEED);
15851584

1586-
if (repeatOrders
1587-
&& !dontRepeat
1588-
&& (cmd.GetID() != CMD_STOP)
1589-
&& (cmd.GetID() != CMD_PATROL)
1590-
&& (cmd.GetID() != CMD_SET_WANTED_MAX_SPEED)){
1585+
if (repeatOrders && !dontRepeat && pushCommand)
15911586
commandQue.push_back(cmd);
1592-
}
15931587

15941588
commandQue.pop_front();
1589+
15951590
inCommand = false;
15961591
targetDied = false;
1597-
unimportantMove = false;
1598-
SetOrderTarget(NULL);
1592+
1593+
SetOrderTarget(nullptr);
15991594
eoh->CommandFinished(*owner, cmd);
16001595
eventHandler.UnitCmdDone(owner, cmd);
16011596
ClearTargetLock(cmd);
16021597

16031598
if (commandQue.empty()) {
1604-
if (!owner->group) {
1599+
if (owner->group == nullptr)
16051600
eoh->UnitIdle(*owner);
1606-
}
1601+
16071602
eventHandler.UnitIdle(owner);
16081603
}
16091604

16101605
// avoid infinite loops
1611-
if (lastFinishCommand != gs->frameNum) {
1612-
lastFinishCommand = gs->frameNum;
1613-
if (!owner->IsStunned()) {
1614-
SlowUpdate();
1615-
}
1616-
}
1606+
if (lastFinishCommand == gs->frameNum)
1607+
return;
1608+
1609+
lastFinishCommand = gs->frameNum;
1610+
1611+
if (owner->IsStunned())
1612+
return;
1613+
1614+
SlowUpdate();
16171615
}
16181616

16191617
void CCommandAI::AddStockpileWeapon(CWeapon* weapon)
@@ -1692,13 +1690,15 @@ void CCommandAI::WeaponFired(CWeapon* weapon, const bool searchForNewTarget)
16921690
}
16931691
}
16941692

1695-
// when this fails, we need to take a copy at top instead of a reference
1693+
// if this fails, we need to take a copy at top instead of a reference
16961694
assert(&c == &commandQue.front());
16971695

16981696
eoh->WeaponFired(*owner, *(weapon->weaponDef));
1699-
if (orderFinished) {
1700-
FinishCommand();
1701-
}
1697+
1698+
if (!orderFinished)
1699+
return;
1700+
1701+
FinishCommand();
17021702
}
17031703

17041704
void CCommandAI::PushOrUpdateReturnFight(const float3& cmdPos1, const float3& cmdPos2)
@@ -1750,7 +1750,7 @@ bool CCommandAI::HasMoreMoveCommands(bool skipFirstCmd) const
17501750
bool CCommandAI::SkipParalyzeTarget(const CUnit* target)
17511751
{
17521752
// check to see if we are about to paralyze a unit that is already paralyzed
1753-
if ((target == NULL) || (owner->weapons.empty()))
1753+
if ((target == nullptr) || (owner->weapons.empty()))
17541754
return false;
17551755

17561756
const CWeapon* w = owner->weapons.front();
@@ -1765,31 +1765,33 @@ bool CCommandAI::SkipParalyzeTarget(const CUnit* target)
17651765
return false;
17661766
}
17671767

1768-
bool CCommandAI::CanChangeFireState() {
1769-
const UnitDef* ud = owner->unitDef;
1770-
const bool b = (!ud->weapons.empty() || ud->canKamikaze || ud->IsFactoryUnit());
1771-
1772-
return (ud->canFireControl && b);
1773-
}
1768+
bool CCommandAI::CanChangeFireState() { return (owner->unitDef->CanChangeFireState()); }
17741769

17751770

17761771
void CCommandAI::StopAttackingAllyTeam(int ally)
17771772
{
17781773
std::vector<int> todel;
17791774

17801775
// erasing in the middle invalidates all iterators
1781-
for (CCommandQueue::iterator it = commandQue.begin(); it != commandQue.end(); ++it) {
1776+
for (auto it = commandQue.begin(); it != commandQue.end(); ++it) {
17821777
const Command& c = *it;
17831778

1784-
if ((c.GetID() == CMD_FIGHT || c.GetID() == CMD_ATTACK) && c.params.size() == 1) {
1785-
const CUnit* target = unitHandler->GetUnit(c.params[0]);
1779+
if (c.params.size() != 1)
1780+
continue;
1781+
if (c.GetID() != CMD_FIGHT && c.GetID() != CMD_ATTACK)
1782+
continue;
17861783

1787-
if (target && target->allyteam == ally) {
1788-
todel.push_back(it - commandQue.begin());
1789-
}
1790-
}
1784+
const CUnit* target = unitHandler->GetUnit(c.params[0]);
1785+
1786+
if (target == nullptr)
1787+
continue;
1788+
if (target->allyteam != ally)
1789+
continue;
1790+
1791+
todel.push_back(it - commandQue.begin());
17911792
}
1792-
for (std::vector<int>::reverse_iterator it = todel.rbegin(); it != todel.rend(); ++it) {
1793+
1794+
for (auto it = todel.rbegin(); it != todel.rend(); ++it) {
17931795
commandQue.erase(commandQue.begin() + *it);
17941796
}
17951797
}

rts/Sim/Units/CommandAI/CommandAI.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class CCommandAI : public CObject
130130
bool inCommand;
131131
bool repeatOrders;
132132
int lastSelectedCommandPage;
133-
bool unimportantMove;
134133

135134
protected:
136135
// return true by default so non-AirCAI's trigger FinishCommand

0 commit comments

Comments
 (0)