hedgewars/uScript.pas
changeset 10279 b75e7ebfbe11
parent 10278 cf3ff506734e
child 10280 762c256552e9
equal deleted inserted replaced
10278:cf3ff506734e 10279:b75e7ebfbe11
   105 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   105 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   106 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   106 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   107 
   107 
   108 procedure LuaError(s: shortstring);
   108 procedure LuaError(s: shortstring);
   109 begin
   109 begin
       
   110     s:= 'Lua-script error: ' + s;
   110     WriteLnToConsole(s);
   111     WriteLnToConsole(s);
   111     AddChatString(#5 + s);
   112     AddChatString(#5 + s);
   112     if cTestLua then
   113     if cTestLua then
   113         halt(rtnTestLuaErr);
   114         halt(rtnTestLuaErr);
   114 end;
   115 end;
   115 
   116 
       
   117 procedure LuaCallError(error ,call, paramsyntax: shortstring);
       
   118 begin
       
   119     LuaError(error + '       function syntax: ' + call + ' ( ' + paramsyntax + ' )');
       
   120 end;
       
   121 
   116 procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
   122 procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt);
   117 begin
   123 begin
   118     // TODO: i18n?
   124     // TODO: i18n?
   119     LuaError('Lua: Wrong number of parameters (' + inttostr(wrongcount) + ') passed to ' + call + '!     syntax: ' + call + ' ( ' + paramsyntax + ' )');
   125     LuaCallError('Wrong number of parameters (' + inttostr(wrongcount) + ')!', call, paramsyntax);
       
   126 end;
       
   127 
       
   128 function IsValidGearType(gt: lua_Integer; call, paramsyntax: shortstring): boolean; inline;
       
   129 begin
       
   130     if (gt < ord(Low(TGearType))) or (gt > ord(High(TGearType))) then
       
   131         begin
       
   132         LuaCallError('Invalid gear type!', call, paramsyntax);
       
   133         exit(false);
       
   134         end;
       
   135     IsValidGearType:= true;
   120 end;
   136 end;
   121 
   137 
   122 // wrapped calls //
   138 // wrapped calls //
   123 
   139 
   124 // functions called from Lua:
   140 // functions called from Lua:
   459 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   475 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   460 var gear : PGear;
   476 var gear : PGear;
   461     x, y, s, t: LongInt;
   477     x, y, s, t: LongInt;
   462     dx, dy: hwFloat;
   478     dx, dy: hwFloat;
   463     gt: TGearType;
   479     gt: TGearType;
       
   480 const
       
   481     call = 'AddGear';
       
   482     params = 'x, y, gearType, state, dx, dy, timer';
   464 begin
   483 begin
   465     if lua_gettop(L) <> 7 then
   484     if lua_gettop(L) <> 7 then
   466         begin
   485         begin
   467         LuaParameterCountError('AddGear', 'x, y, gearType, state, dx, dy, timer', lua_gettop(L));
   486         LuaParameterCountError(call, params, lua_gettop(L));
   468         lua_pushnil(L); // return value on stack (nil)
   487         lua_pushnil(L); // return value on stack (nil)
   469         end
   488         end
   470     else
   489     else
   471         begin
   490         begin
   472         x:= lua_tointeger(L, 1);
   491         t:= lua_tointeger(L, 3);
   473         y:= lua_tointeger(L, 2);
   492         if IsValidGearType(t, call, params) then
   474         gt:= TGearType(lua_tointeger(L, 3));
   493             begin
   475         s:= lua_tointeger(L, 4);
   494             x:= lua_tointeger(L, 1);
   476         dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   495             y:= lua_tointeger(L, 2);
   477         dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   496             gt:= TGearType(t);
   478         t:= lua_tointeger(L, 7);
   497             s:= lua_tointeger(L, 4);
   479 
   498             dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   480         gear:= AddGear(x, y, gt, s, dx, dy, t);
   499             dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   481         lastGearByUID:= gear;
   500             t:= lua_tointeger(L, 7);
   482         lua_pushinteger(L, gear^.uid)
   501 
       
   502             gear:= AddGear(x, y, gt, s, dx, dy, t);
       
   503             lastGearByUID:= gear;
       
   504             lua_pushinteger(L, gear^.uid)
       
   505             end
       
   506         else
       
   507             lua_pushnil(L);
   483         end;
   508         end;
   484     lc_addgear:= 1; // 1 return value
   509     lc_addgear:= 1; // 1 return value
   485 end;
   510 end;
   486 
   511 
   487 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;
   512 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;