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

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