-----------------------------------------------------------------------------
-- CONSTANTS

    I3D_LIGHT_OMNI      = 1;
    I3D_LIGHT_SPOT      = 2;
    I3D_LIGHT_INFINITE  = 3;
    
    DEG2RAD             = 0.0174532925;
    PI                  = 3.1415927;
--    TRUE                = 1;
--    FALSE               = 0;
    
    
    WPT_0               = 0;
    WPT_1               = 1;
    WPT_2               = 2;
    WPT_3               = 3;
    WPT_4               = 4;
    WPT_5               = 5;
    SPEED_MIN           = 0.0;
    SPEED_MAX           = 1.0;
    
    
-----------------------------------------------------------------------------
-- CREATE AND NAME ENTITY

function CreateAndNameEntity(class, name, parent)
    local ent;
    ent = Entity.Create(class, parent);
    System.Assert(ent, "Failure creating entity"..name.."!");
    if (ent) then
        Entity.SetString(ent, "name", name);    
        end;
    return ent;        
    end      
    
-----------------------------------------------------------------------------

function FadeOut()
    local entRoot;
    local entViewport;
    entRoot = Game.GetRootEntity();
    entViewport = Entity.FindClass(entRoot, "Viewport");
    if (entViewport) then
        Entity.SetFloat(entViewport, "fadeAlpha",  0.0);    
        Entity.SetFloat(entViewport, "fadeRate",   4.0);    
        Entity.SetFloat(entViewport, "fadeTarget", 1.0);    
        end;         
    end;        
    
-----------------------------------------------------------------------------

function FadeIn()
    local entRoot;
    local entViewport;    
    
    entRoot = Game.GetRootEntity();
    entViewport = Entity.FindClass(entRoot, "Viewport");
    if (entViewport) then
        Entity.SetFloat(entViewport, "fadeAlpha",  1.0);    
        Entity.SetFloat(entViewport, "fadeRate",   1.0);    
        Entity.SetFloat(entViewport, "fadeTarget", 0.0);    
        end;        
    end;     


-----------------------------------------------------------------------------
-- Sets the screen to black immediately.

function FadeSetBlack()
    local entRoot;
    local entViewport;
    entRoot = Game.GetRootEntity();
    entViewport = Entity.FindClass(entRoot, "Viewport");
    if (entViewport) then
        Entity.SetFloat(entViewport, "fadeAlpha",  1.0);    
        Entity.SetFloat(entViewport, "fadeRate",   4.0);    
        Entity.SetFloat(entViewport, "fadeTarget", 1.0);    
        end;         
    end;     
    
-----------------------------------------------------------------------------

function CreateViewports()

    entViewport = Game.GetViewportEntity()

    -- setup a primary viewport    
    entViewportMain = Entity.Create("Viewport", entViewport);
    System.Assert(entViewportMain);
    if (entViewportMain) then
        Entity.SetString(entViewportMain, "name", "ViewportMain");
        -- viewport entities default to full-screen
        --Entity.SetFloat(entViewportMain,  "x",      0);
        --Entity.SetFloat(entViewportMain,  "y",      0);
        Entity.SetFloat(entViewportMain,  "width",  0);
        --Entity.SetFloat(entViewportMain,  "height", 920);
        Entity.SetVoid(entViewportMain, "entDrawRoot", Game.GetWorldEntity());
        Entity.SetBool(entViewportMain, "clear", true);
        Entity.SetBool(entViewportMain, "setEarPos", true);
        Entity.SetFloat(entViewportMain,  "farClip", 4500.0);
        end;
        
    -- setup a second viewport for testing        
--    entViewport2 = Entity.Create("Viewport", entViewport);
--    System.Assert(entViewport2);
--    if (entViewport2) then
--        Entity.SetString(entViewport2, "name", "Main Viewport");
--        Entity.SetInt   (entViewport2, "x",      2);
--        Entity.SetInt   (entViewport2, "y",      2);
--        Entity.SetInt   (entViewport2, "width",  128);
--        Entity.SetInt   (entViewport2, "height", 128);
--        Entity.SetVoid  (entViewport2, "entDrawRoot", Game.GetWorldEntity());
--        Entity.SetBool  (entViewport2, "clear", true);
--        end;        
        
    end;                 
    
    
-----------------------------------------------------------------------------

