TechbloxModdingAPI/GamecraftModdingAPI/Players/PlayerEngine.cs

489 lines
15 KiB
C#
Raw Normal View History

2020-05-12 00:30:16 +00:00
using System;
2020-08-13 14:12:36 +00:00
using System.Collections.Generic;
using System.Reflection;
2020-05-12 00:30:16 +00:00
using System.Runtime.CompilerServices;
using RobocraftX.Character;
using RobocraftX.Character.Movement;
using RobocraftX.Common.Players;
using RobocraftX.Common.Input;
using RobocraftX.CR.MachineEditing.BoxSelect;
2020-05-12 00:30:16 +00:00
using RobocraftX.Physics;
2020-05-30 01:30:24 +00:00
using RobocraftX.Blocks.Ghost;
using RobocraftX.Character.Camera;
using RobocraftX.Character.Factories;
using Gamecraft.CharacterVulnerability;
using Gamecraft.CharacterVulnerability.Entities;
2020-08-13 14:12:36 +00:00
using Gamecraft.GUI.HUDFeedbackBlocks;
2020-05-12 00:30:16 +00:00
using Svelto.ECS;
using Unity.Mathematics;
using Unity.Physics;
using UnityEngine;
2020-05-12 00:30:16 +00:00
using GamecraftModdingAPI.Engines;
2020-08-13 14:12:36 +00:00
using HarmonyLib;
using RobocraftX.Common;
using Svelto.ECS.DataStructures;
2020-05-12 00:30:16 +00:00
namespace GamecraftModdingAPI.Players
{
2020-05-30 01:30:24 +00:00
internal class PlayerEngine : IApiEngine, IFactoryEngine
2020-05-12 00:30:16 +00:00
{
public string Name { get; } = "GamecraftModdingAPIPlayerGameEngine";
public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
2020-05-30 01:30:24 +00:00
public IEntityFactory Factory { set; private get; }
2020-05-12 00:30:16 +00:00
private bool isReady = false;
public void Dispose()
{
isReady = false;
}
public void Ready()
{
isReady = true;
}
public uint GetLocalPlayer()
{
if (!isReady) return uint.MaxValue;
var localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
if (localPlayers.count > 0)
2020-05-12 00:30:16 +00:00
{
return localPlayers[0].ID.entityID;
}
return uint.MaxValue;
}
public uint GetRemotePlayer()
{
if (!isReady) return uint.MaxValue;
var localPlayers = entitiesDB.QueryEntities<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
if (localPlayers.count > 0)
2020-05-12 00:30:16 +00:00
{
return localPlayers[0].ID.entityID;
}
return uint.MaxValue;
}
public long GetAllPlayerCount()
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
long count = 0;
foreach (ExclusiveGroupStruct eg in PlayersExclusiveGroups.AllPlayers)
{
count += entitiesDB.Count<PlayerIDStruct>(eg);
}
return count;
}
public long GetLocalPlayerCount()
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.LocalPlayers);
}
public long GetRemotePlayerCount()
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
return entitiesDB.Count<PlayerIDStruct>(PlayersExclusiveGroups.RemotePlayers);
}
2020-05-12 00:30:16 +00:00
public bool ExistsById(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
return entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.LocalPlayers)
|| entitiesDB.Exists<PlayerIDStruct>(playerId, PlayersExclusiveGroups.RemotePlayers);
2020-05-12 00:30:16 +00:00
}
public float3 GetLocation(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return float3.zero;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
return rbes.position;
}
return float3.zero;
}
public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
var characterGroups = CharacterExclusiveGroups.AllCharacters;
for (int i = 0; i < characterGroups.count; i++)
2020-05-12 00:30:16 +00:00
{
EGID egid = new EGID(playerId, characterGroups[i]);
if (entitiesDB.Exists<RigidBodyEntityStruct>(egid))
{
ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid);
if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
{
entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
}
rbes.position = location;
return true;
}
}
return false;
}
public float3 GetRotation(uint playerId)
2020-05-12 00:30:16 +00:00
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return float3.zero;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
{
return ((Quaternion) rbes.rotation).eulerAngles;
}
return default(float3);
2020-05-12 00:30:16 +00:00
}
public bool SetRotation(uint playerId, float3 value)
2020-05-12 00:30:16 +00:00
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
Quaternion q = rbes.rotation;
q.eulerAngles = value;
rbes.rotation = q;
2020-05-12 00:30:16 +00:00
return true;
}
return false;
}
public float3 GetLinearVelocity(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return float3.zero;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
return rbes.velocity;
}
return float3.zero;
}
public bool SetLinearVelocity(uint playerId, float3 value)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
rbes.velocity = value;
return true;
}
return false;
}
public float3 GetAngularVelocity(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return float3.zero;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
return rbes.angularVelocity;
}
return float3.zero;
}
public bool SetAngularVelocity(uint playerId, float3 value)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
rbes.angularVelocity = value;
return true;
}
return false;
}
public PhysicsMass GetMass(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return default(PhysicsMass);
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
return rbes.physicsMass;
}
return default(PhysicsMass);
2020-05-12 00:30:16 +00:00
}
public bool SetInverseMass(uint playerId, float inverseMass)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var rbes = ref GetCharacterStruct<RigidBodyEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-12 00:30:16 +00:00
{
rbes.physicsMass.InverseInertia = inverseMass;
return true;
}
return false;
}
public float? GetLastPingTime(uint playerId, PlayerType type)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return null;
2020-05-30 01:30:24 +00:00
EGID egid = new EGID(playerId, PlayerGroupFromEnum(type));
2020-05-12 00:30:16 +00:00
if (entitiesDB.Exists<PlayerNetworkStatsEntityStruct>(egid))
{
return entitiesDB.QueryEntity<PlayerNetworkStatsEntityStruct>(egid).lastPingTimeSinceLevelLoad;
}
return null;
}
2020-05-30 01:30:24 +00:00
public float GetInitialHealth(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.initialHealth;
}
return -1f;
}
public bool SetInitialHealth(uint playerId, float val)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var c = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
c.initialHealth = val;
return true;
}
return false;
}
public float GetCurrentHealth(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.currentHealth;
}
return -1f;
}
public bool SetCurrentHealth(uint playerId, float val)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var c = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
c.currentHealth = val;
return true;
}
return false;
}
public bool DamagePlayer(uint playerId, float amount)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
2020-05-30 01:30:24 +00:00
Factory.BuildEntity<DamageEntityDescriptor>(
new EGID(CharacterVulnerabilityExclusiveGroups.NextDamageEntityId, CharacterVulnerabilityExclusiveGroups.CharacterDamageExclusiveGroup)
).Init(new DamageEntityStruct
{
damage = amount,
targetPlayerEntityId = playerId,
});
return true;
}
public bool GetDamageable(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var c = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.canTakeDamageStat;
}
return false;
}
public bool SetDamageable(uint playerId, bool val)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var ches = ref GetCharacterStruct<CharacterHealthEntityStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
ches.canTakeDamage = val;
ches.canTakeDamage = val;
return true;
}
return false;
}
public uint GetInitialLives(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.initialLives;
}
return uint.MaxValue;
}
public bool SetInitialLives(uint playerId, uint val)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var c = ref GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
c.initialLives = val;
return true;
}
return false;
}
public uint GetCurrentLives(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.currentLives;
}
return uint.MaxValue;
}
public bool SetCurrentLives(uint playerId, uint val)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
ref var c = ref GetCharacterStruct<CharacterLivesEntityComponent>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
c.currentLives = val;
return true;
}
return false;
}
public bool GetGameOverScreen(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return false;
2020-08-13 14:12:36 +00:00
ref HudActivatedBlocksEntityStruct habes = ref entitiesDB.QueryEntity<HudActivatedBlocksEntityStruct>(HUDFeedbackBlocksGUIExclusiveGroups.GameOverHudEgid);
NativeDynamicArrayCast<EGID> nativeDynamicArrayCast = new NativeDynamicArrayCast<EGID>(habes.activatedBlocksOrdered);
return nativeDynamicArrayCast.count > 0;
2020-05-30 01:30:24 +00:00
}
2020-08-13 14:12:36 +00:00
public bool IsDead(uint playerId)
2020-05-30 01:30:24 +00:00
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return true;
return entitiesDB.Exists<RigidBodyEntityStruct>(playerId, CharacterExclusiveGroups.DeadCharacters);
2020-05-30 01:30:24 +00:00
}
public int GetSelectedBlock(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<EquippedPartStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.SelectedDBPartID;
}
return ushort.MaxValue;
}
public byte GetSelectedColor(uint playerId)
{
2020-07-01 17:43:56 +00:00
if (entitiesDB == null) return 0;
ref var c = ref GetCharacterStruct<EquippedColourStruct>(playerId, out bool exists);
if (exists)
2020-05-30 01:30:24 +00:00
{
return c.indexInPalette;
}
return 255;
}
2020-05-12 00:30:16 +00:00
// reusable methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2020-05-30 01:30:24 +00:00
private ExclusiveGroup PlayerGroupFromEnum(PlayerType type)
2020-05-12 00:30:16 +00:00
{
return type == PlayerType.Local ? PlayersExclusiveGroups.LocalPlayers : PlayersExclusiveGroups.RemotePlayers;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetCharacterStruct<T>(uint playerId, out bool exists) where T : unmanaged, IEntityComponent
2020-05-12 00:30:16 +00:00
{
var characterGroups = CharacterExclusiveGroups.AllCharacters;
for (int i = 0; i < characterGroups.count; i++)
2020-05-12 00:30:16 +00:00
{
EGID egid = new EGID(playerId, characterGroups[i]);
if (entitiesDB.Exists<T>(egid))
{
exists = true;
return ref entitiesDB.QueryEntity<T>(egid);
2020-05-12 00:30:16 +00:00
}
}
exists = false;
T[] arr = new T[1];
return ref arr[0]; //Return default value
2020-05-12 00:30:16 +00:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetPlayerStruct<T>(uint playerId, out T s) where T : unmanaged, IEntityComponent
{
var playerGroups = PlayersExclusiveGroups.AllPlayers;
for (int i = 0; i < playerGroups.count; i++)
2020-05-12 00:30:16 +00:00
{
EGID egid = new EGID(playerId, playerGroups[i]);
if (entitiesDB.Exists<T>(egid))
{
s = entitiesDB.QueryEntity<T>(egid);
return true;
}
}
s = default;
return false;
}
2020-05-22 22:06:49 +00:00
public EGID? GetThingLookedAt(uint playerId, float maxDistance = -1f)
{
if (!entitiesDB.TryQueryMappedEntities<CharacterCameraRayCastEntityStruct>(
CameraExclusiveGroups.CameraGroup, out var mapper))
return null;
mapper.TryGetEntity(playerId, out CharacterCameraRayCastEntityStruct rayCast);
float distance = maxDistance < 0
? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast,
GhostBlockUtils.GhostCastMethod.GhostCastProportionalToBlockSize)
: maxDistance;
if (rayCast.hit && rayCast.distance <= distance)
2020-05-22 22:06:49 +00:00
return rayCast.hitEgid;
return null;
}
public unsafe Block[] GetSelectedBlocks(uint playerid)
{
if (!entitiesDB.Exists<BoxSelectStateEntityStruct>(playerid,
BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup))
return new Block[0];
var state = entitiesDB.QueryEntity<BoxSelectStateEntityStruct>(playerid,
BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
var blocks = entitiesDB.QueryEntity<SelectedBlocksStruct>(playerid,
BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup);
if (!state.active) return new Block[0];
var pointer = (EGID*) blocks.selectedBlocks.ToPointer();
var ret = new Block[blocks.count];
for (int j = 0; j < blocks.count; j++)
{
var egid = pointer[j];
ret[j] = new Block(egid);
}
return ret;
}
2020-05-12 00:30:16 +00:00
}
}