OpenXcom  1.0
Open-source clone of the original X-Com
TileEngine.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 <vector>
21 #include "Position.h"
22 #include "../Mod/RuleItem.h"
23 #include <SDL.h>
24 
25 namespace OpenXcom
26 {
27 
28 class SavedBattleGame;
29 class BattleUnit;
30 class BattleItem;
31 class Tile;
32 struct BattleAction;
38 {
39 private:
40  static const int MAX_VIEW_DISTANCE = 20;
41  static const int MAX_VIEW_DISTANCE_SQR = MAX_VIEW_DISTANCE * MAX_VIEW_DISTANCE;
42  static const int MAX_VOXEL_VIEW_DISTANCE = MAX_VIEW_DISTANCE * 16;
43  static const int MAX_DARKNESS_TO_SEE_UNITS = 9;
44  SavedBattleGame *_save;
45  std::vector<Uint16> *_voxelData;
46  static const int heightFromCenter[11];
47  void addLight(Position center, int power, int layer);
48  int blockage(Tile *tile, const int part, ItemDamageType type, int direction = -1, bool checkingFromOrigin = false);
49  bool _personalLighting;
50 public:
52  TileEngine(SavedBattleGame *save, std::vector<Uint16> *voxelData);
54  ~TileEngine();
56  void calculateSunShading();
58  void calculateSunShading(Tile *tile);
60  bool calculateFOV(BattleUnit *unit);
62  void calculateFOV(Position position);
64  bool checkReactionFire(BattleUnit *unit);
68  void calculateUnitLighting();
70  BattleUnit *hit(Position center, int power, ItemDamageType type, BattleUnit *unit);
72  void explode(Position center, int power, ItemDamageType type, int maxRadius, BattleUnit *unit = 0);
76  int unitOpensDoor(BattleUnit *unit, bool rClick = false, int dir = -1);
78  int closeUfoDoors();
80  int calculateLine(Position origin, Position target, bool storeTrajectory, std::vector<Position> *trajectory, BattleUnit *excludeUnit, bool doVoxelCheck = true, bool onlyVisible = false, BattleUnit *excludeAllBut = 0);
82  int calculateParabola(Position origin, Position target, bool storeTrajectory, std::vector<Position> *trajectory, BattleUnit *excludeUnit, double curvature, const Position delta);
86  bool visible(BattleUnit *currentUnit, Tile *tile);
90  int distance(Position pos1, Position pos2) const;
92  int distanceSq(Position pos1, Position pos2, bool considerZ = true) const;
94  int horizontalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject = false);
96  int verticalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject = false);
98  Tile *applyGravity(Tile *t);
100  bool validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir);
102  bool validMeleeRange(const Position& pos, int direction, BattleUnit *attacker, BattleUnit *target, Position *dest, bool preferEnemy = true);
104  int faceWindow(Position position);
106  int checkVoxelExposure(Position *originVoxel, Tile *tile, BattleUnit *excludeUnit, BattleUnit *excludeAllBut);
108  bool canTargetUnit(Position *originVoxel, Tile *tile, Position *scanVoxel, BattleUnit *excludeUnit, BattleUnit *potentialUnit = 0);
110  bool canTargetTile(Position *originVoxel, Tile *tile, int part, Position *scanVoxel, BattleUnit *excludeUnit);
112  int castedShade(Position voxel);
114  bool isVoxelVisible(Position voxel);
116  int voxelCheck(Position voxel, BattleUnit *excludeUnit, bool excludeAllUnits = false, bool onlyVisible = false, BattleUnit *excludeAllBut = 0);
118  bool detonate(Tile* tile);
120  bool validateThrow(BattleAction &action, const Position& originVoxel, const Position& targetVoxel, double *curve = 0, int *voxelType = 0);
122  void checkAdjacentDoors(const Position& pos, int part);
124  std::vector<std::pair<BattleUnit *, int> > getSpottingUnits(BattleUnit* unit);
126  BattleUnit* getReactor(std::vector<std::pair<BattleUnit *, int> > spotters, int &attackType, BattleUnit *unit);
128  int determineReactionType(BattleUnit *unit, BattleUnit *target);
130  bool tryReaction(BattleUnit *unit, BattleUnit *target, int attackType);
132  void recalculateFOV();
134  int getDirectionTo(Position origin, Position target) const;
136  Position getOriginVoxel(BattleAction &action, Tile *tile);
138  void setDangerZone(const Position& pos, int radius, BattleUnit *unit);
139 
140 };
141 
142 }
bool canTargetUnit(Position *originVoxel, Tile *tile, Position *scanVoxel, BattleUnit *excludeUnit, BattleUnit *potentialUnit=0)
Checks validity for targetting a unit.
Definition: TileEngine.cpp:537
A utility class that modifies tile properties on a battlescape map.
Definition: TileEngine.h:37
void togglePersonalLighting()
Turn XCom soldier&#39;s personal lighting on or off.
Definition: TileEngine.cpp:2542
BattleUnit * getReactor(std::vector< std::pair< BattleUnit *, int > > spotters, int &attackType, BattleUnit *unit)
Given a vector of spotters, and a unit, picks the spotter with the highest reaction score...
Definition: TileEngine.cpp:908
void checkAdjacentDoors(const Position &pos, int part)
Opens any doors this door is connected to.
Definition: TileEngine.cpp:2108
Definition: BattlescapeGame.h:43
int closeUfoDoors()
Closes ufo doors.
Definition: TileEngine.cpp:2138
std::vector< std::pair< BattleUnit *, int > > getSpottingUnits(BattleUnit *unit)
Creates a vector of units that can spot this unit.
Definition: TileEngine.cpp:830
int unitOpensDoor(BattleUnit *unit, bool rClick=false, int dir=-1)
Unit opens door?
Definition: TileEngine.cpp:1953
void recalculateFOV()
Recalculates FOV of all units in-game.
Definition: TileEngine.cpp:2877
TileEngine(SavedBattleGame *save, std::vector< Uint16 > *voxelData)
Creates a new TileEngine class.
Definition: TileEngine.cpp:56
bool tryReaction(BattleUnit *unit, BattleUnit *target, int attackType)
Tries to perform a reaction snap shot to this location.
Definition: TileEngine.cpp:988
Position getOriginVoxel(BattleAction &action, Tile *tile)
determine the origin voxel of a given action.
Definition: TileEngine.cpp:2944
bool visible(BattleUnit *currentUnit, Tile *tile)
Checks visibility of a unit on this tile.
Definition: TileEngine.cpp:392
int voxelCheck(Position voxel, BattleUnit *excludeUnit, bool excludeAllUnits=false, bool onlyVisible=false, BattleUnit *excludeAllBut=0)
Checks what type of voxel occupies this space.
Definition: TileEngine.cpp:2460
bool validateThrow(BattleAction &action, const Position &originVoxel, const Position &targetVoxel, double *curve=0, int *voxelType=0)
Validates a throwing action.
Definition: TileEngine.cpp:2810
Tile * applyGravity(Tile *t)
Applies gravity to anything that occupy this tile.
Definition: TileEngine.cpp:2581
Tile * checkForTerrainExplosions()
Checks if a destroyed tile starts an explosion.
Definition: TileEngine.cpp:1565
bool validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir)
Returns melee validity between two units.
Definition: TileEngine.cpp:2682
bool checkReactionFire(BattleUnit *unit)
Checks reaction fire.
Definition: TileEngine.cpp:781
void calculateSunShading()
Calculates sun shading of the whole map.
Definition: TileEngine.cpp:71
int castedShade(Position voxel)
Calculates the z voxel for shadows.
Definition: TileEngine.cpp:2402
int distanceSq(Position pos1, Position pos2, bool considerZ=true) const
Checks the distance squared between two positions.
Definition: TileEngine.cpp:2568
void setDangerZone(const Position &pos, int radius, BattleUnit *unit)
mark a region of the map as "dangerous" for a turn.
Definition: TileEngine.cpp:3009
bool detonate(Tile *tile)
Blows this tile up.
Definition: TileEngine.cpp:1445
int calculateLine(Position origin, Position target, bool storeTrajectory, std::vector< Position > *trajectory, BattleUnit *excludeUnit, bool doVoxelCheck=true, bool onlyVisible=false, BattleUnit *excludeAllBut=0)
Calculates a line trajectory.
Definition: TileEngine.cpp:2175
void calculateTerrainLighting()
Recalculates lighting of the battlescape for terrain.
Definition: TileEngine.cpp:115
int horizontalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject=false)
Checks the horizontal blockage of a tile.
Definition: TileEngine.cpp:1641
Basic element of which a battle map is build.
Definition: Tile.h:42
int verticalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject=false)
Checks the vertical blockage of a tile.
Definition: TileEngine.cpp:1584
Position getSightOriginVoxel(BattleUnit *currentUnit)
Gets the origin voxel of a unit&#39;s eyesight.
Definition: TileEngine.cpp:361
BattleUnit * hit(Position center, int power, ItemDamageType type, BattleUnit *unit)
Handles bullet/weapon hits.
Definition: TileEngine.cpp:1058
int calculateParabola(Position origin, Position target, bool storeTrajectory, std::vector< Position > *trajectory, BattleUnit *excludeUnit, double curvature, const Position delta)
Calculates a parabola trajectory.
Definition: TileEngine.cpp:2343
The battlescape data that gets written to disk when the game is saved.
Definition: SavedBattleGame.h:47
void explode(Position center, int power, ItemDamageType type, int maxRadius, BattleUnit *unit=0)
Handles explosions.
Definition: TileEngine.cpp:1162
void calculateUnitLighting()
Recalculates lighting of the battlescape for units.
Definition: TileEngine.cpp:162
int checkVoxelExposure(Position *originVoxel, Tile *tile, BattleUnit *excludeUnit, BattleUnit *excludeAllBut)
Checks a unit&#39;s % exposure on a tile.
Definition: TileEngine.cpp:459
int distance(Position pos1, Position pos2) const
Checks the distance between two positions.
Definition: TileEngine.cpp:2554
Easy handling of X-Y-Z coordinates.
Definition: Position.h:28
int faceWindow(Position position)
Gets the AI to look through a window.
Definition: TileEngine.cpp:2784
bool canTargetTile(Position *originVoxel, Tile *tile, int part, Position *scanVoxel, BattleUnit *excludeUnit)
Check validity for targetting a tile.
Definition: TileEngine.cpp:635
bool isVoxelVisible(Position voxel)
Checks the visibility of a given voxel.
Definition: TileEngine.cpp:2431
int getDirectionTo(Position origin, Position target) const
Get direction to a certain point.
Definition: TileEngine.cpp:2894
~TileEngine()
Cleans up the TileEngine.
Definition: TileEngine.cpp:63
Represents a moving unit in the battlescape, player controlled or AI controlled it holds info about i...
Definition: BattleUnit.h:59
Definition: BaseInfoState.cpp:40
int determineReactionType(BattleUnit *unit, BattleUnit *target)
Checks validity of a snap shot to this position.
Definition: TileEngine.cpp:945
bool calculateFOV(BattleUnit *unit)
Calculates the field of view from a units view point.
Definition: TileEngine.cpp:227