mapnavigationbehaviour.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. enum EMapNavigationType
  2. {
  3. BASIC = 0,
  4. COMPASS = 1,
  5. GPS = 2,
  6. ALL = 4
  7. }
  8. class MapNavigationBehaviour
  9. {
  10. const int RANDOM_DEVIATION_MIN = 4;
  11. const int RANDOM_DEVIATION_MAX = 15;
  12. static const int DISPLAY_GRID_POS_MAX_CHARS_COUNT = 3;
  13. static const int DISPLAY_ALT_MAX_CHARS_COUNT = 4;
  14. protected static const string GRID_SIZE_CFG_PATH = "CfgWorlds %1 Grid Zoom1 stepX";
  15. protected int m_RandomPositionDeviationX;
  16. protected int m_RandomPositionDeviationZ;
  17. protected EMapNavigationType m_NavigationType;
  18. protected PlayerBase m_Player;
  19. protected ref array<EntityAI> m_GPSInPossessionArr;
  20. protected ref array<EntityAI> m_CompassInPossessionArr;
  21. void MapNavigationBehaviour(PlayerBase pPlayer, EMapNavigationType pNavigationType = EMapNavigationType.BASIC)
  22. {
  23. m_Player = pPlayer;
  24. m_NavigationType = pNavigationType;
  25. m_GPSInPossessionArr = new array<EntityAI>();
  26. m_CompassInPossessionArr = new array<EntityAI>();
  27. }
  28. protected void SetNavigationType(EMapNavigationType pType)
  29. {
  30. m_NavigationType = m_NavigationType | pType;
  31. }
  32. protected void UnsetNavigationType(EMapNavigationType pType)
  33. {
  34. m_NavigationType = m_NavigationType & ~pType;
  35. }
  36. EMapNavigationType GetNavigationType()
  37. {
  38. return m_NavigationType;
  39. }
  40. void OnItemInPlayerPossession(EntityAI item)
  41. {
  42. if (item.IsInherited(ItemGPS))
  43. {
  44. if (m_GPSInPossessionArr.Find(item) == INDEX_NOT_FOUND)
  45. {
  46. m_GPSInPossessionArr.Insert(item);
  47. SetNavigationType(EMapNavigationType.GPS);
  48. }
  49. }
  50. if (item.IsInherited(ItemCompass))
  51. {
  52. if (m_CompassInPossessionArr.Find(item) == INDEX_NOT_FOUND)
  53. {
  54. m_CompassInPossessionArr.Insert(item);
  55. SetNavigationType(EMapNavigationType.COMPASS);
  56. }
  57. }
  58. }
  59. void OnItemNotInPlayerPossession(EntityAI item)
  60. {
  61. if (item.IsInherited(ItemGPS))
  62. {
  63. m_GPSInPossessionArr.RemoveItem(item);
  64. if (m_GPSInPossessionArr.Count() == 0)
  65. {
  66. UnsetNavigationType(EMapNavigationType.GPS);
  67. }
  68. }
  69. if (item.IsInherited(ItemCompass))
  70. {
  71. m_CompassInPossessionArr.RemoveItem(item);
  72. if (m_CompassInPossessionArr.Count() == 0)
  73. {
  74. UnsetNavigationType(EMapNavigationType.COMPASS);
  75. }
  76. }
  77. }
  78. void RandomizePosition()
  79. {
  80. m_RandomPositionDeviationX = RandomizedDeviation();
  81. m_RandomPositionDeviationZ = RandomizedDeviation();
  82. }
  83. protected float RandomizedDeviation()
  84. {
  85. Math.Randomize(GetWorldTime() + Math.RandomIntInclusive(2, 4096));
  86. if ((Math.RandomIntInclusive(0, 10) % 2) == 0)
  87. {
  88. return Math.RandomIntInclusive(-RANDOM_DEVIATION_MAX, -RANDOM_DEVIATION_MIN);
  89. }
  90. else
  91. {
  92. return Math.RandomIntInclusive(RANDOM_DEVIATION_MIN, RANDOM_DEVIATION_MAX);
  93. }
  94. }
  95. vector GetPositionRandomized()
  96. {
  97. vector realPosition = m_Player.GetPosition();
  98. vector randomizedPosition = Vector(realPosition[0] + m_RandomPositionDeviationX, realPosition[1], realPosition[2] + m_RandomPositionDeviationZ);
  99. return randomizedPosition;
  100. }
  101. vector GetPositionReal()
  102. {
  103. return m_Player.GetPosition();
  104. }
  105. static array<int> OrderedPositionNumbersFromGridCoords(EntityAI pEntity)
  106. {
  107. float gridSize = GetGame().ConfigGetFloat(string.Format(GRID_SIZE_CFG_PATH, GetGame().GetWorldName()));
  108. int gridX, gridZ;
  109. GetGame().GetWorld().GetGridCoords(pEntity.GetPosition(), gridSize, gridX, gridZ);
  110. gridX = Math.AbsInt(gridX);
  111. gridZ = Math.AbsInt(gridZ);
  112. array<int> positions = new array<int>();
  113. string gridXStr = gridX.ToStringLen(DISPLAY_GRID_POS_MAX_CHARS_COUNT);
  114. string gridZStr = gridZ.ToStringLen(DISPLAY_GRID_POS_MAX_CHARS_COUNT);
  115. int i = 0;
  116. int gridCoordNumber;
  117. for (i = 0; i < gridXStr.Length(); ++i)
  118. {
  119. gridCoordNumber = gridXStr.Get(i).ToInt();
  120. if (IsOutOfMap(pEntity))
  121. {
  122. gridCoordNumber = -1;
  123. }
  124. positions.Insert(gridCoordNumber);
  125. }
  126. for (i = 0; i < gridZStr.Length(); ++i)
  127. {
  128. gridCoordNumber = gridZStr.Get(i).ToInt();
  129. if (IsOutOfMap(pEntity))
  130. {
  131. gridCoordNumber = -1;
  132. }
  133. positions.Insert(gridCoordNumber);
  134. }
  135. return positions;
  136. }
  137. static array<int> OrderedAltitudeNumbersPosition(EntityAI pEntity)
  138. {
  139. array<int> altArray = new array<int>();
  140. float altF = pEntity.GetPosition()[1];
  141. int altI = Math.Round(altF);
  142. string altString = altI.ToStringLen(DISPLAY_ALT_MAX_CHARS_COUNT);
  143. for (int i = 0; i < altString.Length(); ++i)
  144. {
  145. altArray.Insert(altString.Get(i).ToInt());
  146. }
  147. return altArray;
  148. }
  149. static bool IsOutOfMap(EntityAI pEntity)
  150. {
  151. vector worldPos = pEntity.GetPosition();
  152. if (worldPos[0] < 0 || worldPos[0] > GetGame().GetWorld().GetWorldSize() || worldPos[2] < 0 || worldPos[2] > GetGame().GetWorld().GetWorldSize())
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. }