check automatic attacks/spells fewer times to avoid performance penalty of AStar each cycle

This commit is contained in:
Tim Felgentreff 2022-05-24 05:09:58 +02:00
parent 7127bd00ff
commit f252352462
4 changed files with 21 additions and 4 deletions

View file

@ -460,6 +460,13 @@ void COrder_Attack::SetAutoTarget(CUnit &unit, CUnit *target)
**/
bool COrder_Attack::AutoSelectTarget(CUnit &unit)
{
// don't check if we want to switch targets each cycle, once every second is enough
if (Sleep > 0) {
Sleep -= 1;
return true;
}
Sleep = CYCLES_PER_SECOND / 2;
// if unit can't attack, or if unit is not bunkered and removed - exit, no targets
if (unit.Type->CanAttack == false
|| (unit.Removed

View file

@ -374,6 +374,14 @@ bool AutoAttack(CUnit &unit)
}
this->State = SUB_STILL_STANDBY;
this->Finished = (this->Action == UnitActionStill);
if (this->Sleep > 0) {
this->Sleep -= 1;
return;
}
// sleep some time before trying again for automatic actions
this->Sleep = CYCLES_PER_SECOND / 2;
if (this->Action == UnitActionStandGround || unit.Removed || unit.CanMove() == false) {
if (unit.AutoCastSpell) {
this->AutoCastStand(unit);

View file

@ -41,7 +41,7 @@ class COrder_Attack : public COrder
friend COrder *COrder::NewActionAttackGround(const CUnit &attacker, const Vec2i &dest);
public:
explicit COrder_Attack(bool ground) : COrder(ground ? UnitActionAttackGround : UnitActionAttack),
State(0), MinRange(0), Range(0), SkirmishRange(0), offeredTarget(NULL), goalPos(-1, -1), attackMovePos(-1, -1) {}
State(0), MinRange(0), Range(0), SkirmishRange(0), offeredTarget(NULL), goalPos(-1, -1), attackMovePos(-1, -1), Sleep(0) {}
virtual COrder_Attack *Clone() const { return new COrder_Attack(*this); }
@ -84,6 +84,7 @@ private:
CUnitPtr offeredTarget; // Stores pointer to target offered from outside (f.e. by HitUnit_AttackBack() event).
Vec2i goalPos; // Current goal position
Vec2i attackMovePos; // If attack-move was ordered
unsigned char Sleep;
};
//@}

View file

@ -37,7 +37,7 @@
class COrder_Still : public COrder
{
public:
explicit COrder_Still(bool stand) : COrder(stand ? UnitActionStandGround : UnitActionStill), State(0) {}
explicit COrder_Still(bool stand) : COrder(stand ? UnitActionStandGround : UnitActionStill), State(0), Sleep(0) {}
virtual COrder_Still *Clone() const { return new COrder_Still(*this); }
@ -53,8 +53,9 @@ public:
private:
bool AutoAttackStand(CUnit &unit);
bool AutoCastStand(CUnit &unit);
private:
int State;
unsigned char State;
unsigned char Sleep;
};
//@}