Lua API: Add GetVisualGearType, onVisualGearAdd, onVisualGearDelete
authorWuzzy <almikes@aol.com>
Tue, 15 Nov 2016 00:42:44 +0100
changeset 11885 2eac7a96b342
parent 11884 c6eafb6f2735
child 11886 34ede05e4d4f
Lua API: Add GetVisualGearType, onVisualGearAdd, onVisualGearDelete Fixes #99 and #122.
ChangeLog.txt
hedgewars/uScript.pas
hedgewars/uVisualGearsList.pas
--- a/ChangeLog.txt	Tue Nov 15 00:17:31 2016 +0100
+++ b/ChangeLog.txt	Tue Nov 15 00:42:44 2016 +0100
@@ -60,6 +60,9 @@
 Lua-API:
  + New call: SetCinematicMode(enable) -- e.g. for cutscenes etc.
  + New call: GetAmmoName(ammoType) -- returns the localized name for the specified ammoType
+ + New call: GetVisualGearType(vgUid) -- returns the visual gear type
+ + New hook: onVisualGearAdd(vgUid) -- called when a visual gear is added
+ + New hook: onVisualGearDelete(vgUid) -- called when a visual gear is deleted
  + New variable: WorldEdge -- World edge type (weNone, weWrap, weBounce, weSea)
 
 0.9.21 -> 0.9.22
--- a/hedgewars/uScript.pas	Tue Nov 15 00:17:31 2016 +0100
+++ b/hedgewars/uScript.pas	Tue Nov 15 00:42:44 2016 +0100
@@ -843,6 +843,23 @@
     lc_deletevisualgear:= 1
 end;
 
+function lc_getvisualgeartype(L : Plua_State) : LongInt; Cdecl;
+var vg : PVisualGear;
+begin
+    if CheckLuaParamCount(L, 1, 'GetVisualGearType', 'vgUid') then
+        begin
+        vg := VisualGearByUID(lua_tointeger(L, 1));
+        if vg <> nil then
+            lua_pushinteger(L, ord(vg^.Kind))
+        else
+            lua_pushnil(L);
+        end
+    else
+        lua_pushnil(L); // return value on stack (nil)
+    lc_getvisualgeartype:= 1
+end;
+
+
 function lc_getvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
 var vg: PVisualGear;
 begin
@@ -3306,6 +3323,7 @@
 lua_register(luaState, _P'DeleteGear', @lc_deletegear);
 lua_register(luaState, _P'AddVisualGear', @lc_addvisualgear);
 lua_register(luaState, _P'DeleteVisualGear', @lc_deletevisualgear);
+lua_register(luaState, _P'GetVisualGearType', @lc_getvisualgeartype);
 lua_register(luaState, _P'GetVisualGearValues', @lc_getvisualgearvalues);
 lua_register(luaState, _P'SetVisualGearValues', @lc_setvisualgearvalues);
 lua_register(luaState, _P'GetGearValues', @lc_getgearvalues);
--- a/hedgewars/uVisualGearsList.pas	Tue Nov 15 00:17:31 2016 +0100
+++ b/hedgewars/uVisualGearsList.pas	Tue Nov 15 00:42:44 2016 +0100
@@ -36,7 +36,7 @@
     VisualGearLayers: array[0..6] of PVisualGear;
 
 implementation
-uses uCollisions, uFloat, uVariables, uConsts, uTextures, uVisualGearsHandlers;
+uses uCollisions, uFloat, uVariables, uConsts, uTextures, uVisualGearsHandlers, uScript;
 
 function AddVisualGear(X, Y: LongInt; Kind: TVisualGearType): PVisualGear; inline;
 begin
@@ -437,10 +437,12 @@
 VisualGearLayers[gear^.Layer]:= gear;
 
 AddVisualGear:= gear;
+ScriptCall('onVisualGearAdd', gear^.uid);
 end;
 
 procedure DeleteVisualGear(Gear: PVisualGear);
 begin
+    ScriptCall('onVisualGearDelete', Gear^.uid);
     FreeAndNilTexture(Gear^.Tex);
 
     if Gear^.NextGear <> nil then