forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle_use.cpp
2436 lines (2217 loc) · 94 KB
/
vehicle_use.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 "vehicle.h" // IWYU pragma: associated
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <tuple>
#include "action.h"
#include "activity_actor_definitions.h"
#include "activity_handlers.h"
#include "activity_type.h"
#include "avatar.h"
#include "character.h"
#include "clzones.h"
#include "color.h"
#include "creature.h"
#include "creature_tracker.h"
#include "debug.h"
#include "enums.h"
#include "game.h"
#include "iexamine.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "item_pocket.h"
#include "itype.h"
#include "iuse.h"
#include "json.h"
#include "make_static.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "messages.h"
#include "monster.h"
#include "mtype.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pickup.h"
#include "player_activity.h"
#include "requirements.h"
#include "ret_val.h"
#include "rng.h"
#include "smart_controller_ui.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "ui.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_interact.h"
#include "veh_type.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
static const activity_id ACT_REPAIR_ITEM( "ACT_REPAIR_ITEM" );
static const activity_id ACT_START_ENGINES( "ACT_START_ENGINES" );
static const ammotype ammo_battery( "battery" );
static const efftype_id effect_harnessed( "harnessed" );
static const efftype_id effect_tied( "tied" );
static const fault_id fault_engine_starter( "fault_engine_starter" );
static const flag_id json_flag_FILTHY( "FILTHY" );
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_muscle( "muscle" );
static const itype_id fuel_type_none( "null" );
static const itype_id fuel_type_wind( "wind" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_detergent( "detergent" );
static const itype_id itype_fungal_seeds( "fungal_seeds" );
static const itype_id itype_marloss_seed( "marloss_seed" );
static const itype_id itype_null( "null" );
static const itype_id itype_soldering_iron( "soldering_iron" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_clean( "water_clean" );
static const itype_id itype_water_faucet( "water_faucet" );
static const itype_id itype_water_purifier( "water_purifier" );
static const itype_id itype_welder( "welder" );
static const quality_id qual_SCREW( "SCREW" );
static const skill_id skill_mechanics( "mechanics" );
static const vpart_id vpart_horn_bicycle( "horn_bicycle" );
static const zone_type_id zone_type_VEHICLE_PATROL( "VEHICLE_PATROL" );
static const std::string flag_APPLIANCE( "APPLIANCE" );
enum change_types : int {
OPENCURTAINS = 0,
OPENBOTH,
CLOSEDOORS,
CLOSEBOTH,
CANCEL
};
static input_event keybind( const std::string &opt,
const std::string &context = "VEHICLE" )
{
const std::vector<input_event> keys = input_context( context, keyboard_mode::keycode )
.keys_bound_to( opt, /*maximum_modifier_count=*/1 );
return keys.empty() ? input_event() : keys.front();
}
void vehicle::add_toggle_to_opts( std::vector<uilist_entry> &options,
std::vector<std::function<void()>> &actions,
const std::string &name,
const input_event &key,
const std::string &flag )
{
// fetch matching parts and abort early if none found
const auto found = get_avail_parts( flag );
if( empty( found ) ) {
return;
}
// can this menu option be selected by the user?
bool allow = true;
// determine target state - currently parts of similar type are all switched concurrently
bool state = std::none_of( found.begin(), found.end(), []( const vpart_reference & vp ) {
return vp.part().enabled;
} );
// if toggled part potentially usable check if could be enabled now (sufficient fuel etc.)
if( state ) {
allow = std::any_of( found.begin(), found.end(), []( const vpart_reference & vp ) {
return vp.vehicle().can_enable( vp.part() );
} );
}
auto msg = string_format( state ?
_( "Turn on %s" ) :
colorize( _( "Turn off %s" ), c_pink ),
name );
options.emplace_back( -1, allow, key, msg );
actions.emplace_back( [ = ] {
for( const vpart_reference &vp : found )
{
vehicle_part &e = vp.part();
if( e.enabled != state ) {
add_msg( state ? _( "Turned on %s." ) : _( "Turned off %s." ), e.name() );
e.enabled = state;
}
}
refresh();
} );
}
void handbrake()
{
Character &player_character = get_player_character();
const optional_vpart_position vp = get_map().veh_at( player_character.pos() );
if( !vp ) {
return;
}
vehicle *const veh = &vp->vehicle();
add_msg( _( "You pull a handbrake." ) );
veh->cruise_velocity = 0;
if( veh->last_turn != 0_degrees && rng( 15, 60 ) * 100 < std::abs( veh->velocity ) ) {
veh->skidding = true;
add_msg( m_warning, _( "You lose control of %s." ), veh->name );
veh->turn( veh->last_turn > 0_degrees ? 60_degrees : -60_degrees );
} else {
int braking_power = std::abs( veh->velocity ) / 2 + 10 * 100;
if( std::abs( veh->velocity ) < braking_power ) {
veh->stop();
} else {
int sgn = veh->velocity > 0 ? 1 : -1;
veh->velocity = sgn * ( std::abs( veh->velocity ) - braking_power );
}
}
player_character.moves = 0;
}
void vehicle::control_doors()
{
const auto door_motors = get_avail_parts( "DOOR_MOTOR" );
// Indices of doors
std::vector< int > doors_with_motors;
// Locations used to display the doors
std::vector< tripoint > locations;
// it is possible to have one door to open and one to close for single motor
if( empty( door_motors ) ) {
debugmsg( "vehicle::control_doors called but no door motors found" );
return;
}
uilist pmenu;
pmenu.title = _( "Select door to toggle" );
for( const vpart_reference &vp : door_motors ) {
const size_t p = vp.part_index();
if( vp.part().is_unavailable() ) {
continue;
}
const std::array<int, 2> doors = { { next_part_to_open( p ), next_part_to_close( p ) } };
for( int door : doors ) {
if( door == -1 ) {
continue;
}
int val = doors_with_motors.size();
doors_with_motors.push_back( door );
locations.push_back( global_part_pos3( p ) );
const char *actname = parts[door].open ? _( "Close" ) : _( "Open" );
pmenu.addentry( val, true, MENU_AUTOASSIGN, "%s %s", actname, parts[ door ].name() );
}
}
pmenu.addentry( doors_with_motors.size() + OPENCURTAINS, true, MENU_AUTOASSIGN,
_( "Open all curtains" ) );
pmenu.addentry( doors_with_motors.size() + OPENBOTH, true, MENU_AUTOASSIGN,
_( "Open all curtains and doors" ) );
pmenu.addentry( doors_with_motors.size() + CLOSEDOORS, true, MENU_AUTOASSIGN,
_( "Close all doors" ) );
pmenu.addentry( doors_with_motors.size() + CLOSEBOTH, true, MENU_AUTOASSIGN,
_( "Close all curtains and doors" ) );
pointmenu_cb callback( locations );
pmenu.callback = &callback;
// Move the menu so that we can see our vehicle
pmenu.w_y_setup = 0;
pmenu.query();
if( pmenu.ret >= 0 ) {
if( pmenu.ret < static_cast<int>( doors_with_motors.size() ) ) {
int part = doors_with_motors[pmenu.ret];
open_or_close( part, !( parts[part].open ) );
} else if( pmenu.ret < ( static_cast<int>( doors_with_motors.size() ) + CANCEL ) ) {
int option = pmenu.ret - static_cast<int>( doors_with_motors.size() );
bool open = option == OPENBOTH || option == OPENCURTAINS;
for( const vpart_reference &vp : door_motors ) {
const size_t motor = vp.part_index();
int next_part = -1;
if( open ) {
int part = next_part_to_open( motor );
if( part != -1 ) {
if( !part_flag( part, "CURTAIN" ) && option == OPENCURTAINS ) {
continue;
}
open_or_close( part, open );
if( option == OPENBOTH ) {
next_part = next_part_to_open( motor );
}
if( next_part != -1 ) {
open_or_close( next_part, open );
}
}
} else {
int part = next_part_to_close( motor );
if( part != -1 ) {
if( part_flag( part, "CURTAIN" ) && option == CLOSEDOORS ) {
continue;
}
open_or_close( part, open );
if( option == CLOSEBOTH ) {
next_part = next_part_to_close( motor );
}
if( next_part != -1 ) {
open_or_close( next_part, open );
}
}
}
}
}
}
}
void vehicle::set_electronics_menu_options( std::vector<uilist_entry> &options,
std::vector<std::function<void()>> &actions )
{
auto add_toggle = [&]( const std::string & name, const input_event & key,
const std::string & flag ) {
add_toggle_to_opts( options, actions, name, key, flag );
};
add_toggle( pgettext( "electronics menu option", "reactor" ),
keybind( "TOGGLE_REACTOR" ), "REACTOR" );
add_toggle( pgettext( "electronics menu option", "headlights" ),
keybind( "TOGGLE_HEADLIGHT" ), "CONE_LIGHT" );
add_toggle( pgettext( "electronics menu option", "wide angle headlights" ),
keybind( "TOGGLE_WIDE_HEADLIGHT" ), "WIDE_CONE_LIGHT" );
add_toggle( pgettext( "electronics menu option", "directed overhead lights" ),
keybind( "TOGGLE_HALF_OVERHEAD_LIGHT" ), "HALF_CIRCLE_LIGHT" );
add_toggle( pgettext( "electronics menu option", "overhead lights" ),
keybind( "TOGGLE_OVERHEAD_LIGHT" ), "CIRCLE_LIGHT" );
add_toggle( pgettext( "electronics menu option", "aisle lights" ),
keybind( "TOGGLE_AISLE_LIGHT" ), "AISLE_LIGHT" );
add_toggle( pgettext( "electronics menu option", "dome lights" ),
keybind( "TOGGLE_DOME_LIGHT" ), "DOME_LIGHT" );
add_toggle( pgettext( "electronics menu option", "atomic lights" ),
keybind( "TOGGLE_ATOMIC_LIGHT" ), "ATOMIC_LIGHT" );
add_toggle( pgettext( "electronics menu option", "stereo" ),
keybind( "TOGGLE_STEREO" ), "STEREO" );
add_toggle( pgettext( "electronics menu option", "chimes" ),
keybind( "TOGGLE_CHIMES" ), "CHIMES" );
add_toggle( pgettext( "electronics menu option", "fridge" ),
keybind( "TOGGLE_FRIDGE" ), "FRIDGE" );
add_toggle( pgettext( "electronics menu option", "freezer" ),
keybind( "TOGGLE_FREEZER" ), "FREEZER" );
add_toggle( pgettext( "electronics menu option", "space heater" ),
keybind( "TOGGLE_SPACE_HEATER" ), "SPACE_HEATER" );
add_toggle( pgettext( "electronics menu option", "cooler" ),
keybind( "TOGGLE_COOLER" ), "COOLER" );
add_toggle( pgettext( "electronics menu option", "recharger" ),
keybind( "TOGGLE_RECHARGER" ), "RECHARGE" );
add_toggle( pgettext( "electronics menu option", "plow" ),
keybind( "TOGGLE_PLOW" ), "PLOW" );
add_toggle( pgettext( "electronics menu option", "reaper" ),
keybind( "TOGGLE_REAPER" ), "REAPER" );
add_toggle( pgettext( "electronics menu option", "planter" ),
keybind( "TOGGLE_PLANTER" ), "PLANTER" );
add_toggle( pgettext( "electronics menu option", "rockwheel" ),
keybind( "TOGGLE_PLOW" ), "ROCKWHEEL" );
add_toggle( pgettext( "electronics menu option", "roadheader" ),
keybind( "TOGGLE_PLOW" ), "ROADHEAD" );
add_toggle( pgettext( "electronics menu option", "scoop" ),
keybind( "TOGGLE_SCOOP" ), "SCOOP" );
add_toggle( pgettext( "electronics menu option", "water purifier" ),
keybind( "TOGGLE_WATER_PURIFIER" ), "WATER_PURIFIER" );
add_toggle( pgettext( "electronics menu option", "smart controller" ),
keybind( "TOGGLE_SMART_ENGINE_CONTROLLER" ), "SMART_ENGINE_CONTROLLER" );
if( has_part( "DOOR_MOTOR" ) ) {
options.emplace_back( _( "Toggle doors" ), keybind( "TOGGLE_DOORS" ) );
actions.emplace_back( [&] { control_doors(); refresh(); } );
}
if( camera_on || ( has_part( "CAMERA" ) && has_part( "CAMERA_CONTROL" ) ) ) {
options.emplace_back( camera_on ?
colorize( _( "Turn off camera system" ), c_pink ) :
_( "Turn on camera system" ),
keybind( "TOGGLE_CAMERA" ) );
actions.emplace_back( [&] {
if( camera_on )
{
camera_on = false;
add_msg( _( "Camera system disabled" ) );
} else if( fuel_left( fuel_type_battery, true ) )
{
camera_on = true;
add_msg( _( "Camera system enabled" ) );
} else
{
add_msg( _( "Camera system won't turn on" ) );
}
map &m = get_map();
m.invalidate_map_cache( m.get_abs_sub().z );
refresh();
} );
}
}
void vehicle::control_electronics()
{
// exit early if you can't control the vehicle
if( !interact_vehicle_locked() ) {
return;
}
bool valid_option = false;
do {
std::vector<uilist_entry> options;
std::vector<std::function<void()>> actions;
set_electronics_menu_options( options, actions );
uilist menu;
menu.text = _( "Electronics controls" );
menu.entries = options;
menu.query();
valid_option = menu.ret >= 0 && static_cast<size_t>( menu.ret ) < actions.size();
if( valid_option ) {
actions[menu.ret]();
}
} while( valid_option );
}
void vehicle::control_engines()
{
int e_toggle = 0;
bool dirty = false;
//count active engines
int fuel_count = 0;
for( int e : engines ) {
fuel_count += part_info( e ).engine_fuel_opts().size();
}
const auto adjust_engine = [this]( int e_toggle ) {
int i = 0;
for( int e : engines ) {
for( const itype_id &fuel : part_info( e ).engine_fuel_opts() ) {
if( i == e_toggle ) {
if( parts[ e ].fuel_current() == fuel ) {
toggle_specific_part( e, !is_part_on( e ) );
} else {
parts[ e ].fuel_set( fuel );
}
return;
}
i += 1;
}
}
};
//show menu until user finishes
do {
e_toggle = select_engine();
if( e_toggle < 0 || e_toggle >= fuel_count ) {
break;
}
dirty = true;
adjust_engine( e_toggle );
} while( e_toggle < fuel_count );
if( !dirty ) {
return;
}
bool engines_were_on = engine_on;
for( int e : engines ) {
engine_on |= is_part_on( e );
}
// if current velocity greater than new configuration safe speed
// drop down cruise velocity.
int safe_vel = safe_velocity();
if( velocity > safe_vel ) {
cruise_velocity = safe_vel;
}
if( engines_were_on && !engine_on ) {
add_msg( _( "You turn off the %s's engines to change their configurations." ), name );
} else if( !get_player_character().controlling_vehicle ) {
add_msg( _( "You change the %s's engine configuration." ), name );
}
if( engine_on ) {
start_engines();
}
}
int vehicle::select_engine()
{
uilist tmenu;
tmenu.text = _( "Toggle which?" );
int i = 0;
for( size_t x = 0; x < engines.size(); x++ ) {
int e = engines[ x ];
for( const itype_id &fuel_id : part_info( e ).engine_fuel_opts() ) {
bool is_active = parts[ e ].enabled && parts[ e ].fuel_current() == fuel_id;
bool is_available = parts[ e ].is_available() &&
( is_perpetual_type( x ) || fuel_id == fuel_type_muscle ||
fuel_left( fuel_id ) );
tmenu.addentry( i++, is_available, -1, "[%s] %s %s",
is_active ? "x" : " ", parts[ e ].name(),
item::nname( fuel_id ) );
}
}
tmenu.query();
return tmenu.ret;
}
bool vehicle::interact_vehicle_locked()
{
if( !is_locked ) {
return true;
}
Character &player_character = get_player_character();
add_msg( _( "You don't find any keys in the %s." ), name );
const inventory &inv = player_character.crafting_inventory();
if( inv.has_quality( qual_SCREW ) ) {
if( query_yn( _( "You don't find any keys in the %s. Attempt to hotwire vehicle?" ), name ) ) {
///\EFFECT_MECHANICS speeds up vehicle hotwiring
int skill = player_character.get_skill_level( skill_mechanics );
const int moves = to_moves<int>( 6000_seconds / ( ( skill > 0 ) ? skill : 1 ) );
tripoint target = global_square_location().raw() + coord_translate( parts[0].mount );
player_character.assign_activity(
player_activity( hotwire_car_activity_actor( moves, target ) ) );
} else if( has_security_working() && query_yn( _( "Trigger the %s's Alarm?" ), name ) ) {
is_alarm_on = true;
} else {
add_msg( _( "You leave the controls alone." ) );
}
} else {
add_msg( _( "You could use a screwdriver to hotwire it." ) );
}
return false;
}
void vehicle::smash_security_system()
{
//get security and controls location
int s = -1;
int c = -1;
for( int p : speciality ) {
if( part_flag( p, "SECURITY" ) && !parts[ p ].is_broken() ) {
s = p;
c = part_with_feature( s, "CONTROLS", true );
break;
}
}
Character &player_character = get_player_character();
//controls and security must both be valid
if( c >= 0 && s >= 0 ) {
///\EFFECT_MECHANICS reduces chance of damaging controls when smashing security system
int skill = player_character.get_skill_level( skill_mechanics );
int percent_controls = 70 / ( 1 + skill );
int percent_alarm = ( skill + 3 ) * 10;
int rand = rng( 1, 100 );
if( percent_controls > rand ) {
damage_direct( c, part_info( c ).durability / 4 );
if( parts[ c ].removed || parts[ c ].is_broken() ) {
player_character.controlling_vehicle = false;
is_alarm_on = false;
add_msg( _( "You destroy the controls…" ) );
} else {
add_msg( _( "You damage the controls." ) );
}
}
if( percent_alarm > rand ) {
damage_direct( s, part_info( s ).durability / 5 );
// chance to disable alarm immediately, or disable on destruction
if( percent_alarm / 4 > rand || parts[ s ].is_broken() ) {
is_alarm_on = false;
}
}
add_msg( ( is_alarm_on ) ? _( "The alarm keeps going." ) : _( "The alarm stops." ) );
} else {
debugmsg( "No security system found on vehicle." );
}
}
std::string vehicle::tracking_toggle_string()
{
return tracking_on ? _( "Forget vehicle position" ) : _( "Remember vehicle position" );
}
void vehicle::autopilot_patrol_check()
{
zone_manager &mgr = zone_manager::get_manager();
if( mgr.has_near( zone_type_VEHICLE_PATROL, global_square_location(), 60 ) ) {
enable_patrol();
} else {
g->zones_manager();
}
}
void vehicle::toggle_autopilot()
{
uilist smenu;
enum autopilot_option : int {
PATROL,
FOLLOW,
STOP
};
smenu.desc_enabled = true;
smenu.text = _( "Choose action for the autopilot" );
smenu.addentry_col( PATROL, true, 'P', _( "Patrol…" ),
"", string_format( _( "Program the autopilot to patrol a nearby vehicle patrol zone. "
"If no zones are nearby, you will be prompted to create one." ) ) );
smenu.addentry_col( FOLLOW, true, 'F', _( "Follow…" ),
"", string_format(
_( "Program the autopilot to follow you. It might be a good idea to have a remote control available to tell it to stop, too." ) ) );
smenu.addentry_col( STOP, true, 'S', _( "Stop…" ),
"", string_format( _( "Stop all autopilot related activities." ) ) );
smenu.query();
switch( smenu.ret ) {
case PATROL:
autopilot_patrol_check();
break;
case STOP:
autopilot_on = false;
is_patrolling = false;
is_following = false;
autodrive_local_target = tripoint_zero;
stop_engines();
break;
case FOLLOW:
autopilot_on = true;
is_following = true;
is_patrolling = false;
start_engines();
refresh();
default:
return;
}
}
void vehicle::toggle_tracking()
{
if( tracking_on ) {
overmap_buffer.remove_vehicle( this );
tracking_on = false;
add_msg( _( "You stop keeping track of the vehicle position." ) );
} else {
overmap_buffer.add_vehicle( this );
tracking_on = true;
add_msg( _( "You start keeping track of this vehicle's position." ) );
}
}
void vehicle::use_controls( const tripoint &pos )
{
std::vector<uilist_entry> options;
std::vector<std::function<void()>> actions;
bool remote = g->remoteveh() == this;
bool has_electronic_controls = false;
avatar &player_character = get_avatar();
if( remote ) {
options.emplace_back( _( "Stop controlling" ), keybind( "RELEASE_CONTROLS" ) );
actions.emplace_back( [&] {
player_character.controlling_vehicle = false;
g->setremoteveh( nullptr );
add_msg( _( "You stop controlling the vehicle." ) );
refresh();
} );
has_electronic_controls = has_part( "CTRL_ELECTRONIC" ) || has_part( "REMOTE_CONTROLS" );
} else if( veh_pointer_or_null( get_map().veh_at( pos ) ) == this ) {
if( player_character.controlling_vehicle ) {
options.emplace_back( _( "Let go of controls" ), keybind( "RELEASE_CONTROLS" ) );
actions.emplace_back( [&] {
player_character.controlling_vehicle = false;
add_msg( _( "You let go of the controls." ) );
refresh();
} );
}
has_electronic_controls = !get_parts_at( pos, "CTRL_ELECTRONIC",
part_status_flag::any ).empty();
}
if( get_parts_at( pos, "CONTROLS", part_status_flag::any ).empty() && !has_electronic_controls ) {
add_msg( m_info, _( "No controls there." ) );
return;
}
// exit early if you can't control the vehicle
if( !interact_vehicle_locked() ) {
return;
}
if( has_part( "ENGINE" ) ) {
if( player_character.controlling_vehicle || ( remote && engine_on ) ) {
options.emplace_back( _( "Stop driving" ), keybind( "TOGGLE_ENGINE" ) );
actions.emplace_back( [&] {
if( engine_on && has_engine_type_not( fuel_type_muscle, true ) )
{
add_msg( _( "You turn the engine off and let go of the controls." ) );
sounds::sound( pos, 2, sounds::sound_t::movement,
_( "the engine go silent" ) );
} else
{
add_msg( _( "You let go of the controls." ) );
}
for( size_t e = 0; e < engines.size(); ++e )
{
if( is_engine_on( e ) ) {
if( sfx::has_variant_sound( "engine_stop", parts[ engines[ e ] ].info().get_id().str() ) ) {
sfx::play_variant_sound( "engine_stop", parts[ engines[ e ] ].info().get_id().str(),
parts[ engines[ e ] ].info().engine_noise_factor() );
} else if( is_engine_type( e, fuel_type_muscle ) ) {
sfx::play_variant_sound( "engine_stop", "muscle",
parts[ engines[ e ] ].info().engine_noise_factor() );
} else if( is_engine_type( e, fuel_type_wind ) ) {
sfx::play_variant_sound( "engine_stop", "wind",
parts[ engines[ e ] ].info().engine_noise_factor() );
} else if( is_engine_type( e, fuel_type_battery ) ) {
sfx::play_variant_sound( "engine_stop", "electric",
parts[ engines[ e ] ].info().engine_noise_factor() );
} else {
sfx::play_variant_sound( "engine_stop", "combustion",
parts[ engines[ e ] ].info().engine_noise_factor() );
}
}
}
vehicle_noise = 0;
engine_on = false;
player_character.controlling_vehicle = false;
g->setremoteveh( nullptr );
sfx::do_vehicle_engine_sfx();
refresh();
} );
} else if( has_engine_type_not( fuel_type_muscle, true ) ) {
options.emplace_back( engine_on ? _( "Turn off the engine" ) : _( "Turn on the engine" ),
keybind( "TOGGLE_ENGINE" ) );
actions.emplace_back( [&] {
if( engine_on )
{
engine_on = false;
sounds::sound( pos, 2, sounds::sound_t::movement,
_( "the engine go silent" ) );
stop_engines();
} else
{
start_engines();
}
refresh();
} );
}
}
if( has_part( "HORN" ) ) {
options.emplace_back( _( "Honk horn" ), keybind( "SOUND_HORN" ) );
actions.emplace_back( [&] { honk_horn(); refresh(); } );
}
if( has_part( "AUTOPILOT" ) && ( has_part( "CTRL_ELECTRONIC" ) ||
has_part( "REMOTE_CONTROLS" ) ) ) {
options.emplace_back( _( "Control autopilot" ),
keybind( "CONTROL_AUTOPILOT" ) );
actions.emplace_back( [&] { toggle_autopilot(); refresh(); } );
}
options.emplace_back( cruise_on ? _( "Disable cruise control" ) : _( "Enable cruise control" ),
keybind( "TOGGLE_CRUISE_CONTROL" ) );
actions.emplace_back( [&] {
cruise_on = !cruise_on;
add_msg( cruise_on ? _( "Cruise control turned on" ) : _( "Cruise control turned off" ) );
refresh();
} );
if( has_electronic_controls ) {
set_electronics_menu_options( options, actions );
options.emplace_back( _( "Control multiple electronics" ), keybind( "CONTROL_MANY_ELECTRONICS" ) );
actions.emplace_back( [&] { control_electronics(); refresh(); } );
}
options.emplace_back( tracking_on ? _( "Forget vehicle position" ) :
_( "Remember vehicle position" ),
keybind( "TOGGLE_TRACKING" ) );
actions.emplace_back( [&] { toggle_tracking(); } );
if( ( is_foldable() || tags.count( "convertible" ) ) && !remote ) {
options.emplace_back( string_format( _( "Fold %s" ), name ), keybind( "FOLD_VEHICLE" ) );
actions.emplace_back( [&] { fold_up(); } );
}
if( has_part( "ENGINE" ) ) {
options.emplace_back( _( "Control individual engines" ), keybind( "CONTROL_ENGINES" ) );
actions.emplace_back( [&] { control_engines(); refresh(); } );
}
if( has_part( "SMART_ENGINE_CONTROLLER" ) ) {
options.emplace_back( _( "Smart controller settings" ),
keybind( "TOGGLE_SMART_ENGINE_CONTROLLER" ) );
actions.emplace_back( [&] {
if( !smart_controller_cfg )
{
smart_controller_cfg = smart_controller_config();
}
smart_controller_settings cfg_view = smart_controller_settings( has_enabled_smart_controller,
smart_controller_cfg -> battery_lo, smart_controller_cfg -> battery_hi );
smart_controller_ui( cfg_view ).control();
for( const vpart_reference &vp : get_avail_parts( "SMART_ENGINE_CONTROLLER" ) )
{
vp.part().enabled = cfg_view.enabled;
}
refresh();
} );
}
if( is_alarm_on ) {
if( velocity == 0 && !remote ) {
options.emplace_back( _( "Try to disarm alarm" ), keybind( "TOGGLE_ALARM" ) );
actions.emplace_back( [&] { smash_security_system(); refresh(); } );
} else if( has_electronic_controls && has_part( "SECURITY" ) ) {
options.emplace_back( _( "Trigger alarm" ), keybind( "TOGGLE_ALARM" ) );
actions.emplace_back( [&] {
is_alarm_on = true;
add_msg( _( "You trigger the alarm" ) );
refresh();
} );
}
}
if( has_part( "TURRET" ) ) {
options.emplace_back( _( "Set turret targeting modes" ), keybind( "TURRET_TARGET_MODE" ) );
actions.emplace_back( [&] { turrets_set_targeting(); refresh(); } );
options.emplace_back( _( "Set turret firing modes" ), keybind( "TURRET_FIRE_MODE" ) );
actions.emplace_back( [&] { turrets_set_mode(); refresh(); } );
// We can also fire manual turrets with ACTION_FIRE while standing at the controls.
options.emplace_back( _( "Aim turrets manually" ), keybind( "TURRET_MANUAL_AIM" ) );
actions.emplace_back( [&] { turrets_aim_and_fire_all_manual( true ); refresh(); } );
// This lets us manually override and set the target for the automatic turrets instead.
options.emplace_back( _( "Aim automatic turrets" ), keybind( "TURRET_MANUAL_OVERRIDE" ) );
actions.emplace_back( [&] { turrets_override_automatic_aim(); refresh(); } );
options.emplace_back( _( "Aim individual turret" ), keybind( "TURRET_SINGLE_FIRE" ) );
actions.emplace_back( [&] { turrets_aim_and_fire_single(); refresh(); } );
}
uilist menu;
menu.text = _( "Vehicle controls" );
menu.entries = options;
menu.query();
if( menu.ret >= 0 ) {
// allow player to turn off engine without triggering another warning
if( menu.ret != 0 && menu.ret != 1 && menu.ret != 2 && menu.ret != 3 ) {
if( !handle_potential_theft( player_character ) ) {
return;
}
}
actions[menu.ret]();
// Don't access `this` from here on, one of the actions above is to call
// fold_up(), which may have deleted `this` object.
}
}
item vehicle::init_cord( const tripoint &pos )
{
item powercord( "power_cord" );
powercord.set_var( "source_x", pos.x );
powercord.set_var( "source_y", pos.y );
powercord.set_var( "source_z", pos.z );
powercord.set_var( "state", "pay_out_cable" );
powercord.active = true;
return powercord;
}
void vehicle::plug_in( const tripoint &pos )
{
item powercord = init_cord( pos );
if( powercord.get_use( "CABLE_ATTACH" ) ) {
powercord.get_use( "CABLE_ATTACH" )->call( get_player_character(), powercord, powercord.active,
pos );
}
}
void vehicle::connect( const tripoint &source_pos, const tripoint &target_pos )
{
item cord = init_cord( source_pos );
map &here = get_map();
const optional_vpart_position target_vp = here.veh_at( target_pos );
const optional_vpart_position source_vp = here.veh_at( source_pos );
if( !target_vp ) {
return;
}
vehicle *const target_veh = &target_vp->vehicle();
vehicle *const source_veh = &source_vp->vehicle();
if( source_veh == target_veh ) {
return ;
}
tripoint target_global = here.getabs( target_pos );
// TODO: make sure there is always a matching vpart id here. Maybe transform this into
// a iuse_actor class, or add a check in item_factory.
const vpart_id vpid( cord.typeId().str() );
point vcoords = source_vp->mount();
vehicle_part source_part( vpid, "", vcoords, item( cord ) );
source_part.target.first = target_global;
source_part.target.second = target_veh->global_square_location().raw();
source_veh->install_part( vcoords, source_part );
vcoords = target_vp->mount();
vehicle_part target_part( vpid, "", vcoords, item( cord ) );
tripoint source_global( cord.get_var( "source_x", 0 ),
cord.get_var( "source_y", 0 ),
cord.get_var( "source_z", 0 ) );
target_part.target.first = source_global;
target_part.target.second = source_veh->global_square_location().raw();
target_veh->install_part( vcoords, target_part );
}
bool vehicle::fold_up()
{
const bool can_be_folded = is_foldable();
const bool is_convertible = ( tags.count( "convertible" ) > 0 );
if( !( can_be_folded || is_convertible ) ) {
debugmsg( _( "Tried to fold non-folding vehicle %s" ), name );
return false;
}
avatar &player_character = get_avatar();
if( player_character.controlling_vehicle ) {
add_msg( m_warning,
_( "As the pitiless metal bars close on your nether regions, you reconsider trying to fold the %s while riding it." ),
name );
return false;
}
if( velocity > 0 ) {
add_msg( m_warning, _( "You can't fold the %s while it's in motion." ), name );
return false;
}
add_msg( _( "You painstakingly pack the %s into a portable configuration." ), name );
if( player_character.get_grab_type() != object_type::NONE ) {
player_character.grab( object_type::NONE );
add_msg( _( "You let go of %s as you fold it." ), name );
}
std::string itype_id = "folding_bicycle";
for( const auto &elem : tags ) {
if( elem.compare( 0, 12, "convertible:" ) == 0 ) {
itype_id = elem.substr( 12 );
break;
}
}
// create a folding [non]bicycle item
item bicycle( can_be_folded ? "generic_folded_vehicle" : "folding_bicycle", calendar::turn );
map &here = get_map();
// Drop stuff in containers on ground
for( const vpart_reference &vp : get_any_parts( "CARGO" ) ) {
const size_t p = vp.part_index();
for( auto &elem : get_items( p ) ) {
here.add_item_or_charges( player_character.pos(), elem );
}
while( !get_items( p ).empty() ) {
get_items( p ).erase( get_items( p ).begin() );
}
}
unboard_all();
// Store data of all parts, iuse::unfold_bicyle only loads
// some of them, some are expect to be
// vehicle specific and therefore constant (like id, mount).
// Writing everything here is easier to manage, as only
// iuse::unfold_bicyle has to adopt to changes.
try {
std::ostringstream veh_data;
JsonOut json( veh_data );
json.write( parts );
bicycle.set_var( "folding_bicycle_parts", veh_data.str() );
} catch( const JsonError &e ) {
debugmsg( "Error storing vehicle: %s", e.c_str() );
}
if( can_be_folded ) {
bicycle.set_var( "weight", to_milligram( total_mass() ) );
bicycle.set_var( "volume", total_folded_volume() / units::legacy_volume_factor );
bicycle.set_var( "name", string_format( _( "folded %s" ), name ) );
bicycle.set_var( "vehicle_name", name );
// TODO: a better description?
bicycle.set_var( "description", string_format( _( "A folded %s." ), name ) );
}
here.add_item_or_charges( global_part_pos3( 0 ), bicycle );
here.destroy_vehicle( this );
// TODO: take longer to fold bigger vehicles
// TODO: make this interruptible
player_character.moves -= 500;
return true;
}
double vehicle::engine_cold_factor( const int e ) const
{
if( !part_info( engines[e] ).has_flag( "E_COLD_START" ) ) {
return 0.0;
}
int eff_temp = get_weather().get_temperature( get_player_character().pos() );
if( !parts[ engines[ e ] ].has_fault_flag( "BAD_COLD_START" ) ) {
eff_temp = std::min( eff_temp, 20 );
}
return 1.0 - ( std::max( 0, std::min( 30, eff_temp ) ) / 30.0 );
}
int vehicle::engine_start_time( const int e ) const
{
if( !is_engine_on( e ) || part_info( engines[e] ).has_flag( "E_STARTS_INSTANTLY" ) ||
!engine_fuel_left( e ) ) {
return 0;