wormDelay = nil
testCondition = true
botPlayers = {}
isAnyBotsHere = false
currentConyards = {}
WorldLoaded = function()
  --get players
	mp0=Player.GetPlayer("Multi0")
	mp1=Player.GetPlayer("Multi1")
	mp2=Player.GetPlayer("Multi2")
	mp3=Player.GetPlayer("Multi3")
	mp4=Player.GetPlayer("Multi4")
	mp5=Player.GetPlayer("Multi5")
	mp6=Player.GetPlayer("Multi6")
	mp7=Player.GetPlayer("Multi7")
	mp8=Player.GetPlayer("Multi8")
	mp9=Player.GetPlayer("Multi9")
	mp10=Player.GetPlayer("Multi10")
	mp11=Player.GetPlayer("Multi11")
	mp12=Player.GetPlayer("Multi12")
	mp13=Player.GetPlayer("Multi13")
	mp14=Player.GetPlayer("Multi14")
	mp15=Player.GetPlayer("Multi15")
	mp16=Player.GetPlayer("Multi16")
	mp17=Player.GetPlayer("Multi17")
	mp18=Player.GetPlayer("Multi18")
	mp19=Player.GetPlayer("Multi19")
	mp20=Player.GetPlayer("Multi20")
	mp21=Player.GetPlayer("Multi21")
	mp22=Player.GetPlayer("Multi22")
	mp23=Player.GetPlayer("Multi23")
	mp24=Player.GetPlayer("Multi24")
	mp25=Player.GetPlayer("Multi25")
	mp26=Player.GetPlayer("Multi26")
	mp27=Player.GetPlayer("Multi27")
  players = {mp0, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12, mp13, mp14, mp15, mp16, mp17, mp18, mp19, mp20, mp21, mp22, mp23, mp24, mp25, mp26, mp27}
  -- freecarryall section
  for i, player in pairs(players)
  do
    if player.HasPrerequisites({"FreeCarry"})
    then
      addCarryToPlayer(player)
    end
    --adding bots to talbe
    if player.IsBot
    then
      table.insert(botPlayers, player)
    end  
  end  
  -- worm delay section
  wormDelay = Map.LobbyOption("WormsSpawnDelay")
  if tonumber(wormDelay) > 0
  then
    delay = tonumber(Map.LobbyOption("WormsSpawnDelay"))
    Trigger.AfterDelay(DateTime.Minutes(delay), function()
        wormSpawnFunction()  
     end)
  else
    wormSpawnFunction()
  end
-- testing bots locations:
  for i, bot in pairs(botPlayers)
  do
  isAnyBotsHere = true
  end  
end

Tick = function()
-- Bot capture protection scripts: executed every 2 sec,testing new Bots Conyards executed every 5 sec
  if isAnyBotsHere then
    if DateTime.GameTime % DateTime.Seconds(5) == 0
    then
      findConyards()
    end
    if DateTime.GameTime % DateTime.Seconds(2) == 0 then
      engiProtection()
    end
  end
end

function engiProtection()

  for i, conyard in pairs(currentConyards) do
    if conyard.IsDead then
        --Media.DisplayMessage("conyard dead")
    else
      actorsInRadius = Map.ActorsInCircle(conyard.CenterPosition, WDist.New(3036))
      --Media.DisplayMessage("testing")
      for _, actor in pairs(actorsInRadius) do
        if actor.Type == "engineer" then
          --Media.DisplayMessage(actor.Type)
          conyard.Sell()
          currentConyards[i] = nil
        end
      end
    end
  end
end

function addCarryToPlayer(player)
  local actors = player.GetActorsByType("mcv")
  local hasCarry = false
  for i, actor in pairs(actors)
  do
    if (actor.Type == "mcv") or (actor.Type == "construction_yard")
    then
      if (not hasCarry )
      then
        Reinforcements.Reinforce(player, { "carryall" }, { actor.Location }, 10)
        hasCarry = true
      end
    end
  end
end

function wormSpawnFunction()
    creepPlayer = Player.GetPlayer("Creeps")
    actorsSpawner = creepPlayer.GetActorsByType("wormspawner")
  if actorsSpawner ~= nil
  then
    for i, spawner in pairs(actorsSpawner)
    do
      if spawner.AcceptsCondition("WormSpamDelayed")
      then
        spawner.GrantCondition("WormSpamDelayed") 	
      end
    end
  end
end

function findConyards()
  local newConyards = {}
  local newLocations = {}
 for i, bot in pairs(botPlayers)
  do
    conyards = bot.GetActorsByType("construction_yard")
    for _, conyard in pairs(conyards) do
      table.insert(newConyards, conyard)
      --Media.DisplayMessage("adding",conyard.Type)
      --table.insert(newLocations, conyard.Location)
    end
  end
  currentConyards = newConyards
end
