A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.7KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using RobocraftX.Character;
  4. using RobocraftX.Character.Movement;
  5. using RobocraftX.Common.Players;
  6. using RobocraftX.Common.Input;
  7. using RobocraftX.Physics;
  8. using Svelto.ECS;
  9. using Unity.Mathematics;
  10. using Unity.Physics;
  11. using GamecraftModdingAPI.Engines;
  12. using RobocraftX.Blocks.Ghost;
  13. using RobocraftX.Character.Camera;
  14. using RobocraftX.Character.Factories;
  15. namespace GamecraftModdingAPI.Players
  16. {
  17. internal class PlayerEngine : IApiEngine
  18. {
  19. public string Name { get; } = "GamecraftModdingAPIPlayerGameEngine";
  20. public EntitiesDB entitiesDB { set; private get; }
  21. public bool isRemovable => false;
  22. private bool isReady = false;
  23. public void Dispose()
  24. {
  25. isReady = false;
  26. }
  27. public void Ready()
  28. {
  29. isReady = true;
  30. }
  31. public uint GetLocalPlayer()
  32. {
  33. if (!isReady) return uint.MaxValue;
  34. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  35. if (count > 0)
  36. {
  37. return localPlayers[0].ID.entityID;
  38. }
  39. return uint.MaxValue;
  40. }
  41. public uint GetRemotePlayer()
  42. {
  43. if (!isReady) return uint.MaxValue;
  44. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out uint count);
  45. if (count > 0)
  46. {
  47. return localPlayers[0].ID.entityID;
  48. }
  49. return uint.MaxValue;
  50. }
  51. public bool ExistsById(uint playerId)
  52. {
  53. PlayerIDStruct[] players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  54. for (int i = 0; i < count; i++)
  55. {
  56. if (players[i].ID.entityID == playerId)
  57. {
  58. return true;
  59. }
  60. }
  61. players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out count);
  62. for (int i = 0; i < count; i++)
  63. {
  64. if (players[i].ID.entityID == playerId)
  65. {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. public float3 GetLocation(uint playerId)
  72. {
  73. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  74. {
  75. return rbes.position;
  76. }
  77. return float3.zero;
  78. }
  79. public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  80. {
  81. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  82. for (int i = 0; i < characterGroups.Length; i++)
  83. {
  84. EGID egid = new EGID(playerId, characterGroups[i]);
  85. if (entitiesDB.Exists<RigidBodyEntityStruct>(egid))
  86. {
  87. ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid);
  88. if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  89. {
  90. entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
  91. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
  92. }
  93. rbes.position = location;
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. public quaternion GetRotation(uint playerId)
  100. {
  101. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  102. {
  103. return rbes.rotation;
  104. }
  105. return quaternion.identity;
  106. }
  107. public bool SetRotation(uint playerId, quaternion value)
  108. {
  109. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  110. {
  111. rbes.rotation = value;
  112. return true;
  113. }
  114. return false;
  115. }
  116. public float3 GetLinearVelocity(uint playerId)
  117. {
  118. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  119. {
  120. return rbes.velocity;
  121. }
  122. return float3.zero;
  123. }
  124. public bool SetLinearVelocity(uint playerId, float3 value)
  125. {
  126. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  127. {
  128. rbes.velocity = value;
  129. return true;
  130. }
  131. return false;
  132. }
  133. public float3 GetAngularVelocity(uint playerId)
  134. {
  135. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  136. {
  137. return rbes.angularVelocity;
  138. }
  139. return float3.zero;
  140. }
  141. public bool SetAngularVelocity(uint playerId, float3 value)
  142. {
  143. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  144. {
  145. rbes.angularVelocity = value;
  146. return true;
  147. }
  148. return false;
  149. }
  150. public PhysicsMass GetMass(uint playerId)
  151. {
  152. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  153. {
  154. return rbes.physicsMass;
  155. }
  156. return default;
  157. }
  158. public bool SetInverseMass(uint playerId, float inverseMass)
  159. {
  160. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  161. {
  162. rbes.physicsMass.InverseInertia = inverseMass;
  163. return true;
  164. }
  165. return false;
  166. }
  167. public float? GetLastPingTime(uint playerId, PlayerType type)
  168. {
  169. EGID egid = new EGID(playerId, GroupFromEnum(type));
  170. if (entitiesDB.Exists<PlayerNetworkStatsEntityStruct>(egid))
  171. {
  172. return entitiesDB.QueryEntity<PlayerNetworkStatsEntityStruct>(egid).lastPingTimeSinceLevelLoad;
  173. }
  174. return null;
  175. }
  176. // reusable methods
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. private ExclusiveGroup GroupFromEnum(PlayerType type)
  179. {
  180. return type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  181. }
  182. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  183. public bool GetCharacterStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  184. {
  185. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  186. for (int i = 0; i < characterGroups.Length; i++)
  187. {
  188. EGID egid = new EGID(playerId, characterGroups[i]);
  189. if (entitiesDB.Exists<T>(egid))
  190. {
  191. s = entitiesDB.QueryEntity<T>(egid);
  192. return true;
  193. }
  194. }
  195. s = default;
  196. return false;
  197. }
  198. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  199. public bool GetPlayerStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  200. {
  201. ExclusiveGroup[] playerGroups = PlayersExclusiveGroups.AllPlayers;
  202. for (int i = 0; i < playerGroups.Length; i++)
  203. {
  204. EGID egid = new EGID(playerId, playerGroups[i]);
  205. if (entitiesDB.Exists<T>(egid))
  206. {
  207. s = entitiesDB.QueryEntity<T>(egid);
  208. return true;
  209. }
  210. }
  211. s = default;
  212. return false;
  213. }
  214. public EGID? GetThingLookedAt(uint playerId, float maxDistance = -1f)
  215. {
  216. if (!entitiesDB.TryQueryMappedEntities<CharacterCameraRayCastEntityStruct>(
  217. CameraExclusiveGroups.CameraGroup, out var mapper))
  218. return null;
  219. mapper.TryGetEntity(playerId, out CharacterCameraRayCastEntityStruct rayCast);
  220. float distance = maxDistance < 0
  221. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast)
  222. : maxDistance;
  223. if (rayCast.hit && rayCast.distance <= distance)
  224. return rayCast.hitEgid;
  225. return null;
  226. }
  227. }
  228. }