hedgewars/uScript.pas
branchwebgl
changeset 8096 453917e94e55
parent 8026 4a4f21070479
parent 8077 2ea5cde93abc
child 8330 aaefa587e277
equal deleted inserted replaced
8053:2e836bebb518 8096:453917e94e55
    79     uRenderUtils,
    79     uRenderUtils,
    80     uTextures,
    80     uTextures,
    81     uLandGraphics,
    81     uLandGraphics,
    82     SDLh,
    82     SDLh,
    83     SysUtils, 
    83     SysUtils, 
    84     uIO;
    84     uIO,
       
    85     uPhysFSLayer
       
    86     ;
    85 
    87 
    86 var luaState : Plua_State;
    88 var luaState : Plua_State;
    87     ScriptAmmoLoadout : shortstring;
    89     ScriptAmmoLoadout : shortstring;
    88     ScriptAmmoProbability : shortstring;
    90     ScriptAmmoProbability : shortstring;
    89     ScriptAmmoDelay : shortstring;
    91     ScriptAmmoDelay : shortstring;
  1592         begin
  1594         begin
  1593         LuaError('Lua: Wrong number of parameters passed to GetDataPath!');
  1595         LuaError('Lua: Wrong number of parameters passed to GetDataPath!');
  1594         lua_pushnil(L);
  1596         lua_pushnil(L);
  1595         end
  1597         end
  1596     else
  1598     else
  1597         lua_pushstring(L, str2pchar(Pathz[ptData]));
  1599         lua_pushstring(L, str2pchar(cPathz[ptData]));
  1598     lc_getdatapath:= 1
  1600     lc_getdatapath:= 1
  1599 end;
  1601 end;
  1600 
  1602 
  1601 function lc_getuserdatapath(L : Plua_State) : LongInt; Cdecl;
  1603 function lc_getuserdatapath(L : Plua_State) : LongInt; Cdecl;
  1602 begin
  1604 begin
  1604         begin
  1606         begin
  1605         LuaError('Lua: Wrong number of parameters passed to GetUserDataPath!');
  1607         LuaError('Lua: Wrong number of parameters passed to GetUserDataPath!');
  1606         lua_pushnil(L);
  1608         lua_pushnil(L);
  1607         end
  1609         end
  1608     else
  1610     else
  1609         lua_pushstring(L, str2pchar(UserPathz[ptData]));
  1611         lua_pushstring(L, str2pchar(cPathz[ptData]));
  1610     lc_getuserdatapath:= 1
  1612     lc_getuserdatapath:= 1
  1611 end;
  1613 end;
  1612 
  1614 
  1613 function lc_maphasborder(L : Plua_State) : LongInt; Cdecl;
  1615 function lc_maphasborder(L : Plua_State) : LongInt; Cdecl;
  1614 begin
  1616 begin
  1792             gear^.aihints:= lua_tointeger(L, 2);
  1794             gear^.aihints:= lua_tointeger(L, 2);
  1793         end;
  1795         end;
  1794     lc_setaihintsongear:= 0
  1796     lc_setaihintsongear:= 0
  1795 end;
  1797 end;
  1796 
  1798 
       
  1799 
       
  1800 function lc_hedgewarsscriptload(L : Plua_State) : LongInt; Cdecl;
       
  1801 begin
       
  1802     if lua_gettop(L) <> 1 then
       
  1803         begin
       
  1804         LuaError('Lua: Wrong number of parameters passed to HedgewarsScriptLoad!');
       
  1805         lua_pushnil(L)
       
  1806         end
       
  1807     else
       
  1808         ScriptLoad(lua_tostring(L, 1));
       
  1809     lc_hedgewarsscriptload:= 0;
       
  1810 end;
  1797 ///////////////////
  1811 ///////////////////
  1798 
  1812 
  1799 procedure ScriptPrintStack;
  1813 procedure ScriptPrintStack;
  1800 var n, i : LongInt;
  1814 var n, i : LongInt;
  1801 begin
  1815 begin
  1964 ScriptSetInteger('ScreenHeight', cScreenHeight);
  1978 ScriptSetInteger('ScreenHeight', cScreenHeight);
  1965 ScriptSetInteger('ScreenWidth', cScreenWidth);
  1979 ScriptSetInteger('ScreenWidth', cScreenWidth);
  1966 ScriptCall('onScreenResize');
  1980 ScriptCall('onScreenResize');
  1967 end;
  1981 end;
  1968 
  1982 
       
  1983 // custom script loader via physfs, passed to lua_load
       
  1984 const BUFSIZE = 1024;
  1969 
  1985 
  1970 procedure ScriptLoad(name : shortstring);
  1986 procedure ScriptLoad(name : shortstring);
  1971 var ret : LongInt;
  1987 var ret : LongInt;
  1972       s : shortstring;
  1988       s : shortstring;
  1973 begin
  1989       f : PFSFile;
  1974 s:= UserPathz[ptData] + '/' + name;
  1990     buf : array[0..Pred(BUFSIZE)] of byte;
  1975 if not FileExists(s) then
  1991 begin
  1976     s:= Pathz[ptData] + '/' + name;
  1992 s:= cPathz[ptData] + name;
  1977 if not FileExists(s) then
  1993 if not pfsExists(s) then
  1978     exit;
  1994     exit;
  1979 
  1995 
  1980 ret:= luaL_loadfile(luaState, Str2PChar(s));
  1996 f:= pfsOpenRead(s);
       
  1997 if f = nil then 
       
  1998     exit;
       
  1999 
       
  2000 physfsReaderSetBuffer(@buf);
       
  2001 ret:= lua_load(luaState, @physfsReader, f, Str2PChar(s));
       
  2002 pfsClose(f);
       
  2003 
  1981 if ret <> 0 then
  2004 if ret <> 0 then
  1982     begin
  2005     begin
  1983     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  2006     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  1984     LuaError('Lua: ' + lua_tostring(luaState, -1));
  2007     LuaError('Lua: ' + lua_tostring(luaState, -1));
  1985     end
  2008     end
  2393 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  2416 lua_register(luaState, _P'PlaceGirder', @lc_placegirder);
  2394 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  2417 lua_register(luaState, _P'GetCurAmmoType', @lc_getcurammotype);
  2395 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  2418 lua_register(luaState, _P'TestRectForObstacle', @lc_testrectforobstacle);
  2396 
  2419 
  2397 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
  2420 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
       
  2421 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
  2398 
  2422 
  2399 
  2423 
  2400 ScriptClearStack; // just to be sure stack is empty
  2424 ScriptClearStack; // just to be sure stack is empty
  2401 ScriptLoaded:= false;
  2425 ScriptLoaded:= false;
  2402 end;
  2426 end;