function Initialise()  
    MyPositions = 1
    SWdelay = 4
    Hdelay = 4
    CCdelay = 4
    DDdelay = 4
    RedT = 2
    BlueT = 2
    GreenT = 2
    YellowT = 2
    RedZ = 3
    BlueZ = 3
    GreenZ = 3
    YellowZ = 3
    numbr = 0
    numbb = 0
    numbg = 0
    numby = 0
    ChooseSTType()

	lib_SetupMultiplayer()
	SendMessage("WormManager.Reinitialise")


	StartFirstTurn()

   SpawnRandomStarsRed()
   SpawnRandomStarsBlue()
   SpawnRandomStarsGreen()
   SpawnRandomStarsYellow()

    SetData("HUD.Counter.Active", 1)
    SetData("HUD.Counter.Value", 15)

    lock, scheme = EditContainer("GM.SchemeData")
    scheme.WeaponChance = 0
    scheme.UtilityChance = 0
    scheme.MysteryChance = 0
    scheme.HealthChance = 0
    CloseContainer(lock)

   SpawnAllTriggers()

	SendMessage("GameLogic.ClearInventories")
	Index = 0
	InventoryName = lib_GetAllianceInventoryName(Index)
	while InventoryName ~= "" do
	 local Lock,Container = EditContainer(InventoryName)
	 Container.Jetpack = 2
	 Container.Parachute = -1
	 Container.ChangeWorm = 2
	 Container.Shotgun = -1
	 Container.FirePunch = -1
         Container.PoisonArrow = -1
         Container.Grenade = -1
         Container.Bazooka = 15
         Container.ClusterGrenade = 15
         Container.HomingMissile = 8
         Container.Dynamite = 8
         Container.SuperSheep = 3
         Container.HolyHandGrenade = 3
	 CloseContainer(Lock) 
	 Index = Index + 1
	 InventoryName = lib_GetAllianceInventoryName(Index)
        end

   for NumberTeam = 0 ,3 do
         local Delaylock, Delay = EditContainer("Inventory" .. (NumberTeam) .. ".WeaponDelays") 
         Delay.PoisonArrow = -1
         Delay.Grenade = -1
         Delay.Bazooka = -1
         Delay.ClusterGrenade = -1
         Delay.HomingMissile = -1
         Delay.Dynamite = -1
         Delay.SuperSheep = -1
         Delay.HolyHandGrenade = -1
         CloseContainer(Delaylock)
   end

end


function SpawnRandomStarsRed()
    x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    for i = 1 ,15 do
     x[i] = math.random(1,21)
     n = i - 1
     RedCheck()
     lib_SpawnCrate("Star"..(x[i]))
     numbr = numbr + 1
    end
end


function RedCheck()
    while n ~= 0 do
     if x[i] ~= x[n] then
       n = n - 1
     else
       x[i] = math.random(1,21)
       n = i - 1
     end
    end
end


function SpawnRandomStarsBlue()
    x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    for i = 1 ,15 do
     x[i] = math.random(22,42)
     n = i - 1
     BlueCheck()
     lib_SpawnCrate("Star"..(x[i]))
     numbb = numbb + 1
    end
end


function BlueCheck()
    while n ~= 0 do
     if x[i] ~= x[n] then
       n = n - 1
     else
       x[i] = math.random(22,42)
       n = i - 1
     end
    end
end


function SpawnRandomStarsGreen()
    x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    for i = 1 ,15 do
     x[i] = math.random(43,63)
     n = i - 1
     GreenCheck()
     lib_SpawnCrate("Star"..(x[i]))
     numbg = numbg + 1
    end
end


function GreenCheck()
    while n ~= 0 do
     if x[i] ~= x[n] then
       n = n - 1
     else
       x[i] = math.random(43,63)
       n = i - 1
     end
    end
end


function SpawnRandomStarsYellow()
    x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    for i = 1 ,15 do
     x[i] = math.random(64,84)
     n = i - 1
     YellowCheck()
     lib_SpawnCrate("Star"..(x[i]))
     numby = numby + 1
    end
