//-------------------------------------------------------------------
//
//  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 CCockpit extends CStrings
{
  array StringTable =
    array(
      str_NAVPOINT_SELECTED  // from CStrings
    );

  string strSkinFile          = "Common/Cockpit.skin";
  string strWeaponChangeSound = "CWeaponChangeSound";
  string strLockingSound      = "CTargetLockingSound";
  string strLockedSound       = "CTargetLockedSound";
  string strTargetSwitchSound = "CTargetSwitchSound";
  string strObjCompleteSound  = "CObjectiveCompleteSound";

  string strLockWeaponName    = str_VIKHR; // from CStrings
  string strNoLockWeaponName  = str_S8;    // from CStrings
  string strKph               = str_Kph;   // from CStrings

  array m_UnitClassificators  = array(
      "AntiAir",
      "Tank",
      "GunTower",
      "Tent",
      "AJeep",
      "Refueller",
      "Jeep",
      "Truck",
      "Flak"
    );

  array m_UnitNames =
    array(
      str_AntiAir,
      str_Tank,
      str_GunTower,
      str_Tent,
      str_ArmJeep,
      str_Refueller,
      str_Jeep,
      str_Truck,
      str_Flak
    );

  void CCockpit()
  {
    // navigation bar
    CreateNavBar("MainNavBar", true);

    //
    //  Short range radar
    //

    CreateRadar(
      "ShortRangeRadar",
      true,
      1500,
      0.01,                                     // x
      1.0 - (0.01 + 0.08 * 2) / 0.75,           // y
      0.08                                      // radius
    );

    SetRadarBackgroundColor("ShortRangeRadar", color(0.0, 0.7, 0.0, 0.6));

    SetRadarObjectMarkerColor(
      "ShortRangeRadar",
      color(0.0, 0.8, 0.8),  // friendly objects
      color(1.0, 0.0, 0.0)   // enemy objects
    );

    SetRadarEnemyClassificator(
      "ShortRangeRadar",
      "German"
    );

    SetRadarTargetMarkerColor(
      "ShortRangeRadar",
      color(1.0, 1.0, 0.0)
    );

    //
    //  Long range map
    //

    CreateMap("LongRangeMap",
      true,
      4000,
      2000,
      16,
      4000.0,
      0.01,         // x
      0.01 / 0.75,  // y
      0.2           // side size
    );

    SetMapObjectMarkerColor(
      "LongRangeMap",
      color(0.0, 0.8, 0.8), // friendly objects
      color(1.0, 0.0, 0.0)  // enemy objects
    );

    SetMapEnemyClassificator(
      "LongRangeMap",
      "German"
    );

    SetMapTargetMarkerColor(
      "LongRangeMap",
      color(1.0, 1.0, 0.0)
    );

    SetMapHeliMarkerColor(
      "LongRangeMap",
      color(0.0, 1.0, 0.0)
    );

    //
    //  Target pointer
    //

    CreateTargetPointer("TargetPointer", true);
    SetTargetPointerEnemyClassificator("TargetPointer", "German");

    SetTargetPointerColors(
      "TargetPointer",
      color(0.0, 0.7, 0.0),
      color(0.8, 0.0, 0.0)
    );

    SetTargetPointerLockedColors(
      "TargetPointer",
      color(0.0, 1.0, 0.0),
      color(1.0, 0.2, 0.2)
    );
  }

  void InitializeCockpit()
  {
    array points =
      Core_CallFunction(SOID_MissionController, "GetNavPoints");

    array descs =
      Core_CallFunction(SOID_MissionController, "GetNavPointsDescriptions");

    SetNavPoints("MainNavBar", points, descs);

    string skin_file =
      Core_CallFunction(SOID_MissionController, "GetMapSkinFileName");
    LoadMapSkinFile(skin_file);
  }

  void OnDisplayMessage(string _message, color _color)
  {
    DisplayMessage(_message, _color);
  }

  void OnDisplayMessage(string _message, color _color, int _time, int _line)
  {
    DisplayMessage(_message, _color, _time, _line);
  }

  void OnToggleCrosshair(bool _enable)
  {
    ToggleCrosshair(_enable);
  }

  void OnToggleTargetDesc(bool _enable)
  {
    ToggleTargetDesc(_enable);
  }
}


class CDebugCameraCockpit extends CCockpit
{
  // initialize cocpit on mission loaded
  void OnMissionLoaded()
  {
    InitializeCockpit();
    MissionEditorInit();
  }

  // Mission Editor functions
  void CreateObject(
    string _ObjectID,
    string _ScriptClass,
    string _ComponentID,
    matrix _Matrix)
  {
    //Core_LogMessage("CreateObject() called");

    // call script user's function
    MissionEditorCreateObject(
      _ObjectID,
      _ScriptClass,
      _ComponentID,
      _Matrix);
  }

  void UnitComeHere(string _ObjectID)
  {
    //Core_LogMessage("UnitComeHere");

    // call script user's function
    MissionEditorUnitComeHere(_ObjectID);
  }

