Lua API: AddAmmo unselects weapons before it would remove current ammo from current hog
authorWuzzy <Wuzzy2@mail.ru>
Fri, 09 Feb 2018 21:04:53 +0100
changeset 12952 c8b7bc04eb4c
parent 12951 1a1a514aef2e
child 12953 f85a040d0350
Lua API: AddAmmo unselects weapons before it would remove current ammo from current hog
ChangeLog.txt
hedgewars/uScript.pas
--- a/ChangeLog.txt	Fri Feb 09 19:58:55 2018 +0100
+++ b/ChangeLog.txt	Fri Feb 09 21:04:53 2018 +0100
@@ -43,6 +43,7 @@
  + New call: SpawnSupplyCrate(x, y, content, [, amount]): Spawn ammo or utility crate, depending on content
  + New call: HealHog(gearUid, healthBoost[, showMessage[, tint]]): Heal hedgehog with graphical effects and message
  + New callback: onEndTurn(): Called at the end of a turn (when gears have settled)
+ * AddAmmo now automatically unselects weapon if it would remove current ammo from current hedgehog
  * Fix call: SetWeapon(amNothing) now unselects weapon
  * Fix global: TotalRounds was stuck at -1 for several turns
 
--- a/hedgewars/uScript.pas	Fri Feb 09 19:58:55 2018 +0100
+++ b/hedgewars/uScript.pas	Fri Feb 09 21:04:53 2018 +0100
@@ -1689,7 +1689,7 @@
 
 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
 var gear : PGear;
-    at, n: LongInt;
+    at, n, c: LongInt;
 const
     call = 'AddAmmo';
     params = 'gearUid, ammoType [, ammoCount]';
@@ -1697,14 +1697,19 @@
     if CheckAndFetchParamCount(L, 2, 3, call, params, n) then
         begin
         at:= LuaToAmmoTypeOrd(L, 2, call, params);
-        if at >= 0 then
+        if (at >= 0) and (TAmmoType(at) <> amNothing) then
             begin
             gear:= GearByUID(Trunc(lua_tonumber(L, 1)));
             if (gear <> nil) and (gear^.Hedgehog <> nil) then
                 if n = 2 then
                     AddAmmo(gear^.Hedgehog^, TAmmoType(at))
                 else
-                    SetAmmo(gear^.Hedgehog^, TAmmoType(at), Trunc(lua_tonumber(L, 3)))
+                    begin
+                    c:= Trunc(lua_tonumber(L, 3));
+                    if (c = 0) and (CurrentHedgehog = gear^.Hedgehog) and (gear^.Hedgehog^.CurAmmoType = TAmmoType(at)) then
+                        ParseCommand('setweap ' + char(0), true, true);
+                    SetAmmo(gear^.Hedgehog^, TAmmoType(at), c);
+                    end;
             end;
         end;
     lc_addammo:= 0