Allow scripting to set arbitrary weapon counts
authornemo
Mon, 20 Jun 2011 18:58:26 -0400
changeset 5272 a85d331ab5bb
parent 5271 48d18e329298
child 5273 102728b20c4d
Allow scripting to set arbitrary weapon counts
hedgewars/uAmmos.pas
hedgewars/uScript.pas
--- a/hedgewars/uAmmos.pas	Mon Jun 20 15:21:27 2011 -0400
+++ b/hedgewars/uAmmos.pas	Mon Jun 20 18:58:26 2011 -0400
@@ -32,6 +32,7 @@
 procedure SetAmmoReinforcement(s: shortstring);
 procedure AssignStores;
 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
+procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
 function  HHHasAmmo(var Hedgehog: THedgehog; Ammo: TAmmoType): boolean;
 procedure PackAmmo(Ammo: PHHAmmo; Slot: LongInt);
 procedure OnUsedAmmo(var Hedgehog: THedgehog);
@@ -43,6 +44,7 @@
 procedure ResetWeapons;
 function  GetAmmoByNum(num: Longword): PHHAmmo;
 function  GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
+function  GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
 
 var StoreCnt: Longword;
 
@@ -139,13 +141,18 @@
 end;
 
 function GetAmmoEntry(var Hedgehog: THedgehog): PAmmo;
+begin
+GetAmmoEntry:= GetAmmoEntry(Hedgehog, Hedgehog.CurAmmoType)
+end;
+
+function GetAmmoEntry(var Hedgehog: THedgehog; am: TAmmoType): PAmmo;
 var ammoidx, slot: LongWord;
 begin
 with Hedgehog do
     begin
-    slot:= Ammoz[CurAmmoType].Slot;
+    slot:= Ammoz[am].Slot;
     ammoidx:= 0;
-    while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> CurAmmoType) do inc(ammoidx);
+    while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> am) do inc(ammoidx);
     GetAmmoEntry:= @Ammo^[slot, ammoidx];
     end
 end;
@@ -170,6 +177,12 @@
 end;
 
 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
+begin
+if GetAmmoEntry(Hedgehog, ammo)^.Count <> AMMO_INFINITE then
+    AddAmmo(Hedgehog, ammo, Ammoz[ammo].NumberInCase);
+end;
+
+procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType; cnt: LongWord);
 var ammos: TAmmoCounts;
     slot, ami: LongInt;
     hhammo: PHHAmmo;
@@ -184,11 +197,8 @@
         if hhammo^[slot, ami].Count > 0 then
            ammos[hhammo^[slot, ami].AmmoType]:= hhammo^[slot, ami].Count;
 
-if ammos[ammo] <> AMMO_INFINITE then
-   begin
-   inc(ammos[ammo], Ammoz[ammo].NumberInCase);
-   if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE
-   end;
+ammos[ammo]:= cnt;
+if ammos[ammo] > AMMO_INFINITE then ammos[ammo]:= AMMO_INFINITE;
 
 FillAmmoStore(hhammo, ammos)
 end;
--- a/hedgewars/uScript.pas	Mon Jun 20 15:21:27 2011 -0400
+++ b/hedgewars/uScript.pas	Mon Jun 20 18:58:26 2011 -0400
@@ -854,16 +854,14 @@
 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
 var gear : PGear;
 begin
-    if lua_gettop(L) <> 2 then
-        begin
-        LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
-        end
-    else
+    if (lua_gettop(L) = 3) or (lua_gettop(L) = 2) then
         begin
         gear:= GearByUID(lua_tointeger(L, 1));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
-            AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
-        end;
+            if lua_gettop(L) = 2 then AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)))
+            else AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L, 3))
+        end
+    else LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
     lc_addammo:= 0
 end;