  void ChangeID(string _OldID, string _NewID)
  {
    // call script user's function
    MissionEditorChangeID(_OldID, _NewID);
  }

  void SetBehaviorScript(string _ID, string _ScriptClass)
  {
    // call script user's function
    MissionEditorSetBehaviorScript(_ID, _ScriptClass);
  }

  void SetCustomParameter(string _ObjectID, string _CustomParameter)
  {
    // call script user's function
    MissionEditorSetCustomParameter(_ObjectID, _CustomParameter);
  }
}




class CCockpitTargetingCameraLink extends CInteriorMeshObjectPrefix
{
  final string g_DefaultTarget = "Helicopter";

  void CCockpitTargetingCameraLink()
  {
    SetCameraCollisionRadius(10.0);
    SelectTargetObject(g_DefaultTarget);
  }

  float m_OnLostDistance = 20.0;
  float m_OnLostRotSpeed = 0.0;

  void ShowTargetObject(
      string _TargetId,
      float  _Distance,
      float  _RotSpeed
    )
  {
    if ("" == _TargetId)
      _TargetId = g_DefaultTarget;

    SetCameraPosition(vector(0.0, 0.0, 2.0), vector(0.0, 0.0, 2.0), _Distance);
    SetCameraRotation(rand(Math_PI), _RotSpeed);

    m_OnLostDistance = _Distance;
    m_OnLostRotSpeed = _RotSpeed;

    SetTargetObjectId(_TargetId);
  }

  void ShowTargetPoint(
      vector _Point,
      float  _Distance,
      float  _RotSpeed,
      bool   _PutOnTer
    )
  {
    SetCameraPosition(vector(0.0, 0.0, 50.0), vector(0.0, 0.0, 10.0), _Distance);
    SetCameraRotation(rand(Math_PI), _RotSpeed);

    m_OnLostDistance = _Distance;
    m_OnLostRotSpeed = _RotSpeed;

    SetTargetPoint(_Point, _PutOnTer);
  }


  void SelectTargetObject(
      string _TargetId
    )
  {
    ShowTargetObject(_TargetId, 20.0, 0.0);

    m_OnLostDistance = 50.0;
    m_OnLostRotSpeed = Math_PI * 0.05;
  }

  void SelectNavigationPoint(
      vector _Point
    )
  {
    ShowTargetPoint(_Point, 200.0, Math_PI * 0.05, true);
  }


  string m_MissileTargetId = "";

  void SelectMissileAsTarget(
      int    _BulletId,
      string _TargetId
    )
  {
    LinkCameraToTarget(vector(10.0, 0.0, 0.0), vector(1000.0, 0.0, 0.0));

    m_MissileTargetId = _TargetId;
    SetTargetBulletId(_BulletId);
  }

  void OnLostTargetObject(
      string _LinkMode,
      string _TargetId,
      matrix _Position
    )
  {
    if ((TLM_Object == _LinkMode) &&
        IsInteriorObjectId(_TargetId))
    {
      SelectTargetObject(g_DefaultTarget);
      return;
    }

    SetCameraPosition(vector(0.0, 0.0, 10.0), vector(0.0, 0.0, 5.0), m_OnLostDistance);
    SetCameraRotation(rand(Math_PI), m_OnLostRotSpeed);

    if (TLM_Object == _LinkMode)
    {
      SetTargetObjectId(ConvertIdToInteriorId(_TargetId));
    }
    else
    if (TLM_Bullet == _LinkMode)
    {
      if ("" != m_MissileTargetId)
        SelectTargetObject(m_MissileTargetId);
      else
        SetTargetPoint(Core_GetMatrixOrigin(_Position), true);
    }
  }
}

class CCockpitTargetingCamera
{
  final float hor_size = 0.2;
  final float m_ScreenRectLeft   = 1.0 - 0.01 - hor_size;
  final float m_ScreenRectTop    = 0.01 / 0.75;
  final float m_ScreenRectRight  = 1.0 - 0.01;
  final float m_ScreenRectBottom = (0.01 + hor_size) / 0.75;

  float FOV        = 1.0;

  float RectLeft   = m_ScreenRectLeft;
  float RectTop    = m_ScreenRectTop;
  float RectRight  = m_ScreenRectRight;
  float RectBottom = m_ScreenRectBottom;

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

  float Priority   = 1;

  void CCockpitTargetingCamera()
  {
    Core_AddClassificator(CLASSIFICATOR_LOW_DETAIL_CAMERA);
  }

  void SetTargetingCameraViewMode(
      string _ViewMode
    )
  {
    if ("FullScreen" == _ViewMode)
    {
      SetRenderRectangle(0.0, 0.0, 1.0, 1.0);
      Core_RemoveClassificator(CLASSIFICATOR_LOW_DETAIL_CAMERA);
    }
    else
    {
      SetRenderRectangle(m_ScreenRectLeft, m_ScreenRectTop,
                         m_ScreenRectRight, m_ScreenRectBottom);
      Core_AddClassificator(CLASSIFICATOR_LOW_DETAIL_CAMERA);
    }
  }
}

