From 20499e4d29a0af3143ca2aaa163390d97c04c258 Mon Sep 17 00:00:00 2001 From: Darshan Shaligram Date: Sun, 18 Oct 2009 18:09:22 +0530 Subject: Add test for monster placement. Adds test/monplace.lua to test placing monsters using the Lua direct monster creation binding (does not exercise all the map code). Allow choosing what tests to run with "crawl -test test1,test2". --- crawl-ref/source/ctest.cc | 18 +++++++++ crawl-ref/source/debug.cc | 75 ++------------------------------------ crawl-ref/source/initfile.cc | 5 +++ crawl-ref/source/luadgn.cc | 10 +++++ crawl-ref/source/monstuff.cc | 49 +++++++++++++++++++++++++ crawl-ref/source/monstuff.h | 1 + crawl-ref/source/state.h | 1 + crawl-ref/source/test/monplace.lua | 24 ++++++++++++ 8 files changed, 112 insertions(+), 71 deletions(-) create mode 100644 crawl-ref/source/test/monplace.lua (limited to 'crawl-ref') diff --git a/crawl-ref/source/ctest.cc b/crawl-ref/source/ctest.cc index c0c7f743c6..daca476c7d 100644 --- a/crawl-ref/source/ctest.cc +++ b/crawl-ref/source/ctest.cc @@ -20,6 +20,7 @@ #include "files.h" #include "luadgn.h" #include "maps.h" +#include "state.h" #include "stuff.h" #include @@ -73,8 +74,25 @@ namespace crawl_tests luaL_openlib(dlua, "crawl", crawl_test_lib, 0); } + bool is_test_selected(const std::string &testname) + { + if (crawl_state.tests_selected.empty()) + return (true); + for (int i = 0, size = crawl_state.tests_selected.size(); + i < size; ++i) + { + const std::string &phrase(crawl_state.tests_selected[i]); + if (testname.find(phrase) != std::string::npos) + return (true); + } + return (false); + } + void run_test(const std::string &file) { + if (!is_test_selected(file)) + return; + ++ntests; const std::string path(catpath(test_dir, file)); dlua.execfile(path.c_str(), true, false); diff --git a/crawl-ref/source/debug.cc b/crawl-ref/source/debug.cc index 451b00f500..6305da0922 100644 --- a/crawl-ref/source/debug.cc +++ b/crawl-ref/source/debug.cc @@ -5097,32 +5097,6 @@ void debug_place_map() debug_load_map_by_name(what); } -// Make all of the monster's original equipment disappear, unless it's a fixed -// artefact or unrand artefact. -static void _vanish_orig_eq(monsters* mons) -{ - for (int i = 0; i < NUM_MONSTER_SLOTS; ++i) - { - if (mons->inv[i] == NON_ITEM) - continue; - - item_def &item(mitm[mons->inv[i]]); - - if (!is_valid_item(item)) - continue; - - if (item.orig_place != 0 || item.orig_monnum != 0 - || !item.inscription.empty() - || is_unrandom_artefact(item) - || (item.flags & (ISFLAG_DROPPED | ISFLAG_THROWN | ISFLAG_NOTED_GET - | ISFLAG_BEEN_IN_INV) ) ) - { - continue; - } - item.flags |= ISFLAG_SUMMONED; - } -} - // Detects all monsters on the level, using their exact positions. void wizard_detect_creatures() { @@ -5141,7 +5115,7 @@ void wizard_detect_creatures() // specified regex. void wizard_dismiss_all_monsters(bool force_all) { - char buf[80]; + char buf[80] = ""; if (!force_all) { mpr("Regex of monsters to dismiss (ENTER for all): ", MSGCH_PROMPT); @@ -5154,51 +5128,10 @@ void wizard_dismiss_all_monsters(bool force_all) } } - // Make all of the monsters' original equipment disappear unless "keepitem" - // is found in the regex (except for fixed arts and unrand arts). - bool keep_item = false; - if (strstr(buf, "keepitem") != NULL) - { - std::string str = replace_all(buf, "keepitem", ""); - trim_string(str); - strcpy(buf, str.c_str()); - - keep_item = true; - } - - // Dismiss all - if (buf[0] == '\0' || force_all) - { - // Genocide... "unsummon" all the monsters from the level. - for (int mon = 0; mon < MAX_MONSTERS; mon++) - { - monsters *monster = &menv[mon]; - - if (monster->alive()) - { - if (!keep_item) - _vanish_orig_eq(monster); - monster_die(monster, KILL_DISMISSED, NON_MONSTER, false, true); - } - } - // If it was turned off turn autopickup back on. + dismiss_monsters(buf); + // If it was turned off turn autopickup back on if all monsters went away. + if (!*buf) autotoggle_autopickup(false); - return; - } - - // Dismiss by regex - text_pattern tpat(buf); - for (int mon = 0; mon < MAX_MONSTERS; mon++) - { - monsters *monster = &menv[mon]; - - if (monster->alive() && tpat.matches(monster->name(DESC_PLAIN, true))) - { - if (!keep_item) - _vanish_orig_eq(monster); - monster_die(monster, KILL_DISMISSED, NON_MONSTER, false, true); - } - } } static void _debug_kill_traps() diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index 710f8e8321..7ad2e809a6 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -3703,6 +3703,11 @@ bool parse_args( int argc, char **argv, bool rc_only ) case CLO_TEST: crawl_state.test = true; + if (next_is_param) + { + crawl_state.tests_selected = split_string(",", next_arg); + nextUsed = true; + } break; case CLO_MACRO: diff --git a/crawl-ref/source/luadgn.cc b/crawl-ref/source/luadgn.cc index c01bc869e8..2300ebefab 100644 --- a/crawl-ref/source/luadgn.cc +++ b/crawl-ref/source/luadgn.cc @@ -33,6 +33,7 @@ REVISION("$Rev$"); #include "misc.h" #include "mon-util.h" #include "monplace.h" +#include "monstuff.h" #include "place.h" #include "spells3.h" #include "spl-util.h" @@ -2985,6 +2986,13 @@ LUAFN(dgn_dbg_test_explore) return (0); } +LUAFN(dgn_dismiss_monsters) +{ + PLUARET(number, + dismiss_monsters(lua_gettop(ls) == 0 ? "" : + luaL_checkstring(ls, 1))); +} + LUAFN(dgn_cell_see_cell) { COORDS(p, 1, 2); @@ -3000,6 +3008,8 @@ static const struct luaL_reg dgn_lib[] = { "dbg_dump_map", dgn_dbg_dump_map }, { "dbg_test_explore", dgn_dbg_test_explore }, + { "dismiss_monsters", dgn_dismiss_monsters }, + { "default_depth", dgn_default_depth }, { "name", dgn_name }, { "depth", dgn_depth }, diff --git a/crawl-ref/source/monstuff.cc b/crawl-ref/source/monstuff.cc index 4a90029b39..a63b23d233 100644 --- a/crawl-ref/source/monstuff.cc +++ b/crawl-ref/source/monstuff.cc @@ -9775,3 +9775,52 @@ bool shift_monster(monsters *mon, coord_def p) return (count > 0); } + +// Make all of the monster's original equipment disappear, unless it's a fixed +// artefact or unrand artefact. +static void _vanish_orig_eq(monsters* mons) +{ + for (int i = 0; i < NUM_MONSTER_SLOTS; ++i) + { + if (mons->inv[i] == NON_ITEM) + continue; + + item_def &item(mitm[mons->inv[i]]); + + if (!is_valid_item(item)) + continue; + + if (item.orig_place != 0 || item.orig_monnum != 0 + || !item.inscription.empty() + || is_unrandom_artefact(item) + || (item.flags & (ISFLAG_DROPPED | ISFLAG_THROWN | ISFLAG_NOTED_GET + | ISFLAG_BEEN_IN_INV) ) ) + { + continue; + } + item.flags |= ISFLAG_SUMMONED; + } +} + +int dismiss_monsters(std::string pattern) { + // Make all of the monsters' original equipment disappear unless "keepitem" + // is found in the regex (except for fixed arts and unrand arts). + const bool keep_item = strip_tag(pattern, "keepitem"); + + // Dismiss by regex + text_pattern tpat(pattern); + int ndismissed = 0; + for (int mon = 0; mon < MAX_MONSTERS; mon++) + { + monsters *monster = &menv[mon]; + if (monster->alive() && + (tpat.empty() || tpat.matches(monster->name(DESC_PLAIN, true)))) + { + if (!keep_item) + _vanish_orig_eq(monster); + monster_die(monster, KILL_DISMISSED, NON_MONSTER, false, true); + ++ndismissed; + } + } + return (ndismissed); +} diff --git a/crawl-ref/source/monstuff.h b/crawl-ref/source/monstuff.h index b4995ab97e..5189e806f7 100644 --- a/crawl-ref/source/monstuff.h +++ b/crawl-ref/source/monstuff.h @@ -109,6 +109,7 @@ void mons_check_pool(monsters *monster, const coord_def &oldpos, * *********************************************************************** */ void monster_cleanup(monsters *monster); +int dismiss_monsters(std::string pattern); void behaviour_event(monsters *mon, mon_event_type event_type, int src = MHITNOT, coord_def src_pos = coord_def(), diff --git a/crawl-ref/source/state.h b/crawl-ref/source/state.h index 00332582fb..e1d887f966 100644 --- a/crawl-ref/source/state.h +++ b/crawl-ref/source/state.h @@ -58,6 +58,7 @@ struct game_state // suspended. bool test; // Set if we want to run self-tests and exit. + std::vector tests_selected; // Tests to be run. bool unicode_ok; // Is unicode support available? diff --git a/crawl-ref/source/test/monplace.lua b/crawl-ref/source/test/monplace.lua new file mode 100644 index 0000000000..982d5b22ae --- /dev/null +++ b/crawl-ref/source/test/monplace.lua @@ -0,0 +1,24 @@ +-- Monster placement tests. + +local place = dgn.point(20, 20) + +local function place_monster_on(x, y, monster, feature) + dgn.grid(place.x, place.y, feature) + return dgn.create_monster(x, y, monster) +end + +local function assert_place_monster_on(monster, feature) + dgn.dismiss_monsters() + crawl.mpr("Placing " .. monster .. " on " .. feature) + assert(place_monster_on(place.x, place.y, monster, feature), + "Could not place monster " .. monster .. " on " .. feature) +end + +assert_place_monster_on("quokka", "floor") +assert_place_monster_on("necrophage", "altar_zin") +assert_place_monster_on("rat", "shallow water") + +-- [ds] One wonders why due has this morbid fetish involving flying +-- skulls and lava... +assert_place_monster_on("flying skull", "lava") +assert_place_monster_on("rock worm", "rock_wall") -- cgit v1.2.3-54-g00ecf