-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
4366 lines (3980 loc) · 141 KB
/
game.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 <ctime>
#include <cerrno> // n.b., needed on Linux at least
#include <sstream>
using std::stringstream;
#ifdef _WIN32
#include <windows.h>
#endif
#ifndef _WIN32
#include <dirent.h>
#include <string.h>
#endif
#ifdef AROS
#include <proto/dos.h>
#endif
#include "game.h"
#include "utils.h"
#include "sector.h"
#include "gamestate.h"
#include "gui.h"
#include "player.h"
#include "screen.h"
#include "image.h"
#include "sound.h"
//---------------------------------------------------------------------------
// onemousebutton means UI can be used with one mouse button only
#if defined(__ANDROID__)
bool onemousebutton = true;
#else
bool onemousebutton = false;
#endif
bool oneMouseButtonMode() {
return onemousebutton || application->isBlankMouse();
}
// mobile_ui means no mouse pointer
#if defined(__ANDROID__)
bool mobile_ui = true;
#else
bool mobile_ui = false;
#endif
bool using_old_gfx = false;
bool is_testing = false;
Application *application = NULL;
const char *maps_dirname = "islands";
#if !defined(__ANDROID__) && defined(__linux)
const char *alt_maps_dirname = "/usr/share/gigalomania/islands";
#endif
//int lastmouseclick_time = 0;
float scale_factor_w = 1.0f; // how much the input graphics are scaled
float scale_factor_h = 1.0f;
float scale_width = 0.0f; // the scale of the logical resolution or graphics size wrt the default 320x240 coordinate system
float scale_height = 0.0f;
int offset_flag_x_c = 22;
int offset_flag_y_c = 6;
bool use_amigadata = true;
GameMode gameMode = GAMEMODE_SINGLEPLAYER;
//GameMode gameMode = GAMEMODE_MULTIPLAYER_CLIENT;
GameType gameType = GAMETYPE_SINGLEISLAND;
//GameType gameType = GAMETYPE_ALLISLANDS;
DifficultyLevel difficulty_level = DIFFICULTY_EASY;
//Image *player_select = NULL;
Image *background = NULL;
Image *land[MAP_N_COLOURS];
Image *fortress[n_epochs_c];
Image *mine[n_epochs_c];
Image *factory[n_epochs_c];
Image *lab[n_epochs_c];
Image *men[n_epochs_c];
Image *unarmed_man = NULL;
Image *flags[n_players_c][n_flag_frames_c];
Image *panel_design = NULL;
//Image *panel_design_dark = NULL;
Image *panel_lab = NULL;
Image *panel_factory = NULL;
Image *panel_shield = NULL;
Image *panel_defence = NULL;
Image *panel_attack = NULL;
Image *panel_bloody_attack = NULL;
Image *panel_twoattack = NULL;
Image *panel_build[N_BUILDINGS];
Image *panel_building[N_BUILDINGS];
Image *panel_knowndesigns = NULL;
Image *panel_bigdesign = NULL;
Image *panel_biglab = NULL;
Image *panel_bigfactory = NULL;
Image *panel_bigshield = NULL;
Image *panel_bigdefence = NULL;
Image *panel_bigattack = NULL;
Image *panel_bigbuild = NULL;
Image *panel_bigknowndesigns = NULL;
Image *numbers_blue[10];
Image *numbers_grey[10];
Image *numbers_white[10];
Image *numbers_orange[10];
Image *numbers_yellow[10];
Image *numbers_largegrey[10];
Image *numbers_largeshiny[10];
Image *numbers_small[n_players_c][10];
Image *numbers_half = NULL;
Image *letters_large[n_font_chars_c];
Image *letters_small[n_font_chars_c];
Image *mouse_pointers[n_players_c];
Image *playershields[n_playershields_c];
Image *building_health = NULL;
Image *dash_grey = NULL;
Image *icon_shield = NULL;
Image *icon_defence = NULL;
Image *icon_weapon = NULL;
Image *icon_shields[n_shields_c];
Image *icon_defences[n_epochs_c];
Image *icon_weapons[n_epochs_c];
Image *numbered_defences[n_epochs_c];
Image *numbered_weapons[n_epochs_c];
Image *icon_elements[N_ID];
Image *icon_clocks[13];
Image *icon_infinity = NULL;
Image *icon_bc = NULL;
Image *icon_ad = NULL;
Image *icon_ad_shiny = NULL;
Image *icon_towers[n_players_c];
Image *icon_armies[n_players_c];
Image *icon_nuke_hole = NULL;
Image *mine_gatherable_small = NULL;
Image *mine_gatherable_large = NULL;
Image *icon_ergo = NULL;
Image *icon_trash = NULL;
Image *coast_icons[n_coast_c];
int map_sq_offset = 0, map_sq_coast_offset = 0;
Image *map_sq[MAP_N_COLOURS][n_map_sq_c];
Image *defenders[n_players_c][n_epochs_c][n_defender_frames_c];
Image *nuke_defences[n_players_c];
//Image *attackers_walking[n_players_c][n_epochs_c+1][n_attacker_frames_c];
int n_attacker_frames[n_epochs_c+1][n_attacker_directions_c];
Image *attackers_walking[n_players_c][n_epochs_c+1][n_attacker_directions_c][max_attacker_frames_c]; // epochs 6-9 are special case!
Image *planes[n_players_c][n_epochs_c];
Image *nukes[n_players_c][n_nuke_frames_c];
Image *saucers[n_players_c][n_saucer_frames_c];
Image *attackers_ammo[n_epochs_c][N_ATTACKER_AMMO_DIRS];
Image *icon_openpitmine = NULL;
Image *icon_trees[n_trees_c][n_tree_frames_c];
Image *icon_clutter[n_clutter_c];
Image *flashingmapsquare = NULL;
Image *mapsquare = NULL;
Image *arrow_left = NULL;
Image *arrow_right = NULL;
Image *death_flashes[n_death_flashes_c];
Image *blue_flashes[n_blue_flashes_c];
Image *explosions[n_explosions_c];
//Image *icon_mice[3];
Image *icon_mice[2];
Image *icon_speeds[3];
Image *smoke_image = NULL;
Image *background_islands = NULL;
// speech
Sample *s_design_is_ready = NULL;
Sample *s_ergo = NULL;
Sample *s_advanced_tech = NULL;
Sample *s_fcompleted = NULL;
Sample *s_on_hold = NULL;
Sample *s_running_out_of_elements = NULL;
Sample *s_tower_critical = NULL;
Sample *s_sector_destroyed = NULL;
Sample *s_mine_destroyed = NULL;
Sample *s_factory_destroyed = NULL;
Sample *s_lab_destroyed = NULL;
Sample *s_itis_all_over = NULL;
Sample *s_conquered = NULL;
Sample *s_won = NULL;
Sample *s_weve_nuked_them = NULL;
Sample *s_weve_been_nuked = NULL;
Sample *s_alliance_yes[n_players_c] = {NULL, NULL, NULL, NULL};
Sample *s_alliance_no[n_players_c] = {NULL, NULL, NULL, NULL};
Sample *s_alliance_ask[n_players_c] = {NULL, NULL, NULL, NULL};
Sample *s_quit[n_players_c] = {NULL, NULL, NULL, NULL};
Sample *s_cant_nuke_ally = NULL;
// effects
Sample *s_explosion = NULL;
Sample *s_scream = NULL;
Sample *s_buildingdestroyed = NULL;
Sample *s_guiclick = NULL;
Sample *s_biplane = NULL;
Sample *s_jetplane = NULL;
Sample *s_spaceship = NULL;
const unsigned char shadow_alpha_c = (unsigned char)160;
const int epoch_dates[n_epochs_c] = {-10000, -2000, 1, 900, 1400, 1850, 1914, 1944, 1980, 2100};
const char *epoch_names[n_epochs_c] = { "FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH", "SEVENTH", "EIGHTH", "NINTH", "TENTH" };
Invention *invention_shields[n_epochs_c];
Invention *invention_defences[n_epochs_c];
Weapon *invention_weapons[n_epochs_c];
Element *elements[N_ID];
Player *players[n_players_c];
//bool player_won = false;
GameResult gameResult = GAMERESULT_UNDEFINED;
GameStateID gameStateID = GAMESTATEID_UNDEFINED;
int human_player = 0;
bool isDemo() {
return human_player == PLAYER_DEMO;
}
//int enemy_player = 1;
//GameWon gameWon = GAME_PLAYING;
bool state_changed = false;
bool paused = false;
//const int n_men_per_epoch_c = 33;
int n_men_store = 0;
int n_suspended[n_players_c];
//int n_men_for_this_island = 0;
bool validDifficulty(DifficultyLevel difficulty) {
return difficulty >= 0 && difficulty < DIFFICULTY_N_LEVELS;
}
int getMenPerEpoch() {
ASSERT( gameType == GAMETYPE_ALLISLANDS );
if( difficulty_level == DIFFICULTY_EASY )
return 150;
else if( difficulty_level == DIFFICULTY_MEDIUM )
return 100;
else if( difficulty_level == DIFFICULTY_HARD )
return 75;
ASSERT(false);
return 0;
}
int getMenAvailable() {
if( start_epoch == end_epoch_c && gameType == GAMETYPE_ALLISLANDS )
return n_suspended[human_player];
return n_men_store;
}
int getNSuspended() {
return n_suspended[human_player];
}
int start_epoch = 0;
int n_sub_epochs = 4;
int selected_island = 0;
bool completed_island[max_islands_per_epoch_c];
//const int n_islands_c = 10;
Map *maps[n_epochs_c][max_islands_per_epoch_c];
Map *map = NULL;
const Map *getMap() {
return map;
}
Screen *screen = NULL;
GameState *gamestate = NULL;
//Sector *current_sector = NULL;
const bool default_pref_sound_on_c = true;
const bool default_pref_music_on_c = true;
const bool default_pref_disallow_nukes_c = false;
bool pref_sound_on = default_pref_sound_on_c;
bool pref_music_on = default_pref_music_on_c;
bool pref_disallow_nukes = default_pref_disallow_nukes_c;
Sample *music = NULL;
Map::Map(MapColour colour,int n_opponents,const char *name) {
//*this->filename = '\0';
this->colour = colour;
this->n_opponents = n_opponents;
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
sector_at[x][y] = false;
sectors[x][y] = NULL;
reserved[x][y] = false;
//panels[x][y] = NULL;
}
}
/*for(int i=0;i<N_ID;i++) {
this->elements[i] = 0;
}*/
//clearTemp();
/*strncpy(this->name,name,MAP_MAX_NAME);
this->name[MAP_MAX_NAME] = '\0';*/
this->name = name;
}
Map::~Map() {
freeSectors();
}
/*void Map::clearTemp() {
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
temp[x][y] = false;
}
}
}*/
const Sector *Map::getSector(int x, int y) const {
ASSERT(x >= 0 && x < map_width_c && y >= 0 && y < map_height_c);
Sector *sector = this->sectors[x][y];
return sector;
}
Sector *Map::getSector(int x, int y) {
ASSERT(x >= 0 && x < map_width_c && y >= 0 && y < map_height_c);
Sector *sector = this->sectors[x][y];
return sector;
}
bool Map::isSectorAt(int x, int y) const {
ASSERT(x >= 0 && x < map_width_c && y >= 0 && y < map_height_c);
return this->sector_at[x][y];
}
void Map::newSquareAt(int x,int y) {
ASSERT(x >= 0 && x < map_width_c && y >= 0 && y < map_height_c);
this->sector_at[x][y] = true;
}
void Map::createSectors(PlayingGameState *gamestate, int epoch) {
ASSERT_EPOCH(epoch);
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
if( sector_at[x][y] ) {
this->sectors[x][y] = new Sector(gamestate, epoch, x, y, map->getColour());
/*for(int i=0;i<N_ID;i++) {
this->sectors[x][y]->setElements(i, this->elements[i]);
}*/
}
}
}
}
#if 0
void Map::checkSectors() const {
//LOG("Map::checkSectors()\n");
#ifdef _WIN32
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
if( sectors[x][y] != NULL ) {
//LOG("check sector at %d, %d : %d\n", x, y, sectors[x][y]);
if( !_CrtIsValidHeapPointer( sectors[x][y] ) ) {
LOG("invalid sector at %d, %d\n", x, y);
LOG(" ptr %d\n", sectors[x][y]);
ASSERT( false );
}
}
}
}
#endif
//LOG("Map::checkSectors exit\n");
}
#endif
void Map::freeSectors() {
//LOG("Map::freeSectors()\n");
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
if( sectors[x][y] != NULL ) {
//LOG("free sector at %d, %d\n", x, y);
//LOG(" ptr %d\n", sectors[x][y]);
delete sectors[x][y];
sectors[x][y] = NULL;
}
}
}
//current_sector = NULL;
//LOG("Map::freeSectors exit\n");
}
/*void Map::setElements(int id,int n_elements) {
ASSERT_ELEMENT_ID(id);
this->elements[id] = n_elements;
}*/
void Map::findRandomSector(int *rx,int *ry) const {
while(true) {
int x = rand() % map_width_c;
int y = rand() % map_height_c;
if( sector_at[x][y] ) {
*rx = x;
*ry = y;
break;
}
}
}
void Map::canMoveTo(bool temp[map_width_c][map_height_c], int sx,int sy,int player) const {
ASSERT(player != -1 );
ASSERT(this->sector_at[sx][sy]);
//clearTemp();
//bool temp[map_width_c][map_height_c];
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
temp[x][y] = false;
}
}
temp[sx][sy] = true;
bool changed = false;
do {
changed = false;
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
if( temp[x][y] ) {
// can we move through this square?
if( ( x == sx && y == sy ) ||
sectors[x][y]->getPlayer() == player ||
( sectors[x][y]->getPlayer() == -1 &&
!sectors[x][y]->enemiesPresent(player) ) ) {
for(int c=0;c<4;c++) {
int cx = x, cy = y;
if( c == 0 )
cy--;
else if( c == 1 )
cx++;
else if( c == 2 )
cy++;
else if( c == 3 )
cx--;
if( cx >= 0 && cy >= 0 && cx < map_width_c && cy < map_height_c
&& sector_at[cx][cy]
&& !sectors[x][y]->isNuked()
&& !temp[cx][cy] ) {
temp[cx][cy] = true;
changed = true;
}
}
}
}
}
}
} while( changed );
}
void Map::calculateStats() const {
// deaths = start + births - remaining
for(int i=0;i<n_players_c;i++) {
if( players[i] != NULL ) {
players[i]->setNDeaths( players[i]->getNMenForThisIsland() + players[i]->getNBirths() );
players[i]->setNSuspended(0);
}
}
for(int x=0;x<map_width_c;x++) {
for(int y=0;y<map_height_c;y++) {
if( this->sector_at[x][y] ) {
Sector *sector = this->sectors[x][y];
if( sector->getPlayer() != -1 && players[sector->getPlayer()] != NULL ) {
// check for players[sector->getPlayer()] not being NULL should be redundant, but just to be safe
players[sector->getPlayer()]->addNDeaths( - sector->getPopulation() );
if( sector->isShutdown() && gameResult == GAMERESULT_WON ) {
//players[sector->getPlayer()]->n_suspended += sector->getPopulation();
players[sector->getPlayer()]->addNSuspended(sector->getPopulation());
}
}
for(int i=0;i<n_players_c;i++) {
if( players[i] != NULL ) {
players[i]->addNDeaths( - sector->getArmy(i)->getTotalMen() );
}
}
}
}
}
/*for(int i=0;i<n_players_c;i++) {
if( players[i] != NULL )
ASSERT( players[i]->n_deaths >= 0 );
}*/
}
/*bool Map::mapIs(char *that_name) {
//return strcmp( this->name, that_name ) == 0;
return this->name == that_name;
}*/
int Map::getNSquares() const {
int n_squares = 0;
for(int y=0;y<map_height_c;y++) {
for(int x=0;x<map_width_c;x++) {
if( map->sector_at[x][y] ) {
n_squares++;
}
}
}
return n_squares;
}
void Map::draw(int offset_x, int offset_y) const {
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 icon = 0;
if( y > 0 && map->sector_at[x][y-1] )
icon += 1;
if( x < map_width_c-1 && map->sector_at[x+1][y] )
icon += 2;
if( y < map_height_c-1 && map->sector_at[x][y+1] )
icon += 4;
if( x > 0 && map->sector_at[x-1][y] )
icon += 8;
ASSERT( icon >= 0 && icon < 16 );
if( map_sq[colour][icon] == NULL ) {
LOG("map name: %s\n", map->getName());
LOG("ERROR map icon not available [%d,%d]: %d, %d\n", x, y, colour, icon);
ASSERT( map_sq[colour][icon] != NULL );
}
int map_x = offset_x - map_sq_offset + 16 * x;
int map_y = offset_y - map_sq_offset + 16 * y;
int coast_map_x = offset_x - map_sq_coast_offset + 16 * x;
int coast_map_y = offset_y - map_sq_coast_offset + 16 * y;
//LOG("draw at: %d, %d : %d, %d\n", x, y, map_sq[colour][icon]->getWidth(), map_sq[colour][icon]->getHeight());
map_sq[colour][icon]->draw(map_x, map_y);
/*int coast = 15 - icon;
if( coast > 0 && coast_icons[coast-1] != NULL ) {
coast_icons[coast-1]->draw(map_x, map_y, true);
}*/
//LOG(">>> %d %d %d\n", icon, icon & 1, 4 & 1);
if( (icon & 1) == 0 && coast_icons[0] != NULL )
coast_icons[0]->draw(coast_map_x, coast_map_y);
if( (icon & 2) == 0 && coast_icons[1] != NULL )
coast_icons[1]->draw(coast_map_x, coast_map_y);
if( (icon & 4) == 0 && coast_icons[3] != NULL )
coast_icons[3]->draw(coast_map_x, coast_map_y);
if( (icon & 8) == 0 && coast_icons[7] != NULL )
coast_icons[7]->draw(coast_map_x, coast_map_y);
}
}
}
}
void setEpoch(int epoch) {
LOG("set epoch %d\n", epoch);
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
ASSERT( epoch >= 0 && epoch < n_epochs_c );
start_epoch = epoch;
n_sub_epochs = 4;
//if( start_epoch == n_epochs_c-1 )
if( start_epoch == end_epoch_c )
n_sub_epochs = 0;
else if( start_epoch + n_sub_epochs > n_epochs_c ) {
n_sub_epochs = n_epochs_c - start_epoch;
}
selected_island = 0;
if( gameType == GAMETYPE_ALLISLANDS ) {
// skip islands we've completed
while( completed_island[selected_island] ) {
selected_island++;
if( selected_island == max_islands_per_epoch_c || maps[start_epoch][selected_island] == NULL ) {
LOG("error, should be at least one island on this epoch that isn't completed\n");
ASSERT( false );
}
}
}
map = maps[start_epoch][selected_island];
ASSERT( map != NULL );
gamestate->reset();
}
void drawProgress(int percentage) {
const int width = (int)(screen->getWidth() * 0.25f);
const int height = (int)(screen->getHeight()/15.0f);
const int xpos = (int)(screen->getWidth()*0.5f - width*0.5f);
const int ypos = (int)(screen->getHeight()*0.5f - height*0.5f);
screen->clear(); // n.b., needed for Qt/Symbian, where background defaults to white
screen->fillRect(xpos, ypos, width+1, height+1, 255, 255, 255);
int progress_width = (int)(((width-1) * percentage) / 100.0f);
screen->fillRect(xpos+1, ypos+1, progress_width, height-1, 127, 0, 0);
screen->refresh();
}
void newGame() {
gamestate->fadeScreen(false, 0, NULL);
LOG("newGame()\n");
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
if( gameType == GAMETYPE_SINGLEISLAND )
n_men_store = 1000;
else
n_men_store = getMenPerEpoch();
for(int i=0;i<max_islands_per_epoch_c;i++)
completed_island[i] = false;
//completed_island[0] = true;
for(int i=0;i<n_players_c;i++)
n_suspended[i] = 0;
setEpoch(0);
//n_men_store = 1000;
//setEpoch(9);
}
void setClientPlayer(int set_client_player) {
::human_player = set_client_player;
if( gamestate != NULL ) {
gamestate->setClientPlayer(set_client_player);
}
}
void nextEpoch() {
LOG("nextEpoch()\n");
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
start_epoch++;
if( start_epoch == n_epochs_c ) {
ASSERT( gameType == GAMETYPE_SINGLEISLAND );
start_epoch = 0;
}
for(int i=0;i<max_islands_per_epoch_c;i++)
completed_island[i] = false;
setEpoch(start_epoch);
}
void nextIsland() {
if( gameType == GAMETYPE_SINGLEISLAND ) {
selected_island++;
if( selected_island == max_islands_per_epoch_c || maps[start_epoch][selected_island] == NULL )
selected_island = 0;
}
else {
// skip islands we've completed
do {
selected_island++;
if( selected_island == max_islands_per_epoch_c || maps[start_epoch][selected_island] == NULL )
selected_island = 0;
} while( completed_island[selected_island] );
}
LOG("next island: %d\n", selected_island);
map = maps[start_epoch][selected_island];
LOG("map name: %s\n", map==NULL ? "NULL?!" : map->getName());
gamestate->reset();
LOG("done reset\n");
}
bool loadGameInfo(DifficultyLevel *difficulty, int *player, int *n_men, int suspended[n_players_c], int *epoch, bool completed[max_islands_per_epoch_c], const char *filename) {
//LOG("loadGameInfo(%d)\n",slot);
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
//LOG("loading: %s\n", filename);
FILE *file = fopen(filename, "rb+");
if( file == NULL ) {
//LOG("FAILED to open file\n");
return false;
}
const int bufsize = 1024;
char buffer[bufsize+1] = "";
if( fgets(buffer, bufsize, file) == NULL ) { // header line
return false;
}
// data line
/*for(int i=0;i<8;i++) {
buffer[i] = fgetc(file);
}*/
for(int i=0;;) {
ASSERT( i <= bufsize );
//buffer[i] = fgetc(file);
int c = fgetc(file);
if( c == EOF ) {
buffer[i] = '\0';
break;
}
buffer[i] = (char)c;
i++;
}
char *ptr = buffer;
*difficulty = (DifficultyLevel)*((int *)ptr);
ptr += sizeof(int);
if( !validDifficulty( *difficulty ) )
return false;
*player = *((int *)ptr);
ptr += sizeof(int);
if( !validPlayer( *player ) )
return false;
*n_men = *((int *)ptr);
ptr += sizeof(int);
if( *n_men < 0 )
return false;
for(int i=0;i<n_players_c;i++) {
suspended[i] = *((int *)ptr);
ptr += sizeof(int);
if( suspended[i] < 0 )
return false;
}
*epoch = *((int *)ptr);
ptr += sizeof(int);
if( *epoch < 0 || *epoch >= n_epochs_c )
return false;
for(int i=0;i<max_islands_per_epoch_c;i++) {
int val = *((int *)ptr);
if( val != 0 && val != 1 )
return false;
completed[i] = (val==1);
ptr += sizeof(int);
}
fclose(file);
return true;
}
bool loadGame(const char *filename) {
LOG("loadGame(%s)\n",filename);
ASSERT( gameType == GAMETYPE_ALLISLANDS );
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
DifficultyLevel temp_difficulty = DIFFICULTY_EASY;
int temp_player = 0;
int temp_n_men_store = 0;
int temp_start_epoch = 0;
int temp_suspended[n_players_c];
bool temp_completed[max_islands_per_epoch_c];
if( loadGameInfo(&temp_difficulty, &temp_player, &temp_n_men_store, temp_suspended, &temp_start_epoch, temp_completed, filename) ) {
difficulty_level = temp_difficulty;
setClientPlayer(temp_player);
n_men_store = temp_n_men_store;
for(int i=0;i<n_players_c;i++)
n_suspended[i] = temp_suspended[i];
for(int i=0;i<max_islands_per_epoch_c;i++)
completed_island[i] = temp_completed[i];
setEpoch(temp_start_epoch);
return true;
}
return false;
}
char *getFilename(int slot) {
char name[300] = "";
sprintf(name, "game_%d.SAV", slot);
char *filename = getApplicationFilename(name);
//LOG("filename: %s\n", filename);
return filename;
}
bool loadGameInfo(DifficultyLevel *difficulty, int *player, int *n_men, int suspended[n_players_c], int *epoch, bool completed[max_islands_per_epoch_c], int slot) {
ASSERT( slot >= 0 && slot < n_slots_c );
char *filename = getFilename(slot);
bool ok = loadGameInfo(difficulty, player, n_men, suspended, epoch, completed, filename);
delete [] filename;
return ok;
}
bool loadGame(int slot) {
LOG("loadGame(%d)\n",slot);
ASSERT( slot >= 0 && slot < n_slots_c );
char *filename = getFilename(slot);
bool ok = loadGame(filename);
delete [] filename;
return ok;
}
void saveGame(int slot) {
LOG("saveGame(%d)\n",slot);
ASSERT( gameType == GAMETYPE_ALLISLANDS );
ASSERT( gameStateID == GAMESTATEID_PLACEMEN );
ASSERT( slot >= 0 && slot < n_slots_c );
char *filename = getFilename(slot);
FILE *file = fopen(filename, "wb+");
delete [] filename;
if( file == NULL ) {
LOG("FAILED to open file\n");
return;
}
const int savVersion = 1;
fprintf(file, "GLMSAV%d.%d.%d\n", majorVersion, minorVersion, savVersion);
char buffer[256] = "";
char *ptr = buffer;
*((int *)ptr) = difficulty_level;
ptr += sizeof(int);
*((int *)ptr) = human_player;
ptr += sizeof(int);
*((int *)ptr) = n_men_store;
ptr += sizeof(int);
for(int i=0;i<n_players_c;i++) {
*((int *)ptr) = n_suspended[i];
ptr += sizeof(int);
}
*((int *)ptr) = start_epoch;
ptr += sizeof(int);
for(int i=0;i<max_islands_per_epoch_c;i++) {
int val = completed_island[i] ? 1 : 0;
*((int *)ptr) = val;
ptr += sizeof(int);
}
ptrdiff_t n_bytes = ptr - buffer;
int sum = 0;
for(int i=0;i<n_bytes;i++) {
sum += buffer[i];
}
LOG("checksum %d\n", sum);
*((int *)ptr) = sum;
ptr += sizeof(int);
*ptr = '\0';
//fprintf(file, "%s\n", buffer);
//fwrite(buffer, (int)ptr - (int)buffer, 1, file);
ptrdiff_t diff = ptr - buffer;
fwrite(buffer, diff, 1, file);
fclose(file);
}
bool validPlayer(int player) {
bool valid = player >= 0 && player < n_players_c;
if( !valid ) {
LOG("ERROR invalid player %d\n", player);
}
return valid;
}
void addTextEffect(TextEffect *effect) {
gamestate->addTextEffect(effect);
}
void stopMusic() {
if( music != NULL ) {
delete music;
music = NULL;
}
}
void fadeMusic(int duration_ms) {
if( music != NULL ) {
music->fadeOut(duration_ms);
}
}
void playMusic() {
stopMusic();
if( gameStateID == GAMESTATEID_CHOOSEPLAYER ) {
}
else if( gameStateID == GAMESTATEID_PLACEMEN ) {
}
else if( pref_music_on && gameStateID == GAMESTATEID_PLAYING ) {
music = Sample::loadMusic("music/gamemusic.ogg");
// n.b., a music structure is always created, even if we fail to load, so no need to check for NULL pointers here (though we do elsewhere, as music not created if pref_music_on is false)
music->play(SOUND_CHANNEL_MUSIC, -1);
}
}
bool loadSamples() {
string sound_dir = "sound/";
s_design_is_ready = Sample::loadSample(sound_dir + "the_design_is_finished.wav");
s_design_is_ready->setText("the design is finished");
s_ergo = Sample::loadSample(sound_dir + "ergonomically_terrific.wav");
s_ergo->setText("ergonomically terrific!");
s_fcompleted = Sample::loadSample(sound_dir + "the_production_run_s_completed.wav");
s_fcompleted->setText("the production run's completed");
s_advanced_tech = Sample::loadSample(sound_dir + "we_ve_advanced_a_tech_level.wav");
s_advanced_tech->setText("we've advanced a tech level!");
s_running_out_of_elements = Sample::loadSample(sound_dir + "we_re_running_out_of_elements.wav");
s_running_out_of_elements->setText("we're running out of elements");
s_tower_critical = Sample::loadSample(sound_dir + "tower_critical.wav");
s_tower_critical->setText("tower critical!");
s_sector_destroyed = Sample::loadSample(sound_dir + "the_sector_s_been_destroyed.wav");
s_sector_destroyed->setText("the sector's been destroyed!");
s_mine_destroyed = Sample::loadSample(sound_dir + "the_mine_is_destroyed.wav");
s_mine_destroyed->setText("the mine is destroyed");
s_factory_destroyed = Sample::loadSample(sound_dir + "the_factory_s_been_destroyed.wav");
s_factory_destroyed->setText("the factory's been destroyed");
s_lab_destroyed = Sample::loadSample(sound_dir + "the_lab_s_been_destroyed.wav");
s_lab_destroyed->setText("the lab's been destroyed");
s_itis_all_over = Sample::loadSample(sound_dir + "it_s_all_over.wav");
s_itis_all_over->setText("game over");
s_conquered = Sample::loadSample(sound_dir + "we_ve_conquered_the_sector.wav");
s_conquered->setText("we've conquered the sector");
s_won = Sample::loadSample(sound_dir + "we_ve_won.wav");
s_won->setText("we've won!");
s_weve_nuked_them = Sample::loadSample(sound_dir + "we_ve_nuked_them.wav");
s_weve_nuked_them->setText("we've nuked them!");
s_weve_been_nuked = Sample::loadSample(sound_dir + "we_ve_been_nuked.wav");
s_weve_been_nuked->setText("we've been nuked!");
s_on_hold = Sample::loadSample(sound_dir + "putting_you_on_hold.wav");
// no text for s_on_hold
s_alliance_yes[0] = new Sample();
s_alliance_yes[0]->setText("red team says okay");
s_alliance_yes[1] = new Sample();
s_alliance_yes[1]->setText("green team says okay");
s_alliance_yes[2] = new Sample();
s_alliance_yes[2]->setText("yellow team says okay");
s_alliance_yes[3] = new Sample();
s_alliance_yes[3]->setText("blue team says okay");
s_alliance_no[0] = new Sample();
s_alliance_no[0]->setText("red team says no");
s_alliance_no[1] = new Sample();
s_alliance_no[1]->setText("green team says no");
s_alliance_no[2] = new Sample();
s_alliance_no[2]->setText("yellow team says no");
s_alliance_no[3] = new Sample();
s_alliance_no[3]->setText("blue team says no");
s_alliance_ask[0] = new Sample();
s_alliance_ask[0]->setText("red team requests alliance");
s_alliance_ask[1] = new Sample();
s_alliance_ask[1]->setText("green team requests alliance");
s_alliance_ask[2] = new Sample();
s_alliance_ask[2]->setText("yellow team requests alliance");
s_alliance_ask[3] = new Sample();
s_alliance_ask[3]->setText("blue team requests alliance");
// text messages without corresponding samples
//s_cant_nuke_ally = new Sample(NULL);
s_cant_nuke_ally = new Sample();
s_cant_nuke_ally->setText("cannot nuke our ally");
// no samples or text messages, but keep supported in case we re-add samples later
s_quit[0] = new Sample();
s_quit[1] = new Sample();
s_quit[2] = new Sample();
s_quit[3] = new Sample();
// sound effects
s_explosion = Sample::loadSample(sound_dir + "bomb.wav");
#if !defined(__ANDROID__) && defined(__linux)
if( s_explosion == NULL || errorSound() ) {
if( s_explosion != NULL ) {
delete s_explosion;
s_explosion = NULL;
}
resetErrorSound();
sound_dir = "/usr/share/gigalomania/" + sound_dir;
LOG("look in %s for sound\n", sound_dir.c_str());
s_explosion = Sample::loadSample(sound_dir + "bomb.wav");
}
#endif
s_scream = Sample::loadSample(sound_dir + "pain1.wav");
s_buildingdestroyed = Sample::loadSample(sound_dir + "woodbrk.wav");
s_guiclick = Sample::loadSample(sound_dir + "misc_menu_3.wav");
s_biplane = Sample::loadSample(sound_dir + "biplane.ogg");
s_jetplane = Sample::loadSample(sound_dir + "jetplane.ogg");
s_spaceship = Sample::loadSample(sound_dir + "spaceship.ogg");
bool ok = !errorSound();
return ok;
}
bool remapLand(Image *image,MapColour colour) {
for(int y=0;y<image->getHeight();y++) {
for(int x=0;x<image->getWidth();x++) {
unsigned char c = image->getPixelIndex(x,y);
if( c == 0 ) {
// leave as 0
}