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 "../Mod/MapData.h"
24 #include <SDL.h>
25 
26 namespace OpenXcom
27 {
28 
29 class SavedBattleGame;
30 class BattleUnit;
31 class BattleItem;
32 class Tile;
33 struct BattleAction;
39 {
40 private:
41  static const int MAX_VIEW_DISTANCE = 20;
42  static const int MAX_VIEW_DISTANCE_SQR = MAX_VIEW_DISTANCE * MAX_VIEW_DISTANCE;
43  static const int MAX_VOXEL_VIEW_DISTANCE = MAX_VIEW_DISTANCE * 16;
44  static const int MAX_DARKNESS_TO_SEE_UNITS = 9;
45  SavedBattleGame *_save;
46  std::vector<Uint16> *_voxelData;
47  static const int heightFromCenter[11];
48  void addLight(Position center, int power, int layer);
49  int blockage(Tile *tile, const TilePart part, ItemDamageType type, int direction = -1, bool checkingFromOrigin = false);
50  bool _personalLighting;
51  Tile *_cacheTile;
52  Tile *_cacheTileBelow;
53  Position _cacheTilePos;
54 public:
56  TileEngine(SavedBattleGame *save, std::vector<Uint16> *voxelData);
58  ~TileEngine();
60  void calculateSunShading();
62  void calculateSunShading(Tile *tile);
64  bool calculateFOV(BattleUnit *unit);
66  void calculateFOV(Position position);
68  bool checkReactionFire(BattleUnit *unit);
72  void calculateUnitLighting();
74  BattleUnit *hit(Position center, int power, ItemDamageType type, BattleUnit *unit);
76  void explode(Position center, int power, ItemDamageType type, int maxRadius, BattleUnit *unit = 0);
80  int unitOpensDoor(BattleUnit *unit, bool rClick = false, int dir = -1);
82  int closeUfoDoors();
84  int calculateLine(Position origin, Position target, bool storeTrajectory, std::vector<Position> *trajectory, BattleUnit *excludeUnit, bool doVoxelCheck = true, bool onlyVisible = false, BattleUnit *excludeAllBut = 0);
86  int calculateParabola(Position origin, Position target, bool storeTrajectory, std::vector<Position> *trajectory, BattleUnit *excludeUnit, double curvature, const Position delta);
90  bool visible(BattleUnit *currentUnit, Tile *tile);
94  int distance(Position pos1, Position pos2) const;
96  int distanceSq(Position pos1, Position pos2, bool considerZ = true) const;
98  int horizontalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject = false);
100  int verticalBlockage(Tile *startTile, Tile *endTile, ItemDamageType type, bool skipObject = false);
102  Tile *applyGravity(Tile *t);
104  bool validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir);
106  bool validMeleeRange(const Position& pos, int direction, BattleUnit *attacker, BattleUnit *target, Position *dest, bool preferEnemy = true);
108  int faceWindow(Position position);
110  int checkVoxelExposure(Position *originVoxel, Tile *tile, BattleUnit *excludeUnit, BattleUnit *excludeAllBut);
112  bool canTargetUnit(Position *originVoxel, Tile *tile, Position *scanVoxel, BattleUnit *excludeUnit, bool rememberObstacles, BattleUnit *potentialUnit = 0);
114  bool canTargetTile(Position *originVoxel, Tile *tile, int part, Position *scanVoxel, BattleUnit *excludeUnit, bool rememberObstacles);
116  int castedShade(Position voxel);
118  bool isVoxelVisible(Position voxel);
120  VoxelType voxelCheck(Position voxel, BattleUnit *excludeUnit, bool excludeAllUnits = false, bool onlyVisible = false, BattleUnit *excludeAllBut = 0);
122  void voxelCheckFlush();
124  bool detonate(Tile* tile);
126  bool validateThrow(BattleAction &action, Position originVoxel, Position targetVoxel, double *curve = 0, int *voxelType = 0, bool forced = false);
128  void checkAdjacentDoors(const Position& pos, TilePart part);
130  std::vector<std::pair<BattleUnit *, int> > getSpottingUnits(BattleUnit* unit);
132  BattleUnit* getReactor(std::vector<std::pair<BattleUnit *, int> > spotters, int &attackType, BattleUnit *unit);
134  int determineReactionType(BattleUnit *unit, BattleUnit *target);
136  bool tryReaction(BattleUnit *unit, BattleUnit *target, int attackType);
138  void recalculateFOV();
140  int getDirectionTo(Position origin, Position target) const;
142  Position getOriginVoxel(BattleAction &action, Tile *tile);
144  void setDangerZone(const Position& pos, int radius, BattleUnit *unit);
145 
146 };
147 
148 }
A utility class that modifies tile properties on a battlescape map.
Definition: TileEngine.h:38
void togglePersonalLighting()
Turn XCom soldier&#39;s personal lighting on or off.
Definition: TileEngine.cpp:2631
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:954
Definition: BattlescapeGame.h:43
int closeUfoDoors()
Closes ufo doors.
Definition: TileEngine.cpp:2179
std::vector< std::pair< BattleUnit *, int > > getSpottingUnits(BattleUnit *unit)
Creates a vector of units that can spot this unit.
Definition: TileEngine.cpp:876
int unitOpensDoor(BattleUnit *unit, bool rClick=false, int dir=-1)
Unit opens door?
Definition: TileEngine.cpp:1994
void recalculateFOV()
Recalculates FOV of all units in-game.
Definition: TileEngine.cpp:2976
TileEngine(SavedBattleGame *save, std::vector< Uint16 > *voxelData)
Creates a new TileEngine class.
Definition: TileEngine.cpp:55
bool tryReaction(BattleUnit *unit, BattleUnit *target, int attackType)
Tries to perform a reaction snap shot to this location.
Definition: TileEngine.cpp:1034
Position getOriginVoxel(BattleAction &action, Tile *tile)
determine the origin voxel of a given action.
Definition: TileEngine.cpp:3043
bool validateThrow(BattleAction &action, Position originVoxel, Position targetVoxel, double *curve=0, int *voxelType=0, bool forced=false)
Validates a throwing action.
Definition: TileEngine.cpp:2899
bool visible(BattleUnit *currentUnit, Tile *tile)
Checks visibility of a unit on this tile.
Definition: TileEngine.cpp:392
Tile * applyGravity(Tile *t)
Applies gravity to anything that occupy this tile.
Definition: TileEngine.cpp:2670
Tile * checkForTerrainExplosions()
Checks if a destroyed tile starts an explosion.
Definition: TileEngine.cpp:1603
bool canTargetTile(Position *originVoxel, Tile *tile, int part, Position *scanVoxel, BattleUnit *excludeUnit, bool rememberObstacles)
Check validity for targetting a tile.
Definition: TileEngine.cpp:642
bool validMeleeRange(BattleUnit *attacker, BattleUnit *target, int dir)
Returns melee validity between two units.
Definition: TileEngine.cpp:2771
bool checkReactionFire(BattleUnit *unit)
Checks reaction fire.
Definition: TileEngine.cpp:827
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:2451
int distanceSq(Position pos1, Position pos2, bool considerZ=true) const
Checks the distance squared between two positions.
Definition: TileEngine.cpp:2657
void setDangerZone(const Position &pos, int radius, BattleUnit *unit)
mark a region of the map as "dangerous" for a turn.
Definition: TileEngine.cpp:3108
bool detonate(Tile *tile)
Blows this tile up.
Definition: TileEngine.cpp:1482
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:2216
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:1682
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:1622
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:1104
VoxelType 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:2512
void checkAdjacentDoors(const Position &pos, TilePart part)
Opens any doors this door is connected to.
Definition: TileEngine.cpp:2149
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:2394
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:1209
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:2643
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:2873
bool isVoxelVisible(Position voxel)
Checks the visibility of a given voxel.
Definition: TileEngine.cpp:2481
void voxelCheckFlush()
Flushes cache of voxel check.
Definition: TileEngine.cpp:2621
int getDirectionTo(Position origin, Position target) const
Get direction to a certain point.
Definition: TileEngine.cpp:2993
~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
bool canTargetUnit(Position *originVoxel, Tile *tile, Position *scanVoxel, BattleUnit *excludeUnit, bool rememberObstacles, BattleUnit *potentialUnit=0)
Checks validity for targetting a unit.
Definition: TileEngine.cpp:538
int determineReactionType(BattleUnit *unit, BattleUnit *target)
Checks validity of a snap shot to this position.
Definition: TileEngine.cpp:991
bool calculateFOV(BattleUnit *unit)
Calculates the field of view from a units view point.
Definition: TileEngine.cpp:227