-- These functions handle making bots jump out of bus at various times

function DeliveryVehicle:getSkydiveDistances(numBots)
	local skydiveTimes = {}
	for _ = 1, math.ceil(0.3 * numBots) do
		table.insert(skydiveTimes, self.randomGenerator:NextNumber(self.travelBeforeActive, self.travelBeforeActive + 200))
	end

	for _ = 1, math.ceil(0.25 * numBots) do
		table.insert(skydiveTimes, self.randomGenerator:NextNumber(self.travelBeforeActive + 200, self.travelDistance * (2 / 7)))
	end

	for _ = 1, math.ceil(0.2 * numBots) do
		table.insert(skydiveTimes, self.randomGenerator:NextNumber(self.travelDistance * (2 / 7), self.travelDistance * (1 / 2)))
	end

	for _ = 1, math.ceil(0.15 * numBots) do
		table.insert(skydiveTimes,self.randomGenerator:NextNumber(self.travelDistance * (1 / 2), self.travelUntilInactive))
	end

	for _ = 1, math.ceil(0.1 * numBots) do
		table.insert(skydiveTimes, 100000)
	end

	table.sort(skydiveTimes)

	return skydiveTimes
end

function DeliveryVehicle:spawnSpecificPlayer(player)
	if not self.playersJumped[player] and tostring(PlayerMatchInfo.GetField(player, "teamNumber")) == string.sub(self.currentModel.Name, 16) then
		self.playersJumped[player] = true
		self:spawnPlayerFromVehicle(player)
		EventData:FireClient(player, "ExitedDeliveryVehicle")
	end
end

function DeliveryVehicle:manageSkydivingForBots()
	coroutine.wrap(function()
		local botsInGame = {}
		for _, player in pairs(Players:GetPlayers()) do
			if Util.playerIsBot(player) then
				table.insert(botsInGame, player)
			end
		end
		local skydiveDistances = self:getSkydiveDistances(#botsInGame)

		local curID = 1
		while curID <= #botsInGame and self:getCurrentPos() < self.travelUntilInactive do -- stop trying to skydive players when skydiving period ends
			if skydiveDistances[curID] <= self:getCurrentPos() then
				self:spawnSpecificPlayer(botsInGame[curID])
				curID = curID + 1
			else
				wait()
			end
		end
	end)()
end

return DeliveryVehicle
