Compare commits

..

4 Commits

Author SHA1 Message Date
DrSmugleaf
190c35dbdf Version: 240.1.3-source-gen-debug 2025-01-21 19:11:31 -08:00
DrSmugleaf
b52c26d0eb Merge branch 'master' of https://github.com/space-wizards/RobustToolbox into serialization-source-gen-debug 2025-01-21 19:11:05 -08:00
DrSmugleaf
35c983d2fd Version: 240.1.2-source-gen-debug 2025-01-21 18:47:40 -08:00
DrSmugleaf
88cfd04f01 Source gen changes 2025-01-21 18:47:25 -08:00
68 changed files with 879 additions and 2537 deletions

View File

@@ -1,4 +1,4 @@
<Project>
<!-- This file automatically reset by Tools/version.py -->
<!-- This file automatically reset by Tools/version.py -->

View File

@@ -54,86 +54,10 @@ END TEMPLATE-->
*None yet*
## 243.0.1
### Bugfixes
* Fixed `BaseWindow` sometimes not properly updating the mouse cursor shape.
* Revert `BaseWindow` OnClose ordering due to prior reliance upon the ordering.
## 240.1.3-source-gen-debug
## 243.0.0
### Breaking changes
* RemoveChild is called after OnClose for BaseWindow.
### New features
* BUIs now have their positions saved when closed and re-used when opened when using the `CreateWindow<T>` helper or via manually registering it via RegisterControl.
### Other
* Ensure grid fixtures get updated in client state handling even if exceptions occur.
## 242.0.1
### Bugfixes
* Fixed prototype reloading/hotloading not properly handling data-fields with the `AlwaysPushInheritanceAttribute`
* Fix the pooled polygons using incorrect vertices for EntityLookup and MapManager.
### Internal
* Avoid normalizing angles constructed from vectors.
## 242.0.0
### Breaking changes
* The order in which the client initialises networked entities has changed. It will now always apply component states, initialise, and start an entity's parent before processing any children. This might break anything that was relying on the old behaviour where all component states were applied before any entities were initialised & started.
* `IClydeViewport` overlay rendering methods now take in an `IRenderHandle` instead of a world/screen handle.
* The `OverlayDrawArgs` struct now has an internal constructor.
### New features
* Controls can now be manually restyled via `Control.InvalidateStyleSheet()` and `Control.DoStyleUpdate()`
* Added `IUserInterfaceManager.RenderControl()` for manually drawing controls.
* `OverlayDrawArgs` struct now has an `IRenderHandle` field such that overlays can use the new `RenderControl()` methods.
* TileSpawnWindow will now take focus when opened.
### Bugfixes
* Fixed a client-side bug where `TransformComponent.GridUid` does not get set properly when an existing entity is attached to a new entity outside of the player's PVS range.
* EntityPrototypeView will only create entities when it's on the UI tree and not when the prototype is set.
* Make CollisionWake not log errors if it can't resolve.
### Other
* Replace IPhysShape API with generics on IMapManager and EntityLookupSystem.
### Internal
* Significantly reduce allocations for Box2 / Box2Rotated queries.
## 241.0.0
### Breaking changes
* Remove DeferredClose from BUIs.
### New features
* Added `EntityManager.DirtyFields()`, which allows components with delta states to simultaneously mark several fields as dirty at the same time.
* Add `CloserUserUIs<T>` to close keys of a specific key.
### Bugfixes
* Fixed `RaisePredictiveEvent()` not properly re-raising events during prediction for event handlers that did not take an `EntitySessionEventArgs` argument.
* BUI openings are now deferred to avoid having slight desync between deferred closes and opens occurring in the same tick.
## 240.1.2-source-gen-debug
## 240.1.2

View File

