--- a/hedgewars/LuaPas.pas Sun Mar 21 22:26:12 2010 +0000
+++ b/hedgewars/LuaPas.pas Mon Mar 22 11:52:16 2010 +0000
@@ -1,1047 +1,1047 @@
-unit LuaPas;
-
-(*
- * A complete Pascal wrapper for Lua 5.1 DLL module.
- *
- * Created by Geo Massar, 2006
- * Distributed as free/open source.
- *)
-
-interface
-
-{.$DEFINE LUA_GETHOOK}
-{$INCLUDE "config.inc"}
-
-type
- size_t = type Cardinal;
- Psize_t = ^size_t;
- PPointer = ^Pointer;
-
- lua_State = record end;
- Plua_State = ^lua_State;
-
-const
- LuaLibName = cLuaLibrary;
-
-
-(*****************************************************************************)
-(* luaconfig.h *)
-(*****************************************************************************)
-
-(*
-** $Id: luaconf.h,v 1.81 2006/02/10 17:44:06 roberto Exp $
-** Configuration file for Lua
-** See Copyright Notice in lua.h
-*)
-
-(*
-** {==================================================================
-@@ LUA_NUMBER is the type of numbers in Lua.
-** CHANGE the following definitions only if you want to build Lua
-** with a number type different from double. You may also need to
-** change lua_number2int & lua_number2integer.
-** ===================================================================
-*)
-type
- LUA_NUMBER_ = type Double; // ending underscore is needed in Pascal
- LUA_INTEGER_ = type LongInt;
-
-(*
-@@ LUA_IDSIZE gives the maximum size for the description of the source
-@* of a function in debug information.
-** CHANGE it if you want a different size.
-*)
-const
- LUA_IDSIZE = 60;
-
-(*
-@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
-*)
-const
- LUAL_BUFFERSIZE = 1024;
-
-(*
-@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
-@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
-** CHANGE them if you want different prompts. (You can also change the
-** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
-*)
-const
- LUA_PROMPT = '> ';
- LUA_PROMPT2 = '>> ';
-
-(*
-@@ lua_readline defines how to show a prompt and then read a line from
-@* the standard input.
-@@ lua_saveline defines how to "save" a read line in a "history".
-@@ lua_freeline defines how to free a line read by lua_readline.
-** CHANGE them if you want to improve this functionality (e.g., by using
-** GNU readline and history facilities).
-*)
-function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
-procedure lua_saveline(L : Plua_State; idx : LongInt);
-procedure lua_freeline(L : Plua_State; b : PChar);
-
-(*
-@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
-@* is, whether we're running lua interactively).
-** CHANGE it if you have a better definition for non-POSIX/non-Windows
-** systems.
-*/
-#include <io.h>
-#include <stdio.h>
-#define lua_stdin_is_tty() _isatty(_fileno(stdin))
-*)
-const
- lua_stdin_is_tty = TRUE;
-
-(*****************************************************************************)
-(* lua.h *)
-(*****************************************************************************)
-
-(*
-** $Id: lua.h,v 1.216 2006/01/10 12:50:13 roberto Exp $
-** Lua - An Extensible Extension Language
-** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
-** See Copyright Notice at the end of this file
-*)
-
-const
- LUA_VERSION = 'Lua 5.1';
- LUA_VERSION_NUM = 501;
- LUA_COPYRIGHT = 'Copyright (C) 1994-2006 Tecgraf, PUC-Rio';
- LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
-
- (* mark for precompiled code (`<esc>Lua') *)
- LUA_SIGNATURE = #27'Lua';
-
- (* option for multiple returns in `lua_pcall' and `lua_call' *)
- LUA_MULTRET = -1;
-
- (*
- ** pseudo-indices
- *)
- LUA_REGISTRYINDEX = -10000;
- LUA_ENVIRONINDEX = -10001;
- LUA_GLOBALSINDEX = -10002;
-
-function lua_upvalueindex(idx : LongInt) : LongInt; // a marco
-
-const
- (* thread status; 0 is OK *)
- LUA_YIELD_ = 1; // Note: the ending underscore is needed in Pascal
- LUA_ERRRUN = 2;
- LUA_ERRSYNTAX = 3;
- LUA_ERRMEM = 4;
- LUA_ERRERR = 5;
-
-type
- lua_CFunction = function(L : Plua_State) : LongInt; cdecl;
-
- (*
- ** functions that read/write blocks when loading/dumping Lua chunks
- *)
- lua_Reader = function (L : Plua_State; ud : Pointer;
- sz : Psize_t) : PChar; cdecl;
- lua_Writer = function (L : Plua_State; const p : Pointer; sz : size_t;
- ud : Pointer) : LongInt; cdecl;
-
- (*
- ** prototype for memory-allocation functions
- *)
- lua_Alloc = function (ud, ptr : Pointer;
- osize, nsize : size_t) : Pointer; cdecl;
-
-const
- (*
- ** basic types
- *)
- LUA_TNONE = -1;
-
- LUA_TNIL = 0;
- LUA_TBOOLEAN = 1;
- LUA_TLIGHTUSERDATA = 2;
- LUA_TNUMBER = 3;
- LUA_TSTRING = 4;
- LUA_TTABLE = 5;
- LUA_TFUNCTION = 6;
- LUA_TUSERDATA = 7;
- LUA_TTHREAD = 8;
-
- (* minimum Lua stack available to a C function *)
- LUA_MINSTACK = 20;
-
-type
- (* type of numbers in Lua *)
- lua_Number = LUA_NUMBER_;
-
- (* type for integer functions *)
- lua_Integer = LUA_INTEGER_;
-
-(*
-** state manipulation
-*)
-function lua_newstate(f : lua_Alloc; ud : Pointer) : Plua_State;
- cdecl; external LuaLibName;
-procedure lua_close(L: Plua_State);
- cdecl; external LuaLibName;
-function lua_newthread(L : Plua_State) : Plua_State;
- cdecl; external LuaLibName;
-
-function lua_atpanic(L : Plua_State; panicf : lua_CFunction) : lua_CFunction;
- cdecl; external LuaLibName;
-
-
-(*
-** basic stack manipulation
-*)
-function lua_gettop(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-procedure lua_settop(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_pushvalue(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_remove(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_insert(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_replace(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-function lua_checkstack(L : Plua_State; sz : LongInt) : LongBool;
- cdecl; external LuaLibName;
-
-procedure lua_xmove(src, dest : Plua_State; n : LongInt);
- cdecl; external LuaLibName;
-
-
-(*
-** access functions (stack -> C)
-*)
-function lua_isnumber(L : Plua_State; idx : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_isstring(L : Plua_State; idx : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_iscfunction(L : Plua_State; idx : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_isuserdata(L : Plua_State; idx : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_type(L : Plua_State; idx : LongInt) : LongInt;
- cdecl; external LuaLibName;
-function lua_typename(L : Plua_State; tp : LongInt) : PChar;
- cdecl; external LuaLibName;
-
-function lua_equal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_rawequal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_lessthan(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
- cdecl; external LuaLibName;
-
-function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
- cdecl; external LuaLibName;
-function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
- cdecl; external LuaLibName;
-function lua_toboolean(L : Plua_State; idx : LongInt) : LongBool;
- cdecl; external LuaLibName;
-function lua_tolstring(L : Plua_State; idx : LongInt;
- len : Psize_t) : PChar;
- cdecl; external LuaLibName;
-function lua_objlen(L : Plua_State; idx : LongInt) : size_t;
- cdecl; external LuaLibName;
-function lua_tocfunction(L : Plua_State; idx : LongInt) : lua_CFunction;
- cdecl; external LuaLibName;
-function lua_touserdata(L : Plua_State; idx : LongInt) : Pointer;
- cdecl; external LuaLibName;
-function lua_tothread(L : Plua_State; idx : LongInt) : Plua_State;
- cdecl; external LuaLibName;
-function lua_topointer(L : Plua_State; idx : LongInt) : Pointer;
- cdecl; external LuaLibName;
-
-
-(*
-** push functions (C -> stack)
-*)
-procedure lua_pushnil(L : Plua_State);
- cdecl; external LuaLibName;
-procedure lua_pushnumber(L : Plua_State; n : lua_Number);
- cdecl; external LuaLibName;
-procedure lua_pushinteger(L : Plua_State; n : lua_Integer);
- cdecl; external LuaLibName;
-procedure lua_pushlstring(L : Plua_State; const s : PChar; ls : size_t);
- cdecl; external LuaLibName;
-procedure lua_pushstring(L : Plua_State; const s : PChar);
- cdecl; external LuaLibName;
-function lua_pushvfstring(L : Plua_State;
- const fmt : PChar; argp : Pointer) : PChar;
- cdecl; external LuaLibName;
-function lua_pushfstring(L : Plua_State; const fmt : PChar) : PChar; varargs;
- cdecl; external LuaLibName;
-procedure lua_pushcclosure(L : Plua_State; fn : lua_CFunction; n : LongInt);
- cdecl; external LuaLibName;
-procedure lua_pushboolean(L : Plua_State; b : LongBool);
- cdecl; external LuaLibName;
-procedure lua_pushlightuserdata(L : Plua_State; p : Pointer);
- cdecl; external LuaLibName;
-function lua_pushthread(L : Plua_state) : Cardinal;
- cdecl; external LuaLibName;
-
-
-(*
-** get functions (Lua -> stack)
-*)
-procedure lua_gettable(L : Plua_State ; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_getfield(L : Plua_State; idx : LongInt; k : PChar);
- cdecl; external LuaLibName;
-procedure lua_rawget(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_rawgeti(L : Plua_State; idx, n : LongInt);
- cdecl; external LuaLibName;
-procedure lua_createtable(L : Plua_State; narr, nrec : LongInt);
- cdecl; external LuaLibName;
-function lua_newuserdata(L : Plua_State; sz : size_t) : Pointer;
- cdecl; external LuaLibName;
-function lua_getmetatable(L : Plua_State; objindex : LongInt) : LongBool;
- cdecl; external LuaLibName;
-procedure lua_getfenv(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-
-
-(*
-** set functions (stack -> Lua)
-*)
-procedure lua_settable(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_setfield(L : Plua_State; idx : LongInt; const k : PChar);
- cdecl; external LuaLibName;
-procedure lua_rawset(L : Plua_State; idx : LongInt);
- cdecl; external LuaLibName;
-procedure lua_rawseti(L : Plua_State; idx , n: LongInt);
- cdecl; external LuaLibName;
-function lua_setmetatable(L : Plua_State; objindex : LongInt): LongBool;
- cdecl; external LuaLibName;
-function lua_setfenv(L : Plua_State; idx : LongInt): LongBool;
- cdecl; external LuaLibName;
-
-(*
-** `load' and `call' functions (load and run Lua code)
-*)
-procedure lua_call(L : Plua_State; nargs, nresults : LongInt);
- cdecl; external LuaLibName;
-function lua_pcall(L : Plua_State;
- nargs, nresults, errfunc : LongInt) : LongInt;
- cdecl; external LuaLibName;
-function lua_cpcall(L : Plua_State;
- func : lua_CFunction; ud : Pointer) : LongInt;
- cdecl; external LuaLibName;
-function lua_load(L : Plua_State; reader : lua_Reader;
- dt : Pointer; const chunkname : PChar) : LongInt;
- cdecl; external LuaLibName;
-
-function lua_dump(L : Plua_State; writer : lua_Writer; data: Pointer) : LongInt;
- cdecl; external LuaLibName;
-
-
-(*
-** coroutine functions
-*)
-function lua_yield(L : Plua_State; nresults : LongInt) : LongInt;
- cdecl; external LuaLibName;
-function lua_resume(L : Plua_State; narg : LongInt) : LongInt;
- cdecl; external LuaLibName;
-function lua_status(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-(*
-** garbage-collection functions and options
-*)
-const
- LUA_GCSTOP = 0;
- LUA_GCRESTART = 1;
- LUA_GCCOLLECT = 2;
- LUA_GCCOUNT = 3;
- LUA_GCCOUNTB = 4;
- LUA_GCSTEP = 5;
- LUA_GCSETPAUSE = 6;
- LUA_GCSETSTEPMUL = 7;
-
-function lua_gc(L : Plua_State; what, data : LongInt) : LongInt;
- cdecl; external LuaLibName;
-
-(*
-** miscellaneous functions
-*)
-function lua_error(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function lua_next(L : Plua_State; idx : LongInt) : LongInt;
- cdecl; external LuaLibName;
-
-procedure lua_concat(L : Plua_State; n : LongInt);
- cdecl; external LuaLibName;
-
-function lua_getallocf(L : Plua_State; ud : PPointer) : lua_Alloc;
- cdecl; external LuaLibName;
-procedure lua_setallocf(L : Plua_State; f : lua_Alloc; ud : Pointer);
- cdecl; external LuaLibName;
-
-(*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*)
-procedure lua_pop(L : Plua_State; n : LongInt);
-
-procedure lua_newtable(L : Plua_State);
-
-procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
-
-procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
-
-function lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
-
-function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
-function lua_istable(L : Plua_State; n : LongInt) : Boolean;
-function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
-function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
-function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
-function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
-function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
-function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
-
-procedure lua_pushliteral(L : Plua_State; s : PChar);
-
-procedure lua_setglobal(L : Plua_State; s : PChar);
-procedure lua_getglobal(L : Plua_State; s : PChar);
-
-function lua_tostring(L : Plua_State; idx : LongInt) : PChar;
-
-
-(*
-** compatibility macros and functions
-*)
-function lua_open : Plua_State;
-
-procedure lua_getregistry(L : Plua_State);
-
-function lua_getgccount(L : Plua_State) : LongInt;
-
-type
- lua_Chuckreader = type lua_Reader;
- lua_Chuckwriter = type lua_Writer;
-
-(* ====================================================================== *)
-
-(*
-** {======================================================================
-** Debug API
-** =======================================================================
-*)
-
-(*
-** Event codes
-*)
-const
- LUA_HOOKCALL = 0;
- LUA_HOOKRET = 1;
- LUA_HOOKLINE = 2;
- LUA_HOOKCOUNT = 3;
- LUA_HOOKTAILRET = 4;
-
-
-(*
-** Event masks
-*)
- LUA_MASKCALL = 1 shl LUA_HOOKCALL;
- LUA_MASKRET = 1 shl LUA_HOOKRET;
- LUA_MASKLINE = 1 shl LUA_HOOKLINE;
- LUA_MASKCOUNT = 1 shl LUA_HOOKCOUNT;
-
-type
- lua_Debug = packed record
- event : LongInt;
- name : PChar; (* (n) *)
- namewhat : PChar; (* (n) `global', `local', `field', `method' *)
- what : PChar; (* (S) `Lua', `C', `main', `tail' *)
- source : PChar; (* (S) *)
- currentline : LongInt; (* (l) *)
- nups : LongInt; (* (u) number of upvalues *)
- linedefined : LongInt; (* (S) *)
- short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *)
- (* private part *)
- i_ci : LongInt; (* active function *)
- end;
- Plua_Debug = ^lua_Debug;
-
- (* Functions to be called by the debuger in specific events *)
- lua_Hook = procedure (L : Plua_State; ar : Plua_Debug); cdecl;
-
-
-function lua_getstack(L : Plua_State; level : LongInt;
- ar : Plua_Debug) : LongInt;
- cdecl; external LuaLibName;
-function lua_getinfo(L : Plua_State; const what : PChar;
- ar: Plua_Debug): LongInt;
- cdecl; external LuaLibName;
-function lua_getlocal(L : Plua_State;
- ar : Plua_Debug; n : LongInt) : PChar;
- cdecl; external LuaLibName;
-function lua_setlocal(L : Plua_State;
- ar : Plua_Debug; n : LongInt) : PChar;
- cdecl; external LuaLibName;
-function lua_getupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
- cdecl; external LuaLibName;
-function lua_setupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
- cdecl; external LuaLibName;
-
-function lua_sethook(L : Plua_State; func : lua_Hook;
- mask, count: LongInt): LongInt;
- cdecl; external LuaLibName;
-{$IFDEF LUA_GETHOOK}
-function lua_gethook(L : Plua_State) : lua_Hook;
- cdecl; external LuaLibName;
-{$ENDIF}
-
-function lua_gethookmask(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-function lua_gethookcount(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-
-(*****************************************************************************)
-(* lualib.h *)
-(*****************************************************************************)
-
-(*
-** $Id: lualib.h,v 1.36 2005/12/27 17:12:00 roberto Exp $
-** Lua standard libraries
-** See Copyright Notice at the end of this file
-*)
-
-const
- (* Key to file-handle type *)
- LUA_FILEHANDLE = 'FILE*';
-
- LUA_COLIBNAME = 'coroutine';
- LUA_TABLIBNAME = 'table';
- LUA_IOLIBNAME = 'io';
- LUA_OSLIBNAME = 'os';
- LUA_STRLIBNAME = 'string';
- LUA_MATHLIBNAME = 'math';
- LUA_DBLIBNAME = 'debug';
- LUA_LOADLIBNAME = 'package';
-
-function luaopen_base(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_table(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_io(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_os(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_string(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_math(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_debug(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-function luaopen_package(L : Plua_State) : LongInt;
- cdecl; external LuaLibName;
-
-procedure luaL_openlibs(L : Plua_State);
- cdecl; external LuaLibName;
-
-procedure lua_assert(x : Boolean); // a macro
-
-
-(*****************************************************************************)
-(* lauxlib.h *)
-(*****************************************************************************)
-
-(*
-** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp $
-** Auxiliary functions for building Lua libraries
-** See Copyright Notice at the end of this file.
-*)
-
-// not compatibility with the behavior of setn/getn in Lua 5.0
-function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
-procedure luaL_setn(L : Plua_State; i, j : LongInt);
-
-const
- LUA_ERRFILE = LUA_ERRERR + 1;
-
-type
- luaL_Reg = packed record
- name : PChar;
- func : lua_CFunction;
- end;
- PluaL_Reg = ^luaL_Reg;
-
-
-procedure luaL_openlib(L : Plua_State; const libname : PChar;
- const lr : PluaL_Reg; nup : LongInt);
- cdecl; external LuaLibName;
-procedure luaL_register(L : Plua_State; const libname : PChar;
- const lr : PluaL_Reg);
- cdecl; external LuaLibName;
-function luaL_getmetafield(L : Plua_State; obj : LongInt;
- const e : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_callmeta(L : Plua_State; obj : LongInt;
- const e : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_typerror(L : Plua_State; narg : LongInt;
- const tname : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_argerror(L : Plua_State; numarg : LongInt;
- const extramsg : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_checklstring(L : Plua_State; numArg : LongInt;
- ls : Psize_t) : PChar;
- cdecl; external LuaLibName;
-function luaL_optlstring(L : Plua_State; numArg : LongInt;
- const def: PChar; ls: Psize_t) : PChar;
- cdecl; external LuaLibName;
-function luaL_checknumber(L : Plua_State; numArg : LongInt) : lua_Number;
- cdecl; external LuaLibName;
-function luaL_optnumber(L : Plua_State; nArg : LongInt;
- def : lua_Number) : lua_Number;
- cdecl; external LuaLibName;
-
-function luaL_checkinteger(L : Plua_State; numArg : LongInt) : lua_Integer;
- cdecl; external LuaLibName;
-function luaL_optinteger(L : Plua_State; nArg : LongInt;
- def : lua_Integer) : lua_Integer;
- cdecl; external LuaLibName;
-
-procedure luaL_checkstack(L : Plua_State; sz : LongInt; const msg : PChar);
- cdecl; external LuaLibName;
-procedure luaL_checktype(L : Plua_State; narg, t : LongInt);
- cdecl; external LuaLibName;
-procedure luaL_checkany(L : Plua_State; narg : LongInt);
- cdecl; external LuaLibName;
-
-function luaL_newmetatable(L : Plua_State; const tname : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_checkudata(L : Plua_State; ud : LongInt;
- const tname : PChar) : Pointer;
- cdecl; external LuaLibName;
-
-procedure luaL_where(L : Plua_State; lvl : LongInt);
- cdecl; external LuaLibName;
-function luaL_error(L : Plua_State; const fmt : PChar) : LongInt; varargs;
- cdecl; external LuaLibName;
-
-function luaL_checkoption(L : Plua_State; narg : LongInt; const def : PChar;
- const lst : array of PChar) : LongInt;
- cdecl; external LuaLibName;
-
-function luaL_ref(L : Plua_State; t : LongInt) : LongInt;
- cdecl; external LuaLibName;
-procedure luaL_unref(L : Plua_State; t, ref : LongInt);
- cdecl; external LuaLibName;
-
-function luaL_loadfile(L : Plua_State; const filename : PChar) : LongInt;
- cdecl; external LuaLibName;
-function luaL_loadbuffer(L : Plua_State; const buff : PChar;
- sz : size_t; const name: PChar) : LongInt;
- cdecl; external LuaLibName;
-
-function luaL_loadstring(L : Plua_State; const s : Pchar) : LongInt;
- cdecl; external LuaLibName;
-
-function luaL_newstate : Plua_State;
- cdecl; external LuaLibName;
-
-function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar;
- cdecl; external LuaLibName;
-
-function luaL_findtable(L : Plua_State; idx : LongInt;
- const fname : PChar; szhint : LongInt) : PChar;
- cdecl; external LuaLibName;
-
-
-(*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*)
-
-function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt;
- extramsg : PChar): LongInt;
-function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
-function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
-function luaL_checkint(L : Plua_State; n : LongInt) : LongInt;
-function luaL_optint(L : Plua_State; n, d : LongInt): LongInt;
-function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
-function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt;
-
-function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
-
-function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
-
-function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
-
-procedure luaL_getmetatable(L : Plua_State; n : PChar);
-
-(* not implemented yet
-#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
-*)
-
-(*
-** {======================================================
-** Generic Buffer manipulation
-** =======================================================
-*)
-
-type
- luaL_Buffer = packed record
- p : PChar; (* current position in buffer *)
- lvl : LongInt; (* number of strings in the stack (level) *)
- L : Plua_State;
- buffer : array [0..LUAL_BUFFERSIZE-1] of Char;
- end;
- PluaL_Buffer = ^luaL_Buffer;
-
-procedure luaL_addchar(B : PluaL_Buffer; c : Char);
-
-(* compatibility only *)
-procedure luaL_putchar(B : PluaL_Buffer; c : Char);
-
-procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
-
-procedure luaL_buffinit(L : Plua_State; B : PluaL_Buffer);
- cdecl; external LuaLibName;
-function luaL_prepbuffer(B : PluaL_Buffer) : PChar;
- cdecl; external LuaLibName;
-procedure luaL_addlstring(B : PluaL_Buffer; const s : PChar; ls : size_t);
- cdecl; external LuaLibName;
-procedure luaL_addstring(B : PluaL_Buffer; const s : PChar);
- cdecl; external LuaLibName;
-procedure luaL_addvalue(B : PluaL_Buffer);
- cdecl; external LuaLibName;
-procedure luaL_pushresult(B : PluaL_Buffer);
- cdecl; external LuaLibName;
-
-(* ====================================================== *)
-
-
-(* compatibility with ref system *)
-
-(* pre-defined references *)
-const
- LUA_NOREF = -2;
- LUA_REFNIL = -1;
-
-function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
-
-procedure lua_unref(L : Plua_State; ref : LongInt);
-
-procedure lua_getref(L : Plua_State; ref : LongInt);
-
-
-(******************************************************************************)
-(******************************************************************************)
-(******************************************************************************)
-
-implementation
-
-uses
- SysUtils;
-
-(*****************************************************************************)
-(* luaconfig.h *)
-(*****************************************************************************)
-
-function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
-var
- s : AnsiString;
-begin
- Write(p); // show prompt
- ReadLn(s); // get line
- b := PChar(s); // and return it
- lua_readline := (b[0] <> #4); // test for ctrl-D
-end;
-
-procedure lua_saveline(L : Plua_State; idx : LongInt);
-begin
-end;
-
-procedure lua_freeline(L : Plua_State; b : PChar);
-begin
-end;
-
-
-(*****************************************************************************)
-(* lua.h *)
-(*****************************************************************************)
-
-function lua_upvalueindex(idx : LongInt) : LongInt;
-begin
- lua_upvalueindex := LUA_GLOBALSINDEX - idx;
-end;
-
-procedure lua_pop(L : Plua_State; n : LongInt);
-begin
- lua_settop(L, -n - 1);
-end;
-
-procedure lua_newtable(L : Plua_State);
-begin
- lua_createtable(L, 0, 0);
-end;
-
-procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
-begin
- lua_pushcfunction(L, f);
- lua_setglobal(L, n);
-end;
-
-procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
-begin
- lua_pushcclosure(L, f, 0);
-end;
-
-function lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
-begin
- lua_strlen := lua_objlen(L, idx);
-end;
-
-function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isfunction := lua_type(L, n) = LUA_TFUNCTION;
-end;
-
-function lua_istable(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_istable := lua_type(L, n) = LUA_TTABLE;
-end;
-
-function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_islightuserdata := lua_type(L, n) = LUA_TLIGHTUSERDATA;
-end;
-
-function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isnil := lua_type(L, n) = LUA_TNIL;
-end;
-
-function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isboolean := lua_type(L, n) = LUA_TBOOLEAN;
-end;
-
-function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isthread := lua_type(L, n) = LUA_TTHREAD;
-end;
-
-function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isnone := lua_type(L, n) = LUA_TNONE;
-end;
-
-function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
-begin
- lua_isnoneornil := lua_type(L, n) <= 0;
-end;
-
-procedure lua_pushliteral(L : Plua_State; s : PChar);
-begin
- lua_pushlstring(L, s, StrLen(s));
-end;
-
-procedure lua_setglobal(L : Plua_State; s : PChar);
-begin
- lua_setfield(L, LUA_GLOBALSINDEX, s);
-end;
-
-procedure lua_getglobal(L: Plua_State; s: PChar);
-begin
- lua_getfield(L, LUA_GLOBALSINDEX, s);
-end;
-
-function lua_tostring(L : Plua_State; idx : LongInt) : PChar;
-begin
- lua_tostring := lua_tolstring(L, idx, nil);
-end;
-
-function lua_open : Plua_State;
-begin
- lua_open := luaL_newstate;
-end;
-
-procedure lua_getregistry(L : Plua_State);
-begin
- lua_pushvalue(L, LUA_REGISTRYINDEX);
-end;
-
-function lua_getgccount(L : Plua_State) : LongInt;
-begin
- lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0);
-end;
-
-
-(*****************************************************************************)
-(* lualib.h *)
-(*****************************************************************************)
-
-procedure lua_assert(x : Boolean);
-begin
-end;
-
-
-(*****************************************************************************)
-(* lauxlib.h n *)
-(*****************************************************************************)
-
-function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
-begin
- luaL_getn := lua_objlen(L, idx);
-end;
-
-procedure luaL_setn(L : plua_State; i, j : LongInt);
-begin
- (* no op *)
-end;
-
-function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt;
- extramsg : PChar): LongInt;
-begin
- if not cond then
- luaL_argcheck := luaL_argerror(L, numarg, extramsg)
- else
- luaL_argcheck := 0;
-end;
-
-function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
-begin
- luaL_checkstring := luaL_checklstring(L, n, nil);
-end;
-
-function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
-begin
- luaL_optstring := luaL_optlstring(L, n, d, nil);
-end;
-
-function luaL_checkint(L : Plua_State; n : LongInt) : LongInt;
-begin
- luaL_checkint := luaL_checkinteger(L, n);
-end;
-
-function luaL_optint(L : Plua_State; n, d : LongInt): LongInt;
-begin
- luaL_optint := luaL_optinteger(L, n, d);
-end;
-
-function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
-begin
- luaL_checklong := luaL_checkinteger(L, n);
-end;
-
-function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt;
-begin
- luaL_optlong := luaL_optinteger(L, n, d);
-end;
-
-function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
-begin
- luaL_typename := lua_typename( L, lua_type(L, idx) );
-end;
-
-function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
-begin
- luaL_dofile := luaL_loadfile(L, fn);
- if luaL_dofile = 0 then
- luaL_dofile := lua_pcall(L, 0, 0, 0);
-end;
-
-function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
-begin
- luaL_dostring := luaL_loadstring(L, s);
- if luaL_dostring = 0 then
- luaL_dostring := lua_pcall(L, 0, 0, 0);
-end;
-
-procedure luaL_getmetatable(L : Plua_State; n : PChar);
-begin
- lua_getfield(L, LUA_REGISTRYINDEX, n);
-end;
-
-procedure luaL_addchar(B : PluaL_Buffer; c : Char);
-begin
- if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then
- luaL_prepbuffer(B);
- B^.p^ := c;
- Inc(B^.p);
-end;
-
-procedure luaL_putchar(B : PluaL_Buffer; c : Char);
-begin
- luaL_addchar(B, c);
-end;
-
-procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
-begin
- Inc(B^.p, n);
-end;
-
-function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
-begin
- if lock then
- lua_ref := luaL_ref(L, LUA_REGISTRYINDEX)
- else begin
- lua_pushstring(L, 'unlocked references are obsolete');
- lua_error(L);
- lua_ref := 0;
- end;
-end;
-
-procedure lua_unref(L : Plua_State; ref : LongInt);
-begin
- luaL_unref(L, LUA_REGISTRYINDEX, ref);
-end;
-
-procedure lua_getref(L : Plua_State; ref : LongInt);
-begin
- lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
-end;
-
-
-(******************************************************************************
-* Original copyright for the lua source and headers:
-* 1994-2004 Tecgraf, PUC-Rio.
-* www.lua.org.
-*
-*
-* Permission is hereby granted, free of charge, to any person obtaining
-* a copy of this software and associated documentation files (the
-* "Software"), to deal in the Software without restriction, including
-* without limitation the rights to use, copy, modify, merge, publish,
-* distribute, sublicense, and/or sell copies of the Software, and to
-* permit persons to whom the Software is furnished to do so, subject to
-* the following conditions:
-*
-* The above copyright notice and this permission notice shall be
-* included in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-******************************************************************************)
-
-end.
-
+unit LuaPas;
+
+(*
+ * A complete Pascal wrapper for Lua 5.1 DLL module.
+ *
+ * Created by Geo Massar, 2006
+ * Distributed as free/open source.
+ *)
+
+interface
+
+{.$DEFINE LUA_GETHOOK}
+{$INCLUDE "config.inc"}
+
+type
+ size_t = type Cardinal;
+ Psize_t = ^size_t;
+ PPointer = ^Pointer;
+
+ lua_State = record end;
+ Plua_State = ^lua_State;
+
+const
+ LuaLibName = cLuaLibrary;
+
+
+(*****************************************************************************)
+(* luaconfig.h *)
+(*****************************************************************************)
+
+(*
+** $Id: luaconf.h,v 1.81 2006/02/10 17:44:06 roberto Exp $
+** Configuration file for Lua
+** See Copyright Notice in lua.h
+*)
+
+(*
+** {==================================================================
+@@ LUA_NUMBER is the type of numbers in Lua.
+** CHANGE the following definitions only if you want to build Lua
+** with a number type different from double. You may also need to
+** change lua_number2int & lua_number2integer.
+** ===================================================================
+*)
+type
+ LUA_NUMBER_ = type Double; // ending underscore is needed in Pascal
+ LUA_INTEGER_ = type LongInt;
+
+(*
+@@ LUA_IDSIZE gives the maximum size for the description of the source
+@* of a function in debug information.
+** CHANGE it if you want a different size.
+*)
+const
+ LUA_IDSIZE = 60;
+
+(*
+@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
+*)
+const
+ LUAL_BUFFERSIZE = 1024;
+
+(*
+@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
+@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
+** CHANGE them if you want different prompts. (You can also change the
+** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
+*)
+const
+ LUA_PROMPT = '> ';
+ LUA_PROMPT2 = '>> ';
+
+(*
+@@ lua_readline defines how to show a prompt and then read a line from
+@* the standard input.
+@@ lua_saveline defines how to "save" a read line in a "history".
+@@ lua_freeline defines how to free a line read by lua_readline.
+** CHANGE them if you want to improve this functionality (e.g., by using
+** GNU readline and history facilities).
+*)
+function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
+procedure lua_saveline(L : Plua_State; idx : LongInt);
+procedure lua_freeline(L : Plua_State; b : PChar);
+
+(*
+@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
+@* is, whether we're running lua interactively).
+** CHANGE it if you have a better definition for non-POSIX/non-Windows
+** systems.
+*/
+#include <io.h>
+#include <stdio.h>
+#define lua_stdin_is_tty() _isatty(_fileno(stdin))
+*)
+const
+ lua_stdin_is_tty = TRUE;
+
+(*****************************************************************************)
+(* lua.h *)
+(*****************************************************************************)
+
+(*
+** $Id: lua.h,v 1.216 2006/01/10 12:50:13 roberto Exp $
+** Lua - An Extensible Extension Language
+** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
+** See Copyright Notice at the end of this file
+*)
+
+const
+ LUA_VERSION = 'Lua 5.1';
+ LUA_VERSION_NUM = 501;
+ LUA_COPYRIGHT = 'Copyright (C) 1994-2006 Tecgraf, PUC-Rio';
+ LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
+
+ (* mark for precompiled code (`<esc>Lua') *)
+ LUA_SIGNATURE = #27'Lua';
+
+ (* option for multiple returns in `lua_pcall' and `lua_call' *)
+ LUA_MULTRET = -1;
+
+ (*
+ ** pseudo-indices
+ *)
+ LUA_REGISTRYINDEX = -10000;
+ LUA_ENVIRONINDEX = -10001;
+ LUA_GLOBALSINDEX = -10002;
+
+function lua_upvalueindex(idx : LongInt) : LongInt; // a marco
+
+const
+ (* thread status; 0 is OK *)
+ LUA_YIELD_ = 1; // Note: the ending underscore is needed in Pascal
+ LUA_ERRRUN = 2;
+ LUA_ERRSYNTAX = 3;
+ LUA_ERRMEM = 4;
+ LUA_ERRERR = 5;
+
+type
+ lua_CFunction = function(L : Plua_State) : LongInt; cdecl;
+
+ (*
+ ** functions that read/write blocks when loading/dumping Lua chunks
+ *)
+ lua_Reader = function (L : Plua_State; ud : Pointer;
+ sz : Psize_t) : PChar; cdecl;
+ lua_Writer = function (L : Plua_State; const p : Pointer; sz : size_t;
+ ud : Pointer) : LongInt; cdecl;
+
+ (*
+ ** prototype for memory-allocation functions
+ *)
+ lua_Alloc = function (ud, ptr : Pointer;
+ osize, nsize : size_t) : Pointer; cdecl;
+
+const
+ (*
+ ** basic types
+ *)
+ LUA_TNONE = -1;
+
+ LUA_TNIL = 0;
+ LUA_TBOOLEAN = 1;
+ LUA_TLIGHTUSERDATA = 2;
+ LUA_TNUMBER = 3;
+ LUA_TSTRING = 4;
+ LUA_TTABLE = 5;
+ LUA_TFUNCTION = 6;
+ LUA_TUSERDATA = 7;
+ LUA_TTHREAD = 8;
+
+ (* minimum Lua stack available to a C function *)
+ LUA_MINSTACK = 20;
+
+type
+ (* type of numbers in Lua *)
+ lua_Number = LUA_NUMBER_;
+
+ (* type for integer functions *)
+ lua_Integer = LUA_INTEGER_;
+
+(*
+** state manipulation
+*)
+function lua_newstate(f : lua_Alloc; ud : Pointer) : Plua_State;
+ cdecl; external LuaLibName;
+procedure lua_close(L: Plua_State);
+ cdecl; external LuaLibName;
+function lua_newthread(L : Plua_State) : Plua_State;
+ cdecl; external LuaLibName;
+
+function lua_atpanic(L : Plua_State; panicf : lua_CFunction) : lua_CFunction;
+ cdecl; external LuaLibName;
+
+
+(*
+** basic stack manipulation
+*)
+function lua_gettop(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+procedure lua_settop(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_pushvalue(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_remove(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_insert(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_replace(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+function lua_checkstack(L : Plua_State; sz : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+
+procedure lua_xmove(src, dest : Plua_State; n : LongInt);
+ cdecl; external LuaLibName;
+
+
+(*
+** access functions (stack -> C)
+*)
+function lua_isnumber(L : Plua_State; idx : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_isstring(L : Plua_State; idx : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_iscfunction(L : Plua_State; idx : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_isuserdata(L : Plua_State; idx : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_type(L : Plua_State; idx : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+function lua_typename(L : Plua_State; tp : LongInt) : PChar;
+ cdecl; external LuaLibName;
+
+function lua_equal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_rawequal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_lessthan(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+
+function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
+ cdecl; external LuaLibName;
+function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
+ cdecl; external LuaLibName;
+function lua_toboolean(L : Plua_State; idx : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+function lua_tolstring(L : Plua_State; idx : LongInt;
+ len : Psize_t) : PChar;
+ cdecl; external LuaLibName;
+function lua_objlen(L : Plua_State; idx : LongInt) : size_t;
+ cdecl; external LuaLibName;
+function lua_tocfunction(L : Plua_State; idx : LongInt) : lua_CFunction;
+ cdecl; external LuaLibName;
+function lua_touserdata(L : Plua_State; idx : LongInt) : Pointer;
+ cdecl; external LuaLibName;
+function lua_tothread(L : Plua_State; idx : LongInt) : Plua_State;
+ cdecl; external LuaLibName;
+function lua_topointer(L : Plua_State; idx : LongInt) : Pointer;
+ cdecl; external LuaLibName;
+
+
+(*
+** push functions (C -> stack)
+*)
+procedure lua_pushnil(L : Plua_State);
+ cdecl; external LuaLibName;
+procedure lua_pushnumber(L : Plua_State; n : lua_Number);
+ cdecl; external LuaLibName;
+procedure lua_pushinteger(L : Plua_State; n : lua_Integer);
+ cdecl; external LuaLibName;
+procedure lua_pushlstring(L : Plua_State; const s : PChar; ls : size_t);
+ cdecl; external LuaLibName;
+procedure lua_pushstring(L : Plua_State; const s : PChar);
+ cdecl; external LuaLibName;
+function lua_pushvfstring(L : Plua_State;
+ const fmt : PChar; argp : Pointer) : PChar;
+ cdecl; external LuaLibName;
+function lua_pushfstring(L : Plua_State; const fmt : PChar) : PChar; varargs;
+ cdecl; external LuaLibName;
+procedure lua_pushcclosure(L : Plua_State; fn : lua_CFunction; n : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_pushboolean(L : Plua_State; b : LongBool);
+ cdecl; external LuaLibName;
+procedure lua_pushlightuserdata(L : Plua_State; p : Pointer);
+ cdecl; external LuaLibName;
+function lua_pushthread(L : Plua_state) : Cardinal;
+ cdecl; external LuaLibName;
+
+
+(*
+** get functions (Lua -> stack)
+*)
+procedure lua_gettable(L : Plua_State ; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_getfield(L : Plua_State; idx : LongInt; k : PChar);
+ cdecl; external LuaLibName;
+procedure lua_rawget(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_rawgeti(L : Plua_State; idx, n : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_createtable(L : Plua_State; narr, nrec : LongInt);
+ cdecl; external LuaLibName;
+function lua_newuserdata(L : Plua_State; sz : size_t) : Pointer;
+ cdecl; external LuaLibName;
+function lua_getmetatable(L : Plua_State; objindex : LongInt) : LongBool;
+ cdecl; external LuaLibName;
+procedure lua_getfenv(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+
+
+(*
+** set functions (stack -> Lua)
+*)
+procedure lua_settable(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_setfield(L : Plua_State; idx : LongInt; const k : PChar);
+ cdecl; external LuaLibName;
+procedure lua_rawset(L : Plua_State; idx : LongInt);
+ cdecl; external LuaLibName;
+procedure lua_rawseti(L : Plua_State; idx , n: LongInt);
+ cdecl; external LuaLibName;
+function lua_setmetatable(L : Plua_State; objindex : LongInt): LongBool;
+ cdecl; external LuaLibName;
+function lua_setfenv(L : Plua_State; idx : LongInt): LongBool;
+ cdecl; external LuaLibName;
+
+(*
+** `load' and `call' functions (load and run Lua code)
+*)
+procedure lua_call(L : Plua_State; nargs, nresults : LongInt);
+ cdecl; external LuaLibName;
+function lua_pcall(L : Plua_State;
+ nargs, nresults, errfunc : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+function lua_cpcall(L : Plua_State;
+ func : lua_CFunction; ud : Pointer) : LongInt;
+ cdecl; external LuaLibName;
+function lua_load(L : Plua_State; reader : lua_Reader;
+ dt : Pointer; const chunkname : PChar) : LongInt;
+ cdecl; external LuaLibName;
+
+function lua_dump(L : Plua_State; writer : lua_Writer; data: Pointer) : LongInt;
+ cdecl; external LuaLibName;
+
+
+(*
+** coroutine functions
+*)
+function lua_yield(L : Plua_State; nresults : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+function lua_resume(L : Plua_State; narg : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+function lua_status(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+(*
+** garbage-collection functions and options
+*)
+const
+ LUA_GCSTOP = 0;
+ LUA_GCRESTART = 1;
+ LUA_GCCOLLECT = 2;
+ LUA_GCCOUNT = 3;
+ LUA_GCCOUNTB = 4;
+ LUA_GCSTEP = 5;
+ LUA_GCSETPAUSE = 6;
+ LUA_GCSETSTEPMUL = 7;
+
+function lua_gc(L : Plua_State; what, data : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+
+(*
+** miscellaneous functions
+*)
+function lua_error(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function lua_next(L : Plua_State; idx : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+
+procedure lua_concat(L : Plua_State; n : LongInt);
+ cdecl; external LuaLibName;
+
+function lua_getallocf(L : Plua_State; ud : PPointer) : lua_Alloc;
+ cdecl; external LuaLibName;
+procedure lua_setallocf(L : Plua_State; f : lua_Alloc; ud : Pointer);
+ cdecl; external LuaLibName;
+
+(*
+** ===============================================================
+** some useful macros
+** ===============================================================
+*)
+procedure lua_pop(L : Plua_State; n : LongInt);
+
+procedure lua_newtable(L : Plua_State);
+
+procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
+
+procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
+
+function lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
+
+function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
+function lua_istable(L : Plua_State; n : LongInt) : Boolean;
+function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
+function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
+function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
+function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
+function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
+function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
+
+procedure lua_pushliteral(L : Plua_State; s : PChar);
+
+procedure lua_setglobal(L : Plua_State; s : PChar);
+procedure lua_getglobal(L : Plua_State; s : PChar);
+
+function lua_tostring(L : Plua_State; idx : LongInt) : PChar;
+
+
+(*
+** compatibility macros and functions
+*)
+function lua_open : Plua_State;
+
+procedure lua_getregistry(L : Plua_State);
+
+function lua_getgccount(L : Plua_State) : LongInt;
+
+type
+ lua_Chuckreader = type lua_Reader;
+ lua_Chuckwriter = type lua_Writer;
+
+(* ====================================================================== *)
+
+(*
+** {======================================================================
+** Debug API
+** =======================================================================
+*)
+
+(*
+** Event codes
+*)
+const
+ LUA_HOOKCALL = 0;
+ LUA_HOOKRET = 1;
+ LUA_HOOKLINE = 2;
+ LUA_HOOKCOUNT = 3;
+ LUA_HOOKTAILRET = 4;
+
+
+(*
+** Event masks
+*)
+ LUA_MASKCALL = 1 shl LUA_HOOKCALL;
+ LUA_MASKRET = 1 shl LUA_HOOKRET;
+ LUA_MASKLINE = 1 shl LUA_HOOKLINE;
+ LUA_MASKCOUNT = 1 shl LUA_HOOKCOUNT;
+
+type
+ lua_Debug = packed record
+ event : LongInt;
+ name : PChar; (* (n) *)
+ namewhat : PChar; (* (n) `global', `local', `field', `method' *)
+ what : PChar; (* (S) `Lua', `C', `main', `tail' *)
+ source : PChar; (* (S) *)
+ currentline : LongInt; (* (l) *)
+ nups : LongInt; (* (u) number of upvalues *)
+ linedefined : LongInt; (* (S) *)
+ short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *)
+ (* private part *)
+ i_ci : LongInt; (* active function *)
+ end;
+ Plua_Debug = ^lua_Debug;
+
+ (* Functions to be called by the debuger in specific events *)
+ lua_Hook = procedure (L : Plua_State; ar : Plua_Debug); cdecl;
+
+
+function lua_getstack(L : Plua_State; level : LongInt;
+ ar : Plua_Debug) : LongInt;
+ cdecl; external LuaLibName;
+function lua_getinfo(L : Plua_State; const what : PChar;
+ ar: Plua_Debug): LongInt;
+ cdecl; external LuaLibName;
+function lua_getlocal(L : Plua_State;
+ ar : Plua_Debug; n : LongInt) : PChar;
+ cdecl; external LuaLibName;
+function lua_setlocal(L : Plua_State;
+ ar : Plua_Debug; n : LongInt) : PChar;
+ cdecl; external LuaLibName;
+function lua_getupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
+ cdecl; external LuaLibName;
+function lua_setupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
+ cdecl; external LuaLibName;
+
+function lua_sethook(L : Plua_State; func : lua_Hook;
+ mask, count: LongInt): LongInt;
+ cdecl; external LuaLibName;
+{$IFDEF LUA_GETHOOK}
+function lua_gethook(L : Plua_State) : lua_Hook;
+ cdecl; external LuaLibName;
+{$ENDIF}
+
+function lua_gethookmask(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+function lua_gethookcount(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+
+(*****************************************************************************)
+(* lualib.h *)
+(*****************************************************************************)
+
+(*
+** $Id: lualib.h,v 1.36 2005/12/27 17:12:00 roberto Exp $
+** Lua standard libraries
+** See Copyright Notice at the end of this file
+*)
+
+const
+ (* Key to file-handle type *)
+ LUA_FILEHANDLE = 'FILE*';
+
+ LUA_COLIBNAME = 'coroutine';
+ LUA_TABLIBNAME = 'table';
+ LUA_IOLIBNAME = 'io';
+ LUA_OSLIBNAME = 'os';
+ LUA_STRLIBNAME = 'string';
+ LUA_MATHLIBNAME = 'math';
+ LUA_DBLIBNAME = 'debug';
+ LUA_LOADLIBNAME = 'package';
+
+function luaopen_base(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_table(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_io(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_os(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_string(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_math(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_debug(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaopen_package(L : Plua_State) : LongInt;
+ cdecl; external LuaLibName;
+
+procedure luaL_openlibs(L : Plua_State);
+ cdecl; external LuaLibName;
+
+procedure lua_assert(x : Boolean); // a macro
+
+
+(*****************************************************************************)
+(* lauxlib.h *)
+(*****************************************************************************)
+
+(*
+** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp $
+** Auxiliary functions for building Lua libraries
+** See Copyright Notice at the end of this file.
+*)
+
+// not compatibility with the behavior of setn/getn in Lua 5.0
+function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
+procedure luaL_setn(L : Plua_State; i, j : LongInt);
+
+const
+ LUA_ERRFILE = LUA_ERRERR + 1;
+
+type
+ luaL_Reg = packed record
+ name : PChar;
+ func : lua_CFunction;
+ end;
+ PluaL_Reg = ^luaL_Reg;
+
+
+procedure luaL_openlib(L : Plua_State; const libname : PChar;
+ const lr : PluaL_Reg; nup : LongInt);
+ cdecl; external LuaLibName;
+procedure luaL_register(L : Plua_State; const libname : PChar;
+ const lr : PluaL_Reg);
+ cdecl; external LuaLibName;
+function luaL_getmetafield(L : Plua_State; obj : LongInt;
+ const e : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_callmeta(L : Plua_State; obj : LongInt;
+ const e : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_typerror(L : Plua_State; narg : LongInt;
+ const tname : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_argerror(L : Plua_State; numarg : LongInt;
+ const extramsg : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_checklstring(L : Plua_State; numArg : LongInt;
+ ls : Psize_t) : PChar;
+ cdecl; external LuaLibName;
+function luaL_optlstring(L : Plua_State; numArg : LongInt;
+ const def: PChar; ls: Psize_t) : PChar;
+ cdecl; external LuaLibName;
+function luaL_checknumber(L : Plua_State; numArg : LongInt) : lua_Number;
+ cdecl; external LuaLibName;
+function luaL_optnumber(L : Plua_State; nArg : LongInt;
+ def : lua_Number) : lua_Number;
+ cdecl; external LuaLibName;
+
+function luaL_checkinteger(L : Plua_State; numArg : LongInt) : lua_Integer;
+ cdecl; external LuaLibName;
+function luaL_optinteger(L : Plua_State; nArg : LongInt;
+ def : lua_Integer) : lua_Integer;
+ cdecl; external LuaLibName;
+
+procedure luaL_checkstack(L : Plua_State; sz : LongInt; const msg : PChar);
+ cdecl; external LuaLibName;
+procedure luaL_checktype(L : Plua_State; narg, t : LongInt);
+ cdecl; external LuaLibName;
+procedure luaL_checkany(L : Plua_State; narg : LongInt);
+ cdecl; external LuaLibName;
+
+function luaL_newmetatable(L : Plua_State; const tname : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_checkudata(L : Plua_State; ud : LongInt;
+ const tname : PChar) : Pointer;
+ cdecl; external LuaLibName;
+
+procedure luaL_where(L : Plua_State; lvl : LongInt);
+ cdecl; external LuaLibName;
+function luaL_error(L : Plua_State; const fmt : PChar) : LongInt; varargs;
+ cdecl; external LuaLibName;
+
+function luaL_checkoption(L : Plua_State; narg : LongInt; const def : PChar;
+ const lst : array of PChar) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaL_ref(L : Plua_State; t : LongInt) : LongInt;
+ cdecl; external LuaLibName;
+procedure luaL_unref(L : Plua_State; t, ref : LongInt);
+ cdecl; external LuaLibName;
+
+function luaL_loadfile(L : Plua_State; const filename : PChar) : LongInt;
+ cdecl; external LuaLibName;
+function luaL_loadbuffer(L : Plua_State; const buff : PChar;
+ sz : size_t; const name: PChar) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaL_loadstring(L : Plua_State; const s : Pchar) : LongInt;
+ cdecl; external LuaLibName;
+
+function luaL_newstate : Plua_State;
+ cdecl; external LuaLibName;
+
+function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar;
+ cdecl; external LuaLibName;
+
+function luaL_findtable(L : Plua_State; idx : LongInt;
+ const fname : PChar; szhint : LongInt) : PChar;
+ cdecl; external LuaLibName;
+
+
+(*
+** ===============================================================
+** some useful macros
+** ===============================================================
+*)
+
+function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt;
+ extramsg : PChar): LongInt;
+function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
+function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
+function luaL_checkint(L : Plua_State; n : LongInt) : LongInt;
+function luaL_optint(L : Plua_State; n, d : LongInt): LongInt;
+function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
+function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt;
+
+function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
+
+function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
+
+function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
+
+procedure luaL_getmetatable(L : Plua_State; n : PChar);
+
+(* not implemented yet
+#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
+*)
+
+(*
+** {======================================================
+** Generic Buffer manipulation
+** =======================================================
+*)
+
+type
+ luaL_Buffer = packed record
+ p : PChar; (* current position in buffer *)
+ lvl : LongInt; (* number of strings in the stack (level) *)
+ L : Plua_State;
+ buffer : array [0..LUAL_BUFFERSIZE-1] of Char;
+ end;
+ PluaL_Buffer = ^luaL_Buffer;
+
+procedure luaL_addchar(B : PluaL_Buffer; c : Char);
+
+(* compatibility only *)
+procedure luaL_putchar(B : PluaL_Buffer; c : Char);
+
+procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
+
+procedure luaL_buffinit(L : Plua_State; B : PluaL_Buffer);
+ cdecl; external LuaLibName;
+function luaL_prepbuffer(B : PluaL_Buffer) : PChar;
+ cdecl; external LuaLibName;
+procedure luaL_addlstring(B : PluaL_Buffer; const s : PChar; ls : size_t);
+ cdecl; external LuaLibName;
+procedure luaL_addstring(B : PluaL_Buffer; const s : PChar);
+ cdecl; external LuaLibName;
+procedure luaL_addvalue(B : PluaL_Buffer);
+ cdecl; external LuaLibName;
+procedure luaL_pushresult(B : PluaL_Buffer);
+ cdecl; external LuaLibName;
+
+(* ====================================================== *)
+
+
+(* compatibility with ref system *)
+
+(* pre-defined references *)
+const
+ LUA_NOREF = -2;
+ LUA_REFNIL = -1;
+
+function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
+
+procedure lua_unref(L : Plua_State; ref : LongInt);
+
+procedure lua_getref(L : Plua_State; ref : LongInt);
+
+
+(******************************************************************************)
+(******************************************************************************)
+(******************************************************************************)
+
+implementation
+
+uses
+ SysUtils;
+
+(*****************************************************************************)
+(* luaconfig.h *)
+(*****************************************************************************)
+
+function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean;
+var
+ s : AnsiString;
+begin
+ Write(p); // show prompt
+ ReadLn(s); // get line
+ b := PChar(s); // and return it
+ lua_readline := (b[0] <> #4); // test for ctrl-D
+end;
+
+procedure lua_saveline(L : Plua_State; idx : LongInt);
+begin
+end;
+
+procedure lua_freeline(L : Plua_State; b : PChar);
+begin
+end;
+
+
+(*****************************************************************************)
+(* lua.h *)
+(*****************************************************************************)
+
+function lua_upvalueindex(idx : LongInt) : LongInt;
+begin
+ lua_upvalueindex := LUA_GLOBALSINDEX - idx;
+end;
+
+procedure lua_pop(L : Plua_State; n : LongInt);
+begin
+ lua_settop(L, -n - 1);
+end;
+
+procedure lua_newtable(L : Plua_State);
+begin
+ lua_createtable(L, 0, 0);
+end;
+
+procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
+begin
+ lua_pushcfunction(L, f);
+ lua_setglobal(L, n);
+end;
+
+procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
+begin
+ lua_pushcclosure(L, f, 0);
+end;
+
+function lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
+begin
+ lua_strlen := lua_objlen(L, idx);
+end;
+
+function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isfunction := lua_type(L, n) = LUA_TFUNCTION;
+end;
+
+function lua_istable(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_istable := lua_type(L, n) = LUA_TTABLE;
+end;
+
+function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_islightuserdata := lua_type(L, n) = LUA_TLIGHTUSERDATA;
+end;
+
+function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isnil := lua_type(L, n) = LUA_TNIL;
+end;
+
+function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isboolean := lua_type(L, n) = LUA_TBOOLEAN;
+end;
+
+function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isthread := lua_type(L, n) = LUA_TTHREAD;
+end;
+
+function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isnone := lua_type(L, n) = LUA_TNONE;
+end;
+
+function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
+begin
+ lua_isnoneornil := lua_type(L, n) <= 0;
+end;
+
+procedure lua_pushliteral(L : Plua_State; s : PChar);
+begin
+ lua_pushlstring(L, s, StrLen(s));
+end;
+
+procedure lua_setglobal(L : Plua_State; s : PChar);
+begin
+ lua_setfield(L, LUA_GLOBALSINDEX, s);
+end;
+
+procedure lua_getglobal(L: Plua_State; s: PChar);
+begin
+ lua_getfield(L, LUA_GLOBALSINDEX, s);
+end;
+
+function lua_tostring(L : Plua_State; idx : LongInt) : PChar;
+begin
+ lua_tostring := lua_tolstring(L, idx, nil);
+end;
+
+function lua_open : Plua_State;
+begin
+ lua_open := luaL_newstate;
+end;
+
+procedure lua_getregistry(L : Plua_State);
+begin
+ lua_pushvalue(L, LUA_REGISTRYINDEX);
+end;
+
+function lua_getgccount(L : Plua_State) : LongInt;
+begin
+ lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0);
+end;
+
+
+(*****************************************************************************)
+(* lualib.h *)
+(*****************************************************************************)
+
+procedure lua_assert(x : Boolean);
+begin
+end;
+
+
+(*****************************************************************************)
+(* lauxlib.h n *)
+(*****************************************************************************)
+
+function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
+begin
+ luaL_getn := lua_objlen(L, idx);
+end;
+
+procedure luaL_setn(L : plua_State; i, j : LongInt);
+begin
+ (* no op *)
+end;
+
+function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt;
+ extramsg : PChar): LongInt;
+begin
+ if not cond then
+ luaL_argcheck := luaL_argerror(L, numarg, extramsg)
+ else
+ luaL_argcheck := 0;
+end;
+
+function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
+begin
+ luaL_checkstring := luaL_checklstring(L, n, nil);
+end;
+
+function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
+begin
+ luaL_optstring := luaL_optlstring(L, n, d, nil);
+end;
+
+function luaL_checkint(L : Plua_State; n : LongInt) : LongInt;
+begin
+ luaL_checkint := luaL_checkinteger(L, n);
+end;
+
+function luaL_optint(L : Plua_State; n, d : LongInt): LongInt;
+begin
+ luaL_optint := luaL_optinteger(L, n, d);
+end;
+
+function luaL_checklong(L : Plua_State; n : LongInt) : LongInt;
+begin
+ luaL_checklong := luaL_checkinteger(L, n);
+end;
+
+function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt;
+begin
+ luaL_optlong := luaL_optinteger(L, n, d);
+end;
+
+function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
+begin
+ luaL_typename := lua_typename( L, lua_type(L, idx) );
+end;
+
+function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
+begin
+ luaL_dofile := luaL_loadfile(L, fn);
+ if luaL_dofile = 0 then
+ luaL_dofile := lua_pcall(L, 0, 0, 0);
+end;
+
+function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
+begin
+ luaL_dostring := luaL_loadstring(L, s);
+ if luaL_dostring = 0 then
+ luaL_dostring := lua_pcall(L, 0, 0, 0);
+end;
+
+procedure luaL_getmetatable(L : Plua_State; n : PChar);
+begin
+ lua_getfield(L, LUA_REGISTRYINDEX, n);
+end;
+
+procedure luaL_addchar(B : PluaL_Buffer; c : Char);
+begin
+ if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then
+ luaL_prepbuffer(B);
+ B^.p^ := c;
+ Inc(B^.p);
+end;
+
+procedure luaL_putchar(B : PluaL_Buffer; c : Char);
+begin
+ luaL_addchar(B, c);
+end;
+
+procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
+begin
+ Inc(B^.p, n);
+end;
+
+function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
+begin
+ if lock then
+ lua_ref := luaL_ref(L, LUA_REGISTRYINDEX)
+ else begin
+ lua_pushstring(L, 'unlocked references are obsolete');
+ lua_error(L);
+ lua_ref := 0;
+ end;
+end;
+
+procedure lua_unref(L : Plua_State; ref : LongInt);
+begin
+ luaL_unref(L, LUA_REGISTRYINDEX, ref);
+end;
+
+procedure lua_getref(L : Plua_State; ref : LongInt);
+begin
+ lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
+end;
+
+
+(******************************************************************************
+* Original copyright for the lua source and headers:
+* 1994-2004 Tecgraf, PUC-Rio.
+* www.lua.org.
+*
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+******************************************************************************)
+
+end.
+
--- a/hedgewars/uScript.pas Sun Mar 21 22:26:12 2010 +0000
+++ b/hedgewars/uScript.pas Mon Mar 22 11:52:16 2010 +0000
@@ -1,857 +1,857 @@
-(*
- * Hedgewars, a free turn based strategy game
- * Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- *)
-
-{$INCLUDE "options.inc"}
-
-unit uScript;
-interface
-
-procedure ScriptPrintStack;
-procedure ScriptClearStack;
-
-procedure ScriptLoad(name : shortstring);
-procedure ScriptOnGameInit;
-
-procedure ScriptCall(fname : shortstring);
-function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
-function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
-function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
-function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
-function ScriptExists(fname : shortstring) : boolean;
-
-procedure initModule;
-procedure freeModule;
-
-implementation
-{$IFNDEF IPHONEOS}
-uses LuaPas in 'LuaPas.pas',
- uConsole,
- uMisc,
- uConsts,
- uGears,
- uFloat,
- uWorld,
- uAmmos,
- uSound,
- uTeams,
- uKeys,
- typinfo;
-
-var luaState : Plua_State;
- ScriptAmmoStore : shortstring;
- ScriptLoaded : boolean;
-
-procedure ScriptPrepareAmmoStore; forward;
-procedure ScriptApplyAmmoStore; forward;
-procedure ScriptSetAmmo(ammo : TAmmoType; count, propability, delay: Byte); forward;
-
-// wrapped calls //
-
-// functions called from lua:
-// function(L : Plua_State) : LongInt; Cdecl;
-// where L contains the state, returns the number of return values on the stack
-// call lua_gettop(L) to receive number of parameters passed
-
-function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) = 1 then
- begin
- WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
- end
- else
- WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
- lc_writelntoconsole:= 0;
-end;
-
-function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) = 1 then
- begin
- ParseCommand(lua_tostring(L ,1), true);
- end
- else
- WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
- lc_parsecommand:= 0;
-end;
-
-function lc_showmission(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) = 5 then
- begin
- ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
- end
- else
- WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
- lc_showmission:= 0;
-end;
-
-function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
-begin
- HideMission;
- lc_hidemission:= 0;
-end;
-
-function lc_addgear(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
- x, y, s, t: LongInt;
- dx, dy: hwFloat;
- gt: TGearType;
-begin
- if lua_gettop(L) <> 7 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- x:= lua_tointeger(L, 1);
- y:= lua_tointeger(L, 2);
- gt:= TGearType(lua_tointeger(L, 3));
- s:= lua_tointeger(L, 4);
- dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
- dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
- t:= lua_tointeger(L, 7);
-
- gear:= AddGear(x, y, gt, s, dx, dy, t);
- lua_pushnumber(L, gear^.uid)
- end;
- lc_addgear:= 1; // 1 return value
-end;
-
-function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- lua_pushinteger(L, ord(gear^.Kind))
- else
- lua_pushnil(L);
- end;
- lc_getgeartype:= 1
-end;
-
-function lc_gethogclan(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogClan!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
- begin
- lua_pushinteger(L, PHedgehog(gear^.Hedgehog)^.Team^.Clan^.ClanIndex)
- end
- else
- lua_pushnil(L);
- end;
- lc_gethogclan:= 1
-end;
-
-function lc_gethogname(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogName!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
- begin
- lua_pushstring(L, str2pchar(PHedgehog(gear^.Hedgehog)^.Name))
- end
- else
- lua_pushnil(L);
- end;
- lc_gethogname:= 1
-end;
-
-function lc_getx(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetX!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- lua_pushnumber(L, hwRound(gear^.X))
- else
- lua_pushnil(L);
- end;
- lc_getx:= 1
-end;
-
-function lc_gety(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetY!');
- lua_pushnil(L); // return value on stack (nil)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- lua_pushnumber(L, hwRound(gear^.Y))
- else
- lua_pushnil(L);
- end;
- lc_gety:= 1
-end;
-
-function lc_copypv(L : Plua_State) : LongInt; Cdecl;
-var gears, geard : PGear;
-begin
- if lua_gettop(L) <> 2 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to CopyPV!');
- end
- else
- begin
- gears:= GearByUID(lua_tointeger(L, 1));
- geard:= GearByUID(lua_tointeger(L, 2));
- if (gears <> nil) and (geard <> nil) then
- begin
- geard^.X:= gears^.X;
- geard^.Y:= gears^.Y;
- geard^.dX:= gears^.dX;
- geard^.dY:= gears^.dY;
- end
- end;
- lc_copypv:= 1
-end;
-
-function lc_copypv2(L : Plua_State) : LongInt; Cdecl;
-var gears, geard : PGear;
-begin
- if lua_gettop(L) <> 2 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to CopyPV2!');
- end
- else
- begin
- gears:= GearByUID(lua_tointeger(L, 1));
- geard:= GearByUID(lua_tointeger(L, 2));
- if (gears <> nil) and (geard <> nil) then
- begin
- geard^.X:= gears^.X;
- geard^.Y:= gears^.Y;
- geard^.dX:= gears^.dX * 2;
- geard^.dY:= gears^.dY * 2;
- end
- end;
- lc_copypv2:= 1
-end;
-
-function lc_followgear(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- WriteLnToConsole('LUA: Wrong number of parameters passed to FollowGear!')
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then FollowGear:= gear
- end;
- lc_followgear:= 0
-end;
-
-function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 2 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then gear^.Health:= lua_tointeger(L, 2)
- end;
- lc_sethealth:= 0
-end;
-
-function lc_setstate(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 2 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to SetState!');
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then gear^.State:= lua_tointeger(L, 2)
- end;
- lc_setstate:= 0
-end;
-
-function lc_getstate(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetState!');
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- lua_pushinteger(L, gear^.State)
- else
- lua_pushnil(L)
- end;
- lc_getstate:= 1
-end;
-
-function lc_settag(L : Plua_State) : LongInt; Cdecl;
-var gear : PGear;
-begin
- if lua_gettop(L) <> 2 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to SetTag!');
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then gear^.Tag:= lua_tointeger(L, 2)
- end;
- lc_settag:= 0
-end;
-
-function lc_endgame(L : Plua_State) : LongInt; Cdecl;
-begin
- GameState:= gsExit;
- lc_endgame:= 0
-end;
-
-function lc_findplace(L : Plua_State) : LongInt; Cdecl;
-var gear: PGear;
- fall: boolean;
- left, right: LongInt;
-begin
- if lua_gettop(L) <> 4 then
- WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- fall:= lua_toboolean(L, 2);
- left:= lua_tointeger(L, 3);
- right:= lua_tointeger(L, 4);
- if gear <> nil then
- FindPlace(gear, fall, left, right)
- end;
- lc_findplace:= 0
-end;
-
-function lc_playsound(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) <> 1 then
- WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
- else
- PlaySound(TSound(lua_tointeger(L, 1)));
- lc_playsound:= 0;
-end;
-
-function lc_addteam(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) <> 5 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!');
- //lua_pushnil(L)
- end
- else
- begin
- ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
- ParseCommand('grave ' + lua_tostring(L, 3), true);
- ParseCommand('fort ' + lua_tostring(L, 4), true);
- ParseCommand('voicepack ' + lua_tostring(L, 5), true);
- CurrentTeam^.Binds:= DefaultBinds
- // fails on x64
- //lua_pushinteger(L, LongInt(CurrentTeam));
- end;
- lc_addteam:= 0;//1;
-end;
-
-function lc_addhog(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) <> 4 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
- lua_pushnil(L)
- end
- else
- begin
- ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
- ParseCommand('hat ' + lua_tostring(L, 4), true);
- lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
- end;
- lc_addhog:= 1;
-end;
-
-function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
-var gear: PGear;
-begin
- if lua_gettop(L) <> 1 then
- begin
- WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
- lua_pushnil(L);
- lua_pushnil(L)
- end
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- begin
- lua_pushinteger(L, hwRound(gear^.X));
- lua_pushinteger(L, hwRound(gear^.Y))
- end
- end;
- lc_getgearposition:= 2;
-end;
-
-function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
-var gear: PGear;
- x, y: LongInt;
-begin
- if lua_gettop(L) <> 3 then
- WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
- else
- begin
- gear:= GearByUID(lua_tointeger(L, 1));
- if gear <> nil then
- begin
- x:= lua_tointeger(L, 2);
- y:= lua_tointeger(L, 3);
- gear^.X:= int2hwfloat(x);
- gear^.Y:= int2hwfloat(y);
- end
- end;
- lc_setgearposition:= 0
-end;
-
-function lc_setammo(L : Plua_State) : LongInt; Cdecl;
-begin
- if lua_gettop(L) <> 4 then
- WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
- else
- begin
- ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
- end;
- lc_setammo:= 0
-end;
-///////////////////
-
-procedure ScriptPrintStack;
-var n, i : LongInt;
-begin
- n:= lua_gettop(luaState);
- WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
- for i:= 1 to n do
- if not lua_isboolean(luaState, i) then
- WriteLnToConsole('LUA: ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
- else if lua_toboolean(luaState, i) then
- WriteLnToConsole('LUA: ' + inttostr(i) + ': true')
- else
- WriteLnToConsole('LUA: ' + inttostr(i) + ': false');
-end;
-
-procedure ScriptClearStack;
-begin
-lua_settop(luaState, 0)
-end;
-
-procedure ScriptSetNil(name : shortstring);
-begin
-lua_pushnil(luaState);
-lua_setglobal(luaState, Str2PChar(name));
-end;
-
-procedure ScriptSetInteger(name : shortstring; value : LongInt);
-begin
-lua_pushinteger(luaState, value);
-lua_setglobal(luaState, Str2PChar(name));
-end;
-
-procedure ScriptSetString(name : shortstring; value : shortstring);
-begin
-lua_pushstring(luaState, Str2PChar(value));
-lua_setglobal(luaState, Str2PChar(name));
-end;
-
-function ScriptGetInteger(name : shortstring) : LongInt;
-begin
-lua_getglobal(luaState, Str2PChar(name));
-ScriptGetInteger:= lua_tointeger(luaState, -1);
-lua_pop(luaState, 1);
-end;
-
-function ScriptGetString(name : shortstring) : shortstring;
-begin
-lua_getglobal(luaState, Str2PChar(name));
-ScriptGetString:= lua_tostring(luaState, -1);
-lua_pop(luaState, 1);
-end;
-
-procedure ScriptOnGameInit;
-var s, t : ansistring;
-begin
-// not required if there is no script to run
-if not ScriptLoaded then
- exit;
-
-// push game variables so they may be modified by the script
-ScriptSetInteger('GameFlags', GameFlags);
-ScriptSetString('Seed', cSeed);
-ScriptSetInteger('TurnTime', cHedgehogTurnTime);
-ScriptSetInteger('CaseFreq', cCaseFactor);
-ScriptSetInteger('LandAdds', cLandAdditions);
-ScriptSetInteger('Explosives', cExplosives);
-ScriptSetInteger('Delay', cInactDelay);
-ScriptSetString('Map', '');
-ScriptSetString('Theme', '');
-
-// import locale
-s:= cLocaleFName;
-SplitByChar(s, t, '.');
-ScriptSetString('L', s);
-
-ScriptCall('onGameInit');
-
-// pop game variables
-ParseCommand('seed ' + ScriptGetString('Seed'), true);
-ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
-ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
-ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
-ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
-ParseCommand('$explosives ' + ScriptGetString('Explosives'), true);
-ParseCommand('$delay ' + ScriptGetString('Delay'), true);
-if ScriptGetString('Map') <> '' then
- ParseCommand('map ' + ScriptGetString('Map'), true);
-if ScriptGetString('Theme') <> '' then
- ParseCommand('theme ' + ScriptGetString('Theme'), true);
-
-if ScriptExists('onAmmoStoreInit') then
- begin
- ScriptPrepareAmmoStore;
- ScriptCall('onAmmoStoreInit');
- ScriptApplyAmmoStore
- end;
-
-ScriptSetInteger('ClansCount', ClansCount)
-end;
-
-procedure ScriptLoad(name : shortstring);
-var ret : LongInt;
-begin
-ret:= luaL_loadfile(luaState, Str2PChar(name));
-if ret <> 0 then
- WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
-else
- begin
- WriteLnToConsole('LUA: ' + name + ' loaded');
- // call the script file
- lua_pcall(luaState, 0, 0, 0);
- ScriptLoaded:= true
- end
-end;
-
-procedure SetGlobals;
-begin
-ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
-if (CurrentHedgehog <> nil) and (CurrentHedgehog^.Gear <> nil) then
- ScriptSetInteger('CurrentHedgehog', CurrentHedgehog^.Gear^.UID)
-else
- ScriptSetNil('CurrentHedgehog');
-end;
-
-procedure GetGlobals;
-begin
-TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
-end;
-
-procedure ScriptCall(fname : shortstring);
-begin
-if not ScriptLoaded or not ScriptExists(fname) then
- exit;
-SetGlobals;
-lua_getglobal(luaState, Str2PChar(fname));
-if lua_pcall(luaState, 0, 0, 0) <> 0 then
- begin
- WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
- lua_pop(luaState, 1)
- end;
-GetGlobals;
-end;
-
-function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
-begin
-ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
-end;
-
-function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
-begin
-ScriptCall:= ScriptCall(fname, par1, par2, 0, 0)
-end;
-
-function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
-begin
-ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
-end;
-
-function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
-begin
-if not ScriptLoaded or not ScriptExists(fname) then
- exit;
-SetGlobals;
-lua_getglobal(luaState, Str2PChar(fname));
-lua_pushinteger(luaState, par1);
-lua_pushinteger(luaState, par2);
-lua_pushinteger(luaState, par3);
-lua_pushinteger(luaState, par4);
-ScriptCall:= 0;
-if lua_pcall(luaState, 4, 1, 0) <> 0 then
- begin
- WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
- lua_pop(luaState, 1)
- end
-else
- begin
- ScriptCall:= lua_tointeger(luaState, -1);
- lua_pop(luaState, 1)
- end;
-GetGlobals;
-end;
-
-function ScriptExists(fname : shortstring) : boolean;
-begin
-if not ScriptLoaded then
- begin
- ScriptExists:= false;
- exit
- end;
-lua_getglobal(luaState, Str2PChar(fname));
-ScriptExists:= not lua_isnoneornil(luaState, -1);
-lua_pop(luaState, -1)
-end;
-
-procedure ScriptPrepareAmmoStore;
-var i: ShortInt;
-begin
-// reset ammostore (quite unclean, but works?)
-uAmmos.freeModule;
-uAmmos.initModule;
-ScriptAmmoStore:= '';
-for i:=1 to ord(High(TAmmoType)) do
- ScriptAmmoStore:= ScriptAmmoStore + '0000';
-end;
-
-procedure ScriptSetAmmo(ammo : TAmmoType; count, propability, delay: Byte);
-begin
-if (ord(ammo) < 1) or (count > 9) or (count < 0) or (propability < 0) or (propability > 8) or (delay < 0) or (delay > 9)then
- exit;
-ScriptAmmoStore[ord(ammo)]:= inttostr(count)[1];
-ScriptAmmoStore[ord(ammo) + ord(high(TAmmoType))]:= inttostr(propability)[1];
-ScriptAmmoStore[ord(ammo) + 2 * ord(high(TAmmoType))]:= inttostr(delay)[1];
-end;
-
-procedure ScriptApplyAmmoStore;
-var i : LongInt;
-begin
-for i:= 0 to Pred(TeamsCount) do
- AddAmmoStore(ScriptAmmoStore);
-end;
-
-// small helper functions making registering enums a lot easier
-function str(const en : TGearType) : shortstring; overload;
-begin
-str:= GetEnumName(TypeInfo(TGearType), ord(en))
-end;
-
-function str(const en : TSound) : shortstring; overload;
-begin
-str:= GetEnumName(TypeInfo(TSound), ord(en))
-end;
-
-function str(const en : TAmmoType) : shortstring; overload;
-begin
-str:= GetEnumName(TypeInfo(TAmmoType), ord(en))
-end;
-///////////////////
-
-procedure initModule;
-var at : TGearType;
- am : TAmmoType;
- st : TSound;
-begin
-// initialize lua
-luaState:= lua_open;
-
-// open internal libraries
-luaopen_base(luaState);
-luaopen_string(luaState);
-luaopen_math(luaState);
-
-// import some variables
-ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
-ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
-
-// import game flags
-ScriptSetInteger('gfForts', gfForts);
-ScriptSetInteger('gfMultiWeapon', gfMultiWeapon);
-ScriptSetInteger('gfSolidLand', gfSolidLand);
-ScriptSetInteger('gfBorder', gfBorder);
-ScriptSetInteger('gfDivideTeams', gfDivideTeams);
-ScriptSetInteger('gfLowGravity', gfLowGravity);
-ScriptSetInteger('gfLaserSight', gfLaserSight);
-ScriptSetInteger('gfInvulnerable', gfInvulnerable);
-ScriptSetInteger('gfMines', gfMines);
-ScriptSetInteger('gfVampiric', gfVampiric);
-ScriptSetInteger('gfKarma', gfKarma);
-ScriptSetInteger('gfArtillery', gfArtillery);
-ScriptSetInteger('gfOneClanMode', gfOneClanMode);
-ScriptSetInteger('gfRandomOrder', gfRandomOrder);
-ScriptSetInteger('gfKing', gfKing);
-ScriptSetInteger('gfPlaceHog', gfPlaceHog);
-ScriptSetInteger('gfSharedAmmo', gfSharedAmmo);
-ScriptSetInteger('gfDisableGirders', gfDisableGirders);
-ScriptSetInteger('gfExplosives', gfExplosives);
-
-// register gear types
-for at:= Low(TGearType) to High(TGearType) do
- ScriptSetInteger(str(at), ord(at));
-
-// register sounds
-for st:= Low(TSound) to High(TSound) do
- ScriptSetInteger(str(st), ord(st));
-
-// register ammo types
-for am:= Low(TAmmoType) to High(TAmmoType) do
- ScriptSetInteger(str(am), ord(am));
-
-// register functions
-lua_register(luaState, 'AddGear', @lc_addgear);
-lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
-lua_register(luaState, 'GetGearType', @lc_getgeartype);
-lua_register(luaState, 'EndGame', @lc_endgame);
-lua_register(luaState, 'FindPlace', @lc_findplace);
-lua_register(luaState, 'SetGearPosition', @lc_setgearposition);
-lua_register(luaState, 'GetGearPosition', @lc_getgearposition);
-lua_register(luaState, 'ParseCommand', @lc_parsecommand);
-lua_register(luaState, 'ShowMission', @lc_showmission);
-lua_register(luaState, 'HideMission', @lc_hidemission);
-lua_register(luaState, 'SetAmmo', @lc_setammo);
-lua_register(luaState, 'PlaySound', @lc_playsound);
-lua_register(luaState, 'AddTeam', @lc_addteam);
-lua_register(luaState, 'AddHog', @lc_addhog);
-lua_register(luaState, 'SetHealth', @lc_sethealth);
-lua_register(luaState, 'GetHogClan', @lc_gethogclan);
-lua_register(luaState, 'GetHogName', @lc_gethogname);
-lua_register(luaState, 'GetX', @lc_getx);
-lua_register(luaState, 'GetY', @lc_gety);
-lua_register(luaState, 'CopyPV', @lc_copypv);
-lua_register(luaState, 'CopyPV2', @lc_copypv2);
-lua_register(luaState, 'FollowGear', @lc_followgear);
-lua_register(luaState, 'SetState', @lc_setstate);
-lua_register(luaState, 'GetState', @lc_getstate);
-lua_register(luaState, 'SetTag', @lc_settag);
-
-
-ScriptClearStack; // just to be sure stack is empty
-ScriptLoaded:= false;
-end;
-
-procedure freeModule;
-begin
-lua_close(luaState);
-end;
-
-{$ELSE}
-procedure ScriptPrintStack;
-begin
-end;
-
-procedure ScriptClearStack;
-begin
-end;
-
-procedure ScriptLoad(name : shortstring);
-begin
-end;
-
-procedure ScriptOnGameInit;
-begin
-end;
-
-procedure ScriptCall(fname : shortstring);
-begin
-end;
-
-function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
-begin
-ScriptCall:= 0
-end;
-
-function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
-begin
-ScriptCall:= 0
-end;
-
-function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
-begin
-ScriptCall:= 0
-end;
-
-function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
-begin
-ScriptCall:= 0
-end;
-
-function ScriptExists(fname : shortstring) : boolean;
-begin
-ScriptExists:= false
-end;
-
-procedure initModule;
-begin
-end;
-
-procedure freeModule;
-begin
-end;
-
-{$ENDIF}
-end.
+(*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *)
+
+{$INCLUDE "options.inc"}
+
+unit uScript;
+interface
+
+procedure ScriptPrintStack;
+procedure ScriptClearStack;
+
+procedure ScriptLoad(name : shortstring);
+procedure ScriptOnGameInit;
+
+procedure ScriptCall(fname : shortstring);
+function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
+function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
+function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
+function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
+function ScriptExists(fname : shortstring) : boolean;
+
+procedure initModule;
+procedure freeModule;
+
+implementation
+{$IFNDEF IPHONEOS}
+uses LuaPas in 'LuaPas.pas',
+ uConsole,
+ uMisc,
+ uConsts,
+ uGears,
+ uFloat,
+ uWorld,
+ uAmmos,
+ uSound,
+ uTeams,
+ uKeys,
+ typinfo;
+
+var luaState : Plua_State;
+ ScriptAmmoStore : shortstring;
+ ScriptLoaded : boolean;
+
+procedure ScriptPrepareAmmoStore; forward;
+procedure ScriptApplyAmmoStore; forward;
+procedure ScriptSetAmmo(ammo : TAmmoType; count, propability, delay: Byte); forward;
+
+// wrapped calls //
+
+// functions called from lua:
+// function(L : Plua_State) : LongInt; Cdecl;
+// where L contains the state, returns the number of return values on the stack
+// call lua_gettop(L) to receive number of parameters passed
+
+function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) = 1 then
+ begin
+ WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
+ end
+ else
+ WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
+ lc_writelntoconsole:= 0;
+end;
+
+function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) = 1 then
+ begin
+ ParseCommand(lua_tostring(L ,1), true);
+ end
+ else
+ WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
+ lc_parsecommand:= 0;
+end;
+
+function lc_showmission(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) = 5 then
+ begin
+ ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
+ end
+ else
+ WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
+ lc_showmission:= 0;
+end;
+
+function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
+begin
+ HideMission;
+ lc_hidemission:= 0;
+end;
+
+function lc_addgear(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+ x, y, s, t: LongInt;
+ dx, dy: hwFloat;
+ gt: TGearType;
+begin
+ if lua_gettop(L) <> 7 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ x:= lua_tointeger(L, 1);
+ y:= lua_tointeger(L, 2);
+ gt:= TGearType(lua_tointeger(L, 3));
+ s:= lua_tointeger(L, 4);
+ dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
+ dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
+ t:= lua_tointeger(L, 7);
+
+ gear:= AddGear(x, y, gt, s, dx, dy, t);
+ lua_pushnumber(L, gear^.uid)
+ end;
+ lc_addgear:= 1; // 1 return value
+end;
+
+function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ lua_pushinteger(L, ord(gear^.Kind))
+ else
+ lua_pushnil(L);
+ end;
+ lc_getgeartype:= 1
+end;
+
+function lc_gethogclan(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogClan!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
+ begin
+ lua_pushinteger(L, PHedgehog(gear^.Hedgehog)^.Team^.Clan^.ClanIndex)
+ end
+ else
+ lua_pushnil(L);
+ end;
+ lc_gethogclan:= 1
+end;
+
+function lc_gethogname(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogName!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
+ begin
+ lua_pushstring(L, str2pchar(PHedgehog(gear^.Hedgehog)^.Name))
+ end
+ else
+ lua_pushnil(L);
+ end;
+ lc_gethogname:= 1
+end;
+
+function lc_getx(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetX!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ lua_pushnumber(L, hwRound(gear^.X))
+ else
+ lua_pushnil(L);
+ end;
+ lc_getx:= 1
+end;
+
+function lc_gety(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetY!');
+ lua_pushnil(L); // return value on stack (nil)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ lua_pushnumber(L, hwRound(gear^.Y))
+ else
+ lua_pushnil(L);
+ end;
+ lc_gety:= 1
+end;
+
+function lc_copypv(L : Plua_State) : LongInt; Cdecl;
+var gears, geard : PGear;
+begin
+ if lua_gettop(L) <> 2 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to CopyPV!');
+ end
+ else
+ begin
+ gears:= GearByUID(lua_tointeger(L, 1));
+ geard:= GearByUID(lua_tointeger(L, 2));
+ if (gears <> nil) and (geard <> nil) then
+ begin
+ geard^.X:= gears^.X;
+ geard^.Y:= gears^.Y;
+ geard^.dX:= gears^.dX;
+ geard^.dY:= gears^.dY;
+ end
+ end;
+ lc_copypv:= 1
+end;
+
+function lc_copypv2(L : Plua_State) : LongInt; Cdecl;
+var gears, geard : PGear;
+begin
+ if lua_gettop(L) <> 2 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to CopyPV2!');
+ end
+ else
+ begin
+ gears:= GearByUID(lua_tointeger(L, 1));
+ geard:= GearByUID(lua_tointeger(L, 2));
+ if (gears <> nil) and (geard <> nil) then
+ begin
+ geard^.X:= gears^.X;
+ geard^.Y:= gears^.Y;
+ geard^.dX:= gears^.dX * 2;
+ geard^.dY:= gears^.dY * 2;
+ end
+ end;
+ lc_copypv2:= 1
+end;
+
+function lc_followgear(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ WriteLnToConsole('LUA: Wrong number of parameters passed to FollowGear!')
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then FollowGear:= gear
+ end;
+ lc_followgear:= 0
+end;
+
+function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 2 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then gear^.Health:= lua_tointeger(L, 2)
+ end;
+ lc_sethealth:= 0
+end;
+
+function lc_setstate(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 2 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to SetState!');
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then gear^.State:= lua_tointeger(L, 2)
+ end;
+ lc_setstate:= 0
+end;
+
+function lc_getstate(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetState!');
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ lua_pushinteger(L, gear^.State)
+ else
+ lua_pushnil(L)
+ end;
+ lc_getstate:= 1
+end;
+
+function lc_settag(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+begin
+ if lua_gettop(L) <> 2 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to SetTag!');
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then gear^.Tag:= lua_tointeger(L, 2)
+ end;
+ lc_settag:= 0
+end;
+
+function lc_endgame(L : Plua_State) : LongInt; Cdecl;
+begin
+ GameState:= gsExit;
+ lc_endgame:= 0
+end;
+
+function lc_findplace(L : Plua_State) : LongInt; Cdecl;
+var gear: PGear;
+ fall: boolean;
+ left, right: LongInt;
+begin
+ if lua_gettop(L) <> 4 then
+ WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ fall:= lua_toboolean(L, 2);
+ left:= lua_tointeger(L, 3);
+ right:= lua_tointeger(L, 4);
+ if gear <> nil then
+ FindPlace(gear, fall, left, right)
+ end;
+ lc_findplace:= 0
+end;
+
+function lc_playsound(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) <> 1 then
+ WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
+ else
+ PlaySound(TSound(lua_tointeger(L, 1)));
+ lc_playsound:= 0;
+end;
+
+function lc_addteam(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) <> 5 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!');
+ //lua_pushnil(L)
+ end
+ else
+ begin
+ ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
+ ParseCommand('grave ' + lua_tostring(L, 3), true);
+ ParseCommand('fort ' + lua_tostring(L, 4), true);
+ ParseCommand('voicepack ' + lua_tostring(L, 5), true);
+ CurrentTeam^.Binds:= DefaultBinds
+ // fails on x64
+ //lua_pushinteger(L, LongInt(CurrentTeam));
+ end;
+ lc_addteam:= 0;//1;
+end;
+
+function lc_addhog(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) <> 4 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
+ lua_pushnil(L)
+ end
+ else
+ begin
+ ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
+ ParseCommand('hat ' + lua_tostring(L, 4), true);
+ lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
+ end;
+ lc_addhog:= 1;
+end;
+
+function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
+var gear: PGear;
+begin
+ if lua_gettop(L) <> 1 then
+ begin
+ WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
+ lua_pushnil(L);
+ lua_pushnil(L)
+ end
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ begin
+ lua_pushinteger(L, hwRound(gear^.X));
+ lua_pushinteger(L, hwRound(gear^.Y))
+ end
+ end;
+ lc_getgearposition:= 2;
+end;
+
+function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
+var gear: PGear;
+ x, y: LongInt;
+begin
+ if lua_gettop(L) <> 3 then
+ WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
+ else
+ begin
+ gear:= GearByUID(lua_tointeger(L, 1));
+ if gear <> nil then
+ begin
+ x:= lua_tointeger(L, 2);
+ y:= lua_tointeger(L, 3);
+ gear^.X:= int2hwfloat(x);
+ gear^.Y:= int2hwfloat(y);
+ end
+ end;
+ lc_setgearposition:= 0
+end;
+
+function lc_setammo(L : Plua_State) : LongInt; Cdecl;
+begin
+ if lua_gettop(L) <> 4 then
+ WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
+ else
+ begin
+ ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
+ end;
+ lc_setammo:= 0
+end;
+///////////////////
+
+procedure ScriptPrintStack;
+var n, i : LongInt;
+begin
+ n:= lua_gettop(luaState);
+ WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
+ for i:= 1 to n do
+ if not lua_isboolean(luaState, i) then
+ WriteLnToConsole('LUA: ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
+ else if lua_toboolean(luaState, i) then
+ WriteLnToConsole('LUA: ' + inttostr(i) + ': true')
+ else
+ WriteLnToConsole('LUA: ' + inttostr(i) + ': false');
+end;
+
+procedure ScriptClearStack;
+begin
+lua_settop(luaState, 0)
+end;
+
+procedure ScriptSetNil(name : shortstring);
+begin
+lua_pushnil(luaState);
+lua_setglobal(luaState, Str2PChar(name));
+end;
+
+procedure ScriptSetInteger(name : shortstring; value : LongInt);
+begin
+lua_pushinteger(luaState, value);
+lua_setglobal(luaState, Str2PChar(name));
+end;
+
+procedure ScriptSetString(name : shortstring; value : shortstring);
+begin
+lua_pushstring(luaState, Str2PChar(value));
+lua_setglobal(luaState, Str2PChar(name));
+end;
+
+function ScriptGetInteger(name : shortstring) : LongInt;
+begin
+lua_getglobal(luaState, Str2PChar(name));
+ScriptGetInteger:= lua_tointeger(luaState, -1);
+lua_pop(luaState, 1);
+end;
+
+function ScriptGetString(name : shortstring) : shortstring;
+begin
+lua_getglobal(luaState, Str2PChar(name));
+ScriptGetString:= lua_tostring(luaState, -1);
+lua_pop(luaState, 1);
+end;
+
+procedure ScriptOnGameInit;
+var s, t : ansistring;
+begin
+// not required if there is no script to run
+if not ScriptLoaded then
+ exit;
+
+// push game variables so they may be modified by the script
+ScriptSetInteger('GameFlags', GameFlags);
+ScriptSetString('Seed', cSeed);
+ScriptSetInteger('TurnTime', cHedgehogTurnTime);
+ScriptSetInteger('CaseFreq', cCaseFactor);
+ScriptSetInteger('LandAdds', cLandAdditions);
+ScriptSetInteger('Explosives', cExplosives);
+ScriptSetInteger('Delay', cInactDelay);
+ScriptSetString('Map', '');
+ScriptSetString('Theme', '');
+
+// import locale
+s:= cLocaleFName;
+SplitByChar(s, t, '.');
+ScriptSetString('L', s);
+
+ScriptCall('onGameInit');
+
+// pop game variables
+ParseCommand('seed ' + ScriptGetString('Seed'), true);
+ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
+ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
+ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
+ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
+ParseCommand('$explosives ' + ScriptGetString('Explosives'), true);
+ParseCommand('$delay ' + ScriptGetString('Delay'), true);
+if ScriptGetString('Map') <> '' then
+ ParseCommand('map ' + ScriptGetString('Map'), true);
+if ScriptGetString('Theme') <> '' then
+ ParseCommand('theme ' + ScriptGetString('Theme'), true);
+
+if ScriptExists('onAmmoStoreInit') then
+ begin
+ ScriptPrepareAmmoStore;
+ ScriptCall('onAmmoStoreInit');
+ ScriptApplyAmmoStore
+ end;
+
+ScriptSetInteger('ClansCount', ClansCount)
+end;
+
+procedure ScriptLoad(name : shortstring);
+var ret : LongInt;
+begin
+ret:= luaL_loadfile(luaState, Str2PChar(name));
+if ret <> 0 then
+ WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
+else
+ begin
+ WriteLnToConsole('LUA: ' + name + ' loaded');
+ // call the script file
+ lua_pcall(luaState, 0, 0, 0);
+ ScriptLoaded:= true
+ end
+end;
+
+procedure SetGlobals;
+begin
+ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
+if (CurrentHedgehog <> nil) and (CurrentHedgehog^.Gear <> nil) then
+ ScriptSetInteger('CurrentHedgehog', CurrentHedgehog^.Gear^.UID)
+else
+ ScriptSetNil('CurrentHedgehog');
+end;
+
+procedure GetGlobals;
+begin
+TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
+end;
+
+procedure ScriptCall(fname : shortstring);
+begin
+if not ScriptLoaded or not ScriptExists(fname) then
+ exit;
+SetGlobals;
+lua_getglobal(luaState, Str2PChar(fname));
+if lua_pcall(luaState, 0, 0, 0) <> 0 then
+ begin
+ WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
+ lua_pop(luaState, 1)
+ end;
+GetGlobals;
+end;
+
+function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
+begin
+ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
+end;
+
+function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
+begin
+ScriptCall:= ScriptCall(fname, par1, par2, 0, 0)
+end;
+
+function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
+begin
+ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
+end;
+
+function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
+begin
+if not ScriptLoaded or not ScriptExists(fname) then
+ exit;
+SetGlobals;
+lua_getglobal(luaState, Str2PChar(fname));
+lua_pushinteger(luaState, par1);
+lua_pushinteger(luaState, par2);
+lua_pushinteger(luaState, par3);
+lua_pushinteger(luaState, par4);
+ScriptCall:= 0;
+if lua_pcall(luaState, 4, 1, 0) <> 0 then
+ begin
+ WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
+ lua_pop(luaState, 1)
+ end
+else
+ begin
+ ScriptCall:= lua_tointeger(luaState, -1);
+ lua_pop(luaState, 1)
+ end;
+GetGlobals;
+end;
+
+function ScriptExists(fname : shortstring) : boolean;
+begin
+if not ScriptLoaded then
+ begin
+ ScriptExists:= false;
+ exit
+ end;
+lua_getglobal(luaState, Str2PChar(fname));
+ScriptExists:= not lua_isnoneornil(luaState, -1);
+lua_pop(luaState, -1)
+end;
+
+procedure ScriptPrepareAmmoStore;
+var i: ShortInt;
+begin
+// reset ammostore (quite unclean, but works?)
+uAmmos.freeModule;
+uAmmos.initModule;
+ScriptAmmoStore:= '';
+for i:=1 to ord(High(TAmmoType)) do
+ ScriptAmmoStore:= ScriptAmmoStore + '0000';
+end;
+
+procedure ScriptSetAmmo(ammo : TAmmoType; count, propability, delay: Byte);
+begin
+if (ord(ammo) < 1) or (count > 9) or (count < 0) or (propability < 0) or (propability > 8) or (delay < 0) or (delay > 9)then
+ exit;
+ScriptAmmoStore[ord(ammo)]:= inttostr(count)[1];
+ScriptAmmoStore[ord(ammo) + ord(high(TAmmoType))]:= inttostr(propability)[1];
+ScriptAmmoStore[ord(ammo) + 2 * ord(high(TAmmoType))]:= inttostr(delay)[1];
+end;
+
+procedure ScriptApplyAmmoStore;
+var i : LongInt;
+begin
+for i:= 0 to Pred(TeamsCount) do
+ AddAmmoStore(ScriptAmmoStore);
+end;
+
+// small helper functions making registering enums a lot easier
+function str(const en : TGearType) : shortstring; overload;
+begin
+str:= GetEnumName(TypeInfo(TGearType), ord(en))
+end;
+
+function str(const en : TSound) : shortstring; overload;
+begin
+str:= GetEnumName(TypeInfo(TSound), ord(en))
+end;
+
+function str(const en : TAmmoType) : shortstring; overload;
+begin
+str:= GetEnumName(TypeInfo(TAmmoType), ord(en))
+end;
+///////////////////
+
+procedure initModule;
+var at : TGearType;
+ am : TAmmoType;
+ st : TSound;
+begin
+// initialize lua
+luaState:= lua_open;
+
+// open internal libraries
+luaopen_base(luaState);
+luaopen_string(luaState);
+luaopen_math(luaState);
+
+// import some variables
+ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
+ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
+
+// import game flags
+ScriptSetInteger('gfForts', gfForts);
+ScriptSetInteger('gfMultiWeapon', gfMultiWeapon);
+ScriptSetInteger('gfSolidLand', gfSolidLand);
+ScriptSetInteger('gfBorder', gfBorder);
+ScriptSetInteger('gfDivideTeams', gfDivideTeams);
+ScriptSetInteger('gfLowGravity', gfLowGravity);
+ScriptSetInteger('gfLaserSight', gfLaserSight);
+ScriptSetInteger('gfInvulnerable', gfInvulnerable);
+ScriptSetInteger('gfMines', gfMines);
+ScriptSetInteger('gfVampiric', gfVampiric);
+ScriptSetInteger('gfKarma', gfKarma);
+ScriptSetInteger('gfArtillery', gfArtillery);
+ScriptSetInteger('gfOneClanMode', gfOneClanMode);
+ScriptSetInteger('gfRandomOrder', gfRandomOrder);
+ScriptSetInteger('gfKing', gfKing);
+ScriptSetInteger('gfPlaceHog', gfPlaceHog);
+ScriptSetInteger('gfSharedAmmo', gfSharedAmmo);
+ScriptSetInteger('gfDisableGirders', gfDisableGirders);
+ScriptSetInteger('gfExplosives', gfExplosives);
+
+// register gear types
+for at:= Low(TGearType) to High(TGearType) do
+ ScriptSetInteger(str(at), ord(at));
+
+// register sounds
+for st:= Low(TSound) to High(TSound) do
+ ScriptSetInteger(str(st), ord(st));
+
+// register ammo types
+for am:= Low(TAmmoType) to High(TAmmoType) do
+ ScriptSetInteger(str(am), ord(am));
+
+// register functions
+lua_register(luaState, 'AddGear', @lc_addgear);
+lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
+lua_register(luaState, 'GetGearType', @lc_getgeartype);
+lua_register(luaState, 'EndGame', @lc_endgame);
+lua_register(luaState, 'FindPlace', @lc_findplace);
+lua_register(luaState, 'SetGearPosition', @lc_setgearposition);
+lua_register(luaState, 'GetGearPosition', @lc_getgearposition);
+lua_register(luaState, 'ParseCommand', @lc_parsecommand);
+lua_register(luaState, 'ShowMission', @lc_showmission);
+lua_register(luaState, 'HideMission', @lc_hidemission);
+lua_register(luaState, 'SetAmmo', @lc_setammo);
+lua_register(luaState, 'PlaySound', @lc_playsound);
+lua_register(luaState, 'AddTeam', @lc_addteam);
+lua_register(luaState, 'AddHog', @lc_addhog);
+lua_register(luaState, 'SetHealth', @lc_sethealth);
+lua_register(luaState, 'GetHogClan', @lc_gethogclan);
+lua_register(luaState, 'GetHogName', @lc_gethogname);
+lua_register(luaState, 'GetX', @lc_getx);
+lua_register(luaState, 'GetY', @lc_gety);
+lua_register(luaState, 'CopyPV', @lc_copypv);
+lua_register(luaState, 'CopyPV2', @lc_copypv2);
+lua_register(luaState, 'FollowGear', @lc_followgear);
+lua_register(luaState, 'SetState', @lc_setstate);
+lua_register(luaState, 'GetState', @lc_getstate);
+lua_register(luaState, 'SetTag', @lc_settag);
+
+
+ScriptClearStack; // just to be sure stack is empty
+ScriptLoaded:= false;
+end;
+
+procedure freeModule;
+begin
+lua_close(luaState);
+end;
+
+{$ELSE}
+procedure ScriptPrintStack;
+begin
+end;
+
+procedure ScriptClearStack;
+begin
+end;
+
+procedure ScriptLoad(name : shortstring);
+begin
+end;
+
+procedure ScriptOnGameInit;
+begin
+end;
+
+procedure ScriptCall(fname : shortstring);
+begin
+end;
+
+function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
+begin
+ScriptCall:= 0
+end;
+
+function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
+begin
+ScriptCall:= 0
+end;
+
+function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
+begin
+ScriptCall:= 0
+end;
+
+function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
+begin
+ScriptCall:= 0
+end;
+
+function ScriptExists(fname : shortstring) : boolean;
+begin
+ScriptExists:= false
+end;
+
+procedure initModule;
+begin
+end;
+
+procedure freeModule;
+begin
+end;
+
+{$ENDIF}
+end.