//
// 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 Portal Object Class
//

#include "scripts/common.txt"

/*
==============================================================
TurokLevelPortal
==============================================================
*/

class TurokLevelPortal : BPR::CL_BPR_WorldObject //[BP_Randomizer]
{
    kActor @self;
    
    TurokLevelPortal(kActor @actor)
    {
		super(actor); //[BP_Randomizer]
        @self = actor;
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
        if((self.Flags() & AF_ACTIVATED) != 0)
        {
            self.PlaySound("sounds/shaders/generic_187.ksnd");
        }
    }
    
    /*
    ==============================================================
    OnBeginLevel
    ==============================================================
    */
    
    void OnBeginLevel(void)
    {
        if(Game.GetCurrentMapID() == 5)
        {
            self.Flags() |= AF_HIDDEN;
        }
        else
        {
            self.Flags() |= AF_ACTIVATED;
            World.ChangeAreaFlag(self.AreaID(), AAF_TELEPORT, true);
        }
    }
    
    /*
    ==============================================================
    OnActivate
    ==============================================================
    */
    
    void OnActivate(void)
    {
        //[BP_Randomizer]
        if(BPR::g_bForceLevelPortalActivate)
        {
            self.MarkPersistentBit(false);
            OnRestore();
            return;
        }
        
        int nKeys, remainingKeys, keyBits;
        
        if(Game.GetCurrentMapID() != 5)
        {
            return;
        }
        
        if(Game.GetHubKeyInfo(self.TID(), nKeys, remainingKeys, keyBits))
        {
            int access = 0;
            int nPieces = self.TID() == 8 ? 6 : 3;
            
            for(int i = 0; i < nPieces; ++i)
            {
                if((keyBits & (1 << i)) != 0)
                {
                    access++;
                }
            }

            if(access < nPieces)
            {
                // allow it to be retriggered
                self.Flags() |= AF_HIDDEN;
                self.Flags() &= ~AF_ACTIVATED;
                return;
            }
        }
        
        self.Flags() &= ~AF_HIDDEN;
        self.RunFxEvent("Portal_Init");
        World.ChangeAreaFlag(self.AreaID(), AAF_TELEPORT, true);
        
        self.MarkPersistentBit(false);
    }
    
    /*
    ==============================================================
    OnSpawn
    ==============================================================
    */
    
    void OnSpawn(void)
    {
    }
    
    /*
    ==============================================================
    OnRestore
    ==============================================================
    */
    
    void OnRestore(void)
    {
        if(Game.GetCurrentMapID() != 5)
        {
            return;
        }
        
        self.Flags() |= AF_ACTIVATED;
        self.Flags() &= ~AF_HIDDEN;
        self.RunFxEvent("Portal_Init");
        World.ChangeAreaFlag(self.AreaID(), AAF_TELEPORT, true);
    }
};
