1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using HarmonyLib ;
4
+ using UnityEngine ;
5
+
6
+ namespace KSPCommunityFixes
7
+ {
8
+ public class EditorPhysics : BasePatch
9
+ {
10
+ protected override bool IgnoreConfig => true ;
11
+
12
+ protected override Version VersionMin => new Version ( 1 , 12 , 3 ) ;
13
+
14
+ protected override void ApplyPatches ( )
15
+ {
16
+ instance = this ;
17
+
18
+ AddPatch ( PatchType . Postfix , typeof ( DeltaVAppSituation ) , nameof ( DeltaVAppSituation . UpdatePressureDisplay ) ) ;
19
+
20
+ GameEvents . onEditorShipModified . Add ( OnEditorShipModified ) ;
21
+ GameEvents . onDeltaVAppAtmosphereChanged . Add ( OnDeltaVAppAtmosphereChanged ) ;
22
+ GameEvents . OnControlPointChanged . Add ( OnControlPointChanged ) ;
23
+ GameEvents . onGameSceneLoadRequested . Add ( OnGameSceneLoadRequested ) ;
24
+ }
25
+
26
+ private static EditorPhysics instance ;
27
+ public static bool TryGetAndUpdate ( out EditorPhysics updatedInstance )
28
+ {
29
+ instance . Update ( ) ;
30
+ if ( instance . isValid )
31
+ {
32
+ updatedInstance = instance ;
33
+ return true ;
34
+ }
35
+
36
+ updatedInstance = null ;
37
+ return false ;
38
+ }
39
+
40
+ private bool isValid ;
41
+
42
+ public Vector3 CoM => EditorMarker_CoM . CraftCoM ;
43
+ public Transform referenceTransform ;
44
+ public Part referencePart ;
45
+ public int referencePartShipIndex ;
46
+
47
+ public CelestialBody body ;
48
+ public double atmStaticPressureKpa ;
49
+ public double atmStaticPressure ;
50
+ public double atmDensity ;
51
+ public double atmTemperature ;
52
+
53
+ public int lastShipModificationFrame = int . MaxValue ;
54
+ private int lastShipStatsUpdateFrame ;
55
+
56
+ private void Update ( )
57
+ {
58
+ if ( lastShipStatsUpdateFrame < lastShipModificationFrame )
59
+ {
60
+ lastShipStatsUpdateFrame = lastShipModificationFrame ;
61
+ EditorUpdateShipStats ( ) ;
62
+ }
63
+ }
64
+
65
+ private void EditorUpdateShipStats ( )
66
+ {
67
+ if ( EditorLogic . fetch . ship == null || EditorLogic . fetch . ship . parts . Count == 0 )
68
+ {
69
+ isValid = false ;
70
+ return ;
71
+ }
72
+
73
+ if ( DeltaVGlobals . fetch != null && DeltaVGlobals . ready && DeltaVGlobals . DeltaVAppValues . body != null )
74
+ {
75
+ body = DeltaVGlobals . DeltaVAppValues . body ;
76
+
77
+ if ( ! DeltaVGlobals . DeltaVAppValues . body . atmosphere || DeltaVGlobals . DeltaVAppValues . situation == DeltaVSituationOptions . Vaccum )
78
+ {
79
+
80
+ atmStaticPressureKpa = 0.0 ;
81
+ atmStaticPressure = 0.0 ;
82
+ atmTemperature = 0.0 ;
83
+ atmDensity = 0.0 ;
84
+ }
85
+ else
86
+ {
87
+ if ( DeltaVGlobals . DeltaVAppValues . situation == DeltaVSituationOptions . Altitude )
88
+ {
89
+ atmTemperature = body . GetFullTemperature ( DeltaVGlobals . DeltaVAppValues . altitude , 0.0 ) ;
90
+ atmStaticPressureKpa = body . GetPressure ( DeltaVGlobals . DeltaVAppValues . altitude ) ;
91
+ }
92
+ else
93
+ {
94
+ atmTemperature = body . GetFullTemperature ( 0.0 , 0.0 ) ;
95
+ atmStaticPressureKpa = body . GetPressure ( 0.0 ) ;
96
+ }
97
+
98
+ atmStaticPressure = atmStaticPressureKpa * 0.0098692326671601278 ;
99
+ atmDensity = DeltaVGlobals . DeltaVAppValues . atmDensity ;
100
+ }
101
+ }
102
+ else
103
+ {
104
+ atmStaticPressureKpa = 0.0 ;
105
+ atmStaticPressure = 0.0 ;
106
+ atmTemperature = 0.0 ;
107
+ atmDensity = 0.0 ;
108
+ }
109
+
110
+ if ( referenceTransform . IsNullOrDestroyed ( ) || EditorLogic . fetch . ship . Parts . IndexOf ( referencePart ) != referencePartShipIndex )
111
+ {
112
+ if ( ! GetFirstReferenceTransform ( EditorLogic . RootPart , ref referenceTransform , ref referencePart ) )
113
+ {
114
+ referencePart = EditorLogic . RootPart ;
115
+ referenceTransform = EditorLogic . RootPart . referenceTransform ;
116
+ }
117
+
118
+ referencePartShipIndex = EditorLogic . fetch . ship . Parts . IndexOf ( referencePart ) ;
119
+ }
120
+
121
+ if ( referenceTransform . IsNullOrDestroyed ( ) )
122
+ {
123
+ isValid = false ;
124
+ return ;
125
+ }
126
+
127
+ bool GetFirstReferenceTransform ( Part part , ref Transform referenceTransform , ref Part referencePart )
128
+ {
129
+ if ( part . isControlSource != Vessel . ControlLevel . NONE )
130
+ {
131
+ ModuleCommand mc = part . FindModuleImplementing < ModuleCommand > ( ) ;
132
+ if ( mc != null && mc . controlPoints != null && mc . controlPoints . TryGetValue ( mc . activeControlPointName , out ControlPoint ctrlPoint ) )
133
+ referenceTransform = ctrlPoint . transform ;
134
+ else
135
+ referenceTransform = part . referenceTransform ;
136
+
137
+ referencePart = part ;
138
+ return true ;
139
+ }
140
+
141
+ int childIdx = part . children . Count ;
142
+ while ( childIdx -- > 0 )
143
+ {
144
+ if ( GetFirstReferenceTransform ( part . children [ childIdx ] , ref referenceTransform , ref referencePart ) )
145
+ return true ;
146
+ }
147
+
148
+ return false ;
149
+ }
150
+
151
+ EditorMarker_CoM comMarker = EditorVesselOverlays . fetch . CoMmarker ;
152
+ if ( comMarker == null )
153
+ {
154
+ isValid = false ;
155
+ return ;
156
+ }
157
+
158
+ if ( ! comMarker . isActiveAndEnabled )
159
+ {
160
+ comMarker . rootPart = EditorLogic . RootPart ;
161
+ comMarker . UpdatePosition ( ) ;
162
+ }
163
+
164
+ isValid = true ;
165
+ }
166
+
167
+ private void OnGameSceneLoadRequested ( GameScenes data )
168
+ {
169
+ referenceTransform = null ;
170
+ referencePart = null ;
171
+ referencePartShipIndex = - 1 ;
172
+ atmStaticPressure = 0f ;
173
+ lastShipModificationFrame = 0 ;
174
+ lastShipStatsUpdateFrame = 0 ;
175
+ }
176
+
177
+ private void OnControlPointChanged ( Part part , ControlPoint controlPoint )
178
+ {
179
+ if ( HighLogic . LoadedScene != GameScenes . EDITOR || EditorLogic . fetch . ship == null )
180
+ return ;
181
+
182
+ int partIndex = EditorLogic . fetch . ship . Parts . IndexOf ( part ) ;
183
+ if ( partIndex < 0 )
184
+ return ;
185
+
186
+ lastShipModificationFrame = Time . frameCount ;
187
+ referenceTransform = controlPoint . transform ;
188
+ referencePart = part ;
189
+ referencePartShipIndex = partIndex ;
190
+ }
191
+
192
+ private void OnDeltaVAppAtmosphereChanged ( DeltaVSituationOptions data )
193
+ {
194
+ lastShipModificationFrame = Time . frameCount ;
195
+ }
196
+
197
+ private void OnEditorShipModified ( ShipConstruct ship )
198
+ {
199
+ if ( ship != EditorLogic . fetch . ship || ship . parts . Count == 0 )
200
+ return ;
201
+
202
+ lastShipModificationFrame = Time . frameCount ;
203
+ }
204
+
205
+ // OnDeltaVAppAtmosphereChanged isn't fired when the altitude is modified, so implement our own event
206
+ static void DeltaVAppSituation_UpdatePressureDisplay_Postfix ( )
207
+ {
208
+ instance . lastShipModificationFrame = Time . frameCount ;
209
+ }
210
+ }
211
+ }
0 commit comments