function init(bIsRestart)

	-- Initialise arena - backdrop_name, start_pos, is_restart
	init_level("level-train-platform", CCPoint(-50, -20), bIsRestart)

end

function onLevelIntro()

	bot_manager.MaxBotsOnScreen = 3

	-- say("lumberjack", "Woah! What is this place?", talkToFist)
	-- say("fist", "The heart of the bear's time travel plan.", fistTalk)
	-- say("fist", "It echos their twisted agenda through history.", fistTalk)
	-- say("lumberjack", "I see... Just like Gangham Style!", talkToFist)
	-- say("fist", "It's best if you don't think too much about this stuff.", fistTalk, onLevelIntroEnd)

say("lumberjack", localize("TRAIN_PLATFORM_INTRO_1"), talkToFist)
say("fist", localize("TRAIN_PLATFORM_INTRO_2"), fistTalk)
say("lumberjack", localize("TRAIN_PLATFORM_INTRO_3"), talkToFist)
say("fist", localize("TRAIN_PLATFORM_INTRO_4"), fistTalk)
say("fist", localize("TRAIN_PLATFORM_INTRO_5"), fistTalk)
say("fist", localize("TRAIN_PLATFORM_INTRO_6"), fistTalk)
say("fist", localize("TRAIN_PLATFORM_INTRO_7"), fistTalk)
say("fist", localize("TRAIN_PLATFORM_INTRO_8"), fistTalk)
say("fist", localize("TRAIN_PLATFORM_INTRO_9"), fistTalk)
say("lumberjack", localize("TRAIN_PLATFORM_INTRO_10"), talkToFist)
say("fist", localize("TRAIN_PLATFORM_INTRO_11"), fistTalk, onLevelIntroEnd)

end

function onAmbush1(bIsRestart) onAmbush(bIsRestart, "onAmbush1", 1); return true; end
function onAmbush2(bIsRestart) onAmbush(bIsRestart, "onAmbush2", 2); return true; end
function onAmbush3(bIsRestart) onAmbush(bIsRestart, "onAmbush3", 3); return true; end
function onAmbush4(bIsRestart) onAmbush(bIsRestart, "onAmbush4", 4); return true; end

function onAmbush(bIsRestart, ambush_id, ambush_num)

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

	local backdrop = game_manager.Background

	if (ambush_num == 4) then
		backdrop:playAnimation("train_arrive_final", onFinalTrainArrived)
	else
		backdrop:playAnimation("train_arrive", onTrainArrived)
	end

	local block_node = game_manager.Background:getNodeByName("screenBlock" .. ambush_num)
	local bounds = boundingBox(block_node)
	camera_manager:setRightEdgeCollision(bounds:getMaxX())

	return true
end

function onTrainArrived()

	local backdrop = game_manager.Background
	backdrop:playAnimation("open_doors", onTrainDoorsOpened)

	bot_manager.MaxBotsOnScreen = 8

	-- Some useful structures...
	gBotIDToSpawnZone = {}
	gBotIDToStartPos = {}

	for i = 1, 8, 1 do
		spawnBotsFromZone(i, 1)
	end
--[[
	-- Simple test code
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("bib-gruntbear.plist"))
	startAmbush(bot_descs, 1)
--]]

end

function onFinalTrainArrived()

	-- Last ditch ambush
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("jurassic-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("bib-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"))

	bot_manager.MaxBotsOnScreen = 8

	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 3)

end

function spawnBotsFromZone(zone_num, num_bots)

--	CCLuaLog("spawnBotsFromZone("..zone_num..","..num_bots..")")

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

	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("jurassic-gruntbear.plist"))
	table.insert(bot_descs, game_manager:actorDescWithFile("bib-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"))

	local spawn_rect = spawn_zone:boundingBox()
	spawn_rect.origin = layer_pos

	for i = 1, num_bots, 1 do

		-- Get random bot desc
		bot_index = math.random(table.getn(bot_descs))
		local bot_desc = bot_descs[bot_index];


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

		gBotIDToSpawnZone[bot_id] = spawn_zone

		-- Tell us when spawned so we can fiddle with some stuff
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Spawned, trainBotSpawned)

		-- Check if we can stop camera block now...
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_Killed, checkStopCameraBlock)
	end

