//-------------------------------------------------------------------
//
//  This code is copyright 2001 by G5 Software.
//  Any unauthorized usage, either in part or in whole of this code
//  is strictly prohibited. Violators WILL be prosecuted to the
//  maximum extent allowed by law.
//
//-------------------------------------------------------------------

class CGameMachine extends CStrings
{
  color ConsoleStartColor = color(0.3, 0.3, 0.3, 1.0);
  color ConsoleLoadColor  = color(0.3, 0.3, 0.3, 0.0);
  color ConsoleGameColor  = color(0.3, 0.3, 0.3, 0.75);

  void CGameMachine()
  {
    // load text rendering component
    LoadTextRender("CFonts");

    // SetConsoleState(Enabled, ScreenAreaPersent, BackColor)
    SetConsoleState(true, 100, ConsoleStartColor);

    // InitMainCamera(CameraClass) camera automaticaly activated
    InitMainCamera("CMainGameCamera");

    // Create music controller
    CreateMusicController("CMusicController");

    // InitMainListener(???)
    InitMainListener();

    // Load game menu
    CreateGameMenu("CGameMenu");

    // Load mission menu
    CreateMissionMenu("CMissionMenu");

    // Hide console after everything's loaded
    SetConsoleState(false, 50, ConsoleGameColor);

    // Disable all controls just for a case
    EnableControl("", false);

    // Enable game menu control
    EnableControl(SOID_GameMenu, true);

    // Start menu music playing
    Core_SendEventTo(SOID_MusicController, "PlayMenuMusic");
  }

  bool m_bMissionLoaded = false;

  void OnShowMissionMenu()
  {
    if (!m_bMissionLoaded)
      return;

    Core_SendEventTo(SOID_MissionController, "OnShowMissionMenu");
  }

  void OnForceMissionMenu()
  {
    if (!m_bMissionLoaded)
      return;

    Core_SendEventTo(SOID_MissionController, "OnForceMissionMenu");
  }

  void OnShowMissionStatus()
  {
    if (!m_bMissionLoaded)
      return;

    Core_SendEventTo(SOID_MissionController, "OnShowMissionStatus");
  }

  void OnShowMissionHelpScreen()
  {
    if (!m_bMissionLoaded)
      return;

    Core_SendEventTo(SOID_MissionController, "OnShowMissionHelpScreen");
  }

  void OnQuitGame()
  {
    QuitGame();
  }

  // loaded mission name,
  // saved for 'restart' button
  string m_strCurrentMission;

  // loaded mission number,
  // saved for open new mission functionality
  int    m_nCurrentMissionNumber;

  void OnLoadMission(
      string _MissionName,
      int    _MissionNumber
    )
  {
    m_strCurrentMission = _MissionName;
    m_nCurrentMissionNumber = _MissionNumber;

    // configure game menu to show load screen while loading
    Core_CallFunction(
      SOID_GameMenu,
      "GoToSubMenu",
      "GameMenu_LoadScreen"
    );
    // turn off mouse cursor while loading
    Core_CallFunction(
      SOID_GameMenu,
      "HideCursor"
    );

    SetConsoleState(false, 100, ConsoleLoadColor);

    EnableControl("", false); // Disable all
    ActivateCamera("", false); // Disable all

    EnableControl(SOID_GameMenu, true);
    ActivateCamera(SOID_MainCamera, true);

    LoadMission(_MissionName);

    // turn on mouse cursor for menu
    Core_CallFunction(
        SOID_GameMenu,
        "ShowCursor"
      );

    EnableControl("", false); // Disable all
    ActivateCamera("", false); // Disable all

    SetConsoleState(false, 50, ConsoleGameColor);

    Core_PostEventTo(
        SOID_GameController,
        "SwitchControlToHelicopter"
      );

    m_bMissionLoaded = true;
  }

  void SwitchControlToHelicopter()
  {
    ActivateObject("Helicopter", true); // enable for helicopter
  }

  // which missions open which
  array m_MissionOpens =
    array(
      array(1, 2),
      array(3, 5),
      array(4, 6),
      array(8),
      array(10),
      array(7),
      array(11),
      array(9),
      array(14),
      array(),
      array(13),
      array(12),
      array(),
      array(),
      array()
    );

  void OnSetMissionResult(
           array  _objectives,
           array  _statuses,
           array  _bonus_objectives,
           array  _bonus_statuses,
           string _statistics
         )
  {
    // analyze whether mission is completed and enable the next mission
    // button on the play! menu

    // mission is complete if all the main objectives are complete
    bool bMissionComplete = true;
    for (int i = 0; i < _statuses.size(); i = i + 1)
    {
      bMissionComplete = bMissionComplete && (_statuses[i] == str_ObjectiveComplete);
    }

    //
    // prepare mission results screen
    //

    Core_SendEventTo(SOID_GameMenu,
       "BaseMenu_SetMissionStatus",
       _objectives,
       _statuses,
       _bonus_objectives,
       _bonus_statuses,
       _statistics,
       false,           // in-game flag, notifies that mission is over
                        // and menu should display 'Failed' but not 'In progress'
                        // for incomplete objectives
       bMissionComplete // flag showing whether mission is complete
    );

    if (bMissionComplete)
    {
      int cur_state = GetMissionState(m_nCurrentMissionNumber);

      // mission medal

      int level = Core_CallFunction(
                    SOID_GameMenu,
                    "GetDifficultyLevel"
                  );

      if (cur_state < level + 2)
        cur_state = level + 2;

      SetMissionState(m_nCurrentMissionNumber, cur_state);

      // open missions

      array Missions2Open = m_MissionOpens[m_nCurrentMissionNumber];

      for (int i = 0; i < Missions2Open.size(); i = i + 1)
      {
        int state = GetMissionState(Missions2Open[i]);
        if (state == 0)
          state = 1;
        SetMissionState(Missions2Open[i], state);
      }
    }
  }

  void OnQuitMission()
  {
    // Start menu music playing
    Core_SendEventTo(SOID_MusicController, "PlayMenuMusic");

    EnableControl("", false); // Disable all
    ActivateCamera("", false); // Disable all

    m_bMissionLoaded = false;

    UnloadMission();

    ActivateCamera(SOID_MainCamera, true);

    Core_CallFunction(SOID_GameMenu,
       "GoToSubMenu",
       "BaseMenu_ShowMissionStatus");

    EnableControl(SOID_GameMenu, true);
  }

  void OnRestartMission()
  {
    // Start menu music playing
    Core_SendEventTo(SOID_MusicController, "PlayMenuMusic");

    EnableControl("", false); // Disable all
    ActivateCamera("", false); // Disable all

    m_bMissionLoaded = false;

    UnloadMission();

    ActivateCamera(SOID_MainCamera, true);

    // load mission
    Core_SendEventTo(SOID_GameController, "OnLoadMission", m_strCurrentMission, m_nCurrentMissionNumber);
  }
}

class CMainGameCamera
{
  float FOV        = 1.0;

  float RectLeft   = 0;
  float RectTop    = 0;
  float RectRight  = 1.0;
  float RectBottom = 1.0;

  float ZMin       = 0.0;
  float ZMax       = 1.0;
  float ZNear      = 1.0;
  float ZFar       = 1000.0;

  float Priority   = 0;
}


