From 7a66364b0a9a5ee83119698e5d84b8293d3b3b62 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Wed, 12 Nov 2008 22:47:16 +0000 Subject: Added crawl.process_keys to user Lua API to allow a Lua macro to ask Crawl to process keystrokes immediately, i.e., not wait until the macro has finished running. Also removed player EV penalty during run/rest/travel. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@7443 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/acr.cc | 4 +++- crawl-ref/source/clua.cc | 20 ++++++++++++++++++++ crawl-ref/source/dat/clua/userbase.lua | 12 ++++++++++++ crawl-ref/source/delay.cc | 15 +++++++++++++-- crawl-ref/source/delay.h | 1 + crawl-ref/source/enum.h | 4 ++++ crawl-ref/source/player.cc | 3 ++- 7 files changed, 55 insertions(+), 4 deletions(-) diff --git a/crawl-ref/source/acr.cc b/crawl-ref/source/acr.cc index 7f3502d00e..166c029f5c 100644 --- a/crawl-ref/source/acr.cc +++ b/crawl-ref/source/acr.cc @@ -1502,7 +1502,7 @@ static void _input() _center_cursor(); - if (you_are_delayed()) + if (you_are_delayed() && current_delay_action() != DELAY_MACRO_PROCESS_KEY) { if (you.time_taken) _world_reacts(); @@ -1534,6 +1534,8 @@ static void _input() cursor_control con(true); #endif + clear_macro_process_key_delay(); + crawl_state.waiting_for_command = true; c_input_reset(true); diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index 3edabd36f0..477d772a40 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -1810,6 +1810,25 @@ static int crawl_sendkeys(lua_State *ls) return (0); } +// Tell Crawl to process one command. +static int crawl_process_command(lua_State *ls) +{ + const bool will_process = + current_delay_action() == DELAY_MACRO || !you_are_delayed(); + + if (will_process) + { + // This should only be called from a macro delay, but run_macro + // may not have started the macro delay; do so now. + if (!you_are_delayed()) + start_delay(DELAY_MACRO, 1); + start_delay(DELAY_MACRO_PROCESS_KEY, 1); + } + + lua_pushboolean(ls, will_process); + return (1); +} + static int crawl_playsound(lua_State *ls) { const char *sf = luaL_checkstring(ls, 1); @@ -2137,6 +2156,7 @@ static const struct luaL_reg crawl_lib[] = { "kbhit", crawl_kbhit }, { "flush_input", crawl_flush_input }, { "sendkeys", crawl_sendkeys }, + { "process_command", crawl_process_command }, { "playsound", crawl_playsound }, { "runmacro", crawl_runmacro }, { "bindkey", crawl_bindkey }, diff --git a/crawl-ref/source/dat/clua/userbase.lua b/crawl-ref/source/dat/clua/userbase.lua index fe2dcf4d8e..9183a0e380 100644 --- a/crawl-ref/source/dat/clua/userbase.lua +++ b/crawl-ref/source/dat/clua/userbase.lua @@ -112,3 +112,15 @@ end function add_no_autopickup_func(func) table.insert(chk_deny_autopickup, func) end + +-- Sends the characters in keys as keyboard input to crawl, then requests +-- that Crawl process one command. +function crawl.process_keys(keys, yield_value) + if yield_value == nil then + yield_value = true + end + + crawl.sendkeys(keys) + crawl.process_command() + coroutine.yield(yield_value) +end \ No newline at end of file diff --git a/crawl-ref/source/delay.cc b/crawl-ref/source/delay.cc index b8752d97d0..638897114b 100644 --- a/crawl-ref/source/delay.cc +++ b/crawl-ref/source/delay.cc @@ -631,6 +631,12 @@ static void _xom_check_corpse_waste() xom_is_stimulated(64 + (191 * food_need / 6000)); } +void clear_macro_process_key_delay() +{ + if (current_delay_action() == DELAY_MACRO_PROCESS_KEY) + _pop_delay(); +} + void handle_delay( void ) { if (!you_are_delayed()) @@ -638,6 +644,10 @@ void handle_delay( void ) delay_queue_item &delay = you.delay_queue.front(); + // If a Lua macro wanted Crawl to process a key normally, early exit. + if (delay.type == DELAY_MACRO_PROCESS_KEY) + return; + if (!delay.started) { switch (delay.type) @@ -1516,7 +1526,8 @@ void run_macro(const char *macroname) } else { - start_delay(DELAY_MACRO, 1); + if (!you_are_delayed()) + start_delay(DELAY_MACRO, 1); } #else stop_delay(); @@ -1854,7 +1865,7 @@ static const char *delay_names[] = "jewellery_on", "memorise", "butcher", "bottle_blood", "offer_corpse", "weapon_swap", "passwall", "drop_item", "multidrop", "ascending_stairs", "descending_stairs", "recite", "run", "rest", "travel", "macro", - "interruptible", "uninterruptible" + "macro_process_key", "interruptible", "uninterruptible" }; // Gets a delay given its name. diff --git a/crawl-ref/source/delay.h b/crawl-ref/source/delay.h index 5f65eabec8..dd47926e18 100644 --- a/crawl-ref/source/delay.h +++ b/crawl-ref/source/delay.h @@ -77,6 +77,7 @@ bool is_run_delay(int delay); bool is_being_butchered(const item_def &item, bool just_first = true); bool is_vampire_feeding( void ); void stop_butcher_delay(); +void clear_macro_process_key_delay(); const char *activity_interrupt_name(activity_interrupt_type ai); activity_interrupt_type get_activity_interrupt(const std::string &); diff --git a/crawl-ref/source/enum.h b/crawl-ref/source/enum.h index 6a2f7b7642..23d4e15c44 100644 --- a/crawl-ref/source/enum.h +++ b/crawl-ref/source/enum.h @@ -741,6 +741,10 @@ enum delay_type DELAY_MACRO, + // In a macro delay, a stacked delay to tell Crawl to read and act on + // one input command. + DELAY_MACRO_PROCESS_KEY, + DELAY_INTERRUPTIBLE, // simple interruptible delay DELAY_UNINTERRUPTIBLE, // simple uninterruptible delay diff --git a/crawl-ref/source/player.cc b/crawl-ref/source/player.cc index f796518b40..7a66a9538a 100644 --- a/crawl-ref/source/player.cc +++ b/crawl-ref/source/player.cc @@ -6416,7 +6416,8 @@ int player::melee_evasion(const actor *act) const { return (player_evasion() - ((!act || act->visible()) ? 0 : 10) - - (you_are_delayed()? 5 : 0)); + - (you_are_delayed() + && !is_run_delay(current_delay_action())? 5 : 0)); } void player::heal(int amount, bool max_too) -- cgit v1.2.3-54-g00ecf