File: macro.lua

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (44 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (4)
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
------------------------------------------------------------------
-- @module Globals

--- 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.
--
-- This is the internal crawl hook for executing a macro. If you would like to
-- start one from clua use @{crawl.runmacro}.
-- @param fn macro name
-- @local
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
      error("Unknown function in macro: '" .. fn .. "'")
      return false
   end
   c_macro_name = fn
   c_macro_coroutine = coroutine.create(_G[fn])
   return c_macro()
end