@@ -101,33 +101,11 @@ namespace Robust.Client.GameObjects
/// <inheritdoc />
public override void Dirty<T>(Entity<T> ent, MetaDataComponent? meta = null)
{
// Client only dirties during prediction
// Client only dirties during prediction
if (_gameTiming.InPrediction)
base.Dirty(ent, meta);
}
public override void DirtyField<T>(EntityUid uid, T comp, string fieldName, MetaDataComponent? metadata = null)
{
// TODO Prediction
// does the client actually need to dirty the field?
// I.e., can't it just dirty the whole component to trigger a reset?
// Client only dirties during prediction
if (_gameTiming.InPrediction)
base.DirtyField(uid, comp, fieldName, metadata);
}
public override void DirtyFields<T>(EntityUid uid, T comp, MetaDataComponent? meta, params ReadOnlySpan<string> fields)
{
// TODO Prediction
// does the client actually need to dirty the field?
// I.e., can't it just dirty the whole component to trigger a reset?
// Client only dirties during prediction
if (_gameTiming.InPrediction)
base.DirtyFields(uid, comp, meta, fields);
}
/// <inheritdoc />
public override void Dirty<T1, T2>(Entity<T1, T2> ent, MetaDataComponent? meta = null)
{

View File

@@ -1,18 +1,10 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Robust.Client.UserInterface;
using Robust.Shared.GameObjects;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Robust.Client.GameObjects;
public sealed class UserInterfaceSystem : SharedUserInterfaceSystem
{
private Dictionary<EntityUid, Dictionary<Enum, Vector2>> _savedPositions = new();
private Dictionary<BoundUserInterface, Control> _registeredControls = new();
public override void Initialize()
{
base.Initialize();
@@ -25,59 +17,6 @@ public sealed class UserInterfaceSystem : SharedUserInterfaceSystem
ProtoManager.PrototypesReloaded -= OnProtoReload;
}
protected override void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
{
base.OnUserInterfaceShutdown(ent, ref args);
_savedPositions.Remove(ent.Owner);
}
/// <inheritdoc />
public override void OpenUi(Entity<UserInterfaceComponent?> entity, Enum key, bool predicted = false)
{
var player = Player.LocalEntity;
if (player == null)
return;
OpenUi(entity, key, player.Value, predicted);
}
protected override void SavePosition(BoundUserInterface bui)
{
if (!_registeredControls.Remove(bui, out var control))
return;
var keyed = _savedPositions[bui.Owner];
keyed[bui.UiKey] = control.Position;
}
/// <summary>
/// Registers a control so it will later have its position stored by <see cref="SavePosition"/> when the BUI is closed.
/// </summary>
public void RegisterControl(BoundUserInterface bui, Control control)
{
DebugTools.Assert(!_registeredControls.ContainsKey(bui));
_registeredControls[bui] = control;
_savedPositions.GetOrNew(bui.Owner);
}
public override bool TryGetPosition(Entity<UserInterfaceComponent?> entity, Enum key, out Vector2 position)
{
position = default;
if (!_savedPositions.TryGetValue(entity.Owner, out var keyed))
{
return false;
}
if (!keyed.TryGetValue(key, out position))
{
return false;
}
return true;
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
var player = Player.LocalEntity;

View File

@@ -23,6 +23,7 @@ using Robust.Shared.Input;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Profiling;
@@ -41,13 +42,13 @@ namespace Robust.Client.GameStates
private uint _nextInputCmdSeq = 1;
private readonly Queue<FullInputCmdMessage> _pendingInputs = new();
private readonly Queue<(uint sequence, GameTick sourceTick, object msg, object sessionMsg)>
private readonly Queue<(uint sequence, GameTick sourceTick, EntityEventArgs msg, object sessionMsg)>
_pendingSystemMessages
= new();
// Game state dictionaries that get used every tick.
private readonly Dictionary<EntityUid, StateData> _toApply = new();
private StateData[] _toApplySorted = default!;
private readonly Dictionary<EntityUid, (NetEntity NetEntity, MetaDataComponent Meta, bool EnteringPvs, GameTick LastApplied, EntityState? curState, EntityState? nextState)> _toApply = new();
private readonly Dictionary<NetEntity, EntityState> _toCreate = new();
private readonly Dictionary<ushort, (IComponent Component, IComponentState? curState, IComponentState? nextState)> _compStateWork = new();
private readonly Dictionary<EntityUid, HashSet<Type>> _pendingReapplyNetStates = new();
private readonly HashSet<NetEntity> _stateEnts = new();
@@ -55,29 +56,15 @@ namespace Robust.Client.GameStates
private readonly List<IComponent> _toRemove = new();
private readonly Dictionary<NetEntity, Dictionary<ushort, IComponentState?>> _outputData = new();
private readonly List<(EntityUid, TransformComponent)> _queuedBroadphaseUpdates = new();
private readonly HashSet<EntityUid> _sorted = new();
private readonly List<NetEntity> _created = new();
private readonly List<NetEntity> _detached = new();
private readonly record struct StateData(
EntityUid Uid,
NetEntity NetEntity,
MetaDataComponent Meta,
bool Created,
bool EnteringPvs,
GameTick LastApplied,
EntityState? CurState,
EntityState? NextState,
HashSet<Type>? PendingReapply);
private readonly ObjectPool<Dictionary<ushort, IComponentState?>> _compDataPool =
new DefaultObjectPool<Dictionary<ushort, IComponentState?>>(new DictPolicy<ushort, IComponentState?>(), 256);
private uint _metaCompNetId;
private uint _xformCompNetId;
[Dependency] private readonly IReplayRecordingManager _replayRecording = default!;
[Dependency] private readonly IComponentFactory _compFactory = default!;
[Dependency] private readonly IClientEntityManagerInternal _entities = default!;
[Dependency] private readonly IPlayerManager _players = default!;
[Dependency] private readonly IClientNetManager _network = default!;
[Dependency] private readonly IBaseClient _client = default!;
@@ -85,7 +72,7 @@ namespace Robust.Client.GameStates
[Dependency] private readonly INetConfigurationManager _config = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IConsoleHost _conHost = default!;
[Dependency] private readonly ClientEntityManager _entities = default!;
[Dependency] private readonly ClientEntityManager _entityManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly ProfManager _prof = default!;
[Dependency] private readonly IRuntimeLog _runtimeLog = default!;
@@ -139,6 +126,7 @@ namespace Robust.Client.GameStates
private bool _resettingPredictedEntities;
private readonly List<EntityUid> _brokenEnts = new();
private readonly List<(EntityUid, NetEntity)> _toStart = new();
/// <inheritdoc />
public void Initialize()
@@ -184,12 +172,6 @@ namespace Robust.Client.GameStates
throw new InvalidOperationException("MetaDataComponent does not have a NetId.");
_metaCompNetId = metaId.Value;
var xformId = _compFactory.GetRegistration(typeof(TransformComponent)).NetID;
if (!xformId.HasValue)
throw new InvalidOperationException("TransformComponent does not have a NetId.");
_xformCompNetId = xformId.Value;
}
private void OnComponentAdded(AddedComponentEventArgs args)
@@ -201,11 +183,11 @@ namespace Robust.Client.GameStates
if (comp.NetID == null)
return;
if (_entities.IsClientSide(args.BaseArgs.Owner))
if (_entityManager.IsClientSide(args.BaseArgs.Owner))
return;
_sawmill.Error($"""
Added component {comp.Name} to entity {_entities.ToPrettyString(args.BaseArgs.Owner)} while resetting predicted entities.
Added component {comp.Name} to entity {_entityManager.ToPrettyString(args.BaseArgs.Owner)} while resetting predicted entities.
Stack trace:
{Environment.StackTrace}
""");
@@ -403,7 +385,7 @@ namespace Robust.Client.GameStates
try
{
#endif
ApplyGameState(curState, nextState);
createdEntities = ApplyGameState(curState, nextState);
#if EXCEPTION_TOLERANCE
}
catch (MissingMetadataException e)
@@ -417,7 +399,7 @@ namespace Robust.Client.GameStates
using (_prof.Group("MergeImplicitData"))
{
MergeImplicitData();
GenerateImplicitStates(createdEntities);
}
if (_lastProcessedInput < curState.LastProcessedInput)
@@ -474,7 +456,7 @@ namespace Robust.Client.GameStates
using (_prof.Group("Tick"))
{
_entities.TickUpdate((float) _timing.TickPeriod.TotalSeconds, noPredictions: !IsPredictionEnabled, histogram: null);
_entities.TickUpdate((float) _timing.TickPeriod.TotalSeconds, noPredictions: !IsPredictionEnabled);
}
}
@@ -522,7 +504,9 @@ namespace Robust.Client.GameStates
while (hasPendingMessage && pendingMessagesEnumerator.Current.sourceTick <= _timing.CurTick)
{
_entities.EventBus.RaiseEvent(EventSource.Local, pendingMessagesEnumerator.Current.msg);
var msg = pendingMessagesEnumerator.Current.msg;
_entities.EventBus.RaiseEvent(EventSource.Local, msg);
_entities.EventBus.RaiseEvent(EventSource.Local, pendingMessagesEnumerator.Current.sessionMsg);
hasPendingMessage = pendingMessagesEnumerator.MoveNext();
}
@@ -561,7 +545,7 @@ namespace Robust.Client.GameStates
PredictionNeedsResetting = false;
var countReset = 0;
var system = _entitySystemManager.GetEntitySystem<ClientDirtySystem>();
var metaQuery = _entities.GetEntityQuery<MetaDataComponent>();
var metaQuery = _entityManager.GetEntityQuery<MetaDataComponent>();
RemQueue<IComponent> toRemove = new();
foreach (var entity in system.DirtyEntities)
@@ -648,7 +632,7 @@ namespace Robust.Client.GameStates
if (!last.TryGetValue(netId, out var state))
continue;
var comp = _entities.AddComponent(entity, netId, meta);
var comp = _entityManager.AddComponent(entity, netId, meta);
if (_sawmill.Level <= LogLevel.Debug)
_sawmill.Debug($" A component was removed: {comp.GetType()}");
@@ -668,7 +652,7 @@ namespace Robust.Client.GameStates
meta.EntityLastModifiedTick = _timing.LastRealTick;
}
_entities.System<PhysicsSystem>().ResetContacts();
_entityManager.System<PhysicsSystem>().ResetContacts();
// TODO maybe reset more of physics?
// E.g., warm impulses for warm starting?
@@ -687,21 +671,21 @@ namespace Robust.Client.GameStates
/// initial server state for any newly created entity. It does this by simply using the standard <see
/// cref="IEntityManager.GetComponentState"/>.
/// </remarks>
public void MergeImplicitData()
public void GenerateImplicitStates(IEnumerable<NetEntity> createdEntities)
{
var bus = _entities.EventBus;
var bus = _entityManager.EventBus;
foreach (var netEntity in _created)
foreach (var netEntity in createdEntities)
{
if (!_entities.TryGetEntityData(netEntity, out _, out var meta))
#if EXCEPTION_TOLERANCE
if (!_entityManager.TryGetEntityData(netEntity, out _, out var meta))
{
_sawmill.Error($"Encountered deleted entity while merging implicit data! NetEntity: {netEntity}");
#if !EXCEPTION_TOLERANCE
throw new KeyNotFoundException();
#endif
continue;
}
#else
var (_, meta) = _entityManager.GetEntityData(netEntity);
#endif
var compData = _compDataPool.Get();
_outputData.Add(netEntity, compData);
@@ -710,13 +694,12 @@ namespace Robust.Client.GameStates
{
DebugTools.Assert(component.NetSyncEnabled);
var state = _entities.GetComponentState(bus, component, null, GameTick.Zero);
var state = _entityManager.GetComponentState(bus, component, null, GameTick.Zero);
DebugTools.Assert(state is not IComponentDeltaState);
compData.Add(netId, state);
}
}
_created.Clear();
_processor.MergeImplicitData(_outputData);
foreach (var data in _outputData.Values)
@@ -752,9 +735,10 @@ namespace Robust.Client.GameStates
_config.TickProcessMessages();
}
(IEnumerable<NetEntity> Created, List<NetEntity> Detached) output;
using (_prof.Group("Entity"))
{
ApplyEntityStates(curState, nextState);
output = ApplyEntityStates(curState, nextState);
}
using (_prof.Group("Player"))
@@ -764,13 +748,13 @@ namespace Robust.Client.GameStates
using (_prof.Group("Callback"))
{
GameStateApplied?.Invoke(new GameStateAppliedArgs(curState, _detached));
GameStateApplied?.Invoke(new GameStateAppliedArgs(curState, output.Detached));
}
return _created;
return output.Created;
}
private void ApplyEntityStates(GameState curState, GameState? nextState)
private (IEnumerable<NetEntity> Created, List<NetEntity> Detached) ApplyEntityStates(GameState curState, GameState? nextState)
{
var metas = _entities.GetEntityQuery<MetaDataComponent>();
var xforms = _entities.GetEntityQuery<TransformComponent>();
@@ -778,74 +762,90 @@ namespace Robust.Client.GameStates
var enteringPvs = 0;
_toApply.Clear();
_created.Clear();
_toCreate.Clear();
_pendingReapplyNetStates.Clear();
var curSpan = curState.EntityStates.Span;
// Create new entities
// This is done BEFORE state application to ensure any new parents exist before existing children have their states applied, otherwise, we may have issues with entity transforms!
using (_prof.Group("Create uninitialized entities"))
{
var created = 0;
using var _ = _prof.Group("Create uninitialized entities");
var count = 0;
foreach (var es in curSpan)
{
if (_entities.TryGetEntity(es.NetEntity, out var nUid))
if (_entityManager.TryGetEntity(es.NetEntity, out var nUid))
{
DebugTools.Assert(_entities.EntityExists(nUid));
DebugTools.Assert(_entityManager.EntityExists(nUid));
continue;
}
created++;
CreateNewEntity(es, curState.ToSequence);
count++;
var metaState = (MetaDataComponentState?)es.ComponentChanges.Value?.FirstOrDefault(c => c.NetID == _metaCompNetId).State;
if (metaState == null)
throw new MissingMetadataException(es.NetEntity);
var uid = _entities.CreateEntity(metaState.PrototypeId, out var newMeta);
_toCreate.Add(es.NetEntity, es);
_toApply.Add(uid, (es.NetEntity, newMeta, false, GameTick.Zero, es, null));
// Client creates a client-side net entity for the newly created entity.
// We need to clear this mapping before assigning the real net id.
// TODO NetEntity Jank: prevent the client from creating this in the first place.
_entityManager.ClearNetEntity(newMeta.NetEntity);
_entityManager.SetNetEntity(uid, es.NetEntity, newMeta);
newMeta.LastStateApplied = curState.ToSequence;
// Check if there's any component states awaiting this entity.
if (_entityManager.PendingNetEntityStates.Remove(es.NetEntity, out var value))
{
foreach (var (type, owner) in value)
{
var pending = _pendingReapplyNetStates.GetOrNew(owner);
pending.Add(type);
}
}
}
_prof.WriteValue("Count", ProfData.Int32(created));
_prof.WriteValue("Count", ProfData.Int32(count));
}
// Add entity entities that aren't new to _toCreate.
// In the process, we also check if these entities are re-entering PVS range.
foreach (var es in curSpan)
{
if (!_entities.TryGetEntityData(es.NetEntity, out var uid, out var meta))
if (_toCreate.ContainsKey(es.NetEntity))
continue;
var isEnteringPvs = (meta.Flags & MetaDataFlags.Detached) != 0;
if (!_entityManager.TryGetEntityData(es.NetEntity, out var uid, out var meta))
continue;
bool isEnteringPvs = (meta.Flags & MetaDataFlags.Detached) != 0;
if (isEnteringPvs)
{
// _toApply already contains newly created entities, but these should never be "entering PVS"
DebugTools.Assert(!_toApply.ContainsKey(uid.Value));
meta.Flags &= ~MetaDataFlags.Detached;
enteringPvs++;
}
else if (meta.LastStateApplied >= es.EntityLastModified && meta.LastStateApplied != GameTick.Zero)
{
// _toApply already contains newly created entities, but for those this set should have no effect
DebugTools.Assert(!_toApply.ContainsKey(uid.Value) || meta.LastStateApplied == curState.ToSequence);
meta.LastStateApplied = curState.ToSequence;
continue;
}
// Any newly created entities already added to _toApply should've already been caught by the previous continue
DebugTools.Assert(!_toApply.ContainsKey(uid.Value));
_toApply.Add(uid.Value, new(uid.Value, es.NetEntity, meta, false, isEnteringPvs, meta.LastStateApplied, es, null, null));
_toApply.Add(uid.Value, (es.NetEntity, meta, isEnteringPvs, meta.LastStateApplied, es, null));
meta.LastStateApplied = curState.ToSequence;
}
// Detach entities to null space
var containerSys = _entitySystemManager.GetEntitySystem<ContainerSystem>();
var lookupSys = _entitySystemManager.GetEntitySystem<EntityLookupSystem>();
ProcessPvsDeparture(curState.ToSequence, metas, xforms, xformSys, containerSys, lookupSys);
var detached = ProcessPvsDeparture(curState.ToSequence, metas, xforms, xformSys, containerSys, lookupSys);
// Check next state (AFTER having created new entities introduced in curstate)
if (nextState != null)
{
foreach (var es in nextState.EntityStates.Span)
{
if (!_entities.TryGetEntityData(es.NetEntity, out var uid, out var meta))
if (!_entityManager.TryGetEntityData(es.NetEntity, out var uid, out var meta))
continue;
// Does the next state actually have any future information about this entity that could be used for interpolation?
@@ -854,14 +854,15 @@ namespace Robust.Client.GameStates
ref var state = ref CollectionsMarshal.GetValueRefOrAddDefault(_toApply, uid.Value, out var exists);
state = exists
? state with {NextState = es}
: new(uid.Value, es.NetEntity, meta, false, false, GameTick.Zero, null, es, null);
if (exists)
state = (es.NetEntity, meta, state.EnteringPvs, state.LastApplied, state.curState, es);
else
state = (es.NetEntity, meta, false, GameTick.Zero, null, es);
}
}
// Check pending states and see if we need to force any entities to re-run component states.
foreach (var (uid, pending) in _pendingReapplyNetStates)
foreach (var uid in _pendingReapplyNetStates.Keys)
{
// Original entity referencing the NetEntity may have been deleted.
if (!metas.TryGetComponent(uid, out var meta))
@@ -878,30 +879,51 @@ namespace Robust.Client.GameStates
DebugTools.Assert(!curState.EntityDeletions.Value.Contains(meta.NetEntity));
// State already being re-applied so don't bulldoze it.
ref var state = ref CollectionsMarshal.GetValueRefOrAddDefault(_toApply, uid, out var exists);
state = exists
? state with {PendingReapply = pending}
: new(uid, meta.NetEntity, meta, false, false, GameTick.Zero, null, null, pending);
if (exists)
continue;
state = (meta.NetEntity, meta, false, GameTick.Zero, null, null);
}
_queuedBroadphaseUpdates.Clear();
using (_prof.Group("Sort States"))
{
SortStates(_toApply);
}
// Apply entity states.
using (_prof.Group("Apply States"))
{
var span = _toApplySorted.AsSpan(0, _toApply.Count);
foreach (ref var data in span)
foreach (var (entity, data) in _toApply)
{
ApplyEntState(data, curState.ToSequence);
#if EXCEPTION_TOLERANCE
try
{
#endif
HandleEntityState(entity, data.NetEntity, data.Meta, _entities.EventBus, data.curState,
data.nextState, data.LastApplied, curState.ToSequence, data.EnteringPvs);
#if EXCEPTION_TOLERANCE
}
catch (Exception e)
{
_sawmill.Error($"Caught exception while applying entity state. Entity: {_entities.ToPrettyString(entity)}. Exception: {e}");
_entityManager.DeleteEntity(entity);
RequestFullState();
continue;
}
#endif
if (!data.EnteringPvs)
continue;
// Now that things like collision data, fixtures, and positions have been updated, we queue a
// broadphase update. However, if this entity is parented to some other entity also re-entering PVS,
// we only need to update it's parent (as it recursively updates children anyways).
var xform = xforms.GetComponent(entity);
DebugTools.Assert(xform.Broadphase == BroadphaseData.Invalid);
xform.Broadphase = null;
if (!_toApply.TryGetValue(xform.ParentUid, out var parent) || !parent.EnteringPvs)
_queuedBroadphaseUpdates.Add((entity, xform));
}
Array.Clear(_toApplySorted, 0, _toApply.Count);
_prof.WriteValue("Count", ProfData.Int32(_toApply.Count));
}
@@ -936,166 +958,14 @@ namespace Robust.Client.GameStates
}
}
// Delete any entities that failed to properly initialize/start
foreach (var entity in _brokenEnts)
{
_entities.DeleteEntity(entity);
}
_brokenEnts.Clear();
// Initialize and start the newly created entities.
if (_toCreate.Count > 0)
InitializeAndStart(_toCreate, metas, xforms);
_prof.WriteValue("State Size", ProfData.Int32(curSpan.Length));
_prof.WriteValue("Entered PVS", ProfData.Int32(enteringPvs));
}
private void ApplyEntState(in StateData data, GameTick toTick)
{
try
{
HandleEntityState(data, _entities.EventBus, toTick);
}
catch (Exception e)
{
_sawmill.Error($"Caught exception while applying entity state. Entity: {_entities.ToPrettyString(data.Uid)}. Exception: {e}");
_brokenEnts.Add(data.Uid);
RequestFullState();
#if !EXCEPTION_TOLERANCE
throw;
#endif
return;
}
if (data.Created)
{
try
{
_entities.InitializeEntity(data.Uid, data.Meta);
_entities.StartEntity(data.Uid);
}
catch (Exception e)
{
_sawmill.Error(
$"Caught exception while initializing or starting entity: {_entities.ToPrettyString(data.Uid)}. Exception: {e}");
_brokenEnts.Add(data.Uid);
RequestFullState();
#if !EXCEPTION_TOLERANCE
throw;
#endif
return;
}
}
if (!data.EnteringPvs)
return;
// Now that things like collision data, fixtures, and positions have been updated, we queue a
// broadphase update. However, if this entity is parented to some other entity also re-entering PVS,
// we only need to update it's parent (as it recursively updates children anyways).
var xform = _entities.TransformQuery.Comp(data.Uid);
DebugTools.Assert(xform.Broadphase == BroadphaseData.Invalid);
xform.Broadphase = null;
if (!_toApply.TryGetValue(xform.ParentUid, out var parent) || !parent.EnteringPvs)
_queuedBroadphaseUpdates.Add((data.Uid, xform));
}
private void CreateNewEntity(EntityState state, GameTick toTick)
{
// TODO GAME STATE
// store MetaData & Transform information separately.
var metaState =
(MetaDataComponentState?) state.ComponentChanges.Value?.FirstOrDefault(c => c.NetID == _metaCompNetId)
.State;
if (metaState == null)
throw new MissingMetadataException(state.NetEntity);
var uid = _entities.CreateEntity(metaState.PrototypeId, out var newMeta);
_toApply.Add(uid, new(uid, state.NetEntity, newMeta, true, false, GameTick.Zero, state, null, null));
_created.Add(state.NetEntity);
// Client creates a client-side net entity for the newly created entity.
// We need to clear this mapping before assigning the real net id.
// TODO NetEntity Jank: prevent the client from creating this in the first place.
_entities.ClearNetEntity(newMeta.NetEntity);
_entities.SetNetEntity(uid, state.NetEntity, newMeta);
newMeta.LastStateApplied = toTick;
// Check if there's any component states awaiting this entity.
if (!_entities.PendingNetEntityStates.Remove(state.NetEntity, out var value))
return;
foreach (var (type, owner) in value)
{
var pending = _pendingReapplyNetStates.GetOrNew(owner);
pending.Add(type);
}
}
/// <summary>
/// Sort states to ensure that we always apply states, initialize, and start parent entities before any of their
/// children.
/// </summary>
private void SortStates(Dictionary<EntityUid, StateData> toApply)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (_toApplySorted == null || _toApplySorted.Length < toApply.Count)
Array.Resize(ref _toApplySorted, toApply.Count);
_sorted.Clear();
var i = 0;
foreach (var (ent, data) in toApply)
{
AddToSorted(ent, data, ref i);
}
DebugTools.AssertEqual(i, toApply.Count);
}
private void AddToSorted(EntityUid ent, in StateData data, ref int i)
{
if (!_sorted.Add(ent))
return;
EnsureParentsSorted(ent, data, ref i);
_toApplySorted[i++] = data;
}
private void EnsureParentsSorted(EntityUid ent, in StateData data, ref int i)
{
var parent = GetStateParent(ent, data);
while (parent != EntityUid.Invalid)
{
if (_toApply.TryGetValue(parent, out var parentData))
{
AddToSorted(parent, parentData, ref i);
// The above method will handle the rest of the transform hierarchy, so we can just return early.
return;
}
parent = _entities.TransformQuery.GetComponent(parent).ParentUid;
}
}
/// <summary>
/// Get the entity's parent in the game state that is being applies. I.e., if the state contains a new
/// transform state, get the parent from that. Otherwise, return the entity's current parent.
/// </summary>
private EntityUid GetStateParent(EntityUid uid, in StateData data)
{
// TODO GAME STATE
// store MetaData & Transform information separately.
if (data.CurState != null
&& data.CurState.ComponentChanges.Value
.TryFirstOrNull(c => c.NetID == _xformCompNetId, out var found))
{
var state = (TransformComponentState) found.Value.State!;
return _entities.GetEntity(state.ParentID);
}
return _entities.TransformQuery.GetComponent(uid).ParentUid;
return (_toCreate.Keys, detached);
}
/// <inheritdoc />
@@ -1130,7 +1000,7 @@ namespace Robust.Client.GameStates
_toDelete.Clear();
// Client side entities won't need the transform, but that should always be a tiny minority of entities
var metaQuery = _entities.AllEntityQueryEnumerator<MetaDataComponent, TransformComponent>();
var metaQuery = _entityManager.AllEntityQueryEnumerator<MetaDataComponent, TransformComponent>();
while (metaQuery.MoveNext(out var ent, out var metadata, out var xform))
{
@@ -1197,9 +1067,9 @@ namespace Robust.Client.GameStates
foreach (var netEntity in delSpan)
{
// Don't worry about this for later.
_entities.PendingNetEntityStates.Remove(netEntity);
_entityManager.PendingNetEntityStates.Remove(netEntity);
if (!_entities.TryGetEntity(netEntity, out var id))
if (!_entityManager.TryGetEntity(netEntity, out var id))
continue;
if (!xforms.TryGetComponent(id, out var xform))
@@ -1229,10 +1099,9 @@ namespace Robust.Client.GameStates
var containerSys = _entitySystemManager.GetEntitySystem<ContainerSystem>();
var lookupSys = _entitySystemManager.GetEntitySystem<EntityLookupSystem>();
Detach(GameTick.MaxValue, null, entities, metas, xforms, xformSys, containerSys, lookupSys);
_detached.Clear();
}
private void ProcessPvsDeparture(
private List<NetEntity> ProcessPvsDeparture(
GameTick toTick,
EntityQuery<MetaDataComponent> metas,
EntityQuery<TransformComponent> xforms,
@@ -1241,23 +1110,25 @@ namespace Robust.Client.GameStates
EntityLookupSystem lookupSys)
{
var toDetach = _processor.GetEntitiesToDetach(toTick, _pvsDetachBudget);
var detached = new List<NetEntity>();
if (toDetach.Count == 0)
return;
return detached;
// TODO optimize
// If an entity is leaving PVS, so are all of its children. If we can preserve the hierarchy we can avoid
// things like container insertion and ejection.
using var _ = _prof.Group("Leave PVS");
detached.EnsureCapacity(toDetach.Count);
_detached.Clear();
foreach (var (tick, ents) in toDetach)
{
Detach(tick, toTick, ents, metas, xforms, xformSys, containerSys, lookupSys);
Detach(tick, toTick, ents, metas, xforms, xformSys, containerSys, lookupSys, detached);
}
_prof.WriteValue("Count", ProfData.Int32(_detached.Count));
_prof.WriteValue("Count", ProfData.Int32(detached.Count));
return detached;
}
private void Detach(GameTick maxTick,
@@ -1267,11 +1138,12 @@ namespace Robust.Client.GameStates
EntityQuery<TransformComponent> xforms,
SharedTransformSystem xformSys,
ContainerSystem containerSys,
EntityLookupSystem lookupSys)
EntityLookupSystem lookupSys,
List<NetEntity>? detached = null)
{
foreach (var netEntity in entities)
{
if (!_entities.TryGetEntityData(netEntity, out var ent, out var meta))
if (!_entityManager.TryGetEntityData(netEntity, out var ent, out var meta))
continue;
if (meta.LastStateApplied > maxTick)
@@ -1312,75 +1184,159 @@ namespace Robust.Client.GameStates
containerSys.AddExpectedEntity(netEntity, container);
}
_detached.Add(netEntity);
detached?.Add(netEntity);
}
}
private void HandleEntityState(in StateData data, IEventBus bus, GameTick toTick)
private void InitializeAndStart(
Dictionary<NetEntity, EntityState> toCreate,
EntityQuery<MetaDataComponent> metas,
EntityQuery<TransformComponent> xforms)
{
_toStart.Clear();
using (_prof.Group("Initialize Entity"))
{
EntityUid entity = default;
foreach (var netEntity in toCreate.Keys)
{
(entity, var meta) = _entityManager.GetEntityData(netEntity);
InitializeRecursive(entity, meta, metas, xforms);
}
}
using (_prof.Group("Start Entity"))
{
foreach (var (entity, netEntity) in _toStart)
{
try
{
_entities.StartEntity(entity);
}
catch (Exception e)
{
_sawmill.Error($"Server entity threw in Start: nent={netEntity}, ent={_entityManager.ToPrettyString(entity)}");
_runtimeLog.LogException(e, $"{nameof(ClientGameStateManager)}.{nameof(InitializeAndStart)}");
_toCreate.Remove(netEntity);
_brokenEnts.Add(entity);
#if !EXCEPTION_TOLERANCE
throw;
#endif
}
}
}
foreach (var entity in _brokenEnts)
{
_entityManager.DeleteEntity(entity);
}
_brokenEnts.Clear();
}
private void InitializeRecursive(
EntityUid entity,
MetaDataComponent meta,
EntityQuery<MetaDataComponent> metas,
EntityQuery<TransformComponent> xforms)
{
var xform = xforms.GetComponent(entity);
if (xform.ParentUid is {Valid: true} parent)
{
var parentMeta = metas.GetComponent(parent);
if (parentMeta.EntityLifeStage < EntityLifeStage.Initialized)
InitializeRecursive(parent, parentMeta, metas, xforms);
}
if (meta.EntityLifeStage >= EntityLifeStage.Initialized)
{
// Was probably already initialized because one of its children appeared earlier in the list.
DebugTools.AssertEqual(_toStart.Count(x => x.Item1 == entity), 1);
return;
}
try
{
_entities.InitializeEntity(entity, meta);
_toStart.Add((entity, meta.NetEntity));
}
catch (Exception e)
{
_sawmill.Error($"Server entity threw in Init: nent={meta.NetEntity}, ent={_entities.ToPrettyString(entity)}");
_runtimeLog.LogException(e, $"{nameof(ClientGameStateManager)}.{nameof(InitializeAndStart)}");
_toCreate.Remove(meta.NetEntity);
_brokenEnts.Add(entity);
#if !EXCEPTION_TOLERANCE
throw;
#endif
}
}
private void HandleEntityState(EntityUid uid, NetEntity netEntity, MetaDataComponent meta, IEventBus bus, EntityState? curState,
EntityState? nextState, GameTick lastApplied, GameTick toTick, bool enteringPvs)
{
_compStateWork.Clear();
// First remove any deleted components
if (data.CurState?.NetComponents is {} netComps)
if (curState?.NetComponents != null)
{
_toRemove.Clear();
foreach (var (id, comp) in data.Meta.NetComponents)
foreach (var (id, comp) in meta.NetComponents)
{
DebugTools.Assert(comp.NetSyncEnabled);
if (!netComps.Contains(id))
if (!curState.NetComponents.Contains(id))
_toRemove.Add(comp);
}
foreach (var comp in _toRemove)
{
_entities.RemoveComponent(data.Uid, comp, data.Meta);
_entities.RemoveComponent(uid, comp, meta);
}
}
if (data.EnteringPvs)
if (enteringPvs)
{
// last-server state has already been updated with new information from curState
// --> simply reset to the most recent server state.
//
// as to why we need to reset: because in the process of detaching to null-space, we will have dirtied
// the entity. most notably, all entities will have been ejected from their containers.
foreach (var (id, state) in _processor.GetLastServerStates(data.NetEntity))
foreach (var (id, state) in _processor.GetLastServerStates(netEntity))
{
if (!data.Meta.NetComponents.TryGetValue(id, out var comp))
if (!meta.NetComponents.TryGetValue(id, out var comp))
{
comp = _compFactory.GetComponent(id);
_entities.AddComponent(data.Uid, comp, true, metadata: data.Meta);
_entityManager.AddComponent(uid, comp, true, metadata: meta);
}
_compStateWork[id] = (comp, state, null);
}
}
else if (data.CurState != null)
else if (curState != null)
{
foreach (var compChange in data.CurState.ComponentChanges.Span)
foreach (var compChange in curState.ComponentChanges.Span)
{
if (!data.Meta.NetComponents.TryGetValue(compChange.NetID, out var comp))
if (!meta.NetComponents.TryGetValue(compChange.NetID, out var comp))
{
comp = _compFactory.GetComponent(compChange.NetID);
_entities.AddComponent(data.Uid, comp, true, metadata: data.Meta);
_entityManager.AddComponent(uid, comp, true, metadata:meta);
}
else if (compChange.LastModifiedTick <= data.LastApplied && data.LastApplied != GameTick.Zero)
else if (compChange.LastModifiedTick <= lastApplied && lastApplied != GameTick.Zero)
continue;
_compStateWork[compChange.NetID] = (comp, compChange.State, null);
}
}
if (data.NextState != null)
if (nextState != null)
{
foreach (var compState in data.NextState.ComponentChanges.Span)
foreach (var compState in nextState.ComponentChanges.Span)
{
if (compState.LastModifiedTick != toTick + 1)
continue;
if (!data.Meta.NetComponents.TryGetValue(compState.NetID, out var comp))
if (!meta.NetComponents.TryGetValue(compState.NetID, out var comp))
{
// The component can be null here due to interp, because the NEXT state will have a new
// component, but the component does not yet exist.
@@ -1398,10 +1354,9 @@ namespace Robust.Client.GameStates
}
// If we have a NetEntity we reference come in then apply their state.
DebugTools.Assert(_pendingReapplyNetStates.ContainsKey(data.Uid) == (data.PendingReapply != null));
if (data.PendingReapply is {} reapplyTypes)
if (_pendingReapplyNetStates.TryGetValue(uid, out var reapplyTypes))
{
var lastState = _processor.GetLastServerStates(data.NetEntity);
var lastState = _processor.GetLastServerStates(netEntity);
foreach (var type in reapplyTypes)
{
@@ -1411,7 +1366,7 @@ namespace Robust.Client.GameStates
if (netId == null)
continue;
if (!data.Meta.NetComponents.TryGetValue(netId.Value, out var comp) ||
if (!meta.NetComponents.TryGetValue(netId.Value, out var comp) ||
!lastState.TryGetValue(netId.Value, out var lastCompState))
{
continue;
@@ -1433,7 +1388,7 @@ namespace Robust.Client.GameStates
continue;
var handleState = new ComponentHandleState(cur, next);
bus.RaiseComponentEvent(data.Uid, comp, ref handleState);
bus.RaiseComponentEvent(uid, comp, ref handleState);
}
}
@@ -1555,7 +1510,7 @@ namespace Robust.Client.GameStates
{
using var _ = _timing.StartStateApplicationArea();
var query = _entities.AllEntityQueryEnumerator<MetaDataComponent>();
var query = _entityManager.AllEntityQueryEnumerator<MetaDataComponent>();
while (query.MoveNext(out var uid, out var meta))
{
@@ -1581,14 +1536,14 @@ namespace Robust.Client.GameStates
if (!meta.NetComponents.TryGetValue(id, out var comp))
{
comp = _compFactory.GetComponent(id);
_entities.AddComponent(uid, comp, true, meta);
_entityManager.AddComponent(uid, comp, true, meta);
}
if (state == null)
continue;
var handleState = new ComponentHandleState(state, null);
_entities.EventBus.RaiseComponentEvent(uid, comp, ref handleState);
_entityManager.EventBus.RaiseComponentEvent(uid, comp, ref handleState);
}
// ensure we don't have any extra components

View File

@@ -82,7 +82,11 @@ namespace Robust.Client.GameStates
/// </summary>
IEnumerable<NetEntity> ApplyGameState(GameState curState, GameState? nextState);
void MergeImplicitData();
/// <summary>
/// Generates implicit component states for newly created entities.
/// This should always be called after running <see cref="ApplyGameState(GameState, GameState)"/>.
/// </summary>
void GenerateImplicitStates(IEnumerable<NetEntity> states);
/// <summary>
/// Resets any entities that have changed while predicting future ticks.

View File

@@ -1,5 +0,0 @@
using Robust.Shared.GameStates;
namespace Robust.Client.GameStates;
public sealed partial class PvsOverrideSystem : SharedPvsOverrideSystem;

View File

@@ -125,7 +125,7 @@ namespace Robust.Client.Graphics.Clyde
{
DebugTools.Assert(space != OverlaySpace.ScreenSpaceBelowWorld && space != OverlaySpace.ScreenSpace);
var args = new OverlayDrawArgs(space, null, vp, _renderHandle, new UIBox2i((0, 0), vp.Size), vp.Eye!.Position.MapId, worldBox, worldBounds);
var args = new OverlayDrawArgs(space, null, vp, _renderHandle.DrawingHandleWorld, new UIBox2i((0, 0), vp.Size), vp.Eye!.Position.MapId, worldBox, worldBounds);
if (!overlay.BeforeDraw(args))
return;
@@ -165,7 +165,7 @@ namespace Robust.Client.Graphics.Clyde
private void RenderOverlaysDirect(
Viewport vp,
IViewportControl vpControl,
IRenderHandle handle,
DrawingHandleBase handle,
OverlaySpace space,
in UIBox2i bounds)
{

View File

@@ -171,7 +171,7 @@ namespace Robust.Client.Graphics.Clyde
}
public void RenderScreenOverlaysBelow(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds)
{
@@ -179,7 +179,7 @@ namespace Robust.Client.Graphics.Clyde
}
public void RenderScreenOverlaysAbove(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds)
{

View File

@@ -497,7 +497,7 @@ namespace Robust.Client.Graphics.Clyde
}
public void RenderScreenOverlaysBelow(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds)
{
@@ -505,7 +505,7 @@ namespace Robust.Client.Graphics.Clyde
}
public void RenderScreenOverlaysAbove(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds)
{

View File

@@ -66,7 +66,7 @@ namespace Robust.Client.Graphics
/// Not relative to the current transform of <see cref="handle"/>.
/// </param>
public void RenderScreenOverlaysBelow(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds);
@@ -80,7 +80,7 @@ namespace Robust.Client.Graphics
/// Not relative to the current transform of <see cref="handle"/>.
/// </param>
public void RenderScreenOverlaysAbove(
IRenderHandle handle,
DrawingHandleScreen handle,
IViewportControl control,
in UIBox2i viewportBounds);
}

View File

@@ -54,29 +54,23 @@ namespace Robust.Client.Graphics
/// </summary>
public readonly Box2Rotated WorldBounds;
public readonly IRenderHandle RenderHandle;
public DrawingHandleScreen ScreenHandle => (DrawingHandleScreen) DrawingHandle;
public DrawingHandleWorld WorldHandle => (DrawingHandleWorld) DrawingHandle;
internal OverlayDrawArgs(
public OverlayDrawArgs(
OverlaySpace space,
IViewportControl? viewportControl,
IClydeViewport viewport,
IRenderHandle renderHandle,
DrawingHandleBase drawingHandle,
in UIBox2i viewportBounds,
in MapId mapId,
in Box2 worldAabb,
in Box2Rotated worldBounds)
{
DrawingHandle = space is OverlaySpace.ScreenSpace or OverlaySpace.ScreenSpaceBelowWorld
? renderHandle.DrawingHandleScreen
: renderHandle.DrawingHandleWorld;
Space = space;
ViewportControl = viewportControl;
Viewport = viewport;
RenderHandle = renderHandle;
DrawingHandle = drawingHandle;
ViewportBounds = viewportBounds;
MapId = mapId;
WorldAABB = worldAabb;

View File

@@ -93,7 +93,7 @@ public sealed partial class PhysicsSystem
var maps = new HashSet<EntityUid>();
var enumerator = AllEntityQuery<PredictedPhysicsComponent, PhysicsComponent, TransformComponent>();
while (enumerator.MoveNext(out _, out var physics, out var xform))
while (enumerator.MoveNext(out var _, out var physics, out var xform))
{
DebugTools.Assert(physics.Predict);

View File

@@ -352,9 +352,6 @@ public sealed partial class ReplayLoadManager
// prototype changes when jumping around in time. This also requires reworking how the initial
// implicit state data is generated, because we can't simply cache it anymore.
// Also, does reloading prototypes in release mode modify existing entities?
// Yes, yes it does. See PrototypeReloadSystem.UpdateEntity()
// Its just not supported ATM.
// TBH it'd be easier if overriding existing prototypes in release mode was just forbidden.
var msg = $"Overwriting an existing prototype! Kind: {kind.Name}. Ids: {string.Join(", ", ids)}";
if (_confMan.GetCVar(CVars.ReplayIgnoreErrors))
@@ -364,6 +361,7 @@ public sealed partial class ReplayLoadManager
}
}
_protoMan.ResolveResults();
_protoMan.ReloadPrototypes(changed);
_locMan.ReloadLocalizations();
}

View File

@@ -44,8 +44,8 @@ internal sealed partial class ReplayPlaybackManager
_gameState.UpdateFullRep(state, cloneDelta: true);
var next = Replay.NextState;
BeforeApplyState?.Invoke((state, next));
_gameState.ApplyGameState(state, next);
_gameState.MergeImplicitData();
var created = _gameState.ApplyGameState(state, next);
_gameState.GenerateImplicitStates(created);
DebugTools.Assert(Replay.LastApplied >= state.FromSequence);
DebugTools.Assert(Replay.LastApplied + 1 <= state.ToSequence);
Replay.LastApplied = state.ToSequence;

View File

@@ -1,5 +1,4 @@
using System;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
@@ -7,47 +6,14 @@ namespace Robust.Client.UserInterface;
public static class BoundUserInterfaceExt
{
private static T GetWindow<T>(BoundUserInterface bui) where T : BaseWindow, new()
{
var window = bui.CreateDisposableControl<T>();
window.OnClose += bui.Close;
var system = bui.EntMan.System<UserInterfaceSystem>();
system.RegisterControl(bui, window);
return window;
}
/// <summary>
/// Helper method to create a window and also handle closing the BUI when it's closed.
/// </summary>
public static T CreateWindow<T>(this BoundUserInterface bui) where T : BaseWindow, new()
{
var window = GetWindow<T>(bui);
if (bui.EntMan.System<UserInterfaceSystem>().TryGetPosition(bui.Owner, bui.UiKey, out var position))
{
window.Open(position);
}
else
{
window.OpenCentered();
}
return window;
}
public static T CreateWindowCenteredLeft<T>(this BoundUserInterface bui) where T : BaseWindow, new()
{
var window = GetWindow<T>(bui);
if (bui.EntMan.System<UserInterfaceSystem>().TryGetPosition(bui.Owner, bui.UiKey, out var position))
{
window.Open(position);
}
else
{
window.OpenCenteredLeft();
}
var window = bui.CreateDisposableControl<T>();
window.OpenCentered();
window.OnClose += bui.Close;
return window;
}

View File

@@ -128,7 +128,7 @@ namespace Robust.Client.UserInterface
UserInterfaceManagerInternal.QueueStyleUpdate(this);
}
public void InvalidateStyleSheet()
internal void StyleSheetUpdate()
{
_stylesheetUpdateNeeded = true;
@@ -137,7 +137,7 @@ namespace Robust.Client.UserInterface
internal void StylesheetUpdateRecursive()
{
InvalidateStyleSheet();
StyleSheetUpdate();
foreach (var child in Children)
{
@@ -149,7 +149,7 @@ namespace Robust.Client.UserInterface
}
}
public void DoStyleUpdate()
internal void DoStyleUpdate()
{
_styleProperties.Clear();

View File

@@ -549,7 +549,7 @@ namespace Robust.Client.UserInterface
{
}
protected internal virtual void Draw(IRenderHandle renderHandle)
internal virtual void DrawInternal(IRenderHandle renderHandle)
{
Draw(renderHandle.DrawingHandleScreen);
}

View File

@@ -87,6 +87,7 @@ public sealed class TileSpawningUIController : UIController
return;
_window = UIManager.CreateWindow<TileSpawnWindow>();
LayoutContainer.SetAnchorPreset(_window,LayoutContainer.LayoutPreset.CenterLeft);
_window.SearchBar.GrabKeyboardFocus();
_window.ClearButton.OnPressed += OnTileClearPressed;
_window.SearchBar.OnTextChanged += OnTileSearchChanged;
_window.TileList.OnItemSelected += OnTileItemSelected;

View File

@@ -22,6 +22,8 @@ public class EntityPrototypeView : SpriteView
public void SetPrototype(EntProtoId? entProto)
{
SpriteSystem ??= EntMan.System<SpriteSystem>();
if (entProto == _currentPrototype
&& EntMan.TryGetComponent(Entity?.Owner, out MetaDataComponent? meta)
&& meta.EntityPrototype?.ID == _currentPrototype)
@@ -30,10 +32,14 @@ public class EntityPrototypeView : SpriteView
}
_currentPrototype = entProto;
SetEntity(null);
EntMan.DeleteEntity(_ourEntity);
if (_ourEntity != null)
if (_currentPrototype != null)
{
UpdateEntity();
_ourEntity = EntMan.Spawn(_currentPrototype);
SpriteSystem.ForceUpdate(_ourEntity.Value);
SetEntity(_ourEntity);
}
}
@@ -42,34 +48,12 @@ public class EntityPrototypeView : SpriteView
base.EnteredTree();
if (_currentPrototype != null)
{
UpdateEntity();
}
SetPrototype(_currentPrototype);
}
protected override void ExitedTree()
{
base.ExitedTree();
EntMan.TryQueueDeleteEntity(_ourEntity);
_ourEntity = null;
}
private void UpdateEntity()
{
SetEntity(null);
EntMan.DeleteEntity(_ourEntity);
if (_currentPrototype != null)
{
SpriteSystem ??= EntMan.System<SpriteSystem>();
_ourEntity = EntMan.Spawn(_currentPrototype);
SpriteSystem.ForceUpdate(_ourEntity.Value);
SetEntity(_ourEntity);
}
else
{
_ourEntity = null;
}
}
}

View File

@@ -224,7 +224,7 @@ namespace Robust.Client.UserInterface.Controls
_spriteSize = new Vector2(longestRotatedSide, longestRotatedSide);
}
protected internal override void Draw(IRenderHandle renderHandle)
internal override void DrawInternal(IRenderHandle renderHandle)
{
if (!ResolveEntity(out var uid, out var sprite, out var xform))
return;

View File

@@ -5,7 +5,6 @@ using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
@@ -103,8 +102,6 @@ namespace Robust.Client.UserInterface.CustomControls
{
var cursor = CursorShape.Arrow;
var previewDragMode = GetDragModeFor(args.RelativePosition);
previewDragMode &= ~DragMode.Move;
switch (previewDragMode)
{
case DragMode.Top:
@@ -119,6 +116,9 @@ namespace Robust.Client.UserInterface.CustomControls
case DragMode.Bottom | DragMode.Left:
case DragMode.Top | DragMode.Right:
cursor = CursorShape.Crosshair;
break;
case DragMode.Bottom | DragMode.Right:
case DragMode.Top | DragMode.Left:
cursor = CursorShape.Crosshair;
@@ -160,6 +160,15 @@ namespace Robust.Client.UserInterface.CustomControls
var rect = new UIBox2(left, top, right, bottom);
LayoutContainer.SetPosition(this, rect.TopLeft);
SetSize = rect.Size;
/*
var timing = IoCManager.Resolve<IGameTiming>();
var l = GetValue<float>(LayoutContainer.MarginLeftProperty);
var t = GetValue<float>(LayoutContainer.MarginTopProperty);
Logger.Debug($"{timing.CurFrame}: {rect.TopLeft}/({l}, {t}), {rect.Size}/{SetSize}");
*/
}
}
@@ -220,16 +229,6 @@ namespace Robust.Client.UserInterface.CustomControls
OnOpen?.Invoke();
}
/// <summary>
/// Opens the window and places it at the specified position.
/// </summary>
public void Open(Vector2 position)
{
Measure(Vector2Helpers.Infinity);
Open();
LayoutContainer.SetPosition(this, position);
}
public void OpenCentered() => OpenCenteredAt(new Vector2(0.5f, 0.5f));
public void OpenToLeft() => OpenCenteredAt(new Vector2(0, 0.5f));

View File

@@ -10,11 +10,4 @@ public sealed partial class TileSpawnWindow : DefaultWindow
{
RobustXamlLoader.Load(this);
}
protected override void Opened()
{
base.Opened();
SearchBar.GrabKeyboardFocus();
}
}

View File

@@ -65,13 +65,13 @@ namespace Robust.Client.UserInterface.CustomControls
// -- Handlers: Out --
protected internal override void Draw(IRenderHandle handle)
protected internal override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (Viewport == null)
{
handle.DrawingHandleScreen.DrawRect(UIBox2.FromDimensions(new Vector2(0, 0), Size * UIScale), Color.Red);
handle.DrawRect(UIBox2.FromDimensions(new Vector2(0, 0), Size * UIScale), Color.Red);
}
else
{
@@ -82,7 +82,7 @@ namespace Robust.Client.UserInterface.CustomControls
Viewport.RenderScreenOverlaysBelow(handle, this, viewportBounds);
Viewport.Render();
handle.DrawingHandleScreen.DrawTextureRect(Viewport.RenderTarget.Texture,
handle.DrawTextureRect(Viewport.RenderTarget.Texture,
UIBox2.FromDimensions(new Vector2(0, 0), (Vector2i) (Viewport.Size / _viewportResolution)));
Viewport.RenderScreenOverlaysAbove(handle, this, viewportBounds);

View File

@@ -7,7 +7,6 @@ using Robust.Client.UserInterface.CustomControls;
using Robust.Shared;
using Robust.Shared.Audio.Sources;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface
{
@@ -147,16 +146,6 @@ namespace Robust.Client.UserInterface
/// but not necessarily a new or existing control is rearranged.
/// </summary>
void UpdateHovered();
/// <summary>
/// Render a control and all of its children.
/// </summary>
void RenderControl(in Control.ControlRenderArguments args, Control control);
/// <summary>
/// Render a control and all of its children.
/// </summary>
void RenderControl(IRenderHandle handle, Control control, Vector2i position);
}
public readonly struct PostDrawUIRootEventArgs

View File

@@ -313,10 +313,7 @@ internal partial class UserInterfaceManager
private void _clearTooltip()
{
_resetTooltipTimer();
if (!_showingTooltip)
return;
if (!_showingTooltip) return;
if (_suppliedTooltip != null)
{
@@ -325,6 +322,7 @@ internal partial class UserInterfaceManager
}
CurrentlyHovered?.PerformHideTooltip();
_resetTooltipTimer();
_showingTooltip = false;
}

View File

@@ -37,7 +37,7 @@ internal sealed partial class UserInterfaceManager
_roots.Add(newRoot);
_windowsToRoot.Add(window.Id, newRoot);
newRoot.InvalidateStyleSheet();
newRoot.StyleSheetUpdate();
newRoot.InvalidateMeasure();
QueueMeasureUpdate(newRoot);
QueueArrangeUpdate(newRoot);

View File

@@ -335,30 +335,6 @@ namespace Robust.Client.UserInterface
}
}
public void RenderControl(in Control.ControlRenderArguments args, Control control)
{
var _ = 0;
RenderControl(args.Handle,
ref _,
control,
args.Position,
args.Modulate,
args.ScissorBox,
args.CoordinateTransform);
}
public void RenderControl(IRenderHandle handle, Control control, Vector2i position)
{
var _ = 0;
RenderControl(handle,
ref _,
control,
position,
Color.White,
null,
Matrix3x2.Identity);
}
public void RenderControl(IRenderHandle renderHandle, ref int total, Control control, Vector2i position, Color modulate,
UIBox2i? scissorBox, Matrix3x2 coordinateTransform)
{
@@ -417,7 +393,7 @@ namespace Robust.Client.UserInterface
// Handle modulation with care.
var oldMod = handle.Modulate;
handle.Modulate = modulate * control.ActualModulateSelf;
control.Draw(renderHandle);
control.DrawInternal(renderHandle);
handle.Modulate = oldMod;
handle.UseShader(null);
}
@@ -433,11 +409,10 @@ namespace Robust.Client.UserInterface
control.PreRenderChildren(ref args);
for (var index = 0; index < control.ChildCount; index++)
foreach (var child in control.Children)
{
var child = control.GetChild(index);
var pos = position + (Vector2i)Vector2.Transform(child.PixelPosition, coordinateTransform);
control.RenderChildOverride(ref args, index, pos);
control.RenderChildOverride(ref args, child.GetPositionInParent(), pos);
}
control.PostRenderChildren(ref args);

View File

@@ -19,112 +19,117 @@ public class Generator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext initContext)
{
IncrementalValuesProvider<(string name, string code)?> dataDefinitions = initContext.SyntaxProvider
.CreateSyntaxProvider(
static (node, _) => node is TypeDeclarationSyntax,
static (context, _) =>
{
var type = (TypeDeclarationSyntax)context.Node;
var symbol = (ITypeSymbol)context.SemanticModel.GetDeclaredSymbol(type)!;
if (!IsDataDefinition(symbol))
return null;
return GenerateForDataDefinition(type, symbol);
}
)
.Where(static type => type != null);
IncrementalValuesProvider<TypeDeclarationSyntax> dataDefinitions = initContext.SyntaxProvider.CreateSyntaxProvider(
static (node, _) => node is TypeDeclarationSyntax,
static (context, _) =>
{
var type = (TypeDeclarationSyntax) context.Node;
var symbol = (ITypeSymbol) context.SemanticModel.GetDeclaredSymbol(type)!;
return IsDataDefinition(symbol) ? type : null;
}
).Where(static type => type != null)!;
var comparer = new DataDefinitionComparer();
initContext.RegisterSourceOutput(
dataDefinitions,
initContext.CompilationProvider.Combine(dataDefinitions.WithComparer(comparer).Collect()),
static (sourceContext, source) =>
{
// TODO: deduplicate based on name?
var (name, code) = source!.Value;
var (compilation, declarations) = source;
var builder = new StringBuilder();
var containingTypes = new Stack<INamedTypeSymbol>();
var declarationsGenerated = new HashSet<string>();
var deltaType = compilation.GetTypeByMetadataName(ComponentDeltaInterfaceName)!;
sourceContext.AddSource(name, code);
foreach (var declaration in declarations)
{
builder.Clear();
containingTypes.Clear();
var type = compilation.GetSemanticModel(declaration.SyntaxTree).GetDeclaredSymbol(declaration)!;
var symbolName = type
.ToDisplayString()
.Replace('<', '{')
.Replace('>', '}');
if (!declarationsGenerated.Add(symbolName))
continue;
var nonPartial = !IsPartial(declaration);
var namespaceString = type.ContainingNamespace.IsGlobalNamespace
? string.Empty
: $"namespace {type.ContainingNamespace.ToDisplayString()};";
var containingType = type.ContainingType;
while (containingType != null)
{
containingTypes.Push(containingType);
containingType = containingType.ContainingType;
}
var containingTypesStart = new StringBuilder();
var containingTypesEnd = new StringBuilder();
foreach (var parent in containingTypes)
{
var syntax = (ClassDeclarationSyntax) parent.DeclaringSyntaxReferences[0].GetSyntax();
if (!IsPartial(syntax))
{
nonPartial = true;
continue;
}
containingTypesStart.AppendLine($"{GetPartialTypeDefinitionLine(parent)}\n{{");
containingTypesEnd.AppendLine("}");
}
var definition = GetDataDefinition(type);
if (nonPartial || definition.InvalidFields)
continue;
builder.AppendLine($$"""
#nullable enable
using System;
using Robust.Shared.Analyzers;
using Robust.Shared.IoC;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Exceptions;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0108 // Member hides inherited member; missing new keyword
#pragma warning disable RA0002 // Robust access analyzer
{{namespaceString}}
{{containingTypesStart}}
{{GetPartialTypeDefinitionLine(type)}} : ISerializationGenerated<{{definition.GenericTypeName}}>
{
{{GetConstructor(definition)}}
{{GetCopyMethods(definition, deltaType)}}
{{GetInstantiators(definition, deltaType)}}
}
{{containingTypesEnd}}
""");
var sourceText = CSharpSyntaxTree
.ParseText(builder.ToString())
.GetRoot()
.NormalizeWhitespace()
.ToFullString();
sourceContext.AddSource($"{symbolName}.g.cs", sourceText);
}
}
);
}
private static (string, string)? GenerateForDataDefinition(
TypeDeclarationSyntax declaration,
ITypeSymbol type)
{
var builder = new StringBuilder();
var containingTypes = new Stack<INamedTypeSymbol>();
containingTypes.Clear();
var symbolName = type
.ToDisplayString()
.Replace('<', '{')
.Replace('>', '}');
var nonPartial = !IsPartial(declaration);
var namespaceString = type.ContainingNamespace.IsGlobalNamespace
? string.Empty
: $"namespace {type.ContainingNamespace.ToDisplayString()};";
var containingType = type.ContainingType;
while (containingType != null)
{
containingTypes.Push(containingType);
containingType = containingType.ContainingType;
}
var containingTypesStart = new StringBuilder();
var containingTypesEnd = new StringBuilder();
foreach (var parent in containingTypes)
{
var syntax = (ClassDeclarationSyntax)parent.DeclaringSyntaxReferences[0].GetSyntax();
if (!IsPartial(syntax))
{
nonPartial = true;
continue;
}
containingTypesStart.AppendLine($"{GetPartialTypeDefinitionLine(parent)}\n{{");
containingTypesEnd.AppendLine("}");
}
var definition = GetDataDefinition(type);
if (nonPartial || definition.InvalidFields)
return null;
builder.AppendLine($$"""
#nullable enable
using System;
using Robust.Shared.Analyzers;
using Robust.Shared.IoC;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Exceptions;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0108 // Member hides inherited member; missing new keyword
#pragma warning disable RA0002 // Robust access analyzer
{{namespaceString}}
{{containingTypesStart}}
{{GetPartialTypeDefinitionLine(type)}} : ISerializationGenerated<{{definition.GenericTypeName}}>
{
{{GetConstructor(definition)}}
{{GetCopyMethods(definition)}}
{{GetInstantiators(definition)}}
}
{{containingTypesEnd}}
""");
return ($"{symbolName}.g.cs", builder.ToString());
}
private static DataDefinition GetDataDefinition(ITypeSymbol definition)
{
var fields = new List<DataField>();
@@ -191,7 +196,7 @@ public class Generator : IIncrementalGenerator
return builder.ToString();
}
private static string GetCopyMethods(DataDefinition definition)
private static string GetCopyMethods(DataDefinition definition, ITypeSymbol deltaType)
{
var builder = new StringBuilder();
@@ -262,36 +267,36 @@ public class Generator : IIncrementalGenerator
{{baseCopy}}
""");
foreach (var interfaceName in InternalGetImplicitDataDefinitionInterfaces(definition.Type, true))
foreach (var @interface in InternalGetImplicitDataDefinitionInterfaces(definition.Type, true, deltaType))
{
var interfaceModifiers = baseType != null &&
baseType.AllInterfaces.Any(i => i.ToDisplayString() == interfaceName)
var interfaceModifiers = baseType != null && baseType.AllInterfaces.Contains(@interface, SymbolEqualityComparer.Default)
? "override "
: modifiers;
var interfaceName = @interface.ToDisplayString();
builder.AppendLine($$"""
/// <seealso cref="ISerializationManager.CopyTo"/>
[Obsolete("Use ISerializationManager.CopyTo instead")]
public {{interfaceModifiers}} void InternalCopy(ref {{interfaceName}} target, ISerializationManager serialization, SerializationHookContext hookCtx, ISerializationContext? context = null)
{
var def = ({{definition.GenericTypeName}}) target;
Copy(ref def, serialization, hookCtx, context);
target = def;
}
/// <seealso cref="ISerializationManager.CopyTo"/>
[Obsolete("Use ISerializationManager.CopyTo instead")]
public {{interfaceModifiers}} void InternalCopy(ref {{interfaceName}} target, ISerializationManager serialization, SerializationHookContext hookCtx, ISerializationContext? context = null)
{
var def = ({{definition.GenericTypeName}}) target;
Copy(ref def, serialization, hookCtx, context);
target = def;
}
/// <seealso cref="ISerializationManager.CopyTo"/>
[Obsolete("Use ISerializationManager.CopyTo instead")]
public {{interfaceModifiers}} void Copy(ref {{interfaceName}} target, ISerializationManager serialization, SerializationHookContext hookCtx, ISerializationContext? context = null)
{
InternalCopy(ref target, serialization, hookCtx, context);
}
""");
/// <seealso cref="ISerializationManager.CopyTo"/>
[Obsolete("Use ISerializationManager.CopyTo instead")]
public {{interfaceModifiers}} void Copy(ref {{interfaceName}} target, ISerializationManager serialization, SerializationHookContext hookCtx, ISerializationContext? context = null)
{
InternalCopy(ref target, serialization, hookCtx, context);
}
""");
}
return builder.ToString();
}
private static string GetInstantiators(DataDefinition definition)
private static string GetInstantiators(DataDefinition definition, ITypeSymbol deltaType)
{
var builder = new StringBuilder();
var modifiers = string.Empty;
@@ -325,28 +330,27 @@ public class Generator : IIncrementalGenerator
""");
}
foreach (var interfaceName in InternalGetImplicitDataDefinitionInterfaces(definition.Type, false))
foreach (var @interface in InternalGetImplicitDataDefinitionInterfaces(definition.Type, false, deltaType))
{
var interfaceName = @interface.ToDisplayString();
builder.AppendLine($$"""
{{interfaceName}} {{interfaceName}}.Instantiate()
{
return Instantiate();
}
{{interfaceName}} {{interfaceName}}.Instantiate()
{
return Instantiate();
}
{{interfaceName}} ISerializationGenerated<{{interfaceName}}>.Instantiate()
{
return Instantiate();
}
""");
{{interfaceName}} ISerializationGenerated<{{interfaceName}}>.Instantiate()
{
return Instantiate();
}
""");
}
return builder.ToString();
}
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
private static IEnumerable<string> InternalGetImplicitDataDefinitionInterfaces(
ITypeSymbol type,
bool all)
private static IEnumerable<ITypeSymbol> InternalGetImplicitDataDefinitionInterfaces(ITypeSymbol type, bool all, ITypeSymbol deltaType)
{
var symbols = GetImplicitDataDefinitionInterfaces(type, all);
@@ -364,10 +368,10 @@ public class Generator : IIncrementalGenerator
return symbols;
}
if (symbols.Any(x => x == ComponentDeltaInterfaceName))
if (symbols.Any(x => x.ToDisplayString() == deltaType.ToDisplayString()))
return symbols;
return symbols.Append(ComponentDeltaInterfaceName);
return symbols.Append(deltaType);
}
// TODO serveronly? do we care? who knows!!

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -94,13 +93,13 @@ internal static class Types
return false;
}
internal static IEnumerable<string> GetImplicitDataDefinitionInterfaces(ITypeSymbol type, bool all)
internal static IEnumerable<ITypeSymbol> GetImplicitDataDefinitionInterfaces(ITypeSymbol type, bool all)
{
var interfaces = all ? type.AllInterfaces : type.Interfaces;
foreach (var @interface in interfaces)
{
if (IsImplicitDataDefinitionInterface(@interface))
yield return @interface.ToDisplayString();
yield return @interface;
}
}

