forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_turn.cpp
882 lines (784 loc) · 34.6 KB
/
do_turn.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
#include "do_turn.h"
#include "action.h"
#include "avatar.h"
#include "bionics.h"
#include "cached_options.h"
#include "calendar.h"
#include "creature_tracker.h"
#include "event_bus.h"
#include "explosion.h"
#include "game.h"
#include "gamemode.h"
#include "help.h"
#include "kill_tracker.h"
#include "make_static.h"
#include "map.h"
#include "mapbuffer.h"
#include "memorial_logger.h"
#include "messages.h"
#include "mission.h"
#include "monattack.h"
#include "mtype.h"
#include "music.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "popup.h"
#include "scent_map.h"
#include "sdlsound.h"
#include "string_input_popup.h"
#include "timed_event.h"
#include "ui_manager.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "wcwidth.h"
#include "worldfactory.h"
static const activity_id ACT_AUTODRIVE( "ACT_AUTODRIVE" );
static const activity_id ACT_FIRSTAID( "ACT_FIRSTAID" );
static const activity_id ACT_OPERATION( "ACT_OPERATION" );
static const bionic_id bio_alarm( "bio_alarm" );
static const efftype_id effect_controlled( "controlled" );
static const efftype_id effect_npc_suspend( "npc_suspend" );
static const efftype_id effect_ridden( "ridden" );
static const efftype_id effect_sleep( "sleep" );
static const itype_id itype_holybook_bible1( "holybook_bible1" );
static const itype_id itype_holybook_bible2( "holybook_bible2" );
static const itype_id itype_holybook_bible3( "holybook_bible3" );
static const trait_id trait_CANNIBAL( "CANNIBAL" );
static const trait_id trait_HAS_NEMESIS( "HAS_NEMESIS" );
static const trait_id trait_PSYCHOPATH( "PSYCHOPATH" );
#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 );
#endif
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
namespace turn_handler
{
bool cleanup_at_end()
{
avatar &u = get_avatar();
if( g->uquit == QUIT_DIED || g->uquit == QUIT_SUICIDE ) {
// Put (non-hallucinations) into the overmap so they are not lost.
for( monster &critter : g->all_monsters() ) {
g->despawn_monster( critter );
}
// if player has "hunted" trait, remove their nemesis monster on death
if( u.has_trait( trait_HAS_NEMESIS ) ) {
overmap_buffer.remove_nemesis();
}
// Reset NPC factions and disposition
g->reset_npc_dispositions();
// Save the factions', missions and set the NPC's overmap coordinates
// Npcs are saved in the overmap.
g->save_factions_missions_npcs(); //missions need to be saved as they are global for all saves.
// and the overmap, and the local map.
g->save_maps(); //Omap also contains the npcs who need to be saved.
}
if( g->uquit == QUIT_DIED || g->uquit == QUIT_SUICIDE ) {
std::vector<std::string> vRip;
int iMaxWidth = 0;
int iNameLine = 0;
int iInfoLine = 0;
if( u.has_amount( itype_holybook_bible1, 1 ) || u.has_amount( itype_holybook_bible2, 1 ) ||
u.has_amount( itype_holybook_bible3, 1 ) ) {
if( !( u.has_trait( trait_CANNIBAL ) || u.has_trait( trait_PSYCHOPATH ) ) ) {
vRip.emplace_back( " _______ ___" );
vRip.emplace_back( " < `/ |" );
vRip.emplace_back( " > _ _ (" );
vRip.emplace_back( " | |_) | |_) |" );
vRip.emplace_back( " | | \\ | | |" );
vRip.emplace_back( " ______.__%_| |_________ __" );
vRip.emplace_back( " _/ \\| |" );
iNameLine = vRip.size();
vRip.emplace_back( "| <" );
vRip.emplace_back( "| |" );
iMaxWidth = utf8_width( vRip.back() );
vRip.emplace_back( "| |" );
vRip.emplace_back( "|_____.-._____ __/|_________|" );
vRip.emplace_back( " | |" );
iInfoLine = vRip.size();
vRip.emplace_back( " | |" );
vRip.emplace_back( " | <" );
vRip.emplace_back( " | |" );
vRip.emplace_back( " | _ |" );
vRip.emplace_back( " |__/ |" );
vRip.emplace_back( " % / `--. |%" );
vRip.emplace_back( " * .%%| -< @%%%" ); // NOLINT(cata-text-style)
vRip.emplace_back( " `\\%`@| |@@%@%%" );
vRip.emplace_back( " .%%%@@@|% ` % @@@%%@%%%%" );
vRip.emplace_back( " _.%%%%%%@@@@@@%%%__/\\%@@%%@@@@@@@%%%%%%" );
} else {
vRip.emplace_back( " _______ ___" );
vRip.emplace_back( " | \\/ |" );
vRip.emplace_back( " | |" );
vRip.emplace_back( " | |" );
iInfoLine = vRip.size();
vRip.emplace_back( " | |" );
vRip.emplace_back( " | |" );
vRip.emplace_back( " | |" );
vRip.emplace_back( " | |" );
vRip.emplace_back( " | <" );
vRip.emplace_back( " | _ |" );
vRip.emplace_back( " |__/ |" );
vRip.emplace_back( " ______.__%_| |__________ _" );
vRip.emplace_back( " _/ \\| \\" );
iNameLine = vRip.size();
vRip.emplace_back( "| <" );
vRip.emplace_back( "| |" );
iMaxWidth = utf8_width( vRip.back() );
vRip.emplace_back( "| |" );
vRip.emplace_back( "|_____.-._______ __/|__________|" );
vRip.emplace_back( " % / `_-. _ |%" );
vRip.emplace_back( " * .%%| |_) | |_)< @%%%" ); // NOLINT(cata-text-style)
vRip.emplace_back( " `\\%`@| | \\ | | |@@%@%%" );
vRip.emplace_back( " .%%%@@@|% ` % @@@%%@%%%%" );
vRip.emplace_back( " _.%%%%%%@@@@@@%%%__/\\%@@%%@@@@@@@%%%%%%" );
}
} else {
vRip.emplace_back( R"( _________ ____ )" );
vRip.emplace_back( R"( _/ `/ \_ )" );
vRip.emplace_back( R"( _/ _ _ \_. )" );
vRip.emplace_back( R"( _%\ |_) | |_) \_ )" );
vRip.emplace_back( R"( _/ \/ | \ | | \_ )" );
vRip.emplace_back( R"( _/ \_ )" );
vRip.emplace_back( R"(| |)" );
iNameLine = vRip.size();
vRip.emplace_back( R"( ) < )" );
vRip.emplace_back( R"(| |)" );
vRip.emplace_back( R"(| |)" );
vRip.emplace_back( R"(| _ |)" );
vRip.emplace_back( R"(|__/ |)" );
iMaxWidth = utf8_width( vRip.back() );
vRip.emplace_back( R"( / `--. |)" );
vRip.emplace_back( R"(| ( )" );
iInfoLine = vRip.size();
vRip.emplace_back( R"(| |)" );
vRip.emplace_back( R"(| |)" );
vRip.emplace_back( R"(| % . |)" );
vRip.emplace_back( R"(| @` %% |)" );
vRip.emplace_back( R"(| %@%@%\ * %`%@%|)" );
vRip.emplace_back( R"(%%@@@.%@%\%% `\ %%.%%@@%@)" );
vRip.emplace_back( R"(@%@@%%%%%@@@@@@%%%%%%%%@@%%@@@%%%@%%@)" );
}
const point iOffset( TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0,
TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0 );
catacurses::window w_rip = catacurses::newwin( FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH,
iOffset );
draw_border( w_rip );
sfx::do_player_death_hurt( get_player_character(), true );
sfx::fade_audio_group( sfx::group::weather, 2000 );
sfx::fade_audio_group( sfx::group::time_of_day, 2000 );
sfx::fade_audio_group( sfx::group::context_themes, 2000 );
sfx::fade_audio_group( sfx::group::fatigue, 2000 );
for( size_t iY = 0; iY < vRip.size(); ++iY ) {
size_t iX = 0;
const char *str = vRip[iY].data();
for( int slen = vRip[iY].size(); slen > 0; ) {
const uint32_t cTemp = UTF8_getch( &str, &slen );
if( cTemp != U' ' ) {
nc_color ncColor = c_light_gray;
if( cTemp == U'%' ) {
ncColor = c_green;
} else if( cTemp == U'_' || cTemp == U'|' ) {
ncColor = c_white;
} else if( cTemp == U'@' ) {
ncColor = c_brown;
} else if( cTemp == U'*' ) {
ncColor = c_red;
}
mvwputch( w_rip, point( iX + FULL_SCREEN_WIDTH / 2 - ( iMaxWidth / 2 ), iY + 1 ), ncColor,
cTemp );
}
iX += mk_wcwidth( cTemp );
}
}
std::string sTemp;
center_print( w_rip, iInfoLine++, c_white, _( "Survived:" ) );
const time_duration survived = calendar::turn - calendar::start_of_game;
const int minutes = to_minutes<int>( survived ) % 60;
const int hours = to_hours<int>( survived ) % 24;
const int days = to_days<int>( survived );
if( days > 0 ) {
// NOLINTNEXTLINE(cata-translate-string-literal)
sTemp = string_format( "%dd %dh %dm", days, hours, minutes );
} else if( hours > 0 ) {
// NOLINTNEXTLINE(cata-translate-string-literal)
sTemp = string_format( "%dh %dm", hours, minutes );
} else {
// NOLINTNEXTLINE(cata-translate-string-literal)
sTemp = string_format( "%dm", minutes );
}
center_print( w_rip, iInfoLine++, c_white, sTemp );
const int iTotalKills = g->get_kill_tracker().monster_kill_count();
sTemp = _( "Kills:" );
mvwprintz( w_rip, point( FULL_SCREEN_WIDTH / 2 - 5, 1 + iInfoLine++ ), c_light_gray,
( sTemp + " " ) );
wprintz( w_rip, c_magenta, "%d", iTotalKills );
sTemp = _( "In memory of:" );
mvwprintz( w_rip, point( FULL_SCREEN_WIDTH / 2 - utf8_width( sTemp ) / 2, iNameLine++ ),
c_light_gray,
sTemp );
sTemp = u.get_name();
mvwprintz( w_rip, point( FULL_SCREEN_WIDTH / 2 - utf8_width( sTemp ) / 2, iNameLine++ ), c_white,
sTemp );
sTemp = _( "Last Words:" );
mvwprintz( w_rip, point( FULL_SCREEN_WIDTH / 2 - utf8_width( sTemp ) / 2, iNameLine++ ),
c_light_gray,
sTemp );
int iStartX = FULL_SCREEN_WIDTH / 2 - ( ( iMaxWidth - 4 ) / 2 );
std::string sLastWords = string_input_popup()
.window( w_rip, point( iStartX, iNameLine ), iStartX + iMaxWidth - 4 - 1 )
.max_length( iMaxWidth - 4 - 1 )
.query_string();
g->death_screen();
const bool is_suicide = g->uquit == QUIT_SUICIDE;
std::chrono::seconds time_since_load =
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now() - g->time_of_last_load );
std::chrono::seconds total_time_played = g->time_played_at_last_load + time_since_load;
get_event_bus().send<event_type::game_over>( is_suicide, sLastWords, total_time_played );
// Struck the save_player_data here to forestall Weirdness
g->move_save_to_graveyard();
g->write_memorial_file( sLastWords );
get_memorial().clear();
std::vector<std::string> characters = g->list_active_saves();
// remove current player from the active characters list, as they are dead
std::vector<std::string>::iterator curchar = std::find( characters.begin(),
characters.end(), u.get_save_id() );
if( curchar != characters.end() ) {
characters.erase( curchar );
}
if( characters.empty() ) {
bool queryDelete = false;
bool queryReset = false;
if( get_option<std::string>( "WORLD_END" ) == "query" ) {
bool decided = false;
std::string buffer = _( "Warning: NPC interactions and some other global flags "
"will not all reset when starting a new character in an "
"already-played world. This can lead to some strange "
"behavior.\n\n"
"Are you sure you wish to keep this world?"
);
while( !decided ) {
uilist smenu;
smenu.allow_cancel = false;
smenu.addentry( 0, true, 'r', "%s", _( "Reset world" ) );
smenu.addentry( 1, true, 'd', "%s", _( "Delete world" ) );
smenu.addentry( 2, true, 'k', "%s", _( "Keep world" ) );
smenu.query();
switch( smenu.ret ) {
case 0:
queryReset = true;
decided = true;
break;
case 1:
queryDelete = true;
decided = true;
break;
case 2:
decided = query_yn( buffer );
break;
}
}
}
if( queryDelete || get_option<std::string>( "WORLD_END" ) == "delete" ) {
world_generator->delete_world( world_generator->active_world->world_name, true );
} else if( queryReset || get_option<std::string>( "WORLD_END" ) == "reset" ) {
world_generator->delete_world( world_generator->active_world->world_name, false );
}
} else if( get_option<std::string>( "WORLD_END" ) != "keep" ) {
std::string tmpmessage;
for( auto &character : characters ) {
tmpmessage += "\n ";
tmpmessage += character;
}
popup( _( "World retained. Characters remaining:%s" ), tmpmessage );
}
if( g->gamemode ) {
g->gamemode = std::make_unique<special_game>(); // null gamemode or something..
}
}
//Reset any offset due to driving
g->set_driving_view_offset( point_zero );
//clear all sound channels
sfx::fade_audio_channel( sfx::channel::any, 300 );
sfx::fade_audio_group( sfx::group::weather, 300 );
sfx::fade_audio_group( sfx::group::time_of_day, 300 );
sfx::fade_audio_group( sfx::group::context_themes, 300 );
sfx::fade_audio_group( sfx::group::fatigue, 300 );
zone_manager::get_manager().clear();
MAPBUFFER.clear();
overmap_buffer.clear();
#if defined(__ANDROID__)
quick_shortcuts_map.clear();
#endif
return true;
}
} // namespace turn_handler
void handle_key_blocking_activity()
{
if( test_mode ) {
return;
}
avatar &u = get_avatar();
const bool has_unfinished_activity = u.activity && (
u.activity.id()->based_on() == based_on_type::NEITHER
|| u.activity.moves_left > 0 );
if( has_unfinished_activity || u.has_destination() ) {
input_context ctxt = get_default_mode_input_context();
const std::string action = ctxt.handle_input( 0 );
bool refresh = true;
if( action == "pause" ) {
if( u.activity.is_interruptible_with_kb() ) {
g->cancel_activity_query( _( "Confirm:" ) );
}
} else if( action == "player_data" ) {
u.disp_info( true );
} else if( action == "messages" ) {
Messages::display_messages();
} else if( action == "help" ) {
get_help().display_help();
} else if( action != "HELP_KEYBINDINGS" ) {
refresh = false;
}
if( refresh ) {
ui_manager::redraw();
refresh_display();
}
} else {
refresh_display();
inp_mngr.pump_events();
}
}
namespace
{
void monmove()
{
g->cleanup_dead();
map &m = get_map();
avatar &u = get_avatar();
for( monster &critter : g->all_monsters() ) {
// Critters in impassable tiles get pushed away, unless it's not impassable for them
if( !critter.is_dead() && m.impassable( critter.pos() ) && !critter.can_move_to( critter.pos() ) ) {
dbg( D_ERROR ) << "game:monmove: " << critter.name()
<< " can't move to its location! (" << critter.posx()
<< ":" << critter.posy() << ":" << critter.posz() << "), "
<< m.tername( critter.pos() );
add_msg_debug( debugmode::DF_MONSTER, "%s can't move to its location! (%d,%d,%d), %s",
critter.name(),
critter.posx(), critter.posy(), critter.posz(), m.tername( critter.pos() ) );
bool okay = false;
for( const tripoint &dest : m.points_in_radius( critter.pos(), 3 ) ) {
if( critter.can_move_to( dest ) && g->is_empty( dest ) ) {
critter.setpos( dest );
okay = true;
break;
}
}
if( !okay ) {
// die of "natural" cause (overpopulation is natural)
critter.die( nullptr );
}
}
if( !critter.is_dead() ) {
critter.process_turn();
}
m.creature_in_field( critter );
if( calendar::once_every( 1_days ) ) {
if( critter.has_flag( MF_MILKABLE ) ) {
critter.refill_udders();
}
critter.try_biosignature();
critter.try_reproduce();
}
while( critter.moves > 0 && !critter.is_dead() && !critter.has_effect( effect_ridden ) ) {
critter.made_footstep = false;
// Controlled critters don't make their own plans
if( !critter.has_effect( effect_controlled ) ) {
// Formulate a path to follow
critter.plan();
} else {
critter.moves = 0;
break;
}
critter.move(); // Move one square, possibly hit u
critter.process_triggers();
m.creature_in_field( critter );
}
if( !critter.is_dead() &&
u.has_active_bionic( bio_alarm ) &&
u.get_power_level() >= bio_alarm->power_trigger &&
rl_dist( u.pos(), critter.pos() ) <= 5 &&
!critter.is_hallucination() ) {
u.mod_power_level( -bio_alarm->power_trigger );
add_msg( m_warning, _( "Your motion alarm goes off!" ) );
g->cancel_activity_or_ignore_query( distraction_type::motion_alarm,
_( "Your motion alarm goes off!" ) );
if( u.has_effect( effect_sleep ) ) {
u.wake_up();
}
}
}
g->cleanup_dead();
// The remaining monsters are all alive, but may be outside of the reality bubble.
// If so, despawn them. This is not the same as dying, they will be stored for later and the
// monster::die function is not called.
for( monster &critter : g->all_monsters() ) {
if( critter.posx() < 0 - MAPSIZE_X / 6 ||
critter.posy() < 0 - MAPSIZE_Y / 6 ||
critter.posx() > ( MAPSIZE_X * 7 ) / 6 ||
critter.posy() > ( MAPSIZE_Y * 7 ) / 6 ) {
g->despawn_monster( critter );
}
}
// Now, do active NPCs.
for( npc &guy : g->all_npcs() ) {
int turns = 0;
if( guy.is_mounted() ) {
guy.check_mount_is_spooked();
}
m.creature_in_field( guy );
if( !guy.has_effect( effect_npc_suspend ) ) {
guy.process_turn();
}
while( !guy.is_dead() && ( !guy.in_sleep_state() || guy.activity.id() == ACT_OPERATION ) &&
guy.moves > 0 && turns < 10 ) {
int moves = guy.moves;
guy.move();
if( moves == guy.moves ) {
// Count every time we exit npc::move() without spending any moves.
turns++;
}
// Turn on debug mode when in infinite loop
// It has to be done before the last turn, otherwise
// there will be no meaningful debug output.
if( turns == 9 ) {
debugmsg( "NPC %s entered infinite loop. Turning on debug mode",
guy.get_name() );
debug_mode = true;
// make sure the filter is active
if( std::find(
debugmode::enabled_filters.begin(), debugmode::enabled_filters.end(),
debugmode::DF_NPC ) == debugmode::enabled_filters.end() ) {
debugmode::enabled_filters.emplace_back( debugmode::DF_NPC );
}
}
}
// If we spun too long trying to decide what to do (without spending moves),
// Invoke cognitive suspension to prevent an infinite loop.
if( turns == 10 ) {
add_msg( _( "%s faints!" ), guy.get_name() );
guy.reboot();
}
if( !guy.is_dead() ) {
guy.npc_update_body();
}
}
g->cleanup_dead();
}
void overmap_npc_move()
{
avatar &u = get_avatar();
std::vector<npc *> travelling_npcs;
static constexpr int move_search_radius = 600;
for( auto &elem : overmap_buffer.get_npcs_near_player( move_search_radius ) ) {
if( !elem ) {
continue;
}
npc *npc_to_add = elem.get();
if( ( !npc_to_add->is_active() || rl_dist( u.pos(), npc_to_add->pos() ) > SEEX * 2 ) &&
npc_to_add->mission == NPC_MISSION_TRAVELLING ) {
travelling_npcs.push_back( npc_to_add );
}
}
bool npcs_need_reload = false;
for( npc *&elem : travelling_npcs ) {
if( elem->has_omt_destination() ) {
if( !elem->omt_path.empty() ) {
if( rl_dist( elem->omt_path.back(), elem->global_omt_location() ) > 2 ) {
// recalculate path, we got distracted doing something else probably
elem->omt_path.clear();
} else if( elem->omt_path.back() == elem->global_omt_location() ) {
elem->omt_path.pop_back();
}
}
if( elem->omt_path.empty() ) {
elem->omt_path = overmap_buffer.get_travel_path( elem->global_omt_location(), elem->goal,
overmap_path_params::for_npc() );
if( elem->omt_path.empty() ) { // goal is unreachable, or already reached goal, reset it
elem->goal = npc::no_goal_point;
}
} else {
elem->travel_overmap( elem->omt_path.back() );
npcs_need_reload = true;
}
}
if( !elem->has_omt_destination() && calendar::once_every( 1_hours ) && one_in( 3 ) ) {
// travelling destination is reached/not set, try different one
elem->set_omt_destination();
}
}
if( npcs_need_reload ) {
g->reload_npcs();
}
}
} // namespace
// MAIN GAME LOOP
// Returns true if game is over (death, saved, quit, etc)
bool do_turn()
{
if( g->is_game_over() ) {
return turn_handler::cleanup_at_end();
}
// Actual stuff
if( g-> new_game ) {
g->new_game = false;
} else {
g->gamemode->per_turn();
calendar::turn += 1_turns;
}
play_music( music::get_music_id_string() );
weather_manager &weather = get_weather();
// starting a new turn, clear out temperature cache
weather.temperature_cache.clear();
if( g->npcs_dirty ) {
g->load_npcs();
}
timed_event_manager &timed_events = get_timed_events();
timed_events.process();
mission::process_all();
avatar &u = get_avatar();
map &m = get_map();
// If controlling a vehicle that is owned by someone else
if( u.in_vehicle && u.controlling_vehicle ) {
vehicle *veh = veh_pointer_or_null( m.veh_at( u.pos() ) );
if( veh && !veh->handle_potential_theft( dynamic_cast<Character &>( u ), true ) ) {
veh->handle_potential_theft( dynamic_cast<Character &>( u ), false, false );
}
}
// Make sure players cant defy gravity by standing still, Looney tunes style.
u.gravity_check();
// If riding a horse - chance to spook
if( u.is_mounted() ) {
u.check_mount_is_spooked();
}
if( calendar::once_every( 1_days ) ) {
overmap_buffer.process_mongroups();
}
// Move hordes every 2.5 min
if( calendar::once_every( time_duration::from_minutes( 2.5 ) ) ) {
overmap_buffer.move_hordes();
if( u.has_trait( trait_HAS_NEMESIS ) ) {
overmap_buffer.move_nemesis();
}
// Hordes that reached the reality bubble need to spawn,
// make them spawn in invisible areas only.
m.spawn_monsters( false );
}
g->debug_hour_timer.print_time();
u.update_body();
// Auto-save if autosave is enabled
if( get_option<bool>( "AUTOSAVE" ) &&
calendar::once_every( 1_turns * get_option<int>( "AUTOSAVE_TURNS" ) ) &&
!u.is_dead_state() ) {
g->autosave();
}
weather.update_weather();
g->reset_light_level();
g->perhaps_add_random_npc();
while( u.moves > 0 && u.activity ) {
u.activity.do_turn( u );
}
// Process NPC sound events before they move or they hear themselves talking
for( npc &guy : g->all_npcs() ) {
if( rl_dist( guy.pos(), u.pos() ) < MAX_VIEW_DISTANCE ) {
sounds::process_sound_markers( &guy );
}
}
music::deactivate_music_id( music::music_id::sound );
// Process sound events into sound markers for display to the player.
sounds::process_sound_markers( &u );
if( u.is_deaf() ) {
sfx::do_hearing_loss();
}
if( !u.has_effect( effect_sleep ) || g->uquit == QUIT_WATCH ) {
if( u.moves > 0 || g->uquit == QUIT_WATCH ) {
while( u.moves > 0 || g->uquit == QUIT_WATCH ) {
g->cleanup_dead();
g->mon_info_update();
// Process any new sounds the player caused during their turn.
for( npc &guy : g->all_npcs() ) {
if( rl_dist( guy.pos(), u.pos() ) < MAX_VIEW_DISTANCE ) {
sounds::process_sound_markers( &guy );
}
}
explosion_handler::process_explosions();
sounds::process_sound_markers( &u );
if( !u.activity && g->uquit != QUIT_WATCH
&& ( !u.has_distant_destination() || calendar::once_every( 10_seconds ) ) ) {
g->wait_popup.reset();
ui_manager::redraw();
}
if( g->queue_screenshot ) {
g->take_screenshot();
g->queue_screenshot = false;
}
if( g->handle_action() ) {
++g->moves_since_last_save;
u.action_taken();
}
if( g->is_game_over() ) {
return turn_handler::cleanup_at_end();
}
if( g->uquit == QUIT_WATCH ) {
break;
}
while( u.moves > 0 && u.activity ) {
u.activity.do_turn( u );
}
}
// Reset displayed sound markers now that the turn is over.
// We only want this to happen if the player had a chance to examine the sounds.
sounds::reset_markers();
} else {
// Rate limit key polling to 10 times a second.
static auto start = std::chrono::time_point_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() );
const auto now = std::chrono::time_point_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() );
if( ( now - start ).count() > 100 ) {
handle_key_blocking_activity();
start = now;
}
g->mon_info_update();
// If player is performing a task, a monster is dangerously close,
// and monster can reach to the player or it has some sort of a ranged attack,
// warn them regardless of previous safemode warnings
if( u.activity ) {
for( std::pair<const distraction_type, std::string> &dist : u.activity.get_distractions() ) {
if( g->cancel_activity_or_ignore_query( dist.first, dist.second ) ) {
break;
}
}
}
}
}
if( g->driving_view_offset.x != 0 || g->driving_view_offset.y != 0 ) {
// Still have a view offset, but might not be driving anymore,
// or the option has been deactivated,
// might also happen when someone dives from a moving car.
// or when using the handbrake.
vehicle *veh = veh_pointer_or_null( m.veh_at( u.pos() ) );
g->calc_driving_offset( veh );
}
scent_map &scent = get_scent();
// No-scent debug mutation has to be processed here or else it takes time to start working
if( !u.has_flag( STATIC( json_character_flag( "NO_SCENT" ) ) ) ) {
scent.set( u.pos(), u.scent, u.get_type_of_scent() );
overmap_buffer.set_scent( u.global_omt_location(), u.scent );
}
scent.update( u.pos(), m );
// We need floor cache before checking falling 'n stuff
m.build_floor_caches();
m.process_falling();
m.vehmove();
m.process_fields();
m.process_items();
explosion_handler::process_explosions();
m.creature_in_field( u );
// Apply sounds from previous turn to monster and NPC AI.
sounds::process_sounds();
const int levz = m.get_abs_sub().z();
u.process_turn();
// Update vision caches for monsters. If this turns out to be expensive,
// consider a stripped down cache just for monsters.
m.build_map_cache( levz, true );
monmove();
if( calendar::once_every( 5_minutes ) ) {
overmap_npc_move();
}
if( calendar::once_every( 10_seconds ) ) {
for( const tripoint &elem : m.get_furn_field_locations() ) {
const furn_t &furn = *m.furn( elem );
for( const emit_id &e : furn.emissions ) {
m.emit_field( elem, e );
}
}
for( const tripoint &elem : m.get_ter_field_locations() ) {
const ter_t &ter = *m.ter( elem );
for( const emit_id &e : ter.emissions ) {
m.emit_field( elem, e );
}
}
}
g->mon_info_update();
if( u.moves < 0 && get_option<bool>( "FORCE_REDRAW" ) ) {
ui_manager::redraw();
refresh_display();
}
if( levz >= 0 && !u.is_underwater() ) {
handle_weather_effects( weather.weather_id );
}
const bool player_is_sleeping = u.has_effect( effect_sleep );
bool wait_redraw = false;
std::string wait_message;
time_duration wait_refresh_rate;
if( player_is_sleeping ) {
wait_redraw = true;
wait_message = _( "Wait till you wake up…" );
wait_refresh_rate = 30_minutes;
} else if( const cata::optional<std::string> progress = u.activity.get_progress_message( u ) ) {
wait_redraw = true;
wait_message = *progress;
if( u.activity.is_interruptible() && u.activity.interruptable_with_kb ) {
wait_message += string_format( _( "\n%s to interrupt" ), press_x( ACTION_PAUSE ) );
}
if( u.activity.id() == ACT_AUTODRIVE ) {
wait_refresh_rate = 1_turns;
} else if( u.activity.id() == ACT_FIRSTAID ) {
wait_refresh_rate = 5_turns;
} else {
wait_refresh_rate = 5_minutes;
}
}
if( wait_redraw ) {
if( g->first_redraw_since_waiting_started ||
calendar::once_every( std::min( 1_minutes, wait_refresh_rate ) ) ) {
if( g->first_redraw_since_waiting_started || calendar::once_every( wait_refresh_rate ) ) {
ui_manager::redraw();
}
// Avoid redrawing the main UI every time due to invalidation
ui_adaptor dummy( ui_adaptor::disable_uis_below {} );
g->wait_popup = std::make_unique<static_popup>();
g->wait_popup->on_top( true ).wait_message( "%s", wait_message );
ui_manager::redraw();
refresh_display();
g->first_redraw_since_waiting_started = false;
}
} else {
// Nothing to wait for now
g->wait_popup.reset();
g->first_redraw_since_waiting_started = true;
}
u.update_bodytemp();
u.update_body_wetness( *weather.weather_precise );
u.apply_wetness_morale( weather.temperature );
if( calendar::once_every( 1_minutes ) ) {
u.update_morale();
}
if( calendar::once_every( 9_turns ) ) {
u.check_and_recover_morale();
}
if( !u.is_deaf() ) {
sfx::remove_hearing_loss();
}
sfx::do_danger_music();
sfx::do_vehicle_engine_sfx();
sfx::do_vehicle_exterior_engine_sfx();
sfx::do_fatigue();
// reset player noise
u.volume = 0;
return false;
}