hedgewars/uScript.pas
changeset 9752 656c511ab0f3
parent 9751 7a6038c3faa2
child 9764 8dc9d268330f
--- a/hedgewars/uScript.pas	Wed Dec 04 10:47:15 2013 +0100
+++ b/hedgewars/uScript.pas	Wed Dec 04 12:28:04 2013 +0100
@@ -80,7 +80,7 @@
     uRenderUtils,
     uTextures,
     uLandGraphics,
-    SysUtils, 
+    SysUtils,
     uIO,
     uVisualGearsList,
     uGearsHandlersMess,
@@ -107,6 +107,12 @@
     AddChatString(#5 + s);
 end;
 
+procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
+begin
+    // TODO: i18n?
+    LuaError('Lua: Wrong number of parameters (' + inttostr(wrongcount) + ') passed to ' + call + '!     syntax: ' + call + ' ( ' + paramsyntax + ' )');
+end;
+
 // wrapped calls //
 
 // functions called from Lua:
@@ -116,9 +122,9 @@
 
 function lc_band(L: PLua_State): LongInt; Cdecl;
 begin
-    if lua_gettop(L) <> 2 then 
+    if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to band!');
+        LuaParameterCountError('band', 'value1, value2', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -128,9 +134,9 @@
 
 function lc_bor(L: PLua_State): LongInt; Cdecl;
 begin
-    if lua_gettop(L) <> 2 then 
+    if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to bor!');
+        LuaParameterCountError('bor', 'value1, value2', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -140,9 +146,9 @@
 
 function lc_bnot(L: PLua_State): LongInt; Cdecl;
 begin
-    if lua_gettop(L) <> 1 then 
+    if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to bnot!');
+        LuaParameterCountError('bnot', 'value', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -152,9 +158,9 @@
 
 function lc_div(L: PLua_State): LongInt; Cdecl;
 begin
-    if lua_gettop(L) <> 2 then 
+    if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to div!');
+        LuaParameterCountError('div', 'dividend, divisor', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -165,7 +171,7 @@
 function lc_getinputmask(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 0 then
-        LuaError('Lua: Wrong number of parameters passed to GetInputMask!')
+        LuaParameterCountError('GetInputMask', '', lua_gettop(L))
     else
         lua_pushinteger(L, InputMask);
     lc_getinputmask:= 1
@@ -174,7 +180,7 @@
 function lc_setinputmask(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to SetInputMask!')
+        LuaParameterCountError('SetInputMask', 'mask', lua_gettop(L))
     else
         InputMask:= lua_tointeger(L, 1);
     lc_setinputmask:= 0
@@ -187,7 +193,7 @@
         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to WriteLnToConsole!');
+        LuaParameterCountError('WriteLnToConsole', 'string', lua_gettop(L));
     lc_writelntoconsole:= 0;
 end;
 
@@ -207,7 +213,7 @@
 
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to ParseCommand!');
+        LuaParameterCountError('ParseCommand', 'string', lua_gettop(L));
     lc_parsecommand:= 0;
 end;
 
@@ -218,7 +224,7 @@
         ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to ShowMission!');
+        LuaParameterCountError('ShowMission', 'caption, subcaption, text, icon, time', lua_gettop(L));
     lc_showmission:= 0;
 end;
 
@@ -265,7 +271,7 @@
         AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to AddCaption!');
+        LuaParameterCountError('AddCaption', 'text[, color, captiongroup]', lua_gettop(L));
     lc_addcaption:= 0;
 end;
 
@@ -276,7 +282,7 @@
         // to be done
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to CampaignLock!');
+        LuaParameterCountError('CampaignLock', 'TODO', lua_gettop(L));
     lc_campaignlock:= 0;
 end;
 
@@ -287,7 +293,7 @@
         // to be done
         end
     else
-        LuaError('Lua: Wrong number of parameters passed to CampaignUnlock!');
+        LuaParameterCountError('CampaignUnlock', 'TODO', lua_gettop(L));
     lc_campaignunlock:= 0;
 end;
 
@@ -296,7 +302,7 @@
 begin
     if lua_gettop(L) <> 4 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnFakeHealthCrate!');
+        LuaParameterCountError('SpawnFakeHealthCrate', 'x, y, explode, poison', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -305,7 +311,7 @@
         HealthCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
         lua_pushinteger(L, gear^.uid);
         end;
-    lc_spawnfakehealthcrate := 1;        
+    lc_spawnfakehealthcrate := 1;
 end;
 
 function lc_spawnfakeammocrate(L: PLua_State): LongInt; Cdecl;
@@ -313,7 +319,7 @@
 begin
     if lua_gettop(L) <> 4 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnFakeAmmoCrate!');
+        LuaParameterCountError('SpawnFakeAmmoCrate', 'x, y, explode, poison', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -330,11 +336,11 @@
 begin
     if lua_gettop(L) <> 4 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnFakeUtilityCrate!');
+        LuaParameterCountError('SpawnFakeUtilityCrate', 'x, y, explode, poison', lua_gettop(L));
         lua_pushnil(L);
         end
     else
-        begin  
+        begin
         gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
         UtilityCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
         lua_pushinteger(L, gear^.uid);
@@ -348,7 +354,7 @@
 begin
     if (lua_gettop(L) < 2) or (lua_gettop(L) > 3) then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnHealthCrate!');
+        LuaParameterCountError('SpawnHealthCrate', 'x, y[, health]', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -363,7 +369,7 @@
         else
             lua_pushnil(L);
         end;
-    lc_spawnhealthcrate := 1;        
+    lc_spawnhealthcrate := 1;
 end;
 
 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
@@ -371,12 +377,12 @@
 begin
     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnAmmoCrate!');
+        LuaParameterCountError('SpawnAmmoCrate', 'x, y, content[, amount]', lua_gettop(L));
         lua_pushnil(L);
         end
     else
         begin
-        if (lua_gettop(L) = 3) then 
+        if (lua_gettop(L) = 3) then
              gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), 0)
         else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
         if gear <> nil then
@@ -392,7 +398,7 @@
 begin
     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SpawnUtilityCrate!');
+        LuaParameterCountError('SpawnUtilityCrate', 'x, y, content[, amount]', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -416,7 +422,7 @@
 begin
     if lua_gettop(L) <> 7 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to AddGear!');
+        LuaParameterCountError('AddGear', 'x, y, gearType, state, dx, dy, timer', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -441,7 +447,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to DeleteGear!');
+        LuaParameterCountError('DeleteGear', 'gearUid', lua_gettop(L));
         end
     else
         begin
@@ -460,7 +466,7 @@
 begin
     if lua_gettop(L) <> 5 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to AddVisualGear!');
+        LuaParameterCountError('AddVisualGear', 'x, y, visualGearType, state, critical', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -472,7 +478,7 @@
         c:= lua_toboolean(L, 5);
 
         vg:= AddVisualGear(x, y, vgt, s, c);
-        if vg <> nil then 
+        if vg <> nil then
             begin
             lastVisualGearByUID:= vg;
             lua_pushinteger(L, vg^.uid)
@@ -488,7 +494,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to DeleteVisualGear!');
+        LuaParameterCountError('DeleteVisualGear', 'vgUid', lua_gettop(L));
         end
     else
         begin
@@ -504,7 +510,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetVisualGearValues!');
+        LuaParameterCountError('GetVisualGearValues', 'vgUid', lua_gettop(L));
         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L);
         lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L)
         end
@@ -538,7 +544,7 @@
 begin
     if lua_gettop(L) <> 11 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetVisualGearValues!');
+        LuaParameterCountError('SetVisualGearValues', 'vgUid, X, Y, dX, dY, Angle, Frame, FrameTicks, State, Timer, Tint', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -566,7 +572,7 @@
 begin
     if lua_gettop(L) <> 0 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetFollowGear!');
+        LuaParameterCountError('GetFollowGear', '', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -582,7 +588,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearType!');
+        LuaParameterCountError('GetGearType', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -601,7 +607,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearMessage!');
+        LuaParameterCountError('GetGearMessage', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -620,7 +626,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearElasticity!');
+        LuaParameterCountError('GetGearElasticity', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -638,7 +644,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearMessage!')
+        LuaParameterCountError('SetGearMessage', 'gearUid, message', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -653,7 +659,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearPos!');
+        LuaParameterCountError('GetGearPos', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -671,7 +677,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearPos!')
+        LuaParameterCountError('SetGearPos', 'gearUid, value', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -686,7 +692,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearCollisionMask!');
+        LuaParameterCountError('GetGearCollisionMask', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -704,7 +710,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearCollisionMask!')
+        LuaParameterCountError('SetGearCollisionMask', 'gearUid, mask', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -718,7 +724,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to GetHogLevel!')
+        LuaParameterCountError('GetHogLevel', 'gearUid', lua_gettop(L))
     else
         begin
         gear := GearByUID(lua_tointeger(L, 1));
@@ -734,7 +740,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetHogLevel!')
+        LuaParameterCountError('SetHogLevel', 'gearUid, level', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -749,7 +755,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetHogClan!');
+        LuaParameterCountError('GetHogClan', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -769,7 +775,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetClanColor!');
+        LuaParameterCountError('GetClanColor', 'clan', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else lua_pushinteger(L, ClansArray[lua_tointeger(L, 1)]^.Color shl 8 or $FF);
@@ -783,7 +789,7 @@
     i, j : LongInt;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetClanColor!')
+        LuaParameterCountError('SetClanColor', 'clan, color', lua_gettop(L))
     else
         begin
         clan := ClansArray[lua_tointeger(L, 1)];
@@ -795,7 +801,7 @@
             for j:= 0 to 7 do
                 begin
                 hh:= team^.Hedgehogs[j];
-                if (hh.Gear <> nil) or (hh.GearHidden <> nil) then 
+                if (hh.Gear <> nil) or (hh.GearHidden <> nil) then
                     begin
                     FreeTexture(hh.NameTagTex);
                     hh.NameTagTex:= RenderStringTex(hh.Name, clan^.Color, fnt16);
@@ -817,7 +823,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetHogTeamName!');
+        LuaParameterCountError('GetHogTeamName', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -838,7 +844,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetHogName!');
+        LuaParameterCountError('GetHogName', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -859,7 +865,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetHogName!');
+        LuaParameterCountError('SetHogName', 'gearUid, name', lua_gettop(L));
         lua_pushnil(L)
         end
     else
@@ -881,7 +887,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetTimer!');
+        LuaParameterCountError('GetTimer', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -900,7 +906,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetHealth!');
+        LuaParameterCountError('GetHealth', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -919,7 +925,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetX!');
+        LuaParameterCountError('GetX', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -938,7 +944,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetY!');
+        LuaParameterCountError('GetY', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -957,7 +963,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to CopyPV!');
+        LuaParameterCountError('CopyPV', 'fromGearUid, toGearUid', lua_gettop(L));
         end
     else
         begin
@@ -978,7 +984,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to FollowGear!')
+        LuaParameterCountError('FollowGear', 'gearUid', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1016,7 +1022,7 @@
             else
                 lua_pushnil(L)
         end
-    else LuaError('Lua: Wrong number of parameters passed to HogSay!');
+    else LuaParameterCountError('HogSay', 'gearUid, text, manner[, vgState]', lua_gettop(L));
     lc_hogsay:= 1
 end;
 
@@ -1024,7 +1030,7 @@
 var gear, prevgear : PGear;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to SwitchHog!')
+        LuaParameterCountError('SwitchHog', 'gearUid', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1039,7 +1045,7 @@
                 prevgear^.Z := cHHZ;
                 prevgear^.Message:= prevgear^.Message or gmRemoveFromList or gmAddToList;
                 end;
-            
+
             SwitchCurrentHedgehog(gear^.Hedgehog);
             CurrentTeam:= CurrentHedgehog^.Team;
 
@@ -1062,7 +1068,7 @@
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
             AddAmmoAmount(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L,3) );
     end else
-    
+
     if lua_gettop(L) = 2 then
     begin
     gear:= GearByUID(lua_tointeger(L, 1));
@@ -1070,7 +1076,7 @@
             AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
     end else
     begin
-        LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
+        LuaParameterCountError('AddAmmo', 'TODO', lua_gettop(L));
     end;
 
     lc_addammo:= 0;
@@ -1089,7 +1095,7 @@
             else
                 SetAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L, 3))
         end
-    else LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
+    else LuaParameterCountError('AddAmmo', 'gearUid, ammoType[, ammoCount]', lua_gettop(L));
     lc_addammo:= 0
 end;
 
@@ -1100,7 +1106,7 @@
     if (lua_gettop(L) = 2) then
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
-        if (gear <> nil) and (gear^.Hedgehog <> nil) then 
+        if (gear <> nil) and (gear^.Hedgehog <> nil) then
             begin
             ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
             if ammo^.AmmoType = amNothing then
@@ -1110,9 +1116,9 @@
             end
         else lua_pushinteger(L, 0)
         end
-    else 
+    else
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetAmmoCount!');
+        LuaParameterCountError('GetAmmoCount', 'gearUid, ammoType', lua_gettop(L));
         lua_pushnil(L)
         end;
     lc_getammocount:= 1
@@ -1123,7 +1129,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetHealth!');
+        LuaParameterCountError('SetHealth', 'gearUid, health', lua_gettop(L));
         end
     else
         begin
@@ -1133,7 +1139,7 @@
             gear^.Health:= lua_tointeger(L, 2);
 
         if (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
-            begin  
+            begin
             RenderHealth(gear^.Hedgehog^);
             RecountTeamHealth(gear^.Hedgehog^.Team)
             end;
@@ -1149,7 +1155,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetTimer!');
+        LuaParameterCountError('SetTimer', 'gearUid, timer', lua_gettop(L));
         end
     else
         begin
@@ -1163,7 +1169,7 @@
 var gear: PGear;
 begin
     if lua_gettop(L) <> 3 then
-        LuaError('Lua: Wrong number of parameters passed to SetEffect!')
+        LuaParameterCountError('SetEffect', 'gearUid, effect, enabled', lua_gettop(L))
     else begin
         gear := GearByUID(lua_tointeger(L, 1));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
@@ -1171,12 +1177,13 @@
     end;
     lc_seteffect := 0;
 end;
+
 function lc_geteffect(L : Plua_State) : LongInt; Cdecl;
 var gear : PGear;
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetEffect!');
+        LuaParameterCountError('GetEffect', 'gearUid, effect', lua_gettop(L));
         end
     else
         begin
@@ -1194,7 +1201,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetState!');
+        LuaParameterCountError('SetState', 'gearUid, state', lua_gettop(L));
         end
     else
         begin
@@ -1213,7 +1220,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetState!');
+        LuaParameterCountError('GetState', 'gearUid', lua_gettop(L));
         end
     else
         begin
@@ -1231,7 +1238,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetX!');
+        LuaParameterCountError('GetTag', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -1250,7 +1257,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetTag!');
+        LuaParameterCountError('SetTag', 'gearUid, tag', lua_gettop(L));
         end
     else
         begin
@@ -1276,49 +1283,49 @@
 var i : LongInt;
 var color : shortstring;
 begin
-	statInfo := TStatInfoType(lua_tointeger(L, 1));
-	if (lua_gettop(L) <> 2) and ((statInfo <> siPlayerKills) 
-			and (statInfo <> siClanHealth)) then
+    statInfo := TStatInfoType(lua_tointeger(L, 1));
+    if (lua_gettop(L) <> 2) and ((statInfo <> siPlayerKills)
+            and (statInfo <> siClanHealth)) then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SendStat! Expected 2 parameters.');
+        LuaParameterCountError('SendStat', 'statInfoType, color', lua_gettop(L));
         end
-    else if (lua_gettop(L) <> 3) and ((statInfo = siPlayerKills) 
-			or (statInfo = siClanHealth)) then
-		begin
-        LuaError('Lua: Wrong number of parameters passed to SendStat! Expected 3 parameters.');
+    else if (lua_gettop(L) <> 3) and ((statInfo = siPlayerKills)
+            or (statInfo = siClanHealth)) then
+        begin
+        LuaParameterCountError('SendStat', 'siClanHealth, color, teamname', lua_gettop(L));
         end
     else
-		begin
-		if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
-			begin
-			// 3: team name
-			for i:= 0 to Pred(TeamsCount) do
-				begin
-				with TeamsArray[i]^ do
-					begin
-						if TeamName = lua_tostring(L, 3) then
-							begin
-							color := uUtils.IntToStr(Clan^.Color);
-							Break;
-							end
-					end				
-				end;
-			if (statInfo = siPlayerKills) then
-				begin
-					SendStat(siPlayerKills, color + ' ' +
-						lua_tostring(L, 2) + ' ' + TeamsArray[i]^.TeamName);
-				end
-			else if (statInfo = siClanHealth) then
-				begin
-					SendStat(siClanHealth, color + ' ' +
-						lua_tostring(L, 2));
-				end
-			end
-		else
-			begin
-			SendStat(statInfo,lua_tostring(L, 2));
-			end;
-		end;
+        begin
+        if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
+            begin
+            // 3: team name
+            for i:= 0 to Pred(TeamsCount) do
+                begin
+                with TeamsArray[i]^ do
+                    begin
+                        if TeamName = lua_tostring(L, 3) then
+                            begin
+                            color := uUtils.IntToStr(Clan^.Color);
+                            Break;
+                            end
+                    end
+                end;
+            if (statInfo = siPlayerKills) then
+                begin
+                    SendStat(siPlayerKills, color + ' ' +
+                        lua_tostring(L, 2) + ' ' + TeamsArray[i]^.TeamName);
+                end
+            else if (statInfo = siClanHealth) then
+                begin
+                    SendStat(siClanHealth, color + ' ' +
+                        lua_tostring(L, 2));
+                end
+            end
+        else
+            begin
+            SendStat(statInfo,lua_tostring(L, 2));
+            end;
+        end;
     lc_sendstat:= 0
 end;
 
@@ -1337,7 +1344,7 @@
 begin
     tryhard:= false;
     if (lua_gettop(L) <> 4) and (lua_gettop(L) <> 5) then
-        LuaError('Lua: Wrong number of parameters passed to FindPlace!')
+        LuaParameterCountError('FindPlace', 'gearUid, fall, left, right[, tryHarder]', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1367,7 +1374,7 @@
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             AddVoice(TSound(lua_tointeger(L, 1)),gear^.Hedgehog^.Team^.Voicepack)
         end
-    else LuaError('Lua: Wrong number of parameters passed to PlaySound!');
+    else LuaParameterCountError('PlaySound', 'soundId', lua_gettop(L));
     lc_playsound:= 0;
 end;
 
@@ -1377,7 +1384,7 @@
     np:= lua_gettop(L);
     if (np < 5) or (np > 6) then
         begin
-        LuaError('Lua: Wrong number of parameters passed to AddTeam!');
+        LuaParameterCountError('AddTeam', 'teamname, color, grave, fort, voicepack[, flag]', lua_gettop(L));
         //lua_pushnil(L)
         end
     else
@@ -1399,7 +1406,7 @@
 begin
     if lua_gettop(L) <> 4 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to AddHog!');
+        LuaParameterCountError('AddHog', 'hogname, botlevel, health, hat', lua_gettop(L));
         lua_pushnil(L)
         end
     else
@@ -1417,7 +1424,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to HogTurnLeft!');
+        LuaParameterCountError('HogTurnLeft', 'gearUid, boolean', lua_gettop(L));
         end
     else
         begin
@@ -1433,7 +1440,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearPosition!');
+        LuaParameterCountError('GetGearPosition', 'gearUid', lua_gettop(L));
         lua_pushnil(L);
         lua_pushnil(L)
         end
@@ -1460,7 +1467,7 @@
     x, y: LongInt;
 begin
     if lua_gettop(L) <> 3 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearPosition!')
+        LuaParameterCountError('SetGearPosition', 'gearUid, x, y', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1486,7 +1493,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearTarget!');
+        LuaParameterCountError('GetGearTarget', 'gearUid', lua_gettop(L));
         lua_pushnil(L);
         lua_pushnil(L)
         end
@@ -1511,7 +1518,7 @@
 var gear: PGear;
 begin
     if lua_gettop(L) <> 3 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearTarget!')
+        LuaParameterCountError('SetGearTarget', 'gearUid, x, y', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1530,7 +1537,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearVelocity!');
+        LuaParameterCountError('GetGearVelocity', 'gearUid', lua_gettop(L));
         lua_pushnil(L);
         lua_pushnil(L)
         end
@@ -1553,7 +1560,7 @@
 var gear: PGear;
 begin
     if lua_gettop(L) <> 3 then
-        LuaError('Lua: Wrong number of parameters passed to SetGearVelocity!')
+        LuaParameterCountError('SetGearVelocity', 'gearUid, dx, dy', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1570,7 +1577,7 @@
 function lc_setzoom(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to SetZoom!')
+        LuaParameterCountError('SetZoom', 'zoomLevel', lua_gettop(L))
     else
         begin
         ZoomValue:= lua_tonumber(L, 1);
@@ -1586,7 +1593,7 @@
 begin
     if lua_gettop(L) <> 0 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetZoom!');
+        LuaParameterCountError('GetZoom', '', lua_gettop(L));
         lua_pushnil(L)
         end
     else
@@ -1599,7 +1606,7 @@
 begin
     np:= lua_gettop(L);
     if (np < 4) or (np > 5) then
-        LuaError('Lua: Wrong number of parameters passed to SetAmmo!')
+        LuaParameterCountError('SetAmmo', 'ammoType, count, probability, delay[, numberInCrate]', lua_gettop(L))
     else if np = 4 then
         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
     else
@@ -1612,7 +1619,7 @@
 begin
     np:= lua_gettop(L);
     if (np <> 2) then
-        LuaError('Lua: Wrong number of parameters passed to SetAmmoDelay!')
+        LuaParameterCountError('SetAmmoDelay', 'ammoType, delay', lua_gettop(L))
     else
         ScriptSetAmmoDelay(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2));
     lc_setammodelay:= 0
@@ -1623,7 +1630,7 @@
 begin
     np:= lua_gettop(L);
     if (np <> 4) then
-        LuaError('Lua: Wrong number of parameters passed to SetAmmoStore!')
+        LuaParameterCountError('SetAmmoStore', 'loadouts, probabilities, delays, reinforments', lua_gettop(L))
     else
         begin
         ScriptAmmoLoadout:= lua_tostring(L, 1);
@@ -1639,7 +1646,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetRandom!');
+        LuaParameterCountError('GetRandom', 'number', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -1659,7 +1666,7 @@
 function lc_setwind(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to SetWind!')
+        LuaParameterCountError('SetWind', 'windSpeed', lua_gettop(L))
     else
         begin
         cWindSpeed:= int2hwfloat(lua_tointeger(L, 1)) / 100 * cMaxWindSpeed;
@@ -1675,7 +1682,7 @@
 begin
     if lua_gettop(L) <> 0 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetDataPath!');
+        LuaParameterCountError('GetDataPath', '', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -1687,7 +1694,7 @@
 begin
     if lua_gettop(L) <> 0 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetUserDataPath!');
+        LuaParameterCountError('GetUserDataPath', '', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -1699,7 +1706,7 @@
 begin
     if lua_gettop(L) <> 0 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to MapHasBorder!');
+        LuaParameterCountError('MapHasBorder', '', lua_gettop(L));
         lua_pushnil(L);
         end
     else
@@ -1712,7 +1719,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to GetGearRadius!');
+        LuaParameterCountError('GetGearRadius', 'gearUid', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -1730,7 +1737,7 @@
 var gear : PGear;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to GetHogHat!')
+        LuaParameterCountError('GetHogHat', 'gearUid', lua_gettop(L))
     else begin
         gear := GearByUID(lua_tointeger(L, 1));
         if (gear <> nil) and ((gear^.Kind = gtHedgehog) or (gear^.Kind = gtGrave)) and (gear^.Hedgehog <> nil) then
@@ -1747,7 +1754,7 @@
 begin
     if lua_gettop(L) <> 2 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to SetHogHat!');
+        LuaParameterCountError('SetHogHat', 'gearUid, hat', lua_gettop(L));
         lua_pushnil(L)
         end
     else
@@ -1770,7 +1777,7 @@
 function lc_placegirder(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 3 then
-        LuaError('Lua: Wrong number of parameters passed to PlaceGirder!')
+        LuaParameterCountError('PlaceGirder', 'x, y, state', lua_gettop(L))
     else
         TryPlaceOnLand(
             lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
@@ -1782,7 +1789,7 @@
 function lc_getcurammotype(L : Plua_State): LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 0 then
-        LuaError('Lua: Wrong number of parameters passed to GetCurAmmoType!')
+        LuaParameterCountError('GetCurAmmoType', '', lua_gettop(L))
     else
         lua_pushinteger(L, ord(CurrentHedgehog^.CurAmmoType));
     lc_getcurammotype := 1;
@@ -1791,7 +1798,7 @@
 function lc_savecampaignvar(L : Plua_State): LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SaveCampaignVar!')
+        LuaParameterCountError('SaveCampaignVar', 'varname, value', lua_gettop(L))
     else begin
         SendIPC('V!' + lua_tostring(L, 1) + ' ' + lua_tostring(L, 2) + #0);
     end;
@@ -1801,7 +1808,7 @@
 function lc_getcampaignvar(L : Plua_State): LongInt; Cdecl;
 begin
     if (lua_gettop(L) <> 1) then
-        LuaError('Lua: Wrong number of parameters passed to GetCampaignVar!')
+        LuaParameterCountError('GetCampaignVar', 'varname', lua_gettop(L))
     else
         SendIPCAndWaitReply('V?' + lua_tostring(L, 1) + #0);
     lua_pushstring(L, str2pchar(CampaignVariable));
@@ -1812,7 +1819,7 @@
 var gear: PGear;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to HideHog!')
+        LuaParameterCountError('HideHog', 'gearUid', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1826,7 +1833,7 @@
     uid: LongWord;
 begin
     if lua_gettop(L) <> 1 then
-        LuaError('Lua: Wrong number of parameters passed to RestoreHog!')
+        LuaParameterCountError('RestoreHog', 'gearUid', lua_gettop(L))
     else
         begin
         uid:= LongWord(lua_tointeger(L, 1));
@@ -1848,7 +1855,7 @@
 begin
     if lua_gettop(L) <> 5 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to TestRectForObstacle!');
+        LuaParameterCountError('TestRectForObstacle', 'x1, y1, x2, y2, landOnly', lua_gettop(L));
         lua_pushnil(L); // return value on stack (nil)
         end
     else
@@ -1870,7 +1877,7 @@
 var gear: PGear;
 begin
     if lua_gettop(L) <> 2 then
-        LuaError('Lua: Wrong number of parameters passed to SetAIHintOnGear!')
+        LuaParameterCountError('SetAIHintOnGear', 'gearUid, aiHints', lua_gettop(L))
     else
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
@@ -1885,7 +1892,7 @@
 begin
     if lua_gettop(L) <> 1 then
         begin
-        LuaError('Lua: Wrong number of parameters passed to HedgewarsScriptLoad!');
+        LuaParameterCountError('HedgewarsScriptLoad', 'scriptPath', lua_gettop(L));
         lua_pushnil(L)
         end
     else
@@ -1897,7 +1904,7 @@
 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
 begin
     if lua_gettop(L) <> 4 then
-        LuaError('Lua: Wrong number of parameters passed to DeclareAchievement!')
+        LuaParameterCountError('DeclareAchievement', 'achievementId, teamname, location, value', lua_gettop(L))
     else
         declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
     lc_declareachievement:= 0
@@ -2036,7 +2043,7 @@
             if StoreCnt-1 < k then AddAmmoStore;
             inc(k)
             end
-else 
+else
     for i:= 0 to Pred(TeamsCount) do
         begin
         for j:= 0 to Pred(TeamsArray[i]^.HedgehogsNumber) do
@@ -2092,7 +2099,7 @@
     end;
 
 f:= pfsOpenRead(s);
-if f = nil then 
+if f = nil then
     exit;
 
 physfsReaderSetBuffer(@buf);
@@ -2303,7 +2310,7 @@
             AddAmmoStore;
             TeamsArray[i]^.Hedgehogs[j].AmmoStore:= StoreCnt - 1
             end
-else 
+else
     for i:= 0 to Pred(TeamsCount) do
         begin
         if ScriptExists('onNewAmmoStore') then