class CCockpitTargetingScreen
{
  void CCockpitTargetingScreen()
  {
    CreateComponent("Camera", "Camera", "CCockpitTargetingCamera");
    SetCompoundObjectPositionable("Camera");

    CreateComponent("Control", "ShowObjectCameraControl", "CCockpitTargetingCameraLink");
    SetComponentSlaveObject("Control", "Camera");

    Core_AddClassificator(CLASSIFICATOR_NOTVISIBLEONRADAR);
  }
}




class CHelicopterCockpit extends CCockpit
{
  void CHelicopterCockpit()
  {
    // altimeter
    CreateAltimeter("MainAltimeter", true);

    // damage, etc. bar
    CreateSelfBar(
      "MainSelfBar",
      true,
      0.02 + 0.16,
      1.0 - (0.01 + 0.16) / 0.75,
      0.017,
      0.16 / 0.75
    );

    ShowDamage("MainSelfBar", 100);

    // weapon bar
    CreateWeaponBar(
      "WeaponBar",
      true,
      0.01,                               // x
      1.0 - (0.16 + 0.01) / 0.75 - 0.05,  // y
      0.10,                               // width
      0.05                                // height
    );
  }

  // initialize cocpit on mission loaded
  void OnMissionLoaded()
  {
    InitializeCockpit();

    Core_CallFunction(
        SOID_MissionController,
        "CreateComponent",
        "CockpitTargetingScreen",
        "GameObject",
        "CCockpitTargetingScreen"
      );
  }

  void OnHitpointsWasChanged(
      float _HPPercent
    )
  {
    ShowDamage("MainSelfBar", int(100.0 * _HPPercent));
  }

  void OnEnableControl(
      bool _Enable
    )
  {
    UpdateTargetingScreenState();
  }

  void OnEnableTargetScreen(
      bool _Enable
    )
  {
    UpdateTargetingScreenState();
  }

  bool m_ShowTargetScreen = true;
  void SetCocpitRenderMode(
      string _RenderMode
    )
  {
    SetRenderMode(_RenderMode);
    if (CRM_Cocpit == _RenderMode)
      m_ShowTargetScreen = true;
    else
      m_ShowTargetScreen = false;

    UpdateTargetingScreenState();
  }

  bool m_FullScreenMode = false;

  void OnSetCameraView(
      int _ViewType
    )
  {
    if (CVT_DefaultView == _ViewType)
      m_FullScreenMode = false;
    else
    if (CVT_NextView == _ViewType)
      m_FullScreenMode = false;
    else
    if (CVT_TargetView == _ViewType)
      m_FullScreenMode = !m_FullScreenMode;


    string ScreenMode = "CockipScreen";
    if (m_FullScreenMode)
      ScreenMode = "FullScreen";

    Core_SendEventTo(
        "CockpitTargetingScreen",
        "SetTargetingCameraViewMode",
        ScreenMode
      );

    Core_CallFunction(
        SOID_GameController,
        "ActivateCamera",
        "Helicopter",
        !m_FullScreenMode
      );

    UpdateTargetingScreenState();
  }

  void UpdateTargetingScreenState()
  {
    Core_CallFunction(
        SOID_GameController,
        "ActivateCamera",
        "CockpitTargetingScreen",
        (IsControlEnabled() &&
         IsTargetScreenEnabled() &&
         m_ShowTargetScreen) ||
        m_FullScreenMode
      );
  }

  void OnTargetSelected(
      string _TargetId
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "SelectTargetObject",
        _TargetId
      );
  }

  void OnNavPointSelected(
      vector _NavPoint
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "SelectNavigationPoint",
        _NavPoint
      );
  }

  void OnWeaponFire(
      int    _BulletId,
      string _TargetId
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "SelectMissileAsTarget",
        _BulletId,
        _TargetId
      );
  }

  void ShowEventObject(
      string _ObjectId,
      float  _Distance
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "ShowTargetObject",
        _ObjectId,
        _Distance,
        Math_PI * 0.05
      );
  }

  void ShowEventPoint(
      vector _Point,
      float  _Distance
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "ShowTargetPoint",
        _Point,
        _Distance,
        Math_PI * 0.05,
        true
      );
  }

  void ShowEventPoint(
      vector _Point,
      float  _Distance,
      bool   _PutOnTer
    )
  {
    Core_SendEventTo(
        "CockpitTargetingScreen",
        "ShowTargetPoint",
        _Point,
        _Distance,
        Math_PI * 0.05,
        _PutOnTer
      );
  }

  void OnInitiallyEnableTargetScreen(
      bool _enable
    )
  {
    EnableTargetScreen(_enable);
  }

  bool OnIsTargetScreenEnabled()
  {
    return IsTargetScreenEnabled();
  }
}


