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.

401 lines
12KB

  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 RobocraftX.Blocks.Ghost;
  9. using RobocraftX.Character.Camera;
  10. using RobocraftX.Character.Factories;
  11. using Gamecraft.CharacterVulnerability;
  12. using Gamecraft.CharacterVulnerability.Entities;
  13. using Svelto.ECS;
  14. using Unity.Mathematics;
  15. using Unity.Physics;
  16. using GamecraftModdingAPI.Engines;
  17. namespace GamecraftModdingAPI.Players
  18. {
  19. internal class PlayerEngine : IApiEngine, IFactoryEngine
  20. {
  21. public string Name { get; } = "GamecraftModdingAPIPlayerGameEngine";
  22. public EntitiesDB entitiesDB { set; private get; }
  23. public bool isRemovable => false;
  24. public IEntityFactory Factory { set; private get; }
  25. private bool isReady = false;
  26. public void Dispose()
  27. {
  28. isReady = false;
  29. }
  30. public void Ready()
  31. {
  32. isReady = true;
  33. }
  34. public uint GetLocalPlayer()
  35. {
  36. if (!isReady) return uint.MaxValue;
  37. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  38. if (count > 0)
  39. {
  40. return localPlayers[0].ID.entityID;
  41. }
  42. return uint.MaxValue;
  43. }
  44. public uint GetRemotePlayer()
  45. {
  46. if (!isReady) return uint.MaxValue;
  47. PlayerIDStruct[] localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out uint count);
  48. if (count > 0)
  49. {
  50. return localPlayers[0].ID.entityID;
  51. }
  52. return uint.MaxValue;
  53. }
  54. public bool ExistsById(uint playerId)
  55. {
  56. PlayerIDStruct[] players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
  57. for (int i = 0; i < count; i++)
  58. {
  59. if (players[i].ID.entityID == playerId)
  60. {
  61. return true;
  62. }
  63. }
  64. players = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers).ToFastAccess(out count);
  65. for (int i = 0; i < count; i++)
  66. {
  67. if (players[i].ID.entityID == playerId)
  68. {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. public float3 GetLocation(uint playerId)
  75. {
  76. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  77. {
  78. return rbes.position;
  79. }
  80. return float3.zero;
  81. }
  82. public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  83. {
  84. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  85. for (int i = 0; i < characterGroups.Length; i++)
  86. {
  87. EGID egid = new EGID(playerId, characterGroups[i]);
  88. if (entitiesDB.Exists<RigidBodyEntityStruct>(egid))
  89. {
  90. ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid);
  91. if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  92. {
  93. entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
  94. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
  95. }
  96. rbes.position = location;
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. public quaternion GetRotation(uint playerId)
  103. {
  104. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  105. {
  106. return rbes.rotation;
  107. }
  108. return quaternion.identity;
  109. }
  110. public bool SetRotation(uint playerId, quaternion value)
  111. {
  112. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  113. {
  114. rbes.rotation = value;
  115. return true;
  116. }
  117. return false;
  118. }
  119. public float3 GetLinearVelocity(uint playerId)
  120. {
  121. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  122. {
  123. return rbes.velocity;
  124. }
  125. return float3.zero;
  126. }
  127. public bool SetLinearVelocity(uint playerId, float3 value)
  128. {
  129. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  130. {
  131. rbes.velocity = value;
  132. return true;
  133. }
  134. return false;
  135. }
  136. public float3 GetAngularVelocity(uint playerId)
  137. {
  138. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  139. {
  140. return rbes.angularVelocity;
  141. }
  142. return float3.zero;
  143. }
  144. public bool SetAngularVelocity(uint playerId, float3 value)
  145. {
  146. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  147. {
  148. rbes.angularVelocity = value;
  149. return true;
  150. }
  151. return false;
  152. }
  153. public PhysicsMass GetMass(uint playerId)
  154. {
  155. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  156. {
  157. return rbes.physicsMass;
  158. }
  159. return default;
  160. }
  161. public bool SetInverseMass(uint playerId, float inverseMass)
  162. {
  163. if (GetCharacterStruct<RigidBodyEntityStruct>(playerId, out RigidBodyEntityStruct rbes))
  164. {
  165. rbes.physicsMass.InverseInertia = inverseMass;
  166. return true;
  167. }
  168. return false;
  169. }
  170. public float? GetLastPingTime(uint playerId, PlayerType type)
  171. {
  172. EGID egid = new EGID(playerId, PlayerGroupFromEnum(type));
  173. if (entitiesDB.Exists<PlayerNetworkStatsEntityStruct>(egid))
  174. {
  175. return entitiesDB.QueryEntity<PlayerNetworkStatsEntityStruct>(egid).lastPingTimeSinceLevelLoad;
  176. }
  177. return null;
  178. }
  179. public float GetInitialHealth(uint playerId)
  180. {
  181. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
  182. {
  183. return c.initialHealth;
  184. }
  185. return -1f;
  186. }
  187. public bool SetInitialHealth(uint playerId, float val)
  188. {
  189. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
  190. {
  191. c.initialHealth = val;
  192. return true;
  193. }
  194. return false;
  195. }
  196. public float GetCurrentHealth(uint playerId)
  197. {
  198. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
  199. {
  200. return c.currentHealth;
  201. }
  202. return -1f;
  203. }
  204. public bool SetCurrentHealth(uint playerId, float val)
  205. {
  206. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
  207. {
  208. c.currentHealth = val;
  209. return true;
  210. }
  211. return false;
  212. }
  213. public bool DamagePlayer(uint playerId, float amount)
  214. {
  215. Factory.BuildEntity<DamageEntityDescriptor>(
  216. new EGID(CharacterVulnerabilityExclusiveGroups.NextDamageEntityId, CharacterVulnerabilityExclusiveGroups.CharacterDamageExclusiveGroup)
  217. ).Init(new DamageEntityStruct
  218. {
  219. damage = amount,
  220. targetPlayerEntityId = playerId,
  221. });
  222. return true;
  223. }
  224. public bool GetDamageable(uint playerId)
  225. {
  226. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct c))
  227. {
  228. return c.canTakeDamageStat;
  229. }
  230. return false;
  231. }
  232. public bool SetDamageable(uint playerId, bool val)
  233. {
  234. if (GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out CharacterHealthEntityStruct ches))
  235. {
  236. ches.canTakeDamage = val;
  237. ches.canTakeDamage = val;
  238. return true;
  239. }
  240. return false;
  241. }
  242. public uint GetInitialLives(uint playerId)
  243. {
  244. if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
  245. {
  246. return c.initialLives;
  247. }
  248. return uint.MaxValue;
  249. }
  250. public bool SetInitialLives(uint playerId, uint val)
  251. {
  252. if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
  253. {
  254. c.initialLives = val;
  255. return true;
  256. }
  257. return false;
  258. }
  259. public uint GetCurrentLives(uint playerId)
  260. {
  261. if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
  262. {
  263. return c.currentLives;
  264. }
  265. return uint.MaxValue;
  266. }
  267. public bool SetCurrentLives(uint playerId, uint val)
  268. {
  269. if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
  270. {
  271. c.currentLives = val;
  272. return true;
  273. }
  274. return false;
  275. }
  276. public bool GetGameOverScreen(uint playerId)
  277. {
  278. if (GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out CharacterLivesEntityComponent c))
  279. {
  280. return c.gameOverScreen;
  281. }
  282. return false;
  283. }
  284. public bool IsDead(uint playerId)
  285. {
  286. return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadGroup);
  287. }
  288. public int GetSelectedBlock(uint playerId)
  289. {
  290. if (GetCharacterStruct<EquippedPartStruct>(playerId, out EquippedPartStruct c))
  291. {
  292. return c.SelectedDBPartID;
  293. }
  294. return ushort.MaxValue;
  295. }
  296. public byte GetSelectedColor(uint playerId)
  297. {
  298. if (GetCharacterStruct<EquippedColourStruct>(playerId, out EquippedColourStruct c))
  299. {
  300. return c.indexInPalette;
  301. }
  302. return 255;
  303. }
  304. // reusable methods
  305. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  306. private ExclusiveGroup PlayerGroupFromEnum(PlayerType type)
  307. {
  308. return type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  309. }
  310. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  311. public bool GetCharacterStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  312. {
  313. ExclusiveGroup[] characterGroups = CharacterExclusiveGroups.AllCharacters;
  314. for (int i = 0; i < characterGroups.Length; i++)
  315. {
  316. EGID egid = new EGID(playerId, characterGroups[i]);
  317. if (entitiesDB.Exists<T>(egid))
  318. {
  319. s = entitiesDB.QueryEntity<T>(egid);
  320. return true;
  321. }
  322. }
  323. s = default;
  324. return false;
  325. }
  326. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  327. public bool GetPlayerStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
  328. {
  329. ExclusiveGroup[] playerGroups = PlayersExclusiveGroups.AllPlayers;
  330. for (int i = 0; i < playerGroups.Length; i++)
  331. {
  332. EGID egid = new EGID(playerId, playerGroups[i]);
  333. if (entitiesDB.Exists<T>(egid))
  334. {
  335. s = entitiesDB.QueryEntity<T>(egid);
  336. return true;
  337. }
  338. }
  339. s = default;
  340. return false;
  341. }
  342. public EGID? GetThingLookedAt(uint playerId, float maxDistance = -1f)
  343. {
  344. if (!entitiesDB.TryQueryMappedEntities<CharacterCameraRayCastEntityStruct>(
  345. CameraExclusiveGroups.CameraGroup, out var mapper))
  346. return null;
  347. mapper.TryGetEntity(playerId, out CharacterCameraRayCastEntityStruct rayCast);
  348. float distance = maxDistance < 0
  349. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast)
  350. : maxDistance;
  351. if (rayCast.hit && rayCast.distance <= distance)
  352. return rayCast.hitEgid;
  353. return null;
  354. }
  355. }
  356. }