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
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.
102 changes: 102 additions & 0 deletions Utility Mods/SCDefenseBlocks/Data/Audio_FieldGen.sbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0"?>
<Definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Sounds>

<Sound>
<Id>
<TypeId>MyObjectBuilder_AudioDefinition</TypeId>
<SubtypeId>FieldGen_Brake</SubtypeId>
</Id>
<Category>HUD</Category>
<DisablePitchEffects>true</DisablePitchEffects>
<Loopable>false</Loopable>
<MaxDistance>50</MaxDistance>
<ModifiableByHelmetFilters>false</ModifiableByHelmetFilters>
<PreventSynchronization>4</PreventSynchronization>
<Volume>0.75</Volume>
<Waves>
<Wave Type="D2">
<Start>Audio\Brake_Applied.wav</Start>
</Wave>
</Waves>
</Sound>

<Sound>
<Id>
<TypeId>MyObjectBuilder_AudioDefinition</TypeId>
<SubtypeId>FieldGen_Damaged</SubtypeId>
</Id>
<Category>HUD</Category>
<DisablePitchEffects>true</DisablePitchEffects>
<Loopable>false</Loopable>
<MaxDistance>50</MaxDistance>
<ModifiableByHelmetFilters>false</ModifiableByHelmetFilters>
<PreventSynchronization>4</PreventSynchronization>
<Volume>0.75</Volume>
<Waves>
<Wave Type="D2">
<Start>Audio\Warning_Damaged.wav</Start>
<Start>Audio\Alert_Damaged.wav</Start>
</Wave>
</Waves>
</Sound>

<Sound>
<Id>
<TypeId>MyObjectBuilder_AudioDefinition</TypeId>
<SubtypeId>FieldGen_Offline</SubtypeId>
</Id>
<Category>HUD</Category>
<DisablePitchEffects>true</DisablePitchEffects>
<Loopable>false</Loopable>
<MaxDistance>50</MaxDistance>
<ModifiableByHelmetFilters>false</ModifiableByHelmetFilters>
<PreventSynchronization>4</PreventSynchronization>
<Volume>0.75</Volume>
<Waves>
<Wave Type="D2">
<Start>Audio\Warning_Offline.wav</Start>
<Start>Audio\Alert_Offline.wav</Start>
</Wave>
</Waves>
</Sound>

<Sound>
<Id>
<TypeId>MyObjectBuilder_AudioDefinition</TypeId>
<SubtypeId>FieldGen_PowerRestored</SubtypeId>
</Id>
<Category>HUD</Category>
<DisablePitchEffects>true</DisablePitchEffects>
<Loopable>false</Loopable>
<MaxDistance>50</MaxDistance>
<ModifiableByHelmetFilters>false</ModifiableByHelmetFilters>
<PreventSynchronization>4</PreventSynchronization>
<Volume>0.75</Volume>
<Waves>
<Wave Type="D2">
<Start>Audio\PowerRestored.wav</Start>
</Wave>
</Waves>
</Sound>

<Sound>
<Id>
<TypeId>MyObjectBuilder_AudioDefinition</TypeId>
<SubtypeId>FieldGen_DamagedRepaired</SubtypeId>
</Id>
<Category>HUD</Category>
<DisablePitchEffects>true</DisablePitchEffects>
<Loopable>false</Loopable>
<MaxDistance>50</MaxDistance>
<ModifiableByHelmetFilters>false</ModifiableByHelmetFilters>
<PreventSynchronization>4</PreventSynchronization>
<Volume>0.75</Volume>
<Waves>
<Wave Type="D2">
<Start>Audio\DamageRepaired.wav</Start>
</Wave>
</Waves>
</Sound>
</Sounds>
</Definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using ProtoBuf;
using Sandbox.Common.ObjectBuilders;
using Sandbox.Game;
using Sandbox.Game.Entities;
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
Expand All @@ -16,11 +16,11 @@
using VRage.Network;
using VRage.ObjectBuilders;
using VRage.Sync;
using VRage.Utils;
using VRageMath;
using static Draygo.API.HudAPIv2;
using static VRageRender.MyBillboard;
using Sandbox.Game.Gui;
using Draygo.API;


namespace Starcore.FieldGenerator
{
Expand All @@ -36,15 +36,24 @@ public class FieldGenerator : MyGameLogicComponent, IMyEventProxy
private int _damageEventCounter = 0;
private float _stabilityChange = 0;
private int _resetCounter = 0;
private bool _lowStability = false;
private bool _lowStability = false;

internal int _stopTickCounter = -1;
internal Vector3? angularAxis1 = null;
internal Vector3? angularAxis2 = null;

internal bool _cachedState;
internal MyEntity3DSoundEmitter BlockSoundEmitter;
internal MySoundPair BlockSoundPair;

private int initValueDelayTicks = 60; // 1 second delay (60 ticks)
private bool valuesInitialized = false;

#region Sync Properties
public MySync<bool, SyncDirection.BothWays> SiegeMode;
public MySync<bool, SyncDirection.BothWays> SiegeCooldownActive;
public MySync<bool, SyncDirection.FromServer> GridStopped = null;
public MySync<bool, SyncDirection.BothWays> SlowdownActive;
public MySync<bool, SyncDirection.FromServer> GridStopped;

public MySync<int, SyncDirection.BothWays> SiegeElapsedTime;
public MySync<int, SyncDirection.BothWays> SiegeCooldownTime;
Expand Down Expand Up @@ -85,6 +94,7 @@ public override void UpdateOnceBeforeFrame()
if (Block?.CubeGrid?.Physics == null)
return;

BlockSoundEmitter = new MyEntity3DSoundEmitter((MyEntity)MyAPIGateway.Entities.GetEntityById(Block.EntityId));
FieldGeneratorControls.DoOnce(ModContext);

Sink = Block.Components.Get<MyResourceSinkComponent>();
Expand Down Expand Up @@ -130,7 +140,7 @@ public override void UpdateOnceBeforeFrame()

NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME;
NeedsUpdate |= MyEntityUpdateEnum.EACH_10TH_FRAME;
}
}

