forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overmap_ui.cpp
2196 lines (1982 loc) · 88.9 KB
/
overmap_ui.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 "overmap_ui.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cstddef>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "activity_actor_definitions.h"
#include "avatar.h"
#include "basecamp.h"
#include "cached_options.h"
#include "calendar.h"
#ifdef TILES
#include "cata_tiles.h"
#endif // TILES
#include "cata_utility.h"
#include "catacharset.h"
#include "clzones.h"
#include "color.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "cursesdef.h"
#include "cursesport.h"
#include "distribution_grid.h"
#include "enums.h"
#include "game.h"
#include "game_constants.h"
#include "game_ui.h"
#include "hash_utils.h"
#include "ime.h"
#include "input.h"
#include "int_id.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapbuffer.h"
#include "mission.h"
#include "mongroup.h"
#include "npc.h"
#include "omdata.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "overmap_types.h"
#include "overmapbuffer.h"
#include "overmap_special.h"
#include "regional_settings.h"
#include "rng.h"
#include "sdltiles.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "string_input_popup.h"
#include "string_utils.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
#include "ui_manager.h"
#include "uistate.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "weather.h"
#include "weather_gen.h"
static const activity_id ACT_TRAVELLING( "ACT_TRAVELLING" );
static const mongroup_id GROUP_FOREST( "GROUP_FOREST" );
static const trait_id trait_DEBUG_NIGHTVISION( "DEBUG_NIGHTVISION" );
#if defined(__ANDROID__)
#include <SDL_keyboard.h>
#endif
static constexpr int UILIST_MAP_NOTE_DELETED = -2047;
static constexpr int UILIST_MAP_NOTE_EDITED = -2048;
static constexpr int UILIST_CHANGE_SORT = -2049;
static constexpr int max_note_length = 450;
static constexpr int max_note_display_length = 45;
/** Note preview map width without borders. Odd number. */
static const int npm_width = 3;
/** Note preview map height without borders. Odd number. */
static const int npm_height = 3;
namespace overmap_ui
{
// persistent data for distribution grid debug drawing
struct grids_draw_data {
public:
std::optional<char> get_active( const tripoint_abs_omt &omp ) {
// TODO: fix point types
uintptr_t id = get_distribution_grid_tracker().debug_grid_id( omp );
if( id == 0 ) {
return std::nullopt;
}
auto it = list_active.find( id );
if( it != list_active.end() ) {
return it->second;
}
auto ch = pick_char( [this]( char c ) -> bool {
for( const auto &it : list_active ) {
if( it.second == c ) {
return false;
}
}
return true;
} );
char c = ch.has_value() ? *ch : '?';
list_active.insert( std::make_pair( id, c ) );
return c;
}
std::optional<char> get_inactive( const tripoint_abs_omt &omp ) {
std::set<tripoint_abs_omt> grid = overmap_buffer.electric_grid_at( omp );
if( grid.size() <= 1 ) {
return std::nullopt;
}
std::vector<tripoint_abs_omt> sorted( grid.begin(), grid.end() );
std::sort( sorted.begin(), sorted.end() );
std::size_t id = cata::range_hash{}( sorted );
auto it = list_inactive.find( id );
if( it != list_inactive.end() ) {
return it->second.second;
}
// There may be a lot of grids visible at the same time.
// We have no choice but to allow repeating symbols,
// but also have to make sure neighbouring grids don't receive same ones.
auto ch = pick_char( [omp, this]( char c ) {
for( const auto &it : list_inactive ) {
if( it.second.second != c ) {
continue;
}
for( const tripoint_abs_omt &p : it.second.first ) {
tripoint_rel_omt delta = p - omp;
if( abs( delta.x() ) < 5 && abs( delta.y() ) < 5 && abs( delta.z() ) < 5 ) {
return false;
}
}
}
return true;
} );
char c = ch.has_value() ? *ch : '?';
list_inactive.insert( std::make_pair( id, std::make_pair( sorted, c ) ) );
return c;
}
private:
// Fn(char) -> bool
template<typename Fn>
std::optional<char> pick_char( Fn filter_func ) {
static std::string candidates( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" );
for( char c : candidates ) {
if( filter_func( c ) ) {
return c;
}
}
return std::nullopt;
}
std::unordered_map<std::uintptr_t, char> list_active;
std::unordered_map<std::size_t, std::pair<std::vector<tripoint_abs_omt>, char>> list_inactive;
};
static std::string fmt_omt_coords( const tripoint_abs_omt &coord )
{
if( get_option<std::string>( "OVERMAP_COORDINATE_FORMAT" ) == "subdivided" ) {
point_abs_om abs_coord;
tripoint_om_omt rel_coord;
std::tie( abs_coord, rel_coord ) = project_remain<coords::om>( coord );
return string_format( "%d'%d, %d'%d", abs_coord.x(), rel_coord.x(), abs_coord.y(), rel_coord.y() );
} else {
return string_format( "%d, %d", coord.x(), coord.y() );
}
}
static void create_note( const tripoint_abs_omt &curs );
// {note symbol, note color, offset to text}
std::tuple<char, nc_color, size_t> get_note_display_info( const std::string ¬e )
{
std::tuple<char, nc_color, size_t> result {'N', c_yellow, 0};
bool set_color = false;
bool set_symbol = false;
size_t pos = 0;
for( int i = 0; i < 2; ++i ) {
// find the first non-whitespace non-delimiter
pos = note.find_first_not_of( " :;", pos, 3 );
if( pos == std::string::npos ) {
return result;
}
// find the first following delimiter
const auto end = note.find_first_of( " :;", pos, 3 );
if( end == std::string::npos ) {
return result;
}
// set color or symbol
if( !set_symbol && note[end] == ':' ) {
std::get<0>( result ) = note[end - 1];
std::get<2>( result ) = end + 1;
set_symbol = true;
} else if( !set_color && note[end] == ';' ) {
std::get<1>( result ) = get_note_color( note.substr( pos, end - pos ) );
std::get<2>( result ) = end + 1;
set_color = true;
}
pos = end + 1;
}
return result;
}
static std::array<std::pair<nc_color, std::string>, npm_width *npm_height> get_overmap_neighbors(
const tripoint_abs_omt ¤t )
{
const bool has_debug_vision = get_player_character().has_trait( trait_DEBUG_NIGHTVISION );
std::array<std::pair<nc_color, std::string>, npm_width *npm_height> map_around;
int index = 0;
const point shift( npm_width / 2, npm_height / 2 );
for( const tripoint_abs_omt &dest :
tripoint_range<tripoint_abs_omt>( current - shift, current + shift ) ) {
nc_color ter_color = c_black;
std::string ter_sym = " ";
const bool see = has_debug_vision || overmap_buffer.seen( dest );
if( see ) {
// Only load terrain if we can actually see it
oter_id cur_ter = overmap_buffer.ter( dest );
ter_color = cur_ter->get_color();
ter_sym = cur_ter->get_symbol();
} else {
ter_color = c_dark_gray;
ter_sym = "#";
}
map_around[index++] = std::make_pair( ter_color, ter_sym );
}
return map_around;
}
static void update_note_preview( const std::string ¬e,
const std::array<std::pair<nc_color, std::string>, npm_width *npm_height> &map_around,
const std::tuple<catacurses::window *, catacurses::window *, catacurses::window *>
&preview_windows )
{
auto om_symbol = get_note_display_info( note );
const nc_color note_color = std::get<1>( om_symbol );
const char symbol = std::get<0>( om_symbol );
const std::string note_text = note.substr( std::get<2>( om_symbol ), std::string::npos );
auto w_preview = std::get<0>( preview_windows );
auto w_preview_title = std::get<1>( preview_windows );
auto w_preview_map = std::get<2>( preview_windows );
draw_border( *w_preview );
// NOLINTNEXTLINE(cata-use-named-point-constants)
mvwprintz( *w_preview, point( 1, 1 ), c_white, _( "Note preview" ) );
wnoutrefresh( *w_preview );
werase( *w_preview_title );
nc_color default_color = c_unset;
print_colored_text( *w_preview_title, point_zero, default_color, note_color, note_text,
report_color_error::no );
int note_text_width = utf8_width( note_text );
mvwputch( *w_preview_title, point( note_text_width, 0 ), c_white, LINE_XOXO );
for( int i = 0; i < note_text_width; i++ ) {
mvwputch( *w_preview_title, point( i, 1 ), c_white, LINE_OXOX );
}
mvwputch( *w_preview_title, point( note_text_width, 1 ), c_white, LINE_XOOX );
wnoutrefresh( *w_preview_title );
const int npm_offset_x = 1;
const int npm_offset_y = 1;
werase( *w_preview_map );
draw_border( *w_preview_map, c_yellow );
for( int i = 0; i < npm_height; i++ ) {
for( int j = 0; j < npm_width; j++ ) {
const auto &ter = map_around[i * npm_width + j];
mvwputch( *w_preview_map, point( j + npm_offset_x, i + npm_offset_y ), ter.first, ter.second );
}
}
mvwputch( *w_preview_map, point( npm_width / 2 + npm_offset_x, npm_height / 2 + npm_offset_y ),
note_color, symbol );
wnoutrefresh( *w_preview_map );
}
weather_type_id get_weather_at_point( const point_abs_omt &pos )
{
// Weather calculation is a bit expensive, so it's cached here.
static std::map<point_abs_omt, weather_type_id> weather_cache;
static time_point last_weather_display = calendar::before_time_starts;
if( last_weather_display != calendar::turn ) {
last_weather_display = calendar::turn;
weather_cache.clear();
}
auto iter = weather_cache.find( pos );
if( iter == weather_cache.end() ) {
// TODO: fix point types
tripoint_abs_omt pos_z( pos, OVERMAP_HEIGHT );
const tripoint abs_ms_pos = project_to<coords::ms>( pos_z ).raw();
const auto &wgen = overmap_buffer.get_settings( pos_z ).weather;
auto weather = wgen.get_weather_conditions( abs_ms_pos, calendar::turn, g->get_seed() );
iter = weather_cache.insert( std::make_pair( pos, weather ) ).first;
}
return iter->second;
}
static bool get_scent_glyph( const tripoint_abs_omt &pos, nc_color &ter_color,
std::string &ter_sym )
{
auto possible_scent = overmap_buffer.scent_at( pos );
if( possible_scent.creation_time != calendar::before_time_starts ) {
color_manager &color_list = get_all_colors();
int i = 0;
time_duration scent_age = calendar::turn - possible_scent.creation_time;
while( i < num_colors && scent_age > 0_turns ) {
i++;
scent_age /= 10;
}
ter_color = color_list.get( static_cast<color_id>( i ) );
int scent_strength = possible_scent.initial_strength;
char c = '0';
while( c <= '9' && scent_strength > 0 ) {
c++;
scent_strength /= 10;
}
ter_sym = std::string( 1, c );
return true;
}
// but it makes no scents!
return false;
}
static void draw_city_labels( const catacurses::window &w, const tripoint_abs_omt ¢er )
{
const int win_x_max = getmaxx( w );
const int win_y_max = getmaxy( w );
const int sm_radius = std::max( win_x_max, win_y_max );
const point screen_center_pos( win_x_max / 2, win_y_max / 2 );
for( const auto &element : overmap_buffer.get_cities_near(
project_to<coords::sm>( center ), sm_radius ) ) {
const point_abs_omt city_pos =
project_to<coords::omt>( element.abs_sm_pos.xy() );
const point_rel_omt screen_pos( city_pos - center.xy() + screen_center_pos );
const int text_width = utf8_width( element.city->name, true );
const int text_x_min = screen_pos.x() - text_width / 2;
const int text_x_max = text_x_min + text_width;
const int text_y = screen_pos.y();
if( text_x_min < 0 ||
text_x_max > win_x_max ||
text_y < 0 ||
text_y > win_y_max ) {
continue; // outside of the window bounds.
}
if( screen_center_pos.x >= ( text_x_min - 1 ) &&
screen_center_pos.x <= ( text_x_max ) &&
screen_center_pos.y >= ( text_y - 1 ) &&
screen_center_pos.y <= ( text_y + 1 ) ) {
continue; // right under the cursor.
}
if( !overmap_buffer.seen( tripoint_abs_omt( city_pos, center.z() ) ) ) {
continue; // haven't seen it.
}
mvwprintz( w, point( text_x_min, text_y ), i_yellow, element.city->name );
}
}
static void draw_camp_labels( const catacurses::window &w, const tripoint_abs_omt ¢er )
{
const int win_x_max = getmaxx( w );
const int win_y_max = getmaxy( w );
const int sm_radius = std::max( win_x_max, win_y_max );
const point screen_center_pos( win_x_max / 2, win_y_max / 2 );
for( const auto &element : overmap_buffer.get_camps_near(
project_to<coords::sm>( center ), sm_radius ) ) {
const point_abs_omt camp_pos( element.camp->camp_omt_pos().xy() );
const point screen_pos( ( camp_pos - center.xy() ).raw() + screen_center_pos );
const int text_width = utf8_width( element.camp->name, true );
const int text_x_min = screen_pos.x - text_width / 2;
const int text_x_max = text_x_min + text_width;
const int text_y = screen_pos.y;
const std::string camp_name = element.camp->name;
if( text_x_min < 0 ||
text_x_max > win_x_max ||
text_y < 0 ||
text_y > win_y_max ) {
continue; // outside of the window bounds.
}
if( screen_center_pos.x >= ( text_x_min - 1 ) &&
screen_center_pos.x <= ( text_x_max ) &&
screen_center_pos.y >= ( text_y - 1 ) &&
screen_center_pos.y <= ( text_y + 1 ) ) {
continue; // right under the cursor.
}
if( !overmap_buffer.seen( tripoint_abs_omt( camp_pos, center.z() ) ) ) {
continue; // haven't seen it.
}
mvwprintz( w, point( text_x_min, text_y ), i_white, camp_name );
}
}
static bool query_confirm_delete( bool &ask_when_deleting )
{
if( !ask_when_deleting ) {
return true;
}
uilist qry;
qry.text = _( "Really delete note?" );
qry.addentry( 1, true, 'Y', _( "Yes." ) );
qry.addentry( 2, true, 'I', _( "Yes, and don't ask again." ) );
qry.addentry( 3, true, 'N', _( "No." ) );
qry.query();
switch( qry.ret ) {
case 1:
return true;
case 2:
ask_when_deleting = false;
return true;
default:
return false;
}
}
struct note_cached {
tripoint_abs_omt p;
nc_color col;
std::string symbol;
std::string text;
std::string text_nocolor;
int dist_from_pl;
};
class map_notes_callback : public uilist_callback
{
private:
std::vector<note_cached> const *_notes;
int _selected = 0;
catacurses::window w_preview;
catacurses::window w_preview_title;
catacurses::window w_preview_map;
std::tuple<catacurses::window *, catacurses::window *, catacurses::window *> preview_windows;
ui_adaptor ui;
tripoint_abs_omt note_location() {
return ( *_notes )[_selected].p;
}
public:
bool ask_when_deleting = true;
map_notes_callback( std::vector<note_cached> const *notes ) : _notes( notes ) {
ui.on_screen_resize( [this]( ui_adaptor & ui ) {
w_preview = catacurses::newwin( npm_height + 2, max_note_display_length - npm_width - 1,
point( npm_width + 2, 2 ) );
w_preview_title = catacurses::newwin( 2, max_note_display_length + 1, point_zero );
w_preview_map = catacurses::newwin( npm_height + 2, npm_width + 2, point( 0, 2 ) );
preview_windows = std::make_tuple( &w_preview, &w_preview_title, &w_preview_map );
ui.position( point_zero, point( max_note_display_length + 1, npm_height + 4 ) );
} );
ui.mark_resize();
ui.on_redraw( [this]( const ui_adaptor & ) {
if( _selected >= 0 && static_cast<size_t>( _selected ) < _notes->size() ) {
const tripoint_abs_omt note_pos = note_location();
const auto map_around = get_overmap_neighbors( note_pos );
update_note_preview( overmap_buffer.note( note_pos ), map_around, preview_windows );
} else {
update_note_preview( {}, {}, preview_windows );
}
} );
}
bool key( const input_context &ctxt, const input_event &event, int, uilist *menu ) override {
const std::string &action = ctxt.input_to_action( event );
if( action == "CHANGE_SORT" ) {
menu->ret = UILIST_CHANGE_SORT;
return true;
}
if( action == "CLEAR_FILTER" ) {
menu->clear_filter();
return true;
}
_selected = menu->selected;
if( _selected >= 0 && _selected < static_cast<int>( _notes->size() ) ) {
if( action == "DELETE_NOTE" ) {
if( overmap_buffer.has_note( note_location() ) &&
query_confirm_delete( ask_when_deleting ) ) {
overmap_buffer.delete_note( note_location() );
menu->ret = UILIST_MAP_NOTE_DELETED;
}
return true;
}
if( action == "EDIT_NOTE" ) {
create_note( note_location() );
menu->ret = UILIST_MAP_NOTE_EDITED;
return true;
}
if( action == "MARK_DANGER" ) {
// NOLINTNEXTLINE(cata-text-style): No need for two whitespaces
if( query_yn( _( "Mark area as dangerous ( to avoid on automove paths? )" ) ) ) {
const int max_amount = 20;
// NOLINTNEXTLINE(cata-text-style): No need for two whitespaces
const std::string popupmsg = _( "Danger radius in overmap squares? ( 0-20 )" );
int amount = string_input_popup()
.title( popupmsg )
.width( 20 )
.text( std::to_string( 0 ) )
.only_digits( true )
.query_int();
if( amount > -1 && amount <= max_amount ) {
overmap_buffer.mark_note_dangerous( note_location(), amount, true );
menu->ret = UILIST_MAP_NOTE_EDITED;
return true;
}
} else if( overmap_buffer.is_marked_dangerous( note_location() ) &&
query_yn( _( "Remove dangerous mark?" ) ) ) {
overmap_buffer.mark_note_dangerous( note_location(), 0, false );
menu->ret = UILIST_MAP_NOTE_EDITED;
return true;
}
}
}
return false;
}
void select( uilist *menu ) override {
_selected = menu->selected;
ui.invalidate_ui();
}
};
enum class sort_mode_t : int {
name,
distance,
symbol,
num,
};
static bool sortfunc_dist( const note_cached &a, const note_cached &b )
{
if( a.dist_from_pl == b.dist_from_pl ) {
// Compare points to get stable order
return a.p < b.p;
} else {
return a.dist_from_pl < b.dist_from_pl;
}
}
static bool sortfunc_name( const note_cached &a, const note_cached &b )
{
if( a.text_nocolor == b.text_nocolor ) {
return sortfunc_dist( a, b );
} else {
return localized_compare( a.text_nocolor, b.text_nocolor );
}
}
static bool sortfunc_symbol( const note_cached &a, const note_cached &b )
{
if( a.symbol == b.symbol ) {
return sortfunc_name( a, b );
} else {
// Not using lexicographic comparator here because it's case-insensitive
// NOLINTNEXTLINE(cata-use-localized-sorting)
return a.symbol < b.symbol;
}
}
static tripoint_abs_omt show_notes_manager( const tripoint_abs_omt &origin )
{
tripoint_abs_omt result = tripoint_abs_omt( tripoint_min );
bool ask_when_deleting = true;
uilist nmenu;
std::string filter;
tripoint_abs_omt selected = origin;
sort_mode_t sort_mode = sort_mode_t::name;
const tripoint_abs_omt p_player = g->u.global_omt_location();
bool quit = false;
while( !quit ) {
nmenu.init();
nmenu.color_error( false );
nmenu.desc_enabled = true;
nmenu.input_category = "OVERMAP_NOTES";
nmenu.additional_actions.emplace_back( "DELETE_NOTE", translation() );
nmenu.additional_actions.emplace_back( "EDIT_NOTE", translation() );
nmenu.additional_actions.emplace_back( "CHANGE_SORT", translation() );
nmenu.additional_actions.emplace_back( "CLEAR_FILTER", translation() );
nmenu.additional_actions.emplace_back( "MARK_DANGER", translation() );
const input_context ctxt( nmenu.input_category );
nmenu.text = string_format(
_( "<%s> - center on note, <%s> - edit note, <%s> - mark as dangerous, <%s> - delete note, <%s> - close window" ),
colorize( "RETURN", c_yellow ),
colorize( ctxt.key_bound_to( "EDIT_NOTE" ), c_yellow ),
colorize( ctxt.key_bound_to( "MARK_DANGER" ), c_red ),
colorize( ctxt.key_bound_to( "DELETE_NOTE" ), c_yellow ),
colorize( "ESCAPE", c_yellow )
);
std::vector<note_cached> notes;
for( int zlev = -OVERMAP_DEPTH; zlev <= OVERMAP_HEIGHT; zlev++ ) {
overmapbuffer::t_notes_vector notes_raw = overmap_buffer.get_all_notes( zlev );
notes.reserve( notes.size() + notes_raw.size() );
for( const auto &it : notes_raw ) {
auto om_symbol = get_note_display_info( it.second );
note_cached n;
n.p = tripoint_abs_omt( it.first, zlev );
n.col = std::get<1>( om_symbol );
n.symbol = std::string( 1, std::get<0>( om_symbol ) );
n.text = it.second.substr( std::get<2>( om_symbol ), std::string::npos );
n.text_nocolor = remove_color_tags( n.text );
n.dist_from_pl = rl_dist( p_player, n.p );
notes.push_back( std::move( n ) );
}
}
const char *sort_str;
switch( sort_mode ) {
case sort_mode_t::name:
sort_str = pgettext( "Sorted by:", "name" );
std::sort( notes.begin(), notes.end(), sortfunc_name );
break;
case sort_mode_t::distance:
sort_str = pgettext( "Sorted by:", "distance" );
std::sort( notes.begin(), notes.end(), sortfunc_dist );
break;
case sort_mode_t::symbol:
sort_str = pgettext( "Sorted by:", "symbol" );
std::sort( notes.begin(), notes.end(), sortfunc_symbol );
break;
default:
debugmsg( "Unimplemented" );
break;
}
//~ %1$d is total number of notes, %2$s is hotkey for sorting, %3$s is sort criterion
nmenu.title = string_format( _( "Map notes (%1$d) [%2$s] Sorted by: %3$s" ), notes.size(),
ctxt.key_bound_to( "CHANGE_SORT" ), sort_str );
int entry_to_select = -1;
for( size_t i = 0; i < notes.size(); i++ ) {
const note_cached ¬e = notes[i];
if( note.p == selected ) {
entry_to_select = i;
}
const std::string direction_str = direction_name_short( direction_from( p_player, note.p ) );
const std::string location_desc = overmap_buffer.get_description_at(
project_to<coords::sm>( note.p ) );
//~ "Dangerous" indicator for overmap note in note manager.
//~ Must occupy exactly 2 columns, and not resemble a number or a digit.
//~ English uses D for Danger, but it's acceptable to leave it untranslated
//~ or use some special symbol instead (e.g. exclamation mark)
//~ if you're having trouble making it look nice in your language.
const char *danger_abbr = pgettext( "danger indicator", " D" );
const bool is_dangerous = overmap_buffer.is_marked_dangerous( note.p );
std::optional<int> this_dr = overmap_buffer.has_note_with_danger_radius( note.p );
std::string dr_short;
if( this_dr ) {
if( *this_dr == 0 ) {
// Dangerous area
dr_short = colorize( danger_abbr, c_red );
} else {
// Dangerous area with danger radius
dr_short = string_format( "<color_red>%2d</color>", *this_dr );
}
} else {
if( is_dangerous ) {
// Not dangerous by itself, but falls under danger radius
// of some other note
dr_short = colorize( danger_abbr, c_yellow );
} else {
// Safe
dr_short = " ";
}
}
nmenu.addentry_desc( string_format(
"[%s] %s", colorize( note.symbol, note.col ), note.text ),
string_format(
_( "<color_red>LEVEL %i, %s</color>: %s (Distance: <color_white>%d %s</color>) <color_red>%s</color>" ),
note.p.z(),
fmt_omt_coords( note.p ), location_desc, note.dist_from_pl,
trim_whitespaces( direction_str ), is_dangerous ? _( "DANGEROUS AREA!" ) : "" ) );
nmenu.entries[i].ctxt = string_format(
"%s<color_white>% 4d %s</color>", dr_short, note.dist_from_pl, direction_str
);
}
nmenu.set_filter( filter ); // Restore filter
nmenu.set_selected( entry_to_select ); // Restore selection
map_notes_callback cb( ¬es );
cb.ask_when_deleting = ask_when_deleting;
nmenu.callback = &cb;
nmenu.query();
if( nmenu.ret == UILIST_CHANGE_SORT ) {
sort_mode = static_cast<sort_mode_t>(
( static_cast<int>( sort_mode ) + 1 ) % static_cast<int>( sort_mode_t::num )
);
}
if( nmenu.ret == UILIST_MAP_NOTE_DELETED || nmenu.ret == UILIST_MAP_NOTE_EDITED ||
nmenu.ret == UILIST_CHANGE_SORT ) {
// Save state
ask_when_deleting = cb.ask_when_deleting;
filter = nmenu.get_filter();
if( nmenu.ret == UILIST_MAP_NOTE_EDITED || nmenu.ret == UILIST_CHANGE_SORT ) {
// Reselect same note
assert( nmenu.selected >= 0 && nmenu.selected < static_cast<int>( notes.size() ) );
selected = notes[nmenu.selected].p;
} else {
assert( nmenu.ret == UILIST_MAP_NOTE_DELETED );
// Select next visible note (if one exists) or the last visible
// note if removed one was the last.
bool take_next = false;
selected = tripoint_abs_omt( tripoint_min );
for( const int i : nmenu.get_filtered() ) {
if( nmenu.selected == i ) {
take_next = true;
continue;
}
selected = notes[i].p;
if( take_next ) {
break;
}
}
}
} else if( nmenu.ret >= 0 && nmenu.ret < static_cast<int>( notes.size() ) ) {
result = notes[nmenu.ret].p;
quit = true;
} else {
quit = true;
}
}
return result;
}
static void draw_ascii( const catacurses::window &w,
const tripoint_abs_omt ¢er,
const tripoint_abs_omt &/*orig*/,
bool blink,
bool show_explored,
bool /*fast_scroll*/,
input_context */*inp_ctxt*/,
const draw_data_t &data,
grids_draw_data &grids_data )
{
const int om_map_width = OVERMAP_WINDOW_WIDTH;
const int om_map_height = OVERMAP_WINDOW_HEIGHT;
const int om_half_width = om_map_width / 2;
const int om_half_height = om_map_height / 2;
const bool viewing_weather =
( ( uistate.overmap_debug_weather || uistate.overmap_visible_weather ) && center.z() >= 0 );
avatar &player_character = get_avatar();
// Target of current mission
const tripoint_abs_omt target = player_character.get_active_mission_target();
const bool has_target = target != overmap::invalid_tripoint;
oter_id ccur_ter = oter_str_id::NULL_ID();
// Debug vision allows seeing everything
const bool has_debug_vision = player_character.has_trait( trait_DEBUG_NIGHTVISION );
// sight_points is hoisted for speed reasons.
const int sight_points = !has_debug_vision ?
player_character.overmap_sight_range( g->light_level( player_character.posz() ) ) :
100;
// Whether showing hordes is currently enabled
const bool showhordes = uistate.overmap_show_hordes;
const oter_id forest = oter_str_id( "forest" ).id();
std::string sZoneName;
tripoint_abs_omt tripointZone( -1, -1, -1 );
const auto &zones = zone_manager::get_manager();
if( data.iZoneIndex != -1 ) {
const auto &zone = zones.get_zones()[data.iZoneIndex].get();
sZoneName = zone.get_name();
// TODO: fix point types
tripointZone = project_to<coords::omt>(
tripoint_abs_ms( zone.get_center_point() ) );
}
// If we're debugging monster groups, find the monster group we've selected
const mongroup *mgroup = nullptr;
std::vector<mongroup *> mgroups;
if( uistate.overmap_debug_mongroup ) {
mgroups = overmap_buffer.monsters_at( center );
for( const auto &mgp : mgroups ) {
mgroup = mgp;
if( mgp->horde ) {
break;
}
}
}
// A small LRU cache: most oter_id's occur in clumps like forests of swamps.
// This cache helps avoid much more costly lookups in the full hashmap.
constexpr size_t cache_size = 8; // used below to calculate the next index
std::array<std::pair<oter_id, oter_t const *>, cache_size> cache{ {} };
size_t cache_next = 0;
const auto set_color_and_symbol = [&]( const oter_id & cur_ter, const tripoint_abs_omt & omp,
std::string & ter_sym, nc_color & ter_color ) {
// First see if we have the oter_t cached
oter_t const *info = nullptr;
for( const auto &c : cache ) {
if( c.first == cur_ter ) {
info = c.second;
break;
}
}
// Nope, look in the hash map next
if( !info ) {
info = &cur_ter.obj();
cache[cache_next] = std::make_pair( cur_ter, info );
cache_next = ( cache_next + 1 ) % cache_size;
}
// Ok, we found something
if( info ) {
const bool explored = show_explored && overmap_buffer.is_explored( omp );
ter_color = explored ? c_dark_gray : info->get_color( uistate.overmap_show_land_use_codes );
ter_sym = info->get_symbol( uistate.overmap_show_land_use_codes );
}
};
const tripoint_abs_omt corner = center - point( om_half_width, om_half_height );
// For use with place_special: cache the color and symbol of each submap
// and record the bounds to optimize lookups below
std::unordered_map<point_rel_omt, std::pair<std::string, nc_color>> special_cache;
point_rel_omt s_begin;
point_rel_omt s_end;
if( blink && uistate.place_special ) {
for( const auto &s_ter : uistate.place_special->terrains ) {
if( s_ter.p.z == 0 ) {
// TODO: fix point types
const point_rel_omt rp( om_direction::rotate( s_ter.p.xy(), uistate.omedit_rotation ) );
const oter_id oter = s_ter.terrain->get_rotated( uistate.omedit_rotation );
special_cache.insert( std::make_pair(
rp, std::make_pair( oter->get_symbol(), oter->get_color() ) ) );
s_begin.x() = std::min( s_begin.x(), rp.x() );
s_begin.y() = std::min( s_begin.y(), rp.y() );
s_end.x() = std::max( s_end.x(), rp.x() );
s_end.y() = std::max( s_end.y(), rp.y() );
}
}
}
// Cache NPCs since time to draw them is linear (per seen tile) with their count
struct npc_coloring {
nc_color color;
size_t count;
};
std::unordered_set<tripoint_abs_omt> npc_path_route;
std::unordered_map<point_abs_omt, int> player_path_route;
std::unordered_map<tripoint_abs_omt, npc_coloring> npc_color;
auto npcs_near_player = overmap_buffer.get_npcs_near_player( sight_points );
if( blink ) {
// get seen NPCs
for( const auto &np : npcs_near_player ) {
if( np->posz() != center.z() ) {
continue;
}
const tripoint_abs_omt pos = np->global_omt_location();
if( has_debug_vision || overmap_buffer.seen( pos ) ) {
auto iter = npc_color.find( pos );
nc_color np_color = np->basic_symbol_color();
if( iter == npc_color.end() ) {
npc_color[pos] = { np_color, 1 };
} else {
iter->second.count++;
// Randomly change to new NPC's color
if( iter->second.color != np_color && one_in( iter->second.count ) ) {
iter->second.color = np_color;
}
}
}
}
std::vector<npc *> followers;
// get friendly followers
for( auto &elem : g->get_follower_list() ) {
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
if( !npc_to_get ) {
continue;
}
npc *npc_to_add = npc_to_get.get();
followers.push_back( npc_to_add );
}
// get all traveling NPCs for the debug menu to show pathfinding routes.
if( g->debug_pathfinding ) {
for( auto &elem : overmap_buffer.get_npcs_near_player( 200 ) ) {
if( !elem ) {
continue;
}
npc *npc_to_add = elem.get();
if( npc_to_add->mission == NPC_MISSION_TRAVELLING && !npc_to_add->omt_path.empty() ) {
for( auto &elem : npc_to_add->omt_path ) {
npc_path_route.insert( elem );
}
}
}
}
for( auto &elem : player_character.omt_path ) {
player_path_route[ elem.xy() ] = elem.z();
}
for( const auto &np : followers ) {
if( np->posz() != center.z() ) {
continue;
}
const tripoint_abs_omt pos = np->global_omt_location();
auto iter = npc_color.find( pos );
nc_color np_color = np->basic_symbol_color();
if( iter == npc_color.end() ) {
npc_color[pos] = { np_color, 1 };
} else {
iter->second.count++;
// Randomly change to new NPC's color
if( iter->second.color != np_color && one_in( iter->second.count ) ) {
iter->second.color = np_color;
}
}
}
}
tripoint_abs_omt pl_pos = get_player_character().global_omt_location();
for( int i = 0; i < om_map_width; ++i ) {
for( int j = 0; j < om_map_height; ++j ) {
const tripoint_abs_omt omp = corner + point( i, j );
const tripoint_abs_omt omp_sky( omp.xy(), OVERMAP_HEIGHT );
oter_id cur_ter = oter_str_id::NULL_ID();
nc_color ter_color = c_black;
std::string ter_sym = " ";
const bool see = has_debug_vision || overmap_buffer.seen( omp );
if( see ) {
// Only load terrain if we can actually see it
cur_ter = overmap_buffer.ter( omp );
}
// Check if location is within player line-of-sight
const bool los = see && player_character.overmap_los( omp, sight_points );
const bool los_sky = player_character.overmap_los( omp_sky, sight_points * 2 );
const bool is_npc_path = npc_path_route.find( omp ) != npc_path_route.end();
const bool is_player_path = player_path_route.find( omp.xy() ) != player_path_route.end();
const int player_path_z = is_player_path ? player_path_route[ omp.xy() ] : 0;
if( blink && omp == pl_pos ) {
// Display player pos, should always be visible
ter_color = player_character.symbol_color();
ter_sym = "@";
} else if( viewing_weather && ( uistate.overmap_debug_weather || los_sky ) ) {
const weather_type_id type = get_weather_at_point( omp_sky.xy() );
ter_color = type->map_color;