hedgewars/uScript.pas
changeset 4485 85695e3d2623
parent 4484 b7a098f2649a
child 4496 ba5da3388110
equal deleted inserted replaced
4478:05029b4d8490 4485:85695e3d2623
    81 // functions called from Lua:
    81 // functions called from Lua:
    82 // function(L : Plua_State) : LongInt; Cdecl;
    82 // function(L : Plua_State) : LongInt; Cdecl;
    83 // where L contains the state, returns the number of return values on the stack
    83 // where L contains the state, returns the number of return values on the stack
    84 // call lua_gettop(L) to receive number of parameters passed
    84 // call lua_gettop(L) to receive number of parameters passed
    85 
    85 
       
    86 function lc_band(L: PLua_State): LongInt; Cdecl;
       
    87 begin
       
    88     if lua_gettop(L) <> 2 then 
       
    89         begin
       
    90         LuaError('Lua: Wrong number of parameters passed to band!');
       
    91         lua_pushnil(L);
       
    92         end
       
    93     else lua_pushinteger(L, lua_tointeger(L, 2) and lua_tointeger(L, 1));
       
    94     lc_band := 1;
       
    95 end;
       
    96 
       
    97 function lc_bor(L: PLua_State): LongInt; Cdecl;
       
    98 begin
       
    99     if lua_gettop(L) <> 2 then 
       
   100         begin
       
   101         LuaError('Lua: Wrong number of parameters passed to bor!');
       
   102         lua_pushnil(L);
       
   103         end
       
   104     else lua_pushinteger(L, lua_tointeger(L, 2) or lua_tointeger(L, 1));
       
   105     lc_bor := 1;
       
   106 end;
       
   107 
       
   108 function lc_bnot(L: PLua_State): LongInt; Cdecl;
       
   109 begin
       
   110     if lua_gettop(L) <> 1 then 
       
   111         begin
       
   112         LuaError('Lua: Wrong number of parameters passed to bnot!');
       
   113         lua_pushnil(L);
       
   114         end
       
   115     else lua_pushinteger(L, not lua_tointeger(L, 1));
       
   116     lc_bnot := 1;
       
   117 end;
       
   118 
    86 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
   119 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
    87 begin
   120 begin
    88     if lua_gettop(L) = 1 then
   121     if lua_gettop(L) = 1 then
    89         begin
   122         begin
    90         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
   123         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
   164         lua_pushnil(L);
   197         lua_pushnil(L);
   165     end
   198     end
   166     else begin
   199     else begin
   167         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   200         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   168             HealthCrate, 0);
   201             HealthCrate, 0);
   169         lua_pushnumber(L, gear^.uid);
   202         lua_pushinteger(L, gear^.uid);
   170     end;
   203     end;
   171     lc_spawnhealthcrate := 1;        
   204     lc_spawnhealthcrate := 1;        
   172 end;
   205 end;
   173 
   206 
   174 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   207 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   179         lua_pushnil(L);
   212         lua_pushnil(L);
   180     end
   213     end
   181     else begin
   214     else begin
   182         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   215         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   183             AmmoCrate, lua_tointeger(L, 3));
   216             AmmoCrate, lua_tointeger(L, 3));
   184         lua_pushnumber(L, gear^.uid);
   217         lua_pushinteger(L, gear^.uid);
   185     end;
   218     end;
   186     lc_spawnammocrate := 1;
   219     lc_spawnammocrate := 1;
   187 end;
   220 end;
   188 
   221 
   189 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   222 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   194         lua_pushnil(L);
   227         lua_pushnil(L);
   195     end
   228     end
   196     else begin  
   229     else begin  
   197         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   230         gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
   198             UtilityCrate, lua_tointeger(L, 3));
   231             UtilityCrate, lua_tointeger(L, 3));
   199         lua_pushnumber(L, gear^.uid);
   232         lua_pushinteger(L, gear^.uid);
   200     end;
   233     end;
   201     lc_spawnutilitycrate := 1;
   234     lc_spawnutilitycrate := 1;
   202 end;
   235 end;
   203 
   236 
   204 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   237 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   221         dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
   254         dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
   222         dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
   255         dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
   223         t:= lua_tointeger(L, 7);
   256         t:= lua_tointeger(L, 7);
   224 
   257 
   225         gear:= AddGear(x, y, gt, s, dx, dy, t);
   258         gear:= AddGear(x, y, gt, s, dx, dy, t);
   226         lua_pushnumber(L, gear^.uid)
   259         lua_pushinteger(L, gear^.uid)
   227         end;
   260         end;
   228     lc_addgear:= 1; // 1 return value
   261     lc_addgear:= 1; // 1 return value
   229 end;
   262 end;
   230 
   263 
   231 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;
   264 function lc_deletegear(L : Plua_State) : LongInt; Cdecl;
   261         vgt:= TVisualGearType(lua_tointeger(L, 3));
   294         vgt:= TVisualGearType(lua_tointeger(L, 3));
   262         s:= lua_tointeger(L, 4);
   295         s:= lua_tointeger(L, 4);
   263         c:= lua_toboolean(L, 5);
   296         c:= lua_toboolean(L, 5);
   264 
   297 
   265         vg:= AddVisualGear(x, y, vgt, s, c); 
   298         vg:= AddVisualGear(x, y, vgt, s, c); 
   266         if vg <> nil then lua_pushnumber(L, vg^.uid)
   299         if vg <> nil then lua_pushinteger(L, vg^.uid)
   267         else lua_pushnumber(L, 0)
   300         else lua_pushinteger(L, 0)
   268         end;
   301         end;
   269     lc_addvisualgear:= 1; // 1 return value
   302     lc_addvisualgear:= 1; // 1 return value
   270 end;
   303 end;
   271 
   304 
   272 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   305 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   350         end
   383         end
   351     else
   384     else
   352         if FollowGear = nil then
   385         if FollowGear = nil then
   353             lua_pushnil(L)
   386             lua_pushnil(L)
   354         else
   387         else
   355             lua_pushnumber(L, FollowGear^.uid);
   388             lua_pushinteger(L, FollowGear^.uid);
   356     lc_getfollowgear:= 1; // 1 return value
   389     lc_getfollowgear:= 1; // 1 return value
   357 end;
   390 end;
   358 
   391 
   359 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   392 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   360 var gear : PGear;
   393 var gear : PGear;
   496         end
   529         end
   497     else
   530     else
   498         begin
   531         begin
   499         gear:= GearByUID(lua_tointeger(L, 1));
   532         gear:= GearByUID(lua_tointeger(L, 1));
   500         if gear <> nil then
   533         if gear <> nil then
   501             lua_pushnumber(L, gear^.Timer)
   534             lua_pushinteger(L, gear^.Timer)
   502         else
   535         else
   503             lua_pushnil(L);
   536             lua_pushnil(L);
   504         end;
   537         end;
   505     lc_gettimer:= 1
   538     lc_gettimer:= 1
   506 end;
   539 end;
   515         end
   548         end
   516     else
   549     else
   517         begin
   550         begin
   518         gear:= GearByUID(lua_tointeger(L, 1));
   551         gear:= GearByUID(lua_tointeger(L, 1));
   519         if gear <> nil then
   552         if gear <> nil then
   520             lua_pushnumber(L, gear^.Health)
   553             lua_pushinteger(L, gear^.Health)
   521         else
   554         else
   522             lua_pushnil(L);
   555             lua_pushnil(L);
   523         end;
   556         end;
   524     lc_gethealth:= 1
   557     lc_gethealth:= 1
   525 end;
   558 end;
   534         end
   567         end
   535     else
   568     else
   536         begin
   569         begin
   537         gear:= GearByUID(lua_tointeger(L, 1));
   570         gear:= GearByUID(lua_tointeger(L, 1));
   538         if gear <> nil then
   571         if gear <> nil then
   539             lua_pushnumber(L, hwRound(gear^.X))
   572             lua_pushinteger(L, hwRound(gear^.X))
   540         else
   573         else
   541             lua_pushnil(L);
   574             lua_pushnil(L);
   542         end;
   575         end;
   543     lc_getx:= 1
   576     lc_getx:= 1
   544 end;
   577 end;
   553         end
   586         end
   554     else
   587     else
   555         begin
   588         begin
   556         gear:= GearByUID(lua_tointeger(L, 1));
   589         gear:= GearByUID(lua_tointeger(L, 1));
   557         if gear <> nil then
   590         if gear <> nil then
   558             lua_pushnumber(L, hwRound(gear^.Y))
   591             lua_pushinteger(L, hwRound(gear^.Y))
   559         else
   592         else
   560             lua_pushnil(L);
   593             lua_pushnil(L);
   561         end;
   594         end;
   562     lc_gety:= 1
   595     lc_gety:= 1
   563 end;
   596 end;
   641                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then vgear^.FrameTicks:= 1;
   674                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then vgear^.FrameTicks:= 1;
   642                end;
   675                end;
   643             end
   676             end
   644         end;
   677         end;
   645     lc_hogsay:= 0
   678     lc_hogsay:= 0
       
   679 end;
       
   680 
       
   681 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
       
   682 var gear : PGear;
       
   683 begin
       
   684     if lua_gettop(L) <> 2 then
       
   685         begin
       
   686         LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
       
   687         end
       
   688     else
       
   689         begin
       
   690         gear:= GearByUID(lua_tointeger(L, 1));
       
   691         if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
   692             AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
       
   693         end;
       
   694     lc_addammo:= 0
   646 end;
   695 end;
   647 
   696 
   648 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
   697 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
   649 var gear : PGear;
   698 var gear : PGear;
   650 begin
   699 begin
  1300 
  1349 
  1301 for he:= Low(THogEffect) to High(THogEffect) do
  1350 for he:= Low(THogEffect) to High(THogEffect) do
  1302     ScriptSetInteger(EnumToStr(he), ord(he));
  1351     ScriptSetInteger(EnumToStr(he), ord(he));
  1303 
  1352 
  1304 // register functions
  1353 // register functions
       
  1354 lua_register(luaState, 'band', @lc_band);
       
  1355 lua_register(luaState, 'bor', @lc_bor);
       
  1356 lua_register(luaState, 'bnot', @lc_bnot);
  1305 lua_register(luaState, 'AddGear', @lc_addgear);
  1357 lua_register(luaState, 'AddGear', @lc_addgear);
  1306 lua_register(luaState, 'DeleteGear', @lc_deletegear);
  1358 lua_register(luaState, 'DeleteGear', @lc_deletegear);
  1307 lua_register(luaState, 'AddVisualGear', @lc_addvisualgear);
  1359 lua_register(luaState, 'AddVisualGear', @lc_addvisualgear);
  1308 lua_register(luaState, 'DeleteVisualGear', @lc_deletevisualgear);
  1360 lua_register(luaState, 'DeleteVisualGear', @lc_deletevisualgear);
  1309 lua_register(luaState, 'GetVisualGearValues', @lc_getvisualgearvalues);
  1361 lua_register(luaState, 'GetVisualGearValues', @lc_getvisualgearvalues);
  1323 lua_register(luaState, 'AddCaption', @lc_addcaption);
  1375 lua_register(luaState, 'AddCaption', @lc_addcaption);
  1324 lua_register(luaState, 'SetAmmo', @lc_setammo);
  1376 lua_register(luaState, 'SetAmmo', @lc_setammo);
  1325 lua_register(luaState, 'PlaySound', @lc_playsound);
  1377 lua_register(luaState, 'PlaySound', @lc_playsound);
  1326 lua_register(luaState, 'AddTeam', @lc_addteam);
  1378 lua_register(luaState, 'AddTeam', @lc_addteam);
  1327 lua_register(luaState, 'AddHog', @lc_addhog);
  1379 lua_register(luaState, 'AddHog', @lc_addhog);
       
  1380 lua_register(luaState, 'AddAmmo', @lc_addammo);
  1328 lua_register(luaState, 'SetHealth', @lc_sethealth);
  1381 lua_register(luaState, 'SetHealth', @lc_sethealth);
  1329 lua_register(luaState, 'GetHealth', @lc_gethealth);
  1382 lua_register(luaState, 'GetHealth', @lc_gethealth);
  1330 lua_register(luaState, 'SetEffect', @lc_seteffect);
  1383 lua_register(luaState, 'SetEffect', @lc_seteffect);
  1331 lua_register(luaState, 'GetHogClan', @lc_gethogclan);
  1384 lua_register(luaState, 'GetHogClan', @lc_gethogclan);
  1332 lua_register(luaState, 'GetHogTeamName', @lc_gethogteamname);
  1385 lua_register(luaState, 'GetHogTeamName', @lc_gethogteamname);