end


function YellowCheck()
    while n ~= 0 do
     if x[i] ~= x[n] then
       n = n - 1
     else
       x[i] = math.random(64,84)
       n = i - 1
     end
    end
end


function Crate_Collected()
    local CrateIndex = GetData("Crate.Index")
    if CrateIndex > 8 then
      local StarNumber = CrateIndex - 8
      lib_SpawnCrate("Star"..(StarNumber))
    end
end


function ChooseSTType()
    STtype = {1, 2, 3, 4}
    for i = 1 ,4 do
     STtype[i] = math.random(0,3)
     n = i - 1
     while n ~= 0 do
       if STtype[i] ~= STtype[n] then
         n = n - 1
       else
         STtype[i] = math.random(0,3)
         n = i - 1
       end
     end
    end
end


function Trigger_Collected()
   local CurrentW = GetData("ActiveWormIndex")
   local Collector = GetData("Trigger.Collector")
   TrInd = GetData("Trigger.Index")
   local worm = lib_QueryWormContainer(CurrentW)
   TmInd = worm.TeamIndex 
   if Collector==CurrentW then
     if (TmInd==0) and  (TrInd<5) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==0) and  (TrInd==5) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==0) and  (TrInd==6) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==0) and  (TrInd==7) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==0) and  (TrInd==8) then

        Tower3()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==0) and  (TrInd==9) then

        SpecialTower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==10) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==11) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==12) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==13) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==14) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==15) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==16) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==17) then

        Tower3()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==1) and  (TrInd==18) then

        SpecialTower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==19) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==20) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==21) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==22) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==23) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==24) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==25) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==26) then

        Tower3()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==2) and  (TrInd==27) then

        SpecialTower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==28) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==29) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==30) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==31) then

        Tower()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==32) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==33) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==34) then

        Tower2()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==35) then

        Tower3()

        StartTimer("TurnOn", 50)
     end

     if (TmInd==3) and  (TrInd==36) then

        SpecialTower()

        StartTimer("TurnOn", 50)
     end
   end
   if Collector~=CurrentW then
      local wormdataname = lib_GetWormContainerName(Collector)
      CopyContainer(wormdataname, "Worm.BeforeRespawning")
                                                                          
      UnspawningWorms = true
         SendIntMessage("WXWormManager.UnspawnWorm", Collector)
         
         lib_SetupWorm(Collector, "Worm.BeforeRespawning")
             
         
         local lock, worm = EditContainer(wormdataname) 
            worm.Active = true 
            worm.PlayedInGame = true 
         CloseContainer(lock) 
             
         SendIntMessage("Worm.Respawn", Collector)
    
      UnspawningWorms = false 
      StartTimer("TurnOn", 150)
   end
end 


