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.

486 lines
15KB

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