Reactors, sonars, nav terminals, pumps and batteries use similar delayed correction logic as doors and inventories.

I.e. the clients delay correcting the state of the item until the local player stops manipulating the state (atm the delay is 1 sec). Prevents pumping speeds, steering directions and whatnot from switching to an old state and back - now the corrections should not be visible to the players unless the client predicts the state wrong.
This commit is contained in:
Regalis
2017-04-11 00:41:12 +03:00
parent fac31b4892
commit 347f549ac1
8 changed files with 114 additions and 25 deletions
@@ -59,9 +59,7 @@ namespace Barotrauma.Items.Components
public bool WasUsed;
public readonly Dictionary<ActionType, List<StatusEffect>> statusEffectLists;
protected bool updated;
public List<RelatedItem> requiredItems;
public List<Skill> requiredSkills;
@@ -72,6 +70,10 @@ namespace Barotrauma.Items.Components
public ItemComponent Parent;
protected const float CorrectionDelay = 1.0f;
protected CoroutineHandle delayedCorrectionCoroutine;
protected float correctionTimer;
private string msg;
[HasDefaultValue(0.0f, false)]
@@ -86,15 +88,7 @@ namespace Barotrauma.Items.Components
{
get { return properties; }
}
//has the component already been updated this frame
public bool Updated
{
get { return updated; }
set { updated = value; }
}
public bool NetworkUpdateSent;
public virtual bool IsActive
{
get { return isActive; }
@@ -535,6 +529,12 @@ namespace Barotrauma.Items.Components
Sounds.SoundManager.Stop(loopingSoundIndex);
}
if (delayedCorrectionCoroutine != null)
{
CoroutineManager.StopCoroutines(delayedCorrectionCoroutine);
delayedCorrectionCoroutine = null;
}
RemoveComponentSpecific();
}
@@ -680,6 +680,30 @@ namespace Barotrauma.Items.Components
}
}
//Starts a coroutine that will read the correct state of the component from the NetBuffer when correctionTimer reaches zero.
protected void StartDelayedCorrection(ServerNetObject type, NetBuffer buffer, float sendingTime)
{
if (delayedCorrectionCoroutine != null) CoroutineManager.StopCoroutines(delayedCorrectionCoroutine);
delayedCorrectionCoroutine = CoroutineManager.StartCoroutine(DoDelayedCorrection(type, buffer, sendingTime));
}
private IEnumerable<object> DoDelayedCorrection(ServerNetObject type, NetBuffer buffer, float sendingTime)
{
while (correctionTimer > 0.0f)
{
correctionTimer -= CoroutineManager.DeltaTime;
yield return CoroutineStatus.Running;
}
((IServerSerializable)this).ClientRead(type, buffer, sendingTime);
correctionTimer = 0.0f;
delayedCorrectionCoroutine = null;
yield return CoroutineStatus.Success;
}
public virtual XElement Save(XElement parentElement)
{
XElement componentElement = new XElement(name);