hedgewars/uVisualGearsList.pas
changeset 14589 ab79cd4a7382
parent 14577 34e810295d08
child 14662 b390479f24c1
--- a/hedgewars/uVisualGearsList.pas	Mon Jan 14 17:59:32 2019 +0100
+++ b/hedgewars/uVisualGearsList.pas	Mon Jan 14 00:34:56 2019 +0100
@@ -33,7 +33,8 @@
     cExplFrameTicks = 110;
 
 var VGCounter: LongWord;
-    VisualGearLayers: array[0..6] of PVisualGear;
+    VisualGearLayersStart: array[0..6] of PVisualGear;
+    VisualGearLayersEnd: array[0..6] of PVisualGear;
 
 implementation
 uses uCollisions, uFloat, uVariables, uConsts, uTextures, uVisualGearsHandlers, uScript;
@@ -428,12 +429,15 @@
 
 if Layer <> -1 then gear^.Layer:= Layer;
 
-if VisualGearLayers[gear^.Layer] <> nil then
+if VisualGearLayersStart[gear^.Layer] = nil then
+    VisualGearLayersStart[gear^.Layer]:= gear;
+
+if VisualGearLayersEnd[gear^.Layer] <> nil then
     begin
-    VisualGearLayers[gear^.Layer]^.PrevGear:= gear;
-    gear^.NextGear:= VisualGearLayers[gear^.Layer]
+    VisualGearLayersEnd[gear^.Layer]^.NextGear:= gear;
+    gear^.PrevGear:= VisualGearLayersEnd[gear^.Layer]
     end;
-VisualGearLayers[gear^.Layer]:= gear;
+VisualGearLayersEnd[gear^.Layer]:= gear;
 
 AddVisualGear:= gear;
 ScriptCall('onVisualGearAdd', gear^.uid);
@@ -444,12 +448,19 @@
     ScriptCall('onVisualGearDelete', Gear^.uid);
     FreeAndNilTexture(Gear^.Tex);
 
-    if Gear^.NextGear <> nil then
-        Gear^.NextGear^.PrevGear:= Gear^.PrevGear;
+    if (Gear^.NextGear = nil) and (Gear^.PrevGear = nil) then
+        begin
+        VisualGearLayersStart[Gear^.Layer]:= nil;
+        VisualGearLayersEnd[Gear^.Layer]:= nil;
+        end;
     if Gear^.PrevGear <> nil then
         Gear^.PrevGear^.NextGear:= Gear^.NextGear
-    else
-        VisualGearLayers[Gear^.Layer]:= Gear^.NextGear;
+    else if Gear^.NextGear <> nil then
+        VisualGearLayersStart[Gear^.Layer]:= Gear^.NextGear;
+    if Gear^.NextGear <> nil then
+        Gear^.NextGear^.PrevGear:= Gear^.PrevGear
+    else if Gear^.PrevGear <> nil then
+        VisualGearLayersEnd[Gear^.Layer]:= Gear^.PrevGear;
 
     if lastVisualGearByUID = Gear then
         lastVisualGearByUID:= nil;
@@ -472,7 +483,7 @@
 // search in an order that is more likely to return layers they actually use.  Could perhaps track statistically AddVisualGear in uScript, since that is most likely the ones they want
 for i:= 2 to 5 do
     begin
-    vg:= VisualGearLayers[i mod 4];
+    vg:= VisualGearLayersStart[i mod 4];
     while vg <> nil do
         begin
         if vg^.uid = uid then