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.

550 lines
20KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using RobocraftX.Character;
  4. using RobocraftX.Character.Movement;
  5. using Unity.Mathematics;
  6. using RobocraftX.Common;
  7. using RobocraftX.Common.Players;
  8. using RobocraftX.GUI.Wires;
  9. using RobocraftX.Physics;
  10. using Svelto.ECS;
  11. using Techblox.BuildingDrone;
  12. using Techblox.Camera;
  13. using Techblox.Character;
  14. using TechbloxModdingAPI.Blocks;
  15. using TechbloxModdingAPI.Client.App;
  16. using TechbloxModdingAPI.Common;
  17. using TechbloxModdingAPI.Common.Engines;
  18. using TechbloxModdingAPI.Players;
  19. using UnityEngine;
  20. namespace TechbloxModdingAPI
  21. {
  22. /// <summary>
  23. /// An in-game player character. Any Leo you see is a player.
  24. /// </summary>
  25. public partial class Player : EcsObjectBase, IEquatable<Player>, IEquatable<EGID>
  26. {
  27. // static functionality
  28. private static readonly PlayerEngine playerEngine = new();
  29. private static readonly PlayerEventsEngine playerEventsEngine = new();
  30. private static Player localPlayer;
  31. /// <summary>
  32. /// Checks if the specified player exists.
  33. /// </summary>
  34. /// <returns>Whether the player exists.</returns>
  35. /// <param name="player">Player type.</param>
  36. public static bool Exists(PlayerType player)
  37. {
  38. switch (player)
  39. {
  40. case PlayerType.Remote:
  41. return playerEngine.GetRemotePlayers().Length > 0;
  42. case PlayerType.Local:
  43. return playerEngine.GetLocalPlayer() != uint.MaxValue;
  44. }
  45. return false;
  46. }
  47. /// <summary>
  48. /// Checks if the specified player exists.
  49. /// </summary>
  50. /// <returns>Whether the player exists.</returns>
  51. /// <param name="player">The player's unique identifier.</param>
  52. public static bool Exists(uint player)
  53. {
  54. return playerEngine.ExistsById(player);
  55. }
  56. /// <summary>
  57. /// The amount of Players in the current game.
  58. /// </summary>
  59. /// <returns>The count.</returns>
  60. public static uint Count()
  61. {
  62. return (uint) playerEngine.GetAllPlayerCount();
  63. }
  64. /// <summary>
  65. /// Returns the current player belonging to this client. It will be different after entering/leaving simulation.
  66. /// May return null if the local player doesn't exist.
  67. /// </summary>
  68. public static Player LocalPlayer
  69. {
  70. get
  71. {
  72. var playerId = playerEngine.GetLocalPlayer();
  73. if (playerId == uint.MaxValue) return null;
  74. if (localPlayer == null || localPlayer.Id != playerId)
  75. localPlayer = GetInstance(playerId);
  76. return localPlayer;
  77. }
  78. }
  79. internal static Player GetInstance(uint id)
  80. {
  81. return EcsObjectBase.GetInstanceExisting(new EGID(id, CharacterExclusiveGroups.OnFootGroup),
  82. e => new Player(e.entityID));
  83. }
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="T:TechbloxModdingAPI.Player"/> class.
  86. /// </summary>
  87. /// <param name="id">The player's unique identifier.</param>
  88. public Player(uint id) : base(new EGID(id, CharacterExclusiveGroups.OnFootGroup), typeof(CharacterEntityDescriptor))
  89. {
  90. this.Id = id;
  91. if (!Exists(id))
  92. {
  93. throw new PlayerNotFoundException($"No player with id {id} exists");
  94. }
  95. this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
  96. }
  97. // object fields & properties
  98. /// <summary>
  99. /// The player's type.
  100. /// The player type is always relative to the current client, not the game host.
  101. /// </summary>
  102. /// <value>The enumerated player type.</value>
  103. public PlayerType Type { get; }
  104. /// <summary>
  105. /// The player's unique identifier.
  106. /// </summary>
  107. /// <value>The identifier.</value>
  108. public new uint Id { get; }
  109. /// <summary>
  110. /// The player's current position.
  111. /// </summary>
  112. /// <value>The position.</value>
  113. public float3 Position
  114. {
  115. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().position;
  116. set => playerEngine.SetLocation(Id, value, false);
  117. }
  118. /// <summary>
  119. /// The player's current rotation.
  120. /// </summary>
  121. /// <value>The rotation.</value>
  122. public float3 Rotation
  123. {
  124. get => ((Quaternion) (GameClient.IsBuildMode
  125. ? playerEngine.GetCameraStruct<CameraEntityStruct>(Id).Get().rotation
  126. : playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().rotation)).eulerAngles;
  127. set => _ = GameClient.IsBuildMode
  128. ? playerEngine.GetCameraStruct<CameraEntityStruct>(Id).Get().rotation = quaternion.Euler(value)
  129. : playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().rotation = quaternion.Euler(value);
  130. }
  131. /// <summary>
  132. /// The player's current velocity.
  133. /// </summary>
  134. /// <value>The velocity.</value>
  135. public float3 Velocity
  136. {
  137. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().velocity;
  138. set => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().velocity = value;
  139. }
  140. /// <summary>
  141. /// The player's current angular velocity.
  142. /// </summary>
  143. /// <value>The angular velocity.</value>
  144. public float3 AngularVelocity
  145. {
  146. get => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().angularVelocity;
  147. set => playerEngine.GetCharacterStruct<RigidBodyEntityStruct>(Id).Get().angularVelocity = value;
  148. }
  149. /// <summary>
  150. /// The player's mass.
  151. /// </summary>
  152. /// <value>The mass.</value>
  153. [Obsolete] // We cannot get it clientside or something
  154. public float Mass => 0;
  155. /// <summary>
  156. /// The player's latest network ping time.
  157. /// </summary>
  158. /// <value>The ping (s).</value>
  159. public float Ping
  160. {
  161. get
  162. {
  163. return playerEngine.GetPing() / 1000f;
  164. }
  165. }
  166. /// <summary>
  167. /// The player's initial health when entering Simulation (aka Time Running) mode.
  168. /// </summary>
  169. /// <value>The initial health.</value>
  170. [Obsolete("We can no longer get initial health, returns max health.")]
  171. public float InitialHealth
  172. {
  173. get
  174. {
  175. var opt = playerEngine.GetCharacterStruct<CharacterHealthEntityComponent>(Id);
  176. return opt ? opt.Get().maxHealth : -1f;
  177. }
  178. set => playerEngine.GetCharacterStruct<CharacterHealthEntityComponent>(Id).Get().maxHealth = value;
  179. }
  180. /// <summary>
  181. /// The player's current health in Simulation (aka Time Running) mode.
  182. /// </summary>
  183. /// <value>The current health.</value>
  184. public float CurrentHealth
  185. {
  186. get
  187. {
  188. var opt = playerEngine.GetCharacterStruct<CharacterHealthEntityComponent>(Id);
  189. return opt ? opt.Get().currentHealth : -1f;
  190. }
  191. set => playerEngine.GetCharacterStruct<CharacterHealthEntityComponent>(Id).Get().currentHealth = value;
  192. }
  193. /// <summary>
  194. /// Whether this <see cref="T:TechbloxModdingAPI.Player"/> is damageable.
  195. /// </summary>
  196. /// <value><c>true</c> if damageable; otherwise, <c>false</c>.</value>
  197. [Obsolete("Players are probably always damageable")]
  198. public bool Damageable
  199. {
  200. get => true;
  201. // ReSharper disable once ValueParameterNotUsed
  202. set
  203. {
  204. }
  205. }
  206. /// <summary>
  207. /// The player's lives when initially entering Simulation (aka Time Running) mode.
  208. /// </summary>
  209. /// <value>The initial lives.</value>
  210. [Obsolete("The player has infinite lives")]
  211. public uint InitialLives
  212. {
  213. get => uint.MaxValue;
  214. // ReSharper disable once ValueParameterNotUsed
  215. set { }
  216. }
  217. /// <summary>
  218. /// The player's current lives in Simulation (aka Time Running) mode.
  219. /// </summary>
  220. /// <value>The current lives.</value>
  221. [Obsolete("The player has infinite lives")]
  222. public uint CurrentLives
  223. {
  224. get => uint.MaxValue;
  225. // ReSharper disable once ValueParameterNotUsed
  226. set { }
  227. }
  228. /*/// <summary>
  229. /// Whether the Game Over screen is displayed for the player.
  230. /// </summary>
  231. /// <value><c>true</c> if game over; otherwise, <c>false</c>.</value>
  232. public bool GameOver
  233. {
  234. get => playerEngine.GetGameOverScreen(Id);
  235. }*/
  236. /// <summary>
  237. /// Whether the player is dead.
  238. /// If <c>true</c>, hopefully it was quick.
  239. /// </summary>
  240. /// <value><c>true</c> if dead; otherwise, <c>false</c>.</value>
  241. public bool Dead
  242. {
  243. get => playerEngine.IsDead(Id);
  244. }
  245. /// <summary>
  246. /// The player's selected block ID in their hand.
  247. /// </summary>
  248. /// <value>The selected block.</value>
  249. public BlockIDs SelectedBlock
  250. {
  251. get
  252. {
  253. var optstruct = playerEngine.GetCharacterStruct<EquippedPartStruct>(Id);
  254. return optstruct ? (BlockIDs) optstruct.Get().selectedDBPartID : BlockIDs.Invalid;
  255. }
  256. }
  257. /// <summary>
  258. /// The player's selected block color in their hand.
  259. /// </summary>
  260. /// <value>The selected block's color.</value>
  261. public BlockColor SelectedColor
  262. {
  263. get
  264. {
  265. var optstruct = playerEngine.GetCharacterStruct<EquippedColourStruct>(Id);
  266. return optstruct ? new BlockColor(optstruct.Get().indexInPalette) : BlockColors.Default;
  267. }
  268. }
  269. /// <summary>
  270. /// The player's selected block colour in their hand.
  271. /// </summary>
  272. /// <value>The selected block's colour.</value>
  273. public BlockColor SelectedColour
  274. {
  275. get
  276. {
  277. var optstruct = playerEngine.GetCharacterStruct<EquippedColourStruct>(Id);
  278. return optstruct ? new BlockColor(optstruct.Get().indexInPalette) : BlockColors.Default;
  279. }
  280. }
  281. /// <summary>
  282. /// The player's selected blueprint in their hand. Set to null to clear. Dispose after usage.
  283. /// </summary>
  284. public Blueprint SelectedBlueprint
  285. {
  286. get
  287. {
  288. var lbiso = playerEngine.GetPlayerStruct<LocalBlueprintInputStruct>(Id, Type);
  289. return lbiso ? new Blueprint(lbiso.Get().selectedBlueprintId) : null;
  290. }
  291. set => BlockGroup._engine.SelectBlueprint(value?.Id ?? uint.MaxValue);
  292. }
  293. /// <summary>
  294. /// The player's mode in time stopped mode, determining what they place.
  295. /// </summary>
  296. public PlayerBuildingMode BuildingMode => (PlayerBuildingMode)Math.Log((double)playerEngine
  297. .GetCharacterStruct<TimeStoppedModeComponent>(Id).Get().timeStoppedContext, 2); // It's a bit field in game now
  298. public PlayerState State =>
  299. playerEngine.GetCharacterStruct<CharacterTagEntityStruct>(Id).Get().ID.groupID switch
  300. {
  301. var group when group == CharacterExclusiveGroups.MachineSpawningGroup => PlayerState.HoldingMachine,
  302. var group when group == CharacterExclusiveGroups.OnFootGroup => PlayerState.OnFoot,
  303. var group when group == CharacterExclusiveGroups.InPilotSeatGroup => PlayerState.InSeat,
  304. var group when group == CharacterExclusiveGroups.DyingOnFootGroup => PlayerState.OnFoot,
  305. var group when group == CharacterExclusiveGroups.DyingInPilotSeatGroup => PlayerState.InSeat,
  306. var group when group == CharacterExclusiveGroups.DeadGroup => PlayerState.OnFoot,
  307. _ => throw new ArgumentOutOfRangeException("", "Unknown player state")
  308. };
  309. /// <summary>
  310. /// Whether the player is sprinting.
  311. /// </summary>
  312. public bool Sprinting
  313. {
  314. get => GameClient.IsBuildMode
  315. ? playerEngine.GetCharacterStruct<BuildingDroneMovementComponent>(Id).Get().sprinting
  316. : playerEngine.GetCharacterStruct<CharacterMovementEntityStruct>(Id).Get().isSprinting;
  317. set => _ = GameClient.IsBuildMode
  318. ? playerEngine.GetCharacterStruct<BuildingDroneMovementComponent>(Id).Get().sprinting = value
  319. : playerEngine.GetCharacterStruct<CharacterMovementEntityStruct>(Id).Get().isSprinting = value;
  320. }
  321. /// <summary>
  322. /// Movement speed setting. Build mode (camera) and simulation mode settings are separate.
  323. /// </summary>
  324. public float SpeedSetting
  325. {
  326. get => GameClient.IsBuildMode
  327. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speed
  328. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().moveSpeed;
  329. set => _ = GameClient.IsBuildMode
  330. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speed = value
  331. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().moveSpeed = value;
  332. }
  333. /// <summary>
  334. /// The multiplier setting to use when sprinting. Build mode (camera) and simulation mode settings are separate.
  335. /// </summary>
  336. public float SpeedSprintMultiplierSetting
  337. {
  338. get => GameClient.IsBuildMode
  339. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speedSprintMultiplier
  340. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().sprintSpeedMultiplier;
  341. set => _ = GameClient.IsBuildMode
  342. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().speedSprintMultiplier = value
  343. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().sprintSpeedMultiplier = value;
  344. }
  345. /// <summary>
  346. /// The acceleration setting of the player. Build mode (camera) and simulation mode settings are separate.
  347. /// </summary>
  348. public float AccelerationSetting
  349. {
  350. get => GameClient.IsBuildMode
  351. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().acceleration
  352. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().acceleration;
  353. set => _ = GameClient.IsBuildMode
  354. ? playerEngine.GetCharacterStruct<BuildingDroneMovementSettingsComponent>(Id).Get().acceleration = value
  355. : playerEngine.GetCharacterStruct<CharacterMovementSettingsEntityStruct>(Id).Get().acceleration = value;
  356. }
  357. // object methods
  358. /// <summary>
  359. /// Teleport the player to the specified coordinates.
  360. /// </summary>
  361. /// <param name="x">The x coordinate.</param>
  362. /// <param name="y">The y coordinate.</param>
  363. /// <param name="z">The z coordinate.</param>
  364. /// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
  365. /// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
  366. public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
  367. {
  368. float3 location = new float3(x, y, z);
  369. if (relative)
  370. {
  371. location += Position;
  372. }
  373. playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
  374. }
  375. /// <summary>
  376. /// Enter the given seat.
  377. /// </summary>
  378. /// <param name="seat">The seat to enter.</param>
  379. public void EnterSeat(Seat seat)
  380. {
  381. playerEngine.EnterSeat(Id, seat.Id);
  382. }
  383. /// <summary>
  384. /// Exit the seat the player is currently in.
  385. /// </summary>
  386. public void ExitSeat()
  387. {
  388. playerEngine.ExitSeat(Id);
  389. }
  390. /// <summary>
  391. /// Spawn the machine the player is holding in time running mode.
  392. /// </summary>
  393. public bool SpawnMachine()
  394. {
  395. return playerEngine.SpawnMachine(Id);
  396. }
  397. /// <summary>
  398. /// Despawn the player's machine in time running mode and place it in their hand.
  399. /// </summary>
  400. public bool DespawnMachine()
  401. {
  402. return playerEngine.DespawnMachine(Id);
  403. }
  404. /// <summary>
  405. /// Returns the block the player is currently looking at in build mode.
  406. /// </summary>
  407. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  408. /// <returns>The block or null if not found</returns>
  409. public Block GetBlockLookedAt(float maxDistance = -1f)
  410. {
  411. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  412. return egid != default && egid.groupID != CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  413. && egid.groupID != WiresGUIExclusiveGroups.WireGroup
  414. ? Block.New(egid)
  415. : null;
  416. }
  417. /// <summary>
  418. /// Returns the rigid body the player is currently looking at during simulation.
  419. /// </summary>
  420. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  421. /// <returns>The body or null if not found</returns>
  422. public SimBody GetSimBodyLookedAt(float maxDistance = -1f)
  423. {
  424. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  425. return egid != default && egid.groupID == CommonExclusiveGroups.SIMULATION_BODIES_GROUP
  426. ? EcsObjectBase.GetInstanceExisting(egid, e => new SimBody(e))
  427. : null;
  428. }
  429. /// <summary>
  430. /// Returns the wire the player is currently looking at in build mode.
  431. /// </summary>
  432. /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
  433. /// <returns>The wire or null if not found</returns>
  434. public Wire GetWireLookedAt(float maxDistance = -1f)
  435. {
  436. var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
  437. return egid != default && egid.groupID == WiresGUIExclusiveGroups.WireGroup
  438. ? EcsObjectBase.GetInstanceExisting(new EGID(egid.entityID, BuildModeWiresGroups.WiresGroup.Group),
  439. e => new Wire(e))
  440. : null;
  441. }
  442. /// <summary>
  443. /// Returns the blocks that are in the player's current selection.
  444. /// </summary>
  445. /// <returns>An array of blocks or an empty array</returns>
  446. public Block[] GetSelectedBlocks()
  447. {
  448. return playerEngine.GetSelectedBlocks(Id);
  449. }
  450. /// <summary>
  451. /// Returns the ghost block that shows the block to be placed in build mode.
  452. /// </summary>
  453. /// <returns>A block instance or null if not found</returns>
  454. public Block GetGhostBlock()
  455. {
  456. return playerEngine.GetGhostBlock(Id);
  457. }
  458. public bool Equals(Player other)
  459. {
  460. if (ReferenceEquals(null, other)) return false;
  461. if (ReferenceEquals(this, other)) return true;
  462. return Id == other.Id;
  463. }
  464. public bool Equals(EGID other)
  465. {
  466. return Id == other.entityID && other.groupID == (Type == PlayerType.Local
  467. ? PlayersExclusiveGroups.LocalPlayers
  468. : PlayersExclusiveGroups.RemotePlayers);
  469. }
  470. public override bool Equals(object obj)
  471. {
  472. if (ReferenceEquals(null, obj)) return false;
  473. if (ReferenceEquals(this, obj)) return true;
  474. if (obj.GetType() != this.GetType()) return false;
  475. return Equals((Player) obj);
  476. }
  477. public override int GetHashCode()
  478. {
  479. return (int) Id;
  480. }
  481. public override string ToString()
  482. {
  483. return $"{nameof(Type)}: {Type}, {nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(Mass)}: {Mass}";
  484. }
  485. // internal methods
  486. internal static void Init()
  487. {
  488. // TODO: Separate build mode, client and server into separate classes
  489. EngineManager.AddEngine(playerEngine, ApiEngineType.Build, ApiEngineType.PlayClient, ApiEngineType.PlayServer);
  490. EngineManager.AddEngine(playerEventsEngine, ApiEngineType.Build, ApiEngineType.PlayClient, ApiEngineType.PlayServer);
  491. }
  492. }
  493. }