-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
game.cpp
11947 lines (10704 loc) · 445 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 <wctype.h>
#include <assert.h>
#include <cstdio>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <ctime>
#include <iterator>
#include <locale>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <limits>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <unordered_map>
#include "action.h"
#include "activity_handlers.h"
#include "artifact.h"
#include "auto_pickup.h"
#include "avatar.h"
#include "avatar_action.h"
#include "bionics.h"
#include "bodypart.h"
#include "cata_utility.h"
#include "auto_note.h"
#include "catacharset.h"
#include "clzones.h"
#include "computer.h"
#include "construction.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "creature_tracker.h"
#include "cursesport.h"
#include "debug.h"
#include "dependency_tree.h"
#include "editmap.h"
#include "enums.h"
#include "faction.h"
#include "filesystem.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 "item_category.h"
#include "item_location.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_enchantment.h"
#include "map.h"
#include "map_item_stack.h"
#include "map_iterator.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 "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 "popup.h"
#include "recipe_dictionary.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_input_popup.h"
#include "submap.h"
#include "timed_event.h"
#include "translations.h"
#include "trap.h"
#include "uistate.h"
#include "veh_interact.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
#include "worldfactory.h"
#include "map_selector.h"
#include "basecamp.h"
#include "character.h"
#include "color.h"
#include "damage.h"
#include "field.h"
#include "item_stack.h"
#include "itype.h"
#include "iuse.h"
#include "player.h"
#include "player_activity.h"
#include "recipe.h"
#include "ret_val.h"
#include "tileray.h"
#include "ui.h"
#include "units.h"
#include "int_id.h"
#include "string_id.h"
#include "colony.h"
#include "item.h"
class inventory;
#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 to prevent reordering of #include "platform_win.h" by IWYU
# include "platform_win.h"
#endif
# include <tchar.h>
#endif
#define dbg(x) DebugLog((x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
const int core_version = 6;
static constexpr int DANGEROUS_PROXIMITY = 5;
/** Will be set to true when running unit tests */
bool test_mode = false;
const mtype_id mon_manhack( "mon_manhack" );
const skill_id skill_melee( "melee" );
const skill_id skill_dodge( "dodge" );
const skill_id skill_driving( "driving" );
const skill_id skill_firstaid( "firstaid" );
const skill_id skill_survival( "survival" );
const skill_id skill_electronics( "electronics" );
const skill_id skill_mechanics( "mechanics" );
const skill_id skill_computer( "computer" );
const species_id ZOMBIE( "ZOMBIE" );
const species_id PLANT( "PLANT" );
const efftype_id effect_adrenaline_mycus( "adrenaline_mycus" );
const efftype_id effect_alarm_clock( "alarm_clock" );
const efftype_id effect_amigara( "amigara" );
const efftype_id effect_assisted( "assisted" );
const efftype_id effect_blind( "blind" );
const efftype_id effect_boomered( "boomered" );
const efftype_id effect_bouldering( "bouldering" );
const efftype_id effect_contacts( "contacts" );
const efftype_id effect_controlled( "controlled" );
const efftype_id effect_deaf( "deaf" );
const efftype_id effect_docile( "docile" );
const efftype_id effect_downed( "downed" );
const efftype_id effect_drunk( "drunk" );
const efftype_id effect_emp( "emp" );
const efftype_id effect_evil( "evil" );
const efftype_id effect_flu( "flu" );
const efftype_id effect_glowing( "glowing" );
const efftype_id effect_hot( "hot" );
const efftype_id effect_infected( "infected" );
const efftype_id effect_laserlocked( "laserlocked" );
const efftype_id effect_no_sight( "no_sight" );
const efftype_id effect_onfire( "onfire" );
const efftype_id effect_pacified( "pacified" );
const efftype_id effect_paid( "paid" );
const efftype_id effect_pet( "pet" );
const efftype_id effect_relax_gas( "relax_gas" );
const efftype_id effect_sleep( "sleep" );
const efftype_id effect_stunned( "stunned" );
const efftype_id effect_teleglow( "teleglow" );
const efftype_id effect_tetanus( "tetanus" );
const efftype_id effect_visuals( "visuals" );
const efftype_id effect_winded( "winded" );
const efftype_id effect_ridden( "ridden" );
const efftype_id effect_riding( "riding" );
const efftype_id effect_has_bag( "has_bag" );
const efftype_id effect_harnessed( "harnessed" );
static const bionic_id bio_remote( "bio_remote" );
static const trait_id trait_GRAZER( "GRAZER" );
static const trait_id trait_ILLITERATE( "ILLITERATE" );
static const trait_id trait_INFIMMUNE( "INFIMMUNE" );
static const trait_id trait_INFRESIST( "INFRESIST" );
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_RUMINANT( "RUMINANT" );
static const trait_id trait_VINES2( "VINES2" );
static const trait_id trait_VINES3( "VINES3" );
const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
static const faction_id your_followers( "your_followers" );
void intro();
#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( const point &p )
{
return p.x >= 0 && p.x < TERRAIN_WINDOW_WIDTH && p.y >= 0 && p.y < TERRAIN_WINDOW_HEIGHT;
}
// This is the main game set-up process.
game::game() :
liveview( *liveview_ptr ),
scent_ptr( *this ),
m( *map_ptr ),
u( *u_ptr ),
scent( *scent_ptr ),
timed_events( *timed_event_manager_ptr ),
uquit( QUIT_NO ),
new_game( false ),
displaying_scent( false ),
displaying_temperature( false ),
displaying_visibility( false ),
displaying_radiation( false ),
safe_mode( SAFE_MODE_ON ),
pixel_minimap_option( 0 ),
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 ),
last_mouse_edge_scroll( std::chrono::steady_clock::now() )
{
player_was_sleeping = false;
reset_light_level();
events().subscribe( &*stats_tracker_ptr );
events().subscribe( &*kill_tracker_ptr );
events().subscribe( &*memorial_logger_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.
}
// 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 or game::load_core_data
get_auto_pickup().load_global();
get_safemode().load_global();
}
bool game::check_mod_data( const std::vector<mod_id> &opts, loading_ui &ui )
{
auto &tree = world_generator->get_mod_manager().get_tree();
// deduplicated list of mods to check
std::set<mod_id> check( opts.begin(), opts.end() );
// if no specific mods specified check all non-obsolete mods
if( check.empty() ) {
for( const mod_id &e : world_generator->get_mod_manager().all_mods() ) {
if( !e->obsolete ) {
check.emplace( e );
}
}
}
if( check.empty() ) {
world_generator->set_active_world( nullptr );
world_generator->init();
const std::vector<mod_id> mods_empty;
WORLDPTR test_world = world_generator->make_new_world( mods_empty );
world_generator->set_active_world( test_world );
// if no loadable mods then test core data only
try {
load_core_data( ui );
DynamicDataLoader::get_instance().finalize_loaded_data( ui );
} catch( const std::exception &err ) {
std::cerr << "Error loading data from json: " << err.what() << std::endl;
}
std::string world_name = world_generator->active_world->world_name;
world_generator->delete_world( world_name, true );
MAPBUFFER.reset();
overmap_buffer.clear();
}
for( const auto &e : check ) {
world_generator->set_active_world( nullptr );
world_generator->init();
const std::vector<mod_id> mods_empty;
WORLDPTR test_world = world_generator->make_new_world( mods_empty );
world_generator->set_active_world( test_world );
if( !e.is_valid() ) {
std::cerr << "Unknown mod: " << e.str() << std::endl;
return false;
}
const MOD_INFORMATION &mod = *e;
if( !tree.is_available( mod.ident ) ) {
std::cerr << "Missing dependencies: " << mod.name() << "\n"
<< tree.get_node( mod.ident )->s_errors() << std::endl;
return false;
}
std::cout << "Checking mod " << mod.name() << " [" << mod.ident.str() << "]" << std::endl;
try {
load_core_data( ui );
// Load any dependencies
for( auto &dep : tree.get_dependencies_of_X_as_strings( mod.ident ) ) {
load_data_from_dir( dep->path, dep->ident.str(), ui );
}
// Load mod itself
load_data_from_dir( mod.path, mod.ident.str(), ui );
DynamicDataLoader::get_instance().finalize_loaded_data( ui );
} catch( const std::exception &err ) {
std::cerr << "Error loading data: " << err.what() << std::endl;
}
std::string world_name = world_generator->active_world->world_name;
world_generator->delete_world( world_name, true );
MAPBUFFER.reset();
overmap_buffer.clear();
}
return true;
}
bool game::is_core_data_loaded() const
{
return DynamicDataLoader::get_instance().is_data_finalized();
}
void game::load_core_data( loading_ui &ui )
{
// core data can be loaded only once and must be first
// anyway.
DynamicDataLoader::get_instance().unload_data();
load_data_from_dir( FILENAMES[ "jsondir" ], "core", ui );
}
void game::load_data_from_dir( const std::string &path, const std::string &src, loading_ui &ui )
{
DynamicDataLoader::get_instance().load_data_from_path( path, src, ui );
}
game::~game()
{
MAPBUFFER.reset();
}
// Fixed window sizes
#define MINIMAP_HEIGHT 7
#define MINIMAP_WIDTH 7
void game::init_ui( const bool resized )
{
// clear the screen
static bool first_init = true;
if( first_init ) {
catacurses::clear();
// print an intro screen, making sure the terminal is the correct size
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();
if( resized ) {
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
( void ) resized;
intro();
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
// remove some space for the sidebar, this is the maximal space
// (using standard font) that the terrain window can have
int sidebar_left = panel_manager::get_manager().get_width_left();
int sidebar_right = panel_manager::get_manager().get_width_right();
TERRAIN_WINDOW_HEIGHT = TERMY;
TERRAIN_WINDOW_WIDTH = TERMX - ( sidebar_left + sidebar_right );
TERRAIN_WINDOW_TERM_WIDTH = TERRAIN_WINDOW_WIDTH;
TERRAIN_WINDOW_TERM_HEIGHT = TERRAIN_WINDOW_HEIGHT;
/**
* In tiles mode w_terrain can have a different font (with a different
* tile dimension) or can be drawn by cata_tiles which uses tiles that again
* might have a different dimension then the normal font used everywhere else.
*
* TERRAIN_WINDOW_WIDTH/TERRAIN_WINDOW_HEIGHT defines how many squares can
* be displayed in w_terrain (using it's specific tile dimension), not
* including partially drawn squares at the right/bottom. You should
* use it whenever you want to draw specific squares in that window or to
* determine whether a specific square is draw on screen (or outside the screen
* and needs scrolling).
*
* TERRAIN_WINDOW_TERM_WIDTH/TERRAIN_WINDOW_TERM_HEIGHT defines the size of
* w_terrain in the standard font dimension (the font that everything else uses).
* You usually don't have to use it, expect for positioning of windows,
* because the window positions use the standard font dimension.
*
* VIEW_OFFSET_X/VIEW_OFFSET_Y is the position of w_terrain on screen,
* it is (as every window position) in standard font dimension.
* As the sidebar is located right of w_terrain it also controls its position.
* It was used to move everything into the center of the screen,
* when the screen was larger than what the game required. They are currently
* set to zero to prevent the other game windows from being truncated if
* w_terrain is too small for the current window.
*
* The code here calculates size available for w_terrain, caps it at
* max_view_size (the maximal view range than any character can have at
* any time).
* It is stored in TERRAIN_WINDOW_*.
* If w_terrain does not occupy the whole available area, VIEW_OFFSET_*
* are set to move everything into the middle of the screen.
*/
to_map_font_dimension( TERRAIN_WINDOW_WIDTH, TERRAIN_WINDOW_HEIGHT );
VIEW_OFFSET_X = 0;
VIEW_OFFSET_Y = 0;
// VIEW_OFFSET_* are in standard font dimension.
from_map_font_dimension( VIEW_OFFSET_X, VIEW_OFFSET_Y );
// Position of the player in the terrain window, it is always in the center
POSX = TERRAIN_WINDOW_WIDTH / 2;
POSY = TERRAIN_WINDOW_HEIGHT / 2;
w_terrain = w_terrain_ptr = catacurses::newwin( TERRAIN_WINDOW_HEIGHT, TERRAIN_WINDOW_WIDTH,
point( VIEW_OFFSET_X + sidebar_left, VIEW_OFFSET_Y ) );
werase( w_terrain );
/**
* Doing the same thing as above for the overmap
*/
static const int OVERMAP_LEGEND_WIDTH = 28;
OVERMAP_WINDOW_HEIGHT = TERMY;
OVERMAP_WINDOW_WIDTH = TERMX - OVERMAP_LEGEND_WIDTH;
to_overmap_font_dimension( OVERMAP_WINDOW_WIDTH, OVERMAP_WINDOW_HEIGHT );
//Bring the framebuffer to the maximum required dimensions
//Otherwise it segfaults when the overmap needs a bigger buffer size than it provides
reinitialize_framebuffer();
// minimap is always MINIMAP_WIDTH x MINIMAP_HEIGHT in size
int _y = VIEW_OFFSET_Y;
int _x = VIEW_OFFSET_X;
w_minimap = w_minimap_ptr = catacurses::newwin( MINIMAP_HEIGHT, MINIMAP_WIDTH, point( _x, _y ) );
werase( w_minimap );
w_panel_adm = w_panel_adm_ptr = catacurses::newwin( 20, 75, point( ( TERMX / 2 ) - 38,
( TERMY / 2 ) - 10 ) );
werase( w_panel_adm );
// need to init in order to avoid crash. gets updated by the panel code.
w_pixel_minimap = catacurses::newwin( 1, 1, point_zero );
liveview.init();
// Only refresh if we are in-game, otherwise all resources are not initialized
// and this refresh will crash the game.
if( resized && u.getID().is_valid() ) {
refresh_all();
}
}
void game::toggle_fullscreen()
{
#if !defined(TILES)
fullscreen = !fullscreen;
init_ui();
refresh_all();
#else
toggle_fullscreen_window();
refresh_all();
#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;
init_ui();
refresh_all();
#endif // TILES
}
void game::toggle_panel_adm()
{
show_panel_adm = !show_panel_adm;
}
void game::reload_tileset()
{
#if defined(TILES)
try {
tilecontext->reinit();
tilecontext->load_tileset( get_option<std::string>( "TILES" ), false, true );
tilecontext->do_tile_loading_report();
} catch( const std::exception &err ) {
popup( _( "Loading the tileset failed: %s" ), err.what() );
}
g->reset_zoom();
g->refresh_all();
#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()
{
popup_status( _( "Please wait while the world data loads..." ), _( "Loading core data" ) );
loading_ui ui( true );
load_core_data( ui );
load_world_modfiles( ui );
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" ) );
weather.weather = WEATHER_CLEAR; // Start with some nice weather...
// Weather shift in 30
weather.nextweather = calendar::start_of_cataclysm + time_duration::from_hours(
get_option<int>( "INITIAL_TIME" ) ) + 30_minutes;
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();
SCT.vSCT.clear(); //Delete pending messages
stats().clear();
// reset kill counts
kill_tracker_ptr->clear();
// reset follower list
follower_ids.clear();
scent.reset();
remoteveh_cache_time = calendar::before_time_starts;
remoteveh_cache = nullptr;
// back to menu for save loading, new game etc
}
bool game::has_gametype() const
{
return gamemode && gamemode->id() != SGAME_NULL;
}
special_game_id game::gametype() const
{
return gamemode ? gamemode->id() : SGAME_NULL;
}
void game::load_map( const tripoint &pos_sm )
{
m.load( pos_sm, true );
}
// 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();
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();
init_autosave();
catacurses::clear();
catacurses::refresh();
popup_nowait( _( "Please wait as we build your world" ) );
load_master();
u.setID( assign_npc_id() ); // should be as soon as possible, but *after* load_master
const start_location &start_loc = u.start_location.obj();
const tripoint omtstart = start_loc.find_player_initial_location();
if( omtstart == overmap::invalid_tripoint ) {
return false;
}
start_loc.prepare_map( omtstart );
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() );
}
tripoint lev = omt_to_sm_copy( omtstart );
// 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 );
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.stamina = u.get_stamina_max();
weather.temperature = SPRING_TEMPERATURE;
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();
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(), true );
}
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;
}
}
}
}
for( auto &e : u.inv_dump() ) {
e->set_owner( g->u.get_faction() );
}
// Now that we're done handling coordinates, ensure the player's submap is in the center of the map
update_map( u );
// Profession pets
if( !u.starting_pets.empty() ) {
std::vector<tripoint> valid;
for( const tripoint &jk : g->m.points_in_radius( u.pos(), 5 ) ) {
if( is_empty( jk ) ) {
valid.push_back( jk );
}
}
for( auto elem : u.starting_pets ) {
if( !valid.empty() ) {
tripoint chosen = random_entry( valid );
valid.erase( std::remove( valid.begin(), valid.end(), chosen ), valid.end() );
monster *mon = summon_mon( elem, chosen );
mon->friendly = -1;
mon->add_effect( effect_pet, 1_turns, num_bp, true );
} 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() );
return true;
}
//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<std::shared_ptr<npc>> just_added;
for( const auto &temp : overmap_buffer.get_npcs_near_player( radius ) ) {
if( temp->is_active() ) {
continue;
}
if( ( temp->has_companion_mission() ) && ( temp->mission != NPC_MISSION_TRAVELLING ) ) {
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
}
std::shared_ptr<npc> tmp = std::make_shared<npc>();
tmp->normalize();
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" ) );
//One random starting NPC mission
tmp->add_new_mission( mission::reserve_random( ORIGIN_OPENER_NPC, tmp->global_omt_location(),
tmp->getID() ) );
}
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;