OpenXcom  1.0
Open-source clone of the original X-Com
AIModule.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <yaml-cpp/yaml.h>
21 #include "BattlescapeGame.h"
22 #include "Position.h"
23 #include "../Savegame/BattleUnit.h"
24 #include <vector>
25 
26 
27 namespace OpenXcom
28 {
29 
30 class SavedBattleGame;
31 class BattleUnit;
32 struct BattleAction;
33 class BattlescapeState;
34 class Node;
35 
36 enum AIMode { AI_PATROL, AI_AMBUSH, AI_COMBAT, AI_ESCAPE };
40 class AIModule
41 {
42 private:
43  SavedBattleGame *_save;
44  BattleUnit *_unit;
45  BattleUnit *_aggroTarget;
46  int _knownEnemies, _visibleEnemies, _spottingEnemies;
47  int _escapeTUs, _ambushTUs;
48  BattleAction *_escapeAction, *_ambushAction, *_attackAction, *_patrolAction, *_psiAction;
49  bool _rifle, _melee, _blaster;
50  bool _traceAI, _didPsi;
51  int _AIMode, _intelligence, _closestDist;
52  Node *_fromNode, *_toNode;
53  std::vector<int> _reachable, _reachableWithAttack, _wasHitBy;
54  BattleActionType _reserve;
55  UnitFaction _targetFaction;
56 public:
60  ~AIModule();
62  void load(const YAML::Node& node);
64  YAML::Node save() const;
66  void think(BattleAction *action);
68  void setWasHitBy(BattleUnit *attacker);
70  bool getWasHitBy(int attacker) const;
72  void setupPatrol();
74  void setupAmbush();
76  void setupAttack();
78  void setupEscape();
80  int countKnownTargets() const;
82  int getSpottingUnits(const Position& pos) const;
84  int selectNearestTarget();
88  bool selectRandomTarget();
90  bool selectPointNearTarget(BattleUnit *target, int maxTUs) const;
92  void evaluateAIMode();
94  bool findFirePoint();
96  bool explosiveEfficacy(Position targetPos, BattleUnit *attackingUnit, int radius, int diff, bool grenade = false) const;
97  bool getNodeOfBestEfficacy(BattleAction *action);
99  void meleeAction();
101  void wayPointAction();
103  void projectileAction();
105  void selectFireMethod();
107  void grenadeAction();
109  bool psiAction();
111  void meleeAttack();
113  bool validTarget(BattleUnit* unit, bool assessDanger, bool includeCivs) const;
115  BattleActionType getReserveMode();
117  void selectMeleeOrRanged();
120 };
121 
122 }
bool getWasHitBy(int attacker) const
Gets whether the unit was hit.
Definition: AIModule.cpp:396
bool selectPointNearTarget(BattleUnit *target, int maxTUs) const
Selects the nearest reachable point relative to a target.
Definition: AIModule.cpp:1131
Definition: BattlescapeGame.h:43
void evaluateAIMode()
re-evaluate our situation, and make a decision from our available options.
Definition: AIModule.cpp:1173
void setupAmbush()
setup an ambush objective.
Definition: AIModule.cpp:569
int selectNearestTarget()
Selects the nearest target we can see, and return the number of viable targets.
Definition: AIModule.cpp:1028
bool psiAction()
Performs a psionic attack.
Definition: AIModule.cpp:1860
void wayPointAction()
Attempts to fire a waypoint projectile at an enemy we, or one of our teammates sees.
Definition: AIModule.cpp:1650
bool selectClosestKnownEnemy()
Selects the closest known xcom unit for ambushing.
Definition: AIModule.cpp:1082
bool findFirePoint()
Selects a suitable position from which to attack.
Definition: AIModule.cpp:1422
int countKnownTargets() const
count how many xcom/civilian units are known to this unit.
Definition: AIModule.cpp:968
AIModule(SavedBattleGame *save, BattleUnit *unit, Node *node)
Creates a new AIModule linked to the game and a certain unit.
Definition: AIModule.cpp:49
void setWasHitBy(BattleUnit *attacker)
Sets the "unit was hit" flag true.
Definition: AIModule.cpp:386
YAML::Node save() const
Saves the AI Module to YAML.
Definition: AIModule.cpp:107
void setupEscape()
setup an escape objective.
Definition: AIModule.cpp:769
void load(const YAML::Node &node)
Loads the AI Module from YAML.
Definition: AIModule.cpp:85
bool validTarget(BattleUnit *unit, bool assessDanger, bool includeCivs) const
Checks to make sure a target is valid, given the parameters.
Definition: AIModule.cpp:1988
This class is used by the BattleUnit AI.
Definition: AIModule.h:40
~AIModule()
Cleans up the AIModule.
Definition: AIModule.cpp:72
void think(BattleAction *action)
Runs Module functionality every AI cycle.
Definition: AIModule.cpp:127
bool selectRandomTarget()
Selects a random known target.
Definition: AIModule.cpp:1105
void meleeAttack()
Performs a melee attack action.
Definition: AIModule.cpp:1970
bool explosiveEfficacy(Position targetPos, BattleUnit *attackingUnit, int radius, int diff, bool grenade=false) const
Decides if we should throw a grenade/launch a missile to this position.
Definition: AIModule.cpp:1498
void selectMeleeOrRanged()
Assuming we have both a ranged and a melee weapon, we have to select one.
Definition: AIModule.cpp:2023
bool getNodeOfBestEfficacy(BattleAction *action)
Checks nearby nodes to see if they&#39;d make good grenade targets.
Definition: AIModule.cpp:2084
void setupPatrol()
setup a patrol objective.
Definition: AIModule.cpp:405
void selectFireMethod()
Selects a fire method.
Definition: AIModule.cpp:1751
The battlescape data that gets written to disk when the game is saved.
Definition: SavedBattleGame.h:47
BattleActionType getReserveMode()
Checks the alien&#39;s TU reservation setting.
Definition: AIModule.cpp:2014
Represents a node/spawnpoint in the battlescape, loaded from RMP files.
Definition: Node.h:32
void grenadeAction()
Attempts to throw a grenade at an enemy (or group of enemies) we can see.
Definition: AIModule.cpp:1815
Easy handling of X-Y-Z coordinates.
Definition: Position.h:28
void setupAttack()
setup a combat objective.
Definition: AIModule.cpp:688
void meleeAction()
Attempts to take a melee attack/charge an enemy we can see.
Definition: AIModule.cpp:1596
int getSpottingUnits(const Position &pos) const
count how many known XCom units are able to see this unit.
Definition: AIModule.cpp:990
void projectileAction()
Attempts to fire at an enemy we can see.
Definition: AIModule.cpp:1738
Represents a moving unit in the battlescape, player controlled or AI controlled it holds info about i...
Definition: BattleUnit.h:59
BattleUnit * getTarget()
Gets the current targetted unit.
Definition: AIModule.cpp:2137
Definition: BaseInfoState.cpp:40