
function setValidationOfEntities()

  -- Invalidem algunes colisions logiques invisibles 
  setEntityValidByName("platform_vertical_block_acces_Lvl1", false);

  -- Invalidem la colisio del finalBoss
  setEntityValidByName("platform_vertical_bonusZone", false);
  setEntityValidByName("platform_horitzontal_bonusZone", false);
  setEntityValidByName("platform_horitzontal_stopPicat", false);

  -- Invalidem el barrel del menu
  setEntityValidByName("BarrelMenu", false);

  -- TODO(marc): De moment aqui, despres organitzar-ho
  setEntityValidByName("hideShark16", false);
  setEntityValidByName("hideShark17", false);
  setEntityValidByName("hideShark18", false);
  setEntityValidByName("seaUrchinNPC", false);
  
  setEntityValidByName("Cage01", true);
  setEntityValidByName("Cage02", true);
	
  -- Entitats que cal apagar
  -- setEntityValidByName("Cage03", false);
  
  setEntityValidByName("Barrel52", false);
  setEntityValidByName("Barrel53", false);
  setEntityValidByName("Barrel54", false);
  setEntityValidByName("Barrel55", false);
  setEntityValidByName("Barrel56", false);
  setEntityValidByName("Barrel57", false);
  setEntityValidByName("Barrel58", false);
  setEntityValidByName("Barrel59", false);
  
  setEntityValidByName("Mag_PowerUp13", true);
  
  setEntityValidByName("BarrelIntro", false);
  
  -- NEW: Carles
  -- la plataforma del llac d'aigua que NO es pinti mai :D
  setEntityVisibleByName( "trackerPlatform_scriptedLake_01", false )

return 0;
end

function disableCollsionsOfEntities()

  eraseCollisionEntityByName( "ruin_bloc01" );
  eraseCollisionEntityByName( "ruin_bloc02" );
  eraseCollisionEntityByName( "trunk_down01" );

return 0;
end

function disableParticleEmiters()
	
	-- Desactivem tots els sistemes excepte els inicials (els 6 primers de la cascada)
	for i=7,16 do

		if(i < 10) then
			setEntityValidByName("PsEmiter_water_impact0" .. i .. "", false);
		end
		
		if(i >= 10) then
			setEntityValidByName("PsEmiter_water_impact1" .. i .. "", false);
		end
		
	end
	
return 0;
end

function associateEntitiesToTrackers()

	-- Associate tracker to entities in the last zone
	setTrackerEntity( "Cage05", "trackerCage_05", 3, 4.0 );
	setTrackerEntity( "Cage06", "trackerCage_06", 3, 4.0 );
	setTrackerEntity( "Cage07", "trackerCage_07", 3, 4.0 );
	setTrackerEntity( "Cage08", "trackerCage_08", 3, 4.0 );
	
	setTrackerEntity( "Barrel52", "trackerBarrel_52", 3, 4.0 );
	setTrackerEntity( "Barrel53", "trackerBarrel_53", 3, 4.0 );
	setTrackerEntity( "Barrel54", "trackerBarrel_54", 3, 4.0 );
	setTrackerEntity( "Barrel55", "trackerBarrel_55", 3, 4.0 );
	setTrackerEntity( "Barrel56", "trackerBarrel_56", 3, 4.0 );
	setTrackerEntity( "Barrel57", "trackerBarrel_57", 3, 4.0 );
	setTrackerEntity( "Barrel58", "trackerBarrel_58", 3, 4.0 );
	setTrackerEntity( "Barrel59", "trackerBarrel_59", 3, 4.0 );
	
	setTrackerEntity( "platform_vertical_2_35", "tracker_FinalBoss_RingScriptedMov", 3, 0.2 );
	setTrackerEntity( "platform_horitzontal_2_60_rock", "tracker_FinalBoss_RingScriptedMov", 3, 0.2 );

return 0;
end

------------------------------------------ Funcions generals

function unhideInitialEnemiesLvl1( )
	
	setEntityValidByName("SeaUrchin01_Kamikaze", true);
	setEntityValidByName("Octopus16_scary", true);

	-- setEntityValidByName("SeaUrchin18_h", true);
	-- setEntityValidByName("SeaUrchin19_h", true);
	-- setEntityValidByName("SeaUrchin20_h", true);
	-- setEntityValidByName("SeaUrchin21_h", true);
	
	
return 0;
end

function enable_enemies_scenario1_lv1( )

	setEntityValidByName("Octopus02", true);
	setEntityValidByName("Octopus03", true);

	setEntityValidByName("SeaUrchin02_Kamikaze", true);
	setEntityValidByName("SeaUrchin03_Kamikaze", true);

return 0;
end

function enable_enemies_scenario1_lv2( )
	setEntityValidByName("Octopus04", true);
	setEntityValidByName("SeaUrchin04_Kamikaze", true);
return 0;
end

function enable_enemies_scenario1_lv3( )
	setEntityValidByName("Octopus05", true);
	setEntityValidByName("Octopus06", true);
	setEntityValidByName("Shark11", true);
	setEntityValidByName("SeaUrchin05_Kamikaze", true);
return 0;
end

function enable_enemies_scenario1_lv4( )
	setEntityValidByName("Octopus07", true);
	setEntityValidByName("SeaUrchin06_Kamikaze", true);
return 0;
end

function hideLvl1Platforms( )

	hidePlatform("trackerPlatform_hiddePlayer_01");
	hidePlatform("trackerPlatform_hiddePlayer_02");
	hidePlatform("trackerPlatform_hiddePlayer_03");
	hidePlatform("trackerPlatform_hiddePlayer_04");
	hidePlatform("trackerPlatform_hiddePlayer_05");
	hidePlatform("trackerPlatform_hiddePlayer_06");
	hidePlatform("trackerPlatform_hiddePlayer_07");
	hidePlatform("trackerPlatform_hiddePlayer_08");
	hidePlatform("trackerPlatform_hiddePlayer_09");
	hidePlatform("trackerPlatform_hiddePlayer_10");
	hidePlatform("trackerPlatform_hiddePlayer_11");
	hidePlatform("trackerPlatform_hiddePlayer_12");
	hidePlatform("trackerPlatform_hiddePlayer_13");
	hidePlatform("trackerPlatform_hiddePlayer_14");
	hidePlatform("trackerPlatform_hiddePlayer_15");

	hidePlatform("trackerPlatform_hidde_01");
	hidePlatform("trackerPlatform_hidde_02");
	hidePlatform("trackerPlatform_hidde_03");
	hidePlatform("trackerPlatform_hidde_04");

	hidePlatform("trackerPlatform_translate_01");
	hidePlatform("trackerPlatform_translate_02");
	hidePlatform("trackerPlatform_translate_03");
	hidePlatform("trackerPlatform_translate_13");

	hidePlatform("trackerPlatform_static_01");
	hidePlatform("trackerPlatform_static_02");

	hidePlatform("trackerPlatform_circular_01");
	hidePlatform("trackerPlatform_circular_02");
	
	-- Amagem també els elements del nivell 1 del llac
	setEntityValidByName("Cage02", false);

return 0;
end

