hedgewars/uScript.pas
branchsdl2transition
changeset 11362 ed5a6478e710
parent 11317 62287d4044e7
child 11468 2f6f8baa2a97
equal deleted inserted replaced
11361:31570b766315 11362:ed5a6478e710
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uScript;
    21 unit uScript;
    33 
    33 
    34 procedure ScriptPrintStack;
    34 procedure ScriptPrintStack;
    35 procedure ScriptClearStack;
    35 procedure ScriptClearStack;
    36 
    36 
    37 procedure ScriptLoad(name : shortstring);
    37 procedure ScriptLoad(name : shortstring);
       
    38 procedure ScriptOnPreviewInit;
    38 procedure ScriptOnGameInit;
    39 procedure ScriptOnGameInit;
    39 procedure ScriptOnScreenResize;
    40 procedure ScriptOnScreenResize;
    40 procedure ScriptSetInteger(name : shortstring; value : LongInt);
    41 procedure ScriptSetInteger(name : shortstring; value : LongInt);
       
    42 procedure ScriptSetString(name : shortstring; value : shortstring);
    41 
    43 
    42 procedure ScriptCall(fname : shortstring);
    44 procedure ScriptCall(fname : shortstring);
    43 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
    45 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
    44 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
    46 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
    45 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
    47 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
    46 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
    48 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
    47 function ScriptExists(fname : shortstring) : boolean;
    49 function ScriptExists(fname : shortstring) : boolean;
    48 
    50 
       
    51 procedure LuaParseString(s: shortString);
    49 
    52 
    50 //function ParseCommandOverride(key, value : shortstring) : shortstring;  This did not work out well
    53 //function ParseCommandOverride(key, value : shortstring) : shortstring;  This did not work out well
    51 
    54 
    52 procedure initModule;
    55 procedure initModule;
    53 procedure freeModule;
    56 procedure freeModule;
    83     SysUtils,
    86     SysUtils,
    84     uIO,
    87     uIO,
    85     uVisualGearsList,
    88     uVisualGearsList,
    86     uGearsHandlersMess,
    89     uGearsHandlersMess,
    87     uPhysFSLayer,
    90     uPhysFSLayer,
    88     typinfo
    91     SDLh
       
    92 {$IFNDEF PAS2C}
       
    93     , typinfo
       
    94 {$ENDIF}
    89     ;
    95     ;
    90 
    96 
    91 var luaState : Plua_State;
    97 var luaState : Plua_State;
    92     ScriptAmmoLoadout : shortstring;
    98     ScriptAmmoLoadout : shortstring;
    93     ScriptAmmoProbability : shortstring;
    99     ScriptAmmoProbability : shortstring;
    94     ScriptAmmoDelay : shortstring;
   100     ScriptAmmoDelay : shortstring;
    95     ScriptAmmoReinforcement : shortstring;
   101     ScriptAmmoReinforcement : shortstring;
    96     ScriptLoaded : boolean;
   102     ScriptLoaded : boolean;
    97     mapDims : boolean;
   103     mapDims : boolean;
       
   104     PointsBuffer: shortstring;
       
   105     prevCursorPoint: TPoint;  // why is tpoint still in sdlh...
    98 
   106 
    99 procedure ScriptPrepareAmmoStore; forward;
   107 procedure ScriptPrepareAmmoStore; forward;
   100 procedure ScriptApplyAmmoStore; forward;
   108 procedure ScriptApplyAmmoStore; forward;
   101 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   109 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   102 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   110 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   103 
   111 
       
   112 var LuaDebugInfo: lua_Debug;
       
   113 
       
   114 procedure SetGlobals; forward;
       
   115 procedure LuaParseString(s: shortString);
       
   116 begin
       
   117     SetGlobals;
       
   118     AddFileLog('[Lua] input string: ' + s);
       
   119     AddChatString(#3 + '[Lua] > ' + s);
       
   120     if luaL_dostring(luaState, Str2PChar(s)) <> 0 then
       
   121         begin
       
   122         AddFileLog('[Lua] input string parsing error!');
       
   123         AddChatString(#5 + '[Lua] Error while parsing!');
       
   124         end;
       
   125 end;
       
   126 
       
   127 function LuaUpdateDebugInfo(): Boolean;
       
   128 begin
       
   129     FillChar(LuaDebugInfo, sizeof(LuaDebugInfo), 0);
       
   130 
       
   131     if lua_getstack(luaState, 1, @LuaDebugInfo) = 0 then
       
   132         exit(false); // stack not deep enough
       
   133 
       
   134     // get source name and line count
       
   135     lua_getinfo(luaState, PChar('Sl'), @LuaDebugInfo);
       
   136     exit(true);
       
   137 end;
       
   138 
   104 procedure LuaError(s: shortstring);
   139 procedure LuaError(s: shortstring);
   105 begin
   140 var src: shortstring;
       
   141 const
       
   142     maxsrclen = 20;
       
   143 begin
       
   144     if LuaUpdateDebugInfo() then
       
   145         begin
       
   146         src:= StrPas(LuaDebugInfo.source);
       
   147         s:= 'LUA ERROR [ ... '
       
   148             + copy(src, Length(src) - maxsrclen, maxsrclen - 3) + ':'
       
   149             + inttostr(LuaDebugInfo.currentLine) + ']: ' + s;
       
   150         end
       
   151     else
       
   152         s:= 'LUA ERROR: ' + s;
   106     WriteLnToConsole(s);
   153     WriteLnToConsole(s);
   107     AddChatString(#5 + s);
   154     AddChatString(#5 + s);
   108 end;
   155     if cTestLua then
   109 
   156         halt(HaltTestLuaError);
   110 procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
   157 end;
       
   158 
       
   159 procedure LuaCallError(error, call, paramsyntax: shortstring);
       
   160 begin
       
   161     LuaError(call + ': ' + error);
       
   162     LuaError('-- SYNTAX: ' + call + ' ( ' + paramsyntax + ' )');
       
   163 end;
       
   164 
       
   165 procedure LuaParameterCountError(expected, call, paramsyntax: shortstring; wrongcount: LongInt); inline;
   111 begin
   166 begin
   112     // TODO: i18n?
   167     // TODO: i18n?
   113     LuaError('Lua: Wrong number of parameters (' + inttostr(wrongcount) + ') passed to ' + call + '!     syntax: ' + call + ' ( ' + paramsyntax + ' )');
   168     LuaCallError('Wrong number of parameters! (is: ' + inttostr(wrongcount) + ', should be: '+ expected + ')', call, paramsyntax);
       
   169 end;
       
   170 
       
   171 // compare with allowed count
       
   172 function CheckLuaParamCount(L : Plua_State; count: LongInt; call, paramsyntax: shortstring): boolean; inline;
       
   173 var c: LongInt;
       
   174 begin
       
   175     c:= lua_gettop(L);
       
   176     if c <> count then
       
   177         begin
       
   178         LuaParameterCountError('exactly ' + inttostr(count), call, paramsyntax, c);
       
   179         exit(false);
       
   180         end;
       
   181 
       
   182     CheckLuaParamCount:= true;
       
   183 end;
       
   184 
       
   185 // check if is either count1 or count2
       
   186 function CheckAndFetchParamCount(L : Plua_State; count1, count2: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
       
   187 begin
       
   188     actual:= lua_gettop(L);
       
   189     if (actual <> count1) and (actual <> count2) then
       
   190         begin
       
   191         LuaParameterCountError('either ' + inttostr(count1) + ' or ' + inttostr(count2), call, paramsyntax, actual);
       
   192         exit(false);
       
   193         end;
       
   194 
       
   195     CheckAndFetchParamCount:= true;
       
   196 end;
       
   197 
       
   198 // check if is in range of count1 and count2
       
   199 function CheckAndFetchParamCountRange(L : Plua_State; count1, count2: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
       
   200 begin
       
   201     actual:= lua_gettop(L);
       
   202     if (actual < count1) or (actual > count2) then
       
   203         begin
       
   204         LuaParameterCountError('at least ' + inttostr(count1) + ', but at most ' + inttostr(count2), call, paramsyntax, actual);
       
   205         exit(false);
       
   206         end;
       
   207 
       
   208     CheckAndFetchParamCountRange:= true;
       
   209 end;
       
   210 
       
   211 // check if is same or higher as minCount
       
   212 function CheckAndFetchLuaParamMinCount(L : Plua_State; minCount: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
       
   213 begin
       
   214     actual:= lua_gettop(L);
       
   215     if (actual < minCount) then
       
   216         begin
       
   217         LuaParameterCountError(inttostr(minCount) + ' or more', call, paramsyntax, actual);
       
   218         exit(false);
       
   219         end;
       
   220 
       
   221     CheckAndFetchLuaParamMinCount:= true;
       
   222 end;
       
   223 
       
   224 function LuaToGearTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   225 begin
       
   226     if lua_isnoneornil(L, i) then i:= -1
       
   227     else i:= lua_tointeger(L, i);
       
   228     if (i < ord(Low(TGearType))) or (i > ord(High(TGearType))) then
       
   229         begin
       
   230         LuaCallError('Invalid gearType!', call, paramsyntax);
       
   231         LuaToGearTypeOrd:= -1;
       
   232         end
       
   233     else
       
   234         LuaToGearTypeOrd:= i;
       
   235 end;
       
   236 
       
   237 function LuaToVisualGearTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   238 begin
       
   239     if lua_isnoneornil(L, i) then i:= -1
       
   240     else i:= lua_tointeger(L, i);
       
   241     if (i < ord(Low(TVisualGearType))) or (i > ord(High(TVisualGearType))) then
       
   242         begin
       
   243         LuaCallError('Invalid visualGearType!', call, paramsyntax);
       
   244         LuaToVisualGearTypeOrd:= -1;
       
   245         end
       
   246     else
       
   247         LuaToVisualGearTypeOrd:= i;
       
   248 end;
       
   249 
       
   250 function LuaToAmmoTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   251 begin
       
   252     if lua_isnoneornil(L, i) then i:= -1
       
   253     else i:= lua_tointeger(L, i);
       
   254     if (i < ord(Low(TAmmoType))) or (i > ord(High(TAmmoType))) then
       
   255         begin
       
   256         LuaCallError('Invalid ammoType!', call, paramsyntax);
       
   257         LuaToAmmoTypeOrd:= -1;
       
   258         end
       
   259     else
       
   260         LuaToAmmoTypeOrd:= i;
       
   261 end;
       
   262 
       
   263 function LuaToStatInfoTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   264 begin
       
   265     if lua_isnoneornil(L, i) then i:= -1
       
   266     else i:= lua_tointeger(L, i);
       
   267     if (i < ord(Low(TStatInfoType))) or (i > ord(High(TStatInfoType))) then
       
   268         begin
       
   269         LuaCallError('Invalid statInfoType!', call, paramsyntax);
       
   270         LuaToStatInfoTypeOrd:= -1;
       
   271         end
       
   272     else
       
   273         LuaToStatInfoTypeOrd:= i;
       
   274 end;
       
   275 
       
   276 function LuaToSoundOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   277 begin
       
   278     if lua_isnoneornil(L, i) then i:= -1
       
   279     else i:= lua_tointeger(L, i);
       
   280     if (i < ord(Low(TSound))) or (i > ord(High(TSound))) then
       
   281         begin
       
   282         LuaCallError('Invalid soundId!', call, paramsyntax);
       
   283         LuaToSoundOrd:= -1;
       
   284         end
       
   285     else
       
   286         LuaToSoundOrd:= i;
       
   287 end;
       
   288 
       
   289 function LuaToHogEffectOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   290 begin
       
   291     if lua_isnoneornil(L, i) then i:= -1
       
   292     else i:= lua_tointeger(L, i);
       
   293     if (i < ord(Low(THogEffect))) or (i > ord(High(THogEffect))) then
       
   294         begin
       
   295         LuaCallError('Invalid gear type!', call, paramsyntax);
       
   296         LuaToHogEffectOrd:= -1;
       
   297         end
       
   298     else
       
   299         LuaToHogEffectOrd:= i;
       
   300 end;
       
   301 
       
   302 function LuaToCapGroupOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   303 begin
       
   304     if lua_isnoneornil(L, i) then i:= -1
       
   305     else i:= lua_tointeger(L, i);
       
   306     if (i < ord(Low(TCapGroup))) or (i > ord(High(TCapGroup))) then
       
   307         begin
       
   308         LuaCallError('Invalid capgroup type!', call, paramsyntax);
       
   309         LuaToCapGroupOrd:= -1;
       
   310         end
       
   311     else
       
   312         LuaToCapGroupOrd:= i;
       
   313 end;
       
   314 
       
   315 function LuaToSpriteOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   316 begin
       
   317     if lua_isnoneornil(L, i) then i:= -1
       
   318     else i:= lua_tointeger(L, i);
       
   319     if (i < ord(Low(TSprite))) or (i > ord(High(TSprite))) then
       
   320         begin
       
   321         LuaCallError('Invalid sprite id!', call, paramsyntax);
       
   322         LuaToSpriteOrd:= -1;
       
   323         end
       
   324     else
       
   325         LuaToSpriteOrd:= i;
       
   326 end;
       
   327 
       
   328 function LuaToMapGenOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   329 begin
       
   330     if lua_isnoneornil(L, i) then i:= -1
       
   331     else i:= lua_tointeger(L, i);
       
   332     if (i < ord(Low(TMapGen))) or (i > ord(High(TMapGen))) then
       
   333         begin
       
   334         LuaCallError('Invalid mapgen id!', call, paramsyntax);
       
   335         LuaToMapGenOrd:= -1;
       
   336         end
       
   337     else
       
   338         LuaToMapGenOrd:= i;
   114 end;
   339 end;
   115 
   340 
   116 // wrapped calls //
   341 // wrapped calls //
   117 
   342 
   118 // functions called from Lua:
   343 // functions called from Lua:
   119 // function(L : Plua_State) : LongInt; Cdecl;
   344 // function(L : Plua_State) : LongInt; Cdecl;
   120 // where L contains the state, returns the number of return values on the stack
   345 // where L contains the state, returns the number of return values on the stack
   121 // call lua_gettop(L) to receive number of parameters passed
   346 // call CheckLuaParamCount or CheckAndFetchParamCount
       
   347 // to validate/get the number of passed arguments (see their call definitions)
       
   348 //
       
   349 // use as return value the number of variables pushed back to the lua script
   122 
   350 
   123 function lc_band(L: PLua_State): LongInt; Cdecl;
   351 function lc_band(L: PLua_State): LongInt; Cdecl;
   124 begin
   352 begin
   125     if lua_gettop(L) <> 2 then
   353     if CheckLuaParamCount(L, 2, 'band', 'value1, value2') then
   126         begin
   354         lua_pushinteger(L, lua_tointeger(L, 2) and lua_tointeger(L, 1))
   127         LuaParameterCountError('band', 'value1, value2', lua_gettop(L));
   355     else
   128         lua_pushnil(L);
   356         lua_pushnil(L);
   129         end
       
   130     else
       
   131         lua_pushinteger(L, lua_tointeger(L, 2) and lua_tointeger(L, 1));
       
   132     lc_band := 1;
   357     lc_band := 1;
   133 end;
   358 end;
   134 
   359 
   135 function lc_bor(L: PLua_State): LongInt; Cdecl;
   360 function lc_bor(L: PLua_State): LongInt; Cdecl;
   136 begin
   361 begin
   137     if lua_gettop(L) <> 2 then
   362     if CheckLuaParamCount(L, 2, 'bor', 'value1, value2') then
   138         begin
   363         lua_pushinteger(L, lua_tointeger(L, 2) or lua_tointeger(L, 1))
   139         LuaParameterCountError('bor', 'value1, value2', lua_gettop(L));
   364     else
   140         lua_pushnil(L);
   365         lua_pushnil(L);
   141         end
       
   142     else
       
   143         lua_pushinteger(L, lua_tointeger(L, 2) or lua_tointeger(L, 1));
       
   144     lc_bor := 1;
   366     lc_bor := 1;
   145 end;
   367 end;
   146 
   368 
   147 function lc_bnot(L: PLua_State): LongInt; Cdecl;
   369 function lc_bnot(L: PLua_State): LongInt; Cdecl;
   148 begin
   370 begin
   149     if lua_gettop(L) <> 1 then
   371     if CheckLuaParamCount(L, 1, 'bnot', 'value') then
   150         begin
   372         lua_pushinteger(L, (not lua_tointeger(L, 1)))
   151         LuaParameterCountError('bnot', 'value', lua_gettop(L));
   373     else
   152         lua_pushnil(L);
   374         lua_pushnil(L);
   153         end
       
   154     else
       
   155         lua_pushinteger(L, not lua_tointeger(L, 1));
       
   156     lc_bnot := 1;
   375     lc_bnot := 1;
   157 end;
   376 end;
   158 
   377 
   159 function lc_div(L: PLua_State): LongInt; Cdecl;
   378 function lc_div(L: PLua_State): LongInt; Cdecl;
   160 begin
   379 begin
   161     if lua_gettop(L) <> 2 then
   380     if CheckLuaParamCount(L, 2, 'div', 'dividend, divisor') then
   162         begin
   381         lua_pushinteger(L, lua_tointeger(L, 1) div lua_tointeger(L, 2))
   163         LuaParameterCountError('div', 'dividend, divisor', lua_gettop(L));
   382     else
   164         lua_pushnil(L);
   383         lua_pushnil(L);
   165         end
       
   166     else
       
   167         lua_pushinteger(L, lua_tointeger(L, 1) div lua_tointeger(L, 2));
       
   168     lc_div := 1;
   384     lc_div := 1;
   169 end;
   385 end;
   170 
   386 
   171 function lc_getinputmask(L : Plua_State) : LongInt; Cdecl;
   387 function lc_getinputmask(L : Plua_State) : LongInt; Cdecl;
   172 begin
   388 begin
   173     if lua_gettop(L) <> 0 then
   389     if CheckLuaParamCount(L, 0, 'GetInputMask', '') then
   174         LuaParameterCountError('GetInputMask', '', lua_gettop(L))
       
   175     else
       
   176         lua_pushinteger(L, InputMask);
   390         lua_pushinteger(L, InputMask);
   177     lc_getinputmask:= 1
   391     lc_getinputmask:= 1
   178 end;
   392 end;
   179 
   393 
   180 function lc_setinputmask(L : Plua_State) : LongInt; Cdecl;
   394 function lc_setinputmask(L : Plua_State) : LongInt; Cdecl;
   181 begin
   395 begin
   182     if lua_gettop(L) <> 1 then
   396     if CheckLuaParamCount(L, 1, 'SetInputMask', 'mask') then
   183         LuaParameterCountError('SetInputMask', 'mask', lua_gettop(L))
       
   184     else
       
   185         InputMask:= lua_tointeger(L, 1);
   397         InputMask:= lua_tointeger(L, 1);
   186     lc_setinputmask:= 0
   398     lc_setinputmask:= 0
   187 end;
   399 end;
   188 
   400 
   189 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
   401 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
   190 begin
   402 begin
   191     if lua_gettop(L) = 1 then
   403     if CheckLuaParamCount(L, 1, 'WriteLnToConsole', 'string') then
   192         begin
       
   193         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
   404         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
   194         end
       
   195     else
       
   196         LuaParameterCountError('WriteLnToConsole', 'string', lua_gettop(L));
       
   197     lc_writelntoconsole:= 0;
   405     lc_writelntoconsole:= 0;
   198 end;
   406 end;
   199 
   407 
   200 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
   408 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
   201 var t: PChar;
   409 var t: PChar;
   202     i,c: LongWord;
   410     i,c: LongWord;
   203     s: shortstring;
   411     s: shortstring;
   204 begin
   412 begin
   205     if lua_gettop(L) = 1 then
   413     if CheckLuaParamCount(L, 1, 'ParseCommand', 'string') then
   206         begin
   414         begin
   207         t:= lua_tolstring(L,1,@c);
   415         t:= lua_tolstring(L, 1, Psize_t(@c));
   208 
   416 
   209         for i:= 1 to c do s[i]:= t[i-1];
   417         for i:= 1 to c do s[i]:= t[i-1];
   210         s[0]:= char(c);
   418         s[0]:= char(c);
   211 
   419 
   212         ParseCommand(s, true, true);
   420         ParseCommand(s, true, true);
   213 
   421 
   214         end
   422         end;
   215     else
       
   216         LuaParameterCountError('ParseCommand', 'string', lua_gettop(L));
       
   217     lc_parsecommand:= 0;
   423     lc_parsecommand:= 0;
   218 end;
   424 end;
   219 
   425 
       
   426 // sets weapon to the desired ammo type
       
   427 function lc_setweapon(L : Plua_State) : LongInt; Cdecl;
       
   428 var at: LongInt;
       
   429 const
       
   430     call = 'SetWeapon';
       
   431     params = 'ammoType';
       
   432 begin
       
   433     // no point to run this without any CurrentHedgehog
       
   434     if (CurrentHedgehog <> nil) and (CheckLuaParamCount(L, 1, call, params)) then
       
   435         begin
       
   436         at:= LuaToAmmoTypeOrd(L, 1, call, params);
       
   437         if at >= 0 then
       
   438             ParseCommand('setweap ' + char(at), true, true);
       
   439         end;
       
   440     lc_setweapon:= 0;
       
   441 end;
       
   442 
       
   443 // no parameter means reset to default (and 0 means unlimited)
       
   444 function lc_setmaxbuilddistance(L : Plua_State) : LongInt; Cdecl;
       
   445 var np: LongInt;
       
   446 const
       
   447     call = 'SetMaxBuildDistance';
       
   448     params = '[ distInPx ]';
       
   449 begin
       
   450     if CheckAndFetchParamCountRange(L, 0, 1, call, params, np) then
       
   451         begin
       
   452         if np = 0 then
       
   453             begin
       
   454             // no args? reset
       
   455             cBuildMaxDist:= cDefaultBuildMaxDist;
       
   456             end
       
   457         else
       
   458             CBuildMaxDist:= lua_tointeger(L, 1);
       
   459         end;
       
   460     lc_setmaxbuilddistance:= 0;
       
   461 end;
       
   462 
       
   463 // sets weapon to whatever weapons is next (wraps around, amSkip is skipped)
       
   464 function lc_setnextweapon(L : Plua_State) : LongInt; Cdecl;
       
   465 var at          : LongInt;
       
   466     nextAmmo    : TAmmo;
       
   467     s, a, cs, fa: LongInt;
       
   468 const
       
   469     call = 'SetNextWeapon';
       
   470     params = '';
       
   471 begin
       
   472     if (CurrentHedgehog <> nil) and (CheckLuaParamCount(L, 0, call, params)) then
       
   473         begin
       
   474         at:= -1;
       
   475         with CurrentHedgehog^ do
       
   476             begin
       
   477             cs:= 0; // current slot
       
   478             fa:= 0; // first ammo item to check
       
   479 
       
   480             // if something is selected, find it's successor
       
   481             if curAmmoType <> amNothing then
       
   482                 begin
       
   483                 // get current slot index
       
   484                 cs:= Ammoz[CurAmmoType].Slot;
       
   485                 // find current ammo index
       
   486                 while (fa < cMaxSlotAmmoIndex)
       
   487                     and (Ammo^[cs, fa].AmmoType <> CurAmmoType) do
       
   488                         inc(fa);
       
   489                 // increase once more because we won't successor
       
   490                 inc(fa);
       
   491                 end;
       
   492 
       
   493             // find first available ammo
       
   494             // revisit current slot too (current item might not be first)
       
   495             for s:= cs to cs + cMaxSlotIndex + 1 do
       
   496                 begin
       
   497                 for a:= fa to cMaxSlotAmmoIndex do
       
   498                     begin
       
   499                     // check if we went full circle
       
   500                     if (a = fa) and (s = cs + cMaxSlotIndex + 1)  then
       
   501                         exit(0);
       
   502 
       
   503                     // get ammo
       
   504                     nextAmmo:= Ammo^[s mod (cMaxSlotIndex + 1), a];
       
   505                     // only switch to ammos the hog actually has
       
   506                     if (nextAmmo.AmmoType <> amNothing)
       
   507                         and (nextAmmo.AmmoType <> amSkip) and (nextAmmo.Count > 0) then
       
   508                             begin
       
   509                             at:= ord(nextAmmo.AmmoType);
       
   510                             break;
       
   511                             end;
       
   512                     end;
       
   513                 // stop slot loop if something was found
       
   514                 if at >= 0 then
       
   515                     break;
       
   516                 // check following slots starting with first item
       
   517                 fa:= 0;
       
   518                 end;
       
   519             end;
       
   520 
       
   521         if at >= 0 then
       
   522             ParseCommand('setweap ' + char(at), true, true);
       
   523         end;
       
   524     lc_setnextweapon:= 0;
       
   525 end;
       
   526 
   220 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
   527 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
   221 begin
   528 begin
   222     if lua_gettop(L) = 5 then
   529     if CheckLuaParamCount(L, 5, 'ShowMission', 'caption, subcaption, text, icon, time') then
   223         begin
   530         ShowMission(lua_tostringA(L, 1), lua_tostringA(L, 2), lua_tostringA(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
   224         ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
       
   225         end
       
   226     else
       
   227         LuaParameterCountError('ShowMission', 'caption, subcaption, text, icon, time', lua_gettop(L));
       
   228     lc_showmission:= 0;
   531     lc_showmission:= 0;
   229 end;
   532 end;
   230 
   533 
   231 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   534 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   232 begin
   535 begin
   234     HideMission;
   537     HideMission;
   235     lc_hidemission:= 0;
   538     lc_hidemission:= 0;
   236 end;
   539 end;
   237 
   540 
   238 function lc_enablegameflags(L : Plua_State) : LongInt; Cdecl;
   541 function lc_enablegameflags(L : Plua_State) : LongInt; Cdecl;
   239 var i : integer;
   542 var i, n : integer;
   240 begin
   543 begin
   241     for i:= 1 to lua_gettop(L) do
   544     // can have 1 or more arguments
   242         GameFlags := GameFlags or LongWord(lua_tointeger(L, i));
   545     if CheckAndFetchLuaParamMinCount(L, 1, 'EnableGameFlags', 'gameFlag, ... ', n) then
   243     ScriptSetInteger('GameFlags', GameFlags);
   546         begin
       
   547         for i:= 1 to n do
       
   548             GameFlags := GameFlags or LongWord(lua_tointeger(L, i));
       
   549         ScriptSetInteger('GameFlags', GameFlags);
       
   550         end;
   244     lc_enablegameflags:= 0;
   551     lc_enablegameflags:= 0;
   245 end;
   552 end;
   246 
   553 
   247 function lc_disablegameflags(L : Plua_State) : LongInt; Cdecl;
   554 function lc_disablegameflags(L : Plua_State) : LongInt; Cdecl;
   248 var i : integer;
   555 var i , n: integer;
   249 begin
   556 begin
   250     for i:= 1 to lua_gettop(L) do
   557     // can have 1 or more arguments
   251         GameFlags := GameFlags and not(LongWord(lua_tointeger(L, i)));
   558     if CheckAndFetchLuaParamMinCount(L, 1, 'DisableGameFlags', 'gameFlag, ... ', n) then
   252     ScriptSetInteger('GameFlags', GameFlags);
   559         begin
       
   560         for i:= 1 to n do
       
   561             GameFlags := GameFlags and (not LongWord(lua_tointeger(L, i)));
       
   562         ScriptSetInteger('GameFlags', GameFlags);
       
   563         end;
   253     lc_disablegameflags:= 0;
   564     lc_disablegameflags:= 0;
   254 end;
   565 end;
   255 
   566 
   256 function lc_cleargameflags(L : Plua_State) : LongInt; Cdecl;
   567 function lc_cleargameflags(L : Plua_State) : LongInt; Cdecl;
   257 begin
   568 begin
   258     // Silence hint
   569     if CheckLuaParamCount(L, 0, 'ClearGameFlags', '') then
   259     L:= L;
   570         begin
   260     GameFlags:= 0;
   571         GameFlags:= 0;
   261     ScriptSetInteger('GameFlags', GameFlags);
   572         ScriptSetInteger('GameFlags', GameFlags);
       
   573         end;
   262     lc_cleargameflags:= 0;
   574     lc_cleargameflags:= 0;
   263 end;
   575 end;
   264 
   576 
       
   577 function lc_getgameflag(L : Plua_State) : LongInt; Cdecl;
       
   578 begin
       
   579     if CheckLuaParamCount(L, 1, 'GetGameFlag', 'gameflag') then
       
   580         lua_pushboolean(L, (GameFlags and LongWord(lua_tointeger(L, 1)) <> 0))
       
   581     else
       
   582         lua_pushnil(L);
       
   583     lc_getgameflag:= 1;
       
   584 end;
       
   585 
   265 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
   586 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
   266 begin
   587 var cg: LongInt;
   267     if lua_gettop(L) = 1 then
   588 const
   268         AddCaption(lua_tostring(L, 1), cWhiteColor, capgrpMessage)
   589     call = 'AddCaption';
   269     else if lua_gettop(L) = 3 then
   590     params = 'text [, color, captiongroup]';
   270         begin
   591 begin
   271         AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
   592     if CheckAndFetchParamCount(L, 1, 3, call, params, cg) then
   272         end
   593         begin
   273     else
   594         if cg = 1 then
   274         LuaParameterCountError('AddCaption', 'text[, color, captiongroup]', lua_gettop(L));
   595             AddCaption(lua_tostringA(L, 1), cWhiteColor, capgrpMessage)
       
   596         else
       
   597             begin
       
   598             cg:= LuaToCapGroupOrd(L, 3, call, params);
       
   599             if cg >= 0 then
       
   600                 AddCaption(lua_tostringA(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(cg));
       
   601             end
       
   602         end;
   275     lc_addcaption:= 0;
   603     lc_addcaption:= 0;
   276 end;
   604 end;
   277 
   605 
   278 function lc_campaignlock(L : Plua_State) : LongInt; Cdecl;
   606 function lc_campaignlock(L : Plua_State) : LongInt; Cdecl;
   279 begin
   607 begin
   280     if lua_gettop(L) = 1 then
   608     if CheckLuaParamCount(L, 1, 'CampaignLock', 'TODO') then
   281         begin
   609         begin
   282         // to be done
   610         // TODO
   283         end
   611         end;
   284     else
       
   285         LuaParameterCountError('CampaignLock', 'TODO', lua_gettop(L));
       
   286     lc_campaignlock:= 0;
   612     lc_campaignlock:= 0;
   287 end;
   613 end;
   288 
   614 
   289 function lc_campaignunlock(L : Plua_State) : LongInt; Cdecl;
   615 function lc_campaignunlock(L : Plua_State) : LongInt; Cdecl;
   290 begin
   616 begin
   291     if lua_gettop(L) = 1 then
   617     if CheckLuaParamCount(L, 1, 'CampaignUnlock', 'TODO') then
   292         begin
   618         begin
   293         // to be done
   619         // TODO
   294         end
   620         end;
   295     else
       
   296         LuaParameterCountError('CampaignUnlock', 'TODO', lua_gettop(L));
       
   297     lc_campaignunlock:= 0;
   621     lc_campaignunlock:= 0;
   298 end;
   622 end;
   299 
   623 
   300 function lc_spawnfakehealthcrate(L: Plua_State) : LongInt; Cdecl;
   624 function lc_spawnfakehealthcrate(L: Plua_State) : LongInt; Cdecl;
   301 var gear: PGear;
   625 var gear: PGear;
   302 begin
   626 begin
   303     if lua_gettop(L) <> 4 then
   627     if CheckLuaParamCount(L, 4,'SpawnFakeHealthCrate', 'x, y, explode, poison') then
   304         begin
       
   305         LuaParameterCountError('SpawnFakeHealthCrate', 'x, y, explode, poison', lua_gettop(L));
       
   306         lua_pushnil(L);
       
   307         end
       
   308     else
       
   309         begin
   628         begin
   310         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   629         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   311         HealthCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   630         HealthCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   312         lua_pushinteger(L, gear^.uid);
   631         if gear <> nil then
   313         end;
   632              lua_pushinteger(L, gear^.uid)
       
   633         else lua_pushnil(L)
       
   634         end
       
   635     else
       
   636         lua_pushnil(L);
   314     lc_spawnfakehealthcrate := 1;
   637     lc_spawnfakehealthcrate := 1;
   315 end;
   638 end;
   316 
   639 
   317 function lc_spawnfakeammocrate(L: PLua_State): LongInt; Cdecl;
   640 function lc_spawnfakeammocrate(L: PLua_State): LongInt; Cdecl;
   318 var gear: PGear;
   641 var gear: PGear;
   319 begin
   642 begin
   320     if lua_gettop(L) <> 4 then
   643     if CheckLuaParamCount(L, 4,'SpawnFakeAmmoCrate', 'x, y, explode, poison') then
   321         begin
       
   322         LuaParameterCountError('SpawnFakeAmmoCrate', 'x, y, explode, poison', lua_gettop(L));
       
   323         lua_pushnil(L);
       
   324         end
       
   325     else
       
   326         begin
   644         begin
   327         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   645         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   328         AmmoCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   646         AmmoCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   329         lua_pushinteger(L, gear^.uid);
   647         if gear <> nil then
   330         end;
   648              lua_pushinteger(L, gear^.uid)
       
   649         else lua_pushnil(L)
       
   650         end
       
   651     else
       
   652         lua_pushnil(L);
   331     lc_spawnfakeammocrate := 1;
   653     lc_spawnfakeammocrate := 1;
   332 end;
   654 end;
   333 
   655 
   334 function lc_spawnfakeutilitycrate(L: PLua_State): LongInt; Cdecl;
   656 function lc_spawnfakeutilitycrate(L: PLua_State): LongInt; Cdecl;
   335 var gear: PGear;
   657 var gear: PGear;
   336 begin
   658 begin
   337     if lua_gettop(L) <> 4 then
   659     if CheckLuaParamCount(L, 4,'SpawnFakeUtilityCrate', 'x, y, explode, poison') then
   338         begin
       
   339         LuaParameterCountError('SpawnFakeUtilityCrate', 'x, y, explode, poison', lua_gettop(L));
       
   340         lua_pushnil(L);
       
   341         end
       
   342     else
       
   343         begin
   660         begin
   344         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   661         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   345         UtilityCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   662         UtilityCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
   346         lua_pushinteger(L, gear^.uid);
   663         if gear <> nil then
   347         end;
   664              lua_pushinteger(L, gear^.uid)
       
   665         else lua_pushnil(L)
       
   666         end
       
   667     else
       
   668         lua_pushnil(L);
   348     lc_spawnfakeutilitycrate := 1;
   669     lc_spawnfakeutilitycrate := 1;
   349 end;
   670 end;
   350 
   671 
   351 function lc_spawnhealthcrate(L: Plua_State) : LongInt; Cdecl;
   672 function lc_spawnhealthcrate(L: Plua_State) : LongInt; Cdecl;
   352 var gear: PGear;
   673 var gear: PGear;
   353 var health: LongInt;
   674 var health, n: LongInt;
   354 begin
   675 begin
   355     if (lua_gettop(L) < 2) or (lua_gettop(L) > 3) then
   676     if CheckAndFetchParamCount(L, 2, 3, 'SpawnHealthCrate', 'x, y [, health]', n) then
   356         begin
   677         begin
   357         LuaParameterCountError('SpawnHealthCrate', 'x, y[, health]', lua_gettop(L));
   678         if n = 3 then
   358         lua_pushnil(L);
       
   359         end
       
   360     else
       
   361         begin
       
   362         if lua_gettop(L) = 3 then
       
   363             health:= lua_tointeger(L, 3)
   679             health:= lua_tointeger(L, 3)
   364         else
   680         else
   365             health:= cHealthCaseAmount;
   681             health:= cHealthCaseAmount;
   366         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), HealthCrate, health, 0);
   682         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), HealthCrate, health, 0);
   367         if gear <> nil then
   683         if gear <> nil then
   368             lua_pushinteger(L, gear^.uid)
   684              lua_pushinteger(L, gear^.uid)
   369         else
   685         else lua_pushnil(L);
   370             lua_pushnil(L);
   686         end
   371         end;
   687     else
       
   688         lua_pushnil(L);
   372     lc_spawnhealthcrate := 1;
   689     lc_spawnhealthcrate := 1;
   373 end;
   690 end;
   374 
   691 
   375 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   692 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   376 var gear: PGear;
   693 var gear: PGear;
   377 begin
   694     n   : LongInt;
   378     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   695 begin
   379         begin
   696     if CheckAndFetchParamCount(L, 3, 4, 'SpawnAmmoCrate', 'x, y, content [, amount]', n) then
   380         LuaParameterCountError('SpawnAmmoCrate', 'x, y, content[, amount]', lua_gettop(L));
   697         begin
   381         lua_pushnil(L);
   698         if n = 3 then
   382         end
       
   383     else
       
   384         begin
       
   385         if (lua_gettop(L) = 3) then
       
   386              gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), 0)
   699              gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), 0)
   387         else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
   700         else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
   388         if gear <> nil then
   701         if gear <> nil then
   389             lua_pushinteger(L, gear^.uid)
   702              lua_pushinteger(L, gear^.uid)
   390         else
   703         else lua_pushnil(L);
   391             lua_pushnil(L);
   704         end
   392         end;
   705     else
       
   706         lua_pushnil(L);
   393     lc_spawnammocrate := 1;
   707     lc_spawnammocrate := 1;
   394 end;
   708 end;
   395 
   709 
   396 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   710 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   397 var gear: PGear;
   711 var gear: PGear;
   398 begin
   712     n   : LongInt;
   399     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   713 begin
   400         begin
   714     if CheckAndFetchParamCount(L, 3, 4, 'SpawnUtilityCrate', 'x, y, content [, amount]', n) then
   401         LuaParameterCountError('SpawnUtilityCrate', 'x, y, content[, amount]', lua_gettop(L));
   715         begin
   402         lua_pushnil(L);
   716         if n = 3 then
   403         end
       
   404     else
       
   405         begin
       
   406         if (lua_gettop(L) = 3) then
       
   407              gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), 0)
   717              gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), 0)
   408         else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
   718         else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
   409         if gear <> nil then
   719         if gear <> nil then
   410             lua_pushinteger(L, gear^.uid)
   720              lua_pushinteger(L, gear^.uid)
   411         else
   721         else lua_pushnil(L);
   412             lua_pushnil(L);
   722        end
   413        end;
   723     else
       
   724         lua_pushnil(L);
   414     lc_spawnutilitycrate := 1;
   725     lc_spawnutilitycrate := 1;
   415 end;
   726 end;
   416 
   727 
   417 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   728 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   418 var gear : PGear;
   729 var gear : PGear;
   419     x, y, s, t: LongInt;
   730     x, y, s, t: LongInt;
   420     dx, dy: hwFloat;
   731     dx, dy: hwFloat;
   421     gt: TGearType;
   732     gt: TGearType;
   422 begin
   733 const
   423     if lua_gettop(L) <> 7 then
   734     call = 'AddGear';
   424         begin
   735     params = 'x, y, gearType, state, dx, dy, timer';
   425         LuaParameterCountError('AddGear', 'x, y, gearType, state, dx, dy, timer', lua_gettop(L));
   736 begin
   426         lua_pushnil(L); // return value on stack (nil)
   737     if CheckLuaParamCount(L, 7, call, params) then
   427         end
   738         begin
   428     else
   739         t:= LuaToGearTypeOrd(L, 3, call, params);
   429         begin
   740         if t >= 0 then
   430         x:= lua_tointeger(L, 1);
   741             begin
   431         y:= lua_tointeger(L, 2);
   742             gt:= TGearType(t);
   432         gt:= TGearType(lua_tointeger(L, 3));
   743             x:= lua_tointeger(L, 1);
   433         s:= lua_tointeger(L, 4);
   744             y:= lua_tointeger(L, 2);
   434         dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   745             s:= lua_tointeger(L, 4);
   435         dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   746             dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   436         t:= lua_tointeger(L, 7);
   747             dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   437 
   748             t:= lua_tointeger(L, 7);
   438         gear:= AddGear(x, y, gt, s, dx, dy, t);
   749 
   439         lastGearByUID:= gear;
   750             gear:= AddGear(x, y, gt, s, dx, dy, t);
   440         lua_pushinteger(L, gear^.uid)
   751             lastGearByUID:= gear;
   441         end;
   752             lua_pushinteger(L, gear^.uid)
       
   753             end
       
   754         else
       
   755             lua_pushnil(L);
       
   756         end
       
   757     else
       
   758         lua_pushnil(L);
   442     lc_addgear:= 1; // 1 return value
   759     lc_addgear:= 1; // 1 return value
   443 end;
   760 end;
   444 
   761 
   445 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;
   762 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;
   446 var gear : PGear;
   763 var gear : PGear;
   447 begin
   764 begin
   448     if lua_gettop(L) <> 1 then
   765     if CheckLuaParamCount(L, 1, 'DeleteGear', 'gearUid') then
   449         begin
       
   450         LuaParameterCountError('DeleteGear', 'gearUid', lua_gettop(L));
       
   451         end
       
   452     else
       
   453         begin
   766         begin
   454         gear:= GearByUID(lua_tointeger(L, 1));
   767         gear:= GearByUID(lua_tointeger(L, 1));
   455         if gear <> nil then
   768         if gear <> nil then
   456             gear^.Message:= gear^.Message or gmDelete;
   769             gear^.Message:= gear^.Message or gmDelete;
   457         end;
   770         end;
   458     lc_deletegear:= 0
   771     lc_deletegear:= 0
   459 end;
   772 end;
   460 
   773 
   461 function lc_addvisualgear(L : Plua_State) : LongInt; Cdecl;
   774 function lc_addvisualgear(L : Plua_State) : LongInt; Cdecl;
   462 var vg : PVisualGear;
   775 var vg : PVisualGear;
   463     x, y, s: LongInt;
   776     x, y, s, n, layer: LongInt;
   464     c: Boolean;
   777     c: Boolean;
   465     vgt: TVisualGearType;
   778     vgt: TVisualGearType;
   466 begin
   779     uid: Longword;
   467     if lua_gettop(L) <> 5 then
   780 const
   468         begin
   781     call = 'AddVisualGear';
   469         LuaParameterCountError('AddVisualGear', 'x, y, visualGearType, state, critical', lua_gettop(L));
   782     params = 'x, y, visualGearType, state, critical [, layer]';
       
   783 begin
       
   784     uid:= 0;
       
   785     if CheckAndFetchParamCount(L, 5, 6, call, params, n) then
       
   786         begin
       
   787         s:= LuaToVisualGearTypeOrd(L, 3, call, params);
       
   788         if s >= 0 then
       
   789             begin
       
   790             vgt:= TVisualGearType(s);
       
   791             x:= lua_tointeger(L, 1);
       
   792             y:= lua_tointeger(L, 2);
       
   793             s:= lua_tointeger(L, 4);
       
   794             c:= lua_toboolean(L, 5);
       
   795 
       
   796             if n = 6 then
       
   797                 begin
       
   798                 layer:= lua_tointeger(L, 6);
       
   799                 vg:= AddVisualGear(x, y, vgt, s, c, layer);
       
   800                 end
       
   801             else
       
   802                 vg:= AddVisualGear(x, y, vgt, s, c);
       
   803 
       
   804             if vg <> nil then
       
   805                 begin
       
   806                 lastVisualGearByUID:= vg;
       
   807                 uid:= vg^.uid;
       
   808                 lua_pushinteger(L, uid);
       
   809                 end;
       
   810             end
       
   811             else
       
   812                 lua_pushnil(L);
       
   813         end
       
   814     else
   470         lua_pushnil(L); // return value on stack (nil)
   815         lua_pushnil(L); // return value on stack (nil)
   471         end
       
   472     else
       
   473         begin
       
   474         x:= lua_tointeger(L, 1);
       
   475         y:= lua_tointeger(L, 2);
       
   476         vgt:= TVisualGearType(lua_tointeger(L, 3));
       
   477         s:= lua_tointeger(L, 4);
       
   478         c:= lua_toboolean(L, 5);
       
   479 
       
   480         vg:= AddVisualGear(x, y, vgt, s, c);
       
   481         if vg <> nil then
       
   482             begin
       
   483             lastVisualGearByUID:= vg;
       
   484             lua_pushinteger(L, vg^.uid)
       
   485             end
       
   486         else
       
   487             lua_pushinteger(L, 0)
       
   488         end;
       
   489     lc_addvisualgear:= 1; // 1 return value
   816     lc_addvisualgear:= 1; // 1 return value
   490 end;
   817 end;
   491 
   818 
   492 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   819 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   493 var vg : PVisualGear;
   820 var vg : PVisualGear;
   494 begin
   821 begin
   495     if lua_gettop(L) <> 1 then
   822     vg:= nil;
   496         begin
   823     if CheckLuaParamCount(L, 1, 'DeleteVisualGear', 'vgUid') then
   497         LuaParameterCountError('DeleteVisualGear', 'vgUid', lua_gettop(L));
       
   498         end
       
   499     else
       
   500         begin
   824         begin
   501         vg:= VisualGearByUID(lua_tointeger(L, 1));
   825         vg:= VisualGearByUID(lua_tointeger(L, 1));
   502         if vg <> nil then
   826         if vg <> nil then
   503             DeleteVisualGear(vg);
   827             DeleteVisualGear(vg);
   504         end;
   828         end;
   505     lc_deletevisualgear:= 0
   829     // allow caller to know whether there was something to delete
       
   830     lua_pushboolean(L, vg <> nil);
       
   831     lc_deletevisualgear:= 1
   506 end;
   832 end;
   507 
   833 
   508 function lc_getvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   834 function lc_getvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   509 var vg: PVisualGear;
   835 var vg: PVisualGear;
   510 begin
   836 begin
   511     if lua_gettop(L) <> 1 then
   837     if CheckLuaParamCount(L, 1, 'GetVisualGearValues', 'vgUid') then
   512         begin
       
   513         LuaParameterCountError('GetVisualGearValues', 'vgUid', lua_gettop(L));
       
   514         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   515         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L)
       
   516         end
       
   517     else
       
   518         begin
   838         begin
   519         vg:= VisualGearByUID(lua_tointeger(L, 1));
   839         vg:= VisualGearByUID(lua_tointeger(L, 1));
   520         if vg <> nil then
   840         if vg <> nil then
   521             begin
   841             begin
   522             lua_pushinteger(L, round(vg^.X));
   842             lua_pushinteger(L, round(vg^.X));
   531             lua_pushinteger(L, vg^.Tint);
   851             lua_pushinteger(L, vg^.Tint);
   532             end
   852             end
   533         else
   853         else
   534             begin
   854             begin
   535             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
   855             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
   536             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L)
   856             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
   537             end
   857             end
   538         end;
   858         end
   539     lc_getvisualgearvalues:= 10;
   859     else
       
   860         begin
       
   861         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   862         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   863         end;
       
   864     lc_getvisualgearvalues:= 10
   540 end;
   865 end;
   541 
   866 
   542 function lc_setvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   867 function lc_setvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   543 var vg : PVisualGear;
   868 var vg : PVisualGear;
   544 begin
   869 begin
   545     if lua_gettop(L) <> 11 then
   870 // Param count can be 1-11 at present
   546         begin
   871 //    if CheckLuaParamCount(L, 11, 'SetVisualGearValues', 'vgUid, X, Y, dX, dY, Angle, Frame, FrameTicks, State, Timer, Tint') then
   547         LuaParameterCountError('SetVisualGearValues', 'vgUid, X, Y, dX, dY, Angle, Frame, FrameTicks, State, Timer, Tint', lua_gettop(L));
   872 //        begin
   548         lua_pushnil(L); // return value on stack (nil)
       
   549         end
       
   550     else
       
   551         begin
       
   552         vg:= VisualGearByUID(lua_tointeger(L, 1));
   873         vg:= VisualGearByUID(lua_tointeger(L, 1));
   553         if vg <> nil then
   874         if vg <> nil then
   554             begin
   875             begin
   555             vg^.X:= lua_tointeger(L, 2);
   876             if not lua_isnoneornil(L, 2) then
   556             vg^.Y:= lua_tointeger(L, 3);
   877                 vg^.X:= lua_tointeger(L, 2);
   557             vg^.dX:= lua_tonumber(L, 4);
   878             if not lua_isnoneornil(L, 3) then
   558             vg^.dY:= lua_tonumber(L, 5);
   879                 vg^.Y:= lua_tointeger(L, 3);
   559             vg^.Angle:= lua_tonumber(L, 6);
   880             if not lua_isnoneornil(L, 4) then
   560             vg^.Frame:= lua_tointeger(L, 7);
   881                 vg^.dX:= lua_tonumber(L, 4);
   561             if lua_tointeger(L, 8) <> 0 then
   882             if not lua_isnoneornil(L, 5) then
   562                 vg^.FrameTicks:= lua_tointeger(L, 8);  // find a better way to do this. maybe need to break all these up.
   883                 vg^.dY:= lua_tonumber(L, 5);
   563             vg^.State:= lua_tointeger(L, 9);
   884             if not lua_isnoneornil(L, 6) then
   564             vg^.Timer:= lua_tointeger(L, 10);
   885                 vg^.Angle:= lua_tonumber(L, 6);
   565             vg^.Tint:= lua_tointeger(L, 11);
   886             if not lua_isnoneornil(L, 7) then
       
   887                 vg^.Frame:= lua_tointeger(L, 7);
       
   888             if not lua_isnoneornil(L, 8) then
       
   889                 vg^.FrameTicks:= lua_tointeger(L, 8);
       
   890             if not lua_isnoneornil(L, 9) then
       
   891                 vg^.State:= lua_tointeger(L, 9);
       
   892             if not lua_isnoneornil(L, 10) then
       
   893                 vg^.Timer:= lua_tointeger(L, 10);
       
   894             if not lua_isnoneornil(L, 11) then
       
   895                 vg^.Tint:= lua_tointeger(L, 11)
       
   896             end;
       
   897 //        end
       
   898 //    else
       
   899 //        lua_pushnil(L); // return value on stack (nil)
       
   900     lc_setvisualgearvalues:= 0
       
   901 end;
       
   902 
       
   903 // so. going to use this to get/set some of the more obscure gear values which weren't already exposed elsewhere
       
   904 // can keep adding things in the future. isnoneornil makes it safe
       
   905 function lc_getgearvalues(L : Plua_State) : LongInt; Cdecl;
       
   906 var gear: PGear;
       
   907 begin
       
   908     if CheckLuaParamCount(L, 1, 'GetGearValues', 'gearUid') then
       
   909         begin
       
   910         gear:= GearByUID(lua_tointeger(L, 1));
       
   911         if gear <> nil then
       
   912             begin
       
   913             lua_pushinteger(L, gear^.Angle);
       
   914             lua_pushinteger(L, gear^.Power);
       
   915             lua_pushinteger(L, gear^.WDTimer);
       
   916             lua_pushinteger(L, gear^.Radius);
       
   917             lua_pushinteger(L, hwRound(gear^.Density * _10000));
       
   918             lua_pushinteger(L, gear^.Karma);
       
   919             lua_pushnumber(L,  gear^.DirAngle);
       
   920             lua_pushinteger(L, gear^.AdvBounce);
       
   921             lua_pushinteger(L, Integer(gear^.ImpactSound));
       
   922             lua_pushinteger(L, gear^.nImpactSounds);
       
   923             lua_pushinteger(L, gear^.Tint);
       
   924             lua_pushinteger(L, gear^.Damage)
   566             end
   925             end
   567         end;
   926         else
   568     lc_setvisualgearvalues:= 0;
   927             begin
       
   928             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   929             lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   930             lua_pushnil(L); lua_pushnil(L)
       
   931             end
       
   932         end
       
   933     else
       
   934         begin
       
   935         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   936         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
       
   937         lua_pushnil(L); lua_pushnil(L)
       
   938         end;
       
   939     lc_getgearvalues:= 12
       
   940 end;
       
   941 
       
   942 function lc_setgearvalues(L : Plua_State) : LongInt; Cdecl;
       
   943 var gear : PGear;
       
   944 begin
       
   945 // Currently allows 1-13 params
       
   946 //    if CheckLuaParamCount(L, 13, 'SetGearValues', 'gearUid, Angle, Power, WDTimer, Radius, Density, Karma, DirAngle, AdvBounce, ImpactSound, # ImpactSounds, Tint, Damage') then
       
   947 //        begin
       
   948         gear:= GearByUID(lua_tointeger(L, 1));
       
   949         if gear <> nil then
       
   950             begin
       
   951             if not lua_isnoneornil(L, 2) then
       
   952                 gear^.Angle := lua_tointeger(L, 2);
       
   953             if not lua_isnoneornil(L, 3) then
       
   954                 gear^.Power := lua_tointeger(L, 3);
       
   955             if not lua_isnoneornil(L, 4) then
       
   956                 gear^.WDTimer := lua_tointeger(L, 4);
       
   957             if not lua_isnoneornil(L, 5) then
       
   958                 gear^.Radius := lua_tointeger(L, 5);
       
   959             if not lua_isnoneornil(L, 6) then
       
   960                 gear^.Density:= int2hwFloat(lua_tointeger(L, 6)) / 10000;
       
   961             if not lua_isnoneornil(L, 7) then
       
   962                 gear^.Karma := lua_tointeger(L, 7);
       
   963             if not lua_isnoneornil(L, 8) then
       
   964                 gear^.DirAngle:= lua_tonumber(L, 8);
       
   965             if not lua_isnoneornil(L, 9) then
       
   966                 gear^.AdvBounce := lua_tointeger(L, 9);
       
   967             if not lua_isnoneornil(L, 10) then
       
   968                 gear^.ImpactSound := TSound(lua_tointeger(L, 10));
       
   969             if not lua_isnoneornil(L, 11) then
       
   970                 gear^.nImpactSounds := lua_tointeger(L, 11);
       
   971             if not lua_isnoneornil(L, 12) then
       
   972                 gear^.Tint := lua_tointeger(L, 12);
       
   973             if not lua_isnoneornil(L, 13) then
       
   974                 gear^.Damage := lua_tointeger(L, 13);
       
   975             end;
       
   976 //        end
       
   977 //    else
       
   978 //        lua_pushnil(L); // return value on stack (nil)
       
   979     lc_setgearvalues:= 0
   569 end;
   980 end;
   570 
   981 
   571 function lc_getfollowgear(L : Plua_State) : LongInt; Cdecl;
   982 function lc_getfollowgear(L : Plua_State) : LongInt; Cdecl;
   572 begin
   983 begin
   573     if lua_gettop(L) <> 0 then
   984     if CheckLuaParamCount(L, 0, 'GetFollowGear', '') then
   574         begin
   985         begin
   575         LuaParameterCountError('GetFollowGear', '', lua_gettop(L));
       
   576         lua_pushnil(L); // return value on stack (nil)
       
   577         end
       
   578     else
       
   579         if FollowGear = nil then
   986         if FollowGear = nil then
   580             lua_pushnil(L)
   987             lua_pushnil(L)
   581         else
   988         else
   582             lua_pushinteger(L, FollowGear^.uid);
   989             lua_pushinteger(L, FollowGear^.uid);
       
   990         end
       
   991     else
       
   992         lua_pushnil(L);
   583     lc_getfollowgear:= 1; // 1 return value
   993     lc_getfollowgear:= 1; // 1 return value
   584 end;
   994 end;
   585 
   995 
   586 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   996 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   587 var gear : PGear;
   997 var gear : PGear;
   588 begin
   998 begin
   589     if lua_gettop(L) <> 1 then
   999     if CheckLuaParamCount(L, 1, 'GetGearType', 'gearUid') then
   590         begin
  1000         begin
   591         LuaParameterCountError('GetGearType', 'gearUid', lua_gettop(L));
  1001         gear:= GearByUID(lua_tointeger(L, 1));
       
  1002         if gear <> nil then
       
  1003             lua_pushinteger(L, ord(gear^.Kind))
       
  1004         else
       
  1005             lua_pushnil(L);
       
  1006         end
       
  1007     else
   592         lua_pushnil(L); // return value on stack (nil)
  1008         lua_pushnil(L); // return value on stack (nil)
   593         end
  1009     lc_getgeartype:= 1
   594     else
  1010 end;
   595         begin
  1011 
   596         gear:= GearByUID(lua_tointeger(L, 1));
  1012 function lc_getgearmessage(L : Plua_State) : LongInt; Cdecl;
   597         if gear <> nil then
  1013 var gear : PGear;
   598             lua_pushinteger(L, ord(gear^.Kind))
  1014 begin
       
  1015     if CheckLuaParamCount(L, 1, 'GetGearMessage', 'gearUid') then
       
  1016         begin
       
  1017         gear:= GearByUID(lua_tointeger(L, 1));
       
  1018         if gear <> nil then
       
  1019             lua_pushinteger(L, gear^.message)
   599         else
  1020         else
   600             lua_pushnil(L);
  1021             lua_pushnil(L);
   601         end;
  1022         end
   602     lc_getgeartype:= 1
  1023     else
   603 end;
       
   604 
       
   605 function lc_getgearmessage(L : Plua_State) : LongInt; Cdecl;
       
   606 var gear : PGear;
       
   607 begin
       
   608     if lua_gettop(L) <> 1 then
       
   609         begin
       
   610         LuaParameterCountError('GetGearMessage', 'gearUid', lua_gettop(L));
       
   611         lua_pushnil(L); // return value on stack (nil)
  1024         lua_pushnil(L); // return value on stack (nil)
   612         end
  1025     lc_getgearmessage:= 1
   613     else
  1026 end;
   614         begin
  1027 
   615         gear:= GearByUID(lua_tointeger(L, 1));
  1028 function lc_getgearelasticity(L : Plua_State) : LongInt; Cdecl;
   616         if gear <> nil then
  1029 var gear : PGear;
   617             lua_pushinteger(L, gear^.message)
  1030 begin
       
  1031     if CheckLuaParamCount(L, 1, 'GetGearElasticity', 'gearUid') then
       
  1032         begin
       
  1033         gear:= GearByUID(lua_tointeger(L, 1));
       
  1034         if gear <> nil then
       
  1035             lua_pushinteger(L, hwRound(gear^.elasticity * _10000))
   618         else
  1036         else
   619             lua_pushnil(L);
  1037             lua_pushnil(L);
   620         end;
  1038         end
   621     lc_getgearmessage:= 1
  1039     else
   622 end;
       
   623 
       
   624 function lc_getgearelasticity(L : Plua_State) : LongInt; Cdecl;
       
   625 var gear : PGear;
       
   626 begin
       
   627     if lua_gettop(L) <> 1 then
       
   628         begin
       
   629         LuaParameterCountError('GetGearElasticity', 'gearUid', lua_gettop(L));
       
   630         lua_pushnil(L); // return value on stack (nil)
  1040         lua_pushnil(L); // return value on stack (nil)
   631         end
  1041     lc_getgearelasticity:= 1
   632     else
  1042 end;
   633         begin
  1043 
   634         gear:= GearByUID(lua_tointeger(L, 1));
  1044 function lc_setgearelasticity(L : Plua_State) : LongInt; Cdecl;
   635         if gear <> nil then
  1045 var gear: PGear;
   636             lua_pushinteger(L, hwRound(gear^.elasticity * _10000))
  1046 begin
       
  1047     if CheckLuaParamCount(L, 2, 'SetGearElasticity', 'gearUid, Elasticity') then
       
  1048         begin
       
  1049         gear:= GearByUID(lua_tointeger(L, 1));
       
  1050         if gear <> nil then
       
  1051             gear^.Elasticity:= int2hwFloat(lua_tointeger(L, 2)) / 10000
       
  1052         end;
       
  1053     lc_setgearelasticity:= 0
       
  1054 end;
       
  1055 
       
  1056 function lc_getgearfriction(L : Plua_State) : LongInt; Cdecl;
       
  1057 var gear : PGear;
       
  1058 begin
       
  1059     if CheckLuaParamCount(L, 1, 'GetGearFriction', 'gearUid') then
       
  1060         begin
       
  1061         gear:= GearByUID(lua_tointeger(L, 1));
       
  1062         if gear <> nil then
       
  1063             lua_pushinteger(L, hwRound(gear^.friction * _10000))
   637         else
  1064         else
   638             lua_pushnil(L);
  1065             lua_pushnil(L);
   639         end;
  1066         end
   640     lc_getgearelasticity:= 1
  1067     else
       
  1068         lua_pushnil(L); // return value on stack (nil)
       
  1069     lc_getgearfriction:= 1
       
  1070 end;
       
  1071 
       
  1072 function lc_setgearfriction(L : Plua_State) : LongInt; Cdecl;
       
  1073 var gear: PGear;
       
  1074 begin
       
  1075     if CheckLuaParamCount(L, 2, 'SetGearFriction', 'gearUid, Friction') then
       
  1076         begin
       
  1077         gear:= GearByUID(lua_tointeger(L, 1));
       
  1078         if gear <> nil then
       
  1079             gear^.Friction:= int2hwFloat(lua_tointeger(L, 2)) / 10000
       
  1080         end;
       
  1081     lc_setgearfriction:= 0
   641 end;
  1082 end;
   642 
  1083 
   643 function lc_setgearmessage(L : Plua_State) : LongInt; Cdecl;
  1084 function lc_setgearmessage(L : Plua_State) : LongInt; Cdecl;
   644 var gear : PGear;
  1085 var gear : PGear;
   645 begin
  1086 begin
   646     if lua_gettop(L) <> 2 then
  1087     if CheckLuaParamCount(L, 2, 'SetGearMessage', 'gearUid, message') then
   647         LuaParameterCountError('SetGearMessage', 'gearUid, message', lua_gettop(L))
       
   648     else
       
   649         begin
  1088         begin
   650         gear:= GearByUID(lua_tointeger(L, 1));
  1089         gear:= GearByUID(lua_tointeger(L, 1));
   651         if gear <> nil then
  1090         if gear <> nil then
   652             gear^.message:= lua_tointeger(L, 2);
  1091             gear^.message:= lua_tointeger(L, 2);
   653         end;
  1092         end;
   655 end;
  1094 end;
   656 
  1095 
   657 function lc_getgearpos(L : Plua_State) : LongInt; Cdecl;
  1096 function lc_getgearpos(L : Plua_State) : LongInt; Cdecl;
   658 var gear : PGear;
  1097 var gear : PGear;
   659 begin
  1098 begin
   660     if lua_gettop(L) <> 1 then
  1099     if CheckLuaParamCount(L, 1, 'GetGearPos', 'gearUid') then
   661         begin
  1100         begin
   662         LuaParameterCountError('GetGearPos', 'gearUid', lua_gettop(L));
  1101         gear:= GearByUID(lua_tointeger(L, 1));
       
  1102         if gear <> nil then
       
  1103             lua_pushinteger(L, gear^.Pos)
       
  1104         else
       
  1105             lua_pushnil(L);
       
  1106         end
       
  1107     else
   663         lua_pushnil(L); // return value on stack (nil)
  1108         lua_pushnil(L); // return value on stack (nil)
   664         end
  1109     lc_getgearpos:= 1
   665     else
  1110 end;
   666         begin
  1111 
   667         gear:= GearByUID(lua_tointeger(L, 1));
  1112 function lc_setgearpos(L : Plua_State) : LongInt; Cdecl;
   668         if gear <> nil then
  1113 var gear : PGear;
   669             lua_pushinteger(L, gear^.Pos)
  1114 begin
       
  1115     if CheckLuaParamCount(L, 2, 'SetGearPos', 'gearUid, value') then
       
  1116         begin
       
  1117         gear:= GearByUID(lua_tointeger(L, 1));
       
  1118         if gear <> nil then
       
  1119             gear^.Pos:= lua_tointeger(L, 2);
       
  1120         end;
       
  1121     lc_setgearpos:= 0
       
  1122 end;
       
  1123 
       
  1124 function lc_getgearcollisionmask(L : Plua_State) : LongInt; Cdecl;
       
  1125 var gear : PGear;
       
  1126 begin
       
  1127     if CheckLuaParamCount(L, 1, 'GetGearCollisionMask', 'gearUid') then
       
  1128         begin
       
  1129         gear:= GearByUID(lua_tointeger(L, 1));
       
  1130         if gear <> nil then
       
  1131             lua_pushinteger(L, gear^.CollisionMask)
   670         else
  1132         else
   671             lua_pushnil(L);
  1133             lua_pushnil(L);
   672         end;
  1134         end
   673     lc_getgearpos:= 1
  1135     else
   674 end;
       
   675 
       
   676 function lc_setgearpos(L : Plua_State) : LongInt; Cdecl;
       
   677 var gear : PGear;
       
   678 begin
       
   679     if lua_gettop(L) <> 2 then
       
   680         LuaParameterCountError('SetGearPos', 'gearUid, value', lua_gettop(L))
       
   681     else
       
   682         begin
       
   683         gear:= GearByUID(lua_tointeger(L, 1));
       
   684         if gear <> nil then
       
   685             gear^.Pos:= lua_tointeger(L, 2);
       
   686         end;
       
   687     lc_setgearpos:= 0
       
   688 end;
       
   689 
       
   690 function lc_getgearcollisionmask(L : Plua_State) : LongInt; Cdecl;
       
   691 var gear : PGear;
       
   692 begin
       
   693     if lua_gettop(L) <> 1 then
       
   694         begin
       
   695         LuaParameterCountError('GetGearCollisionMask', 'gearUid', lua_gettop(L));
       
   696         lua_pushnil(L); // return value on stack (nil)
  1136         lua_pushnil(L); // return value on stack (nil)
   697         end
       
   698     else
       
   699         begin
       
   700         gear:= GearByUID(lua_tointeger(L, 1));
       
   701         if gear <> nil then
       
   702             lua_pushinteger(L, gear^.CollisionMask)
       
   703         else
       
   704             lua_pushnil(L);
       
   705         end;
       
   706     lc_getgearcollisionmask:= 1
  1137     lc_getgearcollisionmask:= 1
   707 end;
  1138 end;
   708 
  1139 
   709 function lc_setgearcollisionmask(L : Plua_State) : LongInt; Cdecl;
  1140 function lc_setgearcollisionmask(L : Plua_State) : LongInt; Cdecl;
   710 var gear : PGear;
  1141 var gear : PGear;
   711 begin
  1142 begin
   712     if lua_gettop(L) <> 2 then
  1143     if CheckLuaParamCount(L, 2, 'SetGearCollisionMask', 'gearUid, mask') then
   713         LuaParameterCountError('SetGearCollisionMask', 'gearUid, mask', lua_gettop(L))
       
   714     else
       
   715         begin
  1144         begin
   716         gear:= GearByUID(lua_tointeger(L, 1));
  1145         gear:= GearByUID(lua_tointeger(L, 1));
   717         if gear <> nil then
  1146         if gear <> nil then
   718             gear^.CollisionMask:= lua_tointeger(L, 2);
  1147             gear^.CollisionMask:= lua_tointeger(L, 2);
   719         end;
  1148         end;
   721 end;
  1150 end;
   722 
  1151 
   723 function lc_gethoglevel(L : Plua_State): LongInt; Cdecl;
  1152 function lc_gethoglevel(L : Plua_State): LongInt; Cdecl;
   724 var gear : PGear;
  1153 var gear : PGear;
   725 begin
  1154 begin
   726     if lua_gettop(L) <> 1 then
  1155     if CheckLuaParamCount(L, 1, 'GetHogLevel', 'gearUid') then
   727         LuaParameterCountError('GetHogLevel', 'gearUid', lua_gettop(L))
       
   728     else
       
   729         begin
  1156         begin
   730         gear := GearByUID(lua_tointeger(L, 1));
  1157         gear := GearByUID(lua_tointeger(L, 1));
   731         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  1158         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
   732             lua_pushinteger(L, gear^.Hedgehog^.BotLevel)
  1159             lua_pushinteger(L, gear^.Hedgehog^.BotLevel)
   733         else
  1160         else
   737 end;
  1164 end;
   738 
  1165 
   739 function lc_sethoglevel(L : Plua_State) : LongInt; Cdecl;
  1166 function lc_sethoglevel(L : Plua_State) : LongInt; Cdecl;
   740 var gear : PGear;
  1167 var gear : PGear;
   741 begin
  1168 begin
   742     if lua_gettop(L) <> 2 then
  1169     if CheckLuaParamCount(L, 2, 'SetHogLevel', 'gearUid, level') then
   743         LuaParameterCountError('SetHogLevel', 'gearUid, level', lua_gettop(L))
       
   744     else
       
   745         begin
  1170         begin
   746         gear:= GearByUID(lua_tointeger(L, 1));
  1171         gear:= GearByUID(lua_tointeger(L, 1));
   747         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1172         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
   748             gear^.Hedgehog^.BotLevel:= lua_tointeger(L, 2);
  1173             gear^.Hedgehog^.BotLevel:= lua_tointeger(L, 2);
   749         end;
  1174         end;
   751 end;
  1176 end;
   752 
  1177 
   753 function lc_gethogclan(L : Plua_State) : LongInt; Cdecl;
  1178 function lc_gethogclan(L : Plua_State) : LongInt; Cdecl;
   754 var gear : PGear;
  1179 var gear : PGear;
   755 begin
  1180 begin
   756     if lua_gettop(L) <> 1 then
  1181     if CheckLuaParamCount(L, 1, 'GetHogClan', 'gearUid') then
   757         begin
       
   758         LuaParameterCountError('GetHogClan', 'gearUid', lua_gettop(L));
       
   759         lua_pushnil(L); // return value on stack (nil)
       
   760         end
       
   761     else
       
   762         begin
  1182         begin
   763         gear:= GearByUID(lua_tointeger(L, 1));
  1183         gear:= GearByUID(lua_tointeger(L, 1));
   764         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  1184         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
   765             begin
  1185             begin
   766             lua_pushinteger(L, gear^.Hedgehog^.Team^.Clan^.ClanIndex)
  1186             lua_pushinteger(L, gear^.Hedgehog^.Team^.Clan^.ClanIndex)
   767             end
  1187             end
   768         else
  1188         else
   769             lua_pushnil(L);
  1189             lua_pushnil(L);
   770         end;
  1190         end
       
  1191     else
       
  1192         lua_pushnil(L); // return value on stack (nil)
   771     lc_gethogclan:= 1
  1193     lc_gethogclan:= 1
   772 end;
  1194 end;
   773 
  1195 
   774 function lc_getclancolor(L : Plua_State) : LongInt; Cdecl;
  1196 function lc_getclancolor(L : Plua_State) : LongInt; Cdecl;
   775 begin
  1197 begin
   776     if lua_gettop(L) <> 1 then
  1198     if CheckLuaParamCount(L, 1, 'GetClanColor', 'clan') then
   777         begin
  1199         lua_pushinteger(L, ClansArray[lua_tointeger(L, 1)]^.Color shl 8 or $FF)
   778         LuaParameterCountError('GetClanColor', 'clan', lua_gettop(L));
  1200     else
   779         lua_pushnil(L); // return value on stack (nil)
  1201         lua_pushnil(L); // return value on stack (nil)
   780         end
       
   781     else lua_pushinteger(L, ClansArray[lua_tointeger(L, 1)]^.Color shl 8 or $FF);
       
   782     lc_getclancolor:= 1
  1202     lc_getclancolor:= 1
   783 end;
  1203 end;
   784 
  1204 
   785 function lc_setclancolor(L : Plua_State) : LongInt; Cdecl;
  1205 function lc_setclancolor(L : Plua_State) : LongInt; Cdecl;
   786 var clan : PClan;
  1206 var clan : PClan;
   787     team : PTeam;
  1207     team : PTeam;
   788     hh   : THedgehog;
  1208     hh   : THedgehog;
   789     i, j : LongInt;
  1209     i, j : LongInt;
   790 begin
  1210 begin
   791     if lua_gettop(L) <> 2 then
  1211     if CheckLuaParamCount(L, 2, 'SetClanColor', 'clan, color') then
   792         LuaParameterCountError('SetClanColor', 'clan, color', lua_gettop(L))
  1212         begin
   793     else
  1213 	i:= lua_tointeger(L,1);
   794         begin
  1214 	if i >= ClansCount then exit(0);
   795         clan := ClansArray[lua_tointeger(L, 1)];
  1215         clan := ClansArray[i];
   796         clan^.Color:= lua_tointeger(L, 2) shr 8;
  1216         clan^.Color:= lua_tointeger(L, 2) shr 8;
   797 
  1217 
   798         for i:= 0 to Pred(clan^.TeamsNumber) do
  1218         for i:= 0 to Pred(clan^.TeamsNumber) do
   799             begin
  1219             begin
   800             team:= clan^.Teams[i];
  1220             team:= clan^.Teams[i];
   801             for j:= 0 to 7 do
  1221             for j:= 0 to 7 do
   802                 begin
  1222                 begin
   803                 hh:= team^.Hedgehogs[j];
  1223                 hh:= team^.Hedgehogs[j];
   804                 if (hh.Gear <> nil) or (hh.GearHidden <> nil) then
  1224                 if (hh.Gear <> nil) or (hh.GearHidden <> nil) then
   805                     begin
  1225                     begin
   806                     FreeTexture(hh.NameTagTex);
  1226                     FreeAndNilTexture(hh.NameTagTex);
   807                     hh.NameTagTex:= RenderStringTex(hh.Name, clan^.Color, fnt16);
  1227                     hh.NameTagTex:= RenderStringTex(ansistring(hh.Name), clan^.Color, fnt16);
   808                     RenderHealth(hh);
  1228                     RenderHealth(hh);
   809                     end;
  1229                     end;
   810                 end;
  1230                 end;
   811             FreeTexture(team^.NameTagTex);
  1231             FreeAndNilTexture(team^.NameTagTex);
   812             team^.NameTagTex:= RenderStringTex(clan^.Teams[i]^.TeamName, clan^.Color, fnt16);
  1232             team^.NameTagTex:= RenderStringTex(ansistring(clan^.Teams[i]^.TeamName), clan^.Color, fnt16);
   813             end;
  1233             end;
   814 
  1234 
       
  1235 	    FreeAndNilTexture(clan^.HealthTex);
   815         clan^.HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, clan^.Teams[0]^.NameTagTex^.h, clan^.Color);
  1236         clan^.HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, clan^.Teams[0]^.NameTagTex^.h, clan^.Color);
   816         end;
  1237         end;
   817 
  1238 
   818     lc_setclancolor:= 0
  1239     lc_setclancolor:= 0
   819 end;
  1240 end;
   820 
  1241 
       
  1242 function lc_gethogvoicepack(L : Plua_State) : LongInt; Cdecl;
       
  1243 var gear : PGear;
       
  1244 begin
       
  1245     if CheckLuaParamCount(L, 1, 'GetHogVoicepack', 'gearUid') then
       
  1246         begin
       
  1247         gear:= GearByUID(lua_tointeger(L, 1));
       
  1248         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
  1249             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.Voicepack^.name))
       
  1250         else
       
  1251             lua_pushnil(L);
       
  1252         end
       
  1253     else
       
  1254         lua_pushnil(L); // return value on stack (nil)
       
  1255     lc_gethogvoicepack:= 1
       
  1256 end;
       
  1257 
       
  1258 function lc_gethoggrave(L : Plua_State) : LongInt; Cdecl;
       
  1259 var gear : PGear;
       
  1260 begin
       
  1261     if CheckLuaParamCount(L, 1, 'GetHogGrave', 'gearUid') then
       
  1262         begin
       
  1263         gear:= GearByUID(lua_tointeger(L, 1));
       
  1264         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
       
  1265             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.GraveName))
       
  1266         else
       
  1267             lua_pushnil(L);
       
  1268         end
       
  1269     else
       
  1270         lua_pushnil(L); // return value on stack (nil)
       
  1271     lc_gethoggrave:= 1
       
  1272 end;
       
  1273 
       
  1274 function lc_gethogflag(L : Plua_State) : LongInt; Cdecl;
       
  1275 var gear : PGear;
       
  1276 begin
       
  1277     if CheckLuaParamCount(L, 1, 'GetHogFlag', 'gearUid') then
       
  1278         begin
       
  1279         gear:= GearByUID(lua_tointeger(L, 1));
       
  1280         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
  1281             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.Flag))
       
  1282         else
       
  1283             lua_pushnil(L);
       
  1284         end
       
  1285     else
       
  1286         lua_pushnil(L); // return value on stack (nil)
       
  1287     lc_gethogflag:= 1
       
  1288 end;
       
  1289 
   821 function lc_gethogteamname(L : Plua_State) : LongInt; Cdecl;
  1290 function lc_gethogteamname(L : Plua_State) : LongInt; Cdecl;
   822 var gear : PGear;
  1291 var gear : PGear;
   823 begin
  1292 begin
   824     if lua_gettop(L) <> 1 then
  1293     if CheckLuaParamCount(L, 1, 'GetHogTeamName', 'gearUid') then
   825         begin
  1294         begin
   826         LuaParameterCountError('GetHogTeamName', 'gearUid', lua_gettop(L));
  1295         gear:= GearByUID(lua_tointeger(L, 1));
       
  1296         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
       
  1297             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.TeamName))
       
  1298         else
       
  1299             lua_pushnil(L);
       
  1300         end
       
  1301     else
   827         lua_pushnil(L); // return value on stack (nil)
  1302         lua_pushnil(L); // return value on stack (nil)
   828         end
       
   829     else
       
   830         begin
       
   831         gear:= GearByUID(lua_tointeger(L, 1));
       
   832         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
       
   833             begin
       
   834             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.TeamName))
       
   835             end
       
   836         else
       
   837             lua_pushnil(L);
       
   838         end;
       
   839     lc_gethogteamname:= 1
  1303     lc_gethogteamname:= 1
   840 end;
  1304 end;
   841 
  1305 
   842 function lc_sethogteamname(L : Plua_State) : LongInt; Cdecl;
  1306 function lc_sethogteamname(L : Plua_State) : LongInt; Cdecl;
   843 var gear : PGear;
  1307 var gear : PGear;
   844 begin
  1308 begin
   845     if lua_gettop(L) <> 2 then
  1309     if CheckLuaParamCount(L, 2, 'SetHogTeamName', 'gearUid, name') then
   846         begin
       
   847         LuaParameterCountError('SetHogTeamName', 'gearUid, name', lua_gettop(L));
       
   848         lua_pushnil(L); // return value on stack (nil)
       
   849         end
       
   850     else
       
   851         begin
  1310         begin
   852         gear := GearByUID(lua_tointeger(L, 1));
  1311         gear := GearByUID(lua_tointeger(L, 1));
   853         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  1312         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
   854             begin
  1313             begin
   855             gear^.Hedgehog^.Team^.TeamName := lua_tostring(L, 2);
  1314             gear^.Hedgehog^.Team^.TeamName := lua_tostring(L, 2);
   856 
  1315 
   857             FreeTexture(gear^.Hedgehog^.Team^.NameTagTex);
  1316             FreeAndNilTexture(gear^.Hedgehog^.Team^.NameTagTex);
   858             gear^.Hedgehog^.Team^.NameTagTex:= RenderStringTex(gear^.Hedgehog^.Team^.TeamName, gear^.Hedgehog^.Team^.Clan^.Color, fnt16);
  1317             gear^.Hedgehog^.Team^.NameTagTex:= RenderStringTex(ansistring(gear^.Hedgehog^.Team^.TeamName), gear^.Hedgehog^.Team^.Clan^.Color, fnt16);
   859             end
  1318             end
   860         else
  1319         else
   861             lua_pushnil(L);
  1320             lua_pushnil(L);
   862         end;
  1321         end
       
  1322     else
       
  1323         lua_pushnil(L); // return value on stack (nil)
   863     lc_sethogteamname:= 1
  1324     lc_sethogteamname:= 1
   864 end;
  1325 end;
   865 
  1326 
   866 function lc_gethogname(L : Plua_State) : LongInt; Cdecl;
  1327 function lc_gethogname(L : Plua_State) : LongInt; Cdecl;
   867 var gear : PGear;
  1328 var gear : PGear;
   868 begin
  1329 begin
   869     if lua_gettop(L) <> 1 then
  1330     if CheckLuaParamCount(L, 1, 'GetHogName', 'gearUid') then
   870         begin
       
   871         LuaParameterCountError('GetHogName', 'gearUid', lua_gettop(L));
       
   872         lua_pushnil(L); // return value on stack (nil)
       
   873         end
       
   874     else
       
   875         begin
  1331         begin
   876         gear:= GearByUID(lua_tointeger(L, 1));
  1332         gear:= GearByUID(lua_tointeger(L, 1));
   877         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  1333         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
   878             begin
  1334             begin
   879             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Name))
  1335             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Name))
   880             end
  1336             end
   881         else
  1337         else
   882             lua_pushnil(L);
  1338             lua_pushnil(L);
   883         end;
  1339         end
       
  1340     else
       
  1341         lua_pushnil(L); // return value on stack (nil)
   884     lc_gethogname:= 1
  1342     lc_gethogname:= 1
   885 end;
  1343 end;
   886 
  1344 
   887 function lc_sethogname(L : Plua_State) : LongInt; Cdecl;
  1345 function lc_sethogname(L : Plua_State) : LongInt; Cdecl;
   888 var gear : PGear;
  1346 var gear : PGear;
   889 begin
  1347 begin
   890     if lua_gettop(L) <> 2 then
  1348     if CheckLuaParamCount(L, 2, 'SetHogName', 'gearUid, name') then
   891         begin
       
   892         LuaParameterCountError('SetHogName', 'gearUid, name', lua_gettop(L));
       
   893         lua_pushnil(L)
       
   894         end
       
   895     else
       
   896         begin
  1349         begin
   897         gear:= GearByUID(lua_tointeger(L, 1));
  1350         gear:= GearByUID(lua_tointeger(L, 1));
   898         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1351         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
   899             begin
  1352             begin
   900             gear^.Hedgehog^.Name:= lua_tostring(L, 2);
  1353             gear^.Hedgehog^.Name:= lua_tostring(L, 2);
   901 
  1354 
   902             FreeTexture(gear^.Hedgehog^.NameTagTex);
  1355             FreeAndNilTexture(gear^.Hedgehog^.NameTagTex);
   903             gear^.Hedgehog^.NameTagTex:= RenderStringTex(gear^.Hedgehog^.Name, gear^.Hedgehog^.Team^.Clan^.Color, fnt16)
  1356             gear^.Hedgehog^.NameTagTex:= RenderStringTex(ansistring(gear^.Hedgehog^.Name), gear^.Hedgehog^.Team^.Clan^.Color, fnt16)
   904             end
  1357             end
   905         end;
  1358         end;
   906     lc_sethogname:= 0;
  1359     lc_sethogname:= 0;
   907 end;
  1360 end;
   908 
  1361 
   909 function lc_gettimer(L : Plua_State) : LongInt; Cdecl;
  1362 function lc_gettimer(L : Plua_State) : LongInt; Cdecl;
   910 var gear : PGear;
  1363 var gear : PGear;
   911 begin
  1364 begin
   912     if lua_gettop(L) <> 1 then
  1365     if CheckLuaParamCount(L, 1, 'GetTimer', 'gearUid') then
   913         begin
  1366         begin
   914         LuaParameterCountError('GetTimer', 'gearUid', lua_gettop(L));
  1367         gear:= GearByUID(lua_tointeger(L, 1));
       
  1368         if gear <> nil then
       
  1369             lua_pushinteger(L, gear^.Timer)
       
  1370         else
       
  1371             lua_pushnil(L);
       
  1372         end
       
  1373     else
   915         lua_pushnil(L); // return value on stack (nil)
  1374         lua_pushnil(L); // return value on stack (nil)
   916         end
  1375     lc_gettimer:= 1
   917     else
  1376 end;
   918         begin
  1377 
   919         gear:= GearByUID(lua_tointeger(L, 1));
  1378 function lc_getflighttime(L : Plua_State) : LongInt; Cdecl;
   920         if gear <> nil then
  1379 var gear : PGear;
   921             lua_pushinteger(L, gear^.Timer)
  1380 begin
       
  1381     if CheckLuaParamCount(L, 1, 'GetFlightTime', 'gearUid') then
       
  1382         begin
       
  1383         gear:= GearByUID(lua_tointeger(L, 1));
       
  1384         if gear <> nil then
       
  1385             lua_pushinteger(L, gear^.FlightTime)
   922         else
  1386         else
   923             lua_pushnil(L);
  1387             lua_pushnil(L);
   924         end;
  1388         end
   925     lc_gettimer:= 1
  1389     else
       
  1390         lua_pushnil(L); // return value on stack (nil)
       
  1391     lc_getflighttime:= 1
   926 end;
  1392 end;
   927 
  1393 
   928 function lc_gethealth(L : Plua_State) : LongInt; Cdecl;
  1394 function lc_gethealth(L : Plua_State) : LongInt; Cdecl;
   929 var gear : PGear;
  1395 var gear : PGear;
   930 begin
  1396 begin
   931     if lua_gettop(L) <> 1 then
  1397     if CheckLuaParamCount(L, 1, 'GetHealth', 'gearUid') then
   932         begin
  1398         begin
   933         LuaParameterCountError('GetHealth', 'gearUid', lua_gettop(L));
  1399         gear:= GearByUID(lua_tointeger(L, 1));
       
  1400         if gear <> nil then
       
  1401             lua_pushinteger(L, gear^.Health)
       
  1402         else
       
  1403             lua_pushnil(L);
       
  1404         end
       
  1405     else
   934         lua_pushnil(L); // return value on stack (nil)
  1406         lua_pushnil(L); // return value on stack (nil)
   935         end
  1407     lc_gethealth:= 1
   936     else
  1408 end;
   937         begin
  1409 
   938         gear:= GearByUID(lua_tointeger(L, 1));
  1410 function lc_getx(L : Plua_State) : LongInt; Cdecl;
   939         if gear <> nil then
  1411 var gear : PGear;
   940             lua_pushinteger(L, gear^.Health)
  1412 begin
       
  1413     if CheckLuaParamCount(L, 1, 'GetX', 'gearUid') then
       
  1414         begin
       
  1415         gear:= GearByUID(lua_tointeger(L, 1));
       
  1416         if gear <> nil then
       
  1417             lua_pushinteger(L, hwRound(gear^.X))
   941         else
  1418         else
   942             lua_pushnil(L);
  1419             lua_pushnil(L);
   943         end;
  1420         end
   944     lc_gethealth:= 1
  1421     else
   945 end;
       
   946 
       
   947 function lc_getx(L : Plua_State) : LongInt; Cdecl;
       
   948 var gear : PGear;
       
   949 begin
       
   950     if lua_gettop(L) <> 1 then
       
   951         begin
       
   952         LuaParameterCountError('GetX', 'gearUid', lua_gettop(L));
       
   953         lua_pushnil(L); // return value on stack (nil)
  1422         lua_pushnil(L); // return value on stack (nil)
   954         end
  1423     lc_getx:= 1
   955     else
  1424 end;
   956         begin
  1425 
   957         gear:= GearByUID(lua_tointeger(L, 1));
  1426 function lc_gety(L : Plua_State) : LongInt; Cdecl;
   958         if gear <> nil then
  1427 var gear : PGear;
   959             lua_pushinteger(L, hwRound(gear^.X))
  1428 begin
       
  1429     if CheckLuaParamCount(L, 1, 'GetY', 'gearUid') then
       
  1430         begin
       
  1431         gear:= GearByUID(lua_tointeger(L, 1));
       
  1432         if gear <> nil then
       
  1433             lua_pushinteger(L, hwRound(gear^.Y))
   960         else
  1434         else
   961             lua_pushnil(L);
  1435             lua_pushnil(L);
   962         end;
  1436         end
   963     lc_getx:= 1
  1437     else
   964 end;
       
   965 
       
   966 function lc_gety(L : Plua_State) : LongInt; Cdecl;
       
   967 var gear : PGear;
       
   968 begin
       
   969     if lua_gettop(L) <> 1 then
       
   970         begin
       
   971         LuaParameterCountError('GetY', 'gearUid', lua_gettop(L));
       
   972         lua_pushnil(L); // return value on stack (nil)
  1438         lua_pushnil(L); // return value on stack (nil)
   973         end
       
   974     else
       
   975         begin
       
   976         gear:= GearByUID(lua_tointeger(L, 1));
       
   977         if gear <> nil then
       
   978             lua_pushinteger(L, hwRound(gear^.Y))
       
   979         else
       
   980             lua_pushnil(L);
       
   981         end;
       
   982     lc_gety:= 1
  1439     lc_gety:= 1
   983 end;
  1440 end;
   984 
  1441 
   985 function lc_copypv(L : Plua_State) : LongInt; Cdecl;
  1442 function lc_copypv(L : Plua_State) : LongInt; Cdecl;
   986 var gears, geard : PGear;
  1443 var gears, geard : PGear;
   987 begin
  1444 begin
   988     if lua_gettop(L) <> 2 then
  1445     if CheckLuaParamCount(L, 2, 'CopyPV', 'fromGearUid, toGearUid') then
   989         begin
       
   990         LuaParameterCountError('CopyPV', 'fromGearUid, toGearUid', lua_gettop(L));
       
   991         end
       
   992     else
       
   993         begin
  1446         begin
   994         gears:= GearByUID(lua_tointeger(L, 1));
  1447         gears:= GearByUID(lua_tointeger(L, 1));
   995         geard:= GearByUID(lua_tointeger(L, 2));
  1448         geard:= GearByUID(lua_tointeger(L, 2));
   996         if (gears <> nil) and (geard <> nil) then
  1449         if (gears <> nil) and (geard <> nil) then
   997             begin
  1450             begin
   999             geard^.Y:= gears^.Y;
  1452             geard^.Y:= gears^.Y;
  1000             geard^.dX:= gears^.dX;
  1453             geard^.dX:= gears^.dX;
  1001             geard^.dY:= gears^.dY;
  1454             geard^.dY:= gears^.dY;
  1002             end
  1455             end
  1003         end;
  1456         end;
  1004     lc_copypv:= 1
  1457     lc_copypv:= 0
  1005 end;
  1458 end;
  1006 
  1459 
  1007 function lc_followgear(L : Plua_State) : LongInt; Cdecl;
  1460 function lc_followgear(L : Plua_State) : LongInt; Cdecl;
  1008 var gear : PGear;
  1461 var gear : PGear;
  1009 begin
  1462 begin
  1010     if lua_gettop(L) <> 1 then
  1463     if CheckLuaParamCount(L, 1, 'FollowGear', 'gearUid') then
  1011         LuaParameterCountError('FollowGear', 'gearUid', lua_gettop(L))
       
  1012     else
       
  1013         begin
  1464         begin
  1014         gear:= GearByUID(lua_tointeger(L, 1));
  1465         gear:= GearByUID(lua_tointeger(L, 1));
  1015         if gear <> nil then FollowGear:= gear
  1466         if gear <> nil then FollowGear:= gear
  1016         end;
  1467         end;
  1017     lc_followgear:= 0
  1468     lc_followgear:= 0
  1019 
  1470 
  1020 function lc_hogsay(L : Plua_State) : LongInt; Cdecl;
  1471 function lc_hogsay(L : Plua_State) : LongInt; Cdecl;
  1021 var gear : PGear;
  1472 var gear : PGear;
  1022    vgear : PVisualGear;
  1473    vgear : PVisualGear;
  1023        s : LongWord;
  1474        s : LongWord;
  1024 begin
  1475        n : LongInt;
  1025     if lua_gettop(L) = 4 then
  1476 begin
  1026         s:= lua_tointeger(L, 4)
  1477     if CheckAndFetchParamCount(L, 3, 4, 'HogSay', 'gearUid, text, manner [, vgState]', n) then
  1027     else
  1478         begin
  1028         s:= 0;
  1479         gear:= GearByUID(lua_tointeger(L, 1));
  1029 
  1480         if gear <> nil then
  1030     if (lua_gettop(L) = 4) or (lua_gettop(L) = 3) then
  1481             begin
  1031         begin
  1482             // state defaults to 0 if state param is given
  1032         gear:= GearByUID(lua_tointeger(L, 1));
  1483             if n = 4 then
  1033         if gear <> nil then
  1484                 s:= lua_tointeger(L, 4)
  1034             begin
  1485             else
       
  1486                 s:= 0;
  1035             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
  1487             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
  1036             if vgear <> nil then
  1488             if vgear <> nil then
  1037                begin
  1489                begin
  1038                vgear^.Text:= lua_tostring(L, 2);
  1490                vgear^.Text:= lua_tostring(L, 2);
  1039                vgear^.Hedgehog:= gear^.Hedgehog;
  1491                if Gear^.Kind = gtHedgehog then
       
  1492                    begin
       
  1493                    AddChatString(#9+'[' + gear^.Hedgehog^.Name + '] '+vgear^.text);
       
  1494                    vgear^.Hedgehog:= gear^.Hedgehog
       
  1495                    end
       
  1496                else vgear^.Frame:= gear^.uid;
       
  1497 
  1040                vgear^.FrameTicks:= lua_tointeger(L, 3);
  1498                vgear^.FrameTicks:= lua_tointeger(L, 3);
  1041                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then
  1499                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then
  1042                    vgear^.FrameTicks:= 1;
  1500                    vgear^.FrameTicks:= 1;
  1043                lua_pushinteger(L, vgear^.Uid);
  1501                lua_pushinteger(L, vgear^.Uid);
  1044                AddChatString(#1+'[' + gear^.Hedgehog^.Name + '] '+vgear^.text)
       
  1045                end
  1502                end
  1046             end
  1503             end
  1047             else
  1504         else
  1048                 lua_pushnil(L)
  1505             lua_pushnil(L)
  1049         end
  1506         end
  1050     else LuaParameterCountError('HogSay', 'gearUid, text, manner[, vgState]', lua_gettop(L));
  1507     else
       
  1508         lua_pushnil(L);
  1051     lc_hogsay:= 1
  1509     lc_hogsay:= 1
  1052 end;
  1510 end;
  1053 
  1511 
  1054 function lc_switchhog(L : Plua_State) : LongInt; Cdecl;
  1512 function lc_switchhog(L : Plua_State) : LongInt; Cdecl;
  1055 var gear, prevgear : PGear;
  1513 var gear, prevgear : PGear;
  1056 begin
  1514 begin
  1057     if lua_gettop(L) <> 1 then
  1515     if CheckLuaParamCount(L, 1, 'SwitchHog', 'gearUid') then
  1058         LuaParameterCountError('SwitchHog', 'gearUid', lua_gettop(L))
       
  1059     else
       
  1060         begin
  1516         begin
  1061         gear:= GearByUID(lua_tointeger(L, 1));
  1517         gear:= GearByUID(lua_tointeger(L, 1));
  1062 // should we allow this when there is no current hedgehog? might do some odd(er) things to turn sequence.
  1518 // should we allow this when there is no current hedgehog? might do some odd(er) things to turn sequence.
  1063         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) and (CurrentHedgehog <> nil) then
  1519         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) and (CurrentHedgehog <> nil) then
  1064             begin
  1520             begin
  1081             end
  1537             end
  1082         end;
  1538         end;
  1083     lc_switchhog:= 0
  1539     lc_switchhog:= 0
  1084 end;
  1540 end;
  1085 
  1541 
  1086 {function lc_addammo(L : Plua_State) : LongInt; Cdecl;
  1542 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
  1087 var gear : PGear;
  1543 var gear : PGear;
  1088 begin
  1544     at, n: LongInt;
  1089 
  1545 const
  1090     if lua_gettop(L) = 3 then
  1546     call = 'AddAmmo';
  1091     begin
  1547     params = 'gearUid, ammoType [, ammoCount]';
  1092     gear:= GearByUID(lua_tointeger(L, 1));
  1548 begin
       
  1549     if CheckAndFetchParamCount(L, 2, 3, call, params, n) then
       
  1550         begin
       
  1551         at:= LuaToAmmoTypeOrd(L, 2, call, params);
       
  1552         if at >= 0 then
       
  1553             begin
       
  1554             gear:= GearByUID(lua_tointeger(L, 1));
       
  1555             if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1556                 if n = 2 then
       
  1557                     AddAmmo(gear^.Hedgehog^, TAmmoType(at))
       
  1558                 else
       
  1559                     SetAmmo(gear^.Hedgehog^, TAmmoType(at), lua_tointeger(L, 3))
       
  1560             end;
       
  1561         end;
       
  1562     lc_addammo:= 0
       
  1563 end;
       
  1564 
       
  1565 function lc_getammocount(L : Plua_State) : LongInt; Cdecl;
       
  1566 var gear : PGear;
       
  1567     ammo : PAmmo;
       
  1568     at   : LongInt;
       
  1569 const
       
  1570     call = 'GetAmmoCount';
       
  1571     params = 'gearUid, ammoType';
       
  1572 begin
       
  1573     if CheckLuaParamCount(L, 2, call, params) then
       
  1574         begin
       
  1575         gear:= GearByUID(lua_tointeger(L, 1));
  1093         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1576         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1094             AddAmmoAmount(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L,3) );
  1577             begin
  1095     end else
  1578             at:= LuaToAmmoTypeOrd(L, 2, call, params);
  1096 
  1579             if at >= 0 then
  1097     if lua_gettop(L) = 2 then
  1580                 begin
  1098     begin
  1581                 ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(at));
  1099     gear:= GearByUID(lua_tointeger(L, 1));
  1582                 if ammo^.AmmoType = amNothing then
  1100         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1583                     lua_pushinteger(L, 0)
  1101             AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
  1584                 else
  1102     end else
  1585                     lua_pushinteger(L, ammo^.Count);
  1103     begin
  1586                 end;
  1104         LuaParameterCountError('AddAmmo', 'TODO', lua_gettop(L));
       
  1105     end;
       
  1106 
       
  1107     lc_addammo:= 0;
       
  1108 
       
  1109 end;}
       
  1110 
       
  1111 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
       
  1112 var gear : PGear;
       
  1113 begin
       
  1114     if (lua_gettop(L) = 3) or (lua_gettop(L) = 2) then
       
  1115         begin
       
  1116         gear:= GearByUID(lua_tointeger(L, 1));
       
  1117         if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1118             if lua_gettop(L) = 2 then
       
  1119                 AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)))
       
  1120             else
       
  1121                 SetAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L, 3))
       
  1122         end
       
  1123     else LuaParameterCountError('AddAmmo', 'gearUid, ammoType[, ammoCount]', lua_gettop(L));
       
  1124     lc_addammo:= 0
       
  1125 end;
       
  1126 
       
  1127 function lc_getammocount(L : Plua_State) : LongInt; Cdecl;
       
  1128 var gear : PGear;
       
  1129     ammo : PAmmo;
       
  1130 begin
       
  1131     if (lua_gettop(L) = 2) then
       
  1132         begin
       
  1133         gear:= GearByUID(lua_tointeger(L, 1));
       
  1134         if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1135             begin
       
  1136             ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
       
  1137             if ammo^.AmmoType = amNothing then
       
  1138                 lua_pushinteger(L, 0)
       
  1139             else
       
  1140                 lua_pushinteger(L, ammo^.Count)
       
  1141             end
  1587             end
  1142         else lua_pushinteger(L, 0)
  1588         else lua_pushinteger(L, 0);
  1143         end
  1589         end
  1144     else
  1590     else
  1145         begin
  1591         lua_pushnil(L);
  1146         LuaParameterCountError('GetAmmoCount', 'gearUid, ammoType', lua_gettop(L));
       
  1147         lua_pushnil(L)
       
  1148         end;
       
  1149     lc_getammocount:= 1
  1592     lc_getammocount:= 1
  1150 end;
  1593 end;
  1151 
  1594 
  1152 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
  1595 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
  1153 var gear : PGear;
  1596 var gear : PGear;
  1154 begin
  1597 begin
  1155     if lua_gettop(L) <> 2 then
  1598     if CheckLuaParamCount(L, 2, 'SetHealth', 'gearUid, health') then
  1156         begin
       
  1157         LuaParameterCountError('SetHealth', 'gearUid, health', lua_gettop(L));
       
  1158         end
       
  1159     else
       
  1160         begin
  1599         begin
  1161         gear:= GearByUID(lua_tointeger(L, 1));
  1600         gear:= GearByUID(lua_tointeger(L, 1));
  1162         if gear <> nil then
  1601         if gear <> nil then
  1163             begin
  1602             begin
  1164             gear^.Health:= lua_tointeger(L, 2);
  1603             gear^.Health:= lua_tointeger(L, 2);
  1165 
  1604 
  1166         if (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1605             if (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1167             begin
  1606                 begin
  1168             RenderHealth(gear^.Hedgehog^);
  1607                 RenderHealth(gear^.Hedgehog^);
  1169             RecountTeamHealth(gear^.Hedgehog^.Team)
  1608                 RecountTeamHealth(gear^.Hedgehog^.Team)
  1170             end;
  1609                 end;
  1171 
  1610             // Why did this do a "setalltoactive" ?
  1172             SetAllToActive;
  1611             //SetAllToActive;
       
  1612             Gear^.Active:= true;
       
  1613             AllInactive:= false
  1173             end
  1614             end
  1174         end;
  1615         end;
  1175     lc_sethealth:= 0
  1616     lc_sethealth:= 0
  1176 end;
  1617 end;
  1177 
  1618 
  1178 function lc_settimer(L : Plua_State) : LongInt; Cdecl;
  1619 function lc_settimer(L : Plua_State) : LongInt; Cdecl;
  1179 var gear : PGear;
  1620 var gear : PGear;
  1180 begin
  1621 begin
  1181     if lua_gettop(L) <> 2 then
  1622     if CheckLuaParamCount(L, 2, 'SetTimer', 'gearUid, timer') then
  1182         begin
       
  1183         LuaParameterCountError('SetTimer', 'gearUid, timer', lua_gettop(L));
       
  1184         end
       
  1185     else
       
  1186         begin
  1623         begin
  1187         gear:= GearByUID(lua_tointeger(L, 1));
  1624         gear:= GearByUID(lua_tointeger(L, 1));
  1188         if gear <> nil then gear^.Timer:= lua_tointeger(L, 2)
  1625         if gear <> nil then gear^.Timer:= lua_tointeger(L, 2)
  1189         end;
  1626         end;
  1190     lc_settimer:= 0
  1627     lc_settimer:= 0
       
  1628 end;
       
  1629 
       
  1630 function lc_setflighttime(L : Plua_State) : LongInt; Cdecl;
       
  1631 var gear : PGear;
       
  1632 begin
       
  1633     if CheckLuaParamCount(L, 2, 'SetFlightTime', 'gearUid, flighttime') then
       
  1634         begin
       
  1635         gear:= GearByUID(lua_tointeger(L, 1));
       
  1636         if gear <> nil then gear^.FlightTime:= lua_tointeger(L, 2)
       
  1637         end;
       
  1638     lc_setflighttime:= 0
  1191 end;
  1639 end;
  1192 
  1640 
  1193 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1641 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1194 var gear: PGear;
  1642 var gear: PGear;
  1195 begin
  1643     t   : LongInt;
  1196     if lua_gettop(L) <> 3 then
  1644 const
  1197         LuaParameterCountError('SetEffect', 'gearUid, effect, enabled', lua_gettop(L))
  1645     call = 'SetEffect';
  1198     else begin
  1646     params = 'gearUid, effect, effectState';
  1199         gear := GearByUID(lua_tointeger(L, 1));
  1647 begin
  1200         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1648     if CheckLuaParamCount(L, 3, call, params) then
  1201             gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))]:= lua_tointeger(L, 3);
  1649         begin
  1202     end;
  1650         t:= LuaToHogEffectOrd(L, 2, call, params);
       
  1651         if t >= 0 then
       
  1652             begin
       
  1653             gear := GearByUID(lua_tointeger(L, 1));
       
  1654             if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1655                 gear^.Hedgehog^.Effects[THogEffect(t)]:= lua_tointeger(L, 3);
       
  1656             end;
       
  1657         end;
  1203     lc_seteffect := 0;
  1658     lc_seteffect := 0;
  1204 end;
  1659 end;
  1205 
  1660 
  1206 function lc_geteffect(L : Plua_State) : LongInt; Cdecl;
  1661 function lc_geteffect(L : Plua_State) : LongInt; Cdecl;
  1207 var gear : PGear;
  1662 var gear : PGear;
  1208 begin
  1663     t    : LongInt;
  1209     if lua_gettop(L) <> 2 then
  1664 const
  1210         begin
  1665     call = 'GetEffect';
  1211         LuaParameterCountError('GetEffect', 'gearUid, effect', lua_gettop(L));
  1666     params = 'gearUid, effect';
  1212         end
  1667 begin
  1213     else
  1668     if CheckLuaParamCount(L, 2, call, params) then
  1214         begin
  1669         begin
  1215         gear:= GearByUID(lua_tointeger(L, 1));
  1670         t:= LuaToHogEffectOrd(L, 2, call, params);
  1216         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1671         if t >= 0 then
  1217             lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))])
  1672             begin
  1218         else
  1673             gear:= GearByUID(lua_tointeger(L, 1));
  1219             lua_pushinteger(L, 0)
  1674             if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1220         end;
  1675                 lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(t)])
       
  1676             else
       
  1677                 lua_pushinteger(L, 0)
       
  1678             end;
       
  1679         end
       
  1680     else
       
  1681         lua_pushinteger(L, 0);
  1221     lc_geteffect:= 1
  1682     lc_geteffect:= 1
  1222 end;
  1683 end;
  1223 
  1684 
  1224 function lc_setstate(L : Plua_State) : LongInt; Cdecl;
  1685 function lc_setstate(L : Plua_State) : LongInt; Cdecl;
  1225 var gear : PGear;
  1686 var gear : PGear;
  1226 begin
  1687 begin
  1227     if lua_gettop(L) <> 2 then
  1688     if CheckLuaParamCount(L, 2, 'SetState', 'gearUid, state') then
  1228         begin
       
  1229         LuaParameterCountError('SetState', 'gearUid, state', lua_gettop(L));
       
  1230         end
       
  1231     else
       
  1232         begin
  1689         begin
  1233         gear:= GearByUID(lua_tointeger(L, 1));
  1690         gear:= GearByUID(lua_tointeger(L, 1));
  1234         if gear <> nil then
  1691         if gear <> nil then
  1235             begin
  1692             begin
  1236             gear^.State:= lua_tointeger(L, 2);
  1693             gear^.State:= lua_tointeger(L, 2);
  1241 end;
  1698 end;
  1242 
  1699 
  1243 function lc_getstate(L : Plua_State) : LongInt; Cdecl;
  1700 function lc_getstate(L : Plua_State) : LongInt; Cdecl;
  1244 var gear : PGear;
  1701 var gear : PGear;
  1245 begin
  1702 begin
  1246     if lua_gettop(L) <> 1 then
  1703     if CheckLuaParamCount(L, 1, 'GetState', 'gearUid') then
  1247         begin
       
  1248         LuaParameterCountError('GetState', 'gearUid', lua_gettop(L));
       
  1249         end
       
  1250     else
       
  1251         begin
  1704         begin
  1252         gear:= GearByUID(lua_tointeger(L, 1));
  1705         gear:= GearByUID(lua_tointeger(L, 1));
  1253         if gear <> nil then
  1706         if gear <> nil then
  1254             lua_pushinteger(L, gear^.State)
  1707             lua_pushinteger(L, gear^.State)
  1255         else
  1708         else
  1256             lua_pushnil(L)
  1709             lua_pushnil(L)
  1257         end;
  1710         end
       
  1711     else
       
  1712         lua_pushnil(L); // return value on stack (nil)
  1258     lc_getstate:= 1
  1713     lc_getstate:= 1
  1259 end;
  1714 end;
  1260 
  1715 
  1261 function lc_gettag(L : Plua_State) : LongInt; Cdecl;
  1716 function lc_gettag(L : Plua_State) : LongInt; Cdecl;
  1262 var gear : PGear;
  1717 var gear : PGear;
  1263 begin
  1718 begin
  1264     if lua_gettop(L) <> 1 then
  1719     if CheckLuaParamCount(L, 1, 'GetTag', 'gearUid') then
  1265         begin
  1720         begin
  1266         LuaParameterCountError('GetTag', 'gearUid', lua_gettop(L));
  1721         gear:= GearByUID(lua_tointeger(L, 1));
       
  1722         if gear <> nil then
       
  1723             lua_pushinteger(L, gear^.Tag)
       
  1724         else
       
  1725             lua_pushnil(L);
       
  1726         end
       
  1727     else
  1267         lua_pushnil(L); // return value on stack (nil)
  1728         lua_pushnil(L); // return value on stack (nil)
  1268         end
       
  1269     else
       
  1270         begin
       
  1271         gear:= GearByUID(lua_tointeger(L, 1));
       
  1272         if gear <> nil then
       
  1273             lua_pushinteger(L, gear^.Tag)
       
  1274         else
       
  1275             lua_pushnil(L);
       
  1276         end;
       
  1277     lc_gettag:= 1
  1729     lc_gettag:= 1
  1278 end;
  1730 end;
  1279 
  1731 
  1280 function lc_settag(L : Plua_State) : LongInt; Cdecl;
  1732 function lc_settag(L : Plua_State) : LongInt; Cdecl;
  1281 var gear : PGear;
  1733 var gear : PGear;
  1282 begin
  1734 begin
  1283     if lua_gettop(L) <> 2 then
  1735     if CheckLuaParamCount(L, 2, 'SetTag', 'gearUid, tag') then
  1284         begin
       
  1285         LuaParameterCountError('SetTag', 'gearUid, tag', lua_gettop(L));
       
  1286         end
       
  1287     else
       
  1288         begin
  1736         begin
  1289         gear:= GearByUID(lua_tointeger(L, 1));
  1737         gear:= GearByUID(lua_tointeger(L, 1));
  1290         if gear <> nil then
  1738         if gear <> nil then
  1291             begin
  1739             begin
  1292             gear^.Tag:= lua_tointeger(L, 2);
  1740             gear^.Tag:= lua_tointeger(L, 2);
  1303     lc_endgame:= 0
  1751     lc_endgame:= 0
  1304 end;
  1752 end;
  1305 
  1753 
  1306 function lc_sendstat(L : Plua_State) : LongInt; Cdecl;
  1754 function lc_sendstat(L : Plua_State) : LongInt; Cdecl;
  1307 var statInfo : TStatInfoType;
  1755 var statInfo : TStatInfoType;
  1308 var i : LongInt;
  1756     i, n     : LongInt;
  1309 var color : shortstring;
  1757     color, tn: shortstring;
  1310 begin
  1758     needsTn  : boolean;
  1311     statInfo := TStatInfoType(lua_tointeger(L, 1));
  1759 const
  1312     if (lua_gettop(L) <> 2) and ((statInfo <> siPlayerKills)
  1760     call = 'SendStat';
  1313             and (statInfo <> siClanHealth)) then
  1761     params = 'statInfoType, color [, teamname]';
  1314         begin
  1762 begin
  1315         LuaParameterCountError('SendStat', 'statInfoType, color', lua_gettop(L));
  1763     if CheckAndFetchParamCount(L, 2, 3, call, params, n) then
  1316         end
  1764         begin
  1317     else if (lua_gettop(L) <> 3) and ((statInfo = siPlayerKills)
  1765         i:= LuaToStatInfoTypeOrd(L, 1, call, params);
  1318             or (statInfo = siClanHealth)) then
  1766         if i >= 0 then
  1319         begin
  1767             begin
  1320         LuaParameterCountError('SendStat', 'siClanHealth, color, teamname', lua_gettop(L));
  1768             statInfo:= TStatInfoType(i);
  1321         end
  1769             needsTn:= ((statInfo = siPlayerKills) or (statInfo = siClanHealth));
  1322     else
  1770             // check if param count is correct for the used statInfo
  1323         begin
  1771             if (n = 3) <> needsTn then
  1324         if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
       
  1325             begin
       
  1326             // 3: team name
       
  1327             for i:= 0 to Pred(TeamsCount) do
       
  1328                 begin
  1772                 begin
  1329                 with TeamsArray[i]^ do
  1773                 if n = 3 then
       
  1774                     LuaCallError(EnumToStr(statInfo) + ' does not support the teamname parameter', call, params)
       
  1775                 else
       
  1776                     LuaCallError(EnumToStr(statInfo) + ' requires the teamname parameter', call, params);
       
  1777                 end
       
  1778             else // count is correct!
       
  1779                 begin
       
  1780                 if needsTn then
  1330                     begin
  1781                     begin
  1331                         if TeamName = lua_tostring(L, 3) then
  1782                     // 3: team name
       
  1783                     for i:= 0 to Pred(TeamsCount) do
       
  1784                         begin
       
  1785                         color:= _S'0';
       
  1786                         tn:= lua_tostring(L, 3);
       
  1787                         with TeamsArray[i]^ do
  1332                             begin
  1788                             begin
  1333                             color := uUtils.IntToStr(Clan^.Color);
  1789                                 if TeamName = tn then
  1334                             Break;
  1790                                     begin
       
  1791                                     color := uUtils.IntToStr(Clan^.Color);
       
  1792                                     Break;
       
  1793                                     end
  1335                             end
  1794                             end
       
  1795                         end;
       
  1796                     if (statInfo = siPlayerKills) then
       
  1797                         begin
       
  1798                             SendStat(siPlayerKills, color + ' ' +
       
  1799                                 lua_tostring(L, 2) + ' ' + tn);
       
  1800                         end
       
  1801                     else if (statInfo = siClanHealth) then
       
  1802                         begin
       
  1803                             SendStat(siClanHealth, color + ' ' +
       
  1804                                 lua_tostring(L, 2));
       
  1805                         end
  1336                     end
  1806                     end
       
  1807                 else
       
  1808                     begin
       
  1809                     SendStat(statInfo,lua_tostring(L, 2));
       
  1810                     end;
  1337                 end;
  1811                 end;
  1338             if (statInfo = siPlayerKills) then
       
  1339                 begin
       
  1340                     SendStat(siPlayerKills, color + ' ' +
       
  1341                         lua_tostring(L, 2) + ' ' + TeamsArray[i]^.TeamName);
       
  1342                 end
       
  1343             else if (statInfo = siClanHealth) then
       
  1344                 begin
       
  1345                     SendStat(siClanHealth, color + ' ' +
       
  1346                         lua_tostring(L, 2));
       
  1347                 end
       
  1348             end
       
  1349         else
       
  1350             begin
       
  1351             SendStat(statInfo,lua_tostring(L, 2));
       
  1352             end;
  1812             end;
  1353         end;
  1813         end;
  1354     lc_sendstat:= 0
  1814     lc_sendstat:= 0
  1355 end;
  1815 end;
  1356 
  1816 
  1363 
  1823 
  1364 function lc_findplace(L : Plua_State) : LongInt; Cdecl;
  1824 function lc_findplace(L : Plua_State) : LongInt; Cdecl;
  1365 var gear: PGear;
  1825 var gear: PGear;
  1366     fall: boolean;
  1826     fall: boolean;
  1367     tryhard: boolean;
  1827     tryhard: boolean;
  1368     left, right: LongInt;
  1828     left, right, n: LongInt;
  1369 begin
  1829 begin
  1370     tryhard:= false;
  1830     if CheckAndFetchParamCount(L, 4, 5, 'FindPlace', 'gearUid, fall, left, right [, tryHarder]', n) then
  1371     if (lua_gettop(L) <> 4) and (lua_gettop(L) <> 5) then
       
  1372         LuaParameterCountError('FindPlace', 'gearUid, fall, left, right[, tryHarder]', lua_gettop(L))
       
  1373     else
       
  1374         begin
  1831         begin
  1375         gear:= GearByUID(lua_tointeger(L, 1));
  1832         gear:= GearByUID(lua_tointeger(L, 1));
  1376         fall:= lua_toboolean(L, 2);
  1833         fall:= lua_toboolean(L, 2);
  1377         left:= lua_tointeger(L, 3);
  1834         left:= lua_tointeger(L, 3);
  1378         right:= lua_tointeger(L, 4);
  1835         right:= lua_tointeger(L, 4);
  1379         if lua_gettop(L) = 5 then
  1836         if n = 5 then
  1380             tryhard:= lua_toboolean(L, 5);
  1837             tryhard:= lua_toboolean(L, 5)
       
  1838         else
       
  1839             tryhard:= false;
  1381         if gear <> nil then
  1840         if gear <> nil then
  1382             FindPlace(gear, fall, left, right, tryhard);
  1841             FindPlace(gear, fall, left, right, tryhard);
  1383         if gear <> nil then
  1842         if gear <> nil then
  1384             lua_pushinteger(L, gear^.uid)
  1843             lua_pushinteger(L, gear^.uid)
  1385         else
  1844         else
  1386             lua_pushnil(L);
  1845             lua_pushnil(L);
  1387         end;
  1846         end
       
  1847     else
       
  1848         lua_pushnil(L); // return value on stack (nil)
  1388     lc_findplace:= 1
  1849     lc_findplace:= 1
  1389 end;
  1850 end;
  1390 
  1851 
  1391 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
  1852 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
  1392 var gear: PGear;
  1853 var gear: PGear;
  1393 begin
  1854     n, s: LongInt;
  1394     if lua_gettop(L) = 1 then
  1855 const
  1395         PlaySound(TSound(lua_tointeger(L, 1)))
  1856     call = 'PlaySound';
  1396     else if lua_gettop(L) = 2 then
  1857     params = 'soundId [, hhGearUid]';
  1397         begin
  1858 begin
  1398         gear:= GearByUID(lua_tointeger(L, 2));
  1859     if CheckAndFetchParamCount(L, 1, 2, call, params, n) then
  1399         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1860         begin
  1400             AddVoice(TSound(lua_tointeger(L, 1)),gear^.Hedgehog^.Team^.Voicepack)
  1861         s:= LuaToSoundOrd(L, 1, call, params);
  1401         end
  1862         if s >= 0 then
  1402     else LuaParameterCountError('PlaySound', 'soundId', lua_gettop(L));
  1863             begin
       
  1864             // no gear specified
       
  1865             if n = 1 then
       
  1866                 PlaySound(TSound(s))
       
  1867             else
       
  1868                 begin
       
  1869                 gear:= GearByUID(lua_tointeger(L, 2));
       
  1870                 if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
  1871                     AddVoice(TSound(s),gear^.Hedgehog^.Team^.Voicepack)
       
  1872                 end;
       
  1873             end;
       
  1874         end;
  1403     lc_playsound:= 0;
  1875     lc_playsound:= 0;
  1404 end;
  1876 end;
  1405 
  1877 
  1406 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1878 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1407 var np: LongInt;
  1879 var np: LongInt;
  1408 begin
  1880 begin
  1409     np:= lua_gettop(L);
  1881     if CheckAndFetchParamCount(L, 5, 6, 'AddTeam', 'teamname, color, grave, fort, voicepack [, flag]', np) then
  1410     if (np < 5) or (np > 6) then
       
  1411         begin
       
  1412         LuaParameterCountError('AddTeam', 'teamname, color, grave, fort, voicepack[, flag]', lua_gettop(L));
       
  1413         //lua_pushnil(L)
       
  1414         end
       
  1415     else
       
  1416         begin
  1882         begin
  1417         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true, true);
  1883         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true, true);
  1418         ParseCommand('grave ' + lua_tostring(L, 3), true, true);
  1884         ParseCommand('grave ' + lua_tostring(L, 3), true, true);
  1419         ParseCommand('fort ' + lua_tostring(L, 4), true, true);
  1885         ParseCommand('fort ' + lua_tostring(L, 4), true, true);
  1420         ParseCommand('voicepack ' + lua_tostring(L, 5), true, true);
  1886         ParseCommand('voicepack ' + lua_tostring(L, 5), true, true);
  1421         if (np = 6) then ParseCommand('flag ' + lua_tostring(L, 6), true, true);
  1887         if (np = 6) then ParseCommand('flag ' + lua_tostring(L, 6), true, true);
  1422         CurrentTeam^.Binds:= DefaultBinds
  1888         CurrentTeam^.Binds:= DefaultBinds
  1423         // fails on x64
  1889         // fails on x64
  1424         //lua_pushinteger(L, LongInt(CurrentTeam));
  1890         //lua_pushinteger(L, LongInt(CurrentTeam));
  1425         end;
  1891         end;
       
  1892     //else
       
  1893         //lua_pushnil(L)
  1426     lc_addteam:= 0;//1;
  1894     lc_addteam:= 0;//1;
       
  1895 end;
       
  1896 
       
  1897 function lc_dismissteam(L : Plua_State) : LongInt; Cdecl;
       
  1898 var HHGear: PGear;
       
  1899     i, h  : LongInt;
       
  1900     hidden: boolean;
       
  1901 begin
       
  1902     if CheckLuaParamCount(L, 1, 'DismissTeam', 'teamname') then
       
  1903         begin
       
  1904         if TeamsCount > 0 then
       
  1905             for i:= 0 to Pred(TeamsCount) do
       
  1906                 begin
       
  1907                 // skip teams that don't have matching name
       
  1908                 if TeamsArray[i]^.TeamName <> lua_tostring(L, 1) then
       
  1909                     continue;
       
  1910 
       
  1911                 // destroy all hogs of matching team, including the hidden ones
       
  1912                 for h:= 0 to cMaxHHIndex do
       
  1913                     begin
       
  1914                     hidden:= (TeamsArray[i]^.Hedgehogs[h].GearHidden <> nil);
       
  1915                     if hidden then
       
  1916                         RestoreHog(@TeamsArray[i]^.Hedgehogs[h]);
       
  1917                     // destroy hedgehog gear, if any
       
  1918                     HHGear:= TeamsArray[i]^.Hedgehogs[h].Gear;
       
  1919                     if HHGear <> nil then
       
  1920                         begin
       
  1921                         // smoke effect
       
  1922                         if (not hidden) then
       
  1923                             begin
       
  1924                             AddVisualGear(hwRound(HHGear^.X), hwRound(HHGear^.Y), vgtSmokeWhite);
       
  1925                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1926                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1927                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1928                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1929                             end;
       
  1930                         HHGear^.Message:= HHGear^.Message or gmDestroy;
       
  1931                         end;
       
  1932                     end;
       
  1933                 // can't dismiss more than one team
       
  1934                 break;
       
  1935                 end;
       
  1936         end;
       
  1937     lc_dismissteam:= 0;
  1427 end;
  1938 end;
  1428 
  1939 
  1429 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
  1940 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
  1430 var temp: ShortString;
  1941 var temp: ShortString;
  1431 begin
  1942 begin
  1432     if lua_gettop(L) <> 4 then
  1943     if CheckLuaParamCount(L, 4, 'AddHog', 'hogname, botlevel, health, hat') then
  1433         begin
       
  1434         LuaParameterCountError('AddHog', 'hogname, botlevel, health, hat', lua_gettop(L));
       
  1435         lua_pushnil(L)
       
  1436         end
       
  1437     else
       
  1438         begin
  1944         begin
  1439         temp:= lua_tostring(L, 4);
  1945         temp:= lua_tostring(L, 4);
  1440         ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true, true);
  1946         ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true, true);
  1441         ParseCommand('hat ' + temp, true, true);
  1947         ParseCommand('hat ' + temp, true, true);
  1442         lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
  1948         lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
  1443         end;
  1949         end
       
  1950     else
       
  1951         lua_pushnil(L);
  1444     lc_addhog:= 1;
  1952     lc_addhog:= 1;
  1445 end;
  1953 end;
  1446 
  1954 
  1447 function lc_hogturnleft(L : Plua_State) : LongInt; Cdecl;
  1955 function lc_hogturnleft(L : Plua_State) : LongInt; Cdecl;
  1448 var gear: PGear;
  1956 var gear: PGear;
  1449 begin
  1957 begin
  1450     if lua_gettop(L) <> 2 then
  1958     if CheckLuaParamCount(L, 2, 'HogTurnLeft', 'gearUid, boolean') then
  1451         begin
       
  1452         LuaParameterCountError('HogTurnLeft', 'gearUid, boolean', lua_gettop(L));
       
  1453         end
       
  1454     else
       
  1455         begin
  1959         begin
  1456         gear:= GearByUID(lua_tointeger(L, 1));
  1960         gear:= GearByUID(lua_tointeger(L, 1));
  1457         if gear <> nil then
  1961         if gear <> nil then
  1458             gear^.dX.isNegative:= lua_toboolean(L, 2);
  1962             gear^.dX.isNegative:= lua_toboolean(L, 2);
  1459         end;
  1963         end;
  1461 end;
  1965 end;
  1462 
  1966 
  1463 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
  1967 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
  1464 var gear: PGear;
  1968 var gear: PGear;
  1465 begin
  1969 begin
  1466     if lua_gettop(L) <> 1 then
  1970     if CheckLuaParamCount(L, 1, 'GetGearPosition', 'gearUid') then
  1467         begin
       
  1468         LuaParameterCountError('GetGearPosition', 'gearUid', lua_gettop(L));
       
  1469         lua_pushnil(L);
       
  1470         lua_pushnil(L)
       
  1471         end
       
  1472     else
       
  1473         begin
  1971         begin
  1474         gear:= GearByUID(lua_tointeger(L, 1));
  1972         gear:= GearByUID(lua_tointeger(L, 1));
  1475         if gear <> nil then
  1973         if gear <> nil then
  1476             begin
  1974             begin
  1477             lua_pushinteger(L, hwRound(gear^.X));
  1975             lua_pushinteger(L, hwRound(gear^.X));
  1480         else
  1978         else
  1481             begin
  1979             begin
  1482             lua_pushnil(L);
  1980             lua_pushnil(L);
  1483             lua_pushnil(L)
  1981             lua_pushnil(L)
  1484             end;
  1982             end;
       
  1983         end
       
  1984     else
       
  1985         begin
       
  1986         lua_pushnil(L);
       
  1987         lua_pushnil(L)
  1485         end;
  1988         end;
  1486     lc_getgearposition:= 2;
  1989     lc_getgearposition:= 2;
  1487 end;
  1990 end;
  1488 
  1991 
  1489 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
  1992 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
  1490 var gear: PGear;
  1993 var gear: PGear;
  1491     col: boolean;
  1994     col: boolean;
  1492     x, y: LongInt;
  1995     x, y: LongInt;
  1493 begin
  1996 begin
  1494     if lua_gettop(L) <> 3 then
  1997     if CheckLuaParamCount(L, 3, 'SetGearPosition', 'gearUid, x, y') then
  1495         LuaParameterCountError('SetGearPosition', 'gearUid, x, y', lua_gettop(L))
       
  1496     else
       
  1497         begin
  1998         begin
  1498         gear:= GearByUID(lua_tointeger(L, 1));
  1999         gear:= GearByUID(lua_tointeger(L, 1));
  1499         if gear <> nil then
  2000         if gear <> nil then
  1500             begin
  2001             begin
  1501             col:= gear^.CollisionIndex >= 0;
  2002             col:= gear^.CollisionIndex >= 0;
  1514 end;
  2015 end;
  1515 
  2016 
  1516 function lc_getgeartarget(L : Plua_State) : LongInt; Cdecl;
  2017 function lc_getgeartarget(L : Plua_State) : LongInt; Cdecl;
  1517 var gear: PGear;
  2018 var gear: PGear;
  1518 begin
  2019 begin
  1519     if lua_gettop(L) <> 1 then
  2020     if CheckLuaParamCount(L, 1, 'GetGearTarget', 'gearUid') then
  1520         begin
       
  1521         LuaParameterCountError('GetGearTarget', 'gearUid', lua_gettop(L));
       
  1522         lua_pushnil(L);
       
  1523         lua_pushnil(L)
       
  1524         end
       
  1525     else
       
  1526         begin
  2021         begin
  1527         gear:= GearByUID(lua_tointeger(L, 1));
  2022         gear:= GearByUID(lua_tointeger(L, 1));
  1528         if gear <> nil then
  2023         if gear <> nil then
  1529             begin
  2024             begin
  1530             lua_pushinteger(L, gear^.Target.X);
  2025             lua_pushinteger(L, gear^.Target.X);
  1533         else
  2028         else
  1534             begin
  2029             begin
  1535             lua_pushnil(L);
  2030             lua_pushnil(L);
  1536             lua_pushnil(L)
  2031             lua_pushnil(L)
  1537             end
  2032             end
       
  2033         end
       
  2034     else
       
  2035         begin
       
  2036         lua_pushnil(L);
       
  2037         lua_pushnil(L)
  1538         end;
  2038         end;
  1539     lc_getgeartarget:= 2;
  2039     lc_getgeartarget:= 2;
  1540 end;
  2040 end;
  1541 
  2041 
  1542 function lc_setgeartarget(L : Plua_State) : LongInt; Cdecl;
  2042 function lc_setgeartarget(L : Plua_State) : LongInt; Cdecl;
  1543 var gear: PGear;
  2043 var gear: PGear;
  1544 begin
  2044 begin
  1545     if lua_gettop(L) <> 3 then
  2045     if CheckLuaParamCount(L, 3, 'SetGearTarget', 'gearUid, x, y') then
  1546         LuaParameterCountError('SetGearTarget', 'gearUid, x, y', lua_gettop(L))
       
  1547     else
       
  1548         begin
  2046         begin
  1549         gear:= GearByUID(lua_tointeger(L, 1));
  2047         gear:= GearByUID(lua_tointeger(L, 1));
  1550         if gear <> nil then
  2048         if gear <> nil then
  1551             begin
  2049             begin
  1552             gear^.Target.X:= lua_tointeger(L, 2);
  2050             gear^.Target.X:= lua_tointeger(L, 2);
  1558 
  2056 
  1559 function lc_getgearvelocity(L : Plua_State) : LongInt; Cdecl;
  2057 function lc_getgearvelocity(L : Plua_State) : LongInt; Cdecl;
  1560 var gear: PGear;
  2058 var gear: PGear;
  1561 var t: LongInt;
  2059 var t: LongInt;
  1562 begin
  2060 begin
  1563     if lua_gettop(L) <> 1 then
  2061     if CheckLuaParamCount(L, 1, 'GetGearVelocity', 'gearUid') then
  1564         begin
       
  1565         LuaParameterCountError('GetGearVelocity', 'gearUid', lua_gettop(L));
       
  1566         lua_pushnil(L);
       
  1567         lua_pushnil(L)
       
  1568         end
       
  1569     else
       
  1570         begin
  2062         begin
  1571         gear:= GearByUID(lua_tointeger(L, 1));
  2063         gear:= GearByUID(lua_tointeger(L, 1));
  1572         if gear <> nil then
  2064         if gear <> nil then
  1573             begin
  2065             begin
  1574             t:= hwRound(gear^.dX * 1000000);
  2066             t:= hwRound(gear^.dX * 1000000);
  1575             // gear dX determines hog orientation
  2067             // gear dX determines hog orientation
  1576             if (gear^.dX.isNegative) and (t = 0) then t:= -1;
  2068             if (gear^.dX.isNegative) and (t = 0) then t:= -1;
  1577             lua_pushinteger(L, t);
  2069             lua_pushinteger(L, t);
  1578             lua_pushinteger(L, hwRound(gear^.dY * 1000000))
  2070             lua_pushinteger(L, hwRound(gear^.dY * 1000000))
  1579             end
  2071             end
       
  2072         end
       
  2073     else
       
  2074         begin
       
  2075         lua_pushnil(L);
       
  2076         lua_pushnil(L);
  1580         end;
  2077         end;
  1581     lc_getgearvelocity:= 2;
  2078     lc_getgearvelocity:= 2;
  1582 end;
  2079 end;
  1583 
  2080 
  1584 function lc_setgearvelocity(L : Plua_State) : LongInt; Cdecl;
  2081 function lc_setgearvelocity(L : Plua_State) : LongInt; Cdecl;
  1585 var gear: PGear;
  2082 var gear: PGear;
  1586 begin
  2083 begin
  1587     if lua_gettop(L) <> 3 then
  2084     if CheckLuaParamCount(L, 3, 'SetGearVelocity', 'gearUid, dx, dy') then
  1588         LuaParameterCountError('SetGearVelocity', 'gearUid, dx, dy', lua_gettop(L))
       
  1589     else
       
  1590         begin
  2085         begin
  1591         gear:= GearByUID(lua_tointeger(L, 1));
  2086         gear:= GearByUID(lua_tointeger(L, 1));
  1592         if gear <> nil then
  2087         if gear <> nil then
  1593             begin
  2088             begin
  1594             gear^.dX:= int2hwFloat(lua_tointeger(L, 2)) / 1000000;
  2089             gear^.dX:= int2hwFloat(lua_tointeger(L, 2)) / 1000000;
  1599     lc_setgearvelocity:= 0
  2094     lc_setgearvelocity:= 0
  1600 end;
  2095 end;
  1601 
  2096 
  1602 function lc_setzoom(L : Plua_State) : LongInt; Cdecl;
  2097 function lc_setzoom(L : Plua_State) : LongInt; Cdecl;
  1603 begin
  2098 begin
  1604     if lua_gettop(L) <> 1 then
  2099     if CheckLuaParamCount(L, 1, 'SetZoom', 'zoomLevel') then
  1605         LuaParameterCountError('SetZoom', 'zoomLevel', lua_gettop(L))
       
  1606     else
       
  1607         begin
  2100         begin
  1608         ZoomValue:= lua_tonumber(L, 1);
  2101         ZoomValue:= lua_tonumber(L, 1);
  1609         if ZoomValue < cMaxZoomLevel then
  2102         if ZoomValue < cMaxZoomLevel then
  1610             ZoomValue:= cMaxZoomLevel;
  2103             ZoomValue:= cMaxZoomLevel;
  1611         if ZoomValue > cMinZoomLevel then
  2104         if ZoomValue > cMinZoomLevel then
  1614     lc_setzoom:= 0
  2107     lc_setzoom:= 0
  1615 end;
  2108 end;
  1616 
  2109 
  1617 function lc_getzoom(L : Plua_State) : LongInt; Cdecl;
  2110 function lc_getzoom(L : Plua_State) : LongInt; Cdecl;
  1618 begin
  2111 begin
  1619     if lua_gettop(L) <> 0 then
  2112     if CheckLuaParamCount(L, 0, 'GetZoom', '') then
  1620         begin
  2113         lua_pushnumber(L, ZoomValue)
  1621         LuaParameterCountError('GetZoom', '', lua_gettop(L));
  2114     else
  1622         lua_pushnil(L)
  2115         lua_pushnil(L);
  1623         end
       
  1624     else
       
  1625         lua_pushnumber(L, ZoomValue);
       
  1626     lc_getzoom:= 1
  2116     lc_getzoom:= 1
  1627 end;
  2117 end;
  1628 
  2118 
  1629 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
  2119 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
  1630 var np: LongInt;
  2120 var np, at: LongInt;
  1631 begin
  2121 const
  1632     np:= lua_gettop(L);
  2122     call = 'SetAmmo';
  1633     if (np < 4) or (np > 5) then
  2123     params = 'ammoType, count, probability, delay [, numberInCrate]';
  1634         LuaParameterCountError('SetAmmo', 'ammoType, count, probability, delay[, numberInCrate]', lua_gettop(L))
  2124 begin
  1635     else if np = 4 then
  2125     if CheckAndFetchParamCount(L, 4, 5, call, params, np) then
  1636         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
  2126         begin
  1637     else
  2127         at:= LuaToAmmoTypeOrd(L, 1, call, params);
  1638         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
  2128         if at >= 0 then
       
  2129             begin
       
  2130             if np = 4 then
       
  2131                 ScriptSetAmmo(TAmmoType(at), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
       
  2132             else
       
  2133                 ScriptSetAmmo(TAmmoType(at), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
       
  2134             end;
       
  2135         end;
  1639     lc_setammo:= 0
  2136     lc_setammo:= 0
  1640 end;
  2137 end;
  1641 
  2138 
  1642 function lc_setammodelay(L : Plua_State) : LongInt; Cdecl;
  2139 function lc_setammodelay(L : Plua_State) : LongInt; Cdecl;
  1643 var np: LongInt;
  2140 var at: LongInt;
  1644 begin
  2141 const
  1645     np:= lua_gettop(L);
  2142     call = 'SetAmmoDelay';
  1646     if (np <> 2) then
  2143     params = 'ammoType, delay';
  1647         LuaParameterCountError('SetAmmoDelay', 'ammoType, delay', lua_gettop(L))
  2144 begin
  1648     else
  2145     if CheckLuaParamCount(L, 2, call, params) then
  1649         ScriptSetAmmoDelay(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2));
  2146         begin
       
  2147         at:= LuaToAmmoTypeOrd(L, 1, call, params);
       
  2148         if at >= 0 then
       
  2149             ScriptSetAmmoDelay(TAmmoType(at), lua_tointeger(L, 2));
       
  2150         end;
  1650     lc_setammodelay:= 0
  2151     lc_setammodelay:= 0
  1651 end;
  2152 end;
  1652 
  2153 
  1653 function lc_setammostore(L : Plua_State) : LongInt; Cdecl;
  2154 function lc_setammostore(L : Plua_State) : LongInt; Cdecl;
  1654 var np: LongInt;
  2155 begin
  1655 begin
  2156     if CheckLuaParamCount(L, 4, 'SetAmmoStore', 'loadouts, probabilities, delays, reinforments') then
  1656     np:= lua_gettop(L);
       
  1657     if (np <> 4) then
       
  1658         LuaParameterCountError('SetAmmoStore', 'loadouts, probabilities, delays, reinforments', lua_gettop(L))
       
  1659     else
       
  1660         begin
  2157         begin
  1661         ScriptAmmoLoadout:= lua_tostring(L, 1);
  2158         ScriptAmmoLoadout:= lua_tostring(L, 1);
  1662         ScriptAmmoProbability:= lua_tostring(L, 2);
  2159         ScriptAmmoProbability:= lua_tostring(L, 2);
  1663         ScriptAmmoDelay:= lua_tostring(L, 3);
  2160         ScriptAmmoDelay:= lua_tostring(L, 3);
  1664         ScriptAmmoReinforcement:= lua_tostring(L, 4);
  2161         ScriptAmmoReinforcement:= lua_tostring(L, 4);
  1667 end;
  2164 end;
  1668 
  2165 
  1669 function lc_getrandom(L : Plua_State) : LongInt; Cdecl;
  2166 function lc_getrandom(L : Plua_State) : LongInt; Cdecl;
  1670 var m : LongInt;
  2167 var m : LongInt;
  1671 begin
  2168 begin
  1672     if lua_gettop(L) <> 1 then
  2169     if CheckLuaParamCount(L, 1, 'GetRandom', 'number') then
  1673         begin
       
  1674         LuaParameterCountError('GetRandom', 'number', lua_gettop(L));
       
  1675         lua_pushnil(L); // return value on stack (nil)
       
  1676         end
       
  1677     else
       
  1678         begin
  2170         begin
  1679         m:= lua_tointeger(L, 1);
  2171         m:= lua_tointeger(L, 1);
  1680         if (m > 0) then
  2172         if (m > 0) then
  1681             lua_pushinteger(L, GetRandom(m))
  2173             lua_pushinteger(L, GetRandom(m))
  1682         else
  2174         else
  1683             begin
  2175             begin
  1684             LuaError('Lua: Tried to pass 0 to GetRandom!');
  2176             LuaError('Lua: Tried to pass 0 to GetRandom!');
  1685             lua_pushnil(L);
  2177             lua_pushnil(L);
  1686             end
  2178             end
  1687         end;
  2179         end
       
  2180     else
       
  2181         lua_pushnil(L); // return value on stack (nil)
  1688     lc_getrandom:= 1
  2182     lc_getrandom:= 1
  1689 end;
  2183 end;
  1690 
  2184 
  1691 function lc_setwind(L : Plua_State) : LongInt; Cdecl;
  2185 function lc_setwind(L : Plua_State) : LongInt; Cdecl;
  1692 begin
  2186 begin
  1693     if lua_gettop(L) <> 1 then
  2187     if CheckLuaParamCount(L, 1, 'SetWind', 'windSpeed') then
  1694         LuaParameterCountError('SetWind', 'windSpeed', lua_gettop(L))
       
  1695     else
       
  1696         begin
  2188         begin
  1697         cWindSpeed:= int2hwfloat(lua_tointeger(L, 1)) / 100 * cMaxWindSpeed;
  2189         cWindSpeed:= int2hwfloat(lua_tointeger(L, 1)) / 100 * cMaxWindSpeed;
  1698         cWindSpeedf:= SignAs(cWindSpeed,cWindSpeed).QWordValue / SignAs(_1,_1).QWordValue;
  2190         cWindSpeedf:= SignAs(cWindSpeed,cWindSpeed).QWordValue / SignAs(_1,_1).QWordValue;
  1699         if cWindSpeed.isNegative then
  2191         if cWindSpeed.isNegative then
  1700             CWindSpeedf := -cWindSpeedf;
  2192             CWindSpeedf := -cWindSpeedf;
  1703     lc_setwind:= 0
  2195     lc_setwind:= 0
  1704 end;
  2196 end;
  1705 
  2197 
  1706 function lc_getdatapath(L : Plua_State) : LongInt; Cdecl;
  2198 function lc_getdatapath(L : Plua_State) : LongInt; Cdecl;
  1707 begin
  2199 begin
  1708     if lua_gettop(L) <> 0 then
  2200     if CheckLuaParamCount(L, 0, 'GetDataPath', '') then
  1709         begin
  2201         lua_pushstring(L, str2pchar(cPathz[ptData]))
  1710         LuaParameterCountError('GetDataPath', '', lua_gettop(L));
  2202     else
  1711         lua_pushnil(L);
  2203         lua_pushnil(L);
  1712         end
       
  1713     else
       
  1714         lua_pushstring(L, str2pchar(cPathz[ptData]));
       
  1715     lc_getdatapath:= 1
  2204     lc_getdatapath:= 1
  1716 end;
  2205 end;
  1717 
  2206 
  1718 function lc_getuserdatapath(L : Plua_State) : LongInt; Cdecl;
  2207 function lc_getuserdatapath(L : Plua_State) : LongInt; Cdecl;
  1719 begin
  2208 begin
  1720     if lua_gettop(L) <> 0 then
  2209     if CheckLuaParamCount(L, 0, 'GetUserDataPath', '') then
  1721         begin
  2210         lua_pushstring(L, str2pchar(cPathz[ptData]))
  1722         LuaParameterCountError('GetUserDataPath', '', lua_gettop(L));
  2211     else
  1723         lua_pushnil(L);
  2212         lua_pushnil(L);
  1724         end
       
  1725     else
       
  1726         lua_pushstring(L, str2pchar(cPathz[ptData]));
       
  1727     lc_getuserdatapath:= 1
  2213     lc_getuserdatapath:= 1
  1728 end;
  2214 end;
  1729 
  2215 
  1730 function lc_maphasborder(L : Plua_State) : LongInt; Cdecl;
  2216 function lc_maphasborder(L : Plua_State) : LongInt; Cdecl;
  1731 begin
  2217 begin
  1732     if lua_gettop(L) <> 0 then
  2218     if CheckLuaParamCount(L, 0, 'MapHasBorder', '') then
  1733         begin
  2219         lua_pushboolean(L, hasBorder)
  1734         LuaParameterCountError('MapHasBorder', '', lua_gettop(L));
  2220     else
  1735         lua_pushnil(L);
  2221         lua_pushnil(L);
  1736         end
       
  1737     else
       
  1738         lua_pushboolean(L, hasBorder);
       
  1739     lc_maphasborder:= 1
  2222     lc_maphasborder:= 1
  1740 end;
  2223 end;
  1741 
  2224 
  1742 function lc_getgearradius(L : Plua_State) : LongInt; Cdecl;
  2225 function lc_getgearradius(L : Plua_State) : LongInt; Cdecl;
  1743 var gear : PGear;
  2226 var gear : PGear;
  1744 begin
  2227 begin
  1745     if lua_gettop(L) <> 1 then
  2228     if CheckLuaParamCount(L, 1, 'GetGearRadius', 'gearUid') then
  1746         begin
  2229         begin
  1747         LuaParameterCountError('GetGearRadius', 'gearUid', lua_gettop(L));
  2230         gear:= GearByUID(lua_tointeger(L, 1));
       
  2231         if gear <> nil then
       
  2232             lua_pushinteger(L, gear^.Radius)
       
  2233         else
       
  2234             lua_pushnil(L);
       
  2235         end
       
  2236     else
  1748         lua_pushnil(L); // return value on stack (nil)
  2237         lua_pushnil(L); // return value on stack (nil)
  1749         end
       
  1750     else
       
  1751         begin
       
  1752         gear:= GearByUID(lua_tointeger(L, 1));
       
  1753         if gear <> nil then
       
  1754             lua_pushinteger(L, gear^.Radius)
       
  1755         else
       
  1756             lua_pushnil(L);
       
  1757         end;
       
  1758     lc_getgearradius:= 1
  2238     lc_getgearradius:= 1
  1759 end;
  2239 end;
  1760 
  2240 
  1761 function lc_gethoghat(L : Plua_State): LongInt; Cdecl;
  2241 function lc_gethoghat(L : Plua_State): LongInt; Cdecl;
  1762 var gear : PGear;
  2242 var gear : PGear;
  1763 begin
  2243 begin
  1764     if lua_gettop(L) <> 1 then
  2244     if CheckLuaParamCount(L, 1, 'GetHogHat', 'gearUid') then
  1765         LuaParameterCountError('GetHogHat', 'gearUid', lua_gettop(L))
  2245         begin
  1766     else begin
       
  1767         gear := GearByUID(lua_tointeger(L, 1));
  2246         gear := GearByUID(lua_tointeger(L, 1));
  1768         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  2247         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
  1769             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Hat))
  2248             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Hat))
  1770         else
  2249         else
  1771             lua_pushnil(L);
  2250             lua_pushnil(L);
  1772     end;
  2251         end
       
  2252     else
       
  2253         lua_pushnil(L);
  1773     lc_gethoghat := 1;
  2254     lc_gethoghat := 1;
  1774 end;
  2255 end;
  1775 
  2256 
  1776 function lc_sethoghat(L : Plua_State) : LongInt; Cdecl;
  2257 function lc_sethoghat(L : Plua_State) : LongInt; Cdecl;
  1777 var gear : PGear;
  2258 var gear : PGear;
  1778     hat: ShortString;
  2259     hat: ShortString;
  1779 begin
  2260 begin
  1780     if lua_gettop(L) <> 2 then
  2261     if CheckLuaParamCount(L, 2, 'SetHogHat', 'gearUid, hat') then
  1781         begin
       
  1782         LuaParameterCountError('SetHogHat', 'gearUid, hat', lua_gettop(L));
       
  1783         lua_pushnil(L)
       
  1784         end
       
  1785     else
       
  1786         begin
  2262         begin
  1787         gear:= GearByUID(lua_tointeger(L, 1));
  2263         gear:= GearByUID(lua_tointeger(L, 1));
  1788         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  2264         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1789             begin
  2265             begin
  1790             hat:= lua_tostring(L, 2);
  2266             hat:= lua_tostring(L, 2);
  1797             end
  2273             end
  1798         end;
  2274         end;
  1799     lc_sethoghat:= 0;
  2275     lc_sethoghat:= 0;
  1800 end;
  2276 end;
  1801 
  2277 
       
  2278 function lc_placesprite(L : Plua_State) : LongInt; Cdecl;
       
  2279 var spr   : TSprite;
       
  2280     lf    : Word;
       
  2281     tint  : LongWord;
       
  2282     i, n : LongInt;
       
  2283     placed, behind, flipHoriz, flipVert : boolean;
       
  2284 const
       
  2285     call = 'PlaceSprite';
       
  2286     params = 'x, y, sprite, frameIdx, tint, behind, flipHoriz, flipVert, [, landFlag, ... ]';
       
  2287 begin
       
  2288     placed:= false;
       
  2289     if CheckAndFetchLuaParamMinCount(L, 4, call, params, n) then
       
  2290         begin
       
  2291         if not lua_isnoneornil(L, 5) then
       
  2292 	        tint := lua_tointeger(L, 5)
       
  2293         else tint := $FFFFFFFF;
       
  2294         if not lua_isnoneornil(L, 6) then
       
  2295 	        behind := lua_toboolean(L, 6)
       
  2296         else behind := false;
       
  2297         if not lua_isnoneornil(L, 7) then
       
  2298 	        flipHoriz := lua_toboolean(L, 7)
       
  2299         else flipHoriz := false;
       
  2300         if not lua_isnoneornil(L, 8) then
       
  2301 	        flipVert := lua_toboolean(L, 8)
       
  2302         else flipVert := false;
       
  2303         lf:= 0;
       
  2304 
       
  2305         // accept any amount of landflags, loop is never executed if n<9
       
  2306         for i:= 9 to n do
       
  2307             lf:= lf or lua_tointeger(L, i);
       
  2308 
       
  2309         n:= LuaToSpriteOrd(L, 3, call, params);
       
  2310         if n >= 0 then
       
  2311             begin
       
  2312             spr:= TSprite(n);
       
  2313             if SpritesData[spr].Surface = nil then
       
  2314                 LuaError(call + ': ' + EnumToStr(spr) + ' cannot be placed! (required information not loaded)' )
       
  2315             else
       
  2316                 placed:= ForcePlaceOnLand(
       
  2317                     lua_tointeger(L, 1) - SpritesData[spr].Width div 2,
       
  2318                     lua_tointeger(L, 2) - SpritesData[spr].Height div 2,
       
  2319                     spr, lua_tointeger(L, 4), lf, tint, behind, flipHoriz, flipVert);
       
  2320             end;
       
  2321         end;
       
  2322 
       
  2323     lua_pushboolean(L, placed);
       
  2324     lc_placesprite:= 1
       
  2325 end;
       
  2326 
       
  2327 function lc_erasesprite(L : Plua_State) : LongInt; Cdecl;
       
  2328 var spr   : TSprite;
       
  2329     lf    : Word;
       
  2330     i, n : LongInt;
       
  2331     eraseOnLFMatch, onlyEraseLF, flipHoriz, flipVert : boolean;
       
  2332 const
       
  2333     call = 'EraseSprite';
       
  2334     params = 'x, y, sprite, frameIdx, eraseOnLFMatch, onlyEraseLF, flipHoriz, flipVert, [, landFlag, ... ]';
       
  2335 begin
       
  2336     if CheckAndFetchLuaParamMinCount(L, 4, call, params, n) then
       
  2337         begin
       
  2338         if not lua_isnoneornil(L, 5) then
       
  2339 	        eraseOnLFMatch := lua_toboolean(L, 5)
       
  2340         else eraseOnLFMatch := false;
       
  2341         if not lua_isnoneornil(L, 6) then
       
  2342 	        onlyEraseLF := lua_toboolean(L, 6)
       
  2343         else onlyEraseLF := false;
       
  2344         if not lua_isnoneornil(L, 7) then
       
  2345 	        flipHoriz := lua_toboolean(L, 7)
       
  2346         else flipHoriz := false;
       
  2347         if not lua_isnoneornil(L, 8) then
       
  2348 	        flipVert := lua_toboolean(L, 8)
       
  2349         else flipVert := false;
       
  2350         lf:= 0;
       
  2351 
       
  2352         // accept any amount of landflags, loop is never executed if n<9
       
  2353         for i:= 9 to n do
       
  2354             lf:= lf or lua_tointeger(L, i);
       
  2355 
       
  2356         n:= LuaToSpriteOrd(L, 3, call, params);
       
  2357         if n >= 0 then
       
  2358             begin
       
  2359             spr:= TSprite(n);
       
  2360             if SpritesData[spr].Surface = nil then
       
  2361                 LuaError(call + ': ' + EnumToStr(spr) + ' cannot be placed! (required information not loaded)' )
       
  2362             else
       
  2363                 EraseLand(
       
  2364                     lua_tointeger(L, 1) - SpritesData[spr].Width div 2,
       
  2365                     lua_tointeger(L, 2) - SpritesData[spr].Height div 2,
       
  2366                     spr, lua_tointeger(L, 4), lf, eraseOnLFMatch, onlyEraseLF, flipHoriz, flipVert);
       
  2367             end;
       
  2368         end;
       
  2369     lc_erasesprite:= 0
       
  2370 end;
       
  2371 
  1802 function lc_placegirder(L : Plua_State) : LongInt; Cdecl;
  2372 function lc_placegirder(L : Plua_State) : LongInt; Cdecl;
  1803 begin
  2373 var placed: boolean;
  1804     if lua_gettop(L) <> 3 then
  2374 begin
  1805         LuaParameterCountError('PlaceGirder', 'x, y, state', lua_gettop(L))
  2375     placed:= false;
  1806     else
  2376     if CheckLuaParamCount(L, 3, 'PlaceGirder', 'x, y, frameIdx') then
  1807         TryPlaceOnLand(
  2377         placed:= TryPlaceOnLandSimple(
  1808             lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
  2378             lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
  1809             lua_tointeger(L, 2) - SpritesData[sprAmGirder].Height div 2,
  2379             lua_tointeger(L, 2) - SpritesData[sprAmGirder].Height div 2,
  1810             sprAmGirder, lua_tointeger(L, 3), true, false);
  2380             sprAmGirder, lua_tointeger(L, 3), true, false);
  1811     lc_placegirder:= 0
  2381 
       
  2382     lua_pushboolean(L, placed);
       
  2383     lc_placegirder:= 1
  1812 end;
  2384 end;
  1813 
  2385 
  1814 function lc_getcurammotype(L : Plua_State): LongInt; Cdecl;
  2386 function lc_getcurammotype(L : Plua_State): LongInt; Cdecl;
  1815 begin
  2387 begin
  1816     if lua_gettop(L) <> 0 then
  2388     if (CurrentHedgehog <> nil) and (CheckLuaParamCount(L, 0, 'GetCurAmmoType', '')) then
  1817         LuaParameterCountError('GetCurAmmoType', '', lua_gettop(L))
  2389         lua_pushinteger(L, ord(CurrentHedgehog^.CurAmmoType))
  1818     else
  2390     else
  1819         lua_pushinteger(L, ord(CurrentHedgehog^.CurAmmoType));
  2391         lua_pushinteger(L, ord(amNothing));
  1820     lc_getcurammotype := 1;
  2392     lc_getcurammotype := 1;
  1821 end;
  2393 end;
  1822 
  2394 
  1823 function lc_savecampaignvar(L : Plua_State): LongInt; Cdecl;
  2395 function lc_savecampaignvar(L : Plua_State): LongInt; Cdecl;
  1824 begin
  2396 begin
  1825     if lua_gettop(L) <> 2 then
  2397     if CheckLuaParamCount(L, 2, 'SaveCampaignVar', 'varname, value') then
  1826         LuaParameterCountError('SaveCampaignVar', 'varname, value', lua_gettop(L))
       
  1827     else begin
       
  1828         SendIPC('V!' + lua_tostring(L, 1) + ' ' + lua_tostring(L, 2) + #0);
  2398         SendIPC('V!' + lua_tostring(L, 1) + ' ' + lua_tostring(L, 2) + #0);
  1829     end;
       
  1830     lc_savecampaignvar := 0;
  2399     lc_savecampaignvar := 0;
  1831 end;
  2400 end;
  1832 
  2401 
  1833 function lc_getcampaignvar(L : Plua_State): LongInt; Cdecl;
  2402 function lc_getcampaignvar(L : Plua_State): LongInt; Cdecl;
  1834 begin
  2403 begin
  1835     if (lua_gettop(L) <> 1) then
  2404     if CheckLuaParamCount(L, 1, 'GetCampaignVar', 'varname') then
  1836         LuaParameterCountError('GetCampaignVar', 'varname', lua_gettop(L))
       
  1837     else
       
  1838         SendIPCAndWaitReply('V?' + lua_tostring(L, 1) + #0);
  2405         SendIPCAndWaitReply('V?' + lua_tostring(L, 1) + #0);
  1839     lua_pushstring(L, str2pchar(CampaignVariable));
  2406     lua_pushstring(L, str2pchar(CampaignVariable));
  1840     lc_getcampaignvar := 1;
  2407     lc_getcampaignvar := 1;
  1841 end;
  2408 end;
  1842 
  2409 
  1843 function lc_hidehog(L: Plua_State): LongInt; Cdecl;
  2410 function lc_hidehog(L: Plua_State): LongInt; Cdecl;
  1844 var gear: PGear;
  2411 var gear: PGear;
  1845 begin
  2412 begin
  1846     if lua_gettop(L) <> 1 then
  2413     if CheckLuaParamCount(L, 1, 'HideHog', 'gearUid') then
  1847         LuaParameterCountError('HideHog', 'gearUid', lua_gettop(L))
       
  1848     else
       
  1849         begin
  2414         begin
  1850         gear:= GearByUID(lua_tointeger(L, 1));
  2415         gear:= GearByUID(lua_tointeger(L, 1));
  1851         HideHog(gear^.hedgehog)
  2416         HideHog(gear^.hedgehog)
  1852         end;
  2417         end;
  1853     lc_hidehog := 0;
  2418     lc_hidehog := 0;
  1855 
  2420 
  1856 function lc_restorehog(L: Plua_State): LongInt; Cdecl;
  2421 function lc_restorehog(L: Plua_State): LongInt; Cdecl;
  1857 var i, h: LongInt;
  2422 var i, h: LongInt;
  1858     uid: LongWord;
  2423     uid: LongWord;
  1859 begin
  2424 begin
  1860     if lua_gettop(L) <> 1 then
  2425     if CheckLuaParamCount(L, 1, 'RestoreHog', 'gearUid') then
  1861         LuaParameterCountError('RestoreHog', 'gearUid', lua_gettop(L))
       
  1862     else
       
  1863         begin
  2426         begin
  1864         uid:= LongWord(lua_tointeger(L, 1));
  2427         uid:= LongWord(lua_tointeger(L, 1));
  1865         if TeamsCount > 0 then
  2428         if TeamsCount > 0 then
  1866             for i:= 0 to Pred(TeamsCount) do
  2429             for i:= 0 to Pred(TeamsCount) do
  1867                 for h:= 0 to cMaxHHIndex do
  2430                 for h:= 0 to cMaxHHIndex do
  1876 
  2439 
  1877 // boolean TestRectForObstacle(x1, y1, x2, y2, landOnly)
  2440 // boolean TestRectForObstacle(x1, y1, x2, y2, landOnly)
  1878 function lc_testrectforobstacle(L : Plua_State) : LongInt; Cdecl;
  2441 function lc_testrectforobstacle(L : Plua_State) : LongInt; Cdecl;
  1879 var rtn: Boolean;
  2442 var rtn: Boolean;
  1880 begin
  2443 begin
  1881     if lua_gettop(L) <> 5 then
  2444     if CheckLuaParamCount(L, 5, 'TestRectForObstacle', 'x1, y1, x2, y2, landOnly') then
  1882         begin
  2445         begin
  1883         LuaParameterCountError('TestRectForObstacle', 'x1, y1, x2, y2, landOnly', lua_gettop(L));
  2446         rtn:= TestRectangleForObstacle(
  1884         lua_pushnil(L); // return value on stack (nil)
       
  1885         end
       
  1886     else
       
  1887         begin
       
  1888         rtn:= TestRectancleForObstacle(
       
  1889                     lua_tointeger(L, 1),
  2447                     lua_tointeger(L, 1),
  1890                     lua_tointeger(L, 2),
  2448                     lua_tointeger(L, 2),
  1891                     lua_tointeger(L, 3),
  2449                     lua_tointeger(L, 3),
  1892                     lua_tointeger(L, 4),
  2450                     lua_tointeger(L, 4),
  1893                     lua_toboolean(L, 5)
  2451                     lua_toboolean(L, 5)
  1894                     );
  2452                     );
  1895         lua_pushboolean(L, rtn);
  2453         lua_pushboolean(L, rtn);
  1896         end;
  2454         end
       
  2455     else
       
  2456         lua_pushnil(L); // return value on stack (nil)
  1897     lc_testrectforobstacle:= 1
  2457     lc_testrectforobstacle:= 1
  1898 end;
  2458 end;
  1899 
  2459 
  1900 
  2460 
  1901 function lc_setaihintsongear(L : Plua_State) : LongInt; Cdecl;
  2461 function lc_getgravity(L : Plua_State) : LongInt; Cdecl;
       
  2462 begin
       
  2463     if CheckLuaParamCount(L, 0, 'GetGravity', '') then
       
  2464         lua_pushinteger(L, hwRound(SignAs(_0_5, cGravity) + (cGravity * 50 / cMaxWindSpeed)));
       
  2465     lc_getgravity:= 1
       
  2466 end;
       
  2467 
       
  2468 function lc_setgravity(L : Plua_State) : LongInt; Cdecl;
       
  2469 begin
       
  2470     if CheckLuaParamCount(L, 1, 'SetGravity', 'percent') then
       
  2471         begin
       
  2472         cGravity:= _0_02 * lua_tointeger(L, 1) * cMaxWindSpeed;
       
  2473         cGravityf:= 0.00025 * lua_tointeger(L, 1) * 0.02
       
  2474         end;
       
  2475     lc_setgravity:= 0
       
  2476 end;
       
  2477 
       
  2478 function lc_setwaterline(L : Plua_State) : LongInt; Cdecl;
       
  2479 var iterator: PGear;
       
  2480 begin
       
  2481     if CheckLuaParamCount(L, 1, 'SetWaterLine', 'waterline') then
       
  2482         begin
       
  2483         cWaterLine:= lua_tointeger(L,1);
       
  2484         AllInactive:= false;
       
  2485         iterator:= GearsList;
       
  2486         while iterator <> nil do
       
  2487             begin
       
  2488             if not (iterator^.Kind in [gtPortal, gtAirAttack]) and (iterator^.Message and (gmAllStoppable or gmLJump or gmHJump) = 0) then
       
  2489                 begin
       
  2490                 iterator^.Active:= true;
       
  2491                 if iterator^.dY.QWordValue = 0 then iterator^.dY.isNegative:= false;
       
  2492                 iterator^.State:= iterator^.State or gstMoving;
       
  2493                 DeleteCI(iterator)
       
  2494                 end;
       
  2495             iterator:= iterator^.NextGear
       
  2496             end
       
  2497         end;
       
  2498     lc_setwaterline:= 0
       
  2499 end;
       
  2500 
       
  2501 function lc_setgearaihints(L : Plua_State) : LongInt; Cdecl;
  1902 var gear: PGear;
  2502 var gear: PGear;
  1903 begin
  2503 begin
  1904     if lua_gettop(L) <> 2 then
  2504     if CheckLuaParamCount(L, 2, 'SetAIHintOnGear', 'gearUid, aiHints') then
  1905         LuaParameterCountError('SetAIHintOnGear', 'gearUid, aiHints', lua_gettop(L))
       
  1906     else
       
  1907         begin
  2505         begin
  1908         gear:= GearByUID(lua_tointeger(L, 1));
  2506         gear:= GearByUID(lua_tointeger(L, 1));
  1909         if gear <> nil then
  2507         if gear <> nil then
  1910             gear^.aihints:= lua_tointeger(L, 2);
  2508             gear^.aihints:= lua_tointeger(L, 2);
  1911         end;
  2509         end;
  1912     lc_setaihintsongear:= 0
  2510     lc_setgearaihints:= 0
  1913 end;
  2511 end;
  1914 
  2512 
  1915 
  2513 
  1916 function lc_hedgewarsscriptload(L : Plua_State) : LongInt; Cdecl;
  2514 function lc_hedgewarsscriptload(L : Plua_State) : LongInt; Cdecl;
  1917 begin
  2515 begin
  1918     if lua_gettop(L) <> 1 then
  2516     if CheckLuaParamCount(L, 1, 'HedgewarsScriptLoad', 'scriptPath') then
  1919         begin
  2517         ScriptLoad(lua_tostring(L, 1))
  1920         LuaParameterCountError('HedgewarsScriptLoad', 'scriptPath', lua_gettop(L));
  2518     else
  1921         lua_pushnil(L)
  2519         lua_pushnil(L);
  1922         end
       
  1923     else
       
  1924         ScriptLoad(lua_tostring(L, 1));
       
  1925     lc_hedgewarsscriptload:= 0;
  2520     lc_hedgewarsscriptload:= 0;
  1926 end;
  2521 end;
  1927 
  2522 
  1928 
  2523 
  1929 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
  2524 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
  1930 begin
  2525 begin
  1931     if lua_gettop(L) <> 4 then
  2526     if CheckLuaParamCount(L, 4, 'DeclareAchievement', 'achievementId, teamname, location, value') then
  1932         LuaParameterCountError('DeclareAchievement', 'achievementId, teamname, location, value', lua_gettop(L))
       
  1933     else
       
  1934         declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
  2527         declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
  1935     lc_declareachievement:= 0
  2528     lc_declareachievement:= 0
       
  2529 end;
       
  2530 
       
  2531 
       
  2532 procedure ScriptFlushPoints();
       
  2533 begin
       
  2534     ParseCommand('draw ' + PointsBuffer, true, true);
       
  2535     PointsBuffer:= '';
       
  2536 end;
       
  2537 
       
  2538 
       
  2539 function lc_addPoint(L : Plua_State) : LongInt; Cdecl;
       
  2540 var np, param: LongInt;
       
  2541 begin
       
  2542     if CheckAndFetchParamCountRange(L, 2, 4, 'AddPoint', 'x, y [, width [, erase] ]', np) then
       
  2543         begin
       
  2544         // x
       
  2545         param:= LongInt(lua_tointeger(L,1));
       
  2546         PointsBuffer:= PointsBuffer + char((param shr 8) and $FF);
       
  2547         PointsBuffer:= PointsBuffer + char((param and $FF));
       
  2548         // y
       
  2549         param:= LongInt(lua_tointeger(L,2));
       
  2550         PointsBuffer:= PointsBuffer + char((param shr 8) and $FF);
       
  2551         PointsBuffer:= PointsBuffer + char((param and $FF));
       
  2552         // width
       
  2553         if np > 2 then
       
  2554             begin
       
  2555             param:= lua_tointeger(L,3);
       
  2556             param:= (param or $80);
       
  2557             // erase
       
  2558             if (np > 3) and lua_toboolean(L, 4) then
       
  2559                 param:= (param or $40);
       
  2560             PointsBuffer:= PointsBuffer + char(param);
       
  2561             end
       
  2562         // no width defined
       
  2563         else
       
  2564             PointsBuffer:= PointsBuffer + char(0);
       
  2565 
       
  2566         // flush before shortstring limit length is reached
       
  2567         if length(PointsBuffer) > 245 then
       
  2568             ScriptFlushPoints();
       
  2569         end;
       
  2570     lc_addPoint:= 0
       
  2571 end;
       
  2572 
       
  2573 
       
  2574 function lc_flushPoints(L : Plua_State) : LongInt; Cdecl;
       
  2575 begin
       
  2576     if CheckLuaParamCount(L, 0, 'FlushPoints', '') then
       
  2577         if length(PointsBuffer) > 0 then
       
  2578             ScriptFlushPoints();
       
  2579     lc_flushPoints:= 0
       
  2580 end;
       
  2581 
       
  2582 // stuff for lua tests
       
  2583 function lc_endluatest(L : Plua_State) : LongInt; Cdecl;
       
  2584 var rstring: shortstring;
       
  2585 const
       
  2586     call = 'EndLuaTest';
       
  2587     params = 'TEST_SUCCESSFUL or TEST_FAILED';
       
  2588 begin
       
  2589     if CheckLuaParamCount(L, 1, call, params) then
       
  2590         begin
       
  2591 
       
  2592         case lua_tointeger(L, 1) of
       
  2593             HaltTestSuccess : rstring:= 'Success';
       
  2594             HaltTestLuaError: rstring:= 'FAILED';
       
  2595         else
       
  2596             begin
       
  2597             LuaCallError('Parameter must be either ' + params, call, params);
       
  2598             exit(0);
       
  2599             end;
       
  2600         end;
       
  2601 
       
  2602         if cTestLua then
       
  2603             begin
       
  2604             WriteLnToConsole('Lua test finished, result: ' + rstring);
       
  2605             halt(lua_tointeger(L, 1));
       
  2606             end
       
  2607         else LuaError('Not in lua test mode, engine will keep running. Reported test result: ' + rstring);
       
  2608 
       
  2609         end;
       
  2610 
       
  2611     lc_endluatest:= 0;
  1936 end;
  2612 end;
  1937 ///////////////////
  2613 ///////////////////
  1938 
  2614 
  1939 procedure ScriptPrintStack;
  2615 procedure ScriptPrintStack;
  1940 var n, i : LongInt;
  2616 var n, i : LongInt;
  1985     lua_getglobal(luaState, Str2PChar(name));
  2661     lua_getglobal(luaState, Str2PChar(name));
  1986     ScriptGetString:= lua_tostring(luaState, -1);
  2662     ScriptGetString:= lua_tostring(luaState, -1);
  1987     lua_pop(luaState, 1);
  2663     lua_pop(luaState, 1);
  1988 end;
  2664 end;
  1989 
  2665 
  1990 procedure ScriptOnGameInit;
  2666 procedure ScriptOnPreviewInit;
  1991 var i, j, k: LongInt;
       
  1992 begin
  2667 begin
  1993 // not required if there is no script to run
  2668 // not required if there is no script to run
  1994 if not ScriptLoaded then
  2669 if not ScriptLoaded then
  1995     exit;
  2670     exit;
  1996 
  2671 
       
  2672 ScriptSetString('Seed', cSeed);
       
  2673 ScriptSetInteger('TemplateFilter', cTemplateFilter);
       
  2674 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
       
  2675 ScriptSetInteger('MapGen', ord(cMapGen));
       
  2676 ScriptSetInteger('MapFeatureSize', cFeatureSize);
       
  2677 
       
  2678 ScriptCall('onPreviewInit');
       
  2679 
       
  2680 // pop game variables
       
  2681 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
       
  2682 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
       
  2683 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
       
  2684 cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
       
  2685 cFeatureSize     := ScriptGetInteger('MapFeatureSize');
       
  2686 end;
       
  2687 
       
  2688 procedure ScriptOnGameInit;
       
  2689 var i, j, k: LongInt;
       
  2690 begin
       
  2691 // not required if there is no script to run
       
  2692 if not ScriptLoaded then
       
  2693     exit;
       
  2694 
  1997 // push game variables so they may be modified by the script
  2695 // push game variables so they may be modified by the script
       
  2696 ScriptSetInteger('CursorX', CursorPoint.X);
       
  2697 ScriptSetInteger('CursorY', CursorPoint.Y);
  1998 ScriptSetInteger('BorderColor', ExplosionBorderColor);
  2698 ScriptSetInteger('BorderColor', ExplosionBorderColor);
  1999 ScriptSetInteger('GameFlags', GameFlags);
  2699 ScriptSetInteger('GameFlags', GameFlags);
  2000 ScriptSetString('Seed', cSeed);
  2700 ScriptSetString('Seed', cSeed);
  2001 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2701 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2002 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2702 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2003 ScriptSetInteger('MapGen', cMapGen);
  2703 ScriptSetInteger('MapGen', ord(cMapGen));
       
  2704 ScriptSetInteger('MapFeatureSize', cFeatureSize);
  2004 ScriptSetInteger('ScreenHeight', cScreenHeight);
  2705 ScriptSetInteger('ScreenHeight', cScreenHeight);
  2005 ScriptSetInteger('ScreenWidth', cScreenWidth);
  2706 ScriptSetInteger('ScreenWidth', cScreenWidth);
  2006 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
  2707 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
  2007 ScriptSetInteger('CaseFreq', cCaseFactor);
  2708 ScriptSetInteger('CaseFreq', cCaseFactor);
  2008 ScriptSetInteger('HealthCaseProb', cHealthCaseProb);
  2709 ScriptSetInteger('HealthCaseProb', cHealthCaseProb);
  2016 ScriptSetInteger('Delay', cInactDelay);
  2717 ScriptSetInteger('Delay', cInactDelay);
  2017 ScriptSetInteger('Ready', cReadyDelay);
  2718 ScriptSetInteger('Ready', cReadyDelay);
  2018 ScriptSetInteger('SuddenDeathTurns', cSuddenDTurns);
  2719 ScriptSetInteger('SuddenDeathTurns', cSuddenDTurns);
  2019 ScriptSetInteger('WaterRise', cWaterRise);
  2720 ScriptSetInteger('WaterRise', cWaterRise);
  2020 ScriptSetInteger('HealthDecrease', cHealthDecrease);
  2721 ScriptSetInteger('HealthDecrease', cHealthDecrease);
       
  2722 ScriptSetInteger('GetAwayTime', cGetAwayTime);
  2021 ScriptSetString('Map', cMapName);
  2723 ScriptSetString('Map', cMapName);
  2022 
       
  2023 ScriptSetString('Theme', '');
  2724 ScriptSetString('Theme', '');
  2024 ScriptSetString('Goals', '');
  2725 ScriptSetString('Goals', '');
  2025 
  2726 
  2026 ScriptCall('onGameInit');
  2727 ScriptCall('onGameInit');
  2027 
  2728 
  2028 // pop game variables
  2729 // pop game variables
  2029 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2730 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2030 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2731 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2031 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2732 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2032 cMapGen          := ScriptGetInteger('MapGen');
  2733 cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
       
  2734 cFeatureSize     := ScriptGetInteger('MapFeatureSize');
  2033 GameFlags        := ScriptGetInteger('GameFlags');
  2735 GameFlags        := ScriptGetInteger('GameFlags');
  2034 cHedgehogTurnTime:= ScriptGetInteger('TurnTime');
  2736 cHedgehogTurnTime:= ScriptGetInteger('TurnTime');
  2035 cCaseFactor      := ScriptGetInteger('CaseFreq');
  2737 cCaseFactor      := ScriptGetInteger('CaseFreq');
  2036 cHealthCaseProb  := ScriptGetInteger('HealthCaseProb');
  2738 cHealthCaseProb  := ScriptGetInteger('HealthCaseProb');
  2037 cHealthCaseAmount:= ScriptGetInteger('HealthCaseAmount');
  2739 cHealthCaseAmount:= ScriptGetInteger('HealthCaseAmount');
  2044 cInactDelay      := ScriptGetInteger('Delay');
  2746 cInactDelay      := ScriptGetInteger('Delay');
  2045 cReadyDelay      := ScriptGetInteger('Ready');
  2747 cReadyDelay      := ScriptGetInteger('Ready');
  2046 cSuddenDTurns    := ScriptGetInteger('SuddenDeathTurns');
  2748 cSuddenDTurns    := ScriptGetInteger('SuddenDeathTurns');
  2047 cWaterRise       := ScriptGetInteger('WaterRise');
  2749 cWaterRise       := ScriptGetInteger('WaterRise');
  2048 cHealthDecrease  := ScriptGetInteger('HealthDecrease');
  2750 cHealthDecrease  := ScriptGetInteger('HealthDecrease');
       
  2751 cGetAwayTime     := ScriptGetInteger('GetAwayTime');
  2049 
  2752 
  2050 if cMapName <> ScriptGetString('Map') then
  2753 if cMapName <> ScriptGetString('Map') then
  2051     ParseCommand('map ' + ScriptGetString('Map'), true, true);
  2754     ParseCommand('map ' + ScriptGetString('Map'), true, true);
  2052 if ScriptGetString('Theme') <> '' then
  2755 if ScriptGetString('Theme') <> '' then
  2053     ParseCommand('theme ' + ScriptGetString('Theme'), true, true);
  2756     ParseCommand('theme ' + ScriptGetString('Theme'), true, true);
  2131 ret:= lua_load(luaState, @physfsReader, f, Str2PChar(s));
  2834 ret:= lua_load(luaState, @physfsReader, f, Str2PChar(s));
  2132 pfsClose(f);
  2835 pfsClose(f);
  2133 
  2836 
  2134 if ret <> 0 then
  2837 if ret <> 0 then
  2135     begin
  2838     begin
  2136     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  2839     LuaError('Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  2137     LuaError('Lua: ' + lua_tostring(luaState, -1));
  2840     LuaError(lua_tostring(luaState, -1));
  2138     end
  2841     end
  2139 else
  2842 else
  2140     begin
  2843     begin
  2141     WriteLnToConsole('Lua: ' + name + ' loaded');
  2844     WriteLnToConsole('Lua: ' + name + ' loaded');
  2142     // call the script file
  2845     // call the script file
  2143     lua_pcall(luaState, 0, 0, 0);
  2846     lua_pcall(luaState, 0, 0, 0);
  2144     ScriptLoaded:= true
  2847     ScriptLoaded:= true
  2145     end;
  2848     end;
  2146     hedgewarsMountPackage(Str2PChar(copy(s, 1, length(s)-4)+'.hwp'));
  2849 hedgewarsMountPackage(Str2PChar(copy(s, 1, length(s)-4)+'.hwp'));
  2147 end;
  2850 end;
  2148 
  2851 
  2149 procedure SetGlobals;
  2852 procedure SetGlobals;
  2150 begin
  2853 begin
  2151 ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
  2854 ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
  2152 ScriptSetInteger('GameTime', GameTicks);
  2855 ScriptSetInteger('GameTime', GameTicks);
  2153 ScriptSetInteger('TotalRounds', TotalRounds);
  2856 ScriptSetInteger('TotalRounds', TotalRounds);
  2154 ScriptSetInteger('WaterLine', cWaterLine);
  2857 ScriptSetInteger('WaterLine', cWaterLine);
       
  2858 if isCursorVisible and (not bShowAmmoMenu) then
       
  2859     begin
       
  2860     if (prevCursorPoint.X <> CursorPoint.X) or 
       
  2861        (prevCursorPoint.Y <> CursorPoint.Y) then
       
  2862         begin
       
  2863         ScriptSetInteger('CursorX', CursorPoint.X - WorldDx);
       
  2864         ScriptSetInteger('CursorY', cScreenHeight - CursorPoint.Y- WorldDy);
       
  2865         prevCursorPoint.X:= CursorPoint.X;
       
  2866         prevCursorPoint.Y:= CursorPoint.Y;
       
  2867         end
       
  2868     end
       
  2869 else
       
  2870     begin
       
  2871     ScriptSetInteger('CursorX', NoPointX);
       
  2872     ScriptSetInteger('CursorY', NoPointX);
       
  2873     prevCursorPoint.X:= NoPointX;
       
  2874     prevCursorPoint.Y:= NoPointX
       
  2875     end;
       
  2876 
  2155 if not mapDims then
  2877 if not mapDims then
  2156     begin
  2878     begin
  2157     mapDims:= true;
  2879     mapDims:= true;
  2158     ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
  2880     ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
  2159     ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
  2881     ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
  2167     ScriptSetNil('CurrentHedgehog');
  2889     ScriptSetNil('CurrentHedgehog');
  2168 end;
  2890 end;
  2169 
  2891 
  2170 procedure GetGlobals;
  2892 procedure GetGlobals;
  2171 begin
  2893 begin
       
  2894 // TODO
       
  2895 // Use setters instead, because globals should be read-only!
       
  2896 // Otherwise globals might be changed by Lua, but then unexpectatly overwritten by engine when a ScriptCall is triggered by whatever Lua is doing!
       
  2897 // Sure, one could work around that in engine (e.g. by setting writable globals in SetGlobals only when their engine-side value has actually changed since SetGlobals was called the last time...), but things just get messier and messier then.
       
  2898 // It is inconsistent anyway to have some globals be read-only and others not with no indication whatsoever.
       
  2899 // -- sheepluva
  2172 TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
  2900 TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
  2173 end;
  2901 end;
  2174 
  2902 
  2175 procedure ScriptCall(fname : shortstring);
  2903 procedure ScriptCall(fname : shortstring);
  2176 begin
  2904 begin
  2177 if not ScriptLoaded or (not ScriptExists(fname)) then
  2905 if (not ScriptLoaded) or (not ScriptExists(fname)) then
  2178     exit;
  2906     exit;
  2179 SetGlobals;
  2907 SetGlobals;
  2180 lua_getglobal(luaState, Str2PChar(fname));
  2908 lua_getglobal(luaState, Str2PChar(fname));
  2181 if lua_pcall(luaState, 0, 0, 0) <> 0 then
  2909 if lua_pcall(luaState, 0, 0, 0) <> 0 then
  2182     begin
  2910     begin
  2183     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  2911     LuaError('Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  2184     lua_pop(luaState, 1)
  2912     lua_pop(luaState, 1)
  2185     end;
  2913     end;
  2186 GetGlobals;
  2914 GetGlobals;
  2187 end;
  2915 end;
  2188 
  2916 
  2223 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
  2951 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
  2224 end;
  2952 end;
  2225 
  2953 
  2226 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
  2954 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
  2227 begin
  2955 begin
  2228 if not ScriptLoaded or (not ScriptExists(fname)) then
  2956 if (not ScriptLoaded) or (not ScriptExists(fname)) then
  2229     exit;
  2957     exit(0);
  2230 SetGlobals;
  2958 SetGlobals;
  2231 lua_getglobal(luaState, Str2PChar(fname));
  2959 lua_getglobal(luaState, Str2PChar(fname));
  2232 lua_pushinteger(luaState, par1);
  2960 lua_pushinteger(luaState, par1);
  2233 lua_pushinteger(luaState, par2);
  2961 lua_pushinteger(luaState, par2);
  2234 lua_pushinteger(luaState, par3);
  2962 lua_pushinteger(luaState, par3);
  2235 lua_pushinteger(luaState, par4);
  2963 lua_pushinteger(luaState, par4);
  2236 ScriptCall:= 0;
  2964 ScriptCall:= 0;
  2237 if lua_pcall(luaState, 4, 1, 0) <> 0 then
  2965 if lua_pcall(luaState, 4, 1, 0) <> 0 then
  2238     begin
  2966     begin
  2239     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  2967     LuaError('Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  2240     lua_pop(luaState, 1)
  2968     lua_pop(luaState, 1)
  2241     end
  2969     end
  2242 else
  2970 else
  2243     begin
  2971     begin
  2244     ScriptCall:= lua_tointeger(luaState, -1);
  2972     ScriptCall:= lua_tointeger(luaState, -1);
  2359     am : TAmmoType;
  3087     am : TAmmoType;
  2360     si : TStatInfoType;
  3088     si : TStatInfoType;
  2361     st : TSound;
  3089     st : TSound;
  2362     he : THogEffect;
  3090     he : THogEffect;
  2363     cg : TCapGroup;
  3091     cg : TCapGroup;
       
  3092     spr: TSprite;
       
  3093     mg : TMapGen;
  2364 begin
  3094 begin
  2365 // initialize lua
  3095 // initialize lua
  2366 luaState:= lua_open;
  3096 luaState:= lua_open;
  2367 TryDo(luaState <> nil, 'lua_open failed', true);
  3097 TryDo(luaState <> nil, 'lua_open failed', true);
  2368 
  3098 
  2383 ScriptSetInteger('gfBottomBorder', gfBottomBorder);
  3113 ScriptSetInteger('gfBottomBorder', gfBottomBorder);
  2384 ScriptSetInteger('gfDivideTeams', gfDivideTeams);
  3114 ScriptSetInteger('gfDivideTeams', gfDivideTeams);
  2385 ScriptSetInteger('gfLowGravity', gfLowGravity);
  3115 ScriptSetInteger('gfLowGravity', gfLowGravity);
  2386 ScriptSetInteger('gfLaserSight', gfLaserSight);
  3116 ScriptSetInteger('gfLaserSight', gfLaserSight);
  2387 ScriptSetInteger('gfInvulnerable', gfInvulnerable);
  3117 ScriptSetInteger('gfInvulnerable', gfInvulnerable);
       
  3118 ScriptSetInteger('gfResetHealth', gfResetHealth);
  2388 ScriptSetInteger('gfVampiric', gfVampiric);
  3119 ScriptSetInteger('gfVampiric', gfVampiric);
  2389 ScriptSetInteger('gfKarma', gfKarma);
  3120 ScriptSetInteger('gfKarma', gfKarma);
  2390 ScriptSetInteger('gfArtillery', gfArtillery);
  3121 ScriptSetInteger('gfArtillery', gfArtillery);
  2391 ScriptSetInteger('gfOneClanMode', gfOneClanMode);
  3122 ScriptSetInteger('gfOneClanMode', gfOneClanMode);
  2392 ScriptSetInteger('gfRandomOrder', gfRandomOrder);
  3123 ScriptSetInteger('gfRandomOrder', gfRandomOrder);
  2418 ScriptSetInteger('gmTimer', gmTimer);
  3149 ScriptSetInteger('gmTimer', gmTimer);
  2419 ScriptSetInteger('gmAnimate', gmAnimate);
  3150 ScriptSetInteger('gmAnimate', gmAnimate);
  2420 ScriptSetInteger('gmPrecise', gmPrecise);
  3151 ScriptSetInteger('gmPrecise', gmPrecise);
  2421 ScriptSetInteger('gmAllStoppable', gmAllStoppable);
  3152 ScriptSetInteger('gmAllStoppable', gmAllStoppable);
  2422 
  3153 
  2423 
       
  2424 // speech bubbles
  3154 // speech bubbles
  2425 ScriptSetInteger('SAY_SAY', 1);
  3155 ScriptSetInteger('SAY_SAY', 1);
  2426 ScriptSetInteger('SAY_THINK', 2);
  3156 ScriptSetInteger('SAY_THINK', 2);
  2427 ScriptSetInteger('SAY_SHOUT', 3);
  3157 ScriptSetInteger('SAY_SHOUT', 3);
  2428 
  3158 
  2448     ScriptSetInteger(EnumToStr(he), ord(he));
  3178     ScriptSetInteger(EnumToStr(he), ord(he));
  2449 
  3179 
  2450 for cg:= Low(TCapGroup) to High(TCapGroup) do
  3180 for cg:= Low(TCapGroup) to High(TCapGroup) do
  2451     ScriptSetInteger(EnumToStr(cg), ord(cg));
  3181     ScriptSetInteger(EnumToStr(cg), ord(cg));
  2452 
  3182 
  2453 ScriptSetInteger('gstDrowning'       ,$00000001);
  3183 for spr:= Low(TSprite) to High(TSprite) do
  2454 ScriptSetInteger('gstHHDriven'       ,$00000002);
  3184     ScriptSetInteger(EnumToStr(spr), ord(spr));
  2455 ScriptSetInteger('gstMoving'         ,$00000004);
  3185 
  2456 ScriptSetInteger('gstAttacked'       ,$00000008);
  3186 for mg:= Low(TMapGen) to High(TMapGen) do
  2457 ScriptSetInteger('gstAttacking'      ,$00000010);
  3187     ScriptSetInteger(EnumToStr(mg), ord(mg));
  2458 ScriptSetInteger('gstCollision'      ,$00000020);
  3188 
  2459 ScriptSetInteger('gstHHChooseTarget' ,$00000040);
  3189 ScriptSetInteger('gstDrowning'      , gstDrowning);
  2460 ScriptSetInteger('gstHHJumping'      ,$00000100);
  3190 ScriptSetInteger('gstHHDriven'      , gstHHDriven);
  2461 ScriptSetInteger('gsttmpFlag'        ,$00000200);
  3191 ScriptSetInteger('gstMoving'        , gstMoving);
  2462 ScriptSetInteger('gstHHThinking'     ,$00000800);
  3192 ScriptSetInteger('gstAttacked'      , gstAttacked);
  2463 ScriptSetInteger('gstNoDamage'       ,$00001000);
  3193 ScriptSetInteger('gstAttacking'     , gstAttacking);
  2464 ScriptSetInteger('gstHHHJump'        ,$00002000);
  3194 ScriptSetInteger('gstCollision'     , gstCollision);
  2465 ScriptSetInteger('gstAnimation'      ,$00004000);
  3195 ScriptSetInteger('gstChooseTarget'  , gstChooseTarget);
  2466 ScriptSetInteger('gstHHDeath'        ,$00008000);
  3196 ScriptSetInteger('gstHHJumping'     , gstHHJumping);
  2467 ScriptSetInteger('gstWinner'         ,$00010000);
  3197 ScriptSetInteger('gsttmpFlag'       , gsttmpFlag);
  2468 ScriptSetInteger('gstWait'           ,$00020000);
  3198 ScriptSetInteger('gstHHThinking'    , gstHHThinking);
  2469 ScriptSetInteger('gstNotKickable'    ,$00040000);
  3199 ScriptSetInteger('gstNoDamage'      , gstNoDamage);
  2470 ScriptSetInteger('gstLoser'          ,$00080000);
  3200 ScriptSetInteger('gstHHHJump'       , gstHHHJump);
  2471 ScriptSetInteger('gstHHGone'         ,$00100000);
  3201 ScriptSetInteger('gstAnimation'     , gstAnimation);
  2472 ScriptSetInteger('gstInvisible'      ,$00200000);
  3202 ScriptSetInteger('gstHHDeath'       , gstHHDeath);
       
  3203 ScriptSetInteger('gstWinner'        , gstWinner);
       
  3204 ScriptSetInteger('gstWait'          , gstWait);
       
  3205 ScriptSetInteger('gstNotKickable'   , gstNotKickable);
       
  3206 ScriptSetInteger('gstLoser'         , gstLoser);
       
  3207 ScriptSetInteger('gstHHGone'        , gstHHGone);
       
  3208 ScriptSetInteger('gstInvisible'     , gstInvisible);
       
  3209 ScriptSetInteger('gstSubmersible'   , gstSubmersible);
       
  3210 ScriptSetInteger('gstFrozen'        , gstFrozen);
       
  3211 ScriptSetInteger('gstNoGravity'     , gstNoGravity);
  2473 
  3212 
  2474 // ai hints
  3213 // ai hints
  2475 ScriptSetInteger('aihUsualProcessing' ,$00000000);
  3214 ScriptSetInteger('aihUsualProcessing', aihUsualProcessing);
  2476 ScriptSetInteger('aihDoesntMatter'    ,$00000001);
  3215 ScriptSetInteger('aihDoesntMatter'   , aihDoesntMatter);
       
  3216 
       
  3217 // land flags
       
  3218 ScriptSetInteger('lfIndestructible', lfIndestructible);
       
  3219 ScriptSetInteger('lfIce'           , lfIce);
       
  3220 ScriptSetInteger('lfBouncy'        , lfBouncy);
  2477 
  3221 
  2478 // register functions
  3222 // register functions
  2479 lua_register(luaState, _P'HideHog', @lc_hidehog);
  3223 lua_register(luaState, _P'HideHog', @lc_hidehog);
  2480 lua_register(luaState, _P'RestoreHog', @lc_restorehog);
  3224 lua_register(luaState, _P'RestoreHog', @lc_restorehog);
  2481 lua_register(luaState, _P'SaveCampaignVar', @lc_savecampaignvar);
  3225 lua_register(luaState, _P'SaveCampaignVar', @lc_savecampaignvar);
  2485 lua_register(luaState, _P'bnot', @lc_bnot);
  3229 lua_register(luaState, _P'bnot', @lc_bnot);
  2486 lua_register(luaState, _P'div', @lc_div);
  3230 lua_register(luaState, _P'div', @lc_div);
  2487 lua_register(luaState, _P'GetInputMask', @lc_getinputmask);
  3231 lua_register(luaState, _P'GetInputMask', @lc_getinputmask);
  2488 lua_register(luaState, _P'SetInputMask', @lc_setinputmask);
  3232 lua_register(luaState, _P'SetInputMask', @lc_setinputmask);
  2489 lua_register(luaState, _P'AddGear', @lc_addgear);
  3233 lua_register(luaState, _P'AddGear', @lc_addgear);
       
  3234 lua_register(luaState, _P'DismissTeam', @lc_dismissteam);
  2490 lua_register(luaState, _P'EnableGameFlags', @lc_enablegameflags);
  3235 lua_register(luaState, _P'EnableGameFlags', @lc_enablegameflags);
  2491 lua_register(luaState, _P'DisableGameFlags', @lc_disablegameflags);
  3236 lua_register(luaState, _P'DisableGameFlags', @lc_disablegameflags);
  2492 lua_register(luaState, _P'ClearGameFlags', @lc_cleargameflags);
  3237 lua_register(luaState, _P'ClearGameFlags', @lc_cleargameflags);
       
  3238 lua_register(luaState, _P'GetGameFlag', @lc_getgameflag);
  2493 lua_register(luaState, _P'DeleteGear', @lc_deletegear);
  3239 lua_register(luaState, _P'DeleteGear', @lc_deletegear);
  2494 lua_register(luaState, _P'AddVisualGear', @lc_addvisualgear);
  3240 lua_register(luaState, _P'AddVisualGear', @lc_addvisualgear);
  2495 lua_register(luaState, _P'DeleteVisualGear', @lc_deletevisualgear);
  3241 lua_register(luaState, _P'DeleteVisualGear', @lc_deletevisualgear);
  2496 lua_register(luaState, _P'GetVisualGearValues', @lc_getvisualgearvalues);
  3242 lua_register(luaState, _P'GetVisualGearValues', @lc_getvisualgearvalues);
  2497 lua_register(luaState, _P'SetVisualGearValues', @lc_setvisualgearvalues);
  3243 lua_register(luaState, _P'SetVisualGearValues', @lc_setvisualgearvalues);
       
  3244 lua_register(luaState, _P'GetGearValues', @lc_getgearvalues);
       
  3245 lua_register(luaState, _P'SetGearValues', @lc_setgearvalues);
  2498 lua_register(luaState, _P'SpawnHealthCrate', @lc_spawnhealthcrate);
  3246 lua_register(luaState, _P'SpawnHealthCrate', @lc_spawnhealthcrate);
  2499 lua_register(luaState, _P'SpawnAmmoCrate', @lc_spawnammocrate);
  3247 lua_register(luaState, _P'SpawnAmmoCrate', @lc_spawnammocrate);
  2500 lua_register(luaState, _P'SpawnUtilityCrate', @lc_spawnutilitycrate);
  3248 lua_register(luaState, _P'SpawnUtilityCrate', @lc_spawnutilitycrate);
  2501 lua_register(luaState, _P'SpawnFakeHealthCrate', @lc_spawnfakehealthcrate);
  3249 lua_register(luaState, _P'SpawnFakeHealthCrate', @lc_spawnfakehealthcrate);
  2502 lua_register(luaState, _P'SpawnFakeAmmoCrate', @lc_spawnfakeammocrate);
  3250 lua_register(luaState, _P'SpawnFakeAmmoCrate', @lc_spawnfakeammocrate);
  2530 lua_register(luaState, _P'SetEffect', @lc_seteffect);
  3278 lua_register(luaState, _P'SetEffect', @lc_seteffect);
  2531 lua_register(luaState, _P'GetEffect', @lc_geteffect);
  3279 lua_register(luaState, _P'GetEffect', @lc_geteffect);
  2532 lua_register(luaState, _P'GetHogClan', @lc_gethogclan);
  3280 lua_register(luaState, _P'GetHogClan', @lc_gethogclan);
  2533 lua_register(luaState, _P'GetClanColor', @lc_getclancolor);
  3281 lua_register(luaState, _P'GetClanColor', @lc_getclancolor);
  2534 lua_register(luaState, _P'SetClanColor', @lc_setclancolor);
  3282 lua_register(luaState, _P'SetClanColor', @lc_setclancolor);
       
  3283 lua_register(luaState, _P'GetHogVoicepack', @lc_gethogvoicepack);
       
  3284 lua_register(luaState, _P'GetHogFlag', @lc_gethogflag);
       
  3285 lua_register(luaState, _P'GetHogGrave', @lc_gethoggrave);
  2535 lua_register(luaState, _P'GetHogTeamName', @lc_gethogteamname);
  3286 lua_register(luaState, _P'GetHogTeamName', @lc_gethogteamname);
  2536 lua_register(luaState, _P'SetHogTeamName', @lc_sethogteamname);
  3287 lua_register(luaState, _P'SetHogTeamName', @lc_sethogteamname);
  2537 lua_register(luaState, _P'GetHogName', @lc_gethogname);
  3288 lua_register(luaState, _P'GetHogName', @lc_gethogname);
  2538 lua_register(luaState, _P'SetHogName', @lc_sethogname);
  3289 lua_register(luaState, _P'SetHogName', @lc_sethogname);
  2539 lua_register(luaState, _P'GetHogLevel', @lc_gethoglevel);
  3290 lua_register(luaState, _P'GetHogLevel', @lc_gethoglevel);
  2547 lua_register(luaState, _P'GetState', @lc_getstate);
  3298 lua_register(luaState, _P'GetState', @lc_getstate);
  2548 lua_register(luaState, _P'GetTag', @lc_gettag);
  3299 lua_register(luaState, _P'GetTag', @lc_gettag);
  2549 lua_register(luaState, _P'SetTag', @lc_settag);
  3300 lua_register(luaState, _P'SetTag', @lc_settag);
  2550 lua_register(luaState, _P'SetTimer', @lc_settimer);
  3301 lua_register(luaState, _P'SetTimer', @lc_settimer);
  2551 lua_register(luaState, _P'GetTimer', @lc_gettimer);
  3302 lua_register(luaState, _P'GetTimer', @lc_gettimer);
       
  3303 lua_register(luaState, _P'SetFlightTime', @lc_setflighttime);
       
  3304 lua_register(luaState, _P'GetFlightTime', @lc_getflighttime);
  2552 lua_register(luaState, _P'SetZoom', @lc_setzoom);
  3305 lua_register(luaState, _P'SetZoom', @lc_setzoom);
  2553 lua_register(luaState, _P'GetZoom', @lc_getzoom);
  3306 lua_register(luaState, _P'GetZoom', @lc_getzoom);
  2554 lua_register(luaState, _P'HogSay', @lc_hogsay);
  3307 lua_register(luaState, _P'HogSay', @lc_hogsay);
  2555 lua_register(luaState, _P'SwitchHog', @lc_switchhog);
  3308 lua_register(luaState, _P'SwitchHog', @lc_switchhog);
  2556 lua_register(luaState, _P'HogTurnLeft', @lc_hogturnleft);
  3309 lua_register(luaState, _P'HogTurnLeft', @lc_hogturnleft);
  2557 lua_register(luaState, _P'CampaignLock', @lc_campaignlock);
  3310 lua_register(luaState, _P'CampaignLock', @lc_campaignlock);
  2558 lua_register(luaState, _P'CampaignUnlock', @lc_campaignunlock);
  3311 lua_register(luaState, _P'CampaignUnlock', @lc_campaignunlock);
  2559 lua_register(luaState, _P'GetGearElasticity', @lc_getgearelasticity);
  3312 lua_register(luaState, _P'GetGearElasticity', @lc_getgearelasticity);
       
  3313 lua_register(luaState, _P'SetGearElasticity', @lc_setgearelasticity);
       
  3314 lua_register(luaState, _P'GetGearFriction', @lc_getgearfriction);
       
  3315 lua_register(luaState, _P'SetGearFriction', @lc_setgearfriction);
  2560 lua_register(luaState, _P'GetGearRadius', @lc_getgearradius);
  3316 lua_register(luaState, _P'GetGearRadius', @lc_getgearradius);
  2561 lua_register(luaState, _P'GetGearMessage', @lc_getgearmessage);
  3317 lua_register(luaState, _P'GetGearMessage', @lc_getgearmessage);
  2562 lua_register(luaState, _P'SetGearMessage', @lc_setgearmessage);
  3318 lua_register(luaState, _P'SetGearMessage', @lc_setgearmessage);
  2563 lua_register(luaState, _P'GetGearPos', @lc_getgearpos);
  3319 lua_register(luaState, _P'GetGearPos', @lc_getgearpos);
  2564 lua_register(luaState, _P'SetGearPos', @lc_setgearpos);
  3320 lua_register(luaState, _P'SetGearPos', @lc_setgearpos);
  2569 lua_register(luaState, _P'GetDataPath', @lc_getdatapath);
  3325 lua_register(luaState, _P'GetDataPath', @lc_getdatapath);
  2570 lua_register(luaState, _P'GetUserDataPath', @lc_getuserdatapath);
  3326 lua_register(luaState, _P'GetUserDataPath', @lc_getuserdatapath);
  2571 lua_register(luaState, _P'MapHasBorder', @lc_maphasborder);
  3327 lua_register(luaState, _P'MapHasBorder', @lc_maphasborder);
  2572 lua_register(luaState, _P'GetHogHat', @lc_gethoghat);
  3328 lua_register(luaState, _P'GetHogHat', @lc_gethoghat);
  2573 lua_register(luaState, _P'SetHogHat', @lc_sethoghat);
  3329 lua_register(luaState, _P'SetHogHat', @lc_sethoghat);
       
  3330 lua_register(luaState, _P'EraseSprite', @lc_erasesprite);
       
  3331 lua_register(luaState, _P'PlaceSprite', @lc_placesprite);
  2574 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  3332 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  2575 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  3333 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  2576 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  3334 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  2577 
  3335 lua_register(luaState, _P'GetGravity', @lc_getgravity);
  2578 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
  3336 lua_register(luaState, _P'SetGravity', @lc_setgravity);
       
  3337 lua_register(luaState, _P'SetWaterLine', @lc_setwaterline);
       
  3338 lua_register(luaState, _P'SetNextWeapon', @lc_setnextweapon);
       
  3339 lua_register(luaState, _P'SetWeapon', @lc_setweapon);
       
  3340 lua_register(luaState, _P'SetMaxBuildDistance', @lc_setmaxbuilddistance);
       
  3341 // drawn map functions
       
  3342 lua_register(luaState, _P'AddPoint', @lc_addPoint);
       
  3343 lua_register(luaState, _P'FlushPoints', @lc_flushPoints);
       
  3344 
       
  3345 lua_register(luaState, _P'SetGearAIHints', @lc_setgearaihints);
  2579 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
  3346 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
  2580 lua_register(luaState, _P'DeclareAchievement', @lc_declareachievement);
  3347 lua_register(luaState, _P'DeclareAchievement', @lc_declareachievement);
  2581 
  3348 
       
  3349 ScriptSetInteger('TEST_SUCCESSFUL'   , HaltTestSuccess);
       
  3350 ScriptSetInteger('TEST_FAILED'       , HaltTestFailed);
       
  3351 lua_register(luaState, _P'EndLuaTest', @lc_endluatest);
  2582 
  3352 
  2583 ScriptClearStack; // just to be sure stack is empty
  3353 ScriptClearStack; // just to be sure stack is empty
  2584 ScriptLoaded:= false;
  3354 ScriptLoaded:= false;
  2585 end;
  3355 end;
  2586 
  3356 
  2670 end;
  3440 end;
  2671 
  3441 
  2672 procedure initModule;
  3442 procedure initModule;
  2673 begin
  3443 begin
  2674 mapDims:= false;
  3444 mapDims:= false;
       
  3445 PointsBuffer:= '';
       
  3446 prevCursorPoint.X:= NoPointX;
       
  3447 prevCursorPoint.Y:= 0;
  2675 end;
  3448 end;
  2676 
  3449 
  2677 procedure freeModule;
  3450 procedure freeModule;
  2678 begin
  3451 begin
  2679 end;
  3452 end;