forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_action.cpp
2550 lines (2243 loc) · 93.1 KB
/
handle_action.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "game.h" // IWYU pragma: associated
#include <chrono>
#include <cstdlib>
#include <initializer_list>
#include <set>
#include <sstream>
#include <utility>
#include "action.h"
#include "advanced_inv.h"
#include "animation.h"
#include "armor_layers.h"
#include "auto_note.h"
#include "auto_pickup.h"
#include "avatar.h"
#include "avatar_action.h"
#include "avatar_functions.h"
#include "bionics.h"
#include "bionics_ui.h"
#include "calendar.h"
#include "catacharset.h"
#include "character.h"
#include "character_display.h"
#include "character_martial_arts.h"
#include "character_turn.h"
#include "clzones.h"
#include "color.h"
#include "construction.h"
#include "crafting.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "debug_menu.h"
#include "diary.h"
#include "distraction_manager.h"
#include "faction.h"
#include "field.h"
#include "field_type.h"
#include "fstream_utils.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "gamemode.h"
#include "gates.h"
#include "gun_mode.h"
#include "help.h"
#include "input.h"
#include "int_id.h"
#include "item.h"
#include "item_contents.h"
#include "item_group.h"
#include "itype.h"
#include "iuse.h"
#include "lightmap.h"
#include "line.h"
#include "magic.h"
#include "make_static.h"
#include "map.h"
#include "map_selector.h"
#include "mapdata.h"
#include "mapsharing.h"
#include "messages.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "mutation_ui.h"
#include "options.h"
#include "output.h"
#include "overmap_ui.h"
#include "panels.h"
#include "player.h"
#include "player_activity.h"
#include "popup.h"
#include "ranged.h"
#include "rng.h"
#include "safemode_ui.h"
#include "scores_ui.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "string_input_popup.h"
#include "translations.h"
#include "ui.h"
#include "ui_manager.h"
#include "units.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
#include "worldfactory.h"
static const activity_id ACT_FERTILIZE_PLOT( "ACT_FERTILIZE_PLOT" );
static const activity_id ACT_MOVE_LOOT( "ACT_MOVE_LOOT" );
static const activity_id ACT_MULTIPLE_BUTCHER( "ACT_MULTIPLE_BUTCHER" );
static const activity_id ACT_MULTIPLE_CHOP_PLANKS( "ACT_MULTIPLE_CHOP_PLANKS" );
static const activity_id ACT_MULTIPLE_CHOP_TREES( "ACT_MULTIPLE_CHOP_TREES" );
static const activity_id ACT_MULTIPLE_CONSTRUCTION( "ACT_MULTIPLE_CONSTRUCTION" );
static const activity_id ACT_MULTIPLE_FARM( "ACT_MULTIPLE_FARM" );
static const activity_id ACT_MULTIPLE_MINE( "ACT_MULTIPLE_MINE" );
static const activity_id ACT_PULP( "ACT_PULP" );
static const activity_id ACT_SPELLCASTING( "ACT_SPELLCASTING" );
static const activity_id ACT_VEHICLE_DECONSTRUCTION( "ACT_VEHICLE_DECONSTRUCTION" );
static const activity_id ACT_VEHICLE_REPAIR( "ACT_VEHICLE_REPAIR" );
static const activity_id ACT_WAIT( "ACT_WAIT" );
static const activity_id ACT_WAIT_STAMINA( "ACT_WAIT_STAMINA" );
static const activity_id ACT_WAIT_WEATHER( "ACT_WAIT_WEATHER" );
static const efftype_id effect_alarm_clock( "alarm_clock" );
static const efftype_id effect_laserlocked( "laserlocked" );
static const efftype_id effect_relax_gas( "relax_gas" );
static const itype_id itype_radiocontrol( "radiocontrol" );
static const itype_id itype_shoulder_strap( "shoulder_strap" );
static const skill_id skill_melee( "melee" );
static const quality_id qual_CUT( "CUT" );
static const bionic_id bio_remote( "bio_remote" );
static const trait_id trait_HIBERNATE( "HIBERNATE" );
static const trait_id trait_PROF_CHURL( "PROF_CHURL" );
static const trait_id trait_SHELL2( "SHELL2" );
static const std::string flag_LITCIG( "LITCIG" );
static const std::string flag_LOCKED( "LOCKED" );
static const std::string flag_MAGIC_FOCUS( "MAGIC_FOCUS" );
static const std::string flag_NO_QUICKDRAW( "NO_QUICKDRAW" );
static const std::string flag_SLEEP_IGNORE( "SLEEP_IGNORE" );
#define dbg(x) DebugLogFL((x),DC::Game)
#if defined(__ANDROID__)
extern std::map<std::string, std::list<input_event>> quick_shortcuts_map;
extern bool add_best_key_for_action_to_quick_shortcuts( action_id action,
const std::string &category, bool back );
extern bool add_key_to_quick_shortcuts( int key, const std::string &category, bool back );
#endif
class user_turn
{
private:
std::chrono::time_point<std::chrono::steady_clock> user_turn_start;
public:
user_turn() {
user_turn_start = std::chrono::steady_clock::now();
}
bool has_timeout_elapsed() {
return moves_elapsed() > 100;
}
int moves_elapsed() {
const float turn_duration = get_option<float>( "TURN_DURATION" );
// Magic number 0.005 chosen due to option menu's 2 digit precision and
// the option menu UI rounding <= 0.005 down to "0.00" in the display.
// This conditional will catch values (e.g. 0.003) that the options menu
// would round down to "0.00" in the options menu display. This prevents
// the user from being surprised by floating point rounding near zero.
if( turn_duration <= 0.005 ) {
return 0;
}
auto now = std::chrono::steady_clock::now();
std::chrono::milliseconds elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>( now - user_turn_start );
return elapsed_ms.count() / ( 10.0 * turn_duration );
}
};
static bool init_weather_anim( const weather_type_id &wtype, weather_printable &wPrint )
{
const weather_animation_t &anim = wtype->animation;
wPrint.colGlyph = anim.color;
wPrint.cGlyph = anim.symbol;
wPrint.wtype = wtype;
wPrint.vdrops.clear();
return anim.symbol != NULL_UNICODE;
}
static void generate_weather_anim_frame( const weather_type_id &wtype, weather_printable &wPrint )
{
map &m = get_map();
avatar &u = get_avatar();
const visibility_variables &cache = m.get_visibility_variables_cache();
const level_cache &map_cache = m.get_cache_ref( u.posz() );
const auto &visibility_cache = map_cache.visibility_cache;
const int TOTAL_VIEW = MAX_VIEW_DISTANCE * 2 + 1;
point iStart( ( TERRAIN_WINDOW_WIDTH > TOTAL_VIEW ) ? ( TERRAIN_WINDOW_WIDTH - TOTAL_VIEW ) / 2 : 0,
( TERRAIN_WINDOW_HEIGHT > TOTAL_VIEW ) ? ( TERRAIN_WINDOW_HEIGHT - TOTAL_VIEW ) / 2 :
0 );
point iEnd( ( TERRAIN_WINDOW_WIDTH > TOTAL_VIEW ) ? TERRAIN_WINDOW_WIDTH -
( TERRAIN_WINDOW_WIDTH - TOTAL_VIEW ) /
2 :
TERRAIN_WINDOW_WIDTH, ( TERRAIN_WINDOW_HEIGHT > TOTAL_VIEW ) ? TERRAIN_WINDOW_HEIGHT -
( TERRAIN_WINDOW_HEIGHT - TOTAL_VIEW ) /
2 : TERRAIN_WINDOW_HEIGHT );
if( g->fullscreen ) {
iStart.x = 0;
iStart.y = 0;
iEnd.x = TERMX;
iEnd.y = TERMY;
}
const weather_animation_t &anim = wtype->animation;
point offset( u.view_offset.xy() + point( -getmaxx( g->w_terrain ) / 2 + u.posx(),
-getmaxy( g->w_terrain ) / 2 + u.posy() ) );
if( tile_iso && use_tiles ) {
iStart.x = 0;
iStart.y = 0;
iEnd.x = MAPSIZE_X;
iEnd.y = MAPSIZE_Y;
offset.x = 0;
offset.y = 0;
}
wPrint.vdrops.clear();
const int dropCount = static_cast<int>( iEnd.x * iEnd.y * anim.factor );
for( int i = 0; i < dropCount; i++ ) {
const point iRand{ rng( iStart.x, iEnd.x - 1 ), rng( iStart.y, iEnd.y - 1 ) };
const point map( iRand + offset );
const tripoint mapp( map, u.posz() );
const lit_level lighting = visibility_cache[mapp.x][mapp.y];
if( m.is_outside( mapp ) && m.get_visibility( lighting, cache ) == VIS_CLEAR &&
!g->critter_at( mapp, true ) ) {
// Suppress if a critter is there
wPrint.vdrops.emplace_back( std::make_pair( iRand.x, iRand.y ) );
}
}
}
input_context game::get_player_input( std::string &action )
{
input_context ctxt;
if( uquit == QUIT_WATCH ) {
ctxt = input_context( "DEFAULTMODE" );
ctxt.set_iso( true );
// The list of allowed actions in death-cam mode in game::handle_action
// *INDENT-OFF*
for( const action_id id : {
ACTION_TOGGLE_MAP_MEMORY,
ACTION_CENTER,
ACTION_SHIFT_N,
ACTION_SHIFT_NE,
ACTION_SHIFT_E,
ACTION_SHIFT_SE,
ACTION_SHIFT_S,
ACTION_SHIFT_SW,
ACTION_SHIFT_W,
ACTION_SHIFT_NW,
ACTION_LOOK,
ACTION_KEYBINDINGS,
} ) {
ctxt.register_action( action_ident( id ) );
}
// *INDENT-ON*
ctxt.register_action( "QUIT", to_translation( "Accept your fate" ) );
} else {
ctxt = get_default_mode_input_context();
}
m.update_visibility_cache( u.posz() );
user_turn current_turn;
// Checking early if we will need to handle animations
// If we do not need to handle animations that will not change as long as the user has not selected an action
// and we can handle it like we are not animating.
weather_printable wPrint;
bool animate_weather = false;
bool animate_sct = false;
bool do_animations = [&]() {
if( get_option<bool>( "ANIMATIONS" ) ) {
const bool weather_has_anim = init_weather_anim( get_weather().weather_id, wPrint );
animate_weather = weather_has_anim && get_option<bool>( "ANIMATION_RAIN" );
animate_sct = !SCT.vSCT.empty() && uquit != QUIT_WATCH && get_option<bool>( "ANIMATION_SCT" );
#if defined(TILES)
// Always animate, minimap and terrain may have animations to run
return true;
#else
// Otherwise we need to see if we actually should animate.
// Minimap and Terrain never animate in !TILES
return animate_weather || animate_sct || uquit == QUIT_WATCH;
#endif
}
return false;
}
();
if( do_animations ) {
ctxt.set_timeout( 125 );
shared_ptr_fast<game::draw_callback_t> animation_cb =
make_shared_fast<game::draw_callback_t>( [&]() {
if( animate_weather ) {
draw_weather( wPrint );
}
if( animate_sct ) {
draw_sct();
}
} );
add_draw_callback( animation_cb );
invalidate_main_ui_adaptor(); // We want to redraw at least once.
do {
if( animate_weather ) {
invalidate_main_ui_adaptor();
generate_weather_anim_frame( get_weather().weather_id, wPrint );
}
// don't bother calculating SCT if we won't show it
if( animate_sct ) {
invalidate_main_ui_adaptor();
SCT.advanceAllSteps();
//Check for creatures on all drawing positions and offset if necessary
for( auto iter = SCT.vSCT.rbegin(); iter != SCT.vSCT.rend(); ++iter ) {
const direction oCurDir = iter->getDirecton();
const int width = utf8_width( iter->getText() );
for( int i = 0; i < width; ++i ) {
tripoint tmp( iter->getPosX() + i, iter->getPosY(), get_levz() );
const Creature *critter = critter_at( tmp, true );
if( critter != nullptr && u.sees( *critter ) ) {
i = -1;
int iPos = iter->getStep() + iter->getStepOffset();
for( auto iter2 = iter; iter2 != SCT.vSCT.rend(); ++iter2 ) {
if( iter2->getDirecton() == oCurDir &&
iter2->getStep() + iter2->getStepOffset() <= iPos ) {
if( iter2->getType() == "hp" ) {
iter2->advanceStepOffset();
}
iter2->advanceStepOffset();
iPos = iter2->getStep() + iter2->getStepOffset();
}
}
}
}
}
// Stop animation when done
animate_sct = !SCT.vSCT.empty();
}
// We don't cache these checks as their result may change after 1st redraw
if( minimap_requires_animation() || terrain_requires_animation() ) {
// TODO: we redraw *everything* just to animate a couple blinking dots
// on the minimap or a few tiles.
// This is far from ideal, and can probably be done much cheaper
// (update only part of the screen? draw static parts into a texture?)
invalidate_main_ui_adaptor();
}
std::unique_ptr<static_popup> deathcam_msg_popup;
if( uquit == QUIT_WATCH ) {
deathcam_msg_popup = std::make_unique<static_popup>();
deathcam_msg_popup
->wait_message( c_red, _( "Press %s to accept your fate…" ), ctxt.get_desc( "QUIT" ) )
.on_top( true );
}
ui_manager::redraw_invalidated();
} while( handle_mouseview( ctxt, action ) && uquit != QUIT_WATCH
&& ( action != "TIMEOUT" || !current_turn.has_timeout_elapsed() ) );
ctxt.reset_timeout();
} else {
invalidate_main_ui_adaptor();
ui_manager::redraw_invalidated();
SCT.vSCT.clear();
ctxt.set_timeout( 125 );
while( handle_mouseview( ctxt, action ) ) {
if( action == "TIMEOUT" && current_turn.has_timeout_elapsed() ) {
break;
}
}
ctxt.reset_timeout();
}
return ctxt;
}
inline static void rcdrive( point d )
{
player &u = g->u;
map &here = get_map();
std::string car_location_string = u.get_value( "remote_controlling" );
if( car_location_string.empty() ) {
u.add_msg_if_player( m_warning, _( "No radio car connected." ) );
return;
}
tripoint c;
deserialize_wrapper( [&]( JsonIn & jsin ) {
c.deserialize( jsin );
}, car_location_string );
map_cursor mc( c );
std::vector<item *> rc_items = mc.items_with( [&]( const item & it ) {
return it.has_flag( "RADIO_CONTROLLED" );
} );
if( rc_items.empty() ) {
u.add_msg_if_player( m_warning, _( "No radio car connected." ) );
u.remove_value( "remote_controlling" );
return;
}
// TODO: keep track of which car is being controlled
item *rc_car = rc_items[0];
tripoint dest( c + d );
if( here.impassable( dest ) || !here.can_put_items_ter_furn( dest ) ||
here.has_furn( dest ) ) {
sounds::sound( dest, 7, sounds::sound_t::combat,
_( "sound of a collision with an obstacle." ), true, "misc", "rc_car_hits_obstacle" );
return;
} else if( !here.add_item_or_charges( dest, *rc_car ).is_null() ) {
tripoint src( c );
//~ Sound of moving a remote controlled car
sounds::sound( src, 6, sounds::sound_t::movement, _( "zzz…" ), true, "misc", "rc_car_drives" );
u.moves -= 50;
here.i_rem( src, rc_car );
u.set_value( "remote_controlling", serialize_wrapper( [&]( JsonOut & jo ) {
dest.serialize( jo );
} ) );
return;
}
}
static void pldrive( const tripoint &p )
{
if( !g->check_safe_mode_allowed() ) {
return;
}
player &u = g->u;
vehicle *veh = g->remoteveh();
bool remote = true;
int part = -1;
map &here = get_map();
if( !veh ) {
if( const optional_vpart_position vp = here.veh_at( u.pos() ) ) {
veh = &vp->vehicle();
part = vp->part_index();
}
remote = false;
}
if( !veh ) {
debugmsg( "game::pldrive error: can't find vehicle! Drive mode is now off." );
u.in_vehicle = false;
return;
}
if( !remote ) {
static const itype_id fuel_type_animal( "animal" );
const bool has_animal_controls = veh->part_with_feature( part, "CONTROL_ANIMAL", true ) >= 0;
const bool has_controls = veh->part_with_feature( part, "CONTROLS", true ) >= 0;
const bool has_animal = veh->has_engine_type( fuel_type_animal, false ) &&
veh->has_harnessed_animal();
if( !has_controls && !has_animal_controls ) {
add_msg( m_info, _( "You can't drive the vehicle from here. You need controls!" ) );
u.controlling_vehicle = false;
return;
} else if( !has_controls && has_animal_controls && !has_animal ) {
add_msg( m_info, _( "You can't drive this vehicle without an animal to pull it." ) );
u.controlling_vehicle = false;
return;
}
} else {
if( empty( veh->get_avail_parts( "REMOTE_CONTROLS" ) ) ) {
add_msg( m_info, _( "Can't drive this vehicle remotely. It has no working controls." ) );
return;
}
}
if( p.z != 0 && !here.has_zlevels() ) {
u.add_msg_if_player( m_info, _( "This vehicle doesn't look very airworthy." ) );
return;
}
if( p.z == -1 ) {
if( veh->check_heli_descend( u ) ) {
u.add_msg_if_player( m_info, _( "You steer the vehicle into a descent." ) );
} else {
return;
}
} else if( p.z == 1 ) {
if( veh->check_heli_ascend( u ) ) {
u.add_msg_if_player( m_info, _( "You steer the vehicle into an ascent." ) );
} else {
return;
}
}
veh->pldrive( get_avatar(), p.xy(), p.z );
}
inline static void pldrive( point d )
{
return pldrive( tripoint( d, 0 ) );
}
static void open()
{
player &u = g->u;
const std::optional<tripoint> openp_ = choose_adjacent_highlight( _( "Open where?" ),
pgettext( "no door, gate, curtain, etc.", "There is nothing that can be opened nearby." ),
ACTION_OPEN, false );
if( !openp_ ) {
return;
}
const tripoint openp = *openp_;
map &here = get_map();
u.moves -= 100;
if( const optional_vpart_position vp = here.veh_at( openp ) ) {
vehicle *const veh = &vp->vehicle();
int openable = veh->next_part_to_open( vp->part_index() );
if( openable >= 0 ) {
const vehicle *player_veh = veh_pointer_or_null( here.veh_at( u.pos() ) );
bool outside = !player_veh || player_veh != veh;
if( !outside ) {
if( !veh->handle_potential_theft( get_avatar() ) ) {
u.moves += 100;
return;
} else {
veh->open( openable );
}
} else {
// Outside means we check if there's anything in that tile outside-openable.
// If there is, we open everything on tile. This means opening a closed,
// curtained door from outside is possible, but it will magically open the
// curtains as well.
int outside_openable = veh->next_part_to_open( vp->part_index(), true );
if( outside_openable == -1 ) {
const std::string name = veh->part_info( openable ).name();
add_msg( m_info, _( "That %s can only opened from the inside." ), name );
u.moves += 100;
} else {
if( !veh->handle_potential_theft( get_avatar() ) ) {
u.moves += 100;
return;
} else {
veh->open_all_at( openable );
}
}
}
} else {
// If there are any OPENABLE parts here, they must be already open
if( const std::optional<vpart_reference> already_open = vp.part_with_feature( "OPENABLE",
true ) ) {
const std::string name = already_open->info().name();
add_msg( m_info, _( "That %s is already open." ), name );
}
u.moves += 100;
}
return;
}
bool didit = here.open_door( openp, !here.is_outside( u.pos() ) );
if( !didit ) {
const ter_str_id tid = here.ter( openp ).id();
if( here.has_flag( flag_LOCKED, openp ) ) {
add_msg( m_info, _( "The door is locked!" ) );
return;
} else if( tid.obj().close ) {
// if the following message appears unexpectedly, the prior check was for t_door_o
add_msg( m_info, _( "That door is already open." ) );
u.moves += 100;
return;
}
add_msg( m_info, _( "No door there." ) );
u.moves += 100;
}
}
static void close()
{
if( const std::optional<tripoint> pnt = choose_adjacent_highlight( _( "Close where?" ),
pgettext( "no door, gate, etc.", "There is nothing that can be closed nearby." ),
ACTION_CLOSE, false ) ) {
doors::close_door( get_map(), g->u, *pnt );
}
}
// Establish or release a grab on a vehicle
static void grab()
{
avatar &you = g->u;
map &here = get_map();
if( you.get_grab_type() != OBJECT_NONE ) {
if( const optional_vpart_position vp = here.veh_at( you.pos() + you.grab_point ) ) {
add_msg( _( "You release the %s." ), vp->vehicle().name );
} else if( here.has_furn( you.pos() + you.grab_point ) ) {
add_msg( _( "You release the %s." ), here.furnname( you.pos() + you.grab_point ) );
}
you.grab( OBJECT_NONE );
return;
}
const std::optional<tripoint> grabp_ = choose_adjacent( _( "Grab where?" ) );
if( !grabp_ ) {
add_msg( _( "Never mind." ) );
return;
}
const tripoint grabp = *grabp_;
if( grabp == you.pos() ) {
add_msg( _( "You get a hold of yourself." ) );
you.grab( OBJECT_NONE );
return;
}
if( const optional_vpart_position vp = here.veh_at( grabp ) ) {
if( !vp->vehicle().handle_potential_theft( get_avatar() ) ) {
return;
}
you.grab( OBJECT_VEHICLE, grabp - you.pos() );
add_msg( _( "You grab the %s." ), vp->vehicle().name );
} else if( here.has_furn( grabp ) ) { // If not, grab furniture if present
if( !here.furn( grabp ).obj().is_movable() ) {
add_msg( _( "You can not grab the %s" ), here.furnname( grabp ) );
return;
}
you.grab( OBJECT_FURNITURE, grabp - you.pos() );
if( !here.can_move_furniture( grabp, &you ) ) {
add_msg( _( "You grab the %s. It feels really heavy." ), here.furnname( grabp ) );
} else {
add_msg( _( "You grab the %s." ), here.furnname( grabp ) );
}
} else { // TODO: grab mob? Captured squirrel = pet (or meat that stays fresh longer).
add_msg( m_info, _( "There's nothing to grab there!" ) );
}
}
static void haul()
{
player &u = g->u;
map &here = get_map();
if( u.is_hauling() ) {
u.stop_hauling();
} else {
if( here.veh_at( u.pos() ) ) {
add_msg( m_info, _( "You cannot haul inside vehicles." ) );
} else if( here.has_flag( TFLAG_DEEP_WATER, u.pos() ) ) {
add_msg( m_info, _( "You cannot haul while in deep water." ) );
} else if( !here.can_put_items( u.pos() ) ) {
add_msg( m_info, _( "You cannot haul items here." ) );
} else if( !here.has_items( u.pos() ) ) {
add_msg( m_info, _( "There are no items to haul here." ) );
} else {
u.start_hauling();
}
}
}
static void smash()
{
player &u = g->u;
map &here = get_map();
if( u.is_mounted() ) {
auto mons = u.mounted_creature.get();
if( mons->has_flag( MF_RIDEABLE_MECH ) ) {
if( !mons->check_mech_powered() ) {
add_msg( m_bad, _( "Your %s refuses to move as its batteries have been drained." ),
mons->get_name() );
return;
}
}
}
item &weapon = u.primary_weapon();
const int move_cost = !u.is_armed() ? 80 : weapon.attack_cost() * 0.8;
bool didit = false;
bool mech_smash = false;
int smashskill;
///\EFFECT_STR increases smashing capability
if( u.is_mounted() ) {
auto mon = u.mounted_creature.get();
smashskill = u.str_cur + mon->mech_str_addition() + mon->type->melee_dice *
mon->type->melee_sides;
mech_smash = true;
} else {
smashskill = u.str_cur + weapon.damage_melee( DT_BASH );
}
const bool allow_floor_bash = here.has_zlevels();
const std::optional<tripoint> smashp_ = choose_adjacent( _( "Smash where?" ), allow_floor_bash );
if( !smashp_ ) {
return;
}
tripoint smashp = *smashp_;
bool smash_floor = false;
if( smashp.z != u.posz() ) {
if( smashp.z > u.posz() ) {
// TODO: Knock on the ceiling
return;
}
smashp.z = u.posz();
smash_floor = true;
}
if( u.is_mounted() ) {
monster *crit = u.mounted_creature.get();
if( crit->has_flag( MF_RIDEABLE_MECH ) ) {
crit->use_mech_power( -3 );
}
}
for( std::pair<const field_type_id, field_entry> &fd_to_smsh : here.field_at( smashp ) ) {
const map_bash_info &bash_info = fd_to_smsh.first->bash_info;
if( bash_info.str_min == -1 ) {
continue;
}
if( smashskill < bash_info.str_min ) {
add_msg( m_neutral, _( "You don't seem to be damaging the %s." ), fd_to_smsh.first->get_name() );
return;
} else if( smashskill >= rng( bash_info.str_min, bash_info.str_max ) ) {
sounds::sound( smashp, bash_info.sound_vol.value_or( -1 ),
sounds::sound_t::combat, bash_info.sound, true, "smash", "field" );
here.remove_field( smashp, fd_to_smsh.first );
here.spawn_items( smashp, item_group::items_from( bash_info.drop_group, calendar::turn ) );
u.mod_moves( - bash_info.fd_bash_move_cost );
add_msg( m_info, bash_info.field_bash_msg_success.translated() );
return;
} else {
sounds::sound( smashp, bash_info.sound_fail_vol.value_or( -1 ),
sounds::sound_t::combat, bash_info.sound_fail, true, "smash", "field" );
return;
}
}
bool should_pulp = false;
for( const item &it : here.i_at( smashp ) ) {
if( it.is_corpse() && it.damage() < it.max_damage() && it.can_revive() ) {
if( it.get_mtype()->bloodType()->has_acid ) {
if( query_yn( _( "Are you sure you want to pulp an acid filled corpse?" ) ) ) {
should_pulp = true;
break; // Don't prompt for the same thing multiple times
} else {
return; // Player doesn't want an acid bath
}
}
should_pulp = true; // There is at least one corpse to pulp
}
}
if( should_pulp ) {
// do activity forever. ACT_PULP stops itself
u.assign_activity( ACT_PULP, calendar::INDEFINITELY_LONG, 0 );
u.activity.placement = here.getabs( smashp );
return; // don't smash terrain if we've smashed a corpse
}
vehicle *veh = veh_pointer_or_null( g->m.veh_at( smashp ) );
if( veh != nullptr ) {
if( !veh->handle_potential_theft( get_avatar() ) ) {
return;
}
}
didit = here.bash( smashp, smashskill, false, false, smash_floor ).did_bash;
if( didit ) {
if( !mech_smash ) {
u.handle_melee_wear( weapon );
const int mod_sta = ( ( weapon.weight() / 10_gram ) + 200 + static_cast<int>
( get_option<float>( "PLAYER_BASE_STAMINA_REGEN_RATE" ) ) ) * -1;
u.mod_stamina( mod_sta );
if( u.get_skill_level( skill_melee ) == 0 ) {
u.practice( skill_melee, rng( 0, 1 ) * rng( 0, 1 ) );
}
const int vol = weapon.volume() / units::legacy_volume_factor;
if( weapon.made_of( material_id( "glass" ) ) &&
rng( 0, vol + 3 ) < vol ) {
add_msg( m_bad, _( "Your %s shatters!" ), weapon.tname() );
weapon.spill_contents( u.pos() );
sounds::sound( u.pos(), 24, sounds::sound_t::combat, "CRACK!", true, "smash", "glass" );
u.deal_damage( nullptr, bodypart_id( "hand_r" ), damage_instance( DT_CUT, rng( 0, vol ) ) );
if( vol > 20 ) {
// Hurt left arm too, if it was big
u.deal_damage( nullptr, bodypart_id( "hand_l" ), damage_instance( DT_CUT, rng( 0,
static_cast<int>( vol * .5 ) ) ) );
}
u.remove_weapon();
u.check_dead_state();
}
}
u.moves -= move_cost;
if( smashskill < here.bash_resistance( smashp ) && one_in( 10 ) ) {
if( here.has_furn( smashp ) && here.furn( smashp ).obj().bash.str_min != -1 ) {
// %s is the smashed furniture
add_msg( m_neutral, _( "You don't seem to be damaging the %s." ), here.furnname( smashp ) );
} else {
// %s is the smashed terrain
add_msg( m_neutral, _( "You don't seem to be damaging the %s." ), here.tername( smashp ) );
}
}
if( !here.has_floor_or_support( u.pos() ) && !here.has_flag_ter( "GOES_DOWN", u.pos() ) ) {
std::optional<tripoint> to_safety;
while( true ) {
to_safety = choose_direction( _( "Floor below destroyed! Move where?" ) );
if( to_safety && *to_safety == tripoint_zero ) {
to_safety.reset();
}
if( !to_safety && query_yn( _( "Fall down?" ) ) ) {
break;
}
if( to_safety ) {
tripoint oldpos = u.pos();
tripoint newpos = u.pos() + *to_safety;
// game::walk_move will return true even if you don't move
if( g->walk_move( newpos ) && u.pos() != oldpos ) {
break;
}
}
}
if( !to_safety ) {
// HACK! We should have a "fall down" function instead of invoking ledge trap
here.creature_on_trap( u, false );
}
}
} else {
add_msg( _( "There's nothing there to smash!" ) );
}
}
static int try_set_alarm()
{
uilist as_m;
const bool already_set = g->u.has_effect( effect_alarm_clock );
as_m.text = already_set ?
_( "You already have an alarm set. What do you want to do?" ) :
_( "You have an alarm clock. What do you want to do?" );
as_m.entries.emplace_back( 0, true, 'w', already_set ?
_( "Keep the alarm and wait a while" ) :
_( "Wait a while" ) );
as_m.entries.emplace_back( 1, true, 'a', already_set ?
_( "Change your alarm" ) :
_( "Set an alarm for later" ) );
as_m.query();
return as_m.ret;
}
static void wait()
{
std::map<int, time_duration> durations;
uilist as_m;
player &u = g->u;
bool setting_alarm = false;
map &here = get_map();
if( u.controlling_vehicle && ( here.veh_at( u.pos() )->vehicle().velocity ||
here.veh_at( u.pos() )->vehicle().cruise_velocity ) ) {
popup( _( "You can't pass time while controlling a moving vehicle." ) );
return;
}
if( u.has_alarm_clock() ) {
int alarm_query = try_set_alarm();
if( alarm_query == UILIST_CANCEL ) {
return;
}
setting_alarm = alarm_query == 1;
}
const bool has_watch = u.has_watch() || setting_alarm;
const auto add_menu_item = [ &as_m, &durations ]
( int retval, int hotkey, const std::string &caption = "",
const time_duration &duration = time_duration::from_turns( calendar::INDEFINITELY_LONG ) ) {
std::string text( caption );
if( duration != time_duration::from_turns( calendar::INDEFINITELY_LONG ) ) {
const std::string dur_str( to_string( duration ) );
text += ( text.empty() ? dur_str : string_format( " (%s)", dur_str ) );
}
as_m.addentry( retval, true, hotkey, text );
durations.emplace( retval, duration );
};
if( setting_alarm ) {
add_menu_item( 0, '0', "", 30_minutes );
for( int i = 1; i <= 9; ++i ) {
add_menu_item( i, '0' + i, "", i * 1_hours );
}
} else {
if( g->u.get_stamina() < g->u.get_stamina_max() ) {
as_m.addentry( 12, true, 'w', _( "Wait until you catch your breath" ) );
durations.emplace( 12, 15_minutes ); // to hide it from showing
}
add_menu_item( 1, '1', "", 5_minutes );
add_menu_item( 2, '2', "", 30_minutes );
add_menu_item( 3, '3', "", 1_hours );
add_menu_item( 4, '4', "", 2_hours );
add_menu_item( 5, '5', "", 3_hours );
add_menu_item( 6, '6', "", 6_hours );
as_m.addentry( 13, true, 'c', _( "Custom input" ) );
}
if( g->get_levz() >= 0 || has_watch ) {
const time_point last_midnight = calendar::turn - time_past_midnight( calendar::turn );
const auto diurnal_time_before = []( const time_point & p ) {
// Either the given time is in the future (e.g. waiting for sunset while it's early morning),
// than use it directly. Otherwise (in the past), add a single day to get the same time tomorrow
// (e.g. waiting for sunrise while it's noon).
const time_point target_time = p > calendar::turn ? p : p + 1_days;
return target_time - calendar::turn;
};
add_menu_item( 7, 'd',
setting_alarm ? _( "Set alarm for dawn" ) : _( "Wait till daylight" ),
diurnal_time_before( daylight_time( calendar::turn ) ) );
add_menu_item( 8, 'n',
setting_alarm ? _( "Set alarm for noon" ) : _( "Wait till noon" ),
diurnal_time_before( last_midnight + 12_hours ) );
add_menu_item( 9, 'k',
setting_alarm ? _( "Set alarm for dusk" ) : _( "Wait till night" ),
diurnal_time_before( night_time( calendar::turn ) ) );
add_menu_item( 10, 'm',
setting_alarm ? _( "Set alarm for midnight" ) : _( "Wait till midnight" ),
diurnal_time_before( last_midnight + 0_hours ) );
if( setting_alarm ) {
if( u.has_effect( effect_alarm_clock ) ) {
add_menu_item( 11, 'x', _( "Cancel the currently set alarm." ),
0_turns );
}
} else {
add_menu_item( 11, 'W', _( "Wait till weather changes" ) );
}
}
as_m.text = ( has_watch ) ? string_format( _( "It's %s now. " ),
to_string_time_of_day( calendar::turn ) ) : "";
as_m.text += setting_alarm ? _( "Set alarm for when?" ) : _( "Wait for how long?" );
as_m.query(); /* calculate key and window variables, generate window, and loop until we get a valid answer */
time_duration time_to_wait;
if( as_m.ret == 13 ) {
int minutes = string_input_popup()
.title( _( "How long? (in minutes)" ) )
.identifier( "wait_duration" )
.query_int();
time_to_wait = minutes * 1_minutes;
} else {
const auto dur_iter = durations.find( as_m.ret );
if( dur_iter == durations.end() ) {
return;
}
time_to_wait = dur_iter->second;
}
if( setting_alarm ) {
// Setting alarm
u.remove_effect( effect_alarm_clock );
if( as_m.ret == 11 ) {
add_msg( _( "You cancel your alarm." ) );
} else {
u.add_effect( effect_alarm_clock, time_to_wait );
add_msg( _( "You set your alarm." ) );
}
} else {
// Waiting
activity_id actType;
if( as_m.ret == 11 ) {
actType = ACT_WAIT_WEATHER;
} else if( as_m.ret == 12 ) {
actType = ACT_WAIT_STAMINA;
} else {
actType = ACT_WAIT;
}