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.

274 lines
9.6KB

  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.Common;
  11. using RobocraftX.Multiplayer;
  12. using RobocraftX.SimulationModeState;
  13. using Svelto.ECS;
  14. using Techblox.Camera;
  15. using Unity.Mathematics;
  16. using Svelto.ECS.DataStructures;
  17. using Techblox.BuildingDrone;
  18. using Techblox.Character;
  19. using TechbloxModdingAPI.Engines;
  20. using TechbloxModdingAPI.Input;
  21. using TechbloxModdingAPI.Utility;
  22. namespace TechbloxModdingAPI.Players
  23. {
  24. internal class PlayerEngine : IFunEngine
  25. {
  26. public string Name { get; } = "TechbloxModdingAPIPlayerGameEngine";
  27. public EntitiesDB entitiesDB { set; private get; }
  28. public bool isRemovable => false;
  29. public IEntityFunctions Functions { get; set; }
  30. private bool isReady = false;
  31. public void Dispose()
  32. {
  33. isReady = false;
  34. }
  35. public void Ready()
  36. {
  37. isReady = true;
  38. }
  39. public uint GetLocalPlayer()
  40. {
  41. if (!isReady) return uint.MaxValue;
  42. var (localPlayers, count) = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
  43. if (count > 0)
  44. {
  45. return localPlayers[0].ID.entityID;
  46. }
  47. return uint.MaxValue;
  48. }
  49. public uint GetRemotePlayer()
  50. {
  51. if (!isReady) return uint.MaxValue;
  52. var (localPlayers, count) = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
  53. if (count > 0)
  54. {
  55. return localPlayers[0].ID.entityID;
  56. }
  57. return uint.MaxValue;
  58. }
  59. public long GetAllPlayerCount()
  60. {
  61. if (entitiesDB == null) return 0;
  62. long count = 0;
  63. foreach (ExclusiveGroupStruct eg in PlayersExclusiveGroups.AllPlayers)
  64. {
  65. count += entitiesDB.Count<PlayerIDStruct>(eg);
  66. }
  67. return count;
  68. }
  69. public long GetLocalPlayerCount()
  70. {
  71. if (entitiesDB == null) return 0;
  72. return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
  73. }
  74. public long GetRemotePlayerCount()
  75. {
  76. if (entitiesDB == null) return 0;
  77. return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
  78. }
  79. public bool ExistsById(uint playerId)
  80. {
  81. if (entitiesDB == null) return false;
  82. return entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.LocalPlayers)
  83. || entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.RemotePlayers);
  84. }
  85. public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  86. {
  87. if (entitiesDB == null) return false;
  88. var rbesOpt = GetCharacterStruct<RigidBodyEntityStruct>(playerId, out var group);
  89. if (!rbesOpt)
  90. return false;
  91. if (group == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  92. {
  93. ExitSeat(playerId);
  94. }
  95. rbesOpt.Get().position = location;
  96. return true;
  97. }
  98. public bool IsDead(uint playerId)
  99. {
  100. if (entitiesDB == null) return true;
  101. return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadGroup);
  102. }
  103. // reusable methods
  104. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  105. public OptionalRef<T> GetCharacterStruct<T>(uint playerId) where T : unmanaged, IEntityComponent =>
  106. GetCharacterStruct<T>(playerId, out _);
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public OptionalRef<T> GetCharacterStruct<T>(uint playerId, out ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  109. {
  110. group = default;
  111. if (GameState.IsBuildMode())
  112. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, LocalBuildingDrone.BuildGroup));
  113. var characterGroups = CharacterExclusiveGroups.AllCharacters;
  114. for (int i = 0; i < characterGroups.count; i++)
  115. {
  116. EGID egid = new EGID(playerId, characterGroups[i]);
  117. var opt = entitiesDB.QueryEntityOptional<T>(egid);
  118. if (opt.Exists)
  119. {
  120. group = characterGroups[i];
  121. return opt;
  122. }
  123. }
  124. return new OptionalRef<T>();
  125. }
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. public OptionalRef<T> GetPlayerStruct<T>(uint playerId, PlayerType type) where T : unmanaged, IEntityComponent
  128. {
  129. var playerGroup = type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  130. EGID egid = new EGID(playerId, playerGroup);
  131. return entitiesDB.QueryEntityOptional<T>(egid);
  132. }
  133. public OptionalRef<T> GetCameraStruct<T>(uint playerId) where T : unmanaged, IEntityComponent
  134. {
  135. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, CameraExclusiveGroups.PhysicCameraGroup));
  136. }
  137. public EGID GetThingLookedAt(uint playerId, float maxDistance = -1f)
  138. {
  139. var opt = GetCameraStruct<PhysicCameraRayCastEntityStruct>(playerId);
  140. if (!opt) return default;
  141. PhysicCameraRayCastEntityStruct rayCast = opt;
  142. EGID physicCameraEgid = new EGID(playerId, CameraExclusiveGroups.PhysicCameraGroup);
  143. float distance = maxDistance < 0
  144. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast, physicCameraEgid,
  145. GhostBlockUtils.GhostCastMethod.GhostCastProportionalToBlockSize)
  146. : maxDistance;
  147. if (rayCast.hit && rayCast.distance <= distance)
  148. return rayCast.hitEgid; //May be EGID.Empty (default)
  149. return default;
  150. }
  151. public unsafe Block[] GetSelectedBlocks(uint playerid)
  152. {
  153. if (!entitiesDB.Exists<BoxSelectStateEntityStruct>(playerid,
  154. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup))
  155. return Array.Empty<Block>();
  156. var state = entitiesDB.QueryEntity<BoxSelectStateEntityStruct>(playerid,
  157. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  158. var blocks = entitiesDB.QueryEntity<SelectedBlocksStruct>(playerid,
  159. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  160. if (!state.active) return Array.Empty<Block>();
  161. var pointer = (EGID*) blocks.selectedBlocks.ToPointer();
  162. var ret = new Block[blocks.count];
  163. for (int j = 0; j < blocks.count; j++)
  164. {
  165. var egid = pointer[j];
  166. ret[j] = Block.New(egid);
  167. }
  168. return ret;
  169. }
  170. public void EnterSeat(uint playerId, EGID seatId)
  171. {
  172. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  173. return;
  174. /*PilotSeatGroupUtils.SwapTagTo<OCCUPIED_TAG>(Functions, seatId);
  175. var opt = GetCharacterStruct<CharacterPilotSeatEntityStruct>(playerId, out var group);
  176. if (!opt) return; - TODO: This is server code and mods run in client code atm. We can only send inputs even in singleplayer as it is.
  177. ref CharacterPilotSeatEntityStruct charSeat = ref opt.Get();
  178. var charId = new EGID(playerId, group);
  179. charSeat.pilotSeatEntity = entitiesDB.GetEntityReference(seatId);
  180. charSeat.entryPositionOffset =
  181. entitiesDB.QueryEntity<PositionEntityStruct>(charId).position -
  182. entitiesDB.QueryEntity<PositionEntityStruct>(seatId).position;
  183. ref var seat = ref entitiesDB.QueryEntity<PilotSeatEntityStruct>(seatId);
  184. seat.occupyingCharacter = entitiesDB.GetEntityReference(charId);
  185. charSeat.followCam = entitiesDB.QueryEntity<SeatFollowCamComponent>(seatId).followCam;
  186. Functions.SwapEntityGroup<CharacterEntityDescriptor>(charId, CharacterExclusiveGroups.InPilotSeatGroup);*/
  187. }
  188. public void ExitSeat(uint playerId)
  189. {
  190. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  191. return;
  192. /*EGID egid = new EGID(playerId, CharacterExclusiveGroups.InPilotSeatGroup);
  193. var opt = entitiesDB.QueryEntityOptional<CharacterPilotSeatEntityStruct>(egid);
  194. if (!opt) return;
  195. opt.Get().instantExit = true;
  196. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);*/
  197. }
  198. public bool SpawnMachine(uint playerId)
  199. {
  200. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  201. return false;
  202. EGID egid = new EGID(playerId, CharacterExclusiveGroups.MachineSpawningGroup);
  203. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  204. return false;
  205. if (entitiesDB.QueryEntity<CharacterMachineSpawningValidityComponent>(egid).isMachinePlacementInvalid)
  206. {
  207. Logging.MetaDebugLog("Machine placement invalid");
  208. return false;
  209. }
  210. //Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.OnFootGroup);
  211. FakeInput.ActionInput(playerId, primary: true);
  212. return true;
  213. }
  214. public bool DespawnMachine(uint playerId)
  215. {
  216. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  217. return false;
  218. GetCharacterStruct<CharacterTagEntityStruct>(playerId, out var group);
  219. if (group.isInvalid)
  220. return false;
  221. EGID egid = new EGID(playerId, group);
  222. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  223. return false;
  224. Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.MachineSpawningGroup);
  225. return true;
  226. }
  227. public uint GetPing()
  228. {
  229. return entitiesDB
  230. .QueryUniqueEntity<NetworkStatsEntityStruct>(MultiplayerExclusiveGroups.MultiplayerStateGroup)
  231. .networkStats.PingMs;
  232. }
  233. public Block GetGhostBlock(uint playerId)
  234. {
  235. var egid = new EGID(playerId, GHOST_BLOCKS_ENABLED.Group);
  236. return entitiesDB.Exists<DBEntityStruct>(egid) ? Block.New(egid) : null;
  237. }
  238. }
  239. }