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_GLOBE_H
20 #define OPENXCOM_GLOBE_H
21 
22 #include <vector>
23 #include <list>
24 #include "../Engine/InteractiveSurface.h"
25 #include "../Engine/FastLineClip.h"
26 #include "Cord.h"
27 
28 namespace OpenXcom
29 {
30 
31 class Game;
32 class Polygon;
33 class SurfaceSet;
34 class Timer;
35 class Target;
36 class LocalizedText;
37 
38 /**
39  * Interactive globe view of the world.
40  * Takes a flat world map made out of land polygons with
41  * polar coordinates and renders it as a 3D-looking globe
42  * with cartesian coordinates that the player can interact with.
43  */
44 class Globe : public InteractiveSurface
45 {
46 private:
47 	static const int NUM_TEXTURES = 13;
48 	static const int NUM_LANDSHADES = 48;
49 	static const int NUM_SEASHADES = 72;
50 	static const int NEAR_RADIUS = 25;
51 	static const int DOGFIGHT_ZOOM = 3;
52 	static const double ROTATE_LONGITUDE;
53 	static const double ROTATE_LATITUDE;
54 
55 	double _cenLon, _cenLat, _rotLon, _rotLat, _hoverLon, _hoverLat;
56 	Sint16 _cenX, _cenY;
57 	size_t _zoom, _zoomOld;
58 	SurfaceSet *_texture;
59 	Game *_game;
60 	Surface *_markers, *_countries, *_radars;
61 	bool _blink, _hover;
62 	Timer *_blinkTimer, *_rotTimer;
63 	std::list<Polygon*> _cacheLand;
64 	Surface *_mkXcomBase, *_mkAlienBase, *_mkCraft, *_mkWaypoint, *_mkCity;
65 	Surface *_mkFlyingUfo, *_mkLandedUfo, *_mkCrashedUfo, *_mkAlienSite;
66 	FastLineClip *_clipper;
67 	double _radius, _radiusStep;
68 	///normal of each pixel in earth globe per zoom level
69 	std::vector<std::vector<Cord> > _earthData;
70 	///data sample used for noise in shading
71 	std::vector<Sint16> _randomNoiseData;
72 	///list of dimension of earth on screen per zoom level
73 	std::vector<double> _zoomRadius;
74 
75 	bool _isMouseScrolling, _isMouseScrolled;
76 	int _xBeforeMouseScrolling, _yBeforeMouseScrolling;
77 	double _lonBeforeMouseScrolling, _latBeforeMouseScrolling;
78 	Uint32 _mouseScrollingStartTime;
79 	int _totalMouseMoveX, _totalMouseMoveY;
80 	bool _mouseMovedOverThreshold;
81 
82 	/// Sets the globe zoom factor.
83 	void setZoom(size_t zoom);
84 	/// Checks if a point is behind the globe.
85 	bool pointBack(double lon, double lat) const;
86 	/// Return latitude of last visible to player point on given longitude.
87 	double lastVisibleLat(double lon) const;
88 	/// Checks if a point is inside a polygon.
89 	bool insidePolygon(double lon, double lat, Polygon *poly) const;
90 	/// Checks if a target is near a point.
91 	bool targetNear(Target* target, int x, int y) const;
92 	/// Caches a set of polygons.
93 	void cache(std::list<Polygon*> *polygons, std::list<Polygon*> *cache);
94 	/// Get position of sun relative to given position in polar cords and date.
95 	Cord getSunDirection(double lon, double lat) const;
96 	/// Draw globe range circle.
97 	void drawGlobeCircle(double lat, double lon, double radius, int segments);
98 	/// Special "transparent" line.
99 	void XuLine(Surface* surface, Surface* src, double x1, double y1, double x2, double y2, int shade);
100 	/// Draw line on globe surface.
101 	void drawVHLine(Surface *surface, double lon1, double lat1, double lon2, double lat2, Uint8 color);
102 	/// Draw flight path.
103 	void drawPath(Surface *surface, double lon1, double lat1, double lon2, double lat2);
104 public:
105 	/// Creates a new globe at the specified position and size.
106 	Globe(Game *game, int cenX, int cenY, int width, int height, int x = 0, int y = 0);
107 	/// Cleans up the globe.
108 	~Globe();
109 	/// Loads a set of polygons from a DAT file.
110 	static void loadDat(const std::string &filename, std::list<Polygon*> *polygons);
111 	/// Converts polar coordinates to cartesian coordinates.
112 	void polarToCart(double lon, double lat, Sint16 *x, Sint16 *y) const;
113 	/// Converts polar coordinates to cartesian coordinates.
114 	void polarToCart(double lon, double lat, double *x, double *y) const;
115 	/// Converts cartesian coordinates to polar coordinates.
116 	void cartToPolar(Sint16 x, Sint16 y, double *lon, double *lat) const;
117 	/// Sets the texture set for the globe's polygons.
118 	void setTexture(SurfaceSet *texture);
119 	/// Starts rotating the globe left.
120 	void rotateLeft();
121 	/// Starts rotating the globe right.
122 	void rotateRight();
123 	/// Starts rotating the globe up.
124 	void rotateUp();
125 	/// Starts rotating the globe down.
126 	void rotateDown();
127 	/// Stops rotating the globe.
128 	void rotateStop();
129 	/// Stops longitude rotation of the globe.
130 	void rotateStopLon();
131 	/// Stops latitude rotation of the globe.
132 	void rotateStopLat();
133 	/// Zooms the globe in.
134 	void zoomIn();
135 	/// Zooms the globe out.
136 	void zoomOut();
137 	/// Zooms the globe minimum.
138 	void zoomMin();
139 	/// Zooms the globe maximum.
140 	void zoomMax();
141 	/// Saves the zoom level for dogfights.
142 	void saveZoomDogfight();
143 	/// Zooms the globe in for dogfights.
144 	bool zoomDogfightIn();
145 	/// Zooms the globe out for dogfights.
146 	bool zoomDogfightOut();
147 	/// Gets the current zoom.
148 	size_t getZoom() const;
149 	/// Centers the globe on a point.
150 	void center(double lon, double lat);
151 	/// Checks if a point is inside land.
152 	bool insideLand(double lon, double lat) const;
153 	/// Turns on/off the globe detail.
154 	void toggleDetail();
155 	/// Gets all the targets near a point on the globe.
156 	std::vector<Target*> getTargets(int x, int y, bool craft) const;
157 	/// Caches visible globe polygons.
158 	void cachePolygons();
159 	/// Sets the palette of the globe.
160 	void setPalette(SDL_Color *colors, int firstcolor = 0, int ncolors = 256);
161 	/// Handles the timers.
162 	void think();
163 	/// Blinks the markers.
164 	void blink();
165 	/// Rotates the globe.
166 	void rotate();
167 	/// Draws the whole globe.
168 	void draw();
169 	/// Draws the ocean of the globe.
170 	void drawOcean();
171 	/// Draws the land of the globe.
172 	void drawLand();
173 	/// Draws the shadow.
174 	void drawShadow();
175 	/// Draws the radar ranges of the globe.
176 	void drawRadars();
177 	/// Draws the flight paths of the globe.
178 	void drawFlights();
179 	/// Draws the country details of the globe.
180 	void drawDetail();
181 	/// Draws all the markers over the globe.
182 	void drawMarkers();
183 	/// Blits the globe onto another surface.
184 	void blit(Surface *surface);
185 	/// Special handling for mouse hover.
186 	void mouseOver(Action *action, State *state);
187 	/// Special handling for mouse presses.
188 	void mousePress(Action *action, State *state);
189 	/// Special handling for mouse releases.
190 	void mouseRelease(Action *action, State *state);
191 	/// Special handling for mouse clicks.
192 	void mouseClick(Action *action, State *state);
193 	/// Special handling for key presses.
194 	void keyboardPress(Action *action, State *state);
195 	/// Get the polygons texture and shade at the given point.
196 	void getPolygonTextureAndShade(double lon, double lat, int *texture, int *shade) const;
197 	/// Get the localized text.
198 	const LocalizedText &tr(const std::string &id) const;
199 	/// Get the localized text.
200 	LocalizedText tr(const std::string &id, unsigned n) const;
201 	/// Sets hover base position.
202 	void setNewBaseHoverPos(double lon, double lat);
203 	/// Turns on new base hover mode.
204 	void setNewBaseHover(void);
205 	/// Turns off new base hover mode.
206 	void unsetNewBaseHover(void);
207 	/// Gets state of base hover mode
208 	bool getNewBaseHover(void);
209 	/// Gets _detail variable
210 	bool getShowRadar(void);
211 	/// set the _radarLines variable
212 	void toggleRadarLines();
213 	/// Update the resolution settings, we just resized the window.
214 	void resize();
215 	/// Set up the radius of earth and stuff.
216 	void setupRadii(int width, int height);
217 	/// Move the mouse back to where it started after we finish drag scrolling.
218 	void stopScrolling(Action *action);
219 };
220 
221 }
222 
223 #endif
224