end

function posToString(pos)

	return "("..pos.x..","..pos.y..")"

end

function trainBotSpawned(bot)

	local bot_id = bot_manager:getBotID(bot)
--	CCLuaLog("Bot Spawned - ID " .. bot_id)

	local spawn_zone = gBotIDToSpawnZone[bot_id]
	local spawn_pos = spawn_zone:getPositionLua()

--	CCLuaLog("SpawnZone pos " .. posToString(spawn_pos))

	-- Keep track of where we originally spawned
	gBotIDToStartPos[bot_id] = bot:getPositionLua()

	bot:removeFromCollisionManager()

	-- Get middle of spawn zone
	local spawn_zone_size = spawn_zone:getContentSize()
	bot:setPosition(ccp(spawn_zone_size.width / 2, spawn_zone_size.height / 2))
	spawn_zone:addChild(bot)

	bot.State = "Disabled"

--[[
	local train = game_manager.Background:getNodeByName("train")

	local bot_pos = bot:getPositionLua()

	local spawn_test = game_manager.Background:getNodeByName("spawnZone" .. "1")
	local spawn_pos = spawn_test:getPositionLua()

	local world_pos = spawn_test:convertToWorldSpace(ccp(0,0))
	local train_pos = train:convertToNodeSpace(world_pos)

	CCLuaLog("spawn_pos - " .. posToString(spawn_pos))
	CCLuaLog("world_pos - " .. posToString(world_pos))
	CCLuaLog("train_pos - " .. posToString(train_pos))

	bot:setPosition(spawn_pos)

	bot:setParent(train)

	bot.State = "Disabled"
]]
end

function onTrainDoorsOpened()

	game_manager:startTimer(3, onCloseTrainDoors)

	for bot_id, spawn_zone in pairs(gBotIDToSpawnZone) do

		local bot = bot_manager:getBotFromID(bot_id)

	--	bot.State = "Seek"
		bot.State = "Wander"
		local start_pos = gBotIDToStartPos[bot_id]
		bot:setPosition(start_pos)
		bot:setTargetPosition(CCPoint(start_pos.x, start_pos.y - 14))
		bot:setTargetPositionLocked(true)
		bot_manager:triggerOnBotEvent(bot_id, kBotTrigger_ReachedTarget, onTrainBotAlighted)

		bot:removeFromParentAndCleanup(false)
		bot:addToCollisionManager()

	end

end

function onTrainBotAlighted(bot)

	bot:setTargetPositionLocked(false)

end

function onCloseTrainDoors()

	local backdrop = game_manager.Background
	backdrop:playAnimation("close_doors", onTrainDoorsClosed)

end

function onTrainDoorsClosed()

	local backdrop = game_manager.Background
	backdrop:playAnimation("train_leave", onTrainLeft)

end

function onTrainLeft()

end

function onAmbushOLD()

	local spawn_test = game_manager.Background:getNodeByName("spawnTest1")
	local world_pos = spawn_test:convertToWorldSpace(ccp(0,0))
	local backdrop = game_manager.Background
	local layer_pos = backdrop:getMainLayer():convertToNodeSpace(world_pos)

	local bot_desc = game_manager:actorDescWithFile("gruntbear.plist")

	local spawn_rect = spawn_test:boundingBox()
	spawn_rect.origin = layer_pos

	local bear_id = bot_manager:addBotToSpawnQueue(bot_desc, spawn_rect)

	-- Restart from here
--[[	game_manager:setCheckpoint("onAmbush")

	bot_manager.MaxBotsOnScreen = bot_manager.MaxBotsOnScreen + 1
                
	-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("gruntbear.plist"))

	-- Spawn lots of enemies
	startAmbush(bot_descs, bot_manager.MaxBotsOnScreen * 2)
]]
	return true
	
end

function onBossAmbush()

	-- Restart from here
	game_manager:setCheckpoint("onBossAmbush")
                
	-- Spawn regular enemies
	local bot_descs = {}
	table.insert(bot_descs, game_manager:actorDescWithFile("gruntbear.plist"))

	-- Spawn lots of enemies
	boss_id = startBossAmbush(bot_descs, "bear")

	return true

end
