hedgewars/uScript.pas
changeset 10280 762c256552e9
parent 10279 b75e7ebfbe11
child 10281 dedc8f23e35e
equal deleted inserted replaced
10279:b75e7ebfbe11 10280:762c256552e9
   114         halt(rtnTestLuaErr);
   114         halt(rtnTestLuaErr);
   115 end;
   115 end;
   116 
   116 
   117 procedure LuaCallError(error ,call, paramsyntax: shortstring);
   117 procedure LuaCallError(error ,call, paramsyntax: shortstring);
   118 begin
   118 begin
   119     LuaError(error + '       function syntax: ' + call + ' ( ' + paramsyntax + ' )');
   119     LuaError(call + ': ' + error + '       function syntax: ' + call + ' ( ' + paramsyntax + ' )');
   120 end;
   120 end;
   121 
   121 
   122 procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
   122 procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
   123 begin
   123 begin
   124     // TODO: i18n?
   124     // TODO: i18n?
   131         begin
   131         begin
   132         LuaCallError('Invalid gear type!', call, paramsyntax);
   132         LuaCallError('Invalid gear type!', call, paramsyntax);
   133         exit(false);
   133         exit(false);
   134         end;
   134         end;
   135     IsValidGearType:= true;
   135     IsValidGearType:= true;
       
   136 end;
       
   137 
       
   138 function IsValidSprite(sprite: lua_Integer; call, paramsyntax: shortstring): boolean; inline;
       
   139 begin
       
   140     if (sprite < ord(Low(TSprite))) or (sprite > ord(High(TSprite))) then
       
   141         begin
       
   142         LuaCallError('Invalid sprite id!', call, paramsyntax);
       
   143         exit(false);
       
   144         end;
       
   145     IsValidSprite:= true;
   136 end;
   146 end;
   137 
   147 
   138 // wrapped calls //
   148 // wrapped calls //
   139 
   149 
   140 // functions called from Lua:
   150 // functions called from Lua:
  1871             end
  1881             end
  1872         end;
  1882         end;
  1873     lc_sethoghat:= 0;
  1883     lc_sethoghat:= 0;
  1874 end;
  1884 end;
  1875 
  1885 
       
  1886 function lc_placesprite(L : Plua_State) : LongInt; Cdecl;
       
  1887 var spr   : TSprite;
       
  1888     lf    : Word;
       
  1889     n : LongInt;
       
  1890     placed: boolean;
       
  1891 const
       
  1892     call = 'PlaceSprite';
       
  1893     params = 'x, y, sprite, frameIdx [, landFlags]';
       
  1894 begin
       
  1895     placed:= false;
       
  1896     n:= lua_gettop(L);
       
  1897     if (n < 4) or (n > 5) then
       
  1898         LuaParameterCountError(call, params, lua_gettop(L))
       
  1899     else
       
  1900         begin
       
  1901         // get landflags, if specified
       
  1902         if n = 5 then
       
  1903             lf:= lua_tointeger(L, 5)
       
  1904         else
       
  1905             lf:= 0;
       
  1906 
       
  1907         n:= lua_tointeger(L, 3);
       
  1908         if IsValidSprite(n, call, params) then
       
  1909             spr:= TSprite(n);
       
  1910             if SpritesData[spr].Surface = nil then
       
  1911                 LuaError(call + ': ' + EnumToStr(spr) + ' cannot be placed! (required information not loaded)' )
       
  1912             else
       
  1913                 placed:= TryPlaceOnLand(
       
  1914                     lua_tointeger(L, 1) - SpritesData[spr].Width div 2,
       
  1915                     lua_tointeger(L, 2) - SpritesData[spr].Height div 2,
       
  1916                     spr, lua_tointeger(L, 4), true, false, lfBouncy);
       
  1917         end;
       
  1918 
       
  1919     lua_pushboolean(L, placed);
       
  1920     lc_placesprite:= 1
       
  1921 end;
       
  1922 
  1876 function lc_placegirder(L : Plua_State) : LongInt; Cdecl;
  1923 function lc_placegirder(L : Plua_State) : LongInt; Cdecl;
  1877 begin
  1924 var placed: boolean;
       
  1925 begin
       
  1926     placed:= false;
  1878     if lua_gettop(L) <> 3 then
  1927     if lua_gettop(L) <> 3 then
  1879         LuaParameterCountError('PlaceGirder', 'x, y, frameIdx', lua_gettop(L))
  1928         LuaParameterCountError('PlaceGirder', 'x, y, frameIdx', lua_gettop(L))
  1880     else
  1929     else
  1881         TryPlaceOnLand(
  1930         placed:= TryPlaceOnLand(
  1882             lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
  1931             lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
  1883             lua_tointeger(L, 2) - SpritesData[sprAmGirder].Height div 2,
  1932             lua_tointeger(L, 2) - SpritesData[sprAmGirder].Height div 2,
  1884             sprAmGirder, lua_tointeger(L, 3), true, false);
  1933             sprAmGirder, lua_tointeger(L, 3), true, false);
  1885     lc_placegirder:= 0
  1934 
       
  1935     lua_pushboolean(L, placed);
       
  1936     lc_placegirder:= 1
  1886 end;
  1937 end;
  1887 
  1938 
  1888 function lc_getcurammotype(L : Plua_State): LongInt; Cdecl;
  1939 function lc_getcurammotype(L : Plua_State): LongInt; Cdecl;
  1889 begin
  1940 begin
  1890     if lua_gettop(L) <> 0 then
  1941     if lua_gettop(L) <> 0 then
  2518     am : TAmmoType;
  2569     am : TAmmoType;
  2519     si : TStatInfoType;
  2570     si : TStatInfoType;
  2520     st : TSound;
  2571     st : TSound;
  2521     he : THogEffect;
  2572     he : THogEffect;
  2522     cg : TCapGroup;
  2573     cg : TCapGroup;
       
  2574     spr: TSprite;
  2523 begin
  2575 begin
  2524 // initialize lua
  2576 // initialize lua
  2525 luaState:= lua_open;
  2577 luaState:= lua_open;
  2526 TryDo(luaState <> nil, 'lua_open failed', true);
  2578 TryDo(luaState <> nil, 'lua_open failed', true);
  2527 
  2579 
  2605 for he:= Low(THogEffect) to High(THogEffect) do
  2657 for he:= Low(THogEffect) to High(THogEffect) do
  2606     ScriptSetInteger(EnumToStr(he), ord(he));
  2658     ScriptSetInteger(EnumToStr(he), ord(he));
  2607 
  2659 
  2608 for cg:= Low(TCapGroup) to High(TCapGroup) do
  2660 for cg:= Low(TCapGroup) to High(TCapGroup) do
  2609     ScriptSetInteger(EnumToStr(cg), ord(cg));
  2661     ScriptSetInteger(EnumToStr(cg), ord(cg));
       
  2662 
       
  2663 for spr:= Low(TSprite) to High(TSprite) do
       
  2664     ScriptSetInteger(EnumToStr(spr), ord(spr));
       
  2665 
  2610 
  2666 
  2611 ScriptSetInteger('gstDrowning'       ,$00000001);
  2667 ScriptSetInteger('gstDrowning'       ,$00000001);
  2612 ScriptSetInteger('gstHHDriven'       ,$00000002);
  2668 ScriptSetInteger('gstHHDriven'       ,$00000002);
  2613 ScriptSetInteger('gstMoving'         ,$00000004);
  2669 ScriptSetInteger('gstMoving'         ,$00000004);
  2614 ScriptSetInteger('gstAttacked'       ,$00000008);
  2670 ScriptSetInteger('gstAttacked'       ,$00000008);
  2728 lua_register(luaState, _P'GetDataPath', @lc_getdatapath);
  2784 lua_register(luaState, _P'GetDataPath', @lc_getdatapath);
  2729 lua_register(luaState, _P'GetUserDataPath', @lc_getuserdatapath);
  2785 lua_register(luaState, _P'GetUserDataPath', @lc_getuserdatapath);
  2730 lua_register(luaState, _P'MapHasBorder', @lc_maphasborder);
  2786 lua_register(luaState, _P'MapHasBorder', @lc_maphasborder);
  2731 lua_register(luaState, _P'GetHogHat', @lc_gethoghat);
  2787 lua_register(luaState, _P'GetHogHat', @lc_gethoghat);
  2732 lua_register(luaState, _P'SetHogHat', @lc_sethoghat);
  2788 lua_register(luaState, _P'SetHogHat', @lc_sethoghat);
       
  2789 lua_register(luaState, _P'PlaceSprite', @lc_placesprite);
  2733 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  2790 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  2734 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  2791 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  2735 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  2792 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  2736 lua_register(luaState, _P'GetGravity', @lc_getgravity);
  2793 lua_register(luaState, _P'GetGravity', @lc_getgravity);
  2737 lua_register(luaState, _P'SetGravity', @lc_setgravity);
  2794 lua_register(luaState, _P'SetGravity', @lc_setgravity);