//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Level Script For Level05.map
//

#include "scripts/common.txt"

/*
==============================================================
Script 0
==============================================================
*/

$script 0
{
    bool bStartedIntro;
    bool bNewGame;
    
    GameVariables.GetBool("g_newgame", bNewGame);
    GameVariables.GetBool("bStartedHubIntro", bStartedIntro);
    
	Game.CallDelayedMapScript(4, instigator, 1.0f); //Check for 8 Chronopieces to unlock final stage.
	
    if(bNewGame)
    {
        // make sure all chronoscepter and key pieces have been cleared when
        // starting a new game
        GameVariables.SetValue("chronoPieceFlags", "0");
        GameVariables.SetValue("hubPanelPieces1", "0");
        GameVariables.SetValue("hubPanelPieces2", "0");
        GameVariables.SetValue("hubPanelPieces3", "0");
        GameVariables.SetValue("hubPanelPieces4", "0");
        GameVariables.SetValue("hubPanelPieces5", "0");
        GameVariables.SetValue("hubPanelPieces6", "0");
        GameVariables.SetValue("hubPanelPieces7", "0");
    }
    
    // start the cinematic if we're starting a new game, but also make
    // sure that we don't trigger this if we're entering this level normally
    if(bStartedIntro && !bNewGame)
    {
        return;
    }
    
    Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_NO_LETTERBOX|CMF_NO_INITIAL_FADEOUT);
    GameVariables.SetValue("bStartedHubIntro", "1");
    
    Camera.fov = 74.0f;
    
    //
    // the camera zooms in and rotates around the hub.
    // two separate camera paths are blended together. Neat eh?
    //
    
    Camera.SetFinalView(0);
    Camera.SetRotateEyeVector(kVec3(-(179*GAME_SCALE), (358*GAME_SCALE), (100*GAME_SCALE)));
    
    Camera.SetPositionTrack(1, 
                            kVec3(-(179*GAME_SCALE), (358*GAME_SCALE), (250*GAME_SCALE)),
                            kVec3(-(179*GAME_SCALE), (358*GAME_SCALE), (100*GAME_SCALE)),
                            kVec3(-(179*GAME_SCALE), (358*GAME_SCALE), (90*GAME_SCALE)),
                            kVec3(-(179*GAME_SCALE), (358*GAME_SCALE), (90*GAME_SCALE)));
                                       
    Camera.AutoPlayPositionTrack(1, 6.0f, CMLT_LINEAR);
    
    Camera.SetRotationTrack(2,
                            Math::Deg2Rad(-360.0f),
                            Math::Deg2Rad(0.0f),
                            (70*GAME_SCALE),
                            (70*GAME_SCALE),
                            (10*GAME_SCALE),
                            (10*GAME_SCALE),
                            (6*GAME_SCALE),
                            (6*GAME_SCALE));
                                       
    Camera.AutoPlayRotationTrack(2, 32.0f, CMLT_LINEARLOOP);
    
    Camera.AutoPlayBlendTrack(0, 1, 2, 6.0f, CMLT_COSINE);
    
    Game.CallDelayedMapScript(2, instigator, 1.0f);
    
    delay(2.0f);
    	
		
	

    
	Game.PrintLine("HAPPY HALLOWEEN ENJOY HALLOROK 2021", 1, 512);
    Game.PrintLine("AND DESTROY THE DARKNESS WITHIN", 2, 512);	
	Game.PrintLine("TO UNLOCK THE GATE OF DESTINY", 3, 512);
	Game.PrintLine("OF THE FIRST PERSON SAINTS", 4, 512);	
	Game.PrintLine("LOCATE THE EIGHT GRAVES", 5, 512);
	
    
    delay(10.0f);
    PlayLoop.ChangeMap("levels/hallorok21_p1.map");
}

/*
==============================================================
Script 2
==============================================================
*/

$script 2
{
    if(Camera.UserInterrupted())
    {
        Game.CallDelayedMapScript(3, instigator, 2.0f);
		Game.CallDelayedMapScript(4, instigator, 0); //Check for 8 Chronopieces to unlock final stage.
        return;
    }
    
    $restart;
}

/*
==============================================================
Script 3
==============================================================
*/

$script 3
{
	
    PlayLoop.ChangeMap("levels/hallorok21_p1.map");	
}


/*
==============================================================
Script 4
==============================================================
*/

$script 4
{
		bool hasUnlocked = false;
        int giveBits = 0;
        int chronoBits;
        GameVariables.GetInt("chronoPieceFlags", chronoBits);
        chronoBits |= (1 << giveBits);
        
        if(chronoBits == 0xFF && !hasUnlocked)
        {
            Game.PrintLine("FINAL AREA UNLOCKED. FACE THE DARKNESS.", 1, 1024);
			hasUnlocked = true;

		
		//We don't want the message to repeat but we want to ensure the portal is always active so we restart constantly.
		//This is a disgusting hack but this project was made in limited time to release on Halloween 2021. - DM23
		
		//Enables Final Boss Teleporter 
		kActor @warp = World.GetActorByTID(18999);
		warp.Flags() &= ~AF_HIDDEN;
        warp.Flags() |= AF_CANBETOUCHED;
		//Disable Hidden and Make Touchable			
			
        }
		
		delay(2.0f);

		if (!hasUnlocked)
		{$restart;}
		
}