forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobe.cpp
More file actions
1911 lines (1689 loc) · 48.4 KB
/
Copy pathGlobe.cpp
File metadata and controls
1911 lines (1689 loc) · 48.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenXcom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Globe.h"
#include <algorithm>
#include "../fmath.h"
#include "../Engine/Action.h"
#include "../Engine/SurfaceSet.h"
#include "../Engine/Timer.h"
#include "../Mod/Mod.h"
#include "../Mod/Polygon.h"
#include "../Mod/Polyline.h"
#include "../Engine/FastLineClip.h"
#include "../Engine/Game.h"
#include "../Savegame/SavedGame.h"
#include "../Savegame/GameTime.h"
#include "../Savegame/Base.h"
#include "../Savegame/Country.h"
#include "../Mod/RuleCountry.h"
#include "../Interface/Text.h"
#include "../Engine/LocalizedText.h"
#include "../Mod/RuleRegion.h"
#include "../Savegame/Region.h"
#include "../Mod/City.h"
#include "../Savegame/Target.h"
#include "../Savegame/Ufo.h"
#include "../Savegame/Craft.h"
#include "../Savegame/Waypoint.h"
#include "../Engine/ShaderMove.h"
#include "../Engine/ShaderRepeat.h"
#include "../Engine/Options.h"
#include "../Savegame/MissionSite.h"
#include "../Savegame/AlienBase.h"
#include "../Engine/Language.h"
#include "../Savegame/BaseFacility.h"
#include "../Mod/RuleBaseFacility.h"
#include "../Mod/RuleCraft.h"
#include "../Mod/RuleGlobe.h"
#include "../Interface/Cursor.h"
#include "../Engine/Screen.h"
namespace OpenXcom
{
const double Globe::ROTATE_LONGITUDE = 0.10;
const double Globe::ROTATE_LATITUDE = 0.06;
Uint8 Globe::OCEAN_COLOR;
bool Globe::OCEAN_SHADING;
Uint8 Globe::COUNTRY_LABEL_COLOR;
Uint8 Globe::LINE_COLOR;
Uint8 Globe::CITY_LABEL_COLOR;
Uint8 Globe::BASE_LABEL_COLOR;
namespace
{
///helper class for `Globe` for drawing earth globe with shadows
struct GlobeStaticData
{
///array of shading gradient
Sint16 shade_gradient[240];
///size of x & y of noise surface
const int random_surf_size;
/**
* Function returning normal vector of sphere surface
* @param ox x cord of sphere center
* @param oy y cord of sphere center
* @param r radius of sphere
* @param x cord of point where we getting this vector
* @param y cord of point where we getting this vector
* @return normal vector of sphere surface
*/
static inline Cord circle_norm(double ox, double oy, double r, double x, double y)
{
const double limit = r*r;
const double norm = 1./r;
Cord ret;
ret.x = (x-ox);
ret.y = (y-oy);
const double temp = (ret.x)*(ret.x) + (ret.y)*(ret.y);
if (limit > temp)
{
ret.x *= norm;
ret.y *= norm;
ret.z = sqrt(limit - temp)*norm;
return ret;
}
else
{
ret.x = 0.;
ret.y = 0.;
ret.z = 0.;
return ret;
}
}
//initialization
GlobeStaticData() : random_surf_size(60)
{
//filling terminator gradient LUT
for (int i=0; i<240; ++i)
{
int j = i - 120;
if (j<-66) j=-16;
else
if (j<-48) j=-15;
else
if (j<-33) j=-14;
else
if (j<-22) j=-13;
else
if (j<-15) j=-12;
else
if (j<-11) j=-11;
else
if (j<-9) j=-10;
if (j>120) j=19;
else
if (j>98) j=18;
else
if (j>86) j=17;
else
if (j>74) j=16;
else
if (j>54) j=15;
else
if (j>38) j=14;
else
if (j>26) j=13;
else
if (j>18) j=12;
else
if (j>13) j=11;
else
if (j>10) j=10;
else
if (j>8) j=9;
shade_gradient[i]= j+16;
}
}
};
GlobeStaticData static_data;
struct Ocean
{
static inline void func(Uint8& dest, const int&, const int&, const int&, const int&)
{
dest = Globe::OCEAN_COLOR;
}
};
struct CreateShadow
{
static inline Uint8 getShadowValue(const Cord& earth, const Cord& sun, const Sint16& noise)
{
Cord temp = earth;
//diff
temp -= sun;
//norm
temp.x *= temp.x;
temp.y *= temp.y;
temp.z *= temp.z;
temp.x += temp.z + temp.y;
//we have norm of distance between 2 vectors, now stored in `x`
temp.x -= 2;
temp.x *= 125.;
if (temp.x < -110)
temp.x = -31;
else if (temp.x > 120)
temp.x = 50;
else
temp.x = static_data.shade_gradient[(Sint16)temp.x + 120];
temp.x -= noise;
return Clamp(temp.x, 0., 31.);
}
static inline Uint8 getOceanShadow(const Uint8& shadow)
{
return Globe::OCEAN_COLOR + shadow;
}
static inline Uint8 getLandShadow(const Uint8& dest, const Uint8& shadow)
{
if (shadow == 0) return dest;
const int s = shadow / 3;
const int e = dest + s;
const int d = dest & helper::ColorGroup;
if (e > d + helper::ColorShade)
return d + helper::ColorShade;
return e;
}
static inline bool isOcean(const Uint8& dest)
{
return Globe::OCEAN_SHADING && dest >= Globe::OCEAN_COLOR && dest < Globe::OCEAN_COLOR + 32;
}
static inline void func(Uint8& dest, const Cord& earth, const Cord& sun, const Sint16& noise, const int&)
{
if (dest && earth.z)
{
const Uint8 shadow = getShadowValue(earth, sun, noise);
//this pixel is ocean
if (isOcean(dest))
{
dest = getOceanShadow(shadow);
}
//this pixel is land
else
{
dest = getLandShadow(dest, shadow);
}
}
else
{
dest = 0;
}
}
};
}//namespace
/**
* Sets up a globe with the specified size and position.
* @param game Pointer to core game.
* @param cenX X position of the center of the globe.
* @param cenY Y position of the center of the globe.
* @param width Width in pixels.
* @param height Height in pixels.
* @param x X position in pixels.
* @param y Y position in pixels.
*/
Globe::Globe(Game* game, int cenX, int cenY, int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _cenX(cenX), _cenY(cenY), _rotLon(0.0), _rotLat(0.0), _hoverLon(0.0), _hoverLat(0.0), _craftLon(0.0), _craftLat(0.0), _craftRange(0.0), _game(game), _hover(false), _craft(false), _blink(-1),
_isMouseScrolling(false), _isMouseScrolled(false), _xBeforeMouseScrolling(0), _yBeforeMouseScrolling(0), _lonBeforeMouseScrolling(0.0), _latBeforeMouseScrolling(0.0), _mouseScrollingStartTime(0), _totalMouseMoveX(0), _totalMouseMoveY(0), _mouseMovedOverThreshold(false)
{
_rules = game->getMod()->getGlobe();
_texture = new SurfaceSet(*_game->getMod()->getSurfaceSet("TEXTURE.DAT"));
_markerSet = new SurfaceSet(*_game->getMod()->getSurfaceSet("GlobeMarkers"));
_countries = new Surface(width, height, x, y);
_markers = new Surface(width, height, x, y);
_radars = new Surface(width, height, x, y);
_clipper = new FastLineClip(x, x+width, y, y+height);
// Animation timers
_blinkTimer = new Timer(100);
_blinkTimer->onTimer((SurfaceHandler)&Globe::blink);
_blinkTimer->start();
_rotTimer = new Timer(10);
_rotTimer->onTimer((SurfaceHandler)&Globe::rotate);
_cenLon = _game->getSavedGame()->getGlobeLongitude();
_cenLat = _game->getSavedGame()->getGlobeLatitude();
_zoom = _game->getSavedGame()->getGlobeZoom();
_zoomOld = _zoom;
setupRadii(width, height);
setZoom(_zoom);
//filling random noise "texture"
_randomNoiseData.resize(static_data.random_surf_size * static_data.random_surf_size);
for (size_t i=0; i<_randomNoiseData.size(); ++i)
_randomNoiseData[i] = rand()%4;
cachePolygons();
}
/**
* Deletes the contained surfaces.
*/
Globe::~Globe()
{
delete _blinkTimer;
delete _rotTimer;
delete _countries;
delete _markers;
delete _texture;
delete _markerSet;
delete _radars;
delete _clipper;
for (std::list<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); ++i)
{
delete *i;
}
}
/**
* Converts a polar point into a cartesian point for
* mapping a polygon onto the 3D-looking globe.
* @param lon Longitude of the polar point.
* @param lat Latitude of the polar point.
* @param x Pointer to the output X position.
* @param y Pointer to the output Y position.
*/
void Globe::polarToCart(double lon, double lat, Sint16 *x, Sint16 *y) const
{
// Orthographic projection
*x = _cenX + (Sint16)floor(_radius * cos(lat) * sin(lon - _cenLon));
*y = _cenY + (Sint16)floor(_radius * (cos(_cenLat) * sin(lat) - sin(_cenLat) * cos(lat) * cos(lon - _cenLon)));
}
void Globe::polarToCart(double lon, double lat, double *x, double *y) const
{
// Orthographic projection
*x = _cenX + _radius * cos(lat) * sin(lon - _cenLon);
*y = _cenY + _radius * (cos(_cenLat) * sin(lat) - sin(_cenLat) * cos(lat) * cos(lon - _cenLon));
}
/**
* Converts a cartesian point into a polar point for
* mapping a globe click onto the flat world map.
* @param x X position of the cartesian point.
* @param y Y position of the cartesian point.
* @param lon Pointer to the output longitude.
* @param lat Pointer to the output latitude.
*/
void Globe::cartToPolar(Sint16 x, Sint16 y, double *lon, double *lat) const
{
// Orthographic projection
x -= _cenX;
y -= _cenY;
double rho = sqrt((double)(x*x + y*y));
double c = asin(rho / _radius);
if ( AreSame(rho, 0.0) )
{
*lat = _cenLat;
*lon = _cenLon;
}
else
{
*lat = asin((y * sin(c) * cos(_cenLat)) / rho + cos(c) * sin(_cenLat));
*lon = atan2(x * sin(c),(rho * cos(_cenLat) * cos(c) - y * sin(_cenLat) * sin(c))) + _cenLon;
}
// Keep between 0 and 2xPI
while (*lon < 0)
*lon += 2 * M_PI;
while (*lon >= 2 * M_PI)
*lon -= 2 * M_PI;
}
/**
* Checks if a polar point is on the back-half of the globe,
* invisible to the player.
* @param lon Longitude of the point.
* @param lat Latitude of the point.
* @return True if it's on the back, False if it's on the front.
*/
bool Globe::pointBack(double lon, double lat) const
{
double c = cos(_cenLat) * cos(lat) * cos(lon - _cenLon) + sin(_cenLat) * sin(lat);
return c < 0.0;
}
Polygon* Globe::getPolygonFromLonLat(double lon, double lat) const
{
const double zDiscard=0.75f;
double coslat = cos(lat);
double sinlat = sin(lat);
for (std::list<Polygon*>::iterator i = _rules->getPolygons()->begin(); i != _rules->getPolygons()->end(); ++i)
{
double x, y, z, x2, y2;
double clat, clon;
z = 0;
for (int j = 0; j < (*i)->getPoints(); ++j)
{
z = coslat * cos((*i)->getLatitude(j)) * cos((*i)->getLongitude(j) - lon) + sinlat * sin((*i)->getLatitude(j));
if (z<zDiscard) break; //discarded
}
if (z<zDiscard) continue; //discarded
bool odd = false;
clat = (*i)->getLatitude(0); //initial point
clon = (*i)->getLongitude(0);
x = cos(clat) * sin(clon - lon);
y = coslat * sin(clat) - sinlat * cos(clat) * cos(clon - lon);
for (int j = 0; j < (*i)->getPoints(); ++j)
{
int k = (j + 1) % (*i)->getPoints(); //index of next point in poly
clat = (*i)->getLatitude(k);
clon = (*i)->getLongitude(k);
x2 = cos(clat) * sin(clon - lon);
y2 = coslat * sin(clat) - sinlat * cos(clat) * cos(clon - lon);
if ( ((y>0)!=(y2>0)) && (0 < (x2-x)*(0-y)/(y2-y)+x) )
odd = !odd;
x = x2;
y = y2;
}
if (odd) return *i;
}
return NULL;
}
/**
* Sets a leftwards rotation speed and starts the timer.
*/
void Globe::rotateLeft()
{
_rotLon = -ROTATE_LONGITUDE;
if (!_rotTimer->isRunning()) _rotTimer->start();
}
/**
* Sets a rightwards rotation speed and starts the timer.
*/
void Globe::rotateRight()
{
_rotLon = ROTATE_LONGITUDE;
if (!_rotTimer->isRunning()) _rotTimer->start();
}
/**
* Sets a upwards rotation speed and starts the timer.
*/
void Globe::rotateUp()
{
_rotLat = -ROTATE_LATITUDE;
if (!_rotTimer->isRunning()) _rotTimer->start();
}
/**
* Sets a downwards rotation speed and starts the timer.
*/
void Globe::rotateDown()
{
_rotLat = ROTATE_LATITUDE;
if (!_rotTimer->isRunning()) _rotTimer->start();
}
/**
* Resets the rotation speed and timer.
*/
void Globe::rotateStop()
{
_rotLon = 0.0;
_rotLat = 0.0;
_rotTimer->stop();
}
/**
* Resets longitude rotation speed and timer.
*/
void Globe::rotateStopLon()
{
_rotLon = 0.0;
if (AreSame(_rotLat, 0.0))
{
_rotTimer->stop();
}
}
/**
* Resets latitude rotation speed and timer.
*/
void Globe::rotateStopLat()
{
_rotLat = 0.0;
if (AreSame(_rotLon, 0.0))
{
_rotTimer->stop();
}
}
/**
* Changes the current globe zoom factor.
* @param zoom New zoom.
*/
void Globe::setZoom(size_t zoom)
{
_zoom = Clamp(zoom, (size_t)0u, _zoomRadius.size() - 1);
_zoomTexture = (2 - (int)floor(_zoom / 2.0)) * (_texture->getTotalFrames() / 3);
_radius = _zoomRadius[_zoom];
_game->getSavedGame()->setGlobeZoom(_zoom);
if (_isMouseScrolling)
{
_lonBeforeMouseScrolling = _cenLon;
_latBeforeMouseScrolling = _cenLat;
_totalMouseMoveX = 0; _totalMouseMoveY = 0;
}
invalidate();
}
/**
* Increases the zoom level on the globe.
*/
void Globe::zoomIn()
{
if (_zoom < _zoomRadius.size() - 1)
{
setZoom(_zoom + 1);
}
}
/**
* Decreases the zoom level on the globe.
*/
void Globe::zoomOut()
{
if (_zoom > 0)
{
setZoom(_zoom - 1);
}
}
/**
* Zooms the globe out as far as possible.
*/
void Globe::zoomMin()
{
if (_zoom > 0)
{
setZoom(0);
}
}
/**
* Zooms the globe in as close as possible.
*/
void Globe::zoomMax()
{
if (_zoom < _zoomRadius.size() - 1)
{
setZoom(_zoomRadius.size() - 1);
}
}
/**
* Stores the zoom used before a dogfight.
*/
void Globe::saveZoomDogfight()
{
_zoomOld = _zoom;
}
/**
* Zooms the globe smoothly into dogfight level.
* @return Is the globe already zoomed in?
*/
bool Globe::zoomDogfightIn()
{
if (_zoom < DOGFIGHT_ZOOM)
{
double radiusNow = _radius;
if (radiusNow + _radiusStep >= _zoomRadius[DOGFIGHT_ZOOM])
{
setZoom(DOGFIGHT_ZOOM);
}
else
{
if (radiusNow + _radiusStep >= _zoomRadius[_zoom + 1])
_zoom++;
setZoom(_zoom);
_radius = radiusNow + _radiusStep;
}
return false;
}
return true;
}
/**
* Zooms the globe smoothly out of dogfight level.
* @return Is the globe already zoomed out?
*/
bool Globe::zoomDogfightOut()
{
if (_zoom > _zoomOld)
{
double radiusNow = _radius;
if (radiusNow - _radiusStep <= _zoomRadius[_zoomOld])
{
setZoom(_zoomOld);
}
else
{
if (radiusNow - _radiusStep <= _zoomRadius[_zoom - 1])
_zoom--;
setZoom(_zoom);
_radius = radiusNow - _radiusStep;
}
return false;
}
return true;
}
/**
* Rotates the globe to center on a certain
* polar point on the world map.
* @param lon Longitude of the point.
* @param lat Latitude of the point.
*/
void Globe::center(double lon, double lat)
{
_cenLon = lon;
_cenLat = lat;
_game->getSavedGame()->setGlobeLongitude(_cenLon);
_game->getSavedGame()->setGlobeLatitude(_cenLat);
invalidate();
}
/**
* Checks if a polar point is inside the globe's landmass.
* @param lon Longitude of the point.
* @param lat Latitude of the point.
* @return True if it's inside, False if it's outside.
*/
bool Globe::insideLand(double lon, double lat) const
{
return (getPolygonFromLonLat(lon,lat))!=NULL;
}
/**
* Switches the amount of detail shown on the globe.
* With detail on, country and city details are shown when zoomed in.
*/
void Globe::toggleDetail()
{
Options::globeDetail = !Options::globeDetail;
drawDetail();
}
/**
* Checks if a certain target is near a certain cartesian point
* (within a circled area around it) over the globe.
* @param target Pointer to target.
* @param x X coordinate of point.
* @param y Y coordinate of point.
* @return True if it's near, false otherwise.
*/
bool Globe::targetNear(Target* target, int x, int y) const
{
Sint16 tx, ty;
if (pointBack(target->getLongitude(), target->getLatitude()))
return false;
polarToCart(target->getLongitude(), target->getLatitude(), &tx, &ty);
int dx = x - tx;
int dy = y - ty;
return (dx * dx + dy * dy <= NEAR_RADIUS);
}
/**
* Returns a list of all the targets currently near a certain
* cartesian point over the globe.
* @param x X coordinate of point.
* @param y Y coordinate of point.
* @param craft Only get craft targets.
* @return List of pointers to targets.
*/
std::vector<Target*> Globe::getTargets(int x, int y, bool craft) const
{
std::vector<Target*> v;
if (!craft)
{
for (std::vector<Base*>::iterator i = _game->getSavedGame()->getBases()->begin(); i != _game->getSavedGame()->getBases()->end(); ++i)
{
if ((*i)->getLongitude() == 0.0 && (*i)->getLatitude() == 0.0)
continue;
if (targetNear((*i), x, y))
{
v.push_back(*i);
}
for (std::vector<Craft*>::iterator j = (*i)->getCrafts()->begin(); j != (*i)->getCrafts()->end(); ++j)
{
if ((*j)->getLongitude() == (*i)->getLongitude() && (*j)->getLatitude() == (*i)->getLatitude() && (*j)->getDestination() == 0)
continue;
if (targetNear((*j), x, y))
{
v.push_back(*j);
}
}
}
}
for (std::vector<Ufo*>::iterator i = _game->getSavedGame()->getUfos()->begin(); i != _game->getSavedGame()->getUfos()->end(); ++i)
{
if (!(*i)->getDetected())
continue;
if (targetNear((*i), x, y))
{
v.push_back(*i);
}
}
for (std::vector<Waypoint*>::iterator i = _game->getSavedGame()->getWaypoints()->begin(); i != _game->getSavedGame()->getWaypoints()->end(); ++i)
{
if (targetNear((*i), x, y))
{
v.push_back(*i);
}
}
for (std::vector<MissionSite*>::iterator i = _game->getSavedGame()->getMissionSites()->begin(); i != _game->getSavedGame()->getMissionSites()->end(); ++i)
{
if (targetNear((*i), x, y))
{
v.push_back(*i);
}
}
for (std::vector<AlienBase*>::iterator i = _game->getSavedGame()->getAlienBases()->begin(); i != _game->getSavedGame()->getAlienBases()->end(); ++i)
{
if (!(*i)->isDiscovered())
{
continue;
}
if (targetNear((*i), x, y))
{
v.push_back(*i);
}
}
return v;
}
/**
* Takes care of pre-calculating all the polygons currently visible
* on the globe and caching them so they only need to be recalculated
* when the globe is actually moved.
*/
void Globe::cachePolygons()
{
cache(_rules->getPolygons(), &_cacheLand);
}
/**
* Caches a set of polygons.
* @param polygons Pointer to list of polygons.
* @param cache Pointer to cache.
*/
void Globe::cache(std::list<Polygon*> *polygons, std::list<Polygon*> *cache)
{
// Clear existing cache
for (std::list<Polygon*>::iterator i = cache->begin(); i != cache->end(); ++i)
{
delete *i;
}
cache->clear();
// Pre-calculate values to cache
for (std::list<Polygon*>::iterator i = polygons->begin(); i != polygons->end(); ++i)
{
// Is quad on the back face?
double closest = 0.0;
double z;
double furthest = 0.0;
for (int j = 0; j < (*i)->getPoints(); ++j)
{
z = cos(_cenLat) * cos((*i)->getLatitude(j)) * cos((*i)->getLongitude(j) - _cenLon) + sin(_cenLat) * sin((*i)->getLatitude(j));
if (z > closest)
closest = z;
else if (z < furthest)
furthest = z;
}
if (-furthest > closest)
continue;
Polygon* p = new Polygon(**i);
// Convert coordinates
for (int j = 0; j < p->getPoints(); ++j)
{
Sint16 x, y;
polarToCart(p->getLongitude(j), p->getLatitude(j), &x, &y);
p->setX(j, x);
p->setY(j, y);
}
cache->push_back(p);
}
}
/**
* Replaces a certain amount of colors in the palette of the globe.
* @param colors Pointer to the set of colors.
* @param firstcolor Offset of the first color to replace.
* @param ncolors Amount of colors to replace.
*/
void Globe::setPalette(SDL_Color *colors, int firstcolor, int ncolors)
{
Surface::setPalette(colors, firstcolor, ncolors);
_texture->setPalette(colors, firstcolor, ncolors);
_markerSet->setPalette(colors, firstcolor, ncolors);
_countries->setPalette(colors, firstcolor, ncolors);
_markers->setPalette(colors, firstcolor, ncolors);
_radars->setPalette(colors, firstcolor, ncolors);
}
/**
* Keeps the animation timers running.
*/
void Globe::think()
{
_blinkTimer->think(0, this);
_rotTimer->think(0, this);
}
/**
* Makes the globe markers blink.
*/
void Globe::blink()
{
_blink = -_blink;
for (std::map<int, Surface*>::iterator i = _markerSet->getFrames()->begin(); i != _markerSet->getFrames()->end(); ++i)
{
if (i->first != CITY_MARKER)
i->second->offset(_blink);
}
drawMarkers();
}
/**
* Rotates the globe by a set amount. Necessary
* since the globe keeps rotating while a button
* is pressed down.
*/
void Globe::rotate()
{
_cenLon += _rotLon * ((110 - Options::geoScrollSpeed) / 100.0) / (_zoom+1);
_cenLat += _rotLat * ((110 - Options::geoScrollSpeed) / 100.0) / (_zoom+1);
_game->getSavedGame()->setGlobeLongitude(_cenLon);
_game->getSavedGame()->setGlobeLatitude(_cenLat);
invalidate();
}
/**
* Draws the whole globe, part by part.
*/
void Globe::draw()
{
if (_redraw)
{
cachePolygons();
}
Surface::draw();
drawOcean();
drawLand();
drawRadars();
drawFlights();
drawShadow();
drawMarkers();
drawDetail();
}
/**
* Renders the ocean, shading it according to the time of day.
*/
void Globe::drawOcean()
{
lock();
drawCircle(_cenX+1, _cenY, _radius+20, OCEAN_COLOR);
// ShaderDraw<Ocean>(ShaderSurface(this));
unlock();
}
/**
* Renders the land, taking all the visible world polygons
* and texturing and shading them accordingly.
*/
void Globe::drawLand()
{
Sint16 x[4], y[4];
for (std::list<Polygon*>::iterator i = _cacheLand.begin(); i != _cacheLand.end(); ++i)
{
// Convert coordinates
for (int j = 0; j < (*i)->getPoints(); ++j)
{
x[j] = (*i)->getX(j);
y[j] = (*i)->getY(j);
}
// Apply textures according to zoom and shade
drawTexturedPolygon(x, y, (*i)->getPoints(), _texture->getFrame((*i)->getTexture() + _zoomTexture), 0, 0);
}
}
/**
* Get position of sun from point on globe
* @param lon longitude of position
* @param lat latitude of position
* @return position of sun
*/
Cord Globe::getSunDirection(double lon, double lat) const
{
const double curTime = _game->getSavedGame()->getTime()->getDaylight();
const double rot = curTime * 2*M_PI;
double sun;
if (Options::globeSeasons)
{
const int MonthDays1[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const int MonthDays2[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
int year=_game->getSavedGame()->getTime()->getYear();
int month=_game->getSavedGame()->getTime()->getMonth()-1;
int day=_game->getSavedGame()->getTime()->getDay()-1;
double tm = (double)(( _game->getSavedGame()->getTime()->getHour() * 60
+ _game->getSavedGame()->getTime()->getMinute() ) * 60
+ _game->getSavedGame()->getTime()->getSecond() ) / 86400; //day fraction is also taken into account
double CurDay;
if (year%4 == 0 && !(year%100 == 0 && year%400 != 0))
CurDay = (MonthDays2[month] + day + tm )/366 - 0.219; //spring equinox (start of astronomic year)
else
CurDay = (MonthDays1[month] + day + tm )/365 - 0.219;
if (CurDay<0) CurDay += 1.;
sun = -0.261 * sin(CurDay*2*M_PI);
}
else
sun = 0;
Cord sun_direction(cos(rot+lon), sin(rot+lon)*-sin(lat), sin(rot+lon)*cos(lat));
Cord pole(0, cos(lat), sin(lat));
if (sun>0)
sun_direction *= 1. - sun;
else
sun_direction *= 1. + sun;
pole *= sun;
sun_direction += pole;
double norm = sun_direction.norm();
//norm should be always greater than 0
norm = 1./norm;
sun_direction *= norm;
return sun_direction;
}
void Globe::drawShadow()
{
ShaderMove<Cord> earth = ShaderMove<Cord>(_earthData[_zoom], getWidth(), getHeight());
ShaderRepeat<Sint16> noise = ShaderRepeat<Sint16>(_randomNoiseData, static_data.random_surf_size, static_data.random_surf_size);
earth.setMove(_cenX-getWidth()/2, _cenY-getHeight()/2);
lock();
ShaderDraw<CreateShadow>(ShaderSurface(this), earth, ShaderScalar(getSunDirection(_cenLon, _cenLat)), noise);
unlock();
}
void Globe::XuLine(Surface* surface, Surface* src, double x1, double y1, double x2, double y2, int shade)
{
if (_clipper->LineClip(&x1,&y1,&x2,&y2) != 1) return; //empty line
double deltax = x2-x1, deltay = y2-y1;
bool inv;
Sint16 tcol;
double len,x0,y0,SX,SY;
if (abs((int)y2-(int)y1) > abs((int)x2-(int)x1))
{