//
// 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:
//      Main Script Module
//

// mod hooks
funcdef void MAIN_VOIDFUNC();
funcdef void MAIN_SAVEFUNC(int);
funcdef void MAIN_PICKUPFUNC(kActor@);
array<MAIN_VOIDFUNC@> hookBeginLevel, hookPostBeginLevel, hookEndLevel, hookTick, hookPostTick, hookMenuTick, hookNewGame;
array<MAIN_SAVEFUNC@> hookSaveGame;
array<MAIN_PICKUPFUNC@> hookPlayerPickup;
bool mainMapChange = true;

#include "scripts/common.txt"
#include "scripts/animal.txt"
#include "scripts/misc/traps.txt"
#include "scripts/misc/portalHub.txt"
#include "scripts/misc/mantisObjects.txt"
#include "scripts/misc/destructibles.txt"
#include "scripts/misc/catacombWater.txt"
#include "scripts/misc/giblet.txt"
#include "scripts/misc/quakeSource.txt"
#include "scripts/misc/warpPortal.txt"
#include "scripts/misc/levelPortal.txt"
#include "scripts/misc/iggy.txt"
#include "scripts/movers/door.txt"
#include "scripts/movers/seqEvent.txt"
#include "scripts/movers/floor.txt"
#include "scripts/enemy/enemy.txt"
#include "scripts/enemy/grunt.txt"
#include "scripts/enemy/alien.txt"
#include "scripts/enemy/insect.txt"
#include "scripts/enemy/purlin.txt"
#include "scripts/enemy/raptor.txt"
#include "scripts/enemy/rider.txt"
#include "scripts/enemy/sandworm.txt"
#include "scripts/enemy/turret.txt"
#include "scripts/enemy/sentry.txt"
#include "scripts/enemy/sewerCrab.txt"
#include "scripts/player.txt"
#include "scripts/weapons.txt"
#include "scripts/pickupSpawner.txt"
#include "scripts/bosses/hummer.txt"
#include "scripts/bosses/longhunter.txt"
#include "scripts/bosses/mantis.txt"
#include "scripts/bosses/trex.txt"
#include "scripts/bosses/campainger.txt"

#include "addons/1/scripts/main.txt"
#include "addons/2/scripts/main.txt"
#include "addons/3/scripts/main.txt"
#include "addons/4/scripts/main.txt"
#include "addons/5/scripts/main.txt"

// Smoke39
const kStr TurokPlusVersion = "3.04";
#include "scripts/enemy/TPlus_KillerPlant.cs"
#include "scripts/TPlus/Debug.cs"
#include "scripts/TPlus/HUD.cs"
#include "scripts/TPlus/Inventory.cs"
#include "scripts/TPlus/Math.cs"
#include "scripts/TPlus/Menu.cs"
#include "scripts/TPlus/Options.cs"
#include "scripts/TPlus/Pickups.cs"
#include "scripts/TPlus/Projectiles.cs"
#include "scripts/TPlus/Taunt_System.cs"
#include "scripts/TPlus/Title.cs"
#include "scripts/TPlus/Util.cs"