function TurnOn() 

      SendIntMessage("GameLogic.DestroyTrigger",1)
      SendIntMessage("GameLogic.DestroyTrigger",2)
      SendIntMessage("GameLogic.DestroyTrigger",3)
      SendIntMessage("GameLogic.DestroyTrigger",4)
      SendIntMessage("GameLogic.DestroyTrigger",5)
      SendIntMessage("GameLogic.DestroyTrigger",6)
      SendIntMessage("GameLogic.DestroyTrigger",7)
      SendIntMessage("GameLogic.DestroyTrigger",8)
      SendIntMessage("GameLogic.DestroyTrigger",9)
      SendIntMessage("GameLogic.DestroyTrigger",10)
      SendIntMessage("GameLogic.DestroyTrigger",11)
      SendIntMessage("GameLogic.DestroyTrigger",12)
      SendIntMessage("GameLogic.DestroyTrigger",13)
      SendIntMessage("GameLogic.DestroyTrigger",14)
      SendIntMessage("GameLogic.DestroyTrigger",15)
      SendIntMessage("GameLogic.DestroyTrigger",16)
      SendIntMessage("GameLogic.DestroyTrigger",17)
      SendIntMessage("GameLogic.DestroyTrigger",18)
      SendIntMessage("GameLogic.DestroyTrigger",19)
      SendIntMessage("GameLogic.DestroyTrigger",20)
      SendIntMessage("GameLogic.DestroyTrigger",21)
      SendIntMessage("GameLogic.DestroyTrigger",22)
      SendIntMessage("GameLogic.DestroyTrigger",23)
      SendIntMessage("GameLogic.DestroyTrigger",24)
      SendIntMessage("GameLogic.DestroyTrigger",25)
      SendIntMessage("GameLogic.DestroyTrigger",26)
      SendIntMessage("GameLogic.DestroyTrigger",27)
      SendIntMessage("GameLogic.DestroyTrigger",28)
      SendIntMessage("GameLogic.DestroyTrigger",29)
      SendIntMessage("GameLogic.DestroyTrigger",30)
      SendIntMessage("GameLogic.DestroyTrigger",31)
      SendIntMessage("GameLogic.DestroyTrigger",32)
      SendIntMessage("GameLogic.DestroyTrigger",33)
      SendIntMessage("GameLogic.DestroyTrigger",34)
      SendIntMessage("GameLogic.DestroyTrigger",35)
      SendIntMessage("GameLogic.DestroyTrigger",36)


   for NumberTeam = 0 ,3 do
         local Delaylock, Delay = EditContainer("Inventory" .. (NumberTeam) .. ".WeaponDelays") 
         Delay.PoisonArrow = -1
         Delay.Grenade = -1
         Delay.Bazooka = -1
         Delay.ClusterGrenade = -1
         Delay.HomingMissile = -1
         Delay.Dynamite = -1
         Delay.SuperSheep = -1
         Delay.HolyHandGrenade = -1
         CloseContainer(Delaylock)
   end

      SpawnAllTriggers()

end


function Trigger_Destroyed()
   TrInd = GetData("Trigger.Index")
   lib_SpawnTrigger("Trigger" .. (TrInd))
end


function Trigger_Sunk()
   TrInd = GetData("Trigger.Index")
   lib_SpawnTrigger("Trigger" .. (TrInd))
end


function Tower()
   local Delaylock, Delay = EditContainer("Inventory" .. (TmInd) .. ".WeaponDelays") 
   Delay.PoisonArrow = 0
   Delay.Grenade = 0
   CloseContainer(Delaylock)
end


function Tower2()
   if TmInd == 0 then
      local Delaylock, Delay = EditContainer("Inventory0.WeaponDelays") 
      Delay.PoisonArrow = RedT
      Delay.Grenade = RedT
      Delay.Bazooka = RedT
      Delay.ClusterGrenade = RedT
      CloseContainer(Delaylock)
   end
   if TmInd == 1 then
      local Delaylock, Delay = EditContainer("Inventory1.WeaponDelays") 
      Delay.PoisonArrow = BlueT
      Delay.Grenade = BlueT
      Delay.Bazooka = BlueT
      Delay.ClusterGrenade = BlueT
      CloseContainer(Delaylock)
   end
   if TmInd == 2 then
      local Delaylock, Delay = EditContainer("Inventory2.WeaponDelays") 
      Delay.PoisonArrow = GreenT
      Delay.Grenade = GreenT
      Delay.Bazooka = GreenT
      Delay.ClusterGrenade = GreenT
      CloseContainer(Delaylock)
   end
   if TmInd == 3 then
      local Delaylock, Delay = EditContainer("Inventory3.WeaponDelays") 
      Delay.PoisonArrow = YellowT
      Delay.Grenade = YellowT
      Delay.Bazooka = YellowT
      Delay.ClusterGrenade = YellowT
      CloseContainer(Delaylock)
   end
end


