forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clzones.cpp
1267 lines (1109 loc) · 39.7 KB
/
clzones.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 "clzones.h"
#include <algorithm>
#include <climits>
#include <iosfwd>
#include <iterator>
#include <string>
#include <tuple>
#include "avatar.h"
#include "cata_utility.h"
#include "construction.h"
#include "construction_group.h"
#include "cursesdef.h"
#include "debug.h"
#include "faction.h"
#include "fstream_utils.h"
#include "game.h"
#include "generic_factory.h"
#include "iexamine.h"
#include "int_id.h"
#include "item.h"
#include "item_category.h"
#include "item_search.h"
#include "itype.h"
#include "json.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "memory_fast.h"
#include "output.h"
#include "player.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "ui.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vpart_position.h"
static const std::string flag_FIREWOOD( "FIREWOOD" );
static const item_category_id itcat_food( "food" );
static const zone_type_id zone_CAMP_FOOD( "CAMP_FOOD" );
static const zone_type_id zone_CONSTRUCTION_BLUEPRINT( "CONSTRUCTION_BLUEPRINT" );
static const zone_type_id zone_FARM_PLOT( "FARM_PLOT" );
static const zone_type_id zone_LOOT_CORPSE( "LOOT_CORPSE" );
static const zone_type_id zone_LOOT_CUSTOM( "LOOT_CUSTOM" );
static const zone_type_id zone_LOOT_DRINK( "LOOT_DRINK" );
static const zone_type_id zone_LOOT_FOOD( "LOOT_FOOD" );
static const zone_type_id zone_LOOT_IGNORE( "LOOT_IGNORE" );
static const zone_type_id zone_LOOT_PDRINK( "LOOT_PDRINK" );
static const zone_type_id zone_LOOT_PFOOD( "LOOT_PFOOD" );
static const zone_type_id zone_LOOT_SEEDS( "LOOT_SEEDS" );
static const zone_type_id zone_LOOT_UNSORTED( "LOOT_UNSORTED" );
static const zone_type_id zone_LOOT_WOOD( "LOOT_WOOD" );
static const zone_type_id zone_NO_AUTO_PICKUP( "NO_AUTO_PICKUP" );
static const zone_type_id zone_NO_NPC_PICKUP( "NO_NPC_PICKUP" );
zone_manager::zone_manager()
{
types.emplace( zone_NO_AUTO_PICKUP,
zone_type( translate_marker( "No Auto Pickup" ),
translate_marker( "You won't auto-pickup items inside the zone." ) ) );
types.emplace( zone_NO_NPC_PICKUP,
zone_type( translate_marker( "No NPC Pickup" ),
translate_marker( "Friendly NPCs don't pickup items inside the zone." ) ) );
types.emplace( zone_type_id( "NPC_RETREAT" ),
zone_type( translate_marker( "NPC Retreat" ),
translate_marker( "When fleeing, friendly NPCs will attempt to retreat toward this zone if it is within 60 tiles." ) ) );
types.emplace( zone_type_id( "NPC_NO_INVESTIGATE" ),
zone_type( translate_marker( "NPC Ignore Sounds" ),
translate_marker( "Friendly NPCs won't investigate unseen sounds coming from this zone." ) ) );
types.emplace( zone_type_id( "NPC_INVESTIGATE_ONLY" ),
zone_type( translate_marker( "NPC Investigation Area" ),
translate_marker( "Friendly NPCs will investigate unseen sounds only if they come from inside this area." ) ) );
for( const zone_type &zone : zone_type::get_all() ) {
types.emplace( zone.id, zone );
}
types.emplace( zone_type_id( "SOURCE_FIREWOOD" ),
zone_type( translate_marker( "Source: Firewood" ),
translate_marker( "Source for firewood or other flammable materials in this zone may be used to automatically refuel fires. "
"This will be done to maintain light during long-running tasks such as crafting, reading or waiting." ) ) );
types.emplace( zone_CONSTRUCTION_BLUEPRINT,
zone_type( translate_marker( "Construction: Blueprint" ),
translate_marker( "Designate a blueprint zone for construction." ) ) );
types.emplace( zone_FARM_PLOT,
zone_type( translate_marker( "Farm: Plot" ),
translate_marker( "Designate a farm plot for tilling and planting." ) ) );
types.emplace( zone_type_id( "CHOP_TREES" ),
zone_type( translate_marker( "Chop Trees" ),
translate_marker( "Designate an area to chop down trees." ) ) );
types.emplace( zone_type_id( "FISHING_SPOT" ),
zone_type( translate_marker( "Fishing Spot" ),
translate_marker( "Designate an area to fish from." ) ) );
types.emplace( zone_type_id( "MINING" ),
zone_type( translate_marker( "Mine Terrain" ),
translate_marker( "Designate an area to mine." ) ) );
types.emplace( zone_type_id( "VEHICLE_DECONSTRUCT" ),
zone_type( translate_marker( "Vehicle Deconstruct Zone" ),
translate_marker( "Any vehicles in this area are marked for deconstruction." ) ) );
types.emplace( zone_type_id( "VEHICLE_REPAIR" ),
zone_type( translate_marker( "Vehicle Repair Zone" ),
translate_marker( "Any vehicles in this area are marked for repair work." ) ) );
types.emplace( zone_type_id( "VEHICLE_PATROL" ),
zone_type( translate_marker( "Vehicle Patrol Zone" ),
translate_marker( "Vehicles with an autopilot will patrol in this zone." ) ) );
types.emplace( zone_type_id( "CAMP_STORAGE" ),
zone_type( translate_marker( "Basecamp: Storage" ),
translate_marker( "Items in this zone will be added to a basecamp's inventory for use by it's workers." ) ) );
types.emplace( zone_CAMP_FOOD,
zone_type( translate_marker( "Basecamp: Food" ),
translate_marker( "Items in this zone will be added to a basecamp's food supply in the Distribute Food mission." ) ) );
types.emplace( zone_type_id( "AUTO_EAT" ),
zone_type( translate_marker( "Auto Eat" ),
translate_marker( "Items in this zone will be automatically eaten during a long activity if you get hungry." ) ) );
types.emplace( zone_type_id( "AUTO_DRINK" ),
zone_type( translate_marker( "Auto Drink" ),
translate_marker( "Items in this zone will be automatically consumed during a long activity if you get thirsty." ) ) );
}
zone_manager &zone_manager::get_manager()
{
static zone_manager manager;
return manager;
}
void zone_manager::reset_manager()
{
get_manager() = zone_manager();
}
std::string zone_type::name() const
{
return _( name_ );
}
std::string zone_type::desc() const
{
return _( desc_ );
}
namespace
{
generic_factory<zone_type> zone_type_factory( "zone_type" );
} // namespace
template<>
const zone_type &string_id<zone_type>::obj() const
{
return zone_type_factory.obj( *this );
}
template<>
bool string_id<zone_type>::is_valid() const
{
return zone_type_factory.is_valid( *this );
}
const std::vector<zone_type> &zone_type::get_all()
{
return zone_type_factory.get_all();
}
void zone_type::load_zones( const JsonObject &jo, const std::string &src )
{
zone_type_factory.load( jo, src );
}
void zone_type::reset_zones()
{
zone_type_factory.reset();
}
void zone_type::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name_ );
mandatory( jo, was_loaded, "id", id );
optional( jo, was_loaded, "description", desc_, "" );
}
shared_ptr_fast<zone_options> zone_options::create( const zone_type_id &type )
{
if( type == zone_FARM_PLOT ) {
return make_shared_fast<plot_options>();
} else if( type == zone_CONSTRUCTION_BLUEPRINT ) {
return make_shared_fast<blueprint_options>();
} else if( type == zone_LOOT_CUSTOM ) {
return make_shared_fast<loot_options>();
}
return make_shared_fast<zone_options>();
}
bool zone_options::is_valid( const zone_type_id &type, const zone_options &options )
{
if( type == zone_FARM_PLOT ) {
return dynamic_cast<const plot_options *>( &options ) != nullptr;
} else if( type == zone_CONSTRUCTION_BLUEPRINT ) {
return dynamic_cast<const blueprint_options *>( &options ) != nullptr;
} else if( type == zone_LOOT_CUSTOM ) {
return dynamic_cast<const loot_options *>( &options ) != nullptr;
}
// ensure options is not derived class for the rest of zone types
return !options.has_options();
}
construction_id blueprint_options::get_final_construction(
const std::vector<construction_id> &list_constructions,
const construction_id &id,
std::set<construction_id> &skip_index
)
{
if( id->post_terrain.is_empty() && id->post_furniture.is_empty() ) {
return id;
}
for( const construction_id &c_id : list_constructions ) {
if( c_id == id || skip_index.find( c_id ) != skip_index.end() ) {
continue;
}
const construction &c = *c_id;
if( id->group == c.group &&
( id->post_terrain == c.pre_terrain || id->post_furniture == c.pre_furniture ) ) {
skip_index.insert( id );
return get_final_construction( list_constructions, c_id, skip_index );
}
}
return id;
}
blueprint_options::query_con_result blueprint_options::query_con()
{
std::optional<construction_id> con_index = construction_menu( true );
if( !con_index ) {
return canceled;
}
const std::vector<construction_id> &list_constructions = constructions::get_all_sorted();
std::set<construction_id> skip_index;
con_index = get_final_construction( list_constructions, *con_index, skip_index );
const construction &chosen = con_index->obj();
const construction_group_str_id &chosen_group = chosen.group;
const std::string &chosen_mark = chosen.post_terrain.is_empty() ?
chosen.post_furniture.str() : chosen.post_terrain.str();
if( *con_index != index || chosen_group != group || chosen_mark != mark ) {
group = chosen_group;
mark = chosen_mark;
index = *con_index;
return changed;
} else {
return successful;
}
}
loot_options::query_loot_result loot_options::query_loot()
{
int w_height = TERMY / 2;
const int w_width = TERMX / 2;
const int w_y0 = ( TERMY > w_height ) ? ( TERMY - w_height ) / 4 : 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 ) );
draw_item_filter_rules( w_con, 1, w_height - 1, item_filter_type::FILTER );
string_input_popup()
.title( _( "Filter:" ) )
.width( 55 )
.identifier( "item_filter" )
.max_length( 256 )
.edit( mark );
return changed;
}
plot_options::query_seed_result plot_options::query_seed()
{
player &p = g->u;
map &here = get_map();
std::vector<item *> seed_inv = p.items_with( []( const item & itm ) {
return itm.is_seed();
} );
auto &mgr = zone_manager::get_manager();
const std::unordered_set<tripoint> &zone_src_set = mgr.get_near( zone_LOOT_SEEDS,
here.getabs( p.pos() ), 60 );
for( const tripoint &elem : zone_src_set ) {
tripoint elem_loc = here.getlocal( elem );
for( item &it : here.i_at( elem_loc ) ) {
if( it.is_seed() ) {
seed_inv.push_back( &it );
}
}
}
std::vector<seed_tuple> seed_entries = iexamine::get_seed_entries( seed_inv );
seed_entries.emplace( seed_entries.begin(), seed_tuple( itype_id( "null" ), _( "No seed" ), 0 ) );
int seed_index = iexamine::query_seed( seed_entries );
if( seed_index > 0 && seed_index < static_cast<int>( seed_entries.size() ) ) {
const auto &seed_entry = seed_entries[seed_index];
const itype_id &new_seed = std::get<0>( seed_entry );
itype_id new_mark;
item it = item( new_seed );
if( it.is_seed() ) {
new_mark = it.type->seed->fruit_id;
} else {
new_mark = seed;
}
if( new_seed != seed || new_mark != mark ) {
seed = new_seed;
mark = new_mark;
return changed;
} else {
return successful;
}
} else if( seed_index == 0 ) {
// No seeds
if( !seed.is_empty() || !mark.is_empty() ) {
seed = itype_id();
mark = itype_id();
return changed;
} else {
return successful;
}
} else {
return canceled;
}
}
bool loot_options::query_at_creation()
{
return query_loot() != canceled;
}
bool loot_options::query()
{
return query_loot() == changed;
}
std::string loot_options::get_zone_name_suggestion() const
{
if( !mark.empty() ) {
return string_format( _( "Loot: Custom: %s" ), mark );
}
return _( "Loot: Custom: No Filter" );
}
std::vector<std::pair<std::string, std::string>> loot_options::get_descriptions() const
{
std::vector<std::pair<std::string, std::string>> options;
options.emplace_back( std::make_pair( _( "Loot: Custom: " ),
!mark.empty() ? mark : _( "No filter" ) ) );
return options;
}
void loot_options::serialize( JsonOut &json ) const
{
json.member( "mark", mark );
}
void loot_options::deserialize( const JsonObject &jo_zone )
{
jo_zone.read( "mark", mark );
}
bool blueprint_options::query_at_creation()
{
return query_con() != canceled;
}
bool plot_options::query_at_creation()
{
return query_seed() != canceled;
}
bool blueprint_options::query()
{
return query_con() == changed;
}
bool plot_options::query()
{
return query_seed() == changed;
}
std::string blueprint_options::get_zone_name_suggestion() const
{
if( group ) {
return group->name();
}
return _( "No construction" );
}
std::string plot_options::get_zone_name_suggestion() const
{
if( !seed.is_empty() ) {
auto type = itype_id( seed );
item it = item( type );
if( it.is_seed() ) {
return it.type->seed->plant_name.translated();
} else {
return item::nname( type );
}
}
return _( "No seed" );
}
std::vector<std::pair<std::string, std::string>> blueprint_options::get_descriptions() const
{
std::vector<std::pair<std::string, std::string>> options =
std::vector<std::pair<std::string, std::string>>();
options.emplace_back( std::make_pair( _( "Construct: " ),
group ? group->name() : _( "No Construction" ) ) );
return options;
}
std::vector<std::pair<std::string, std::string>> plot_options::get_descriptions() const
{
auto options = std::vector<std::pair<std::string, std::string>>();
options.emplace_back(
std::make_pair( _( "Plant seed: " ),
!seed.is_empty() ? item::nname( itype_id( seed ) ) : _( "No seed" ) ) );
return options;
}
void blueprint_options::serialize( JsonOut &json ) const
{
json.member( "mark", mark );
json.member( "group", group );
json.member( "index", index.id() );
}
void blueprint_options::deserialize( const JsonObject &jo_zone )
{
jo_zone.read( "mark", mark );
jo_zone.read( "group", group );
if( jo_zone.has_int( "index" ) ) {
// Oops, saved incorrectly as an int id by legacy code. Just load it and hope for the best
index = construction_id( jo_zone.get_int( "index" ) );
} else {
index = construction_str_id( jo_zone.get_string( "index" ) ).id();
}
}
void plot_options::serialize( JsonOut &json ) const
{
json.member( "mark", mark );
json.member( "seed", seed );
}
void plot_options::deserialize( const JsonObject &jo_zone )
{
jo_zone.read( "mark", mark );
jo_zone.read( "seed", seed );
}
std::optional<std::string> zone_manager::query_name( const std::string &default_name ) const
{
string_input_popup popup;
popup
.title( _( "Zone name:" ) )
.width( 55 )
.text( default_name )
.query();
if( popup.canceled() ) {
return {};
} else {
return popup.text();
}
}
std::optional<zone_type_id> zone_manager::query_type() const
{
const auto &types = get_manager().get_types();
std::vector<std::pair<zone_type_id, zone_type>> types_vec;
std::copy( types.begin(), types.end(),
std::back_inserter<std::vector<std::pair<zone_type_id, zone_type>>>( types_vec ) );
std::sort( types_vec.begin(), types_vec.end(),
[]( const std::pair<zone_type_id, zone_type> &lhs, const std::pair<zone_type_id, zone_type> &rhs ) {
return localized_compare( lhs.second.name(), rhs.second.name() );
} );
uilist as_m;
as_m.desc_enabled = true;
as_m.text = _( "Select zone type:" );
size_t i = 0;
for( const auto &pair : types_vec ) {
const auto &type = pair.second;
as_m.addentry_desc( i++, true, MENU_AUTOASSIGN, type.name(), type.desc() );
}
as_m.query();
if( as_m.ret < 0 ) {
return {};
}
size_t index = as_m.ret;
auto iter = types_vec.begin();
std::advance( iter, index );
return iter->first;
}
bool zone_data::set_name()
{
const auto maybe_name = zone_manager::get_manager().query_name( name );
if( maybe_name.has_value() ) {
auto new_name = maybe_name.value();
if( new_name.empty() ) {
new_name = _( "<no name>" );
}
if( name != new_name ) {
zone_manager::get_manager().zone_edited( *this );
name = new_name;
return true;
}
}
return false;
}
bool zone_data::set_type()
{
const auto maybe_type = zone_manager::get_manager().query_type();
if( maybe_type.has_value() && maybe_type.value() != type ) {
auto new_options = zone_options::create( maybe_type.value() );
if( new_options->query_at_creation() ) {
zone_manager::get_manager().zone_edited( *this );
type = maybe_type.value();
options = new_options;
zone_manager::get_manager().cache_data();
return true;
}
}
return false;
}
void zone_data::set_position( const std::pair<tripoint, tripoint> &position,
const bool manual )
{
if( is_vehicle && manual ) {
debugmsg( "Tried moving a lootzone bound to a vehicle part" );
return;
}
start = position.first;
end = position.second;
zone_manager::get_manager().cache_data();
}
void zone_data::set_enabled( const bool enabled_arg )
{
zone_manager::get_manager().zone_edited( *this );
enabled = enabled_arg;
}
void zone_data::set_is_vehicle( const bool is_vehicle_arg )
{
is_vehicle = is_vehicle_arg;
}
tripoint zone_data::get_center_point() const
{
return tripoint( ( start.x + end.x ) / 2, ( start.y + end.y ) / 2, ( start.z + end.z ) / 2 );
}
std::string zone_manager::get_name_from_type( const zone_type_id &type ) const
{
const auto &iter = types.find( type );
if( iter != types.end() ) {
return iter->second.name();
}
return "Unknown Type";
}
bool zone_manager::has_type( const zone_type_id &type ) const
{
return types.count( type ) > 0;
}
bool zone_manager::has_defined( const zone_type_id &type, const faction_id &fac ) const
{
const auto &type_iter = area_cache.find( zone_data::make_type_hash( type, fac ) );
return type_iter != area_cache.end();
}
void zone_manager::cache_data()
{
area_cache.clear();
for( auto &elem : zones ) {
if( !elem.get_enabled() ) {
continue;
}
const std::string &type_hash = elem.get_type_hash();
auto &cache = area_cache[type_hash];
// Draw marked area
for( const tripoint &p : tripoint_range<tripoint>( elem.get_start_point(),
elem.get_end_point() ) ) {
cache.insert( p );
}
}
}
void zone_manager::cache_vzones()
{
vzone_cache.clear();
auto vzones = get_map().get_vehicle_zones( g->get_levz() );
for( auto elem : vzones ) {
if( !elem->get_enabled() ) {
continue;
}
const std::string &type_hash = elem->get_type_hash();
auto &cache = vzone_cache[type_hash];
// TODO: looks very similar to the above cache_data - maybe merge it?
// Draw marked area
for( const tripoint &p : tripoint_range<tripoint>( elem->get_start_point(),
elem->get_end_point() ) ) {
cache.insert( p );
}
}
}
std::unordered_set<tripoint> zone_manager::get_point_set( const zone_type_id &type,
const faction_id &fac ) const
{
const auto &type_iter = area_cache.find( zone_data::make_type_hash( type, fac ) );
if( type_iter == area_cache.end() ) {
return std::unordered_set<tripoint>();
}
return type_iter->second;
}
std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint &where,
int radius, const faction_id &fac ) const
{
return get_point_set_loot( where, radius, false, fac );
}
std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint &where,
int radius, bool npc_search, const faction_id &/*fac*/ ) const
{
std::unordered_set<tripoint> res;
map &here = get_map();
for( const tripoint elem : here.points_in_radius( here.getlocal( where ), radius ) ) {
const zone_data *zone = get_zone_at( here.getabs( elem ) );
// if not a LOOT zone
if( ( !zone ) || ( zone->get_type().str().substr( 0, 4 ) != "LOOT" ) ) {
continue;
}
if( npc_search && ( has( zone_NO_NPC_PICKUP, elem ) ) ) {
continue;
}
res.insert( elem );
}
return res;
}
std::unordered_set<tripoint> zone_manager::get_vzone_set( const zone_type_id &type,
const faction_id &fac ) const
{
//Only regenerate the vehicle zone cache if any vehicles have moved
const auto &type_iter = vzone_cache.find( zone_data::make_type_hash( type, fac ) );
if( type_iter == vzone_cache.end() ) {
return std::unordered_set<tripoint>();
}
return type_iter->second;
}
bool zone_manager::has( const zone_type_id &type, const tripoint &where,
const faction_id &fac ) const
{
const auto &point_set = get_point_set( type, fac );
const auto &vzone_set = get_vzone_set( type, fac );
return point_set.find( where ) != point_set.end() || vzone_set.find( where ) != vzone_set.end();
}
bool zone_manager::has_near( const zone_type_id &type, const tripoint &where, int range,
const faction_id &fac ) const
{
const auto &point_set = get_point_set( type, fac );
for( auto &point : point_set ) {
if( point.z == where.z ) {
if( square_dist( point, where ) <= range ) {
return true;
}
}
}
const auto &vzone_set = get_vzone_set( type, fac );
for( auto &point : vzone_set ) {
if( point.z == where.z ) {
if( square_dist( point, where ) <= range ) {
return true;
}
}
}
return false;
}
bool zone_manager::has_loot_dest_near( const tripoint &where ) const
{
for( const auto &ztype : get_manager().get_types() ) {
const zone_type_id &type = ztype.first;
if( type == zone_CAMP_FOOD || type == zone_FARM_PLOT ||
type == zone_LOOT_UNSORTED || type == zone_LOOT_IGNORE ||
type == zone_CONSTRUCTION_BLUEPRINT ||
type == zone_NO_AUTO_PICKUP || type == zone_NO_NPC_PICKUP ) {
continue;
}
if( has_near( type, where ) ) {
return true;
}
}
return false;
}
const zone_data *zone_manager::get_zone_at( const tripoint &where, const zone_type_id &type ) const
{
for( const zone_data &zone : zones ) {
if( zone.has_inside( where ) && zone.get_type() == type ) {
return &zone;
}
}
auto vzones = get_map().get_vehicle_zones( g->get_levz() );
for( const zone_data *zone : vzones ) {
if( zone->has_inside( where ) && zone->get_type() == type ) {
return zone;
}
}
return nullptr;
}
bool zone_manager::custom_loot_has( const tripoint &where, const item *it ) const
{
auto zone = get_zone_at( where, zone_LOOT_CUSTOM );
if( !zone || !it ) {
return false;
}
const loot_options &options = dynamic_cast<const loot_options &>( zone->get_options() );
std::string filter_string = options.get_mark();
auto z = item_filter_from_string( filter_string );
return z( *it );
}
std::unordered_set<tripoint> zone_manager::get_near( const zone_type_id &type,
const tripoint &where, int range, const item *it, const faction_id &fac ) const
{
const auto &point_set = get_point_set( type, fac );
auto near_point_set = std::unordered_set<tripoint>();
for( auto &point : point_set ) {
if( point.z == where.z ) {
if( square_dist( point, where ) <= range ) {
if( it && has( zone_LOOT_CUSTOM, point ) ) {
if( custom_loot_has( point, it ) ) {
near_point_set.insert( point );
}
} else {
near_point_set.insert( point );
}
}
}
}
const auto &vzone_set = get_vzone_set( type, fac );
for( auto &point : vzone_set ) {
if( point.z == where.z ) {
if( square_dist( point, where ) <= range ) {
if( it && has( zone_LOOT_CUSTOM, point ) ) {
if( custom_loot_has( point, it ) ) {
near_point_set.insert( point );
}
} else {
near_point_set.insert( point );
}
}
}
}
return near_point_set;
}
std::optional<tripoint> zone_manager::get_nearest( const zone_type_id &type, const tripoint &where,
int range, const faction_id &fac ) const
{
if( range < 0 ) {
return std::nullopt;
}
tripoint nearest_pos = tripoint( INT_MIN, INT_MIN, INT_MIN );
int nearest_dist = range + 1;
const std::unordered_set<tripoint> &point_set = get_point_set( type, fac );
for( const tripoint &p : point_set ) {
int cur_dist = square_dist( p, where );
if( cur_dist < nearest_dist ) {
nearest_dist = cur_dist;
nearest_pos = p;
if( nearest_dist == 0 ) {
return nearest_pos;
}
}
}
const std::unordered_set<tripoint> &vzone_set = get_vzone_set( type, fac );
for( const tripoint &p : vzone_set ) {
int cur_dist = square_dist( p, where );
if( cur_dist < nearest_dist ) {
nearest_dist = cur_dist;
nearest_pos = p;
if( nearest_dist == 0 ) {
return nearest_pos;
}
}
}
if( nearest_dist > range ) {
return std::nullopt;
}
return nearest_pos;
}
zone_type_id zone_manager::get_near_zone_type_for_item( const item &it,
const tripoint &where, int range ) const
{
const item_category &cat = it.get_category();
if( has_near( zone_LOOT_CUSTOM, where, range ) ) {
if( !get_near( zone_LOOT_CUSTOM, where, range, &it ).empty() ) {
return zone_LOOT_CUSTOM;
}
}
if( it.has_flag( flag_FIREWOOD ) ) {
if( has_near( zone_LOOT_WOOD, where, range ) ) {
return zone_LOOT_WOOD;
}
}
if( it.is_corpse() ) {
if( has_near( zone_LOOT_CORPSE, where, range ) ) {
return zone_LOOT_CORPSE;
}
}
std::optional<zone_type_id> zone_check_first = cat.priority_zone( it );
if( zone_check_first && has_near( *zone_check_first, where, range ) ) {
return *zone_check_first;
}
if( cat.zone() ) {
return *cat.zone();
}
if( cat.get_id() == itcat_food ) {
const bool preserves = it.is_food_container() && it.type->container->preserves;
// skip food without comestible, like MREs
if( const item *it_food = it.get_food() ) {
if( it_food->get_comestible()->comesttype == "DRINK" ) {
if( !preserves && it_food->goes_bad() && has_near( zone_LOOT_PDRINK, where, range ) ) {
return zone_LOOT_PDRINK;
} else if( has_near( zone_LOOT_DRINK, where, range ) ) {
return zone_LOOT_DRINK;
}
}
if( !preserves && it_food->goes_bad() && has_near( zone_LOOT_PFOOD, where, range ) ) {
return zone_LOOT_PFOOD;
}
}
return zone_LOOT_FOOD;
}
return zone_type_id();
}
std::vector<zone_data> zone_manager::get_zones( const zone_type_id &type,
const tripoint &where, const faction_id &fac ) const
{
auto zones = std::vector<zone_data>();
for( const auto &zone : this->zones ) {
if( zone.get_type() == type && zone.get_faction() == fac ) {
if( zone.has_inside( where ) ) {
zones.emplace_back( zone );
}
}
}
return zones;
}
const zone_data *zone_manager::get_zone_at( const tripoint &where ) const
{
for( auto it = zones.rbegin(); it != zones.rend(); ++it ) {
const auto &zone = *it;
if( zone.has_inside( where ) ) {
return &zone;
}
}
return nullptr;
}
const zone_data *zone_manager::get_bottom_zone( const tripoint &where,
const faction_id &fac ) const
{
for( auto it = zones.rbegin(); it != zones.rend(); ++it ) {
const auto &zone = *it;
if( zone.get_faction() != fac ) {
continue;
}
if( zone.has_inside( where ) ) {
return &zone;
}
}
auto vzones = get_map().get_vehicle_zones( g->get_levz() );
for( auto it = vzones.rbegin(); it != vzones.rend(); ++it ) {
const auto zone = *it;
if( zone->get_faction() != fac ) {
continue;
}
if( zone->has_inside( where ) ) {
return zone;
}
}
return nullptr;
}
// CAREFUL: This function has the ability to move the passed in zone reference depending on
// which constructor of the key-value pair we use which depends on new_zone being an rvalue or lvalue and constness.
// If you are passing new_zone from a non-const iterator, be prepared for a move! This
// may break some iterators like map iterators if you are less specific!
void zone_manager::create_vehicle_loot_zone( vehicle &vehicle, point mount_point,
zone_data &new_zone )
{
//create a vehicle loot zone
new_zone.set_is_vehicle( true );
auto nz = vehicle.loot_zones.emplace( mount_point, new_zone );
get_map().register_vehicle_zone( &vehicle, g->get_levz() );
vehicle.zones_dirty = false;
added_vzones.push_back( &nz->second );
cache_vzones();
}
void zone_manager::add( const std::string &name, const zone_type_id &type, const faction_id &fac,
const bool invert, const bool enabled, const tripoint &start,
const tripoint &end, shared_ptr_fast<zone_options> options )
{
zone_data new_zone = zone_data( name, type, fac, invert, enabled, start, end, options );
//the start is a vehicle tile with cargo space
map &here = get_map();
if( const std::optional<vpart_reference> vp = here.veh_at( here.getlocal(
start ) ).part_with_feature( "CARGO", false ) ) {
// TODO:Allow for loot zones on vehicles to be larger than 1x1
if( start == end && query_yn( _( "Bind this zone to the cargo part here?" ) ) ) {
// TODO: refactor zone options for proper validation code
if( type == zone_FARM_PLOT || type == zone_CONSTRUCTION_BLUEPRINT ) {
popup( _( "You cannot add that type of zone to a vehicle." ), PF_NONE );
return;
}
create_vehicle_loot_zone( vp->vehicle(), vp->mount(), new_zone );
return;
}
}
//Create a regular zone
zones.push_back( new_zone );
cache_data();
}