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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
|
/*
* File: arena.cc
* Summary: Functions related to the monster arena (stage and watch fights).
*/
#include "AppHdr.h"
#include "arena.h"
#include "areas.h"
#include "artefact.h"
#include "cio.h"
#include "colour.h"
#include "command.h"
#include "coord.h"
#include "dungeon.h"
#include "env.h"
#include "externs.h"
#include "initfile.h"
#include "items.h"
#include "itemname.h" // for make_name()
#include "l_defs.h"
#include "libutil.h"
#include "los.h"
#include "macro.h"
#include "maps.h"
#include "message.h"
#include "mon-behv.h"
#include "mon-iter.h"
#include "mon-pick.h"
#include "mon-util.h"
#include "mon-place.h"
#include "mgen_data.h"
#include "mon-stuff.h"
#include "ng-init.h"
#include "options.h"
#include "spl-mis.h"
#include "spl-util.h"
#include "state.h"
#ifdef USE_TILE
#include "tileview.h"
#endif
#include "view.h"
#include "viewgeom.h"
#define DEBUG_DIAGNOSTICS
extern void world_reacts();
namespace arena
{
void write_error(const std::string &error);
// A faction is just a big list of monsters. Monsters will be dropped
// around the appropriate marker.
struct faction
{
std::string desc;
mons_list members;
bool friendly;
int active_members;
bool won;
std::vector<int> respawn_list;
std::vector<coord_def> respawn_pos;
faction(bool fr) : members(), friendly(fr), active_members(0),
won(false) { }
void place_at(const coord_def &pos);
void reset()
{
active_members = 0;
won = false;
respawn_list.clear();
respawn_pos.clear();
}
void clear()
{
reset();
members.clear();
}
};
std::string teams;
int total_trials = 0;
bool contest_canceled = false;
bool is_respawning = false;
int trials_done = 0;
int team_a_wins = 0;
int ties = 0;
int turns = 0;
bool allow_summons = true;
bool allow_animate = true;
bool allow_chain_summons = true;
bool allow_zero_xp = false;
bool allow_immobile = true;
bool allow_bands = true;
bool name_monsters = false;
bool random_uniques = false;
bool real_summons = false;
bool move_summons = false;
bool respawn = false;
bool move_respawns = false;
bool miscasts = false;
int summon_throttle = INT_MAX;
std::vector<int> uniques_list;
std::vector<int> a_spawners;
std::vector<int> b_spawners;
char to_respawn[MAX_MONSTERS];
int item_drop_times[MAX_ITEMS];
bool banned_glyphs[128];
std::string arena_type = "";
faction faction_a(true);
faction faction_b(false);
coord_def place_a, place_b;
bool cycle_random = false;
int cycle_random_pos = -1;
FILE *file = NULL;
int message_pos = 0;
level_id place(BRANCH_MAIN_DUNGEON, 20);
void adjust_spells(monsters* mons, bool no_summons, bool no_animate)
{
monster_spells &spells(mons->spells);
for (int i = 0; i < NUM_MONSTER_SPELL_SLOTS; ++i)
{
spell_type sp = spells[i];
if (no_summons && spell_typematch(sp, SPTYP_SUMMONING))
spells[i] = SPELL_NO_SPELL;
else if (no_animate && sp == SPELL_ANIMATE_DEAD)
spells[i] = SPELL_NO_SPELL;
}
}
void adjust_monsters()
{
for (monster_iterator mon; mon; ++mon)
{
const bool friendly = mon->friendly();
// Set target to the opposite faction's home base.
mon->target = friendly ? place_b : place_a;
}
}
void list_eq(int imon)
{
if (!Options.arena_list_eq || file == NULL)
return;
const monsters* mon = &menv[imon];
std::vector<int> items;
for (int i = 0; i < NUM_MONSTER_SLOTS; i++)
if (mon->inv[i] != NON_ITEM)
items.push_back(mon->inv[i]);
if (items.size() == 0)
return;
fprintf(file, "%s:\n", mon->name(DESC_PLAIN, true).c_str());
for (unsigned int i = 0; i < items.size(); i++)
{
item_def &item = mitm[items[i]];
fprintf(file, " %s\n",
item.name(DESC_PLAIN, false, true).c_str());
}
}
void faction::place_at(const coord_def &pos)
{
ASSERT(in_bounds(pos));
for (int i = 0, size = members.size(); i < size; ++i)
{
mons_spec spec = members.get_monster(i);
if (friendly)
spec.attitude = ATT_FRIENDLY;
for (int q = 0; q < spec.quantity; ++q)
{
const coord_def loc = pos;
if (!in_bounds(loc))
break;
const int imon = dgn_place_monster(spec, you.absdepth0,
loc, false, true, false);
if (imon == -1)
{
game_ended_with_error(
make_stringf(
"Failed to create monster at (%d,%d) grd: %s",
loc.x, loc.y, dungeon_feature_name(grd(loc))));
}
list_eq(imon);
to_respawn[imon] = i;
}
}
}
void center_print(unsigned sz, std::string text, int number = -1)
{
if (number >= 0)
text = make_stringf("(%d) %s", number, text.c_str());
if (text.length() > sz)
text = text.substr(0, sz);
int padding = (sz - text.length()) / 2 + text.length();
cprintf("%*s", padding, text.c_str());
}
void setup_level()
{
turns = 0;
a_spawners.clear();
b_spawners.clear();
memset(item_drop_times, 0, sizeof(item_drop_times));
if (place.is_valid())
{
you.level_type = place.level_type;
you.where_are_you = place.branch;
you.absdepth0 = place.absdepth();
}
dgn_reset_level();
for (int x = 0; x < GXM; ++x)
for (int y = 0; y < GYM; ++y)
grd[x][y] = DNGN_ROCK_WALL;
unwind_bool gen(Generating_Level, true);
typedef unwind_var< std::set<std::string> > unwind_stringset;
const unwind_stringset mtags(you.uniq_map_tags);
const unwind_stringset mnames(you.uniq_map_names);
std::string map_name = "arena_" + arena_type;
const map_def *map = random_map_for_tag(map_name.c_str());
if (!map)
throw make_stringf("No arena maps named \"%s\"", arena_type.c_str());
#ifdef USE_TILE
tile_init_default_flavour();
tile_clear_flavour();
#endif
ASSERT(map);
bool success = dgn_place_map(map, true, true);
if (!success)
throw make_stringf("Failed to create arena named \"%s\"",
arena_type.c_str());
link_items();
if (!env.rock_colour)
env.rock_colour = CYAN;
if (!env.floor_colour)
env.floor_colour = LIGHTGREY;
#ifdef USE_TILE
tile_new_level(true);
#endif
los_changed();
env.markers.activate_all();
}
std::string find_monster_spec()
{
if (!teams.empty())
return (teams);
else
return ("random v random");
}
void parse_faction(faction &fact, std::string spec)
throw (std::string)
{
fact.clear();
fact.desc = spec;
std::vector<std::string> monsters = split_string(",", spec);
for (int i = 0, size = monsters.size(); i < size; ++i)
{
const std::string err = fact.members.add_mons(monsters[i], false);
if (!err.empty())
throw err;
}
}
void parse_monster_spec()
throw (std::string)
{
std::string spec = find_monster_spec();
allow_chain_summons = !strip_tag(spec, "no_chain_summons");
allow_summons = !strip_tag(spec, "no_summons");
allow_animate = !strip_tag(spec, "no_animate");
allow_immobile = !strip_tag(spec, "no_immobile");
allow_bands = !strip_tag(spec, "no_bands");
allow_zero_xp = strip_tag(spec, "allow_zero_xp");
real_summons = strip_tag(spec, "real_summons");
move_summons = strip_tag(spec, "move_summons");
miscasts = strip_tag(spec, "miscasts");
respawn = strip_tag(spec, "respawn");
move_respawns = strip_tag(spec, "move_respawns");
summon_throttle = strip_number_tag(spec, "summon_throttle:");
if (real_summons && respawn)
throw (std::string("Can't set real_summons and respawn at "
"same time."));
if (summon_throttle <= 0)
summon_throttle = INT_MAX;
cycle_random = strip_tag(spec, "cycle_random");
name_monsters = strip_tag(spec, "names");
random_uniques = strip_tag(spec, "random_uniques");
const int ntrials = strip_number_tag(spec, "t:");
if (ntrials != TAG_UNFOUND && ntrials >= 1 && ntrials <= 99
&& !total_trials)
total_trials = ntrials;
arena_type = strip_tag_prefix(spec, "arena:");
if (arena_type.empty())
arena_type = "default";
const int arena_delay = strip_number_tag(spec, "delay:");
if (arena_delay >= 0 && arena_delay < 2000)
Options.arena_delay = arena_delay;
std::string arena_place = strip_tag_prefix(spec, "arena_place:");
if (!arena_place.empty())
{
try
{
place = level_id::parse_level_id(arena_place);
}
catch (const std::string &err)
{
throw make_stringf("Bad place '%s': %s",
arena_place.c_str(),
err.c_str());
}
if (place.level_type == LEVEL_LABYRINTH)
{
throw (std::string("Can't set arena place to the "
"labyrinth."));
}
else if (place.level_type == LEVEL_PORTAL_VAULT)
{
throw (std::string("Can't set arena place to a portal "
"vault."));
}
}
const std::string glyphs = strip_tag_prefix(spec, "ban_glyphs:");
for (unsigned int i = 0; i < glyphs.size(); i++)
if (!(glyphs[i] & !127))
banned_glyphs[static_cast<int>(glyphs[i])] = true;
std::vector<std::string> factions = split_string(" v ", spec);
if (factions.size() == 1)
factions = split_string(" vs ", spec);
if (factions.size() != 2)
throw make_stringf("Expected arena monster spec \"xxx v yyy\", "
"but got \"%s\"", spec.c_str());
try
{
parse_faction(faction_a, factions[0]);
parse_faction(faction_b, factions[1]);
}
catch (const std::string &err)
{
throw make_stringf("Bad monster spec \"%s\": %s",
spec.c_str(),
err.c_str());
}
if (faction_a.desc == faction_b.desc)
{
faction_a.desc += " (A)";
faction_b.desc += " (B)";
}
}
void setup_monsters()
throw (std::string)
{
faction_a.reset();
faction_b.reset();
for (int i = 0; i < MAX_MONSTERS; i++)
to_respawn[i] = -1;
unwind_var< FixedVector<bool, NUM_MONSTERS> >
uniq(you.unique_creatures);
place_a = dgn_find_feature_marker(DNGN_STONE_STAIRS_UP_I);
place_b = dgn_find_feature_marker(DNGN_STONE_STAIRS_DOWN_I);
// Place the different factions in different orders on
// alternating rounds so that one side doesn't get the
// first-move advantage for all rounds.
if (trials_done & 1)
{
faction_a.place_at(place_a);
faction_b.place_at(place_b);
}
else
{
faction_b.place_at(place_b);
faction_a.place_at(place_a);
}
adjust_monsters();
}
void show_fight_banner(bool after_fight = false)
{
int line = 1;
cgotoxy(1, line++, GOTO_STAT);
textcolor(WHITE);
center_print(crawl_view.hudsz.x, "Crawl " + Version::Long());
line++;
cgotoxy(1, line++, GOTO_STAT);
textcolor(YELLOW);
center_print(crawl_view.hudsz.x, faction_a.desc,
total_trials ? team_a_wins : -1);
cgotoxy(1, line++, GOTO_STAT);
textcolor(LIGHTGREY);
center_print(crawl_view.hudsz.x, "vs");
cgotoxy(1, line++, GOTO_STAT);
textcolor(YELLOW);
center_print(crawl_view.hudsz.x, faction_b.desc,
total_trials ? trials_done - team_a_wins - ties : -1);
if (total_trials > 1 && trials_done < total_trials)
{
cgotoxy(1, line++, GOTO_STAT);
textcolor(BROWN);
center_print(crawl_view.hudsz.x,
make_stringf("Round %d of %d",
after_fight ? trials_done
: trials_done + 1,
total_trials));
}
else
{
cgotoxy(1, line++, GOTO_STAT);
textcolor(BROWN);
clear_to_end_of_line();
}
}
void setup_others()
{
you.species = SP_HUMAN;
you.char_class = JOB_FIGHTER;
you.experience_level = 27;
you.position.y = -1;
coord_def yplace(dgn_find_feature_marker(DNGN_ESCAPE_HATCH_UP));
crawl_view.set_player_at(yplace);
you.mutation[MUT_ACUTE_VISION] = 3;
you.your_name = "Arena";
you.hp = you.hp_max = 99;
for (int i = 0; i < NUM_STATS; ++i)
you.base_stats[i] = 20;
Options.show_gold_turns = false;
show_fight_banner();
}
void expand_mlist(int exp)
{
crawl_view.mlistp.y -= exp;
crawl_view.mlistsz.y += exp;
}
void setup_fight()
throw (std::string)
{
//no_messages mx;
parse_monster_spec();
setup_level();
// Monster setup may block waiting for matchups.
setup_monsters();
setup_others();
}
// Temporarily reset crawl_state.type to force a --more-- to happen.
void more()
{
unwind_var<game_type> type(crawl_state.type, GAME_TYPE_NORMAL);
::more();
}
void count_foes()
{
int orig_a = faction_a.active_members;
int orig_b = faction_b.active_members;
if (orig_a < 0)
mpr("Book-keeping says faction_a has negative active members.",
MSGCH_ERROR);
if (orig_b < 0)
mpr("Book-keeping says faction_b has negative active members.",
MSGCH_ERROR);
faction_a.active_members = 0;
faction_b.active_members = 0;
for (monster_iterator mons; mons; ++mons)
{
if (mons->attitude == ATT_FRIENDLY)
faction_a.active_members++;
else if (mons->attitude == ATT_HOSTILE)
faction_b.active_members++;
}
if (orig_a != faction_a.active_members
|| orig_b != faction_b.active_members)
{
mpr("Book-keeping error in faction member count.", MSGCH_ERROR);
if (faction_a.active_members > 0
&& faction_b.active_members <= 0)
{
faction_a.won = true;
faction_b.won = false;
}
else if (faction_b.active_members > 0
&& faction_a.active_members <= 0)
{
faction_b.won = true;
faction_a.won = false;
}
}
}
// Returns true as long as at least one member of each faction is alive.
bool fight_is_on()
{
if (faction_a.active_members > 0 && faction_b.active_members > 0)
{
if (faction_a.won || faction_b.won)
{
mpr("Both factions alive but one declared the winner.",
MSGCH_ERROR);
faction_a.won = false;
faction_b.won = false;
}
return (true);
}
// Sync up our book-keeping with the actual state, and report
// any inconsistencies.
count_foes();
return (faction_a.active_members > 0 && faction_b.active_members > 0);
}
void report_foes()
{
for (monster_iterator mons; mons; ++mons)
{
if (mons->type == MONS_SIGMUND)
{
coord_def where;
if (mons->get_foe())
where = mons->get_foe()->pos();
mprf("%s (%d,%d) foe: %s (%d,%d)",
mons->name(DESC_PLAIN).c_str(),
mons->pos().x, mons->pos().y,
mons->get_foe()? mons->get_foe()->name(DESC_PLAIN).c_str()
: "(none)",
where.x, where.y);
}
}
}
void fixup_foes()
{
for (monster_iterator mons; mons; ++mons)
behaviour_event(*mons, ME_DISTURB, MHITNOT, mons->pos());
}
void dump_messages()
{
if (!Options.arena_dump_msgs || file == NULL)
return;
std::vector<int> channels;
std::vector<std::string> messages =
get_recent_messages(message_pos,
!Options.arena_dump_msgs_all,
&channels);
for (unsigned int i = 0; i < messages.size(); i++)
{
std::string msg = messages[i];
int chan = channels[i];
std::string prefix;
switch (chan)
{
// Ignore messages generated while the user examines
// the arnea.
case MSGCH_PROMPT:
case MSGCH_MONSTER_TARGET:
case MSGCH_FLOOR_ITEMS:
case MSGCH_EXAMINE:
case MSGCH_EXAMINE_FILTER:
continue;
// If a monster-damage message ends with '!' it's a
// death message, otherwise it's an examination message
// and should be skipped.
case MSGCH_MONSTER_DAMAGE:
if (msg[msg.length() - 1] != '!')
continue;
break;
case MSGCH_ERROR: prefix = "ERROR: "; break;
case MSGCH_WARN: prefix = "WARN: "; break;
case MSGCH_DIAGNOSTICS: prefix = "DIAG: "; break;
case MSGCH_SOUND: prefix = "SOUND: "; break;
case MSGCH_TALK_VISUAL:
case MSGCH_TALK: prefix = "TALK: "; break;
}
msg = prefix + msg;
fprintf(file, "%s\n", msg.c_str());
}
}
// Try to prevent random luck from letting one spawner fill up the
// arena with so many monsters that the other spawner can never get
// back on even footing.
void balance_spawners()
{
if (a_spawners.size() == 0 || b_spawners.size() == 0)
return;
if (faction_a.active_members == 0 || faction_b.active_members == 0)
{
mpr("ERROR: Both sides have spawners, but the active member "
"count of one side has been reduced to zero!", MSGCH_ERROR);
return;
}
for (unsigned int i = 0; i < a_spawners.size(); i++)
{
int idx = a_spawners[i];
menv[idx].speed_increment *= faction_b.active_members;
menv[idx].speed_increment /= faction_a.active_members;
}
for (unsigned int i = 0; i < b_spawners.size(); i++)
{
int idx = b_spawners[i];
menv[idx].speed_increment *= faction_a.active_members;
menv[idx].speed_increment /= faction_b.active_members;
}
}
void do_miscasts()
{
if (!miscasts)
return;
for (monster_iterator mon; mon; ++mon)
{
if (mon->type == MONS_TEST_SPAWNER)
continue;
MiscastEffect(*mon, mon->mindex(), SPTYP_RANDOM,
random_range(1, 3), "arena miscast", NH_NEVER);
}
}
void handle_keypress(int ch)
{
if (key_is_escape(ch) || tolower(ch) == 'q')
{
contest_canceled = true;
mpr("Canceled contest at user request");
return;
}
const command_type cmd = key_to_command(ch, KMC_DEFAULT);
// We only allow a short list of commands to be used in the arena.
switch (cmd)
{
case CMD_LOOK_AROUND:
case CMD_SUSPEND_GAME:
case CMD_REPLAY_MESSAGES:
break;
default:
return;
}
if (file != NULL)
fflush(file);
cursor_control coff(true);
unwind_var<game_type> type(crawl_state.type, GAME_TYPE_NORMAL);
unwind_bool ar_susp(crawl_state.arena_suspended, true);
coord_def yplace(dgn_find_feature_marker(DNGN_ESCAPE_HATCH_UP));
unwind_var<coord_def> pos(you.position);
you.position = yplace;
process_command(cmd);
}
void do_respawn(faction &fac)
{
is_respawning = true;
for (unsigned int _i = fac.respawn_list.size(); _i > 0; _i--)
{
unsigned int i = _i - 1;
coord_def pos = fac.respawn_pos[i];
int spec_idx = fac.respawn_list[i];
mons_spec spec = fac.members.get_monster(spec_idx);
if (fac.friendly)
spec.attitude = ATT_FRIENDLY;
int idx = dgn_place_monster(spec, you.absdepth0, pos, false,
true);
if (idx == -1 && fac.active_members == 0
&& monster_at(pos))
{
// We have no members left, so to prevent the round
// from ending attempt to displace whatever is in
// our position.
int midx = mgrd(pos);
monsters* other = &menv[midx];
if (to_respawn[midx] == -1)
{
// The other monster isn't a respawner itself, so
// just get rid of it.
mprf(MSGCH_DIAGNOSTICS,
"Dismissing non-repsawner %s to make room "
"respawner whose side has 0 active members.",
other->name(DESC_PLAIN, true).c_str());
monster_die(other, KILL_DISMISSED, NON_MONSTER);
}
else
{
// Other monster is a respawner, try to move it.
mprf(MSGCH_DIAGNOSTICS,
"Teleporting respawner %s to make room "
"other respawner whose side has 0 active members.",
other->name(DESC_PLAIN, true).c_str());
monster_teleport(other, true);
}
idx = dgn_place_monster(spec, you.absdepth0, pos, false,
true);
}
if (idx != -1)
{
// We succeeded, so remove from list.
fac.respawn_list.erase(fac.respawn_list.begin() + i);
fac.respawn_pos.erase(fac.respawn_pos.begin() + i);
to_respawn[idx] = spec_idx;
if (move_respawns)
monster_teleport(&menv[idx], true, true);
}
else
{
// Couldn't respawn, so leave it on the list; hopefully
// space will open up later.
}
}
is_respawning = false;
}
void do_fight()
{
viewwindow();
mesclr(true);
{
cursor_control coff(false);
while (fight_is_on())
{
if (kbhit())
{
const int ch = getchm();
handle_keypress(ch);
ASSERT(crawl_state.game_is_arena() && !crawl_state.arena_suspended);
if (contest_canceled)
return;
}
#ifdef DEBUG_DIAGNOSTICS
mprf("---- Turn #%d ----", turns);
#endif
// Check the consistency of our book-keeping every 100 turns.
if ((turns++ % 100) == 0)
count_foes();
viewwindow();
you.time_taken = 10;
// Make sure we don't starve.
you.hunger = 10999;
//report_foes();
world_reacts();
do_miscasts();
do_respawn(faction_a);
do_respawn(faction_b);
balance_spawners();
delay(Options.arena_delay);
mesclr();
dump_messages();
ASSERT(you.pet_target == MHITNOT);
}
viewwindow();
}
mesclr();
trials_done++;
// We bother with all this to properly deal with ties, and with
// ball lightning or giant spores winning the fight via suicide.
// The sanity checking is probably just paranoia.
bool was_tied = false;
if (!faction_a.won && !faction_b.won)
{
if (faction_a.active_members > 0)
{
mpr("Tie declared, but faction_a won.", MSGCH_ERROR);
team_a_wins++;
faction_a.won = true;
}
else if (faction_b.active_members > 0)
{
mpr("Tie declared, but faction_b won.", MSGCH_ERROR);
faction_b.won = true;
}
else
{
ties++;
was_tied = true;
}
}
else if (faction_a.won && faction_b.won)
{
faction_a.won = false;
faction_b.won = false;
mpr("*BOTH* factions won?!", MSGCH_ERROR);
if (faction_a.active_members > 0)
{
mpr("Faction_a real winner.", MSGCH_ERROR);
team_a_wins++;
faction_a.won = true;
}
else if (faction_b.active_members > 0)
{
mpr("Faction_b real winner.", MSGCH_ERROR);
faction_b.won = true;
}
else
{
mpr("Both sides dead.", MSGCH_ERROR);
ties++;
was_tied = true;
}
}
else if (faction_a.won)
team_a_wins++;
show_fight_banner(true);
std::string msg;
if (was_tied)
msg = "Tie";
else
msg = "Winner: %s!";
if (Options.arena_dump_msgs || Options.arena_list_eq)
msg = "---------- " + msg + " ----------";
if (was_tied)
mprf(msg.c_str());
else
mprf(msg.c_str(),
faction_a.won ? faction_a.desc.c_str()
: faction_b.desc.c_str());
dump_messages();
}
void global_setup(const std::string& arena_teams)
{
// [ds] Turning off view_lock crashes arena.
Options.view_lock_x = Options.view_lock_y = true;
teams = arena_teams;
// Set various options from the arena spec's tags
try
{
parse_monster_spec();
}
catch (const std::string &error)
{
write_error(error);
game_ended_with_error(error);
}
if (file != NULL)
end(0, false, "Results file already open");
file = fopen("arena.result", "w");
if (file != NULL)
{
std::string spec = find_monster_spec();
fprintf(file, "%s\n", spec.c_str());
if (Options.arena_dump_msgs || Options.arena_list_eq)
fprintf(file, "========================================\n");
}
expand_mlist(5);
for (int i = 0; i < NUM_MONSTERS; i++)
{
if (i == MONS_PLAYER_GHOST)
continue;
if (mons_is_unique(i)
&& !arena_veto_random_monster(static_cast<monster_type>(i)))
{
uniques_list.push_back(i);
}
}
}
void global_shutdown()
{
if (file != NULL)
fclose(file);
file = NULL;
}
void write_results()
{
if (file != NULL)
{
if (Options.arena_dump_msgs || Options.arena_list_eq)
fprintf(file, "========================================\n");
fprintf(file, "%d-%d", team_a_wins,
trials_done - team_a_wins - ties);
if (ties > 0)
fprintf(file, "-%d", ties);
fprintf(file, "\n");
}
}
void write_error(const std::string &error)
{
if (file != NULL)
{
fprintf(file, "err: %s\n", error.c_str());
fclose(file);
}
file = NULL;
}
void simulate()
{
init_level_connectivity();
do
{
try
{
setup_fight();
}
catch (const std::string &error)
{
write_error(error);
game_ended_with_error(error);
}
do_fight();
if (trials_done < total_trials)
delay(Options.arena_delay * 5);
}
while (!contest_canceled && trials_done < total_trials);
if (total_trials > 0)
{
mprf("Final score: %s (%d); %s (%d) [%d ties]",
faction_a.desc.c_str(), team_a_wins,
faction_b.desc.c_str(), trials_done - team_a_wins - ties,
ties);
}
delay(Options.arena_delay * 5);
write_results();
}
}
/////////////////////////////////////////////////////////////////////////////
// Various arena callbacks
monster_type arena_pick_random_monster(const level_id &place, int power,
int &lev_mons)
{
if (arena::random_uniques)
{
const std::vector<int> &uniques = arena::uniques_list;
const int type = uniques[random2(uniques.size())];
you.unique_creatures[type] = false;
return static_cast<monster_type>(type);
}
if (!arena::cycle_random)
return (RANDOM_MONSTER);
for (int tries = 0; tries <= NUM_MONSTERS; tries++)
{
arena::cycle_random_pos++;
if (arena::cycle_random_pos >= NUM_MONSTERS)
arena::cycle_random_pos = 0;
const monster_type type =
static_cast<monster_type>(arena::cycle_random_pos);
if (mons_rarity(type, place) == 0)
continue;
if (arena_veto_random_monster(type))
continue;
return (type);
}
game_ended_with_error(
make_stringf("No random monsters for place '%s'",
arena::place.describe().c_str()));
return (NUM_MONSTERS);
}
bool arena_veto_random_monster(monster_type type)
{
if (!arena::allow_immobile && mons_class_is_stationary(type))
return (true);
if (!arena::allow_zero_xp && mons_class_flag(type, M_NO_EXP_GAIN))
return (true);
if (!(mons_char(type) & !127) && arena::banned_glyphs[mons_char(type)])
return (true);
return (false);
}
bool arena_veto_place_monster(const mgen_data &mg, bool first_band_member,
const coord_def& pos)
{
// If the first band member makes it past the summon throttle cut,
// let all of the rest of its band in too regardless of the summon
// throttle.
if (mg.abjuration_duration > 0 && first_band_member)
{
if (mg.behaviour == BEH_FRIENDLY
&& arena::faction_a.active_members > arena::summon_throttle)
{
return (true);
}
else if (mg.behaviour == BEH_HOSTILE
&& arena::faction_b.active_members > arena::summon_throttle)
{
return (true);
}
}
return (!arena::allow_bands && !first_band_member
|| !(mons_char(mg.cls) & !127)
&& arena::banned_glyphs[mons_char(mg.cls)]);
}
// XXX: Still having some trouble with book-keeping if a slime creature
// is placed via splitting.
void arena_placed_monster(monsters *monster)
{
if (monster->attitude == ATT_FRIENDLY)
{
arena::faction_a.active_members++;
arena::faction_b.won = false;
}
else if (monster->attitude == ATT_HOSTILE)
{
arena::faction_b.active_members++;
arena::faction_a.won = false;
}
else
{
mprf(MSGCH_ERROR, "Placed neutral (%d) monster %s",
static_cast<int>(monster->attitude),
monster->name(DESC_PLAIN, true).c_str());
}
if (!arena::allow_summons || !arena::allow_animate)
{
arena::adjust_spells(monster, !arena::allow_summons,
!arena::allow_animate);
}
if (monster->type == MONS_TEST_SPAWNER)
{
if (monster->attitude == ATT_FRIENDLY)
arena::a_spawners.push_back(monster->mindex());
else if (monster->attitude == ATT_HOSTILE)
arena::b_spawners.push_back(monster->mindex());
}
const bool summoned = monster->is_summoned();
#ifdef DEBUG_DIAGNOSTICS
mprf("%s %s!",
monster->full_name(DESC_CAP_A, true).c_str(),
arena::is_respawning ? "respawns" :
(summoned && ! arena::real_summons) ? "is summoned"
: "enters the arena");
#endif
for (int i = 0; i < NUM_MONSTER_SLOTS; i++)
{
short it = monster->inv[i];
if (it != NON_ITEM)
{
item_def &item(mitm[it]);
item.flags |= ISFLAG_IDENT_MASK;
// Don't leak info on wands or potions.
if (item.base_type == OBJ_WANDS
|| item.base_type == OBJ_POTIONS)
{
item.colour = random_colour();
}
// Set the "drop" time here in case the monster drops the
// item without dying, like being polymorphed.
arena::item_drop_times[it] = arena::turns;
}
}
if (arena::name_monsters && !monster->is_named())
monster->mname = make_name(random_int(), false);
if (summoned)
{
// Real summons drop corpses and items.
if (arena::real_summons)
{
monster->del_ench(ENCH_ABJ, true, false);
for (int i = 0; i < NUM_MONSTER_SLOTS; i++)
{
short it = monster->inv[i];
if (it != NON_ITEM)
mitm[it].flags &= ~ISFLAG_SUMMONED;
}
}
if (arena::move_summons)
monster_teleport(monster, true, true);
if (!arena::allow_chain_summons)
arena::adjust_spells(monster, true, false);
}
}
// Take care of respawning slime creatures merging and then splitting.
void arena_split_monster(monsters *split_from, monsters *split_to)
{
if (!arena::respawn)
return;
const int from_idx = split_from->mindex();
const int member_idx = arena::to_respawn[from_idx];
if (member_idx == -1)
return;
arena::to_respawn[split_to->mindex()] = member_idx;
}
void arena_monster_died(monsters *monster, killer_type killer,
int killer_index, bool silent, int corpse)
{
if (monster->attitude == ATT_FRIENDLY)
arena::faction_a.active_members--;
else if (monster->attitude == ATT_HOSTILE)
arena::faction_b.active_members--;
if (arena::faction_a.active_members > 0
&& arena::faction_b.active_members <= 0)
{
arena::faction_a.won = true;
}
else if (arena::faction_b.active_members > 0
&& arena::faction_a.active_members <= 0)
{
arena::faction_b.won = true;
}
// Everyone is dead. Is it a tie, or something else?
else if (arena::faction_a.active_members <= 0
&& arena::faction_b.active_members <= 0)
{
if (monster->flags & MF_HARD_RESET && !MON_KILL(killer))
game_ended_with_error("Last arena monster was dismissed.");
// If all monsters are dead, and the last one to die is a giant
// spore or ball lightning, then that monster's faction is the
// winner, since self-destruction is their purpose. But if a
// trap causes the spore to explode, and that kills everything,
// it's a tie, since it counts as the trap killing everyone.
else if (mons_self_destructs(monster) && MON_KILL(killer))
{
if (monster->attitude == ATT_FRIENDLY)
arena::faction_a.won = true;
else if (monster->attitude == ATT_HOSTILE)
arena::faction_b.won = true;
}
}
// Only respawn those monsers which were initally placed in the
// arena.
const int midx = monster->mindex();
if (arena::respawn && arena::to_respawn[midx] != -1
// Don't respawn when a slime 'dies' from merging with another
// slime.
&& !(monster->type == MONS_SLIME_CREATURE && silent
&& killer == KILL_MISC
&& killer_index == NON_MONSTER))
{
arena::faction *fac = NULL;
if (monster->attitude == ATT_FRIENDLY)
fac = &arena::faction_a;
else if (monster->attitude == ATT_HOSTILE)
fac = &arena::faction_b;
if (fac)
{
int member_idx = arena::to_respawn[midx];
fac->respawn_list.push_back(member_idx);
fac->respawn_pos.push_back(monster->pos());
// Un-merge slime when it respawns, but only if it's
// specifically a slime, and not a random monster which
// happens to be a slime.
if (monster->type == MONS_SLIME_CREATURE
&& (fac->members.get_monster(member_idx).mid
== MONS_SLIME_CREATURE))
{
for (unsigned int i = 1; i < monster->number; i++)
{
fac->respawn_list.push_back(member_idx);
fac->respawn_pos.push_back(monster->pos());
}
}
arena::to_respawn[midx] = -1;
}
}
if (corpse != -1 && corpse != NON_ITEM)
arena::item_drop_times[corpse] = arena::turns;
// Won't be dropping any items.
if (monster->flags & MF_HARD_RESET)
return;
for (int i = 0; i < NUM_MONSTER_SLOTS; i++)
{
int idx = monster->inv[i];
if (idx == NON_ITEM)
continue;
if (mitm[idx].flags & ISFLAG_SUMMONED)
continue;
arena::item_drop_times[idx] = arena::turns;
}
}
static bool _sort_by_age(int a, int b)
{
return (arena::item_drop_times[a] < arena::item_drop_times[b]);
}
#define DESTROY_ITEM(i) \
{ \
destroy_item(i, true); \
arena::item_drop_times[i] = 0; \
cull_count++; \
if (first_avail == NON_ITEM) \
first_avail = i; \
}
// Culls the items which have been on the floor the longest, culling the
// newest items last. Items which a monster dropped voluntarily or
// because of being polymorphed, rather than because of dying, are
// culled earlier than they should be, but it's not like we have to be
// fair to the arena monsters.
int arena_cull_items()
{
std::vector<int> items;
int first_avail = NON_ITEM;
for (int i = 0; i < MAX_ITEMS; i++)
{
// All items in mitm[] are valid when we're called.
const item_def &item(mitm[i]);
// We want floor items.
if (!in_bounds(item.pos))
continue;
items.push_back(i);
}
// Cull half of items on the floor.
const int cull_target = items.size() / 2;
int cull_count = 0;
std::sort(items.begin(), items.end(), _sort_by_age);
std::vector<int> ammo;
for (unsigned int i = 0, end = items.size(); i < end; i++)
{
const int idx = items[i];
const item_def &item(mitm[idx]);
// If the drop time is 0 then this is probably thrown ammo.
if (arena::item_drop_times[idx] == 0)
{
// We know it's at least this old.
arena::item_drop_times[idx] = arena::turns;
// Arrows/needles/etc on the floor is just clutter.
if (item.base_type != OBJ_MISSILES
|| item.sub_type == MI_JAVELIN
|| item.sub_type == MI_THROWING_NET)
{
ammo.push_back(idx);
continue;
}
}
DESTROY_ITEM(idx);
if (cull_count >= cull_target)
break;
}
if (cull_count >= cull_target)
{
#ifdef DEBUG_DIAGNOSTICS
mprf(MSGCH_DIAGNOSTICS, "On turn #%d culled %d items dropped by "
"monsters, done.",
arena::turns, cull_count);
#endif
return (first_avail);
}
#ifdef DEBUG_DIAGNOSTICS
mprf(MSGCH_DIAGNOSTICS, "On turn #%d culled %d items dropped by "
"monsters, culling some more.",
arena::turns, cull_count);
#endif
const int count1 = cull_count;
for (unsigned int i = 0; i < ammo.size(); i++)
{
DESTROY_ITEM(ammo[i]);
if (cull_count >= cull_target)
break;
}
if (cull_count >= cull_target)
{
#ifdef DEBUG_DIAGNOSTICS
mprf(MSGCH_DIAGNOSTICS, "Culled %d (probably) ammo items, done.",
cull_count - count1);
#endif
return (first_avail);
}
#ifdef DEBUG_DIAGNOSTICS
mprf(MSGCH_DIAGNOSTICS, "Culled %d items total, short of target %d.",
cull_count, cull_target);
#endif
return (first_avail);
} // arena_cull_items
/////////////////////////////////////////////////////////////////////////////
static void _init_arena()
{
run_map_preludes();
initialise_item_descriptions();
}
void run_arena(const std::string& teams)
{
_init_arena();
ASSERT(!crawl_state.arena_suspended);
#ifdef WIZARD
// The playe has wizard powers for the duration of the arena.
unwind_bool wiz(you.wizard, true);
#endif
arena::global_setup(teams);
arena::simulate();
arena::global_shutdown();
game_ended();
}
|