function SetupWorld(file1, file2, file3)

    entWorld = Game.GetWorldEntity();
    
    entFirst        = CreateAndNameEntity("ENTNULL",   "First",     entWorld);
    entLast         = CreateAndNameEntity("ENTNULL",   "Last",      entWorld);
    entVeryLast     = CreateAndNameEntity("ENTNULL",   "VeryLast",  entWorld);
    entVeryVeryLast = CreateAndNameEntity("ENTNULL",   "VeryVeryLast",  entWorld);
    entParticles    = CreateAndNameEntity("Particles", "Particles", entVeryVeryLast);
    CreateAndNameEntity("Mdl3Particles", "Mdl3Particles", entWorld);        
    
    CreateViewports();
    
    -- flyable viewpoint observer
    entObserver = CreateAndNameEntity("Observer2", "Observer0", entWorld);
    if (entObserver) then
        Entity.SetVector3(entObserver, "pos", 510.0, 80.0, 510.0);
        Entity.SetLocalPlayer(entObserver, 1);
        Entity.BindGamepad(entObserver, 0);
        Entity.Start     (entObserver);
        end;        
     
    -- the camera needs to find the game grid, so do it here        
    entCameraMain = CreateAndNameEntity("CameraRts1", "Observer1", entWorld);
    if (entCameraMain) then
        --Entity.SetString  (entCameraMain, "name", "Observer1");
        Entity.SetVector3 (entCameraMain, "pos", 1024.0, 80.0, 1024.0);
        Entity.SetFloat   (entCameraMain, "minPitch", 0.1);
        Entity.SetFloat   (entCameraMain, "minX", 0);
        Entity.SetFloat   (entCameraMain, "maxX", 2048);
        Entity.SetFloat   (entCameraMain, "minZ", 0);
        Entity.SetFloat   (entCameraMain, "maxZ", 2048);
        Entity.SetFloat   (entCameraMain, "zoom", 200);
        Entity.SetFloat   (entCameraMain, "minZoom", 3);
        Entity.SetFloat   (entCameraMain, "maxZoom", 1000);        
        Entity.BindGamepad(entCameraMain, -1);
        Entity.Start      (entCameraMain);
        end;        
        
    -- the camera needs to find the game grid, so do it here        
    entCameraOrtho = CreateAndNameEntity("OrthoCamera", "OrthoCam", entWorld);
    if (entCameraOrtho) then
        Entity.SetVector3 (entCameraOrtho, "pos", 1024.0, 80.0, 1024.0);
        Entity.SetFloat   (entCameraOrtho, "pitch", 3.1415927/2.0);
        Entity.SetInt     (entCameraOrtho, "viewWidth",  2048);
        Entity.SetInt     (entCameraOrtho, "viewHeight", 2048);
        Entity.Start      (entCameraOrtho);
        end;           
        
    -- now, start all the entities we just made
    --Entity.StartPre(entWorld);        
    
    -- load the entity files
    if (file1) then
        Game.LoadEntitiesIni(file1);
        end;
    if (file2) then
        Game.LoadEntitiesIni(file2);
        end;
    if (file3) then
        Game.LoadEntitiesIni(file3);
        end;
        
    -- tell the main viewport to use the view from the main camera
    Entity.SetVoid(entViewportMain, "entCamera", entObserver);
    
    -- tell the main camera to use look at keyboard input
    Game.SetKeyboardEnt(entObserver);        
    
    -- send a game event notifying everyone that the world is loaded
--MMH    Entity.GameEventPre(Game.GetRootEntity(), GE_WORLDLOADED, 0);    

    -- builds a list of coins on this map    
    --EntGame.PlayNextSong();
        
    end;

   
-----------------------------------------------------------------------------
-- START EVENT

function StartEvent(ini1, ini2, ini3, ini4)


    EntGame.PushMenu("MenuGameRunning");
    
    -- load world
    SetupWorld(ini1, ini2, ini3);

    -- setup tournament logic    
    EntGame.TourneyPrepare();           

    -- go to the intro camer
    EntGame.BindPlayerToEntity("IntroCamera");

    -- go black
    FadeSetBlack();

    -- wait for the game running menu to be created - this may be fixed later
    Script.WaitFrame(1);

    EntGame.ClearFlag(GF_READYTOSTART);
    EntGame.ClearFlag(GF_SHOWTOURNEYTIME);
    EntGame.ClearFlag(GF_SHOWTOURNEYWEIGHT);
    EntGame.SetFlag  (GF_SHOWLINEOUT);
    EntGame.ClearFlag(GF_TOURNEYRUNNING);
--    EntGame.PushMenu("MenuGameRunning");
    --EntGame.PushMenu("MenuEventIntro");
