Reapply "OBT1.1.0 Merge branch 'dev_pte' into dev"

This reverts commit 046483b9da.
This commit is contained in:
NotAlwaysTrue
2026-04-30 21:59:54 +08:00
parent 02689d0d86
commit 25683dcf39
85 changed files with 2413 additions and 779 deletions
@@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
@@ -485,7 +486,10 @@ namespace Barotrauma.Items.Components
}
else
{
item.body.ResetDynamics();
// Calculate target position
Vector2 targetPos;
Submarine forceSubmarine = picker.Submarine;
Limb heldHand, arm;
if (picker.Inventory.IsInLimbSlot(item, InvSlotType.LeftHand))
{
@@ -503,17 +507,42 @@ namespace Barotrauma.Items.Components
Vector2 diff = new Vector2(
(heldHand.SimPosition.X - arm.SimPosition.X) / 2f,
(heldHand.SimPosition.Y - arm.SimPosition.Y) / 2.5f);
targetPos = heldHand.SimPosition + diff;
}
else
{
targetPos = picker.SimPosition;
}
// Defer physics operations if in parallel context
if (PhysicsBodyQueue.IsInParallelContext)
{
var capturedBody = item.body;
var capturedItem = item;
var capturedTargetPos = targetPos;
var capturedForceSubmarine = forceSubmarine;
PhysicsBodyQueue.Enqueue(() =>
{
if (capturedBody.Removed || capturedItem.Removed) { return; }
capturedBody.ResetDynamics();
//we have forced the item to be in the same sub as the dropper above,
//and are placing it to the position of the hands in "local" coordinates
//which may be outside the sub if the character is e.g. standing half-way through the airlock
// -> let's use the forceSubmarine argument ensure the item is still considered to be in the sub's coordinate space,
// or it will end up in a weird state and seemingly disappear
capturedItem.SetTransform(capturedTargetPos, 0.0f, forceSubmarine: capturedForceSubmarine);
});
}
else
{
item.body.ResetDynamics();
//we have forced the item to be in the same sub as the dropper above,
//and are placing it to the position of the hands in "local" coordinates
//which may be outside the sub if the character is e.g. standing half-way through the airlock
// -> let's use the forceSubmarine argument ensure the item is still considered to be in the sub's coordinate space,
// or it will end up in a weird state and seemingly disappear
item.SetTransform(heldHand.SimPosition + diff, 0.0f, forceSubmarine: picker.Submarine);
}
else
{
item.SetTransform(picker.SimPosition, 0.0f, forceSubmarine: picker.Submarine);
item.SetTransform(targetPos, 0.0f, forceSubmarine: forceSubmarine);
}
}
}
@@ -616,12 +645,13 @@ namespace Barotrauma.Items.Components
return CanBeAttached(user, out _);
}
private static List<Item> tempOverlappingItems = new List<Item>();
private static readonly ThreadLocal<List<Item>> tempOverlappingItems = new ThreadLocal<List<Item>>(() => new List<Item>());
private bool CanBeAttached(Character user, out IEnumerable<Item> overlappingItems)
{
tempOverlappingItems.Clear();
overlappingItems = tempOverlappingItems;
var overlapping = tempOverlappingItems.Value;
overlapping.Clear();
overlappingItems = overlapping;
if (!attachable || !Reattachable) { return false; }
//can be attached anywhere in sub editor
@@ -664,9 +694,9 @@ namespace Barotrauma.Items.Components
}
if (attachPos.X + size.X < worldRect.X || attachPos.X - size.X > worldRect.Right) { continue; }
if (attachPos.Y - size.Y > worldRect.Y || attachPos.Y + size.Y < worldRect.Y - worldRect.Height) { continue; }
tempOverlappingItems.Add(otherItem);
overlapping.Add(otherItem);
}
if (tempOverlappingItems.Any()) { return false; }
if (overlapping.Any()) { return false; }
}
//can be attached anywhere inside hulls
@@ -13,6 +13,12 @@ namespace Barotrauma.Items.Components
private Holdable holdable;
private float deattachTimer;
/// <summary>
/// Flag to prevent multiple queued creation requests.
/// Uses volatile to ensure visibility across threads.
/// </summary>
private volatile bool triggerBodyCreationQueued;
[Serialize(1.0f, IsPropertySaveable.No, description: "How long it takes to deattach the item from the level walls (in seconds).")]
public float DeattachDuration
@@ -86,13 +92,16 @@ namespace Barotrauma.Items.Components
{
if (trigger != null && amount.LengthSquared() > 0.00001f)
{
// Defer physics operation if in parallel context (Farseer is not thread-safe)
var capturedTrigger = trigger;
var capturedPos = item.SimPosition;
if (ignoreContacts)
{
trigger.SetTransformIgnoreContacts(item.SimPosition, 0.0f);
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransformIgnoreContacts(capturedPos, 0.0f));
}
else
{
trigger.SetTransform(item.SimPosition, 0.0f);
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
}
}
}
@@ -109,13 +118,29 @@ namespace Barotrauma.Items.Components
}
else
{
if (trigger == null)
if (trigger == null && !triggerBodyCreationQueued)
{
CreateTriggerBody();
// Queue the physics body creation to be processed on the main thread.
// This is necessary because physics body creation is not thread-safe
// and Update() may be called from a parallel loop.
triggerBodyCreationQueued = true;
PhysicsBodyQueue.EnqueueCreation(() =>
{
// Double-check that trigger hasn't been created yet
// (in case this was called multiple times before queue processing)
if (trigger == null && !item.Removed)
{
CreateTriggerBody();
}
triggerBodyCreationQueued = false;
});
}
if (trigger != null && Vector2.DistanceSquared(item.SimPosition, trigger.SimPosition) > 0.01f)
{
trigger.SetTransform(item.SimPosition, 0.0f);
// Defer physics operation if in parallel context (Farseer is not thread-safe)
var capturedTrigger = trigger;
var capturedPos = item.SimPosition;
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
}
IsActive = false;
}
@@ -4,6 +4,7 @@ using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
@@ -25,7 +26,7 @@ namespace Barotrauma.Items.Components
private readonly HashSet<Entity> hitTargets = new HashSet<Entity>();
private readonly Queue<Fixture> impactQueue = new Queue<Fixture>();
private readonly ConcurrentQueue<Fixture> impactQueue = new ConcurrentQueue<Fixture>();
public Character User { get; private set; }
@@ -191,17 +192,16 @@ namespace Barotrauma.Items.Components
{
if (!item.body.Enabled)
{
impactQueue.Clear();
while (impactQueue.TryDequeue(out _)) { } // Clear queue
return;
}
if (picker == null || !picker.HeldItems.Contains(item))
{
impactQueue.Clear();
while (impactQueue.TryDequeue(out _)) { } // Clear queue
IsActive = false;
}
while (impactQueue.Count > 0)
while (impactQueue.TryDequeue(out var impact))
{
var impact = impactQueue.Dequeue();
HandleImpact(impact);
}
//in case handling the impact does something to the picker
@@ -301,7 +301,7 @@ namespace Barotrauma.Items.Components
private void RestoreCollision()
{
impactQueue.Clear();
while (impactQueue.TryDequeue(out _)) { } // Clear queue
item.body.FarseerBody.OnCollision -= OnCollision;
item.body.CollisionCategories = Physics.CollisionItem;
item.body.CollidesWith = Physics.DefaultItemCollidesWith;
@@ -4,6 +4,7 @@ using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Barotrauma.Extensions;
using Barotrauma.MapCreatures.Behavior;
@@ -315,7 +316,7 @@ namespace Barotrauma.Items.Components
partial void UseProjSpecific(float deltaTime, Vector2 raystart);
private static readonly List<Body> hitBodies = new List<Body>();
private static readonly ThreadLocal<List<Body>> hitBodies = new ThreadLocal<List<Body>>(() => new List<Body>());
private readonly HashSet<Character> hitCharacters = new HashSet<Character>();
private readonly List<FireSource> fireSourcesInRange = new List<FireSource>();
private void Repair(Vector2 rayStart, Vector2 rayEnd, float deltaTime, Character user, float degreeOfSuccess, List<Body> ignoredBodies)
@@ -373,13 +374,13 @@ namespace Barotrauma.Items.Components
},
allowInsideFixture: true);
hitBodies.Clear();
hitBodies.AddRange(bodies.Distinct());
hitBodies.Value.Clear();
hitBodies.Value.AddRange(bodies.Distinct());
lastPickedFraction = Submarine.LastPickedFraction;
Type lastHitType = null;
hitCharacters.Clear();
foreach (Body body in hitBodies)
foreach (Body body in hitBodies.Value)
{
Type bodyType = body.UserData?.GetType();
if (!RepairThroughWalls && bodyType != null && bodyType != lastHitType)
@@ -897,48 +898,49 @@ namespace Barotrauma.Items.Components
}
}
private static List<ISerializableEntity> currentTargets = new List<ISerializableEntity>();
private static readonly ThreadLocal<List<ISerializableEntity>> currentTargets = new ThreadLocal<List<ISerializableEntity>>(() => new List<ISerializableEntity>());
private void ApplyStatusEffectsOnTarget(Character user, float deltaTime, ActionType actionType, Item targetItem = null, Character character = null, Limb limb = null, Structure structure = null)
{
if (statusEffectLists == null) { return; }
if (!statusEffectLists.TryGetValue(actionType, out List<StatusEffect> statusEffects)) { return; }
var targets = currentTargets.Value;
foreach (StatusEffect effect in statusEffects)
{
currentTargets.Clear();
targets.Clear();
effect.SetUser(user);
if (effect.HasTargetType(StatusEffect.TargetType.UseTarget))
{
if (targetItem != null)
{
currentTargets.AddRange(targetItem.AllPropertyObjects);
targets.AddRange(targetItem.AllPropertyObjects);
}
if (structure != null)
{
currentTargets.Add(structure);
targets.Add(structure);
}
if (character != null)
{
currentTargets.Add(character);
targets.Add(character);
}
effect.Apply(actionType, deltaTime, item, currentTargets);
effect.Apply(actionType, deltaTime, item, targets);
}
else if (effect.HasTargetType(StatusEffect.TargetType.Character))
{
currentTargets.Add(user);
effect.Apply(actionType, deltaTime, item, currentTargets);
targets.Add(user);
effect.Apply(actionType, deltaTime, item, targets);
}
else if (effect.HasTargetType(StatusEffect.TargetType.Limb))
{
currentTargets.Add(limb);
effect.Apply(actionType, deltaTime, item, currentTargets);
targets.Add(limb);
effect.Apply(actionType, deltaTime, item, targets);
}
#if CLIENT
if (user == null) { return; }
// Hard-coded progress bars for welding doors stuck.
// A general purpose system could be better, but it would most likely require changes in the way we define the status effects in xml.
foreach (ISerializableEntity target in currentTargets)
foreach (ISerializableEntity target in targets)
{
if (target is not Door door) { continue; }
if (!door.CanBeWelded || !door.Item.IsInteractable(user)) { continue; }