Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using VRageMath;
using static Draygo.API.HudAPIv2;
using static VRageRender.MyBillboard;
using static VRage.Game.MyObjectBuilder_BehaviorTreeDecoratorNode;


namespace Starcore.FieldGenerator
Expand Down Expand Up @@ -53,8 +54,7 @@ public class FieldGenerator : MyGameLogicComponent, IMyEventProxy
#region Sync Properties
public MySync<bool, SyncDirection.BothWays> SiegeMode;
public MySync<bool, SyncDirection.BothWays> SiegeCooldownActive;
public MySync<bool, SyncDirection.BothWays> SlowdownActive;
public MySync<bool, SyncDirection.FromServer> GridStopped;
public MySync<bool, SyncDirection.FromServer> _slowdownComplete;

public MySync<int, SyncDirection.BothWays> SiegeElapsedTime;
public MySync<int, SyncDirection.BothWays> SiegeCooldownTime;
Expand Down Expand Up @@ -136,7 +136,6 @@ public override void UpdateOnceBeforeFrame()

if (!IsDedicated)
{
GridStopped.ValueChanged += OnGridStopValueChange;
Block.IsWorkingChanged += Block_IsWorkingChanged;
}

Expand Down Expand Up @@ -165,40 +164,26 @@ public override void UpdateAfterSimulation()
}
}

if (SiegeMode.Value && !GridStopped.Value)
if (Block.IsWorking)
{
if (Block.CubeGrid.Physics.LinearVelocity != Vector3D.Zero)
UpdateSiegeState();
}
else if (!Block.IsWorking)
{
if (SiegeMode.Value)
{
SiegeEmergencyStop();
_stopTickCounter++;
EndSiegeState(true);
SiegeMode.Value = false;
}
else
GridStopped.Value = true;
}
}

if (!IsDedicated)
{
if (SiegeMode.Value && !GridStopped.Value && !SlowdownActive.Value && IsClientInShip())
{
BlockSoundPair = new MySoundPair("FieldGen_Brake");
BlockSoundEmitter.SetPosition(Block.Position);
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
SlowdownActive.Value = true;
}
}
}

