-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamestate.cpp
3029 lines (2755 loc) · 102 KB
/
gamestate.cpp
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
//---------------------------------------------------------------------------
#include "stdafx.h"
#include <cassert>
#include <cerrno> // n.b., needed on Linux at least
#include <sstream>
using std::stringstream;
#include "gamestate.h"
#include "game.h"
#include "utils.h"
#include "sector.h"
#include "gui.h"
#include "player.h"
#include "screen.h"
#include "image.h"
#include "sound.h"
//---------------------------------------------------------------------------
const int defenders_frames_per_update_c = (int)(2.0 * ticks_per_frame_c * time_ratio_c); // consider a turn every this number of frames
const int soldier_move_rate_c = (int)(1.2 * ticks_per_frame_c * time_ratio_c); // ticks per pixel
const int air_move_rate_c = (int)(0.2 * ticks_per_frame_c * time_ratio_c); // ticks per pixel
const int soldier_turn_rate_c = (int)(20.0 * ticks_per_frame_c * time_ratio_c); // mean ticks per turn
//const int shield_step_y_c = 16;
const int shield_step_y_c = 20;
class Soldier {
static int sort_soldier_pair(const void *v1,const void *v2);
public:
int player;
int epoch;
int xpos, ypos;
AmmoDirection dir;
Soldier(int player,int epoch,int xpos,int ypos) {
ASSERT_S_EPOCH(epoch);
this->player = player;
this->epoch = epoch;
this->xpos = xpos;
this->ypos = ypos;
this->dir = (AmmoDirection)(rand() % 4);
}
static void sortSoldiers(Soldier **soldiers,int n_soldiers) {
qsort(soldiers, n_soldiers, sizeof( Soldier *), sort_soldier_pair);
}
};
int Soldier::sort_soldier_pair(const void *v1,const void *v2) {
Soldier *s1 = *(Soldier **)v1;
Soldier *s2 = *(Soldier **)v2;
/*if( s1->epoch >= 6 )
return 1;
else if( s2->epoch >= 6 )
return -1;
else*/
return (s1->ypos - s2->ypos);
}
void Feature::draw() const {
const int ticks_per_frame_c = 110; // tree animation looks better if offset from main animation, and if slightly slower
int counter = ( getRealTime() * time_rate ) / ticks_per_frame_c;
image[counter % n_frames]->draw(xpos, ypos);
}
TimedEffect::TimedEffect() {
this->timeset = getRealTime();
this->func_finish = NULL;
}
TimedEffect::TimedEffect(int delay, void (*func_finish)()) {
this->timeset = getRealTime() + delay;
this->func_finish = func_finish;
}
const int ammo_time_c = 1000;
const float ammo_speed_c = 1.5f; // higher is faster
AmmoEffect::AmmoEffect(PlayingGameState *gamestate,int epoch, AmmoDirection dir, int xpos, int ypos) : TimedEffect(), gamestate(gamestate) {
ASSERT_EPOCH(epoch);
this->gametimeset = getGameTime();
this->epoch = epoch;
this->dir = dir;
this->xpos = xpos;
this->ypos = ypos;
}
bool AmmoEffect::render() const {
int time = getRealTime() - this->timeset;
if( time < 0 )
return false;
int gametime = getGameTime() - this->gametimeset;
int x = xpos;
int y = ypos;
int dist = (int)(gametime * ammo_speed_c);
if( dir == ATTACKER_AMMO_BOMB )
dist /= 2;
if( dir == ATTACKER_AMMO_DOWN )
y += dist;
else if( dir == ATTACKER_AMMO_UP )
y -= dist;
else if( dir == ATTACKER_AMMO_LEFT )
x -= dist;
else if( dir == ATTACKER_AMMO_RIGHT )
x += dist;
else if( dir == ATTACKER_AMMO_BOMB )
y += dist;
else {
ASSERT(0);
}
Image *image = attackers_ammo[epoch][dir];
if( dir == ATTACKER_AMMO_BOMB && dist > 24 ) {
if( explosions[0] != NULL ) {
int w = image->getScaledWidth();
int w2 = explosions[0]->getScaledWidth();
gamestate->explosionEffect(offset_land_x_c + x + (w-w2)/2, offset_land_y_c + y);
}
return true;
}
if( x < 0 || y < 0 )
return true;
x += offset_land_x_c;
y += offset_land_y_c;
if( x + image->getScaledWidth() >= screen->getWidth() || y + image->getScaledHeight() >= screen->getHeight() )
return true;
image->draw(x, y);
if( time > ammo_time_c )
return true;
return false;
}
const int fade_time_c = 1000;
const int whitefade_time_c = 1000;
FadeEffect::FadeEffect(bool white,bool out,int delay, void (*func_finish)()) : TimedEffect(delay, func_finish) {
this->white = white;
this->out = out;
#if SDL_MAJOR_VERSION == 1
this->image = Image::createBlankImage(screen->getWidth(), screen->getHeight(), 24);
int r = 0, g = 0, b = 0;
if( white ) {
r = g = b = 255;
}
else {
r = g = b = 0;
}
image->fillRect(0, 0, screen->getWidth(), screen->getHeight(), r, g, b);
this->image->convertToDisplayFormat();
#else
image = NULL;
#endif
}
FadeEffect::~FadeEffect() {
#if SDL_MAJOR_VERSION == 1
delete image;
#endif
}
bool FadeEffect::render() const {
int time = getRealTime() - this->timeset;
int length = white ? whitefade_time_c : fade_time_c;
if( time < 0 )
return false;
double alpha = 0.0;
if( white ) {
alpha = ((double)time) / (0.5 * (double)length);
if( alpha > 2.0 )
alpha = 2.0;
if( time > length/2.0 ) {
alpha = 2.0 - alpha;
}
}
else {
alpha = ((double)time) / (double)length;
if( alpha > 1.0 )
alpha = 1.0;
if( !out )
alpha = 1.0 - alpha;
}
#if SDL_MAJOR_VERSION == 1
image->drawWithAlpha(0, 0, (unsigned char)(alpha * 255));
#else
unsigned char value = white ? 255 : 0;
screen->fillRectWithAlpha(0, 0, screen->getWidth(), screen->getHeight(), value, value, value, (unsigned char)(alpha * 255));
#endif
if( time > length ) // we still need to draw the fade, on the last time
return true;
return false;
}
const int flashingsquare_flash_time_c = 250;
const int flashing_square_n_flashes_c = 8;
bool FlashingSquare::render() const {
int time = getRealTime() - this->timeset;
if( time < 0 )
return false;
if( time > flashingsquare_flash_time_c * flashing_square_n_flashes_c ) {
return true;
}
bool flash = ( time / flashingsquare_flash_time_c ) % 2 == 0;
if( flash ) {
int map_x = offset_map_x_c + 16 * this->xpos;
int map_y = offset_map_y_c + 16 * this->ypos;
flashingmapsquare->draw(map_x, map_y);
}
return false;
}
bool AnimationEffect::render() const {
int time = getRealTime() - this->timeset;
if( time < 0 )
return false;
int frame = time / time_per_frame;
if( frame >= n_images )
return true;
//this->finished = true;
else {
if( !dir )
frame = n_images - 1 - frame;
images[frame]->draw(xpos, ypos);
}
return false;
}
bool TextEffect::render() const {
int time = getRealTime() - this->timeset;
if( time < 0 )
return false;
else if( time > duration )
return true;
Image::write(xpos, ypos, letters_small, text.c_str(), Image::JUSTIFY_CENTRE);
return false;
}
GameState::GameState(int client_player) : client_player(client_player) {
this->fade = NULL;
this->whitefade = NULL;
//this->effects = new Vector();
//this->effects = new vector<TimedEffect *>();
this->screen_page = new PanelPage(0, 0);
this->screen_page->setTolerance(0);
this->mobile_ui_display_mouse = false;
this->mouse_image = NULL;
this->mouse_off_x = 0;
this->mouse_off_y = 0;
this->confirm_type = CONFIRMTYPE_UNKNOWN;
this->confirm_window = NULL;
this->confirm_yes_button = NULL;
this->confirm_no_button = NULL;
}
GameState::~GameState() {
LOG("~GameState()\n");
/*for(int i=0;i<effects->size();i++) {
//TimedEffect *effect = (TimedEffect *)effects->get(i);
TimedEffect *effect = effects->at(i);
delete effect;
}
delete effects;*/
if( fade != NULL )
delete fade;
if( whitefade != NULL )
delete whitefade;
if( this->screen_page )
delete screen_page;
LOG("~GameState() done\n");
}
void GameState::reset() {
//LOG("GameState::reset()\n");
this->screen_page->free(true);
}
void GameState::setDefaultMouseImage() {
if( isDemo() )
mouse_image = mouse_pointers[0];
else
mouse_image = mouse_pointers[client_player];
mobile_ui_display_mouse = false;
}
void GameState::draw() {
if( mouse_image != NULL ) {
bool touch_mode = mobile_ui || application->isBlankMouse();
if( touch_mode && mobile_ui_display_mouse ) {
mouse_image->draw(default_width_c - mouse_image->getScaledWidth(), 0);
}
else if( !touch_mode ) {
int m_x = 0, m_y = 0;
screen->getMouseCoords(&m_x, &m_y);
m_x = (int)(m_x / scale_width);
m_y = (int)(m_y / scale_height);
m_x += mouse_off_x;
m_y += mouse_off_y;
mouse_image->draw(m_x, m_y);
}
}
if( fade ) {
if( fade->render() ) {
FadeEffect *copy = fade;
delete fade;
if( copy == fade )
fade = NULL;
}
}
if( whitefade ) {
if( whitefade->render() ) {
FadeEffect *copy = whitefade;
delete whitefade;
if( copy == whitefade )
whitefade = NULL;
}
}
if( isPaused() ) {
string str = mobile_ui ? "touch screen\nto unpause game" : "press p or click\nmouse to unpause game";
// n.b., don't use 120 for y pos, need to avoid collision with quit game message
// and offset x pos slightly, to avoid overlapping with GUI
Image::write(120, 100, letters_small, str.c_str(), Image::JUSTIFY_LEFT);
}
if( application->hasFPS() ) {
float fps = application->getFPS();
if( fps > 0.0f ) {
stringstream str;
str << fps;
Image::writeMixedCase(4, default_height_c - 16, letters_large, letters_small, numbers_white, str.str().c_str(), Image::JUSTIFY_LEFT);
}
}
screen->refresh();
}
void GameState::mouseClick(int m_x,int m_y,bool m_left,bool m_middle,bool m_right,bool click) {
this->screen_page->input(m_x, m_y, m_left, m_middle, m_right, click);
}
void GameState::requestQuit() {
application->setQuit();
}
void GameState::createQuitWindow() {
if( confirm_window == NULL && !state_changed ) {
confirm_type = CONFIRMTYPE_QUITGAME;
confirm_window = new PanelPage(120, 120, 64, 32);
Button *text_button = new Button(0, 0, "REALLY QUIT?", letters_small);
confirm_window->add(text_button);
confirm_yes_button = new Button(0, 16, "YES", letters_small);
confirm_window->add(confirm_yes_button);
confirm_no_button = new Button(32, 16, "NO", letters_small);
confirm_window->add(confirm_no_button);
screen_page->add(confirm_window);
}
}
void GameState::closeConfirmWindow() {
//LOG("GameState::closeConfirmWindow()\n");
if( confirm_window != NULL ) {
delete confirm_window;
confirm_window = NULL;
confirm_yes_button = NULL;
confirm_no_button = NULL;
}
//LOG("GameState::closeConfirmWindow() done\n");
}
ChooseGameTypeGameState::ChooseGameTypeGameState(int client_player) : GameState(client_player) {
this->choosegametypePanel = NULL;
}
ChooseGameTypeGameState::~ChooseGameTypeGameState() {
LOG("~ChooseGameTypeGameState()\n");
if( this->choosegametypePanel )
delete choosegametypePanel;
LOG("~ChooseGameTypeGameState() done\n");
}
ChooseGameTypePanel *ChooseGameTypeGameState::getChooseGameTypePanel() {
return this->choosegametypePanel;
}
void ChooseGameTypeGameState::reset() {
//LOG("ChooseGameTypeGameState::reset()\n");
this->screen_page->free(true);
if( this->choosegametypePanel != NULL ) {
delete this->choosegametypePanel;
this->choosegametypePanel = NULL;
}
this->choosegametypePanel = new ChooseGameTypePanel();
}
void ChooseGameTypeGameState::draw() {
#if defined(__ANDROID__)
screen->clear(); // SDL on Android requires screen be cleared (otherwise we get corrupt regions outside of the main area)
#endif
background->draw(0, 0);
this->choosegametypePanel->draw();
this->screen_page->draw();
//this->screen_page->drawPopups();
GameState::setDefaultMouseImage();
GameState::draw();
}
void ChooseGameTypeGameState::mouseClick(int m_x,int m_y,bool m_left,bool m_middle,bool m_right,bool click) {
GameState::mouseClick(m_x, m_y, m_left, m_middle, m_right, click);
this->choosegametypePanel->input(m_x, m_y, m_left, m_middle, m_right, click);
}
ChooseDifficultyGameState::ChooseDifficultyGameState(int client_player) : GameState(client_player) {
this->choosedifficultyPanel = NULL;
}
ChooseDifficultyGameState::~ChooseDifficultyGameState() {
LOG("~ChooseDifficultyGameState()\n");
if( this->choosedifficultyPanel )
delete choosedifficultyPanel;
LOG("~ChooseDifficultyGameState() done\n");
}
ChooseDifficultyPanel *ChooseDifficultyGameState::getChooseDifficultyPanel() {
return this->choosedifficultyPanel;
}
void ChooseDifficultyGameState::reset() {
//LOG("ChooseDifficultyGameState::reset()\n");
this->screen_page->free(true);
if( this->choosedifficultyPanel != NULL ) {
delete this->choosedifficultyPanel;
this->choosedifficultyPanel = NULL;
}
this->choosedifficultyPanel = new ChooseDifficultyPanel();
}
void ChooseDifficultyGameState::draw() {
#if defined(__ANDROID__)
screen->clear(); // SDL on Android requires screen be cleared (otherwise we get corrupt regions outside of the main area)
#endif
background->draw(0, 0);
this->choosedifficultyPanel->draw();
this->screen_page->draw();
GameState::setDefaultMouseImage();
GameState::draw();
}
void ChooseDifficultyGameState::mouseClick(int m_x,int m_y,bool m_left,bool m_middle,bool m_right,bool click) {
GameState::mouseClick(m_x, m_y, m_left, m_middle, m_right, click);
this->choosedifficultyPanel->input(m_x, m_y, m_left, m_middle, m_right, click);
}
ChoosePlayerGameState::ChoosePlayerGameState(int client_player) : GameState(client_player), button_red(NULL), button_yellow(NULL), button_green(NULL), button_blue(NULL) {
}
ChoosePlayerGameState::~ChoosePlayerGameState() {
LOG("~ChoosePlayerGameState()\n");
LOG("~ChoosePlayerGameState() done\n");
}
void ChoosePlayerGameState::reset() {
this->screen_page->free(true);
const int xpos = 64;
int ypos = 48;
const int ydiff = 48;
const int xindent = 8;
const int ysmalldiff = 10;
button_red = new Button(xpos, ypos, 3*ysmalldiff, "CONTROLLER OF THE RED PEOPLE", letters_large);
screen_page->add(new Button(xpos+xindent, ypos+ysmalldiff, "SPECIAL SKILL STRENGTH", letters_small));
screen_page->add(new Button(xpos+xindent, ypos+2*ysmalldiff, "UNARMED MEN ARE STRONGER IN COMBAT", letters_small));
ypos += ydiff;
screen_page->add(button_red);
button_yellow = new Button(xpos, ypos, 3*ysmalldiff, "CONTROLLER OF THE YELLOW PEOPLE", letters_large);
screen_page->add(new Button(xpos+xindent, ypos+ysmalldiff, "SPECIAL SKILL DIPLOMACY", letters_small));
screen_page->add(new Button(xpos+xindent, ypos+2*ysmalldiff, "EASIER TO FORM ALLIANCES", letters_small));
ypos += ydiff;
screen_page->add(button_yellow);
button_green = new Button(xpos, ypos, 3*ysmalldiff, "CONTROLLER OF THE GREEN PEOPLE", letters_large);
screen_page->add(new Button(xpos+xindent, ypos+ysmalldiff, "SPECIAL SKILL CONSTRUCTION", letters_small));
screen_page->add(new Button(xpos+xindent, ypos+2*ysmalldiff, "FASTER AT BUILDING NEW TOWERS", letters_small));
ypos += ydiff;
screen_page->add(button_green);
button_blue = new Button(xpos, ypos, 3*ysmalldiff, "CONTROLLER OF THE BLUE PEOPLE", letters_large);
screen_page->add(new Button(xpos+xindent, ypos+ysmalldiff, "SPECIAL SKILL DEFENCE", letters_small));
screen_page->add(new Button(xpos+xindent, ypos+2*ysmalldiff, "BUILDINGS STRONGER AGAINST ATTACK", letters_small));
ypos += ydiff;
screen_page->add(button_blue);
}
void ChoosePlayerGameState::draw() {
#if defined(__ANDROID__)
screen->clear(); // SDL on Android requires screen be cleared (otherwise we get corrupt regions outside of the main area)
#endif
//player_select->draw(0, 0, false);
background->draw(0, 0);
Image::writeMixedCase(160, 16, letters_large, letters_small, NULL, "Select a Player", Image::JUSTIFY_CENTRE);
this->screen_page->draw();
GameState::setDefaultMouseImage();
GameState::draw();
}
PlaceMenGameState::PlaceMenGameState(int client_player) : GameState(client_player), start_map_x(-1), start_map_y(-1) {
this->off_x = (int)(0.25 * default_width_c);
this->choosemenPanel = NULL;
for(int y=0;y<map_height_c;y++) {
for(int x=0;x<map_width_c;x++) {
map_panels[x][y] = NULL;
}
}
}
PlaceMenGameState::~PlaceMenGameState() {
LOG("~PlaceMenGameState()\n");
if( this->choosemenPanel )
delete choosemenPanel;
LOG("~PlaceMenGameState() done\n");
}
ChooseMenPanel *PlaceMenGameState::getChooseMenPanel() {
return this->choosemenPanel;
}
const PanelPage *PlaceMenGameState::getMapPanel(int x, int y) const {
ASSERT( x >= 0 && x < map_width_c );
ASSERT( y >= 0 && y < map_height_c );
return this->map_panels[x][y];
}
PanelPage *PlaceMenGameState::getMapPanel(int x, int y) {
ASSERT( x >= 0 && x < map_width_c );
ASSERT( y >= 0 && y < map_height_c );
return this->map_panels[x][y];
}
void PlaceMenGameState::reset() {
//LOG("PlaceMenGameState::reset()\n");
/*if( !_CrtCheckMemory() ) {
throw "_CrtCheckMemory FAILED";
}*/
this->screen_page->free(true);
if( this->choosemenPanel != NULL ) {
delete this->choosemenPanel;
this->choosemenPanel = NULL;
}
this->choosemenPanel = new ChooseMenPanel(this);
// setup screen_page buttons
for(int y=0;y<map_height_c;y++) {
for(int x=0;x<map_width_c;x++) {
map_panels[x][y] = NULL;
if( getMap()->isSectorAt(x, y) ) {
//int map_x = offset_map_x_c + 16 * x;
int map_x = this->off_x - 8 * map_width_c + 16 * x;
int map_y = offset_map_y_c + 16 * y;
PanelPage *panel = new PanelPage(map_x, map_y, 16, 16);
panel->setInfoLMB("place starting tower\nin this sector");
panel->setEnabled(false);
screen_page->add(panel);
map_panels[x][y] = panel;
}
}
}
/*if( !_CrtCheckMemory() ) {
throw "_CrtCheckMemory FAILED";
}*/
}
void PlaceMenGameState::draw() {
char buffer[256] = "";
#if defined(__ANDROID__)
screen->clear(); // SDL on Android requires screen be cleared (otherwise we get corrupt regions outside of the main area)
#endif
background_islands->draw(0, 0);
if( !using_old_gfx ) {
sprintf(buffer, "Gigalomania v%d.%d", majorVersion, minorVersion);
Image::writeMixedCase(160, 228, letters_large, letters_small, numbers_white, buffer, Image::JUSTIFY_CENTRE);
}
/*this->choosemenPanel->draw();
this->screen_page->draw();
GameState::draw();
return;*/
int l_h = letters_large[0]->getScaledHeight();
int s_h = letters_small[0]->getScaledHeight();
const int cx = this->off_x;
//int cy = 100;
int cy = 120;
Image::writeMixedCase(cx, cy, letters_large, letters_small, NULL, map->getName(), Image::JUSTIFY_CENTRE);
cy += s_h + 2;
Image::writeMixedCase(cx, cy, letters_large, letters_small, NULL, "of the", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
sprintf(buffer, "%s AGE", epoch_names[start_epoch]);
Image::writeMixedCase(cx, cy, letters_large, letters_small, NULL, buffer, Image::JUSTIFY_CENTRE);
cy += l_h + 2;
int year = epoch_dates[start_epoch];
bool shiny = start_epoch == n_epochs_c-1;
Image::writeNumbers(cx+8, cy, shiny ? numbers_largeshiny : numbers_largegrey, abs(year),Image::JUSTIFY_RIGHT);
Image *era = ( year < 0 ) ? icon_bc :
shiny ? icon_ad_shiny : icon_ad;
era->draw(cx+8, cy);
cy += l_h + 2;
if( !isDemo() && gameType == GAMETYPE_ALLISLANDS ) {
int n_suspended = getNSuspended();
if( n_suspended > 0 )
{
sprintf(buffer, "Saved Men %d", n_suspended);
Image::writeMixedCase(cx, cy, letters_large, letters_small, numbers_white, buffer, Image::JUSTIFY_CENTRE);
}
}
/*cy += l_h + 2;
cy += l_h + 2;
if( choosemenPanel->getPage() == ChooseMenPanel::STATE_LOADGAME ) {
Image::write(cx, cy, letters_large, "LOAD", Image::JUSTIFY_CENTRE, true);
}
else if( choosemenPanel->getPage() == ChooseMenPanel::STATE_SAVEGAME ) {
Image::write(cx, cy, letters_large, "SAVE", Image::JUSTIFY_CENTRE, true);
}
cy += s_h + 2;*/
if( choosemenPanel->getPage() == ChooseMenPanel::STATE_CHOOSEMEN ) {
cy = 60;
Image::writeMixedCase(200, cy, letters_large, letters_small, NULL, "Click on the icon below", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
Image::writeMixedCase(200, cy, letters_large, letters_small, NULL, "to choose how many men", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
Image::writeMixedCase(200, cy, letters_large, letters_small, NULL, "to play with", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
Image::writeMixedCase(200, cy, letters_large, letters_small, NULL, "then click on the map", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
Image::writeMixedCase(200, cy, letters_large, letters_small, NULL, "to the left", Image::JUSTIFY_CENTRE);
cy += l_h + 2;
}
// map
/*for(int y=0;y<map_height_c;y++) {
for(int x=0;x<map_width_c;x++) {
if( map->sector_at[x][y] ) {
int map_x = offset_map_x_c - 3 + 16 * x;
int map_y = offset_map_y_c - 3 + 16 * y;
map_sq[15]->draw(map_x, map_y, true);
}
}
}*/
//map->draw(offset_map_x_c, offset_map_y_c);
map->draw(cx - 8*map_width_c, offset_map_y_c);
this->choosemenPanel->draw();
//this->choosemenPanel->drawPopups();
this->screen_page->draw();
//this->screen_page->drawPopups();
GameState::setDefaultMouseImage();
GameState::draw();
}
void PlaceMenGameState::requestNewGame() {
if( confirm_window != NULL ) {
this->closeConfirmWindow();
}
confirm_window = new PanelPage(120, 120, 64, 32);
confirm_type = CONFIRMTYPE_NEWGAME;
Button *text_button = new Button(0, 0, "NEW GAME", letters_small);
confirm_window->add(text_button);
confirm_yes_button = new Button(0, 16, "YES", letters_small);
confirm_window->add(confirm_yes_button);
confirm_no_button = new Button(32, 16, "NO", letters_small);
confirm_window->add(confirm_no_button);
this->screen_page->add(confirm_window);
}
void PlaceMenGameState::requestQuit() {
if( choosemenPanel->getPage() == ChooseMenPanel::STATE_CHOOSEMEN && !state_changed ) {
choosemenPanel->setPage(ChooseMenPanel::STATE_CHOOSEISLAND);
}
else {
this->createQuitWindow();
}
}
PlayingGameState::PlayingGameState(int client_player) : GameState(client_player) {
this->current_sector = NULL;
this->flag_frame_step = 0;
this->defenders_last_frame_update = 0;
this->soldiers_last_time_moved_x = 0;
this->soldiers_last_time_moved_y = 0;
this->soldiers_last_time_turned = 0;
this->air_last_time_moved = 0;
this->text_effect = NULL;
this->speed_button = NULL;
for(int i=0;i<n_players_c;i++) {
this->shield_buttons[i] = NULL;
this->shield_number_panels[i] = NULL;
}
this->shield_blank_button = NULL;
this->land_panel = NULL;
this->pause_button = NULL;
this->quit_button = NULL;
this->gamePanel = NULL;
this->selected_army = NULL;
this->map_display = MAPDISPLAY_MAP;
//this->map_display = MAPDISPLAY_UNITS;
this->player_asking_alliance = -1;
/*for(i=0;i<n_players_c;i++)
this->n_soldiers[i] = 0;*/
for(int i=0;i<n_players_c;i++)
for(int j=0;j<=n_epochs_c;j++)
this->n_deaths[i][j] = 0;
//this->refreshSoldiers(false);
for(int y=0;y<map_height_c;y++) {
for(int x=0;x<map_width_c;x++) {
map_panels[x][y] = NULL;
}
}
alliance_yes = NULL;
alliance_no = NULL;
}
PlayingGameState::~PlayingGameState() {
LOG("~PlayingGameState()\n");
s_biplane->fadeOut(500);
s_jetplane->fadeOut(500);
s_spaceship->fadeOut(500);
for(size_t i=0;i<effects.size();i++) {
TimedEffect *effect = effects.at(i);
delete effect;
}
for(size_t i=0;i<ammo_effects.size();i++) {
TimedEffect *effect = ammo_effects.at(i);
delete effect;
}
if( text_effect != NULL ) {
delete text_effect;
}
if( this->gamePanel )
delete gamePanel;
LOG("~PlayingGameState() done\n");
}
bool PlayingGameState::readSectorsProcessLine(Map *map, char *line, bool *done_header, int *sec_x, int *sec_y) {
bool ok = true;
line[ strlen(line) - 1 ] = '\0'; // trim new line
line[ strlen(line) - 1 ] = '\0'; // trim carriage return
if( !(*done_header) ) {
if( line[0] != '#' ) {
LOG("expected first character to be '#'\n");
ok = false;
return ok;
}
// ignore rest of header
*done_header = true;
}
else {
char *line_ptr = line;
while( *line_ptr == ' ' || *line_ptr == '\t' ) // avoid initial whitespace
line_ptr++;
char *comment = strchr(line_ptr, '#');
if( comment != NULL ) { // trim comments
*comment = '\0';
}
char *ptr = strtok(line_ptr, " ");
if( ptr == NULL ) {
LOG("can't find first word\n");
ok = false;
return ok;
}
else if( strcmp(ptr, "SECTOR") == 0 ) {
ptr = strtok(NULL, " ");
if( ptr == NULL ) {
LOG("can't find sec_x\n");
ok = false;
return ok;
}
*sec_x = atoi(ptr);
if( *sec_x < 0 || *sec_x >= map_width_c ) {
LOG("invalid map x %d\n", *sec_x);
ok = false;
return ok;
}
ptr = strtok(NULL, " ");
if( ptr == NULL ) {
LOG("can't find sec_y\n");
ok = false;
return ok;
}
*sec_y = atoi(ptr);
if( *sec_y < 0 || *sec_y >= map_height_c ) {
LOG("invalid map y %d\n", *sec_y);
ok = false;
return ok;
}
}
else if( strcmp(ptr, "ELEMENT") == 0 ) {
if( *sec_x == -1 || *sec_y == -1 ) {
LOG("sector not defined\n");
ok = false;
return ok;
}
ptr = strtok(NULL, " ");
if( ptr == NULL ) {
LOG("can't find element name\n");
ok = false;
return ok;
}
/*char elementname[MAX_LINE+1] = "";
strcpy(elementname, ptr);*/
string elementname = ptr;
ptr = strtok(NULL, " ");
if( ptr == NULL ) {
LOG("can't find n_elements\n");
ok = false;
return ok;
}
int n_elements = atoi(ptr);
Id element = UNDEFINED;
for(int i=0;i<N_ID;i++) {
if( strcmp( elements[i]->getName(), elementname.c_str() ) == 0 ) {
element = (Id)i;
}
}
if( element == UNDEFINED ) {
LOG("unknown element: %s\n", elementname.c_str());
ok = false;
return ok;
}
map->getSector(*sec_x, *sec_y)->setElements(element, n_elements);
}
else {
LOG("unknown word: %s\n", ptr);
ok = false;
return ok;
}
}
return ok;
}
bool PlayingGameState::readSectors(Map *map) {
bool ok = true;
const int MAX_LINE = 4096;
char line[MAX_LINE+1] = "";
int sec_x = -1, sec_y = -1;
bool done_header = false;
char fullname[4096] = "";
sprintf(fullname, "%s/%s", maps_dirname, map->getFilename());
// open in binary mode, so that we parse files in an OS-independent manner
// (otherwise, Windows will parse "\r\n" as being "\n", but Linux will still read it as "\n")
//FILE *file = fopen(fullname, "rb");
SDL_RWops *file = SDL_RWFromFile(fullname, "rb");
#if !defined(__ANDROID__) && defined(__linux)
if( file == NULL ) {
LOG("searching in /usr/share/gigalomania/ for islands folder\n");
sprintf(fullname, "%s/%s", alt_maps_dirname, map->getFilename());
file = SDL_RWFromFile(fullname, "rb");
}
#endif
if( file == NULL ) {
LOG("failed to open file: %s\n", fullname);
//perror("perror returns: ");
return false;
}
char buffer[MAX_LINE+1] = "";
int buffer_offset = 0;
bool reached_end = false;
int newline_index = 0;
while( ok ) {
bool done = readLineFromRWOps(ok, file, buffer, line, MAX_LINE, buffer_offset, newline_index, reached_end);
if( !ok ) {
LOG("failed to read line\n");
}
else if( done ) {
break;
}
else {
ok = readSectorsProcessLine(map, line, &done_header, &sec_x, &sec_y);
}
}
file->close(file);
return ok;
}
void PlayingGameState::createSectors(int x, int y, int n_men) {
LOG("PlayingGameState::createSectors(%d, %d, %d)\n", x, y, n_men);
map->createSectors(this, start_epoch);
Sector *sector = map->getSector(x, y);
current_sector = sector;
if( !isDemo() ) {
sector->createTower(client_player, n_men);
}
//current_sector->createTower(human_player, 10);
//current_sector->getArmy(enemy_player)->soldiers[10] = 0;
//map->sectors[2][2]->getArmy(enemy_player)->soldiers[0] = 10;
//Sector *enemy_sector = map->sectors[1][2];
for(int i=0;i<n_players_c;i++) {
if( i == client_player || players[i] == NULL )
continue;
int ex = 0, ey = 0;
while( true ) {
map->findRandomSector(&ex, &ey);
ASSERT( map->isSectorAt(ex, ey) );
if( map->getSector(ex, ey)->getPlayer() == -1 && !map->isReserved(ex, ey) )
break;
}
//Sector *enemy_sector = map->sectors[ex][ey];
Sector *enemy_sector = map->getSector(ex, ey);
//enemy_sector->getArmy(enemy_player)->soldiers[10] = 30;
//enemy_sector->createTower(enemy_player, 12);
if( start_epoch == end_epoch_c ) {
//players[i]->n_men_for_this_island = n_suspended[i];
players[i]->setNMenForThisIsland(100);
}
else {
players[i]->setNMenForThisIsland(20 + 5*start_epoch);
// total: 360*3 + 65 = 1145 men
}
LOG("Enemy %d created at %d , %d\n", i, ex, ey);
enemy_sector->createTower(i, players[i]->getNMenForThisIsland());
//enemy_sector->createTower(enemy_player, 20);
//enemy_sector->createTower(enemy_player, 200);
}
if( !readSectors(map) ) {
LOG("failed to read map sector info!\n");
ASSERT(false);
}
}
GamePanel *PlayingGameState::getGamePanel() {
return this->gamePanel;
}
/*Sector *PlayingGameState::getCurrentSector() {
return current_sector;
}*/
const Sector *PlayingGameState::getCurrentSector() const {
return current_sector;
}
bool PlayingGameState::viewingActiveClientSector() const {
return this->getCurrentSector()->getActivePlayer() == client_player;
}
bool PlayingGameState::viewingAnyClientSector() const {
// includes shutdown sectors
return this->getCurrentSector()->getPlayer() == client_player;
}
bool PlayingGameState::openPitMine() {
if( current_sector->getActivePlayer() != -1 && current_sector->getBuilding(BUILDING_MINE) == NULL && current_sector->getEpoch() >= 1 ) {
for(int i=0;i<N_ID;i++) {
if( current_sector->anyElements((Id)i) ) {
Element *element = ::elements[i];
if( element->getType() == Element::OPENPITMINE )
return true;
}
}
}