-
Notifications
You must be signed in to change notification settings - Fork 30
/
game.cpp
2115 lines (1921 loc) · 65.3 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 "window.h"
#include "stringfunc.h"
#include "map.h"
#include "rng.h"
#include "pathfind.h"
#include "files.h" // For CUSS_DIR
#include "help.h"
#include <stdarg.h>
#include <math.h>
#include <sstream>
std::vector<std::string> get_pickup_strings(std::vector<Item> *items,
std::vector<bool> *picking_up);
std::string pickup_string(Item *item, char letter, bool picking_up);
Game::Game()
{
map = NULL;
worldmap = NULL;
w_map = NULL;
w_hud = NULL;
player = NULL;
last_target = -1;
new_messages = 0;
next_item_uid = 0;
next_furniture_uid = 0;
temp_light_level = 0;
game_over = false;
}
Game::~Game()
{
if (map) {
delete map;
}
if (worldmap) {
delete worldmap;
}
if (w_map) {
delete w_map;
}
}
bool Game::setup_ui()
{
if (!i_hud.load_from_file(CUSS_DIR + "/i_hud.cuss")) {
return false;
}
int xdim, ydim;
get_screen_dims(xdim, ydim);
int win_size = ydim;
/* Commenting this out for now - the extra empty space caused issues
if (win_size % 2 == 0) {
win_size--; // Only odd numbers allowed!
}
*/
w_map = new Window(0, 0, win_size, win_size);
w_hud = new Window(win_size, 0, xdim - win_size, ydim);
// Attempt to resize the messages box to be as tall as the window allows
cuss::element* messages = i_hud.select("text_messages");
if (!messages) {
debugmsg("Couldn't find element text_messages in i_hud");
return false;
}
messages->sizey = ydim - messages->posy;
// Populate Worldmap_names with all the Worldmaps in SAVE_DIR/worlds
worldmap_names = files_in(SAVE_DIR + "/worlds", ".map");
// Strip the ".map" from each Worldmap name
for (int i = 0; i < worldmap_names.size(); i++) {
size_t suffix = worldmap_names[i].find(".map");
if (suffix != std::string::npos) {
worldmap_names[i] = worldmap_names[i].substr(0, suffix);
}
}
return true;
}
bool Game::setup_new_game(int world_index)
{
worldmap = new Worldmap;
if (world_index >= 0 && world_index < worldmap_names.size()) {
std::string world_file = SAVE_DIR + "/worlds/" +
worldmap_names[world_index] + ".map";
if (!worldmap->load_from_file(world_file)) {
debugmsg("Couldn't load worldmap from '%s'.", world_file.c_str());
return false;
}
} else {
worldmap->generate();
}
// Need to set time BEFORE creating a new character - because creating a new
// character uses time to set mission deadlines!
time = Time(0, 0, 8, 1, SEASON_SPRING, STARTING_YEAR);
map = new Map;
player = new Player;
player->prep_new_character();
// Player::create_new_character() returns false if the user cancels the process.
if (!player->create_new_character()) {
return false;
}
// entities[0] should always be the player!
entities.add_entity(player);
// The second argument of 0 means "on the main island"
Point start = worldmap->random_tile_with_terrain("beach", 0);
// Set the starting point to a shipwreck beach!
worldmap->set_terrain(start.x, start.y, "beach_shipwreck");
// Prep our Submap_pool.
/*
if (TESTING_MODE) {
debugmsg("Starting at %s.", start.str().c_str());
}
*/
SUBMAP_POOL.load_area_centered_on(start.x, start.y);
// And then generate our map.
map->generate(worldmap, start.x, start.y, 0);
/*
if (TESTING_MODE) {
debugmsg("Pool covers %s, map covers %s.",
SUBMAP_POOL.get_range_text().c_str(),
map->get_range_text().c_str());
}
*/
worldmap->set_terrain(start.x, start.y, "beach");
last_target = -1;
new_messages = 0;
game_over = false;
return true;
}
bool Game::starting_menu()
{
cuss::interface i_menu;
Window w_menu;
if (!i_menu.load_from_file(CUSS_DIR + "/i_starting_menu.cuss")) {
return false;
}
std::string motd = slurp_file(DATA_DIR + "/motd.txt");
i_menu.set_data("text_motd", motd);
int current_world = -1;
while (true) {
i_menu.draw(&w_menu);
w_menu.refresh();
long ch = input();
if (ch == 'n' || ch == 'N') {
if (setup_new_game(current_world)) {
return true;
}
} else if (ch == 'l' || ch == 'L') {
// TODO: Load character here
return true;
} else if (ch == 'w' || ch == 'W') {
current_world = world_screen();
} else if (ch == 'h' || ch == 'H') {
help_screen();
} else if (ch == 'q' || ch == 'Q') {
return false;
}
}
return false; // Should never be reached
}
int Game::world_screen()
{
cuss::interface i_worlds;
Window w_worlds(0, 0, 80, 24);
if (!i_worlds.load_from_file(CUSS_DIR + "/i_worlds.cuss")) {
return -1;
}
i_worlds.set_data("list_worlds", worldmap_names);
i_worlds.select("list_worlds");
while (true) { // We'll exit when the player hits enter
i_worlds.draw(&w_worlds);
w_worlds.refresh();
long ch = input();
if (ch == 'c' || ch == 'C') {
create_world();
// Repopulate list_worlds with (hopefully) a new world name.
i_worlds.set_data("list_worlds", worldmap_names);
i_worlds.select("list_worlds");
} else if ((ch == 'd' || ch == 'D') && !worldmap_names.empty()) {
int index = i_worlds.get_int("list_worlds");
std::string del_name = trim( worldmap_names[index] );
if (query_yn("Really delete %s and all saves?", del_name.c_str())) {
worldmap_names.erase( worldmap_names.begin() + index );
std::string dir_name = SAVE_DIR + "/" + del_name;
std::string file_name = SAVE_DIR + "/worlds/" + del_name + ".map";
if (directory_exists( dir_name ) && !remove_directory( dir_name )) {
debugmsg("Failed to remove directory '%s'.", dir_name.c_str());
}
if (!remove_file( file_name )) {
debugmsg("Failed to remove file '%s'.", file_name.c_str());
}
i_worlds.set_data("list_worlds", worldmap_names);
i_worlds.set_data("list_worlds", 0);
}
} else if (ch == '\n') {
return i_worlds.get_int("list_worlds");
} else {
i_worlds.handle_action(ch); // Handles any scrolling
}
}
return -1;
}
void Game::create_world()
{
cuss::interface i_editor;
Window w_editor(0, 0, 80, 24);
if (!i_editor.load_from_file(CUSS_DIR + "/i_world_editor.cuss")) {
return;
}
Worldmap tmp_world;
std::string world_name;
i_editor.ref_data("entry_name", &world_name);
i_editor.select("entry_name");
// TODO: Enable climate selection, and generate the world based on the climate.
while (true) { // We'll exit this loop via player input.
i_editor.draw(&w_editor);
w_editor.refresh();
long ch = input();
if (ch == '!') {
tmp_world.randomize_name();
world_name = tmp_world.get_name();
} else if (ch == '\n') {
if (tmp_world.get_name().empty()) {
popup("<c=ltred>Error:<c=/> Name is empty!");
} else {
popup_nowait("Generating world, please wait...");
tmp_world.generate();
if (tmp_world.save_to_name()) {
worldmap_names.push_back(tmp_world.get_name());
}
}
return;
} else if (ch == KEY_ESC) {
return;
} else if (ch == '@') {
// TODO: Unlock advanced options.
} else {
i_editor.handle_keypress(ch);
tmp_world.set_name(world_name);
}
}
}
bool Game::main_loop()
{
// Sanity check
if (!w_map || !w_hud || !player || !worldmap || !map) {
return false;
}
if (game_over) {
return false;
}
// Reset all temp values.
reset_temp_values();
// Process active items; these may set temp values!
process_active_items();
/* TODO: It's be nice to move all of this to Player::take_turn(). Then we
* won't have to special case it - it'd just be another entity taking
* their turn!
*/
// Give the player their action points
player->gain_action_points();
// Set all values, incur hunger/thirst, etc.
player->start_turn();
while (player->action_points > 0) {
// Handle the player's activity (e.g. reloading, crafting, etc)
handle_player_activity();
// Update the map in case we need to right now
shift_if_needed();
// Print everything (update_hud() and map->draw())
if (!player->has_activity()) {
draw_all();
}
// The player doesn't get to give input if they have an active activity.
if (!player->activity.is_active()) {
long ch = input();
// Fetch the action bound to whatever key we pressed...
Interface_action act = KEYBINDINGS.bound_to_key(ch);
// ... and do that action.
do_action(act);
}
}
// Map processes fields after the player
map->process_fields();
// Shift the map - it's likely that the player moved or something
shift_if_needed();
// Now all other entities get their turn
move_entities();
// Maybe a monster killed us
if (game_over) {
return false; // This terminates the game
}
// Advance the turn
time.increment();
return true; // This keeps the game going
}
void Game::reset_temp_values()
{
temp_light_level = 0;
}
void Game::do_action(Interface_action act)
{
switch (act) {
case IACTION_MOVE_N: player_move( 0, -1); break;
case IACTION_MOVE_S: player_move( 0, 1); break;
case IACTION_MOVE_W: player_move(-1, 0); break;
case IACTION_MOVE_E: player_move( 1, 0); break;
case IACTION_MOVE_NW: player_move(-1, -1); break;
case IACTION_MOVE_NE: player_move( 1, -1); break;
case IACTION_MOVE_SW: player_move(-1, 1); break;
case IACTION_MOVE_SE: player_move( 1, 1); break;
case IACTION_PAUSE: player->pause(); break;
case IACTION_MOVE_UP:
if (!map->has_flag(TF_STAIRS_UP, player->pos)) {
add_msg("You cannot go up here.");
player_move_vertical(1); // Snuck this in for debugging purposes
} else {
player_move_vertical(1);
}
break;
case IACTION_MOVE_DOWN:
if (!map->has_flag(TF_STAIRS_DOWN, player->pos)) {
add_msg("You cannot go down here.");
player_move_vertical(-1); // Snuck this in for debugging purposes
} else {
player_move_vertical(-1);
}
break;
case IACTION_PICK_UP:
// Make sure we're not sealed...
if (map->has_flag(TF_SEALED, player->pos)) {
add_msg("<c=dkgray>That %s is sealed; you cannot retrieve items \
there.<c=/>", map->get_name(player->pos).c_str());
} else if (map->item_count(player->pos) == 0) {
add_msg("No items here.");
} else if (map->item_count(player->pos) == 1) {
// Only one item - no need for the interface
std::vector<Item> *items = map->items_at(player->pos);
if (player->add_item( (*items)[0] )) {
items->clear();
}
} else {
pickup_items(player->pos);
}
break;
case IACTION_OPEN: {
add_msg("<c=ltgreen>Open where? (Press direction key)<c=/>");
draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // Error
add_msg("Invalid direction.");
} else {
Tripoint open = player->pos + dir;
if (map->apply_signal("open", open, player)) {
player->use_ap(100);
}
}
} break;
case IACTION_CLOSE: {
add_msg("<c=ltgreen>Close where? (Press direction key)<c=/>");
draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // Error
add_msg("Invalid direction.");
} else {
Tripoint close = player->pos + dir;
Entity* ent = entities.entity_at(close);
if (ent == player) {
add_msg("Maybe you should move out of the doorway first.");
} else if (ent) {
add_msg("There's a %s in the way.", ent->get_name().c_str());
} else if (map->furniture_at(close)) {
add_msg("There's some furniture in the way.");
} else if (map->apply_signal("close", close, player)) {
player->use_ap(100);
}
}
} break;
case IACTION_SMASH: {
add_msg("<c=ltgreen>Smash where? (Press direction key)<c=/>");
draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // Error
add_msg("Invalid direction.");
} else {
Tripoint sm = player->pos + dir;
if (!map->is_smashable(sm)) {
add_msg("Nothing to smash there.");
} else {
add_msg("You smash the %s.", map->get_name(sm).c_str());
map->smash(sm, player->std_attack().roll_damage());
player->use_ap(100);
}
}
} break;
case IACTION_EXAMINE: {
add_msg("<c=ltgreen>Examine where? (Press direction key)<c=/>");
draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // Error
add_msg("Invalid direction.");
} else {
Tripoint examine = player->pos + dir;
// Can't pick up items if we're sealed
if (map->has_flag(TF_SEALED, examine)) {
add_msg("<c=dkgray>That %s is sealed; you cannot retrieve items \
there.<c=/>", map->get_name(examine).c_str());
} else if (map->item_count(examine) > 0) {
pickup_items(examine);
}
std::stringstream description;
description << "That is " << map->get_name_indefinite(examine) << ".";
if (map->furniture_at(examine)) {
description << " You can drag it using the <c=yellow>grab<c=/> " <<
"command (<c=yellow>" <<
KEYBINDINGS.describe_bindings_for(IACTION_GRAB) <<
"<c=/>).";
if (TESTING_MODE) {
add_msg("<c=pink>Furniture uid %d.<c=/>",
map->furniture_at(examine)->get_uid());
}
}
add_msg(description.str());
}
} break;
case IACTION_GRAB: {
if (player->dragged.empty()) {
add_msg("<c=ltgreen>Grab where? (Press direction key)<c=/>");
draw_all();
Point dir = input_direction(input());
if (dir.x == -2) { // Error
add_msg("Invalid direction.");
} else {
Tripoint target = player->pos + dir;
player->dragged = map->grab_furniture(player->pos, target);
if (player->dragged.empty()) {
add_msg("Nothing to grab there.");
} else {
add_msg("You grab the %s.", player->get_dragged_name().c_str());
if (TESTING_MODE) {
for (int i = 0; i < player->dragged.size(); i++) {
add_msg("%s", player->dragged[i].pos.str().c_str());
}
}
}
}
} else { // We're already dragging something; so let go!
add_msg("You let go of the %s.", player->get_dragged_name().c_str());
player->dragged.clear();
}
} break;
case IACTION_INVENTORY: {
Item it = player->inventory_single();
Item_action act = it.show_info();
switch (act) {
case IACT_WIELD:
add_msg( player->wield_item_message(it) );
player->wield_item_uid(it.get_uid());
break;
case IACT_WEAR:
add_msg( player->wear_item_message(it) );
player->wear_item_uid(it.get_uid());
break;
case IACT_DROP:
add_msg( player->drop_item_message(it) );
player->remove_item_uid(it.get_uid());
// TODO: Dropping may fail sometimes(?), so don't automatically add the item
map->add_item(it, player->pos);
break;
case IACT_EAT:
add_msg( player->eat_item_message(it) );
player->eat_item_uid(it.get_uid());
break;
case IACT_APPLY:
add_msg( player->apply_item_message(it) );
player->apply_item_uid(it.get_uid());
break;
case IACT_READ:
add_msg( player->read_item_message(it) );
player->read_item_uid(it.get_uid());
break;
case IACT_UNLOAD:
// TODO: Put unload code here
break;
case IACT_RELOAD:
player->reload_prep(it.get_uid());
break;
case IACT_BUTCHER:
// TODO: Put butcher code here
break;
} // switch (act)
} break;
case IACTION_DROP: {
std::vector<Item> dropped = player->drop_items();
std::stringstream message;
message << "You drop " << list_items(&dropped);
for (int i = 0; i < dropped.size(); i++) {
map->add_item(dropped[i], player->pos);
}
add_msg( message.str() );
} break;
case IACTION_WIELD: {
Item it = player->inventory_single();
add_msg( player->sheath_weapon_message() );
player->sheath_weapon();
add_msg( player->wield_item_message(it) );
player->wield_item_uid(it.get_uid());
} break;
case IACTION_WEAR: {
Item it = player->inventory_single();
add_msg( player->wear_item_message(it) );
player->wear_item_uid(it.get_uid());
} break;
case IACTION_TAKE_OFF: {
Item it = player->inventory_single();
add_msg( player->take_off_item_message(it) );
player->take_off_item_uid(it.get_uid());
} break;
case IACTION_APPLY: {
Item it = player->inventory_single();
Item_action act = it.default_action();
switch (act) {
case IACT_NULL:
add_msg("Can't do anything with that item.");
break;
case IACT_WIELD:
add_msg( player->wield_item_message(it) );
player->wield_item_uid(it.get_uid());
break;
case IACT_WEAR:
add_msg( player->wear_item_message(it) );
player->wear_item_uid(it.get_uid());
break;
case IACT_EAT:
add_msg( player->eat_item_message(it) );
player->eat_item_uid(it.get_uid());
break;
case IACT_APPLY:
// Need to redraw the map
add_msg( player->apply_item_message(it) );
player->apply_item_uid(it.get_uid());
break;
case IACT_READ:
add_msg( player->read_item_message(it) );
player->read_item_uid(it.get_uid());
break;
}
} break;
case IACTION_READ: {
Item it = player->inventory_single();
add_msg( player->read_item_message(it) );
player->read_item_uid(it.get_uid());
} break;
case IACTION_RELOAD: {
Item it = player->inventory_single();
player->reload_prep(it.get_uid());
} break;
case IACTION_RELOAD_EQUIPPED:
player->reload_prep(player->weapon.get_uid());
break;
case IACTION_THROW: {
Item it = player->inventory_single();
if (!it.is_real()) {
add_msg("Never mind.");
} else {
int x = player->pos.x, y = player->pos.y,
range = player->weapon.get_fired_attack().range;
Tripoint target = target_selector(x, y, range, true, true);
if (target.x == -1) { // We canceled
add_msg("Never mind.");
} else {
// If we actually targeted an entity, set that to our last target.
Entity* targeted_entity = entities.entity_at(target);
if (targeted_entity) {
last_target = targeted_entity->uid;
}
player->remove_item_uid(it.get_uid(), 1);
Ranged_attack att = player->throw_item(it);
launch_projectile(player, it, att, player->pos, target);
}
}
} break;
case IACTION_FIRE:
if (player->can_fire_weapon()) {
int x = player->pos.x, y = player->pos.y,
range = player->weapon.get_fired_attack().range;
Tripoint target = target_selector(x, y, range, true, true);
if (target.x == -1) { // We canceled
add_msg("Never mind.");
// If we target ourself, confirm that we want to shoot ourselves...
} else if (target != player->pos ||
msg_query_yn("Really target yourself?")) {
// If we actually targeted an entity, set that to our last target.
Entity* targeted_entity = entities.entity_at(target);
if (targeted_entity) {
last_target = targeted_entity->uid;
}
// And do the actual attack!
Ranged_attack att = player->fire_weapon();
launch_projectile(player, player->weapon, att, player->pos, target);
}
}
break;
case IACTION_ADVANCE_FIRE_MODE:
player->weapon.advance_fire_mode();
add_msg( player->advance_fire_mode_message() );
break;
case IACTION_EAT: {
Item it = player->inventory_single();
add_msg( player->eat_item_message(it) );
player->eat_item_uid(it.get_uid());
} break;
case IACTION_MESSAGES_SCROLL_BACK:
i_hud.add_data("text_messages", -1);
break;
case IACTION_MESSAGES_SCROLL_FORWARD:
i_hud.add_data("text_messages", 1);
break;
case IACTION_VIEW_WORLDMAP: {
Point p = map->get_center_point();
Point got = worldmap->get_point(p.x, p.y);
// Adjust to match the upper-left corner
got.x -= MAP_SIZE / 2;
got.y -= MAP_SIZE / 2;
if (TESTING_MODE) {
int posx = got.x, posy = got.y;
posx += MAP_SIZE / 2;
posy += MAP_SIZE / 2;
SUBMAP_POOL.load_area_centered_on( posx, posy );
map->generate(worldmap, got.x, got.y);
Point mp = map->get_center_point();
//debugmsg("Worldmap %s, map %s", got.str().c_str(), mp.str().c_str());
}
} break;
case IACTION_CHAR_STATUS:
player->status_interface();
break;
case IACTION_CHAR_SKILLS:
player->skills_interface();
break;
case IACTION_DEBUG:
if (!TESTING_MODE) {
add_msg("<c=red>To access debug commands, run the program with the \
--test flag.");
} else {
debug_command();
}
break;
case IACTION_QUIT:
if (query_yn("Commit suicide?")) {
game_over = true;
player->action_points = 0;
}
break;
}
}
void Game::move_entities()
{
clean_up_dead_entities();
//scent_map = map->get_dijkstra_map(player->pos, 15);
// First, give all entities action points
for (std::list<Entity*>::iterator it = entities.instances.begin();
it != entities.instances.end();
it++) {
if (!(*it)->is_player()) {
(*it)->gain_action_points();
}
}
/* Loop through the entities, giving each one a single turn at a time.
* Stop when we go through a loop without finding any entities that can
* take a turn.
*/
bool all_done = true;
do {
all_done = true;
for (std::list<Entity*>::iterator it = entities.instances.begin();
it != entities.instances.end();
it++) {
Entity* ent = *it;
if (!ent->is_player() && ent->action_points > 0 && !ent->dead) {
ent->take_turn();
all_done = false;
}
}
} while (!all_done);
clean_up_dead_entities(); // Just in case an entity killed itself
}
void Game::clean_up_dead_entities()
{
std::list<Entity*>::iterator it = entities.instances.begin();
while (it != entities.instances.end()) {
Entity *ent = (*it);
if ( ent->dead ) {
if (player->can_see(map, ent->pos)) {
if (ent->killed_by_player) {
add_msg("You kill %s!", ent->get_name_to_player().c_str());
} else {
add_msg("%s dies!", ent->get_name_to_player().c_str());
}
}
ent->die();
delete ent;
it = entities.erase(it);
} else {
it++;
}
}
}
void Game::handle_player_activity()
{
if (!player->has_activity()) {
return;
}
Player_activity *act = &(player->activity);
if (act->duration <= player->action_points) {
player->action_points -= act->duration;
complete_player_activity();
} else {
act->duration -= player->action_points;
player->action_points = 0;
}
}
void Game::complete_player_activity()
{
Player_activity *act = &(player->activity);
switch (act->type) {
case PLAYER_ACTIVITY_NULL:
case PLAYER_ACTIVITY_WAIT:
break; // Nothing happens
case PLAYER_ACTIVITY_RELOAD: {
Item *reloaded = player->ref_item_uid(act->primary_item_uid);
if (!reloaded) {
debugmsg("Completed reload, but the item wasn't there!");
return;
}
add_msg("You reload your %s.", reloaded->get_name().c_str());
reloaded->reload(player, act->secondary_item_uid);
} break;
case PLAYER_ACTIVITY_READ: {
Item* read = player->ref_item_uid(act->primary_item_uid);
if (!read) {
debugmsg("Completed reading, but the item wasn't there!");
return;
}
add_msg("You finish reading.");
player->finish_reading(read);
} break;
default:
debugmsg("Our switch doesn't know how to handle action %d, '%s'",
act->type, act->get_name().c_str());
}
player->activity = Player_activity();
}
void Game::process_active_items()
{
for (int i = 0; i < active_items.size(); i++) {
active_items[i]->process_active();
}
}
void Game::shift_if_needed()
{
//return;
int min = SUBMAP_SIZE * (MAP_SIZE / 2), max = min + SUBMAP_SIZE - 1;
int shiftx = 0, shifty = 0;
if (player->pos.x < min) {
shiftx = -1 + (player->pos.x - min) / SUBMAP_SIZE;
} else if (player->pos.x > max) {
shiftx = 1 + (player->pos.x - max) / SUBMAP_SIZE;
}
if (player->pos.y < min) {
shifty = -1 + (player->pos.y - min) / SUBMAP_SIZE;
} else if (player->pos.y > max) {
shifty = 1 + (player->pos.y - max) / SUBMAP_SIZE;
}
int posx = map->posx + shiftx, posy = map->posy + shifty;
posx += MAP_SIZE / 2;
posy += MAP_SIZE / 2;
SUBMAP_POOL.load_area_centered_on(posx, posy);
map->shift(worldmap, shiftx, shifty);
//Point p = map->get_center_point();
//SUBMAP_POOL.load_area_centered_on( p.x, p.y );
for (std::list<Entity*>::iterator it = entities.instances.begin();
it != entities.instances.end();
it++) {
(*it)->shift(shiftx, shifty);
}
}
void Game::make_sound(std::string desc, int volume, Tripoint pos)
{
make_sound(Sound(desc, volume), pos);
}
void Game::make_sound(std::string desc, int volume, Point pos)
{
make_sound(desc, volume, Tripoint(pos.x, pos.y, 0));
}
void Game::make_sound(std::string desc, int volume, int x, int y)
{
make_sound(desc, volume, Point(x, y));
}
void Game::make_sound(Sound snd, Tripoint pos)
{
if (snd.description.empty()) {
return;
}
// TODO: Alert monsters
Direction_full dir = get_general_direction(player->pos, pos);
snd.volume -= rl_dist(pos, player->pos);
if (snd.volume <= 0) {
return; // To quiet to hear!
}
// TODO: Don't hardcode color
// We don't punctuate the messages below - that's for the sound to do!
if (dir == DIRFULL_NULL) { // On top of the player!
add_msg("<c=ltblue>You hear %s<c=/>", snd.description.c_str());
} else {
add_msg("<c=ltblue>To the <c=ltred>%s<c=ltblue>, you hear %s<c=/>",
Direction_name(dir).c_str(), snd.description.c_str());
}
}
void Game::launch_projectile(Ranged_attack attack,
Tripoint origin, Tripoint target)
{
launch_projectile(NULL, attack, origin, target);
}
void Game::launch_projectile(Item it, Ranged_attack attack, Tripoint origin,
Tripoint target)
{
launch_projectile(NULL, it, attack, origin, target);
}
void Game::launch_projectile(Entity* shooter, Ranged_attack attack,
Tripoint origin, Tripoint target)
{
launch_projectile(shooter, Item(), attack, origin, target);
}
void Game::launch_projectile(Entity* shooter, Item it, Ranged_attack attack,
Tripoint origin, Tripoint target)
{
// Set up some nouns and verbs for messages (far below)
std::string shooter_name, verb = "shoot", miss_verb = "miss",
graze_verb = "graze";
if (shooter) {
shooter_name = shooter->get_name_to_player();
verb = shooter->conjugate(verb);
miss_verb = shooter->conjugate(miss_verb);
graze_verb = shooter->conjugate(graze_verb);
} else {
/* If there's no shooter, that implies that natural forces launched the
* projectile, e.g. rubble from an explosion. In that case, we want our hit
* message to be "A piece of rubble hits you!"
*/
if (it.is_real()) {
shooter_name = it.get_name_indefinite();
}
verb = "hits";
miss_verb = "misses";
graze_verb = "grazes";
}
// We want to loop through once for each round. Also, (target) will probably
// change between rounds, so we recalculate range etc.
int retarget_range = 0;
if (attack.rounds < 1) {
attack.rounds = 1;
} else if (it.is_real()) {
/* If we're firing a multiple-round weapon, and we kill our original target or
* wind up targeting a space without a monster in it, we can re-target to a
* nearby monster between rounds. The range of this retargeting is dependent
* upon our skills.
*/
Item_type_launcher* launcher_type =
static_cast<Item_type_launcher*>(it.get_type());
Skill_type launcher_skill = launcher_type->skill_used;
if (shooter) {
retarget_range = shooter->skills.get_level(launcher_skill) +
shooter->skills.get_level(SKILL_LAUNCHERS) / 2;
retarget_range = sqrt(retarget_range);
}
}
/* If we're NOT targeting an entity, then we are probably shooting at scenery
* and we should not use our retargeting ability!
*/
bool targeting_entity = entities.entity_at(target);
for (int round = 0; round < attack.rounds; round++) {
// Figure out the range to the target
int range = rl_dist(origin, target);
// We calculate angle individually for every pellet...
int num_pellets = attack.pellets;
if (num_pellets < 1) {
num_pellets = 1;
}
/* We summarize all pellets in a single message to avoid spamming the player
* with 20 messages for a single round of 00 shot. We do this with a few