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.

PlayerEngine.cs 14KB

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