-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathcrafting.cpp
2697 lines (2399 loc) · 105 KB
/
crafting.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 "crafting.h"
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iosfwd>
#include <limits>
#include <map>
#include <memory>
#include <new>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "activity_actor_definitions.h"
#include "activity_handlers.h"
#include "avatar.h"
#include "bionics.h"
#include "calendar.h"
#include "cata_assert.h"
#include "cata_utility.h"
#include "character.h"
#include "colony.h"
#include "color.h"
#include "craft_command.h"
#include "crafting_gui.h"
#include "debug.h"
#include "enum_traits.h"
#include "enums.h"
#include "faction.h"
#include "flag.h"
#include "game.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "handle_liquid.h"
#include "inventory.h"
#include "item.h"
#include "item_location.h"
#include "item_pocket.h"
#include "item_stack.h"
#include "itype.h"
#include "iuse.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "map_selector.h"
#include "mapdata.h"
#include "messages.h"
#include "mutation.h"
#include "npc.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "pimpl.h"
#include "player_activity.h"
#include "point.h"
#include "proficiency.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "requirements.h"
#include "ret_val.h"
#include "rng.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "visitable.h"
#include "vpart_position.h"
#include "weather.h"
static const activity_id ACT_DISASSEMBLE( "ACT_DISASSEMBLE" );
static const efftype_id effect_contacts( "contacts" );
static const itype_id itype_disassembly( "disassembly" );
static const itype_id itype_plut_cell( "plut_cell" );
static const json_character_flag json_flag_HYPEROPIC( "HYPEROPIC" );
static const limb_score_id limb_score_manip( "manip" );
static const quality_id qual_BOIL( "BOIL" );
static const skill_id skill_electronics( "electronics" );
static const skill_id skill_tailor( "tailor" );
static const string_id<struct furn_t> furn_f_fake_bench_hands( "f_fake_bench_hands" );
static const string_id<struct furn_t> furn_f_ground_crafting_spot( "f_ground_crafting_spot" );
static const trait_id trait_BURROW( "BURROW" );
static const trait_id trait_BURROWLARGE( "BURROWLARGE" );
static const trait_id trait_DEBUG_CNF( "DEBUG_CNF" );
static const trait_id trait_DEBUG_HS( "DEBUG_HS" );
static const std::string flag_BLIND_EASY( "BLIND_EASY" );
static const std::string flag_BLIND_HARD( "BLIND_HARD" );
static const std::string flag_FULL_MAGAZINE( "FULL_MAGAZINE" );
static const std::string flag_NO_RESIZE( "NO_RESIZE" );
static const std::string flag_UNCRAFT_BY_QUANTITY( "UNCRAFT_BY_QUANTITY" );
static const std::string flag_UNCRAFT_LIQUIDS_CONTAINED( "UNCRAFT_LIQUIDS_CONTAINED" );
static const std::string flag_UNCRAFT_SINGLE_CHARGE( "UNCRAFT_SINGLE_CHARGE" );
class basecamp;
static bool crafting_allowed( const Character &p, const recipe &rec )
{
if( p.morale_crafting_speed_multiplier( rec ) <= 0.0f ) {
add_msg( m_info, _( "Your morale is too low to craft such a difficult thing…" ) );
return false;
}
if( p.lighting_craft_speed_multiplier( rec ) <= 0.0f ) {
add_msg( m_info, _( "You can't see to craft!" ) );
return false;
}
if( rec.category == "CC_BUILDING" ) {
add_msg( m_info, _( "Overmap terrain building recipes are not implemented yet!" ) );
return false;
}
return true;
}
float Character::lighting_craft_speed_multiplier( const recipe &rec ) const
{
// negative is bright, 0 is just bright enough, positive is dark, +7.0f is pitch black
float darkness = fine_detail_vision_mod() - 4.0f;
if( darkness <= 0.0f ) {
return 1.0f; // it's bright, go for it
}
bool rec_blind = rec.has_flag( flag_BLIND_HARD ) || rec.has_flag( flag_BLIND_EASY );
if( darkness > 0 && !rec_blind ) {
return 0.0f; // it's dark and this recipe can't be crafted in the dark
}
if( rec.has_flag( flag_BLIND_EASY ) ) {
// 100% speed in well lit area at skill+0
// 25% speed in pitch black at skill+0
// skill+2 removes speed penalty
return 1.0f - ( darkness / ( 7.0f / 0.75f ) ) * std::max( 0,
2 - exceeds_recipe_requirements( rec ) ) / 2.0f;
}
if( rec.has_flag( flag_BLIND_HARD ) && exceeds_recipe_requirements( rec ) >= 2 ) {
// 100% speed in well lit area at skill+2
// 25% speed in pitch black at skill+2
// skill+8 removes speed penalty
return 1.0f - ( darkness / ( 7.0f / 0.75f ) ) * std::max( 0,
8 - exceeds_recipe_requirements( rec ) ) / 6.0f;
}
return 0.0f; // it's dark and you could craft this if you had more skill
}
float Character::morale_crafting_speed_multiplier( const recipe &rec ) const
{
int morale = get_morale_level();
if( morale >= 0 ) {
// No bonus for being happy yet
return 1.0f;
}
// Harder jobs are more frustrating, even when skilled
// For each skill where skill=difficulty, multiply effective morale by 200%
float morale_mult = std::max( 1.0f, 2.0f * rec.difficulty / std::max( 1,
get_skill_level( rec.skill_used ) ) );
for( const auto &pr : rec.required_skills ) {
morale_mult *= std::max( 1.0f, 2.0f * pr.second / std::max( 1, get_skill_level( pr.first ) ) );
}
// Halve speed at -50 effective morale, quarter at -150
float morale_effect = 1.0f + ( morale_mult * morale ) / -50.0f;
return 1.0f / morale_effect;
}
template<typename T>
static float lerped_multiplier( const T &value, const T &low, const T &high )
{
// No effect if less than allowed value
if( value <= low ) {
return 1.0f;
}
// Bottom out at 25% speed
if( value >= high ) {
return 0.25f;
}
// Linear interpolation between high and low
// y = y0 + ( x - x0 ) * ( y1 - y0 ) / ( x1 - x0 )
return 1.0f + ( value - low ) * ( 0.25f - 1.0f ) / ( high - low );
}
static float workbench_crafting_speed_multiplier( const item &craft,
const cata::optional<tripoint> &loc )
{
float multiplier;
units::mass allowed_mass;
units::volume allowed_volume;
map &here = get_map();
if( !loc ) {
// cata::nullopt indicates crafting from inventory
// Use values from f_fake_bench_hands
const furn_t &f = furn_f_fake_bench_hands.obj();
multiplier = f.workbench->multiplier;
allowed_mass = f.workbench->allowed_mass;
allowed_volume = f.workbench->allowed_volume;
} else if( const cata::optional<vpart_reference> vp = here.veh_at(
*loc ).part_with_feature( "WORKBENCH", true ) ) {
// Vehicle workbench
const vpart_info &vp_info = vp->part().info();
if( const cata::optional<vpslot_workbench> &wb_info = vp_info.get_workbench_info() ) {
multiplier = wb_info->multiplier;
allowed_mass = wb_info->allowed_mass;
allowed_volume = wb_info->allowed_volume;
} else {
debugmsg( "part '%S' with WORKBENCH flag has no workbench info", vp->part().name() );
return 0.0f;
}
} else if( here.furn( *loc ).obj().workbench ) {
// Furniture workbench
const furn_t &f = here.furn( *loc ).obj();
multiplier = f.workbench->multiplier;
allowed_mass = f.workbench->allowed_mass;
allowed_volume = f.workbench->allowed_volume;
} else {
// Ground
const furn_t &f = furn_f_ground_crafting_spot.obj();
multiplier = f.workbench->multiplier;
allowed_mass = f.workbench->allowed_mass;
allowed_volume = f.workbench->allowed_volume;
}
const units::mass &craft_mass = craft.weight();
const units::volume &craft_volume = craft.volume();
multiplier *= lerped_multiplier( craft_mass, allowed_mass, 1000_kilogram );
multiplier *= lerped_multiplier( craft_volume, allowed_volume, 1000_liter );
return multiplier;
}
float Character::crafting_speed_multiplier( const recipe &rec, bool in_progress ) const
{
const float result = morale_crafting_speed_multiplier( rec ) *
lighting_craft_speed_multiplier( rec ) *
get_limb_score( limb_score_manip );
// Can't start if we'd need 300% time, but we can still finish the job
if( !in_progress && result < 0.33f ) {
return 0.0f;
}
// If we're working below 10% speed, just give up
if( result < 0.1f ) {
return 0.0f;
}
return result;
}
float Character::crafting_speed_multiplier( const item &craft,
const cata::optional<tripoint> &loc ) const
{
if( !craft.is_craft() ) {
debugmsg( "Can't calculate crafting speed multiplier of non-craft '%s'", craft.tname() );
return 1.0f;
}
const recipe &rec = craft.get_making();
const float light_multi = lighting_craft_speed_multiplier( rec );
const float bench_multi = workbench_crafting_speed_multiplier( craft, loc );
const float morale_multi = morale_crafting_speed_multiplier( rec );
const float mut_multi = mutation_value( "crafting_speed_multiplier" );
const float total_multi = light_multi * bench_multi * morale_multi * mut_multi *
get_limb_score( limb_score_manip );
if( light_multi <= 0.0f ) {
add_msg_if_player( m_bad, _( "You can no longer see well enough to keep crafting." ) );
return 0.0f;
}
if( bench_multi <= 0.1f || ( bench_multi <= 0.33f && total_multi <= 0.2f ) ) {
add_msg_if_player( m_bad, _( "The %s is too large and/or heavy to work on. You may want to"
" use a workbench or a smaller batch size" ), craft.tname() );
return 0.0f;
}
if( morale_multi <= 0.2f || ( morale_multi <= 0.33f && total_multi <= 0.2f ) ) {
add_msg_if_player( m_bad, _( "Your morale is too low to continue crafting." ) );
return 0.0f;
}
// If we're working below 20% speed, just give up
if( total_multi <= 0.2f ) {
add_msg_if_player( m_bad, _( "You are too frustrated to continue and just give up." ) );
return 0.0f;
}
if( calendar::once_every( 1_hours ) && total_multi < 0.75f ) {
if( light_multi <= 0.5f ) {
add_msg_if_player( m_bad, _( "You can't see well and are working slowly." ) );
}
if( bench_multi <= 0.5f ) {
add_msg_if_player( m_bad,
_( "The %s is too large and/or heavy to work on comfortably. You are"
" working slowly." ), craft.tname() );
}
if( morale_multi <= 0.5f ) {
add_msg_if_player( m_bad, _( "You can't focus and are working slowly." ) );
}
}
return total_multi;
}
bool Character::has_morale_to_craft() const
{
return get_morale_level() >= -50;
}
void Character::craft( const cata::optional<tripoint> &loc, const recipe_id &goto_recipe )
{
int batch_size = 0;
const recipe *rec = select_crafting_recipe( batch_size, goto_recipe );
if( rec ) {
if( crafting_allowed( *this, *rec ) ) {
make_craft( rec->ident(), batch_size, loc );
}
}
}
void Character::recraft( const cata::optional<tripoint> &loc )
{
if( lastrecipe.str().empty() ) {
popup( _( "Craft something first" ) );
} else if( making_would_work( lastrecipe, last_batch ) ) {
last_craft->execute( loc );
}
}
void Character::long_craft( const cata::optional<tripoint> &loc, const recipe_id &goto_recipe )
{
int batch_size = 0;
const recipe *rec = select_crafting_recipe( batch_size, goto_recipe );
if( rec ) {
if( crafting_allowed( *this, *rec ) ) {
make_all_craft( rec->ident(), batch_size, loc );
}
}
}
bool Character::making_would_work( const recipe_id &id_to_make, int batch_size )
{
const auto &making = *id_to_make;
if( !( making && crafting_allowed( *this, making ) ) ) {
return false;
}
if( !can_make( &making, batch_size ) ) {
std::string buffer = _( "You can no longer make that craft!" );
buffer += "\n";
buffer += making.simple_requirements().list_missing();
popup( buffer, PF_NONE );
return false;
}
return check_eligible_containers_for_crafting( making, batch_size );
}
int Character::available_assistant_count( const recipe &rec ) const
// NPCs around you should assist in batch production if they have the skills
{
if( rec.is_practice() ) {
return 0;
}
// TODO: Cache them in activity, include them in modifier calculations
const auto helpers = get_crafting_helpers();
return std::count_if( helpers.begin(), helpers.end(),
[&]( const npc * np ) {
return np->get_skill_level( rec.skill_used ) >= rec.difficulty;
} );
}
int64_t Character::expected_time_to_craft( const recipe &rec, int batch_size ) const
{
const size_t assistants = available_assistant_count( rec );
float modifier = crafting_speed_multiplier( rec );
return rec.batch_time( *this, batch_size, modifier, assistants );
}
bool Character::check_eligible_containers_for_crafting( const recipe &rec, int batch_size ) const
{
std::vector<const item *> conts = get_eligible_containers_for_crafting();
const std::vector<item> res = rec.create_results( batch_size );
const std::vector<item> bps = rec.create_byproducts( batch_size );
std::vector<item> all;
all.reserve( res.size() + bps.size() );
all.insert( all.end(), res.begin(), res.end() );
all.insert( all.end(), bps.begin(), bps.end() );
map &here = get_map();
for( const item &prod : all ) {
if( !prod.made_of( phase_id::LIQUID ) ) {
continue;
}
// we go through half-filled containers first, then go through empty containers if we need
std::sort( conts.begin(), conts.end(), item_ptr_compare_by_charges );
int charges_to_store = prod.charges;
for( const item *cont : conts ) {
if( charges_to_store <= 0 ) {
break;
}
charges_to_store -= cont->get_remaining_capacity_for_liquid( prod, true );
}
// also check if we're currently in a vehicle that has the necessary storage
if( charges_to_store > 0 ) {
if( optional_vpart_position vp = here.veh_at( pos() ) ) {
const itype_id &ftype = prod.typeId();
int fuel_cap = vp->vehicle().fuel_capacity( ftype );
int fuel_amnt = vp->vehicle().fuel_left( ftype );
if( fuel_cap >= 0 ) {
int fuel_space_left = fuel_cap - fuel_amnt;
charges_to_store -= fuel_space_left;
}
}
}
if( charges_to_store > 0 ) {
if( !query_yn(
_( "You don't have anything in which to store %s and may have to pour it out as soon as it is prepared! Proceed?" ),
prod.tname() ) ) {
return false;
}
}
}
return true;
}
static bool is_container_eligible_for_crafting( const item &cont, bool allow_bucket )
{
if( cont.is_watertight_container() && cont.num_item_stacks() <= 1 && ( allow_bucket ||
!cont.will_spill() ) ) {
return !cont.is_container_full( allow_bucket );
}
return false;
}
static std::vector<const item *> get_eligible_containers_recursive( const item &cont,
bool allow_bucket )
{
std::vector<const item *> ret;
if( is_container_eligible_for_crafting( cont, allow_bucket ) ) {
ret.push_back( &cont );
}
for( const item *it : cont.all_items_top( item_pocket::pocket_type::CONTAINER ) ) {
//buckets are never allowed when inside another container
std::vector<const item *> inside = get_eligible_containers_recursive( *it, false );
ret.insert( ret.end(), inside.begin(), inside.end() );
}
return ret;
}
std::vector<const item *> Character::get_eligible_containers_for_crafting() const
{
std::vector<const item *> conts;
const item &weapon = get_wielded_item();
conts = get_eligible_containers_recursive( weapon, true );
for( const auto &it : worn ) {
std::vector<const item *> eligible = get_eligible_containers_recursive( it, false );
conts.insert( conts.begin(), eligible.begin(), eligible.end() );
}
map &here = get_map();
// get all potential containers within PICKUP_RANGE tiles including vehicles
for( const tripoint &loc : closest_points_first( pos(), PICKUP_RANGE ) ) {
// can not reach this -> can not access its contents
if( pos() != loc && !here.clear_path( pos(), loc, PICKUP_RANGE, 1, 100 ) ) {
continue;
}
if( here.accessible_items( loc ) ) {
for( const item &it : here.i_at( loc ) ) {
std::vector<const item *> eligible = get_eligible_containers_recursive( it, true );
conts.insert( conts.begin(), eligible.begin(), eligible.end() );
}
}
if( const cata::optional<vpart_reference> vp = here.veh_at( loc ).part_with_feature( "CARGO",
true ) ) {
for( const auto &it : vp->vehicle().get_items( vp->part_index() ) ) {
std::vector<const item *> eligible = get_eligible_containers_recursive( it, true );
conts.insert( conts.begin(), eligible.begin(), eligible.end() );
}
}
}
return conts;
}
bool Character::can_make( const recipe *r, int batch_size )
{
const inventory &crafting_inv = crafting_inventory( r );
if( !has_recipe( r, crafting_inv, get_crafting_helpers() ) ) {
return false;
}
if( !r->character_has_required_proficiencies( *this ) ) {
return false;
}
return r->deduped_requirements().can_make_with_inventory(
crafting_inv, r->get_component_filter(), batch_size );
}
bool Character::can_start_craft( const recipe *rec, recipe_filter_flags flags, int batch_size )
{
if( !rec ) {
return false;
}
if( !rec->character_has_required_proficiencies( *this ) ) {
return false;
}
const inventory &inv = crafting_inventory();
return rec->deduped_requirements().can_make_with_inventory(
inv, rec->get_component_filter( flags ), batch_size, craft_flags::start_only );
}
const inventory &Character::crafting_inventory( bool clear_path ) const
{
return crafting_inventory( tripoint_zero, PICKUP_RANGE, clear_path );
}
const inventory &Character::crafting_inventory( const tripoint &src_pos, int radius,
bool clear_path ) const
{
tripoint inv_pos = src_pos;
if( src_pos == tripoint_zero ) {
inv_pos = pos();
}
if( moves == crafting_cache.moves
&& radius == crafting_cache.radius
&& calendar::turn == crafting_cache.time
&& inv_pos == crafting_cache.position ) {
return *crafting_cache.crafting_inventory;
}
crafting_cache.crafting_inventory->clear();
if( radius >= 0 ) {
crafting_cache.crafting_inventory->form_from_map( inv_pos, radius, this, false, clear_path );
}
// TODO: Add a const overload of all_items_loc() that returns something like
// vector<const_item_location> in order to get rid of the const_cast here.
for( const item_location &it : const_cast<Character *>( this )->all_items_loc() ) {
// add containers separately from their contents
if( !it->empty_container() ) {
// is the non-empty container used for BOIL?
if( !it->is_watertight_container() || it->get_raw_quality( qual_BOIL ) <= 0 ) {
*crafting_cache.crafting_inventory += item( it->typeId(), it->birthday() );
}
continue;
}
crafting_cache.crafting_inventory->add_item( *it );
}
for( const item *i : get_pseudo_items() ) {
*crafting_cache.crafting_inventory += *i;
}
if( has_trait( trait_BURROW ) || has_trait( trait_BURROWLARGE ) ) {
*crafting_cache.crafting_inventory += item( "pickaxe", calendar::turn );
*crafting_cache.crafting_inventory += item( "shovel", calendar::turn );
}
crafting_cache.moves = moves;
crafting_cache.time = calendar::turn;
crafting_cache.position = inv_pos;
crafting_cache.radius = radius;
return *crafting_cache.crafting_inventory;
}
void Character::invalidate_crafting_inventory()
{
crafting_cache.time = calendar::before_time_starts;
}
void Character::make_craft( const recipe_id &id_to_make, int batch_size,
const cata::optional<tripoint> &loc )
{
make_craft_with_command( id_to_make, batch_size, false, loc );
}
void Character::make_all_craft( const recipe_id &id_to_make, int batch_size,
const cata::optional<tripoint> &loc )
{
make_craft_with_command( id_to_make, batch_size, true, loc );
}
void Character::make_craft_with_command( const recipe_id &id_to_make, int batch_size, bool is_long,
const cata::optional<tripoint> &loc )
{
const auto &recipe_to_make = *id_to_make;
if( !recipe_to_make ) {
return;
}
*last_craft = craft_command( &recipe_to_make, batch_size, is_long, this, loc );
last_craft->execute();
}
// @param offset is the index of the created item in the range [0, batch_size-1],
// it makes sure that the used items are distributed equally among the new items.
static void set_components( std::list<item> &components, const std::list<item> &used,
const int batch_size, const size_t offset )
{
if( batch_size <= 1 ) {
components.insert( components.begin(), used.begin(), used.end() );
return;
}
// This count does *not* include items counted by charges!
size_t non_charges_counter = 0;
for( const item &tmp : used ) {
if( tmp.count_by_charges() ) {
components.push_back( tmp );
// This assumes all (count-by-charges) items of the same type have been merged into one,
// which has a charges value that can be evenly divided by batch_size.
components.back().charges = tmp.charges / batch_size;
} else {
if( ( non_charges_counter + offset ) % batch_size == 0 ) {
components.push_back( tmp );
}
non_charges_counter++;
}
}
}
static cata::optional<item_location> wield_craft( Character &p, item &craft )
{
item &weapon = p.get_wielded_item();
if( p.wield( craft ) ) {
if( weapon.invlet ) {
p.add_msg_if_player( m_info, _( "Wielding %c - %s" ), weapon.invlet, weapon.display_name() );
} else {
p.add_msg_if_player( m_info, _( "Wielding - %s" ), weapon.display_name() );
}
return item_location( p, &weapon );
}
return cata::nullopt;
}
static item *set_item_inventory( Character &p, item &newit )
{
item *ret_val = nullptr;
if( newit.made_of( phase_id::LIQUID ) ) {
liquid_handler::handle_all_liquid( newit, PICKUP_RANGE );
} else {
p.inv->assign_empty_invlet( newit, p );
// We might not have space for the item
if( !p.can_pickVolume( newit ) ) { //Accounts for result_mult
put_into_vehicle_or_drop( p, item_drop_reason::too_large, { newit } );
} else if( !p.can_pickWeight( newit, !get_option<bool>( "DANGEROUS_PICKUPS" ) ) ) {
put_into_vehicle_or_drop( p, item_drop_reason::too_heavy, { newit } );
} else {
ret_val = &p.i_add( newit );
add_msg( m_info, "%c - %s", ret_val->invlet == 0 ? ' ' : ret_val->invlet,
ret_val->tname() );
}
}
return ret_val;
}
/**
* Helper for @ref set_item_map_or_vehicle
* This is needed to still get a valid item_location if overflow occurs
*/
static item_location set_item_map( const tripoint &loc, item &newit )
{
// Includes loc
for( const tripoint &tile : closest_points_first( loc, 2 ) ) {
// Pass false to disallow overflow, null_item_reference indicates failure.
item *it_on_map = &get_map().add_item_or_charges( tile, newit, false );
if( it_on_map != &null_item_reference() ) {
return item_location( map_cursor( tile ), it_on_map );
}
}
debugmsg( "Could not place %s on map near (%d, %d, %d)", newit.tname(), loc.x, loc.y, loc.z );
return item_location();
}
/**
* Set an item on the map or in a vehicle and return the new location
*/
static item_location set_item_map_or_vehicle( const Character &p, const tripoint &loc, item &newit )
{
map &here = get_map();
if( const cata::optional<vpart_reference> vp = here.veh_at( loc ).part_with_feature( "CARGO",
false ) ) {
if( const cata::optional<vehicle_stack::iterator> it = vp->vehicle().add_item( vp->part_index(),
newit ) ) {
p.add_msg_player_or_npc(
pgettext( "item, furniture", "You put the %1$s on the %2$s." ),
pgettext( "item, furniture", "<npcname> puts the %1$s on the %2$s." ),
( *it )->tname(), vp->part().name() );
return item_location( vehicle_cursor( vp->vehicle(), vp->part_index() ), & **it );
}
// Couldn't add the in progress craft to the target part, so drop it to the map.
p.add_msg_player_or_npc(
pgettext( "furniture, item", "Not enough space on the %s. You drop the %s on the ground." ),
pgettext( "furniture, item", "Not enough space on the %s. <npcname> drops the %s on the ground." ),
vp->part().name(), newit.tname() );
return set_item_map( loc, newit );
} else {
if( here.has_furn( loc ) ) {
const furn_t &workbench = here.furn( loc ).obj();
p.add_msg_player_or_npc(
pgettext( "item, furniture", "You put the %1$s on the %2$s." ),
pgettext( "item, furniture", "<npcname> puts the %1$s on the %2$s." ),
newit.tname(), workbench.name() );
} else {
p.add_msg_player_or_npc(
pgettext( "item", "You put the %s on the ground." ),
pgettext( "item", "<npcname> puts the %s on the ground." ),
newit.tname() );
}
return set_item_map( loc, newit );
}
}
static item_location place_craft_or_disassembly(
Character &ch, item &craft, cata::optional<tripoint> target )
{
item_location craft_in_world;
// Check if we are standing next to a workbench. If so, just use that.
float best_bench_multi = 0.0f;
map &here = get_map();
for( const tripoint &adj : here.points_in_radius( ch.pos(), 1 ) ) {
if( here.dangerous_field_at( adj ) ) {
continue;
}
if( const cata::value_ptr<furn_workbench_info> &wb = here.furn( adj ).obj().workbench ) {
if( wb->multiplier > best_bench_multi ) {
best_bench_multi = wb->multiplier;
target = adj;
}
} else if( const cata::optional<vpart_reference> vp = here.veh_at(
adj ).part_with_feature( "WORKBENCH", true ) ) {
if( const cata::optional<vpslot_workbench> &wb_info = vp->part().info().get_workbench_info() ) {
if( wb_info->multiplier > best_bench_multi ) {
best_bench_multi = wb_info->multiplier;
target = adj;
}
} else {
debugmsg( "part '%s' with WORKBENCH flag has no workbench info", vp->part().name() );
}
}
}
// Crafting without a workbench
if( !target ) {
if( !ch.has_two_arms_lifting() || ( ch.is_npc() && ch.has_wield_conflicts( craft ) ) ) {
craft_in_world = set_item_map_or_vehicle( ch, ch.pos(), craft );
} else if( !ch.has_wield_conflicts( craft ) ) {
if( cata::optional<item_location> it_loc = wield_craft( ch, craft ) ) {
craft_in_world = *it_loc;
} else {
// This almost certianly shouldn't happen
put_into_vehicle_or_drop( ch, item_drop_reason::tumbling, {craft} );
}
} else {
enum option : int {
WIELD_CRAFT = 0,
DROP_CRAFT,
STASH,
DROP
};
uilist amenu;
amenu.text = string_format( pgettext( "in progress craft", "What to do with the %s?" ),
craft.display_name() );
amenu.addentry( WIELD_CRAFT, ch.can_unwield( ch.get_wielded_item() ).success(),
'1', _( "Dispose of your wielded %s and start working." ), ch.get_wielded_item().tname() );
amenu.addentry( DROP_CRAFT, true, '2', _( "Put it down and start working." ) );
const bool can_stash = ch.can_pickVolume( craft ) &&
ch.can_pickWeight( craft, !get_option<bool>( "DANGEROUS_PICKUPS" ) );
amenu.addentry( STASH, can_stash, '3', _( "Store it in your inventory." ) );
amenu.addentry( DROP, true, '4', _( "Drop it on the ground." ) );
amenu.query();
const option choice = amenu.ret == UILIST_CANCEL ? DROP : static_cast<option>( amenu.ret );
switch( choice ) {
case WIELD_CRAFT: {
if( cata::optional<item_location> it_loc = wield_craft( ch, craft ) ) {
craft_in_world = *it_loc;
} else {
// This almost certainly shouldn't happen
put_into_vehicle_or_drop( ch, item_drop_reason::tumbling, {craft} );
}
break;
}
case DROP_CRAFT: {
craft_in_world = set_item_map_or_vehicle( ch, ch.pos(), craft );
break;
}
case STASH: {
set_item_inventory( ch, craft );
break;
}
case DROP: {
put_into_vehicle_or_drop( ch, item_drop_reason::deliberate, {craft} );
break;
}
}
}
} else {
// We have a workbench, put the item there.
craft_in_world = set_item_map_or_vehicle( ch, *target, craft );
}
if( !craft_in_world ) {
ch.add_msg_if_player( _( "Wield and activate the %s to start working." ), craft.tname() );
}
return craft_in_world;
}
void Character::start_craft( craft_command &command, const cata::optional<tripoint> &loc )
{
if( command.empty() ) {
debugmsg( "Attempted to start craft with empty command" );
return;
}
item craft = command.create_in_progress_craft();
if( craft.is_null() ) {
return;
}
const recipe &making = craft.get_making();
if( get_skill_level( command.get_skill_id() ) > making.get_skill_cap() ) {
handle_skill_warning( command.get_skill_id(), true );
}
// In case we were wearing something just consumed
if( !craft.components.empty() ) {
calc_encumbrance();
}
item_location craft_in_world = place_craft_or_disassembly( *this, craft, loc );
if( !craft_in_world ) {
return;
}
assign_activity( player_activity( craft_activity_actor( craft_in_world, command.is_long() ) ) );
add_msg_player_or_npc(
pgettext( "in progress craft", "You start working on the %s." ),
pgettext( "in progress craft", "<npcname> starts working on the %s." ),
craft.tname() );
}
bool Character::craft_skill_gain( const item &craft, const int &num_practice_ticks )
{
if( !craft.is_craft() ) {
debugmsg( "craft_skill_check() called on non-craft '%s.' Aborting.", craft.tname() );
return false;
}
const recipe &making = craft.get_making();
if( !making.skill_used ) {
return false;
}
const int skill_cap = making.get_skill_cap();
const int batch_size = craft.charges;
// NPCs assisting or watching should gain experience...
for( npc *helper : get_crafting_helpers() ) {
// If the NPC can understand what you are doing, they gain more exp
if( helper->get_skill_level( making.skill_used ) >= making.difficulty ) {
helper->practice( making.skill_used, roll_remainder( num_practice_ticks / 2.0 ),
skill_cap );
if( batch_size > 1 && one_in( 300 ) ) {
add_msg( m_info, _( "%s assists with crafting…" ), helper->get_name() );
}
if( batch_size == 1 && one_in( 300 ) ) {
add_msg( m_info, _( "%s could assist you with a batch…" ), helper->get_name() );
}
} else {
helper->practice( making.skill_used, roll_remainder( num_practice_ticks / 10.0 ),
skill_cap );
if( one_in( 300 ) ) {
add_msg( m_info, _( "%s watches you craft…" ), helper->get_name() );
}
}
}
return practice( making.skill_used, num_practice_ticks, skill_cap, true );
}
bool Character::craft_proficiency_gain( const item &craft, const time_duration &time )
{
if( !craft.is_craft() ) {
debugmsg( "craft_proficiency_gain() called on non-craft %s", craft.tname() );
return false;
}
const recipe &making = craft.get_making();
struct learn_subject {
proficiency_id proficiency;
float time_multiplier;
cata::optional<time_duration> max_experience;
};
const std::vector<npc *> helpers = get_crafting_helpers();
std::vector<Character *> all_crafters{ helpers.begin(), helpers.end() };
all_crafters.push_back( this );
bool player_gained_prof = false;
for( Character *p : all_crafters ) {
std::vector<learn_subject> subjects;
for( const recipe_proficiency &prof : making.proficiencies ) {
if( !p->_proficiencies->has_learned( prof.id ) &&
prof.id->can_learn() &&
p->_proficiencies->has_prereqs( prof.id ) ) {
learn_subject subject{
prof.id,
prof.learning_time_mult / prof.time_multiplier,
prof.max_experience
};
subjects.push_back( subject );
}
}
if( subjects.empty() ) {
player_gained_prof = false;
continue;
}
const time_duration learn_time = time / subjects.size();
bool gained_prof = false;
for( const learn_subject &subject : subjects ) {
int helper_bonus = 1;
for( const Character *other : all_crafters ) {
if( p != other && other->has_proficiency( subject.proficiency ) ) {
// Other characters who know the proficiency help you learn faster
helper_bonus = 2;
}
}
gained_prof |= p->practice_proficiency( subject.proficiency,
subject.time_multiplier * learn_time * helper_bonus,
subject.max_experience );
}
// This depends on the player being the last element in the iteration
player_gained_prof = gained_prof;
}
return player_gained_prof;
}
double Character::crafting_success_roll( const recipe &making ) const
{
if( has_trait( trait_DEBUG_CNF ) ) {
return 1.0;
}
int secondary_dice = 0;
int secondary_difficulty = 0;
for( const auto &pr : making.required_skills ) {
secondary_dice += get_skill_level( pr.first );
secondary_difficulty += pr.second;
}
// # of dice is 75% primary skill, 25% secondary (unless secondary is null)
int skill_dice;
if( secondary_difficulty > 0 ) {
skill_dice = get_skill_level( making.skill_used ) * 3 + secondary_dice;
} else {