function Tower3()
   if TmInd == 0 then
      local Delaylock, Delay = EditContainer("Inventory0.WeaponDelays") 
      Delay.PoisonArrow = RedZ
      Delay.Grenade = RedZ
      Delay.Bazooka = RedZ
      Delay.ClusterGrenade = RedZ
      Delay.HomingMissile = RedZ
      Delay.Dynamite = RedZ
      CloseContainer(Delaylock)
   end
   if TmInd == 1 then
      local Delaylock, Delay = EditContainer("Inventory1.WeaponDelays") 
      Delay.PoisonArrow = BlueZ
      Delay.Grenade = BlueZ
      Delay.Bazooka = BlueZ
      Delay.ClusterGrenade = BlueZ
      Delay.HomingMissile = BlueZ
      Delay.Dynamite = BlueZ
      CloseContainer(Delaylock)
   end
   if TmInd == 2 then
      local Delaylock, Delay = EditContainer("Inventory2.WeaponDelays") 
      Delay.PoisonArrow = GreenZ
      Delay.Grenade = GreenZ
      Delay.Bazooka = GreenZ
      Delay.ClusterGrenade = GreenZ
      Delay.HomingMissile = GreenZ
      Delay.Dynamite = GreenZ
      CloseContainer(Delaylock)
   end
   if TmInd == 3 then
      local Delaylock, Delay = EditContainer("Inventory3.WeaponDelays") 
      Delay.PoisonArrow = YellowZ
      Delay.Grenade = YellowZ
      Delay.Bazooka = YellowZ
      Delay.ClusterGrenade = YellowZ
      Delay.HomingMissile = YellowZ
      Delay.Dynamite = YellowZ
      CloseContainer(Delaylock)
   end
end


function SpecialTower()
 
   if (TmInd == STtype[1]) and (Hdelay == 0) then
     local hcratenum = TmInd + 1
     lib_SpawnCrate("HealthCrate" .. (hcratenum))
     Hdelay = 1
   end

   if TmInd == STtype[2] then
     local Delaylock, Delay = EditContainer("Inventory" .. (TmInd) .. ".WeaponDelays") 
     Delay.SuperSheep = SWdelay
     Delay.HolyHandGrenade = SWdelay
     CloseContainer(Delaylock)
   end

   if (TmInd == STtype[3]) and (CCdelay == 0) then
     cratenum = TmInd + 1
     WSelect()
     lib_SpawnCrate("Crate" .. (cratenum))
     CCdelay = 1
   end

   if (TmInd == STtype[4]) and (DDdelay == 0) then
     SetData("DoubleDamage",1)
     SendMessage("DoubleDamage.Activated")
     prev=GetData("Comment.Sdeath.1")
     SetData("Comment.Sdeath.1", "Double damage activated" ) 
     SetData("CommentaryPanel.Comment", "Comment.Sdeath.1" )
     SendMessage("CommentaryPanel.ScriptText")
     SetData("Comment.Sdeath.1", prev)
     DDdelay = 1
   end

   if (TmInd == STtype[4]) and (DDdelay ~= 0) then
     prev=GetData("Comment.Sdeath.1")
     SetData("Comment.Sdeath.1", "Delay active. " .. (DDdelay) .. " turns left" )
     SetData("CommentaryPanel.Comment", "Comment.Sdeath.1" )
     SendMessage("CommentaryPanel.ScriptText")
     SetData("Comment.Sdeath.1", prev)
   end

   if (TmInd == STtype[3]) and (CCdelay ~= 0) then
     prev=GetData("Comment.Sdeath.1")
     SetData("Comment.Sdeath.1", "Delay active. " .. (CCdelay) .. " turns left" )
     SetData("CommentaryPanel.Comment", "Comment.Sdeath.1" )
     SendMessage("CommentaryPanel.ScriptText")
     SetData("Comment.Sdeath.1", prev)
   end

   if (TmInd == STtype[1]) and (Hdelay ~= 0) then
     prev=GetData("Comment.Sdeath.1")
     SetData("Comment.Sdeath.1", "Delay active. " .. (Hdelay) .. " turns left" )  
     SetData("CommentaryPanel.Comment", "Comment.Sdeath.1" )
     SendMessage("CommentaryPanel.ScriptText")
     SetData("Comment.Sdeath.1", prev)
   end
end


