forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
12068 lines (10844 loc) · 452 KB
/
game.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"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cwctype>
#include <exception>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "achievement.h"
#include "action.h"
#include "activity_actor.h"
#include "activity_actor_definitions.h"
#include "activity_handlers.h"
#include "armor_layers.h"
#include "artifact.h"
#include "auto_note.h"
#include "auto_pickup.h"
#include "avatar.h"
#include "avatar_action.h"
#include "avatar_functions.h"
#include "basecamp.h"
#include "bionics.h"
#include "bodypart.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "character_display.h"
#include "character_functions.h"
#include "character_martial_arts.h"
#include "character_turn.h"
#include "clzones.h"
#include "colony.h"
#include "color.h"
#include "computer_session.h"
#include "construction.h"
#include "construction_group.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "crafting.h"
#include "creature_tracker.h"
#include "cursesport.h"
#include "damage.h"
#include "debug.h"
#include "dependency_tree.h"
#include "diary.h"
#include "distribution_grid.h"
#include "drop_token.h"
#include "distraction_manager.h"
#include "editmap.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "explosion_queue.h"
#include "faction.h"
#include "field.h"
#include "field_type.h"
#include "filesystem.h"
#include "fstream_utils.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "game_ui.h"
#include "gamemode.h"
#include "gates.h"
#include "harvest.h"
#include "help.h"
#include "iexamine.h"
#include "init.h"
#include "input.h"
#include "int_id.h"
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_contents.h"
#include "item_location.h"
#include "item_stack.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "json.h"
#include "kill_tracker.h"
#include "lightmap.h"
#include "line.h"
#include "live_view.h"
#include "loading_ui.h"
#include "magic.h"
#include "map.h"
#include "map_item_stack.h"
#include "map_iterator.h"
#include "map_functions.h"
#include "map_selector.h"
#include "mapbuffer.h"
#include "mapdata.h"
#include "mapsharing.h"
#include "memorial_logger.h"
#include "messages.h"
#include "mission.h"
#include "mod_manager.h"
#include "monattack.h"
#include "monexamine.h"
#include "monstergenerator.h"
#include "morale_types.h"
#include "mtype.h"
#include "mutation.h"
#include "npc.h"
#include "npc_class.h"
#include "omdata.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "overmap_ui.h"
#include "overmapbuffer.h"
#include "panels.h"
#include "path_info.h"
#include "pickup.h"
#include "player.h"
#include "player_activity.h"
#include "point_float.h"
#include "popup.h"
#include "ranged.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "ret_val.h"
#include "rot.h"
#include "rng.h"
#include "safemode_ui.h"
#include "scenario.h"
#include "scent_map.h"
#include "scores_ui.h"
#include "sdltiles.h"
#include "sounds.h"
#include "start_location.h"
#include "stats_tracker.h"
#include "string_formatter.h"
#include "string_id.h"
#include "string_input_popup.h"
#include "submap.h"
#include "tileray.h"
#include "timed_event.h"
#include "translations.h"
#include "trap.h"
#include "ui.h"
#include "ui_manager.h"
#include "uistate.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_interact.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "wcwidth.h"
#include "weather.h"
#include "worldfactory.h"
class computer;
#if defined(TILES)
#include "cata_tiles.h"
#endif // TILES
#if !(defined(_WIN32) || defined(TILES))
#include <langinfo.h>
#include <cstring>
#endif
#if defined(_WIN32)
#if 1 // HACK: Hack to prevent reordering of #include "platform_win.h" by IWYU
# include "platform_win.h"
#endif
# include <tchar.h>
#endif
#define dbg(x) DebugLogFL((x),DC::Game)
static constexpr int DANGEROUS_PROXIMITY = 5;
static const activity_id ACT_OPERATION( "ACT_OPERATION" );
static const activity_id ACT_AUTODRIVE( "ACT_AUTODRIVE" );
static const mtype_id mon_manhack( "mon_manhack" );
static const skill_id skill_melee( "melee" );
static const skill_id skill_dodge( "dodge" );
static const skill_id skill_firstaid( "firstaid" );
static const skill_id skill_survival( "survival" );
static const skill_id skill_electronics( "electronics" );
static const skill_id skill_computer( "computer" );
static const species_id PLANT( "PLANT" );
static const std::string flag_AUTODOC_COUCH( "AUTODOC_COUCH" );
static const efftype_id effect_accumulated_mutagen( "accumulated_mutagen" );
static const efftype_id effect_adrenaline_mycus( "adrenaline_mycus" );
static const efftype_id effect_ai_controlled( "ai_controlled" );
static const efftype_id effect_assisted( "assisted" );
static const efftype_id effect_blind( "blind" );
static const efftype_id effect_bouldering( "bouldering" );
static const efftype_id effect_contacts( "contacts" );
static const efftype_id effect_docile( "docile" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_drunk( "drunk" );
static const efftype_id effect_evil( "evil" );
static const efftype_id effect_flu( "flu" );
static const efftype_id effect_infected( "infected" );
static const efftype_id effect_laserlocked( "laserlocked" );
static const efftype_id effect_no_sight( "no_sight" );
static const efftype_id effect_npc_suspend( "npc_suspend" );
static const efftype_id effect_onfire( "onfire" );
static const efftype_id effect_pacified( "pacified" );
static const efftype_id effect_paid( "paid" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_ridden( "ridden" );
static const efftype_id effect_riding( "riding" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_stunned( "stunned" );
static const efftype_id effect_tied( "tied" );
static const bionic_id bio_remote( "bio_remote" );
static const bionic_id bio_probability_travel( "bio_probability_travel" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_grapnel( "grapnel" );
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 itype_id itype_manhole_cover( "manhole_cover" );
static const itype_id itype_remotevehcontrol( "remotevehcontrol" );
static const itype_id itype_rm13_armor_on( "rm13_armor_on" );
static const itype_id itype_rope_30( "rope_30" );
static const itype_id itype_swim_fins( "swim_fins" );
static const trait_id trait_BADKNEES( "BADKNEES" );
static const trait_id trait_ILLITERATE( "ILLITERATE" );
static const trait_id trait_LEG_TENT_BRACE( "LEG_TENT_BRACE" );
static const trait_id trait_M_IMMUNE( "M_IMMUNE" );
static const trait_id trait_PARKOUR( "PARKOUR" );
static const trait_id trait_VINES2( "VINES2" );
static const trait_id trait_VINES3( "VINES3" );
static const trait_id trait_THICKSKIN( "THICKSKIN" );
static const trait_id trait_WEB_ROPE( "WEB_ROPE" );
static const trait_id trait_WAYFARER( "WAYFARER" );
static const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
static const faction_id your_followers( "your_followers" );
#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
//The one and only game instance
std::unique_ptr<game> g;
//The one and only uistate instance
uistatedata uistate;
bool is_valid_in_w_terrain( point p )
{
return p.x >= 0 && p.x < TERRAIN_WINDOW_WIDTH && p.y >= 0 && p.y < TERRAIN_WINDOW_HEIGHT;
}
static void achievement_attained( const achievement *a )
{
g->u.add_msg_if_player( m_good, _( "You completed the achievement \"%s\"." ),
a->name() );
}
// This is the main game set-up process.
game::game() :
liveview( *liveview_ptr ),
scent_ptr( *this ),
achievements_tracker_ptr( *stats_tracker_ptr, achievement_attained ),
grid_tracker_ptr( MAPBUFFER ),
m( *map_ptr ),
u( *u_ptr ),
scent( *scent_ptr ),
timed_events( *timed_event_manager_ptr ),
uquit( QUIT_NO ),
new_game( false ),
safe_mode( SAFE_MODE_ON ),
mostseen( 0 ),
u_shared_ptr( &u, null_deleter{} ),
safe_mode_warning_logged( false ),
next_npc_id( 1 ),
next_mission_id( 1 ),
remoteveh_cache_time( calendar::before_time_starts ),
user_action_counter( 0 ),
tileset_zoom( DEFAULT_TILESET_ZOOM ),
seed( 0 ),
last_mouse_edge_scroll( std::chrono::steady_clock::now() )
{
first_redraw_since_waiting_started = true;
reset_light_level();
events().subscribe( &*stats_tracker_ptr );
events().subscribe( &*kill_tracker_ptr );
events().subscribe( &*memorial_logger_ptr );
events().subscribe( &*achievements_tracker_ptr );
events().subscribe( &*spell_events_ptr );
world_generator = std::make_unique<worldfactory>();
// do nothing, everything that was in here is moved to init_data() which is called immediately after g = new game; in main.cpp
// The reason for this move is so that g is not uninitialized when it gets to installing the parts into vehicles.
}
game::~game() = default;
// Load everything that will not depend on any mods
void game::load_static_data()
{
// UI stuff, not mod-specific per definition
inp_mngr.init(); // Load input config JSON
// Init mappings for loading the json stuff
DynamicDataLoader::get_instance();
fullscreen = false;
was_fullscreen = false;
show_panel_adm = false;
panel_manager::get_manager().init();
// These functions do not load stuff from json.
// The content they load/initialize is hardcoded into the program.
// Therefore they can be loaded here.
// If this changes (if they load data from json), they have to
// be moved to game::load_mod
get_auto_pickup().load_global();
get_safemode().load_global();
get_distraction_manager().load();
}
#if !(defined(_WIN32) || defined(TILES))
// in ncurses_def.cpp
void check_encoding();
void ensure_term_size();
#endif
void game_ui::init_ui()
{
// clear the screen
static bool first_init = true;
if( first_init ) {
#if !(defined(_WIN32) || defined(TILES))
check_encoding();
#endif
first_init = false;
#if defined(TILES)
//class variable to track the option being active
//only set once, toggle action is used to change during game
pixel_minimap_option = get_option<bool>( "PIXEL_MINIMAP" );
#endif // TILES
}
// First get TERMX, TERMY
#if defined(TILES) || defined(_WIN32)
TERMX = get_terminal_width();
TERMY = get_terminal_height();
get_options().get_option( "TERMINAL_X" ).setValue( TERMX * get_scaling_factor() );
get_options().get_option( "TERMINAL_Y" ).setValue( TERMY * get_scaling_factor() );
get_options().save();
#else
ensure_term_size();
TERMY = getmaxy( catacurses::stdscr );
TERMX = getmaxx( catacurses::stdscr );
// try to make FULL_SCREEN_HEIGHT symmetric according to TERMY
if( TERMY % 2 ) {
FULL_SCREEN_HEIGHT = 25;
} else {
FULL_SCREEN_HEIGHT = 24;
}
#endif
}
void game::toggle_fullscreen()
{
#if !defined(TILES)
fullscreen = !fullscreen;
mark_main_ui_adaptor_resize();
#else
toggle_fullscreen_window();
#endif
}
void game::toggle_pixel_minimap()
{
#if defined(TILES)
if( pixel_minimap_option ) {
clear_window_area( w_pixel_minimap );
}
pixel_minimap_option = !pixel_minimap_option;
mark_main_ui_adaptor_resize();
#endif // TILES
}
void game::reload_tileset( [[maybe_unused]] std::function<void( std::string )> out )
{
#if defined(TILES)
// Disable UIs below to avoid accessing the tile context during loading.
ui_adaptor ui( ui_adaptor::disable_uis_below {} );
try {
tilecontext->reinit();
std::vector<mod_id> dummy;
tilecontext->load_tileset(
get_option<std::string>( "TILES" ),
world_generator->active_world ? world_generator->active_world->active_mod_order : dummy,
/*precheck=*/false,
/*force=*/true,
/*pump_events=*/true
);
tilecontext->do_tile_loading_report( out );
} catch( const std::exception &err ) {
popup( _( "Loading the tileset failed: %s" ), err.what() );
}
g->reset_zoom();
g->mark_main_ui_adaptor_resize();
#endif // TILES
}
// temporarily switch out of fullscreen for functions that rely
// on displaying some part of the sidebar
void game::temp_exit_fullscreen()
{
if( fullscreen ) {
was_fullscreen = true;
toggle_fullscreen();
} else {
was_fullscreen = false;
}
}
void game::reenter_fullscreen()
{
if( was_fullscreen ) {
if( !fullscreen ) {
toggle_fullscreen();
}
}
}
/*
* Initialize more stuff after mapbuffer is loaded.
*/
void game::setup()
{
loading_ui ui( true );
init::load_world_modfiles( ui, get_world_base_save_path() + "/" + SAVE_ARTIFACTS );
if( get_option<bool>( "ELEVATED_BRIDGES" ) && !get_option<bool>( "ZLEVELS" ) ) {
debugmsg( "\"Elevated bridges\" mod requires z-levels to be ENABLED to work properly!" );
}
m = map( get_option<bool>( "ZLEVELS" ) );
next_npc_id = character_id( 1 );
next_mission_id = 1;
new_game = true;
uquit = QUIT_NO; // We haven't quit the game
bVMonsterLookFire = true;
// invalidate calendar caches in case we were previously playing
// a different world
calendar::set_eternal_season( ::get_option<bool>( "ETERNAL_SEASON" ) );
calendar::set_season_length( ::get_option<int>( "SEASON_LENGTH" ) );
get_weather().weather_id = weather_type_id::NULL_ID();
get_weather().nextweather = calendar::before_time_starts;
turnssincelastmon = 0; //Auto safe mode init
sounds::reset_sounds();
clear_zombies();
coming_to_stairs.clear();
active_npc.clear();
faction_manager_ptr->clear();
mission::clear_all();
Messages::clear_messages();
timed_events = timed_event_manager();
explosion_handler::get_explosion_queue().clear();
SCT.vSCT.clear(); //Delete pending messages
stats().clear();
// reset kill counts
kill_tracker_ptr->clear();
achievements_tracker_ptr->clear();
// reset follower list
follower_ids.clear();
scent.reset();
remoteveh_cache_time = calendar::before_time_starts;
remoteveh_cache = nullptr;
token_provider_ptr->clear();
// back to menu for save loading, new game etc
}
bool game::has_gametype() const
{
return gamemode && gamemode->id() != special_game_type::NONE;
}
special_game_type game::gametype() const
{
return gamemode ? gamemode->id() : special_game_type::NONE;
}
void game::load_map( const tripoint &pos_sm, const bool pump_events )
{
// TODO: fix point types
load_map( tripoint_abs_sm( pos_sm ), pump_events );
}
void game::load_map( const tripoint_abs_sm &pos_sm,
const bool pump_events )
{
m.load( pos_sm, true, pump_events );
grid_tracker_ptr->load( m );
}
// Set up all default values for a new game
bool game::start_game()
{
if( !gamemode ) {
gamemode = std::make_unique<special_game>();
}
seed = rng_bits();
new_game = true;
start_calendar();
get_weather().nextweather = calendar::turn;
safe_mode = ( get_option<bool>( "SAFEMODE" ) ? SAFE_MODE_ON : SAFE_MODE_OFF );
mostseen = 0; // ...and mostseen is 0, we haven't seen any monsters yet.
get_safemode().load_global();
get_distraction_manager().load();
init_autosave();
background_pane background;
static_popup popup;
popup.message( "%s", _( "Please wait as we build your world" ) );
ui_manager::redraw();
refresh_display();
load_master();
u.setID( assign_npc_id() ); // should be as soon as possible, but *after* load_master
const start_location &start_loc = u.random_start_location ? scen->random_start_location().obj() :
u.start_location.obj();
tripoint_abs_omt omtstart = overmap::invalid_tripoint;
do {
omtstart = start_loc.find_player_initial_location();
if( omtstart == overmap::invalid_tripoint ) {
if( query_yn(
_( "Try again?\n\nIt may require several attempts until the game finds a valid starting location." ) ) ) {
MAPBUFFER.clear();
overmap_buffer.clear();
} else {
return false;
}
}
} while( omtstart == overmap::invalid_tripoint );
start_loc.prepare_map( omtstart );
// Place vehicles spawned by scenario or profession, has to be placed very early to avoid bugs.
if( u.starting_vehicle &&
!place_vehicle_nearby( u.starting_vehicle, omtstart.xy(), 0, 30,
std::vector<std::string> {} ) ) {
debugmsg( "could not place starting vehicle" );
}
if( scen->has_map_extra() ) {
// Map extras can add monster spawn points and similar and should be done before the main
// map is loaded.
start_loc.add_map_extra( omtstart, scen->get_map_extra() );
}
// TODO: fix point types
tripoint lev = project_to<coords::sm>( omtstart ).raw();
// The player is centered in the map, but lev[xyz] refers to the top left point of the map
lev.x -= HALF_MAPSIZE;
lev.y -= HALF_MAPSIZE;
load_map( lev, /*pump_events=*/true );
m.invalidate_map_cache( get_levz() );
m.build_map_cache( get_levz() );
// Do this after the map cache has been built!
start_loc.place_player( u );
// ...but then rebuild it, because we want visibility cache to avoid spawning monsters in sight
m.invalidate_map_cache( get_levz() );
m.build_map_cache( get_levz() );
// Start the overmap with out immediate neighborhood visible, this needs to be after place_player
overmap_buffer.reveal( u.global_omt_location().xy(),
get_option<int>( "DISTANCE_INITIAL_VISIBILITY" ), 0 );
u.moves = 0;
u.process_turn(); // process_turn adds the initial move points
u.set_stamina( u.get_stamina_max() );
get_weather().temperature = SPRING_TEMPERATURE;
get_weather().update_weather();
u.next_climate_control_check = calendar::before_time_starts; // Force recheck at startup
u.last_climate_control_ret = false;
//Reset character safe mode/pickup rules
get_auto_pickup().clear_character_rules();
get_safemode().clear_character_rules();
get_auto_notes_settings().clear();
get_auto_notes_settings().default_initialize();
//Put some NPCs in there!
if( get_option<std::string>( "STARTING_NPC" ) == "always" ||
( get_option<std::string>( "STARTING_NPC" ) == "scenario" &&
!g->scen->has_flag( "LONE_START" ) ) ) {
create_starting_npcs();
}
//Load NPCs. Set nearby npcs to active.
load_npcs();
// Spawn the monsters
const bool spawn_near =
get_option<bool>( "BLACK_ROAD" ) || g->scen->has_flag( "SUR_START" );
// Surrounded start ones
if( spawn_near ) {
start_loc.surround_with_monsters( omtstart, mongroup_id( "GROUP_ZOMBIE" ), 70 );
}
m.spawn_monsters( !spawn_near ); // Static monsters
// Make sure that no monsters are near the player
// This can happen in lab starts
if( !spawn_near ) {
for( monster &critter : all_monsters() ) {
if( rl_dist( critter.pos(), u.pos() ) <= 5 ||
m.clear_path( critter.pos(), u.pos(), 40, 1, 100 ) ) {
remove_zombie( critter );
}
}
}
//Create mutation_category_level
u.set_highest_cat_level();
//Calculate mutation drench protection stats
u.drench_mut_calc();
u.add_effect( effect_accumulated_mutagen, 27_days, num_bp );
if( scen->has_flag( "FIRE_START" ) ) {
start_loc.burn( omtstart, 3, 3 );
}
if( scen->has_flag( "INFECTED" ) ) {
u.add_effect( effect_infected, 1_turns, random_body_part() );
}
if( scen->has_flag( "BAD_DAY" ) ) {
u.add_effect( effect_flu, 1000_minutes );
u.add_effect( effect_drunk, 270_minutes );
u.add_morale( MORALE_FEELING_BAD, -100, -100, 50_minutes, 50_minutes );
}
if( scen->has_flag( "HELI_CRASH" ) ) {
start_loc.handle_heli_crash( u );
bool success = false;
for( auto v : m.get_vehicles() ) {
std::string name = v.v->type.str();
std::string search = std::string( "helicopter" );
if( name.find( search ) != std::string::npos ) {
for( const vpart_reference &vp : v.v->get_any_parts( VPFLAG_CONTROLS ) ) {
const tripoint pos = vp.pos();
u.setpos( pos );
// Delete the items that would have spawned here from a "corpse"
for( auto sp : v.v->parts_at_relative( vp.mount(), true ) ) {
vehicle_stack here = v.v->get_items( sp );
for( auto iter = here.begin(); iter != here.end(); ) {
iter = here.erase( iter );
}
}
auto mons = critter_tracker->find( pos );
if( mons != nullptr ) {
critter_tracker->remove( *mons );
}
success = true;
break;
}
if( success ) {
v.v->name = "Bird Wreckage";
break;
}
}
}
}
if( scen->has_flag( "BORDERED" ) ) {
overmap &starting_om = get_cur_om();
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) {
starting_om.place_special_forced( overmap_special_id( "world" ), { 0, 0, z },
om_direction::type::north );
}
}
for( auto &e : u.inv_dump() ) {
e->set_owner( g->u );
}
// Now that we're done handling coordinates, ensure the player's submap is in the center of the map
update_map( u );
// Profession pets
for( const mtype_id &elem : u.starting_pets ) {
if( monster *const mon = place_critter_around( elem, u.pos(), 5 ) ) {
mon->friendly = -1;
mon->add_effect( effect_pet, 1_turns, num_bp );
} else {
add_msg( m_debug, "cannot place starting pet, no space!" );
}
}
// Assign all of this scenario's missions to the player.
for( const mission_type_id &m : scen->missions() ) {
const auto mission = mission::reserve_new( m, character_id() );
mission->assign( u );
}
g->events().send<event_type::game_start>( u.getID() );
for( Skill &elem : Skill::skills ) {
int level = u.get_skill_level_object( elem.ident() ).level();
if( level > 0 ) {
g->events().send<event_type::gains_skill_level>( u.getID(), elem.ident(), level );
}
}
return true;
}
vehicle *game::place_vehicle_nearby(
const vproto_id &id, const point_abs_omt &origin, int min_distance,
int max_distance, const std::vector<std::string> &omt_search_types )
{
std::vector<std::string> search_types = omt_search_types;
if( search_types.empty() ) {
vehicle veh( id );
if( veh.can_float() ) {
search_types.push_back( "river" );
search_types.push_back( "lake" );
} else {
search_types.push_back( "field" );
search_types.push_back( "road" );
}
}
for( const std::string &search_type : search_types ) {
omt_find_params find_params;
find_params.must_see = false;
find_params.cant_see = false;
find_params.types.emplace_back( search_type, ot_match_type::type );
// find nearest road
find_params.min_distance = min_distance;
find_params.search_range = max_distance;
// if player spawns underground, park their car on the surface.
const tripoint_abs_omt omt_origin( origin, 0 );
for( const tripoint_abs_omt &goal : overmap_buffer.find_all( omt_origin, find_params ) ) {
// try place vehicle there.
tinymap target_map;
target_map.load( project_to<coords::sm>( goal ), false );
const tripoint tinymap_center( SEEX, SEEY, goal.z() );
static constexpr std::array<units::angle, 4> angles = {{
0_degrees, 90_degrees, 180_degrees, 270_degrees
}
};
vehicle *veh = target_map.add_vehicle(
id, tinymap_center, random_entry( angles ), rng( 50, 80 ), 0, false );
if( veh ) {
tripoint abs_local = m.getlocal( target_map.getabs( tinymap_center ) );
veh->sm_pos = ms_to_sm_remain( abs_local );
veh->pos = abs_local.xy();
overmap_buffer.add_vehicle( veh );
veh->tracking_on = true;
target_map.save();
return veh;
}
}
}
return nullptr;
}
//Make any nearby overmap npcs active, and put them in the right location.
void game::load_npcs()
{
const int radius = HALF_MAPSIZE - 1;
// uses submap coordinates
std::vector<shared_ptr_fast<npc>> just_added;
for( const auto &temp : overmap_buffer.get_npcs_near_player( radius ) ) {
const character_id &id = temp->getID();
const auto found = std::find_if( active_npc.begin(), active_npc.end(),
[id]( const shared_ptr_fast<npc> &n ) {
return n->getID() == id;
} );
if( found != active_npc.end() ) {
continue;
}
if( temp->is_active() ) {
continue;
}
if( temp->has_companion_mission() ) {
continue;
}
const tripoint sm_loc = temp->global_sm_location();
// NPCs who are out of bounds before placement would be pushed into bounds
// This can cause NPCs to teleport around, so we don't want that
if( sm_loc.x < get_levx() || sm_loc.x >= get_levx() + MAPSIZE ||
sm_loc.y < get_levy() || sm_loc.y >= get_levy() + MAPSIZE ||
( sm_loc.z != get_levz() && !m.has_zlevels() ) ) {
continue;
}
add_msg( m_debug, "game::load_npcs: Spawning static NPC, %d:%d:%d (%d:%d:%d)",
get_levx(), get_levy(), get_levz(), sm_loc.x, sm_loc.y, sm_loc.z );
temp->place_on_map();
if( !m.inbounds( temp->pos() ) ) {
continue;
}
// In the rare case the npc was marked for death while
// it was on the overmap. Kill it.
if( temp->marked_for_death ) {
temp->die( nullptr );
} else {
active_npc.push_back( temp );
just_added.push_back( temp );
}
}
for( const auto &npc : just_added ) {
npc->on_load();
}
npcs_dirty = false;
}
void game::unload_npcs()
{
for( const auto &npc : active_npc ) {
npc->on_unload();
}
active_npc.clear();
}
void game::reload_npcs()
{
// TODO: Make it not invoke the "on_unload" command for the NPCs that will be loaded anyway
// and not invoke "on_load" for those NPCs that avoided unloading this way.
unload_npcs();
load_npcs();
}
const kill_tracker &game::get_kill_tracker() const
{
return *kill_tracker_ptr;
}
void game::create_starting_npcs()
{
if( !get_option<bool>( "STATIC_NPC" ) ||
get_option<std::string>( "STARTING_NPC" ) == "never" ) {
return; //Do not generate a starting npc.
}
//We don't want more than one starting npc per starting location
const int radius = 1;
if( !overmap_buffer.get_npcs_near_player( radius ).empty() ) {
return; //There is already an NPC in this starting location
}
shared_ptr_fast<npc> tmp = make_shared_fast<npc>();
tmp->randomize( one_in( 2 ) ? NC_DOCTOR : NC_NONE );
tmp->spawn_at_precise( { get_levx(), get_levy() }, u.pos() - point_south_east );
overmap_buffer.insert_npc( tmp );
tmp->form_opinion( u );
tmp->set_attitude( NPCATT_NULL );
//This sets the NPC mission. This NPC remains in the starting location.
tmp->mission = NPC_MISSION_SHELTER;
tmp->chatbin.first_topic = "TALK_SHELTER";
tmp->toggle_trait( trait_id( "NPC_STARTING_NPC" ) );
tmp->set_fac( faction_id( "no_faction" ) );
//One random starting NPC mission
tmp->add_new_mission( mission::reserve_random( ORIGIN_OPENER_NPC, tmp->global_omt_location(),
tmp->getID() ) );
}
static std::string generate_memorial_filename( const std::string &char_name )
{
// <name>-YYYY-MM-DD-HH-MM-SS.txt
// 123456789012345678901234 ~> 24 chars + a null
constexpr size_t suffix_len = 24 + 1;
constexpr size_t max_name_len = FILENAME_MAX - suffix_len;
const size_t name_len = char_name.size();
// Here -1 leaves space for the ~
const size_t truncated_name_len = ( name_len >= max_name_len ) ? ( max_name_len - 1 ) : name_len;
std::ostringstream memorial_file_path;
memorial_file_path << ensure_valid_file_name( char_name );
// Add a ~ if the player name was actually truncated.
memorial_file_path << ( ( truncated_name_len != name_len ) ? "~-" : "-" );
// Add a timestamp for uniqueness.
char buffer[suffix_len] {};
std::time_t t = std::time( nullptr );
std::strftime( buffer, suffix_len, "%Y-%m-%d-%H-%M-%S", std::localtime( &t ) );
memorial_file_path << buffer;
return memorial_file_path.str();
}
bool game::cleanup_at_end()
{
if( uquit == QUIT_DIED || uquit == QUIT_SUICIDE ) {
// Put (non-hallucinations) into the overmap so they are not lost.
for( monster &critter : all_monsters() ) {
despawn_monster( critter );
}
// Reset NPC factions and disposition
reset_npc_dispositions();
// Save the factions', missions and set the NPC's overmap coordinates
// Npcs are saved in the overmap.
save_factions_missions_npcs(); //missions need to be saved as they are global for all saves.
// save artifacts.
save_artifacts();
// and the overmap, and the local map.
save_maps(); //Omap also contains the npcs who need to be saved.
}
if( uquit == QUIT_DIED || 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_id( "CANNIBAL" ) ) || u.has_trait( trait_id( "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( " | |" );