fishRewardQueueName = "fishRewardQueue" ;
fishRewardJumpedThroughHoopName = "fishRewardJumped" ;

function testwater1_multilevelscript( )
end

function testBucketAliveWait( seconds )
	local countUp = 0.0 ;
	local timeBetweenChecks = 0.5 ;
	
	while ( true ) do
		local timeLeft = seconds - countUp ;
		local timeToWait = math.min( timeLeft, timeBetweenChecks ) ;
		if ( timeLeft <= 0.0 ) then
			return true ;
		end
		
		wait( timeToWait ) ;
		countUp = countUp + timeToWait ;
				
		if ( entityIsAlive( "bucket1" ) == false or entityIsAlive( "marineBio1" ) == false ) then
			DingoAlert( "false" ) ;
			return false ;
		end
	end
end

function testwater2_multilevelscript( )
	DingoAlert( "Running Test Water 1 Script" ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	--playAnim( marineBio, "attack", 1.0, false ) ;
	playAnim( marineBio, "idle", 1.0, true ) ;

	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	local pickupFishTime = 0.5 ;
	local minSecondsBetweenThrows = 3 ;
	local maxSecondsBetweenThrows = 6 ;

	while ( true ) do
		local secondsBetweenThrows = randomDouble( minSecondsBetweenThrows, maxSecondsBetweenThrows ) ;
		if ( testBucketAliveWait( secondsBetweenThrows ) == false ) then
			break ;
		end
		
		playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
		wait( pickupFishTime + attackAnimBlend ) ;
	
		local posX = 4130 ;
		local posY = -895 ;
		local velX = randomDouble( -10.0, -6 ) ;
		local velY = randomDouble( -16, -12.0 ) ;
		local area = randomDouble( 1000.0, 2500.0 ) ;
		local angularVelocity = randomDouble( -300.0, 300.0 ) ;
		local priority = randomInt( 100, 130 ) ;
		createEntity( "mackerel_base", posX, posY, 0.0, velX, velY, area, angularVelocity, priority ) ;
	
		--while ( true ) do
		--	wait( 0.1 ) ;
		--	if ( isAnimDone( marineBio ) ) then
		--		break ;
		--	end
		--end
		wait( attackAnimLength - pickupFishTime ) ;
		playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
	end
	
	--pl

	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	
	
	DingoAlert( "test script 2" ) ;

	wait( 5 ) ;

	DingoAlert( "test script 3" ) ;

end

function testwater3_multilevelscript( )
	DingoAlert( "test water 3" ) ;
end

function giveFish( numFishToGive )
	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	local pickupFishTime = 0.5 ;
	local minSecondsBetweenThrows = 2 ; --3 ;
	local maxSecondsBetweenThrows = 3 ; --6 ;

	local coordSourceLevel = "dolphin1a" ;

	playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
	wait( pickupFishTime + attackAnimBlend ) ;

	local currentLevel = getCurrentLevelName( ) ;
	
	local numPlayers = getNumPlayers( ) ;
	local smallestPlayerArea = 100000000.0 ;
	
	for var=0,(numPlayers-1) do
		local playerName = "player_" .. var ;
		--DingoAlert( playerName ) ;
		smallestPlayerArea = math.min( smallestPlayerArea, getEntityArea( playerName ) ) ;
	end
	
	
	local fishIndex = 0 ;
	while ( fishIndex < numFishToGive ) do
		
		local velMult = 1.0 ;
		if ( currentLevel == "dolphin1a" ) then
			-- goes too far in this level (has to do with friction, or gravity updates?)
			velMult = 0.4 ;
		end
		
		local areaMult = 1.0 ;
		if ( currentLevel == "dolphin1a" ) then
			areaMult = 1.0 ; --0.2 ;
		end

		local posX = 5293 ;
		local posY = -5 ;
		local speed = randomDouble( 12.0, 15.0 ) * velMult ;
		--local velX = randomDouble( -10.0, -6 ) * velMult ;
		--local velY = randomDouble( -16, -12.0 ) * velMult ;
		--local area = randomDouble( 600.0, 1000.0 ) * areaMult ;

		-- base size on entities size, but limit so fish don't get huge
		local area = smallestPlayerArea * randomDouble( 0.5, 0.6 ) ;
		local maxArea = 800.0 ;
		area = math.min( area, maxArea ) ;
		
		
		--DingoAlert( coordSourceLevel ) ;
		--DingoAlert( currentLevel ) ;

		posX, posY = convertLevelPos( posX, posY, coordSourceLevel, currentLevel ) ;		
		speed = convertLevelDist( speed, coordSourceLevel, currentLevel ) ;
		--velX = convertLevelDist( velX, coordSourceLevel, currentLevel ) ;
		--velY = convertLevelDist( velY, coordSourceLevel, currentLevel ) ;
		area = convertLevelArea( area, coordSourceLevel, currentLevel ) ;
		
		
		local launchAngleDegrees = 125.0 ; -- up and to the left
		local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
		local velX = speed * math.cos( launchAngleRadians ) ;
		local velY = - speed * math.sin( launchAngleRadians ) ;
		
		
		local angularVelocity = randomDouble( -300.0, 300.0 ) ;
		local priority = randomInt( 100, 130 ) ;
		createEntity( "mackerel_base", posX, posY, 0.0, velX, velY, area, angularVelocity, priority ) ;
		
		fishIndex = fishIndex + 1 ;
	end

	--while ( true ) do
	--	wait( 0.1 ) ;
	--	if ( isAnimDone( marineBio ) ) then
	--		break ;
	--	end
	--end
	wait( attackAnimLength - pickupFishTime ) ;
	playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
end

function hoopTrack( entityName, fishReward )
	DingoAlert( "running hoopTrack" ) ;
	DingoAlert( "entityName: " .. entityName ) ;
	DingoAlert( "fishReward: " .. fishReward ) ;
	
	local hoopTop = -159.0 ;
	local hoopBottom = 118.0 ;
	local hoopPosX = 4821.0 ;
	local hoopLevel = "dolphin1a" ;
	
	local onLeftLast = false ;

	local entityPosX, entityPosY = getEntityPos( entityName ) ;
	if ( entityPosX < hoopPosX ) then
		onLeftLast = true ;
	end

	while ( entityIsAlive( entityName ) ) do

		if ( getCurrentLevelName( ) ~= hoopLevel ) then
			break ;
		end
		
		entityPosX, entityPosY = getEntityPos( entityName ) ;
		local onLeft = entityPosX < hoopPosX ;
		--DingoAlert( "EntityPosX: " .. entityPosX ) ;
		if ( onLeft ~= onLeftLast ) then
			--DingoAlert( "crossed over" ) ;
			-- crossed over, but was in hoop?
			if ( entityPosY > hoopTop and entityPosY < hoopBottom ) then
				-- went through hoop!
				local fishRewardQueue = getScriptVariable( fishRewardQueueName ) ;
				fishRewardQueue = fishRewardQueue + fishReward ;
				setScriptVariable( fishRewardQueueName, fishRewardQueue ) ;
				
				local whistleVolume = 0.65 ;
				local whistleSound = "whistle" ;
				if ( fishReward > 1 ) then
					whistleVolume = 0.9 ;
					whistleSound = "bell" ;
				end
				
				playSound( whistleSound, whistleVolume ) ;
				
				--setTipTitle( "Hoop" ) ;
				--setTipText( entityName .. " went through hoop" ) ;
				--setTipImage( "" ) ;
				--activateTipWindow( ) ;
			end
		end
		onLeftLast = onLeft ;
		
		coroutine.yield( ) ;
	end
end

function flamingHoop( )
	
	local coordSourceLevel = "dolphin1a" ;
	
	while ( true ) do
		local currentLevel = getCurrentLevelName( ) ;
		local burstSize = convertLevelDist( 1.0, coordSourceLevel, currentLevel ) ;

		if ( currentLevel ~= "dolphin1a" and currentLevel ~= "dolphin1b" ) then
			break ;
		end ; 

		if ( currentLevel == "dolphin1b" ) then
			if ( not entityIsAlive( "hoop1" ) ) then
				break ;
			end
		end

		if ( getGraphicsQuality( ) > 90 ) then
			-- lots of particles
			local numberOfSpots = 12 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4821.0 ;
					emitSpotY = 117.0 ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4846.0 ;
					emitSpotY = 84.0 ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4856.0 ;
					emitSpotY = 39.0 ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4860.0 ;
					emitSpotY = -9.0 ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4858.0 ;
					emitSpotY = -65.0 ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4846.0 ;
					emitSpotY = -119.0 ;
				elseif ( emitSpot == 6 ) then
					emitSpotX = 4821.0 ;
					emitSpotY = -154.0 ;
				elseif ( emitSpot == 7 ) then
					emitSpotX = 4796.0 ;
					emitSpotY = -123.0 ;
				elseif ( emitSpot == 8 ) then
					emitSpotX = 4787.0 ;
					emitSpotY = -81.0 ;
				elseif ( emitSpot == 9 ) then
					emitSpotX = 4782.0 ;
					emitSpotY = -22.0 ;
				elseif ( emitSpot == 10 ) then
					emitSpotX = 4785.0 ;
					emitSpotY = 40.0 ;
				elseif ( emitSpot == 11 ) then
					emitSpotX = 4797.0 ;
					emitSpotY = 89.0 ;
				end
			
				emitSpotX, emitSpotY = convertLevelPos( emitSpotX, emitSpotY, coordSourceLevel, currentLevel ) ;
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		else
			-- less particles
			local numberOfSpots = 6 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4666.0 ;
					emitSpotY = 380.0 ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4708.0 ;
					emitSpotY = 281.0 ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4705.0 ;
					emitSpotY = 156.0 ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4668.0 ;
					emitSpotY = 77.0 ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4635.0 ;
					emitSpotY = 143.0 ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4631.0 ;
					emitSpotY = 301.0 ;
				end
				
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		end
		
		--coroutine.yield( ) ;
		wait( 0.05 ) ;
	end
end

function dolphin1a_levelscript( )
	DingoAlert( "Running dolphin1a Script" ) ;

	local startingLevel = getCurrentLevelName( ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	playAnim( marineBio, "idle", 1.0, true ) ;

	setScriptVariable( fishRewardQueueName, 0 ) ;

	activateScriptMessage( "flamingHoop" ) ;

	activateScriptMessage( "hoopTrack", "player_0", 1 ) ;
	activateScriptMessage( "hoopTrack", "player_1", 1 ) ;
	activateScriptMessage( "hoopTrack", "beachball_1", 4 ) ;

	local throwFish = "throwfish" ;
	local area = 1.0 ; -- really small so always smaller than player
	addScriptedEntityEmitting( throwFish, area ) ;

	local timeSinceLastFishThrown = 0.0 ;
	local maximumFishThrowInterval = 20.0 ;
	while ( entityIsAlive( bucket ) and getCurrentLevelName( ) == startingLevel ) do
		--DingoAlert( "Bucket is alive" ) ;
		timeSinceLastFishThrown = timeSinceLastFishThrown + getDeltaT( ) ;
		if ( timeSinceLastFishThrown > maximumFishThrowInterval ) then
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) + 1 ) ;
		end

		if ( getScriptVariable( fishRewardQueueName ) > 0 ) then
			local numFishToGive = getScriptVariable( fishRewardQueueName ) ;
			--if ( getScriptVariable( fishRewardQueueName ) > 2 ) then
			--	numFishToGive = 2 ;
			--end
			giveFish( numFishToGive ) ;
			timeSinceLastFishThrown = 0.0 ;
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) - numFishToGive ) ;
		end
		
		coroutine.yield( ) ;
	end

	removeScriptedEntityEmitting( throwFish ) ;
	
	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
