# HG changeset patch
# User Wuzzy <Wuzzy2@mail.ru>
# Date 1518152581 -3600
# Node ID e0da398805b6f76a3c3c2cbcad239e327cd2d5d6
# Parent  c27dd59a3ffe89bcd15dadbf836af8afe3696752
Lua API: Add SpawnSupplyCrate

diff -r c27dd59a3ffe -r e0da398805b6 ChangeLog.txt
--- a/ChangeLog.txt	Fri Feb 09 04:37:27 2018 +0100
+++ b/ChangeLog.txt	Fri Feb 09 06:03:01 2018 +0100
@@ -33,6 +33,7 @@
  + New call: SetLaserSight(bool): Toggle laser sight
  + New call: GetWind(): Returns current wind (approximation) from -100 to 100
  + New call: GetTeamName(teamIdx): Returns name of team with given index (starts at 0)
+ + New call: SpawnSupplyCrate(x, y, content, [, amount]): Spawn ammo or utility crate, depending on content
  + New callback: onEndTurn(): Called at the end of a turn (when gears have settled)
  * Fix call: SetWeapon(amNothing) now unselects weapon
  * Fix global: TotalRounds was stuck at -1 for several turns
diff -r c27dd59a3ffe -r e0da398805b6 hedgewars/uScript.pas
--- a/hedgewars/uScript.pas	Fri Feb 09 04:37:27 2018 +0100
+++ b/hedgewars/uScript.pas	Fri Feb 09 06:03:01 2018 +0100
@@ -771,6 +771,31 @@
     lc_spawnutilitycrate := 1;
 end;
 
+function lc_spawnsupplycrate(L: PLua_State): LongInt; Cdecl;
+var gear: PGear;
+    n, at:LongInt;
+    t:    TCrateType;
+begin
+    if CheckAndFetchParamCount(L, 3, 4, 'SpawnSupplyCrate', 'x, y, content [, amount]', n) then
+        begin
+        // Get crate type (ammo or utility)
+        at:= Trunc(lua_tonumber(L, 3));
+        if (Ammoz[TAmmoType(at)].Ammo.Propz and ammoprop_Utility) <> 0 then
+            t:= UtilityCrate
+        else
+            t:= AmmoCrate;
+        if n = 3 then
+             gear := SpawnCustomCrateAt(Trunc(lua_tonumber(L, 1)), Trunc(lua_tonumber(L, 2)), t, at, 0)
+        else gear := SpawnCustomCrateAt(Trunc(lua_tonumber(L, 1)), Trunc(lua_tonumber(L, 2)), t, at, Trunc(lua_tonumber(L, 4)));
+        if gear <> nil then
+             lua_pushnumber(L, gear^.uid)
+        else lua_pushnil(L);
+        end
+    else
+        lua_pushnil(L);
+    lc_spawnsupplycrate := 1;
+end;
+
 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
 var gear : PGear;
     x, y, s, t: LongInt;
@@ -3622,6 +3647,7 @@
 lua_register(luaState, _P'SpawnHealthCrate', @lc_spawnhealthcrate);
 lua_register(luaState, _P'SpawnAmmoCrate', @lc_spawnammocrate);
 lua_register(luaState, _P'SpawnUtilityCrate', @lc_spawnutilitycrate);
+lua_register(luaState, _P'SpawnSupplyCrate', @lc_spawnsupplycrate);
 lua_register(luaState, _P'SpawnFakeHealthCrate', @lc_spawnfakehealthcrate);
 lua_register(luaState, _P'SpawnFakeAmmoCrate', @lc_spawnfakeammocrate);
 lua_register(luaState, _P'SpawnFakeUtilityCrate', @lc_spawnfakeutilitycrate);