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
|
/**
* @file
* @brief Game state functions.
**/
#include "AppHdr.h"
#include "state.h"
#if defined(UNIX) || defined(TARGET_COMPILER_MINGW)
#include <unistd.h>
#endif
#include "dbg-util.h"
#include "delay.h"
#include "directn.h"
#include "hints.h"
#include "initfile.h"
#include "macro.h"
#include "menu.h"
#include "message.h"
#include "monster.h"
#include "player.h"
#include "religion.h"
#include "showsymb.h"
#include "unwind.h"
game_state::game_state()
: game_crashed(false), crash_debug_scans_safe(true),
mouse_enabled(false), waiting_for_command(false),
waiting_for_ui(false),
terminal_resized(false), last_winch(0),
seed(0),
io_inited(false),
need_save(false), save_after_turn(false),
game_started(false), saving_game(false),
updating_scores(false),
parsing_rc(false),
#ifndef USE_TILE_LOCAL
smallterm(false),
#endif
seen_hups(0), map_stat_gen(false), map_stat_dump_disconnect(false),
obj_stat_gen(false), type(GAME_TYPE_NORMAL),
last_type(GAME_TYPE_UNSPECIFIED), last_game_exit(game_exit::unknown),
marked_as_won(false), arena_suspended(false),
generating_level(false), dump_maps(false), test(false), script(false),
build_db(false), use_des_cache(true), tests_selected(),
#ifdef DGAMELAUNCH
throttle(true),
bypassed_startup_menu(true),
#else
throttle(false),
bypassed_startup_menu(false),
#endif
clua_max_memory_mb(16), show_more_prompt(true),
skip_autofight_check(false), terminal_resize_handler(nullptr),
terminal_resize_check(nullptr), doing_prev_cmd_again(false),
prev_cmd(CMD_NO_CMD), repeat_cmd(CMD_NO_CMD),
cmd_repeat_started_unsafe(false),
lua_calls_no_turn(0), lua_script_killed(false),
lua_ready_throttled(false),
stat_gain_prompt(false), simulating_xp_gain(false),
level_annotation_shown(false),
viewport_monster_hp(false), viewport_weapons(false),
tiles_disabled(false),
title_screen(true),
invisible_targeting(false),
darken_range(nullptr), unsaved_macros(false), disables(),
minor_version(-1), save_rcs_version(),
nonempty_buffer_flush_errors(false),
last_builder_error_fatal(false),
mon_act(nullptr)
{
reset_cmd_repeat();
reset_cmd_again();
#ifndef UNIX
no_gdb = "Non-UNIX Platform -> not running gdb.";
#else
no_gdb = access(GDB_PATH, 1) ? "gdb not executable." : 0;
#endif
}
/**
* Cleanup for when the game is reset.
*
* @see main.cc:_reset_game()
*/
void game_state::reset_game()
{
game_started = false;
// need_save is unset by death, but not by saving with restart_after_save.
need_save = false;
type = GAME_TYPE_UNSPECIFIED;
updating_scores = false;
clear_mon_acting();
reset_cmd_repeat();
reset_cmd_again();
}
///////////////////////////////////////////////////////////////////////////
// Repeating commands and doing the previous command over again.
bool game_state::is_replaying_keys() const
{
return crawl_state.doing_prev_cmd_again
|| crawl_state.is_repeating_cmd();
}
bool game_state::is_repeating_cmd() const
{
return repeat_cmd != CMD_NO_CMD;
}
void game_state::cancel_cmd_repeat(string reason, bool force)
{
if (!force && !is_repeating_cmd())
return;
if (repeat_cmd == CMD_WIZARD)
{
// Don't interrupt wizard testing of religion.
if (is_god_acting())
return;
// Don't interrupt wizard testing just because we can't
// move.
if (you.cannot_act())
return;
// We've probably just recovered from being unable to act;
// again, don't interrupt.
if (you.turn_is_over)
return;
}
if (is_replaying_keys() || cmd_repeat_start)
flush_input_buffer(FLUSH_KEY_REPLAY_CANCEL);
if (is_processing_macro())
flush_input_buffer(FLUSH_ABORT_MACRO);
reset_cmd_repeat();
if (!reason.empty())
mpr(reason);
}
void game_state::cancel_cmd_again(string reason, bool force)
{
if (!force && !doing_prev_cmd_again)
return;
if (is_replaying_keys() || cmd_repeat_start)
flush_input_buffer(FLUSH_KEY_REPLAY_CANCEL);
if (is_processing_macro())
flush_input_buffer(FLUSH_ABORT_MACRO);
reset_cmd_again();
if (!reason.empty())
mpr(reason);
}
void game_state::cancel_cmd_all(string reason)
{
cancel_cmd_repeat(reason);
cancel_cmd_again(reason);
}
void game_state::cant_cmd_repeat(string reason)
{
if (reason.empty())
reason = "Can't repeat that command.";
cancel_cmd_repeat(reason);
}
void game_state::cant_cmd_again(string reason)
{
if (reason.empty())
reason = "Can't redo that command.";
cancel_cmd_again(reason);
}
void game_state::cant_cmd_any(string reason)
{
cant_cmd_repeat(reason);
cant_cmd_again(reason);
}
// The method is called to prevent the "no repeating zero turns
// commands" message that input() generates (in the absence of
// cancelling the repetition) for a repeated command that took no
// turns. A wrapper around cancel_cmd_repeat(), its only purpose is
// to make it clear why cancel_cmd_repeat() is being called.
void game_state::zero_turns_taken()
{
ASSERT(!you.turn_is_over);
cancel_cmd_repeat();
}
bool interrupt_cmd_repeat(activity_interrupt ai,
const activity_interrupt_data &at)
{
if (crawl_state.cmd_repeat_start)
return false;
if (crawl_state.repeat_cmd == CMD_WIZARD)
return false;
switch (ai)
{
case activity_interrupt::force:
case activity_interrupt::hp_loss:
case activity_interrupt::monster_attacks:
case activity_interrupt::mimic:
crawl_state.cancel_cmd_repeat("Command repetition interrupted.");
return true;
default:
break;
}
if (ai == activity_interrupt::see_monster)
{
monster* mon = at.mons_data;
ASSERT(mon);
if (!you.can_see(*mon))
return false;
if (crawl_state.cmd_repeat_started_unsafe
&& at.context != SC_NEWLY_SEEN)
{
return false;
}
crawl_state.cancel_cmd_repeat();
#ifndef DEBUG_DIAGNOSTICS
monster_interrupt_message(ai, at);
if (crawl_state.game_is_hints())
hints_monster_seen(*mon);
#else
formatted_string fs(channel_to_colour(MSGCH_WARN));
fs.cprintf("%s (", mon->name(DESC_PLAIN, true).c_str());
monster_info mi(mon);
fs.add_glyph(get_mons_glyph(mi));
fs.cprintf(") in view: (%d,%d), see_cell: %s",
mon->pos().x, mon->pos().y,
you.see_cell(mon->pos())? "yes" : "no");
formatted_mpr(fs, MSGCH_WARN);
#endif
return true;
}
// If command repetition is being used to imitate the rest command,
// then everything interrupts it.
if (crawl_state.repeat_cmd == CMD_WAIT)
{
if (ai == activity_interrupt::full_mp)
crawl_state.cancel_cmd_repeat("Magic restored.");
else if (ai == activity_interrupt::full_hp)
crawl_state.cancel_cmd_repeat("HP restored");
else
crawl_state.cancel_cmd_repeat("Command repetition interrupted.");
return true;
}
if (crawl_state.cmd_repeat_started_unsafe)
return false;
if (ai == activity_interrupt::hit_monster)
{
// This check is for when command repetition is used to
// whack away at a 0xp monster, since the player feels safe
// when the only monsters around are 0xp.
const monster* mon = at.mons_data;
if (!mons_is_threatening(*mon) && mon->visible_to(&you))
return false;
crawl_state.cancel_cmd_repeat("Command repetition interrupted.");
return true;
}
return false;
}
void game_state::reset_cmd_repeat()
{
repeat_cmd = CMD_NO_CMD;
cmd_repeat_start = false;
}
void game_state::reset_cmd_again()
{
doing_prev_cmd_again = false;
prev_cmd = CMD_NO_CMD;
prev_cmd_repeat_goal = 0;
prev_repeat_cmd = CMD_NO_CMD;
prev_cmd_keys.clear();
}
///////////////////////////////////////////////////////////
// Keeping track of which god is currently doing something
///////////////////////////////////////////////////////////
god_act_state::god_act_state()
{
reset();
}
void god_act_state::reset()
{
which_god = GOD_NO_GOD;
retribution = false;
depth = 0;
}
bool game_state::is_god_acting() const
{
ASSERT(god_act.depth >= 0);
bool god_is_acting = god_act.depth > 0;
ASSERT(!(god_is_acting && god_act.which_god == GOD_NO_GOD));
ASSERT(god_is_acting || god_act.which_god == GOD_NO_GOD);
ASSERT(god_is_acting || god_act_stack.empty());
return god_is_acting;
}
bool game_state::is_god_retribution() const
{
ASSERT(is_god_acting());
return god_act.retribution;
}
god_type game_state::which_god_acting() const
{
return god_act.which_god;
}
void game_state::inc_god_acting(bool is_retribution)
{
inc_god_acting(you.religion, is_retribution);
}
void game_state::inc_god_acting(god_type which_god, bool is_retribution)
{
ASSERT(which_god != GOD_NO_GOD);
if (god_act.which_god != GOD_NO_GOD
&& god_act.which_god != which_god)
{
ASSERT(god_act.depth >= 1);
god_act_stack.push_back(god_act);
god_act.reset();
}
god_act.which_god = which_god;
god_act.retribution = is_retribution || god_act.retribution;
god_act.depth++;
}
void game_state::dec_god_acting()
{
dec_god_acting(you.religion);
}
void game_state::dec_god_acting(god_type which_god)
{
ASSERT(which_god != GOD_NO_GOD);
ASSERT(god_act.depth > 0);
ASSERT(god_act.which_god == which_god);
god_act.depth--;
if (god_act.depth == 0)
{
god_act.reset();
if (!god_act_stack.empty())
{
god_act = god_act_stack[god_act_stack.size() - 1];
god_act_stack.pop_back();
ASSERT(god_act.depth >= 1);
ASSERT(god_act.which_god != GOD_NO_GOD);
ASSERT(god_act.which_god != which_god);
}
}
}
void game_state::clear_god_acting()
{
ASSERT(!is_god_acting());
ASSERT(god_act_stack.empty());
god_act.reset();
}
vector<god_act_state> game_state::other_gods_acting() const
{
ASSERT(is_god_acting());
return god_act_stack;
}
bool game_state::is_mon_acting() const
{
return mon_act != nullptr;
}
monster* game_state::which_mon_acting() const
{
return mon_act;
}
void game_state::inc_mon_acting(monster* mon)
{
ASSERT(!invalid_monster(mon));
if (mon_act != nullptr)
mon_act_stack.push_back(mon_act);
mon_act = mon;
}
void game_state::dec_mon_acting(monster* mon)
{
ASSERT(mon_act == mon);
mon_act = nullptr;
const unsigned int size = mon_act_stack.size();
if (size > 0)
{
mon_act = mon_act_stack[size - 1];
ASSERT(!invalid_monster(mon_act));
mon_act_stack.pop_back();
}
}
void game_state::clear_mon_acting()
{
mon_act = nullptr;
mon_act_stack.clear();
}
void game_state::mon_gone(monster* mon)
{
for (unsigned int i = 0, size = mon_act_stack.size(); i < size; i++)
{
if (mon_act_stack[i] == mon)
{
mon_act_stack.erase(mon_act_stack.begin() + i);
i--;
size--;
}
}
if (mon_act == mon)
dec_mon_acting(mon);
}
void game_state::dump()
{
fprintf(stderr, "\nGame state:\n\n");
fprintf(stderr, "mouse_enabled: %d, waiting_for_command: %d, "
"terminal_resized: %d\n",
mouse_enabled, waiting_for_command, terminal_resized);
fprintf(stderr, "io_inited: %d, need_save: %d, saving_game: %d, "
"updating_scores: %d:\n",
io_inited, need_save, saving_game, updating_scores);
fprintf(stderr, "seen_hups: %d, map_stat_gen: %d, type: %d, "
"arena_suspended: %d\n",
seen_hups, map_stat_gen, type, arena_suspended);
if (last_winch)
{
fprintf(stderr, "Last resize was %" PRId64" seconds ago.\n",
(int64_t)(time(0) - last_winch));
}
fprintf(stderr, "\n");
// Arena mode can change behavior of the rest of the code and/or lead
// to asserts.
unwind_var<game_type> _type(type, GAME_TYPE_NORMAL);
unwind_bool _arena_suspended(arena_suspended, false);
fprintf(stderr, "prev_cmd = %s\n", command_to_name(prev_cmd).c_str());
if (doing_prev_cmd_again)
{
fprintf(stderr, "Doing prev_cmd again with keys: ");
for (int key : prev_cmd_keys)
fprintf(stderr, "%d, ", key);
fprintf(stderr, "\n");
fprintf(stderr, "As ASCII keys: ");
for (int key : prev_cmd_keys)
fprintf(stderr, "%c", (char) key);
fprintf(stderr, "\n\n");
}
fprintf(stderr, "repeat_cmd = %s\n", command_to_name(repeat_cmd).c_str());
fprintf(stderr, "\n");
if (god_act.which_god != GOD_NO_GOD || god_act.depth != 0)
{
fprintf(stderr, "God %s currently acting with depth %d\n\n",
god_name(god_act.which_god).c_str(), god_act.depth);
}
if (!god_act_stack.empty())
{
fprintf(stderr, "Other gods acting:\n");
for (const god_act_state &godact : god_act_stack)
{
fprintf(stderr, "God %s with depth %d\n",
god_name(godact.which_god).c_str(), godact.depth);
}
fprintf(stderr, "\n\n");
}
if (mon_act != nullptr)
{
fprintf(stderr, "%s currently acting:\n\n",
debug_mon_str(mon_act).c_str());
debug_dump_mon(mon_act, true);
}
if (!mon_act_stack.empty())
{
fprintf(stderr, "Others monsters acting:\n");
for (const monster *mon : mon_act_stack)
fprintf(stderr, " %s\n", debug_mon_str(mon).c_str());
}
}
bool game_state::player_is_dead() const
{
return updating_scores && !need_save;
}
bool game_state::game_has_random_floors() const
{
return game_is_normal() || game_is_hints() || game_is_descent();
}
bool game_state::game_saves_prefs() const
{
return game_is_normal() || game_is_hints() || game_is_descent();
}
bool game_state::game_is_valid_type() const
{
return type < NUM_GAME_TYPE;
}
bool game_state::game_is_normal() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_NORMAL || type == GAME_TYPE_CUSTOM_SEED
|| type == GAME_TYPE_UNSPECIFIED;
}
bool game_state::game_is_tutorial() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_TUTORIAL;
}
bool game_state::game_is_arena() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_ARENA;
}
bool game_state::game_is_sprint() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_SPRINT;
}
bool game_state::game_is_hints() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_HINTS;
}
bool game_state::game_is_descent() const
{
ASSERT(game_is_valid_type());
return type == GAME_TYPE_DESCENT;
}
bool game_state::game_is_hints_tutorial() const
{
return game_is_hints() || game_is_tutorial();
}
string game_state::game_type_name() const
{
return game_type_name_for(type);
}
string game_state::game_type_name_for(game_type _type)
{
switch (_type)
{
case GAME_TYPE_UNSPECIFIED:
case GAME_TYPE_NORMAL:
case GAME_TYPE_HINTS:
default:
// No explicit game type name for default game.
return "";
case GAME_TYPE_CUSTOM_SEED:
return "Seeded";
case GAME_TYPE_TUTORIAL:
return "Tutorial";
case GAME_TYPE_ARENA:
return "Arena";
case GAME_TYPE_SPRINT:
return "Dungeon Sprint";
case GAME_TYPE_DESCENT:
return "Dungeon Descent";
case NUM_GAME_TYPE:
return "Unknown";
}
}
bool game_state::seed_is_known() const
{
#ifdef DGAMELAUNCH
return player_is_dead()
# ifdef WIZARD
|| you.wizard
# endif
|| type == GAME_TYPE_CUSTOM_SEED;
#else
//offline: it's visible to do what you want with it.
return true;
#endif
}
string game_state::game_savedir_path() const
{
if (!game_is_valid_type())
return ""; // a game from the future...
switch (type)
{
case GAME_TYPE_SPRINT:
return "sprint/";
case GAME_TYPE_DESCENT:
return "descent/";
default:
return "";
}
}
string game_state::game_type_qualifier() const
{
switch (type)
{
case GAME_TYPE_CUSTOM_SEED:
case GAME_TYPE_SPRINT:
case GAME_TYPE_HINTS:
case GAME_TYPE_TUTORIAL:
case GAME_TYPE_DESCENT:
return "-" + gametype_to_str(type);
default:
return "";
}
}
|