-
Notifications
You must be signed in to change notification settings - Fork 6
/
MapSection.cpp
1147 lines (1071 loc) · 46.2 KB
/
MapSection.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 "MapSection.h"
#include "UserConfig.h"
#include "common.h"
#include <cmath>
#include "DetailedTile.h"
int get_map_height(int x, int y)
{
if(map_list.elevation_map_with_water)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(map_list.elevation_map_with_water));
tempy = bind_to_range(tempy , al_get_bitmap_height(map_list.elevation_map_with_water));
ALLEGRO_COLOR pixel_elw = al_get_pixel(map_list.elevation_map_with_water, tempx, tempy);
unsigned char red;
unsigned char green;
unsigned char blue;
al_unmap_rgb(pixel_elw, &red, &green, &blue);
if(red == green && green == blue)
return blue + 25;
if(red == 0 && green == blue)
return blue;
else
return blue+25;
}
else return 100;
}
const char * get_terrain_string(terrain_type input)
{
switch(input)
{
case TERRAIN_ANY:
return "any";
case TERRAIN_NONE:
return "none";
case TERRAIN_MOUNTAIN:
return "mountain";
case TERRAIN_LAKE_TEMP_FRESH:
return "temperate_freshwater_lake";
case TERRAIN_LAKE_TEMP_BRACK:
return "temperate_brackish_lake";
case TERRAIN_LAKE_TEMP_SALT:
return "temperate_saltwater_lake";
case TERRAIN_LAKE_TROP_FRESH:
return "tropical_freshwater_lake";
case TERRAIN_LAKE_TROP_BRACK:
return "tropical_brackish_lake";
case TERRAIN_LAKE_TROP_SALT:
return "tropical_saltwater_lake";
case TERRAIN_OCEAN_ARCT:
return "arctic_ocean";
case TERRAIN_OCEAN_TROP:
return "tropical_ocean";
case TERRAIN_OCEAN_TEMP:
return "temperate_ocean";
case TERRAIN_GLACIER:
return "glacier";
case TERRAIN_TUNDRA:
return "tundra";
case TERRAIN_SWAMP_TEMP_FRESH:
return "temperate_freshwater_swamp";
case TERRAIN_SWAMP_TEMP_SALT:
return "temperate_saltwater_swamp";
case TERRAIN_MARSH_TEMP_FRESH:
return "temperate_freshwater_marsh";
case TERRAIN_MARSH_TEMP_SALT:
return "temperate_saltwater_marsh";
case TERRAIN_SWAMP_TROP_FRESH:
return "tropical_freshwater_swamp";
case TERRAIN_SWAMP_TROP_SALT:
return "tropical_saltwater_swamp";
case TERRAIN_SWAMP_MANGROVE:
return "mangrove_swamp";
case TERRAIN_MARSH_TROP_FRESH:
return "tropical_freshwater_marsh";
case TERRAIN_MARSH_TROP_SALT:
return "tropical_saltwater_marsh";
case TERRAIN_FOREST_TAIGA:
return "taiga_forest";
case TERRAIN_FOREST_TEMP_FIR:
return "temperate_conifer_forest";
case TERRAIN_FOREST_TEMP_BROAD:
return "temperate_broadleaf_forest";
case TERRAIN_FOREST_TROP_FIR:
return "tropical_conifer_forest";
case TERRAIN_FOREST_TROP_BROAD_DRY:
return "tropical_dry_broadleaf_forest";
case TERRAIN_FOREST_TROP_BROAD_MOIST:
return "tropical_moist_broadleaf_forest";
case TERRAIN_GRASS_TEMP:
return "temperate_grassland";
case TERRAIN_SAV_TEMP:
return "temperate_savanna";
case TERRAIN_SHRUB_TEMP:
return "temperate_shrubland";
case TERRAIN_GRASS_TROP:
return "tropical_grassland";
case TERRAIN_SAV_TROP:
return "tropical_savanna";
case TERRAIN_SHRUB_TROP:
return "tropical_shrubland";
case TERRAIN_DESERT_BAD:
return "badland_desert";
case TERRAIN_DESERT_SAND:
return "sand_desert";
case TERRAIN_DESERT_ROCK:
return "rock_desert";
case TERRAIN_MOUNTAIN_TALL:
return "tall_mountain";
case TERRAIN_BEACH_TEMP:
return "temperate_beach";
case TERRAIN_BEACH_TROP:
return "tropical_beach";
case TERRAIN_BEACH_ARCT:
return "arctic_beach";
case TERRAIN_COUNT:
break;
}
return "ERROR";
}
const char* get_structure_string(structure_type input)
{
switch(input)
{
case STRUCTURE_ANY:
return "any";
case STRUCTURE_NONE:
return "none";
case STRUCTURE_CASTLE:
return "castle";
case STRUCTURE_VILLAGE:
return "village";
case STRUCTURE_CROPS1:
return "crops_1";
case STRUCTURE_CROPS2:
return "crops_2";
case STRUCTURE_CROPS3:
return "crops_3";
case STRUCTURE_PASTURE:
return "pasture";
case STRUCTURE_MEADOW:
return "meadow";
case STRUCTURE_WOODLAND:
return "woodland";
case STRUCTURE_ORCHARD:
return "orchard";
case STRUCTURE_TUNNEL:
return "tunnel";
case STRUCTURE_BRIDGE_STONE:
return "stone_bridge";
case STRUCTURE_BRIDGE_OTHER:
return "other_bridge";
case STRUCTURE_ROAD_STONE:
return "stone_road";
case STRUCTURE_ROAD_OTHER:
return "other_road";
case STRUCTURE_WALL_STONE:
return "stone_wall";
case STRUCTURE_WALL_OTHER:
return "other_wall";
case STRUCTURE_RIVER:
return "river";
case STRUCTURE_BROOK:
return "brook";
case STRUCTURE_COUNT:
break;
}
return "ERROR";
}
bool approx_f(float a, float b, float accuracy)
{
return (abs(a-b) <= accuracy);
}
void MapSection::pointToScreen(int *inx, int *iny)
{
int offx = board_center_x;
int offy = board_top_y;
int x = *inx-*iny;
int y = *inx+*iny;
y -= ((user_config.map_width + user_config.map_height) / 2);
x = x * tileset_list.at(current_tileset).tile_width / 2;
y = y * tileset_list.at(current_tileset).tile_height / 2;
x+=offx;
y+=offy;
*inx=x;*iny=y;
}
void MapSection::pointToSprite(float *inx, float *iny, int inz)
{
float offx = 0;
float offy = -inz-tileset_list.at(current_tileset).tile_height;
float x = *inx-*iny;
float y = *inx+*iny;
x = x * tileset_list.at(current_tileset).tile_width / (48*2);
y = y * tileset_list.at(current_tileset).tile_height / (48*2);
x+=offx;
y+=offy;
*inx=floor(x+0.5f); *iny = floor(y+0.5f);
}
MapSection::MapSection(void)
{
block_array = 0;
board_width = 0;
board_height = 0;
num_tiles = 0;
board_center_x = 0;
board_top_y = 0;
set_size(50, 50);
current_tileset = 0;
}
MapSection::~MapSection(void)
{
clear_tiles();
}
bool MapSection::set_size(int x, int y)
{
unsigned int total_needed = x*y;
board_width = x;
board_height = y;
clear_tiles();
block_array = new s_map_block[total_needed];
num_tiles = total_needed;
flood_fill(NULL, 0);
return true;
}
void MapSection::clear_tiles(void)
{
delete[] block_array;
num_tiles = 0;
}
void MapSection::flood_fill(c_tile *tile, int height)
{
for (unsigned int i = 0; i < (board_width * board_height); i++)
{
block_array[i].height = height;
block_array[i].sprite = tile;
block_array[i].biome_color = al_map_rgb(255,255,255);
block_array[i].combined_color = al_map_rgb(255,255,255);
block_array[i].structure_color = al_map_rgb(255,255,255);
block_array[i].trade_color = al_map_rgb(255,255,255);
block_array[i].terrain = TERRAIN_NONE;
block_array[i].structure = STRUCTURE_NONE;
}
}
unsigned int MapSection::coords_to_index(int x, int y)
{
if(x < 0) x=0;
if(y < 0) y=0;
if(x >= board_width) x = board_width-1;
if(y >= board_height) y = board_height-1;
return x + (board_width * y);
}
void MapSection::draw(int inx, int iny)
{
clock_t start_time = clock();
al_hold_bitmap_drawing(true);
for (unsigned int y = 1; y < (board_height-1); y++)
{
for (unsigned int x = 1; x < (board_width-1); x++)
{
int drawx = x;
int drawy = y;
pointToScreen(&drawx, &drawy);
drawx += inx;
drawy += iny;
unsigned int index = coords_to_index(x, y);
int height_back = block_array[index].height;
if(x==(board_width/2) && y==(board_height/2) && user_config.debugmode)
{
int diff = 280-block_array[index].water_height;
block_array[index].height +=diff;
block_array[index].water_height +=diff;
}
int bottom_l = 0;
int bottom_r = 0;
if(x+2 < board_width)
bottom_r = block_array[index+1].height;
if(y+2 < board_height)
bottom_l = block_array[index+board_width].height;
if (bottom_l < bottom_r)
bottom_r = bottom_l;
if(tileset_list[current_tileset].rendered_map && tileset_list[current_tileset].rendered_map->get_tile(x + user_config.map_x, y + user_config.map_y)){
tileset_list[current_tileset].rendered_map->get_tile(x + user_config.map_x, y + user_config.map_y)->draw(drawx, drawy);
}
else {
if(block_array[index].sprite)
{
block_array[index].sprite->draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]), 0, LAYER_BASE);
if(block_array[index].structure_sprite)
{
block_array[index].structure_sprite->draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]), 0, LAYER_BASE);
block_array[index].structure_sprite->draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]), 0, LAYER_OBJECT);
}
else
block_array[index].sprite->draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]), 0, LAYER_OBJECT);
}
if(user_config.showgrid && !((x + user_config.map_x) % 16))
tileset_list.at(current_tileset).grid_tile.draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]));
if(user_config.showgrid && !((y + user_config.map_y) % 16))
tileset_list.at(current_tileset).grid_tile.draw(drawx, drawy, snap_height(block_array[index].height), snap_height(bottom_r), snap_height(block_array[index].water_height), &block_array[index], &(tileset_list[current_tileset]), 1);
}
block_array[index].height = height_back;
}
}
al_hold_bitmap_drawing(false);
draw_time = clock() - start_time;
}
void MapSection::load_heights(ALLEGRO_BITMAP * heightmap)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = coords_to_index( x, y);
if(heightmap)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(heightmap));
tempy = bind_to_range(tempy , al_get_bitmap_height(heightmap));
ALLEGRO_COLOR pixel = al_get_pixel(heightmap, tempx, tempy);
unsigned char red;
unsigned char green;
unsigned char blue;
al_unmap_rgb(pixel, &red, &green, &blue);
if(red == 0 && green == 0)
{
block_array[index].height = blue;
}
else if(red == green && green == blue)
{
block_array[index].height = blue + 25;
}
}
else block_array[index].height = 0;
block_array[index].water_height = block_array[index].height;
}
}
}
void MapSection::generate_noise(void)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
block_array[index].random = (findnoise2(tempx, tempy)+1) / 2;
}
}
}
void MapSection::load_level(ALLEGRO_BITMAP * levelmap, int level)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
if(levelmap)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(levelmap));
tempy = bind_to_range(tempy , al_get_bitmap_height(levelmap));
ALLEGRO_COLOR pixel = al_get_pixel(levelmap, tempx, tempy);
unsigned char red;
unsigned char green;
unsigned char blue;
al_unmap_rgb(pixel, &red, &green, &blue);
block_array[index].levels[level] =red;
}
else block_array[index].levels[level] = 0;
}
}
}
void MapSection::load_water_level(ALLEGRO_BITMAP * watermap)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
if(watermap)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(watermap));
tempy = bind_to_range(tempy , al_get_bitmap_height(watermap));
ALLEGRO_COLOR pixel = al_get_pixel(watermap, tempx, tempy);
unsigned char red;
unsigned char green;
unsigned char blue;
al_unmap_rgb(pixel, &red, &green, &blue);
if(red == 0){
if(green == 0)
{
block_array[index].water_height = blue + 25;
block_array[index].structure = STRUCTURE_NONE;
}
else{
block_array[index].water_height = blue;
block_array[index].structure = STRUCTURE_BROOK;
}
if(block_array[index].height > block_array[index].water_height)
block_array[index].height = block_array[index].water_height;
}
else{
block_array[index].structure = STRUCTURE_NONE;
block_array[index].water_height = block_array[index].height;
}
}
else block_array[index].water_height = block_array[index].height;
}
}
}
void MapSection::load_biome_tiles(s_maplist * maplist)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
if(maplist->elevation_map_with_water)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(maplist->elevation_map_with_water));
tempy = bind_to_range(tempy , al_get_bitmap_height(maplist->elevation_map_with_water));
ALLEGRO_COLOR pixel_elw = al_get_pixel(maplist->elevation_map_with_water, tempx, tempy);
unsigned char red;
unsigned char green;
unsigned char blue;
al_unmap_rgb(pixel_elw, &red, &green, &blue);
unsigned char cR, cG, cB;
al_unmap_rgb(block_array[index].biome_color, &cR, &cG, &cB);
#if 0
float R,G,B;
R=cR;
G=cG;
B=cB;
if(
(cR == 180) &&
(cG == 167) &&
(cB == 20) &&
block_array[index].height > 99
)
{
block_array[index].terrain = TERRAIN_BRIDGE;
block_array[index].height = blue;
block_array[index].water_height = blue;
}
else if(red == 0 && green == blue)
{
if(block_array[index].color.g > 0.0001)
block_array[index].terrain = TERRAIN_STREAM;
else
block_array[index].terrain = TERRAIN_RIVER;
block_array[index].height = blue;
block_array[index].water_height = blue;
}
else if(red == 0 && green == 0)
{
block_array[index].terrain = TERRAIN_OCEAN;
block_array[index].water_height = max(blue+25, 99);
}
#endif
if(
(cR == 128) &&
(cG == 128) &&
(cB == 128) &&
block_array[index].height > 200
)
{
block_array[index].terrain = TERRAIN_MOUNTAIN_TALL;
}
else if(
(cR == 128) &&
(cG == 128) &&
(cB == 128) &&
block_array[index].height <= 200
)
{block_array[index].terrain = TERRAIN_MOUNTAIN;}
else if(
(cR == 0)&&
(cG == 224)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TEMP_FRESH;}
else if(
(cR == 0)&&
(cG == 192)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TEMP_BRACK;}
else if(
(cR == 0)&&
(cG == 160)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TEMP_SALT;}
else if(
(cR == 0)&&
(cG == 96)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TROP_FRESH;}
else if(
(cR == 0)&&
(cG == 64)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TROP_BRACK;}
else if(
(cR == 0)&&
(cG == 32)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_LAKE_TROP_SALT;}
else if(
(cR == 0)&&
(cG == 255)&&
(cB == 255))
{
if(block_array[index].height >= 99)
block_array[index].terrain = TERRAIN_BEACH_ARCT;
else block_array[index].terrain = TERRAIN_OCEAN_ARCT;}
else if(
(cR == 0)&&
(cG == 0)&&
(cB == 255))
{
if(block_array[index].height >= 99)
block_array[index].terrain = TERRAIN_BEACH_TROP;
else block_array[index].terrain = TERRAIN_OCEAN_TROP;}
else if(
(cR == 0)&&
(cG == 128)&&
(cB == 255))
{
if(block_array[index].height >= 99)
block_array[index].terrain = TERRAIN_BEACH_TEMP;
else block_array[index].terrain = TERRAIN_OCEAN_TEMP;}
else if(
(cR == 64)&&
(cG == 255)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_GLACIER;}
else if(
(cR == 128)&&
(cG == 255)&&
(cB == 255))
{block_array[index].terrain = TERRAIN_TUNDRA;}
else if(
(cR == 96)&&
(cG == 192)&&
(cB == 128))
{block_array[index].terrain = TERRAIN_SWAMP_TEMP_FRESH;}
else if(
(cR == 64)&&
(cG == 192)&&
(cB == 128))
{block_array[index].terrain = TERRAIN_SWAMP_TEMP_SALT;}
else if(
(cR == 96)&&
(cG == 255)&&
(cB == 128))
{block_array[index].terrain = TERRAIN_MARSH_TEMP_FRESH;}
else if(
(cR == 64)&&
(cG == 255)&&
(cB == 128))
{block_array[index].terrain = TERRAIN_MARSH_TEMP_SALT;}
else if(
(cR == 96)&&
(cG == 192)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_SWAMP_TROP_FRESH;}
else if(
(cR == 64)&&
(cG == 192)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_SWAMP_TROP_SALT;}
else if(
(cR == 64)&&
(cG == 255)&&
(cB == 96))
{block_array[index].terrain = TERRAIN_SWAMP_MANGROVE;}
else if(
(cR == 96)&&
(cG == 255)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_MARSH_TROP_FRESH;}
else if(
(cR == 64)&&
(cG == 255)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_MARSH_TROP_SALT;}
else if(
(cR == 0)&&
(cG == 96)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_FOREST_TAIGA;}
else if(
(cR == 0)&&
(cG == 96)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_FOREST_TEMP_FIR;}
else if(
(cR == 0)&&
(cG == 160)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_FOREST_TEMP_BROAD;}
else if(
(cR == 0)&&
(cG == 96)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_FOREST_TROP_FIR;}
else if(
(cR == 0)&&
(cG == 128)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_FOREST_TROP_BROAD_DRY;}
else if(
(cR == 0)&&
(cG == 160)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_FOREST_TROP_BROAD_MOIST;}
else if(
(cR == 0)&&
(cG == 255)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_GRASS_TEMP;}
else if(
(cR == 0)&&
(cG == 224)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_SAV_TEMP;}
else if(
(cR == 0)&&
(cG == 192)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_SHRUB_TEMP;}
else if(
(cR == 255)&&
(cG == 160)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_GRASS_TROP;}
else if(
(cR == 255)&&
(cG == 176)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_SAV_TROP;}
else if(
(cR == 255)&&
(cG == 192)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_SHRUB_TROP;}
else if(
(cR == 255)&&
(cG == 96)&&
(cB == 32))
{block_array[index].terrain = TERRAIN_DESERT_BAD;}
else if(
(cR == 255)&&
(cG == 255)&&
(cB == 0))
{block_array[index].terrain = TERRAIN_DESERT_SAND;}
else if(
(cR == 255)&&
(cG == 128)&&
(cB == 64))
{block_array[index].terrain = TERRAIN_DESERT_ROCK;}
else if(block_array[index].height >= 150)
{
block_array[index].terrain = TERRAIN_MOUNTAIN;
}
else block_array[index].terrain = TERRAIN_NONE;
}
}
}
}
void MapSection::load_structure_tiles(ALLEGRO_BITMAP * structuremap)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
if(structuremap)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(structuremap));
tempy = bind_to_range(tempy , al_get_bitmap_height(structuremap));
ALLEGRO_COLOR pixel = al_get_pixel(structuremap, tempx, tempy);
unsigned char cR, cG, cB;
al_unmap_rgb(pixel, &cR, &cG, &cB);
if(
(cR == 128)&&
(cG == 128)&&
(cB == 128))
{block_array[index].structure = STRUCTURE_CASTLE;}
else if(
(cR == 255)&&
(cG == 255)&&
(cB == 255))
{block_array[index].structure = STRUCTURE_VILLAGE;}
else if(
(cR == 255)&&
(cG == 128)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_CROPS1;}
else if(
(cR == 255)&&
(cG == 160)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_CROPS2;}
else if(
(cR == 255)&&
(cG == 192)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_CROPS3;}
else if(
(cR == 0)&&
(cG == 255)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_PASTURE;}
else if(
(cR == 64)&&
(cG == 255)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_MEADOW;}
else if(
(cR == 0)&&
(cG == 128)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_WOODLAND;}
else if(
(cR == 0)&&
(cG == 160)&&
(cB == 0))
{block_array[index].structure = STRUCTURE_ORCHARD;}
else if(
(cR == 20)&&
(cG == 20)&&
(cB == 20))
{block_array[index].structure = STRUCTURE_TUNNEL;}
else if(
(cR == 224)&&
(cG == 224)&&
(cB == 224))
{block_array[index].structure = STRUCTURE_BRIDGE_STONE;}
else if(
(cR == 180)&&
(cG == 167)&&
(cB == 20))
{block_array[index].structure = STRUCTURE_BRIDGE_OTHER;}
else if(
(cR == 192)&&
(cG == 192)&&
(cB == 192))
{block_array[index].structure = STRUCTURE_ROAD_STONE;}
else if(
(cR == 150)&&
(cG == 127)&&
(cB == 20))
{block_array[index].structure = STRUCTURE_ROAD_OTHER;}
else if(
(cR == 96)&&
(cG == 96)&&
(cB == 96))
{block_array[index].structure = STRUCTURE_WALL_STONE;}
else if(
(cR == 160)&&
(cG == 127)&&
(cB == 20))
{block_array[index].structure = STRUCTURE_WALL_OTHER;}
else if(
(cR == 0)&&
(cG == 192)&&
(cB == 255))
{block_array[index].structure = STRUCTURE_RIVER;}
else if(block_array[index].structure != STRUCTURE_BROOK)
block_array[index].structure = STRUCTURE_NONE;
}
}
}
}
ALLEGRO_COLOR get_color(ALLEGRO_BITMAP * input, int x, int y) {
if(input)
{
int tempx = x + user_config.map_x;
int tempy = y + user_config.map_y;
tempx = bind_to_range(tempx , al_get_bitmap_width(input));
tempy = bind_to_range(tempy , al_get_bitmap_height(input));
return al_get_pixel(input, tempx, tempy);
}
else return al_map_rgb(255,255,255);
}
void MapSection::load_colors(s_maplist * map_list)
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
block_array[index].biome_color = get_color(map_list->biome_map, x,y);
block_array[index].combined_color = get_color(map_list->combined_biome_map, x,y);
block_array[index].structure_color = get_color(map_list->structure_map, x,y);
block_array[index].trade_color = get_color(map_list->trade_map, x,y);
}
}
}
void MapSection::propogate_tiles(s_maplist * maplist)
{
clock_t start_time = clock();
load_heights(maplist->elevation_map);
load_colors(maplist);
load_water_level(maplist->elevation_map_with_water);
load_level(maplist->temperature_map, SOURCE_TEMPERATURE);
load_level(maplist->rainfall_map, SOURCE_RAINFALL);
load_level(maplist->drainage_map, SOURCE_DRAINAGE);
load_level(maplist->savagery_map, SOURCE_SAVAGERY);
load_level(maplist->volcanism_map, SOURCE_VOLCANISM);
load_level(maplist->evil_map, SOURCE_EVIL);
load_level(maplist->salinity_map, SOURCE_SALINITY);
load_biome_tiles(maplist);
load_structure_tiles(maplist->structure_map);
generate_special_tile_borders();
generate_noise();
generate_ambient_lighting();
load_time = clock() - start_time;
//now for the actual tile propogating.
clock_t prop_start_time = clock();
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
unsigned int index = x + (board_width * y);
if(tileset_list.size() > 0)
{
block_array[index].sprite = tileset_list.at(current_tileset).get_tile(block_array[index]);
block_array[index].structure_sprite = tileset_list.at(current_tileset).get_structure_tile(block_array[index]);
}
else block_array[index].sprite = 0;
}
}
tile_fetch_time = clock() - prop_start_time;
}
void MapSection::increment_tileset(void)
{
current_tileset ++;
if(current_tileset >= tileset_list.size())
current_tileset = 0;
}
void MapSection::load_tilesets(const char * index_file)
{
ALLEGRO_CONFIG * config = 0;
ALLEGRO_PATH * key = al_create_path(index_file);
config = al_load_config_file(al_path_cstr(key, ALLEGRO_NATIVE_PATH_SEP));
int num_tilesets = get_config_int(config, "TILESETS", "num_tilesets");
char buffer[256];
for(size_t i = 0; i < num_tiles; i++)
{
sprintf(buffer, "tileset_%d", int(i));
const char * file = al_get_config_value(config, "TILESETS", buffer);
if(file)
{
ALLEGRO_PATH * temp = al_create_path(file);
al_rebase_path(key, temp);
TileSet temp_tileset;
temp_tileset.load_ini(temp);
tileset_list.push_back(temp_tileset);
al_destroy_path(temp);
}
}
al_destroy_path(key);
}
int MapSection::snap_height(int in)
{
if(tileset_list.at(current_tileset).snap_height <= 1)
return in;
return (in - (in % tileset_list.at(current_tileset).snap_height));
}
void MapSection::generate_special_tile_borders()
{
for (unsigned int y = 0; y < board_width; y++)
{
for (unsigned int x = 0; x < board_height; x++)
{
for(int i = 0; i < TERRAIN_COUNT ; i++)
{
unsigned char borders = 0;
if((x > 0) && (y > 0))
{
if(block_array[((x-1) + (board_width * (y-1)))].terrain == i) borders |= 1;
}
if(y > 0)
{
if(block_array[((x+0) + (board_width * (y-1)))].terrain == i) borders |= 2;
}
if(x < (board_width-1) && y > 0)
{
if(block_array[((x+1) + (board_width * (y-1)))].terrain == i) borders |= 4;
}
if(x < (board_width-1))
{
if(block_array[((x+1) + (board_width * (y+0)))].terrain == i) borders |= 8;
}
if(x < (board_width-1) && y < (board_height-1))
{
if(block_array[((x+1) + (board_width * (y+1)))].terrain == i) borders |= 16;
}
if(y < (board_height-1))
{
if(block_array[((x+0) + (board_width * (y+1)))].terrain == i) borders |= 32;
}
if(x > 0 && y < (board_height-1))
{
if(block_array[((x-1) + (board_width * (y+1)))].terrain == i) borders |= 64;
}
if(x > 0)
{
if(block_array[((x-1) + (board_width * (y+0)))].terrain == i) borders |= 128;
}
block_array[(x + (board_width * y))].terrain_borders[i]=borders;
}
for(int i = 0; i < STRUCTURE_COUNT ; i++)
{
unsigned char borders = 0;
if((x > 0) && (y > 0))
{
if(block_array[((x-1) + (board_width * (y-1)))].structure == i) borders |= 1;
}
if(y > 0)
{
if(block_array[((x+0) + (board_width * (y-1)))].structure == i) borders |= 2;
}
if(x < (board_width-1) && y > 0)
{
if(block_array[((x+1) + (board_width * (y-1)))].structure == i) borders |= 4;
}
if(x < (board_width-1))
{
if(block_array[((x+1) + (board_width * (y+0)))].structure == i) borders |= 8;
}
if(x < (board_width-1) && y < (board_height-1))
{
if(block_array[((x+1) + (board_width * (y+1)))].structure == i) borders |= 16;
}
if(y < (board_height-1))
{
if(block_array[((x+0) + (board_width * (y+1)))].structure == i) borders |= 32;