Scripting changes. Add override of mapgen, templatefilter. Expose waterline, add "GetAmmoCount". Default to amNothing in uAmmos if entry is not found.
authornemo
Wed, 24 Aug 2011 15:09:41 -0400
changeset 5676 a655dfab27d7
parent 5675 cd10d6cba644
child 5677 017cadd88378
Scripting changes. Add override of mapgen, templatefilter. Expose waterline, add "GetAmmoCount". Default to amNothing in uAmmos if entry is not found.
hedgewars/uAmmos.pas
hedgewars/uScript.pas
--- a/hedgewars/uAmmos.pas	Wed Aug 24 13:33:53 2011 -0400
+++ b/hedgewars/uAmmos.pas	Wed Aug 24 15:09:41 2011 -0400
@@ -156,7 +156,8 @@
     ammoidx:= 0;
     while (ammoidx < cMaxSlotAmmoIndex) and (Ammo^[slot, ammoidx].AmmoType <> am) do inc(ammoidx);
     GetAmmoEntry:= @Ammo^[slot, ammoidx];
-    end
+    if (Ammo^[slot, ammoidx].AmmoType <> am) then GetAmmoEntry:= GetAmmoEntry(Hedgehog, amNothing)
+    end;
 end;
 
 procedure AssignStores;
@@ -180,9 +181,10 @@
 
 procedure AddAmmo(var Hedgehog: THedgehog; ammo: TAmmoType);
 var cnt: LongWord;
+    a: PAmmo;
 begin
-cnt:= GetAmmoEntry(Hedgehog, ammo)^.Count;
-if cnt <> AMMO_INFINITE then
+a:= GetAmmoEntry(Hedgehog, ammo);
+if (a^.AmmoType = amNothing) or (a^.Count <> AMMO_INFINITE) then
     begin
     inc(cnt, Ammoz[ammo].NumberInCase);
     AddAmmo(Hedgehog, ammo, cnt)
--- a/hedgewars/uScript.pas	Wed Aug 24 13:33:53 2011 -0400
+++ b/hedgewars/uScript.pas	Wed Aug 24 15:09:41 2011 -0400
@@ -940,6 +940,25 @@
     lc_addammo:= 0
 end;
 
+function lc_getammocount(L : Plua_State) : LongInt; Cdecl;
+var gear : PGear;
+    ammo : PAmmo;
+begin
+    if (lua_gettop(L) = 2) then
+        begin
+        gear:= GearByUID(lua_tointeger(L, 1));
+        if (gear <> nil) and (gear^.Hedgehog <> nil) then 
+            begin
+            ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
+            if ammo^.AmmoType = amNothing then lua_pushinteger(L, 0)
+            else lua_pushinteger(L, ammo^.Count)
+            end
+        else lua_pushinteger(L, 0)
+        end
+    else LuaError('Lua: Wrong number of parameters passed to GetAmmoCount!');
+    lc_getammocount:= 0
+end;
+
 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
 var gear : PGear;
 begin
@@ -1544,6 +1563,8 @@
 // push game variables so they may be modified by the script
 ScriptSetInteger('GameFlags', GameFlags);
 ScriptSetString('Seed', cSeed);
+ScriptSetInteger('TemplateFilter', cTemplateFilter);
+ScriptSetInteger('MapGen', cMapGen);
 ScriptSetInteger('ScreenHeight', cScreenHeight);
 ScriptSetInteger('ScreenWidth', cScreenWidth);
 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
@@ -1568,6 +1589,8 @@
 
 // pop game variables
 ParseCommand('seed ' + ScriptGetString('Seed'), true);
+ParseCommand('template_filter ' + IntToStr(ScriptGetInteger('TemplateFilter')), true);
+ParseCommand('mapgen' + IntToStr(ScriptGetInteger('MapGen')), true);
 ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
 ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
 ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
@@ -1627,6 +1650,7 @@
 ScriptSetInteger('GameTime', GameTicks);
 ScriptSetInteger('RealTime', RealTicks);
 ScriptSetInteger('TotalRounds', TotalRounds);
+ScriptSetInteger('WaterLine', cWaterLine);
 if (CurrentHedgehog <> nil) and (CurrentHedgehog^.Gear <> nil) then
     ScriptSetInteger('CurrentHedgehog', CurrentHedgehog^.Gear^.UID)
 else
@@ -1904,6 +1928,7 @@
 lua_register(luaState, 'AddTeam', @lc_addteam);
 lua_register(luaState, 'AddHog', @lc_addhog);
 lua_register(luaState, 'AddAmmo', @lc_addammo);
+lua_register(luaState, 'GetAmmoCount', @lc_getammocount);
 lua_register(luaState, 'SetHealth', @lc_sethealth);
 lua_register(luaState, 'GetHealth', @lc_gethealth);
 lua_register(luaState, 'SetEffect', @lc_seteffect);