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.

539 lines
19KB

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