




function Initialise()
  ActRR = 1
  ActRast = 1
    -- remove WormSelect from scheme
    SetData("Land.Indestructable", 1)
    lib_SpawnCrate("crate")

	lib_SetupTeamInventory(0, "Inv_Player")
	lib_SetupTeamInventory(1, "Inv_Player")
	lib_SetupTeamInventory(2, "Inv_Player")
	lib_SetupTeamInventory(3, "Inv_Player")





    lock, scheme = EditContainer("GM.SchemeData")
    scheme.SelectWorm.Crate = 0
    scheme.SelectWorm.Ammo = 0
	scheme.Surrender.Crate = 0
	scheme.Surrender.Ammo = 0
    CloseContainer(lock)

    lib_SetupMultiplayer()

    g_DeadWorms = {0,0,0,0}
    -- disable all but the 1st worm on each team
    local WormIndex
    local WormsOnTeam = {0,0,0,0}
    for WormIndex=0,15 do
        local WormContainerName = lib_GetWormContainerName(WormIndex)
        local lock, worm = EditContainer(WormContainerName)
        if WormsOnTeam[worm.TeamIndex+1] > 0 then
           worm.Active = false 
        end
        CloseContainer(lock)
        WormsOnTeam[worm.TeamIndex+1] = WormsOnTeam[worm.TeamIndex+1]+1
    end



    SendMessage("WormManager.Reinitialise")
    SendMessage("GameLogic.ClearInventories")
   SetData("TurnTime", 30000)


    StartFirstTurn()


SetData("Text.TestComment",'Race!!!')
lib_Comment("Text.TestComment")


end

function Crate_Collected()

    
  SendMessage("GameLogic.Draw")
  SetData("Text.TestComment",'You Win!!!')
  lib_Comment("Text.TestComment")

  local CurrentWorm = GetData("ActiveWormIndex")
  local wormdataname = lib_GetWormContainerName(CurrentWorm)

  local lock, worm = EditContainer(wormdataname) 
    TimIndx = worm.TeamIndex  
  CloseContainer(lock) 

  WinningTeam = TimIndx
  SendIntMessage("GameLogic.Win", WinningTeam)
        
end


function TurnStarted()

    UpdateCounter()
end



function UpdateCounter()
    local GM = QueryContainer("GM.GameInitData")
    local NumOfWorms = { GM.T1_NumOfWorms, GM.T2_NumOfWorms, GM.T3_NumOfWorms, GM.T4_NumOfWorms }
    local Data = { "HUD.Counter.Team00", "HUD.Counter.Team01", "HUD.Counter.Team02", "HUD.Counter.Team03" }
    for team = 0,3 do
       if NumOfWorms[team+1]>0 then
          SetData(Data[team+1], NumOfWorms[team+1]-g_DeadWorms[team+1])
       end
    end
end







function Worm_Died()
    -- spawn a replacement if any left
    local Id = GetData("DeadWorm.Id")
    local worm = lib_QueryWormContainer(Id)
    local team = worm.TeamIndex
    g_DeadWorms[team+1] = g_DeadWorms[team+1] +0

    local GM = QueryContainer("GM.GameInitData")
    local NumOfWorms = { GM.T1_NumOfWorms, GM.T2_NumOfWorms, GM.T3_NumOfWorms, GM.T4_NumOfWorms }

    if g_DeadWorms[team+1] < NumOfWorms[team+1] then
    local WormIndex = GetData("DeadWorm.Id")
    local WormContainer = lib_QueryWormContainer(WormIndex)
    local team = WormContainer.TeamIndex
        local scheme = QueryContainer("GM.SchemeData")
        local lock, EditWorm = EditContainer(lib_GetWormContainerName(WormIndex))
        EditWorm.Energy = scheme.WormHealth
        EditWorm.Active = true
        CloseContainer(lock)
        SendIntMessage("Worm.Respawn", WormIndex)
    end
end


function DoOncePerTurnFunctions()

end

function TurnEnded()
   local RoundTimeRemaining = GetData("RoundTimeRemaining")
   if RoundTimeRemaining == 0 then
      SendMessage("GameLogic.Draw")
SetData("Text.TestComment",'Nothing')
lib_Comment("Text.TestComment")


   else
      CheckOneTeamVictory()      
   end
end