void ResetGameVars()
{
    GameVarsSetAdd("bStartedHubIntro", "0");
    GameVarsSetAdd("lastPortalWarpChoice", "0");
    GameVarsSetAdd("chronoPieceFlags", "0");
    GameVarsSetAdd("hubPanelPieces1", "0");
    GameVarsSetAdd("hubPanelPieces2", "0");
    GameVarsSetAdd("hubPanelPieces3", "0");
    GameVarsSetAdd("hubPanelPieces4", "0");
    GameVarsSetAdd("hubPanelPieces5", "0");
    GameVarsSetAdd("hubPanelPieces6", "0");
    GameVarsSetAdd("hubPanelPieces7", "0");
    GameVarsSetAdd("bGotLonghunterKey", "0");
    GameVarsSetAdd("bGotMantisKey", "0");
    GameVarsSetAdd("hummer1State", "0");
    GameVarsSetAdd("hummer1Health", "0");
    GameVarsSetAdd("hummer1Sector", "-1");
    GameVarsSetAdd("hummer1Origin", "0.0 0.0 0.0");
    GameVarsSetAdd("hummer1Yaw", "0.0");
    GameVarsSetAdd("hummer2State", "0");
    GameVarsSetAdd("hummer2Health", "0");
    GameVarsSetAdd("hummer2Sector", "-1");
    GameVarsSetAdd("hummer2Origin", "0.0 0.0 0.0");
    GameVarsSetAdd("hummer2Yaw", "0.0");
    GameVarsSetAdd("longHunterState", "0");
    GameVarsSetAdd("longHunterHealth", "0");
    GameVarsSetAdd("longHunterSector", "-1");
    GameVarsSetAdd("longHunterOrigin", "0.0 0.0 0.0");
    GameVarsSetAdd("longHunterYaw", "0.0");
    GameVarsSetAdd("longHunterObservationPoint", "0");
    GameVarsSetAdd("mantisState", "0");
    GameVarsSetAdd("mantisHealth", "0");
    GameVarsSetAdd("mantisSector", "-1");
    GameVarsSetAdd("mantisOrigin", "0.0 0.0 0.0");
    GameVarsSetAdd("mantisYaw", "0.0");
    GameVarsSetAdd("mantisWallBits", "0");
    GameVarsSetAdd("mantisActive", "0");
    GameVarsSetAdd("trexState", "0");
    GameVarsSetAdd("trexHealth", "0");
    GameVarsSetAdd("campaignerState", "0");
    GameVarsSetAdd("campaignerHealth", "0");
    GameVarsSetAdd("campaignerShieldHealth", "0");
	// Smoke39
	SetGameVarS( "Turok+Version", TurokPlusVersion );
	SetGameVarB( "Turok+SetupComplete", false );
	SetGameVarS( "secrets", "" );
	// settings
	Opt::InitSettings();
	// inventory
	SetGameVarI( "Smoke39Flags", DefaultSmoke39Flags );
	SetGameVarF( "MeleePoints",  0 );
	// fire modes
	SetGameVarB( "bPistolBurst", true );
	SetGameVarB( "bPistolLaser", true );
	SetGameVarB( "bARAuto",      true );
	SetGameVarB( "bPulseBeam",   false );
	SetGameVarB( "bAlienFlamer", false );
	SetGameVarB( "bGLImpact",    false );
}

#define CALL_HOOK_FUNCS(x) for(uint i = 0; i < x.length(); ++i) x[i]();

//==============================================================
// Called before game is saved
void savegame(int slot)
{
    for(uint i = 0; i < hookSaveGame.length(); ++i)
        hookSaveGame[i](slot);
}
//==============================================================
void newgame()
{
    ResetGameVars();
    CALL_HOOK_FUNCS(hookNewGame)
}
//==============================================================
// Called before all actors OnBeginLevel
void GlobalBeginLevel()
{
    CALL_HOOK_FUNCS(hookBeginLevel)
}
//==============================================================
// Called after all actors OnPostBeginLevel
void GlobalPostBeginLevel()
{
    CALL_HOOK_FUNCS(hookPostBeginLevel)
}
//==============================================================
// Called after all actors OnEndLevel
void GlobalEndLevel()
{
    CALL_HOOK_FUNCS(hookEndLevel)
}
//==============================================================
// Called before all actors OnTick
void GlobalTick()
{
    CALL_HOOK_FUNCS(hookTick)
}
//==============================================================
// Called after all actors OnTick and level script tick
void GlobalPostTick()
{
    CALL_HOOK_FUNCS(hookPostTick)
}
//==============================================================
// Called after all actors OnMenuTick
void GlobalMenuTick()
{
    CALL_HOOK_FUNCS(hookMenuTick)
}

#undef CALL_HOOK_FUNCS

void main()
{
	// Smoke39
	kStr str = "Turok+ v";
	if ( TurokPlusVersion.Contains("WIP") )
		Sys.Warning( str + TurokPlusVersion );
	else
		Sys.Print( str + TurokPlusVersion );
	if ( GetCvarB("developer") )
		Sys.Warning( "Dev Mode: Enabled" );
	else
		Sys.Print( "Dev Mode: Disabled" );
	Opt::LoadGlobalSettings();
	Game.SetModBindings();

    ResetGameVars();

    // variables which are once-only should be added here
    GameVarsSetAdd("bStartedIntro", "0");
    GameVarsSetAdd("g_startmap", "levels/level05.map");
    GameVarsSetAdd("g_restartmap", "levels/level42.map");
    GameVarsSetAdd("g_trainingmap", "levels/level47.map");
    GameVarsSetAdd("g_creditsmap", "levels/level02.map");
    GameVarsSetAdd("g_gallerymap", "levels/level50.map");
    GameVarsSetAdd("g_hubmapid", "5");
    GameVarsSetAdd("g_hubwarptid", "10666");

    mod1_main();
    mod2_main();
    mod3_main();
    mod4_main();
    mod5_main();

    if(mainMapChange)
    {
        kStr szShowIntros;
        if(Sys.GetCvarValue("g_showgameintros", szShowIntros) && szShowIntros.Atoi() == 0)
        {
            // skip directly to title
            Game.ChangeMap("levels/level42.map");
            return;
        }

        // Smoke39 - change from 46 (iguana) back to 45 (acclaim)
        Game.ChangeMap("levels/level45.map");
    }
}