if (MyAPIGateway.Session.GameplayFrameCounter % 60 == 0)
{
if (Block.IsWorking)
{
if (IsServer)
{
if (GridStopped.Value || SiegeCooldownActive.Value)
{
UpdateSiegeState();
}

{
if (!Config.SimplifiedMode)
{
if (_lowStability && Stability.Value < 100)
Expand Down Expand Up @@ -247,13 +232,7 @@ public override void UpdateAfterSimulation()
{
if (FieldPower.Value > 0)
FieldPower.Value = 0;

if (SiegeMode.Value)
{
CancelSiegeMode();
SiegeMode.Value = false;
}


HandleResistence();
}
else
Expand Down Expand Up @@ -299,7 +278,6 @@ public override void Close()

if (!IsDedicated)
{
GridStopped.ValueChanged -= OnGridStopValueChange;
Block.IsWorkingChanged -= Block_IsWorkingChanged;
}

Expand Down Expand Up @@ -389,7 +367,7 @@ private void HandleResistence()
return;
}

if (SiegeMode.Value && GridStopped.Value)
if (SiegeMode.Value)
{
MyVisualScriptLogicProvider.SetGridGeneralDamageModifier(Block.CubeGrid.Name, (1 - Config.SiegeModeResistence));
return;
Expand All @@ -414,6 +392,13 @@ private void SiegeMode_ValueChanged(MySync<bool, SyncDirection.BothWays> obj)

if (IsServer)
HandleResistence();

if (!IsDedicated && obj.Value && IsClientInShip())
{
BlockSoundPair = new MySoundPair("FieldGen_Brake");
BlockSoundEmitter.SetPosition(Block.Position);
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
}
}

private void FieldPower_ValueChanged(MySync<float, SyncDirection.BothWays> obj)
Expand All @@ -433,13 +418,7 @@ private void Stability_ValueChanged(MySync<float, SyncDirection.BothWays> obj)
if (IsServer)
HandleZeroStability();
}

private void OnGridStopValueChange(MySync<bool, SyncDirection.FromServer> obj)
{
if (obj?.Value ?? false)
Block.CubeGrid.Physics.LinearVelocity = Vector3.Zero;
}


private void Block_IsWorkingChanged(IMyCubeBlock block)
{
if (IsClientInShip())
Expand Down Expand Up @@ -467,43 +446,56 @@ private void UpdateSiegeState()
{
if (SiegeMode.Value && !SiegeCooldownActive.Value)
{
if (SiegeElapsedTime.Value + 1 <= Config.MaxSiegeTime)
{
SiegeElapsedTime.Value++; ;
SiegeBlockEnabler(Block.CubeGrid.GetFatBlocks<IMyFunctionalBlock>(), false);

if (!_slowdownComplete.Value)
{
SiegeSlowdown();
_stopTickCounter++;
return;
}
else if (_slowdownComplete.Value)
{
if (Block.CubeGrid.Physics.LinearVelocity != Vector3D.Zero)
{
Block.CubeGrid.Physics.LinearVelocity = Vector3.Zero;
if (IsServer && !GridStopped.Value)
GridStopped.Value = true;
}
}
else
{
EndSiegeMode();
SiegeMode.Value = false;
return;
}
}
}

if (!SiegeMode.Value && !SiegeCooldownActive.Value && SiegeElapsedTime.Value > 0)
if (MyAPIGateway.Session.GameplayFrameCounter % 60 == 0)
{
EndSiegeMode();
return;
}
if (SiegeMode.Value && !SiegeCooldownActive.Value)
{
if (SiegeElapsedTime.Value + 1 <= Config.MaxSiegeTime)
{
SiegeElapsedTime.Value++; ;
SiegeBlockEnabler(Block.CubeGrid.GetFatBlocks<IMyFunctionalBlock>(), false);
}
else
{
EndSiegeState();
SiegeMode.Value = false;
return;
}
}

if (SiegeCooldownActive.Value)
{
if (SiegeCooldownTime.Value > 0)
if (!SiegeMode.Value && !SiegeCooldownActive.Value && SiegeElapsedTime.Value > 0)
{
SiegeCooldownTime.Value--;
EndSiegeState();
return;
}
else

if (SiegeCooldownActive.Value)
{
SiegeCooldownActive.Value = false;
if (SiegeCooldownTime.Value > 0)
{
SiegeCooldownTime.Value--;
}
else
{
SiegeCooldownActive.Value = false;
}
}
}
}
}

private void SiegeBlockEnabler(IEnumerable<IMyFunctionalBlock> allFunctionalBlocks, bool enabled)
Expand All @@ -528,7 +520,7 @@ private void SiegeBlockEnabler(IEnumerable<IMyFunctionalBlock> allFunctionalBloc
}
}

private void SiegeEmergencyStop()
private void SiegeSlowdown()
{
if (Block.CubeGrid.Physics == null)
return;
Expand Down Expand Up @@ -568,11 +560,10 @@ private void SiegeEmergencyStop()
if (Block.CubeGrid.Physics.LinearVelocity.LengthSquared() < 25f || elapsed >= 10f) // 5 m/s squared = 25
{
Block.CubeGrid.Physics.LinearVelocity = Vector3D.Zero;
GridStopped.Value = true;
SlowdownActive.Value = false;
_slowdownComplete.Value = true;
_stopTickCounter = -1;
angularAxis1 = null;
angularAxis2 = null;
angularAxis2 = null;
return;
}
}
Expand All @@ -587,27 +578,13 @@ Vector3 GetRandomUnitVector()
return Vector3.Normalize(v);
}

private void EndSiegeMode()
private void EndSiegeState(bool cancel = false)
{
if (IsServer && GridStopped.Value)
GridStopped.Value = false;

SiegeBlockEnabler(Block.CubeGrid.GetFatBlocks<IMyFunctionalBlock>(), true);

SiegeCooldownTime.Value = Math.Max(SiegeElapsedTime.Value * 2, Config.MinSiegeTime);
SiegeElapsedTime.Value = 0;
SiegeCooldownActive.Value = true;
}

private void CancelSiegeMode()
{
if (IsServer && GridStopped.Value)
GridStopped.Value = false;

SiegeBlockEnabler(Block.CubeGrid.GetFatBlocks<IMyFunctionalBlock>(), true);

SiegeCooldownTime.Value = 0;
SiegeCooldownTime.Value = cancel ? 0 : Math.Max(SiegeElapsedTime.Value * 2, Config.MinSiegeTime);
SiegeElapsedTime.Value = 0;
SiegeCooldownActive.Value = cancel ? false : true;
}
#endregion

Expand Down Expand Up @@ -888,7 +865,7 @@ private void UpdateHUD()
GeneratorHUDContent.Append(GenerateBar("Stability:", Stability.Value, 100, true));
}

if (SiegeMode.Value && !GridStopped.Value)
if (SiegeMode.Value && !_slowdownComplete.Value)
{
GeneratorHUDContent.Append($"\nSiege Mode Actived | Emergency Braking Applied");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static bool CooldownEnabler(IMyTerminalBlock b)
var logic = GetLogic(b);
if (logic != null)
{
return !logic.SiegeCooldownActive && !logic.SlowdownActive;
return logic.SiegeMode.Value ? logic._slowdownComplete.Value : !logic.SiegeCooldownActive.Value;
}
return false;
}
Expand Down