2020using VRageMath ;
2121using static Draygo . API . HudAPIv2 ;
2222using static VRageRender . MyBillboard ;
23+ using static VRage . Game . MyObjectBuilder_BehaviorTreeDecoratorNode ;
2324
2425
2526namespace Starcore . FieldGenerator
@@ -53,8 +54,7 @@ public class FieldGenerator : MyGameLogicComponent, IMyEventProxy
5354 #region Sync Properties
5455 public MySync < bool , SyncDirection . BothWays > SiegeMode ;
5556 public MySync < bool , SyncDirection . BothWays > SiegeCooldownActive ;
56- public MySync < bool , SyncDirection . BothWays > SlowdownActive ;
57- public MySync < bool , SyncDirection . FromServer > GridStopped ;
57+ public MySync < bool , SyncDirection . FromServer > _slowdownComplete ;
5858
5959 public MySync < int , SyncDirection . BothWays > SiegeElapsedTime ;
6060 public MySync < int , SyncDirection . BothWays > SiegeCooldownTime ;
@@ -136,7 +136,6 @@ public override void UpdateOnceBeforeFrame()
136136
137137 if ( ! IsDedicated )
138138 {
139- GridStopped . ValueChanged += OnGridStopValueChange ;
140139 Block . IsWorkingChanged += Block_IsWorkingChanged ;
141140 }
142141
@@ -165,40 +164,26 @@ public override void UpdateAfterSimulation()
165164 }
166165 }
167166
168- if ( SiegeMode . Value && ! GridStopped . Value )
167+ if ( Block . IsWorking )
169168 {
170- if ( Block . CubeGrid . Physics . LinearVelocity != Vector3D . Zero )
169+ UpdateSiegeState ( ) ;
170+ }
171+ else if ( ! Block . IsWorking )
172+ {
173+ if ( SiegeMode . Value )
171174 {
172- SiegeEmergencyStop ( ) ;
173- _stopTickCounter ++ ;
175+ EndSiegeState ( true ) ;
176+ SiegeMode . Value = false ;
174177 }
175- else
176- GridStopped . Value = true ;
177178 }
178- }
179-
180- if ( ! IsDedicated )
181- {
182- if ( SiegeMode . Value && ! GridStopped . Value && ! SlowdownActive . Value && IsClientInShip ( ) )
183- {
184- BlockSoundPair = new MySoundPair ( "FieldGen_Brake" ) ;
185- BlockSoundEmitter . SetPosition ( Block . Position ) ;
186- BlockSoundEmitter ? . PlaySound ( BlockSoundPair , false , false , true , true , false , null , true ) ;
187- SlowdownActive . Value = true ;
188- }
189- }
179+ }
190180
191181 if ( MyAPIGateway . Session . GameplayFrameCounter % 60 == 0 )
192182 {
193183 if ( Block . IsWorking )
194184 {
195185 if ( IsServer )
196- {
197- if ( GridStopped . Value || SiegeCooldownActive . Value )
198- {
199- UpdateSiegeState ( ) ;
200- }
201-
186+ {
202187 if ( ! Config . SimplifiedMode )
203188 {
204189 if ( _lowStability && Stability . Value < 100 )
@@ -247,13 +232,7 @@ public override void UpdateAfterSimulation()
247232 {
248233 if ( FieldPower . Value > 0 )
249234 FieldPower . Value = 0 ;
250-
251- if ( SiegeMode . Value )
252- {
253- CancelSiegeMode ( ) ;
254- SiegeMode . Value = false ;
255- }
256-
235+
257236 HandleResistence ( ) ;
258237 }
259238 else
@@ -299,7 +278,6 @@ public override void Close()
299278
300279 if ( ! IsDedicated )
301280 {
302- GridStopped . ValueChanged -= OnGridStopValueChange ;
303281 Block . IsWorkingChanged -= Block_IsWorkingChanged ;
304282 }
305283
@@ -389,7 +367,7 @@ private void HandleResistence()
389367 return ;
390368 }
391369
392- if ( SiegeMode . Value && GridStopped . Value )
370+ if ( SiegeMode . Value )
393371 {
394372 MyVisualScriptLogicProvider . SetGridGeneralDamageModifier ( Block . CubeGrid . Name , ( 1 - Config . SiegeModeResistence ) ) ;
395373 return ;
@@ -414,6 +392,13 @@ private void SiegeMode_ValueChanged(MySync<bool, SyncDirection.BothWays> obj)
414392
415393 if ( IsServer )
416394 HandleResistence ( ) ;
395+
396+ if ( ! IsDedicated && obj . Value && IsClientInShip ( ) )
397+ {
398+ BlockSoundPair = new MySoundPair ( "FieldGen_Brake" ) ;
399+ BlockSoundEmitter . SetPosition ( Block . Position ) ;
400+ BlockSoundEmitter ? . PlaySound ( BlockSoundPair , false , false , true , true , false , null , true ) ;
401+ }
417402 }
418403
419404 private void FieldPower_ValueChanged ( MySync < float , SyncDirection . BothWays > obj )
@@ -433,13 +418,7 @@ private void Stability_ValueChanged(MySync<float, SyncDirection.BothWays> obj)
433418 if ( IsServer )
434419 HandleZeroStability ( ) ;
435420 }
436-
437- private void OnGridStopValueChange ( MySync < bool , SyncDirection . FromServer > obj )
438- {
439- if ( obj ? . Value ?? false )
440- Block . CubeGrid . Physics . LinearVelocity = Vector3 . Zero ;
441- }
442-
421+
443422 private void Block_IsWorkingChanged ( IMyCubeBlock block )
444423 {
445424 if ( IsClientInShip ( ) )
@@ -467,43 +446,56 @@ private void UpdateSiegeState()
467446 {
468447 if ( SiegeMode . Value && ! SiegeCooldownActive . Value )
469448 {
470- if ( SiegeElapsedTime . Value + 1 <= Config . MaxSiegeTime )
471- {
472- SiegeElapsedTime . Value ++ ; ;
473- SiegeBlockEnabler ( Block . CubeGrid . GetFatBlocks < IMyFunctionalBlock > ( ) , false ) ;
474-
449+ if ( ! _slowdownComplete . Value )
450+ {
451+ SiegeSlowdown ( ) ;
452+ _stopTickCounter ++ ;
453+ return ;
454+ }
455+ else if ( _slowdownComplete . Value )
456+ {
475457 if ( Block . CubeGrid . Physics . LinearVelocity != Vector3D . Zero )
476458 {
477459 Block . CubeGrid . Physics . LinearVelocity = Vector3 . Zero ;
478- if ( IsServer && ! GridStopped . Value )
479- GridStopped . Value = true ;
480460 }
481- }
482- else
483- {
484- EndSiegeMode ( ) ;
485- SiegeMode . Value = false ;
486- return ;
487- }
461+ }
488462 }
489463
490- if ( ! SiegeMode . Value && ! SiegeCooldownActive . Value && SiegeElapsedTime . Value > 0 )
464+ if ( MyAPIGateway . Session . GameplayFrameCounter % 60 == 0 )
491465 {
492- EndSiegeMode ( ) ;
493- return ;
494- }
466+ if ( SiegeMode . Value && ! SiegeCooldownActive . Value )
467+ {
468+ if ( SiegeElapsedTime . Value + 1 <= Config . MaxSiegeTime )
469+ {
470+ SiegeElapsedTime . Value ++ ; ;
471+ SiegeBlockEnabler ( Block . CubeGrid . GetFatBlocks < IMyFunctionalBlock > ( ) , false ) ;
472+ }
473+ else
474+ {
475+ EndSiegeState ( ) ;
476+ SiegeMode . Value = false ;
477+ return ;
478+ }
479+ }
495480
496- if ( SiegeCooldownActive . Value )
497- {
498- if ( SiegeCooldownTime . Value > 0 )
481+ if ( ! SiegeMode . Value && ! SiegeCooldownActive . Value && SiegeElapsedTime . Value > 0 )
499482 {
500- SiegeCooldownTime . Value -- ;
483+ EndSiegeState ( ) ;
484+ return ;
501485 }
502- else
486+
487+ if ( SiegeCooldownActive . Value )
503488 {
504- SiegeCooldownActive . Value = false ;
489+ if ( SiegeCooldownTime . Value > 0 )
490+ {
491+ SiegeCooldownTime . Value -- ;
492+ }
493+ else
494+ {
495+ SiegeCooldownActive . Value = false ;
496+ }
505497 }
506- }
498+ }
507499 }
508500
509501 private void SiegeBlockEnabler ( IEnumerable < IMyFunctionalBlock > allFunctionalBlocks , bool enabled )
@@ -528,7 +520,7 @@ private void SiegeBlockEnabler(IEnumerable<IMyFunctionalBlock> allFunctionalBloc
528520 }
529521 }
530522
531- private void SiegeEmergencyStop ( )
523+ private void SiegeSlowdown ( )
532524 {
533525 if ( Block . CubeGrid . Physics == null )
534526 return ;
@@ -568,11 +560,10 @@ private void SiegeEmergencyStop()
568560 if ( Block . CubeGrid . Physics . LinearVelocity . LengthSquared ( ) < 25f || elapsed >= 10f ) // 5 m/s squared = 25
569561 {
570562 Block . CubeGrid . Physics . LinearVelocity = Vector3D . Zero ;
571- GridStopped . Value = true ;
572- SlowdownActive . Value = false ;
563+ _slowdownComplete . Value = true ;
573564 _stopTickCounter = - 1 ;
574565 angularAxis1 = null ;
575- angularAxis2 = null ;
566+ angularAxis2 = null ;
576567 return ;
577568 }
578569 }
@@ -587,27 +578,13 @@ Vector3 GetRandomUnitVector()
587578 return Vector3 . Normalize ( v ) ;
588579 }
589580
590- private void EndSiegeMode ( )
581+ private void EndSiegeState ( bool cancel = false )
591582 {
592- if ( IsServer && GridStopped . Value )
593- GridStopped . Value = false ;
594-
595- SiegeBlockEnabler ( Block . CubeGrid . GetFatBlocks < IMyFunctionalBlock > ( ) , true ) ;
596-
597- SiegeCooldownTime . Value = Math . Max ( SiegeElapsedTime . Value * 2 , Config . MinSiegeTime ) ;
598- SiegeElapsedTime . Value = 0 ;
599- SiegeCooldownActive . Value = true ;
600- }
601-
602- private void CancelSiegeMode ( )
603- {
604- if ( IsServer && GridStopped . Value )
605- GridStopped . Value = false ;
606-
607583 SiegeBlockEnabler ( Block . CubeGrid . GetFatBlocks < IMyFunctionalBlock > ( ) , true ) ;
608584
609- SiegeCooldownTime . Value = 0 ;
585+ SiegeCooldownTime . Value = cancel ? 0 : Math . Max ( SiegeElapsedTime . Value * 2 , Config . MinSiegeTime ) ;
610586 SiegeElapsedTime . Value = 0 ;
587+ SiegeCooldownActive . Value = cancel ? false : true ;
611588 }
612589 #endregion
613590
@@ -888,7 +865,7 @@ private void UpdateHUD()
888865 GeneratorHUDContent . Append ( GenerateBar ( "Stability:" , Stability . Value , 100 , true ) ) ;
889866 }
890867
891- if ( SiegeMode . Value && ! GridStopped . Value )
868+ if ( SiegeMode . Value && ! _slowdownComplete . Value )
892869 {
893870 GeneratorHUDContent . Append ( $ "\n Siege Mode Actived | Emergency Braking Applied") ;
894871 }
0 commit comments