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 @@ -29,6 +29,7 @@ public class FieldGenerator : MyGameLogicComponent, IMyEventProxy
{
private IMyCubeBlock Block;
private readonly bool IsServer = MyAPIGateway.Session.IsServer;
private readonly bool IsDedicated = MyAPIGateway.Utilities.IsDedicated;
public readonly Guid SettingsID = new Guid("7A7AC398-FAE3-44E5-ABD5-8AE49434DDF6");

private Generator_Settings Config = FieldGenerator_Config.Config;
Expand Down Expand Up @@ -133,9 +134,10 @@ public override void UpdateOnceBeforeFrame()
Stability.ValueChanged += Stability_ValueChanged;
}

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

NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME;
Expand Down Expand Up @@ -173,22 +175,17 @@ public override void UpdateAfterSimulation()
}
}

if (SiegeMode.Value && !GridStopped.Value)
if (!IsDedicated)
{
if (!SlowdownActive.Value)
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 (_cachedState != Block.IsWorking)
{
_cachedState = Block.IsWorking;
Block_IsWorkingChanged();
}

if (MyAPIGateway.Session.GameplayFrameCounter % 60 == 0)
{
if (Block.IsWorking)
Expand Down Expand Up @@ -298,10 +295,11 @@ public override void Close()
Stability.ValueChanged -= Stability_ValueChanged;
}

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

Block = null;
}
Expand Down Expand Up @@ -440,18 +438,25 @@ private void OnGridStopValueChange(MySync<bool, SyncDirection.FromServer> obj)
Block.CubeGrid.Physics.LinearVelocity = Vector3.Zero;
}

private void Block_IsWorkingChanged()
{
if (!Block.IsWorking)
private void Block_IsWorkingChanged(IMyCubeBlock block)
{
if (IsClientInShip())
{
BlockSoundPair = Block.IsFunctional ? BlockSoundPair = new MySoundPair("FieldGen_Offline") : BlockSoundPair = new MySoundPair("FieldGen_Damaged");
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
if (!block.IsWorking)
{
BlockSoundPair = block.IsFunctional ? BlockSoundPair = new MySoundPair("FieldGen_Offline") : BlockSoundPair = new MySoundPair("FieldGen_Damaged");
BlockSoundEmitter.SetPosition(Block.Position);
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
}
else
{
BlockSoundPair = block.SlimBlock.IsFullIntegrity ? BlockSoundPair = new MySoundPair("FieldGen_PowerRestored") : BlockSoundPair = new MySoundPair("FieldGen_DamagedRepaired");
BlockSoundEmitter.SetPosition(block.Position);
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
}
}
else
{
BlockSoundPair = Block.SlimBlock.IsFullIntegrity ? BlockSoundPair = new MySoundPair("FieldGen_PowerRestored") : BlockSoundPair = new MySoundPair("FieldGen_DamagedRepaired");
BlockSoundEmitter?.PlaySound(BlockSoundPair, false, false, true, true, false, null, true);
}
return;
}
#endregion

Expand Down Expand Up @@ -526,32 +531,17 @@ private void SiegeEmergencyStop()
if (Block.CubeGrid.Physics == null)
return;

// Linear Drag
float elapsed = _stopTickCounter / 60f;
float maxTime = 10f;

float minForce = 1000000f;
float maxForce = 120000000f;

float timeScale = MathHelper.Clamp(elapsed / maxTime, 0f, 1f);
float forceMagnitude = MathHelper.Lerp(minForce, maxForce, timeScale);
float timeScale = MathHelper.Clamp(elapsed / 10f, 0f, 1f);
float forceMagnitude = MathHelper.Lerp(1000000f, 120000000f, timeScale);

Vector3 direction = Vector3.Normalize(Block.CubeGrid.Physics.LinearVelocity);
Vector3 linearDrag = -direction * forceMagnitude;

Block.CubeGrid.Physics.AddForce(MyPhysicsForceType.APPLY_WORLD_FORCE, linearDrag, null, null);

if (Block.CubeGrid.Physics.LinearVelocity.LengthSquared() < 25f || elapsed >= maxTime) // 5 m/s squared = 25
{
Block.CubeGrid.Physics.LinearVelocity = Vector3D.Zero;
GridStopped.Value = true;
SlowdownActive.Value = false;
_stopTickCounter = -1;
angularAxis1 = null;
angularAxis2 = null;
return;
}

// ----- ANGULAR DRAG -----
// Angular Drag
if (angularAxis1 == null)
{
angularAxis1 = GetRandomUnitVector();
Expand All @@ -566,15 +556,23 @@ private void SiegeEmergencyStop()
angularAxis2 = candidate;
}

float targetAngularAccel = 0.1f;
Vector3 torqueDirection = new Vector3(0,0,0);

Vector3 torqueDirection = new Vector3(0, 0, 0);
torqueDirection += angularAxis1.Value;
torqueDirection += angularAxis2.Value;

Vector3 angularImpulse = torqueDirection * targetAngularAccel / 60f;
Block.CubeGrid.Physics.AngularVelocity += torqueDirection * 0.1f / 60f;

Block.CubeGrid.Physics.AngularVelocity += angularImpulse;
// Finish if under 5m/s
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;
_stopTickCounter = -1;
angularAxis1 = null;
angularAxis2 = null;
return;
}
}

Vector3 GetRandomUnitVector()
Expand Down Expand Up @@ -731,11 +729,11 @@ public float CalculatePowerDraw()

public float CheckPowerDraw(float fieldValue)
{
float maxPossibleFieldPowerV = Config.PerModuleAmount * Config.MaxModuleCount;
float clampedFieldPowerV = MathHelper.Clamp(fieldValue, 0, maxPossibleFieldPowerV);
float tV = clampedFieldPowerV / maxPossibleFieldPowerV;
float maxPossibleFieldPower = Config.PerModuleAmount * Config.MaxModuleCount;
float clampedFieldPower = MathHelper.Clamp(fieldValue, 0, maxPossibleFieldPower);
float t = clampedFieldPower / maxPossibleFieldPower;

return Config.MinPowerDraw + tV * (Config.MaxPowerDraw - Config.MinPowerDraw);
return Config.MinPowerDraw + t * (Config.MaxPowerDraw - Config.MinPowerDraw);
}

public float CheckPowerGeneration()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.