function setup_levels()
  if space_warp == 0 and chickens_on_level == 0 and level_cooldown_timer > 0 then level_cooldown_timer = level_cooldown_timer - 1 end

  if level_cooldown_timer <= 0 then
    level_cooldown_timer = 50
      
    level = level + 1

    if level == 13 then 
      re_run_amount = re_run_amount + 1
      level = 1
    end

    if level == 1 or level == 2 then 
      for i = 1,8 do
        for o = 1,3 do
          if level == 1 then table.insert(chickenx,i*50-480) end
          if level == 2 then table.insert(chickenx,i*50+480) end
          table.insert(chickeny,(o-1)*45)
          table.insert(chicken_type,1)--math.random(14)

          table.insert(chicken_health, re_run_amount)

          chickens_on_level = chickens_on_level + 1
        end
      end
    end

    if level == 3 then
      for i = 1,12 do
        table.insert(chickenx,math.random(200) - 300)
        table.insert(chickeny,math.random(50))
        table.insert(chicken_type,16)
        table.insert(rock_move_speed, (math.random(400)/100)+2)
        table.insert(chicken_health, math.random(5)+10)
        
        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 4 then 
      for i = 1,24 do
        table.insert(chicken_angle, math.random(628) /100 - 3.14)
        table.insert(chickenx,i*50+480)
        table.insert(chickeny,45)
        table.insert(chicken_type,1)--math.random(14)

        table.insert(chicken_health, re_run_amount)

        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 5 then 
      for i = 1,24 do
        table.insert(chickenx,math.random(440))
        table.insert(chickeny,math.random(120))
        tmp = math.random(2)
        if tmp == 1 then table.insert(chickenxv,(math.random(400)/100)+1) else table.insert(chickenxv,((math.random(400)/100)*-1)-1) end
        tmp = math.random(2)
        if tmp == 1 then table.insert(chickenyv,(math.random(400)/100)+1) else table.insert(chickenyv,((math.random(400)/100)*-1)-1) end
        table.insert(chicken_type,1)--math.random(14)
        table.insert(chicken_health, re_run_amount)
        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 6 then
      for i = 1,20 do
        table.insert(chickenx,math.random(440))
        table.insert(chickeny,math.random(1000)-2480)
        table.insert(chicken_type,16)
        table.insert(rock_move_speed, (math.random(100)/100)+7)
        table.insert(chicken_health, 5)
        
        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 7 then
      for i = 1,(re_run_amount * 3) + 5 do
        table.insert(chickenx,math.random(440))
        table.insert(chickeny,math.random(30))
        table.insert(chicken_type,1)
        table.insert(chickenxv, (math.random(500)/100)+2)
        table.insert(chicken_health, re_run_amount)
        
        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 8 then
      in_wave = (re_run_amount * 2) + 3
      tmp2 = false
    end

    if level == 9 then 
      for i = 1,20 do
        table.insert(chickenx,math.random(440))
        table.insert(chickeny,math.random(50)-100)
        table.insert(chickenxv,(math.random(500)/100)-3)
        table.insert(chickenyv,(math.random(400)/100)+1)
        table.insert(chicken_type,16)--math.random(14)
        table.insert(chicken_health, 5)
        chickens_on_level = chickens_on_level + 1
      end
    end

    if level == 10 then 
      table.insert(chickenx,math.random(440))
      table.insert(chickeny,20)
      table.insert(chickenxv,re_run_amount+2)
      table.insert(chicken_type,18)--math.random(14)
      table.insert(chicken_health, (re_run_amount * 15)+30)
      chickens_on_level = chickens_on_level + 1
    end

    if level == 12 then 
      space_warp = 100
    end


  end
end

