Skip to content

Commit

Permalink
store vector of attackers rather than a bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Warboy1982 committed Aug 18, 2014
1 parent 8d7e21b commit 0f60ee3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
18 changes: 11 additions & 7 deletions src/Battlescape/AlienBAIState.cpp
Expand Up @@ -52,7 +52,7 @@ namespace OpenXcom
*/
AlienBAIState::AlienBAIState(SavedBattleGame *save, BattleUnit *unit, Node *node) : BattleAIState(save, unit), _aggroTarget(0), _knownEnemies(0), _visibleEnemies(0), _spottingEnemies(0),
_escapeTUs(0), _ambushTUs(0), _reserveTUs(0), _rifle(false), _melee(false), _blaster(false),
_wasHit(false), _didPsi(false), _AIMode(AI_PATROL), _closestDist(100), _fromNode(node), _toNode(0)
_didPsi(false), _AIMode(AI_PATROL), _closestDist(100), _fromNode(node), _toNode(0)
{
_traceAI = Options::traceAI;

Expand Down Expand Up @@ -87,7 +87,7 @@ void AlienBAIState::load(const YAML::Node &node)
fromNodeID = node["fromNode"].as<int>(-1);
toNodeID = node["toNode"].as<int>(-1);
_AIMode = node["AIMode"].as<int>(0);
_wasHit = node["wasHit"].as<bool>(false);
_wasHitBy = node["wasHitBy"].as<std::vector<int> >(_wasHitBy);
if (fromNodeID != -1)
{
_fromNode = _save->getNodes()->at(fromNodeID);
Expand All @@ -114,7 +114,7 @@ YAML::Node AlienBAIState::save() const
node["fromNode"] = fromNodeID;
node["toNode"] = toNodeID;
node["AIMode"] = _AIMode;
node["wasHit"] = _wasHit;
node["wasHitBy"] = _wasHitBy;
return node;
}

Expand Down Expand Up @@ -157,6 +157,8 @@ void AlienBAIState::think(BattleAction *action)
_rifle = false;
_blaster = false;
_reachable = _save->getPathfinding()->findReachable(_unit, _unit->getTimeUnits());
_wasHitBy.clear();

if(_unit->getCharging() && _unit->getCharging()->isOut())
{
_unit->setCharging(0);
Expand Down Expand Up @@ -390,21 +392,23 @@ void AlienBAIState::think(BattleAction *action)
}
}


/*
* sets the "was hit" flag to true.
*/
void AlienBAIState::setWasHit()
void AlienBAIState::setWasHitBy(BattleUnit *attacker)
{
_wasHit = true;
if (attacker->getFaction() != _unit->getFaction() && !getWasHitBy(attacker->getId()))
_wasHitBy.push_back(attacker->getId());
}

/*
* Gets whether the unit was hit.
* @return if it was hit.
*/
bool AlienBAIState::getWasHit() const
bool AlienBAIState::getWasHitBy(int attacker) const
{
return _wasHit;
return std::find(_wasHitBy.begin(), _wasHitBy.end(), attacker) != _wasHitBy.end();
}
/*
* Sets up a patrol action.
Expand Down
8 changes: 4 additions & 4 deletions src/Battlescape/AlienBAIState.h
Expand Up @@ -43,10 +43,10 @@ class AlienBAIState : public BattleAIState
int _escapeTUs, _ambushTUs, _reserveTUs;
BattleAction *_escapeAction, *_ambushAction, *_attackAction, *_patrolAction, *_psiAction;
bool _rifle, _melee, _blaster;
bool _traceAI, _wasHit, _didPsi;
bool _traceAI, _didPsi;
int _AIMode, _intelligence, _closestDist;
Node *_fromNode, *_toNode;
std::vector<int> _reachable, _reachableWithAttack;
std::vector<int> _reachable, _reachableWithAttack, _wasHitBy;
BattleActionType _reserve;
public:
/// Creates a new AlienBAIState linked to the game and a certain unit.
Expand All @@ -64,9 +64,9 @@ class AlienBAIState : public BattleAIState
/// Runs state functionality every AI cycle.
void think(BattleAction *action);
/// Sets the "unit was hit" flag true.
void setWasHit();
void setWasHitBy(BattleUnit *attacker);
/// Gets whether the unit was hit.
bool getWasHit() const;
bool getWasHitBy(int attacker) const;
/// setup a patrol objective.
void setupPatrol();
/// setup an ambush objective.
Expand Down
2 changes: 1 addition & 1 deletion src/Battlescape/ProjectileFlyBState.cpp
Expand Up @@ -576,7 +576,7 @@ void ProjectileFlyBState::think()
AlienBAIState *aggro = dynamic_cast<AlienBAIState*>(victim->getCurrentAIState());
if (aggro != 0)
{
aggro->setWasHit();
aggro->setWasHitBy(_unit);
_unit->setTurnsSinceSpotted(0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Battlescape/TileEngine.cpp
Expand Up @@ -848,7 +848,7 @@ std::vector<BattleUnit *> TileEngine::getSpottingUnits(BattleUnit* unit)
originVoxel.z -= 2;
Position targetVoxel;
AlienBAIState *aggro = dynamic_cast<AlienBAIState*>((*i)->getCurrentAIState());
bool gotHit = (aggro != 0 && aggro->getWasHit());
bool gotHit = (aggro != 0 && aggro->getWasHitBy(unit->getId()));
// can actually see the target Tile, or we got hit
if (((*i)->checkViewSector(unit->getPosition()) || gotHit) &&
// can actually target the unit
Expand Down

0 comments on commit 0f60ee3

Please sign in to comment.