-- CS mode (Castle Sturm, Castle Seige)
function Initialise()


  StartTimer("Gir", 500)        
  ActRR = 0                     
  ActRast = 1               
   StartedSuddenDeath = false   



-- Setup of weapon damage

     local lock, weap = EditContainer("kWeaponBazooka") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponGrenade") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponClusterGrenade") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("firecluster") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponHomingMissile") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponShotgun") 
       weap.LandDamageRadius = 0
       weap.LandDamageMagnitude = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponSniperRifle") 
       weap.LandDamageRadius = 0
       weap.LandDamageMagnitude = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponHolyHandGrenade") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponBananaBomb") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponAirstrike") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponDynamite") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponLandmine") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponConcreteDonkey") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponLandmineCluster") 
       weap.LandDamageRadius = 0
     CloseContainer(lock)
     local lock, weap = EditContainer("kWeaponSentryGun") 
       weap.DeathLandDamageRadius = 0
     CloseContainer(lock)
   
--end setup damage

   SetupScheme()
    SetData("Worm.FallDamageRatio", 0) 
    SetData("Bubble.Radius", 100) 
   SetData("TurnTime", 30000)

    SendMessage("GameLogic.SetNoFallDamage")
   lib_SetupMultiplayerWormsAndTeams()
	lib_SetupTeamInventory(0, "Inv_defend")
	lib_SetupTeamInventory(1, "Inv_attack")
	lib_SetupTeamInventory(2, "Inv_attack")
	lib_SetupTeamInventory(3, "Inv_defend")
   SendMessage("WormManager.Reinitialise")
    SendMessage("GameLogic.ClearInventories")

    cr = 1
    lib_SpawnCrate("gravity")





   local scheme = QueryContainer("GM.SchemeData")
   if scheme.RoundTime == 0 then
      StartSuddenDeath() -- do this after worms are set up to readjust health
      local GameOver = GetData("FCS.GameOver")
      -- do no further processing if sudden death drew the game
      if GameOver ~= 0 then
         return 
      end
   end
   
   if scheme.MineFactoryOn == true then
      SendMessage("GameLogic.CreateRandMineFactory")
   end
   
   if scheme.TelepadsOn == true then
       SendMessage("GameLogic.PlaceTelepads")       
   end
   
   SetData("Camera.StartOfTurnCamera","Default")
   
   SetWormpotModes()
   

   WaitingForStartFirstTurn = false
   StartFirstTurn()
--   Activity = GetData("ObjectCount.Active")
--   if Activity == 0 then
--		WaitingForStartFirstTurn = false
--        StartFirstTurn()
--	else
--		WaitingForStartFirstTurn = true
--	end




end


function Crate_Collected()
 local CrateIndex = GetData("Crate.Index")
   SetData("Text.TestComment",CrateIndex)
    if  CrateIndex == 1 then
       cr = 2
       lib_SpawnCrate("girder")
    elseif  CrateIndex == 2 then
       cr = 3
       lib_SpawnCrate("crate")
    elseif  CrateIndex == 3 then


  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
end








function DoOncePerTurnFunctions()
   -- skip all this if the end of the deathmatch
   SendMessage("WormManager.GetActiveAlliances")
   AllianceCount = GetData("AllianceCount")
   if AllianceCount >1 then
      CheckSuddenDeath()

      if cr == 1 then
	SendIntMessage("Crate.Delete",1)
	lib_SpawnCrate("gravity")
      elseif cr == 2 then
	SendIntMessage("Crate.Delete",2)
	lib_SpawnCrate("girder")
      elseif cr == 3 then
	SendIntMessage("Crate.Delete",3)
	lib_SpawnCrate("crate")
      end


  local RandCr = math.random(3) -- Генерируем число для выброса случайных ящиков на пляж



     if RandCr == 1 then



     elseif RandCr == 2 then



     elseif RandCr == 3 then


     end

      SendMessage("GameLogic.StartMineFactory")
      DoWormpotOncePerTurnFunctions()
   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
    local GM = QueryContainer("GM.GameInitData")
    local NumOfWorms = { GM.T1_NumOfWorms, GM.T2_NumOfWorms, GM.T3_NumOfWorms, GM.T4_NumOfWorms }

    if 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 = 50
        EditWorm.Active = true
        CloseContainer(lock)
        SendIntMessage("Worm.Respawn", WormIndex)
    end
end