View File

@@ -20,10 +20,6 @@ namespace Robust.Server.GameStates;
/// </summary>
internal sealed class PvsSession(ICommonSession session, ResizableMemoryRegion<PvsData> memoryRegion)
{
#if DEBUG
public HashSet<NetEntity> ToSendSet = new();
#endif
public readonly ICommonSession Session = session;
public readonly ResizableMemoryRegion<PvsData> DataMemory = memoryRegion;
@@ -201,7 +197,7 @@ internal struct PvsMetadata
{
DebugTools.AssertEqual(NetEntity, comp.NetEntity);
DebugTools.AssertEqual(VisMask, comp.VisibilityMask);
DebugTools.Assert(LifeStage == comp.EntityLifeStage);
DebugTools.AssertEqual(LifeStage, comp.EntityLifeStage);
DebugTools.Assert(LastModifiedTick == comp.EntityLastModifiedTick || LastModifiedTick.Value == 0);
}
}

View File

@@ -5,14 +5,13 @@ using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.Utility;
namespace Robust.Server.GameStates;
public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
public sealed class PvsOverrideSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IConsoleHost _console = default!;
@@ -135,10 +134,8 @@ public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations,
/// causing them to always be sent to all clients.
/// </summary>
public override void AddGlobalOverride(EntityUid uid)
public void AddGlobalOverride(EntityUid uid)
{
base.AddGlobalOverride(uid);
if (GlobalOverride.Add(uid))
_hasOverride.Add(uid);
}
@@ -146,10 +143,8 @@ public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
/// <summary>
/// Removes an entity from the global overrides.
/// </summary>
public override void RemoveGlobalOverride(EntityUid uid)
public void RemoveGlobalOverride(EntityUid uid)
{
base.RemoveGlobalOverride(uid);
GlobalOverride.Remove(uid);
// Not bothering to clear _hasOverride, as we'd have to check all the other collections, and at that point we
// might as well just do that when the entity gets deleted anyways.
@@ -208,10 +203,8 @@ public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations for a
/// specific session.
/// </summary>
public override void AddSessionOverride(EntityUid uid, ICommonSession session)
public void AddSessionOverride(EntityUid uid, ICommonSession session)
{
base.AddSessionOverride(uid, session);
if (SessionOverrides.GetOrNew(session).Add(uid))
_hasOverride.Add(uid);
}
@@ -219,10 +212,8 @@ public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
/// <summary>
/// Removes an entity from a session's overrides.
/// </summary>
public override void RemoveSessionOverride(EntityUid uid, ICommonSession session)
public void RemoveSessionOverride(EntityUid uid, ICommonSession session)
{
base.RemoveSessionOverride(uid, session);
if (!SessionOverrides.TryGetValue(session, out var overrides))
return;
@@ -237,10 +228,8 @@ public sealed class PvsOverrideSystem : SharedPvsOverrideSystem
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations,
/// causing them to always be sent to all clients.
/// </summary>
public override void AddSessionOverrides(EntityUid uid, Filter filter)
public void AddSessionOverrides(EntityUid uid, Filter filter)
{
base.AddSessionOverrides(uid, filter);
foreach (var session in filter.Recipients)
{
AddSessionOverride(uid, session);

View File

@@ -302,9 +302,7 @@ internal sealed partial class PvsSystem : EntitySystem
// Process all entities in visible PVS chunks
AddPvsChunks(session);
#if DEBUG
VerifySessionData(session);
#endif
var toSend = session.ToSend!;
session.ToSend = null;
@@ -334,12 +332,11 @@ internal sealed partial class PvsSystem : EntitySystem
session.Overflow = oldEntry.Value;
}
#if DEBUG
[Conditional("DEBUG")]
private void VerifySessionData(PvsSession pvsSession)
{
var toSend = pvsSession.ToSend!;
var toSendSet = pvsSession.ToSendSet;
toSendSet.Clear();
var toSend = pvsSession.ToSend;
var toSendSet = new HashSet<NetEntity>(toSend!.Count);
foreach (var intPtr in toSend)
{
@@ -363,7 +360,6 @@ internal sealed partial class PvsSystem : EntitySystem
|| data.LastSeen == _gameTiming.CurTick - 1);
}
}
#endif
private (Vector2 worldPos, float range, EntityUid? map) CalcViewBounds(Entity<TransformComponent, EyeComponent?> eye)
{

View File

@@ -39,6 +39,7 @@ namespace Robust.Shared.Maths
/// <param name="dir"></param>
public Angle(Vector2 dir)
{
dir = dir.Normalized();
Theta = Math.Atan2(dir.Y, dir.X);
}

View File

@@ -197,10 +197,6 @@ public abstract partial class SharedContainerSystem
{
if (entity.Comp2 is { } physics)
{
// TODO CONTAINER
// Is this actually needed?
// I.e., shouldn't this just do a if (_timing.ApplyingState) return
// Here we intentionally don't dirty the physics comp. Client-side state handling will apply these same
// changes. This also ensures that the server doesn't have to send the physics comp state to every
// player for any entity inside of a container during init.

View File

@@ -11,7 +11,7 @@ namespace Robust.Shared.GameObjects
/// </summary>
public abstract class BoundUserInterface : IDisposable
{
[Dependency] protected internal readonly IEntityManager EntMan = default!;
[Dependency] protected readonly IEntityManager EntMan = default!;
[Dependency] protected readonly ISharedPlayerManager PlayerManager = default!;
protected readonly SharedUserInterfaceSystem UiSystem;
@@ -28,6 +28,13 @@ namespace Robust.Shared.GameObjects
/// </summary>
protected internal BoundUserInterfaceState? State { get; internal set; }
// Bandaid just for storage :)
/// <summary>
/// Defers state handling
/// </summary>
[Obsolete]
public virtual bool DeferredClose { get; } = true;
protected BoundUserInterface(EntityUid owner, Enum uiKey)
{
IoCManager.InjectDependencies(this);

View File

@@ -1,4 +1,3 @@
using System;
using Robust.Shared.Timing;
namespace Robust.Shared.GameObjects;
@@ -39,14 +38,11 @@ public abstract partial class EntityManager
Dirty(uid, comp, metadata);
}
public virtual void DirtyField<T>(EntityUid uid, T comp, string fieldName, MetaDataComponent? metadata = null)
public void DirtyField<T>(EntityUid uid, T comp, string fieldName, MetaDataComponent? metadata = null)
where T : IComponentDelta
{
var compReg = ComponentFactory.GetRegistration(CompIdx.Index<T>());
// TODO
// consider storing this on MetaDataComponent?
// We alsready store other dirtying information there anyways, and avoids having to fetch the registration.
if (!compReg.NetworkedFieldLookup.TryGetValue(fieldName, out var idx))
{
_sawmill.Error($"Tried to dirty delta field {fieldName} on {ToPrettyString(uid)} that isn't implemented.");
@@ -58,24 +54,6 @@ public abstract partial class EntityManager
comp.LastModifiedFields[idx] = curTick;
Dirty(uid, comp, metadata);
}
public virtual void DirtyFields<T>(EntityUid uid, T comp, MetaDataComponent? meta, params ReadOnlySpan<string> fields)
where T : IComponentDelta
{
var compReg = ComponentFactory.GetRegistration(CompIdx.Index<T>());
var curTick = _gameTiming.CurTick;
foreach (var field in fields)
{
if (!compReg.NetworkedFieldLookup.TryGetValue(field, out var idx))
_sawmill.Error($"Tried to dirty delta field {field} on {ToPrettyString(uid)} that isn't implemented.");
else
comp.LastModifiedFields[idx] = curTick;
}
comp.LastFieldUpdate = curTick;
Dirty(uid, comp, meta);
}
}
/// <summary>

View File

@@ -1694,6 +1694,7 @@ namespace Robust.Shared.GameObjects
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public bool Resolve(EntityUid uid, [NotNullWhen(true)] ref TComp1? component, bool logMissing = true)
{
if (component != null)
@@ -1716,7 +1717,7 @@ namespace Robust.Shared.GameObjects
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public bool Resolve(ref Entity<TComp1?> entity, bool logMissing = true)
{
return Resolve(entity.Owner, ref entity.Comp, logMissing);

View File

@@ -171,13 +171,6 @@ public partial class EntitySystem
EntityManager.DirtyField(uid, component, fieldName, meta);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void DirtyFields<T>(EntityUid uid, T comp, MetaDataComponent? meta, params ReadOnlySpan<string> fields)
where T : IComponentDelta
{
EntityManager.DirtyFields(uid, comp, meta);
}
/// <summary>
/// Marks a component as dirty. This also implicitly dirties the entity this component belongs to.
/// </summary>

View File

@@ -26,7 +26,7 @@ namespace Robust.Shared.GameObjects
public void SetEnabled(EntityUid uid, bool enabled, CollisionWakeComponent? component = null)
{
if (!_query.Resolve(uid, ref component, false) || component.Enabled == enabled)
if (!_query.Resolve(uid, ref component) || component.Enabled == enabled)
return;
component.Enabled = enabled;

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Robust.Shared.Collections;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
@@ -9,6 +11,7 @@ using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Shapes;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Utility;
@@ -74,14 +77,14 @@ public sealed partial class EntityLookupSystem
/// <summary>
/// Wrapper around the per-grid version.
/// </summary>
private void AddEntitiesIntersecting<T>(MapId mapId,
private void AddEntitiesIntersecting(MapId mapId,
HashSet<EntityUid> intersecting,
T shape,
IPhysShape shape,
Transform shapeTransform,
LookupFlags flags) where T : IPhysShape
LookupFlags flags)
{
var worldAABB = shape.ComputeAABB(shapeTransform, 0);
var state = new EntityQueryState<T>(intersecting,
var state = new EntityQueryState(intersecting,
shape,
shapeTransform,
_fixtures,
@@ -93,7 +96,7 @@ public sealed partial class EntityLookupSystem
// Need to include maps
_mapManager.FindGridsIntersecting(mapId, worldAABB, ref state,
static (EntityUid uid, MapGridComponent _, ref EntityQueryState<T> state) =>
static (EntityUid uid, MapGridComponent _, ref EntityQueryState state) =>
{
var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, uid);
var localAabb = state.Shape.ComputeAABB(localTransform, 0);
@@ -109,19 +112,19 @@ public sealed partial class EntityLookupSystem
AddContained(intersecting, flags);
}
private void AddEntitiesIntersecting<T>(
private void AddEntitiesIntersecting(
EntityUid lookupUid,
HashSet<EntityUid> intersecting,
T shape,
IPhysShape shape,
Box2 localAABB,
Transform localShapeTransform,
LookupFlags flags,
BroadphaseComponent? lookup = null) where T : IPhysShape
BroadphaseComponent? lookup = null)
{
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return;
var state = new EntityQueryState<T>(
var state = new EntityQueryState(
intersecting,
shape,
localShapeTransform,
@@ -154,7 +157,7 @@ public sealed partial class EntityLookupSystem
return;
static bool PhysicsQuery(ref EntityQueryState<T> state, in FixtureProxy value)
static bool PhysicsQuery(ref EntityQueryState state, in FixtureProxy value)
{
var sensors = (state.Flags & LookupFlags.Sensors) != 0x0;
@@ -176,7 +179,7 @@ public sealed partial class EntityLookupSystem
return true;
}
static bool SundriesQuery(ref EntityQueryState<T> state, in EntityUid value)
static bool SundriesQuery(ref EntityQueryState state, in EntityUid value)
{
var approx = (state.Flags & LookupFlags.Approximate) != 0x0;
@@ -224,14 +227,14 @@ public sealed partial class EntityLookupSystem
/// <summary>
/// Wrapper around the per-grid version.
/// </summary>
private bool AnyEntitiesIntersecting<T>(MapId mapId,
T shape,
private bool AnyEntitiesIntersecting(MapId mapId,
IPhysShape shape,
Transform shapeTransform,
LookupFlags flags,
EntityUid? ignored = null) where T : IPhysShape
EntityUid? ignored = null)
{
var worldAABB = shape.ComputeAABB(shapeTransform, 0);
var state = new AnyEntityQueryState<T>(false,
var state = new AnyEntityQueryState(false,
ignored,
shape,
shapeTransform,
@@ -244,7 +247,7 @@ public sealed partial class EntityLookupSystem
// Need to include maps
_mapManager.FindGridsIntersecting(mapId, worldAABB, ref state,
static (EntityUid uid, MapGridComponent _, ref AnyEntityQueryState<T> state) =>
static (EntityUid uid, MapGridComponent _, ref AnyEntityQueryState state) =>
{
var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, uid);
var localAabb = state.Shape.ComputeAABB(localTransform, 0);
@@ -269,18 +272,18 @@ public sealed partial class EntityLookupSystem
return state.Found;
}
private bool AnyEntitiesIntersecting<T>(EntityUid lookupUid,
T shape,
private bool AnyEntitiesIntersecting(EntityUid lookupUid,
IPhysShape shape,
Box2 localAABB,
Transform shapeTransform,
LookupFlags flags,
EntityUid? ignored = null,
BroadphaseComponent? lookup = null) where T : IPhysShape
BroadphaseComponent? lookup = null)
{
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return false;
var state = new AnyEntityQueryState<T>(false,
var state = new AnyEntityQueryState(false,
ignored,
shape,
shapeTransform,
@@ -322,7 +325,7 @@ public sealed partial class EntityLookupSystem
return state.Found;
static bool PhysicsQuery(ref AnyEntityQueryState<T> state, in FixtureProxy value)
static bool PhysicsQuery(ref AnyEntityQueryState state, in FixtureProxy value)
{
if (state.Ignored == value.Entity)
return true;
@@ -347,7 +350,7 @@ public sealed partial class EntityLookupSystem
return false;
}
static bool SundriesQuery(ref AnyEntityQueryState<T> state, in EntityUid value)
static bool SundriesQuery(ref AnyEntityQueryState state, in EntityUid value)
{
if (state.Ignored == value)
return true;
@@ -406,16 +409,9 @@ public sealed partial class EntityLookupSystem
var broadphaseInv = _transform.GetInvWorldMatrix(lookupUid);
var localBounds = broadphaseInv.TransformBounds(worldBounds);
var polygon = _physics.GetPooled(localBounds);
var result = AnyEntitiesIntersecting(lookupUid,
polygon,
localBounds.CalcBoundingBox(),
Physics.Transform.Empty,
flags,
ignored);
var shape = new Polygon(localBounds);
_physics.ReturnPooled(polygon);
return result;
return AnyEntitiesIntersecting(lookupUid, shape, localBounds.CalcBoundingBox(), Physics.Transform.Empty, flags, ignored);
}
#endregion
@@ -458,10 +454,8 @@ public sealed partial class EntityLookupSystem
{
if (mapId == MapId.Nullspace) return false;
var polygon = _physics.GetPooled(worldAABB);
var result = AnyEntitiesIntersecting(mapId, polygon, Physics.Transform.Empty, flags);
_physics.ReturnPooled(polygon);
return result;
var shape = new Polygon(worldAABB);
return AnyEntitiesIntersecting(mapId, shape, Physics.Transform.Empty, flags);
}
public HashSet<EntityUid> GetEntitiesIntersecting(MapId mapId, Box2 worldAABB, LookupFlags flags = DefaultFlags)
@@ -475,9 +469,8 @@ public sealed partial class EntityLookupSystem
{
if (mapId == MapId.Nullspace) return;
var polygon = _physics.GetPooled(worldAABB);
AddEntitiesIntersecting(mapId, intersecting, polygon, Physics.Transform.Empty, flags);
_physics.ReturnPooled(polygon);
var shape = new Polygon(worldAABB);
AddEntitiesIntersecting(mapId, intersecting, shape, Physics.Transform.Empty, flags);
}
#endregion
@@ -487,18 +480,15 @@ public sealed partial class EntityLookupSystem
public bool AnyEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, LookupFlags flags = DefaultFlags)
{
// Don't need to check contained entities as they have the same bounds as the parent.
var polygon = _physics.GetPooled(worldBounds);
var result = AnyEntitiesIntersecting(mapId, polygon, Physics.Transform.Empty, flags);
_physics.ReturnPooled(polygon);
return result;
var shape = new Polygon(worldBounds);
return AnyEntitiesIntersecting(mapId, shape, Physics.Transform.Empty, flags);
}
public HashSet<EntityUid> GetEntitiesIntersecting(MapId mapId, Box2Rotated worldBounds, LookupFlags flags = DefaultFlags)
{
var intersecting = new HashSet<EntityUid>();
var polygon = _physics.GetPooled(worldBounds);
AddEntitiesIntersecting(mapId, intersecting, polygon, Physics.Transform.Empty, flags);
_physics.ReturnPooled(polygon);
var shape = new Polygon(worldBounds);
AddEntitiesIntersecting(mapId, intersecting, shape, Physics.Transform.Empty, flags);
return intersecting;
}
@@ -713,12 +703,12 @@ public sealed partial class EntityLookupSystem
return entities;
}
public void GetEntitiesIntersecting<T>(
public void GetEntitiesIntersecting(
MapId mapId,
T shape,
IPhysShape shape,
Transform transform,
HashSet<EntityUid> entities,
LookupFlags flags = LookupFlags.All) where T : IPhysShape
LookupFlags flags = LookupFlags.All)
{
if (mapId == MapId.Nullspace)
return;
@@ -761,11 +751,10 @@ public sealed partial class EntityLookupSystem
return;
var localAABB = _transform.GetInvWorldMatrix(gridId).TransformBox(worldAABB);
var polygon = _physics.GetPooled(localAABB);
var shape = new Polygon(localAABB);
AddEntitiesIntersecting(gridId, intersecting, polygon, localAABB, Physics.Transform.Empty, flags, lookup);
AddEntitiesIntersecting(gridId, intersecting, shape, localAABB, Physics.Transform.Empty, flags, lookup);
AddContained(intersecting, flags);
_physics.ReturnPooled(polygon);
}
public void GetEntitiesIntersecting(EntityUid gridId, Box2Rotated worldBounds, HashSet<EntityUid> intersecting, LookupFlags flags = DefaultFlags)
@@ -774,11 +763,10 @@ public sealed partial class EntityLookupSystem
return;
var localBounds = _transform.GetInvWorldMatrix(gridId).TransformBounds(worldBounds);
var polygon = _physics.GetPooled(localBounds);
var shape = new Polygon(localBounds);
AddEntitiesIntersecting(gridId, intersecting, polygon, localBounds.CalcBoundingBox(), Physics.Transform.Empty, flags, lookup);
AddEntitiesIntersecting(gridId, intersecting, shape, localBounds.CalcBoundingBox(), Physics.Transform.Empty, flags, lookup);
AddContained(intersecting, flags);
_physics.ReturnPooled(polygon);
}
#endregion
@@ -844,10 +832,10 @@ public sealed partial class EntityLookupSystem
#endregion
private record struct AnyEntityQueryState<T>(
private record struct AnyEntityQueryState(
bool Found,
EntityUid? Ignored,
T Shape,
IPhysShape Shape,
Transform Transform,
FixtureSystem Fixtures,
EntityLookupSystem Lookup,
@@ -855,11 +843,11 @@ public sealed partial class EntityLookupSystem
IManifoldManager Manifolds,
EntityQuery<FixturesComponent> FixturesQuery,
LookupFlags Flags
) where T : IPhysShape;
);
private readonly record struct EntityQueryState<T>(
private readonly record struct EntityQueryState(
HashSet<EntityUid> Intersecting,
T Shape,
IPhysShape Shape,
Transform Transform,
FixtureSystem Fixtures,
EntityLookupSystem Lookup,
@@ -867,5 +855,5 @@ public sealed partial class EntityLookupSystem
IManifoldManager Manifolds,
EntityQuery<FixturesComponent> FixturesQuery,
LookupFlags Flags
) where T : IPhysShape;
);
}

View File

@@ -67,7 +67,7 @@ public sealed partial class EntityLookupSystem
/// <summary>
/// Common method to determine if an entity overlaps the specified shape.
/// </summary>
private bool IsIntersecting<TShape>(MapId mapId, EntityUid uid, TransformComponent xform, TShape shape, Transform shapeTransform, Box2 worldAABB, LookupFlags flags) where TShape : IPhysShape
private bool IsIntersecting(MapId mapId, EntityUid uid, TransformComponent xform, IPhysShape shape, Transform shapeTransform, Box2 worldAABB, LookupFlags flags)
{
var (entPos, entRot) = _transform.GetWorldPositionRotation(xform);
@@ -121,27 +121,25 @@ public sealed partial class EntityLookupSystem
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return;
var polygon = _physics.GetPooled(localAABB);
AddEntitiesIntersecting(lookupUid, intersecting, polygon, localAABB, Physics.Transform.Empty, flags, query, lookup);
_physics.ReturnPooled(polygon);
var lookupPoly = new Polygon(localAABB);
AddEntitiesIntersecting(lookupUid, intersecting, lookupPoly, localAABB, Physics.Transform.Empty, flags, query, lookup);
}
private void AddEntitiesIntersecting<T, TShape>(
private void AddEntitiesIntersecting<T>(
EntityUid lookupUid,
HashSet<Entity<T>> intersecting,
TShape shape,
IPhysShape shape,
Box2 localAABB,
Transform localTransform,
LookupFlags flags,
EntityQuery<T> query,
BroadphaseComponent? lookup = null)
where T : IComponent
where TShape : IPhysShape
BroadphaseComponent? lookup = null) where T : IComponent
{
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return;
var state = new QueryState<T, TShape>(
var state = new QueryState<T>(
intersecting,
shape,
localTransform,
@@ -176,7 +174,7 @@ public sealed partial class EntityLookupSystem
return;
static bool PhysicsQuery(ref QueryState<T, TShape> state, in FixtureProxy value)
static bool PhysicsQuery(ref QueryState<T> state, in FixtureProxy value)
{
if (!state.Sensors && !value.Fixture.Hard)
return true;
@@ -197,7 +195,7 @@ public sealed partial class EntityLookupSystem
return true;
}
static bool SundriesQuery(ref QueryState<T, TShape> state, in EntityUid value)
static bool SundriesQuery(ref QueryState<T> state, in EntityUid value)
{
if (!state.Query.TryGetComponent(value, out var comp))
return true;
@@ -252,26 +250,22 @@ public sealed partial class EntityLookupSystem
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return false;
var polygon = _physics.GetPooled(localAABB);
var shape = new Polygon(localAABB);
var (lookupPos, lookupRot) = _transform.GetWorldPositionRotation(lookupUid);
var transform = new Transform(lookupPos, lookupRot);
var result = AnyComponentsIntersecting(lookupUid, polygon, localAABB, transform, flags, query, ignored, lookup);
_physics.ReturnPooled(polygon);
return result;
return AnyComponentsIntersecting(lookupUid, shape, localAABB, transform, flags, query, ignored, lookup);
}
private bool AnyComponentsIntersecting<T, TShape>(
private bool AnyComponentsIntersecting<T>(
EntityUid lookupUid,
TShape shape,
IPhysShape shape,
Box2 localAABB,
Transform shapeTransform,
LookupFlags flags,
EntityQuery<T> query,
EntityUid? ignored = null,
BroadphaseComponent? lookup = null)
where T : IComponent
where TShape : IPhysShape
BroadphaseComponent? lookup = null) where T : IComponent
{
/*
* Unfortunately this is split from the other query as we can short-circuit here, hence the code duplication.
@@ -281,7 +275,7 @@ public sealed partial class EntityLookupSystem
if (!_broadQuery.Resolve(lookupUid, ref lookup))
return false;
var state = new AnyQueryState<T, TShape>(false,
var state = new AnyQueryState<T>(false,
ignored,
shape,
shapeTransform,
@@ -323,7 +317,7 @@ public sealed partial class EntityLookupSystem
return state.Found;
static bool PhysicsQuery(ref AnyQueryState<T, TShape> state, in FixtureProxy value)
static bool PhysicsQuery(ref AnyQueryState<T> state, in FixtureProxy value)
{
if (value.Entity == state.Ignored)
return true;
@@ -348,7 +342,7 @@ public sealed partial class EntityLookupSystem
return false;
}
static bool SundriesQuery(ref AnyQueryState<T, TShape> state, in EntityUid value)
static bool SundriesQuery(ref AnyQueryState<T> state, in EntityUid value)
{
if (state.Ignored == value)
return true;
@@ -427,14 +421,13 @@ public sealed partial class EntityLookupSystem
public bool AnyComponentsIntersecting(Type type, MapId mapId, Box2 worldAABB, EntityUid? ignored = null, LookupFlags flags = DefaultFlags)
{
var polygon = _physics.GetPooled(worldAABB);
var shape = new Polygon(worldAABB);
var transform = Physics.Transform.Empty;
var result = AnyComponentsIntersecting(type, mapId, polygon, transform, ignored, flags);
_physics.ReturnPooled(polygon);
return result;
return AnyComponentsIntersecting(type, mapId, shape, transform, ignored, flags);
}
public bool AnyComponentsIntersecting<T>(Type type, MapId mapId, T shape, Transform shapeTransform, EntityUid? ignored = null, LookupFlags flags = DefaultFlags) where T : IPhysShape
public bool AnyComponentsIntersecting(Type type, MapId mapId, IPhysShape shape, Transform shapeTransform, EntityUid? ignored = null, LookupFlags flags = DefaultFlags)
{
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type));
if (mapId == MapId.Nullspace)
@@ -496,40 +489,37 @@ public sealed partial class EntityLookupSystem
if (mapId == MapId.Nullspace)
return;
var polygon = _physics.GetPooled(worldAABB);
var shape = new Polygon(worldAABB);
var transform = Physics.Transform.Empty;
GetEntitiesIntersecting(type, mapId, polygon, transform, intersecting, flags);
_physics.ReturnPooled(polygon);
GetEntitiesIntersecting(type, mapId, shape, transform, intersecting, flags);
}
public void GetEntitiesIntersecting<T>(MapId mapId, Box2Rotated worldBounds, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags) where T : IComponent
{
if (mapId == MapId.Nullspace) return;
var polygon = _physics.GetPooled(worldBounds);
var shape = new Polygon(worldBounds);
var shapeTransform = Physics.Transform.Empty;
GetEntitiesIntersecting(mapId, polygon, shapeTransform, entities, flags);
_physics.ReturnPooled(polygon);
GetEntitiesIntersecting(mapId, shape, shapeTransform, entities, flags);
}
public void GetEntitiesIntersecting<T>(MapId mapId, Box2 worldAABB, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags) where T : IComponent
{
if (mapId == MapId.Nullspace) return;
var polygon = _physics.GetPooled(worldAABB);
var shape = new Polygon(worldAABB);
var shapeTransform = Physics.Transform.Empty;
GetEntitiesIntersecting(mapId, polygon, shapeTransform, entities, flags);
_physics.ReturnPooled(polygon);
GetEntitiesIntersecting(mapId, shape, shapeTransform, entities, flags);
}
#endregion
#region IPhysShape
public void GetEntitiesIntersecting<T>(Type type, MapId mapId, T shape, Transform shapeTransform, HashSet<Entity<IComponent>> intersecting, LookupFlags flags = DefaultFlags) where T : IPhysShape
public void GetEntitiesIntersecting(Type type, MapId mapId, IPhysShape shape, Transform shapeTransform, HashSet<Entity<IComponent>> intersecting, LookupFlags flags = DefaultFlags)
{
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type));
if (mapId == MapId.Nullspace)
@@ -554,10 +544,10 @@ public sealed partial class EntityLookupSystem
var query = EntityManager.GetEntityQuery(type);
// Get grid entities
var state = new GridQueryState<IComponent, T>(intersecting, shape, shapeTransform, this, _physics, flags, query);
var state = new GridQueryState<IComponent>(intersecting, shape, shapeTransform, this, _physics, flags, query);
_mapManager.FindGridsIntersecting(mapId, worldAABB, ref state,
static (EntityUid uid, MapGridComponent grid, ref GridQueryState<IComponent, T> state) =>
static (EntityUid uid, MapGridComponent grid, ref GridQueryState<IComponent> state) =>
{
var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, uid);
var localAabb = state.Shape.ComputeAABB(localTransform, 0);
@@ -575,9 +565,7 @@ public sealed partial class EntityLookupSystem
}
}
public void GetEntitiesIntersecting<T, TShape>(MapId mapId, TShape shape, Transform shapeTransform, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags)
where T : IComponent
where TShape : IPhysShape
public void GetEntitiesIntersecting<T>(MapId mapId, IPhysShape shape, Transform shapeTransform, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags) where T : IComponent
{
if (mapId == MapId.Nullspace) return;
@@ -600,10 +588,10 @@ public sealed partial class EntityLookupSystem
var query = GetEntityQuery<T>();
// Get grid entities
var state = new GridQueryState<T, TShape>(entities, shape, shapeTransform, this, _physics, flags, query);
var state = new GridQueryState<T>(entities, shape, shapeTransform, this, _physics, flags, query);
_mapManager.FindGridsIntersecting(mapId, worldAABB, ref state,
static (EntityUid uid, MapGridComponent grid, ref GridQueryState<T, TShape> state) =>
static (EntityUid uid, MapGridComponent grid, ref GridQueryState<T> state) =>
{
var localTransform = state.Physics.GetRelativePhysicsTransform(state.Transform, uid);
var localAabb = state.Shape.ComputeAABB(localTransform, 0);
@@ -691,9 +679,7 @@ public sealed partial class EntityLookupSystem
GetEntitiesInRange(mapId, shape, transform, entities, flags);
}
public void GetEntitiesInRange<T, TShape>(MapId mapId, TShape shape, Transform transform, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags)
where T : IComponent
where TShape : IPhysShape
public void GetEntitiesInRange<T>(MapId mapId, IPhysShape shape, Transform transform, HashSet<Entity<T>> entities, LookupFlags flags = DefaultFlags) where T : IComponent
{
DebugTools.Assert(shape.Radius > 0, "Range must be a positive float");
@@ -843,21 +829,20 @@ public sealed partial class EntityLookupSystem
}
}
private readonly record struct GridQueryState<T, TShape>(
private readonly record struct GridQueryState<T>(
HashSet<Entity<T>> Intersecting,
TShape Shape,
IPhysShape Shape,
Transform Transform,
EntityLookupSystem Lookup,
SharedPhysicsSystem Physics,
LookupFlags Flags,
EntityQuery<T> Query
) where T : IComponent
where TShape : IPhysShape;
) where T : IComponent;
private record struct AnyQueryState<T, TShape>(
private record struct AnyQueryState<T>(
bool Found,
EntityUid? Ignored,
TShape Shape,
IPhysShape Shape,
Transform Transform,
FixtureSystem Fixtures,
SharedPhysicsSystem Physics,
@@ -865,12 +850,11 @@ public sealed partial class EntityLookupSystem
EntityQuery<T> Query,
EntityQuery<FixturesComponent> FixturesQuery,
LookupFlags Flags
) where T : IComponent
where TShape : IPhysShape;
) where T : IComponent;
private readonly record struct QueryState<T, TShape>(
private readonly record struct QueryState<T>(
HashSet<Entity<T>> Intersecting,
TShape Shape,
IPhysShape Shape,
Transform Transform,
FixtureSystem Fixtures,
SharedPhysicsSystem Physics,
@@ -879,6 +863,5 @@ public sealed partial class EntityLookupSystem
EntityQuery<FixturesComponent> FixturesQuery,
bool Sensors,
bool Approximate
) where T : IComponent
where TShape : IPhysShape;
) where T : IComponent;
}

View File

@@ -312,7 +312,7 @@ public abstract partial class SharedMapSystem
if (data.IsDeleted())
{
if (!component.Chunks.Remove(index, out var deletedChunk))
if (!component.Chunks.TryGetValue(index, out var deletedChunk))
return;
// Deleted chunks still need to raise tile-changed events.
@@ -330,13 +330,11 @@ public abstract partial class SharedMapSystem
}
}
component.Chunks.Remove(index);
return;
}
var chunk = GetOrAddChunk(uid, component, index);
chunk.Fixtures.Clear();
chunk.Fixtures.UnionWith(data.Fixtures);
chunk.SuppressCollisionRegeneration = true;
DebugTools.Assert(data.TileData.Any(x => !x.IsEmpty));
DebugTools.Assert(data.TileData.Length == component.ChunkSize * component.ChunkSize);
@@ -357,6 +355,12 @@ public abstract partial class SharedMapSystem
// These should never refer to the same object
DebugTools.AssertNotEqual(chunk.Fixtures, data.Fixtures);
if (!chunk.Fixtures.SetEquals(data.Fixtures))
{
chunk.Fixtures.Clear();
chunk.Fixtures.UnionWith(data.Fixtures);
}
chunk.CachedBounds = data.CachedBounds!.Value;
chunk.SuppressCollisionRegeneration = false;
}

View File

@@ -289,26 +289,18 @@ public abstract partial class SharedTransformSystem
EntityUid uid,
TransformComponent xform)
{
if (xform._gridInitialized)
// Dont set pre-init, as the map grid component might not have been added yet.
if (xform._gridInitialized || xform.LifeStage < ComponentLifeStage.Initializing)
return;
xform._gridInitialized = true;
DebugTools.Assert(xform.GridUid == null);
if (_gridQuery.HasComponent(uid))
{
xform._gridUid = uid;
xform._gridInitialized = true;
return;
}
// We don't set _gridInitialized to true unless the transform (and hence entity) is already being initialized,
// as otherwise the current entity's grid component might just not have been added yet.
//
// We don't just return early, on the off chance that what is happening here is some convoluted entity
// initialization pasta, where an an entity has been attached to an un-initialized entity on an already
// initialized grid. In that case, the newly attached entity needs to be able to figure out the new grid id.
// AFAIK this shouldn't happen anymore, but might as well keep this just in case.
if (xform.LifeStage >= ComponentLifeStage.Initializing)
xform._gridInitialized = true;
if (!xform._parent.IsValid())
return;
@@ -734,11 +726,6 @@ public abstract partial class SharedTransformSystem
{
if (args.Current is TransformComponentState newState)
{
// TODO Delta-states
// If the transform component ever gets delta states, then the client state manager needs to be updated.
// Currently it explicitly looks for a "TransformComponentState" when determining an entity's parent for the
// sake of sorting the states that need to be applied base on the transform hierarchy.
var parent = EnsureEntity<TransformComponent>(newState.ParentID, uid);
var oldAnchored = xform.Anchored;
@@ -912,11 +899,11 @@ public abstract partial class SharedTransformSystem
_mapManager.TryFindGridAt(mapUid, coordinates.Position, out var targetGrid, out _))
{
var invWorldMatrix = GetInvWorldMatrix(targetGrid);
SetCoordinates((entity.Owner, entity.Comp, MetaData(entity.Owner)), new EntityCoordinates(targetGrid, Vector2.Transform(coordinates.Position, invWorldMatrix)));
SetCoordinates(entity, new EntityCoordinates(targetGrid, Vector2.Transform(coordinates.Position, invWorldMatrix)));
}
else
{
SetCoordinates((entity.Owner, entity.Comp, MetaData(entity.Owner)), new EntityCoordinates(mapUid, coordinates.Position));
SetCoordinates(entity, new EntityCoordinates(mapUid, coordinates.Position));
}
}

View File

@@ -316,10 +316,6 @@ namespace Robust.Shared.GameObjects
/// Current parent entity of this entity.
/// </summary>
public readonly NetEntity ParentID;
// TODO Delta-states
// If the transform component ever gets delta states, then the client state manager needs to be updated.
// Currently it explicitly looks for a "TransformComponentState" when determining an entity's parent for the
// sake of sorting the states that need to be applied base on the transform hierarchy.
/// <summary>
/// Current position offset of the entity.

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using Robust.Shared.Collections;
@@ -37,16 +36,9 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
private ActorRangeCheckJob _rangeJob;
/// <summary>
/// Defer BUIs during state handling so client doesn't spam a BUI constantly during prediction.
/// Defer closing BUIs during state handling so client doesn't spam a BUI constantly during prediction.
/// </summary>
private readonly List<(BoundUserInterface Bui, bool value)> _queuedBuis = new();
/// <summary>
/// Temporary storage for BUI keys
/// </summary>
private ValueList<Enum> _keys = new();
private ValueList<EntityUid> _entList = new();
private readonly List<BoundUserInterface> _queuedCloses = new();
public override void Initialize()
{
@@ -89,11 +81,6 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentShutdown>(OnActorShutdown);
}
private void AddQueued(BoundUserInterface bui, bool value)
{
_queuedBuis.Add((bui, value));
}
/// <summary>
/// Validates the received message, and then pass it onto systems/components
/// </summary>
@@ -240,7 +227,13 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
if (ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui))
{
AddQueued(cBui, false);
if (cBui.DeferredClose)
_queuedCloses.Add(cBui);
else
{
ent.Comp.ClientOpenInterfaces.Remove(key);
cBui.Dispose();
}
}
if (ent.Comp.Actors.Count == 0)
@@ -282,17 +275,24 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
// PlayerAttachedEvent will catch some of these.
foreach (var (key, bui) in ent.Comp.ClientOpenInterfaces)
{
AddQueued(bui, true);
bui.Open();
if (ent.Comp.States.TryGetValue(key, out var state))
{
bui.UpdateState(state);
bui.Update();
}
}
}
protected virtual void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
private void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
{
var actors = new List<EntityUid>();
foreach (var (key, acts) in ent.Comp.Actors)
{
_entList.Clear();
_entList.AddRange(acts);
foreach (var actor in _entList)
actors.Clear();
actors.AddRange(acts);
foreach (var actor in actors)
{
CloseUiInternal(ent!, key, actor);
DebugTools.Assert(!acts.Contains(actor));
@@ -301,7 +301,7 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
DebugTools.Assert(!ent.Comp.Actors.ContainsKey(key));
}
DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, false))));
DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedCloses.Contains(x)));
}
private void OnUserInterfaceGetState(Entity<UserInterfaceComponent> ent, ref ComponentGetState args)
@@ -463,7 +463,14 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
}
var bui = ent.Comp.ClientOpenInterfaces[key];
AddQueued(bui, false);
if (bui.DeferredClose)
_queuedCloses.Add(bui);
else
{
ent.Comp.ClientOpenInterfaces.Remove(key);
bui.Dispose();
}
}
}
@@ -520,7 +527,9 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
// Existing BUI just keep it.
if (entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var existing))
{
_queuedBuis.Remove((existing, false));
if (existing.DeferredClose)
_queuedCloses.Remove(existing);
return;
}
@@ -536,6 +545,10 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
// Try-catch to try prevent error loops / bricked clients that constantly throw exceptions while applying game
// states. E.g., stripping UI used to throw NREs in some instances while fetching the identity of unknown
// entities.
#if EXCEPTION_TOLERANCE
try
{
#endif
var type = _reflection.LooseGetType(data.ClientType);
var boundUserInterface = (BoundUserInterface) _factory.CreateInstance(type, [entity.Owner, key]);
entity.Comp.ClientOpenInterfaces[key] = boundUserInterface;
@@ -544,7 +557,23 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
if (!open)
return;
AddQueued(boundUserInterface, true);
boundUserInterface.Open();
if (entity.Comp.States.TryGetValue(key, out var buiState))
{
boundUserInterface.State = buiState;
boundUserInterface.UpdateState(buiState);
boundUserInterface.Update();
}
#if EXCEPTION_TOLERANCE
}
catch (Exception e)
{
Log.Error(
$"Caught exception while attempting to create a BUI {key} with type {data.ClientType} on entity {ToPrettyString(entity.Owner)}. Exception: {e}");
return;
}
#endif
}
/// <summary>
@@ -660,14 +689,6 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
return true;
}
/// <summary>
/// Opens the UI for the local client. Does nothing on server.
/// </summary>
public virtual void OpenUi(Entity<UserInterfaceComponent?> entity, Enum key, bool predicted = false)
{
}
public void OpenUi(Entity<UserInterfaceComponent?> entity, Enum key, EntityUid? actor, bool predicted = false)
{
if (actor == null || !UIQuery.Resolve(entity.Owner, ref entity.Comp, false))
@@ -705,23 +726,6 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
OpenUi(entity, key, actorEnt.Value, predicted);
}
/// <summary>
/// Tries to return the saved position of a user interface.
/// </summary>
public virtual bool TryGetPosition(Entity<UserInterfaceComponent?> entity, Enum key, out Vector2 position)
{
position = Vector2.Zero;
return false;
}
/// <summary>
/// Saves a position for the BUI.
/// </summary>
protected virtual void SavePosition(BoundUserInterface bui)
{
}
/// <summary>
/// Sets a BUI state and networks it to all clients.
/// </summary>
@@ -883,32 +887,6 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
RaiseNetworkEvent(new BoundUIWrapMessage(GetNetEntity(entity.Owner), message, key));
}
/// <summary>
/// Closes the user's UIs that match the specified key.
/// </summary>
public void CloseUserUis<T>(Entity<UserInterfaceUserComponent?> actor) where T: Enum
{
if (!UserQuery.Resolve(actor.Owner, ref actor.Comp, false))
return;
if (actor.Comp.OpenInterfaces.Count == 0)
return;
foreach (var (uid, enums) in actor.Comp.OpenInterfaces)
{
_keys.Clear();
_keys.AddRange(enums);
foreach (var weh in _keys)
{
if (weh is not T)
continue;
CloseUiInternal(uid, weh, actor.Owner);
}
}
}
/// <summary>
/// Closes all Uis for the actor.
/// </summary>
@@ -920,12 +898,13 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
if (actor.Comp.OpenInterfaces.Count == 0)
return;
var enumCopy = new ValueList<Enum>();
foreach (var (uid, enums) in actor.Comp.OpenInterfaces)
{
_keys.Clear();
_keys.AddRange(enums);
enumCopy.Clear();
enumCopy.AddRange(enums);
foreach (var key in _keys)
foreach (var key in enumCopy)
{
CloseUiInternal(uid, key, actor.Owner);
}
@@ -1060,48 +1039,17 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
{
if (_timing.IsFirstTimePredicted)
{
foreach (var (bui, open) in _queuedBuis)
foreach (var bui in _queuedCloses)
{
if (open)
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
bui.Open();
#if EXCEPTION_TOLERANCE
try
{
#endif
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
if (uiComp.States.TryGetValue(bui.UiKey, out var buiState))
{
bui.State = buiState;
bui.UpdateState(buiState);
bui.Update();
}
}
#if EXCEPTION_TOLERANCE
}
catch (Exception e)
{
Log.Error(
$"Caught exception while attempting to create a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}");
}
#endif
uiComp.ClientOpenInterfaces.Remove(bui.UiKey);
}
// Close BUI
else
{
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
uiComp.ClientOpenInterfaces.Remove(bui.UiKey);
}
SavePosition(bui);
bui.Dispose();
}
bui.Dispose();
}
_queuedBuis.Clear();
_queuedCloses.Clear();
}
var query = AllEntityQuery<ActiveUserInterfaceComponent, UserInterfaceComponent>();

View File

@@ -1,50 +0,0 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Player;
namespace Robust.Shared.GameStates;
public abstract class SharedPvsOverrideSystem : EntitySystem
{
/// <summary>
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations,
/// causing them to always be sent to all clients.
/// </summary>
public virtual void AddGlobalOverride(EntityUid uid)
{
}
/// <summary>
/// Removes an entity from the global overrides.
/// </summary>
public virtual void RemoveGlobalOverride(EntityUid uid)
{
}
/// <summary>
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations for a
/// specific session.
/// </summary>
public virtual void AddSessionOverride(EntityUid uid, ICommonSession session)
{
}
/// <summary>
/// Removes an entity from a session's overrides.
/// </summary>
public virtual void RemoveSessionOverride(EntityUid uid, ICommonSession session)
{
}
/// <summary>
/// Forces the entity, all of its parents, and all of its children to ignore normal PVS range limitations,
/// causing them to always be sent to all clients.
/// </summary>
public virtual void AddSessionOverrides(EntityUid uid, Filter filter)
{
}
}

View File

@@ -77,11 +77,11 @@ namespace Robust.Shared.Map
#region MapId
public void FindGridsIntersecting<T>(MapId mapId, T shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape;
public void FindGridsIntersecting(MapId mapId, IPhysShape shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = Approximate, bool includeMap = IncludeMap);
public void FindGridsIntersecting<T>(MapId mapId, T shape, Transform transform, GridCallback callback,
bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape;
public void FindGridsIntersecting(MapId mapId, IPhysShape shape, Transform transform, GridCallback callback,
bool approx = Approximate, bool includeMap = IncludeMap);
public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, GridCallback callback, bool approx = Approximate,
bool includeMap = IncludeMap);
@@ -107,11 +107,11 @@ namespace Robust.Shared.Map
#region MapEnt
public void FindGridsIntersecting<T>(EntityUid mapEnt, T shape, Transform transform, GridCallback callback,
bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape;
public void FindGridsIntersecting(EntityUid mapEnt, IPhysShape shape, Transform transform, GridCallback callback,
bool approx = Approximate, bool includeMap = IncludeMap);
public void FindGridsIntersecting<T, TState>(EntityUid mapEnt, T shape, Transform transform,
ref TState state, GridCallback<TState> callback, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape;
public void FindGridsIntersecting<TState>(EntityUid mapEnt, IPhysShape shape, Transform transform,
ref TState state, GridCallback<TState> callback, bool approx = Approximate, bool includeMap = IncludeMap);
/// <summary>
/// Returns true if any grids overlap the specified shapes.
@@ -119,8 +119,8 @@ namespace Robust.Shared.Map
public void FindGridsIntersecting(EntityUid mapEnt, List<IPhysShape> shapes, Transform transform,
ref List<Entity<MapGridComponent>> entities, bool approx = Approximate, bool includeMap = IncludeMap);
public void FindGridsIntersecting<T>(EntityUid mapEnt, T shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape;
public void FindGridsIntersecting(EntityUid mapEnt, IPhysShape shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = Approximate, bool includeMap = IncludeMap);
public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, GridCallback callback,
bool approx = Approximate, bool includeMap = IncludeMap);

View File

@@ -8,16 +8,17 @@ using Robust.Shared.Map.Enumerators;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Shapes;
namespace Robust.Shared.Map;
internal partial class MapManager
{
private bool IsIntersecting<T>(
private bool IsIntersecting(
ChunkEnumerator enumerator,
T shape,
IPhysShape shape,
Transform shapeTransform,
Entity<FixturesComponent> grid) where T : IPhysShape
Entity<FixturesComponent> grid)
{
var gridTransform = _physics.GetPhysicsTransform(grid);
@@ -42,14 +43,14 @@ internal partial class MapManager
#region MapId
public void FindGridsIntersecting<T>(MapId mapId, T shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
public void FindGridsIntersecting(MapId mapId, IPhysShape shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
if (_mapEntities.TryGetValue(mapId, out var mapEnt))
FindGridsIntersecting(mapEnt, shape, transform, ref grids, approx, includeMap);
}
public void FindGridsIntersecting<T>(MapId mapId, T shape, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
public void FindGridsIntersecting(MapId mapId, IPhysShape shape, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
if (_mapEntities.TryGetValue(mapId, out var mapEnt))
FindGridsIntersecting(mapEnt, shape, transform, callback, includeMap, approx);
@@ -99,18 +100,18 @@ internal partial class MapManager
#region MapEnt
public void FindGridsIntersecting<T>(
public void FindGridsIntersecting(
EntityUid mapEnt,
T shape,
IPhysShape shape,
Transform transform,
GridCallback callback,
bool approx = IMapManager.Approximate,
bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
bool includeMap = IMapManager.IncludeMap)
{
FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, callback, approx, includeMap);
}
private void FindGridsIntersecting<T>(EntityUid mapEnt, T shape, Box2 worldAABB, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
private void FindGridsIntersecting(EntityUid mapEnt, IPhysShape shape, Box2 worldAABB, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
// This is here so we don't double up on code.
var state = callback;
@@ -120,27 +121,20 @@ internal partial class MapManager
approx: approx, includeMap: includeMap);
}
public void FindGridsIntersecting<T, TState>(
public void FindGridsIntersecting<TState>(
EntityUid mapEnt,
T shape,
IPhysShape shape,
Transform transform,
ref TState state,
GridCallback<TState> callback,
bool approx = IMapManager.Approximate,
bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
bool includeMap = IMapManager.IncludeMap)
{
FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref state, callback, approx, includeMap);
}
private void FindGridsIntersecting<T, TState>(
EntityUid mapEnt,
T shape,
Box2 worldAABB,
Transform transform,
ref TState state,
GridCallback<TState> callback,
bool approx = IMapManager.Approximate,
bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
private void FindGridsIntersecting<TState>(EntityUid mapEnt, IPhysShape shape, Box2 worldAABB, Transform transform,
ref TState state, GridCallback<TState> callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
if (!_gridTreeQuery.TryGetComponent(mapEnt, out var gridTree))
return;
@@ -150,7 +144,7 @@ internal partial class MapManager
callback(mapEnt, mapGrid, ref state);
}
var gridState = new GridQueryState<T, TState>(
var gridState = new GridQueryState<TState>(
callback,
state,
worldAABB,
@@ -162,7 +156,8 @@ internal partial class MapManager
_transformSystem,
approx);
gridTree.Tree.Query(ref gridState, static (ref GridQueryState<T, TState> state, DynamicTree.Proxy proxy) =>
gridTree.Tree.Query(ref gridState, static (ref GridQueryState<TState> state, DynamicTree.Proxy proxy) =>
{
// Even for approximate we'll check if any chunks roughly overlap.
var data = state.Tree.GetUserData(proxy);
@@ -203,14 +198,14 @@ internal partial class MapManager
}
}
public void FindGridsIntersecting<T>(EntityUid mapEnt, T shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
public void FindGridsIntersecting(EntityUid mapEnt, IPhysShape shape, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx, includeMap);
}
public void FindGridsIntersecting<T>(EntityUid mapEnt, T shape, Box2 worldAABB, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape
public void FindGridsIntersecting(EntityUid mapEnt, IPhysShape shape, Box2 worldAABB, Transform transform,
ref List<Entity<MapGridComponent>> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var state = grids;
@@ -224,48 +219,39 @@ internal partial class MapManager
public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldAABB);
FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, callback, approx, includeMap);
_physics.ReturnPooled(polygon);
FindGridsIntersecting(mapEnt, new Polygon(worldAABB), worldAABB, Transform.Empty, callback, approx, includeMap);
}
public void FindGridsIntersecting<TState>(EntityUid mapEnt, Box2 worldAABB, ref TState state, GridCallback<TState> callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldAABB);
FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, ref state, callback, approx, includeMap);
_physics.ReturnPooled(polygon);
FindGridsIntersecting(mapEnt, new Polygon(worldAABB), worldAABB, Transform.Empty, ref state, callback, approx, includeMap);
}
public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref List<Entity<MapGridComponent>> grids,
bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldAABB);
FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, ref grids, approx, includeMap);
_physics.ReturnPooled(polygon);
FindGridsIntersecting(mapEnt, new Polygon(worldAABB), worldAABB, Transform.Empty, ref grids, approx, includeMap);
}
public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, GridCallback callback, bool approx = IMapManager.Approximate,
bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldBounds);
FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, callback, approx, includeMap);
_physics.ReturnPooled(polygon);
var shape = new Polygon(worldBounds);
FindGridsIntersecting(mapEnt, shape, worldBounds.CalcBoundingBox(), Transform.Empty, callback, approx, includeMap);
}
public void FindGridsIntersecting<TState>(EntityUid mapEnt, Box2Rotated worldBounds, ref TState state, GridCallback<TState> callback,
bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldBounds);
FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, ref state, callback, approx, includeMap);
_physics.ReturnPooled(polygon);
var shape = new Polygon(worldBounds);
FindGridsIntersecting(mapEnt, shape, worldBounds.CalcBoundingBox(), Transform.Empty, ref state, callback, approx, includeMap);
}
public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref List<Entity<MapGridComponent>> grids,
bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap)
{
var polygon = _physics.GetPooled(worldBounds);
FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, ref grids, approx, includeMap);
_physics.ReturnPooled(polygon);
var shape = new Polygon(worldBounds);
FindGridsIntersecting(mapEnt, shape, worldBounds.CalcBoundingBox(), Transform.Empty, ref grids, approx, includeMap);
}
#endregion
@@ -356,11 +342,22 @@ internal partial class MapManager
#endregion
private record struct GridQueryState<T, TState>(
private readonly record struct GridQueryState(
GridCallback Callback,
Box2 WorldAABB,
IPhysShape Shape,
Transform Transform,
B2DynamicTree<(EntityUid Uid, FixturesComponent Fixtures, MapGridComponent Grid)> Tree,
SharedMapSystem MapSystem,
MapManager MapManager,
SharedTransformSystem TransformSystem,
bool Approximate);
private record struct GridQueryState<TState>(
GridCallback<TState> Callback,
TState State,
Box2 WorldAABB,
T Shape,
IPhysShape Shape,
Transform Transform,
B2DynamicTree<(EntityUid Uid, FixturesComponent Fixtures, MapGridComponent Grid)> Tree,
SharedMapSystem MapSystem,

View File

@@ -12,12 +12,12 @@ internal sealed partial class CollisionManager
/// <param name="xfA">The transform for the first shape.</param>
/// <param name="xfB">The transform for the seconds shape.</param>
/// <returns></returns>
public bool TestOverlap<T, U>(T shapeA, int indexA, U shapeB, int indexB, in Transform xfA, in Transform xfB) where T : IPhysShape where U : IPhysShape
public bool TestOverlap(IPhysShape shapeA, int childIndexA, IPhysShape shapeB, int childIndexB, in Transform xfA, in Transform xfB)
{
var input = new DistanceInput();
input.ProxyA.Set(shapeA, indexA);
input.ProxyB.Set(shapeB, indexB);
input.ProxyA.Set(shapeA, childIndexA);
input.ProxyB.Set(shapeB, childIndexB);
input.TransformA = xfA;
input.TransformB = xfB;
input.UseRadii = true;

View File

@@ -24,7 +24,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -57,12 +56,12 @@ internal ref struct DistanceProxy
/// must remain in scope while the proxy is in use.
/// </summary>
/// <param name="shape">The shape.</param>
internal void Set<T>(T shape, int index) where T : IPhysShape
public void Set(IPhysShape shape, int index)
{
switch (shape.ShapeType)
{
case ShapeType.Circle:
var circle = Unsafe.As<PhysShapeCircle>(shape);
PhysShapeCircle circle = (PhysShapeCircle) shape;
Buffer._00 = circle.Position;
Vertices = Buffer.AsSpan[..1];
Radius = circle.Radius;
@@ -71,20 +70,20 @@ internal ref struct DistanceProxy
case ShapeType.Polygon:
if (shape is Polygon poly)
{
Vertices = poly.Vertices.AsSpan()[..poly.VertexCount];
Vertices = poly.Vertices;
Radius = poly.Radius;
}
else
{
var polyShape = Unsafe.As<PolygonShape>(shape);
Vertices = polyShape.Vertices.AsSpan()[..polyShape.VertexCount];
var polyShape = (PolygonShape) shape;
Vertices = polyShape.Vertices;
Radius = polyShape.Radius;
}
break;
case ShapeType.Chain:
var chain = Unsafe.As<ChainShape>(shape);
ChainShape chain = (ChainShape) shape;
Debug.Assert(0 <= index && index < chain.Vertices.Length);
Buffer._00 = chain.Vertices[index];
@@ -94,7 +93,7 @@ internal ref struct DistanceProxy
Radius = chain.Radius;
break;
case ShapeType.Edge:
var edge = Unsafe.As<EdgeShape>(shape);
EdgeShape edge = (EdgeShape) shape;
Buffer._00 = edge.Vertex1;
Buffer._01 = edge.Vertex2;

View File

@@ -12,9 +12,7 @@ internal interface IManifoldManager
void ReturnEdge(EdgeShape edge);
bool TestOverlap<T, U>(T shapeA, int indexA, U shapeB, int indexB, in Transform xfA, in Transform xfB)
where T : IPhysShape
where U : IPhysShape;
bool TestOverlap(IPhysShape shapeA, int indexA, IPhysShape shapeB, int indexB, in Transform xfA, in Transform xfB);
void CollideCircles(ref Manifold manifold, PhysShapeCircle circleA, in Transform xfA,
PhysShapeCircle circleB, in Transform xfB);

View File

@@ -30,7 +30,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
@@ -132,14 +131,6 @@ namespace Robust.Shared.Physics.Dynamics.Contacts
/// </summary>
public float TangentSpeed { get; set; }
[ViewVariables]
public bool Deleting => (Flags & ContactFlags.Deleting) == ContactFlags.Deleting;
/// <summary>
/// If either fixture is hard then it's a hard contact.
/// </summary>
public bool Hard => FixtureA != null && FixtureB != null && (FixtureA.Hard && FixtureB.Hard);
public void ResetRestitution()
{
Restitution = MathF.Max(FixtureA?.Restitution ?? 0.0f, FixtureB?.Restitution ?? 0.0f);
@@ -362,21 +353,9 @@ namespace Robust.Shared.Physics.Dynamics.Contacts
return HashCode.Combine(EntityA, EntityB);
}
[Pure]
public EntityUid OurEnt(EntityUid uid)
{
if (uid == EntityA)
return EntityA;
else if (uid == EntityB)
return EntityB;
throw new InvalidOperationException();
}
/// <summary>
/// Gets the other ent for this contact.
/// </summary>
[Pure]
public EntityUid OtherEnt(EntityUid uid)
{
if (uid == EntityA)
@@ -387,18 +366,6 @@ namespace Robust.Shared.Physics.Dynamics.Contacts
throw new InvalidOperationException();
}
[Pure, PublicAPI]
public (string Id, Fixture) OurFixture(EntityUid uid)
{
if (uid == EntityA)
return (FixtureAId, FixtureA!);
else if (uid == EntityB)
return (FixtureBId, FixtureB!);
throw new InvalidOperationException();
}
[Pure, PublicAPI]
public (string Id, Fixture) OtherFixture(EntityUid uid)
{
if (uid == EntityA)

View File

@@ -16,7 +16,7 @@ internal record struct Polygon : IPhysShape
[DataField]
public Vector2[] Vertices;
public byte VertexCount;
public int VertexCount => Vertices.Length;
public Vector2[] Normals;
@@ -42,19 +42,6 @@ internal record struct Polygon : IPhysShape
Array.Copy(polyShape.Vertices, Vertices, Vertices.Length);
Array.Copy(polyShape.Normals, Normals, Vertices.Length);
VertexCount = (byte) Vertices.Length;
}
/// <summary>
/// Manually constructed polygon for internal use to take advantage of pooling.
/// </summary>
internal Polygon(Vector2[] vertices, Vector2[] normals, Vector2 centroid, byte count)
{
Unsafe.SkipInit(out this);
Vertices = vertices;
Normals = normals;
Centroid = centroid;
VertexCount = count;
}
public Polygon(Box2 aabb)
@@ -74,25 +61,22 @@ internal record struct Polygon : IPhysShape
Normals[2] = new Vector2(0.0f, 1.0f);
Normals[3] = new Vector2(-1.0f, 0.0f);
VertexCount = 4;
Centroid = aabb.Center;
}
public Polygon(Box2Rotated bounds)
{
Unsafe.SkipInit(out this);
Vertices = new Vector2[4];
Normals = new Vector2[4];
Radius = 0f;
Span<Vector2> verts = stackalloc Vector2[4];
verts[0] = bounds.BottomLeft;
verts[1] = bounds.BottomRight;
verts[2] = bounds.TopRight;
verts[3] = bounds.TopLeft;
Vertices[0] = bounds.BottomLeft;
Vertices[1] = bounds.BottomRight;
Vertices[2] = bounds.TopRight;
Vertices[3] = bounds.TopLeft;
var hull = new PhysicsHull(verts, 4);
Set(hull);
CalculateNormals(Normals, 4);
VertexCount = 4;
Centroid = bounds.Center;
}
@@ -103,13 +87,11 @@ internal record struct Polygon : IPhysShape
if (hull.Count < 3)
{
VertexCount = 0;
Vertices = [];
Normals = [];
return;
}
VertexCount = (byte) vertices.Length;
Vertices = vertices;
Normals = new Vector2[vertices.Length];
Set(hull);
@@ -134,14 +116,9 @@ internal record struct Polygon : IPhysShape
}
// Compute normals. Ensure the edges have non-zero length.
CalculateNormals(Normals, vertexCount);
}
internal void CalculateNormals(Span<Vector2> normals, int count)
{
for (var i = 0; i < count; i++)
for (var i = 0; i < vertexCount; i++)
{
var next = i + 1 < count ? i + 1 : 0;
var next = i + 1 < vertexCount ? i + 1 : 0;
var edge = Vertices[next] - Vertices[i];
DebugTools.Assert(edge.LengthSquared() > float.Epsilon * float.Epsilon);

View File

@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
@@ -7,6 +8,7 @@ using Robust.Shared.Collections;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
@@ -411,34 +413,24 @@ namespace Robust.Shared.Physics.Systems
}, aabb, true);
}
[Obsolete("Use Entity<T> variant")]
public void RegenerateContacts(EntityUid uid, PhysicsComponent body, FixturesComponent? fixtures = null, TransformComponent? xform = null)
{
RegenerateContacts((uid, body, fixtures, xform));
}
public void RegenerateContacts(Entity<PhysicsComponent?, FixturesComponent?, TransformComponent?> entity)
{
if (!Resolve(entity.Owner, ref entity.Comp1))
_physicsSystem.DestroyContacts(body);
if (!Resolve(uid, ref xform, ref fixtures))
return;
_physicsSystem.DestroyContacts(entity.Comp1);
if (!Resolve(entity.Owner, ref entity.Comp2 , ref entity.Comp3))
if (xform.MapUid == null)
return;
if (entity.Comp3.MapUid == null)
if (!_xformQuery.TryGetComponent(xform.Broadphase?.Uid, out var broadphase))
return;
if (!_xformQuery.TryGetComponent(entity.Comp3.Broadphase?.Uid, out var broadphase))
return;
_physicsSystem.SetAwake(entity!, true);
_physicsSystem.SetAwake((uid, body), true);
var matrix = _transform.GetWorldMatrix(broadphase);
foreach (var fixture in entity.Comp2.Fixtures.Values)
foreach (var fixture in fixtures.Fixtures.Values)
{
TouchProxies(entity.Comp3.MapUid.Value, matrix, fixture);
TouchProxies(xform.MapUid.Value, matrix, fixture);
}
}

View File

@@ -141,26 +141,26 @@ public partial class SharedPhysicsSystem
if (args.Current is PhysicsLinearVelocityDeltaState linearState)
{
SetLinearVelocity(uid, linearState.LinearVelocity, dirty: false, body: component, manager: manager);
SetLinearVelocity(uid, linearState.LinearVelocity, body: component, manager: manager);
}
else if (args.Current is PhysicsVelocityDeltaState velocityState)
{
SetLinearVelocity(uid, velocityState.LinearVelocity, dirty: false, body: component, manager: manager);
SetAngularVelocity(uid, velocityState.AngularVelocity, dirty: false, body: component, manager: manager);
SetLinearVelocity(uid, velocityState.LinearVelocity, body: component, manager: manager);
SetAngularVelocity(uid, velocityState.AngularVelocity, body: component, manager: manager);
}
else if (args.Current is PhysicsComponentState newState)
{
SetSleepingAllowed(uid, component, newState.SleepingAllowed, dirty: false);
SetFixedRotation(uid, newState.FixedRotation, body: component, dirty: false);
SetCanCollide(uid, newState.CanCollide, body: component, dirty: false);
SetSleepingAllowed(uid, component, newState.SleepingAllowed);
SetFixedRotation(uid, newState.FixedRotation, body: component);
SetCanCollide(uid, newState.CanCollide, body: component);
component.BodyStatus = newState.Status;
SetLinearVelocity(uid, newState.LinearVelocity, dirty: false, body: component, manager: manager);
SetAngularVelocity(uid, newState.AngularVelocity, dirty: false, body: component, manager: manager);
SetLinearVelocity(uid, newState.LinearVelocity, body: component, manager: manager);
SetAngularVelocity(uid, newState.AngularVelocity, body: component, manager: manager);
SetBodyType(uid, newState.BodyType, manager, component);
SetFriction(uid, component, newState.Friction, dirty: false);
SetLinearDamping(uid, component, newState.LinearDamping, dirty: false);
SetAngularDamping(uid, component, newState.AngularDamping, dirty: false);
SetFriction(uid, component, newState.Friction);
SetLinearDamping(uid, component, newState.LinearDamping);
SetAngularDamping(uid, component, newState.AngularDamping);
component.Force = newState.Force;
component.Torque = newState.Torque;
}
@@ -276,12 +276,29 @@ public partial class SharedPhysicsSystem
/// </summary>
public void ResetDynamics(EntityUid uid, PhysicsComponent body, bool dirty = true)
{
body.Torque = 0f;
body.AngularVelocity = 0f;
body.Force = Vector2.Zero;
body.LinearVelocity = Vector2.Zero;
if (dirty)
DirtyFields(uid, body, null, nameof(PhysicsComponent.Torque), nameof(PhysicsComponent.AngularVelocity), nameof(PhysicsComponent.Force), nameof(PhysicsComponent.LinearVelocity));
if (body.Torque != 0f)
{
body.Torque = 0f;
DirtyField(uid, body, nameof(PhysicsComponent.Torque));
}
if (body.AngularVelocity != 0f)
{
body.AngularVelocity = 0f;
DirtyField(uid, body, nameof(PhysicsComponent.AngularVelocity));
}
if (body.Force != Vector2.Zero)
{
body.Force = Vector2.Zero;
DirtyField(uid, body, nameof(PhysicsComponent.Force));
}
if (body.LinearVelocity != Vector2.Zero)
{
body.LinearVelocity = Vector2.Zero;
DirtyField(uid, body, nameof(PhysicsComponent.LinearVelocity));
}
}
public void ResetMassData(EntityUid uid, FixturesComponent? manager = null, PhysicsComponent? body = null)
@@ -386,8 +403,7 @@ public partial class SharedPhysicsSystem
return false;
body.AngularVelocity = value;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.AngularVelocity));
DirtyField(uid, body, nameof(PhysicsComponent.AngularVelocity));
return true;
}
@@ -413,9 +429,7 @@ public partial class SharedPhysicsSystem
return false;
body.LinearVelocity = velocity;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.LinearVelocity));
DirtyField(uid, body, nameof(PhysicsComponent.LinearVelocity));
return true;
}
@@ -425,8 +439,7 @@ public partial class SharedPhysicsSystem
return;
body.AngularDamping = value;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.AngularDamping));
DirtyField(uid, body, nameof(PhysicsComponent.AngularDamping));
}
public void SetLinearDamping(EntityUid uid, PhysicsComponent body, float value, bool dirty = true)
@@ -435,8 +448,7 @@ public partial class SharedPhysicsSystem
return;
body.LinearDamping = value;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.LinearDamping));
DirtyField(uid, body, nameof(PhysicsComponent.LinearDamping));
}
[Obsolete("Use SetAwake with EntityUid<PhysicsComponent>")]
@@ -512,27 +524,32 @@ public partial class SharedPhysicsSystem
body.BodyType = value;
ResetMassData(uid, manager, body);
body.Force = Vector2.Zero;
body.Torque = 0f;
if (body.BodyType == BodyType.Static)
{
SetAwake((uid, body), false);
body.LinearVelocity = Vector2.Zero;
body.AngularVelocity = 0f;
if (body.LinearVelocity != Vector2.Zero)
{
body.LinearVelocity = Vector2.Zero;
DirtyField(uid, body, nameof(PhysicsComponent.LinearVelocity));
}
DirtyFields(uid, body, null,
nameof(PhysicsComponent.LinearVelocity),
nameof(PhysicsComponent.AngularVelocity),
nameof(PhysicsComponent.Force),
nameof(PhysicsComponent.Torque));
if (body.AngularVelocity != 0f)
{
body.AngularVelocity = 0f;
DirtyField(uid, body, nameof(PhysicsComponent.AngularVelocity));
}
}
// Even if it's dynamic if it can't collide then don't force it awake.
else if (body.CanCollide)
{
SetAwake((uid, body), true);
DirtyFields(uid, body, null, nameof(PhysicsComponent.Force), nameof(PhysicsComponent.Torque));
}
if (body.Torque != 0f)
{
body.Torque = 0f;
DirtyField(uid, body, nameof(PhysicsComponent.Torque));
}
_broadphase.RegenerateContacts(uid, body, manager, xform);
@@ -550,8 +567,7 @@ public partial class SharedPhysicsSystem
return;
body.BodyStatus = status;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.BodyStatus));
DirtyField(uid, body, nameof(PhysicsComponent.BodyStatus));
}
/// <summary>
@@ -602,10 +618,7 @@ public partial class SharedPhysicsSystem
var ev = new CollisionChangeEvent(uid, body, value);
RaiseLocalEvent(ref ev);
}
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.CanCollide));
DirtyField(uid, body, nameof(PhysicsComponent.CanCollide));
return value;
}
@@ -615,10 +628,13 @@ public partial class SharedPhysicsSystem
return;
body.FixedRotation = value;
body.AngularVelocity = 0.0f;
DirtyField(uid, body, nameof(PhysicsComponent.FixedRotation));
if (dirty)
DirtyFields(uid, body, null, nameof(PhysicsComponent.FixedRotation), nameof(PhysicsComponent.AngularVelocity));
if (body.AngularVelocity != 0f)
{
body.AngularVelocity = 0.0f;
DirtyField(uid, body, nameof(PhysicsComponent.AngularVelocity));
}
ResetMassData(uid, manager: manager, body: body);
}
@@ -629,8 +645,7 @@ public partial class SharedPhysicsSystem
return;
body._friction = value;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.Friction));
DirtyField(uid, body, nameof(PhysicsComponent.Friction));
}
public void SetInertia(EntityUid uid, PhysicsComponent body, float value, bool dirty = true)
@@ -669,8 +684,7 @@ public partial class SharedPhysicsSystem
SetAwake((uid, body), true);
body.SleepingAllowed = value;
if (dirty)
DirtyField(uid, body, nameof(PhysicsComponent.SleepingAllowed));
DirtyField(uid, body, nameof(PhysicsComponent.SleepingAllowed));
}
public void SetSleepTime(PhysicsComponent body, float value)

View File

@@ -809,10 +809,10 @@ public abstract partial class SharedPhysicsSystem
/// Returns all of this entity's contacts.
/// </summary>
[Pure]
public ContactEnumerator GetContacts(Entity<FixturesComponent?> entity, bool includeDeleting = false)
public ContactEnumerator GetContacts(Entity<FixturesComponent?> entity)
{
_fixturesQuery.Resolve(entity.Owner, ref entity.Comp);
return new ContactEnumerator(entity.Comp, includeDeleting);
return new ContactEnumerator(entity.Comp);
}
}
@@ -823,16 +823,8 @@ public record struct ContactEnumerator
private Dictionary<string, Fixture>.ValueCollection.Enumerator _fixtureEnumerator;
private Dictionary<Fixture, Contact>.ValueCollection.Enumerator _contactEnumerator;
/// <summary>
/// Also include deleting contacts.
/// This typically includes the current contact if you're invoking this in the eventbus for an EndCollideEvent.
/// </summary>
public bool IncludeDeleting;
public ContactEnumerator(FixturesComponent? fixtures, bool includeDeleting = false)
public ContactEnumerator(FixturesComponent? fixtures)
{
IncludeDeleting = includeDeleting;
if (fixtures == null || fixtures.Fixtures.Count == 0)
{
this = Empty;
@@ -859,10 +851,6 @@ public record struct ContactEnumerator
}
contact = _contactEnumerator.Current;
if (!IncludeDeleting && contact.Deleting)
return MoveNext(out contact);
return true;
}
}

View File

@@ -1,55 +0,0 @@
using System.Buffers;
using System.Numerics;
using Microsoft.Extensions.ObjectPool;
using Robust.Shared.Maths;
using Robust.Shared.Physics.Shapes;
namespace Robust.Shared.Physics.Systems;
public abstract partial class SharedPhysicsSystem
{
/// <summary>
/// Gets a polygon with pooled arrays backing it.
/// </summary>
internal Polygon GetPooled(Box2 box)
{
var vertices = ArrayPool<Vector2>.Shared.Rent(4);
var normals = ArrayPool<Vector2>.Shared.Rent(4);
var centroid = box.Center;
vertices[0] = box.BottomLeft;
vertices[1] = box.BottomRight;
vertices[2] = box.TopRight;
vertices[3] = box.TopLeft;
normals[0] = new Vector2(0.0f, -1.0f);
normals[1] = new Vector2(1.0f, 0.0f);
normals[2] = new Vector2(0.0f, 1.0f);
normals[3] = new Vector2(-1.0f, 0.0f);
return new Polygon(vertices, normals, centroid, 4);
}
internal Polygon GetPooled(Box2Rotated box)
{
var vertices = ArrayPool<Vector2>.Shared.Rent(4);
var normals = ArrayPool<Vector2>.Shared.Rent(4);
var centroid = box.Center;
vertices[0] = box.BottomLeft;
vertices[1] = box.BottomRight;
vertices[2] = box.TopRight;
vertices[3] = box.TopLeft;
var polygon = new Polygon(vertices, normals, centroid, 4);
polygon.CalculateNormals(normals, 4);
return polygon;
}
internal void ReturnPooled(Polygon polygon)
{
ArrayPool<Vector2>.Shared.Return(polygon.Vertices);
ArrayPool<Vector2>.Shared.Return(polygon.Normals);
}
}

View File

@@ -202,27 +202,27 @@ namespace Robust.Shared.Physics.Systems
return bodies;
}
public void GetContactingEntities(Entity<PhysicsComponent?> ent, HashSet<EntityUid> contacting, bool approximate = false)
public HashSet<EntityUid> GetContactingEntities(EntityUid uid, PhysicsComponent? body = null, bool approximate = false)
{
if (!Resolve(ent.Owner, ref ent.Comp))
return;
// HashSet to ensure that we only return each entity once, instead of once per colliding fixture.
var result = new HashSet<EntityUid>();
var node = ent.Comp.Contacts.First;
if (!Resolve(uid, ref body))
return result;
var node = body.Contacts.First;
while (node != null)
{
var contact = node.Value;
node = node.Next;
if (approximate || contact.IsTouching)
contacting.Add(ent.Owner == contact.EntityA ? contact.EntityB : contact.EntityA);
}
}
if (!approximate && !contact.IsTouching)
continue;
result.Add(uid == contact.EntityA ? contact.EntityB : contact.EntityA);
}
public HashSet<EntityUid> GetContactingEntities(EntityUid uid, PhysicsComponent? body = null, bool approximate = false)
{
var result = new HashSet<EntityUid>();
GetContactingEntities((uid, body), result, approximate);
return result;
}

View File

@@ -24,6 +24,7 @@ namespace Robust.Shared.Physics.Systems
/*
* TODO:
* Raycasts for non-box shapes.
* TOI Solver (continuous collision detection)
* Poly cutting
*/
@@ -82,13 +83,6 @@ namespace Robust.Shared.Physics.Systems
_physicsReg = EntityManager.ComponentFactory.GetRegistration(CompIdx.Index<PhysicsComponent>());
// TODO PHYSICS STATE
// Consider condensing the possible fields into just Linear velocity, angular velocity, and "Other"
// Or maybe even just "velocity" & "other"
// Then get-state doesn't have to iterate over a 10-element array.
// And it simplifies the DirtyField calls.
// Though I guess combining fixtures & physics will complicate it a bit more again.
// If you update this then update the delta state + GetState + HandleState!
EntityManager.ComponentFactory.RegisterNetworkedFields(_physicsReg,
nameof(PhysicsComponent.CanCollide),
@@ -326,28 +320,6 @@ namespace Robust.Shared.Physics.Systems
RaiseLocalEvent(ref updateMapBeforeSolve);
}
// TODO PHYSICS Fix Collision Mispredicts
// If a physics update induces a position update that brings fixtures into contact, the collision starts in the NEXT tick,
// as positions are updated after CollideContacts() gets called.
//
// If a player input induces a position update that brings fixtures into contact, the collision happens on the SAME tick,
// as inputs are handled before system updates.
//
// When applying a server's game state with new positions, the client won't know what caused the positions to update,
// and thus can't know whether the collision already occurred (i.e., whether its effects are already contained within the
// game state currently being applied), or whether it should start on the next tick and needs to predict the start of
// the collision.
//
// Currently the client assumes that any position updates happened due to physics steps. I.e., positions are reset, then
// contacts are reset via ResetContacts(), then positions are updated using the new game state. Alternatively, we could
// call ResetContacts() AFTER applying the server state, which would correspond to assuming that the collisions have
// already "started" in the state, and we don't want to re-raise the events.
//
// Currently there is no way to avoid mispredicts from happening. E.g., a simple collision-counter component will always
// either mispredict on physics induced position changes, or on player/input induced updates. The easiest way I can think
// of to fix this would be to always call `CollideContacts` again at the very end of a physics update.
// But that might be unnecessarily expensive for what are hopefully only infrequent mispredicts.
CollideContacts();
var enumerator = AllEntityQuery<PhysicsMapComponent>();

View File

@@ -284,21 +284,15 @@ public interface IPrototypeManager
void LoadDefaultPrototypes(Dictionary<Type, HashSet<string>>? loaded = null);
/// <summary>
/// Call this when operations adding new prototypes are done.
/// This will handle prototype inheritance, instance creation, and update entity categories.
/// When loading extra prototypes, or reloading a subset of existing prototypes, you should probably use
/// <see cref="ReloadPrototypes"/> instead.
/// Syncs all inter-prototype data. Call this when operations adding new prototypes are done.
/// </summary>
void ResolveResults();
/// <summary>
/// This should be called after new or updated prototypes ahve been loaded.
/// This will handle prototype inheritance, instance creation, and update entity categories.
/// It will also invoke <see cref="PrototypesReloaded"/> and raise a <see cref="PrototypesReloadedEventArgs"/>
/// event with information about the modified prototypes.
/// Invokes <see cref="PrototypesReloaded"/> with information about the modified prototypes.
/// When built with development tools, this will also push inheritance for reloaded prototypes/
/// </summary>
void ReloadPrototypes(
Dictionary<Type, HashSet<string>> modified,
void ReloadPrototypes(Dictionary<Type, HashSet<string>> modified,
Dictionary<Type, HashSet<string>>? removed = null);
/// <summary>

View File

@@ -209,10 +209,10 @@ public partial class PrototypeManager
var kindData = _kinds[kind];
if (!overwrite && kindData.RawResults.ContainsKey(id))
if (!overwrite && kindData.Results.ContainsKey(id))
throw new PrototypeLoadException($"Duplicate ID: '{id}' for kind '{kind}");
kindData.RawResults[id] = data;
kindData.Results[id] = data;
if (kindData.Inheritance is { } inheritance)
{
@@ -295,7 +295,6 @@ public partial class PrototypeManager
kindData.UnfrozenInstances ??= kindData.Instances.ToDictionary();
kindData.UnfrozenInstances.Remove(id);
kindData.Results.Remove(id);
kindData.RawResults.Remove(id);
modified.Add(kindData);
}
}

View File

@@ -335,117 +335,98 @@ namespace Robust.Shared.Prototypes
}
/// <inheritdoc />
public void ReloadPrototypes(
Dictionary<Type, HashSet<string>> modified,
public void ReloadPrototypes(Dictionary<Type, HashSet<string>> modified,
Dictionary<Type, HashSet<string>>? removed = null)
{
#if TOOLS
var prototypeTypeOrder = modified.Keys.ToList();
prototypeTypeOrder.Sort(SortPrototypesByPriority);
var byType = new Dictionary<Type, PrototypesReloadedEventArgs.PrototypeChangeSet>();
var pushed = new Dictionary<Type, HashSet<string>>();
var modifiedKinds = new HashSet<KindData>();
var toProcess = new HashSet<string>();
var processQueue = new Queue<string>();
foreach (var kind in prototypeTypeOrder)
{
var modifiedInstances = new Dictionary<string, IPrototype>();
var kindData = _kinds[kind];
var tree = kindData.Inheritance;
toProcess.Clear();
processQueue.Clear();
DebugTools.AssertEqual(kind.IsAssignableTo(typeof(IInheritingPrototype)), tree != null);
DebugTools.Assert(tree != null || kindData.RawResults == kindData.Results);
foreach (var id in modified[kind])
if (!kind.IsAssignableTo(typeof(IInheritingPrototype)))
{
AddToQueue(id);
foreach (var id in modified[kind])
{
var prototype = (IPrototype)_serializationManager.Read(kind, kindData.Results[id])!;
kindData.UnfrozenInstances ??= kindData.Instances.ToDictionary();
kindData.UnfrozenInstances[id] = prototype;
modifiedKinds.Add(kindData);
}
continue;
}
void AddToQueue(string id)
var tree = kindData.Inheritance!;
var processQueue = new Queue<string>();
foreach (var id in modified[kind])
{
if (!toProcess.Add(id))
return;
processQueue.Enqueue(id);
if (tree == null)
return;
if (!tree.TryGetChildren(id, out var children))
return;
foreach (var child in children!)
{
AddToQueue(child);
}
}
while (processQueue.TryDequeue(out var id))
{
DebugTools.Assert(toProcess.Contains(id));
if (tree != null)
{
if (tree.TryGetParents(id, out var parents))
{
DebugTools.Assert(parents.Length > 0);
var nonPushedParent = false;
foreach (var parent in parents)
{
if (!toProcess.Contains(parent))
continue;
var pushedSet = pushed.GetOrNew(kind);
// our parent has been modified, but has not yet been processed.
// we re-queue ourselves at the end of the queue.
DebugTools.Assert(processQueue.Contains(parent));
if (tree.TryGetParents(id, out var parents))
{
var nonPushedParent = false;
foreach (var parent in parents)
{
//our parent has been reloaded and has not been added to the pushedSet yet
if (modified[kind].Contains(parent) && !pushedSet.Contains(parent))
{
//we re-queue ourselves at the end of the queue
processQueue.Enqueue(id);
nonPushedParent = true;
break;
}
if (nonPushedParent)
continue;
var parentMaps = new MappingDataNode[parents.Length];
for (var i = 0; i < parentMaps.Length; i++)
{
parentMaps[i] = kindData.Results[parents[i]];
}
kindData.Results[id] = _serializationManager.PushCompositionWithGenericNode(
kind,
parentMaps,
kindData.RawResults[id]);
}
else
if (nonPushedParent)
continue;
var parentMaps = new MappingDataNode[parents.Length];
for (var i = 0; i < parentMaps.Length; i++)
{
kindData.Results[id] = kindData.RawResults[id];
parentMaps[i] = kindData.Results[parents[i]];
}
kindData.Results[id] = _serializationManager.PushCompositionWithGenericNode(
kind,
parentMaps,
kindData.Results[id]);
}
toProcess.Remove(id);
var prototype = TryReadPrototype(kind, id, kindData.Results[id], SerializationHookContext.DontSkipHooks);
if (prototype == null)
continue;
if (prototype != null)
{
kindData.UnfrozenInstances ??= kindData.Instances.ToDictionary();
kindData.UnfrozenInstances[id] = prototype;
modifiedKinds.Add(kindData);
}
kindData.UnfrozenInstances ??= kindData.Instances.ToDictionary();
kindData.UnfrozenInstances[id] = prototype;
modifiedInstances.Add(id, prototype);
pushedSet.Add(id);
}
if (modifiedInstances.Count == 0)
continue;
byType.Add(kindData.Type, new(modifiedInstances));
modifiedKinds.Add(kindData);
}
Freeze(modifiedKinds);
if (modifiedKinds.Any(x => x.Type == typeof(EntityPrototype) || x.Type == typeof(EntityCategoryPrototype)))
UpdateCategories();
#endif
//todo paul i hate it but i am not opening that can of worms in this refactor
var byType = modified
.ToDictionary(
g => g.Key,
g => new PrototypesReloadedEventArgs.PrototypeChangeSet(
g.Value.Where(x => _kinds[g.Key].Instances.ContainsKey(x))
.ToDictionary(a => a, a => _kinds[g.Key].Instances[a])));
var modifiedTypes = new HashSet<Type>(byType.Keys);
if (removed != null)
@@ -611,9 +592,11 @@ namespace Robust.Shared.Prototypes
// var sw = RStopwatch.StartNew();
var results = data.RawResults.ToDictionary(
k => k.Key,
k => new InheritancePushDatum(k.Value, tree.GetParentsCount(k.Key)));
var results = new Dictionary<string, InheritancePushDatum>(
data.Results.Select(k => new KeyValuePair<string, InheritancePushDatum>(
k.Key,
new InheritancePushDatum(k.Value, tree.GetParentsCount(k.Key))))
);
using var countDown = new CountdownEvent(results.Count);
@@ -1032,8 +1015,6 @@ namespace Robust.Shared.Prototypes
if (kind.IsAssignableTo(typeof(IInheritingPrototype)))
kindData.Inheritance = new MultiRootInheritanceGraph<string>();
else
kindData.Results = kindData.RawResults;
}
/// <inheritdoc />
@@ -1045,13 +1026,7 @@ namespace Robust.Shared.Prototypes
public FrozenDictionary<string, IPrototype> Instances = FrozenDictionary<string, IPrototype>.Empty;
public Dictionary<string, MappingDataNode> Results = new();
/// <summary>
/// Variant of <see cref="Results"/> prior to inheritance pushing. If the kind does not have inheritance,
/// then this is just the same dictionary.
/// </summary>
public readonly Dictionary<string, MappingDataNode> RawResults = new();
public readonly Dictionary<string, MappingDataNode> Results = new();
public readonly Type Type = kind;
public readonly string Name = name;

View File

@@ -26,27 +26,14 @@ public partial class SerializationManager
public DataNode PushComposition(Type type, DataNode[] parents, DataNode child, ISerializationContext? context = null)
{
if (parents.Length == 0)
return child.Copy();
DebugTools.Assert(parents.All(x => x.GetType() == child.GetType()));
// TODO SERIALIZATION
// Change inheritance pushing so that it modifies the passed in child.
// I.e., move the child.Clone() statement to the beginning here, then make the delegate modify the clone.
// Currently pusing more than one parent requires multiple unnecessary clones.
var pusher = GetOrCreatePushCompositionDelegate(type, child);
var node = child;
foreach (var parent in parents)
for (int i = 0; i < parents.Length; i++)
{
var newNode = pusher(type, parent, node, context);
// Currently delegate pusher should be returning a new instance, and not modifying the passed in child.
DebugTools.Assert(!ReferenceEquals(newNode, node));
node = newNode;
node = pusher(type, parents[i], node, context);
}
return node;
@@ -108,7 +95,7 @@ public partial class SerializationManager
Expression.Convert(parentParam, nodeType)),
MappingDataNode => Expression.Call(
instanceConst,
nameof(CombineMappings),
nameof(PushInheritanceMapping),
Type.EmptyTypes,
Expression.Convert(childParam, nodeType),
Expression.Convert(parentParam, nodeType)),
@@ -130,26 +117,32 @@ public partial class SerializationManager
//todo implement different inheritancebehaviours for yamlfield
// I have NFI what this comment means.
var result = child.Copy();
var result = new SequenceDataNode(child.Count + parent.Count);
foreach (var entry in parent)
{
result.Add(entry.Copy());
result.Add(entry);
}
foreach (var entry in child)
{
result.Add(entry);
}
return result;
}
public MappingDataNode CombineMappings(MappingDataNode child, MappingDataNode parent)
private MappingDataNode PushInheritanceMapping(MappingDataNode child, MappingDataNode parent)
{
//todo implement different inheritancebehaviours for yamlfield
// I have NFI what this comment means.
// I still don't know what it means, but if it's talking about the always/never push inheritance attributes,
// make sure it doesn't break entity serialization.
var result = child.Copy();
var result = new MappingDataNode(child.Count + parent.Count);
foreach (var (k, v) in parent)
{
result.TryAddCopy(k, v);
result[k] = v;
}
foreach (var (k, v) in child)
{
result[k] = v;
}
return result;

View File

@@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Robust.Shared.Serialization.Markdown.Value;
using Robust.Shared.Utility;
@@ -397,20 +396,5 @@ namespace Robust.Shared.Serialization.Markdown.Mapping
public int Count => _children.Count;
public bool IsReadOnly => false;
public bool TryAdd(DataNode key, DataNode value)
{
return _children.TryAdd(key, value);
}
public bool TryAddCopy(DataNode key, DataNode value)
{
ref var entry = ref CollectionsMarshal.GetValueRefOrAddDefault(_children, key, out var exists);
if (exists)
return false;
entry = value.Copy();
return true;
}
}
}

View File

@@ -42,6 +42,7 @@ public abstract class SharedPrototypeLoadManager : IGamePrototypeLoadManager
var changed = new Dictionary<Type, HashSet<string>>();
_prototypeManager.LoadString(data, true, changed);
_prototypeManager.ResolveResults();
_prototypeManager.ReloadPrototypes(changed);
_localizationManager.ReloadLocalizations();

View File

@@ -1,446 +0,0 @@
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using NUnit.Framework;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Player;
namespace Robust.UnitTesting.Server.GameStates;
public sealed class DetachedParentTest : RobustIntegrationTest
{
/// <summary>
/// Check that the client can handle an entity getting attached to an entity that is outside of their PVS range, or
/// that they have never seen. Previously this could result in entities with improperly assigned GridUids due to
/// an existing/initialized entity being attached to an un-initialized entity on an already initialized grid.
/// </summary>
[Test]
public async Task TestDetachedParent()
{
var server = StartServer();
var client = StartClient();
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
var mapSys = server.System<SharedMapSystem>();
var xformSys = server.System<SharedTransformSystem>();
var mapMan = server.ResolveDependency<IMapManager>();
var sEntMan = server.ResolveDependency<IEntityManager>();
var confMan = server.ResolveDependency<IConfigurationManager>();
var sPlayerMan = server.ResolveDependency<ISharedPlayerManager>();
var netMan = client.ResolveDependency<IClientNetManager>();
Assert.DoesNotThrow(() => client.SetConnectTarget(server));
client.Post(() => netMan.ClientConnect(null!, 0, null!));
server.Post(() => confMan.SetCVar(CVars.NetPVS, true));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Ensure client & server ticks are synced.
// Client runs 1 tick ahead
{
var sTick = (int)server.Timing.CurTick.Value;
var cTick = (int)client.Timing.CurTick.Value;
var delta = cTick - sTick;
if (delta > 1)
await server.WaitRunTicks(delta - 1);
else if (delta < 1)
await client.WaitRunTicks(1 - delta);
sTick = (int)server.Timing.CurTick.Value;
cTick = (int)client.Timing.CurTick.Value;
delta = cTick - sTick;
Assert.That(delta, Is.EqualTo(1));
}
// Set up map and spawn player
MapId mapId = default;
EntityUid map = default;
EntityUid grid = default;
EntityUid parent = default;
EntityUid player = default;
EntityUid child = default;
EntityCoordinates gridCoords = default;
EntityCoordinates mapCoords = default;
await server.WaitPost(() =>
{
// Cycle through some EntityUids to avoid server-side and client-side uids accidentally matching up.
// I made a mistake earlier in this test where I used a server-side uid on the client
for (var i = 0; i < 10; i++)
{
server.EntMan.DeleteEntity(server.EntMan.SpawnEntity(null, MapCoordinates.Nullspace));
}
map = mapSys.CreateMap(out mapId);
var gridEnt = mapMan.CreateGridEntity(mapId);
mapSys.SetTile(gridEnt.Owner, gridEnt.Comp, Vector2i.Zero, new Tile(1));
gridCoords = new EntityCoordinates(gridEnt, .5f, .5f);
mapCoords = new EntityCoordinates(map, 200, 200);
grid = gridEnt.Owner;
parent = sEntMan.SpawnEntity(null, gridCoords);
player = sEntMan.SpawnEntity(null, gridCoords);
child = sEntMan.SpawnEntity(null, mapCoords);
// Attach player.
var session = sPlayerMan.Sessions.First();
server.PlayerMan.SetAttachedEntity(session, player);
sPlayerMan.JoinGame(session);
});
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check that transforms are as expected.
var childX = server.Transform(child);
var parentX = server.Transform(parent);
var playerX = server.Transform(player);
var gridX = server.Transform(grid);
Assert.That(childX.MapID, Is.EqualTo(mapId));
Assert.That(parentX.MapID, Is.EqualTo(mapId));
Assert.That(playerX.MapID, Is.EqualTo(mapId));
Assert.That(gridX.MapID, Is.EqualTo(mapId));
Assert.That(childX.ParentUid, Is.EqualTo(map));
Assert.That(parentX.ParentUid, Is.EqualTo(grid));
Assert.That(playerX.ParentUid, Is.EqualTo(grid));
Assert.That(gridX.ParentUid, Is.EqualTo(map));
Assert.That(childX.GridUid, Is.Null);
Assert.That(parentX.GridUid, Is.EqualTo(grid));
Assert.That(playerX.GridUid, Is.EqualTo(grid));
Assert.That(gridX.GridUid, Is.EqualTo(grid));
// Check that the player received the entities, and that their transforms are as expected.
// Note that the child entity should be outside of PVS range.
var cMap = client.EntMan.GetEntity(server.EntMan.GetNetEntity(map));
var cGrid = client.EntMan.GetEntity(server.EntMan.GetNetEntity(grid));
var cPlayer = client.EntMan.GetEntity(server.EntMan.GetNetEntity(player));
var cParent = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent));
var cChild = client.EntMan.GetEntity(server.EntMan.GetNetEntity(child));
Assert.That(cMap, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cGrid, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cPlayer, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cParent, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cChild, Is.EqualTo(EntityUid.Invalid));
var cParentX = client.Transform(cParent);
var cPlayerX = client.Transform(cPlayer);
var cGridX = client.Transform(cGrid);
Assert.That(cParentX.MapID, Is.EqualTo(mapId));
Assert.That(cPlayerX.MapID, Is.EqualTo(mapId));
Assert.That(cGridX.MapID, Is.EqualTo(mapId));
Assert.That(cParentX.ParentUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cGrid));
Assert.That(cGridX.ParentUid, Is.EqualTo(cMap));
Assert.That(cParentX.GridUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.GridUid, Is.EqualTo(cGrid));
Assert.That(cGridX.GridUid, Is.EqualTo(cGrid));
// Move the player into pvs range of the child, which will move them outside of the grid & parent's PVS range.
await server.WaitPost(() => xformSys.SetCoordinates(player, mapCoords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// the client now knows about the child.
cChild = client.EntMan.GetEntity(server.EntMan.GetNetEntity(child));
Assert.That(cChild, Is.Not.EqualTo(EntityUid.Invalid));
var cChildX = client.Transform(cChild);
Assert.That(childX.MapID, Is.EqualTo(mapId));
Assert.That(cChildX.ParentUid, Is.EqualTo(cMap));
Assert.That(cChildX.GridUid, Is.Null);
// Player transform has updated
Assert.That(cPlayerX.GridUid, Is.Null);
Assert.That(cPlayerX.MapID, Is.EqualTo(mapId));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cMap));
// But the other entities have left PVS range
Assert.That(cParentX.ParentUid, Is.EqualTo(EntityUid.Invalid));
Assert.That(cParentX.MapID, Is.EqualTo(MapId.Nullspace));
Assert.That(cParentX.GridUid, Is.Null);
Assert.That((client.MetaData(cParent).Flags & MetaDataFlags.Detached) != 0);
// Attach the child & player entities to the parent
// This is the main step that the test is actually checking
var parentCoords = new EntityCoordinates(parent, Vector2.Zero);
await server.WaitPost(() => xformSys.SetCoordinates(player, parentCoords));
await server.WaitPost(() => xformSys.SetCoordinates(child, parentCoords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check that server-side transforms are as expected
Assert.That(childX.ParentUid, Is.EqualTo(parent));
Assert.That(parentX.ParentUid, Is.EqualTo(grid));
Assert.That(playerX.ParentUid, Is.EqualTo(parent));
Assert.That(gridX.ParentUid, Is.EqualTo(map));
Assert.That(childX.GridUid, Is.EqualTo(grid));
Assert.That(parentX.GridUid, Is.EqualTo(grid));
Assert.That(playerX.GridUid, Is.EqualTo(grid));
Assert.That(gridX.GridUid, Is.EqualTo(grid));
// Next check the client-side transforms
Assert.That((client.MetaData(cParent).Flags & MetaDataFlags.Detached) == 0);
Assert.That(cChildX.ParentUid, Is.EqualTo(cParent));
Assert.That(cParentX.ParentUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cParent));
Assert.That(cGridX.ParentUid, Is.EqualTo(cMap));
Assert.That(cChildX.GridUid, Is.EqualTo(cGrid));
Assert.That(cParentX.GridUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.GridUid, Is.EqualTo(cGrid));
Assert.That(cGridX.GridUid, Is.EqualTo(cGrid));
// Repeat the previous test, but this time attaching to an entity that gets spawned outside of PVS range, that
// the client never new about previously.
await server.WaitPost(() => xformSys.SetCoordinates(player, mapCoords));
await server.WaitPost(() => xformSys.SetCoordinates(child, mapCoords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Child transform has updated.
Assert.That(childX.MapID, Is.EqualTo(mapId));
Assert.That(cChildX.ParentUid, Is.EqualTo(cMap));
Assert.That(cChildX.GridUid, Is.Null);
// Player transform has updated
Assert.That(cPlayerX.GridUid, Is.Null);
Assert.That(cPlayerX.MapID, Is.EqualTo(mapId));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cMap));
// The other entities have left PVS range
Assert.That(cParentX.ParentUid, Is.EqualTo(EntityUid.Invalid));
Assert.That(cParentX.MapID, Is.EqualTo(MapId.Nullspace));
Assert.That(cParentX.GridUid, Is.Null);
Assert.That((client.MetaData(cParent).Flags & MetaDataFlags.Detached) != 0);
// Create a new parent entity
EntityUid parent2 = default;
await server.WaitPost(() => parent2 = sEntMan.SpawnEntity(null, gridCoords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
var parent2X = server.Transform(parent2);
Assert.That(parent2X.MapID, Is.EqualTo(mapId));
Assert.That(parent2X.ParentUid, Is.EqualTo(grid));
Assert.That(parent2X.GridUid, Is.EqualTo(grid));
// Client does not know that parent2 exists yet.
var cParent2 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent2));
Assert.That(cParent2, Is.EqualTo(EntityUid.Invalid));
// Attach player & child to the new parent.
var parent2Coords = new EntityCoordinates(parent2, Vector2.Zero);
await server.WaitPost(() => xformSys.SetCoordinates(player, parent2Coords));
await server.WaitPost(() => xformSys.SetCoordinates(child, parent2Coords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check all the transforms
cParent2 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent2));
Assert.That(cParent2, Is.Not.EqualTo(EntityUid.Invalid));
var cParent2X = client.Transform(cParent2);
Assert.That(cChildX.ParentUid, Is.EqualTo(cParent2));
Assert.That(cParent2X.ParentUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cParent2));
Assert.That(cGridX.ParentUid, Is.EqualTo(cMap));
Assert.That(cParent2X.GridUid, Is.EqualTo(cGrid));
Assert.That(cChildX.GridUid, Is.EqualTo(cGrid));
Assert.That(cPlayerX.GridUid, Is.EqualTo(cGrid));
Assert.That(cGridX.GridUid, Is.EqualTo(cGrid));
// Repeat again, but with a new map.
// Set up map and spawn player
MapId mapId2 = default;
EntityUid map2 = default;
EntityUid grid2 = default;
EntityUid parent3 = default;
await server.WaitPost(() =>
{
map2 = mapSys.CreateMap(out mapId2);
var gridEnt = mapMan.CreateGridEntity(mapId2);
mapSys.SetTile(gridEnt.Owner, gridEnt.Comp, Vector2i.Zero, new Tile(1));
var grid2Coords = new EntityCoordinates(gridEnt, .5f, .5f);
grid2 = gridEnt.Owner;
parent3 = sEntMan.SpawnEntity(null, grid2Coords);
});
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check server-side transforms
var grid2X = server.Transform(grid2);
var parent3X = server.Transform(parent3);
Assert.That(parent3X.MapID, Is.EqualTo(mapId2));
Assert.That(grid2X.MapID, Is.EqualTo(mapId2));
Assert.That(parent3X.ParentUid, Is.EqualTo(grid2));
Assert.That(grid2X.ParentUid, Is.EqualTo(map2));
Assert.That(parent3X.GridUid, Is.EqualTo(grid2));
Assert.That(grid2X.GridUid, Is.EqualTo(grid2));
// Client does not know that parent3 exists, but (at least for now) clients always know about all maps and grids.
var cParent3 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent3));
var cGrid2 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(grid2));
var cMap2 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(map2));
Assert.That(cMap2, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cGrid2, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cParent3, Is.EqualTo(EntityUid.Invalid));
// Attach the entities to the parent on the new map.
var parent3Coords = new EntityCoordinates(parent3, Vector2.Zero);
await server.WaitPost(() => xformSys.SetCoordinates(player, parent3Coords));
await server.WaitPost(() => xformSys.SetCoordinates(child, parent3Coords));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check all the transforms
cParent3 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent3));
Assert.That(cParent3, Is.Not.EqualTo(EntityUid.Invalid));
var cParent3X = client.Transform(cParent3);
var cGrid2X = client.Transform(cGrid2);
Assert.That(cChildX.ParentUid, Is.EqualTo(cParent3));
Assert.That(cParent3X.ParentUid, Is.EqualTo(cGrid2));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cParent3));
Assert.That(cGrid2X.ParentUid, Is.EqualTo(cMap2));
Assert.That(cParent3X.GridUid, Is.EqualTo(cGrid2));
Assert.That(cChildX.GridUid, Is.EqualTo(cGrid2));
Assert.That(cPlayerX.GridUid, Is.EqualTo(cGrid2));
Assert.That(cGrid2X.GridUid, Is.EqualTo(cGrid2));
Assert.That(cParent3X.MapID, Is.EqualTo(mapId2));
Assert.That(cChildX.MapID, Is.EqualTo(mapId2));
Assert.That(cPlayerX.MapID, Is.EqualTo(mapId2));
Assert.That(cGrid2X.MapID, Is.EqualTo(mapId2));
Assert.That(cParent3X.MapUid, Is.EqualTo(cMap2));
Assert.That(cChildX.MapUid, Is.EqualTo(cMap2));
Assert.That(cPlayerX.MapUid, Is.EqualTo(cMap2));
Assert.That(cGrid2X.MapUid, Is.EqualTo(cMap2));
// Create a new map & grid and move entities in the same tick
MapId mapId3 = default;
EntityUid map3 = default;
EntityUid grid3 = default;
EntityUid parent4 = default;
await server.WaitPost(() =>
{
map3 = mapSys.CreateMap(out mapId3);
var gridEnt = mapMan.CreateGridEntity(mapId3);
mapSys.SetTile(gridEnt.Owner, gridEnt.Comp, Vector2i.Zero, new Tile(1));
var grid3Coords = new EntityCoordinates(gridEnt, .5f, .5f);
grid3 = gridEnt.Owner;
parent4 = sEntMan.SpawnEntity(null, grid3Coords);
var parent4Coords = new EntityCoordinates(parent4, Vector2.Zero);
// Move existing entity to new parent
xformSys.SetCoordinates(player, parent4Coords);
// Move existing parent & child combination to new grid
xformSys.SetCoordinates(parent3, grid3Coords);
});
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Check all the transforms
var cParent4 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(parent4));
var cMap3 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(map3));
var cGrid3 = client.EntMan.GetEntity(server.EntMan.GetNetEntity(grid3));
Assert.That(cParent4, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cMap3, Is.Not.EqualTo(EntityUid.Invalid));
Assert.That(cGrid3, Is.Not.EqualTo(EntityUid.Invalid));
var cParent4X = client.Transform(cParent4);
var cGrid3X = client.Transform(cGrid3);
Assert.That(cChildX.ParentUid, Is.EqualTo(cParent3));
Assert.That(cPlayerX.ParentUid, Is.EqualTo(cParent4));
Assert.That(cParent3X.ParentUid, Is.EqualTo(cGrid3));
Assert.That(cParent4X.ParentUid, Is.EqualTo(cGrid3));
Assert.That(cGrid3X.ParentUid, Is.EqualTo(cMap3));
Assert.That(cChildX.GridUid, Is.EqualTo(cGrid3));
Assert.That(cPlayerX.GridUid, Is.EqualTo(cGrid3));
Assert.That(cParent3X.GridUid, Is.EqualTo(cGrid3));
Assert.That(cParent4X.GridUid, Is.EqualTo(cGrid3));
Assert.That(cGrid3X.GridUid, Is.EqualTo(cGrid3));
Assert.That(cChildX.MapID, Is.EqualTo(mapId3));
Assert.That(cPlayerX.MapID, Is.EqualTo(mapId3));
Assert.That(cParent3X.MapID, Is.EqualTo(mapId3));
Assert.That(cParent4X.MapID, Is.EqualTo(mapId3));
Assert.That(cGrid3X.MapID, Is.EqualTo(mapId3));
Assert.That(cChildX.MapUid, Is.EqualTo(cMap3));
Assert.That(cPlayerX.MapUid, Is.EqualTo(cMap3));
Assert.That(cParent3X.MapUid, Is.EqualTo(cMap3));
Assert.That(cParent4X.MapUid, Is.EqualTo(cMap3));
Assert.That(cGrid3X.MapUid, Is.EqualTo(cMap3));
}
}

View File

@@ -1,515 +0,0 @@
using System;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using NUnit.Framework;
using Robust.Client.Physics;
using Robust.Shared;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
namespace Robust.UnitTesting.Shared.Physics;
/// <summary>
/// This test is meant to check that collision start & stop events are raised correctly by the client.
/// The expectation is that start & stop events are only raised if the client predicts that two entities will move into
/// contact. They do not get raised as a result of applying component states received from the server.
/// I.e., the assumption is that if a collision results in changes to data on a component, then that data will already
/// have been sent to clients in the component's state, so we don't want to "double count" collisions.
/// </summary>
public sealed class CollisionPredictionTest : RobustIntegrationTest
{
private static readonly string Prototypes = @"
- type: entity
id: CollisionTest1
components:
- type: CollisionPredictionTest
- type: Physics
bodyType: Dynamic
sleepingAllowed: false
- type: entity
id: CollisionTest2
components:
- type: Physics
bodyType: Dynamic
sleepingAllowed: false
";
[Test]
[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public async Task TestCollisionPrediction(bool hard1, bool hard2)
{
var serverOpts = new ServerIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes };
var clientOpts = new ClientIntegrationOptions { Pool = false, ExtraPrototypes = Prototypes };
var server = StartServer(serverOpts);
var client = StartClient(clientOpts);
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
var netMan = client.ResolveDependency<IClientNetManager>();
Assert.DoesNotThrow(() => client.SetConnectTarget(server));
await server.WaitPost(() => server.CfgMan.SetCVar(CVars.NetPVS, false));
await client.WaitPost(() => netMan.ClientConnect(null!, 0, null!));
var sFix = server.System<FixtureSystem>();
var sPhys = server.System<SharedPhysicsSystem>();
var sSys = server.System<CollisionPredictionTestSystem>();
// Set up entities
EntityUid map = default;
EntityUid sEntity1 = default;
EntityUid sEntity2 = default;
MapCoordinates coords1 = default;
MapCoordinates coords2 = default;
await server.WaitPost(() =>
{
var radius = 0.25f;
map = server.System<SharedMapSystem>().CreateMap(out var mapId);
coords1 = new(default, mapId);
coords2 = new(Vector2.One, mapId);
sEntity1 = server.EntMan.Spawn("CollisionTest1", coords1);
sEntity2 = server.EntMan.Spawn("CollisionTest2", new MapCoordinates(coords2.Position + new Vector2(0, radius), mapId));
sFix.CreateFixture(sEntity1, "a", new Fixture(new PhysShapeCircle(radius), 1, 1, hard1));
sFix.CreateFixture(sEntity2, "a", new Fixture(new PhysShapeCircle(radius), 1, 1, hard2));
sPhys.SetCanCollide(sEntity1, true);
sPhys.SetCanCollide(sEntity2, true);
sPhys.SetAwake((sEntity1, server.EntMan.GetComponent<PhysicsComponent>(sEntity1)), true);
sPhys.SetAwake((sEntity2, server.EntMan.GetComponent<PhysicsComponent>(sEntity2)), true);
});
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
await server.WaitPost(() => server.PlayerMan.JoinGame(server.PlayerMan.Sessions.First()));
for (var i = 0; i < 10; i++)
{
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
// Ensure client & server ticks are synced.
// Client runs 2 tick ahead
{
var targetDelta = 2;
var sTick = (int)server.Timing.CurTick.Value;
var cTick = (int)client.Timing.CurTick.Value;
var delta = cTick - sTick;
if (delta > targetDelta)
await server.WaitRunTicks(delta - targetDelta);
else if (delta < targetDelta)
await client.WaitRunTicks(targetDelta - delta);
sTick = (int)server.Timing.CurTick.Value;
cTick = (int)client.Timing.CurTick.Value;
delta = cTick - sTick;
Assert.That(delta, Is.EqualTo(targetDelta));
}
var cPhys = client.System<SharedPhysicsSystem>();
var cSys = client.System<CollisionPredictionTestSystem>();
void ResetSystem()
{
sSys.CollisionEnded = false;
sSys.CollisionStarted = false;
cSys.CollisionEnded = false;
cSys.CollisionStarted = false;
}
async Task Tick()
{
ResetSystem();
await server.WaitRunTicks(1);
await client.WaitRunTicks(1);
}
var nEntity1 = server.EntMan.GetNetEntity(sEntity1);
var nEntity2 = server.EntMan.GetNetEntity(sEntity2);
var cEntity1 = client.EntMan.GetEntity(nEntity1);
var cEntity2 = client.EntMan.GetEntity(nEntity2);
var sComp = server.EntMan.GetComponent<CollisionPredictionTestComponent>(sEntity1);
var cComp = client.EntMan.GetComponent<CollisionPredictionTestComponent>(cEntity1);
cPhys.UpdateIsPredicted(cEntity1);
// Initially, the objects are not colliding.
{
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// We now simulate a predictive event that gets raised due to some client-side input, causing the entities to
// move and start colliding. Instead of setting up a proper input / keybind handler, The predictive event will
// just be raised in the system update method, which updates before the physics system does.
{
cSys.Ev = new CollisionTestMoveEvent(nEntity1, coords2);
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.True);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// Run another tick. Client should reset states, and re-predict the event.
{
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.WasTouching, Is.True);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.True);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// Next tick the server should raise the event received from the client, which will raise a serve-side
// collide-start event.
{
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.True);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.True);
Assert.That(cSys.CollisionStarted, Is.True);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// The client will have received the server-state, but will take some time for it to leave the state buffer.
// In the meantime, the client will keep predicting that the collision will "starts"
for (var i = 0; i < 2; i ++)
{
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.True);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.True);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// Then in the next tick the client should apply the new server state, wherein the contacts were already touching.
// I.e., the contact start event never actually gets raised.
{
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.False); // IsTouching gets resets to false before server state is applied
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// for the next few ticks, nothing should change
for (var i = 0; i < 10; i ++)
{
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.True);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.EquivalentTo(new []{ cEntity2 }));
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.EquivalentTo(new []{ cEntity1 }));
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// Next we move the entity away again, so the contact should stop
{
cSys.Ev = new CollisionTestMoveEvent(nEntity1, coords1);
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.True);
}
// Next tick, the client should reset to a state where the entities were touching, and then re-predict the stop-collide events
{
await Tick();
Assert.That(sComp.IsTouching, Is.True);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.EquivalentTo(new []{ sEntity2 }));
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.EquivalentTo(new []{ sEntity1 }));
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.True);
}
// Next, the server should receive the networked event
{
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.StopTick, Is.EqualTo(sComp.StopTick));
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.True);
Assert.That(cSys.CollisionEnded, Is.True);
}
// nothing changes while waiting for the client to apply the new server state
for (var i = 0; i < 2; i ++)
{
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.StopTick, Is.EqualTo(sComp.StopTick));
Assert.That(cComp.WasTouching, Is.False);
Assert.That(cComp.LastState, Is.True);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.True);
}
// And then the client should apply the new server state
{
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.StopTick, Is.EqualTo(sComp.StopTick));
Assert.That(cComp.WasTouching, Is.True);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
// Nothing should change in the next few ticks
for (var i = 0; i < 10; i ++)
{
await Tick();
Assert.That(sComp.IsTouching, Is.False);
Assert.That(cComp.IsTouching, Is.False);
Assert.That(cComp.StartTick, Is.EqualTo(sComp.StartTick));
Assert.That(cComp.StopTick, Is.EqualTo(sComp.StopTick));
Assert.That(cComp.WasTouching, Is.True);
Assert.That(cComp.LastState, Is.False);
Assert.That(sPhys.GetContactingEntities(sEntity1), Is.Empty);
Assert.That(sPhys.GetContactingEntities(sEntity2), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity1), Is.Empty);
Assert.That(cPhys.GetContactingEntities(cEntity2), Is.Empty);
Assert.That(sSys.CollisionStarted, Is.False);
Assert.That(cSys.CollisionStarted, Is.False);
Assert.That(sSys.CollisionEnded, Is.False);
Assert.That(cSys.CollisionEnded, Is.False);
}
}
}
[RegisterComponent, NetworkedComponent]
public sealed partial class CollisionPredictionTestComponent : Component
{
public bool IsTouching;
public bool WasTouching;
public bool LastState;
public GameTick StartTick;
public GameTick StopTick;
[Serializable, NetSerializable]
public sealed class State(bool isTouching) : ComponentState
{
public bool IsTouching = isTouching;
}
}
[Serializable, NetSerializable]
public sealed class CollisionTestMoveEvent(NetEntity ent, MapCoordinates coords) : EntityEventArgs
{
public NetEntity Ent = ent;
public MapCoordinates Coords = coords;
}
public sealed class CollisionPredictionTestSystem : EntitySystem
{
[Dependency] private readonly SharedTransformSystem _xform = default!;
[Dependency] private readonly IGameTiming _timing = default!;
public bool CollisionStarted;
public bool CollisionEnded;
public override void Initialize()
{
SubscribeLocalEvent<CollisionPredictionTestComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<CollisionPredictionTestComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<CollisionPredictionTestComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<CollisionPredictionTestComponent, EndCollideEvent>(OnEndCollide);
SubscribeLocalEvent<CollisionPredictionTestComponent, UpdateIsPredictedEvent>(OnIsPredicted);
SubscribeAllEvent<CollisionTestMoveEvent>(OnMove);
// Updates before physics to simulate input events.
// inputs are processed before systems update, but I CBF setting up a proper input / keybinding.
UpdatesBefore.Add(typeof(SharedPhysicsSystem));
}
public CollisionTestMoveEvent? Ev;
public override void Update(float frameTime)
{
if (Ev == null || !_timing.IsFirstTimePredicted)
return;
RaisePredictiveEvent(Ev);
Ev = null;
}
private void OnIsPredicted(Entity<CollisionPredictionTestComponent> ent, ref UpdateIsPredictedEvent args)
{
args.IsPredicted = true;
}
private void OnMove(CollisionTestMoveEvent ev)
{
_xform.SetMapCoordinates(GetEntity(ev.Ent), ev.Coords);
}
private void OnEndCollide(Entity<CollisionPredictionTestComponent> ent, ref EndCollideEvent args)
{
// TODO PHYSICS Collision Mispredicts
// Currently the client will raise collision start/stop events multiple times for each collision
// If this ever gets fixed, re-add the assert:
// Assert.That(ent.Comp.IsTouching, Is.True);
if (!ent.Comp.IsTouching)
return;
Assert.That(CollisionEnded, Is.False);
ent.Comp.StopTick = _timing.CurTick;
ent.Comp.IsTouching = false;
CollisionEnded = true;
Dirty(ent);
}
private void OnStartCollide(Entity<CollisionPredictionTestComponent> ent, ref StartCollideEvent args)
{
// TODO PHYSICS Collision Mispredicts
// Currently the client will raise collision start/stop events multiple times for each collision
// If this ever gets fixed, re-add the assert:
// Assert.That(ent.Comp.IsTouching, Is.False);
if (ent.Comp.IsTouching)
return;
Assert.That(CollisionStarted, Is.False);
ent.Comp.StartTick = _timing.CurTick;
ent.Comp.IsTouching = true;
CollisionStarted = true;
Dirty(ent);
}
private void OnGetState(Entity<CollisionPredictionTestComponent> ent, ref ComponentGetState args)
{
args.State = new CollisionPredictionTestComponent.State(ent.Comp.IsTouching);
}
private void OnHandleState(Entity<CollisionPredictionTestComponent> ent, ref ComponentHandleState args)
{
if (args.Current is not CollisionPredictionTestComponent.State state)
return;
ent.Comp.WasTouching = ent.Comp.IsTouching;
ent.Comp.LastState = state.IsTouching;
ent.Comp.IsTouching = state.IsTouching;
}
}