forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruction.cpp
1649 lines (1498 loc) · 65.5 KB
/
construction.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 "construction.h"
#include <algorithm>
#include <sstream>
#include <array>
#include <iterator>
#include <memory>
#include <utility>
#include "action.h"
#include "avatar.h"
#include "cata_utility.h"
#include "coordinate_conversions.h"
#include "debug.h"
#include "event_bus.h"
#include "game.h"
#include "input.h"
#include "item_group.h"
#include "iuse.h"
#include "json.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "messages.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "player.h"
#include "requirements.h"
#include "rng.h"
#include "skill.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "trap.h"
#include "uistate.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "calendar.h"
#include "character.h"
#include "color.h"
#include "cursesdef.h"
#include "enums.h"
#include "game_constants.h"
#include "int_id.h"
#include "item.h"
#include "player_activity.h"
#include "morale_types.h"
#include "colony.h"
#include "construction_category.h"
#include "item_stack.h"
#include "mtype.h"
#include "point.h"
#include "units.h"
class inventory;
static const skill_id skill_fabrication( "fabrication" );
static const skill_id skill_electronics( "electronics" );
static const trait_id trait_DEBUG_HS( "DEBUG_HS" );
static const trait_id trait_PAINRESIST_TROGLO( "PAINRESIST_TROGLO" );
static const trait_id trait_STOCKY_TROGLO( "STOCKY_TROGLO" );
static const trait_id trait_SPIRITUAL( "SPIRITUAL" );
const trap_str_id tr_firewood_source( "tr_firewood_source" );
const trap_str_id tr_practice_target( "tr_practice_target" );
const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
// Construction functions.
namespace construct
{
// Checks for whether terrain mod can proceed
static bool check_nothing( const tripoint & )
{
return true;
}
bool check_empty( const tripoint & ); // tile is empty
bool check_support( const tripoint & ); // at least two orthogonal supports
bool check_deconstruct( const tripoint & ); // either terrain or furniture must be deconstructible
bool check_empty_up_OK( const tripoint & ); // tile is empty and below OVERMAP_HEIGHT
bool check_up_OK( const tripoint & ); // tile is below OVERMAP_HEIGHT
bool check_down_OK( const tripoint & ); // tile is above OVERMAP_DEPTH
bool check_no_trap( const tripoint & );
// Special actions to be run post-terrain-mod
static void done_nothing( const tripoint & ) {}
void done_trunk_plank( const tripoint & );
void done_grave( const tripoint & );
void done_vehicle( const tripoint & );
void done_deconstruct( const tripoint & );
void done_digormine_stair( const tripoint &, bool );
void done_dig_stair( const tripoint & );
void done_mine_downstair( const tripoint & );
void done_mine_upstair( const tripoint & );
void done_wood_stairs( const tripoint & );
void done_window_curtains( const tripoint & );
void done_extract_maybe_revert_to_dirt( const tripoint & );
void done_mark_firewood( const tripoint & );
void done_mark_practice_target( const tripoint & );
void failure_standard( const tripoint & );
void failure_deconstruct( const tripoint & );
} // namespace construct
std::vector<construction> constructions;
// Helper functions, nobody but us needs to call these.
static bool can_construct( const std::string &desc );
static bool can_construct( const construction &con );
static bool player_can_build( player &p, const inventory &inv, const std::string &desc );
static void place_construction( const std::string &desc );
// Color standardization for string streams
static const deferred_color color_title = def_c_light_red; //color for titles
static const deferred_color color_data = def_c_cyan; //color for data parts
void standardize_construction_times( const int time )
{
for( auto &c : constructions ) {
c.time = time;
}
}
static std::vector<construction *> constructions_by_desc( const std::string &description )
{
std::vector<construction *> result;
for( auto &constructions_a : constructions ) {
if( constructions_a.description == description ) {
result.push_back( &constructions_a );
}
}
return result;
}
static void load_available_constructions( std::vector<std::string> &available,
std::map<construction_category_id, std::vector<std::string>> &cat_available,
bool hide_unconstructable )
{
cat_available.clear();
available.clear();
for( auto &it : constructions ) {
if( it.on_display && ( !hide_unconstructable || can_construct( it ) ) ) {
bool already_have_it = false;
for( auto &avail_it : available ) {
if( avail_it == it.description ) {
already_have_it = true;
break;
}
}
if( !already_have_it ) {
available.push_back( it.description );
cat_available[it.category].push_back( it.description );
}
}
}
}
static void draw_grid( const catacurses::window &w, const int list_width )
{
draw_border( w );
mvwprintz( w, point( 2, 0 ), c_light_red, _( " Construction " ) );
// draw internal lines
mvwvline( w, point( list_width, 1 ), LINE_XOXO, getmaxy( w ) - 2 );
mvwhline( w, point( 1, 2 ), LINE_OXOX, list_width );
// draw intersections
mvwputch( w, point( list_width, 0 ), c_light_gray, LINE_OXXX );
mvwputch( w, point( list_width, getmaxy( w ) - 1 ), c_light_gray, LINE_XXOX );
mvwputch( w, point( 0, 2 ), c_light_gray, LINE_XXXO );
mvwputch( w, point( list_width, 2 ), c_light_gray, LINE_XOXX );
wrefresh( w );
}
static nc_color construction_color( const std::string &con_name, bool highlight )
{
nc_color col = c_dark_gray;
if( g->u.has_trait( trait_id( "DEBUG_HS" ) ) ) {
col = c_white;
} else if( can_construct( con_name ) ) {
construction *con_first = nullptr;
std::vector<construction *> cons = constructions_by_desc( con_name );
const inventory &total_inv = g->u.crafting_inventory();
for( auto &con : cons ) {
if( con->requirements->can_make_with_inventory( total_inv, is_crafting_component ) ) {
con_first = con;
break;
}
}
if( con_first != nullptr ) {
col = c_white;
for( const auto &pr : con_first->required_skills ) {
int s_lvl = g->u.get_skill_level( pr.first );
if( s_lvl < pr.second ) {
col = c_red;
} else if( s_lvl < pr.second * 1.25 ) {
col = c_light_blue;
}
}
}
}
return highlight ? hilite( col ) : col;
}
const std::vector<construction> &get_constructions()
{
return constructions;
}
int construction_menu( bool blueprint )
{
static bool hide_unconstructable = false;
// only display constructions the player can theoretically perform
std::vector<std::string> available;
std::map<construction_category_id, std::vector<std::string>> cat_available;
load_available_constructions( available, cat_available, hide_unconstructable );
if( available.empty() ) {
popup( _( "You can not construct anything here." ) );
return -1;
}
int w_height = TERMY;
if( static_cast<int>( available.size() ) + 2 < w_height ) {
w_height = available.size() + 2;
}
if( w_height < FULL_SCREEN_HEIGHT ) {
w_height = FULL_SCREEN_HEIGHT;
}
const int w_width = std::max( FULL_SCREEN_WIDTH, TERMX * 2 / 3 );
const int w_y0 = ( TERMY > w_height ) ? ( TERMY - w_height ) / 2 : 0;
const int w_x0 = ( TERMX > w_width ) ? ( TERMX - w_width ) / 2 : 0;
catacurses::window w_con = catacurses::newwin( w_height, w_width, point( w_x0, w_y0 ) );
const int w_list_width = static_cast<int>( .375 * w_width );
const int w_list_height = w_height - 4;
const int w_list_x0 = 1;
catacurses::window w_list = catacurses::newwin( w_list_height, w_list_width,
point( w_x0 + w_list_x0, w_y0 + 3 ) );
draw_grid( w_con, w_list_width + w_list_x0 );
int ret = -1;
std::vector<construction_category> construct_cat;
construct_cat = construction_categories::get_all();
bool update_info = true;
bool update_cat = true;
bool isnew = true;
int tabindex = 0;
int select = 0;
int offset = 0;
bool exit = false;
construction_category_id category_id;
std::vector<std::string> constructs;
//storage for the color text so it can be scrolled
std::vector< std::vector < std::string > > construct_buffers;
std::vector<std::string> full_construct_buffer;
std::vector<int> construct_buffer_breakpoints;
int total_project_breakpoints = 0;
int current_construct_breakpoint = 0;
bool previous_hide_unconstructable = false;
//track the cursor to determine when to refresh the list of construction recipes
int previous_tabindex = -1;
int previous_select = -1;
const inventory &total_inv = g->u.crafting_inventory();
input_context ctxt( "CONSTRUCTION" );
ctxt.register_action( "UP", translate_marker( "Move cursor up" ) );
ctxt.register_action( "DOWN", translate_marker( "Move cursor down" ) );
ctxt.register_action( "RIGHT", translate_marker( "Move tab right" ) );
ctxt.register_action( "LEFT", translate_marker( "Move tab left" ) );
ctxt.register_action( "PAGE_UP" );
ctxt.register_action( "PAGE_DOWN" );
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" );
ctxt.register_action( "QUIT" );
ctxt.register_action( "HELP_KEYBINDINGS" );
ctxt.register_action( "FILTER" );
static const int tabcount = static_cast<int>( construction_category::count() );
std::string filter;
int previous_index = 0;
do {
if( update_cat ) {
update_cat = false;
category_id = construction_categories::get_all()[tabindex].id;
if( category_id == "ALL" ) {
constructs = available;
previous_index = tabindex;
} else if( category_id == "FILTER" ) {
constructs.clear();
previous_select = -1;
std::copy_if( available.begin(), available.end(),
std::back_inserter( constructs ),
[&]( const std::string & a ) {
return lcmatch( _( a ), filter );
} );
} else {
constructs = cat_available[category_id];
previous_index = tabindex;
}
if( isnew ) {
if( !uistate.last_construction.empty() ) {
select = std::distance( constructs.begin(),
std::find( constructs.begin(),
constructs.end(),
uistate.last_construction ) );
}
filter = uistate.construction_filter;
}
}
isnew = false;
// Erase existing tab selection & list of constructions
mvwhline( w_con, point_south_east, ' ', w_list_width );
werase( w_list );
// Print new tab listing
// NOLINTNEXTLINE(cata-use-named-point-constants)
mvwprintz( w_con, point( 1, 1 ), c_yellow, "<< %s >>", _( construct_cat[tabindex].name ) );
// Determine where in the master list to start printing
calcStartPos( offset, select, w_list_height, constructs.size() );
// Print the constructions between offset and max (or how many will fit)
for( size_t i = 0; static_cast<int>( i ) < w_list_height &&
( i + offset ) < constructs.size(); i++ ) {
int current = i + offset;
std::string con_name = constructs[current];
bool highlight = ( current == select );
trim_and_print( w_list, point( 0, i ), w_list_width,
construction_color( con_name, highlight ), _( con_name ) );
}
if( update_info ) {
update_info = false;
// Clear out lines for tools & materials
const int pos_x = w_list_width + w_list_x0 + 2;
const int available_window_width = w_width - pos_x - 1;
for( int i = 1; i < w_height - 1; i++ ) {
mvwhline( w_con, point( pos_x, i ), ' ', available_window_width );
}
std::vector<std::string> notes;
notes.push_back( string_format( _( "Press %s or %s to tab." ), ctxt.get_desc( "LEFT" ),
ctxt.get_desc( "RIGHT" ) ) );
notes.push_back( string_format( _( "Press %s to search." ), ctxt.get_desc( "FILTER" ) ) );
notes.push_back( string_format( _( "Press %s to toggle unavailable constructions." ),
ctxt.get_desc( "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" ) ) );
notes.push_back( string_format( _( "Press %s to view and edit key-bindings." ),
ctxt.get_desc( "HELP_KEYBINDINGS" ) ) );
//leave room for top and bottom UI text
const int available_buffer_height = w_height - 3 - 3 - static_cast<int>( notes.size() );
// print the hotkeys regardless of if there are constructions
for( size_t i = 0; i < notes.size(); ++i ) {
trim_and_print( w_con, point( pos_x,
w_height - 1 - static_cast<int>( notes.size() ) + static_cast<int>( i ) ),
available_window_width, c_white, notes[i] );
}
if( !constructs.empty() ) {
nc_color color_stage = c_white;
if( select >= static_cast<int>( constructs.size() ) ) {
select = 0;
}
std::string current_desc = constructs[select];
// Print construction name
trim_and_print( w_con, point( pos_x, 1 ), available_window_width, c_white, _( current_desc ) );
//only reconstruct the project list when moving away from the current item, or when changing the display mode
if( previous_select != select || previous_tabindex != tabindex ||
previous_hide_unconstructable != hide_unconstructable ) {
previous_select = select;
previous_tabindex = tabindex;
previous_hide_unconstructable = hide_unconstructable;
//construct the project list buffer
// Print stages and their requirement.
std::vector<construction *> options = constructions_by_desc( current_desc );
construct_buffers.clear();
current_construct_breakpoint = 0;
construct_buffer_breakpoints.clear();
full_construct_buffer.clear();
int stage_counter = 0;
for( std::vector<construction *>::iterator it = options.begin();
it != options.end(); ++it ) {
stage_counter++;
construction *current_con = *it;
if( hide_unconstructable && !can_construct( *current_con ) ) {
continue;
}
// Update the cached availability of components and tools in the requirement object
current_con->requirements->can_make_with_inventory( total_inv, is_crafting_component );
std::vector<std::string> current_buffer;
std::ostringstream current_line;
// display final product name only if more than one step.
// Assume single stage constructions should be clear
// in their title what their result is.
if( !current_con->post_terrain.empty() && options.size() > 1 ) {
//also print out stage number when multiple stages are available
current_line << _( "Stage/Variant #" ) << stage_counter << ": ";
// print name of the result of each stage
std::string result_string;
if( current_con->post_is_furniture ) {
result_string = furn_str_id( current_con->post_terrain ).obj().name();
} else {
result_string = ter_str_id( current_con->post_terrain ).obj().name();
}
current_line << colorize( result_string, color_title );
std::vector<std::string> folded_result_string = foldstring( current_line.str(),
available_window_width );
current_buffer.insert( current_buffer.end(), folded_result_string.begin(),
folded_result_string.end() );
// display description of the result for multi-stages
current_line.str( "" );
current_line << _( "Result: " );
if( current_con->post_is_furniture ) {
current_line << colorize(
furn_str_id( current_con->post_terrain ).obj().description,
color_data
);
} else {
current_line << colorize(
ter_str_id( current_con->post_terrain ).obj().description,
color_data
);
}
folded_result_string = foldstring( current_line.str(), available_window_width );
current_buffer.insert( current_buffer.end(), folded_result_string.begin(),
folded_result_string.end() );
// display description of the result for single stages
} else if( !current_con->post_terrain.empty() ) {
current_line.str( "" );
current_line << _( "Result: " );
if( current_con->post_is_furniture ) {
current_line << colorize(
furn_str_id( current_con->post_terrain ).obj().description,
color_data
);
} else {
current_line << colorize(
ter_str_id( current_con->post_terrain ).obj().description,
color_data
);
}
std::vector<std::string> folded_result_string = foldstring( current_line.str(),
available_window_width );
current_buffer.insert( current_buffer.end(), folded_result_string.begin(),
folded_result_string.end() );
}
current_line.str( "" );
// display required skill and difficulty
if( current_con->required_skills.empty() ) {
current_line << _( "N/A" );
} else {
current_line << _( "Required skills: " ) <<
enumerate_as_string( current_con->required_skills.begin(),
current_con->required_skills.end(),
[]( const std::pair<skill_id, int> &skill ) {
nc_color col;
int s_lvl = g->u.get_skill_level( skill.first );
if( s_lvl < skill.second ) {
col = c_red;
} else if( s_lvl < skill.second * 1.25 ) {
col = c_light_blue;
} else {
col = c_green;
}
std::string color_s = "<color_" + string_from_color( col ) + ">";
return string_format( "%s%s (%d)</color>", color_s,
skill.first.obj().name(), skill.second );
}, enumeration_conjunction::none );
}
current_buffer.push_back( current_line.str() );
// TODO: Textify pre_flags to provide a bit more information.
// Example: First step of dig pit could say something about
// requiring diggable ground.
current_line.str( "" );
if( !current_con->pre_terrain.empty() ) {
std::string require_string;
if( current_con->pre_is_furniture ) {
require_string = furn_str_id( current_con->pre_terrain ).obj().name();
} else {
require_string = ter_str_id( current_con->pre_terrain ).obj().name();
}
current_line << _( "Requires: " )
<< colorize( require_string, color_data );
std::vector<std::string> folded_result_string = foldstring( current_line.str(),
available_window_width );
current_buffer.insert( current_buffer.end(), folded_result_string.begin(),
folded_result_string.end() );
}
if( !current_con->pre_note.empty() ) {
current_line.str( "" );
current_line << _( "Annotation: " )
<< colorize( _( current_con->pre_note ), color_data );
std::vector<std::string> folded_result_string =
foldstring( current_line.str(), available_window_width );
current_buffer.insert( current_buffer.end(), folded_result_string.begin(),
folded_result_string.end() );
}
// get pre-folded versions of the rest of the construction project to be displayed later
// get time needed
std::vector<std::string> folded_time = current_con->get_folded_time_string(
available_window_width );
current_buffer.insert( current_buffer.end(), folded_time.begin(), folded_time.end() );
std::vector<std::string> folded_tools = current_con->requirements->get_folded_tools_list(
available_window_width, color_stage, total_inv );
current_buffer.insert( current_buffer.end(), folded_tools.begin(), folded_tools.end() );
std::vector<std::string> folded_components = current_con->requirements->get_folded_components_list(
available_window_width, color_stage, total_inv, is_crafting_component );
current_buffer.insert( current_buffer.end(), folded_components.begin(), folded_components.end() );
construct_buffers.push_back( current_buffer );
}
//determine where the printing starts for each project, so it can be scrolled to those points
size_t current_buffer_location = 0;
for( size_t i = 0; i < construct_buffers.size(); i++ ) {
construct_buffer_breakpoints.push_back( static_cast<int>( current_buffer_location ) );
full_construct_buffer.insert( full_construct_buffer.end(), construct_buffers[i].begin(),
construct_buffers[i].end() );
//handle text too large for one screen
if( construct_buffers[i].size() > static_cast<size_t>( available_buffer_height ) ) {
construct_buffer_breakpoints.push_back( static_cast<int>( current_buffer_location +
static_cast<size_t>( available_buffer_height ) ) );
}
current_buffer_location += construct_buffers[i].size();
if( i < construct_buffers.size() - 1 ) {
full_construct_buffer.push_back( std::string() );
current_buffer_location++;
}
}
total_project_breakpoints = static_cast<int>( construct_buffer_breakpoints.size() );
}
if( current_construct_breakpoint > 0 ) {
// Print previous stage indicator if breakpoint is past the beginning
trim_and_print( w_con, point( pos_x, 2 ), available_window_width, c_white,
_( "Press %s to show previous stage(s)." ),
ctxt.get_desc( "PAGE_UP" ) );
}
if( static_cast<size_t>( construct_buffer_breakpoints[current_construct_breakpoint] +
available_buffer_height ) < full_construct_buffer.size() ) {
// Print next stage indicator if more breakpoints are remaining after screen height
trim_and_print( w_con, point( pos_x, w_height - 2 - static_cast<int>( notes.size() ) ),
available_window_width,
c_white, _( "Press %s to show next stage(s)." ),
ctxt.get_desc( "PAGE_DOWN" ) );
}
// Leave room for above/below indicators
int ypos = 3;
nc_color stored_color = color_stage;
for( size_t i = static_cast<size_t>( construct_buffer_breakpoints[current_construct_breakpoint] );
i < full_construct_buffer.size(); i++ ) {
//the value of 3 is from leaving room at the top of window
if( ypos > available_buffer_height + 3 ) {
break;
}
print_colored_text( w_con, point( w_list_width + w_list_x0 + 2, ypos++ ), stored_color, color_stage,
full_construct_buffer[i] );
}
}
} // Finished updating
draw_scrollbar( w_con, select, w_list_height, constructs.size(), point( 0, 3 ) );
wrefresh( w_con );
wrefresh( w_list );
const std::string action = ctxt.handle_input();
if( action == "FILTER" ) {
string_input_popup()
.title( _( "Search" ) )
.width( 50 )
.description( _( "Filter" ) )
.max_length( 100 )
.edit( filter );
if( !filter.empty() ) {
update_info = true;
update_cat = true;
tabindex = tabcount - 1;
select = 0;
} else if( previous_index != tabcount - 1 ) {
tabindex = previous_index;
update_info = true;
update_cat = true;
select = 0;
}
uistate.construction_filter = filter;
} else if( action == "DOWN" ) {
update_info = true;
if( select < static_cast<int>( constructs.size() ) - 1 ) {
select++;
} else {
select = 0;
}
} else if( action == "UP" ) {
update_info = true;
if( select > 0 ) {
select--;
} else {
select = constructs.size() - 1;
}
} else if( action == "LEFT" ) {
update_info = true;
update_cat = true;
select = 0;
tabindex--;
if( tabindex < 0 ) {
tabindex = tabcount - 1;
}
} else if( action == "RIGHT" ) {
update_info = true;
update_cat = true;
select = 0;
tabindex = ( tabindex + 1 ) % tabcount;
} else if( action == "PAGE_UP" ) {
update_info = true;
if( current_construct_breakpoint > 0 ) {
current_construct_breakpoint--;
}
if( current_construct_breakpoint < 0 ) {
current_construct_breakpoint = 0;
}
} else if( action == "PAGE_DOWN" ) {
update_info = true;
if( current_construct_breakpoint < total_project_breakpoints - 1 ) {
current_construct_breakpoint++;
}
if( current_construct_breakpoint >= total_project_breakpoints ) {
current_construct_breakpoint = total_project_breakpoints - 1;
}
} else if( action == "QUIT" ) {
exit = true;
} else if( action == "HELP_KEYBINDINGS" ) {
draw_grid( w_con, w_list_width + w_list_x0 );
} else if( action == "TOGGLE_UNAVAILABLE_CONSTRUCTIONS" ) {
update_info = true;
update_cat = true;
hide_unconstructable = !hide_unconstructable;
select = 0;
offset = 0;
load_available_constructions( available, cat_available, hide_unconstructable );
} else if( action == "CONFIRM" ) {
if( constructs.empty() || select >= static_cast<int>( constructs.size() ) ) {
// Nothing to be done here
continue;
}
if( !blueprint ) {
if( player_can_build( g->u, total_inv, constructs[select] ) ) {
if( g->u.fine_detail_vision_mod() > 4 && !g->u.has_trait( trait_DEBUG_HS ) ) {
add_msg( m_info, _( "It is too dark to construct right now." ) );
} else {
place_construction( constructs[select] );
uistate.last_construction = constructs[select];
}
exit = true;
} else {
popup( _( "You can't build that!" ) );
draw_grid( w_con, w_list_width + w_list_x0 );
update_info = true;
}
} else {
// get the index of the overall constructions list from current_desc
const std::vector<construction> &list_constructions = get_constructions();
for( int i = 0; i < static_cast<int>( list_constructions.size() ); ++i ) {
if( constructs[select] == list_constructions[i].description ) {
ret = i;
break;
}
}
exit = true;
}
}
} while( !exit );
w_list = catacurses::window();
w_con = catacurses::window();
g->refresh_all();
return ret;
}
bool player_can_build( player &p, const inventory &inv, const std::string &desc )
{
// check all with the same desc to see if player can build any
std::vector<construction *> cons = constructions_by_desc( desc );
for( auto &con : cons ) {
if( player_can_build( p, inv, *con ) ) {
return true;
}
}
return false;
}
bool player_can_build( player &p, const inventory &inv, const construction &con )
{
if( p.has_trait( trait_DEBUG_HS ) ) {
return true;
}
if( !p.meets_skill_requirements( con ) ) {
return false;
}
return con.requirements->can_make_with_inventory( inv, is_crafting_component );
}
bool can_construct( const std::string &desc )
{
// check all with the same desc to see if player can build any
std::vector<construction *> cons = constructions_by_desc( desc );
for( auto &con : cons ) {
if( can_construct( *con ) ) {
return true;
}
}
return false;
}
bool can_construct( const construction &con, const tripoint &p )
{
// see if the special pre-function checks out
bool place_okay = con.pre_special( p );
// see if the terrain type checks out
if( !con.pre_terrain.empty() ) {
if( con.pre_is_furniture ) {
furn_id f = furn_id( con.pre_terrain );
place_okay &= g->m.furn( p ) == f;
} else {
ter_id t = ter_id( con.pre_terrain );
place_okay &= g->m.ter( p ) == t;
}
}
// see if the flags check out
place_okay &= std::all_of( con.pre_flags.begin(), con.pre_flags.end(),
[&p]( const std::string & flag ) {
return g->m.has_flag( flag, p );
} );
// make sure the construction would actually do something
if( !con.post_terrain.empty() ) {
if( con.post_is_furniture ) {
furn_id f = furn_id( con.post_terrain );
place_okay &= g->m.furn( p ) != f;
} else {
ter_id t = ter_id( con.post_terrain );
place_okay &= g->m.ter( p ) != t;
}
}
return place_okay;
}
bool can_construct( const construction &con )
{
for( const tripoint &p : g->m.points_in_radius( g->u.pos(), 1 ) ) {
if( p != g->u.pos() && can_construct( con, p ) ) {
return true;
}
}
return false;
}
void place_construction( const std::string &desc )
{
g->refresh_all();
const inventory &total_inv = g->u.crafting_inventory();
std::vector<construction *> cons = constructions_by_desc( desc );
std::map<tripoint, const construction *> valid;
for( const tripoint &p : g->m.points_in_radius( g->u.pos(), 1 ) ) {
for( const auto *con : cons ) {
if( p != g->u.pos() && can_construct( *con, p ) && player_can_build( g->u, total_inv, *con ) ) {
valid[ p ] = con;
}
}
}
for( auto &elem : valid ) {
g->m.drawsq( g->w_terrain, g->u, elem.first, true, false,
g->u.pos() + g->u.view_offset );
}
wrefresh( g->w_terrain );
g->draw_panels();
const cata::optional<tripoint> pnt_ = choose_adjacent( _( "Construct where?" ) );
if( !pnt_ ) {
return;
}
const tripoint pnt = *pnt_;
if( valid.find( pnt ) == valid.end() ) {
cons.front()->explain_failure( pnt );
return;
}
// Maybe there is alreayd a partial_con on an existing trap, that isnt caught by the usual trap-checking.
// because the pre-requisite construction is already a trap anyway.
// This shouldnt normally happen, unless it's a spike pit being built on a pit for example.
partial_con *pre_c = g->m.partial_con_at( pnt );
if( pre_c ) {
add_msg( m_info,
_( "There is already an unfinished construction there, examine it to continue working on it" ) );
return;
}
std::list<item> used;
const construction &con = *valid.find( pnt )->second;
// create the partial construction struct
partial_con pc;
pc.id = con.id;
pc.counter = 0;
// Set the trap that has the examine function
// Special handling for constructions that take place on existing traps.
// Basically just dont add the unfinished construction trap.
// TODO : handle this cleaner, instead of adding a special case to pit iexamine.
if( g->m.tr_at( pnt ).loadid == tr_null ) {
g->m.trap_set( pnt, tr_unfinished_construction );
}
// Use up the components
for( const auto &it : con.requirements->get_components() ) {
std::list<item> tmp = g->u.consume_items( it, 1, is_crafting_component );
used.splice( used.end(), tmp );
}
pc.components = used;
g->m.partial_con_set( pnt, pc );
for( const auto &it : con.requirements->get_tools() ) {
g->u.consume_tools( it );
}
g->u.assign_activity( activity_id( "ACT_BUILD" ) );
g->u.activity.placement = g->m.getabs( pnt );
}
void complete_construction( player *p )
{
const tripoint terp = g->m.getlocal( p->activity.placement );
partial_con *pc = g->m.partial_con_at( terp );
if( !pc ) {
debugmsg( "No partial construction found at activity placement in complete_construction()" );
if( g->m.tr_at( terp ).loadid == tr_unfinished_construction ) {
g->m.remove_trap( terp );
}
if( p->is_npc() ) {
npc *guy = dynamic_cast<npc *>( p );
guy->current_activity_id = activity_id::NULL_ID();
guy->revert_after_activity();
guy->set_moves( 0 );
}
return;
}
const construction &built = constructions[pc->id];
const auto award_xp = [&]( player & c ) {
for( const auto &pr : built.required_skills ) {
c.practice( pr.first, static_cast<int>( ( 10 + 15 * pr.second ) * ( 1 + built.time / 180000.0 ) ),
static_cast<int>( pr.second * 1.25 ) );
}
};
award_xp( *p );
// Friendly NPCs gain exp from assisting or watching...
// TODO NPCs watching other NPCs do stuff and learning from it
if( p->is_player() ) {
for( auto &elem : g->u.get_crafting_helpers() ) {
if( elem->meets_skill_requirements( built ) ) {
add_msg( m_info, _( "%s assists you with the work..." ), elem->name );
} else {
//NPC near you isn't skilled enough to help
add_msg( m_info, _( "%s watches you work..." ), elem->name );
}
award_xp( *elem );
}
}
if( g->m.tr_at( terp ).loadid == tr_unfinished_construction ) {
g->m.remove_trap( terp );
}
g->m.partial_con_remove( terp );
// Some constructions are allowed to have items left on the tile.
if( built.post_flags.count( "keep_items" ) == 0 ) {
// Move any items that have found their way onto the construction site.
std::vector<tripoint> dump_spots;
for( const tripoint &pt : g->m.points_in_radius( terp, 1 ) ) {
if( g->m.can_put_items( pt ) && pt != terp ) {
dump_spots.push_back( pt );
}
}
if( !dump_spots.empty() ) {
tripoint dump_spot = random_entry( dump_spots );
map_stack items = g->m.i_at( terp );
for( map_stack::iterator it = items.begin(); it != items.end(); ) {
g->m.add_item_or_charges( dump_spot, *it );
it = items.erase( it );
}
} else {
debugmsg( "No space to displace items from construction finishing" );
}
}
// Make the terrain change
if( !built.post_terrain.empty() ) {
if( built.post_is_furniture ) {
g->m.furn_set( terp, furn_str_id( built.post_terrain ) );
} else {
g->m.ter_set( terp, ter_str_id( built.post_terrain ) );
}
}
// Spawn byproducts
if( built.byproduct_item_group ) {
g->m.spawn_items( p->pos(), item_group::items_from( *built.byproduct_item_group, calendar::turn ) );
}
add_msg( m_info, _( "%s finished construction: %s." ), p->disp_name(), _( built.description ) );
// clear the activity
p->activity.set_to_null();
// This comes after clearing the activity, in case the function interrupts
// activities
built.post_special( terp );
// npcs will automatically resume backlog, players wont.
if( p->is_player() && !p->backlog.empty() &&
p->backlog.front().id() == activity_id( "ACT_MULTIPLE_CONSTRUCTION" ) ) {
p->backlog.clear();
p->assign_activity( activity_id( "ACT_MULTIPLE_CONSTRUCTION" ) );
}
}
bool construct::check_empty( const tripoint &p )
{
return ( g->m.has_flag( "FLAT", p ) && !g->m.has_furn( p ) &&
g->is_empty( p ) && g->m.tr_at( p ).is_null() &&
g->m.i_at( p ).empty() && !g->m.veh_at( p ) );
}
inline std::array<tripoint, 4> get_orthogonal_neighbors( const tripoint &p )
{
return {{
p + point_north,
p + point_south,
p + point_west,
p + point_east
}};
}
bool construct::check_support( const tripoint &p )
{
// need two or more orthogonally adjacent supports
if( g->m.impassable( p ) ) {
return false;
}
int num_supports = 0;
for( const tripoint &nb : get_orthogonal_neighbors( p ) ) {
if( g->m.has_flag( "SUPPORTS_ROOF", nb ) ) {
num_supports++;
}
}
return num_supports >= 2;
}
bool construct::check_deconstruct( const tripoint &p )
{
if( g->m.has_furn( p.xy() ) ) {
return g->m.furn( p.xy() ).obj().deconstruct.can_do;
}
// terrain can only be deconstructed when there is no furniture in the way
return g->m.ter( p.xy() ).obj().deconstruct.can_do;
}
bool construct::check_empty_up_OK( const tripoint &p )
{
return check_empty( p ) && check_up_OK( p );
}
bool construct::check_up_OK( const tripoint & )
{
// You're not going above +OVERMAP_HEIGHT.
return ( g->get_levz() < OVERMAP_HEIGHT );
}
bool construct::check_down_OK( const tripoint & )
{
// You're not going below -OVERMAP_DEPTH.
return ( g->get_levz() > -OVERMAP_DEPTH );
}
bool construct::check_no_trap( const tripoint &p )
{
return g->m.tr_at( p ).is_null();
}