end

function dolphin1_multilevelscript( )
	DingoAlert( "Running Dolphin 1 Script" ) ;

	--[[
	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	playAnim( marineBio, "idle", 1.0, true ) ;

	setScriptVariable( fishRewardQueueName, 0 ) ;

	activateScriptMessage( "flamingHoop" ) ;

	activateScriptMessage( "hoopTrack", "player_0", 1 ) ;
	activateScriptMessage( "hoopTrack", "player_1", 1 ) ;
	activateScriptMessage( "hoopTrack", "beachball_1", 4 ) ;

	local timeSinceLastFishThrown = 0.0 ;
	local maximumFishThrowInterval = 20.0 ;
	while ( entityIsAlive( bucket ) ) do
		DingoAlert( "Bucket is alive" ) ;
		timeSinceLastFishThrown = timeSinceLastFishThrown + getDeltaT( ) ;
		if ( timeSinceLastFishThrown > maximumFishThrowInterval ) then
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) + 1 ) ;
		end

		if ( getScriptVariable( fishRewardQueueName ) > 0 ) then
			local numFishToGive = getScriptVariable( fishRewardQueueName ) ;
			--if ( getScriptVariable( fishRewardQueueName ) > 2 ) then
			--	numFishToGive = 2 ;
			--end
			giveFish( numFishToGive ) ;
			timeSinceLastFishThrown = 0.0 ;
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) - numFishToGive ) ;
		end
		
		coroutine.yield( ) ;
	end
	
	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	--]]
end


function dolphin1test_multilevelscript( )
	DingoAlert( "Running Dolphin 1 Script" ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	--playAnim( marineBio, "attack", 1.0, false ) ;
	playAnim( marineBio, "idle", 1.0, true ) ;

	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	local pickupFishTime = 0.5 ;
	local minSecondsBetweenThrows = 2 ; --3 ;
	local maxSecondsBetweenThrows = 3 ; --6 ;

	local coordSourceLevel = "dolphin1b" ;


	while ( true ) do
		
	
		local secondsBetweenThrows = randomDouble( minSecondsBetweenThrows, maxSecondsBetweenThrows ) ;
		if ( testBucketAliveWait( secondsBetweenThrows ) == false ) then
			break ;
		end
		
		playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
		wait( pickupFishTime + attackAnimBlend ) ;

		local currentLevel = getCurrentLevelName( ) ;
		
		local velMult = 1.0 ;
		if ( currentLevel == "dolphin1a" ) then
			-- goes too far in this level (has to do with friction, or gravity updates?)
			velMult = 0.4 ;
		end
		
		local areaMult = 1.0 ;
		if ( currentLevel == "dolphin1a" ) then
			areaMult = 0.2 ;
		end
	
		local posX = 4130 ;
		local posY = -895 ;
		local speed = randomDouble( 12.0, 15.0 ) * velMult ;
		--local velX = randomDouble( -10.0, -6 ) * velMult ;
		--local velY = randomDouble( -16, -12.0 ) * velMult ;
		local area = randomDouble( 1000.0, 2500.0 ) * areaMult ;
		
		--DingoAlert( coordSourceLevel ) ;
		--DingoAlert( currentLevel ) ;

		posX, posY = convertLevelPos( posX, posY, coordSourceLevel, currentLevel ) ;		
		speed = convertLevelDist( speed, coordSourceLevel, currentLevel ) ;
		--velX = convertLevelDist( velX, coordSourceLevel, currentLevel ) ;
		--velY = convertLevelDist( velY, coordSourceLevel, currentLevel ) ;
		area = convertLevelArea( area, coordSourceLevel, currentLevel ) ;
		
		local launchAngleDegrees = 125.0 ; -- up and to the left
		local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
		local velX = speed * math.cos( launchAngleRadians ) ;
		local velY = - speed * math.sin( launchAngleRadians ) ;
		
		
		local angularVelocity = randomDouble( -300.0, 300.0 ) ;
		local priority = randomInt( 100, 130 ) ;
		createEntity( "mackerel_base", posX, posY, 0.0, velX, velY, area, angularVelocity, priority ) ;
	
		--while ( true ) do
		--	wait( 0.1 ) ;
		--	if ( isAnimDone( marineBio ) ) then
		--		break ;
		--	end
		--end
		wait( attackAnimLength - pickupFishTime ) ;
		playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
	end
	
	--pl

	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	
	
	DingoAlert( "test script 2" ) ;

	wait( 5 ) ;

	DingoAlert( "test script 3" ) ;

end


















function giveFish2( numFishToGive, hoopLevel, giveEntityType )
	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	local pickupFishTime = 0.5 ;
	local minSecondsBetweenThrows = 2 ; --3 ;
	local maxSecondsBetweenThrows = 3 ; --6 ;

	local coordSourceLevel = hoopLevel ;

	playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
	wait( pickupFishTime + attackAnimBlend ) ;

	local currentLevel = getCurrentLevelName( ) ;
	
	local numPlayers = getNumPlayers( ) ;
	local smallestPlayerArea = 100000000.0 ;
	
	for var=0,(numPlayers-1) do
		local playerName = "player_" .. var ;
		--DingoAlert( playerName ) ;
		smallestPlayerArea = math.min( smallestPlayerArea, getEntityArea( playerName ) ) ;
	end
	
	
	local fishIndex = 0 ;
	while ( fishIndex < numFishToGive ) do
		
		local velMult = 1.0 ;
		if ( currentLevel == hoopLevel ) then
			-- goes too far in this level (has to do with friction, or gravity updates?)
			velMult = 0.4 ;
		end
		
		local areaMult = 1.0 ;
		if ( currentLevel == hoopLevel ) then
			areaMult = 1.0 ; --0.2 ;
		end

		local posX = 5293 ;
		local posY = -5 ;
		local speed = randomDouble( 12.0, 15.0 ) * velMult ;
		--local velX = randomDouble( -10.0, -6 ) * velMult ;
		--local velY = randomDouble( -16, -12.0 ) * velMult ;
		--local area = randomDouble( 600.0, 1000.0 ) * areaMult ;

		-- base size on entities size, but limit so fish don't get huge
		local area = smallestPlayerArea * randomDouble( 0.5, 0.6 ) ;
		local maxArea = 800.0 ;
		area = math.min( area, maxArea ) ;
		
		
		--DingoAlert( coordSourceLevel ) ;
		--DingoAlert( currentLevel ) ;

		posX, posY = convertLevelPos( posX, posY, coordSourceLevel, currentLevel ) ;		
		speed = convertLevelDist( speed, coordSourceLevel, currentLevel ) ;
		--velX = convertLevelDist( velX, coordSourceLevel, currentLevel ) ;
		--velY = convertLevelDist( velY, coordSourceLevel, currentLevel ) ;
		area = convertLevelArea( area, coordSourceLevel, currentLevel ) ;
		
		
		local launchAngleDegrees = 125.0 ; -- up and to the left
		local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
		local velX = speed * math.cos( launchAngleRadians ) ;
		local velY = - speed * math.sin( launchAngleRadians ) ;
		
		
		local angularVelocity = randomDouble( -300.0, 300.0 ) ;
		local priority = randomInt( 100, 130 ) ;
		
		local maxEntityAtSameTime = 15 ;
		if ( countNumEntity( giveEntityType ) < maxEntityAtSameTime ) then
			createEntity( giveEntityType, posX, posY, 0.0, velX, velY, area, angularVelocity, priority ) ;
		end
		
		fishIndex = fishIndex + 1 ;
	end

	--while ( true ) do
	--	wait( 0.1 ) ;
	--	if ( isAnimDone( marineBio ) ) then
	--		break ;
	--	end
	--end
	wait( attackAnimLength - pickupFishTime ) ;
	playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
end

function hoopTrack2( entityName, fishReward, hoopLevel )
	DingoAlert( "running hoopTrack2" ) ;
	DingoAlert( "entityName: " .. entityName ) ;
	DingoAlert( "fishReward: " .. fishReward ) ;
	
	local hoopTop = -159.0 ;
	local hoopBottom = 118.0 ;
	local hoopPosX = 4821.0 ;
	--local hoopLevel = "dolphin2a" ;
	
	local onLeftLast = false ;

	local entityPosX, entityPosY = getEntityPos( entityName ) ;
	if ( entityPosX < hoopPosX ) then
		onLeftLast = true ;
	end

	while ( entityIsAlive( entityName ) ) do

		if ( getCurrentLevelName( ) ~= hoopLevel ) then
			break ;
		end
		
		entityPosX, entityPosY = getEntityPos( entityName ) ;
		local onLeft = entityPosX < hoopPosX ;
		--DingoAlert( "EntityPosX: " .. entityPosX ) ;
		if ( onLeft ~= onLeftLast ) then
			--DingoAlert( "crossed over" ) ;
			-- crossed over, but was in hoop?
			if ( entityPosY > hoopTop and entityPosY < hoopBottom ) then
				-- went through hoop!
				local fishRewardQueue = getScriptVariable( fishRewardQueueName ) ;
				fishRewardQueue = fishRewardQueue + fishReward ;
				setScriptVariable( fishRewardQueueName, fishRewardQueue ) ;
				setScriptVariable( fishRewardJumpedThroughHoopName, 1 ) ;
				
				local whistleVolume = 0.7 ;
				local whistleSound = "whistle" ;
				if ( fishReward > 1 ) then
					whistleVolume = 0.9 ;
					whistleSound = "bell" ;
				end
				
				playSound( whistleSound, whistleVolume ) ;
				
				--setTipTitle( "Hoop" ) ;
				--setTipText( entityName .. " went through hoop" ) ;
				--setTipImage( "" ) ;
				--activateTipWindow( ) ;
			end
		end
		onLeftLast = onLeft ;
		
		coroutine.yield( ) ;
	end
end

function flamingHoop2( hoopLevel1, hoopLevel2 )
	
	local coordSourceLevel = hoopLevel1 ; --"dolphin2a" ;
	
	while ( true ) do
		local currentLevel = getCurrentLevelName( ) ;
		local burstSize = convertLevelDist( 1.0, coordSourceLevel, currentLevel ) ;

		if ( currentLevel ~= hoopLevel1 and currentLevel ~= hoopLevel2 ) then
			break ;
		end ; 

		if ( currentLevel == hoopLevel2 ) then
			if ( not entityIsAlive( "hoop1" ) ) then
				break ;
			end
		end

		if ( getGraphicsQuality( ) > 90 ) then
			-- lots of particles
			local numberOfSpots = 12 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4821.0 ;
					emitSpotY = 117.0 ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4846.0 ;
					emitSpotY = 84.0 ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4856.0 ;
					emitSpotY = 39.0 ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4860.0 ;
					emitSpotY = -9.0 ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4858.0 ;
					emitSpotY = -65.0 ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4846.0 ;
					emitSpotY = -119.0 ;
				elseif ( emitSpot == 6 ) then
					emitSpotX = 4821.0 ;
					emitSpotY = -154.0 ;
				elseif ( emitSpot == 7 ) then
					emitSpotX = 4796.0 ;
					emitSpotY = -123.0 ;
				elseif ( emitSpot == 8 ) then
					emitSpotX = 4787.0 ;
					emitSpotY = -81.0 ;
				elseif ( emitSpot == 9 ) then
					emitSpotX = 4782.0 ;
					emitSpotY = -22.0 ;
				elseif ( emitSpot == 10 ) then
					emitSpotX = 4785.0 ;
					emitSpotY = 40.0 ;
				elseif ( emitSpot == 11 ) then
					emitSpotX = 4797.0 ;
					emitSpotY = 89.0 ;
				end
			
				emitSpotX, emitSpotY = convertLevelPos( emitSpotX, emitSpotY, coordSourceLevel, currentLevel ) ;
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		else
			-- less particles
			local numberOfSpots = 6 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4666.0 ;
					emitSpotY = 380.0 ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4708.0 ;
					emitSpotY = 281.0 ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4705.0 ;
					emitSpotY = 156.0 ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4668.0 ;
					emitSpotY = 77.0 ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4635.0 ;
					emitSpotY = 143.0 ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4631.0 ;
					emitSpotY = 301.0 ;
				end
				
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		end
		
		--coroutine.yield( ) ;
		wait( 0.05 ) ;
	end
end

function dolphin2a_levelscript( )
	local scriptName = "dolphin2a" ;
	printStartScript( scriptName ) ;

	local startingLevel = getCurrentLevelName( ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	playAnim( marineBio, "idle", 1.0, true ) ;

	setScriptVariable( fishRewardQueueName, 0 ) ;
	setScriptVariable( fishRewardJumpedThroughHoopName, 0 ) ;

	activateScriptMessage( "flamingHoop2", "dolphin2a", "dolphin2b" ) ;

	activateScriptMessage( "hoopTrack2", "player_0", 1, "dolphin2a" ) ;
	activateScriptMessage( "hoopTrack2", "player_1", 1, "dolphin2a" ) ;
	activateScriptMessage( "hoopTrack2", "beachball_1", 4, "dolphin2a" ) ;

	local throwFish = "throwfish" ;
	local area = 1.0 ; -- really small so always smaller than player
	addScriptedEntityEmitting( throwFish, area ) ;




	local tutArrow1Name = "arrow1" ;
	local showingArrow1 = false ;

	local timeSinceLastFishThrown = 0.0 ;
	local maximumFishThrowInterval = 20.0 ;
	while ( entityIsAlive( bucket ) and getCurrentLevelName( ) == startingLevel ) do
		--DingoAlert( "Bucket is alive" ) ;
		timeSinceLastFishThrown = timeSinceLastFishThrown + getDeltaT( ) ;
		if ( timeSinceLastFishThrown > maximumFishThrowInterval ) then
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) + 1 ) ;
		end
		
		-- speed up forced fish throwing if player is taking too long
		if ( maximumFishThrowInterval > 0.2 ) then
			if ( getSubLevelTime( ) > 75.0 ) then
				maximumFishThrowInterval = maximumFishThrowInterval - getDeltaT( ) ;
				DingoAlert( maximumFishThrowInterval ) ;
			end
		end

		if ( getScriptVariable( fishRewardJumpedThroughHoopName ) == 0 ) then
			if ( getSubLevelTime( ) > 10.0 ) then
				if ( not showingArrow1 ) then
					createTutArrow( tutArrow1Name, "arrowpath1" ) ;	
					showingArrow1 = true ;
				end
			end
		else
			if ( showingArrow1 ) then
				removeTutArrow( tutArrow1Name ) ;
				showingArrow1 = false
			end
		end

		if ( getScriptVariable( fishRewardQueueName ) > 0 ) then
			local numFishToGive = getScriptVariable( fishRewardQueueName ) ;
			--if ( getScriptVariable( fishRewardQueueName ) > 2 ) then
			--	numFishToGive = 2 ;
			--end
			giveFish2( numFishToGive, "dolphin2a", "mackerel_base" ) ;
			timeSinceLastFishThrown = 0.0 ;
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) - numFishToGive ) ;
		end
		
		coroutine.yield( ) ;
	end

	removeScriptedEntityEmitting( throwFish ) ;
	
	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	
	printEndScript( scriptName ) ;
