building.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, AJAR animation optional
  38. proto native void UnlockDoor(int index, bool animate = true);
  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. /**
  120. \brief Which door is compatible with which key (door idx supplied). Bitwise.
  121. @param doorIdx
  122. @return bitwise value of all compatible locks
  123. \note you can combine the bit values like so:
  124. \note return (1 << EBuildingLockType.LOCKPICK) | (1 << EBuildingLockType.SHIP_CONTAINER_1);
  125. \note you can also this for each individual door idx
  126. */
  127. int GetLockCompatibilityType(int doorIdx)
  128. {
  129. return 1 << EBuildingLockType.LOCKPICK; //all doors are lockpickable by default
  130. }
  131. override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
  132. {
  133. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_OUTPUT_LOG, "Output Door Log", FadeColors.LIGHT_GREY));
  134. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, " --- ", FadeColors.LIGHT_GREY));
  135. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_PLAY_DOOR_SOUND, "Play Door Sound", FadeColors.LIGHT_GREY));
  136. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_OPEN_DOOR, "Open Door", FadeColors.LIGHT_GREY));
  137. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_CLOSE_DOOR, "Close Door", FadeColors.LIGHT_GREY));
  138. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_LOCK_DOOR, "Lock Door", FadeColors.LIGHT_GREY));
  139. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.BUILDING_UNLOCK_DOOR, "Unlock Door", FadeColors.LIGHT_GREY));
  140. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, " --- ", FadeColors.LIGHT_GREY));
  141. super.GetDebugActions(outputList);
  142. }
  143. override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
  144. {
  145. if (super.OnAction(action_id, player, ctx))
  146. return true;
  147. switch (action_id)
  148. {
  149. case EActions.BUILDING_PLAY_DOOR_SOUND:
  150. PlayDoorSound(GetNearestDoorBySoundPos(player.GetPosition()));
  151. return true;
  152. }
  153. if (!GetGame().IsServer())
  154. return false;
  155. switch (action_id)
  156. {
  157. case EActions.BUILDING_OUTPUT_LOG:
  158. OutputDoorLog();
  159. return true;
  160. case EActions.BUILDING_OPEN_DOOR:
  161. OpenDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  162. return true;
  163. case EActions.BUILDING_CLOSE_DOOR:
  164. CloseDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  165. return true;
  166. case EActions.BUILDING_LOCK_DOOR:
  167. LockDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  168. return true;
  169. case EActions.BUILDING_UNLOCK_DOOR:
  170. UnlockDoor(GetNearestDoorBySoundPos(player.GetPosition()));
  171. return true;
  172. }
  173. return false;
  174. }
  175. override bool IsBuilding()
  176. {
  177. return true;
  178. }
  179. override bool CanObstruct()
  180. {
  181. return true;
  182. }
  183. override bool IsHealthVisible()
  184. {
  185. return false;
  186. }
  187. ref TIntArray m_InteractActions;
  188. void Building()
  189. {
  190. m_InteractActions = new TIntArray;
  191. g_Game.ConfigGetIntArray("cfgVehicles " +GetType() + " InteractActions", m_InteractActions);
  192. }
  193. override bool IsInventoryVisible()
  194. {
  195. return false;
  196. }
  197. override int GetMeleeTargetType()
  198. {
  199. return EMeleeTargetType.NONALIGNABLE;
  200. }
  201. };