summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dat/clua/macro.lua
diff options
context:
space:
mode:
authordshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2007-06-22 12:26:53 +0000
committerdshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2007-06-22 12:26:53 +0000
commitabb8fd86e78c57461f6729faafec5ea35ccd18a9 (patch)
treec262a150967152852cb015ddcd12a08530aedf7f /crawl-ref/source/dat/clua/macro.lua
parent609ccd171cbe8473c0a03c47392a12e72de2baaa (diff)
downloadcrawl-ref-abb8fd86e78c57461f6729faafec5ea35ccd18a9.tar.gz
crawl-ref-abb8fd86e78c57461f6729faafec5ea35ccd18a9.zip
Pull c_macro out of the code and into clua/macro.lua.
User-script dofile and loadfile check for "clua" anywhere in the file name and refuse to load the file in that case; only core Crawl code can load Lua files from clua/*. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1622 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/dat/clua/macro.lua')
-rw-r--r--crawl-ref/source/dat/clua/macro.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/crawl-ref/source/dat/clua/macro.lua b/crawl-ref/source/dat/clua/macro.lua
new file mode 100644
index 0000000000..b624ed0292
--- /dev/null
+++ b/crawl-ref/source/dat/clua/macro.lua
@@ -0,0 +1,39 @@
+------------------------------------------------------------------
+-- macro.lua:
+-- Crawl macro framework for Lua.
+--
+-- Macros are called as Lua coroutines. If the macro yields false, the
+-- coroutine is discarded (assuming an error). If the macro yields
+-- true, Crawl will start a macro delay for the macro and call the
+-- coroutine again next turn. If the macro just returns, the coroutine
+-- is assumed to be done.
+--
+-- Why coroutines: Macros may need to perform actions that take
+-- multiple turns, which requires control to return to Crawl to
+-- perform world updates between actions. Coroutines are the simplest
+-- way to pass control back and forth without losing the macro's
+-- state.
+------------------------------------------------------------------
+function c_macro(fn)
+ if fn == nil then
+ if c_macro_coroutine ~= nil then
+ local coret, mret
+ coret, mret = coroutine.resume(c_macro_coroutine)
+ if not coret or not mret then
+ c_macro_coroutine = nil
+ c_macro_name = nil
+ end
+ if not coret and mret then
+ error(mret)
+ end
+ return (coret and mret)
+ end
+ return false
+ end
+ if _G[fn] == nil or type(_G[fn]) ~= 'function' then
+ return false
+ end
+ c_macro_name = fn
+ c_macro_coroutine = coroutine.create(_G[fn])
+ return c_macro()
+end \ No newline at end of file