public override void UpdateAfterSimulation()
{
Expand All @@ -146,11 +156,37 @@ public override void UpdateAfterSimulation()
}
else
{
_cachedState = Block.IsWorking;
Stability.Value = 100;
InitExistingUpgrades();
valuesInitialized = true;
}
}

if (SiegeMode.Value && !GridStopped.Value)
{
if (Block.CubeGrid.Physics.LinearVelocity != Vector3D.Zero)
{
SiegeEmergencyStop();
_stopTickCounter++;
}
}
}

if (SiegeMode.Value && !GridStopped.Value)
{
if (!SlowdownActive.Value)
{
BlockSoundPair = new MySoundPair("FieldGen_Brake");
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)
Expand All @@ -159,7 +195,10 @@ public override void UpdateAfterSimulation()
{
if (IsServer)
{
UpdateSiegeState();
if (GridStopped.Value || SiegeCooldownActive.Value)
{
UpdateSiegeState();
}

if (!Config.SimplifiedMode)
{
Expand Down Expand Up @@ -350,7 +389,7 @@ private void HandleResistence()
return;
}

if (SiegeMode.Value)
if (SiegeMode.Value && GridStopped.Value)
{
MyVisualScriptLogicProvider.SetGridGeneralDamageModifier(Block.CubeGrid.Name, (1 - Config.SiegeModeResistence));
return;
Expand Down Expand Up @@ -400,6 +439,20 @@ private void OnGridStopValueChange(MySync<bool, SyncDirection.FromServer> obj)
if (obj?.Value ?? false)
Block.CubeGrid.Physics.LinearVelocity = Vector3.Zero;
}

private void Block_IsWorkingChanged()
{
if (!Block.IsWorking)
{
BlockSoundPair = Block.IsFunctional ? BlockSoundPair = new MySoundPair("FieldGen_Offline") : BlockSoundPair = new MySoundPair("FieldGen_Damaged");
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);
}
}
#endregion

#region Siege Mode
Expand Down Expand Up @@ -458,12 +511,82 @@ private void SiegeBlockEnabler(IEnumerable<IMyFunctionalBlock> allFunctionalBloc
FieldGeneratorSession.CoreSysAPI.SetFiringAllowed(entBlock, enabled);
block.Enabled = enabled;
}
else if (block is IMyThrust || block is IMyGyro)
{
block.Enabled = enabled;
}
}
else
continue;
}
}

private void SiegeEmergencyStop()
{
if (Block.CubeGrid.Physics == null)
return;

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);

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 -----
if (angularAxis1 == null)
{
angularAxis1 = GetRandomUnitVector();

Vector3 candidate;
do
{
candidate = GetRandomUnitVector();
}
while (Math.Abs(Vector3D.Dot(candidate, angularAxis1.Value)) > 0.99f);

angularAxis2 = candidate;
}

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

torqueDirection += angularAxis1.Value;
torqueDirection += angularAxis2.Value;

Vector3 angularImpulse = torqueDirection * targetAngularAccel / 60f;

Block.CubeGrid.Physics.AngularVelocity += angularImpulse;
}

Vector3 GetRandomUnitVector()
{
Vector3 v = new Vector3(
MyUtils.GetRandomFloat(-1f, 1f),
MyUtils.GetRandomFloat(-1f, 1f),
MyUtils.GetRandomFloat(-1f, 1f)
);
return Vector3.Normalize(v);
}

private void EndSiegeMode()
{
if (IsServer && GridStopped.Value)
Expand Down Expand Up @@ -606,6 +729,20 @@ public float CalculatePowerDraw()
return Config.MinPowerDraw + t * (Config.MaxPowerDraw - Config.MinPowerDraw);
}

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

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

public float CheckPowerGeneration()
{
return Block.CubeGrid.ResourceDistributor.MaxAvailableResourceByType(MyResourceDistributorComponent.ElectricityId);
}

private bool IsClientInShip()
{
if (Block != null)
Expand Down Expand Up @@ -751,7 +888,11 @@ private void UpdateHUD()
GeneratorHUDContent.Append(GenerateBar("Stability:", Stability.Value, 100, true));
}

if (SiegeMode.Value)
if (SiegeMode.Value && !GridStopped.Value)
{
GeneratorHUDContent.Append($"\nSiege Mode Actived | Emergency Braking Applied");
}
else if (SiegeMode.Value)
{
GeneratorHUDContent.Append($"\nSiege Mode Active | {SiegeElapsedTime.Value} / {Config.MaxSiegeTime}");
}
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;
return !logic.SiegeCooldownActive && !logic.SlowdownActive;
}
return false;
}
Expand Down Expand Up @@ -149,6 +149,9 @@ static void CreateActions(IMyModContext context)
var logic = GetLogic(b);
if (logic != null)
{
if (logic.CheckPowerDraw(logic.FieldPower.Value + 2.5f) > logic.CheckPowerGeneration())
return;

logic.FieldPower.Value += 2.5f;
}
};
Expand Down