* SpawnCustomCrateAt procedure + lua bindings
authorburp
Tue, 10 Aug 2010 21:57:33 +0200
changeset 3734 304a83637eb4
parent 3733 447860ec0093
child 3735 26de8c8a5b5d
child 3736 d8982f9e7e2c
* SpawnCustomCrateAt procedure + lua bindings
hedgewars/uConsts.pas
hedgewars/uGears.pas
hedgewars/uScript.pas
share/hedgewars/Data/Maps/FlightJoust/map.lua
--- a/hedgewars/uConsts.pas	Sat Aug 07 23:32:43 2010 -0400
+++ b/hedgewars/uConsts.pas	Tue Aug 10 21:57:33 2010 +0200
@@ -133,6 +133,8 @@
             amLaserSight, amVampiric, amSniperRifle, amJetpack, amMolotov, amBirdy, amPortalGun,
             amPiano, amGasBomb, amSineGun, amFlamethrower, amSMine, amHammer);
 
+    TCrateType = (HealthCrate, AmmoCrate, UtilityCrate);
+
     THWFont = (fnt16, fntBig, fntSmall, CJKfnt16, CJKfntBig, CJKfntSmall);
 
     TCapGroup = (capgrpGameState, capgrpAmmoinfo, capgrpVolume,
--- a/hedgewars/uGears.pas	Sat Aug 07 23:32:43 2010 -0400
+++ b/hedgewars/uGears.pas	Tue Aug 10 21:57:33 2010 +0200
@@ -81,7 +81,7 @@
 procedure initModule;
 procedure freeModule;
 function  AddGear(X, Y: LongInt; Kind: TGearType; State: Longword; dX, dY: hwFloat; Timer: LongWord): PGear;
-procedure SpawnHealthCrate(x, y: LongInt);
+procedure SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword );
 procedure ProcessGears;
 procedure EndTurnCleanup;
 procedure ApplyDamage(Gear: PGear; Damage: Longword; Source: TDamageSource);
@@ -1539,12 +1539,31 @@
 CountGears:= count;
 end;
 
-procedure SpawnHealthCrate(x, y: LongInt);
+procedure SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword);
 begin
-    FollowGear:= AddGear(x, y, gtCase, 0, _0, _0, 0);
-    FollowGear^.Health:= 25;
-    FollowGear^.Pos:= posCaseHealth;
-    AddCaption(GetEventString(eidNewHealthPack), cWhiteColor, capgrpAmmoInfo);
+    FollowGear := AddGear(x, y, gtCase, 0, _0, _0, 0);
+    cCaseFactor := 0;
+
+    if (content < ord(Low(TAmmoType))) then content := 0;
+    if (content > ord(High(TAmmoType))) then content := ord(High(TAmmoType));
+
+    case crate of
+        HealthCrate: begin
+            FollowGear^.Health := 25;
+            FollowGear^.Pos := posCaseHealth;
+            AddCaption(GetEventString(eidNewHealthPack), cWhiteColor, capgrpAmmoInfo);
+            end;
+        AmmoCrate: begin
+            FollowGear^.Pos := posCaseAmmo;
+            FollowGear^.State := content;
+            AddCaption(GetEventString(eidNewAmmoPack), cWhiteColor, capgrpAmmoInfo);
+            end;
+        UtilityCrate: begin
+            FollowGear^.Pos := posCaseUtility;
+            FollowGear^.State := content;
+            AddCaption(GetEventString(eidNewUtilityPack), cWhiteColor, capgrpAmmoInfo);
+            end;
+    end;
 end;
 
 procedure SpawnBoxOfSmth;
--- a/hedgewars/uScript.pas	Sat Aug 07 23:32:43 2010 -0400
+++ b/hedgewars/uScript.pas	Tue Aug 10 21:57:33 2010 +0200
@@ -118,19 +118,42 @@
 end;
 
 function lc_spawnhealthcrate(L: Plua_State) : LongInt; Cdecl;
-var x, y: LongInt;
 begin
     if lua_gettop(L) <> 2 then begin
         LuaError('Lua: Wrong number of parameters passed to SpawnHealthCrate!');
         lua_pushnil(L);
     end
     else begin
-        x:= lua_tointeger(L, 1);
-        y:= lua_tointeger(L, 2);
-        cCaseFactor := 0;
-        SpawnHealthCrate(x, y);
+        SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+            HealthCrate, 0);
     end;
-    lc_spawnhealthCrate := 1;
+    lc_spawnhealthcrate := 1;        
+end;
+
+function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
+begin
+    if lua_gettop(L) <> 3 then begin
+        LuaError('Lua: Wrong number of parameters passed to SpawnAmmoCrate!');
+        lua_pushnil(L);
+    end
+    else begin
+        SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+            AmmoCrate, lua_tointeger(L, 3));
+    end;
+    lc_spawnammocrate := 1;
+end;
+
+function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
+begin
+    if lua_gettop(L) <> 3 then begin
+        LuaError('Lua: Wrong number of parameters passed to SpawnUtilityCrate!');
+        lua_pushnil(L);
+    end
+    else begin  
+        SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+            UtilityCrate, lua_tointeger(L, 3));
+    end;
+    lc_spawnutilitycrate := 1;
 end;
 
 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
@@ -879,6 +902,8 @@
 // register functions
 lua_register(luaState, 'AddGear', @lc_addgear);
 lua_register(luaState, 'SpawnHealthCrate', @lc_spawnhealthcrate);
+lua_register(luaState, 'SpawnAmmoCrate', @lc_spawnammocrate);
+lua_register(luaState, 'SpawnUtilityCrate', @lc_spawnutilitycrate);
 lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
 lua_register(luaState, 'GetGearType', @lc_getgeartype);
 lua_register(luaState, 'EndGame', @lc_endgame);
--- a/share/hedgewars/Data/Maps/FlightJoust/map.lua	Sat Aug 07 23:32:43 2010 -0400
+++ b/share/hedgewars/Data/Maps/FlightJoust/map.lua	Tue Aug 10 21:57:33 2010 +0200
@@ -1,79 +1,79 @@
-local hogs = {}
-local spawncrate = 0
-
-function mapM_(func, tbl)
-    for i,v in pairs(tbl) do
-        func(v)
-    end 
-end
-
-function map(func, tbl)
-    local newtbl = {}
-    for i,v in pairs(tbl) do
-        newtbl[i] = func(v)
-    end 
-    return newtbl
-end
-
-function filter(func, tbl)
-    local newtbl = {}
-    for i,v in pairs(tbl) do
-        if func(v) then
-            table.insert(newtbl, v)
-        end
-    end
-    return newtbl
-end
-
-function onGameInit()
-    GameFlags = gfSolidLand + gfDivideTeams
-    TurnTime = 10000
-    CaseFreq = 0 
-    LandAdds = 0 
-    Explosives = 0 
-    Delay = 500 
-    SuddenDeathTurns = 99999 -- "disable" sudden death
-    Theme = Compost
-end
-
-function onGameStart()
-    local offset = 50
-    local team1hh = filter(function(h) return GetHogClan(h) == 0 end, hogs)
-    local team2hh = filter(function(h) return GetHogClan(h) == 1 end, hogs)
-
-    for i,h in ipairs(team1hh) do
-        SetGearPosition(h, 250+(i-1)*offset, 1000)
-    end
-    for i,h in ipairs(team2hh) do
-        SetGearPosition(h, 3500-(i-1)*offset, 1000)
-    end
-
-    SpawnHealthCrate(1800, 1150)
-end
-
-function onAmmoStoreInit()
-    SetAmmo(amRCPlane, 9, 0, 0, 0)
-    SetAmmo(amSkip, 9, 0, 0, 0)
-end
-
-function onGearAdd(gear)
-    if GetGearType(gear) == gtRCPlane then
-        SetTimer(gear,60000)
-    end 
-    if GetGearType(gear) == gtHedgehog then
-        table.insert(hogs, gear)
-    end 
-end
-
-function onGameTick()
-    if (TurnTimeLeft == 9999 and spawncrate == 1) then
-        SpawnHealthCrate(1800, 1150)
-        spawncrate = 0
-    end
-end
-
-function onGearDelete(gear)
-    if GetGearType(gear) == gtCase then
-        spawncrate = 1
-    end
+local hogs = {}
+local spawncrate = 0
+
+function mapM_(func, tbl)
+    for i,v in pairs(tbl) do
+        func(v)
+    end 
+end
+
+function map(func, tbl)
+    local newtbl = {}
+    for i,v in pairs(tbl) do
+        newtbl[i] = func(v)
+    end 
+    return newtbl
+end
+
+function filter(func, tbl)
+    local newtbl = {}
+    for i,v in pairs(tbl) do
+        if func(v) then
+            table.insert(newtbl, v)
+        end
+    end
+    return newtbl
+end
+
+function onGameInit()
+    GameFlags = gfSolidLand + gfDivideTeams
+    TurnTime = 10000
+    CaseFreq = 0 
+    LandAdds = 0 
+    Explosives = 0 
+    Delay = 500 
+    SuddenDeathTurns = 99999 -- "disable" sudden death
+    Theme = Compost
+end
+
+function onGameStart()
+    local offset = 50
+    local team1hh = filter(function(h) return GetHogClan(h) == 0 end, hogs)
+    local team2hh = filter(function(h) return GetHogClan(h) == 1 end, hogs)
+
+    for i,h in ipairs(team1hh) do
+        SetGearPosition(h, 250+(i-1)*offset, 1000)
+    end
+    for i,h in ipairs(team2hh) do
+        SetGearPosition(h, 3500-(i-1)*offset, 1000)
+    end
+
+    SpawnHealthCrate(1800, 1150)
+end
+
+function onAmmoStoreInit()
+    SetAmmo(amRCPlane, 9, 0, 0, 0)
+    SetAmmo(amSkip, 9, 0, 0, 0)
+end
+
+function onGearAdd(gear)
+    if GetGearType(gear) == gtRCPlane then
+        SetTimer(gear,60000)
+    end 
+    if GetGearType(gear) == gtHedgehog then
+        table.insert(hogs, gear)
+    end 
+end
+
+function onGameTick()
+    if (TurnTimeLeft == 9999 and spawncrate == 1) then
+        SpawnHealthCrate(1800, 1150)
+        spawncrate = 0
+    end
+end
+
+function onGearDelete(gear)
+    if GetGearType(gear) == gtCase then
+        spawncrate = 1
+    end
 end
\ No newline at end of file