function move_chickens()
  if chickens_on_level > 0 or level == 8 then

    if level == 1 or level == 2 then
      for i = 1,chickens_on_level do
        if chickenx[i] < 0 then chicken_move_direction = "right" end
        if chickenx[i] > 430 then chicken_move_direction = "left" end
      end
      for i = 1,chickens_on_level do
        if chicken_move_direction == "right" then chickenx[i] = chickenx[i] + chicken_move_speed end
        if chicken_move_direction == "left" then chickenx[i] = chickenx[i] - chicken_move_speed end
      end
    end

    if level == 3 then
      for i = 1,chickens_on_level do
        chickenx[i] = chickenx[i] + rock_move_speed[i]
        chickeny[i] = chickeny[i] + rock_move_speed[i] * 0.25
      end
    end

    if level == 4 then
      for i = 1,chickens_on_level do
        chicken_angle[i] = chicken_angle[i] + 0.02
        chickenx[i] = 240 + math.cos(chicken_angle[i])*200
        chickeny[i] = (50 + math.sin(chicken_angle[i])*70)
      end
    end

    if level == 5 then
      for i = 1,chickens_on_level do
        chickenx[i] = chickenx[i] + chickenxv[i]
        chickeny[i] = chickeny[i] + chickenyv[i]
        if chickenx[i] < 3 and chickenxv[i] < 0 then chickenxv[i] = chickenxv[i] * -1 end
        if chickenx[i] > 440 and chickenxv[i] > 0 then chickenxv[i] = chickenxv[i] * -1 end
        if chickeny[i] < 3 and chickenyv[i] < 0 then chickenyv[i] = chickenyv[i] * -1 end
        if chickeny[i] > 120 and chickenyv[i] > 0 then chickenyv[i] = chickenyv[i] * -1 end
      end
    end

    if level == 6 then
      for i = 1,chickens_on_level do
        chickeny[i] = chickeny[i] + rock_move_speed[i]
      end
    end

    if level == 7 then
      for i = 1,chickens_on_level do
        chickenx[i] = chickenx[i] + chickenxv[i]
        chickeny[i] = chickeny[i] + 1
        if chickenx[i] < 3 and chickenxv[i] < 0 then chickenxv[i] = chickenxv[i] * -1 end
        if chickenx[i] > 440 and chickenxv[i] > 0 then chickenxv[i] = chickenxv[i] * -1 end
      end
    end

    if level == 8 then
      if in_wave >= 0 then
        if chickens_on_level == 0 and in_wave > 0 then
          if tmp2 then tmp2 = false else tmp2 = true end
          if tmp2 then
            for i = 1,20 do
              table.insert(chickenx,i*50-300)
              table.insert(chickeny,-50)
              table.insert(chicken_type,1)--math.random(14)
              table.insert(chicken_health, re_run_amount)
              chickens_on_level = chickens_on_level + 1
            end
            in_wave = in_wave - 1
          else
            for i = 1,20 do
              table.insert(chickenx,i*50)
              table.insert(chickeny,-50)
              table.insert(chicken_type,1)--math.random(14)
              table.insert(chicken_health, re_run_amount)
              chickens_on_level = chickens_on_level + 1
            end
            in_wave = in_wave - 1
          end
        end
        for i = 1,chickens_on_level do
          if tmp2 then chickenx[i] = chickenx[i] + re_run_amount + 1 end
          if not tmp2 then chickenx[i] = chickenx[i] - re_run_amount - 1 end
          chickeny[i] = chickeny[i] + (re_run_amount/2) + 2
          if chickeny[i] > 272 then table.insert(chickens_to_be_removed,i) end
        end
      end
    end

    if level == 9 then
      for i = 1,chickens_on_level do
        chickenx[i] = chickenx[i] + chickenxv[i]
        chickeny[i] = chickeny[i] + chickenyv[i]
      end
    end

    if level == 10 then
      for i = 1,chickens_on_level do
        chickenx[i] = chickenx[i] + chickenxv[i]
        if chickenx[i] < 3 and chickenxv[i] < 0 then chickenxv[i] = chickenxv[i] * -1 end
        if chickenx[i] > 370 and chickenxv[i] > 0 then chickenxv[i] = chickenxv[i] * -1 end
        for o = 1,(re_run_amount * 3) + 10 do
          if math.random(300) == 1 then
            table.insert(eggx,chickenx[i] + math.random(100))
            table.insert(eggy,chickeny[i] + 70)
            table.insert(egg_move_speed,(math.random(200)/100) + 1)
          end
        end
      end
    end

  end

  if level == 12 then
    if space_warp > 0 then space_warp = space_warp - 1 end
  end
end