SpawnCustomCrateAt: spawn crate at random position for x=y=0, also let it return the gear, and luabindings the gear uid
authorburp
Sat, 21 Aug 2010 22:54:43 +0200
changeset 3750 3aa85b5f3318
parent 3749 d0920e92008e
child 3751 a70c61c076ae
SpawnCustomCrateAt: spawn crate at random position for x=y=0, also let it return the gear, and luabindings the gear uid
hedgewars/uGears.pas
hedgewars/uScript.pas
--- a/hedgewars/uGears.pas	Sat Aug 21 22:36:59 2010 +0200
+++ b/hedgewars/uGears.pas	Sat Aug 21 22:54:43 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 SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword );
+function SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword ): PGear;
 procedure ProcessGears;
 procedure EndTurnCleanup;
 procedure ApplyDamage(Gear: PGear; Damage: Longword; Source: TDamageSource);
@@ -1539,12 +1539,11 @@
 CountGears:= count;
 end;
 
-procedure SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword);
+function SpawnCustomCrateAt(x, y: LongInt; crate: TCrateType; content: Longword): PGear;
 begin
     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
@@ -1564,6 +1563,10 @@
             AddCaption(GetEventString(eidNewUtilityPack), cWhiteColor, capgrpAmmoInfo);
             end;
     end;
+
+    if ( (x = 0) and (y = 0) ) then FindPlace(FollowGear, true, 0, LAND_WIDTH);
+
+    SpawnCustomCrateAt := FollowGear;
 end;
 
 procedure SpawnBoxOfSmth;
--- a/hedgewars/uScript.pas	Sat Aug 21 22:36:59 2010 +0200
+++ b/hedgewars/uScript.pas	Sat Aug 21 22:54:43 2010 +0200
@@ -118,40 +118,46 @@
 end;
 
 function lc_spawnhealthcrate(L: Plua_State) : LongInt; Cdecl;
+var gear: PGear;
 begin
     if lua_gettop(L) <> 2 then begin
         LuaError('Lua: Wrong number of parameters passed to SpawnHealthCrate!');
         lua_pushnil(L);
     end
     else begin
-        SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+        gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
             HealthCrate, 0);
+        lua_pushnumber(L, gear^.uid);
     end;
     lc_spawnhealthcrate := 1;        
 end;
 
 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
+var gear: PGear;
 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),
+        gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
             AmmoCrate, lua_tointeger(L, 3));
+        lua_pushnumber(L, gear^.uid);
     end;
     lc_spawnammocrate := 1;
 end;
 
 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
+var gear: PGear;
 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),
+        gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
             UtilityCrate, lua_tointeger(L, 3));
+        lua_pushnumber(L, gear^.uid);
     end;
     lc_spawnutilitycrate := 1;
 end;