forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cata_tiles.cpp
1671 lines (1505 loc) · 54.8 KB
/
cata_tiles.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
#if (defined SDLTILES)
#include <algorithm>
#include "cata_tiles.h"
#include "debug.h"
#include "json.h"
#include "path_info.h"
#include "monstergenerator.h"
#include "item.h"
#include "veh_type.h"
#include <fstream>
#include "SDL2/SDL_image.h"
#define dbg(x) DebugLog((DebugLevel)(x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
#define ITEM_HIGHLIGHT "highlight_item"
extern game *g;
//extern SDL_Surface *screen;
extern int WindowHeight, WindowWidth;
extern int fontwidth, fontheight;
static const std::string empty_string;
static const std::string TILE_CATEGORY_IDS[] = {
"", // C_NONE,
"vehicle_part", // C_VEHICLE_PART,
"terrain", // C_TERRAIN,
"item", // C_ITEM,
"furniture", // C_FURNITURE,
"trap", // C_TRAP,
"field", // C_FIELD,
"lighting", // C_LIGHTING,
"monster", // C_MONSTER,
"bullet", // C_BULLET,
"hit_entity", // C_HIT_ENTITY,
"weather", // C_WEATHER,
};
cata_tiles::cata_tiles(SDL_Renderer *render)
{
//ctor
renderer = render;
tile_height = 0;
tile_width = 0;
tile_ratiox = 0;
tile_ratioy = 0;
in_animation = false;
do_draw_explosion = false;
do_draw_bullet = false;
do_draw_hit = false;
do_draw_line = false;
do_draw_weather = false;
do_draw_sct = false;
do_draw_zones = false;
boomered = false;
sight_impaired = false;
bionight_bionic_active = false;
last_pos_x = 0;
last_pos_y = 0;
}
cata_tiles::~cata_tiles()
{
clear();
}
void cata_tiles::clear()
{
// release maps
for (tile_iterator it = tile_values.begin(); it != tile_values.end(); ++it) {
SDL_DestroyTexture(*it);
}
tile_values.clear();
for (tile_id_iterator it = tile_ids.begin(); it != tile_ids.end(); ++it) {
it->second = NULL;
}
tile_ids.clear();
}
void cata_tiles::init(std::string load_file_path)
{
std::string json_path, tileset_path;
// get path information from load_file_path
get_tile_information(load_file_path, json_path, tileset_path);
// send this information to old init to avoid redundant code
load_tilejson(json_path, tileset_path);
}
void cata_tiles::reinit(std::string load_file_path)
{
clear_buffer();
clear();
init(load_file_path);
}
void cata_tiles::get_tile_information(std::string dir_path, std::string &json_path, std::string &tileset_path)
{
dbg( D_INFO ) << "Attempting to Initialize JSON and TILESET path information from [" << dir_path << "]";
const std::string filename = "tileset.txt"; // tileset-information-file
const std::string default_json = FILENAMES["defaulttilejson"]; // defaults
const std::string default_tileset = FILENAMES["defaulttilepng"];
std::vector<std::string> files;
files = file_finder::get_files_from_path(filename, dir_path, true); // search for the files (tileset.txt)
for(std::vector<std::string>::iterator it = files.begin(); it != files.end(); ++it) { // iterate through every file found
std::ifstream fin;
fin.open(it->c_str());
if(!fin.is_open()) {
fin.close();
dbg( D_ERROR ) << "Could not read " << *it << " -- Setting to default values!";
json_path = default_json;
tileset_path = default_tileset;
return;
}
// should only have 2 values inside it, otherwise is going to only load the last 2 values
while(!fin.eof()) {
std::string sOption;
fin >> sOption;
if(sOption == "") {
getline(fin, sOption); // Empty line, chomp it
} else if(sOption[0] == '#') { // # indicates a comment
getline(fin, sOption);
} else {
if (sOption.find("NAME") != std::string::npos) {
std::string tileset_name;
tileset_name = "";
fin >> tileset_name;
if(tileset_name != OPTIONS["TILES"].getValue()) { // if the current tileset name isn't the same
// as the current one in the options break
break; // out of the while loop, close the file and
} // continue with the next file
} else if (sOption.find("VIEW") != std::string::npos) { // we don't need the view name here
getline(fin, sOption); // so we just skip it
} else if (sOption.find("JSON") != std::string::npos) {
fin >> json_path;
json_path = FILENAMES["gfxdir"] + json_path;
dbg( D_INFO ) << "JSON path set to [" << json_path << "].";
} else if (sOption.find("TILESET") != std::string::npos) {
fin >> tileset_path;
tileset_path = FILENAMES["gfxdir"] + tileset_path;
dbg( D_INFO ) << "TILESET path set to [" << tileset_path << "].";
}
}
}
fin.close();
}
if (json_path == "") {
json_path = default_json;
dbg( D_INFO ) << "JSON set to default [" << json_path << "].";
}
if (tileset_path == "") {
tileset_path = default_tileset;
dbg( D_INFO ) << "TILESET set to default [" << tileset_path << "].";
}
}
int cata_tiles::load_tileset(std::string path, int R, int G, int B)
{
std::string img_path = path;
#ifdef PREFIX // use the PREFIX path over the current directory
img_path = (FILENAMES["datadir"] + "/" + img_path);
#endif
/** reinit tile_atlas */
SDL_Surface *tile_atlas = IMG_Load(img_path.c_str());
if(!tile_atlas) {
throw std::string("Could not load tileset image at ") + img_path + ", error: " + IMG_GetError();
}
/** get dimensions of the atlas image */
int w = tile_atlas->w;
int h = tile_atlas->h;
/** sx and sy will take care of any extraneous pixels that do not add up to a full tile */
int sx = w / tile_width;
int sy = h / tile_height;
sx *= tile_width;
sy *= tile_height;
// Set up initial source and destination information. Destination is going to be unchanging
SDL_Rect source_rect = {0,0,tile_width,tile_height};
SDL_Rect dest_rect = {0,0,tile_width,tile_height};
/** split the atlas into tiles using SDL_Rect structs instead of slicing the atlas into individual surfaces */
int tilecount = 0;
for (int y = 0; y < sy; y += tile_height) {
for (int x = 0; x < sx; x += tile_width) {
source_rect.x = x;
source_rect.y = y;
SDL_Surface *tile_surf = create_tile_surface();
if( tile_surf == nullptr ) {
continue;
}
if( SDL_BlitSurface( tile_atlas, &source_rect, tile_surf, &dest_rect ) != 0 ) {
dbg( D_ERROR ) << "SDL_BlitSurface failed: " << SDL_GetError();
}
if (R >= 0 && R <= 255 && G >= 0 && G <= 255 && B >= 0 && B <= 255) {
Uint32 key = SDL_MapRGB(tile_surf->format, 0,0,0);
SDL_SetColorKey(tile_surf, SDL_TRUE, key);
SDL_SetSurfaceRLE(tile_surf, true);
}
SDL_Texture *tile_tex = SDL_CreateTextureFromSurface(renderer,tile_surf);
if( tile_tex == nullptr ) {
dbg( D_ERROR) << "failed to create texture: " << SDL_GetError();
}
SDL_FreeSurface(tile_surf);
if( tile_tex != nullptr ) {
tile_values.push_back(tile_tex);
tilecount++;
}
}
}
dbg( D_INFO ) << "Tiles Created: " << tilecount;
SDL_FreeSurface(tile_atlas);
return tilecount;
}
void cata_tiles::set_draw_scale(int scale) {
tile_width = default_tile_width * scale / 16;
tile_height = default_tile_height * scale / 16;
tile_ratiox = ((float)tile_width/(float)fontwidth);
tile_ratioy = ((float)tile_height/(float)fontheight);
}
void cata_tiles::load_tilejson(std::string path, const std::string &image_path)
{
dbg( D_INFO ) << "Attempting to Load JSON file " << path;
std::ifstream config_file(path.c_str(), std::ifstream::in | std::ifstream::binary);
if (!config_file.good()) {
throw std::string("failed to open tile info json: ") + path;
}
load_tilejson_from_file( config_file, image_path );
if (tile_ids.count("unknown") == 0) {
debugmsg("The tileset you're using has no 'unknown' tile defined!");
}
}
void cata_tiles::load_tilejson_from_file(std::ifstream &f, const std::string &image_path)
{
JsonIn config_json(f);
// it's all one json object
JsonObject config = config_json.get_object();
/** 1) Make sure that the loaded file has the "tile_info" section */
if (!config.has_member("tile_info")) {
config.throw_error( "\"tile_info\" missing" );
}
JsonArray info = config.get_array("tile_info");
while (info.has_more()) {
JsonObject curr_info = info.next_object();
tile_height = curr_info.get_int("height");
tile_width = curr_info.get_int("width");
default_tile_width = tile_width;
default_tile_height = tile_height;
}
set_draw_scale(16);
/** 2) Load tile information if available */
if (config.has_array("tiles-new")) {
// new system, several entries
// When loading multiple tileset images this defines where
// the tiles from the most recently loaded image start from.
int offset = 0;
JsonArray tiles_new = config.get_array("tiles-new");
while (tiles_new.has_more()) {
JsonObject tile_part_def = tiles_new.next_object();
const std::string tileset_image_path = tile_part_def.get_string("file");
int R = -1;
int G = -1;
int B = -1;
if (tile_part_def.has_object("transparency")) {
JsonObject tra = tile_part_def.get_object("transparency");
R = tra.get_int("R");
G = tra.get_int("G");
B = tra.get_int("B");
}
// First load the tileset image to get the number of available tiles.
dbg( D_INFO ) << "Attempting to Load Tileset file " << tileset_image_path;
const int newsize = load_tileset(tileset_image_path, R, G, B);
// Now load the tile definitions for the loaded tileset image.
load_tilejson_from_file(tile_part_def, offset, newsize);
if (tile_part_def.has_member("ascii")) {
load_ascii_tilejson_from_file(tile_part_def, offset, newsize);
}
// Make sure the tile definitions of the next tileset image don't
// override the current ones.
offset += newsize;
}
} else {
// old system, no tile file path entry, only one array of tiles
dbg( D_INFO ) << "Attempting to Load Tileset file " << image_path;
const int newsize = load_tileset(image_path, -1, -1, -1);
load_tilejson_from_file(config, 0, newsize);
}
}
void cata_tiles::add_ascii_subtile(tile_type *curr_tile, const std::string &t_id, int fg, const std::string &s_id)
{
const std::string m_id = t_id + "_" + s_id;
tile_type *curr_subtile = new tile_type();
curr_subtile->fg = fg;
curr_subtile->bg = -1;
curr_subtile->rotates = true;
tile_ids[m_id] = curr_subtile;
curr_tile->available_subtiles.push_back(s_id);
}
void cata_tiles::load_ascii_tilejson_from_file(JsonObject &config, int offset, int size)
{
if (!config.has_member("ascii")) {
config.throw_error( "\"ascii\" section missing" );
}
JsonArray ascii = config.get_array("ascii");
while (ascii.has_more()) {
JsonObject entry = ascii.next_object();
load_ascii_set(entry, offset, size);
}
}
void cata_tiles::load_ascii_set(JsonObject &entry, int offset, int size)
{
// tile for ASCII char 0 is at `in_image_offset`,
// the other ASCII chars follow from there.
const int in_image_offset = entry.get_int("offset");
if (in_image_offset >= size) {
entry.throw_error("invalid offset (out of range)", "offset");
}
// color, of the ASCII char. Can be -1 to indicate all/default colors.
int FG = -1;
const std::string scolor = entry.get_string("color", "DEFAULT");
if (scolor == "BLACK") {
FG = COLOR_BLACK;
} else if (scolor == "RED") {
FG = COLOR_RED;
} else if (scolor == "GREEN") {
FG = COLOR_GREEN;
} else if (scolor == "YELLOW") {
FG = COLOR_YELLOW;
} else if (scolor == "BLUE") {
FG = COLOR_BLUE;
} else if (scolor == "MAGENTA") {
FG = COLOR_MAGENTA;
} else if (scolor == "CYAN") {
FG = COLOR_CYAN;
} else if (scolor == "WHITE") {
FG = COLOR_WHITE;
} else if (scolor == "DEFAULT") {
FG = -1;
} else {
entry.throw_error("invalid color for ascii", "color");
}
// Add an offset for bold colors (ncrses has this bold attribute,
// this mimics it). bold does not apply to default color.
if (FG != -1 && entry.get_bool("bold", false)) {
FG += 8;
}
const int base_offset = offset + in_image_offset;
// template for the id of the ascii chars:
// X is replaced by ascii code (converted to char)
// F is replaced by foreground (converted to char)
// B is replaced by background (converted to char)
std::string id("ASCII_XFB");
// Finally load all 256 ascii chars (actually extended ascii)
for (int ascii_char = 0; ascii_char < 256; ascii_char++) {
const int index_in_image = ascii_char + in_image_offset;
if (index_in_image < 0 || index_in_image >= size) {
// Out of range is ignored for now.
continue;
}
id[6] = static_cast<char>(ascii_char);
id[7] = static_cast<char>(FG);
id[8] = static_cast<char>(-1);
tile_type *curr_tile = new tile_type();
curr_tile->fg = index_in_image + offset;
curr_tile->bg = 0;
switch(ascii_char) {
case LINE_OXOX_C://box bottom/top side (horizontal line)
curr_tile->fg = 205 + base_offset;
break;
case LINE_XOXO_C://box left/right side (vertical line)
curr_tile->fg = 186 + base_offset;
break;
case LINE_OXXO_C://box top left
curr_tile->fg = 201 + base_offset;
break;
case LINE_OOXX_C://box top right
curr_tile->fg = 187 + base_offset;
break;
case LINE_XOOX_C://box bottom right
curr_tile->fg = 188 + base_offset;
break;
case LINE_XXOO_C://box bottom left
curr_tile->fg = 200 + base_offset;
break;
case LINE_XXOX_C://box bottom north T (left, right, up)
curr_tile->fg = 202 + base_offset;
break;
case LINE_XXXO_C://box bottom east T (up, right, down)
curr_tile->fg = 208 + base_offset;
break;
case LINE_OXXX_C://box bottom south T (left, right, down)
curr_tile->fg = 203 + base_offset;
break;
case LINE_XXXX_C://box X (left down up right)
curr_tile->fg = 206 + base_offset;
break;
case LINE_XOXX_C://box bottom east T (left, down, up)
curr_tile->fg = 184 + base_offset;
break;
}
tile_ids[id] = curr_tile;
if (ascii_char == LINE_XOXO_C || ascii_char == LINE_OXOX_C) {
curr_tile->rotates = false;
curr_tile->multitile = true;
add_ascii_subtile(curr_tile, id, 206 + base_offset, "center");
add_ascii_subtile(curr_tile, id, 201 + base_offset, "corner");
add_ascii_subtile(curr_tile, id, 186 + base_offset, "edge");
add_ascii_subtile(curr_tile, id, 203 + base_offset, "t_connection");
add_ascii_subtile(curr_tile, id, 208 + base_offset, "end_piece");
add_ascii_subtile(curr_tile, id, 219 + base_offset, "unconnected");
}
}
}
void cata_tiles::load_tilejson_from_file(JsonObject &config, int offset, int size)
{
if (!config.has_member("tiles")) {
config.throw_error( "\"tiles\" section missing" );
}
JsonArray tiles = config.get_array("tiles");
while (tiles.has_more()) {
JsonObject entry = tiles.next_object();
std::string t_id = entry.get_string("id");
tile_type *curr_tile = load_tile(entry, t_id, offset, size);
bool t_multi = entry.get_bool("multitile", false);
bool t_rota = entry.get_bool("rotates", t_multi);
if (t_multi) {
// fetch additional tiles
JsonArray subentries = entry.get_array("additional_tiles");
while (subentries.has_more()) {
JsonObject subentry = subentries.next_object();
const std::string s_id = subentry.get_string("id");
const std::string m_id = t_id + "_" + s_id;
tile_type *curr_subtile = load_tile(subentry, m_id, offset, size);
curr_subtile->rotates = true;
curr_tile->available_subtiles.push_back(s_id);
}
}
// write the information of the base tile to curr_tile
curr_tile->multitile = t_multi;
curr_tile->rotates = t_rota;
}
dbg( D_INFO ) << "Tile Width: " << tile_width << " Tile Height: " << tile_height << " Tile Definitions: " << tile_ids.size();
}
tile_type *cata_tiles::load_tile(JsonObject &entry, const std::string &id, int offset, int size)
{
int fg = entry.get_int("fg", -1);
int bg = entry.get_int("bg", -1);
if (fg == -1) {
// OK, keep this value, indicates "doesn't have a foreground"
} else if (fg < 0 || fg >= size) {
entry.throw_error("invalid value for fg (out of range)", "fg");
} else {
fg += offset;
}
if (bg == -1) {
// OK, keep this value, indicates "doesn't have a background"
} else if (bg < 0 || bg >= size) {
entry.throw_error("invalid value for bg (out of range)", "bg");
} else {
bg += offset;
}
tile_type *curr_subtile = new tile_type();
curr_subtile->fg = fg;
curr_subtile->bg = bg;
tile_ids[id] = curr_subtile;
return curr_subtile;
}
void cata_tiles::draw(int destx, int desty, int centerx, int centery, int width, int height)
{
if (!g) {
return;
}
{
//set clipping to prevent drawing over stuff we shouldn't
SDL_Rect clipRect = {destx, desty, width, height};
SDL_RenderSetClipRect(renderer, &clipRect);
}
int posx = centerx;
int posy = centery;
int sx, sy;
get_window_tile_counts(width, height, sx, sy);
init_light();
int x, y;
LIGHTING l;
o_x = posx - POSX;
o_y = posy - POSY;
op_x = destx;
op_y = desty;
// Rounding up to include incomplete tiles at the bottom/right edges
screentile_width = (width + tile_width - 1) / tile_width;
screentile_height = (height + tile_height - 1) / tile_height;
for (int my = 0; my < sy; ++my) {
for (int mx = 0; mx < sx; ++mx) {
x = mx + o_x;
y = my + o_y;
l = light_at(x, y);
if (l != CLEAR) {
// Draw lighting
draw_lighting(x, y, l);
// continue on to next part of loop
continue;
}
// light is no longer being considered, for now.
// Draw Terrain if possible. If not possible then we need to continue on to the next part of loop
if (!draw_terrain(x, y)) {
continue;
}
draw_furniture(x, y);
draw_trap(x, y);
draw_field_or_item(x, y);
draw_vpart(x, y);
draw_entity(x, y);
draw_entity_with_overlays(x, y);
}
}
in_animation = do_draw_explosion || do_draw_bullet || do_draw_hit ||
do_draw_line || do_draw_weather || do_draw_sct ||
do_draw_zones;
draw_footsteps_frame();
if (in_animation) {
if (do_draw_explosion) {
draw_explosion_frame();
}
if (do_draw_bullet) {
draw_bullet_frame();
}
if (do_draw_hit) {
draw_hit_frame();
void_hit();
}
if (do_draw_line) {
draw_line();
void_line();
}
if (do_draw_weather) {
draw_weather_frame();
void_weather();
}
if (do_draw_sct) {
draw_sct_frame();
void_sct();
}
if (do_draw_zones) {
draw_zones_frame();
void_zones();
}
}
// check to see if player is located at ter
else if (g->u.posx + g->u.view_offset_x != g->ter_view_x ||
g->u.posy + g->u.view_offset_y != g->ter_view_y) {
draw_from_id_string("cursor", C_NONE, empty_string, g->ter_view_x, g->ter_view_y, 0, 0);
}
SDL_RenderSetClipRect(renderer, NULL);
}
void cata_tiles::clear_buffer()
{
//TODO convert this to use sdltiles ClearScreen() function
SDL_RenderClear(renderer);
}
void cata_tiles::get_window_tile_counts(const int width, const int height, int &columns, int &rows) const
{
columns = ceil((double) width / tile_width);
rows = ceil((double) height / tile_height);
}
bool cata_tiles::draw_from_id_string(std::string id, int x, int y, int subtile, int rota)
{
return cata_tiles::draw_from_id_string(id, C_NONE, empty_string, x, y, subtile, rota);
}
bool cata_tiles::draw_from_id_string(std::string id, TILE_CATEGORY category,
const std::string &subcategory, int x, int y,
int subtile, int rota)
{
// If the ID string does not produce a drawable tile
// it will revert to the "unknown" tile.
// The "unknown" tile is one that is highly visible so you kinda can't miss it :D
// check to make sure that we are drawing within a valid area
// [0->width|height / tile_width|height]
if( x - o_x < 0 || x - o_x >= screentile_width ||
y - o_y < 0 || y - o_y >= screentile_height ) {
return false;
}
std::string seasonal_id;
switch (calendar::turn.get_season()) {
case SPRING:
seasonal_id = id + "_season_spring";
break;
case SUMMER:
seasonal_id = id + "_season_summer";
break;
case AUTUMN:
seasonal_id = id + "_season_autumn";
break;
case WINTER:
seasonal_id = id + "_season_winter";
break;
}
tile_id_iterator it = tile_ids.find(seasonal_id);
if (it == tile_ids.end()) {
it = tile_ids.find(id);
} else {
id = seasonal_id;
}
if (it == tile_ids.end()) {
long sym = -1;
nc_color col = c_white;
if (category == C_FURNITURE) {
if (furnmap.count(id) > 0) {
const furn_t &f = furnmap[id];
sym = f.sym;
col = f.color;
}
} else if (category == C_TERRAIN) {
if (termap.count(id) > 0) {
const ter_t &t = termap[id];
sym = t.sym;
col = t.color;
}
} else if (category == C_MONSTER) {
if (MonsterGenerator::generator().has_mtype(id)) {
const mtype *m = MonsterGenerator::generator().get_mtype(id);
int len = m->sym.length();
const char *s = m->sym.c_str();
sym = UTF8_getch(&s, &len);
col = m->color;
}
} else if (category == C_VEHICLE_PART) {
if (vehicle_part_types.count(id.substr(3)) > 0) {
const vpart_info &v = vehicle_part_types[id.substr(3)];
sym = v.sym;
if (!subcategory.empty()) {
sym = special_symbol(subcategory[0]);
rota = 0;
subtile = -1;
}
col = v.color;
}
} else if (category == C_FIELD) {
const field_id fid = field_from_ident( id );
sym = fieldlist[fid].sym;
// TODO: field density?
col = fieldlist[fid].color[0];
} else if (category == C_TRAP) {
if (trapmap.count(id) > 0) {
const trap *t = traplist[trapmap[id]];
sym = t->sym;
col = t->color;
}
} else if (category == C_ITEM) {
const auto tmp = item( id, 0 );
sym = tmp.symbol();
col = tmp.color();
}
// Special cases for walls
switch(sym) {
case LINE_XOXO: sym = LINE_XOXO_C; break;
case LINE_OXOX: sym = LINE_OXOX_C; break;
case LINE_XXOO: sym = LINE_XXOO_C; break;
case LINE_OXXO: sym = LINE_OXXO_C; break;
case LINE_OOXX: sym = LINE_OOXX_C; break;
case LINE_XOOX: sym = LINE_XOOX_C; break;
case LINE_XXXO: sym = LINE_XXXO_C; break;
case LINE_XXOX: sym = LINE_XXOX_C; break;
case LINE_XOXX: sym = LINE_XOXX_C; break;
case LINE_OXXX: sym = LINE_OXXX_C; break;
case LINE_XXXX: sym = LINE_XXXX_C; break;
default: break; // sym goes unchanged
}
if (sym != 0 && sym < 256 && sym >= 0) {
// see cursesport.cpp, function wattron
const int pairNumber = (col & A_COLOR) >> 17;
const pairs &colorpair = colorpairs[pairNumber];
// What about isBlink?
const bool isBold = col & A_BOLD;
const int FG = colorpair.FG + (isBold ? 8 : 0);
// const int BG = colorpair.BG;
// static so it does not need to be allocated every time,
// see load_ascii_set for the meaning
static std::string generic_id("ASCII_XFG");
generic_id[6] = static_cast<char>(sym);
generic_id[7] = static_cast<char>(FG);
generic_id[8] = static_cast<char>(-1);
if (tile_ids.count(generic_id) > 0) {
return draw_from_id_string(generic_id, x, y, subtile, rota);
}
// Try again without color this time (using default color).
generic_id[7] = static_cast<char>(-1);
generic_id[8] = static_cast<char>(-1);
if (tile_ids.count(generic_id) > 0) {
return draw_from_id_string(generic_id, x, y, subtile, rota);
}
}
}
// if id is not found, try to find a tile for the category+subcategory combination
if (it == tile_ids.end()) {
const std::string &category_id = TILE_CATEGORY_IDS[category];
if(!category_id.empty() && !subcategory.empty()) {
it = tile_ids.find("unknown_" + category_id + "_" + subcategory);
}
}
// if at this point we have no tile, try just the category
if (it == tile_ids.end()) {
const std::string &category_id = TILE_CATEGORY_IDS[category];
if(!category_id.empty()) {
it = tile_ids.find("unknown_" + category_id);
}
}
// if we still have no tile, we're out of luck, fall back to unknown
if (it == tile_ids.end()) {
it = tile_ids.find("unknown");
}
// this really shouldn't happen, but the tileset creator might have forgotten to define an unknown tile
if (it == tile_ids.end()) {
return false;
}
tile_type *display_tile = it->second;
// if found id does not have a valid tile_type then return unknown tile
if (!display_tile) {
return draw_from_id_string("unknown", x, y, subtile, rota);
}
// if both bg and fg are -1 then return unknown tile
if (display_tile->bg == -1 && display_tile->fg == -1) {
return draw_from_id_string("unknown", x, y, subtile, rota);
}
// check to see if the display_tile is multitile, and if so if it has the key related to subtile
if (subtile != -1 && display_tile->multitile) {
std::vector<std::string> display_subtiles = display_tile->available_subtiles;
if (std::find(display_subtiles.begin(), display_subtiles.end(), multitile_keys[subtile]) != display_subtiles.end()) {
// append subtile name to tile and re-find display_tile
const std::string new_id = id + "_" + multitile_keys[subtile];
return draw_from_id_string(new_id, x, y, -1, rota);
}
}
// make sure we aren't going to rotate the tile if it shouldn't be rotated
if (!display_tile->rotates) {
rota = 0;
}
// translate from player-relative to screen relative tile position
const int screen_x = (x - o_x) * tile_width + op_x;
const int screen_y = (y - o_y) * tile_height + op_y;
//draw it!
draw_tile_at(display_tile, screen_x, screen_y, rota);
return true;
}
bool cata_tiles::draw_tile_at(tile_type *tile, int x, int y, int rota)
{
// don't need to check for tile existance, should always exist if it gets this far
const int fg = tile->fg;
const int bg = tile->bg;
SDL_Rect destination;
destination.x = x;
destination.y = y;
destination.w = tile_width;
destination.h = tile_height;
// blit background first : always non-rotated
if( bg >= 0 && static_cast<size_t>( bg ) < tile_values.size() ) {
SDL_Texture *bg_tex = tile_values[bg];
if( SDL_RenderCopyEx( renderer, bg_tex, NULL, &destination, 0, NULL, SDL_FLIP_NONE ) != 0 ) {
dbg( D_ERROR ) << "SDL_RenderCopyEx(bg) failed: " << SDL_GetError();
}
}
int ret = 0;
// blit foreground based on rotation
if (rota == 0) {
if (fg >= 0 && static_cast<size_t>( fg ) < tile_values.size()) {
SDL_Texture *fg_tex = tile_values[fg];
ret = SDL_RenderCopyEx( renderer, fg_tex, NULL, &destination, 0, NULL, SDL_FLIP_NONE );
}
} else {
if (fg >= 0 && static_cast<size_t>( fg ) < tile_values.size()) {
SDL_Texture *fg_tex = tile_values[fg];
if(rota == 1) {
#if (defined _WIN32 || defined WINDOWS)
destination.y -= 1;
#endif
ret = SDL_RenderCopyEx( renderer, fg_tex, NULL, &destination,
-90, NULL, SDL_FLIP_NONE );
} else if(rota == 2) {
//flip rather then rotate here
ret = SDL_RenderCopyEx( renderer, fg_tex, NULL, &destination,
0, NULL, static_cast<SDL_RendererFlip>( SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL ) );
} else { //rota == 3
#if (defined _WIN32 || defined WINDOWS)
destination.x -= 1;
#endif
ret = SDL_RenderCopyEx( renderer, fg_tex, NULL, &destination,
90, NULL, SDL_FLIP_NONE );
}
}
}
if( ret != 0 ) {
dbg( D_ERROR ) << "SDL_RenderCopyEx(fg) failed: " << SDL_GetError();
}
return true;
}
bool cata_tiles::draw_lighting(int x, int y, LIGHTING l)
{
std::string light_name;
switch(l) {
case HIDDEN:
light_name = "lighting_hidden";
break;
case LIGHT_NORMAL:
light_name = "lighting_lowlight_light";
break;
case LIGHT_DARK:
light_name = "lighting_lowlight_dark";
break;
case BOOMER_NORMAL:
light_name = "lighting_boomered_light";
break;
case BOOMER_DARK:
light_name = "lighting_boomered_dark";
break;
case CLEAR: // Actually handled by the caller.
return false;
}
// lighting is never rotated, though, could possibly add in random rotation?
draw_from_id_string(light_name, C_LIGHTING, empty_string, x, y, 0, 0);
return false;
}
bool cata_tiles::draw_terrain(int x, int y)
{
int t = g->m.ter(x, y); // get the ter_id value at this point
// check for null, if null return false
if (t == t_null) {
return false;
}
// need to check for walls, and then deal with wallfication details!
int s = terlist[t].sym;
//char alteration = 0;
int subtile = 0, rotation = 0;
// check walls
if (s == LINE_XOXO /*vertical*/ || s == LINE_OXOX /*horizontal*/) {
get_wall_values(x, y, LINE_XOXO, LINE_OXOX, subtile, rotation);
}
// check windows and doors for wall connections, may or may not have a subtile available, but should be able to rotate to some extent
else if (s == '"' || s == '+' || s == '\'') {
get_wall_values(x, y, LINE_XOXO, LINE_OXOX, subtile, rotation);
} else {
get_terrain_orientation(x, y, rotation, subtile);
// do something to get other terrain orientation values
}
std::string tname;
tname = terlist[t].id;
return draw_from_id_string(tname, C_TERRAIN, empty_string, x, y, subtile, rotation);
}
bool cata_tiles::draw_furniture(int x, int y)
{
// get furniture ID at x,y
bool has_furn = g->m.has_furn(x, y);
if (!has_furn) {
return false;
}
int f_id = g->m.furn(x, y);
// for rotation information
const int neighborhood[4] = {
static_cast<int> (g->m.furn(x, y + 1)), // south
static_cast<int> (g->m.furn(x + 1, y)), // east
static_cast<int> (g->m.furn(x - 1, y)), // west
static_cast<int> (g->m.furn(x, y - 1)) // north
};
int subtile = 0, rotation = 0;
get_tile_values(f_id, neighborhood, subtile, rotation);
// get the name of this furniture piece
std::string f_name = furnlist[f_id].id; // replace with furniture names array access
bool ret = draw_from_id_string(f_name, C_FURNITURE, empty_string, x, y, subtile, rotation);
if (ret && g->m.sees_some_items(x, y, g->u)) {
draw_item_highlight(x, y);
}
return ret;
}
bool cata_tiles::draw_trap(int x, int y)
{
int tr_id = g->m.tr_at(x, y);
if (tr_id == tr_null) {
return false;
}
if (!traplist[tr_id]->can_see(g->u, x, y)) {
return false;
}
const std::string tr_name = traplist[tr_id]->id;
const int neighborhood[4] = {
static_cast<int> (g->m.tr_at(x, y + 1)), // south
static_cast<int> (g->m.tr_at(x + 1, y)), // east
static_cast<int> (g->m.tr_at(x - 1, y)), // west
static_cast<int> (g->m.tr_at(x, y - 1)) // north
};
int subtile = 0, rotation = 0;
get_tile_values(tr_id, neighborhood, subtile, rotation);
return draw_from_id_string(tr_name, C_TRAP, empty_string, x, y, subtile, rotation);
}
bool cata_tiles::draw_field_or_item(int x, int y)
{
// check for field
const field &f = g->m.field_at(x, y);
field_id f_id = f.fieldSymbol();
bool is_draw_field;
bool do_item;
switch(f_id) {
case fd_null:
//only draw items
is_draw_field = false;
do_item = true;
break;
case fd_blood:
case fd_blood_veggy:
case fd_blood_insect:
case fd_blood_invertebrate:
case fd_gibs_flesh:
case fd_gibs_veggy:
case fd_gibs_insect: