Skip to content

Commit

Permalink
added some extra methods to IBuildable
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielWillett committed Oct 3, 2024
1 parent ce0daac commit 18d2b79
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 52 deletions.
168 changes: 155 additions & 13 deletions UncreatedWarfare/Buildables/IBuildable.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,97 @@
using System;
using System.Runtime.CompilerServices;
using DanielWillett.ReflectionTools;
using Uncreated.Warfare.Util;

namespace Uncreated.Warfare.Buildables;

/// <summary>
/// Any structure or barricade. Can be <see cref="BuildableBarricade"/> or <see cref="BuildableStructure"/>.
/// </summary>
[CannotApplyEqualityOperator]
public interface IBuildable : IEquatable<IBuildable>
public interface IBuildable : IEquatable<IBuildable>, ITransformObject
{
/// <summary>
/// The instance ID of this buildable specific to it's buildable type.
/// </summary>
uint InstanceId { get; }

/// <summary>
/// If the buildable is a structure instead of a barricade.
/// </summary>
bool IsStructure { get; }

/// <summary>
/// If the buildable has been destroyed.
/// </summary>
bool IsDead { get; }

/// <summary>
/// The asset for the buildable.
/// </summary>
ItemPlaceableAsset Asset { get; }

/// <summary>
/// The transform of the buildable.
/// </summary>
Transform Model { get; }
ulong Owner { get; }
ulong Group { get; }

/// <summary>
/// The player that placed the buildable.
/// </summary>
CSteamID Owner { get; }

/// <summary>
/// The group of the player that placed the buildable.
/// </summary>
CSteamID Group { get; }

/// <summary>
/// The <see cref="BarricadeDrop"/> or <see cref="StructureDrop"/> of the buildable.
/// </summary>
object Drop { get; }

/// <summary>
/// The <see cref="BarricadeData"/> or <see cref="StructureData"/> of the buildable.
/// </summary>
object Data { get; }

/// <summary>
/// The <see cref="Barricade"/> or <see cref="Structure"/> of the buildable.
/// </summary>
object Item { get; }

/// <summary>
/// The ID used to refer to networked objects.
/// </summary>
NetId NetId { get; }
Vector3 Position { get; }
Quaternion Rotation { get; }

/// <summary>
/// Get <see cref="Drop"/> as either a <see cref="BarricadeDrop"/> or <see cref="StructureDrop"/>.
/// </summary>
/// <exception cref="InvalidCastException"/>
TDrop GetDrop<TDrop>() where TDrop : class
{
return Drop as TDrop ?? throw new InvalidCastException($"This buildable's drop is not a {Accessor.ExceptionFormatter.Format<TDrop>()}.");
}

/// <summary>
/// Get <see cref="Data"/> as either a <see cref="BarricadeData"/> or <see cref="StructureData"/>.
/// </summary>
/// <exception cref="InvalidCastException"/>
TData GetData<TData>() where TData : class
{
return Data as TData ?? throw new InvalidCastException($"This buildable's data is not a {Accessor.ExceptionFormatter.Format<TData>()}.");
}

/// <summary>
/// Get <see cref="Item"/> as either a <see cref="Barricade"/> or <see cref="Structure"/>.
/// </summary>
/// <exception cref="InvalidCastException"/>
TData GetItem<TData>() where TData : class
{
return Item as TData ?? throw new InvalidCastException($"This buildable's item is not a {Accessor.ExceptionFormatter.Format<TData>()}.");
}
}

[CannotApplyEqualityOperator]
Expand All @@ -27,11 +102,32 @@ public class BuildableBarricade : IBuildable, IEquatable<BuildableBarricade>, IE
public bool IsDead => Data.barricade.isDead;
public ItemPlaceableAsset Asset => Drop.asset;
public Transform Model => Drop.model == null || Data.barricade.isDead ? null! : Drop.model; // so you can use ? on it
public ulong Owner => Data.owner;
public ulong Group => Data.group;
public CSteamID Owner => Unsafe.As<ulong, CSteamID>(ref Data.owner);
public CSteamID Group => Unsafe.As<ulong, CSteamID>(ref Data.group);
public NetId NetId => Drop.GetNetId();
public Vector3 Position => Data.point;
public Quaternion Rotation => Data.rotation;
public Vector3 Position
{
get => Data.point;
set
{
GameThread.AssertCurrent();

BarricadeManager.ServerSetBarricadeTransform(Drop.model, value, Data.rotation);
}
}

public Quaternion Rotation
{
get => Data.rotation;
set
{
GameThread.AssertCurrent();

BarricadeManager.ServerSetBarricadeTransform(Drop.model, Data.point, value);
}
}


public BarricadeDrop Drop { get; }
public BarricadeData Data { get; }
public BuildableBarricade(BarricadeDrop drop)
Expand All @@ -40,8 +136,21 @@ public BuildableBarricade(BarricadeDrop drop)
Data = drop.GetServersideData();
}

public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
{
GameThread.AssertCurrent();

BarricadeManager.ServerSetBarricadeTransform(Drop.model, position, rotation);
}

object IBuildable.Drop => Drop;
object IBuildable.Data => Data;
object IBuildable.Item => Data.barricade;
Vector3 ITransformObject.Scale
{
get => Vector3.one;
set => throw new NotSupportedException();
}

public bool Equals(BarricadeDrop? other) => other is not null && other.instanceID == Drop.instanceID;
public bool Equals(BuildableBarricade? other) => other is not null && other.Drop.instanceID == Drop.instanceID;
Expand All @@ -58,21 +167,54 @@ public class BuildableStructure : IBuildable, IEquatable<BuildableStructure>, IE
public bool IsDead => Data.structure.isDead;
public ItemPlaceableAsset Asset => Drop.asset;
public Transform Model => Drop.model == null || Data.structure.isDead ? null! : Drop.model; // so you can use ? on it
public ulong Owner => Data.owner;
public ulong Group => Data.group;
public CSteamID Owner => Unsafe.As<ulong, CSteamID>(ref Data.owner);
public CSteamID Group => Unsafe.As<ulong, CSteamID>(ref Data.group);
public NetId NetId => Drop.GetNetId();
public Vector3 Position => Data.point;
public Quaternion Rotation => Data.rotation;
public Vector3 Position
{
get => Data.point;
set
{
GameThread.AssertCurrent();

StructureManager.ServerSetStructureTransform(Drop.model, value, Data.rotation);
}
}

public Quaternion Rotation
{
get => Data.rotation;
set
{
GameThread.AssertCurrent();

StructureManager.ServerSetStructureTransform(Drop.model, Data.point, value);
}
}
public StructureDrop Drop { get; }
public StructureData Data { get; }
public Structure Structure => Data.structure;
public BuildableStructure(StructureDrop drop)
{
Drop = drop;
Data = drop.GetServersideData();
}

public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
{
GameThread.AssertCurrent();

StructureManager.ServerSetStructureTransform(Drop.model, position, rotation);
}

object IBuildable.Drop => Drop;
object IBuildable.Data => Data;
object IBuildable.Item => Data.structure;
Vector3 ITransformObject.Scale
{
get => Vector3.one;
set => throw new NotSupportedException();
}

public bool Equals(StructureDrop? other) => other is not null && other.instanceID == Drop.instanceID;
public bool Equals(BuildableStructure? other) => other is not null && other.Drop.instanceID == Drop.instanceID;
Expand Down
3 changes: 0 additions & 3 deletions UncreatedWarfare/FOBs/FOB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ object ICloneable.Clone()

IReadOnlyList<InteractableVehicle> ITrackingProximity<InteractableVehicle>.ActiveObjects => Vehicles;

Matrix4x4 ITransformObject.WorldToLocal => transform.worldToLocalMatrix;

public Matrix4x4 LocalToWorld => throw new NotImplementedException();

public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
Expand All @@ -266,7 +264,6 @@ public void SetPositionAndRotation(Vector3 position, Quaternion rotation)

public Matrix4x4 WorldToLocal => throw new NotImplementedException();

Matrix4x4 ITransformObject.LocalToWorld => transform.localToWorldMatrix;
BoundingSphere ISphereProximity.Sphere
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class FlagActionPhaseLayout : IFlagRotationPhase
{
private readonly ILogger<FlagActionPhaseLayout> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IPlayerService _playerService;
private IList<Zone>? _pathingResult;
private ZoneStore _zoneStore;

Expand All @@ -39,11 +38,10 @@ public class FlagActionPhaseLayout : IFlagRotationPhase
/// <inheritdoc />
public ActiveZoneCluster EndingTeam { get; private set; }

public FlagActionPhaseLayout(ILogger<FlagActionPhaseLayout> logger, IServiceProvider serviceProvider, IPlayerService playerService, IConfiguration config)
public FlagActionPhaseLayout(ILogger<FlagActionPhaseLayout> logger, IServiceProvider serviceProvider, IConfiguration config)
{
_logger = logger;
_serviceProvider = serviceProvider;
_playerService = playerService;
Configuration = config;
}

Expand Down
6 changes: 0 additions & 6 deletions UncreatedWarfare/Players/WarfarePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public Vector3 Position
}
}

/// <inheritdoc />
public Matrix4x4 WorldToLocal => Transform.worldToLocalMatrix;

/// <inheritdoc />
public Matrix4x4 LocalToWorld => Transform.localToWorldMatrix;

/// <summary>
/// If the player this object represents is currently online. Set to <see langword="false"/> *after* the leave event is fired.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions UncreatedWarfare/Plugins/WarfarePluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ internal void ConfigureServices(ContainerBuilder bldr)

bldr.RegisterInstance(plugin)
.As<WarfarePlugin>()
.Named<WarfarePluginConfiguration>(plugin.AssemblyName.Name)
.Named<WarfarePlugin>(plugin.AssemblyName.Name)
.OwnedByLifetimeScope();
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ private object[] InjectParameters(ParameterInfo[] parameters, Type type, Contain

int index = Array.FindIndex(ctors, x => x.IsDefinedSafe<ActivatorUtilitiesConstructorAttribute>());

if (index < 0)
if (index >= 0)
{
ConstructorInfo expectedCtor = ctors[index];
expectedParameters = expectedCtor.GetParameters();
Expand Down
11 changes: 3 additions & 8 deletions UncreatedWarfare/UncreatedWarfare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,11 @@
</Using>
</ItemGroup>

<!-- Installation -->
<PropertyGroup>
<U3DSPath>C:/SteamCMD/steamapps/common/U3DS</U3DSPath>
</PropertyGroup>

<!-- Unturned Files -->
<Choose>
<When Condition="exists('$(U3DSPath)/Unturned_Data/Managed/Assembly-CSharp.dll')">
<When Condition="exists('$(ServerPath)/Unturned_Data/Managed/Assembly-CSharp.dll')">
<PropertyGroup>
<UnturnedDllPath>$(U3DSPath)/Unturned_Data/Managed</UnturnedDllPath>
<UnturnedDllPath>$(ServerPath)/Unturned_Data/Managed</UnturnedDllPath>
</PropertyGroup>
</When>
<Otherwise>
Expand Down Expand Up @@ -310,7 +305,7 @@
<Folder Include="Commands\VehicleBay\" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="exists('$(U3DSPath)/Unturned_Data/Managed/Assembly-CSharp.dll')">
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="exists('$(ServerPath)/Unturned_Data/Managed/Assembly-CSharp.dll')">
<Exec Command="XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),Assembly-CSharp.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,Assembly-CSharp.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnturnedDat.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnturnedDat.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEx.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEx.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),SDG.NetTransport.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,SDG.NetTransport.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),SDG.NetPak.Runtime.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,SDG.NetPak.Runtime.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),com.rlabrecque.steamworks.net.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,com.rlabrecque.steamworks.net.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.CoreModule.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.CoreModule.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.ImageConversionModule.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.ImageConversionModule.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.PhysicsModule.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.PhysicsModule.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.UnityWebRequestModule.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.UnityWebRequestModule.dll))*&quot;&#xD;&#xA;XCOPY /v /y &quot;$([System.IO.Path]::Combine($(UnturnedDllPath),UnityEngine.AssetBundleModule.dll))*&quot; &quot;$([System.IO.Path]::Combine(../Libraries,UnityEngine.AssetBundleModule.dll))*&quot;" />
</Target>
</Project>
10 changes: 0 additions & 10 deletions UncreatedWarfare/Util/Transform/ITransformObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public interface ITransformObject
/// <exception cref="NotSupportedException">Not able to set scale.</exception>
Vector3 Scale { get; set; }

/// <summary>
/// A matrix transforming from world space to local space.
/// </summary>
Matrix4x4 WorldToLocal { get; }

/// <summary>
/// A matrix transforming from local space to world space.
/// </summary>
Matrix4x4 LocalToWorld { get; }

/// <summary>
/// Set the position and rotation at the same time.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion UncreatedWarfare/Vehicles/VehicleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async UniTask<InteractableVehicle> SpawnVehicleAsync(VehicleSpawnInfo spa

Vector3 spawnPosition = spawner.Position + Vector3.up * VehicleSpawnOffset;

InteractableVehicle vehicle = await SpawnVehicleAsync(spawn.Vehicle, spawnPosition, spawnRotation, group: new CSteamID(spawner.Group), token: token);
InteractableVehicle vehicle = await SpawnVehicleAsync(spawn.Vehicle, spawnPosition, spawnRotation, group: spawner.Group, token: token);
await UniTask.SwitchToMainThread(token);

spawn.LinkVehicle(vehicle);
Expand Down
7 changes: 1 addition & 6 deletions UncreatedWarfare/Zones/ActiveZoneCluster.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Players.Management;
using Uncreated.Warfare.Proximity;
using Uncreated.Warfare.Util;
using Uncreated.Warfare.Util.List;
Expand All @@ -17,7 +15,6 @@ namespace Uncreated.Warfare.Zones;
public class ActiveZoneCluster : IDisposable
{
private readonly ZoneProximity[] _zones;
private readonly IPlayerService _playerService;
private bool _disposed;
private readonly TrackingList<WarfarePlayer> _players = new TrackingList<WarfarePlayer>(8);

Expand Down Expand Up @@ -64,8 +61,6 @@ internal ActiveZoneCluster(ZoneProximity[] zones, IServiceProvider serviceProvid

Players = new ReadOnlyTrackingList<WarfarePlayer>(_players);

_playerService = serviceProvider.GetRequiredService<IPlayerService>();

// move primary to the front of the array
for (int i = 1; i < zones.Length; ++i)
{
Expand Down

0 comments on commit 18d2b79

Please sign in to comment.