1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

Code style and improved readability

This commit is contained in:
Ewilhan 2019-05-17 09:34:59 +01:00
parent ab1c598d4e
commit 023d6c88fd
3 changed files with 51 additions and 50 deletions

View File

@ -1062,7 +1062,7 @@ bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b)
int as = a->getInitiative(turn), bs = b->getInitiative(turn); int as = a->getInitiative(turn), bs = b->getInitiative(turn);
if(as != bs) if(as != bs)
return as > bs; return as > bs;
else if(as == bs) else
{ {
if(a->unitSide() == b->unitSide()) if(a->unitSide() == b->unitSide())
return a->unitSlot() < b->unitSlot(); return a->unitSlot() < b->unitSlot();
@ -1077,7 +1077,7 @@ bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b)
int as = a->getInitiative(turn), bs = b->getInitiative(turn); int as = a->getInitiative(turn), bs = b->getInitiative(turn);
if(as != bs) if(as != bs)
return as > bs; return as > bs;
else if(as == bs) else
{ {
if(a->unitSide() == b->unitSide()) if(a->unitSide() == b->unitSide())
return a->unitSlot() < b->unitSlot(); return a->unitSlot() < b->unitSlot();
@ -1091,7 +1091,7 @@ bool CMP_stack::operator()(const battle::Unit * a, const battle::Unit * b)
} }
} }
CMP_stack::CMP_stack(int Phase, int Turn, int Side) CMP_stack::CMP_stack(int Phase, int Turn, uint8_t Side)
{ {
phase = Phase; phase = Phase;
turn = Turn; turn = Turn;

View File

@ -144,9 +144,9 @@ class DLL_LINKAGE CMP_stack
{ {
int phase; //rules of which phase will be used int phase; //rules of which phase will be used
int turn; int turn;
bool side; uint8_t side;
public: public:
bool operator ()(const battle::Unit * a, const battle::Unit * b); bool operator ()(const battle::Unit * a, const battle::Unit * b);
CMP_stack(int Phase = 1, int Turn = 0, int Side = BattleSide::ATTACKER); CMP_stack(int Phase = 1, int Turn = 0, uint8_t Side = BattleSide::ATTACKER);
}; };

View File

@ -355,65 +355,66 @@ battle::Units CBattleInfoCallback::battleAliveUnits(ui8 side) const
template <typename T> template <typename T>
const T * takeOneUnit(std::vector<const T*> & all, const int turn, int8_t & lastMoved, int phase) const T * takeOneUnit(std::vector<const T*> & all, const int turn, int8_t & lastMoved, int phase)
{ {
const T * retCreature = nullptr; const T * returnedUnit = nullptr;
size_t fastestIndex = 0; size_t currentUnitIndex = 0;
for(int i = 0; i < all.size(); i++) for(size_t i = 0; i < all.size(); i++)
{ {
int32_t curUnitSpeed = -1; int32_t currentUnitSpeed = -1;
int32_t retUnitSpeed = -1; int32_t returnedUnitSpeed = -1;
if(returnedUnit)
returnedUnitSpeed = returnedUnit->getInitiative(turn);
if(all[i]) if(all[i])
curUnitSpeed = all[i]->getInitiative(turn); {
if(retCreature) currentUnitSpeed = all[i]->getInitiative(turn);
retUnitSpeed = retCreature->getInitiative(turn);
switch (phase) switch (phase)
{ {
case 1: // Faster first, attacker priority, higher slot first case 1: // Faster first, attacker priority, higher slot first
if(all[i] && (retCreature == nullptr || (curUnitSpeed > retUnitSpeed))) if(returnedUnit == nullptr || currentUnitSpeed > returnedUnitSpeed)
{ {
retCreature = all[i]; returnedUnit = all[i];
fastestIndex = i; currentUnitIndex = i;
} }
else if(all[i] && (curUnitSpeed == retUnitSpeed)) else if(currentUnitSpeed == returnedUnitSpeed)
{ {
if(lastMoved == -1 && turn <= 0 && all[i]->unitSide() == BattleSide::ATTACKER if(lastMoved == -1 && turn <= 0 && all[i]->unitSide() == BattleSide::ATTACKER
&& !(retCreature->unitSide() == all[i]->unitSide() && retCreature->unitSlot() < all[i]->unitSlot())) // Turn 0 attacker priority && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Turn 0 attacker priority
{ {
retCreature = all[i]; returnedUnit = all[i];
fastestIndex = i; currentUnitIndex = i;
} }
else if(lastMoved != -1 && all[i]->unitSide() != lastMoved else if(lastMoved != -1 && all[i]->unitSide() != lastMoved
&& !(retCreature->unitSide() == all[i]->unitSide() && retCreature->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
{ {
retCreature = all[i]; returnedUnit = all[i];
fastestIndex = i; currentUnitIndex = i;
} }
} }
break; break;
case 2: // Slower first, higher slot first case 2: // Slower first, higher slot first
case 3: case 3:
if(all[i] && (retCreature == nullptr || (curUnitSpeed < retUnitSpeed))) if(returnedUnit == nullptr || currentUnitSpeed < returnedUnitSpeed)
{ {
retCreature = all[i]; returnedUnit = all[i];
fastestIndex = i; currentUnitIndex = i;
} }
else if(all[i] && curUnitSpeed == retUnitSpeed && lastMoved != -1 && all[i]->unitSide() != lastMoved else if(currentUnitSpeed == returnedUnitSpeed && lastMoved != -1 && all[i]->unitSide() != lastMoved
&& !(retCreature->unitSide() == all[i]->unitSide() && retCreature->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units && !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
{ {
retCreature = all[i]; returnedUnit = all[i];
fastestIndex = i; currentUnitIndex = i;
} }
break; break;
default: default:
break; break;
} }
} }
}
if(!retCreature) if(!returnedUnit)
return nullptr; return nullptr;
all[fastestIndex] = nullptr; all[currentUnitIndex] = nullptr;
return retCreature; return returnedUnit;
} }
void CBattleInfoCallback::battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn, int8_t lastMoved) const void CBattleInfoCallback::battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn, int8_t lastMoved) const