cctwatersurface.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. class CCTWaterSurface : CCTBase
  2. {
  3. protected const int HEIGHT_DIFF_LIMIT_METERS = 1.0;
  4. protected float m_MaximalActionDistanceSq;
  5. protected string m_SurfaceType; //!DEPRECATED
  6. protected ref array<string> m_AllowedSurfaceList;
  7. void CCTWaterSurface(float maximal_target_distance = UAMaxDistances.DEFAULT, string surfaceType = "")
  8. {
  9. m_MaximalActionDistanceSq = maximal_target_distance * maximal_target_distance;
  10. m_SurfaceType = surfaceType;
  11. m_AllowedSurfaceList = new array<string>();
  12. surfaceType.Split("|", m_AllowedSurfaceList);
  13. }
  14. override bool Can(PlayerBase player, ActionTarget target)
  15. {
  16. if (!target || (target && target.GetObject()))
  17. return false;
  18. //! use hit position from ActionTarget otherwise player's position
  19. vector hitPosition = target.GetCursorHitPos();
  20. string surfaceType;
  21. float waterLevel = player.GetCurrentWaterLevel();
  22. g_Game.SurfaceGetType3D(hitPosition[0], hitPosition[1] + waterLevel, hitPosition[2], surfaceType);
  23. if (waterLevel > 0.0)
  24. return Surface.AllowedWaterSurface(hitPosition[1] + waterLevel, surfaceType, m_AllowedSurfaceList);
  25. float surfaceHeight = g_Game.SurfaceY(hitPosition[0], hitPosition[2]);
  26. //! special handling for sea
  27. if (!surfaceType)
  28. {
  29. surfaceHeight = hitPosition[1];
  30. }
  31. float heightDiff = Math.AbsFloat(hitPosition[1] - surfaceHeight);
  32. if (surfaceType != "" && heightDiff > HEIGHT_DIFF_LIMIT_METERS)
  33. return false;
  34. float distSq = vector.DistanceSq(player.GetPosition(), hitPosition);
  35. if (distSq > m_MaximalActionDistanceSq)
  36. return false;
  37. return Surface.AllowedWaterSurface(hitPosition[1], surfaceType, m_AllowedSurfaceList);
  38. }
  39. override bool CanContinue(PlayerBase player, ActionTarget target)
  40. {
  41. return true;
  42. }
  43. }
  44. class CCTWaterSurfaceEx : CCTBase
  45. {
  46. protected float m_MaximalActionDistanceSq;
  47. protected int m_AllowedLiquidSource;
  48. protected int m_LiquidType = LIQUID_NONE;
  49. void CCTWaterSurfaceEx(float maximal_target_distance, int allowedLiquidSource)
  50. {
  51. m_MaximalActionDistanceSq = maximal_target_distance * maximal_target_distance;
  52. m_AllowedLiquidSource = allowedLiquidSource;
  53. }
  54. override bool Can(PlayerBase player, ActionTarget target)
  55. {
  56. if (!target)
  57. return false;
  58. vector hitPosition = target.GetCursorHitPos();
  59. float distSq = vector.DistanceSq(player.GetPosition(), hitPosition);
  60. if (distSq > m_MaximalActionDistanceSq)
  61. return false;
  62. int liquidType = GetSurfaceLiquidType(target);
  63. string surfaceName = target.GetSurfaceName();
  64. return CheckLiquidSource(hitPosition, surfaceName, liquidType, m_AllowedLiquidSource);
  65. }
  66. bool CheckLiquidSource(vector hitPos, string surfaceName, int liquidType, int allowedWaterSourceMask)
  67. {
  68. bool success = false;
  69. if (surfaceName == "" && hitPos != vector.Zero)
  70. {
  71. //! Only return true if surface name is empty and
  72. bool isSea = g_Game.SurfaceIsSea(hitPos[0], hitPos[2]);
  73. if (allowedWaterSourceMask & LIQUID_SALTWATER && isSea)
  74. {
  75. success = hitPos[1] <= (g_Game.SurfaceGetSeaLevel() + 0.25);
  76. }
  77. }
  78. else
  79. {
  80. success = allowedWaterSourceMask & liquidType;
  81. }
  82. return success;
  83. }
  84. int GetSurfaceLiquidType(ActionTarget target)
  85. {
  86. m_LiquidType = LIQUID_NONE;
  87. //! Check target surface liquid source first
  88. if (target.GetSurfaceLiquidType() != LIQUID_NONE)
  89. {
  90. m_LiquidType = target.GetSurfaceLiquidType();
  91. }
  92. //! Check target object for liquid source next
  93. Object targetObject = target.GetObject();
  94. if (m_LiquidType == LIQUID_NONE && targetObject)
  95. {
  96. m_LiquidType = targetObject.GetLiquidSourceType();
  97. }
  98. return m_LiquidType;
  99. }
  100. override bool CanContinue(PlayerBase player, ActionTarget target)
  101. {
  102. return true;
  103. }
  104. int GetLiquidType()
  105. {
  106. return m_LiquidType;
  107. }
  108. }