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.

275 lines
9.7KB

  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. using TechbloxModdingAPI.Utility.ECS;
  23. namespace TechbloxModdingAPI.Players
  24. {
  25. internal class PlayerEngine : IFunEngine
  26. {
  27. public string Name { get; } = "TechbloxModdingAPIPlayerGameEngine";
  28. public EntitiesDB entitiesDB { set; private get; }
  29. public bool isRemovable => false;
  30. public IEntityFunctions Functions { get; set; }
  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, count) = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
  44. if (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, count) = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
  54. if (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 bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
  87. {
  88. if (entitiesDB == null) return false;
  89. var rbesOpt = GetCharacterStruct<RigidBodyEntityStruct>(playerId, out var group);
  90. if (!rbesOpt)
  91. return false;
  92. if (group == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
  93. {
  94. ExitSeat(playerId);
  95. }
  96. rbesOpt.Get().position = location;
  97. return true;
  98. }
  99. public bool IsDead(uint playerId)
  100. {
  101. if (entitiesDB == null) return true;
  102. return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadGroup);
  103. }
  104. // reusable methods
  105. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106. public OptionalRef<T> GetCharacterStruct<T>(uint playerId) where T : unmanaged, IEntityComponent =>
  107. GetCharacterStruct<T>(playerId, out _);
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. public OptionalRef<T> GetCharacterStruct<T>(uint playerId, out ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  110. {
  111. group = default;
  112. if (GameState.IsBuildMode())
  113. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, LocalBuildingDrone.BuildGroup));
  114. var characterGroups = CharacterExclusiveGroups.AllCharacters;
  115. for (int i = 0; i < characterGroups.count; i++)
  116. {
  117. EGID egid = new EGID(playerId, characterGroups[i]);
  118. var opt = entitiesDB.QueryEntityOptional<T>(egid);
  119. if (opt.Exists)
  120. {
  121. group = characterGroups[i];
  122. return opt;
  123. }
  124. }
  125. return new OptionalRef<T>();
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. public OptionalRef<T> GetPlayerStruct<T>(uint playerId, PlayerType type) where T : unmanaged, IEntityComponent
  129. {
  130. var playerGroup = type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
  131. EGID egid = new EGID(playerId, playerGroup);
  132. return entitiesDB.QueryEntityOptional<T>(egid);
  133. }
  134. public OptionalRef<T> GetCameraStruct<T>(uint playerId) where T : unmanaged, IEntityComponent
  135. {
  136. return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, CameraExclusiveGroups.PhysicCameraGroup));
  137. }
  138. public EGID GetThingLookedAt(uint playerId, float maxDistance = -1f)
  139. {
  140. var opt = GetCameraStruct<PhysicCameraRayCastEntityStruct>(playerId);
  141. if (!opt) return default;
  142. PhysicCameraRayCastEntityStruct rayCast = opt;
  143. EGID physicCameraEgid = new EGID(playerId, CameraExclusiveGroups.PhysicCameraGroup);
  144. float distance = maxDistance < 0
  145. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast, physicCameraEgid,
  146. GhostBlockUtils.GhostCastMethod.GhostCastProportionalToBlockSize)
  147. : maxDistance;
  148. if (rayCast.hit && rayCast.distance <= distance)
  149. return rayCast.hitEgid; //May be EGID.Empty (default)
  150. return default;
  151. }
  152. public unsafe Block[] GetSelectedBlocks(uint playerid)
  153. {
  154. if (!entitiesDB.Exists<BoxSelectStateEntityStruct>(playerid,
  155. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup))
  156. return Array.Empty<Block>();
  157. var state = entitiesDB.QueryEntity<BoxSelectStateEntityStruct>(playerid,
  158. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  159. var blocks = entitiesDB.QueryEntity<SelectedBlocksStruct>(playerid,
  160. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  161. if (!state.active) return Array.Empty<Block>();
  162. var pointer = (EGID*) blocks.selectedBlocks.ToPointer();
  163. var ret = new Block[blocks.count];
  164. for (int j = 0; j < blocks.count; j++)
  165. {
  166. var egid = pointer[j];
  167. ret[j] = Block.New(egid);
  168. }
  169. return ret;
  170. }
  171. public void EnterSeat(uint playerId, EGID seatId)
  172. {
  173. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  174. return;
  175. /*PilotSeatGroupUtils.SwapTagTo<OCCUPIED_TAG>(Functions, seatId);
  176. var opt = GetCharacterStruct<CharacterPilotSeatEntityStruct>(playerId, out var group);
  177. 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.
  178. ref CharacterPilotSeatEntityStruct charSeat = ref opt.Get();
  179. var charId = new EGID(playerId, group);
  180. charSeat.pilotSeatEntity = entitiesDB.GetEntityReference(seatId);
  181. charSeat.entryPositionOffset =
  182. entitiesDB.QueryEntity<PositionEntityStruct>(charId).position -
  183. entitiesDB.QueryEntity<PositionEntityStruct>(seatId).position;
  184. ref var seat = ref entitiesDB.QueryEntity<PilotSeatEntityStruct>(seatId);
  185. seat.occupyingCharacter = entitiesDB.GetEntityReference(charId);
  186. charSeat.followCam = entitiesDB.QueryEntity<SeatFollowCamComponent>(seatId).followCam;
  187. Functions.SwapEntityGroup<CharacterEntityDescriptor>(charId, CharacterExclusiveGroups.InPilotSeatGroup);*/
  188. }
  189. public void ExitSeat(uint playerId)
  190. {
  191. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  192. return;
  193. /*EGID egid = new EGID(playerId, CharacterExclusiveGroups.InPilotSeatGroup);
  194. var opt = entitiesDB.QueryEntityOptional<CharacterPilotSeatEntityStruct>(egid);
  195. if (!opt) return;
  196. opt.Get().instantExit = true;
  197. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);*/
  198. }
  199. public bool SpawnMachine(uint playerId)
  200. {
  201. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  202. return false;
  203. EGID egid = new EGID(playerId, CharacterExclusiveGroups.MachineSpawningGroup);
  204. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  205. return false;
  206. if (entitiesDB.QueryEntity<CharacterMachineSpawningValidityComponent>(egid).isMachinePlacementInvalid)
  207. {
  208. Logging.MetaDebugLog("Machine placement invalid");
  209. return false;
  210. }
  211. //Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.OnFootGroup);
  212. FakeInput.ActionInput(playerId, primary: true);
  213. return true;
  214. }
  215. public bool DespawnMachine(uint playerId)
  216. {
  217. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  218. return false;
  219. GetCharacterStruct<CharacterTagEntityStruct>(playerId, out var group);
  220. if (group.isInvalid)
  221. return false;
  222. EGID egid = new EGID(playerId, group);
  223. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  224. return false;
  225. Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.MachineSpawningGroup);
  226. return true;
  227. }
  228. public uint GetPing()
  229. {
  230. return entitiesDB
  231. .QueryUniqueEntity<NetworkStatsEntityStruct>(MultiplayerExclusiveGroups.MultiplayerStateGroup)
  232. .networkStats.PingMs;
  233. }
  234. public Block GetGhostBlock(uint playerId)
  235. {
  236. var egid = new EGID(playerId, GHOST_BLOCKS_ENABLED.Group);
  237. return entitiesDB.Exists<DBEntityStruct>(egid) ? Block.New(egid) : null;
  238. }
  239. }
  240. }