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.

273 lines
9.5KB

  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.DeadCharacters);
  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. float distance = maxDistance < 0
  143. ? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast,
  144. GhostBlockUtils.GhostCastMethod.GhostCastProportionalToBlockSize)
  145. : maxDistance;
  146. if (rayCast.hit && rayCast.distance <= distance)
  147. return rayCast.hitEgid; //May be EGID.Empty (default)
  148. return default;
  149. }
  150. public unsafe Block[] GetSelectedBlocks(uint playerid)
  151. {
  152. if (!entitiesDB.Exists<BoxSelectStateEntityStruct>(playerid,
  153. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup))
  154. return Array.Empty<Block>();
  155. var state = entitiesDB.QueryEntity<BoxSelectStateEntityStruct>(playerid,
  156. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  157. var blocks = entitiesDB.QueryEntity<SelectedBlocksStruct>(playerid,
  158. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
  159. if (!state.active) return Array.Empty<Block>();
  160. var pointer = (EGID*) blocks.selectedBlocks.ToPointer();
  161. var ret = new Block[blocks.count];
  162. for (int j = 0; j < blocks.count; j++)
  163. {
  164. var egid = pointer[j];
  165. ret[j] = Block.New(egid);
  166. }
  167. return ret;
  168. }
  169. public void EnterSeat(uint playerId, EGID seatId)
  170. {
  171. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  172. return;
  173. /*PilotSeatGroupUtils.SwapTagTo<OCCUPIED_TAG>(Functions, seatId);
  174. var opt = GetCharacterStruct<CharacterPilotSeatEntityStruct>(playerId, out var group);
  175. 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.
  176. ref CharacterPilotSeatEntityStruct charSeat = ref opt.Get();
  177. var charId = new EGID(playerId, group);
  178. charSeat.pilotSeatEntity = entitiesDB.GetEntityReference(seatId);
  179. charSeat.entryPositionOffset =
  180. entitiesDB.QueryEntity<PositionEntityStruct>(charId).position -
  181. entitiesDB.QueryEntity<PositionEntityStruct>(seatId).position;
  182. ref var seat = ref entitiesDB.QueryEntity<PilotSeatEntityStruct>(seatId);
  183. seat.occupyingCharacter = entitiesDB.GetEntityReference(charId);
  184. charSeat.followCam = entitiesDB.QueryEntity<SeatFollowCamComponent>(seatId).followCam;
  185. Functions.SwapEntityGroup<CharacterEntityDescriptor>(charId, CharacterExclusiveGroups.InPilotSeatGroup);*/
  186. }
  187. public void ExitSeat(uint playerId)
  188. {
  189. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  190. return;
  191. /*EGID egid = new EGID(playerId, CharacterExclusiveGroups.InPilotSeatGroup);
  192. var opt = entitiesDB.QueryEntityOptional<CharacterPilotSeatEntityStruct>(egid);
  193. if (!opt) return;
  194. opt.Get().instantExit = true;
  195. entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);*/
  196. }
  197. public bool SpawnMachine(uint playerId)
  198. {
  199. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  200. return false;
  201. EGID egid = new EGID(playerId, CharacterExclusiveGroups.MachineSpawningGroup);
  202. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  203. return false;
  204. if (entitiesDB.QueryEntity<CharacterMachineSpawningValidityComponent>(egid).isMachinePlacementInvalid)
  205. {
  206. Logging.MetaDebugLog("Machine placement invalid");
  207. return false;
  208. }
  209. //Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.OnFootGroup);
  210. FakeInput.ActionInput(playerId, primary: true);
  211. return true;
  212. }
  213. public bool DespawnMachine(uint playerId)
  214. {
  215. if (!TimeRunningModeUtil.IsTimeRunningMode(entitiesDB))
  216. return false;
  217. GetCharacterStruct<CharacterTagEntityStruct>(playerId, out var group);
  218. if (group.isInvalid)
  219. return false;
  220. EGID egid = new EGID(playerId, group);
  221. if (!entitiesDB.Exists<CharacterTagEntityStruct>(egid))
  222. return false;
  223. Functions.SwapEntityGroup<CharacterEntityDescriptor>(egid, CharacterExclusiveGroups.MachineSpawningGroup);
  224. return true;
  225. }
  226. public uint GetPing()
  227. {
  228. return entitiesDB
  229. .QueryUniqueEntity<NetworkStatsEntityStruct>(MultiplayerExclusiveGroups.MultiplayerStateGroup)
  230. .networkStats.PingMs;
  231. }
  232. public Block GetGhostBlock(uint playerId)
  233. {
  234. var egid = new EGID(playerId, GHOST_BLOCKS_ENABLED.Group);
  235. return entitiesDB.Exists<DBEntityStruct>(egid) ? Block.New(egid) : null;
  236. }
  237. }
  238. }