building.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. typedef Param1<int> DoorStartParams;
  2. typedef Param2<int, bool> DoorFinishParams;
  3. typedef Param1<int> DoorLockParams;
  4. class Building extends EntityAI
  5. {
  6. proto native int GetLaddersCount();
  7. proto native vector GetLadderPosTop(int ladderIndex);
  8. proto native vector GetLadderPosBottom(int ladderIndex);
  9. //! Gets the index of the door based on the view geometry component index
  10. proto native int GetDoorIndex(int componentIndex);
  11. //! Returns the number of the doors in the building
  12. proto native int GetDoorCount();
  13. //! When the door is requested to be fully open (animation wanted phase is greater than 0.5)
  14. proto native bool IsDoorOpen(int index);
  15. //! When the wanted phase is at the open phase target (1.0)
  16. proto native bool IsDoorOpening(int index);
  17. //! When the wanted phase is at the ajar phase target (0.2)
  18. proto native bool IsDoorOpeningAjar(int index);
  19. //! When the wanted phase is at the close phase target (0.0)
  20. proto native bool IsDoorClosing(int index);
  21. //! When the phase is at the open phase target (1.0)
  22. proto native bool IsDoorOpened(int index);
  23. //! When the phase is at the ajar phase target (0.2)
  24. proto native bool IsDoorOpenedAjar(int index);
  25. //! When the phase is at the close phase target (0.0)
  26. proto native bool IsDoorClosed(int index);
  27. //! When the door is locked
  28. proto native bool IsDoorLocked(int index);
  29. //! Plays the appropriate sound at the door, based on animation phase and state
  30. proto native void PlayDoorSound(int index);
  31. //! Attempts to open the door
  32. proto native void OpenDoor(int index);
  33. //! Attempts to close the door
  34. proto native void CloseDoor(int index);
  35. //! Locks the door if not already locked, resets the door health. 'force = true' will close the door if open
  36. proto native void LockDoor(int index, bool force = false);
  37. //! Unlocks the door if locked
  38. proto native void UnlockDoor(int index);
  39. //! Position in world space for where the door sounds are played from
  40. proto native vector GetDoorSoundPos(int index);
  41. //! Audible distance for the door sound
  42. proto native float GetDoorSoundDistance(int index);
  43. //! Requires special build as logging is disabled even on internal builds due to memory usage. Used for checking of possible causes of navmesh desync
  44. proto native void OutputDoorLog();
  45. //! Gets the nearest door based on the sound position (quick and dirty function for debugging)
  46. int GetNearestDoorBySoundPos(vector position)
  47. {
  48. float smallestDist = float.MAX;
  49. int nearestDoor = -1;
  50. int count = GetDoorCount();
  51. for (int i = 0; i < count; i++)
  52. {
  53. float dist = vector.DistanceSq(GetDoorSoundPos(i), position);
  54. if (dist < smallestDist)
  55. {
  56. nearestDoor = i;
  57. smallestDist = dist;
  58. }
  59. }
  60. return nearestDoor;
  61. }
  62. //! Event for when the door starts opening
  63. void OnDoorOpenStart(DoorStartParams params)
  64. {
  65. }
  66. //! Event for when the door finishes opening
  67. void OnDoorOpenFinish(DoorFinishParams params)
  68. {
  69. }
  70. //! Event for when the door starts opening ajarred (usually after unlock)
  71. void OnDoorOpenAjarStart(DoorStartParams params)
  72. {
  73. }
  74. //! Event for when the door finishes opening ajarred (usually after unlock)
  75. void OnDoorOpenAjarFinish(DoorFinishParams params)
  76. {
  77. }
  78. //! Event for when the door starts closing
  79. void OnDoorCloseStart(DoorStartParams params)
  80. {
  81. }
  82. //! Event for when the door finishes closing
  83. void OnDoorCloseFinish(DoorFinishParams params)
  84. {
  85. }
  86. //! Event for when the door is locked
  87. void OnDoorLocked(DoorLockParams params)
  88. {
  89. }
  90. //! Event for when the door is unlocked
  91. void OnDoorUnlocked(DoorLockParams params)
  92. {
  93. }
  94. bool CanDoorBeOpened(int doorIndex, bool checkIfLocked = false)
  95. {
  96. if (IsDoorOpen(doorIndex))
  97. return false;
  98. if (checkIfLocked)
  99. {
  100. if (IsDoorLocked(doorIndex))
  101. return false;
  102. }
  103. else
  104. {
  105. if (!IsDoorLocked(doorIndex))
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool CanDoorBeClosed(int doorIndex)
  111. {
  112. return IsDoorOpen(doorIndex);
  113. }
  114. //! Check if the door is closed and if the door is unlocked
  115. bool CanDoorBeLocked(int doorIndex)
  116. {
  117. return (!IsDoorOpen(doorIndex) && !IsDoorLocked(doorIndex));
  118. }
  119. override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
  120. {
  121. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_OUTPUT_LOG, "Output Door Log", FadeColors.LIGHT_GREY));
  122. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, " --- ", FadeColors.LIGHT_GREY));
  123. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_PLAY_DOOR_SOUND, "Play Door Sound", FadeColors.LIGHT_GREY));
  124. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_OPEN_DOOR, "Open Door", FadeColors.LIGHT_GREY));
  125. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_CLOSE_DOOR, "Close Door", FadeColors.LIGHT_GREY));
  126. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_LOCK_DOOR, "Lock Door", FadeColors.LIGHT_GREY));
  127. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_UNLOCK_DOOR, "Unlock Door", FadeColors.LIGHT_GREY));
  128. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, " --- ", FadeColors.LIGHT_GREY));
  129. super.GetDebugActions(outputList);
  130. }
  131. override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
  132. {
  133. if (super.OnAction(action_id, player, ctx))
  134. return true;
  135. switch (action_id)
  136. {
  137. case EActions.BUILDING_PLAY_DOOR_SOUND:
  138. PlayDoorSound(GetNearestDoorBySoundPos(player.GetPosition()));
  139. return true;
  140. }
  141. if (!GetGame().IsServer())
  142. return false;
  143. switch (action_id)
  144. {
  145. case EActions.BUILDING_OUTPUT_LOG:
  146. OutputDoorLog();
  147. return true;
  148. case EActions.BUILDING_OPEN_DOOR:
  149. OpenDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  150. return true;
  151. case EActions.BUILDING_CLOSE_DOOR:
  152. CloseDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  153. return true;
  154. case EActions.BUILDING_LOCK_DOOR:
  155. LockDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  156. return true;
  157. case EActions.BUILDING_UNLOCK_DOOR:
  158. UnlockDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  159. return true;
  160. }
  161. return false;
  162. }
  163. override bool IsBuilding()
  164. {
  165. return true;
  166. }
  167. override bool CanObstruct()
  168. {
  169. return true;
  170. }
  171. override bool IsHealthVisible()
  172. {
  173. return false;
  174. }
  175. ref TIntArray m_InteractActions;
  176. void Building()
  177. {
  178. m_InteractActions = new TIntArray;
  179. g_Game.ConfigGetIntArray("cfgVehicles " +GetType() + " InteractActions", m_InteractActions);
  180. }
  181. override bool IsInventoryVisible()
  182. {
  183. return false;
  184. }
  185. override int GetMeleeTargetType()
  186. {
  187. return EMeleeTargetType.NONALIGNABLE;
  188. }
  189. };