forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
9521 lines (8759 loc) · 288 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 "rng.h"
#include "input.h"
#include "keypress.h"
#include "output.h"
#include "skill.h"
#include "line.h"
#include "computer.h"
#include "veh_interact.h"
#include "options.h"
#include "mapbuffer.h"
#include "debug.h"
#include "bodypart.h"
#include "map.h"
#include "output.h"
#include "item_factory.h"
#include "helper.h"
#include "text_snippets.h"
#include "catajson.h"
#include "artifact.h"
#include "overmapbuffer.h"
#include <map>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <dirent.h>
#endif
#include <sys/stat.h>
#include "debug.h"
#include "artifactdata.h"
#if (defined _WIN32 || defined __WIN32__)
#include <windows.h>
#include <tchar.h>
#endif
#define dbg(x) dout((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
#define MAX_ITEM_IN_SQUARE 64
void intro();
nc_color sev(int a); // Right now, ONLY used for scent debugging....
// This is the main game set-up process.
game::game() :
w_terrain(NULL),
w_minimap(NULL),
w_HP(NULL),
w_moninfo(NULL),
w_messages(NULL),
w_location(NULL),
w_status(NULL),
om_hori(NULL),
om_vert(NULL),
om_diag(NULL),
gamemode(NULL)
{
dout() << "Game initialized.";
// Gee, it sure is init-y around here!
init_skills();
init_bionics(); // Set up bionics (SEE bionics.cpp)
init_itypes(); // Set up item types (SEE itypedef.cpp)
SNIPPET.load();
item_controller->init(this); //Item manager
init_mtypes(); // Set up monster types (SEE mtypedef.cpp)
init_monitems(); // Set up the items monsters carry (SEE monitemsdef.cpp)
init_traps(); // Set up the trap types (SEE trapdef.cpp)
init_mapitems(); // Set up which items appear where (SEE mapitemsdef.cpp)
init_recipes(); // Set up crafting reciptes (SEE crafting.cpp)
init_mongroups(); // Set up monster groupings (SEE mongroupdef.cpp)
init_missions(); // Set up mission templates (SEE missiondef.cpp)
init_construction(); // Set up constructables (SEE construction.cpp)
init_mutations();
init_vehicles(); // Set up vehicles (SEE veh_typedef.cpp)
init_autosave(); // Set up autosave
load_keyboard_settings();
gamemode = new special_game; // Nothing, basically.
}
game::~game()
{
delete gamemode;
itypes.clear();
for (int i = 0; i < mtypes.size(); i++)
delete mtypes[i];
delwin(w_terrain);
delwin(w_minimap);
delwin(w_HP);
delwin(w_moninfo);
delwin(w_messages);
delwin(w_location);
delwin(w_status);
}
void game::init_skills()
{
Skill::skills = Skill::loadSkills();
}
void game::init_ui(){
clear(); // Clear the screen
intro(); // Print an intro screen, make sure we're at least 80x25
#if (defined _WIN32 || defined __WIN32__)
TERMX = 55 + (OPTIONS[OPT_VIEWPORT_X] * 2 + 1);
TERMY = OPTIONS[OPT_VIEWPORT_Y] * 2 + 1;
VIEWX = (OPTIONS[OPT_VIEWPORT_X] > 60) ? 60 : OPTIONS[OPT_VIEWPORT_X];
VIEWY = (OPTIONS[OPT_VIEWPORT_Y] > 60) ? 60 : OPTIONS[OPT_VIEWPORT_Y];
VIEW_OFFSET_X = (OPTIONS[OPT_VIEWPORT_X] > 60) ? OPTIONS[OPT_VIEWPORT_X]-60 : 0;
VIEW_OFFSET_Y = (OPTIONS[OPT_VIEWPORT_Y] > 60) ? OPTIONS[OPT_VIEWPORT_Y]-60 : 0;
TERRAIN_WINDOW_WIDTH = (VIEWX * 2) + 1;
TERRAIN_WINDOW_HEIGHT = (VIEWY * 2) + 1;
#else
getmaxyx(stdscr, TERMY, TERMX);
//make sure TERRAIN_WINDOW_WIDTH and TERRAIN_WINDOW_HEIGHT are uneven
if (TERMX%2 == 1) {
TERMX--;
}
if (TERMY%2 == 0) {
TERMY--;
}
TERRAIN_WINDOW_WIDTH = (TERMX - STATUS_WIDTH > 121) ? 121 : TERMX - STATUS_WIDTH;
TERRAIN_WINDOW_HEIGHT = (TERMY > 121) ? 121 : TERMY;
VIEW_OFFSET_X = (TERMX - STATUS_WIDTH > 121) ? (TERMX - STATUS_WIDTH - 121)/2 : 0;
VIEW_OFFSET_Y = (TERMY > 121) ? (TERMY - 121)/2 : 0;
VIEWX = (TERRAIN_WINDOW_WIDTH - 1) / 2;
VIEWY = (TERRAIN_WINDOW_HEIGHT - 1) / 2;
#endif
if (VIEWX < 12) {
VIEWX = 12;
}
if (VIEWY < 12) {
VIEWY = 12;
}
// Set up the main UI windows.
w_terrain = newwin(TERRAIN_WINDOW_HEIGHT, TERRAIN_WINDOW_WIDTH, VIEW_OFFSET_Y, VIEW_OFFSET_X);
werase(w_terrain);
w_minimap = newwin(MINIMAP_HEIGHT, MINIMAP_WIDTH, VIEW_OFFSET_Y, TERMX - MONINFO_WIDTH - MINIMAP_WIDTH - VIEW_OFFSET_X);
werase(w_minimap);
w_HP = newwin(HP_HEIGHT, HP_WIDTH, VIEW_OFFSET_Y + MINIMAP_HEIGHT, TERMX - MESSAGES_WIDTH - HP_WIDTH - VIEW_OFFSET_X);
werase(w_HP);
w_moninfo = newwin(MONINFO_HEIGHT, MONINFO_WIDTH, VIEW_OFFSET_Y, TERMX - MONINFO_WIDTH - VIEW_OFFSET_X);
werase(w_moninfo);
w_messages = newwin(MESSAGES_HEIGHT, MESSAGES_WIDTH, MONINFO_HEIGHT + VIEW_OFFSET_Y, TERMX - MESSAGES_WIDTH - VIEW_OFFSET_X);
werase(w_messages);
w_location = newwin(LOCATION_HEIGHT, LOCATION_WIDTH, MONINFO_HEIGHT+MESSAGES_HEIGHT + VIEW_OFFSET_Y, TERMX - LOCATION_WIDTH - VIEW_OFFSET_X);
werase(w_location);
w_status = newwin(STATUS_HEIGHT, STATUS_WIDTH, MONINFO_HEIGHT+MESSAGES_HEIGHT+LOCATION_HEIGHT + VIEW_OFFSET_Y, TERMX - STATUS_WIDTH - VIEW_OFFSET_X);
werase(w_status);
w_void = newwin(TERMY-(MONINFO_HEIGHT+MESSAGES_HEIGHT+LOCATION_HEIGHT+STATUS_HEIGHT), STATUS_WIDTH, MONINFO_HEIGHT+MESSAGES_HEIGHT+LOCATION_HEIGHT+STATUS_HEIGHT + VIEW_OFFSET_Y, TERMX - STATUS_WIDTH - VIEW_OFFSET_X);
werase(w_void);
}
void game::setup()
{
u = player();
m = map(&itypes, &mapitems, &traps); // Init the root map with our vectors
z.reserve(1000); // Reserve some space
// Even though we may already have 'd', nextinv will be incremented as needed
nextinv = 'd';
next_npc_id = 1;
next_faction_id = 1;
next_mission_id = 1;
// Clear monstair values
monstairx = -1;
monstairy = -1;
monstairz = -1;
last_target = -1; // We haven't targeted any monsters yet
curmes = 0; // We haven't read any messages yet
uquit = QUIT_NO; // We haven't quit the game
debugmon = false; // We're not printing debug messages
weather = WEATHER_CLEAR; // Start with some nice weather...
nextweather = MINUTES(STARTING_MINUTES + 30); // Weather shift in 30
turnssincelastmon = 0; //Auto safe mode init
autosafemode = OPTIONS[OPT_AUTOSAFEMODE];
footsteps.clear();
footsteps_source.clear();
z.clear();
coming_to_stairs.clear();
active_npc.clear();
factions.clear();
active_missions.clear();
items_dragged.clear();
messages.clear();
events.clear();
turn.set_season(SUMMER); // ... with winter conveniently a long ways off
for (int i = 0; i < num_monsters; i++) // Reset kill counts to 0
kills[i] = 0;
// Set the scent map to 0
for (int i = 0; i < SEEX * MAPSIZE; i++) {
for (int j = 0; j < SEEX * MAPSIZE; j++)
grscent[i][j] = 0;
}
if (opening_screen()) {// Opening menu
// Finally, draw the screen!
refresh_all();
draw();
}
}
// Set up all default values for a new game
void game::start_game()
{
//turn = MINUTES(STARTING_MINUTES);// It's turn 0...
turn = HOURS(OPTIONS[OPT_INITIAL_TIME]);
run_mode = (OPTIONS[OPT_SAFEMODE] ? 1 : 0);
mostseen = 0; // ...and mostseen is 0, we haven't seen any monsters yet.
popup_nowait("Please wait as we build your world");
// Init some factions.
if (!load_master()) // Master data record contains factions.
create_factions();
cur_om = &overmap_buffer.get(this, 0, 0); // We start in the (0,0,0) overmap.
// Find a random house on the map, and set us there.
cur_om->first_house(levx, levy);
levx -= int(int(MAPSIZE / 2) / 2);
levy -= int(int(MAPSIZE / 2) / 2);
levz = 0;
// Start the overmap with out immediate neighborhood visible
for (int i = -15; i <= 15; i++) {
for (int j = -15; j <= 15; j++)
cur_om->seen(levx + i, levy + j, 0) = true;
}
// Convert the overmap coordinates to submap coordinates
levx = levx * 2 - 1;
levy = levy * 2 - 1;
set_adjacent_overmaps(true);
// Init the starting map at this location.
m.load(this, levx, levy, levz);
// Start us off somewhere in the shelter.
u.posx = SEEX * int(MAPSIZE / 2) + 5;
u.posy = SEEY * int(MAPSIZE / 2) + 6;
u.str_cur = u.str_max;
u.per_cur = u.per_max;
u.int_cur = u.int_max;
u.dex_cur = u.dex_max;
nextspawn = int(turn);
temperature = 65; // Springtime-appropriate?
//Load NPCs. Set nearby npcs to active.
load_npcs();
//spawn the monsters
m.spawn_monsters(this); // Static monsters
//Put some NPCs in there!
create_starting_npcs();
MAPBUFFER.set_dirty();
}
void game::create_factions()
{
int num = dice(4, 3);
faction tmp(0);
tmp.make_army();
factions.push_back(tmp);
for (int i = 0; i < num; i++) {
tmp = faction(assign_faction_id());
tmp.randomize();
tmp.likes_u = 100;
tmp.respects_u = 100;
tmp.known_by_u = true;
factions.push_back(tmp);
}
}
//Make any nearby overmap npcs active, and put them in the right location.
void game::load_npcs()
{
for (int i = 0; i < cur_om->npcs.size(); i++)
{
if (rl_dist(levx + int(MAPSIZE / 2), levy + int(MAPSIZE / 2),
cur_om->npcs[i]->mapx, cur_om->npcs[i]->mapy) <=
int(MAPSIZE / 2) + 1 && !cur_om->npcs[i]->is_active(this))
{
int dx = cur_om->npcs[i]->mapx - levx, dy = cur_om->npcs[i]->mapy - levy;
if (debugmon)debugmsg("game::load_npcs: Spawning static NPC, %d:%d (%d:%d)", levx, levy, cur_om->npcs[i]->mapx, cur_om->npcs[i]->mapy);
npc * temp = cur_om->npcs[i];
if (temp->posx == -1 || temp->posy == -1)
{
dbg(D_ERROR) << "game::load_npcs: Static NPC with no fine location "
"data (" << temp->posx << ":" << temp->posy << ").";
debugmsg("game::load_npcs Static NPC with no fine location data (%d:%d) New loc data (%d:%d).",
temp->posx, temp->posy, SEEX * 2 * (temp->mapx - levx) + rng(0 - SEEX, SEEX),
SEEY * 2 * (temp->mapy - levy) + rng(0 - SEEY, SEEY));
temp->posx = SEEX * 2 * (temp->mapx - levx) + rng(0 - SEEX, SEEX);
temp->posy = SEEY * 2 * (temp->mapy - levy) + rng(0 - SEEY, SEEY);
} else {
if (debugmon) debugmsg("game::load_npcs Static NPC fine location %d:%d (%d:%d)", temp->posx, temp->posy, temp->posx + dx * SEEX, temp->posy + dy * SEEY);
temp->posx += dx * SEEX;
temp->posy += dy * SEEY;
}
//check if the loaded position doesn't already contain an object, monster or npc.
//If it isn't free, spiralsearch for a free spot.
temp->place_near(this, temp->posx, temp->posy);
//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(this, false);
else
active_npc.push_back(temp);
}
}
}
void game::create_starting_npcs()
{
if(!OPTIONS[OPT_STATIC_NPC])
return; //Do not generate a starting npc.
npc * tmp = new npc();
tmp->normalize(this);
tmp->randomize(this, (one_in(2) ? NC_DOCTOR : NC_NONE));
tmp->spawn_at(cur_om, levx, levy, levz); //spawn the npc in the overmap.
tmp->place_near(this, SEEX * int(MAPSIZE / 2) + SEEX, SEEY * int(MAPSIZE / 2) + 6);
tmp->form_opinion(&u);
tmp->attitude = NPCATT_NULL;
tmp->mission = NPC_MISSION_SHELTER; //This sets the npc mission. This NPC remains in the shelter.
tmp->chatbin.first_topic = TALK_SHELTER;
tmp->chatbin.missions.push_back(
reserve_random_mission(ORIGIN_OPENER_NPC, om_location(), tmp->getID()) ); //one random shelter mission/
active_npc.push_back(tmp);
}
void game::cleanup_at_end(){
write_msg();
if (uquit == QUIT_DIED || uquit == QUIT_SUICIDE || uquit == QUIT_SAVED)
{
// Save the factions's, missions and set the NPC's overmap coords
// 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.
}
// Save the monsters before we die!
despawn_monsters();
// Clear the future weather for future projects
future_weather.clear();
if (uquit == QUIT_DIED)
popup_top("Game over! Press spacebar...");
if (uquit == QUIT_DIED || uquit == QUIT_SUICIDE)
{
death_screen();
if (OPTIONS[OPT_DELETE_WORLD] == 1
|| (OPTIONS[OPT_DELETE_WORLD] == 2 && query_yn("Delete saved world?")))
{
delete_save();
MAPBUFFER.reset();
MAPBUFFER.make_volatile();
}
if(gamemode)
{
delete gamemode;
gamemode = new special_game; // null gamemode or something..
}
}
}
// MAIN GAME LOOP
// Returns true if game is over (death, saved, quit, etc)
bool game::do_turn()
{
if (is_game_over()) {
cleanup_at_end();
return true;
}
// Actual stuff
gamemode->per_turn(this);
turn.increment();
process_events();
process_missions();
if (turn.hours() == 0 && turn.minutes() == 0 && turn.seconds() == 0) // Midnight!
cur_om->process_mongroups();
// Check if we've overdosed... in any deadly way.
if (u.stim > 250) {
add_msg("You have a sudden heart attack!");
u.hp_cur[hp_torso] = 0;
} else if (u.stim < -200 || u.pkill > 240) {
add_msg("Your breathing stops completely.");
u.hp_cur[hp_torso] = 0;
}
// Check if we're starving or have starved
if (u.hunger > 2999) {
switch (u.hunger) {
case 3000: if (turn % 10 == 0)
add_msg("You haven't eaten in over a week!"); break;
case 4000: if (turn % 10 == 0)
add_msg("You are STARVING!"); break;
case 5000: if (turn % 10 == 0)
add_msg("Food..."); break;
case 6000:
add_msg("You have starved to death.");
u.hp_cur[hp_torso] = 0;
break;
}
}
// Check if we're dying of thirst
if (u.thirst > 599) {
switch (u.thirst) {
case 600: if (turn % 10 == 0)
add_msg("You haven't had anything to drink in 2 days!"); break;
case 800: if (turn % 10 == 0)
add_msg("You are THIRSTY!"); break;
case 1000: if (turn % 10 == 0)
add_msg("4 days... no water.."); break;
case 1200:
add_msg("You have died of dehydration.");
u.hp_cur[hp_torso] = 0;
break;
}
}
// Check if we're falling asleep
if (u.fatigue > 599) {
switch (u.fatigue) {
case 600: if (turn % 10 == 0)
add_msg("You haven't slept in 2 days!"); break;
case 800: if (turn % 10 == 0)
add_msg("Anywhere would be a good place to sleep..."); break;
case 1000:
add_msg("Surivor sleep now.");
u.fatigue -= 10;
u.try_to_sleep(this);
break;
}
}
if (turn % 50 == 0) { // Hunger, thirst, & fatigue up every 5 minutes
if ((!u.has_trait(PF_LIGHTEATER) || !one_in(3)) &&
(!u.has_bionic("bio_recycler") || turn % 300 == 0))
u.hunger++;
if ((!u.has_bionic("bio_recycler") || turn % 100 == 0) &&
(!u.has_trait(PF_PLANTSKIN) || !one_in(5)))
u.thirst++;
u.fatigue++;
if (u.fatigue == 192 && !u.has_disease(DI_LYING_DOWN) &&
!u.has_disease(DI_SLEEP)) {
if (u.activity.type == ACT_NULL)
add_msg("You're feeling tired. Press '$' to lie down for sleep.");
else
cancel_activity_query("You're feeling tired.");
}
if (u.stim < 0)
u.stim++;
if (u.stim > 0)
u.stim--;
if (u.pkill > 0)
u.pkill--;
if (u.pkill < 0)
u.pkill++;
if (u.has_bionic("bio_solar") && is_in_sunlight(u.posx, u.posy))
u.charge_power(1);
}
if (turn % 300 == 0) { // Pain up/down every 30 minutes
if (u.pain > 0)
u.pain -= 1 + int(u.pain / 10);
else if (u.pain < 0)
u.pain++;
// Mutation healing effects
if (u.has_trait(PF_FASTHEALER2) && one_in(5))
u.healall(1);
if (u.has_trait(PF_REGEN) && one_in(2))
u.healall(1);
if (u.has_trait(PF_ROT2) && one_in(5))
u.hurtall(1);
if (u.has_trait(PF_ROT3) && one_in(2))
u.hurtall(1);
if (u.radiation > 1 && one_in(3))
u.radiation--;
u.get_sick(this);
// Auto-save on the half-hour if autosave is enabled
if (OPTIONS[OPT_AUTOSAVE])
autosave();
}
update_weather();
// The following happens when we stay still; 10/40 minutes overdue for spawn
if ((!u.has_trait(PF_INCONSPICUOUS) && turn > nextspawn + 100) ||
( u.has_trait(PF_INCONSPICUOUS) && turn > nextspawn + 400) ) {
spawn_mon(-1 + 2 * rng(0, 1), -1 + 2 * rng(0, 1));
nextspawn = turn;
}
process_activity();
if(u.moves > 0) {
while (u.moves > 0) {
cleanup_dead();
if (!u.has_disease(DI_SLEEP) && u.activity.type == ACT_NULL)
draw();
if(handle_action())
++moves_since_last_save;
if (is_game_over()) {
cleanup_at_end();
return true;
}
}
} else {
handle_key_blocking_activity();
}
update_scent();
m.vehmove(this);
m.process_fields(this);
m.process_active_items(this);
m.step_in_field(u.posx, u.posy, this);
monmove();
update_stair_monsters();
u.reset(this);
u.process_active_items(this);
u.suffer(this);
if (levz >= 0) {
weather_effect weffect;
(weffect.*(weather_data[weather].effect))(this);
}
if (u.has_disease(DI_SLEEP) && int(turn) % 300 == 0) {
draw();
refresh();
}
u.update_bodytemp(this);
rustCheck();
if (turn % 10 == 0)
u.update_morale();
return false;
}
void game::rustCheck()
{
bool forgetful = u.has_trait(PF_FORGETFUL);
for (std::vector<Skill*>::iterator aSkill = ++Skill::skills.begin();
aSkill != Skill::skills.end(); ++aSkill) {
bool charged_bio_mem = u.has_bionic("bio_memory") && u.power_level > 0;
int oldSkillLevel = u.skillLevel(*aSkill);
if (u.skillLevel(*aSkill).rust(turn, forgetful, charged_bio_mem))
{
u.power_level--;
}
int newSkill =u.skillLevel(*aSkill);
if (newSkill < oldSkillLevel)
{
add_msg("Your skill in %s has reduced to %d!",
(*aSkill)->name().c_str(), newSkill);
}
}
}
void game::process_events()
{
for (int i = 0; i < events.size(); i++) {
events[i].per_turn(this);
if (events[i].turn <= int(turn)) {
events[i].actualize(this);
events.erase(events.begin() + i);
i--;
}
}
}
void game::process_activity()
{
it_book* reading;
bool no_recipes;
if (u.activity.type != ACT_NULL) {
if (int(turn) % 150 == 0)
draw();
if (u.activity.type == ACT_WAIT) { // Based on time, not speed
u.activity.moves_left -= 100;
u.pause(this);
} else if (u.activity.type == ACT_REFILL_VEHICLE) {
vehicle *veh = m.veh_at( u.activity.placement.x, u.activity.placement.y );
if (!veh) { // Vehicle must've moved or something!
u.activity.moves_left = 0;
return;
}
veh->refill (AT_GAS, 200);
if(one_in(800)) {
// Scan for the gas pump we're refuelling from and deactivate it.
for(int i = -1; i <= 1; i++)
for(int j = -1; j <= 1; j++)
if(m.ter(u.posx + i, u.posy + j) == t_gas_pump) {
add_msg("With a clang and a shudder, the gas pump goes silent.");
m.ter_set(u.posx + i, u.posy + j, t_gas_pump_empty);
u.activity.moves_left = 0;
// Found it, break out of the loop.
i = 2;
j = 2;
break;
}
}
u.pause(this);
u.activity.moves_left -= 100;
} else {
u.activity.moves_left -= u.moves;
u.moves = 0;
}
if (u.activity.moves_left <= 0) { // We finished our activity!
switch (u.activity.type) {
case ACT_RELOAD:
if (u.weapon.reload(u, u.activity.invlet))
if (u.weapon.is_gun() && u.weapon.has_flag("RELOAD_ONE")) {
add_msg("You insert a cartridge into your %s.",
u.weapon.tname(this).c_str());
if (u.recoil < 8)
u.recoil = 8;
if (u.recoil > 8)
u.recoil = (8 + u.recoil) / 2;
} else {
add_msg("You reload your %s.", u.weapon.tname(this).c_str());
u.recoil = 6;
}
else
add_msg("Can't reload your %s.", u.weapon.tname(this).c_str());
break;
case ACT_READ:
if (u.activity.index == -2)
reading = dynamic_cast<it_book*>(u.weapon.type);
else
reading = dynamic_cast<it_book*>(u.inv.item_by_letter(u.activity.invlet).type);
if (reading->fun != 0) {
std::stringstream morale_text;
u.add_morale(MORALE_BOOK, reading->fun * 5, reading->fun * 15, reading);
}
no_recipes = true;
if (reading->recipes.size() > 0)
{
bool recipe_learned = false;
recipe_learned = u.try_study_recipe(this, reading);
if (!u.studied_all_recipes(reading))
{
no_recipes = false;
}
// for books that the player cannot yet read due to skill level, but contain
// lower level recipes, break out once recipe has been studied
if ((u.skillLevel(reading->type) < (int)reading->req))
{
if (recipe_learned)
add_msg("The rest of the book is currently still beyond your understanding.");
break;
}
}
if (u.skillLevel(reading->type) < (int)reading->level) {
int min_ex = reading->time / 10 + u.int_cur / 4,
max_ex = reading->time / 5 + u.int_cur / 2 - u.skillLevel(reading->type);
if (min_ex < 1)
min_ex = 1;
if (max_ex < 2)
max_ex = 2;
if (max_ex > 10)
max_ex = 10;
int originalSkillLevel = u.skillLevel(reading->type);
u.skillLevel(reading->type).readBook(min_ex, max_ex, turn, reading->level);
add_msg("You learn a little about %s! (%d%%%%)", reading->type->name().c_str(),
u.skillLevel(reading->type).exercise());
if (u.skillLevel(reading->type) == originalSkillLevel && (u.activity.continuous || query_yn("Study %s?", reading->type->name().c_str()))) {
u.cancel_activity();
if (u.activity.index == -2) {
u.read(this,u.weapon.invlet);
} else {
u.read(this,u.activity.invlet);
}
if (u.activity.type != ACT_NULL) {
u.activity.continuous = true;
return;
}
}
u.activity.continuous = false;
if (u.skillLevel(reading->type) > originalSkillLevel)
add_msg("You increase %s to level %d.",
reading->type->name().c_str(),
(int)u.skillLevel(reading->type));
if (u.skillLevel(reading->type) == (int)reading->level) {
if (no_recipes) {
add_msg("You can no longer learn from %s.", reading->name.c_str());
} else {
add_msg("Your skill level won't improve, but %s has more recipes for you.", reading->name.c_str());
}
}
}
break;
case ACT_WAIT:
add_msg("You finish waiting.");
break;
case ACT_CRAFT:
case ACT_LONGCRAFT:
complete_craft();
break;
case ACT_DISASSEMBLE:
complete_disassemble();
break;
case ACT_BUTCHER:
complete_butcher(u.activity.index);
break;
case ACT_FORAGE:
forage();
break;
case ACT_BUILD:
complete_construction();
break;
case ACT_TRAIN:
if (u.activity.index < 0) {
add_msg("You learn %s.", martial_arts_itype_ids[0 - u.activity.index].c_str());
u.styles.push_back( martial_arts_itype_ids[0 - u.activity.index] );
} else {
u.sklevel[ u.activity.index ]++;
int skillLevel = u.skillLevel(u.activity.index);
u.skillLevel(u.activity.index).level(skillLevel + 1);
add_msg("You finish training %s to level %d.",
skill_name(u.activity.index).c_str(),
(int)u.skillLevel(u.activity.index));
}
break;
case ACT_VEHICLE:
complete_vehicle (this);
break;
}
bool act_veh = (u.activity.type == ACT_VEHICLE);
bool act_longcraft = (u.activity.type == ACT_LONGCRAFT);
u.activity.type = ACT_NULL;
if (act_veh) {
if (u.activity.values.size() < 7)
{
dbg(D_ERROR) << "game:process_activity: invalid ACT_VEHICLE values: "
<< u.activity.values.size();
debugmsg ("process_activity invalid ACT_VEHICLE values:%d",
u.activity.values.size());
}
else {
vehicle *veh = m.veh_at(u.activity.values[0], u.activity.values[1]);
if (veh) {
exam_vehicle(*veh, u.activity.values[0], u.activity.values[1],
u.activity.values[2], u.activity.values[3]);
return;
} else
{
dbg(D_ERROR) << "game:process_activity: ACT_VEHICLE: vehicle not found";
debugmsg ("process_activity ACT_VEHICLE: vehicle not found");
}
}
} else if (act_longcraft) {
if (making_would_work(u.lastrecipe))
make_all_craft(u.lastrecipe);
}
}
}
}
void game::cancel_activity()
{
u.cancel_activity();
}
void game::cancel_activity_query(const char* message, ...)
{
char buff[1024];
va_list ap;
va_start(ap, message);
vsprintf(buff, message, ap);
va_end(ap);
std::string s(buff);
bool doit = false;;
switch (u.activity.type) {
case ACT_NULL:
doit = false;
break;
case ACT_READ:
if (query_yn("%s Stop reading?", s.c_str()))
doit = true;
break;
case ACT_RELOAD:
if (query_yn("%s Stop reloading?", s.c_str()))
doit = true;
break;
case ACT_CRAFT:
case ACT_LONGCRAFT:
if (query_yn("%s Stop crafting?", s.c_str()))
doit = true;
break;
case ACT_DISASSEMBLE:
if (query_yn("%s Stop disassembly?", s.c_str()))
doit = true;
break;
case ACT_BUTCHER:
if (query_yn("%s Stop butchering?", s.c_str()))
doit = true;
break;
case ACT_FORAGE:
if (query_yn("%s Stop foraging?", s.c_str()))
doit = true;
break;
case ACT_BUILD:
case ACT_VEHICLE:
if (query_yn("%s Stop construction?", s.c_str()))
doit = true;
break;
case ACT_REFILL_VEHICLE:
if (query_yn("%s Stop pumping gas?", s.c_str()))
doit = true;
break;
case ACT_TRAIN:
if (query_yn("%s Stop training?", s.c_str()))
doit = true;
break;
default:
doit = true;
}
if (doit)
u.cancel_activity();
}
void game::update_weather()
{
season_type season;
// Default to current weather, and update to the furthest future weather if any.
weather_segment prev_weather = {temperature, weather, nextweather};
if( !future_weather.empty() )
{
prev_weather = future_weather.back();
}
while( prev_weather.deadline < turn + HOURS(MAX_FUTURE_WEATHER) )
{
weather_segment new_weather;
// Pick a new weather type (most likely the same one)
int chances[NUM_WEATHER_TYPES];
int total = 0;
season = prev_weather.deadline.get_season();
for (int i = 0; i < NUM_WEATHER_TYPES; i++) {
// Reduce the chance for freezing-temp-only weather to 0 if it's above freezing
// and vice versa.
if ((weather_data[i].avg_temperature[season] < 32 && temperature > 32) ||
(weather_data[i].avg_temperature[season] > 32 && temperature < 32) )
{
chances[i] = 0;
} else {
chances[i] = weather_shift[season][prev_weather.weather][i];
if (weather_data[i].dangerous && u.has_artifact_with(AEP_BAD_WEATHER))
{
chances[i] = chances[i] * 4 + 10;
}
total += chances[i];
}
}
int choice = rng(0, total - 1);
new_weather.weather = WEATHER_CLEAR;
if (total > 0)
{
while (choice >= chances[new_weather.weather])
{
choice -= chances[new_weather.weather];
new_weather.weather = weather_type(int(new_weather.weather) + 1);
}
} else {
new_weather.weather = weather_type(int(new_weather.weather) + 1);
}
// Advance the weather timer
int minutes = rng(weather_data[new_weather.weather].mintime,
weather_data[new_weather.weather].maxtime);
new_weather.deadline = prev_weather.deadline + MINUTES(minutes);
if (new_weather.weather == WEATHER_SUNNY && new_weather.deadline.is_night())
{
new_weather.weather = WEATHER_CLEAR;
}
// Now update temperature
if (!one_in(4))
{ // 3 in 4 chance of respecting avg temp for the weather
int average = weather_data[weather].avg_temperature[season];
if (prev_weather.temperature < average)
{
new_weather.temperature = prev_weather.temperature + 1;
} else if (prev_weather.temperature > average) {
new_weather.temperature = prev_weather.temperature - 1;
} else {
new_weather.temperature = prev_weather.temperature;
}
} else {// 1 in 4 chance of random walk
new_weather.temperature = prev_weather.temperature + rng(-1, 1);
}
if (turn.is_night())
{
new_weather.temperature += rng(-2, 1);
} else {
new_weather.temperature += rng(-1, 2);
}
prev_weather = new_weather;
future_weather.push_back(new_weather);
}
if( turn >= nextweather )
{
weather_type old_weather = weather;
weather = future_weather.front().weather;
temperature = future_weather.front().temperature;
nextweather = future_weather.front().deadline;
future_weather.pop_front();
if (weather != old_weather && weather_data[weather].dangerous &&
levz >= 0 && m.is_outside(u.posx, u.posy))
{
std::stringstream weather_text;
weather_text << "The weather changed to " << weather_data[weather].name << "!";
cancel_activity_query(weather_text.str().c_str());
}
}
}
int game::assign_mission_id()
{
int ret = next_mission_id;
next_mission_id++;
return ret;
}
void game::give_mission(mission_id type)
{
mission tmp = mission_types[type].create(this);
active_missions.push_back(tmp);
u.active_missions.push_back(tmp.uid);
u.active_mission = u.active_missions.size() - 1;
mission_start m_s;
mission *miss = find_mission(tmp.uid);
(m_s.*miss->type->start)(this, miss);
}
void game::assign_mission(int id)
{
u.active_missions.push_back(id);
u.active_mission = u.active_missions.size() - 1;
mission_start m_s;
mission *miss = find_mission(id);