1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +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);
if(as != bs)
return as > bs;
else if(as == bs)
else
{
if(a->unitSide() == b->unitSide())
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);
if(as != bs)
return as > bs;
else if(as == bs)
else
{
if(a->unitSide() == b->unitSide())
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;
turn = Turn;

View File

@ -144,9 +144,9 @@ class DLL_LINKAGE CMP_stack
{
int phase; //rules of which phase will be used
int turn;
bool side;
uint8_t side;
public:
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>
const T * takeOneUnit(std::vector<const T*> & all, const int turn, int8_t & lastMoved, int phase)
{
const T * retCreature = nullptr;
size_t fastestIndex = 0;
const T * returnedUnit = nullptr;
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 retUnitSpeed = -1;
int32_t currentUnitSpeed = -1;
int32_t returnedUnitSpeed = -1;
if(returnedUnit)
returnedUnitSpeed = returnedUnit->getInitiative(turn);
if(all[i])
curUnitSpeed = all[i]->getInitiative(turn);
if(retCreature)
retUnitSpeed = retCreature->getInitiative(turn);
{
currentUnitSpeed = all[i]->getInitiative(turn);
switch (phase)
{
case 1: // Faster first, attacker priority, higher slot first
if(all[i] && (retCreature == nullptr || (curUnitSpeed > retUnitSpeed)))
if(returnedUnit == nullptr || currentUnitSpeed > returnedUnitSpeed)
{
retCreature = all[i];
fastestIndex = i;
returnedUnit = all[i];
currentUnitIndex = i;
}
else if(all[i] && (curUnitSpeed == retUnitSpeed))
else if(currentUnitSpeed == returnedUnitSpeed)
{
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];
fastestIndex = i;
returnedUnit = all[i];
currentUnitIndex = i;
}
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];
fastestIndex = i;
returnedUnit = all[i];
currentUnitIndex = i;
}
}
break;
case 2: // Slower first, higher slot first
case 3:
if(all[i] && (retCreature == nullptr || (curUnitSpeed < retUnitSpeed)))
if(returnedUnit == nullptr || currentUnitSpeed < returnedUnitSpeed)
{
retCreature = all[i];
fastestIndex = i;
returnedUnit = all[i];
currentUnitIndex = i;
}
else if(all[i] && curUnitSpeed == retUnitSpeed && lastMoved != -1 && all[i]->unitSide() != lastMoved
&& !(retCreature->unitSide() == all[i]->unitSide() && retCreature->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
else if(currentUnitSpeed == returnedUnitSpeed && lastMoved != -1 && all[i]->unitSide() != lastMoved
&& !(returnedUnit->unitSide() == all[i]->unitSide() && returnedUnit->unitSlot() < all[i]->unitSlot())) // Alternate equal speeds units
{
retCreature = all[i];
fastestIndex = i;
returnedUnit = all[i];
currentUnitIndex = i;
}
break;
default:
break;
}
}
}
if(!retCreature)
if(!returnedUnit)
return nullptr;
all[fastestIndex] = nullptr;
return retCreature;
all[currentUnitIndex] = nullptr;
return returnedUnit;
}
void CBattleInfoCallback::battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn, int8_t lastMoved) const