forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cpp
8607 lines (7986 loc) · 252 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 "weather_data.h"
#include "veh_interact.h"
#include "options.h"
#include "mapbuffer.h"
#include "debug.h"
#include "bodypart.h"
#include "map.h"
#include "weather.h"
#include <map>
#include <algorithm>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "debug.h"
#if (defined _WIN32 || defined __WIN32__)
#include <windows.h>
#include <tchar.h>
#endif
#define MAX_MONSTERS_MOVING 40 // Efficiency!
#define dbg(x) dout((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
void intro();
nc_color sev(int a); // Right now, ONLY used for scent debugging....
moncat_id mt_to_mc(mon_id type); // Pick the moncat that contains type
// 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.";
clear(); // Clear the screen
intro(); // Print an intro screen, make sure we're at least 80x25
// Gee, it sure is init-y around here!
init_itypes(); // Set up item types (SEE itypedef.cpp)
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_moncats(); // Set up monster categories (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();
VIEWX = OPTIONS[OPT_VIEWPORT_X];
VIEWY = OPTIONS[OPT_VIEWPORT_Y];
if (VIEWX <= 0) {
VIEWX = 1;
}
if (VIEWY <= 0) {
VIEWY = 1;
}
TERRAIN_WINDOW_WIDTH = (VIEWX * 2) + 1;
TERRAIN_WINDOW_HEIGHT = (VIEWY * 2) + 1;
// Set up the main UI windows.
w_terrain = newwin(TERRAIN_WINDOW_HEIGHT, TERRAIN_WINDOW_WIDTH, 0, 0);
werase(w_terrain);
w_minimap = newwin(7, 7, 0, TERRAIN_WINDOW_WIDTH);
werase(w_minimap);
w_HP = newwin(14, 7, 7, TERRAIN_WINDOW_WIDTH);
werase(w_HP);
w_moninfo = newwin(12, 48, 0, VIEWX * 2 + 8);
werase(w_moninfo);
w_messages = newwin(8, 48, 12, VIEWX * 2 + 8);
werase(w_messages);
w_location = newwin(1, 48, 20, VIEWX * 2 + 8);
werase(w_location);
w_status = newwin(4, 55, 21, TERRAIN_WINDOW_WIDTH);
werase(w_status);
gamemode = new special_game; // Nothing, basically.
}
game::~game()
{
delete gamemode;
for (int i = 0; i < itypes.size(); i++)
delete itypes[i];
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::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
no_npc = false; // We're not suppressing NPC spawns
// ... Unless data/no_npc.txt exists.
std::ifstream ifile("data/no_npc.txt");
if (ifile)
no_npc = true;
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();
z.clear();
coming_to_stairs.clear();
active_npc.clear();
factions.clear();
active_missions.clear();
items_dragged.clear();
messages.clear();
events.clear();
turn.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();
}
}
bool game::opening_screen()
{
WINDOW* w_open = newwin(25, 80, 0, 0);
erase();
for (int i = 0; i < 80; i++)
mvwputch(w_open, 21, i, c_white, LINE_OXOX);
mvwprintz(w_open, 0, 0, c_blue, "Welcome to Cataclysm: Dark Days Ahead!");
mvwprintz(w_open, 1, 0, c_red, "\
Please report bugs to TheDarklingWolf@gmail.com or post on the forums.");
refresh();
wrefresh(w_open);
refresh();
std::vector<std::string> savegames, templates;
std::string tmp;
dirent *dp;
DIR *dir = opendir("save");
if (!dir) {
#if (defined _WIN32 || defined __WIN32__)
mkdir("save");
#else
mkdir("save", 0777);
#endif
dir = opendir("save");
}
if (!dir) {
dbg(D_ERROR) << "game:opening_screen: Unable to make save directory.";
debugmsg("Could not make './save' directory");
endwin();
exit(1);
}
while ((dp = readdir(dir))) {
tmp = dp->d_name;
if (tmp.find(".sav") != std::string::npos)
savegames.push_back(tmp.substr(0, tmp.find(".sav")));
}
closedir(dir);
dir = opendir("data");
while ((dp = readdir(dir))) {
tmp = dp->d_name;
if (tmp.find(".template") != std::string::npos)
templates.push_back(tmp.substr(0, tmp.find(".template")));
}
int sel1 = 0, sel2 = 1, layer = 1;
InputEvent input;
bool start = false;
// Load MOTD and store it in a string
std::vector<std::string> motd;
std::ifstream motd_file;
motd_file.open("data/motd");
if (!motd_file.is_open())
motd.push_back("No message today.");
else {
while (!motd_file.eof()) {
std::string tmp;
getline(motd_file, tmp);
if (tmp[0] != '#')
motd.push_back(tmp);
}
}
while(!start) {
if (layer == 1) {
mvwprintz(w_open, 4, 1, (sel1 == 0 ? h_white : c_white), "MOTD");
mvwprintz(w_open, 5, 1, (sel1 == 1 ? h_white : c_white), "New Game");
mvwprintz(w_open, 6, 1, (sel1 == 2 ? h_white : c_white), "Load Game");
mvwprintz(w_open, 7, 1, (sel1 == 3 ? h_white : c_white), "New World");
mvwprintz(w_open, 8, 1, (sel1 == 4 ? h_white : c_white), "Special...");
mvwprintz(w_open, 9, 1, (sel1 == 5 ? h_white : c_white), "Help");
mvwprintz(w_open, 10, 1, (sel1 == 6 ? h_white : c_white), "Quit");
if (sel1 == 0) { // Print the MOTD.
for (int i = 0; i < motd.size() && i < 16; i++)
mvwprintz(w_open, i + 4, 12, c_ltred, motd[i].c_str());
} else { // Clear the lines if not viewing MOTD.
for (int i = 4; i < 20; i++) {
for (int j = 12; j < 79; j++)
mvwputch(w_open, i, j, c_black, 'x');
}
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionN) {
if (sel1 > 0)
sel1--;
else
sel1 = 6;
} else if (input == DirectionS) {
if (sel1 < 6)
sel1++;
else
sel1 = 0;
} else if ((input == DirectionE || input == Confirm) && sel1 > 0) {
if (sel1 == 6) {
uquit = QUIT_MENU;
return false;
} else if (sel1 == 5) {
help();
clear();
mvwprintz(w_open, 0, 1, c_blue, "Welcome to Cataclysm!");
mvwprintz(w_open, 1, 0, c_red, "\
This alpha release is highly unstable. Please report any crashes or bugs to\n\
fivedozenwhales@gmail.com.");
refresh();
wrefresh(w_open);
refresh();
} else {
sel2 = 1;
layer = 2;
}
mvwprintz(w_open, 4, 1, (sel1 == 0 ? c_white : c_dkgray), "MOTD");
mvwprintz(w_open, 5, 1, (sel1 == 1 ? c_white : c_dkgray), "New Game");
mvwprintz(w_open, 6, 1, (sel1 == 2 ? c_white : c_dkgray), "Load Game");
mvwprintz(w_open, 7, 1, (sel1 == 3 ? c_white : c_dkgray), "New World");
mvwprintz(w_open, 8, 1, (sel1 == 4 ? c_white : c_dkgray), "Special...");
mvwprintz(w_open, 9, 1, (sel1 == 5 ? c_white : c_dkgray), "Help");
mvwprintz(w_open, 10, 1, (sel1 == 6 ? c_white : c_dkgray), "Quit");
}
} else if (layer == 2) {
if (sel1 == 1) { // New Character
mvwprintz(w_open, 5, 12, (sel2 == 1 ? h_white : c_white),
"Custom Character");
mvwprintz(w_open, 6, 12, (sel2 == 2 ? h_white : c_white),
"Preset Character");
mvwprintz(w_open, 7, 12, (sel2 == 3 ? h_white : c_white),
"Random Character");
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionN) {
if (sel2 > 1)
sel2--;
else
sel2 = 3;
} if (input == DirectionS) {
if (sel2 < 3)
sel2++;
else
sel2 = 1;
} else if (input == DirectionW) {
mvwprintz(w_open, 5, 12, c_black, " ");
mvwprintz(w_open, 6, 12, c_black, " ");
mvwprintz(w_open, 7, 12, c_black, " ");
layer = 1;
sel1 = 1;
}
if (input == DirectionE || input == Confirm) {
if (sel2 == 1) {
if (!u.create(this, PLTYPE_CUSTOM)) {
u = player();
delwin(w_open);
return (opening_screen());
}
start_game();
start = true;
}
if (sel2 == 2) {
layer = 3;
sel1 = 0;
mvwprintz(w_open, 5, 12, c_dkgray, "Custom Character");
mvwprintz(w_open, 6, 12, c_white, "Preset Character");
mvwprintz(w_open, 7, 12, c_dkgray, "Random Character");
}
if (sel2 == 3) {
if (!u.create(this, PLTYPE_RANDOM)) {
u = player();
delwin(w_open);
return (opening_screen());
}
start_game();
start = true;
}
}
} else if (sel1 == 2) { // Load Character
if (savegames.size() == 0)
mvwprintz(w_open, 6, 12, c_red, "No save games found!");
else {
int savestart = (sel2 < 7 ? 0 : sel2 - 7),
saveend = (sel2 < 7 ? 14 : sel2 + 7);
for (int i = savestart; i < saveend; i++) {
int line = 6 + i - savestart;
mvwprintz(w_open, line, 12, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
if (unsigned(i) < savegames.size())
mvwprintz(w_open, line, 12, (sel2 - 1 == i ? h_white : c_white),
savegames[i].c_str());
}
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionN) {
if (sel2 > 1)
sel2--;
else
sel2 = savegames.size();
} else if (input == DirectionS) {
if (unsigned(sel2) < savegames.size())
sel2++;
else
sel2 = 1;
} else if (input == DirectionW) {
layer = 1;
for (int i = 0; i < 14; i++)
mvwprintz(w_open, 6 + i, 12, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
if (input == DirectionE || input == Confirm) {
if (sel2 > 0 && savegames.size() > 0) {
load(savegames[sel2 - 1]);
start = true;
}
}
} else if (sel1 == 3) { // Delete world
if (query_yn("Delete the world and all saves?")) {
delete_save();
savegames.clear();
MAPBUFFER.reset();
MAPBUFFER.make_volatile();
}
layer = 1;
} else if (sel1 == 4) { // Special game
for (int i = 1; i < NUM_SPECIAL_GAMES; i++) {
mvwprintz(w_open, 6 + i, 12, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
mvwprintz(w_open, 6 + i, 12, (sel2 == i ? h_white : c_white),
special_game_name( special_game_id(i) ).c_str());
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionN) {
if (sel2 > 1)
sel2--;
else
sel2 = NUM_SPECIAL_GAMES - 1;
} else if (input == DirectionS) {
if (sel2 < NUM_SPECIAL_GAMES - 1)
sel2++;
else
sel2 = 1;
} else if (input == DirectionW) {
layer = 1;
for (int i = 6; i < 15; i++)
mvwprintz(w_open, i, 12, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
if (input == DirectionE || input == Confirm) {
if (sel2 >= 1 && sel2 < NUM_SPECIAL_GAMES) {
delete gamemode;
gamemode = get_special_game( special_game_id(sel2) );
if (!gamemode->init(this)) {
delete gamemode;
gamemode = new special_game;
u = player();
delwin(w_open);
return (opening_screen());
}
start = true;
}
}
}
} else if (layer == 3) { // Character Templates
if (templates.size() == 0)
mvwprintz(w_open, 6, 12, c_red, "No templates found!");
else {
int tempstart = (sel1 < 6 ? 0 : sel1 - 6),
tempend = (sel1 < 6 ? 14 : sel1 + 8);
for (int i = tempstart; i < tempend; i++) {
int line = 6 + i - tempstart;
mvwprintz(w_open, line, 29, c_black, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
if (unsigned(i) < templates.size())
mvwprintz(w_open, line, 29, (sel1 == i ? h_white : c_white),
templates[i].c_str());
}
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionN) {
if (sel1 > 0)
sel1--;
else
sel1 = templates.size() - 1;
} else if (input == DirectionS) {
if (unsigned(sel1) < templates.size() - 1)
sel1++;
else
sel1 = 0;
} else if (input == DirectionW || templates.size() == 0) {
sel1 = 1;
layer = 2;
for (int i = 0; i+6 < 21; i++)
mvwprintz(w_open, 6 + i, 12, c_black, " ");
for (int i = 22; i < 25; i++)
mvwprintw(w_open, i, 0, " \
");
}
else if (input == DirectionE || input == Confirm) {
if (!u.create(this, PLTYPE_TEMPLATE, templates[sel1])) {
u = player();
delwin(w_open);
return (opening_screen());
}
start_game();
start = true;
}
}
}
delwin(w_open);
if (start == false)
uquit = QUIT_MENU;
return start;
}
// Set up all default values for a new game
void game::start_game()
{
turn = MINUTES(STARTING_MINUTES);// It's turn 0...
run_mode = (OPTIONS[OPT_SAFEMODE] ? 1 : 0);
mostseen = 0; // ...and mostseen is 0, we haven't seen any monsters yet.
// Init some factions.
if (!load_master()) // Master data record contains factions.
create_factions();
cur_om = overmap(this, 0, 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 out with none of it seen by the player...
for (int i = 0; i < OMAPX; i++) {
for (int j = 0; j < OMAPX; j++)
cur_om.seen(i, j) = false;
}
// ...except for our immediate neighborhood.
for (int i = -15; i <= 15; i++) {
for (int j = -15; j <= 15; j++)
cur_om.seen(levx + i, levy + j) = 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);
// 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?
// 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);
}
}
void game::create_starting_npcs()
{
npc tmp;
tmp.normalize(this);
tmp.randomize(this, (one_in(2) ? NC_DOCTOR : NC_NONE));
tmp.spawn_at(&cur_om, levx, levy);
tmp.posx = SEEX * int(MAPSIZE / 2) + SEEX;
tmp.posy = SEEY * int(MAPSIZE / 2) + 6;
tmp.form_opinion(&u);
tmp.attitude = NPCATT_NULL;
tmp.mission = NPC_MISSION_SHELTER;
tmp.chatbin.first_topic = TALK_SHELTER;
tmp.chatbin.missions.push_back(
reserve_random_mission(ORIGIN_OPENER_NPC, om_location(), tmp.id) );
active_npc.push_back(tmp);
}
void game::cleanup_at_end(){
write_msg();
// Save the monsters before we die!
for (int i = 0; i < z.size(); i++) {
if (z[i].spawnmapx != -1) { // Static spawn, move them back there
tinymap tmp(&itypes, &mapitems, &traps);
tmp.load(this, z[i].spawnmapx, z[i].spawnmapy, false);
tmp.add_spawn(&(z[i]));
tmp.save(&cur_om, turn, z[i].spawnmapx, z[i].spawnmapy);
} else { // Absorb them back into a group
int group = valid_group((mon_id)(z[i].type->id), levx, levy);
if (group != -1) {
cur_om.zg[group].population++;
if (cur_om.zg[group].population / pow(cur_om.zg[group].radius, 2.0) > 5 &&
!cur_om.zg[group].diffuse)
cur_om.zg[group].radius++;
}
}
}
if (uquit == QUIT_DIED)
popup_top("Game over! Press spacebar...");
if (uquit == QUIT_DIED || uquit == QUIT_SUICIDE)
death_screen();
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.hour == 0 && turn.minute == 0 && turn.second == 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;
}
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 the weather, if it's time.
if (turn >= nextweather)
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();
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;
}
}
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();
}
// update_bodytemp();
rustCheck();
if (turn % 10 == 0)
u.update_morale();
return false;
}
/* Here lies the intended effects of body temperature
Assumption 1 : a naked person is comfortable at 31C/87.8F.
Assumption 2 : a "lightly clothed" person is comfortable at 25C/77F.
Assumption 3 : frostbite cannot happen above 0C temperature.*
* In the current model, a naked person can get frostbite at 1C. This isn't true, but it's a compromise with using nice whole numbers.
Here is a list of warmth values and the corresponding temperatures in which the player is comfortable, and in which the player is very cold.
Warmth Temperature (Comfortable) Temperature (Very cold) Notes
0 31C / 87.8F 1C / 33.8F * Naked
10 25C / 77.0F -5C / 23.0F * Lightly clothed
20 19C / 66.2F -11C / 12.2F
30 13C / 55.4F -17C / 1.4F
40 7C / 44.6F -23C / -9.4F
50 1C / 33.8F -29C / -20.2F
60 -5C / 23.0F -35C / -31.0F
70 -11C / 12.2F -41C / -41.8F
80 -17C / 1.4F -47C / -52.6F
90 -23C / -9.4F -53C / -63.4F
100 -29C / -20.2F -59C / -74.2F
*/
void game::update_bodytemp() // TODO bionics, diseases and humidity (not in yet) can affect body temp.
{
// NOTE : visit weather.h for some details on the numbers used
// Converts temperature to Celsius/10(Wito plans on using degrees Kelvin later)
int Ctemperature = 100*(temperature - 32) * 5/9;
// Temperature norms
const int ambient_norm = 3100;
// This adjusts the temperature scale to match the bodytemp scale
int adjusted_temp = (Ctemperature - ambient_norm);
// Creative thinking for clean morale penalties: this gets incremented in the for loop and applied after the loop
int morale_pen = 0;
// Fetch the morale value of wetness for bodywetness
int bodywetness = 0;
for (int i = 0; bodywetness == 0 && i < u.morale.size(); i++)
if( u.morale[i].type == MORALE_WET ) {
bodywetness = abs(u.morale[i].bonus); // Make it positive, less confusing
break;
}
// Current temperature and converging temperature calculations
for (int i = 0 ; i < num_bp ; i++){
if (i == bp_eyes) continue; // Skip eyes
// Represents the fact that the body generates heat when it is cold. TODO : should this increase hunger?
float homeostasis_adjustement = (u.temp_cur[i] > BODYTEMP_NORM ? 40.0 : 60.0);
int clothing_warmth_adjustement = homeostasis_adjustement * (float)u.warmth(body_part(i)) * (1.0 - (float)bodywetness / 100.0);
// Disease name shorthand
int blister_pen = dis_type(DI_BLISTERS) + 1 + i, hot_pen = dis_type(DI_HOT) + 1 + i;
int cold_pen = dis_type(DI_COLD)+ 1 + i, frost_pen = dis_type(DI_FROSTBITE) + 1 + i;
// Convergeant temperature is affected by ambient temperature, clothing warmth, and body wetness.
signed int temp_conv = BODYTEMP_NORM + adjusted_temp + clothing_warmth_adjustement;
// Fatigue also affects convergeant temperature
if (!u.has_disease(DI_SLEEP)) temp_conv -= 10*u.fatigue/6;
else {
int vpart = -1;
vehicle *veh = m.veh_at (u.posx, u.posy, vpart);
if (m.ter(u.posx, u.posy) == t_bed) temp_conv += 1000;
else if (m.ter(u.posx, u.posy) == t_makeshift_bed) temp_conv += 500;
else if (m.tr_at(u.posx, u.posy) == tr_cot) temp_conv -= 500;
else if (m.tr_at(u.posx, u.posy) == tr_rollmat) temp_conv -= 1000;
else if (veh && veh->part_with_feature (vpart, vpf_seat) >= 0) temp_conv += 200;
else if (veh && veh->part_with_feature (vpart, vpf_bed) >= 0) temp_conv += 300;
else temp_conv -= 2000;
}
// Convection heat sources : generates body heat, helps fight frostbite
int blister_count = 0; // If the counter is high, your skin starts to burn
for (int j = -6 ; j <= 6 ; j++){
for (int k = -6 ; k <= 6 ; k++){
// Bizarre workaround for u_see() and friends not taking const arguments.
int l = std::max(j, k);
int heat_intensity = 0;
if(m.field_at(u.posx + j, u.posy + k).type == fd_fire)
heat_intensity = m.field_at(u.posx + j, u.posy + k).density;
else if (m.tr_at(u.posx + j, u.posy + k) == tr_lava )
heat_intensity = 3;
if (heat_intensity > 0 && u_see(u.posx + j, u.posy + k, l)) {
// Ensure fire_dist >=1 to avoid divide-by-zero errors.
int fire_dist = std::max(1, std::max(j, k));
if (u.frostbite_timer[i] > 0) u.frostbite_timer[i] -= heat_intensity - fire_dist / 2;
temp_conv += 50 * heat_intensity / (fire_dist * fire_dist);
blister_count += heat_intensity / (fire_dist * fire_dist);
}
}
}
// TODO Balance bionics
// Bionic "Internal Climate Control" says it is effective from 0F to 140F, these are the corresponding bodytemp values
// Bug: temperature < 140 will always result true because temperature is a signed char. - AkrionXxarr
if (u.has_bionic(bio_climate) && temperature > 0 && temperature < 140)
temp_conv = (9*BODYTEMP_NORM + temp_conv)/10; // Bionic "eases" the effects
// Bionic "Thermal Dissapation" says it prevents fire damage up to 2000F. 500 is picked at random...
if (u.has_bionic(bio_heatsink) && blister_count < 500)
blister_count = 0;
// Skin gets blisters from intense heat exposure.
if (blister_count - 10*u.resist(body_part(i)) > 20) u.add_disease(dis_type(blister_pen), 1, this);
// Increments current body temperature towards convergant.
int temp_difference = u.temp_cur[i] - temp_conv;
int temp_before = u.temp_cur[i];
// Bodytemp equalization code
if (i == bp_torso){u.temp_equalizer(bp_torso, bp_arms); u.temp_equalizer(bp_torso, bp_legs); u.temp_equalizer(bp_torso, bp_head);}
else if (i == bp_head) {u.temp_equalizer(bp_head, bp_eyes); u.temp_equalizer(bp_head, bp_mouth);}
else if (i == bp_arms) u.temp_equalizer(bp_arms, bp_hands);
else if (i == bp_legs) u.temp_equalizer(bp_legs, bp_feet);
if (u.temp_cur[i] != temp_conv) u.temp_cur[i] = temp_difference*exp(-0.002) + temp_conv; // It takes half an hour for bodytemp to converge half way to its convergeance point (think half-life)
int temp_after = u.temp_cur[i];
// Penalties
if (u.temp_cur[i] < BODYTEMP_FREEZING) {u.add_disease(dis_type(cold_pen), 1, this, 3, 3); u.frostbite_timer[i] += 3;}
else if (u.temp_cur[i] < BODYTEMP_VERY_COLD) {u.add_disease(dis_type(cold_pen), 1, this, 2, 3); u.frostbite_timer[i] += 2;}
else if (u.temp_cur[i] < BODYTEMP_COLD) {u.add_disease(dis_type(cold_pen), 1, this, 1, 3); u.frostbite_timer[i] += 1;} // Frostbite timer does not go down if you are still cold.
else if (u.temp_cur[i] > BODYTEMP_SCORCHING) {u.add_disease(dis_type(hot_pen), 1, this, 3, 3); } // If body temp rises over 15000, disease.cpp (DI_HOT_HEAD) acts weird and the player will die
else if (u.temp_cur[i] > BODYTEMP_VERY_HOT) {u.add_disease(dis_type(hot_pen), 1, this, 2, 3); }
else if (u.temp_cur[i] > BODYTEMP_HOT) {u.add_disease(dis_type(hot_pen), 1, this, 1, 3); }
// Morale penalties : a negative morale_pen means the player is cold
// Intensity multiplier is negative for cold, positive for hot
int intensity_mult = -u.disease_intensity(dis_type(cold_pen)) + u.disease_intensity(dis_type(hot_pen));
if (u.has_disease(dis_type(cold_pen)) > 0 || u.has_disease(dis_type(hot_pen)) > 0) {
switch (i) {
case bp_head :
case bp_torso :
case bp_mouth : morale_pen += 2*intensity_mult;
case bp_arms :
case bp_legs : morale_pen += 1*intensity_mult;
case bp_hands:
case bp_feet : morale_pen += 1*intensity_mult;
}
}
// Frostbite (level 1 after 2 hours, level 2 after 4 hours)
if (u.frostbite_timer[i] > 0) u.frostbite_timer[i]--;
if (u.frostbite_timer[i] >= 240) {
if (u.disease_intensity(dis_type(frost_pen)) < 2 && i == bp_mouth) add_msg("Your %s hardens from the frostbite!", body_part_name(body_part(i), -1).c_str());
else if (u.disease_intensity(dis_type(frost_pen)) < 2 && (i == bp_hands || i == bp_feet)) add_msg("Your %s harden from the frostbite!", body_part_name(body_part(i), -1).c_str());
u.add_disease(dis_type(frost_pen), 1, this, 2, 2);}
else if (u.frostbite_timer[i] >= 120) {
if (!u.has_disease(dis_type(frost_pen))) add_msg("You lose sensation in your %s.", body_part_name(body_part(i), -1).c_str());
u.add_disease(dis_type(frost_pen), 1, this, 1, 2);}
// Warn the player if condition worsens
if (temp_before > BODYTEMP_FREEZING && temp_after < BODYTEMP_FREEZING) add_msg("You feel your %s beginning to go numb from the cold!", body_part_name(body_part(i), -1).c_str());
else if (temp_before > BODYTEMP_VERY_COLD && temp_after < BODYTEMP_VERY_COLD) add_msg("You feel your %s getting very cold.", body_part_name(body_part(i), -1).c_str());
else if (temp_before > BODYTEMP_COLD && temp_after < BODYTEMP_COLD) add_msg("You feel your %s getting cold.", body_part_name(body_part(i), -1).c_str());
else if (temp_before < BODYTEMP_SCORCHING && temp_after > BODYTEMP_SCORCHING) add_msg("You feel your %s getting red hot from the heat!", body_part_name(body_part(i), -1).c_str());
else if (temp_before < BODYTEMP_VERY_HOT && temp_after > BODYTEMP_VERY_HOT) add_msg("You feel your %s getting very hot.", body_part_name(body_part(i), -1).c_str());
else if (temp_before < BODYTEMP_HOT && temp_after > BODYTEMP_HOT) add_msg("You feel your %s getting hot.", body_part_name(body_part(i), -1).c_str());
// Debug
//add_msg("%s temperature : %d", body_part_name(body_part(i), -1).c_str(), u.temp_cur[i]);
}
// Morale penalties
if (morale_pen < 0) u.add_morale(MORALE_COLD, -1, -abs(morale_pen));
if (morale_pen > 0) u.add_morale(MORALE_HOT, -1, -abs(morale_pen));
}
void game::rustCheck() {
if (OPTIONS[OPT_SKILL_RUST] == 2)
return;
for (std::vector<Skill*>::iterator aSkill = Skill::skills.begin()++; aSkill != Skill::skills.end(); ++aSkill) {
int skillLevel = u.skillLevel(*aSkill).level();
int forgetCap = skillLevel > 7 ? 7 : skillLevel;
if (skillLevel > 0 && turn % (8192 / int(pow(2, double(forgetCap - 1)))) == 0) {
if (rng(1,12) % (u.has_trait(PF_FORGETFUL) ? 3 : 4)) {
if (u.has_bionic(bio_memory) && u.power_level > 0) {
if (one_in(5))
u.power_level--;
} else {
if (OPTIONS[OPT_SKILL_RUST] == 0 || u.skillLevel(*aSkill).exercise() > 0) {
int newLevel;
u.skillLevel(*aSkill).rust(newLevel);
if (newLevel < skillLevel) {
add_msg("Your skill in %s has reduced to %d!", (*aSkill)->name().c_str(), newLevel);
}
}
}
}
}
}
}
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;
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(10)) {
// 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(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.index))
if (u.weapon.is_gun() && u.weapon.has_flag(IF_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[u.activity.index].type);
if (reading->fun != 0) {
std::stringstream morale_text;
u.add_morale(MORALE_BOOK, reading->fun * 5, reading->fun * 15, reading);
}
if (u.skillLevel(reading->type) < reading->level) {
add_msg("You learn a little about %s! (%d%%%%)", reading->type->name().c_str(), u.skillLevel(reading->type).exercise());
int min_ex = reading->time / 10 + u.int_cur / 4,
max_ex = reading->time / 5 + u.int_cur / 2 - u.skillLevel(reading->type).level();
if (min_ex < 1)
min_ex = 1;
if (max_ex < 2)
max_ex = 2;
if (max_ex > 10)
max_ex = 10;