end

function getRightmostPlayerX( )
	local numPlayers = getNumPlayers( ) ;
	local rightmostPlayerX = -1000000.0 ;
		
	for var=0,(numPlayers-1) do
		local playerName = "player_" .. var ;
		local entityPosX, entityPosY = getEntityPos( playerName ) ;
		rightmostPlayerX = math.max( rightmostPlayerX, entityPosX ) ;
	end
	
	return rightmostPlayerX ;
end

function printStartScript( scriptName )
	DingoAlert( "Start " .. scriptName .. " script" ) ;
end

function printEndScript( scriptName )
	DingoAlert( "End " .. scriptName .. " script" ) ;
end

function dolphin2c_p2force( )
	local scriptName = "dolphin2c_p2force" ;
	printStartScript( scriptName ) ;

	local startingLevel = "dolphin2c" ;
	
	local numPlayers = getNumPlayers( ) ;
	if ( numPlayers < 2 ) then
		return ;
	end

	local inPoolX = 4850 ;
	local inPoolY = -450 ;
	local inOcean = false ;

	while ( getCurrentLevelName( ) == startingLevel and not inOcean ) do
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosX > inPoolX and entityPosY > inPoolY ) then
				inOcean = true ;
			end
		end
		
		wait( 0.1 ) ;
	end
	
	if ( isSplitScreen( ) ) then
		wait( 6.0 ) ;
	else
		wait( 2.0 ) ;
	end

	if ( getCurrentLevelName( ) == startingLevel ) then
		-- force players into drain
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosX < inPoolX and entityPosY < inPoolY ) then
				-- not in drain yet, force into it
				setEntityPos( playerName, 5200, -360 ) ;
			end
		end	
	end

	printEndScript( scriptName ) ;
end

function dolphin2c_levelscript( )
	local scriptName = "dolphin2c" ;
	printStartScript( scriptName ) ;

	local startingLevel = getCurrentLevelName( ) ;

	local aquariumRight = 4850.0

	local leftAquarium = false ;

	activateScriptMessage( "dolphin2c_p2force" ) ;
		
	local tutArrow2Name = "arrow2"
	local arrow2Added = false ;
	while ( ( getCurrentLevelName( ) == startingLevel ) and ( getRightmostPlayerX( ) < aquariumRight ) ) do
		if ( not arrow2Added ) then
			if ( getSubLevelTime( ) > 10.0 ) then
				createTutArrow( tutArrow2Name, "arrowpath2" ) ;
				arrow2Added = true ;
			end
		end
		
		wait( 0.1 ) ;
	end

	if ( arrow2Added ) then
		removeTutArrow( tutArrow2Name ) ;
	end
	
	printEndScript( scriptName ) ;
end

function skullhoop1a_levelscript( )
	DingoAlert( "Running skullhoop1a Script" ) ;

	local startingLevel = getCurrentLevelName( ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	playAnim( marineBio, "idle", 1.0, true ) ;

	setScriptVariable( fishRewardQueueName, 0 ) ;

	activateScriptMessage( "flamingHoop2", "skullhoop1a", "skullhoop1b" ) ;

	--activateScriptMessage( "hoopTrack2", "player_0", 1, "skullhoop1a" ) ;
	--activateScriptMessage( "hoopTrack2", "player_1", 1, "skullhoop1a" ) ;
	activateScriptMessage( "hoopTrack2", "skull_1", 3, "skullhoop1a" ) ;

	local throwFish = "throwfish" ;
	local area = 1.0 ; -- really small so always smaller than player
	addScriptedEntityEmitting( throwFish, area ) ;

	local timeSinceLastFishThrown = 0.0 ;
	local maximumFishThrowInterval = 1000.0 ;
	while ( entityIsAlive( bucket ) and getCurrentLevelName( ) == startingLevel ) do
		--DingoAlert( "Bucket is alive" ) ;
		timeSinceLastFishThrown = timeSinceLastFishThrown + getDeltaT( ) ;
		if ( timeSinceLastFishThrown > maximumFishThrowInterval ) then
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) + 1 ) ;
		end

		if ( getScriptVariable( fishRewardQueueName ) > 0 ) then
			local numFishToGive = getScriptVariable( fishRewardQueueName ) ;
			if ( getScriptVariable( fishRewardQueueName ) > 1 ) then
				numFishToGive = 1 ;
			end
			giveFish2( numFishToGive, "skullhoop1a", "goldbar_physic" ) ;
			timeSinceLastFishThrown = 0.0 ;
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) - numFishToGive ) ;
		end
		
		coroutine.yield( ) ;
	end

	removeScriptedEntityEmitting( throwFish ) ;
	
	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
