function init(bIsRestart)

	-- Initialise arena - backdrop_name, start_pos, is_restart
	init_level("level-train-interior", CCPoint(-50, -30), bIsRestart)
--	init_level("level-train-interior", CCPoint(610, -30), bIsRestart)

end

function onLevelIntro()

	camera_manager.ShakeScale = 0.1

	bot_manager.MaxBotsOnScreen = 2

	say("lumberjack", localize("TRAIN_INTERIOR_INTRO_1"), talkToFist)
	say("fist", localize("TRAIN_INTERIOR_INTRO_2"), fistTalk)
	say("lumberjack", localize("TRAIN_INTERIOR_INTRO_3"), talkToFist)
	say("fist", localize("TRAIN_INTERIOR_INTRO_4"), fistTalk)
	say("lumberjack", localize("TRAIN_INTERIOR_INTRO_5"), talkToFist, onLevelIntroEnd)

end


function spawn_flux_capacitor()

	local spawn_zone = game_manager.Background:getNodeByName("fluxCapacitor")
	spawn_zone:setVisible(false)
	local world_pos = spawn_zone:convertToWorldSpace(ccp(0,0))
	local backdrop = game_manager.Background
	local layer_pos = backdrop:getMainLayer():convertToNodeSpace(world_pos)

	local bot_desc = game_manager:actorDescWithFile("flux-capacitor.plist")

	local spawn_rect = spawn_zone:boundingBox()
	spawn_rect.origin = layer_pos
	spawn_rect.size.height = 0

	local bot_id = bot_manager:addBotToSpawnQueue(bot_desc, spawn_rect, 0)

		-- Tell us when spawned so we can fiddle with some stuff
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, fluxCapacitorSpawned)
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Killed, doFluxExplosion)
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Died, nextLevel)

end

function fluxCapacitorSpawned(bot)

	-- Time Machines can't walk silly!
	bot.Speed = 0

	flux_capacitor = bot

end

function doFluxExplosion()

	local particle = game_manager.Background:getParticleSystemByName("fluxParticle1")
	particle:resetSystem()

	particle = game_manager.Background:getParticleSystemByName("fluxParticle2")
	particle:resetSystem()

end


function onAmbush(bIsRestart)

	-- Restart from here
	if (bIsRestart ~= true) then
   		-- Make this a bit harder than the last ambush
		bot_manager.MaxBotsOnScreen = bot_manager.MaxBotsOnScreen + 1

		game_manager:setCheckpoint("onAmbush")
    end

	-- Spawn bad guys
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("deer.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("bib-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("jurassic-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("warriors-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("peasant-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("asscred-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("bison.plist"))
	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 3)

	return true
	
end

function onTimeMachineSpawn(bIsRestart)

	-- Restart from here
	if (bIsRestart ~= true) then
		game_manager:setCheckpoint("onTimeMachineSpawn")
    end

	spawn_flux_capacitor()

	return true

end

function onTimeMachineSpeech()

	-- Pause player movement
	disconnectControlsAndPauseBots()

	say("lumberjack", localize("TRAIN_INTERIOR_FLUX_1"), talkToFist)
	say("fist", localize("TRAIN_INTERIOR_FLUX_2"), fistTalk)
	say("lumberjack", localize("TRAIN_INTERIOR_FLUX_3"), talkToFist)
	say("fist", localize("TRAIN_INTERIOR_FLUX_4"), fistTalk)
	say("fist", localize("TRAIN_INTERIOR_FLUX_5"), fistTalk)
	say("lumberjack", localize("TRAIN_INTERIOR_FLUX_6"), talkToFist, onTimeMachineFight)

	return true

end

function onTimeMachineFight()

	connectControlsAndResumeBots()

	hud:showBossHealth(flux_capacitor)

end