function hideLvl2Platforms( )

    hidePlatform("trackerPlatform_translate_27");
    hidePlatform("trackerPlatform_translate_28");
	--hidePlatform("trackerPlatform_sinTranslate_01");
	hidePlatform("trackerPlatform_sinTranslate_02");

	hidePlatform("trackerPlatform_translate_05");
	hidePlatform("trackerPlatform_translate_04");
	hidePlatform("trackerPlatform_translate_09");
	hidePlatform("trackerPlatform_translate_10");
	hidePlatform("trackerPlatform_translate_12");
	
	setEntityValidByName("Octopus36_platform", false);
	setEntityValidByName("Octopus37_platform", false);
	
	setInstanceBoxValidByName("lakeplants2", false);
	setInstanceBoxValidByName("lakeplants3", true);

return 0;
end

---------------------------------------------------------------------------------------- Items Functions
-- Note: For appear and hidde items, you must to associate tracker to entity in associateEntitiesToTrackers

function appearItem( ItemName )
	throwParticles( "picat_ground", getEntityPosition( ItemName ), NULL );
	setEntityValidByName( ItemName, true );
	incrementEntityScriptedTracker( ItemName );
return 0;
end

function hideItem( ItemName, trackerName )
	decrementEntityScriptedTracker( ItemName );
	throwParticles( "picat_ground", getEntityPosition( ItemName ), NULL );
return 0;
end

function appearCage( CageName )
	enableCageComicDialog( CageName, true );
	appearItem( CageName );
return 0;
end

function hideCage( CageName, trackerName )
	enableCageComicDialog( CageName, false );
	hideItem( CageName, trackerName );
return 0;
end

---------------------------------------------------------------------------------------- Last Battle Fuctions
function startFinalBattle( )

  setCameraMode( 4, "", "trackerCamera_22", 100.0 );
  throwFirstBattalion_finalBattle( );
  throwHiddenBarrelsScript( );

return 0;
end