end

function emitHoopFlame( hoopName, hoopTop, hoopBottom, hoopPosX )
	
	local coordSourceLevel = hoopLevel1 ; --"dolphin2a" ;
	local burstSize = 1.0 ;
	
	local useHoopPosY = -159 + 342 - 10 + hoopTop 
	
	-- lots of particles
	local numberOfSpots = 6 ;--6 ;
	local emitSpot = 0 ;
	while ( emitSpot < numberOfSpots ) do
		--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
		local emitSpotX = 0.0 ;
		local emitSpotY = 0.0 ;
		
		if ( emitSpot == 0 ) then
			emitSpotX = 4821.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = 117.0 + useHoopPosY ;
		elseif ( emitSpot == 1 ) then
			emitSpotX = 4856.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = 39.0 + useHoopPosY ;
		elseif ( emitSpot == 2 ) then
			emitSpotX = 4858.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = -65.0 + useHoopPosY ;
		elseif ( emitSpot == 3 ) then
			emitSpotX = 4821.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = -154.0 + useHoopPosY ;
		elseif ( emitSpot == 4 ) then
			emitSpotX = 4787.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = -81.0 + useHoopPosY ;
		elseif ( emitSpot == 5 ) then
			emitSpotX = 4785.0 - 4170 - 650 + hoopPosX ;
			emitSpotY = 40.0 + useHoopPosY ;
		end
	
		--emitSpotX, emitSpotY = convertLevelPos( emitSpotX, emitSpotY, coordSourceLevel, currentLevel ) ;
		createFlameBurstShort( emitSpotX, emitSpotY, burstSize ) ;
		
		emitSpot = emitSpot + 1 ;
	end
	
end

function flamingHoop3( hoopName, hoopTop, hoopBottom, hoopPosX )
	
	local coordSourceLevel = hoopLevel1 ; --"dolphin2a" ;
	local burstSize = 1.0 ;
	
	local useHoopPosY = -159 + 342 - 10 + hoopTop 
	
	while ( true ) do
		if ( not entityIsAlive( hoopName ) ) then
			break ;
		end

		if ( getGraphicsQuality( ) > 90 ) then
			-- lots of particles
			local numberOfSpots = 12 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4821.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 117.0 + useHoopPosY ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4846.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 84.0 + useHoopPosY ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4856.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 39.0 + useHoopPosY ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4860.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -9.0 + useHoopPosY ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4858.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -65.0 + useHoopPosY ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4846.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -119.0 + useHoopPosY ;
				elseif ( emitSpot == 6 ) then
					emitSpotX = 4821.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -154.0 + useHoopPosY ;
				elseif ( emitSpot == 7 ) then
					emitSpotX = 4796.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -123.0 + useHoopPosY ;
				elseif ( emitSpot == 8 ) then
					emitSpotX = 4787.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -81.0 + useHoopPosY ;
				elseif ( emitSpot == 9 ) then
					emitSpotX = 4782.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = -22.0 + useHoopPosY ;
				elseif ( emitSpot == 10 ) then
					emitSpotX = 4785.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 40.0 + useHoopPosY ;
				elseif ( emitSpot == 11 ) then
					emitSpotX = 4797.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 89.0 + useHoopPosY ;
				end
			
				--emitSpotX, emitSpotY = convertLevelPos( emitSpotX, emitSpotY, coordSourceLevel, currentLevel ) ;
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		else
			-- less particles
			local numberOfSpots = 6 ;--6 ;
			local emitSpot = 0 ;
			while ( emitSpot < numberOfSpots ) do
				--local emitSpot = randomInt( 0, numberOfSpots + 1 ) ;
				local emitSpotX = 0.0 ;
				local emitSpotY = 0.0 ;
				
				if ( emitSpot == 0 ) then
					emitSpotX = 4666.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 380.0 + useHoopPosY ;
				elseif ( emitSpot == 1 ) then
					emitSpotX = 4708.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 281.0 + useHoopPosY ;
				elseif ( emitSpot == 2 ) then
					emitSpotX = 4705.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 156.0 + useHoopPosY ;
				elseif ( emitSpot == 3 ) then
					emitSpotX = 4668.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 77.0 + useHoopPosY ;
				elseif ( emitSpot == 4 ) then
					emitSpotX = 4635.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 143.0 + useHoopPosY ;
				elseif ( emitSpot == 5 ) then
					emitSpotX = 4631.0 - 4170 - 650 + hoopPosX ;
					emitSpotY = 301.0 + useHoopPosY ;
				end
				
				createFlameBurst( emitSpotX, emitSpotY, burstSize ) ;
				
				emitSpot = emitSpot + 1 ;
			end
		end
		
		--coroutine.yield( ) ;
		wait( 0.05 ) ;
	end
end

function hoopTrack3( entityName, hoopName, hoopTop, hoopBottom, hoopPosX )
	DingoAlert( "running hoopTrack3 " .. hoopName .. " " .. hoopPosX ) ;

	local timeSinceLastEmit = 1.0 ;

	local p1EntityName = "player_0"
	local p1OnLeftLast = false ;
	local p1EntityPosX, p1EntityPosY = getEntityPos( p1EntityName ) ;
	if ( p1EntityPosX < hoopPosX ) then
		p1OnLeftLast = true ;
	end
	
	local p2EntityName = "player_1"
	local p2OnLeftLast = false ;
	local p2EntityPosX, p2EntityPosY = getEntityPos( p2EntityName ) ;
	if ( p2EntityPosX < hoopPosX ) then
		p2OnLeftLast = true ;
	end

	while ( entityIsAlive( entityName ) and entityIsAlive( hoopName ) ) do
		if ( entityIsAlive( p1EntityName ) ) then
			p1EntityPosX, p1EntityPosY = getEntityPos( p1EntityName ) ;
			local onLeft = p1EntityPosX < hoopPosX ;
			--DingoAlert( "EntityPosX: " .. entityPosX ) ;
			if ( onLeft ~= p1OnLeftLast ) then
				--DingoAlert( "crossed over" ) ;
				-- crossed over, but was in hoop?
				if ( p1EntityPosY > hoopTop and p1EntityPosY < hoopBottom ) then
					-- went through hoop!
					removeEntity( hoopName ) ;

					local whistleVolume = 0.7 ;
					local whistleSound = "whistle" ;

					playSound( whistleSound, whistleVolume ) ;

					setScriptVariable( "hoopsCompleted", getScriptVariable( "hoopsCompleted" ) + 1 ) ;
				end
			end
			p1OnLeftLast = onLeft ;
		end
		
		if ( entityIsAlive( p2EntityName ) ) then
			p2EntityPosX, p2EntityPosY = getEntityPos( p2EntityName ) ;
			local onLeft = p2EntityPosX < hoopPosX ;
			--DingoAlert( "EntityPosX: " .. entityPosX ) ;
			if ( onLeft ~= p2OnLeftLast ) then
				--DingoAlert( "crossed over" ) ;
				-- crossed over, but was in hoop?
				if ( p2EntityPosY > hoopTop and p2EntityPosY < hoopBottom ) then
					-- went through hoop!
					removeEntity( hoopName ) ;

					local whistleVolume = 0.7 ;
					local whistleSound = "whistle" ;

					playSound( whistleSound, whistleVolume ) ;
					
					setScriptVariable( "hoopsCompleted", getScriptVariable( "hoopsCompleted" ) + 1 ) ;
				end
			end
			p2OnLeftLast = onLeft ;
		end
		
		timeSinceLastEmit = timeSinceLastEmit + getDeltaT( ) ;
		if ( timeSinceLastEmit > 0.05 ) then
			--DingoAlert( "emitHoopFlame: " .. hoopName .. " " .. hoopPosX ) ;
			emitHoopFlame( hoopName, hoopTop, hoopBottom, hoopPosX ) ;
			timeSinceLastEmit = 0.0 ;
		end
		
		coroutine.yield( ) ;
	end
end

function hoopTrackPlayers( hoopName, hoopTop, hoopBottom, hoopPosX )
	--activateScriptMessage( "flamingHoop3", hoopName, hoopTop, hoopBottom, hoopPosX ) ;

	activateScriptMessage( "hoopTrack3", "player_0", hoopName, hoopTop, hoopBottom, hoopPosX ) ;
	--activateScriptMessage( "hoopTrack3", "player_1", hoopName, hoopTop, hoopBottom, hoopPosX ) ;
end

function randomMackerelDrop( dropPosX, dropPosY )
	local scriptName = "fwewefwf" ;
	while ( true ) do
		--wait( randomDouble( 0.1, 0.3 ) ) ;

		if ( not entityIsAlive( scriptName ) ) then
			scriptName = createEntity( "mackerel_base", dropPosX + randomDouble( -200.0, 200.0 ), dropPosY, randomDouble( 0.0, 360.0 ), 0.0, 0.0, 1500.0, randomDouble( -300.0, 300.0 ), 0 ) ;
		end
		
		wait( randomDouble( 0.1, 0.4 ) ) ;
	end
end

function hoopcourse1a_levelscript( )
	DingoAlert( "Running hoopcourse1a Script" ) ;

	local throwFish = "dropfish" ;
	local area = 1.0 ; -- really small so always smaller than player
	addScriptedEntityEmitting( throwFish, area ) ;

	local startingLevel = getCurrentLevelName( ) ;
	setScriptVariable( "hoopsCompleted", 0 ) ;

	hoopTrackPlayers( "hoop1", -159-3-4, 118-3-4, 4623 ) ;
	hoopTrackPlayers( "hoop2", -159-175-7, 118-175-7, 5368 ) ;
	hoopTrackPlayers( "hoop3", -159-10-6, 118-10-6, 6137 ) ;
	hoopTrackPlayers( "hoop4", -159-145-5, 118-145-5, 6852 ) ;
	hoopTrackPlayers( "hoop5", -159-20-5, 118-20-5, 7584 ) ;
	hoopTrackPlayers( "hoop6", -159-266-5, 118-266-5, 8258 ) ;
	
	local numHoops = 6 ;
	local maxLevelTime = 80 ; -- give them fish after this time
	while ( getScriptVariable( "hoopsCompleted" ) < numHoops and getLevelTime( ) < maxLevelTime ) do
		wait( 0.2 ) ;
	end

	DingoAlert( "hit all hoops!" ) ;

	wait( 0.5 ) ;	
	playSound( "explosion", 1.0 ) ;
	
	local dropPosX = 8000 ;
	local dropPosY = -600 ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	activateScriptMessage( "randomMackerelDrop", dropPosX, dropPosY ) ;
	
	--removeScriptedEntityEmitting( throwFish ) ;