--    EntGame.PlaySong("Music\\Tournament1Theme.ogg")

    FadeIn();
    
    Script.WaitSec(1.0);
    EntGame.PushMenu("MenuEventIntro");

    -- wait until the intro dialog is dispatched
    while (EntGame.GetFlag(GF_READYTOSTART) == false) do
        Script.WaitFrame(1);
        end;

    -- give the player control of the fisherman entity - must always be named Player0
    EntGame.BindPlayerToEntity("Player0");
    -- if in single player mode, disable the player until the stage-start menu is done
    if (EntGame.GetGameMode() == GM_PARTY) then
        EntGame.EnablePlayerControl(true);
        end;
    if (EntGame.GetGameMode() == GM_FREE) then
        EntGame.EnablePlayerControl(true);
        end;        

    -- set a bunch of game-state flags
    EntGame.SetFlag(GF_SHOWTOURNEYTIME);
    EntGame.SetFlag(GF_SHOWTOURNEYWEIGHT);
    EntGame.SetFlag(GF_TOURNEYRUNNING);

    --entRoot = Game.GetRootEntity();
    --entGame = Entity.FindName(entRoot, "Game");
    --Entity.SetFloat(entGame, "tourneyTimer", 180.0);
    --Entity.SetFloat(entGame, "tourneyWeight", 0.0);
    --Entity.SetFloat(entGame, "tourneySpecies", 0.0);
    
    
    end;
    
    
-----------------------------------------------------------------------------    
-- WAIT FOR KEY
-- Useful function to suspend script activity until the app clears the
-- DEBUGWAIT flag.

function WaitForKey()
    EntGame.SetFlag(GF_DEBUGWAIT);
--    System.ConsoleBeep(440, 400);
    while (EntGame.GetFlag(GF_DEBUGWAIT) == true) do
       Script.WaitFrame(10);
       end;
--    System.ConsoleBeep(880, 200);       
    end;     
        
        
-----------------------------------------------------------------------------    
-- DEBUG MESSAGE
        
debug = true

function DebugMessage(message, time)
    if (debug == true) then
        EntGame.ShowCaption(message, 0, time);
        end;
    end;        
    
-----------------------------------------------------------------------------    
-- RGB255
-- Converts 4 0-255 values into 4 0-1 values

function RGB255(a,r,g,b)      
    return a/255, r/255, g/255, b/255
    end;


-----------------------------------------------------------------------------    
-- STARTUP SEQUENCE
-- Common startup sequence for stages.    
    
function StartupSequence(musicFile)
    -- setup tournament logic    
    EntGame.TourneyPrepare();           

    -- show the scene from the intro camera
    EntGame.BindCameraToEntity("IntroCamera");
    
    -- tell the game the world has loaded
    Entity.GameEventPre(Game.GetRootEntity(), GE_WORLDLOADED, 0);	

    -- wait until the intro dialog is dispatched
    while (EntGame.GetFlag(GF_READYTOSTART) == false) do
        Script.WaitFrame(1);
        end;

    -- do a fade out        
    FadeOut();
    Script.WaitSec(0.5);    
   
    EntGame.PlaySong(musicFile);

    -- while the scene is black, change the view
    -- grab the camera
    entCamera = Entity.FindName(entRoot, "MainCamera");
    -- show the scene from the camera view
    EntGame.BindCameraToEntity(entCamera);
        
    -- now, fade in, give the player control and turn the HUD on        
    FadeIn();            
    EntGame.BindInputToEntity("Player1");
    Entity.GameEventPre(Game.GetRootEntity(), SSA1_START, 0);
    end;
    
    
-----------------------------------------------------------------------------

function Run(name)    
    local animal;
	animal = Entity.FindName(entRoot, name);
	if (animal) then
    	Animal.SetState(animal, ST_WPT_RUN);	
    	end;
    end;    

-----------------------------------------------------------------------------    
    
function Walk(name)    
    local animal1;
	animal1 = Entity.FindName(entRoot, name);
	if (animal1) then
    	Animal.SetState(animal1, ST_WPT_WALK);
    	end;
    end;  
  
-----------------------------------------------------------------------------    
    
function Graze(name)    
    local animal3;
	animal3 = Entity.FindName(entRoot, name);
	if (animal3) then
    	Animal.SetState(animal3, ST_GRAZING);
    	end;
    end;  
  
-----------------------------------------------------------------------------   

function Gone(name)    
    local animal2;
	animal2 = Entity.FindName(entRoot, name);
	if (animal2) then
    	Animal.SetState(animal2, ST_GONE);	
    	end;
    end;      