function throwHiddenBarrelsScript( )

  ----------------------------------------------------------------- Barrel 52
  functionOrder = "appearItem(\"Barrel52\")";
  addOrderToSequencerWithId ( 4, functionOrder, 5.0 );
  
  functionOrder = "hideItem(\"Barrel52\", \"trackerBarrel_52\")";
  addOrderToSequencerWithId ( 4, functionOrder, 5.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel52\", false )";
  addOrderToSequencerWithId ( 4, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 53
  functionOrder = "appearItem(\"Barrel53\")";
  addOrderToSequencerWithId ( 1, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel53\", \"trackerBarrel_53\")";
  addOrderToSequencerWithId ( 1, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel53\", false )";
  addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 54
  functionOrder = "appearItem(\"Barrel54\")";
  addOrderToSequencerWithId ( 4, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel54\", \"trackerBarrel_54\")";
  addOrderToSequencerWithId ( 4, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel54\", false )";
  addOrderToSequencerWithId ( 4, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 56
  functionOrder = "appearItem(\"Barrel56\")";
  addOrderToSequencerWithId ( 1, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel56\", \"trackerBarrel_56\")";
  addOrderToSequencerWithId ( 1, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel56\", false )";
  addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 57
  functionOrder = "appearItem(\"Barrel57\")";
  addOrderToSequencerWithId ( 4, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel57\", \"trackerBarrel_57\")";
  addOrderToSequencerWithId ( 4, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel57\", false )";
  addOrderToSequencerWithId ( 4, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 58
  functionOrder = "appearItem(\"Barrel58\")";
  addOrderToSequencerWithId ( 1, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel58\", \"trackerBarrel_58\")";
  addOrderToSequencerWithId ( 1, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel58\", false )";
  addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  
    ----------------------------------------------------------------- Barrel 55
  functionOrder = "appearItem(\"Barrel55\")";
  addOrderToSequencerWithId ( 4, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel55\", \"trackerBarrel_55\")";
  addOrderToSequencerWithId ( 4, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel55\", false )";
  addOrderToSequencerWithId ( 4, functionOrder, 0.5 );
  
  ----------------------------------------------------------------- Barrel 59
  functionOrder = "appearItem(\"Barrel59\")";
  addOrderToSequencerWithId ( 1, functionOrder, 2.0 );
  
  functionOrder = "hideItem(\"Barrel59\", \"trackerBarrel_59\")";
  addOrderToSequencerWithId ( 1, functionOrder, 7.0 );
  
  functionOrder = "setEntityValidByName( \"Barrel59\", false )";
  addOrderToSequencerWithId ( 1, functionOrder, 0.5 );

return 0;
end

-- Llamada recursiva que tira seaUrchins cada cierto tiempo
function throwSeaUrchinBattalion_finalBattle( )

  local functionOrder;

  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard01") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard01\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.0 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard02") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard02\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard03") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard03\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard04") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard04\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end
  
  if( isReached_FinalBattle == false ) then 
	functionOrder = "throwSeaUrchinBattalion_finalBattle( )";
	addOrderToSequencerWithId ( 1, functionOrder, 10.0 );
  end

return 0;
end

-- Llamada recursiva que tira seaUrchins cada cierto tiempo
function throwSeaUrchinBattalion_finalBoss( )

  local functionOrder;

  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard07") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard07\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.0 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard08") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard08\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard09") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard09\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end
  
  if( getEnemyValidByName("SeaUrchin_Kamikaze_Hard10") == false ) then 
	functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard10\")";
	addOrderToSequencerWithId ( 1, functionOrder, 0.5 );
  end

return 0;
end

function cancelOctopusBattalion_Stage2_finalBoss( )

	-- Netejem les deferred orders del sequencer on hem posat enemics
	clearAllSequencerOrdersWithId( 1 );

	setEntityValidByName( "Octopus44_BraveLongRange", false );
	setEntityValidByName( "Octopus43_BraveLongRange", false );
	setEntityValidByName( "Octopus42_BraveLongRange", false );
	setEntityValidByName( "Octopus41_BraveLongRange", false );
	
return 0;
end

function cancelSeaUrchinBattalion_finalBoss( )

	-- Netejem les deferred orders del sequencer on hem posat enemics
	clearAllSequencerOrdersWithId( 1 );

	setEntityValidByName( "SeaUrchin_Kamikaze_Hard07", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard08", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard09", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard10", false );
	
return 0;
end

function cancelSeaUrchinBattalion_finalBattle( )

	-- Netejem les deferred orders del sequencer on hem posat enemics
	clearAllSequencerOrdersWithId( 1 );

	setEntityValidByName( "SeaUrchin_Kamikaze_Hard01", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard02", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard03", false );
	setEntityValidByName( "SeaUrchin_Kamikaze_Hard04", false );
	
return 0;
end

function throwFirstBattalion_finalBattle( )

  local functionOrder;
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard01\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard02\")";
  addOrderToSequencer ( functionOrder, 0.4 );
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard03\")";
  addOrderToSequencer ( functionOrder, 0.4 );
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard04\")";
  addOrderToSequencer ( functionOrder, 0.4 );
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard05\")";
  addOrderToSequencer ( functionOrder, 0.4 );
  
  functionOrder = "unHideEnemy(\"SeaUrchin_Kamikaze_Hard06\")";
  addOrderToSequencer ( functionOrder, 0.0 );
  
  ----------------------------------------------------------- Mostrem la primera gabia
  
  functionOrder = "appearCage(\"Cage05\")";
  addOrderToSequencerWithId ( 3, functionOrder, 1.5 );
  
  functionOrder = "hideCage(\"Cage05\", \"trackerCage_05\")";
  addOrderToSequencerWithId ( 3, functionOrder, 9.0 );
  
  functionOrder = "setEntityValidByName( \"Cage05\", false )";
  addOrderToSequencerWithId ( 3, functionOrder, 0.5 );
  
  -----------------------------------------------------------
  
  functionOrder = "throwSeaUrchinBattalion_finalBattle()";
  addOrderToSequencer ( functionOrder, 5.0 );
  
  functionOrder = "is_throwBattleSequence1_End = true";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );
  
  functionOrder = "Start_lastBattle_second()";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );

return 0;
end

function Start_lastBattle_second( )

  if( is_killed_EnemiesInBattleSequence1 == true ) then
	if( is_throwBattleSequence1_End == true ) then
  
		local functionOrder = "setCameraMode( 4, \"\", \"trackerCamera_23\", 100.0 )";
		addOrderToSequencer ( functionOrder, 2.0 );

		throwSecondBattalion_finalBattle( );
	end
  end

return 0;
end

function throwSecondBattalion_finalBattle( )

  local functionOrder;
  
  functionOrder = "unHideEnemy(\"Shark03\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "unHideEnemy(\"Shark04\")";
  addOrderToSequencer ( functionOrder, 1.0 );

  functionOrder = "unHideEnemy(\"Octopus39\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "unHideEnemy(\"Octopus40\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  ----------------------------------------------------------- Mostrem la segona gabia
  
  functionOrder = "appearCage(\"Cage06\")";
  addOrderToSequencerWithId ( 3, functionOrder, 3.0 );
  
  functionOrder = "hideCage(\"Cage06\", \"trackerCage_06\")";
  addOrderToSequencerWithId ( 3, functionOrder, 9.0 );
  
  functionOrder = "setEntityValidByName( \"Cage06\", false )";
  addOrderToSequencerWithId ( 3, functionOrder, 0.5 );
  
  -----------------------------------------------------------
  
  functionOrder = "is_throwBattleSequence2_End = true";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );
  
  functionOrder = "Start_lastBattle_third()";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );

return 0;
end

function Start_lastBattle_third( )

  if( is_killed_EnemiesInBattleSequence2 == true ) then
	if( is_throwBattleSequence2_End == true ) then
  
	    local functionOrder = "setCameraMode( 4, \"\", \"trackerCamera_24\", 100.0 )";
	    addOrderToSequencer ( functionOrder, 2.0 );

		throwThirdBattalion_finalBattle( );
	end
  end

return 0;
end

function throwThirdBattalion_finalBattle( )

  local functionOrder;

  functionOrder = "unHideEnemy(\"Octopus34\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "unHideEnemy(\"Octopus36\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  ----------------------------------------------------------- Mostrem la tercera gabia
  
  functionOrder = "appearCage(\"Cage07\")";
  addOrderToSequencerWithId ( 3, functionOrder, 3.0 );
  
  functionOrder = "hideCage(\"Cage07\", \"trackerCage_07\")";
  addOrderToSequencerWithId ( 3, functionOrder, 9.0 );
  
  functionOrder = "setEntityValidByName( \"Cage07\", false )";
  addOrderToSequencerWithId ( 3, functionOrder, 0.5 );
  
  -----------------------------------------------------------
  
  functionOrder = "unHideEnemy(\"Octopus38\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "unHideEnemy(\"Shark07\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "unHideEnemy(\"Shark08\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "unHideEnemy(\"Shark10\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  ----------------------------------------------------------- Mostrem la quarta gabia
  
  functionOrder = "appearCage(\"Cage08\")";
  addOrderToSequencerWithId ( 3, functionOrder, 3.0 );
  
  functionOrder = "hideCage(\"Cage08\", \"trackerCage_08\")";
  addOrderToSequencerWithId ( 3, functionOrder, 9.0 );
  
  functionOrder = "setEntityValidByName( \"Cage08\", false )";
  addOrderToSequencerWithId ( 3, functionOrder, 0.5 );
  
  -----------------------------------------------------------
  
  functionOrder = "is_throwBattleSequence3_End = true";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );
  
  functionOrder = "Start_lastBattle_end()";
  addOrderToSequencerWithId ( 3, functionOrder, 0.0 );
  
return 0;
end

function Start_lastBattle_end( )

  if( is_killed_EnemiesInBattleSequence3 == true ) then
	if( is_throwBattleSequence3_End == true ) then
  
	  cancelSeaUrchinBattalion_finalBattle();
	  
	  -- Netejem les deferred orders del sequencer on hem posat enemics
	  clearAllSequencerOrdersWithId( 1 );
	  
	  setChangeFOV( false );
	  
	  local functionOrder = "StartCutScene13()";
	  addOrderToSequencer ( functionOrder, 0.5 );
	  
	  isTrigger_FollowPath07_enabled = true;
	  
	  -- Aixo fara que deixin de sortir enemics
	  isReached_FinalBattle = true;
	  
	  -- Activem els particleEmiters del cami per sobre l'aigua
	  setEntityValidByName("PsEmiter_water_impact11", true );
	  setEntityValidByName("PsEmiter_water_impact12", true );
	  setEntityValidByName("PsEmiter_water_impact13", true );
	  setEntityValidByName("PsEmiter_water_impact14", true );
	  setEntityValidByName("PsEmiter_water_impact15", true );
	  setEntityValidByName("PsEmiter_water_impact16", true );
	  
	end
  end

return 0;
end

----------------------------------------------- Restore FinalBattle Gameplay
function restoreFinalBattleGameplay( )

	restoreCagesFinalBattleGameplay();
	
	decrementEntityScriptedTracker( "Barrel52" );
	decrementEntityScriptedTracker( "Barrel53" );
	decrementEntityScriptedTracker( "Barrel54" );
	decrementEntityScriptedTracker( "Barrel55" );
	decrementEntityScriptedTracker( "Barrel56" );
	decrementEntityScriptedTracker( "Barrel57" );
	decrementEntityScriptedTracker( "Barrel58" );
	decrementEntityScriptedTracker( "Barrel59" );
	
	is_throwBattleSequence1_End = false;
	is_killed_EnemiesInBattleSequence1 = false;
  
    is_throwBattleSequence2_End = false;
    is_killed_EnemiesInBattleSequence2 = false;
  
    is_throwBattleSequence3_End = false;
    is_killed_EnemiesInBattleSequence3 = false;

return 0;
end

function restoreCagesFinalBattleGameplay()

	decrementEntityScriptedTracker( "Cage05" );
	decrementEntityScriptedTracker( "Cage06" );
	decrementEntityScriptedTracker( "Cage07" );
	decrementEntityScriptedTracker( "Cage08" );

	restoreCageState("Cage05");
	restoreCageState("Cage06");
	restoreCageState("Cage07");
	restoreCageState("Cage08");

return 0;
end

-----------------------------------------------

function unlockPathToStaticPlatform01( )

	unHidePlatform("trackerPlatform_translate_11");
	unHidePlatform("trackerPlatform_hiddePlayer_01");
	unHidePlatform("trackerPlatform_hiddePlayer_02");
	unHidePlatform("trackerPlatform_hiddePlayer_03");
	unHidePlatform("trackerPlatform_hiddePlayer_04");
	unHidePlatform("trackerPlatform_hiddePlayer_05");
	unHidePlatform("trackerPlatform_hiddePlayer_06");
	unHidePlatform("trackerPlatform_translate_01");
	unHidePlatform("trackerPlatform_static_01");
	unHidePlatform("trackerPlatform_translate_13");

return 0;
end

function throwBattalion_staticPlatform01_first( )

  local functionOrder;
  
  functionOrder = "start_StaticPlatformsBattleSeq_Lvl2()";
  addOrderToSequencer ( functionOrder, 0.3 );

  -- Amagem la aleta
  functionOrder = "setEntityValidByName( \"hideShark14\", false )";
  addOrderToSequencer ( functionOrder, 1.0 );

  -- reproduim el event de appearShark
  functionOrder = "shark_appearWater(getEntityPosition(\"Shark14\"), nil)";
  addOrderToSequencer ( functionOrder, 0.0 );

  functionOrder = "unHideEnemy( \"Shark14\")";
  addOrderToSequencer ( functionOrder, 0.0 );
  
return 0;
end

function throwBattalion_staticPlatform01_second( )

  local functionOrder;
  
  -- Amagem la aleta
  functionOrder = "setEntityValidByName( \"hideShark13\", false )";
  addOrderToSequencer ( functionOrder, 2.0 );
  
  functionOrder = "unHideEnemy(\"Shark13\")";
  addOrderToSequencer ( functionOrder, 0.0 );
  
  -- Amagem la aleta
  functionOrder = "setEntityValidByName( \"hideShark12\", false )";
  addOrderToSequencer ( functionOrder, 2.0 );
  
  functionOrder = "unHideEnemy(\"Shark12\")";
  addOrderToSequencer ( functionOrder, 0.0 );
  
return 0;
end

function unlockPathToStaticPlatform02( )

	local functionOrder;

	functionOrder = "setCameraMode( 6, \"trackerPlatform_static_02\", \"trackerCamera_15\", 0.0 )";
	addOrderToSequencer ( functionOrder, 0.0 );

	functionOrder = "unHidePlatform(\"trackerPlatform_static_02\")";
	addOrderToSequencer ( functionOrder, 0.0 );
	
	functionOrder = "setCameraMode( 0, \"\", \"\", 0.0 )";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "isTrigger_FollowPath01_enabled = true";
	addOrderToSequencer ( functionOrder, 0.0 );
	
return 0;
end

function unlockPathToSwitch03( )

	local functionOrder;
	
	StartCutScene();
	
	functionOrder = "incrementWptScriptedPlatform(\"trackerPlatform_scriptedLake_01\")";
	addOrderToSequencer ( functionOrder, 0.2 );

	functionOrder = "setCameraMode( 2, \"Switch02\", \"trackerCamera_04\", 200.0 )";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_translate_27\")";
	addOrderToSequencer ( functionOrder, 0.3 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_translate_28\")";
	addOrderToSequencer ( functionOrder, 0.0 );
	
	functionOrder = "setCameraMode( 1, \"ruin_bloc02\", \"trackerCamera_04\", 200.0 )";
	addOrderToSequencer ( functionOrder, 0.7 );

	--functionOrder = "unHidePlatform(\"trackerPlatform_translate_09\")";
	--addOrderToSequencer ( functionOrder, 0.3 );
	
	--functionOrder = "unHidePlatform(\"trackerPlatform_translate_10\")";
	--addOrderToSequencer ( functionOrder, 0.3 );
	
	--functionOrder = "unHidePlatform(\"trackerPlatform_translate_12\")";
	--addOrderToSequencer ( functionOrder, 0.3 );

	--functionOrder = "unHidePlatform(\"trackerPlatform_translate_29\")";
	functionOrder = "unHidePlatform(\"trackerPlatform_sinTranslate_02\")";
	addOrderToSequencer ( functionOrder, 0.3 );

	functionOrder = "setCameraMode( 2, \"trackerPlatform_translate_04\", \"trackerCamera_04\", 0.0 )";
	addOrderToSequencer ( functionOrder, 3.5 );
	
	-- Parem el so de l'aigua
	functionOrder = "stopSound(\"Game_situations/platformBoss_Down\")";
	addOrderToSequencer ( functionOrder, 0.1);

	functionOrder = "unHidePlatform(\"trackerPlatform_translate_04\")";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "setCameraMode( 2, \"trackerPlatform_translate_05\", \"trackerCamera_04\", 0.0 )";
	addOrderToSequencer ( functionOrder, 2.0 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_translate_05\")";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "setCameraMode( 2, \"Switch03\", \"trackerCamera_04\", 0.0 )";
	addOrderToSequencer ( functionOrder, 2.0 );
	
	functionOrder = "setFOV( 25, 2.0 )";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "setCameraMode( 4, \"\", \"trackerCamera_checkPoint10\", 4.0 )";
	addOrderToSequencer ( functionOrder, 3.0 );
	
	functionOrder = "setFOV( 40, 2.0 )";
	addOrderToSequencer ( functionOrder, 0.0 );

	functionOrder = "EndCutScene()";
	addOrderToSequencer ( functionOrder, 0.0 );

return 0;
end

function unlockPathToFinalBoss( )

  local functionOrder;
  
  	functionOrder = "unHideItem( \"Mag_PowerUp05\" )";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp06\" )";
	addOrderToSequencer ( functionOrder, 0.3 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp07\" )";
	addOrderToSequencer ( functionOrder, 0.3 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp08\" )";
	addOrderToSequencer ( functionOrder, 0.5 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp09\" )";
	addOrderToSequencer ( functionOrder, 0.3 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp10\" )";
	addOrderToSequencer ( functionOrder, 0.3 );
	
	functionOrder = "unHideItem( \"Mag_PowerUp11\" )";
	addOrderToSequencer ( functionOrder, 0.3 );
  
return 0;
end

function hidePathToFinalBoss( )

  local functionOrder;
  
  functionOrder = "hidePlatform(\"trackerPlatform_translate_06\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_translate_07\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_translate_08\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hideEnemy(\"Octopus42\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hideEnemy(\"Octopus43\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_16\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_17\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_18\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_19\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_20\")";
  addOrderToSequencer ( functionOrder, 0.5 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_axisRotation01\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_sinTranslate_03\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_sinTranslate_04\")";
  addOrderToSequencer ( functionOrder, 0.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_22\")";
  addOrderToSequencer ( functionOrder, 1.0 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_23\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_24\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_25\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_26\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_27\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_28\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_29\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_30\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_31\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_32\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
  functionOrder = "hidePlatform(\"trackerPlatform_hiddePlayer_33\")";
  addOrderToSequencer ( functionOrder, 0.3 );
  
return 0;
end

function EnableDefaultCamera( )

	local functionOrder;

	functionOrder = "setCameraMode( 0, \"\", \"\", 0.0 )";
	addOrderToSequencer ( functionOrder, 0.0 );
	
return 0;
end

function throwFirstBattalion_staticPlatform02( )

	local functionOrder;

	functionOrder = "unHideEnemy(\"Octopus17_brave\")";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "unHideEnemy(\"Octopus18_brave\")";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "unHideEnemy(\"Octopus19_brave\")";
	addOrderToSequencer ( functionOrder, 2.0 );

return 0;
end

function throwSecondBattalion_staticPlatform02( )

	local functionOrder;
	
	functionOrder = "unHideEnemy(\"Shark15\")";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "unHideEnemy(\"Octopus21_brave\")";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "unHideEnemy(\"Octopus22_brave\")";
	addOrderToSequencer ( functionOrder, 1.0 );

	functionOrder = "unHideEnemy(\"Octopus20_brave\")";
	addOrderToSequencer ( functionOrder, 1.0 );

return 0;
end

function throwFirstBattalion_baseballGamePlay( )

	local functionOrder;

	functionOrder = "start_BaseballBattleSeq_Lvl2()";
	addOrderToSequencer ( functionOrder, 0.0 );
	
	if( areActionsWithKeyBoard() ) then
		functionOrder = "setComicDialog( getMainPlayer(), \"info_twister\")";
	elseif( areActionsWithPad() ) then
		functionOrder = "setComicDialog( getMainPlayer(), \"info_twister_pad\")";
	else
		functionOrder = "setComicDialog( getMainPlayer(), \"info_twister_mouse\")";
	end
	
	addOrderToSequencer ( functionOrder, 0.0 );

	functionOrder = "unHideEnemy(\"Octopus33_braveLake\")";
    addOrderToSequencer ( functionOrder, 1.0 );
    
    functionOrder = "unHideEnemy(\"Octopus35_braveLake\")";
    addOrderToSequencer ( functionOrder, 1.5 );
  
    functionOrder = "unHideEnemy(\"Octopus34_braveLake\")";
    addOrderToSequencer ( functionOrder, 1.5 );
    
    functionOrder = "setVisiblityComicDialog( getMainPlayer(), false )";
	addOrderToSequencer ( functionOrder, 0.0 );
    
    functionOrder = "unHideEnemy(\"SeaUrchin29_Kamikaze\")";
    addOrderToSequencer ( functionOrder, 4.0 );

return 0;
end

function throwSecondBattalion_baseballGamePlay( )

	local functionOrder;

	functionOrder = "unHideEnemy(\"Octopus36_plat\")";
    addOrderToSequencer ( functionOrder, 1.0 );
  
    functionOrder = "unHideEnemy(\"Octopus37_plat\")";
    addOrderToSequencer ( functionOrder, 1.5 );
    
    -- Guardem l'estat un cop surten els dos octopus
    --functionOrder = "saveStateOfAllGameInstances( )";
    --addOrderToSequencer ( functionOrder, 0.0 );
    
    functionOrder = "setPlayerLastCheckPoint(\"trigger_posSave11\")";
    addOrderToSequencer ( functionOrder, 0.0 );
    
    functionOrder = "unHideEnemy(\"SeaUrchin30_Kamikaze\")";
    addOrderToSequencer ( functionOrder, 2.0 );
    
    functionOrder = "unHideEnemy(\"SeaUrchin31_Kamikaze\")";
    addOrderToSequencer ( functionOrder, 1.0 );

return 0;
end

function throwThirdBattalion_baseballGamePlay( )

	local functionOrder;

	functionOrder = "unHideEnemy(\"Octopus38_king\")";
    addOrderToSequencer ( functionOrder, 1.0 );
    
    -- Guardem l'estat un cop surten els dos octopus
    --functionOrder = "saveStateOfAllGameInstances( )";
    --addOrderToSequencer ( functionOrder, 0.0 );
    
    functionOrder = "setPlayerLastCheckPoint(\"trigger_posSave11\")";
    addOrderToSequencer ( functionOrder, 0.0 );

return 0;
end

function startBaseBallGamePlay( )

    hidePlatform("trackerPlatform_translate_27");
    hidePlatform("trackerPlatform_translate_28");
    --hidePlatform("trackerPlatform_translate_29");

	--hidePlatform("trackerPlatform_sinTranslate_01");
	hidePlatform("trackerPlatform_sinTranslate_02");
	
	setCameraMode( 6, "Octopus38_king", "trackerCamera_26", 10.0 );
  
    StartCutScene06();
	throwFirstBattalion_baseballGamePlay();
    -- throwThirdBattalion_baseballGamePlay();

return 0;
end

function setCagesTypes( )

	setCageType("Cage01", 0);
	setCageType("Cage02", 1);
	setCageType("Cage03", 2);
	setCageType("Cage04", 3);
	setCageType("Cage05", 4);
	setCageType("Cage06", 4);
	setCageType("Cage07", 4);
	setCageType("Cage08", 4);

return 0;
end

----------------------------------------------------------------------- Player respawn functions

function playerRestartCheckPoint()

  local functionOrder;
  
  -- Restart variables
  isAlternativePathLakeVisited = false;
  
  -- Al morir restablim els first enter dels triggers dels checkpoints
  init_CheckPointTriggersVariables( );
  
  -- Netejem les deferred orders dels sequencers
  clearAllSequencerOrdersWithId( 0 );
  clearAllSequencerOrdersWithId( 1 );
  clearAllSequencerOrdersWithId( 2 );
  clearAllSequencerOrdersWithId( 3 );
  clearAllSequencerOrdersWithId( 4 );
  
  -- Parem la musica activa
  functionOrder = "stopCurrentMusic( true )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  functionOrder = "stopCurrentAmbient( true )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  -- Afegim les ordres al nou sequencer
  functionOrder = "activateInput( false )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );

  functionOrder = "enableDeadGUI( true )";
  addOrderToSequencerWithId ( 2, functionOrder, 1.0 );
  
  functionOrder = "playSound( \"Gui_Menu/player_die\", Vector3(0,0,0) )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.2 );
  
  functionOrder = "setCameraMode( 0, \"\", \"\", 0.0 )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.3 );
  
  functionOrder = "restoreStateOfAllGameInstances()";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  -- A part de posicionar el player, també posiciona la camera!
  functionOrder = "situatePlayerInLastCheckpoint()";
  addOrderToSequencerWithId ( 2, functionOrder, 1.0 );
  
  -- Restablim l'estat del final Boss si estem lluitant contra ell
  functionOrder = "restart_state_finalBoss()";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  functionOrder = "enableDeadGUI( false )";
  addOrderToSequencerWithId ( 2, functionOrder, 1.5 );

  functionOrder = "rebornPlayer()";
  addOrderToSequencerWithId ( 2, functionOrder, 0.5 );
  
  functionOrder = "activateInput( true )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  functionOrder = "restartTriggers()";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  -- Per si les mosques posem el fov a 45 al morir
  functionOrder = "setFOV( 45, 1.0 )";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );
  
  -- Reprenem la musica activa
  functionOrder = "playLastMusic()";
  addOrderToSequencerWithId ( 2, functionOrder, 0.0 );

return 0;
end

----------------------------------------------------------------------- Logica de enfonsar plataformes del MegaWhiteShark

-- Num Stage: 0 totes les plataformes
--            1 primera platform enfonsada
--            2 segona plataforma enfonsada

function redistributePlatformsFinalBoss( namePlatformLost )

    -- DEPRECATED!
	--[[if(		namePlatformLost ~= "trackerPlatform_translate_20" 
		or	namePlatformLost ~= "trackerPlatform_translate_21"
		or	namePlatformLost ~= "trackerPlatform_translate_22") then
		assert( "ERROR" )
	end
	
	local isHiden20 = isHidedPlatform( "trackerPlatform_translate_20" );
    local isHiden21 = isHidedPlatform( "trackerPlatform_translate_21" );
    local isHiden22 = isHidedPlatform( "trackerPlatform_translate_22" );
    
    -- Calculate the current Stage geting the unhidedPlatforms    
    local numStage = 1;
    if( isHiden20 == true ) then
        numStage = numStage + 1;
    end
    
    if( isHiden21 == true ) then
        numStage = numStage + 1;
    end
    
    if( isHiden22 == true ) then
        numStage = numStage + 1;
    end
    
	hidePlatform( namePlatformLost );

	-- First Stage
	if( namePlatformLost == "trackerPlatform_translate_20" and numStage == 1 ) then
		setTrackerEntity( "trackerPlatform_translate_21", "tracker_translate_23_final", 3, 3.0 );
		setTrackerEntity( "trackerPlatform_translate_22", "tracker_translate_24_final", 3, 3.0 );

	elseif( namePlatformLost == "trackerPlatform_translate_21" and numStage == 1 ) then
		setTrackerEntity( "trackerPlatform_translate_20", "tracker_translate_23_final", 3, 3.0 );
		setTrackerEntity( "trackerPlatform_translate_22", "tracker_translate_24_final", 3, 3.0 );
		
	elseif( namePlatformLost == "trackerPlatform_translate_22" and numStage == 1 ) then
		setTrackerEntity( "trackerPlatform_translate_20", "tracker_translate_23_final", 3, 3.0 );
		setTrackerEntity( "trackerPlatform_translate_21", "tracker_translate_24_final", 3, 3.0 );
	
	-- Second Stage
	elseif( numStage == 2 ) then
        setTrackerEntity( "trackerPlatform_translate_20", "tracker_translate_21_final", 3, 3.0 );
        setTrackerEntity( "trackerPlatform_translate_21", "tracker_translate_21_final", 3, 3.0 );
        setTrackerEntity( "trackerPlatform_translate_22", "tracker_translate_21_final", 3, 3.0 );
	end--]]

return 0;
end

function restartBlinkPlatformsFinalBoss()

    -- Desblinkejem totes les plataformes
    
    -- Dash01
	local first_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[0][0];
	local second_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[0][1];
	setBlinkerByName( first_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 );
	setBlinkerByName( second_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 );

    -- Dash02
	local first_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[1][0];
	local second_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[1][1];
	setBlinkerByName( first_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 );
	setBlinkerByName( second_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 );

    -- Dash03
	local first_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[2][0];
	local second_platform_to_dash = MWS_SCRIPTED_DASH_SEQUENCE[2][1];
	setBlinkerByName( first_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 );
	setBlinkerByName( second_platform_to_dash, ColorRGBA(0.8,0.8,0.8,0.8), 0, 0 ); 
	
 return 0;
end

function enableFinalBossCoinPlatforms( )

    local isHiden20 = isHidedPlatform( "trackerPlatform_translate_20" );
    if( isHiden20 == false ) then
        makeAppearItem("Coin_translate_20", 0.0, 7.0 )
    end
    
    local isHiden21 = isHidedPlatform( "trackerPlatform_translate_21" );
    if( isHiden21 == false ) then
        makeAppearItem("Coin_translate_21", 0.0, 7.0 )
    end
    
    local isHiden22 = isHidedPlatform( "trackerPlatform_translate_22" );
    if( isHiden22 == false ) then
        makeAppearItem("Coin_translate_22", 0.0, 7.0 )
    end
    
    local isHiden23 = isHidedPlatform( "trackerPlatform_translate_23" );
    if( isHiden23 == false ) then
        makeAppearItem("Coin_translate_23", 0.0, 7.0 )
    end
    
    local isHiden24 = isHidedPlatform( "trackerPlatform_translate_24" );
    if( isHiden24 == false ) then
        makeAppearItem("Coin_translate_24", 0.0, 7.0 )
    end
    
    local isHiden25 = isHidedPlatform( "trackerPlatform_translate_25" );
    if( isHiden25 == false ) then
        makeAppearItem("Coin_translate_25", 0.0, 6.0 )
    end
    
    local isHiden26 = isHidedPlatform( "trackerPlatform_translate_26" );
    if( isHiden26 ==  false ) then
        makeAppearItem("Coin_translate_26", 0.0, 6.0 )
    end

return 0;
end

function disableFinalBossCoinPlatforms( )

    setEntityValidByName("Coin_translate_20", false);
    setEntityValidByName("Coin_translate_21", false);
    setEntityValidByName("Coin_translate_22", false);
    setEntityValidByName("Coin_translate_23", false);
    setEntityValidByName("Coin_translate_24", false);
    setEntityValidByName("Coin_translate_25", false);
    setEntityValidByName("Coin_translate_26", false);

return 0;
end

function checkandUnhidePlatform( namePlatform )

    local isHiden = isHidedPlatform( namePlatform );
    if( isHiden == true ) then
        unHidePlatform( namePlatform );
    end

return 0;
end

function restartPlatformFinalBoss( numStage )

	restartBlinkPlatformsFinalBoss();
	disableFinalBossCoinPlatforms();

    if ( numStage > 2 ) then
    
      hidePlatform( "trackerPlatform_translate_20" );
      checkandUnhidePlatform( "trackerPlatform_translate_21" );
      hidePlatform( "trackerPlatform_translate_22" );
      hidePlatform( "trackerPlatform_translate_23" );
      hidePlatform( "trackerPlatform_translate_24" );
      hidePlatform( "trackerPlatform_translate_25" );
      hidePlatform( "trackerPlatform_translate_26" );
    
    elseif ( numStage == 2 ) then
    
      --[[checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[2][0] );
      checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[2][1] );]]--
      
      hidePlatform( "trackerPlatform_translate_20" );
      checkandUnhidePlatform( "trackerPlatform_translate_21" );
      checkandUnhidePlatform( "trackerPlatform_translate_22" );
      hidePlatform( "trackerPlatform_translate_23" );
      hidePlatform( "trackerPlatform_translate_24" );
      hidePlatform( "trackerPlatform_translate_25" );
      checkandUnhidePlatform( "trackerPlatform_translate_26" );
    
    elseif( numStage == 1 ) then
    
      --[[checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[2][0] );
      checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[2][1] );
      checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[1][0] );
      checkandUnhidePlatform(  MWS_SCRIPTED_DASH_SEQUENCE[1][1] );]]--
      
      hidePlatform( "trackerPlatform_translate_20" );
      checkandUnhidePlatform( "trackerPlatform_translate_21" );
      checkandUnhidePlatform( "trackerPlatform_translate_22" );
      checkandUnhidePlatform( "trackerPlatform_translate_23" );
      checkandUnhidePlatform( "trackerPlatform_translate_24" );
      hidePlatform( "trackerPlatform_translate_25" );
      checkandUnhidePlatform( "trackerPlatform_translate_26" );
    
    elseif( numStage == 0 ) then
    
      checkandUnhidePlatform( "trackerPlatform_translate_20" );
      checkandUnhidePlatform( "trackerPlatform_translate_21" );
      checkandUnhidePlatform( "trackerPlatform_translate_22" );
      checkandUnhidePlatform( "trackerPlatform_translate_23" );
      checkandUnhidePlatform( "trackerPlatform_translate_24" );
      checkandUnhidePlatform( "trackerPlatform_translate_25" );
      checkandUnhidePlatform( "trackerPlatform_translate_26" );
      
    end

return 0;
end

----------------------------------------------------------------------- Logica de restarts del finalBoss

function restart_state_finalBoss( )
    
    if ( is_in_End_finalBoss == true ) then
        -- Ha mort el finalBoss, per tant aqui ja no recuperem
        setPlayerLastCheckPoint("trigger_posSave13");
        
    elseif ( is_in_Stage4_finalBoss == true ) then
    
         hideGUILifeBoss()
         restart_FinalStage_finalBoss( );
        
    elseif( is_in_Stage3_finalBoss == true ) then
    
        hideGUILifeBoss()
        if( MWS_CURRENT_DASH > 0 ) then
            restart_Stage3_finalBoss( MWS_CURRENT_DASH - 1 );
        else
            restart_Stage3_finalBoss( 0 );
        end
    
    elseif( is_in_EndStage2_finalBoss == true ) then
    
        -- Parem i encenem el so
        hideGUILifeBoss()
	    stopSound( "Game_situations/earthQuake" );
        restart_EndStage2_finalBoss( );
        playSound( "Game_situations/earthQuake", Vector3(0.0, 0.0, 0.0 ) )
    
    elseif( is_in_Stage2_finalBoss == true ) then
    
        hideGUILifeBoss()
        restart_Stage2_finalBoss( );
        
    elseif( is_in_Stage1_finalBoss == true ) then
        
        hideGUILifeBoss()
        restart_Stage1_finalBoss( );
    end
    
return 0;
end

function restart_Stage1_finalBoss( )
    
    -- Restart MegaWhiteShark, setting the initial state
    local idShark = getEntityID( "MegaWhiteShark" );
    sendMessage( 0.0, idShark, idShark, Telegram.Init_States_Phase1, NULL );
    
    local entityMWS = getFinalBoss();
    
    if( entityMWS ~= nil ) then
        -- set Yaw to face checkpoint17
        local checkPoint_pos = getPostionWPTTracker( "trackerCamera_RingFinalBoss_Stage0_Central", 0 );
        local enemy_pos = entityMWS:getPosition()
        local dir = checkPoint_pos - enemy_pos
        local new_yaw = vectorXZToYaw( dir )
    
        entityMWS:setYaw( new_yaw )
    end
    
    -- Recover the currentCamera
    setCameraMode( 4, " ", "trackerCamera_RingFinalBoss_Stage1_01", 4.0 );
    
return 0;
end

function restart_Stage2_finalBoss( )
    
    -- Restart MegaWhiteShark, setting the initial state
    local idShark = getEntityID( "MegaWhiteShark" );
    sendMessage( 0.0, idShark, idShark, Telegram.Init_States_Phase2, NULL );
    
    -- Erase octopus appeared
    cancelOctopusBattalion_Stage2_finalBoss();
    
    -- Erase urchins appeared
    cancelSeaUrchinBattalion_finalBoss();
    
    -- Recover the currentCamera
    setCameraMode( 4, "", "trackerCamera_RingFinalBoss_Stage2_Right", 0.0 );
    
return 0;
end

function activate_psEmiters_EndStage2_finalBoss( timeToDisablePsEmiters )
	
	local functionOrder;
	
	-- Netejem el buffer per si hi ha cancelacions anteriors
	clearAllSequencerOrdersWithId( 4 );
	
	for i=1,9 do
	    functionOrder = "setEntityValidByName( \"PsEmiter_picat_ground0" .. i .. "\", true )";
		addOrderToSequencer ( functionOrder, 0.1 );
		functionOrder = "setEntityValidByName( \"PsEmiter_water_fall0" .. i .. "\", true )";
		addOrderToSequencer ( functionOrder, 0.1 );
	end
	
	for i=1,9 do
		functionOrder = "setEntityValidByName( \"PsEmiter_picat_ground0" .. i .. "\", false )";
		if( i == 1 ) then
		    addOrderToSequencerWithId ( 4, functionOrder, timeToDisablePsEmiters );
		else
		    addOrderToSequencerWithId ( 4, functionOrder, 0.0 );
		end
		
        functionOrder = "setEntityValidByName( \"PsEmiter_water_fall0" .. i .. "\", false )";
		if( i == 1 ) then
		    addOrderToSequencerWithId ( 4, functionOrder, timeToDisablePsEmiters );
		else
		    addOrderToSequencerWithId ( 4, functionOrder, 0.0 );
		end
	end
	
return 0;
end

function desactivate_psEmiters_EndStage2_finalBoss( timeToDisablePsEmiters )

    for i=1,9 do
		functionOrder = "setEntityValidByName( \"PsEmiter_picat_ground0" .. i .. "\", false )";
		if( i == 1 ) then
		    addOrderToSequencer( functionOrder, timeToDisablePsEmiters );
		else
		    addOrderToSequencer( functionOrder, 0.0 );
		end
		
		functionOrder = "setEntityValidByName( \"PsEmiter_water_fall0" .. i .. "\", false )";
		if( i == 1 ) then
		    addOrderToSequencer( functionOrder, timeToDisablePsEmiters );
		else
		    addOrderToSequencer( functionOrder, 0.0 );
		end
	end
	
return 0;
end

function desactivate_psEmiters_lightBugs_Lvl1( )

    for i=13,22 do
		functionOrder = "setEntityValidByName( \"PsEmiter_light_bug" .. i .. "\", false )";
		addOrderToSequencer( functionOrder, 0.0 );
	end
	
return 0;
end

function restart_EndStage2_finalBoss( )

    ------------------------------------------------------------------------- Prevent if cinematic is enabled
    -- Disable cinematicGUI
    enableCinematicBlackBands( false );

    -- Disable firstStep of platforms
    isFirstStep_trackerPlatforms_Stage3 = false;
    
    -- Ensure that platforms are in correctTracker
    if( isEntityInThisTracker( "trackerPlatform_translate_20", "trackerPlatform_translate_20" ) == false or
        isEntityInThisTracker( "trackerPlatform_translate_21", "trackerPlatform_translate_21" ) == false or
        isEntityInThisTracker( "trackerPlatform_translate_22", "trackerPlatform_translate_22" ) == false ) then
        
	        setTrackerEntity( "trackerPlatform_translate_22", "trackerPlatform_translate_22", 3, 6.0 );
	        setTrackerEntity( "trackerPlatform_translate_21", "trackerPlatform_translate_21", 3, 6.0 );
	        setTrackerEntity( "trackerPlatform_translate_20", "trackerPlatform_translate_20", 3, 6.0 );
	        
	        -- SetPosition de cada platform
	        local initPlatform22 = getPostionWPTTracker("trackerPlatform_translate_22", 0 );
	        local initPlatform21 = getPostionWPTTracker("trackerPlatform_translate_21", 0 );
	        local initPlatform20 = getPostionWPTTracker("trackerPlatform_translate_20", 0 );
	        
	        setEntityPositionByName( "trackerPlatform_translate_22", initPlatform22 );
            setEntityPositionByName( "trackerPlatform_translate_21", initPlatform21 );
            setEntityPositionByName( "trackerPlatform_translate_20", initPlatform20 );
	
	        setThrowParticlesPlatform("trackerPlatform_translate_20", false );
	        setThrowParticlesPlatform("trackerPlatform_translate_21", false );
	        setThrowParticlesPlatform("trackerPlatform_translate_22", false );
	end
	---------------------------------------------------------------------------------------------------------

    -- Restart Default Speed
    setSpeedOfEntityTracker( "ringFinalBattle", 0.2 );
    setSpeedOfEntityTracker( "platform_horitzontal_2_60_rock", 0.2 );
    setSpeedOfEntityTracker( "platform_vertical_2_35", 0.2 );
    
    -- Posicionate entities
    local initialPos = getPostionWPTTracker( "tracker_FinalBoss_RingScriptedMov", 0 );
    setEntityPosition( getEntityID( "ringFinalBattle" ), initialPos );
    setEntityPosition( getEntityID( "platform_horitzontal_2_60_rock" ), initialPos );
    setEntityPosition( getEntityID( "platform_vertical_2_35" ), initialPos );
    
    -- Enable psEmiters again
    activate_psEmiters_EndStage2_finalBoss( 9.0 );
    
    -- Recover the currentCamera
    setCameraMode( 4, "", "trackerCamera_RingFinalBoss_Stage2_End", 40.0 );

return 0;
end

function restart_Stage3_finalBoss( numStage )

    -- Disable cinematicGUI
    enableCinematicBlackBands( false );

    -- Restart MegaWhiteShark, setting the initial state
    local idShark = getEntityID( "MegaWhiteShark" );
    sendMessage( 0.0, idShark, idShark, Telegram.Init_States_Phase3, NULL );
    
    MWS_CURRENT_DASH = numStage
    
    -- Put the lakeplants in correctPosition
    restartPlatformFinalBoss( numStage );
    
    -- Recover the currentCamera
    -- setCameraMode( 4, "", "trackerCamera_RingFinalBoss_Stage3", 40.0 );

return 0;
end

function restart_FinalStage_finalBoss()

    -- Restart MegaWhiteShark, setting the initial state
    local idShark = getEntityID( "MegaWhiteShark" );
    sendMessage( 0.0, idShark, idShark, Telegram.Init_States_Phase3, NULL );
    
    -- Restablim la camera d'aquesta zona
    setCameraMode( 4, "", "trackerCamera_LastHit_Prepare", 50.0 )
    
    -- Renovem la posició de respawn
    setPlayerLastCheckPoint("trigger_posSave13");
    
    -- Recuperem el estat de les plataformes
    hidePlatform( "trackerPlatform_translate_20" );
    checkandUnhidePlatform( "trackerPlatform_translate_21" );
    hidePlatform( "trackerPlatform_translate_22" );
    hidePlatform( "trackerPlatform_translate_23" );
    hidePlatform( "trackerPlatform_translate_24" );
    hidePlatform( "trackerPlatform_translate_25" );
    hidePlatform( "trackerPlatform_translate_26" );
    
    -- Eliminem les colisions de la finalZone
    setEntityValidByName("platform_vertical_bonusZone", false );
    setEntityValidByName("platform_horitzontal_bonusZone", false );
    setEntityValidByName("platform_horitzontal_stopPicat", false);
    
    -- desactivem la cinematica
    activateInput(true);
    enableCinematicBlackBands( false );

return 0;
end

----------------------------------------------------------------------- Logica del cami final a JawShark
function Unlock_Path_To_JawShark( )

    local functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_43\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_44\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_45\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_46\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_47\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_48\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_49\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_50\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_51\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_52\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_53\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_54\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_56\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_57\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_58\")";
	addOrderToSequencer ( functionOrder, 0.2 );
	
	functionOrder = "unHidePlatform(\"trackerPlatform_hiddePlayer_59\")";
	addOrderToSequencer ( functionOrder, 0.2 );

return 0;
end

----------------------------------------------------------------------- Unlock the finalLake Plants
function Unlock_FinalBoss_Lakeplants_Stage03( )

  -- BORRAR FASE3 Descomentar aquesta PART!!!
	--  unHidePlatform("trackerPlatform_translate_20");
	--  unHidePlatform("trackerPlatform_translate_21");
	--  unHidePlatform("trackerPlatform_translate_22");

	unHidePlatform("trackerPlatform_translate_23");
	unHidePlatform("trackerPlatform_translate_24");
	unHidePlatform("trackerPlatform_translate_25");
	unHidePlatform("trackerPlatform_translate_26");
	
return 0;
end

----------------------------------------------------------------------- Unlock final coins

function Unlock_RangedCoins( rangInf, rangSup, delayBetweenCoins, timePickUp )

	local functionOrder;
	local delayTime = 0.0;

	for i=rangInf,rangSup do
	
		if(i < 10) then
			makeAppearItem("Coin_hiden0" .. i .. "", delayTime, timePickUp )
			delayTime = delayTime + delayBetweenCoins
		end
	
		if(i >= 10) then
			makeAppearItem("Coin_hiden" .. i .. "", delayTime, timePickUp )
			delayTime = delayTime + delayBetweenCoins
		end
	end

return 0;
end

function BonusCoinsStage01()

	Unlock_RangedCoins(0, 33, 0.2, 5.0);
	
	local functionOrder = "BonusCoinsStage02()";
	addOrderToSequencer ( functionOrder, 10.0 );

return 0;
end

function BonusCoinsStage02()

	Unlock_RangedCoins(54, 73, 0.2, 8.0);
    
    local functionOrder = "BonusCoinsStage03()";
	addOrderToSequencer ( functionOrder, 10.0 );

return 0;
end

function BonusCoinsStage03()
	
	local functionOrder = "Unlock_RangedCoins(34, 38, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 4.0 );
	
	functionOrder = "Unlock_RangedCoins(39, 43, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 4.0 );
	
	functionOrder = "Unlock_RangedCoins(44, 48, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 4.0 );
	
	functionOrder = "Unlock_RangedCoins(49, 53, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 4.0 );
	
	local functionOrder = "BonusCoinsStage04()";
	addOrderToSequencer ( functionOrder, 4.0 );

return 0;
end

function BonusCoinsStage04()
	
	local functionOrder = "Unlock_RangedCoins(158, 160, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 4.0 );
	
	functionOrder = "Unlock_RangedCoins(161, 163, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 1.0 );
	
	functionOrder = "Unlock_RangedCoins(164, 166, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 1.0 );
	
	functionOrder = "Unlock_RangedCoins(167, 169, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 1.0 );
	
	functionOrder = "Unlock_RangedCoins(160, 172, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 1.0 );
	
	functionOrder = "Unlock_RangedCoins(173, 176, 0.1, 5.0)";
	addOrderToSequencer ( functionOrder, 1.0 );
	
	local functionOrder = "BonusCoinsStage05()";
	addOrderToSequencer ( functionOrder, 4.0 );

return 0;
end

function BonusCoinsStage05()
	
	local functionOrder = "Unlock_RangedCoins(177, 200, 0.2, 5.0)";
	addOrderToSequencer ( functionOrder, 0.0 );
	
	local functionOrder = "BonusCoinsStage06()";
	addOrderToSequencer ( functionOrder, 10.0 );

return 0;
end

function BonusCoinsStage06()

	setCameraModeBlendInfo( 2, "trackerPlatform_static_03", "trackerCamera_42", 50.0, 0.9, 0.95 );
	Unlock_RangedCoins( 74, 157, 0.0, -1.0 );

return 0;
end