end

function dolphin2_multilevelscript( )
	DingoAlert( "Running Dolphin 1 Script" ) ;

	--[[
	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	playAnim( marineBio, "idle", 1.0, true ) ;

	setScriptVariable( fishRewardQueueName, 0 ) ;

	activateScriptMessage( "flamingHoop2" ) ;

	activateScriptMessage( "hoopTrack2", "player_0", 1 ) ;
	activateScriptMessage( "hoopTrack2", "player_1", 1 ) ;
	activateScriptMessage( "hoopTrack2", "beachball_1", 4 ) ;

	local timeSinceLastFishThrown = 0.0 ;
	local maximumFishThrowInterval = 20.0 ;
	while ( entityIsAlive( bucket ) ) do
		DingoAlert( "Bucket is alive" ) ;
		timeSinceLastFishThrown = timeSinceLastFishThrown + getDeltaT( ) ;
		if ( timeSinceLastFishThrown > maximumFishThrowInterval ) then
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) + 1 ) ;
		end

		if ( getScriptVariable( fishRewardQueueName ) > 0 ) then
			local numFishToGive = getScriptVariable( fishRewardQueueName ) ;
			--if ( getScriptVariable( fishRewardQueueName ) > 2 ) then
			--	numFishToGive = 2 ;
			--end
			giveFish2( numFishToGive ) ;
			timeSinceLastFishThrown = 0.0 ;
			setScriptVariable( fishRewardQueueName, getScriptVariable( fishRewardQueueName ) - numFishToGive ) ;
		end
		
		coroutine.yield( ) ;
	end
	
	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	--]]
end


function dolphin2test_multilevelscript( )
	DingoAlert( "Running Dolphin 1 Script" ) ;

	local marineBio = "marineBio1" ;
	local bucket = "bucket1" ;

	--playAnim( marineBio, "attack", 1.0, false ) ;
	playAnim( marineBio, "idle", 1.0, true ) ;

	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	local pickupFishTime = 0.5 ;
	local minSecondsBetweenThrows = 2 ; --3 ;
	local maxSecondsBetweenThrows = 3 ; --6 ;

	local coordSourceLevel = "dolphin2b" ;


	while ( true ) do
		
	
		local secondsBetweenThrows = randomDouble( minSecondsBetweenThrows, maxSecondsBetweenThrows ) ;
		if ( testBucketAliveWait( secondsBetweenThrows ) == false ) then
			break ;
		end
		
		playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
		wait( pickupFishTime + attackAnimBlend ) ;

		local currentLevel = getCurrentLevelName( ) ;
		
		local velMult = 1.0 ;
		if ( currentLevel == "dolphin2a" ) then
			-- goes too far in this level (has to do with friction, or gravity updates?)
			velMult = 0.4 ;
		end
		
		local areaMult = 1.0 ;
		if ( currentLevel == "dolphin2a" ) then
			areaMult = 0.2 ;
		end
	
		local posX = 4130 ;
		local posY = -895 ;
		local speed = randomDouble( 12.0, 15.0 ) * velMult ;
		--local velX = randomDouble( -10.0, -6 ) * velMult ;
		--local velY = randomDouble( -16, -12.0 ) * velMult ;
		local area = randomDouble( 1000.0, 2500.0 ) * areaMult ;
		
		--DingoAlert( coordSourceLevel ) ;
		--DingoAlert( currentLevel ) ;

		posX, posY = convertLevelPos( posX, posY, coordSourceLevel, currentLevel ) ;		
		speed = convertLevelDist( speed, coordSourceLevel, currentLevel ) ;
		--velX = convertLevelDist( velX, coordSourceLevel, currentLevel ) ;
		--velY = convertLevelDist( velY, coordSourceLevel, currentLevel ) ;
		area = convertLevelArea( area, coordSourceLevel, currentLevel ) ;
		
		local launchAngleDegrees = 125.0 ; -- up and to the left
		local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
		local velX = speed * math.cos( launchAngleRadians ) ;
		local velY = - speed * math.sin( launchAngleRadians ) ;
		
		
		local angularVelocity = randomDouble( -300.0, 300.0 ) ;
		local priority = randomInt( 100, 130 ) ;
		createEntity( "mackerel_base", posX, posY, 0.0, velX, velY, area, angularVelocity, priority ) ;
	
		--while ( true ) do
		--	wait( 0.1 ) ;
		--	if ( isAnimDone( marineBio ) ) then
		--		break ;
		--	end
		--end
		wait( attackAnimLength - pickupFishTime ) ;
		playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
	end
	
	--pl

	playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
	
	
	DingoAlert( "test script 2" ) ;

	wait( 5 ) ;

	DingoAlert( "test script 3" ) ;

end



function coconut2a_levelscript( )
	DingoAlert( "Running coconut2a Script" ) ;

	while ( entityIsAlive( "log1" ) ) do
		local posX ;
		local posY ;
		posX, posY = getDefinedAreaPoint( "flames" ) ;
		--local posX = 325.0 ;
		--local posY = -25.0 ;
		local size = 2.0 ;
		--local spreadX = 10.0 * size ;
		createFlameBurst( posX, posY, size ) ;
		--createFlameBurst( posX + spreadX, posY + 5.0, size ) ;
		--createFlameBurst( posX - spreadX, posY + 5.0, size ) ;

		--wait( 0.05 ) ;
		coroutine.yield( ) ;
	end
end


function clampdown1a_levelscript( )
	DingoAlert( "Running clampdown1a Script" ) ;

	--local pearlName = "pearl" ;
	--local area = 1.0 ; -- really small so always smaller than player
	--addScriptedEntityEmitting( pearlName, area ) ;

	local emitAreaMult = 0.04 ;
	activateScriptMessage( "oysterClampDown", "oyster1", false, false, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster2", false, false, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster3", false, false, emitAreaMult, 0.0 ) ;

	local startingLevel = getCurrentLevelName( ) ;
	while ( getCurrentLevelName( ) == startingLevel ) do
		wait( 1 ) ;
	end

	--removeScriptedEntityEmitting( pearlName ) ;
end

function clampdown2a_levelscript( )
	DingoAlert( "Running clampdown1a Script" ) ;

	--local pearlName = "pearlb" ;
	--local area = 1.0 ; -- really small so always smaller than player
	--addScriptedEntityEmitting( pearlName, area ) ;

	local emitAreaMult = 0.04 ;
	activateScriptMessage( "oysterClampDown", "oyster1", false, false, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster2", false, false, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster3", false, false, emitAreaMult, 0.0 ) ;

	local startingLevel = getCurrentLevelName( ) ;
	while ( getCurrentLevelName( ) == startingLevel ) do
		wait( 1 ) ;
	end
	
	removeEntity( "oyster1" ) ;
	removeEntity( "oyster2" ) ;
	removeEntity( "oyster3" ) ;
		

	--removeScriptedEntityEmitting( pearlName ) ;
end

