1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http:///www.gnu.org/licenses/>.
18  */
19 #ifndef OPENXCOM_BATTLESCAPEGAME_H
20 #define OPENXCOM_BATTLESCAPEGAME_H
21 
22 #include "Position.h"
23 #include <SDL.h>
24 #include <string>
25 #include <list>
26 #include <vector>
27 
28 namespace OpenXcom
29 {
30 
31 class BattleUnit;
32 class SavedBattleGame;
33 class BattleItem;
34 class BattleState;
35 class BattlescapeState;
36 class ResourcePack;
37 class Map;
38 class TileEngine;
39 class Pathfinding;
40 class Ruleset;
41 class InfoboxOKState;
42 
43 enum BattleActionType { BA_NONE, BA_TURN, BA_WALK, BA_PRIME, BA_THROW, BA_AUTOSHOT, BA_SNAPSHOT, BA_AIMEDSHOT, BA_STUN, BA_HIT, BA_USE, BA_LAUNCH, BA_MINDCONTROL, BA_PANIC, BA_RETHINK };
44 
45 struct BattleAction
46 {
47 	BattleActionType type;
48 	BattleUnit *actor;
49 	BattleItem *weapon;
50 	Position target;
51 	std::list<Position> waypoints;
52 	int TU;
53 	bool targeting;
54 	int value;
55 	std::string result;
56 	bool strafe, run;
57 	int diff;
58 	int autoShotCounter;
59 	Position cameraPosition;
60     bool desperate; // ignoring newly-spotted units
61 	int finalFacing;
62 	bool finalAction;
63     int number; // first action of turn, second, etc.?
BattleActionBattleAction64 	BattleAction() : type(BA_NONE), actor(0), weapon(0), TU(0), targeting(false), value(0), result(""), strafe(false), run(false), diff(0), autoShotCounter(0), cameraPosition(0, 0, -1), desperate(false), finalFacing(-1), finalAction(false), number(0) { }
65 };
66 
67 /**
68  * Battlescape game - the core game engine of the battlescape game.
69  */
70 class BattlescapeGame
71 {
72 private:
73 	SavedBattleGame *_save;
74 	BattlescapeState *_parentState;
75 	std::list<BattleState*> _states, _deleted;
76 	BattleActionType _tuReserved, _playerTUReserved;
77 	bool _playerPanicHandled;
78 	int _AIActionCounter;
79 	BattleAction _currentAction;
80 	bool _AISecondMove;
81 
82 	/// Ends the turn.
83 	void endTurn();
84 	/// Picks the first soldier that is panicking.
85 	bool handlePanickingPlayer();
86 	/// Common function for hanlding panicking units.
87 	bool handlePanickingUnit(BattleUnit *unit);
88 	/// Determines whether there are any actions pending for the given unit.
89 	bool noActionsPending(BattleUnit *bu);
90 	std::vector<InfoboxOKState*> _infoboxQueue;
91 	/// Shows the infoboxes in the queue (if any).
92 	void showInfoBoxQueue();
93 	bool _playedAggroSound, _endTurnRequested, _kneelReserved;
94 public:
95 	/// Creates the BattlescapeGame state.
96 	BattlescapeGame(SavedBattleGame *save, BattlescapeState *parentState);
97 	/// Cleans up the BattlescapeGame state.
98 	~BattlescapeGame();
99 	/// Checks for units panicking or falling and so on.
100 	void think();
101 	/// Initializes the Battlescape game.
102 	void init();
103 	/// Determines whether a playable unit is selected.
104 	bool playableUnitSelected();
105 	/// Handles states timer.
106 	void handleState();
107 	/// Pushes a state to the front of the list.
108 	void statePushFront(BattleState *bs);
109 	/// Pushes a state to second on the list.
110 	void statePushNext(BattleState *bs);
111 	/// Pushes a state to the back of the list.
112 	void statePushBack(BattleState *bs);
113 	/// Handles the result of non target actions, like priming a grenade.
114 	void handleNonTargetAction();
115 	/// Removes current state.
116 	void popState();
117 	/// Sets state think interval.
118 	void setStateInterval(Uint32 interval);
119 	/// Checks for casualties in battle.
120 	void checkForCasualties(BattleItem *murderweapon, BattleUnit *murderer, bool hiddenExplosion = false, bool terrainExplosion = false);
121 	/// Checks if a unit panics.
122 	void checkForPanic(BattleUnit *unit);
123 	/// Checks reserved tu.
124 	bool checkReservedTU(BattleUnit *bu, int tu, bool justChecking = false);
125 	/// Handles unit AI.
126 	void handleAI(BattleUnit *unit);
127 	/// Drops an item and affects it with gravity.
128 	void dropItem(const Position &position, BattleItem *item, bool newItem = false, bool removeItem = false);
129 	/// Converts a unit into a unit of another type.
130 	BattleUnit *convertUnit(BattleUnit *unit, std::string newType);
131 	/// Handles kneeling action.
132 	bool kneel(BattleUnit *bu);
133 	/// Cancels the current action.
134 	bool cancelCurrentAction(bool bForce = false);
135 	/// Gets a pointer to access action members directly.
136 	BattleAction *getCurrentAction();
137 	/// Determines whether there is an action currently going on.
138 	bool isBusy();
139 	/// Activates primary action (left click).
140 	void primaryAction(const Position &pos);
141 	/// Activates secondary action (right click).
142 	void secondaryAction(const Position &pos);
143 	/// Handler for the blaster launcher button.
144 	void launchAction();
145 	/// Handler for the psi button.
146 	void psiButtonAction();
147 	/// Moves a unit up or down.
148 	void moveUpDown(BattleUnit *unit, int dir);
149 	/// Requests the end of the turn (wait for explosions etc to really end the turn).
150 	void requestEndTurn();
151 	/// Sets the TU reserved type.
152 	void setTUReserved(BattleActionType tur, bool player);
153 	/// Sets up the cursor taking into account the action.
154 	void setupCursor();
155 	/// Gets the map.
156 	Map *getMap();
157 	/// Gets the save.
158 	SavedBattleGame *getSave();
159 	/// Gets the tilengine.
160 	TileEngine *getTileEngine();
161 	/// Gets the pathfinding.
162 	Pathfinding *getPathfinding();
163 	/// Gets the resourcepack.
164 	ResourcePack *getResourcePack();
165 	/// Gets the ruleset.
166 	const Ruleset *getRuleset() const;
167 	static bool _debugPlay;
168 	/// Returns whether panic has been handled.
getPanicHandled()169 	bool getPanicHandled() { return _playerPanicHandled; }
170 	/// Tries to find an item and pick it up if possible.
171 	void findItem(BattleAction *action);
172 	/// Checks through all the items on the ground and picks one.
173 	BattleItem *surveyItems(BattleAction *action);
174 	/// Evaluates if it's worthwhile to take this item.
175 	bool worthTaking(BattleItem* item, BattleAction *action);
176 	/// Picks the item up from the ground.
177 	int takeItemFromGround(BattleItem* item, BattleAction *action);
178 	/// Assigns the item to a slot (stolen from battlescapeGenerator::addItem()).
179 	bool takeItem(BattleItem* item, BattleAction *action);
180 	/// Returns the type of action that is reserved.
181 	BattleActionType getReservedAction();
182 	/// Tallies the living units, converting them if necessary.
183 	void tallyUnits(int &liveAliens, int &liveSoldiers, bool convert);
184 	/// Sets the kneel reservation setting.
185 	void setKneelReserved(bool reserved);
186 	/// Checks the kneel reservation setting.
187 	bool getKneelReserved();
188 	/// Checks for and triggers proximity grenades.
189 	bool checkForProximityGrenades(BattleUnit *unit);
190 	void cleanupDeleted();
191 };
192 
193 }
194 
195 #endif
196