forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_inv.cpp
1837 lines (1709 loc) · 65.4 KB
/
advanced_inv.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 "game.h"
#include "output.h"
#include "map.h"
#include "catacharset.h"
#include "translations.h"
#include "uistate.h"
#include "helper.h"
#include "auto_pickup.h"
#include "messages.h"
#include "player_activity.h"
#include "advanced_inv.h"
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <cassert>
advanced_inventory::advanced_inventory()
: head_height( 5 )
, min_w_height( 10 )
, min_w_width( FULL_SCREEN_WIDTH )
, max_w_width( 120 )
, inCategoryMode( false )
, recalc( true )
, redraw( true )
, src( left )
, dest( right )
, filter_edit( false )
// panes don't need initialization, they are recalculated immediately
, squares { {
{ AIM_INVENTORY, 2, 25, 0, 0, _( "Inventory" ), _( "IN" ) },
{ AIM_SOUTHWEST, 3, 30, -1, 1, _( "South West" ), _( "SW" ) },
{ AIM_SOUTH, 3, 33, 0, 1, _( "South" ), _( "S" ) },
{ AIM_SOUTHEAST, 3, 36, 1, 1, _( "South East" ), _( "SE" ) },
{ AIM_WEST, 2, 30, -1, 0, _( "West" ), _( "W" ) },
{ AIM_CENTER, 2, 33, 0, 0, _( "Directly below you" ), _( "DN" ) },
{ AIM_EAST, 2, 36, 1, 0, _( "East" ), _( "E" ) },
{ AIM_NORTHWEST, 1, 30, -1, -1, _( "North West" ), _( "NW" ) },
{ AIM_NORTH, 1, 33, 0, -1, _( "North" ), _( "N" ) },
{ AIM_NORTHEAST, 1, 36, 1, -1, _( "North East" ), _( "NE" ) },
{ AIM_ALL, 3, 25, 0, 0, _( "Surrounding area" ), _( "AL" ) },
{ AIM_DRAGED, 1, 25, 0, 0, _( "Grabbed Vehicle" ), _( "GR" ) },
{ AIM_CONTAINER, 1, 22, 0, 0, _( "Container" ), _( "CN" ) }
}
}
, head( nullptr )
, left_window( nullptr )
, right_window( nullptr )
{
}
advanced_inventory::~advanced_inventory()
{
uistate.adv_inv_last_coords.x = g->u.posx;
uistate.adv_inv_last_coords.y = g->u.posy;
uistate.adv_inv_leftarea = panes[left].area;
uistate.adv_inv_rightarea = panes[right].area;
uistate.adv_inv_leftindex = panes[left].index;
uistate.adv_inv_rightindex = panes[right].index;
uistate.adv_inv_src = src;
uistate.adv_inv_dest = dest;
uistate.adv_inv_leftfilter = panes[left].filter;
uistate.adv_inv_rightfilter = panes[right].filter;
// Only refresh if we exited manually, otherwise we're going to be right back
if( exit ) {
werase( head );
werase( left_window );
werase( right_window );
}
delwin( head );
delwin( left_window );
delwin( right_window );
if( exit ) {
g->refresh_all();
}
}
std::string advanced_inventory::get_sortname( advanced_inv_sortby sortby )
{
switch( sortby ) {
case SORTBY_NONE:
return _( "none" );
case SORTBY_NAME:
return _( "name" );
case SORTBY_WEIGHT:
return _( "weight" );
case SORTBY_VOLUME:
return _( "volume" );
case SORTBY_CHARGES:
return _( "charges" );
case SORTBY_CATEGORY:
return _( "category" );
case SORTBY_DAMAGE:
return _( "damage" );
}
return "!bug!";
}
bool advanced_inventory::get_square( const std::string action, aim_location &ret )
{
if( action == "ITEMS_INVENTORY" ) {
ret = AIM_INVENTORY;
} else if( action == "ITEMS_NW" ) {
ret = AIM_NORTHWEST;
} else if( action == "ITEMS_N" ) {
ret = AIM_NORTH;
} else if( action == "ITEMS_NE" ) {
ret = AIM_NORTHEAST;
} else if( action == "ITEMS_W" ) {
ret = AIM_WEST;
} else if( action == "ITEMS_CE" ) {
ret = AIM_CENTER;
} else if( action == "ITEMS_E" ) {
ret = AIM_EAST;
} else if( action == "ITEMS_SW" ) {
ret = AIM_SOUTHWEST;
} else if( action == "ITEMS_S" ) {
ret = AIM_SOUTH;
} else if( action == "ITEMS_SE" ) {
ret = AIM_SOUTHEAST;
} else if( action == "ITEMS_AROUND" ) {
ret = AIM_ALL;
} else if( action == "ITEMS_DRAGGED_CONTAINER" ) {
ret = AIM_DRAGED;
} else if( action == "ITEMS_CONTAINER" ) {
ret = AIM_CONTAINER;
} else {
return false;
}
return true;
}
void advanced_inventory::print_items( advanced_inventory_pane &pane, bool active )
{
const auto &items = pane.items;
WINDOW *window = pane.window;
const auto index = pane.index;
const int page = index / itemsPerPage;
bool compact = ( TERMX <= 100 );
int columns = getmaxx( window );
std::string spaces( columns - 4, ' ' );
nc_color norm = active ? c_white : c_dkgray;
//print inventory's current and total weight + volume
if( pane.area == AIM_INVENTORY ) {
//right align
int hrightcol = columns -
helper::to_string_int( g->u.convert_weight( g->u.weight_carried() ) ).length() - 3 - //"xxx.y/"
helper::to_string_int( g->u.convert_weight( g->u.weight_capacity() ) ).length() - 3 - //"xxx.y_"
helper::to_string_int( g->u.volume_carried() ).length() - 1 - //"xxx/"
helper::to_string_int( g->u.volume_capacity() - 2 ).length() - 1; //"xxx|"
nc_color color = c_ltgreen;//red color if overload
if( g->u.weight_carried() > g->u.weight_capacity() ) {
color = c_red;
}
mvwprintz( window, 4, hrightcol, color, "%.1f", g->u.convert_weight( g->u.weight_carried() ) );
wprintz( window, c_ltgray, "/%.1f ", g->u.convert_weight( g->u.weight_capacity() ) );
if( g->u.volume_carried() > g->u.volume_capacity() - 2 ) {
color = c_red;
} else {
color = c_ltgreen;
}
wprintz( window, color, "%d", g->u.volume_carried() );
wprintz( window, c_ltgray, "/%d ", g->u.volume_capacity() - 2 );
} else { //print square's current and total weight + volume
std::string head;
if( pane.area == AIM_ALL ) {
head = string_format( "%3.1f %3d",
g->u.convert_weight( squares[pane.area].weight ),
squares[pane.area].volume );
} else {
int maxvolume;
if ( pane.area == AIM_CONTAINER && squares[pane.area].get_container() != nullptr ) {
maxvolume = squares[pane.area].get_container()->type->container->contains;
} else {
if( squares[pane.area].veh != NULL && squares[pane.area].vstor >= 0 ) {
maxvolume = squares[pane.area].veh->max_volume( squares[pane.area].vstor );
} else {
maxvolume = g->m.max_volume( squares[pane.area].x, squares[pane.area].y );
}
}
head = string_format( "%3.1f %3d/%3d",
g->u.convert_weight( squares[pane.area].weight ),
squares[pane.area].volume, maxvolume );
}
mvwprintz( window, 4, columns - 1 - head.length(), norm, "%s", head.c_str() );
}
//print header row and determine max item name length
const int lastcol = columns - 2; // Last printable column
const size_t name_startpos = ( compact ? 1 : 4 );
const size_t src_startpos = lastcol - 17;
const size_t amt_startpos = lastcol - 14;
const size_t weight_startpos = lastcol - 9;
const size_t vol_startpos = lastcol - 3;
int max_name_length = amt_startpos - name_startpos - 1; // Default name length
//~ Items list header. Table fields length without spaces: amt - 4, weight - 5, vol - 4.
const int table_hdr_len1 = utf8_width( _( "amt weight vol" ) ); // Header length type 1
//~ Items list header. Table fields length without spaces: src - 2, amt - 4, weight - 5, vol - 4.
const int table_hdr_len2 = utf8_width( _( "src amt weight vol" ) ); // Header length type 2
mvwprintz( window, 5, ( compact ? 1 : 4 ), c_ltgray, _( "Name (charges)" ) );
if( pane.area == AIM_ALL && !compact ) {
mvwprintz( window, 5, lastcol - table_hdr_len2 + 1, c_ltgray, _( "src amt weight vol" ) );
max_name_length = src_startpos - name_startpos - 1; // 1 for space
} else {
mvwprintz( window, 5, lastcol - table_hdr_len1 + 1, c_ltgray, _( "amt weight vol" ) );
}
for( int i = page * itemsPerPage , x = 0 ; i < ( int )items.size() &&
x < itemsPerPage ; i++ , x++ ) {
const auto &sitem = items[i];
if( sitem.is_category_header() ) {
mvwprintz( window, 6 + x, ( columns - utf8_width( sitem.name.c_str() ) - 6 ) / 2, c_cyan, "[%s]",
sitem.name.c_str() );
continue;
}
if( !sitem.is_item_entry() ) {
// Empty entry at the bottom of a page.
continue;
}
const auto &it = *sitem.it;
const bool selected = active && index == i;
nc_color thiscolor = active ? it.color( &g->u ) : norm;
nc_color thiscolordark = c_dkgray;
nc_color print_color;
if( selected ) {
thiscolor = ( inCategoryMode &&
pane.sortby == SORTBY_CATEGORY ) ? c_white_red : hilite( thiscolor );
thiscolordark = hilite( thiscolordark );
if( compact ) {
mvwprintz( window, 6 + x, 1, thiscolor, " %s", spaces.c_str() );
} else {
mvwprintz( window, 6 + x, 1, thiscolor, ">>%s", spaces.c_str() );
}
}
//print item name
std::string it_name = utf8_truncate( it.display_name(), max_name_length );
mvwprintz( window, 6 + x, compact ? 1 : 4, thiscolor, "%s", it_name.c_str() );
//print src column
if( pane.area == AIM_ALL && !compact ) {
mvwprintz( window, 6 + x, src_startpos, thiscolor, "%s",
squares[sitem.area].shortname.c_str() );
}
//print "amount" column
int it_amt = sitem.stacks;
if( it_amt > 1 ) {
print_color = thiscolor;
if( it_amt > 9999 ) {
it_amt = 9999;
print_color = selected ? hilite( c_red ) : c_red;
}
mvwprintz( window, 6 + x, amt_startpos, print_color, "%4d", it_amt );
}
//print weight column
double it_weight = g->u.convert_weight( sitem.weight );
size_t w_precision;
print_color = ( it_weight > 0 ) ? thiscolor : thiscolordark;
if( it_weight >= 1000.0 ) {
if( it_weight >= 10000.0 ) {
print_color = selected ? hilite( c_red ) : c_red;
it_weight = 9999.0;
}
w_precision = 0;
} else if( it_weight >= 100.0 ) {
w_precision = 1;
} else {
w_precision = 2;
}
mvwprintz( window, 6 + x, weight_startpos, print_color, "%5.*f", w_precision, it_weight );
//print volume column
int it_vol = sitem.volume;
print_color = ( it_vol > 0 ) ? thiscolor : thiscolordark;
if( it_vol > 9999 ) {
it_vol = 9999;
print_color = selected ? hilite( c_red ) : c_red;
}
mvwprintz( window, 6 + x, vol_startpos, print_color, "%4d", it_vol );
if( active && sitem.autopickup ) {
mvwprintz( window, 6 + x, 1, magenta_background( it.color( &g->u ) ), "%s",
( compact ? it.tname().substr( 0, 1 ) : ">" ).c_str() );
}
}
}
struct advanced_inv_sort_case_insensitive_less : public std::binary_function< char, char, bool > {
bool operator()( char x, char y ) const {
return toupper( static_cast< unsigned char >( x ) ) < toupper( static_cast< unsigned char >( y ) );
}
};
struct advanced_inv_sorter {
advanced_inv_sortby sortby;
advanced_inv_sorter( advanced_inv_sortby sort ) {
sortby = sort;
};
bool operator()( const advanced_inv_listitem &d1, const advanced_inv_listitem &d2 ) {
// Note: the item pointer can only be null on sort by category, otherwise it is always valid.
switch( sortby ) {
case SORTBY_NONE:
if( d1.idx != d2.idx ) {
return d1.idx < d2.idx;
}
break;
case SORTBY_NAME:
// Fall through to code below the switch
break;
case SORTBY_WEIGHT:
if( d1.weight != d2.weight ) {
return d1.weight > d2.weight;
}
break;
case SORTBY_VOLUME:
if( d1.volume != d2.volume ) {
return d1.volume > d2.volume;
}
break;
case SORTBY_CHARGES:
if( d1.it->charges != d2.it->charges ) {
return d1.it->charges > d2.it->charges;
}
break;
case SORTBY_CATEGORY:
assert( d1.cat != nullptr );
assert( d2.cat != nullptr );
if( d1.cat != d2.cat ) {
return *d1.cat < *d2.cat;
} else if( d1.is_category_header() ) {
return true;
} else if( d2.is_category_header() ) {
return false;
}
break;
case SORTBY_DAMAGE:
if( d1.it->damage != d2.it->damage ) {
return d1.it->damage < d2.it->damage;
}
break;
}
// secondary sort by name
const std::string *n1;
const std::string *n2;
if( d1.name_without_prefix == d2.name_without_prefix ) {
//if names without prefix equal, compare full name
n1 = &d1.name;
n2 = &d2.name;
} else {
//else compare name without prefix
n1 = &d1.name_without_prefix;
n2 = &d2.name_without_prefix;
}
return std::lexicographical_compare( n1->begin(), n1->end(),
n2->begin(), n2->end(), advanced_inv_sort_case_insensitive_less() );
}
};
void advanced_inventory::menu_square( uimenu *menu )
{
assert( menu != nullptr );
assert( menu->entries.size() >= 9 );
int ofs = -25 - 4;
int sel = menu->selected + 1;
for( int i = 1; i < 10; i++ ) {
char key = ( char )( i + 48 );
char bracket[3] = "[]";
if( squares[i].vstor >= 0 ) {
strcpy( bracket, "<>" );
}
bool canputitems = squares[i].canputitems() && menu->entries[i - 1].enabled;
nc_color bcolor = ( canputitems ? ( sel == i ? h_cyan : c_cyan ) : c_dkgray );
nc_color kcolor = ( canputitems ? ( sel == i ? h_ltgreen : c_ltgreen ) : c_dkgray );
mvwprintz( menu->window, squares[i].hscreenx + 5, squares[i].hscreeny + ofs, bcolor, "%c",
bracket[0] );
wprintz( menu->window, kcolor, "%c", key );
wprintz( menu->window, bcolor, "%c", bracket[1] );
}
}
inline char advanced_inventory::get_location_key( aim_location area )
{
switch( area ) {
case AIM_INVENTORY:
return 'I';
case AIM_SOUTHWEST:
return '1';
case AIM_SOUTH:
return '2';
case AIM_SOUTHEAST:
return '3';
case AIM_WEST:
return '4';
case AIM_CENTER:
return '5';
case AIM_EAST:
return '6';
case AIM_NORTHWEST:
return '7';
case AIM_NORTH:
return '8';
case AIM_NORTHEAST:
return '9';
case AIM_ALL:
return 'A';
case AIM_DRAGED:
return 'D';
case AIM_CONTAINER:
return 'C';
}
assert( false );
return ' ';
}
int advanced_inventory::print_header( advanced_inventory_pane &pane, aim_location sel )
{
WINDOW *window = pane.window;
int area = pane.area;
int wwidth = getmaxx( window );
int ofs = wwidth - 25 - 2 - 14;
for( int i = 0; i < 13; i++ ) {
const char key = get_location_key( static_cast<aim_location>( i ) );
const char *bracket = squares[i].veh == nullptr ? "[]" : "<>";
nc_color bcolor = c_red, kcolor = c_red;
if( squares[i].canputitems( pane.get_cur_item_ptr() ) ) {
bcolor = ( area == i || ( area == AIM_ALL && i != 0 ) ) ? c_cyan : c_ltgray;
kcolor = ( area == i ) ? c_ltgreen : ( i == sel ) ? c_cyan : c_ltgray;
}
mvwprintz( window, squares[i].hscreenx, squares[i].hscreeny + ofs, bcolor, "%c", bracket[0] );
wprintz( window, kcolor, "%c", key );
wprintz( window, bcolor, "%c", bracket[1] );
}
return squares[AIM_INVENTORY].hscreeny + ofs;
}
int advanced_inv_area::get_item_count() const
{
if( id == AIM_INVENTORY ) {
return g->u.inv.size();
} else if( id == AIM_ALL ) {
return 0;
} else if( veh != nullptr ) {
return veh->get_items(vstor).size();
} else {
return g->m.i_at( g->u.posx + offx, g->u.posy + offy ).size();
}
}
void advanced_inv_area::init()
{
x = g->u.posx + offx;
y = g->u.posy + offy;
veh = nullptr;
vstor = -1;
volume = 0; // must update in main function
weight = 0; // must update in main function
switch( id ) {
case AIM_INVENTORY:
canputitemsloc = true;
break;
case AIM_DRAGED:
if( g->u.grab_type != OBJECT_VEHICLE ) {
canputitemsloc = false;
desc = _( "Not dragging any vehicle" );
break;
}
x = g->u.posx + g->u.grab_point.x;
y = g->u.posy + g->u.grab_point.y;
veh = g->m.veh_at( x, y, vstor );
if( veh ) {
vstor = veh->part_with_feature( vstor, "CARGO", false );
}
if( vstor >= 0 ) {
desc = veh->name;
canputitemsloc = true;
max_size = MAX_ITEM_IN_VEHICLE_STORAGE;
max_volume = veh->max_volume( vstor );
} else {
veh = nullptr;
canputitemsloc = false;
desc = _( "No dragged vehicle" );
}
break;
case AIM_CONTAINER:
// set container position based on location
set_container_position();
// location always valid, actual check is done in canputitems()
// and depends on selected item in pane (if it is valid container)
canputitemsloc = true;
if( get_container() == nullptr ) {
desc = _( "Invalid container" );
}
break;
case AIM_ALL:
desc = _( "All 9 squares" );
canputitemsloc = true;
break;
case AIM_SOUTHWEST:
case AIM_SOUTH:
case AIM_SOUTHEAST:
case AIM_WEST:
case AIM_CENTER:
case AIM_EAST:
case AIM_NORTHWEST:
case AIM_NORTH:
case AIM_NORTHEAST:
veh = g->m.veh_at( g->u.posx + offx, g->u.posy + offy, vstor );
if( veh ) {
vstor = veh->part_with_feature( vstor, "CARGO", false );
}
if( vstor >= 0 ) {
desc = veh->name;
canputitemsloc = true;
max_size = MAX_ITEM_IN_VEHICLE_STORAGE;
max_volume = veh->max_volume( vstor );
} else {
veh = nullptr;
canputitemsloc = g->m.can_put_items( g->u.posx + offx, g->u.posy + offy );
max_size = MAX_ITEM_IN_SQUARE;
max_volume = g->m.max_volume( g->u.posx + offx, g->u.posy + offy );
if( g->m.has_graffiti_at( g->u.posx + offx, g->u.posy + offy ) ) {
desc = g->m.graffiti_at( g->u.posx + offx, g->u.posy + offy );
}
}
break;
}
}
std::string center_text( const char *str, int width )
{
std::string spaces;
int numSpaces = width - strlen( str );
for( int i = 0; i < numSpaces / 2; i++ ) {
spaces += " ";
}
return spaces + std::string( str );
}
void advanced_inventory::init()
{
for( auto & square : squares ) {
square.init();
}
panes[left].area = AIM_ALL;
panes[right].area = AIM_INVENTORY;
panes[left].sortby = ( advanced_inv_sortby ) uistate.adv_inv_leftsort;
panes[right].sortby = ( advanced_inv_sortby ) uistate.adv_inv_rightsort;
panes[left].area = ( aim_location ) uistate.adv_inv_leftarea;
panes[right].area = ( aim_location ) uistate.adv_inv_rightarea;
bool moved = ( uistate.adv_inv_last_coords.x != g->u.posx ||
uistate.adv_inv_last_coords.y != g->u.posy );
if( !moved ) {
src = ( side ) uistate.adv_inv_src;
dest = ( side ) uistate.adv_inv_dest;
}
if( !moved || panes[left].area == AIM_INVENTORY ) {
panes[left].index = uistate.adv_inv_leftindex;
}
if( !moved || panes[right].area == AIM_INVENTORY ) {
panes[right].index = uistate.adv_inv_rightindex;
}
panes[left].filter = uistate.adv_inv_leftfilter;
panes[right].filter = uistate.adv_inv_rightfilter;
w_height = ( TERMY < min_w_height + head_height ) ? min_w_height : TERMY - head_height;
w_width = ( TERMX < min_w_width ) ? min_w_width : ( TERMX > max_w_width ) ? max_w_width :
( int )TERMX;
headstart = 0; //(TERMY>w_height)?(TERMY-w_height)/2:0;
colstart = ( TERMX > w_width ) ? ( TERMX - w_width ) / 2 : 0;
head = newwin( head_height, w_width, headstart, colstart );
left_window = newwin( w_height, w_width / 2, headstart + head_height, colstart );
right_window = newwin( w_height, w_width / 2, headstart + head_height,
colstart + w_width / 2 );
itemsPerPage = w_height - 2 - 5; // 2 for the borders, 5 for the header stuff
panes[left].window = left_window;
panes[right].window = right_window;
}
bool cached_lcmatch( const std::string &str, const std::string &findstr,
std::map<std::string, bool> &filtercache )
{
if( filtercache.find( str ) == filtercache.end() ) {
std::string ret = "";
ret.reserve( str.size() );
transform( str.begin(), str.end(), std::back_inserter( ret ), tolower );
bool ismatch = ( ret.find( findstr ) != std::string::npos );
filtercache[ str ] = ismatch;
return ismatch;
} else {
return filtercache[ str ];
}
}
advanced_inv_listitem::advanced_inv_listitem( item *an_item, int index, int count,
aim_location _area )
: idx( index )
, area( _area )
, it( an_item )
, name( an_item->tname( count ) )
, name_without_prefix( an_item->tname( 1, false ) )
, autopickup( hasPickupRule( an_item->tname() ) )
, stacks( count )
, volume( an_item->volume() * stacks )
, weight( an_item->weight() * stacks )
, cat( &an_item->get_category() )
{
assert( stacks >= 1 );
assert( stacks == 1 || !it->count_by_charges() );
}
advanced_inv_listitem::advanced_inv_listitem()
: idx()
, area()
, it( nullptr )
, name()
, name_without_prefix()
, autopickup()
, stacks()
, volume()
, weight()
, cat( nullptr )
{
}
advanced_inv_listitem::advanced_inv_listitem( const item_category *category )
: idx()
, area()
, it( nullptr )
, name( category->name )
, name_without_prefix()
, autopickup()
, stacks()
, volume()
, weight()
, cat( category )
{
}
bool advanced_inv_listitem::is_category_header() const
{
return it == nullptr && cat != nullptr;
}
bool advanced_inv_listitem::is_item_entry() const
{
return it != nullptr;
}
bool advanced_inventory_pane::is_filtered( const advanced_inv_listitem &it ) const
{
return is_filtered( it.name );
}
bool advanced_inventory_pane::is_filtered( const std::string &name ) const
{
return !filter.empty() && !cached_lcmatch( name, filter, filtercache );
}
template <typename Container>
static itemslice i_stacked(Container items)
{
//create a new container for our stacked items
itemslice islice;
//iterate through all items in the vector
for( auto &items_it : items ) {
if( items_it.count_by_charges() ) {
// Those exists as a single item all the item anyway
islice.push_back( std::make_pair( &items_it, 1 ) );
continue;
}
bool list_exists = false;
//iterate through stacked item lists
for( auto &elem : islice ) {
//check if the ID exists
item *first_item = elem.first;
if( first_item->type->id == items_it.type->id ) {
//we've found the list of items with the same type ID
if( first_item->stacks_with( items_it ) ) {
//add it to the existing list
elem.second++;
list_exists = true;
break;
}
}
}
if(!list_exists) {
//insert the list into islice
islice.push_back( std::make_pair( &items_it, 1 ) );
}
} //end items loop
return islice;
}
void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square )
{
assert( square.id != AIM_ALL );
square.volume = 0;
square.weight = 0;
if( !square.canputitems() ) {
return;
}
// Existing items are *not* cleared on purpose, this might be called
// several time in case all surrounding squares are to be shown.
if( square.id == AIM_INVENTORY ) {
const invslice &stacks = g->u.inv.slice();
for( size_t x = 0; x < stacks.size(); ++x ) {
auto &an_item = stacks[x]->front();
advanced_inv_listitem it( &an_item, x, stacks[x]->size(), square.id );
if( is_filtered( it ) ) {
continue;
}
square.volume += it.volume;
square.weight += it.weight;
items.push_back( it );
}
} else if( square.id == AIM_CONTAINER ) {
item *cont = square.get_container();
if( cont != nullptr ) {
if( !cont->is_container_empty() ) {
// filtering does not make sense for liquid in container
item *it = &( square.get_container()->contents[0] );
advanced_inv_listitem ait( it, 0, 1, square.id );
square.volume += ait.volume;
square.weight += ait.weight;
items.push_back( ait );
}
square.desc = cont->tname( 1, false );
}
} else {
map &m = g->m;
const itemslice &stacks = square.veh != nullptr ?
i_stacked( square.veh->get_items(square.vstor) ) :
i_stacked( m.i_at( square.x, square.y ) );
for( size_t x = 0; x < stacks.size(); ++x ) {
advanced_inv_listitem it( stacks[x].first, x, stacks[x].second, square.id );
if( is_filtered( it ) ) {
continue;
}
square.volume += it.volume;
square.weight += it.weight;
items.push_back( it );
}
}
}
void advanced_inventory_pane::paginate( size_t itemsPerPage )
{
if( sortby != SORTBY_CATEGORY ) {
return; // not needed as there are no category entries here.
}
for( size_t i = 0; i < items.size(); ++i ) {
if( i % itemsPerPage == 0 ) {
// first entry on the page, should be a category header
if( items[i].is_item_entry() ) {
items.insert( items.begin() + i, advanced_inv_listitem( items[i].cat ) );
}
}
if( ( i + 1 ) % itemsPerPage == 0 && i + 1 < items.size() ) {
// last entry of the page, but not the last entry at all!
// Must *not* be a category header!
if( items[i].is_category_header() ) {
items.insert( items.begin() + i, advanced_inv_listitem() );
}
}
}
}
void advanced_inventory::recalc_pane( side p )
{
auto &pane = panes[p];
pane.recalc = false;
pane.items.clear();
// Add items from the source location or in case of all 9 surrounding squares,
// add items from several locations.
if( pane.area == AIM_ALL ) {
auto &other = squares[panes[-p + 1].area];
auto &alls = squares[AIM_ALL];
alls.volume = 0;
alls.weight = 0;
for( auto & s : squares ) {
// All the surrounding squares, nothing else
if( s.id == AIM_INVENTORY || s.id == AIM_DRAGED || s.id == AIM_ALL || s.id == AIM_CONTAINER ) {
continue;
}
// to allow the user to transfer all items from all surrounding squares to
// a specific square, filter out items that are already on that square.
// e.g. left pane AIM_ALL, right pane AIM_NORTH. The user holds the
// enter key down in the left square and moves all items to the other side.
if( other.is_same( s ) ) {
continue;
}
pane.add_items_from_area( s );
alls.volume += s.volume;
alls.weight += s.weight;
}
} else {
pane.add_items_from_area( squares[pane.area] );
}
// Insert category headers (only expected when sorting by category)
if( pane.sortby == SORTBY_CATEGORY ) {
std::set<const item_category *> categories;
for( auto & it : pane.items ) {
categories.insert( it.cat );
}
for( auto & cat : categories ) {
pane.items.push_back( advanced_inv_listitem( cat ) );
}
}
// Finally sort all items (category headers will now be moved to their proper position)
std::stable_sort( pane.items.begin(), pane.items.end(), advanced_inv_sorter( pane.sortby ) );
pane.paginate( itemsPerPage );
}
void advanced_inventory_pane::fix_index()
{
if( items.empty() ) {
index = 0;
return;
}
if( index < 0 ) {
index = 0;
} else if( static_cast<size_t>( index ) >= items.size() ) {
index = static_cast<int>( items.size() ) - 1;
}
skip_category_headers( +1 );
}
void advanced_inventory::redraw_pane( side p )
{
auto &pane = panes[p];
if( recalc || pane.recalc ) {
recalc_pane( p );
} else if( !( redraw || pane.redraw ) ) {
return;
}
pane.redraw = false;
pane.fix_index();
const bool active = p == src;
const advanced_inv_area &square = squares[pane.area];
auto w = pane.window;
werase( w );
print_items( pane, active );
auto itm = pane.get_cur_item_ptr();
int width;
if( itm == nullptr ) {
width = print_header( pane, pane.area );
} else {
width = print_header( pane, itm->area );
}
width -= 2 + 1; // starts at offset 2, plus space between the header and the text
mvwprintz( w, 1, 2, active ? c_cyan : c_ltgray, "%s", utf8_truncate( square.name, width ).c_str() );
mvwprintz( w, 2, 2, active ? c_green : c_dkgray , "%s", utf8_truncate( square.desc, width ).c_str() );
if( square.veh != nullptr ) {
const auto &part = square.veh->parts[square.vstor];
const auto label = square.veh->get_label( part.mount_dx, part.mount_dy );
if( !label.empty() ) {
mvwprintz( w, 3, 2, active ? c_green : c_dkgray , "%s", utf8_truncate( label, width ).c_str() );
}
}
const int max_page = ( pane.items.size() + itemsPerPage - 1 ) / itemsPerPage;
if( active && max_page > 1 ) {
const int page = pane.index / itemsPerPage;
mvwprintz( w, 4, 2, c_ltblue, _( "[<] page %d of %d [>]" ), page + 1, max_page );
}
if( active ) {
wattron( w, c_cyan );
}
draw_border( w );
mvwprintw( w, 0, 3, _( "< [s]ort: %s >" ), get_sortname( pane.sortby ).c_str() );
int max = MAX_ITEM_IN_SQUARE; //TODO: use the square, luke
if( pane.area == AIM_ALL ) {
max *= 9;
}
int fmtw = 7 + ( pane.items.size() > 99 ? 3 : pane.items.size() > 9 ? 2 : 1 ) +
( max > 99 ? 3 : max > 9 ? 2 : 1 );
mvwprintw( w, 0 , ( w_width / 2 ) - fmtw, "< %d/%d >", pane.items.size(), max );
const char *fprefix = _( "[F]ilter" );
const char *fsuffix = _( "[R]eset" );
if( ! filter_edit ) {
if( !pane.filter.empty() ) {
mvwprintw( w, getmaxy( w ) - 1, 2, "< %s: %s >", fprefix,
pane.filter.c_str() );
} else {
mvwprintw( w, getmaxy( w ) - 1, 2, "< %s >", fprefix );
}
}
if( active ) {
wattroff( w, c_white );
}
if( ! filter_edit && !pane.filter.empty() ) {
mvwprintz( w, getmaxy( w ) - 1, 6 + strlen( fprefix ), c_white, "%s",
pane.filter.c_str() );
mvwprintz( w, getmaxy( w ) - 1,
getmaxx( w ) - strlen( fsuffix ) - 2, c_white, "%s", fsuffix );
}
wrefresh( w );
}
bool advanced_inventory::move_all_items()
{
auto &spane = panes[src];
auto &dpane = panes[dest];
// Check some preconditions to quickly leave the function.
if( spane.items.empty() ) {
return false;
}
if( dpane.area == AIM_ALL ) {
popup( _( "You have to choose a destination area." ) );
return false;
}
if( spane.area == AIM_ALL ) {
popup( _( "You have to choose a source area." ) );
return false;
}
if( spane.area == AIM_INVENTORY &&
!query_yn( _( "Really move everything from your inventory?" ) ) ) {
return false;
}
auto &sarea = squares[spane.area];
auto &darea = squares[dpane.area];
if( OPTIONS["CLOSE_ADV_INV"] != true ) {
// Why is this here? It's because the activity backlog can act
// like a stack instead of a single deferred activity in order to
// accomplish some UI shenanigans. The inventory menu activity is
// added, then an activity to drop is pushed on the stack, then
// the drop activity is repeatedly popped and pushed on the stack
// until all its items are processed. When the drop activity runs out,
// the inventory menu activity is there waiting and seamlessly returns
// the player to the menu. If the activity is interrupted instead of
// completing, both activities are cancelled.
// Thanks to kevingranade for the explanation.
g->u.assign_activity( ACT_ADV_INVENTORY, 0 );
g->u.activity.auto_resume = true;
}
if( spane.area == AIM_INVENTORY ) {
g->u.assign_activity( ACT_DROP, 0 );
g->u.activity.placement = point( darea.x - g->u.xpos(), darea.y - g->u.ypos() );
for( size_t index = 0; index < g->u.inv.size(); ++index ) {
const auto &stack = g->u.inv.const_stack( index );
if( spane.is_filtered( stack.front().tname() ) ) {
continue;
}
g->u.activity.values.push_back( index );
if( stack.front().count_by_charges() ) {
g->u.activity.values.push_back( stack.front().charges );
} else {
g->u.activity.values.push_back( stack.size() );
}
}
} else {
if( dpane.area == AIM_INVENTORY ) {
g->u.assign_activity( ACT_PICKUP, 0 );
g->u.activity.values.push_back( sarea.veh != nullptr );
} else { // Vehicle and map destinations are handled the same.
g->u.assign_activity( ACT_MOVE_ITEMS, 0 );
// Stash the destination at the start of the values vector.
g->u.activity.values.push_back( darea.x - g->u.xpos() );
g->u.activity.values.push_back( darea.y - g->u.ypos() );
}