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 15KB

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