New Lua API function: GetAmmo, to get ammo config
authorWuzzy <Wuzzy2@mail.ru>
Sun, 08 Jul 2018 01:30:13 +0200
changeset 13454 794dcf69a5aa
parent 13453 8f746b6a477c
child 13455 38814954a248
New Lua API function: GetAmmo, to get ammo config
ChangeLog.txt
hedgewars/uAmmos.pas
hedgewars/uConsts.pas
hedgewars/uScript.pas
--- a/ChangeLog.txt	Sun Jul 08 00:16:33 2018 +0200
+++ b/ChangeLog.txt	Sun Jul 08 01:30:13 2018 +0200
@@ -33,6 +33,7 @@
  + New call: Retreat(time [, respectGetAwayTimeFactor): Force current turn into retreating mode
  + New call: GetAmmoTimer(gearUid, ammoType): Returns current set timer for given ammoType and hog gear in ms. Returns nil for non-timerable ammo
  + New call: EnableSwitchHog(): Enable hog switching
+ + New call: GetAmmo(ammoType): Returns ammo configuration (corresponds to SetAmmo)
  + New parameter: SetAmmoTexts: 5th param. showExtra: Set to false to hide texts like “Not yet available”
  * Fixed variable: TotalRounds was -1 (instead of 0) in first real round after hog placement phase
 
--- a/hedgewars/uAmmos.pas	Sun Jul 08 00:16:33 2018 +0200
+++ b/hedgewars/uAmmos.pas	Sun Jul 08 01:30:13 2018 +0200
@@ -79,7 +79,6 @@
 end;
 
 procedure AddAmmoStore;
-const probability: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
 var cnt: Longword;
     a: TAmmoType;
     ammos: TAmmoCounts;
@@ -99,7 +98,7 @@
     begin
     if a <> amNothing then
         begin
-        Ammoz[a].Probability:= probability[byte(ammoProbability[ord(a)]) - byte('0')];
+        Ammoz[a].Probability:= probabilityLevels[byte(ammoProbability[ord(a)]) - byte('0')];
         Ammoz[a].SkipTurns:= (byte(ammoDelay[ord(a)]) - byte('0'));
         Ammoz[a].NumberInCase:= (byte(ammoReinforcement[ord(a)]) - byte('0'));
         cnt:= byte(ammoLoadout[ord(a)]) - byte('0');
--- a/hedgewars/uConsts.pas	Sun Jul 08 00:16:33 2018 +0200
+++ b/hedgewars/uConsts.pas	Sun Jul 08 01:30:13 2018 +0200
@@ -315,6 +315,8 @@
     AMMO_INFINITE = 100;
     AMMO_FINITE_MAX = 99;
 
+    probabilityLevels: array [0..8] of LongWord = (0,20,30,60,100,200,400,600,800);
+
     // explosion flags
     //EXPLAllDamageInRadius = $00000001;  Completely unused for ages
     EXPLAutoSound         = $00000002;
--- a/hedgewars/uScript.pas	Sun Jul 08 00:16:33 2018 +0200
+++ b/hedgewars/uScript.pas	Sun Jul 08 01:30:13 2018 +0200
@@ -2591,6 +2591,42 @@
     lc_setammo:= 0
 end;
 
+
+function lc_getammo(L : Plua_State) : LongInt; Cdecl;
+var i, at, rawProb, probLevel: LongInt;
+const
+    call = 'GetAmmo';
+    params = 'ammoType';
+begin
+    if CheckLuaParamCount(L, 1, call, params) then
+        begin
+        at:= LuaToAmmoTypeOrd(L, 1, call, params);
+        if at >= 0 then
+            begin
+            // Ammo count
+            i:= Ammoz[TAmmoType(at)].Ammo.Count;
+            if i = AMMO_INFINITE then
+                i:= 9;
+            lua_pushnumber(L, i);
+            // Probability
+            rawProb:=  Ammoz[TAmmoType(at)].Probability;
+            probLevel:= -1;
+            for i := 0 to High(probabilityLevels) do
+                if rawProb = probabilityLevels[i] then
+                    probLevel:= i;
+            lua_pushnumber(L, probLevel);
+            // Delay in turns
+            lua_pushnumber(L, Ammoz[TAmmoType(at)].SkipTurns);
+            // Number in case
+            lua_pushnumber(L, Ammoz[TAmmoType(at)].NumberInCase);
+            lc_getammo:= 4
+            end
+        else
+            lc_getammo:= 0
+        end;
+end;
+
+
 function lc_setammodelay(L : Plua_State) : LongInt; Cdecl;
 var at: LongInt;
 const
@@ -3959,6 +3995,7 @@
 lua_register(luaState, _P'SetAmmoDescriptionAppendix', @lc_setammodescriptionappendix);
 lua_register(luaState, _P'AddCaption', @lc_addcaption);
 lua_register(luaState, _P'SetAmmo', @lc_setammo);
+lua_register(luaState, _P'GetAmmo', @lc_getammo);
 lua_register(luaState, _P'SetAmmoDelay', @lc_setammodelay);
 lua_register(luaState, _P'PlaySound', @lc_playsound);
 lua_register(luaState, _P'SetSoundMask', @lc_setsoundmask);