function TurnStarted()
   SetData("DoubleDamage",0)
   local CurrentW = GetData("ActiveWormIndex")
   local worm = lib_QueryWormContainer(CurrentW)
   TmInd = worm.TeamIndex 
   if (SWdelay ~= 0) and (TmInd == STtype[2]) then
     SWdelay = SWdelay - 1
   end

   if (Hdelay ~= 0) and (TmInd == STtype[1]) then
     Hdelay = Hdelay - 1
   end

   if (CCdelay ~= 0) and (TmInd == STtype[3]) then
     CCdelay = CCdelay - 1
   end

   if (DDdelay ~= 0) and (TmInd == STtype[4]) then
     DDdelay = DDdelay - 1
   end

   if (RedT ~= 0) and (TmInd == 0) then
     RedT = RedT - 1
   end

   if (BlueT ~= 0) and (TmInd == 1) then
      BlueT = BlueT - 1
   end

   if (GreenT ~= 0) and (TmInd == 2) then
     GreenT = GreenT - 1
   end

   if (YellowT ~= 0) and (TmInd == 3) then
     YellowT = YellowT - 1
   end

   if (RedZ ~= 0) and (TmInd == 0) then
     RedZ = RedZ - 1
   end

   if (BlueZ ~= 0) and (TmInd == 1) then
      BlueZ = BlueZ - 1
   end

   if (GreenZ ~= 0) and (TmInd == 2) then
     GreenZ = GreenZ - 1
   end

   if (YellowZ ~= 0) and (TmInd == 3) then
     YellowZ = YellowZ - 1
   end

   if TmInd == 0 then
     SetData("HUD.Counter.Value", numbr)
   end
   if TmInd == 1 then
     SetData("HUD.Counter.Value", numbb)
   end
   if TmInd == 2 then
     SetData("HUD.Counter.Value", numbg)
   end
   if TmInd == 3 then
     SetData("HUD.Counter.Value", numby)
   end
   
   StartTimer("TurnOn", 50)
end


function WSelect()
   c=math.random(1,6)
   
   if c == 1 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "weapon"
     crate.Contents = "kWeaponBazooka"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
   if c == 2 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "weapon"
     crate.Contents = "kWeaponClusterGrenade"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
   if c == 3 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "weapon"
     crate.Contents = "kWeaponHomingMissile"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
   if c == 4 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "weapon"
     crate.Contents = "kWeaponDynamite"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
   if c == 5 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "Utility"
     crate.Contents = "kUtilityJetpack"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
   if c == 6 then
     lock, crate = EditContainer("Crate" .. (cratenum)) 
     crate.Type = "Utility"
     crate.Contents = "kUtilityChangeWorm"
     crate.CustomGraphic = ""
     CloseContainer(lock)
   end
end

function SpawnAllTriggers()
   lib_SpawnTrigger("Trigger1")
   lib_SpawnTrigger("Trigger2")
   lib_SpawnTrigger("Trigger3")
   lib_SpawnTrigger("Trigger4")
   lib_SpawnTrigger("Trigger5")
   lib_SpawnTrigger("Trigger6")
   lib_SpawnTrigger("Trigger7")
   lib_SpawnTrigger("Trigger8")
   lib_SpawnTrigger("Trigger9")
   lib_SpawnTrigger("Trigger10")
   lib_SpawnTrigger("Trigger11")
   lib_SpawnTrigger("Trigger12")
   lib_SpawnTrigger("Trigger13")
   lib_SpawnTrigger("Trigger14")
   lib_SpawnTrigger("Trigger15")
   lib_SpawnTrigger("Trigger16")
   lib_SpawnTrigger("Trigger17")
   lib_SpawnTrigger("Trigger18")
   lib_SpawnTrigger("Trigger19")
   lib_SpawnTrigger("Trigger20")
   lib_SpawnTrigger("Trigger21")
   lib_SpawnTrigger("Trigger22")
   lib_SpawnTrigger("Trigger23")
   lib_SpawnTrigger("Trigger24")
   lib_SpawnTrigger("Trigger25")
   lib_SpawnTrigger("Trigger26")
   lib_SpawnTrigger("Trigger27")
   lib_SpawnTrigger("Trigger28")
   lib_SpawnTrigger("Trigger29")
   lib_SpawnTrigger("Trigger30")
   lib_SpawnTrigger("Trigger31")
   lib_SpawnTrigger("Trigger32")
   lib_SpawnTrigger("Trigger33")
   lib_SpawnTrigger("Trigger34")
   lib_SpawnTrigger("Trigger35")
   lib_SpawnTrigger("Trigger36")
