//-----------------------------------------------------------------------------

// Variables used by client scripts & code.  The ones marked with (c)
// are accessed from code.  Variables preceeded by Pref:: are client
// preferences and stored automatically in the ~/client/prefs.cs file
// in between sessions.
//
//    (c) Client::MissionFile             Mission file name
//    ( ) Client::Password                Password for server join

//    (?) Pref::Player::CurrentFOV
//    (?) Pref::Player::DefaultFov
//    ( ) Pref::Input::KeyboardTurnSpeed

//    (c) pref::Master[n]                 List of master servers
//    (c) pref::Net::RegionMask     
//    (c) pref::Client::ServerFavoriteCount
//    (c) pref::Client::ServerFavorite[FavoriteCount]
//    .. Many more prefs... need to finish this off

// Moves, not finished with this either...
//    (c) firstPerson
//    $mv*Action...

//-----------------------------------------------------------------------------
// These are variables used to control the shell scripts and
// can be overriden by mods:
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// loadMaterials - load all materials.cs files
//-----------------------------------------------------------------------------
function loadMaterials()
{
   for( %file = findFirstFile( "*/materials.cs" ); %file !$= ""; %file = findNextFile( "*/materials.cs" ))
   {
      exec( %file );
   }
}

function reloadMaterials()
{
   reloadTextures();
   loadMaterials();
   reInitMaterials();
}

//-----------------------------------------------------------------------------
function initClient()
{
   echo("\n--------- Initializing " @ $appName @ ": Client Scripts ---------");

   // Make sure this variable reflects the correct state.
   $Server::Dedicated = false;

   // Game information used to query the master server
   $Client::GameTypeQuery = $appName;
   $Client::MissionTypeQuery = "Any";

   exec("./ui/customProfiles.cs"); // override the base profiles if necessary

   // The common module provides basic client functionality
   initBaseClient();

   // Use our prefs to configure our Canvas/Window
   configureCanvas();

   // Set the window title
   if (isObject(Canvas))
      Canvas.setWindowTitle(getEngineName() @ " (" @ getVersionString() @ ") - " @ $appName);
      
   /// Load client-side Audio Profiles/Descriptions
   exec("./scripts/audioProfiles.cs");

   // Load up the Game GUIs
   exec("./ui/defaultGameProfiles.cs");
   exec("./ui/PlayGui.gui");
   //exec("./ui/ChatHud.gui");
   //exec("./ui/playerList.gui");

   // Load up the shell GUIs
   //exec("./ui/aboutDlg.gui");
   //exec("./ui/startMissionGui.gui");
   //exec("./ui/joinServerGui.gui");
   //exec("./ui/endGameGui.gui");
   exec("./ui/loadingGui.gui");
   exec("./ui/optionsDlg.gui");
   exec("./ui/remapDlg.gui");

   // load the unified shell overrides
   exec("./scripts/ShellGui.cs");

   // Client scripts
   exec("./scripts/client.cs");
   exec("./scripts/game.cs");
   exec("./scripts/missionDownload.cs");
   exec("./scripts/serverConnection.cs");
   //exec("./scripts/playerList.cs");
   exec("./scripts/loadingGui.cs");
   exec("./scripts/optionsDlg.cs");
   //exec("./scripts/chatHud.cs");
   //exec("./scripts/messageHud.cs");
   exec("./scripts/playGui.cs");
   //exec("./scripts/centerPrint.cs");
   exec("./scripts/message.cs");

   // Load useful Materials
   exec("./scripts/glowBuffer.cs");
   exec("./scripts/shaders.cs");
   exec("./scripts/commonMaterialData.cs" );

   // Default player key bindings
   exec("./scripts/default.bind.cs");

   if (isFile("./config.cs"))
      exec("./config.cs");

   // Load Example objectView 
   exec("./ui/objectViewExample.gui");
   exec("./scripts/objectViewExample.cs");

   loadMaterials();

   // Really shouldn't be starting the networking unless we are
   // going to connect to a remote server, or host a multi-player
   // game.
   setNetPort(0);

   // Copy saved script prefs into C++ code.
   setDefaultFov( $pref::Player::defaultFov );
   setZoomSpeed( $pref::Player::zoomSpeed );

   // Start up the main menu... this is separated out into a 
   // method for easier mod override.
   loadMainMenu();

   // Connect to server if requested.
   if ($JoinGameAddress !$= "") {
      connect($JoinGameAddress, "", $Pref::Player::Name);
   }
}

//-----------------------------------------------------------------------------
function loadPlayerPickerData()
{
   // Load the datablocks we need for picking a player.
   // This function just needs to populate PlayerDatasGroup
   // with a valid set of objects (could be ScriptObjects,
   // PlayerDatas, or StaticShapeDatas for example). The
   // objects just need to have a name and a "shapeFile"
   // field. For now we are just pulling this directly from
   // the same set of PlayerDatas that we load on the server
   // since it will be much simpler for a new user to figure
   // out what to change to add their own Player's to the
   // picker but it would be a good optimization to use a
   // lighter-weight object.
   
   // Create our SimSet to hold the player datablocks for the picker
   if (!isObject(PlayerDatasGroup))
      new SimSet(PlayerDatasGroup);

   // We have to load these audio profiles to avoid a set of warnings
   exec("~/server/scripts/audioProfiles.cs");

   // Load our default player script
   exec("~/server/scripts/players/player.cs");

   // Load our other player scripts
   exec("~/server/scripts/players/BoomBot.cs");
   exec("~/server/scripts/players/Elf.cs");
   exec("~/server/scripts/players/ForgeSoldier.cs");
   exec("~/server/scripts/players/SpaceOrc.cs");
   exec("~/server/scripts/players/Spacesuit.cs");
   exec("~/server/scripts/players/TorqueOrc.cs");
}

//-----------------------------------------------------------------------------
function loadDefaultMission()
{
   Canvas.setCursor("DefaultCursor");
   createServer( "SinglePlayer", expandFileName("~/data/missions/simple.mis") );

   %conn = new GameConnection(ServerConnection);
   RootGroup.add(ServerConnection);
   %conn.setConnectArgs($pref::Player::Name);
   %conn.setJoinPassword($Client::Password);
   %conn.connectLocal();
}

function loadMainMenu()
{
   // Startup the client with the Main menu...
   if (isObject( MainMenuGui ))
      Canvas.setContent( MainMenuGui );
   else if (isObject( UnifiedMainMenuGui ))
      Canvas.setContent( UnifiedMainMenuGui );
   Canvas.setCursor("DefaultCursor");
   
   // Attempt to load a set of datablocks for use in a player picker
   if (isFunction("loadPlayerPickerData"))
      loadPlayerPickerData();
}