function clampdown2b_levelscript( )
	DingoAlert( "Running clampdown1a Script" ) ;

	--local pearlName = "pearl" ;
	--local area = 1.0 ; -- really small so always smaller than player
	--addScriptedEntityEmitting( pearlName, area ) ;

	--setAnim( "oyster2b", "mouth_open_wide", 1.0, 0.0, true ) ;

	local emitAreaMult = 0.04 ;
	activateScriptMessage( "oysterClampDown", "oyster1b", false, true, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster2b", true, true, emitAreaMult, 0.0 ) ;
	activateScriptMessage( "oysterClampDown", "oyster3b", false, true, emitAreaMult, 0.0 ) ;

	--removeScriptedEntityEmitting( pearlName ) ;
end

function clampdown3a_levelscript( )
	DingoAlert( "Running clampdown1a Script" ) ;

	local extraHeight = 0.015 ;
	local emitAreaMult = 0.08 ;
	activateScriptMessage( "oysterClampDown", "oyster1", false, false, emitAreaMult, extraHeight ) ;
	activateScriptMessage( "oysterClampDown", "oyster2", false, false, emitAreaMult, extraHeight ) ;
	activateScriptMessage( "oysterClampDown", "oyster3", false, false, emitAreaMult, extraHeight ) ;
	activateScriptMessage( "oysterClampDown", "oyster4", false, false, emitAreaMult, extraHeight ) ;
	activateScriptMessage( "oysterClampDown", "oyster5", false, false, emitAreaMult, extraHeight ) ;
	activateScriptMessage( "oysterClampDown", "oyster6", false, false, emitAreaMult, extraHeight ) ;
end

function oysterClampDown( entityName, startOpen, emitOyster, emitAreaMult, extraHeight )
	DingoAlert( "running oysterClampDown" ) ;
	DingoAlert( "entityName: " .. entityName ) ;

	DingoAlert( entityName .. " " .. tostring( emitOyster ) ) ;

	local posX ;
	local posY ;
	posX, posY = getEntityPos( entityName ) ;
	local entityArea = getEntityArea( entityName ) ;
	local pearlArea = entityArea * emitAreaMult ;
	local entityWidth = getEntityWidth( entityName ) ;
	local entityAngle = getEntityAngle( entityName ) ;
	local originalAngle = entityAngle ;
	local flipY = getEntityFlipY( entityName ) ;
	if ( flipY ) then
		entityAngle = entityAngle + 180.0 ;
	end
	
	addScriptedEntityEmitting( entityName, pearlArea ) ;

	local closeTime = 0.2 ;

	if ( entityIsAlive( entityName ) ) then
		if ( startOpen ) then
			setAnim( entityName, "mouth_open_wide", 1.0, 0.0, true ) ;
			wait( 4.0 ) ;
			playAnimBlend( entityName, "idle", 1.0, true, closeTime ) ;
			--startOpen = false ; -- done with this, go back to normal
		end
	end
	
	if ( not startOpen ) then
		wait( randomDouble( 0.1, 3.0 ) ) ;
	end
	
	while ( entityIsAlive( entityName ) ) do

		wait( randomDouble( 1.0, 3.0 ) ) ;
		
		if ( not entityIsAlive( entityName ) ) then
			break ;
		end

		local priority = randomInt( 100, 130 ) ;
		

		local pearl1 = "none" ;
		local pearl2 = "none" ;
		local pearl3 = "none" ;
		local shouldCreate = randomDouble( 0.0, 1.0 ) <= 0.5 ;
		if ( shouldCreate ) then
			if ( emitOyster ) then
				local emitPointX ;
				local emitPointY ;
				emitPointX = - entityWidth * 0.15 ;
				emitPointY = - entityWidth * ( 0.025 - 0.005 + extraHeight ) ;
				if ( flipY ) then
					emitPointY = -emitPointY ;
				end
				emitPointX, emitPointY = rotatePoint( emitPointX, emitPointY, originalAngle ) ;
				emitPointX = emitPointX + posX ;
				emitPointY = emitPointY + posY ;
				pearl1 = createEntity( "oyster_base", emitPointX, emitPointY, entityAngle, 0.0, 0.0, pearlArea, 0.0, priority ) ;

				emitPointX = 0.0 ;
				emitPointY = - entityWidth * ( 0.02 - 0.005 + extraHeight ) ;
				if ( flipY ) then
					emitPointY = -emitPointY ;
				end
				emitPointX, emitPointY = rotatePoint( emitPointX, emitPointY, originalAngle ) ;
				emitPointX = emitPointX + posX ;
				emitPointY = emitPointY + posY ;
				pearl2 = createEntity( "oyster_base", emitPointX, emitPointY, entityAngle, 0.0, 0.0, pearlArea, 0.0, priority ) ;

				emitPointX = entityWidth * 0.15 ;
				emitPointY = - entityWidth * ( 0.005 - 0.005 + extraHeight ) ;
				if ( flipY ) then
					emitPointY = -emitPointY ;
				end
				emitPointX, emitPointY = rotatePoint( emitPointX, emitPointY, originalAngle ) ;
				emitPointX = emitPointX + posX ;
				emitPointY = emitPointY + posY ;
				pearl3 = createEntity( "oyster_base", emitPointX, emitPointY, entityAngle, 0.0, 0.0, pearlArea, 0.0, priority ) ;
			else
				local emitPointX ;
				local emitPointY ;
				emitPointX = 0.0 ;
				emitPointY = - entityWidth * ( 0.035 - 0.01 + extraHeight ) ;
				if ( flipY ) then
					emitPointY = -emitPointY ;
				end
				emitPointX, emitPointY = rotatePoint( emitPointX, emitPointY, originalAngle ) ;
				emitPointX = emitPointX + posX ;
				emitPointY = emitPointY + posY ;
				pearl1 = createEntity( "pearl_base", emitPointX, emitPointY, entityAngle, 0.0, 0.0, pearlArea, 0.0, priority ) ;
			end
		end

		playAnimBlend( entityName, "mouth_open_wide", 1.0, true, 0.3 ) ;

		local mouthStayOpenTime = randomDouble( 0.7, 1.3 ) ;
		wait( mouthStayOpenTime ) ;
		
		playAnimBlend( entityName, "mouth_open_normal", 1.0, true, 0.7 ) ;
		wait( randomDouble( 0.4, 0.6 ) ) ;

		playAnimBlend( entityName, "idle", 1.0, true, closeTime ) ;
		
		local timeIntoClose = 0.0 ;
		while ( timeIntoClose < closeTime ) do
			local numPlayers = getNumPlayers( ) ;
			
			if ( timeIntoClose > closeTime*0.5 ) then
				-- Only damage during second half of closing
				for var=0,(numPlayers-1) do
					local playerName = "player_" .. var ;
					
					if ( entityIsAlive( playerName ) ) then
						local playerPosX ;
						local playerPosY ;
						playerPosX, playerPosY = getEntityPos( playerName ) ;
						local dist = distanceBetweenPoints( playerPosX, playerPosY, posX, posY ) ;
						
						local eatenDist = entityWidth * 0.33 ;
						if ( dist < eatenDist ) then
							if ( getEntityArea( playerName ) < entityArea ) then
								-- player can't eat oyster
								if ( isLevelEffectDeadly( ) ) then
									eatEntity( entityName, playerName ) ;
								else
									damageEntity( entityName, playerName ) ;
								end
							end
						end
					end				
				end
			end
		
			timeIntoClose = timeIntoClose + getDeltaT( ) ;	
			coroutine.yield( ) ;
		end
		
		wait( closeTime ) ;
		
		if ( entityIsAlive( pearl1 ) ) then
			removeEntity( pearl1 ) ;
		end
		
		if ( entityIsAlive( pearl2 ) ) then
			removeEntity( pearl2 ) ;
		end
		
		if ( entityIsAlive( pearl3 ) ) then
			removeEntity( pearl3 ) ;
		end

		coroutine.yield( ) ;
	end
	
	removeScriptedEntityEmitting( entityName ) ;
end

function flying1a_levelscript( )
	DingoAlert( "Running flying1a Script" ) ;

	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	flyingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function flying2a_levelscript( )
	DingoAlert( "Running flying2a Script" ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	flyingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function flying3a_levelscript( )
	DingoAlert( "Running flying3a Script" ) ;
	
	scriptEntName = "test" ;
	addScriptedEntityEmitting( scriptEntName, 1000.0 ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	activateScriptMessage( "flyingScript", minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;

	local cameraLeft ;
	local cameraTop ;
	local cameraRight ;
	local cameraBottom ;
	local levelLeft = -1000.0 ;
	local levelRight = 1000.0 ;

	local minEmitTime = 1.0 ;
	local maxEmitTime = 1.5 ;
	
	local numPlayers = getNumPlayers( ) ;
	
	local finishedFalling = false ;

	while ( not finishedFalling ) do
		wait( randomDouble( minEmitTime, maxEmitTime ) ) ;

		cameraLeft, cameraTop, cameraRight, cameraBottom = getViewExtentsAllCameras( ) ;
		
		local posX = randomDouble( levelLeft, levelRight ) ;
		local posY = cameraTop - 100.0 ;
		local velX = 0.0 ;
		local velY = 0.0 ;
		local angularVelocity = randomDouble( -400, 400 ) ;
		local priority = 0 ;
		local extraFallSpeed = randomDouble( 200.0, 350.0 ) ;
		local fallSpeed = minFallSpeed + randomDouble( 150.0, 250.0 ) ;

		local entityType = "" ;
		local entityArea = 1000.0 ;		
		local numDifferentEntities = 3 ;
		local randomValue = randomDouble( 0.0, 1.0 ) ;
		
		local smallestPlayer = 1000000.0 ;
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local playerArea = getEntityArea( playerName ) ;
			if ( playerArea < smallestPlayer ) then
				smallestPlayer = playerArea ;
			end
		end

		sealionName = "sealion_flying3" ;
		sealionArea = 1000.0 ;
		belugaName = "beluga_flying3" ;
		belugaArea = 6000.0 ;
		killerwhaleName = "killerwhale_flying3" ;
		killerwhaleArea = 25000.0 ;
		
		if ( smallestPlayer >= killerwhaleArea ) then
			-- can eat belugas
			if ( randomValue < 0.2 ) then
				entityType = sealionName ;
				entityArea = sealionArea ;
			elseif ( randomValue < 0.5 ) then
				entityType = belugaName ;
				entityArea = belugaArea ;
			else
				entityType = killerwhaleName ;
				entityArea = killerwhaleArea ;
			end
		elseif ( smallestPlayer >= belugaArea ) then
			-- can eat belugas
			if ( randomValue < 0.3 ) then
				entityType = sealionName ;
				entityArea = sealionArea ;
			elseif ( randomValue < 0.8 ) then
				entityType = belugaName ;
				entityArea = belugaArea ;
			else
				entityType = killerwhaleName ;
				entityArea = killerwhaleArea ;
			end
		else
			-- can't eat belugas
			if ( randomValue < 0.8 ) then
				entityType = sealionName ;
				entityArea = sealionArea ;
			elseif ( randomValue < 0.9 ) then
				entityType = belugaName ;
				entityArea = belugaArea ;
			else
				entityType = killerwhaleName ;
				entityArea = killerwhaleArea ;
			end
		end
		
		local entity1 = createEntity( entityType, posX, posY, 0.0, velX, velY, entityArea, angularVelocity, priority ) ;
		
		activateScriptMessage( "entityFallAtSpeed", minFallSpeed + extraFallSpeed, maxFallSpeed + extraFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, entity1 ) ;
		
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local posX ;
			local posY ;
			posX, posY = getEntityPos( playerName ) ;
			if ( posY > ( waterLevel - fullSpeedAtDist ) ) then
				finishedFalling = true ;
			end
		end
	end
	
	removeScriptedEntityEmitting( scriptEntName ) ;
end

function calcFallSpeed( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, entityName )
	local fallSpeedDiff = maxFallSpeed - minFallSpeed ;
	local startSpeedUpAt = waterLevel - startSpeedUpAtDist ;
	local fullSpeedAtDist = waterLevel - fullSpeedAtDist ;
	local speedUpDist = fullSpeedAtDist - startSpeedUpAt ;

	if ( entityIsAlive( entityName ) ) then
		local posX ;
		local posY ;
		posX, posY = getEntityPos( entityName ) ;
		
		speedUpRatio = ( posY - startSpeedUpAt ) / speedUpDist ;
		
		if ( speedUpRatio > 1.0 ) then
			return true, maxFallSpeed ;
		else
			if ( speedUpRatio < 0.0 ) then
				speedUpRatio = 0.0 ;
			end

			local fallSpeed = minFallSpeed + speedUpRatio * fallSpeedDiff ;
			return false, fallSpeed ;
		end
	end
end

function entityFallAtSpeed( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, entityName ) --fallSpeed, fullSpeedAtDist )
	--collectgarbage( "collect" ) ;
	local velX ;
	local velY ;
	local posX ;
	local posY ;
	local entityWidth = getEntityWidth( entityName ) ;
	local doneFalling = false ;
	while ( entityIsAlive( entityName ) and ( not doneFalling ) ) do

		local fallSpeed = 10.0 ;
		doneFalling, fallSpeed = calcFallSpeed( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, entityName ) ;

		velX, velY = getEntityVel( entityName ) ;
		setEntityVel( entityName, velX, fallSpeed ) ;
		posX, posY = getEntityPos( entityName ) ;
		local bottomCamPos = getViewBottomExtentAllCameras( ) ;
		
		if ( (posY-entityWidth) > bottomCamPos ) then
			removeEntity( entityName ) ;
		end
		
		coroutine.yield( ) ;
	end
end

function flyingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel )
	setAllowAirControl( true ) ;
	local numPlayers = getNumPlayers( ) ;
	local fallSpeedDiff = maxFallSpeed - minFallSpeed ;

	local finishedFalling = false ;

	while ( not finishedFalling ) do
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			
			local doneFalling = false ;
			local fallSpeed = 10.0 ;
			doneFalling, fallSpeed = calcFallSpeed( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, playerName ) ;
			
			DingoAlert( "doneFalling " .. tostring( doneFalling ) ) ;
			DingoAlert( "fallSpeed " .. fallSpeed ) ;	
						
			if ( entityIsAlive( playerName ) ) then
				if ( doneFalling ) then
					setAllowAirControl( false ) ;
					finishedFalling = true ;
				else
					local velX ;
					local velY ;
					velX, velY = getEntityVel( playerName ) ;
					setEntityVel( playerName, velX, fallSpeed ) ;
				end
			else
				finishedFalling = true ;
			end
		end
		
		coroutine.yield( ) ;		
	end
end

function fighterjet2a_levelscript( )
	DingoAlert( "Running fighterjet2a Script" ) ;
	
	scriptEntName = "test" ;
	addScriptedEntityEmitting( scriptEntName, 100.0 ) ;
end

function createRaiseEntity( startX )
	local startY = 40.0 ;
	local endY = -25.0 ;

	aircraft1aLevel = "aircraftcarrier1a" ;

	while ( true ) do
		DingoAlert( "createRaise" ) ;
		
		wait( randomDouble( 0.1, 3.0 ) ) ;

		if ( getCurrentLevelName( ) ~= aircraft1aLevel ) then
			return ;
		end
		
		local emitPos1x = 93.0 ;
		local emitPos1y = 30.0 ;
		local velX = 0.0 ;
		local velY = 0.0 ;
		local area = 1000.0 ;
		local angularVelocity = 0.0 ;
		local priority = -100 ;
		local entityType = "" ;
		local angle = 0.0 ;
		local transferLevels = 0 ;
		if ( randomInt( 0, 2 ) == 1 ) then
			entityType = "attackhelicopter_aircraftcarrier1" ;
		else
			entityType = "fighterjet_aircraftcarrier1" ;
			angle = 5.0 ;
		end
		local entityName = createEntity( entityType, startX, startY, 0.0, velX, velY, area, angularVelocity, priority, transferLevels ) ;
		local posX = startX ;
		local posY = startY ;
		local moveSpeedY = -100.0 ;

		local doneLifting = false ;		
		while ( entityIsAlive( entityName ) and ( not doneLifting ) ) do
			posY = posY + moveSpeedY * getDeltaT( ) ;
			
			if ( posY > endY ) then
				setEntityPos( entityName, posX, posY ) ;
				setEntityAngle( entityName, angle ) ;
			end
			
			coroutine.yield( ) ;
		end
		
		while ( entityIsAlive( entityName ) ) do
			wait( 1 ) ;
		end
	end
end

function aircraftcarrier1a_levelscript( )
	DingoAlert( "Running aircraftcarrier1a Script" ) ;

	activateScriptMessage( "createRaiseEntity", -425.0 ) ;
	activateScriptMessage( "createRaiseEntity", -325.0 ) ;
	activateScriptMessage( "createRaiseEntity", -225.0 ) ;

	activateScriptMessage( "createRaiseEntity", 100.0 ) ;
	activateScriptMessage( "createRaiseEntity", 200.0 ) ;
	activateScriptMessage( "createRaiseEntity", 300.0 ) ;
	activateScriptMessage( "createRaiseEntity", 400.0 ) ;
	activateScriptMessage( "createRaiseEntity", 500.0 ) ;
	activateScriptMessage( "createRaiseEntity", 600.0 ) ;

	scriptEntName = "test" ;
	addScriptedEntityEmitting( scriptEntName, 100.0 ) ;
end

function removeEntityLink( hiderEntity, emitterEntity )
	while ( entityIsAlive( hiderEntity ) ) do
		wait( randomDouble( 0.5, 0.75 ) ) ;
	end
	
	removeEntity( emitterEntity ) ;
end

function coral1a_levelscript( )
	DingoAlert( "Running coral1a Script" ) ;
	
	activateScriptMessage( "removeEntityLink", "pillar1", "clown1" ) ;
	activateScriptMessage( "removeEntityLink", "pillar2", "dory1" ) ;
	activateScriptMessage( "removeEntityLink", "pillar2", "yellow1" ) ;
	activateScriptMessage( "removeEntityLink", "brain1", "dory2" ) ;
	activateScriptMessage( "removeEntityLink", "helmet1", "yellow2" ) ;
	activateScriptMessage( "removeEntityLink", "log1", "clown2" ) ;
	activateScriptMessage( "removeEntityLink", "log1", "dory3" ) ;
end

function updateEmittedListAlive( emittedList )
	local numberAlive
	local removeList = {} ;
	local count = 0 ;
	for key,value in pairs(emittedList) do
		if ( entityIsAlive( value ) == false ) then
			removeList[ value ] = value ;
		else
			count = count + 1 ;
		end
	end
	
	for key,value in pairs(removeList) do
		emittedList[ value ] = nil ;
	end
	
	return count ;
end

function lifeguard1a_throwscript( lifeguardName, endLifeguardName, emitPosX, emitPosY, launchAngle, launchEntity )
	local startingLevel = "lifeguard1a" ; --getCurrentLevelName( ) ;

	playAnim( lifeguardName, "idle", 1.0, true ) ;
	
	local pickupFishTime = 0.55 ;--0.5 ;
	local attackAnimLength = 1.0 ;
	local attackAnimBlend = 0.2 ;
	
	wait( randomDouble( 0.2, 2.0 ) ) ;
	
	local emittedList = { } ;
	
	while ( true ) do
		local emittedAlive = updateEmittedListAlive( emittedList ) ;
		local maxEmitted = 3 ;
		if ( emittedAlive < maxEmitted ) then
			local speed = randomDouble( 6.0, 12.0 ) ;
			local launchAngleDegrees = randomDouble( launchAngle-10.0, launchAngle+10.0 ) ; --125.0 ; -- up and to the left
			local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
			local velX = speed * math.cos( launchAngleRadians ) ;
			local velY = - speed * math.sin( launchAngleRadians ) ;
			local area = 1200.0 ;

			playAnimBlend( lifeguardName, "attack", attackAnimLength, false, attackAnimBlend ) ;
			wait( pickupFishTime + attackAnimBlend ) ;

			local angularVelocity = 0.0 ;
			if ( launchAngle > 100.0 ) then
				angularVelocity = randomDouble( 0.0, 300.0 ) ;
			else
				angularVelocity = randomDouble( -300.0, 0.0 ) ;
			end
			local priority = randomInt( 100, 130 ) ;
			
			if ( getCurrentLevelName( ) ~= startingLevel ) then
				-- level changed, don't emit
				playAnimBlend( endLifeguardName, "custom1", 1.0, true, 0.5 ) ;
				return ;
			end	
			
			local newEntity = createEntity( launchEntity, emitPosX, emitPosY, 0.0, velX, velY, area, angularVelocity, priority ) ;
			emittedList[ newEntity ] = newEntity ;
			--table.insert( emittedList, newEntity ) ;
			
			wait( 0.5 ) ;
			playAnimBlend( lifeguardName, "idle", 1.0, true, 0.5 ) ;
		end
		
		wait( randomDouble( 1.0, 3.0 ) ) ;
	end
end

function lifeguard1a_levelscript( )
	DingoAlert( "Running lifeguard1a Script" ) ;

	local startingLevel = getCurrentLevelName( ) ;

	activateScriptMessage( "lifeguard1a_throwscript", "lifeguard1", "lifeguard3", -2308, 438, 120.0, "lifering_normal" ) ;
	activateScriptMessage( "lifeguard1a_throwscript", "lifeguard2", "lifeguard4", -1934, 428, 60.0, "lifevest_normal" ) ;



	--local marineBio = "lifeguard1" ;

	--playAnim( marineBio, "idle", 1.0, true ) ;
	
	--local emitPosX = -2308 ;
	--local emitPosY = 438 ;

	--local pickupFishTime = 0.55 ;--0.5 ;
	--local attackAnimLength = 1.0 ;
	--local attackAnimBlend = 0.2 ;

	
	--while ( true ) do
	--	local speed = randomDouble( 6.0, 12.0 ) ;
	--	local launchAngleDegrees = randomDouble( 110.0, 135.0 ) ; --125.0 ; -- up and to the left
	--	local launchAngleRadians = launchAngleDegrees * ( 3.14159/180.0 ) ;
	--	local velX = speed * math.cos( launchAngleRadians ) ;
	--	local velY = - speed * math.sin( launchAngleRadians ) ;
	--	local area = 1200.0 ;

	--	playAnimBlend( marineBio, "attack", attackAnimLength, false, attackAnimBlend ) ;
	--	wait( pickupFishTime + attackAnimBlend ) ;

	--	local angularVelocity = randomDouble( 0.0, 300.0 ) ;
	--	local priority = randomInt( 100, 130 ) ;
	--	createEntity( "lifering_normal", emitPosX, emitPosY, 0.0, velX, velY, area, angularVelocity, priority ) ;
		
	--	wait( 0.5 ) ;
	--	playAnimBlend( marineBio, "idle", 1.0, true, 0.5 ) ;
		
	--	wait( 2.0 ) ;
	--end

	
	--playAnimBlend( marineBio, "custom1", 1.0, true, 0.5 ) ;
end

function slideeasy1a_levelscript( )
	DingoAlert( "Running slideeasy1a Script" ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	slidingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function slideeasy2a_levelscript( )
	DingoAlert( "Running slideeasy2a Script" ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	slidingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function slidehard1a_levelscript( )
	DingoAlert( "Running slidehard1a Script" ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	slidingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function slidehard2a_levelscript( )
	DingoAlert( "Running slidehard1a Script" ) ;
	
	local minFallSpeed = 700.0 ;
	local maxFallSpeed = 3000.0 ;
	local startSpeedUpAtDist = 8000.0 ;
	local fullSpeedAtDist = 2000 ;
	local waterLevel = 26250 ;
	slidingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel ) ;
end

function slidingScript( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel )
	--setAllowAirControl( true ) ;
	local numPlayers = getNumPlayers( ) ;
	local fallSpeedDiff = maxFallSpeed - minFallSpeed ;

	local finishedFalling = false ;

	while ( not finishedFalling ) do
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			
			local doneFalling = false ;
			local fallSpeed = 10.0 ;
			--doneFalling, fallSpeed = calcFallSpeed( minFallSpeed, maxFallSpeed, startSpeedUpAtDist, fullSpeedAtDist, waterLevel, playerName ) ;
			doneFalling = false ;
			fallSpeed = minFallSpeed ;
			
			DingoAlert( "doneFalling " .. tostring( doneFalling ) ) ;
			DingoAlert( "fallSpeed " .. fallSpeed ) ;	
						
			if ( entityIsAlive( playerName ) ) then
				if ( doneFalling ) then
					setAllowAirControl( false ) ;
					finishedFalling = true ;
				else
					local velX ;
					local velY ;
					velX, velY = getEntityVel( playerName ) ;
					local maxX = 1300.0 ;
					local maxY = 1300.0 ;
					if ( velX > maxX ) then
						velX = maxX ;
					end
					
					if ( velY > maxY ) then
						velY = maxY ;
					end
					
					setEntityVel( playerName, velX, velY ) ;
				end
			else
				finishedFalling = true ;
			end
		end
		
		coroutine.yield( ) ;		
	end
end

function sink1b_p2force( )
	local scriptName = "sink1b_p2force" ;
	printStartScript( scriptName ) ;

	local startingLevel = "sink1b" ;
	
	local numPlayers = getNumPlayers( ) ;
	if ( numPlayers < 2 ) then
		return ;
	end

	local inDrainY = -1750 ;
	local inDrain = false ;

	while ( getCurrentLevelName( ) == startingLevel and not inDrain ) do
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosY > inDrainY ) then
				inDrain = true ;
			end
		end
		
		wait( 0.1 ) ;
	end
	
	if ( isSplitScreen( ) ) then
		wait( 4.0 ) ;
	else
		wait( 1.0 ) ;
	end

	if ( getCurrentLevelName( ) == startingLevel ) then
		-- force players into drain
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosY < inDrainY ) then
				-- not in drain yet, force into it
				setEntityPos( playerName, 3431, -1426 ) ;
			end
		end	
	end
	
	local pastUPipeX = 4300 ;
	local pastUPipe = false ;

	while ( getCurrentLevelName( ) == startingLevel and not pastUPipe ) do
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosX > pastUPipeX ) then
				pastUPipe = true ;
			end
		end
		
		wait( 0.1 ) ;
	end
	
	if ( isSplitScreen( ) ) then
		wait( 4.0 ) ;
	else
		wait( 1.0 ) ;
	end

	if ( getCurrentLevelName( ) == startingLevel ) then
		-- force players into drain
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			
			if ( entityPosX < pastUPipeX ) then
				-- not in drain yet, force into it
				setEntityPos( playerName, 4365, -1405 ) ;
			end
		end	
	end
	
	printEndScript( scriptName ) ;
end

function sink1b_levelscript( )
	local scriptName = "sink1b" ;
	printStartScript( scriptName ) ;

	local startingLevel = getCurrentLevelName( ) ;

	local kid = "kid1" ;
	playAnim( kid, "idle", 1.0, true ) ;

	local tutArrow1Name = "arrow1"

	local kidStillHere = true ;
	local arrowAdded = false ;

	local stillInBowl = true ;
	local outOfBowlTime = 0.0 ;


	local startingLevel = getCurrentLevelName( ) ;

	activateScriptMessage( "sink1b_p2force" ) ;

	DingoAlert( "test1" ) ;

	while ( ( getCurrentLevelName( ) == startingLevel ) and stillInBowl ) do
		--DingoAlert( "run1" ) ;
		
		if ( not arrowAdded ) then
			if ( getSubLevelTime( ) > 10.0 ) then
				createTutArrow( tutArrow1Name, "arrowpath1" ) ;
				arrowAdded = true ;
			end
		end
		
		local bigPlayer = false ;
	
		local numPlayers = getNumPlayers( ) ;
		local largestPlayerArea = 0.0 ;
		local rightmostPlayerX = -1000000.0 ;
		
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			largestPlayerArea = math.max( largestPlayerArea, getEntityArea( playerName ) ) ;

			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			rightmostPlayerX = math.max( rightmostPlayerX, entityPosX ) ;
		end

		local removeArmSize = 2000.0 ;
		local bowlRight = 2900 ;
		
		local bigPlayer = largestPlayerArea > removeArmSize ;
		local outOfBowl = rightmostPlayerX > bowlRight ;

		--DingoAlert( "largestPlayerArea " .. largestPlayerArea ) ;
		--DingoAlert( "rightmostPlayerX " .. rightmostPlayerX ) ;
		
		if ( kidStillHere ) then
			if ( bigPlayer or outOfBowl ) then
				DingoAlert( "custom anim" ) ;
				playAnimBlend( kid, "custom1", 1.0, true, 1.5 ) ;
				
				local fishFoodEmitter = "fishfoodemit1" ;
				if ( entityIsAlive( fishFoodEmitter ) ) then
					removeEntity( fishFoodEmitter ) ;
				end
				
				kidStillHere = false ;
			end
		end
		
		if ( outOfBowl ) then
			removeTutArrow( tutArrow1Name ) ;
			stillInBowl = false ;
			outOfBowlTime = getSubLevelTime( ) ;
		end
		
		wait( 0.1 ) ;
	end
	
	DingoAlert( "test2" ) ;

	local tutArrow2Name = "arrow2"
	local arrow2Added = false ;
	local rightOfUPipe = 4200.0 ;
	local pastUPipe = false ;
	while ( getCurrentLevelName( ) == startingLevel and (not pastUPipe) ) do
		if ( not arrow2Added ) then
			local timeSinceOutOfBowl = getSubLevelTime( ) - outOfBowlTime ;
			if ( timeSinceOutOfBowl > 10.0 ) then
				createTutArrow( tutArrow2Name, "arrowpath2" ) ;
				arrow2Added = true ;
			end
		end
		
		local numPlayers = getNumPlayers( ) ;
		local rightmostPlayerX = -1000000.0 ;
		
		for var=0,(numPlayers-1) do
			local playerName = "player_" .. var ;
			local entityPosX, entityPosY = getEntityPos( playerName ) ;
			rightmostPlayerX = math.max( rightmostPlayerX, entityPosX ) ;
		end
		
		if ( rightmostPlayerX > rightOfUPipe ) then
			if ( arrow2Added ) then
				removeTutArrow( tutArrow2Name ) ;
			end
		
			pastUPipe = true ;
		end
		
		wait( 0.1 ) ;
	end

	DingoAlert( "test3" ) ;

	printEndScript( scriptName ) ;
end

--function sink1_multilevelscript( )
--	DingoAlert( "sink1full multilevelscript start" ) ;
--	
--	wait( 2 ) ;
--	boostTip( ) ;
--
--	DingoAlert( "sink1full multilevelscript end" ) ;
--end


function plankton1n_levelscript( )
	local scriptName = "plankton1n" ;
	printStartScript( scriptName ) ;

	-- stop drawing air bubbles from now on
	setDrawAirParticles( false ) ;

	-- make sure players aren't in wrong location
	local numPlayers = getNumPlayers( ) ;
	
	for var=0,(numPlayers-1) do
		local playerName = "player_" .. var ;
		local entityPosX, entityPosY = getEntityPos( playerName ) ;
		if ( entityPosY < -620.0 ) then
			DingoAlert( "forcing changing pos " .. playerName ) ;
			setEntityPos( playerName, -80.0, -600.0 ) ;
		end
	end

	printEndScript( scriptName ) ;
end

function plankton1r_levelscript( )
	local scriptName = "plankton1r" ;
	printStartScript( scriptName ) ;
	
	DingoAlert( "explosion" ) ;

	setOverridePlayerEating( true ) ;
	
	createEarthExplosion( 150, -40 ) ;
	playSound( "explosion", 1.0 ) ;

	wait( 0.7 ) ;

	setOverridePlayerEating( false ) ;


	printEndScript( scriptName ) ;
end

function titleearth1a_levelscript( )
	local scriptName = "titleearth1a" ;
	printStartScript( scriptName ) ;

	-- stop drawing air bubbles from now on
	setDrawAirParticles( false ) ;

	printEndScript( scriptName ) ;
end

function getRightMostPlayer( )
	local rightmostPlayerX = -1000000.0 ;
	local numPlayers = getNumPlayers( ) ;
	for var=0,(numPlayers-1) do
		local playerName = "player_" .. var ;
		local entityPosX, entityPosY = getEntityPos( playerName ) ;
		rightmostPlayerX = math.max( rightmostPlayerX, entityPosX ) ;
	end
	
	return rightmostPlayerX ;
end

function jump1a_section( crossingPointX, tutArrowName, arrowPathName, arrowTime, arrowTipTime )
	local arrowAdded = false ;

	local sectionStartTime = getSubLevelTime( ) ;
	local tipTimer = getSubLevelTime( ) ;

	local firstGap = true ;
	while ( firstGap ) do
		if ( getRightMostPlayer( ) > crossingPointX ) then
			firstGap = False ;
		else
			local timeSoFar = getSubLevelTime( ) - tipTimer ;
			if ( arrowAdded ) then
				if ( timeSoFar > arrowTipTime ) then
					if ( not isTipWindowVisible( ) ) then
						jumpTip( ) ;
						tipTimer = getSubLevelTime( ) ;
					end
				end
			else
				-- arrow not added yet
				if ( timeSoFar > arrowTime ) then
					createTutArrow( tutArrowName, arrowPathName ) ;
					arrowAdded = true ;
					tipTimer = getSubLevelTime( ) ;
				end
			end
		end
		
		wait( 0.1 ) ;
	end
	
	if ( arrowAdded ) then
		removeTutArrow( tutArrowName ) ;
	end
end

function jump1a_levelscript( )
	local scriptName = "jump1a" ;
	printStartScript( scriptName ) ;

	local startingLevel = getCurrentLevelName( ) ;

	jump1a_section( -1115, "arrow1", "arrowpath1", 10.0, 8.0 ) ;
	jump1a_section( 125, "arrow2", "arrowpath2", 10.0, 20.0 ) ;
	jump1a_section( 1350, "arrow3", "arrowpath3", 10.0, 30.0 ) ;
	
	DingoAlert( "test3" ) ;

	printEndScript( scriptName ) ;
end