end


function Crate_Destroyed()
   local CurrentW = GetData("ActiveWormIndex")
   local worm = lib_QueryWormContainer(CurrentW)
   local TmInd = worm.TeamIndex 
    local CrateIndex = GetData("Crate.Index")
    if (CrateIndex > 8 and CrateIndex < 30) and (numbr ~= 0) then
      numbr = numbr -1
      if TmInd == 0 then
        SetData("HUD.Counter.Value", numbr)
      end
    end
    if (CrateIndex > 29 and CrateIndex < 51) and (numbb ~= 0) then
      numbb = numbb -1
      if TmInd == 1 then
        SetData("HUD.Counter.Value", numbb)
      end
    end
    if (CrateIndex > 50 and CrateIndex < 72) and (numbg ~= 0) then
      numbg = numbg -1
      if TmInd == 2 then
        SetData("HUD.Counter.Value", numbg)
      end
    end
    if (CrateIndex > 71 and CrateIndex < 93) and (numby ~= 0) then
      numby = numby -1
      if TmInd == 3 then
        SetData("HUD.Counter.Value", numby)
      end
    end
end


function Crate_Sunk()
   local CurrentW = GetData("ActiveWormIndex")
   local worm = lib_QueryWormContainer(CurrentW)
   local TmInd = worm.TeamIndex 
    local CrateIndex = GetData("Crate.Index")
    if (CrateIndex > 8 and CrateIndex < 22) and (numbr ~= 0) then
      numbr = numbr -1
      if TmInd == 0 then
        SetData("HUD.Counter.Value", numbr)
      end
    end
    if (CrateIndex > 21 and CrateIndex < 43) and (numbb ~= 0) then
      numbb = numbb -1
      if TmInd == 1 then
        SetData("HUD.Counter.Value", numbb)
      end
    end
    if (CrateIndex > 42 and CrateIndex < 64) and (numbg ~= 0) then
      numbg = numbg -1
      if TmInd == 2 then
        SetData("HUD.Counter.Value", numbg)
      end
    end
    if (CrateIndex > 63 and CrateIndex < 85) and (numby ~= 0) then
      numby = numby -1
      if TmInd == 3 then
        SetData("HUD.Counter.Value", numby)
      end
    end
end


function DoOncePerTurnFunctions()
    local WormIndex
    for WormIndex=0,15 do
        local worm = QueryContainer(lib_GetWormContainerName(WormIndex))
        if worm.TeamIndex == 0 and numbr == 0 then
        SendIntMessage("WXWormManager.UnspawnWorm", WormIndex)
        end
        if worm.TeamIndex == 1 and numbb == 0 then
        SendIntMessage("WXWormManager.UnspawnWorm", WormIndex)
        end
        if worm.TeamIndex == 2 and numbg == 0 then
        SendIntMessage("WXWormManager.UnspawnWorm", WormIndex)
        end
        if worm.TeamIndex == 3 and numby == 0 then
        SendIntMessage("WXWormManager.UnspawnWorm", WormIndex)
        end
    end
end


function Worm_Died()
   -- if there is only one team left abort the turn now rather than wait on
	 if UnspawningWorms == false then
		 SendMessage("WormManager.GetActiveAlliances")
		 AllianceCount = GetData("AllianceCount")
		 if AllianceCount < 2 then
				SendMessage("Weapon.Delete")
				SendMessage("Utility.Delete")
				SendMessage("Timer.EndTurn") 
				SendMessage("Weapon.DisableWeaponChange")
				EndTurn()
		 end
	 end
end