Merge pull request #100 from Gerry1135/trueanomaly
Merge pull request #100 from Gerry1135/trueanomaly

Fix for issue #99

 Binary files a/Assets/Plugins/KerbalEngineer.Unity.dll and b/Assets/Plugins/KerbalEngineer.Unity.dll differ
  1.1.1.0, 2016-04-29, KSP Build #1250
  Added: Missing cleanup of some contained references within various pooled classes.
  Fixed: Jet engine simulation data is no longer included when IntakeAir deprived (i.e. in space).
  Fixed: NullReferenceException when entering the flight scene within the FlightAppLauncher.
  Fixed: Corrected an exception logging typo within the DisplayStack class.
  Fixed: NullReferenceException within the DeltaVStaged readout upon exiting the flight scene.
  Fixed: Flamed out engines are no longer perceived as active engines within the vessel simulation.
  Removed: Redundant button enabled check within the FlightAppLauncher.
  Removed: '?.' syntax sugar for easier compilation within non VS2015 IDEs.
   
1.1.0.2, 2016-04-19, KSP Build #1230 1.1.0.2, 2016-04-19, KSP Build #1230
Added: Altitude (terrain) readout now shows the altitude above underwater terrain when splashed down. Added: Altitude (terrain) readout now shows the altitude above underwater terrain when splashed down.
Added: Usage of the stock IPartMassModifier for handling the modifying of part module staged mass. Added: Usage of the stock IPartMassModifier for handling the modifying of part module staged mass.
Added: Support for thrustTranformMultipliers Added: Support for thrustTranformMultipliers
Changed: Fairing specific mass adjustments now make use of the generic staged mass modifier implementation. Changed: Fairing specific mass adjustments now make use of the generic staged mass modifier implementation.
Changed: App launcher button handling works with KSP build 1205 changes. Changed: App launcher button handling works with KSP build 1205 changes.
Changed: Impact lat/long readouts to use DMS form. Changed: Impact lat/long readouts to use DMS form.
Fixed: Log spam and physics issue caused when using the EngineerChip part due to a convex mesh collider (now uses a box collider). Fixed: Log spam and physics issue caused when using the EngineerChip part due to a convex mesh collider (now uses a box collider).
Fixed: Issues with the disabling/enabling of the toolbar app launcher button in flight when in career mode. Fixed: Issues with the disabling/enabling of the toolbar app launcher button in flight when in career mode.
Fixed: Bug with impact calculations. Fixed: Bug with impact calculations.
   
1.1.0.1, 2016-04-01, KSP Build #1179 1.1.0.1, 2016-04-01, KSP Build #1179
Fixed: Heat shields acting as staged decouplers when not set to be staged. Fixed: Heat shields acting as staged decouplers when not set to be staged.
Fixed: Enabling and disabling of decouplers in staging is now correctly handled. Fixed: Enabling and disabling of decouplers in staging is now correctly handled.
Fixed: Issue in simulation staging that caused no deltaV above a fairing whilst landed. Fixed: Issue in simulation staging that caused no deltaV above a fairing whilst landed.
Fixed: Fairing mass calculations to work using module mass only (Changed from 1.0.5). Fixed: Fairing mass calculations to work using module mass only (Changed from 1.0.5).
   
1.1.0.0, 2016-03-30, KSP Build #1171 1.1.0.0, 2016-03-30, KSP Build #1171
Added: Asset bundle loading and integration. Added: Asset bundle loading and integration.
Added: Styling system so that Unity UI objects can use KSP stock styles. Added: Styling system so that Unity UI objects can use KSP stock styles.
Added: Unity UI window system. Added: Unity UI window system.
Changed: Thermal flux is now correctly indicated in kilowatts. Changed: Thermal flux is now correctly indicated in kilowatts.
Changed: Complete rewrite of code underpinning the stock toolbar icons. Changed: Complete rewrite of code underpinning the stock toolbar icons.
Changed: Flight Engineer toolbar menu recreated using Unity UI. Changed: Flight Engineer toolbar menu recreated using Unity UI.
Changed: Replaced occurances of IMGUI using RenderingManager to use OnGUI directly. Changed: Replaced occurances of IMGUI using RenderingManager to use OnGUI directly.
Fixed: Toggling the 'Control Bar' now appropriately resizes the display stack. Fixed: Toggling the 'Control Bar' now appropriately resizes the display stack.
Fixed: Part info tooltips in the editor remaining open when no part is selected. Fixed: Part info tooltips in the editor remaining open when no part is selected.
   
1.0.19.4, 2016-02-12 1.0.19.4, 2016-02-12
Fixed: Only 'STAGE_STACK_FLOW' and 'STAGE_STACK_FLOW_BALANCE' resources include surface mounted parts as fuel targets. Fixed: Only 'STAGE_STACK_FLOW' and 'STAGE_STACK_FLOW_BALANCE' resources include surface mounted parts as fuel targets.
Fixed: Fairing mass being doubled in the VAB (removed workaround for a KSP bug which has been fixed). Fixed: Fairing mass being doubled in the VAB (removed workaround for a KSP bug which has been fixed).
   
1.0.19.3, 2016-02-09 1.0.19.3, 2016-02-09
Fixed: Fuel cross-feed from surface attached parts. Fixed: Fuel cross-feed from surface attached parts.
   
1.0.19.2, 2016-11-19 1.0.19.2, 2016-11-19
Rebuild for KSP 1.0.5.1028 silent update. Rebuild for KSP 1.0.5.1028 silent update.
1.0.19.1, 2016-11-09 1.0.19.1, 2016-11-09
Added: Key binding editor accessible under 'Settings' on the Build Engineer. Added: Key binding editor accessible under 'Settings' on the Build Engineer.
Added: Added current vessel name readout. (antplant) Added: Added current vessel name readout. (antplant)
Added: 'Relative Radial Velocity' and 'Time To Rendezvous' readouts. (itwtx) Added: 'Relative Radial Velocity' and 'Time To Rendezvous' readouts. (itwtx)
Added: Readout help strings. (harryyoung) Added: Readout help strings. (harryyoung)
Changed: The 'Torque' value in the editor is now precise to two decimal places. Changed: The 'Torque' value in the editor is now precise to two decimal places.
Changed: Time formatting reference (Kerbin/Earth) is now based on the in-game setting. Changed: Time formatting reference (Kerbin/Earth) is now based on the in-game setting.
Changed: Eccentric Anomaly, Mean Anomaly and Mean Anomaly At Epoc now display in degrees rather than radians. Changed: Eccentric Anomaly, Mean Anomaly and Mean Anomaly At Epoc now display in degrees rather than radians.
Fixed: Optimised time formatting. (itwtx) Fixed: Optimised time formatting. (itwtx)
Fixed: TimeToAtmosphere checks that the Apoapsis is outside atmosphere. (Kerbas-ad-astra) Fixed: TimeToAtmosphere checks that the Apoapsis is outside atmosphere. (Kerbas-ad-astra)
Fixed: Issue with stage priority flow. Caused Rapier calculations to fail if LF and O are drawn from different tanks. (Padishar) Fixed: Issue with stage priority flow. Caused Rapier calculations to fail if LF and O are drawn from different tanks. (Padishar)
Fixed: Issue with angle to prograde/retrograde calculations on highly inclined orbits. Fixed: Issue with angle to prograde/retrograde calculations on highly inclined orbits.
Fixed: Editor input locks not being reset when a scene change is forced (e.g. via Kerbal Construction Time). Fixed: Editor input locks not being reset when a scene change is forced (e.g. via Kerbal Construction Time).
Fixed: Roll axis readout now shows the correct sign. Fixed: Roll axis readout now shows the correct sign.
Removed: Time Formatter readout as it's not required anymore. Removed: Time Formatter readout as it's not required anymore.
   
1.0.18.0 1.0.18.0
Added: Orbital readouts - "Speed at Periapsis" and "Speed at Apoapsis". (Padishar) Added: Orbital readouts - "Speed at Periapsis" and "Speed at Apoapsis". (Padishar)
Added: Manoeuvre readouts - "Post-burn Apoapsis" and "Post-burn Periapsis". (Padishar) Added: Manoeuvre readouts - "Post-burn Apoapsis" and "Post-burn Periapsis". (Padishar)
Added: Orbital readout - "Time to Atmosphere". Added: Orbital readout - "Time to Atmosphere".
Fixed: Synched the minimum simulation time sliders and stopped them from snapping back after 999ms. (saybur) Fixed: Synched the minimum simulation time sliders and stopped them from snapping back after 999ms. (saybur)
Fixed: Added workaround for the bug in Vessel.horizontalSrfSpeed (Padishar) Fixed: Added workaround for the bug in Vessel.horizontalSrfSpeed (Padishar)
Fixed: Physically insignificant part mass was not being correctly cascaded down through multiple parent parts. Fixed: Physically insignificant part mass was not being correctly cascaded down through multiple parent parts.
Fixed: Intake air demand calculation not working. Fixed: Intake air demand calculation not working.
Fixed: Some build engineer settings labels do not scale with UI size. Fixed: Some build engineer settings labels do not scale with UI size.
   
1.0.17.0 1.0.17.0
Added: 'Mach Number' readout under the 'Surface' category and included it on the default surface HUD. Added: 'Mach Number' readout under the 'Surface' category and included it on the default surface HUD.
Added: Stock sections in the Flight Engineer can now become HUDs. Added: Stock sections in the Flight Engineer can now become HUDs.
Added 'Thermal' readouts category including: Added 'Thermal' readouts category including:
Internal Flux Internal Flux
Convection Flux Convection Flux
Radiation Flux Radiation Flux
Critical Part Name Critical Part Name
Critical Part Temperature Critical Part Temperature
Critical Part Skin Temperature Critical Part Skin Temperature
Critical Part Thermal Percentage of Max Temperature Critical Part Thermal Percentage of Max Temperature
Hottest Part Name Hottest Part Name
Hottest Part Temperature Hottest Part Temperature
Hottest Part Skin Temperature Hottest Part Skin Temperature
Coldest Part Name Coldest Part Name
Coldest Part Temperature Coldest Part Temperature
Coldest Part Skin Temperature Coldest Part Skin Temperature
   
Changed: Mach on the Build Engineer now accurate to 2 decimal places. Changed: Mach on the Build Engineer now accurate to 2 decimal places.
Changed: Max mach in the Build Engineer defaults to 1.00 even when no jet engines are present. Changed: Max mach in the Build Engineer defaults to 1.00 even when no jet engines are present.
Changed: Increased eccentricity readout to 5 decimal places. Changed: Increased eccentricity readout to 5 decimal places.
Changed: Implemented Sarbian's object pooling. Changed: Implemented Sarbian's object pooling.
Changed: The default selected body is now assigned via 'Planitarium.Home'. Changed: The default selected body is now assigned via 'Planitarium.Home'.
Changed: HUDs to clamp fully inside the screen instead of allowing them to run off the edge by a certain amount. Changed: HUDs to clamp fully inside the screen instead of allowing them to run off the edge by a certain amount.
Fixed: Physically insignificant part mass is now associated with the parent part. Fixed: Physically insignificant part mass is now associated with the parent part.
Fixed: Longitude and Latitude readouts now use a KER formatter instead of Squad's incorrect implementation. Fixed: Longitude and Latitude readouts now use a KER formatter instead of Squad's incorrect implementation.
Fixed: Possible null reference in the Rendezvous Processor. Fixed: Possible null reference in the Rendezvous Processor.
Fixed: Fairing mass issues introduced with regards to simulation changes. Fixed: Fairing mass issues introduced with regards to simulation changes.
Fixed: Use of per-propellant fuel flow mode override. Fixed: Use of per-propellant fuel flow mode override.
Fixed: Burn times calculated for jet engines. Fixed: Burn times calculated for jet engines.
Fixed: Thrust issues introduced with Sarbian's simulation alterations. Fixed: Thrust issues introduced with Sarbian's simulation alterations.
Fixed: Issue where HUDs positioned close to the top/bottom of the screen could be pushed out of position. Fixed: Issue where HUDs positioned close to the top/bottom of the screen could be pushed out of position.
   
1.0.16.6, 2015-05-02 1.0.16.6, 2015-05-02
Fixed: Separately staged fairing mass jettisons are now calculated in the editor. Fixed: Separately staged fairing mass jettisons are now calculated in the editor.
   
1.0.16.5, 2015-05-02 1.0.16.5, 2015-05-02
Fixed: Delta-V not being correctly calculated. Fixed: Delta-V not being correctly calculated.
Changed: Editor locking now uses the InputLockManager. Changed: Editor locking now uses the InputLockManager.
   
1.0.16.4, 01-05-2015 1.0.16.4, 01-05-2015
Fixed: Physically insignificant part mass is now accounted for. Fixed: Physically insignificant part mass is now accounted for.
Changed: Module mass accounted for as it now makes its way onto the launch pad (e.g. fairings). Changed: Module mass accounted for as it now makes its way onto the launch pad (e.g. fairings).
   
Various optimisations: Various optimisations:
Object pooling. Object pooling.
Removed LINQ expressions. Removed LINQ expressions.
Converted foreach to for loops. Converted foreach to for loops.
   
1.0.16.3, 2015-04-27 1.0.16.3, 2015-04-27
Fixed issue with the toolbar icons not being created. Fixed issue with the toolbar icons not being created.
Removed superfluous 'm/s' on the mach slider in the build engineer. Removed superfluous 'm/s' on the mach slider in the build engineer.
   
1.0.16.2, 2015-04-27 1.0.16.2, 2015-04-27
Changed the atmospheric slider on the build engineer to default to 0km when changing bodies. Changed the atmospheric slider on the build engineer to default to 0km when changing bodies.
   
1.0.16.1, 2015-04-26, KSP Build #828 1.0.16.1, 2015-04-26, KSP Build #828
Merged Sarbian's mach adjustments. Merged Sarbian's mach adjustments.
Fixed bugs relating to thrust and atmosphere/velocity curves. Fixed bugs relating to thrust and atmosphere/velocity curves.
Changed the atmospheric slider on the Build Engineer to work based on altitude. Changed the atmospheric slider on the Build Engineer to work based on altitude.
Changed the atmospheric slider to clamp to the maximum altitude for the selected body. Changed the atmospheric slider to clamp to the maximum altitude for the selected body.
Changed the velocity slider to clamp to the maximum usable mach value for the current vessel. Changed the velocity slider to clamp to the maximum usable mach value for the current vessel.
   
1.0.16.0, 2015-04-25, KSP Build #821 1.0.16.0, 2015-04-25, KSP Build #821
Fixed errors relating to KSP 1.0 update. Fixed errors relating to KSP 1.0 update.
Fixed fuel simulation to account for new thrust system. Fixed fuel simulation to account for new thrust system.
Fixed atmospheric engines to use the new velocity curve. Fixed atmospheric engines to use the new velocity curve.
Fixed atmospheric readouts to work with the new atmospheric model. Fixed atmospheric readouts to work with the new atmospheric model.
   
1.0.15.2, 2015-02-13 1.0.15.2, 2015-02-13
Padishar's Fixes: Padishar's Fixes:
Fixed: Calculation of per-stage resource mass. Fixed: Calculation of per-stage resource mass.
   
1.0.15.1, 2015-02-13 1.0.15.1, 2015-02-13
Rebuild Rebuild
   
1.0.15.0, 2015-02-08 1.0.15.0, 2015-02-08
Padishar's Fixes: Padishar's Fixes:
Added: Support KIDS ISP thrust correction. Added: Support KIDS ISP thrust correction.
Fixed: Log spam for stage priority mode. Fixed: Log spam for stage priority mode.
Fixed: Locked tanks preventing simulation from staging. Fixed: Locked tanks preventing simulation from staging.
Fixed: No flow and all vessel modes to respect flow states. Fixed: No flow and all vessel modes to respect flow states.
   
1.0.14.1, 2014-12-28 1.0.14.1, 2014-12-28
Fixed: Missing texture on the ER-7500 model. Fixed: Missing texture on the ER-7500 model.
   
1.0.14.0, 2014-12-28 1.0.14.0, 2014-12-28
Added: Career mode that limits the Flight Engineer by: Added: Career mode that limits the Flight Engineer by:
- Requiring an Engineer Kerbal of any level, or placement of an Engineer Chip or ER-7500 part. - Requiring an Engineer Kerbal of any level, or placement of an Engineer Chip or ER-7500 part.
- Tracking station level 3 enables Flight Engineer everywhere. - Tracking station level 3 enables Flight Engineer everywhere.
   
Added: New readouts to the orbital category: Added: New readouts to the orbital category:
- Mean Anomaly at Epoc - Mean Anomaly at Epoc
   
Added: New readouts to the miscellaneous category: Added: New readouts to the miscellaneous category:
- System Time - System Time
   
Added: Editor Overlay Tab's X position is now changable in the BuildOverlay.xml settings file. Added: Editor Overlay Tab's X position is now changable in the BuildOverlay.xml settings file.
Changed: Editor Overlay Tabs start position moved over as to not overlap the parts menu. Changed: Editor Overlay Tabs start position moved over as to not overlap the parts menu.
Fixed: Bug where STAGE_PRIORITY_FLOW resources would not be corrently disabled/enabled. Fixed: Bug where STAGE_PRIORITY_FLOW resources would not be corrently disabled/enabled.
Fixed: Issue with the formatting large Mass and Cost values. Fixed: Issue with the formatting large Mass and Cost values.
Fixed: Error when loading the Engineer7500 part model. Fixed: Error when loading the Engineer7500 part model.
   
1.0.13.1, 2014-12-16 1.0.13.1, 2014-12-16
Fixed: Issue with manoeuvre node readouts and low tier tracking station. Fixed: Issue with manoeuvre node readouts and low tier tracking station.
   
1.0.13.0, 2014-12-16 1.0.13.0, 2014-12-16
Updated for KSP version 0.90 Updated for KSP version 0.90
   
Added: New readouts to the vessel category: Added: New readouts to the vessel category:
- Heading Rate - Heading Rate
- Pitch Rate - Pitch Rate
- Roll Rate - Roll Rate
   
Changed: Simulation to look for fuel lines that use CModuleFuelLine module. Changed: Simulation to look for fuel lines that use CModuleFuelLine module.
Fixed: Editor Overlay now loads the saved visibility value properly. Fixed: Editor Overlay now loads the saved visibility value properly.
Fixed: Altitude (Terrain) will no longer give a reading below sea level. Fixed: Altitude (Terrain) will no longer give a reading below sea level.
Fixed: Suicide burn now uses radar altitude that clamps to sea level. Fixed: Suicide burn now uses radar altitude that clamps to sea level.
   
1.0.12.0, 2014-12-01 1.0.12.0, 2014-12-01
Added: Setting in Build Engineer to enable/disable vectored thrust calculations. Added: Setting in Build Engineer to enable/disable vectored thrust calculations.
Added: Thrust torque field in Build Engineer (courtesy of mic_e). Added: Thrust torque field in Build Engineer (courtesy of mic_e).
Added: New readouts to the vessel category: Added: New readouts to the vessel category:
- Thrust Offset Angle (courtesy of mic_e) - Thrust Offset Angle (courtesy of mic_e)
- Thrust Torque (courtesy of mic_e) - Thrust Torque (courtesy of mic_e)
- Part Count: stage/total - Part Count: stage/total
- Heading - Heading
- Pitch - Pitch
- Roll - Roll
   
Added: New readouts to the surface category: Added: New readouts to the surface category:
- Situation - Situation
   
Added: New readouts to the miscellaneous category: Added: New readouts to the miscellaneous category:
- Vectored Thrust Toggle - Vectored Thrust Toggle
   
Fixed: The category selection within the section editors now do not always reset back to 'Orbital'. Fixed: The category selection within the section editors now do not always reset back to 'Orbital'.
Fixed: Issue where the vessel simulation can sometimes permanently freeze. Fixed: Issue where the vessel simulation can sometimes permanently freeze.
Fixed: Issue where the vessel simulation would not show updates when the delay was set lower than the frame rate. Fixed: Issue where the vessel simulation would not show updates when the delay was set lower than the frame rate.
   
1.0.11.3, 2014-11-11 1.0.11.3, 2014-11-11
Changed: Gravity measurements for Isp to 9.82. Changed: Gravity measurements for Isp to 9.82.
   
1.0.11.2, 2014-11-10 1.0.11.2, 2014-11-10
Changed: Gravity measurements for Isp calculations from 9.81 to 9.8066 for accuracy. Changed: Gravity measurements for Isp calculations from 9.81 to 9.8066 for accuracy.
Changed: Manoeuvre node burn times are now more accurate. Changed: Manoeuvre node burn times are now more accurate.
Fixed: Bug in the manoeuvre node burn time calculations where it was not averaging acceleration correctly. Fixed: Bug in the manoeuvre node burn time calculations where it was not averaging acceleration correctly.
   
1.0.11.1, 2014-11-07 1.0.11.1, 2014-11-07
Changed: Build Engineer now shows stage part count as well as total. Changed: Build Engineer now shows stage part count as well as total.
Changed: Build Overlay Vessel tab data: Changed: Build Overlay Vessel tab data:
DeltaV: stage / total DeltaV: stage / total
Mass: stage / total Mass: stage / total
TWR: start (max) <- shows for bottom stage only. TWR: start (max) <- shows for bottom stage only.
Parts: stage / total Parts: stage / total
   
Fixed: Issue with the vessel tab vanishing from the editor. Fixed: Issue with the vessel tab vanishing from the editor.
   
1.0.11.0, 201-11-06 1.0.11.0, 201-11-06
Added: New readouts to the orbital category: Added: New readouts to the orbital category:
- Current SOI - Current SOI
- Manoeuvre Node DeltaV (Prograde) - Manoeuvre Node DeltaV (Prograde)
- Manoeuvre Node DeltaV (Normal) - Manoeuvre Node DeltaV (Normal)
- Manoeuvre Node DeltaV (Radial) - Manoeuvre Node DeltaV (Radial)
- Manoeuvre Node DeltaV (Total) - Manoeuvre Node DeltaV (Total)
- Manoeuvre Node Burn Time - Manoeuvre Node Burn Time
- Manoeuvre Node Half Burn Time - Manoeuvre Node Half Burn Time
- Manoeuvre Node Angle to Prograde - Manoeuvre Node Angle to Prograde
- Manoeuvre Node Angle to Retrograde - Manoeuvre Node Angle to Retrograde
- Time to Manoeuvre Node - Time to Manoeuvre Node
- Time to Manoeuvre Burn - Time to Manoeuvre Burn
   
Added: Readout help strings by ClassyJakey. Added: Readout help strings by ClassyJakey.
   
Fixed: Issue with separators in HUDs. Fixed: Issue with separators in HUDs.
Fixed: Issue with HUDs with backgrounds that have no displayed lines. Fixed: Issue with HUDs with backgrounds that have no displayed lines.
   
Padishar's Fixes: Padishar's Fixes:
Fixed: Issue with multicouplers when attached to parent by bottom node. Fixed: Issue with multicouplers when attached to parent by bottom node.
Fixed: Issue with sepratrons on solid rocket boosters. Fixed: Issue with sepratrons on solid rocket boosters.
   
1.0.10.0, 2014-10-19 1.0.10.0, 2014-10-19
UPDATE NOTICE: If you are updating from a previous version of Kerbal Engineer 1.0, please UPDATE NOTICE: If you are updating from a previous version of Kerbal Engineer 1.0, please
delete the 'Settings/SectionLibrary.xml' file, or remove the old install first. This will delete the 'Settings/SectionLibrary.xml' file, or remove the old install first. This will
reset the Flight Engineer sections to their default values and enable the new HUD functionality. reset the Flight Engineer sections to their default values and enable the new HUD functionality.
   
Added: New reaouts to the vessel category: Added: New reaouts to the vessel category:
- Suicide Burn Altitude (height above terrain to start burn) - Suicide Burn Altitude (height above terrain to start burn)
- Suicide Burn Distance (distance to suicide burn altitude) - Suicide Burn Distance (distance to suicide burn altitude)
- Suicide Burn DeltaV (velocity change required to zero vertical speed) - Suicide Burn DeltaV (velocity change required to zero vertical speed)
*** F5 for safety and use at your own risk! *** *** F5 for safety and use at your own risk! ***
   
Added: HUD type sections to the Flight Engineer. Added: HUD type sections to the Flight Engineer.
Added: HUD sections can have a smoked background for easy visibility. Added: HUD sections can have a smoked background for easy visibility.
Added: 'Switch to Target' button on the Target Selector readout. Added: 'Switch to Target' button on the Target Selector readout.
Changed: The default installed readouts to reduce new user brain melt. Changed: The default installed readouts to reduce new user brain melt.
Fixed: Flight Engineer not saving its hidden state. Fixed: Flight Engineer not saving its hidden state.
Fixed: Bug in the phase angle calculations. Fixed: Bug in the phase angle calculations.
Fixed: Bug where the Build Engineer would stay locked after hiding with the shortcut key. Fixed: Bug where the Build Engineer would stay locked after hiding with the shortcut key.
   
1.0.9.3, 2014-10-08 1.0.9.3, 2014-10-08
Added: Title of the build engineer in compact mode now shows if you are using atmospheric data. Added: Title of the build engineer in compact mode now shows if you are using atmospheric data.
Added: New readout to the surface category: Added: New readout to the surface category:
- Vertical Acceleration - Vertical Acceleration
- Horizontal Acceleration - Horizontal Acceleration
   
Changed: Atmospheric efficiency readout now shows as a percentage. Changed: Atmospheric efficiency readout now shows as a percentage.
Changed: Atmospheric settings (pressure/velocity) in the editor condensed onto a single line. Changed: Atmospheric settings (pressure/velocity) in the editor condensed onto a single line.
Fixed: Bug where the overlays in the editor would stay open outside of parts screen. Fixed: Bug where the overlays in the editor would stay open outside of parts screen.
   
1.0.9.2, 2014-10-07 1.0.9.2, 2014-10-07
Updated for KSP v0.25.0 Updated for KSP v0.25.0
Changed: Prettyfied Latitude and Longitude readouts. Changed: Prettyfied Latitude and Longitude readouts.
Changed: ModuleLandingGear now uses the physical significance flag. Changed: ModuleLandingGear now uses the physical significance flag.
Changed: Updated MiniAVC to 1.0.2.4. Changed: Updated MiniAVC to 1.0.2.4.
   
1.0.9.1, 2014-09-17 1.0.9.1, 2014-09-17
Fixed: Part size bug caused by TweakScale's cost calculator. Fixed: Part size bug caused by TweakScale's cost calculator.
   
1.0.9.0, 2014-09-15 1.0.9.0, 2014-09-15
Added: Build Engineer now also implements the '\' backslash show/hide shortcut. Added: Build Engineer now also implements the '\' backslash show/hide shortcut.
Added: New readouts to the vessel category: Added: New readouts to the vessel category:
- Current Stage DeltaV - Current Stage DeltaV
- Surface Thrust to Weight Ratio - Surface Thrust to Weight Ratio
   
Added: New editor overlay system. Added: New editor overlay system.
- Sleeker design. - Sleeker design.
- Hover over part information options: - Hover over part information options:
- Name only - Name only
- Middle click to show - Middle click to show
- Always show - Always show
- Slide out overlay displays: - Slide out overlay displays:
- Vessel information - Vessel information
- Resources list - Resources list
   
Fixed: Cost calculation now works with mods implementing IPartCostModifier. Fixed: Cost calculation now works with mods implementing IPartCostModifier.
   
1.0.8.1, 2014-09-06 1.0.8.1, 2014-09-06
Fixed: Bug which caused rendezvous readouts to freeze the game or show all zeros. Fixed: Bug which caused rendezvous readouts to freeze the game or show all zeros.
   
1.0.8.0, 2014-09-06 1.0.8.0, 2014-09-06
Added: New readouts to the vessel category: Added: New readouts to the vessel category:
- Intake Air (Usage) - Intake Air (Usage)
   
Added: New readouts to the rendezvous category: Added: New readouts to the rendezvous category:
- Relative Velocity - Relative Velocity
- Relative Speed - Relative Speed
   
Fixed: An issue where deltaV would not be calculated whilst flying. Fixed: An issue where deltaV would not be calculated whilst flying.
Fixed: NullRef whilst loading the in flight Action Menu. Fixed: NullRef whilst loading the in flight Action Menu.
   
1.0.7.1, 2014-09-02 1.0.7.1, 2014-09-02
Changed: Reversed Intake Air readout from 'S/D' to 'D/S' for easier reading. Changed: Reversed Intake Air readout from 'S/D' to 'D/S' for easier reading.
Changed: Increased Intake Air readout precision to 4 decimal places. Changed: Increased Intake Air readout precision to 4 decimal places.
Fixed: Issue where Intake Air supply was not representative of total supply. Fixed: Issue where Intake Air supply was not representative of total supply.
Fixed: Bug where actual thrust does not reset to zero on deactivated engines. Fixed: Bug where actual thrust does not reset to zero on deactivated engines.
Fixed: Thrust now scales with velocity for atmospheric engines. (Padishar's fix) Fixed: Thrust now scales with velocity for atmospheric engines. (Padishar's fix)
   
1.0.7.0, 2014-09-01 1.0.7.0, 2014-09-01
Added: Part count information to the Build Engineer. Added: Part count information to the Build Engineer.
Added: Reset button to the G-Force readout. Added: Reset button to the G-Force readout.
Added: Preset system to the Flight Engineer. Added: Preset system to the Flight Engineer.
Added: New stock presets: Added: New stock presets:
- Orbital - Orbital
- Surface - Surface
- Vessel - Vessel
- Rendezvous - Rendezvous
   
Added: New readouts to the orbital category: Added: New readouts to the orbital category:
- True Anomaly - True Anomaly
- Eccentric Anomaly - Eccentric Anomaly
- Mean Anomaly - Mean Anomaly
- Argument of Periapsis - Argument of Periapsis
- Angle to Prograde - Angle to Prograde
- Angle to Retrograde - Angle to Retrograde
   
Added: New readouts to the vessel category: Added: New readouts to the vessel category:
- Intake Air (Demand) - Intake Air (Demand)
- Intake Air (Supply) - Intake Air (Supply)
- Intake Air (Supply/Demand) - Intake Air (Supply/Demand)
   
Added: New readouts to the rendezvous category. Added: New readouts to the rendezvous category.
- Semi-major Axis - Semi-major Axis
- Semi-minor Axis - Semi-minor Axis
   
Added: Time formatter which can show time as referenced by any celestial body. Added: Time formatter which can show time as referenced by any celestial body.
Added: New readouts to the miscellaneous category: Added: New readouts to the miscellaneous category:
- Time Reference Adjuster - Time Reference Adjuster
   
Changed: Moved Sim Delay readout into the Miscellaneous category. Changed: Moved Sim Delay readout into the Miscellaneous category.
Changed: Updated MiniAVC to v1.0.2.3. Changed: Updated MiniAVC to v1.0.2.3.
Fixed: Issues with large value wrap around in the Flight Engineer. Fixed: Issues with large value wrap around in the Flight Engineer.
Fixed: Bug in the phase angle calculation. Fixed: Bug in the phase angle calculation.
   
1.0.6.0, 2014-08-23 1.0.6.0, 2014-08-23
Added: Time and Angle to equatorial ascending/descending nodes in the orbital display. Added: Time and Angle to equatorial ascending/descending nodes in the orbital display.
Added: Time and Angle to relative ascending/descending nodes in the rendezvous display. Added: Time and Angle to relative ascending/descending nodes in the rendezvous display.
Added: Overlay tooltip information delay adjustment slider to the Build Engineer settings. Added: Overlay tooltip information delay adjustment slider to the Build Engineer settings.
Added: Ability to rename the stock displays in the Flight Engineer. Added: Ability to rename the stock displays in the Flight Engineer.
Changed: Build Engineer is now hidden when not in parts view. Changed: Build Engineer is now hidden when not in parts view.
Changed: Custom display panels will only show in the control bar if an abbreviation is set. Changed: Custom display panels will only show in the control bar if an abbreviation is set.
Changed: Licensing and readme structures are now more verbose to satisfy the new add-on rules. Changed: Licensing and readme structures are now more verbose to satisfy the new add-on rules.
Fixed: Updated MiniAVC to v1.0.2.1 (fixes remote check bug as well as other minor bugs). Fixed: Updated MiniAVC to v1.0.2.1 (fixes remote check bug as well as other minor bugs).
   
1.0.5.0, 2014-08-13 1.0.5.0, 2014-08-13
Added: Acceleration readout to the Vessel category (current / maximum). Added: Acceleration readout to the Vessel category (current / maximum).
Added: Category library system for the Flight Engineer readouts. Added: Category library system for the Flight Engineer readouts.
Added: Drop-down category selection to better support the new system. Added: Drop-down category selection to better support the new system.
Changed: Misc category now called Miscellaneous (this will cause previously added readouts from this category to vanish). Changed: Misc category now called Miscellaneous (this will cause previously added readouts from this category to vanish).
Fixed: Bug with the Build Engineer toolbar button. Fixed: Bug with the Build Engineer toolbar button.
Fixed: Some buggyness when trying to close the bodies drop-down in the Build Engineer via the button. Fixed: Some buggyness when trying to close the bodies drop-down in the Build Engineer via the button.
Fixed: Flight Engineer toolbar menu now hides when hiding the GUI with F2. Fixed: Flight Engineer toolbar menu now hides when hiding the GUI with F2.
Fixed: Flight Engineer toolbar button now disables when in module mode and no engineer is running. Fixed: Flight Engineer toolbar button now disables when in module mode and no engineer is running.
   
1.0.4.0, 2014-08-12 1.0.4.0, 2014-08-12
Added: Better stock toolbar support in the flight engineer. Added: Better stock toolbar support in the flight engineer.
Added: Dynamically generated celestial body library for supporting add-ons that modify the star system. Added: Dynamically generated celestial body library for supporting add-ons that modify the star system.
Changed: Reference bodies are now listed with a nestable menu system. Changed: Reference bodies are now listed with a nestable menu system.
Changed: Extended logging system has been improved. Changed: Extended logging system has been improved.
Changed: Swapped out integrated MiniAVC in place of the official bundle version. Changed: Swapped out integrated MiniAVC in place of the official bundle version.
Changed: Increased general distance precision to 1 decimal place. Changed: Increased general distance precision to 1 decimal place.
Changed: Increased Semi-major/minor axis precision to 3 decimal places. Changed: Increased Semi-major/minor axis precision to 3 decimal places.
Fixed: Impact altitude was mistakenly formatted as an angle, it is now formatted correctly as a distance. Fixed: Impact altitude was mistakenly formatted as an angle, it is now formatted correctly as a distance.
   
1.0.3.0, 2014-07-30 1.0.3.0, 2014-07-30
Added: Integrated KSP-AVC support with MiniAVC. Added: Integrated KSP-AVC support with MiniAVC.
Added: Setting to change the simulation delay in the Build Engineer. Added: Setting to change the simulation delay in the Build Engineer.
Added: Setting to enable and disable the build overlay system. Added: Setting to enable and disable the build overlay system.
Added: Burn time to Delta-V readouts. Added: Burn time to Delta-V readouts.
Added: Atmospheric readouts fully support FAR. Added: Atmospheric readouts fully support FAR.
Added: Atmospheric readouts are disabled with NEAR. Added: Atmospheric readouts are disabled with NEAR.
Changed: Force formatting inversely scales decimal precision with value. Changed: Force formatting inversely scales decimal precision with value.
Fixed: Flickering in VAB and Vessel display. Fixed: Flickering in VAB and Vessel display.
Fixed: Bug saving the GUI display size. Fixed: Bug saving the GUI display size.
   
1.0.2.0, 2014-07-27 1.0.2.0, 2014-07-27
Added: Separator readout module under Misc in the Flight Engineer. Added: Separator readout module under Misc in the Flight Engineer.
Added: Adjustable GUI display size. Added: Adjustable GUI display size.
Added: Display size can be adjusted in the Build Engineer settings. Added: Display size can be adjusted in the Build Engineer settings.
Added: Misc readout for adjusting display size in the Flight Engineer. Added: Misc readout for adjusting display size in the Flight Engineer.
Changed: The rendezvous readout for the target's Orbital Period has higher precision. Changed: The rendezvous readout for the target's Orbital Period has higher precision.
Fixed: White toolbar icon by manually importing the texture if it cannot be found in the game database. Fixed: White toolbar icon by manually importing the texture if it cannot be found in the game database.
Fixed: Engines that have a minimum thrust are now calculated properly. (Thanks to nosscire.) Fixed: Engines that have a minimum thrust are now calculated properly. (Thanks to nosscire.)
Fixed: Compact collapse mode is now saved in the Build Engineer. Fixed: Compact collapse mode is now saved in the Build Engineer.
   
1.0.1.0, 2014-07-26 1.0.1.0, 2014-07-26
Added: Part-less Flight Engineer. Added: Part-less Flight Engineer.
Added: Ability to collapse the Build Engineer into compact mode from left or right. Added: Ability to collapse the Build Engineer into compact mode from left or right.
Added: Settings in Build Engineer for compact collapse mode and partless/module Flight Engineer. Added: Settings in Build Engineer for compact collapse mode and partless/module Flight Engineer.
Added: Biome, Impact Biome and Slope readouts. Added: Biome, Impact Biome and Slope readouts.
Added: Extra logging and exception handling. Added: Extra logging and exception handling.
Added: The original Engineer Chip part. Added: The original Engineer Chip part.
Added: "Show Engineer" toggle on the Flight Engineer toolbar. Added: "Show Engineer" toggle on the Flight Engineer toolbar.
Changed: Extended logging system now also writes to the standard KSP logs. Changed: Extended logging system now also writes to the standard KSP logs.
Changed: Extended logging saves next to the .dll file. Changed: Extended logging saves next to the .dll file.
Changed: ER7500 part has no physical significance. Changed: ER7500 part has no physical significance.
Fixed: ActionMenu and DisplayStack destruction bug. Fixed: ActionMenu and DisplayStack destruction bug.
   
1.0.0.1, 2014-06-24 1.0.0.1, 2014-06-24
Added: Stock toolbar support in the Flight Engineer. Added: Stock toolbar support in the Flight Engineer.
Changed: Orbital Period has higher precision. Changed: Orbital Period has higher precision.
Fixed: Various NullRefs in editor window and overlay. Fixed: Various NullRefs in editor window and overlay.
   
1.0.0.0, 2014-07-24 1.0.0.0, 2014-07-24
Initial release for public testing. Initial release for public testing.
   
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2016 CYBUTEK // Copyright (C) 2016 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer.Unity namespace KerbalEngineer.Unity
{ {
using System; using System;
using System.Collections; using System.Collections;
using UnityEngine; using UnityEngine;
   
[RequireComponent(typeof(CanvasGroup))] [RequireComponent(typeof(CanvasGroup))]
public class CanvasGroupFader : MonoBehaviour public class CanvasGroupFader : MonoBehaviour
{ {
private CanvasGroup m_CanvasGroup; private CanvasGroup m_CanvasGroup;
private IEnumerator m_FadeCoroutine; private IEnumerator m_FadeCoroutine;
   
public bool IsFading public bool IsFading
{ {
get get
{ {
return m_FadeCoroutine != null; return m_FadeCoroutine != null;
} }
} }
   
/// <summary> /// <summary>
/// Fades the canvas group to a specified alpha using the supplied blocking state during fade with optional callback. /// Fades the canvas group to a specified alpha using the supplied blocking state during fade with optional callback.
/// </summary> /// </summary>
public void FadeTo(float alpha, float duration, Action callback = null) public void FadeTo(float alpha, float duration, Action callback = null)
{ {
if (m_CanvasGroup == null) if (m_CanvasGroup == null)
{ {
return; return;
} }
   
Fade(m_CanvasGroup.alpha, alpha, duration, callback); Fade(m_CanvasGroup.alpha, alpha, duration, callback);
} }
   
/// <summary> /// <summary>
/// Sets the alpha value of the canvas group. /// Sets the alpha value of the canvas group.
/// </summary> /// </summary>
public void SetAlpha(float alpha) public void SetAlpha(float alpha)
{ {
if (m_CanvasGroup == null) if (m_CanvasGroup == null)
{ {
return; return;
} }
   
alpha = Mathf.Clamp01(alpha); alpha = Mathf.Clamp01(alpha);
m_CanvasGroup.alpha = alpha; m_CanvasGroup.alpha = alpha;
} }
   
protected virtual void Awake() protected virtual void Awake()
{ {
// cache components // cache components
m_CanvasGroup = GetComponent<CanvasGroup>(); m_CanvasGroup = GetComponent<CanvasGroup>();
} }
   
/// <summary> /// <summary>
/// Starts a fade from one alpha value to another with callback. /// Starts a fade from one alpha value to another with callback.
/// </summary> /// </summary>
private void Fade(float from, float to, float duration, Action callback) private void Fade(float from, float to, float duration, Action callback)
{ {
if (m_FadeCoroutine != null) if (m_FadeCoroutine != null)
{ {
StopCoroutine(m_FadeCoroutine); StopCoroutine(m_FadeCoroutine);
} }
   
m_FadeCoroutine = FadeCoroutine(from, to, duration, callback); m_FadeCoroutine = FadeCoroutine(from, to, duration, callback);
StartCoroutine(m_FadeCoroutine); StartCoroutine(m_FadeCoroutine);
} }
   
/// <summary> /// <summary>
/// Coroutine that handles the fading. /// Coroutine that handles the fading.
/// </summary> /// </summary>
private IEnumerator FadeCoroutine(float from, float to, float duration, Action callback) private IEnumerator FadeCoroutine(float from, float to, float duration, Action callback)
{ {
// wait for end of frame so that only the last call to fade that frame is honoured. // wait for end of frame so that only the last call to fade that frame is honoured.
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
   
float progress = 0.0f; float progress = 0.0f;
   
while (progress <= 1.0f) while (progress <= 1.0f)
{ {
progress += Time.deltaTime / duration; progress += Time.deltaTime / duration;
SetAlpha(Mathf.Lerp(from, to, progress)); SetAlpha(Mathf.Lerp(from, to, progress));
yield return null; yield return null;
} }
   
//print(m_CanvasGroup.alpha); if (callback != null)
callback?.Invoke(); {
  callback.Invoke();
  }
   
m_FadeCoroutine = null; m_FadeCoroutine = null;
} }
} }
} }
namespace KerbalEngineer.Unity namespace KerbalEngineer.Unity
{ {
using System; using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.UI; using UnityEngine.UI;
   
public class Setting : MonoBehaviour public class Setting : MonoBehaviour
{ {
[SerializeField] [SerializeField]
private Text m_Label = null; private Text m_Label = null;
   
[SerializeField] [SerializeField]
private Transform m_ButtonsTransform = null; private Transform m_ButtonsTransform = null;
   
[SerializeField] [SerializeField]
private GameObject m_SettingButtonPrefab = null; private GameObject m_SettingButtonPrefab = null;
   
[SerializeField] [SerializeField]
private GameObject m_SettingTogglePrefab = null; private GameObject m_SettingTogglePrefab = null;
   
private Action m_OnUpdate; private Action m_OnUpdate;
   
public Button AddButton(string text, float width, UnityAction onClick) public Button AddButton(string text, float width, UnityAction onClick)
{ {
Button button = null; Button button = null;
   
if (m_SettingButtonPrefab != null) if (m_SettingButtonPrefab != null)
{ {
GameObject buttonObject = Instantiate(m_SettingButtonPrefab); GameObject buttonObject = Instantiate(m_SettingButtonPrefab);
if (buttonObject != null) if (buttonObject != null)
{ {
button = buttonObject.GetComponent<Button>(); button = buttonObject.GetComponent<Button>();
   
SetParentTransform(buttonObject, m_ButtonsTransform); SetParentTransform(buttonObject, m_ButtonsTransform);
SetWidth(buttonObject, width); SetWidth(buttonObject, width);
SetText(buttonObject, text); SetText(buttonObject, text);
SetButton(buttonObject, onClick); SetButton(buttonObject, onClick);
} }
} }
   
return button; return button;
} }
   
public Toggle AddToggle(string text, float width, UnityAction<bool> onValueChanged) public Toggle AddToggle(string text, float width, UnityAction<bool> onValueChanged)
{ {
Toggle toggle = null; Toggle toggle = null;
   
if (m_SettingTogglePrefab != null) if (m_SettingTogglePrefab != null)
{ {
GameObject toggleObject = Instantiate(m_SettingTogglePrefab); GameObject toggleObject = Instantiate(m_SettingTogglePrefab);
if (toggleObject != null) if (toggleObject != null)
{ {
toggle = toggleObject.GetComponent<Toggle>(); toggle = toggleObject.GetComponent<Toggle>();
   
SetParentTransform(toggleObject, m_ButtonsTransform); SetParentTransform(toggleObject, m_ButtonsTransform);
SetWidth(toggleObject, width); SetWidth(toggleObject, width);
SetText(toggleObject, text); SetText(toggleObject, text);
SetToggle(toggleObject, onValueChanged); SetToggle(toggleObject, onValueChanged);
} }
} }
   
return toggle; return toggle;
} }
   
public void AddUpdateHandler(Action onUpdate) public void AddUpdateHandler(Action onUpdate)
{ {
m_OnUpdate = onUpdate; m_OnUpdate = onUpdate;
} }
   
public void SetLabel(string text) public void SetLabel(string text)
{ {
if (m_Label != null) if (m_Label != null)
{ {
m_Label.text = text; m_Label.text = text;
} }
} }
   
protected virtual void Update() protected virtual void Update()
{ {
m_OnUpdate?.Invoke(); if (m_OnUpdate != null)
  {
  m_OnUpdate.Invoke();
  }
} }
   
private static void SetButton(GameObject buttonObject, UnityAction onClick) private static void SetButton(GameObject buttonObject, UnityAction onClick)
{ {
if (buttonObject != null) if (buttonObject != null)
{ {
Button button = buttonObject.GetComponent<Button>(); Button button = buttonObject.GetComponent<Button>();
if (button != null) if (button != null)
{ {
button.onClick.AddListener(onClick); button.onClick.AddListener(onClick);
} }
} }
} }
   
private static void SetParentTransform(GameObject childObject, Transform parentTransform) private static void SetParentTransform(GameObject childObject, Transform parentTransform)
{ {
if (childObject != null && parentTransform != null) if (childObject != null && parentTransform != null)
{ {
childObject.transform.SetParent(parentTransform, false); childObject.transform.SetParent(parentTransform, false);
} }
} }
   
private static void SetText(GameObject parentObject, string text) private static void SetText(GameObject parentObject, string text)
{ {
if (parentObject != null) if (parentObject != null)
{ {
Text textComponent = parentObject.GetComponentInChildren<Text>(); Text textComponent = parentObject.GetComponentInChildren<Text>();
if (textComponent != null) if (textComponent != null)
{ {
textComponent.text = text; textComponent.text = text;
} }
} }
} }
   
private static void SetToggle(GameObject toggleObject, UnityAction<bool> onValueChanged) private static void SetToggle(GameObject toggleObject, UnityAction<bool> onValueChanged)
{ {
if (toggleObject != null) if (toggleObject != null)
{ {
Toggle toggle = toggleObject.GetComponent<Toggle>(); Toggle toggle = toggleObject.GetComponent<Toggle>();
if (toggle != null) if (toggle != null)
{ {
toggle.onValueChanged.AddListener(onValueChanged); toggle.onValueChanged.AddListener(onValueChanged);
} }
} }
} }
   
private static void SetWidth(GameObject parentObject, float width) private static void SetWidth(GameObject parentObject, float width)
{ {
if (parentObject != null) if (parentObject != null)
{ {
LayoutElement layout = parentObject.GetComponent<LayoutElement>(); LayoutElement layout = parentObject.GetComponent<LayoutElement>();
if (layout != null) if (layout != null)
{ {
if (width > 0.0f) if (width > 0.0f)
{ {
layout.preferredWidth = width; layout.preferredWidth = width;
} }
else else
{ {
layout.flexibleWidth = 1.0f; layout.flexibleWidth = 1.0f;
} }
} }
} }
} }
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2016 CYBUTEK // Copyright (C) 2016 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer.Unity.UI namespace KerbalEngineer.Unity.UI
{ {
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
   
public class StyleApplicator : MonoBehaviour public class StyleApplicator : MonoBehaviour
{ {
public enum ElementTypes public enum ElementTypes
{ {
None, None,
Window, Window,
Box, Box,
Button, Button,
ButtonToggle, ButtonToggle,
Label Label
} }
   
[SerializeField] [SerializeField]
private ElementTypes m_ElementType = ElementTypes.None; private ElementTypes m_ElementType = ElementTypes.None;
   
/// <summary> /// <summary>
/// Gets the UI element type used by the ThemeManager for selecting how to apply the theme. /// Gets the UI element type used by the ThemeManager for selecting how to apply the theme.
/// </summary> /// </summary>
public ElementTypes ElementType public ElementTypes ElementType
{ {
get get
{ {
return m_ElementType; return m_ElementType;
} }
} }
   
/// <summary> /// <summary>
/// Sets a the applicator to apply the selected sprite to an attached image component. /// Sets a the applicator to apply the selected sprite to an attached image component.
/// </summary> /// </summary>
public void SetImage(Sprite sprite, Image.Type type) public void SetImage(Sprite sprite, Image.Type type)
{ {
Image image = GetComponent<Image>(); Image image = GetComponent<Image>();
if (image == null) if (image == null)
{ {
return; return;
} }
   
image.sprite = sprite; image.sprite = sprite;
image.type = type; image.type = type;
} }
   
/// <summary> /// <summary>
/// Sets the applicator to apply the specified values to an attached selectable component. /// Sets the applicator to apply the specified values to an attached selectable component.
/// </summary> /// </summary>
public void SetSelectable(TextStyle textStyle, Sprite normal, Sprite highlight, Sprite pressed, Sprite disabled) public void SetSelectable(TextStyle textStyle, Sprite normal, Sprite highlight, Sprite pressed, Sprite disabled)
{ {
SetText(textStyle, GetComponentInChildren<Text>()); SetText(textStyle, GetComponentInChildren<Text>());
   
Selectable selectable = GetComponent<Selectable>(); Selectable selectable = GetComponent<Selectable>();
if (selectable != null) if (selectable != null)
{ {
selectable.image.sprite = normal; selectable.image.sprite = normal;
selectable.image.type = Image.Type.Sliced; selectable.image.type = Image.Type.Sliced;
   
selectable.transition = Selectable.Transition.SpriteSwap; selectable.transition = Selectable.Transition.SpriteSwap;
   
SpriteState spriteState = selectable.spriteState; SpriteState spriteState = selectable.spriteState;
spriteState.highlightedSprite = highlight; spriteState.highlightedSprite = highlight;
spriteState.pressedSprite = pressed; spriteState.pressedSprite = pressed;
spriteState.disabledSprite = disabled; spriteState.disabledSprite = disabled;
selectable.spriteState = spriteState; selectable.spriteState = spriteState;
} }
} }
   
/// <summary> /// <summary>
/// Sets the applicator to apply a style to an attached text component. /// Sets the applicator to apply a style to an attached text component.
/// </summary> /// </summary>
public void SetText(TextStyle textStyle) public void SetText(TextStyle textStyle)
{ {
SetText(textStyle, GetComponent<Text>()); SetText(textStyle, GetComponent<Text>());
} }
   
/// <summary> /// <summary>
/// Sets the applicator to apply the specified values to an attached toggle component. /// Sets the applicator to apply the specified values to an attached toggle component.
/// </summary> /// </summary>
public void SetToggle(TextStyle textStyle, Sprite normal, Sprite highlight, Sprite pressed, Sprite disabled) public void SetToggle(TextStyle textStyle, Sprite normal, Sprite highlight, Sprite pressed, Sprite disabled)
{ {
SetSelectable(textStyle, normal, highlight, pressed, disabled); SetSelectable(textStyle, normal, highlight, pressed, disabled);
   
Image toggleImage = GetComponent<Toggle>()?.graphic as Image; Toggle toggleComponent = GetComponent<Toggle>();
if (toggleImage != null) if (toggleComponent != null)
{ {
toggleImage.sprite = pressed; Image toggleImage = toggleComponent.graphic as Image;
toggleImage.type = Image.Type.Sliced; if (toggleImage != null)
  {
  toggleImage.sprite = pressed;
  toggleImage.type = Image.Type.Sliced;
  }
} }
} }
   
/// <summary> /// <summary>
/// Sets the applicator to apply a style to the supplied text component. /// Sets the applicator to apply a style to the supplied text component.
/// </summary> /// </summary>
private static void SetText(TextStyle textStyle, Text textComponent) private static void SetText(TextStyle textStyle, Text textComponent)
{ {
if (textStyle == null || textComponent == null) if (textStyle == null || textComponent == null)
{ {
return; return;
} }
   
if (textStyle.Font != null) if (textStyle.Font != null)
{ {
textComponent.font = textStyle.Font; textComponent.font = textStyle.Font;
} }
textComponent.fontSize = textStyle.Size; textComponent.fontSize = textStyle.Size;
textComponent.fontStyle = textStyle.Style; textComponent.fontStyle = textStyle.Style;
textComponent.color = textStyle.Colour; textComponent.color = textStyle.Colour;
} }
} }
} }
namespace KerbalEngineer.Unity.UI namespace KerbalEngineer.Unity.UI
{ {
using System; using System;
using System.Collections; using System.Collections;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
   
[RequireComponent(typeof(RectTransform), typeof(CanvasGroup))] [RequireComponent(typeof(RectTransform), typeof(CanvasGroup))]
public class Window : MonoBehaviour, IBeginDragHandler, IDragHandler public class Window : MonoBehaviour, IBeginDragHandler, IDragHandler
{ {
[SerializeField] [SerializeField]
private Text m_Title = null; private Text m_Title = null;
   
[SerializeField] [SerializeField]
private Transform m_Content = null; private Transform m_Content = null;
   
private Vector2 m_BeginMousePosition; private Vector2 m_BeginMousePosition;
private Vector3 m_BeginWindowPosition; private Vector3 m_BeginWindowPosition;
private CanvasGroup m_CanvasGroup; private CanvasGroup m_CanvasGroup;
private RectTransform m_RectTransform; private RectTransform m_RectTransform;
private IEnumerator m_ScaleFadeCoroutine; private IEnumerator m_ScaleFadeCoroutine;
   
/// <summary> /// <summary>
/// Gets the content transform. /// Gets the content transform.
/// </summary> /// </summary>
public Transform Content public Transform Content
{ {
get get
{ {
return m_Content; return m_Content;
} }
} }
   
/// <summary> /// <summary>
/// Gets the rect transform component. /// Gets the rect transform component.
/// </summary> /// </summary>
public RectTransform RectTransform public RectTransform RectTransform
{ {
get get
{ {
return m_RectTransform; return m_RectTransform;
} }
} }
   
public void OnBeginDrag(PointerEventData eventData) public void OnBeginDrag(PointerEventData eventData)
{ {
if (m_RectTransform == null) if (m_RectTransform == null)
{ {
return; return;
} }
   
// cache starting positions // cache starting positions
m_BeginMousePosition = eventData.position; m_BeginMousePosition = eventData.position;
m_BeginWindowPosition = m_RectTransform.position; m_BeginWindowPosition = m_RectTransform.position;
} }
   
public void OnDrag(PointerEventData eventData) public void OnDrag(PointerEventData eventData)
{ {
if (m_RectTransform != null) if (m_RectTransform != null)
{ {
// new position is the starting window position plus the delta of the current and starting mouse positions // new position is the starting window position plus the delta of the current and starting mouse positions
m_RectTransform.position = m_BeginWindowPosition + (Vector3)(eventData.position - m_BeginMousePosition); m_RectTransform.position = m_BeginWindowPosition + (Vector3)(eventData.position - m_BeginMousePosition);
} }
} }
   
/// <summary> /// <summary>
/// Adds a game object as a child of the window content. /// Adds a game object as a child of the window content.
/// </summary> /// </summary>
public void AddToContent(GameObject childObject) public void AddToContent(GameObject childObject)
{ {
if (m_Content != null && childObject != null) if (m_Content != null && childObject != null)
{ {
childObject.transform.SetParent(m_Content, false); childObject.transform.SetParent(m_Content, false);
} }
} }
   
/// <summary> /// <summary>
/// Closes the window. /// Closes the window.
/// </summary> /// </summary>
public void Close() public void Close()
{ {
ScaleFade(1.0f, 0.0f, () => Destroy(gameObject)); ScaleFade(1.0f, 0.0f, () => Destroy(gameObject));
} }
   
/// <summary> /// <summary>
/// Sets the window title. /// Sets the window title.
/// </summary> /// </summary>
public void SetTitle(string title) public void SetTitle(string title)
{ {
if (m_Title != null) if (m_Title != null)
{ {
m_Title.text = title; m_Title.text = title;
} }
} }
   
/// <summary> /// <summary>
/// Sets the window size. /// Sets the window size.
/// </summary> /// </summary>
public void SetWidth(float width) public void SetWidth(float width)
{ {
if (m_RectTransform != null) if (m_RectTransform != null)
{ {
Vector2 size = m_RectTransform.sizeDelta; Vector2 size = m_RectTransform.sizeDelta;
size.x = width; size.x = width;
m_RectTransform.sizeDelta = size; m_RectTransform.sizeDelta = size;
} }
} }
   
protected virtual void Awake() protected virtual void Awake()
{ {
// component caching // component caching
m_RectTransform = GetComponent<RectTransform>(); m_RectTransform = GetComponent<RectTransform>();
m_CanvasGroup = GetComponent<CanvasGroup>(); m_CanvasGroup = GetComponent<CanvasGroup>();
} }
   
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
// scales and fades the window into view // scales and fades the window into view
ScaleFade(0.0f, 1.0f, null); ScaleFade(0.0f, 1.0f, null);
} }
   
/// <summary> /// <summary>
/// Scales and fades from a value to another with callback. /// Scales and fades from a value to another with callback.
/// </summary> /// </summary>
private void ScaleFade(float from, float to, Action callback) private void ScaleFade(float from, float to, Action callback)
{ {
if (m_ScaleFadeCoroutine != null) if (m_ScaleFadeCoroutine != null)
{ {
StopCoroutine(m_ScaleFadeCoroutine); StopCoroutine(m_ScaleFadeCoroutine);
} }
   
m_ScaleFadeCoroutine = ScaleFadeCoroutine(from, to, callback); m_ScaleFadeCoroutine = ScaleFadeCoroutine(from, to, callback);
StartCoroutine(m_ScaleFadeCoroutine); StartCoroutine(m_ScaleFadeCoroutine);
} }
   
/// <summary> /// <summary>
/// Coroutine to handle the scale and fading of the window. /// Coroutine to handle the scale and fading of the window.
/// </summary> /// </summary>
private IEnumerator ScaleFadeCoroutine(float from, float to, Action callback) private IEnumerator ScaleFadeCoroutine(float from, float to, Action callback)
{ {
float progress = 0.0f; float progress = 0.0f;
float value; float value;
   
while (progress <= 1.0f) while (progress <= 1.0f)
{ {
progress += (Time.deltaTime / 0.2f); progress += (Time.deltaTime / 0.2f);
value = Mathf.Lerp(from, to, progress); value = Mathf.Lerp(from, to, progress);
   
// scale // scale
transform.localScale = Vector3.one * value; transform.localScale = Vector3.one * value;
   
// fade if a canvas group is attached // fade if a canvas group is attached
if (m_CanvasGroup != null) if (m_CanvasGroup != null)
{ {
m_CanvasGroup.alpha = Mathf.Clamp01(value); m_CanvasGroup.alpha = Mathf.Clamp01(value);
} }
   
yield return null; yield return null;
} }
   
callback?.Invoke(); if (callback != null)
  {
  callback.Invoke();
  }
   
m_ScaleFadeCoroutine = null; m_ScaleFadeCoroutine = null;
} }
} }
} }
// //
// Copyright (C) 2015 CYBUTEK // Copyright (C) 2015 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer namespace KerbalEngineer
{ {
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
   
public static class EngineerGlobals public static class EngineerGlobals
{ {
/// <summary> /// <summary>
/// Current version of the Kerbal Engineer assembly. /// Current version of the Kerbal Engineer assembly.
/// </summary> /// </summary>
public const string ASSEMBLY_VERSION = "1.1.0.2"; public const string ASSEMBLY_VERSION = "1.1.1.0";
   
private static string assemblyFile; private static string assemblyFile;
private static string assemblyName; private static string assemblyName;
private static string assemblyPath; private static string assemblyPath;
private static string settingsPath; private static string settingsPath;
   
/// <summary> /// <summary>
/// Gets the Kerbal Engineer assembly's path including the file name. /// Gets the Kerbal Engineer assembly's path including the file name.
/// </summary> /// </summary>
public static string AssemblyFile public static string AssemblyFile
{ {
get get
{ {
return assemblyFile ?? (assemblyFile = Assembly.GetExecutingAssembly().Location); return assemblyFile ?? (assemblyFile = Assembly.GetExecutingAssembly().Location);
} }
} }
   
/// <summary> /// <summary>
/// Gets the Kerbal Engineer assembly's file name. /// Gets the Kerbal Engineer assembly's file name.
/// </summary> /// </summary>
public static string AssemblyName public static string AssemblyName
{ {
get get
{ {
return assemblyName ?? (assemblyName = new FileInfo(AssemblyFile).Name); return assemblyName ?? (assemblyName = new FileInfo(AssemblyFile).Name);
} }
} }
   
/// <summary> /// <summary>
/// Gets the Kerbal Engineer assembly's path excluding the file name. /// Gets the Kerbal Engineer assembly's path excluding the file name.
/// </summary> /// </summary>
public static string AssemblyPath public static string AssemblyPath
{ {
get get
{ {
return assemblyPath ?? (assemblyPath = AssemblyFile.Replace(new FileInfo(AssemblyFile).Name, "")); return assemblyPath ?? (assemblyPath = AssemblyFile.Replace(new FileInfo(AssemblyFile).Name, ""));
} }
} }
   
/// <summary> /// <summary>
/// Gets the settings directory path. /// Gets the settings directory path.
/// </summary> /// </summary>
public static string SettingsPath public static string SettingsPath
{ {
get get
{ {
if (string.IsNullOrEmpty(settingsPath)) if (string.IsNullOrEmpty(settingsPath))
{ {
settingsPath = Path.Combine(AssemblyPath, "Settings"); settingsPath = Path.Combine(AssemblyPath, "Settings");
} }
return settingsPath; return settingsPath;
} }
} }
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
using System; using System;
   
using KerbalEngineer.Helpers; using KerbalEngineer.Helpers;
   
using UnityEngine; using UnityEngine;
   
#endregion #endregion
   
namespace KerbalEngineer.Extensions namespace KerbalEngineer.Extensions
{ {
public static class OrbitExtensions public static class OrbitExtensions
{ {
#region Constants #region Constants
   
public const double Tau = Math.PI * 2.0; public const double Tau = Math.PI * 2.0;
   
#endregion #endregion
   
#region Methods: public #region Methods: public
   
public static double GetAngleToAscendingNode(this Orbit orbit) public static double GetAngleToAscendingNode(this Orbit orbit)
{ {
return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit)); return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit));
} }
   
public static double GetAngleToDescendingNode(this Orbit orbit) public static double GetAngleToDescendingNode(this Orbit orbit)
{ {
return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit)); return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit));
} }
   
public static double GetAngleToPrograde(this Orbit orbit) public static double GetAngleToPrograde(this Orbit orbit)
{ {
return GetAngleToPrograde(orbit, Planetarium.GetUniversalTime()); return GetAngleToPrograde(orbit, Planetarium.GetUniversalTime());
} }
   
public static double GetAngleToPrograde(this Orbit orbit, double universalTime) public static double GetAngleToPrograde(this Orbit orbit, double universalTime)
{ {
if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody) if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody)
{ {
return 0.0; return 0.0;
} }
   
Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime); Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime);
orbitVector.z = 0.0; orbitVector.z = 0.0;
   
Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime); Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime);
bodyVector.z = 0.0; bodyVector.z = 0.0;
   
double angle = AngleHelper.GetAngleBetweenVectors(bodyVector, orbitVector); double angle = AngleHelper.GetAngleBetweenVectors(bodyVector, orbitVector);
   
return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle); return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle);
} }
   
public static double GetAngleToRetrograde(this Orbit orbit) public static double GetAngleToRetrograde(this Orbit orbit)
{ {
return GetAngleToRetrograde(orbit, Planetarium.GetUniversalTime()); return GetAngleToRetrograde(orbit, Planetarium.GetUniversalTime());
} }
   
public static double GetAngleToRetrograde(this Orbit orbit, double universalTime) public static double GetAngleToRetrograde(this Orbit orbit, double universalTime)
{ {
if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody) if (orbit.referenceBody == CelestialBodies.SystemBody.CelestialBody)
{ {
return 0.0; return 0.0;
} }
   
Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime); Vector3d orbitVector = orbit.getRelativePositionAtUT(universalTime);
orbitVector.z = 0.0; orbitVector.z = 0.0;
   
Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime); Vector3d bodyVector = orbit.referenceBody.orbit.getOrbitalVelocityAtUT(universalTime);
bodyVector.z = 0.0; bodyVector.z = 0.0;
   
double angle = AngleHelper.GetAngleBetweenVectors(-bodyVector, orbitVector); double angle = AngleHelper.GetAngleBetweenVectors(-bodyVector, orbitVector);
   
return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle); return AngleHelper.Clamp360(orbit.inclination < 90.0 ? angle : 360.0 - angle);
} }
   
public static double GetAngleToTrueAnomaly(this Orbit orbit, double trueAnomaly) public static double GetAngleToTrueAnomaly(this Orbit orbit, double trueAnomaly)
{ {
return AngleHelper.Clamp360(trueAnomaly - orbit.trueAnomaly); return AngleHelper.Clamp360(trueAnomaly - (orbit.trueAnomaly * Units.RAD_TO_DEG));
} }
   
public static double GetAngleToVector(this Orbit orbit, Vector3d vector) public static double GetAngleToVector(this Orbit orbit, Vector3d vector)
{ {
return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, Vector3d.Exclude(orbit.GetOrbitNormal(), vector))); return GetAngleToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, Vector3d.Exclude(orbit.GetOrbitNormal(), vector)));
} }
   
public static double GetPhaseAngle(this Orbit orbit, Orbit target) public static double GetPhaseAngle(this Orbit orbit, Orbit target)
{ {
var angle = AngleHelper.GetAngleBetweenVectors(Vector3d.Exclude(orbit.GetOrbitNormal(), target.pos), orbit.pos); var angle = AngleHelper.GetAngleBetweenVectors(Vector3d.Exclude(orbit.GetOrbitNormal(), target.pos), orbit.pos);
return (orbit.semiMajorAxis < target.semiMajorAxis) ? angle : angle - 360.0; return (orbit.semiMajorAxis < target.semiMajorAxis) ? angle : angle - 360.0;
} }
   
public static double GetRelativeInclination(this Orbit orbit, Orbit target) public static double GetRelativeInclination(this Orbit orbit, Orbit target)
{ {
return Vector3d.Angle(orbit.GetOrbitNormal(), target.GetOrbitNormal()); return Vector3d.Angle(orbit.GetOrbitNormal(), target.GetOrbitNormal());
} }
   
public static double GetTimeToAscendingNode(this Orbit orbit) public static double GetTimeToAscendingNode(this Orbit orbit)
{ {
return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit)); return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfAscendingNode(orbit));
} }
   
public static double GetTimeToDescendingNode(this Orbit orbit) public static double GetTimeToDescendingNode(this Orbit orbit)
{ {
return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit)); return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyOfDescendingNode(orbit));
} }
   
public static double GetTimeToTrueAnomaly(this Orbit orbit, double trueAnomaly) public static double GetTimeToTrueAnomaly(this Orbit orbit, double trueAnomaly)
{ {
var time = orbit.GetDTforTrueAnomaly(trueAnomaly * Mathf.Deg2Rad, orbit.period); var time = orbit.GetDTforTrueAnomaly(trueAnomaly * Mathf.Deg2Rad, orbit.period);
return time < 0.0 ? time + orbit.period : time; return time < 0.0 ? time + orbit.period : time;
} }
   
public static double GetTimeToVector(this Orbit orbit, Vector3d vector) public static double GetTimeToVector(this Orbit orbit, Vector3d vector)
{ {
return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, vector)); return GetTimeToTrueAnomaly(orbit, GetTrueAnomalyFromVector(orbit, vector));
} }
   
public static double GetTrueAnomalyFromVector(this Orbit orbit, Vector3d vector) public static double GetTrueAnomalyFromVector(this Orbit orbit, Vector3d vector)
{ {
return orbit.GetTrueAnomalyOfZupVector(vector) * Mathf.Rad2Deg; return orbit.GetTrueAnomalyOfZupVector(vector) * Mathf.Rad2Deg;
} }
   
public static double GetTrueAnomalyOfAscendingNode(this Orbit orbit) public static double GetTrueAnomalyOfAscendingNode(this Orbit orbit)
{ {
return 360.0 - orbit.argumentOfPeriapsis; return 360.0 - orbit.argumentOfPeriapsis;
} }
   
public static double GetTrueAnomalyOfDescendingNode(this Orbit orbit) public static double GetTrueAnomalyOfDescendingNode(this Orbit orbit)
{ {
return 180.0 - orbit.argumentOfPeriapsis; return 180.0 - orbit.argumentOfPeriapsis;
} }
   
#endregion #endregion
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
   
using KerbalEngineer.Extensions; using KerbalEngineer.Extensions;
using KerbalEngineer.Flight.Sections; using KerbalEngineer.Flight.Sections;
using KerbalEngineer.Settings; using KerbalEngineer.Settings;
   
using UnityEngine; using UnityEngine;
   
#endregion #endregion
   
namespace KerbalEngineer.Flight namespace KerbalEngineer.Flight
{ {
using KeyBinding; using KeyBinding;
using Upgradeables; using Upgradeables;
   
/// <summary> /// <summary>
/// Graphical controller for displaying stacked sections. /// Graphical controller for displaying stacked sections.
/// </summary> /// </summary>
[KSPAddon(KSPAddon.Startup.Flight, false)] [KSPAddon(KSPAddon.Startup.Flight, false)]
public class DisplayStack : MonoBehaviour public class DisplayStack : MonoBehaviour
{ {
#region Fields #region Fields
   
private GUIStyle buttonStyle; private GUIStyle buttonStyle;
private int numberOfStackSections; private int numberOfStackSections;
private bool resizeRequested; private bool resizeRequested;
private bool showControlBar = true; private bool showControlBar = true;
private GUIStyle titleStyle; private GUIStyle titleStyle;
private int windowId; private int windowId;
private Rect windowPosition; private Rect windowPosition;
private GUIStyle windowStyle; private GUIStyle windowStyle;
   
#endregion #endregion
   
#region Properties #region Properties
   
/// <summary> /// <summary>
/// Gets the current instance of the DisplayStack. /// Gets the current instance of the DisplayStack.
/// </summary> /// </summary>
public static DisplayStack Instance { get; private set; } public static DisplayStack Instance { get; private set; }
   
public bool Hidden { get; set; } public bool Hidden { get; set; }
   
/// <summary> /// <summary>
/// Gets and sets the visibility of the control bar. /// Gets and sets the visibility of the control bar.
/// </summary> /// </summary>
public bool ShowControlBar public bool ShowControlBar
{ {
get { return this.showControlBar; } get { return this.showControlBar; }
set set
{ {
if (showControlBar != value) if (showControlBar != value)
{ {
this.showControlBar = value; this.showControlBar = value;
RequestResize(); RequestResize();
} }
} }
} }
   
#endregion #endregion
   
#region Methods: public #region Methods: public
   
/// <summary> /// <summary>
/// Request that the display stack's size is reset in the next draw call. /// Request that the display stack's size is reset in the next draw call.
/// </summary> /// </summary>
public void RequestResize() public void RequestResize()
{ {
this.resizeRequested = true; this.resizeRequested = true;
} }
   
#endregion #endregion
   
#region Methods: protected #region Methods: protected
   
/// <summary> /// <summary>
/// Sets the instance to this object. /// Sets the instance to this object.
/// </summary> /// </summary>
protected void Awake() protected void Awake()
{ {
try try
{ {
if (Instance == null) if (Instance == null)
{ {
Instance = this; Instance = this;
GuiDisplaySize.OnSizeChanged += this.OnSizeChanged; GuiDisplaySize.OnSizeChanged += this.OnSizeChanged;
Logger.Log("ActionMenu->Awake"); Logger.Log("ActionMenu->Awake");
} }
else else
{ {
Destroy(this); Destroy(this);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Runs when the object is destroyed. /// Runs when the object is destroyed.
/// </summary> /// </summary>
protected void OnDestroy() protected void OnDestroy()
{ {
try try
{ {
this.Save(); this.Save();
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
Logger.Log("ActionMenu->OnDestroy"); Logger.Log("ActionMenu->OnDestroy");
} }
   
/// <summary> /// <summary>
/// Initialises the object's state on creation. /// Initialises the object's state on creation.
/// </summary> /// </summary>
protected void Start() protected void Start()
{ {
try try
{ {
this.windowId = this.GetHashCode(); this.windowId = this.GetHashCode();
this.InitialiseStyles(); this.InitialiseStyles();
this.Load(); this.Load();
Logger.Log("ActionMenu->Start"); Logger.Log("ActionMenu->Start");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
protected void Update() protected void Update()
{ {
try try
{ {
if (!FlightEngineerCore.IsDisplayable) if (!FlightEngineerCore.IsDisplayable)
{ {
return; return;
} }
   
if (Input.GetKeyDown(KeyBinder.FlightShowHide)) if (Input.GetKeyDown(KeyBinder.FlightShowHide))
{ {
this.Hidden = !this.Hidden; this.Hidden = !this.Hidden;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
#endregion #endregion
   
#region Methods: private #region Methods: private
   
/// <summary> /// <summary>
/// Called to draw the display stack when the UI is enabled. /// Called to draw the display stack when the UI is enabled.
/// </summary> /// </summary>
private void OnGUI() private void OnGUI()
{ {
try try
{ {
if (!FlightEngineerCore.IsDisplayable) if (!FlightEngineerCore.IsDisplayable)
{ {
return; return;
} }
   
if (this.resizeRequested || this.numberOfStackSections != SectionLibrary.NumberOfStackSections) if (this.resizeRequested || this.numberOfStackSections != SectionLibrary.NumberOfStackSections)
{ {
this.numberOfStackSections = SectionLibrary.NumberOfStackSections; this.numberOfStackSections = SectionLibrary.NumberOfStackSections;
this.windowPosition.width = 0; this.windowPosition.width = 0;
this.windowPosition.height = 0; this.windowPosition.height = 0;
this.resizeRequested = false; this.resizeRequested = false;
} }
   
if (!this.Hidden && (SectionLibrary.NumberOfStackSections > 0 || this.ShowControlBar)) if (!this.Hidden && (SectionLibrary.NumberOfStackSections > 0 || this.ShowControlBar))
{ {
var shouldCentre = this.windowPosition.min == Vector2.zero; var shouldCentre = this.windowPosition.min == Vector2.zero;
GUI.skin = null; GUI.skin = null;
this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle).ClampToScreen(); this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle).ClampToScreen();
if (shouldCentre) if (shouldCentre)
{ {
this.windowPosition.center = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f); this.windowPosition.center = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Draws the control bar. /// Draws the control bar.
/// </summary> /// </summary>
private void DrawControlBar() private void DrawControlBar()
{ {
GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.ASSEMBLY_VERSION, this.titleStyle); GUILayout.Label("FLIGHT ENGINEER " + EngineerGlobals.ASSEMBLY_VERSION, this.titleStyle);
   
this.DrawControlBarButtons(SectionLibrary.StockSections); this.DrawControlBarButtons(SectionLibrary.StockSections);
this.DrawControlBarButtons(SectionLibrary.CustomSections); this.DrawControlBarButtons(SectionLibrary.CustomSections);
} }
   
/// <summary> /// <summary>
/// Draws a button list for a set of sections. /// Draws a button list for a set of sections.
/// </summary> /// </summary>
private void DrawControlBarButtons(IEnumerable<SectionModule> sections) private void DrawControlBarButtons(IEnumerable<SectionModule> sections)
{ {
var index = 0; var index = 0;
foreach (var section in sections.Where(s => !string.IsNullOrEmpty(s.Abbreviation) || !s.IsCustom)) foreach (var section in sections.Where(s => !string.IsNullOrEmpty(s.Abbreviation) || !s.IsCustom))
{ {
if (index % 4 == 0) if (index % 4 == 0)
{ {
if (index > 0) if (index > 0)
{ {
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
} }
section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle); section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Abbreviation.ToUpper(), this.buttonStyle);
index++; index++;
} }
if (index > 0) if (index > 0)
{ {
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
} }
   
/// <summary> /// <summary>
/// Draws a list of sections. /// Draws a list of sections.
/// </summary> /// </summary>
private void DrawSections(IEnumerable<SectionModule> sections) private void DrawSections(IEnumerable<SectionModule> sections)
{ {
foreach (var section in sections) foreach (var section in sections)
{ {
if (!section.IsFloating) if (!section.IsFloating)
{ {
section.Draw(); section.Draw();
} }
} }
} }
   
/// <summary> /// <summary>
/// Initialises all the styles required for this object. /// Initialises all the styles required for this object.
/// </summary> /// </summary>
private void InitialiseStyles() private void InitialiseStyles()
{ {
this.windowStyle = new GUIStyle(HighLogic.Skin.window) this.windowStyle = new GUIStyle(HighLogic.Skin.window)
{ {
margin = new RectOffset(), margin = new RectOffset(),
padding = new RectOffset(5, 5, 0, 5) padding = new RectOffset(5, 5, 0, 5)
}; };
   
this.titleStyle = new GUIStyle(HighLogic.Skin.label) this.titleStyle = new GUIStyle(HighLogic.Skin.label)
{ {
margin = new RectOffset(0, 0, 5, 3), margin = new RectOffset(0, 0, 5, 3),
padding = new RectOffset(), padding = new RectOffset(),
alignment = TextAnchor.MiddleCenter, alignment = TextAnchor.MiddleCenter,
fontSize = (int)(13 * GuiDisplaySize.Offset), fontSize = (int)(13 * GuiDisplaySize.Offset),
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
stretchWidth = true stretchWidth = true
}; };
   
this.buttonStyle = new GUIStyle(HighLogic.Skin.button) this.buttonStyle = new GUIStyle(HighLogic.Skin.button)
{ {
normal = normal =
{ {
textColor = Color.white textColor = Color.white
}, },
margin = new RectOffset(), margin = new RectOffset(),
padding = new RectOffset(), padding = new RectOffset(),
alignment = TextAnchor.MiddleCenter, alignment = TextAnchor.MiddleCenter,
fontSize = (int)(11 * GuiDisplaySize.Offset), fontSize = (int)(11 * GuiDisplaySize.Offset),
fontStyle = FontStyle.Bold, fontStyle = FontStyle.Bold,
fixedWidth = 60.0f * GuiDisplaySize.Offset, fixedWidth = 60.0f * GuiDisplaySize.Offset,
fixedHeight = 25.0f * GuiDisplaySize.Offset, fixedHeight = 25.0f * GuiDisplaySize.Offset,
}; };
} }
   
/// <summary> /// <summary>
/// Load the stack's state. /// Load the stack's state.
/// </summary> /// </summary>
private void Load() private void Load()
{ {
try try
{ {
var handler = SettingHandler.Load("DisplayStack.xml"); var handler = SettingHandler.Load("DisplayStack.xml");
this.Hidden = handler.Get("hidden", this.Hidden); this.Hidden = handler.Get("hidden", this.Hidden);
this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar); this.ShowControlBar = handler.Get("showControlBar", this.ShowControlBar);
this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x); this.windowPosition.x = handler.Get("windowPositionX", this.windowPosition.x);
this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y); this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex, "DisplayStack->Load"); Logger.Exception(ex, "DisplayStack->Load");
} }
} }
   
private void OnSizeChanged() private void OnSizeChanged()
{ {
this.InitialiseStyles(); this.InitialiseStyles();
this.RequestResize(); this.RequestResize();
} }
   
/// <summary> /// <summary>
/// Saves the stack's state. /// Saves the stack's state.
/// </summary> /// </summary>
private void Save() private void Save()
{ {
try try
{ {
var handler = new SettingHandler(); var handler = new SettingHandler();
handler.Set("hidden", this.Hidden); handler.Set("hidden", this.Hidden);
handler.Set("showControlBar", this.ShowControlBar); handler.Set("showControlBar", this.ShowControlBar);
handler.Set("windowPositionX", this.windowPosition.x); handler.Set("windowPositionX", this.windowPosition.x);
handler.Set("windowPositionY", this.windowPosition.y); handler.Set("windowPositionY", this.windowPosition.y);
handler.Save("DisplayStack.xml"); handler.Save("DisplayStack.xml");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex, "DisplayStack->Save"); Logger.Exception(ex, "DisplayStack->Save");
} }
} }
   
/// <summary> /// <summary>
/// Draws the display stack window. /// Draws the display stack window.
/// </summary> /// </summary>
private void Window(int windowId) private void Window(int windowId)
{ {
try try
{ {
if (this.ShowControlBar) if (this.ShowControlBar)
{ {
this.DrawControlBar(); this.DrawControlBar();
} }
   
if (SectionLibrary.NumberOfStackSections > 0) if (SectionLibrary.NumberOfStackSections > 0)
{ {
this.DrawSections(SectionLibrary.StockSections); this.DrawSections(SectionLibrary.StockSections);
this.DrawSections(SectionLibrary.CustomSections); this.DrawSections(SectionLibrary.CustomSections);
} }
   
GUI.DragWindow(); GUI.DragWindow();
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex, "DisplayStack->Widnow"); Logger.Exception(ex, "DisplayStack->Window");
} }
} }
   
#endregion #endregion
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2016 CYBUTEK // Copyright (C) 2016 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer.Flight namespace KerbalEngineer.Flight
{ {
using System.Collections.Generic; using System.Collections.Generic;
using KSP.UI; using KSP.UI;
using Sections; using Sections;
using Unity.Flight; using Unity.Flight;
using UnityEngine; using UnityEngine;
   
[KSPAddon(KSPAddon.Startup.Flight, false)] [KSPAddon(KSPAddon.Startup.Flight, false)]
public class FlightAppLauncher : AppLauncherButton, IFlightAppLauncher public class FlightAppLauncher : AppLauncherButton, IFlightAppLauncher
{ {
private static FlightAppLauncher s_Instance; private static FlightAppLauncher s_Instance;
private FlightMenu m_FlightMenu; private FlightMenu m_FlightMenu;
private GameObject m_MenuObject; private GameObject m_MenuObject;
private GameObject m_MenuPrefab; private GameObject m_MenuPrefab;
   
/// <summary> /// <summary>
/// Gets the current instance of the FlightAppLauncher object. /// Gets the current instance of the FlightAppLauncher object.
/// </summary> /// </summary>
public static FlightAppLauncher Instance public static FlightAppLauncher Instance
{ {
get get
{ {
return s_Instance; return s_Instance;
} }
} }
   
/// <summary> /// <summary>
/// Applies the KSP theme to a game object and its children. /// Applies the KSP theme to a game object and its children.
/// </summary> /// </summary>
public void ApplyTheme(GameObject gameObject) public void ApplyTheme(GameObject gameObject)
{ {
StyleManager.Process(gameObject); StyleManager.Process(gameObject);
} }
   
/// <summary> /// <summary>
/// Clamps the given rect transform within the screen bounds. /// Clamps the given rect transform within the screen bounds.
/// </summary> /// </summary>
public void ClampToScreen(RectTransform rectTransform) public void ClampToScreen(RectTransform rectTransform)
{ {
UIMasterController.ClampToScreen(rectTransform, Vector2.zero); UIMasterController.ClampToScreen(rectTransform, Vector2.zero);
} }
   
/// <summary> /// <summary>
/// Gets a list of custom sections. /// Gets a list of custom sections.
/// </summary> /// </summary>
IList<ISectionModule> IFlightAppLauncher.GetCustomSections() IList<ISectionModule> IFlightAppLauncher.GetCustomSections()
{ {
return new List<ISectionModule>(SectionLibrary.CustomSections.ToArray()); return new List<ISectionModule>(SectionLibrary.CustomSections.ToArray());
} }
   
/// <summary> /// <summary>
/// Gets a list of stock sections. /// Gets a list of stock sections.
/// </summary> /// </summary>
IList<ISectionModule> IFlightAppLauncher.GetStockSections() IList<ISectionModule> IFlightAppLauncher.GetStockSections()
{ {
return new List<ISectionModule>(SectionLibrary.StockSections.ToArray()); return new List<ISectionModule>(SectionLibrary.StockSections.ToArray());
} }
   
/// <summary> /// <summary>
/// Gets or sets the control bar's visibility. /// Gets or sets the control bar's visibility.
/// </summary> /// </summary>
public bool IsControlBarVisible public bool IsControlBarVisible
{ {
get get
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
return DisplayStack.Instance.ShowControlBar; return DisplayStack.Instance.ShowControlBar;
} }
   
return false; return false;
} }
set set
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
DisplayStack.Instance.ShowControlBar = value; DisplayStack.Instance.ShowControlBar = value;
} }
} }
} }
   
/// <summary> /// <summary>
/// Gets or sets the display stack's visibility. /// Gets or sets the display stack's visibility.
/// </summary> /// </summary>
public bool IsDisplayStackVisible public bool IsDisplayStackVisible
{ {
get get
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
return DisplayStack.Instance.Hidden == false; return DisplayStack.Instance.Hidden == false;
} }
   
return false; return false;
} }
set set
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
DisplayStack.Instance.Hidden = !value; DisplayStack.Instance.Hidden = !value;
} }
} }
} }
   
/// <summary> /// <summary>
/// Creates and initialises a new custom section. /// Creates and initialises a new custom section.
/// </summary> /// </summary>
public ISectionModule NewCustomSection() public ISectionModule NewCustomSection()
{ {
SectionModule section = new SectionModule SectionModule section = new SectionModule
{ {
Name = "Custom " + (SectionLibrary.CustomSections.Count + 1), Name = "Custom " + (SectionLibrary.CustomSections.Count + 1),
Abbreviation = "CUST " + (SectionLibrary.CustomSections.Count + 1), Abbreviation = "CUST " + (SectionLibrary.CustomSections.Count + 1),
IsVisible = true, IsVisible = true,
IsCustom = true, IsCustom = true,
IsEditorVisible = true IsEditorVisible = true
}; };
   
SectionLibrary.CustomSections.Add(section); SectionLibrary.CustomSections.Add(section);
   
return section; return section;
} }
   
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
   
// set singleton instance // set singleton instance
s_Instance = this; s_Instance = this;
   
// cache menu prefab // cache menu prefab
if (m_MenuPrefab == null && AssetBundleLoader.Prefabs != null) if (m_MenuPrefab == null && AssetBundleLoader.Prefabs != null)
{ {
m_MenuPrefab = AssetBundleLoader.Prefabs.LoadAsset<GameObject>("FlightMenu"); m_MenuPrefab = AssetBundleLoader.Prefabs.LoadAsset<GameObject>("FlightMenu");
} }
} }
   
protected override void OnFalse() protected override void OnFalse()
{ {
Close(); Close();
} }
   
protected override void OnHover() protected override void OnHover()
{ {
Open(); Open();
} }
   
protected override void OnHoverOut() protected override void OnHoverOut()
{ {
if (IsOn == false) if (IsOn == false)
{ {
Close(); Close();
} }
} }
   
protected override void OnTrue() protected override void OnTrue()
{ {
Open(); Open();
} }
   
protected virtual void Update() protected virtual void Update()
{ {
if (Button == null) if (FlightEngineerCore.IsDisplayable)
return;  
   
if (FlightEngineerCore.IsDisplayable && Button.IsEnabled == false)  
{ {
Enable(); Enable();
} }
else if (FlightEngineerCore.IsDisplayable == false && Button.IsEnabled) else if (FlightEngineerCore.IsDisplayable == false)
{ {
Disable(); Disable();
} }
} }
   
/// <summary> /// <summary>
/// Closes the menu. /// Closes the menu.
/// </summary> /// </summary>
private void Close() private void Close()
{ {
if (m_FlightMenu != null) if (m_FlightMenu != null)
{ {
m_FlightMenu.Close(); m_FlightMenu.Close();
} }
else if (m_MenuObject != null) else if (m_MenuObject != null)
{ {
Destroy(m_MenuObject); Destroy(m_MenuObject);
} }
} }
   
/// <summary> /// <summary>
/// Opens the menu. /// Opens the menu.
/// </summary> /// </summary>
private void Open() private void Open()
{ {
// fade menu in if already open // fade menu in if already open
if (m_FlightMenu != null) if (m_FlightMenu != null)
{ {
m_FlightMenu.FadeIn(); m_FlightMenu.FadeIn();
return; return;
} }
   
if (m_MenuPrefab == null || m_MenuObject != null) if (m_MenuPrefab == null || m_MenuObject != null)
{ {
return; return;
} }
   
// create object // create object
m_MenuObject = Instantiate(m_MenuPrefab, GetAnchor(), Quaternion.identity) as GameObject; m_MenuObject = Instantiate(m_MenuPrefab, GetAnchor(), Quaternion.identity) as GameObject;
if (m_MenuObject == null) if (m_MenuObject == null)
{ {
return; return;
} }
   
StyleManager.Process(m_MenuObject); StyleManager.Process(m_MenuObject);
   
// set object as a child of the main canvas // set object as a child of the main canvas
m_MenuObject.transform.SetParent(MainCanvasUtil.MainCanvas.transform); m_MenuObject.transform.SetParent(MainCanvasUtil.MainCanvas.transform);
   
// set menu's reference to this object for cross-communication // set menu's reference to this object for cross-communication
m_FlightMenu = m_MenuObject.GetComponent<FlightMenu>(); m_FlightMenu = m_MenuObject.GetComponent<FlightMenu>();
if (m_FlightMenu != null) if (m_FlightMenu != null)
{ {
m_FlightMenu.SetFlightAppLauncher(this); m_FlightMenu.SetFlightAppLauncher(this);
} }
} }
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
#endregion #endregion
   
namespace KerbalEngineer.Flight namespace KerbalEngineer.Flight
{ {
#region Using Directives #region Using Directives
   
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Extensions; using Extensions;
using Readouts; using Readouts;
using Sections; using Sections;
using Settings; using Settings;
using UnityEngine; using UnityEngine;
using VesselSimulator; using VesselSimulator;
   
#endregion #endregion
   
/// <summary> /// <summary>
/// Core management system for the Flight Engineer. /// Core management system for the Flight Engineer.
/// </summary> /// </summary>
[KSPAddon(KSPAddon.Startup.Flight, false)] [KSPAddon(KSPAddon.Startup.Flight, false)]
public sealed class FlightEngineerCore : MonoBehaviour public sealed class FlightEngineerCore : MonoBehaviour
{ {
#region Instance #region Instance
   
/// <summary> /// <summary>
/// Gets the current instance of FlightEngineerCore. /// Gets the current instance of FlightEngineerCore.
/// </summary> /// </summary>
public static FlightEngineerCore Instance { get; private set; } public static FlightEngineerCore Instance { get; private set; }
   
#endregion #endregion
   
#region Fields #region Fields
   
private static bool isCareerMode = true; private static bool isCareerMode = true;
private static bool isKerbalLimited = true; private static bool isKerbalLimited = true;
private static bool isTrackingStationLimited = true; private static bool isTrackingStationLimited = true;
   
#endregion #endregion
   
#region Constructors #region Constructors
   
static FlightEngineerCore() static FlightEngineerCore()
{ {
try try
{ {
var handler = SettingHandler.Load("FlightEngineerCore.xml"); var handler = SettingHandler.Load("FlightEngineerCore.xml");
handler.Get("isCareerMode", ref isCareerMode); handler.Get("isCareerMode", ref isCareerMode);
handler.Get("isKerbalLimited", ref isKerbalLimited); handler.Get("isKerbalLimited", ref isKerbalLimited);
handler.Get("isTrackingStationLimited", ref isTrackingStationLimited); handler.Get("isTrackingStationLimited", ref isTrackingStationLimited);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
#endregion #endregion
   
#region Properties #region Properties
   
/// <summary> /// <summary>
/// Gets and sets whether to the Flight Engineer should be run using career limitations. /// Gets and sets whether to the Flight Engineer should be run using career limitations.
/// </summary> /// </summary>
public static bool IsCareerMode public static bool IsCareerMode
{ {
get { return isCareerMode; } get { return isCareerMode; }
set set
{ {
try try
{ {
if (isCareerMode != value) if (isCareerMode != value)
{ {
var handler = SettingHandler.Load("FlightEngineerCore.xml"); var handler = SettingHandler.Load("FlightEngineerCore.xml");
handler.Set("isCareerMode", value); handler.Set("isCareerMode", value);
handler.Save("FlightEngineerCore.xml"); handler.Save("FlightEngineerCore.xml");
} }
isCareerMode = value; isCareerMode = value;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
} }
   
/// <summary> /// <summary>
/// Gets whether the FlightEngineer should be displayed. /// Gets whether the FlightEngineer should be displayed.
/// </summary> /// </summary>
public static bool IsDisplayable public static bool IsDisplayable
{ {
get get
{ {
if (MainCanvasUtil.MainCanvas.enabled == false) if (MainCanvasUtil.MainCanvas.enabled == false)
{ {
return false; return false;
} }
   
if (isCareerMode) if (isCareerMode)
{ {
if (isKerbalLimited && FlightGlobals.ActiveVessel.GetVesselCrew().Exists(c => c.experienceTrait.TypeName == "Engineer")) if (isKerbalLimited && FlightGlobals.ActiveVessel.GetVesselCrew().Exists(c => c.experienceTrait.TypeName == "Engineer"))
{ {
return true; return true;
} }
if (isTrackingStationLimited && ScenarioUpgradeableFacilities.GetFacilityLevel(SpaceCenterFacility.TrackingStation) == 1.0f) if (isTrackingStationLimited && ScenarioUpgradeableFacilities.GetFacilityLevel(SpaceCenterFacility.TrackingStation) == 1.0f)
{ {
return true; return true;
} }
return FlightGlobals.ActiveVessel.parts.Any(p => p.HasModule<FlightEngineerModule>()); return FlightGlobals.ActiveVessel.parts.Any(p => p.HasModule<FlightEngineerModule>());
} }
   
return true; return true;
} }
} }
   
/// <summary> /// <summary>
/// Gets and sets whether to the Flight Engineer should be kerbal limited. /// Gets and sets whether to the Flight Engineer should be kerbal limited.
/// </summary> /// </summary>
public static bool IsKerbalLimited public static bool IsKerbalLimited
{ {
get { return isKerbalLimited; } get { return isKerbalLimited; }
set set
{ {
try try
{ {
if (isKerbalLimited != value) if (isKerbalLimited != value)
{ {
var handler = SettingHandler.Load("FlightEngineerCore.xml"); var handler = SettingHandler.Load("FlightEngineerCore.xml");
handler.Set("isKerbalLimited", value); handler.Set("isKerbalLimited", value);
handler.Save("FlightEngineerCore.xml"); handler.Save("FlightEngineerCore.xml");
} }
isKerbalLimited = value; isKerbalLimited = value;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
} }
   
/// <summary> /// <summary>
/// Gets and sets whether to the Flight Engineer should be tracking station limited. /// Gets and sets whether to the Flight Engineer should be tracking station limited.
/// </summary> /// </summary>
public static bool IsTrackingStationLimited public static bool IsTrackingStationLimited
{ {
get { return isTrackingStationLimited; } get { return isTrackingStationLimited; }
set set
{ {
try try
{ {
if (isTrackingStationLimited != value) if (isTrackingStationLimited != value)
{ {
var handler = SettingHandler.Load("FlightEngineerCore.xml"); var handler = SettingHandler.Load("FlightEngineerCore.xml");
handler.Set("isTrackingStationLimited", value); handler.Set("isTrackingStationLimited", value);
handler.Save("FlightEngineerCore.xml"); handler.Save("FlightEngineerCore.xml");
} }
isTrackingStationLimited = value; isTrackingStationLimited = value;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
} }
   
/// <summary> /// <summary>
/// Gets the editor windows for sections with open editors. /// Gets the editor windows for sections with open editors.
/// </summary> /// </summary>
public List<SectionEditor> SectionEditors { get; private set; } public List<SectionEditor> SectionEditors { get; private set; }
   
/// <summary> /// <summary>
/// Gets the section windows for floating sections. /// Gets the section windows for floating sections.
/// </summary> /// </summary>
public List<SectionWindow> SectionWindows { get; private set; } public List<SectionWindow> SectionWindows { get; private set; }
   
/// <summary> /// <summary>
/// Gets the list of currently running updatable modules. /// Gets the list of currently running updatable modules.
/// </summary> /// </summary>
public List<IUpdatable> UpdatableModules { get; private set; } public List<IUpdatable> UpdatableModules { get; private set; }
   
#endregion #endregion
   
#region Methods #region Methods
   
/// <summary> /// <summary>
/// Creates a section editor, adds it to the FlightEngineerCore and returns a reference to it. /// Creates a section editor, adds it to the FlightEngineerCore and returns a reference to it.
/// </summary> /// </summary>
public SectionEditor AddSectionEditor(SectionModule section) public SectionEditor AddSectionEditor(SectionModule section)
{ {
try try
{ {
var editor = this.gameObject.AddComponent<SectionEditor>(); var editor = this.gameObject.AddComponent<SectionEditor>();
editor.ParentSection = section; editor.ParentSection = section;
editor.Position = new Rect(section.EditorPositionX, section.EditorPositionY, SectionEditor.Width, SectionEditor.Height); editor.Position = new Rect(section.EditorPositionX, section.EditorPositionY, SectionEditor.Width, SectionEditor.Height);
this.SectionEditors.Add(editor); this.SectionEditors.Add(editor);
return editor; return editor;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
return null; return null;
} }
} }
   
/// <summary> /// <summary>
/// Creates a section window, adds it to the FlightEngineerCore and returns a reference to it. /// Creates a section window, adds it to the FlightEngineerCore and returns a reference to it.
/// </summary> /// </summary>
public SectionWindow AddSectionWindow(SectionModule section) public SectionWindow AddSectionWindow(SectionModule section)
{ {
try try
{ {
var window = this.gameObject.AddComponent<SectionWindow>(); var window = this.gameObject.AddComponent<SectionWindow>();
window.ParentSection = section; window.ParentSection = section;
window.WindowPosition = new Rect(section.FloatingPositionX, section.FloatingPositionY, 0, 0); window.WindowPosition = new Rect(section.FloatingPositionX, section.FloatingPositionY, 0, 0);
this.SectionWindows.Add(window); this.SectionWindows.Add(window);
return window; return window;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
return null; return null;
} }
} }
   
/// <summary> /// <summary>
/// Adds an updatable object to be automatically updated every frame and will ignore duplicate objects. /// Adds an updatable object to be automatically updated every frame and will ignore duplicate objects.
/// </summary> /// </summary>
public void AddUpdatable(IUpdatable updatable) public void AddUpdatable(IUpdatable updatable)
{ {
try try
{ {
if (!this.UpdatableModules.Contains(updatable)) if (!this.UpdatableModules.Contains(updatable))
{ {
this.UpdatableModules.Add(updatable); this.UpdatableModules.Add(updatable);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Create base Flight Engineer child objects. /// Create base Flight Engineer child objects.
/// </summary> /// </summary>
private void Awake() private void Awake()
{ {
try try
{ {
Instance = this; Instance = this;
   
this.SectionWindows = new List<SectionWindow>(); this.SectionWindows = new List<SectionWindow>();
this.SectionEditors = new List<SectionEditor>(); this.SectionEditors = new List<SectionEditor>();
this.UpdatableModules = new List<IUpdatable>(); this.UpdatableModules = new List<IUpdatable>();
   
SimManager.UpdateModSettings(); SimManager.UpdateModSettings();
   
Logger.Log("FlightEngineerCore->Awake"); Logger.Log("FlightEngineerCore->Awake");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Fixed update all required Flight Engineer objects. /// Fixed update all required Flight Engineer objects.
/// </summary> /// </summary>
private void FixedUpdate() private void FixedUpdate()
{ {
if (FlightGlobals.ActiveVessel == null) if (FlightGlobals.ActiveVessel == null)
  {
return; return;
  }
   
try try
{ {
SectionLibrary.FixedUpdate(); SectionLibrary.FixedUpdate();
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Force the destruction of child objects on core destruction. /// Force the destruction of child objects on core destruction.
/// </summary> /// </summary>
private void OnDestroy() private void OnDestroy()
{ {
try try
{ {
SectionLibrary.Save(); SectionLibrary.Save();
   
foreach (var window in this.SectionWindows) foreach (var window in this.SectionWindows)
{ {
print("[FlightEngineer]: Destroying Floating Window for " + window.ParentSection.Name); print("[FlightEngineer]: Destroying Floating Window for " + window.ParentSection.Name);
Destroy(window); Destroy(window);
} }
   
foreach (var editor in this.SectionEditors) foreach (var editor in this.SectionEditors)
{ {
print("[FlightEngineer]: Destroying Editor Window for " + editor.ParentSection.Name); print("[FlightEngineer]: Destroying Editor Window for " + editor.ParentSection.Name);
Destroy(editor); Destroy(editor);
} }
   
Logger.Log("FlightEngineerCore->OnDestroy"); Logger.Log("FlightEngineerCore->OnDestroy");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Initialises the object's state on creation. /// Initialises the object's state on creation.
/// </summary> /// </summary>
private void Start() private void Start()
{ {
try try
{ {
SectionLibrary.Load(); SectionLibrary.Load();
ReadoutLibrary.Reset(); ReadoutLibrary.Reset();
Logger.Log("FlightEngineerCore->Start"); Logger.Log("FlightEngineerCore->Start");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Update all required Flight Engineer objects. /// Update all required Flight Engineer objects.
/// </summary> /// </summary>
private void Update() private void Update()
{ {
if (FlightGlobals.ActiveVessel == null) if (FlightGlobals.ActiveVessel == null)
  {
return; return;
  }
   
try try
{ {
SectionLibrary.Update(); SectionLibrary.Update();
this.UpdateModules(); this.UpdateModules();
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
/// <summary> /// <summary>
/// Update all updatable modules. /// Update all updatable modules.
/// </summary> /// </summary>
private void UpdateModules() private void UpdateModules()
{ {
try try
{ {
foreach (var updatable in this.UpdatableModules) foreach (var updatable in this.UpdatableModules)
{ {
if (updatable is IUpdateRequest) if (updatable is IUpdateRequest)
{ {
var request = updatable as IUpdateRequest; var request = updatable as IUpdateRequest;
if (request.UpdateRequested) if (request.UpdateRequested)
{ {
updatable.Update(); updatable.Update();
request.UpdateRequested = false; request.UpdateRequested = false;
} }
} }
else else
{ {
updatable.Update(); updatable.Update();
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Exception(ex); Logger.Exception(ex);
} }
} }
   
#endregion #endregion
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
using System; using System;
   
using KerbalEngineer.Extensions; using KerbalEngineer.Extensions;
  using KerbalEngineer.Helpers;
using KerbalEngineer.Flight.Sections; using KerbalEngineer.Flight.Sections;
   
#endregion #endregion
   
namespace KerbalEngineer.Flight.Readouts.Orbital namespace KerbalEngineer.Flight.Readouts.Orbital
{ {
public class TrueAnomaly : ReadoutModule public class TrueAnomaly : ReadoutModule
{ {
#region Constructors #region Constructors
   
public TrueAnomaly() public TrueAnomaly()
{ {
this.Name = "True Anomaly"; this.Name = "True Anomaly";
this.Category = ReadoutCategory.GetCategory("Orbital"); this.Category = ReadoutCategory.GetCategory("Orbital");
this.HelpString = String.Empty; this.HelpString = String.Empty;
this.IsDefault = false; this.IsDefault = false;
} }
   
#endregion #endregion
   
#region Methods: public #region Methods: public
   
public override void Draw(SectionModule section) public override void Draw(SectionModule section)
{ {
this.DrawLine(FlightGlobals.ship_orbit.trueAnomaly.ToAngle(), section.IsHud); this.DrawLine((FlightGlobals.ship_orbit.trueAnomaly * Units.RAD_TO_DEG).ToAngle(), section.IsHud);
} }
   
#endregion #endregion
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
using System; using System;
   
using UnityEngine; using UnityEngine;
  using KerbalEngineer.Helpers;
   
#endregion #endregion
   
// The calculations and functional code in this processor were generously developed by mic_e. // The calculations and functional code in this processor were generously developed by mic_e.
   
namespace KerbalEngineer.Flight.Readouts.Surface namespace KerbalEngineer.Flight.Readouts.Surface
{ {
public class ImpactProcessor : IUpdatable, IUpdateRequest public class ImpactProcessor : IUpdatable, IUpdateRequest
{ {
#region Instance #region Instance
   
#region Fields #region Fields
   
private static readonly ImpactProcessor instance = new ImpactProcessor(); private static readonly ImpactProcessor instance = new ImpactProcessor();
   
#endregion #endregion
   
#region Properties #region Properties
   
/// <summary> /// <summary>
/// Gets the current instance of the impact processor. /// Gets the current instance of the impact processor.
/// </summary> /// </summary>
public static ImpactProcessor Instance public static ImpactProcessor Instance
{ {
get { return instance; } get { return instance; }
} }
   
#endregion #endregion
   
#endregion #endregion
   
#region Fields #region Fields
   
private double impactAltitude; private double impactAltitude;
private bool impactHappening; private bool impactHappening;
private double impactLatitude; private double impactLatitude;
private double impactLongitude; private double impactLongitude;
private double impactTime; private double impactTime;
   
#endregion #endregion
   
#region Properties #region Properties
   
/// <summary> /// <summary>
/// Gets the altitude of the impact coordinates. /// Gets the altitude of the impact coordinates.
/// </summary> /// </summary>
public static double Altitude { get; private set; } public static double Altitude { get; private set; }
   
/// <summary> /// <summary>
/// Gets the biome of the impact coordinates. /// Gets the biome of the impact coordinates.
/// </summary> /// </summary>
public static string Biome { get; private set; } public static string Biome { get; private set; }
   
/// <summary> /// <summary>
/// Gets the latitude of the impact coordinates. /// Gets the latitude of the impact coordinates.
/// </summary> /// </summary>
public static double Latitude { get; private set; } public static double Latitude { get; private set; }
   
/// <summary> /// <summary>
/// Gets the longitude of the impact coordinates. /// Gets the longitude of the impact coordinates.
/// </summary> /// </summary>
public static double Longitude { get; private set; } public static double Longitude { get; private set; }
   
/// <summary> /// <summary>
/// Gets whether the details are ready to be shown. /// Gets whether the details are ready to be shown.
/// </summary> /// </summary>
public static bool ShowDetails { get; private set; } public static bool ShowDetails { get; private set; }
   
/// <summary> /// <summary>
/// Gets the time to impact. /// Gets the time to impact.
/// </summary> /// </summary>
public static double Time { get; private set; } public static double Time { get; private set; }
   
#endregion #endregion
   
#region IUpdatable Members #region IUpdatable Members
   
public void Update() public void Update()
{ {
this.impactHappening = false; this.impactHappening = false;
   
if (FlightGlobals.ActiveVessel.mainBody.pqsController != null) if (FlightGlobals.ActiveVessel.mainBody.pqsController != null)
{ {
//do impact site calculations //do impact site calculations
this.impactHappening = true; this.impactHappening = true;
this.impactTime = 0; this.impactTime = 0;
this.impactLongitude = 0; this.impactLongitude = 0;
this.impactLatitude = 0; this.impactLatitude = 0;
this.impactAltitude = 0; this.impactAltitude = 0;
var e = FlightGlobals.ActiveVessel.orbit.eccentricity; var e = FlightGlobals.ActiveVessel.orbit.eccentricity;
//get current position direction vector //get current position direction vector
var currentpos = this.RadiusDirection(FlightGlobals.ActiveVessel.orbit.trueAnomaly * 180.0 / Math.PI); var currentpos = this.RadiusDirection(FlightGlobals.ActiveVessel.orbit.trueAnomaly * Units.RAD_TO_DEG);
//calculate longitude in inertial reference frame from that //calculate longitude in inertial reference frame from that
var currentirflong = 180 * Math.Atan2(currentpos.x, currentpos.y) / Math.PI; var currentirflong = 180 * Math.Atan2(currentpos.x, currentpos.y) / Math.PI;
   
//experimentally determined; even for very flat trajectories, the errors go into the sub-millimeter area after 5 iterations or so //experimentally determined; even for very flat trajectories, the errors go into the sub-millimeter area after 5 iterations or so
const int impactiterations = 6; const int impactiterations = 6;
   
//do a few iterations of impact site calculations //do a few iterations of impact site calculations
for (var i = 0; i < impactiterations; i++) for (var i = 0; i < impactiterations; i++)
{ {
if (FlightGlobals.ActiveVessel.orbit.PeA >= this.impactAltitude) if (FlightGlobals.ActiveVessel.orbit.PeA >= this.impactAltitude)
{ {
//periapsis must be lower than impact alt //periapsis must be lower than impact alt
this.impactHappening = false; this.impactHappening = false;
} }
if ((FlightGlobals.ActiveVessel.orbit.eccentricity < 1) && (FlightGlobals.ActiveVessel.orbit.ApA <= this.impactAltitude)) if ((FlightGlobals.ActiveVessel.orbit.eccentricity < 1) && (FlightGlobals.ActiveVessel.orbit.ApA <= this.impactAltitude))
{ {
//apoapsis must be higher than impact alt //apoapsis must be higher than impact alt
this.impactHappening = false; this.impactHappening = false;
} }
if ((FlightGlobals.ActiveVessel.orbit.eccentricity >= 1) && (FlightGlobals.ActiveVessel.orbit.timeToPe <= 0)) if ((FlightGlobals.ActiveVessel.orbit.eccentricity >= 1) && (FlightGlobals.ActiveVessel.orbit.timeToPe <= 0))
{ {
//if currently escaping, we still need to be before periapsis //if currently escaping, we still need to be before periapsis
this.impactHappening = false; this.impactHappening = false;
} }
if (!this.impactHappening) if (!this.impactHappening)
{ {
this.impactTime = 0; this.impactTime = 0;
this.impactLongitude = 0; this.impactLongitude = 0;
this.impactLatitude = 0; this.impactLatitude = 0;
this.impactAltitude = 0; this.impactAltitude = 0;
break; break;
} }
   
double impacttheta = 0; double impacttheta = 0;
if (e > 0) if (e > 0)
{ {
//in this step, we are using the calculated impact altitude of the last step, to refine the impact site position //in this step, we are using the calculated impact altitude of the last step, to refine the impact site position
impacttheta = -180 * Math.Acos((FlightGlobals.ActiveVessel.orbit.PeR * (1 + e) / (FlightGlobals.ActiveVessel.mainBody.Radius + this.impactAltitude) - 1) / e) / Math.PI; impacttheta = -180 * Math.Acos((FlightGlobals.ActiveVessel.orbit.PeR * (1 + e) / (FlightGlobals.ActiveVessel.mainBody.Radius + this.impactAltitude) - 1) / e) / Math.PI;
} }
   
//calculate time to impact //calculate time to impact
this.impactTime = FlightGlobals.ActiveVessel.orbit.timeToPe - this.TimeToPeriapsis(impacttheta); this.impactTime = FlightGlobals.ActiveVessel.orbit.timeToPe - this.TimeToPeriapsis(impacttheta);
//calculate position vector of impact site //calculate position vector of impact site
var impactpos = this.RadiusDirection(impacttheta); var impactpos = this.RadiusDirection(impacttheta);
//calculate longitude of impact site in inertial reference frame //calculate longitude of impact site in inertial reference frame
var impactirflong = 180 * Math.Atan2(impactpos.x, impactpos.y) / Math.PI; var impactirflong = 180 * Math.Atan2(impactpos.x, impactpos.y) / Math.PI;
var deltairflong = impactirflong - currentirflong; var deltairflong = impactirflong - currentirflong;
//get body rotation until impact //get body rotation until impact
var bodyrot = 360 * this.impactTime / FlightGlobals.ActiveVessel.mainBody.rotationPeriod; var bodyrot = 360 * this.impactTime / FlightGlobals.ActiveVessel.mainBody.rotationPeriod;
//get current longitude in body coordinates //get current longitude in body coordinates
var currentlong = FlightGlobals.ActiveVessel.longitude; var currentlong = FlightGlobals.ActiveVessel.longitude;
//finally, calculate the impact longitude in body coordinates //finally, calculate the impact longitude in body coordinates
this.impactLongitude = this.NormAngle(currentlong - deltairflong - bodyrot); this.impactLongitude = this.NormAngle(currentlong - deltairflong - bodyrot);
//calculate impact latitude from impact position //calculate impact latitude from impact position
this.impactLatitude = 180 * Math.Asin(impactpos.z / impactpos.magnitude) / Math.PI; this.impactLatitude = 180 * Math.Asin(impactpos.z / impactpos.magnitude) / Math.PI;
//calculate the actual altitude of the impact site //calculate the actual altitude of the impact site
//altitude for long/lat code stolen from some ISA MapSat forum post; who knows why this works, but it seems to. //altitude for long/lat code stolen from some ISA MapSat forum post; who knows why this works, but it seems to.
var rad = QuaternionD.AngleAxis(this.impactLongitude, Vector3d.down) * QuaternionD.AngleAxis(this.impactLatitude, Vector3d.forward) * Vector3d.right; var rad = QuaternionD.AngleAxis(this.impactLongitude, Vector3d.down) * QuaternionD.AngleAxis(this.impactLatitude, Vector3d.forward) * Vector3d.right;
this.impactAltitude = FlightGlobals.ActiveVessel.mainBody.pqsController.GetSurfaceHeight(rad) - FlightGlobals.ActiveVessel.mainBody.pqsController.radius; this.impactAltitude = FlightGlobals.ActiveVessel.mainBody.pqsController.GetSurfaceHeight(rad) - FlightGlobals.ActiveVessel.mainBody.pqsController.radius;
if ((this.impactAltitude < 0) && FlightGlobals.ActiveVessel.mainBody.ocean) if ((this.impactAltitude < 0) && FlightGlobals.ActiveVessel.mainBody.ocean)
{ {
this.impactAltitude = 0; this.impactAltitude = 0;
} }
} }
} }
   
// Set accessable properties. // Set accessable properties.
if (this.impactHappening) if (this.impactHappening)
{ {
ShowDetails = true; ShowDetails = true;
Time = this.impactTime; Time = this.impactTime;
Longitude = this.impactLongitude; Longitude = this.impactLongitude;
Latitude = this.impactLatitude; Latitude = this.impactLatitude;
Altitude = this.impactAltitude; Altitude = this.impactAltitude;
Biome = ScienceUtil.GetExperimentBiome(FlightGlobals.ActiveVessel.mainBody, this.impactLatitude, this.impactLongitude); Biome = ScienceUtil.GetExperimentBiome(FlightGlobals.ActiveVessel.mainBody, this.impactLatitude, this.impactLongitude);
} }
else else
{ {
ShowDetails = false; ShowDetails = false;
} }
} }
   
#endregion #endregion
   
#region IUpdateRequest Members #region IUpdateRequest Members
   
public bool UpdateRequested { get; set; } public bool UpdateRequested { get; set; }
   
#endregion #endregion
   
#region Methods: public #region Methods: public
   
public static void RequestUpdate() public static void RequestUpdate()
{ {
instance.UpdateRequested = true; instance.UpdateRequested = true;
} }
   
#endregion #endregion
   
#region Calculations #region Calculations
   
#region Methods: public #region Methods: public
   
public static double ACosh(double x) public static double ACosh(double x)
{ {
return (Math.Log(x + Math.Sqrt((x * x) - 1.0))); return (Math.Log(x + Math.Sqrt((x * x) - 1.0)));
} }
   
#endregion #endregion
   
#region Methods: private #region Methods: private
   
private double NormAngle(double ang) private double NormAngle(double ang)
{ {
if (ang > 180) if (ang > 180)
{ {
ang -= 360 * Math.Ceiling((ang - 180) / 360); ang -= 360 * Math.Ceiling((ang - 180) / 360);
} }
if (ang <= -180) if (ang <= -180)
{ {
ang -= 360 * Math.Floor((ang + 180) / 360); ang -= 360 * Math.Floor((ang + 180) / 360);
} }
   
return ang; return ang;
} }
   
private Vector3d RadiusDirection(double theta) private Vector3d RadiusDirection(double theta)
{ {
theta = Math.PI * theta / 180; theta = Math.PI * theta / 180;
var omega = Math.PI * FlightGlobals.ActiveVessel.orbit.argumentOfPeriapsis / 180; var omega = Math.PI * FlightGlobals.ActiveVessel.orbit.argumentOfPeriapsis / 180;
var incl = Math.PI * FlightGlobals.ActiveVessel.orbit.inclination / 180; var incl = Math.PI * FlightGlobals.ActiveVessel.orbit.inclination / 180;
   
var costheta = Math.Cos(theta); var costheta = Math.Cos(theta);
var sintheta = Math.Sin(theta); var sintheta = Math.Sin(theta);
var cosomega = Math.Cos(omega); var cosomega = Math.Cos(omega);
var sinomega = Math.Sin(omega); var sinomega = Math.Sin(omega);
var cosincl = Math.Cos(incl); var cosincl = Math.Cos(incl);
var sinincl = Math.Sin(incl); var sinincl = Math.Sin(incl);
   
Vector3d result; Vector3d result;
   
result.x = cosomega * costheta - sinomega * sintheta; result.x = cosomega * costheta - sinomega * sintheta;
result.y = cosincl * (sinomega * costheta + cosomega * sintheta); result.y = cosincl * (sinomega * costheta + cosomega * sintheta);
result.z = sinincl * (sinomega * costheta + cosomega * sintheta); result.z = sinincl * (sinomega * costheta + cosomega * sintheta);
   
return result; return result;
} }
   
private double TimeToPeriapsis(double theta) private double TimeToPeriapsis(double theta)
{ {
var e = FlightGlobals.ActiveVessel.orbit.eccentricity; var e = FlightGlobals.ActiveVessel.orbit.eccentricity;
var a = FlightGlobals.ActiveVessel.orbit.semiMajorAxis; var a = FlightGlobals.ActiveVessel.orbit.semiMajorAxis;
var rp = FlightGlobals.ActiveVessel.orbit.PeR; var rp = FlightGlobals.ActiveVessel.orbit.PeR;
var mu = FlightGlobals.ActiveVessel.mainBody.gravParameter; var mu = FlightGlobals.ActiveVessel.mainBody.gravParameter;
   
if (e == 1.0) if (e == 1.0)
{ {
var D = Math.Tan(Math.PI * theta / 360.0); var D = Math.Tan(Math.PI * theta / 360.0);
var M = D + D * D * D / 3.0; var M = D + D * D * D / 3.0;
return (Math.Sqrt(2.0 * rp * rp * rp / mu) * M); return (Math.Sqrt(2.0 * rp * rp * rp / mu) * M);
} }
if (a > 0) if (a > 0)
{ {
var cosTheta = Math.Cos(Math.PI * theta / 180.0); var cosTheta = Math.Cos(Math.PI * theta / 180.0);
var cosE = (e + cosTheta) / (1.0 + e * cosTheta); var cosE = (e + cosTheta) / (1.0 + e * cosTheta);
var radE = Math.Acos(cosE); var radE = Math.Acos(cosE);
var M = radE - e * Math.Sin(radE); var M = radE - e * Math.Sin(radE);
return (Math.Sqrt(a * a * a / mu) * M); return (Math.Sqrt(a * a * a / mu) * M);
} }
if (a < 0) if (a < 0)
{ {
var cosTheta = Math.Cos(Math.PI * theta / 180.0); var cosTheta = Math.Cos(Math.PI * theta / 180.0);
var coshF = (e + cosTheta) / (1.0 + e * cosTheta); var coshF = (e + cosTheta) / (1.0 + e * cosTheta);
var radF = ACosh(coshF); var radF = ACosh(coshF);
var M = e * Math.Sinh(radF) - radF; var M = e * Math.Sinh(radF) - radF;
return (Math.Sqrt(-a * a * a / mu) * M); return (Math.Sqrt(-a * a * a / mu) * M);
} }
   
return 0; return 0;
} }
   
#endregion #endregion
   
#endregion #endregion
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2016 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives  
   
using System.Linq;  
   
using KerbalEngineer.Flight.Sections;  
using KerbalEngineer.Helpers;  
   
#endregion  
   
namespace KerbalEngineer.Flight.Readouts.Vessel namespace KerbalEngineer.Flight.Readouts.Vessel
{ {
  using System.Linq;
  using Helpers;
using KSP.UI.Screens; using KSP.UI.Screens;
  using Sections;
  using VesselSimulator;
   
public class DeltaVStaged : ReadoutModule public class DeltaVStaged : ReadoutModule
{ {
#region Constructors  
   
public DeltaVStaged() public DeltaVStaged()
{ {
this.Name = "DeltaV Staged"; Name = "DeltaV Staged";
this.Category = ReadoutCategory.GetCategory("Vessel"); Category = ReadoutCategory.GetCategory("Vessel");
this.HelpString = "Shows the vessel's delta velocity for each stage."; HelpString = "Shows the vessel's delta velocity for each stage.";
this.IsDefault = true; IsDefault = true;
} }
   
#endregion  
   
#region Methods: public  
   
public override void Draw(SectionModule section) public override void Draw(SectionModule section)
{ {
if (!SimulationProcessor.ShowDetails) if (SimulationProcessor.ShowDetails == false || StageManager.Instance == null)
{ {
return; return;
} }
   
foreach (var stage in SimulationProcessor.Stages.Where(stage => stage.deltaV > 0 || stage.number == StageManager.CurrentStage)) foreach (Stage stage in SimulationProcessor.Stages.Where(stage => stage.deltaV > 0 || stage.number == StageManager.CurrentStage))
{ {
this.DrawLine("DeltaV (S" + stage.number + ")", stage.deltaV.ToString("N0") + "m/s (" + TimeFormatter.ConvertToString(stage.time) + ")", section.IsHud); DrawLine("DeltaV (S" + stage.number + ")", stage.deltaV.ToString("N0") + "m/s (" + TimeFormatter.ConvertToString(stage.time) + ")", section.IsHud);
} }
} }
   
public override void Reset() public override void Reset()
{ {
FlightEngineerCore.Instance.AddUpdatable(SimulationProcessor.Instance); FlightEngineerCore.Instance.AddUpdatable(SimulationProcessor.Instance);
} }
   
public override void Update() public override void Update()
{ {
SimulationProcessor.RequestUpdate(); SimulationProcessor.RequestUpdate();
} }
   
#endregion  
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer.VesselSimulator namespace KerbalEngineer.VesselSimulator
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Editor; using Editor;
using Helpers; using Helpers;
using UnityEngine; using UnityEngine;
   
public class EngineSim public class EngineSim
{ {
private static readonly Pool<EngineSim> pool = new Pool<EngineSim>(Create, Reset); private static readonly Pool<EngineSim> pool = new Pool<EngineSim>(Create, Reset);
   
private readonly ResourceContainer resourceConsumptions = new ResourceContainer(); private readonly ResourceContainer resourceConsumptions = new ResourceContainer();
private readonly ResourceContainer resourceFlowModes = new ResourceContainer(); private readonly ResourceContainer resourceFlowModes = new ResourceContainer();
   
public double actualThrust = 0; public double actualThrust = 0;
public bool isActive = false; public bool isActive = false;
public double isp = 0; public double isp = 0;
public PartSim partSim; public PartSim partSim;
public List<AppliedForce> appliedForces = new List<AppliedForce>(); public List<AppliedForce> appliedForces = new List<AppliedForce>();
public float maxMach; public float maxMach;
  public bool isFlamedOut;
   
public double thrust = 0; public double thrust = 0;
   
// Add thrust vector to account for directional losses // Add thrust vector to account for directional losses
public Vector3 thrustVec; public Vector3 thrustVec;
   
private static EngineSim Create() private static EngineSim Create()
{ {
return new EngineSim(); return new EngineSim();
} }
   
private static void Reset(EngineSim engineSim) private static void Reset(EngineSim engineSim)
{ {
engineSim.resourceConsumptions.Reset(); engineSim.resourceConsumptions.Reset();
engineSim.resourceFlowModes.Reset(); engineSim.resourceFlowModes.Reset();
engineSim.partSim = null; engineSim.partSim = null;
engineSim.actualThrust = 0; engineSim.actualThrust = 0;
engineSim.isActive = false; engineSim.isActive = false;
engineSim.isp = 0; engineSim.isp = 0;
for (int i = 0; i < engineSim.appliedForces.Count; i++) for (int i = 0; i < engineSim.appliedForces.Count; i++)
{ {
engineSim.appliedForces[i].Release(); engineSim.appliedForces[i].Release();
} }
engineSim.appliedForces.Clear(); engineSim.appliedForces.Clear();
engineSim.thrust = 0; engineSim.thrust = 0;
engineSim.maxMach = 0f; engineSim.maxMach = 0f;
  engineSim.isFlamedOut = false;
} }
   
public void Release() public void Release()
{ {
pool.Release(this); pool.Release(this);
} }
   
public static EngineSim New(PartSim theEngine, public static EngineSim New(PartSim theEngine,
ModuleEngines engineMod, ModuleEngines engineMod,
double atmosphere, double atmosphere,
float machNumber, float machNumber,
bool vectoredThrust, bool vectoredThrust,
bool fullThrust, bool fullThrust,
LogMsg log) LogMsg log)
{ {
float maxFuelFlow = engineMod.maxFuelFlow; float maxFuelFlow = engineMod.maxFuelFlow;
float minFuelFlow = engineMod.minFuelFlow; float minFuelFlow = engineMod.minFuelFlow;
float thrustPercentage = engineMod.thrustPercentage; float thrustPercentage = engineMod.thrustPercentage;
List<Transform> thrustTransforms = engineMod.thrustTransforms; List<Transform> thrustTransforms = engineMod.thrustTransforms;
List<float> thrustTransformMultipliers = engineMod.thrustTransformMultipliers; List<float> thrustTransformMultipliers = engineMod.thrustTransformMultipliers;
Vector3 vecThrust = CalculateThrustVector(vectoredThrust ? thrustTransforms : null, Vector3 vecThrust = CalculateThrustVector(vectoredThrust ? thrustTransforms : null,
vectoredThrust ? thrustTransformMultipliers : null, vectoredThrust ? thrustTransformMultipliers : null,
log); log);
FloatCurve atmosphereCurve = engineMod.atmosphereCurve; FloatCurve atmosphereCurve = engineMod.atmosphereCurve;
bool atmChangeFlow = engineMod.atmChangeFlow; bool atmChangeFlow = engineMod.atmChangeFlow;
FloatCurve atmCurve = engineMod.useAtmCurve ? engineMod.atmCurve : null; FloatCurve atmCurve = engineMod.useAtmCurve ? engineMod.atmCurve : null;
FloatCurve velCurve = engineMod.useVelCurve ? engineMod.velCurve : null; FloatCurve velCurve = engineMod.useVelCurve ? engineMod.velCurve : null;
float currentThrottle = engineMod.currentThrottle; float currentThrottle = engineMod.currentThrottle;
float IspG = engineMod.g; float IspG = engineMod.g;
bool throttleLocked = engineMod.throttleLocked || fullThrust; bool throttleLocked = engineMod.throttleLocked || fullThrust;
List<Propellant> propellants = engineMod.propellants; List<Propellant> propellants = engineMod.propellants;
bool active = engineMod.isOperational; bool active = engineMod.isOperational;
float resultingThrust = engineMod.resultingThrust; float resultingThrust = engineMod.resultingThrust;
  bool isFlamedOut = engineMod.flameout;
EngineSim engineSim = pool.Borrow(); EngineSim engineSim = pool.Borrow();
   
engineSim.isp = 0.0; engineSim.isp = 0.0;
engineSim.maxMach = 0.0f; engineSim.maxMach = 0.0f;
engineSim.actualThrust = 0.0; engineSim.actualThrust = 0.0;
engineSim.partSim = theEngine; engineSim.partSim = theEngine;
engineSim.isActive = active; engineSim.isActive = active;
engineSim.thrustVec = vecThrust; engineSim.thrustVec = vecThrust;
  engineSim.isFlamedOut = isFlamedOut;
engineSim.resourceConsumptions.Reset(); engineSim.resourceConsumptions.Reset();
engineSim.resourceFlowModes.Reset(); engineSim.resourceFlowModes.Reset();
engineSim.appliedForces.Clear(); engineSim.appliedForces.Clear();
   
double flowRate = 0.0; double flowRate = 0.0;
if (engineSim.partSim.hasVessel) if (engineSim.partSim.hasVessel)
{ {
if (log != null) log.buf.AppendLine("hasVessel is true"); if (log != null) log.buf.AppendLine("hasVessel is true");
   
float flowModifier = GetFlowModifier(atmChangeFlow, atmCurve, engineSim.partSim.part.atmDensity, velCurve, machNumber, ref engineSim.maxMach); float flowModifier = GetFlowModifier(atmChangeFlow, atmCurve, engineSim.partSim.part.atmDensity, velCurve, machNumber, ref engineSim.maxMach);
engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere); engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere);
engineSim.thrust = GetThrust(Mathf.Lerp(minFuelFlow, maxFuelFlow, GetThrustPercent(thrustPercentage)) * flowModifier, engineSim.isp); engineSim.thrust = GetThrust(Mathf.Lerp(minFuelFlow, maxFuelFlow, GetThrustPercent(thrustPercentage)) * flowModifier, engineSim.isp);
engineSim.actualThrust = engineSim.isActive ? resultingThrust : 0.0; engineSim.actualThrust = engineSim.isActive ? resultingThrust : 0.0;
if (log != null) if (log != null)
{ {
log.buf.AppendFormat("flowMod = {0:g6}\n", flowModifier); log.buf.AppendFormat("flowMod = {0:g6}\n", flowModifier);
log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp); log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp);
log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust); log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust);
log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust); log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust);
} }
   
if (throttleLocked) if (throttleLocked)
{ {
if (log != null) log.buf.AppendLine("throttleLocked is true, using thrust for flowRate"); if (log != null) log.buf.AppendLine("throttleLocked is true, using thrust for flowRate");
flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); flowRate = GetFlowRate(engineSim.thrust, engineSim.isp);
} }
else else
{ {
if (currentThrottle > 0.0f && engineSim.partSim.isLanded == false) if (currentThrottle > 0.0f && engineSim.partSim.isLanded == false)
{ {
// TODO: This bit doesn't work for RF engines // TODO: This bit doesn't work for RF engines
if (log != null) log.buf.AppendLine("throttled up and not landed, using actualThrust for flowRate"); if (log != null) log.buf.AppendLine("throttled up and not landed, using actualThrust for flowRate");
flowRate = GetFlowRate(engineSim.actualThrust, engineSim.isp); flowRate = GetFlowRate(engineSim.actualThrust, engineSim.isp);
} }
else else
{ {
if (log != null) log.buf.AppendLine("throttled down or landed, using thrust for flowRate"); if (log != null) log.buf.AppendLine("throttled down or landed, using thrust for flowRate");
flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); flowRate = GetFlowRate(engineSim.thrust, engineSim.isp);
} }
} }
} }
else else
{ {
if (log != null) log.buf.AppendLine("hasVessel is false"); if (log != null) log.buf.AppendLine("hasVessel is false");
float flowModifier = GetFlowModifier(atmChangeFlow, atmCurve, CelestialBodies.SelectedBody.GetDensity(BuildAdvanced.Altitude), velCurve, machNumber, ref engineSim.maxMach); float flowModifier = GetFlowModifier(atmChangeFlow, atmCurve, CelestialBodies.SelectedBody.GetDensity(BuildAdvanced.Altitude), velCurve, machNumber, ref engineSim.maxMach);
engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere); engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere);
engineSim.thrust = GetThrust(Mathf.Lerp(minFuelFlow, maxFuelFlow, GetThrustPercent(thrustPercentage)) * flowModifier, engineSim.isp); engineSim.thrust = GetThrust(Mathf.Lerp(minFuelFlow, maxFuelFlow, GetThrustPercent(thrustPercentage)) * flowModifier, engineSim.isp);
engineSim.actualThrust = 0d; engineSim.actualThrust = 0d;
if (log != null) if (log != null)
{ {
log.buf.AppendFormat("flowMod = {0:g6}\n", flowModifier); log.buf.AppendFormat("flowMod = {0:g6}\n", flowModifier);
log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp); log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp);
log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust); log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust);
log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust); log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust);
} }
   
if (log != null) log.buf.AppendLine("no vessel, using thrust for flowRate"); if (log != null) log.buf.AppendLine("no vessel, using thrust for flowRate");
flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); flowRate = GetFlowRate(engineSim.thrust, engineSim.isp);
} }
   
if (log != null) log.buf.AppendFormat("flowRate = {0:g6}\n", flowRate); if (log != null) log.buf.AppendFormat("flowRate = {0:g6}\n", flowRate);
   
float flowMass = 0f; float flowMass = 0f;
for (int i = 0; i < propellants.Count; ++i) for (int i = 0; i < propellants.Count; ++i)
{ {
Propellant propellant = propellants[i]; Propellant propellant = propellants[i];
if (!propellant.ignoreForIsp) if (!propellant.ignoreForIsp)
flowMass += propellant.ratio * ResourceContainer.GetResourceDensity(propellant.id); flowMass += propellant.ratio * ResourceContainer.GetResourceDensity(propellant.id);
} }
   
if (log != null) log.buf.AppendFormat("flowMass = {0:g6}\n", flowMass); if (log != null) log.buf.AppendFormat("flowMass = {0:g6}\n", flowMass);
   
for (int i = 0; i < propellants.Count; ++i) for (int i = 0; i < propellants.Count; ++i)
{ {
Propellant propellant = propellants[i]; Propellant propellant = propellants[i];
   
if (propellant.name == "ElectricCharge" || propellant.name == "IntakeAir") if (propellant.name == "ElectricCharge" || propellant.name == "IntakeAir")
{ {
continue; continue;
} }
   
double consumptionRate = propellant.ratio * flowRate / flowMass; double consumptionRate = propellant.ratio * flowRate / flowMass;
if (log != null) log.buf.AppendFormat( if (log != null) log.buf.AppendFormat(
"Add consumption({0}, {1}:{2:d}) = {3:g6}\n", "Add consumption({0}, {1}:{2:d}) = {3:g6}\n",
ResourceContainer.GetResourceName(propellant.id), ResourceContainer.GetResourceName(propellant.id),
theEngine.name, theEngine.name,
theEngine.partId, theEngine.partId,
consumptionRate); consumptionRate);
engineSim.resourceConsumptions.Add(propellant.id, consumptionRate); engineSim.resourceConsumptions.Add(propellant.id, consumptionRate);
engineSim.resourceFlowModes.Add(propellant.id, (double)propellant.GetFlowMode()); engineSim.resourceFlowModes.Add(propellant.id, (double)propellant.GetFlowMode());
} }
   
for (int i = 0; i < thrustTransforms.Count; i++) for (int i = 0; i < thrustTransforms.Count; i++)
{ {
Transform thrustTransform = thrustTransforms[i]; Transform thrustTransform = thrustTransforms[i];
Vector3d direction = thrustTransform.forward.normalized; Vector3d direction = thrustTransform.forward.normalized;
Vector3d position = thrustTransform.position; Vector3d position = thrustTransform.position;
   
AppliedForce appliedForce = AppliedForce.New(direction * engineSim.thrust * thrustTransformMultipliers[i], position); AppliedForce appliedForce = AppliedForce.New(direction * engineSim.thrust * thrustTransformMultipliers[i], position);
engineSim.appliedForces.Add(appliedForce); engineSim.appliedForces.Add(appliedForce);
} }
   
return engineSim; return engineSim;
} }
   
private static Vector3 CalculateThrustVector(List<Transform> thrustTransforms, List<float> thrustTransformMultipliers, LogMsg log) private static Vector3 CalculateThrustVector(List<Transform> thrustTransforms, List<float> thrustTransformMultipliers, LogMsg log)
{ {
if (thrustTransforms == null) if (thrustTransforms == null)
{ {
return Vector3.forward; return Vector3.forward;
} }
   
Vector3 thrustvec = Vector3.zero; Vector3 thrustvec = Vector3.zero;
for (int i = 0; i < thrustTransforms.Count; ++i) for (int i = 0; i < thrustTransforms.Count; ++i)
{ {
Transform trans = thrustTransforms[i]; Transform trans = thrustTransforms[i];
   
if (log != null) log.buf.AppendFormat("Transform = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", trans.forward.x, trans.forward.y, trans.forward.z, trans.forward.magnitude); if (log != null) log.buf.AppendFormat("Transform = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", trans.forward.x, trans.forward.y, trans.forward.z, trans.forward.magnitude);
   
thrustvec -= (trans.forward * thrustTransformMultipliers[i]); thrustvec -= (trans.forward * thrustTransformMultipliers[i]);
} }
   
if (log != null) log.buf.AppendFormat("ThrustVec = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude); if (log != null) log.buf.AppendFormat("ThrustVec = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
   
thrustvec.Normalize(); thrustvec.Normalize();
   
if (log != null) log.buf.AppendFormat("ThrustVecN = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude); if (log != null) log.buf.AppendFormat("ThrustVecN = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
   
return thrustvec; return thrustvec;
} }
   
public ResourceContainer ResourceConsumptions public ResourceContainer ResourceConsumptions
{ {
get get
{ {
return resourceConsumptions; return resourceConsumptions;
} }
} }
   
public static double GetExhaustVelocity(double isp) public static double GetExhaustVelocity(double isp)
{ {
return isp * Units.GRAVITY; return isp * Units.GRAVITY;
} }
   
public static float GetFlowModifier(bool atmChangeFlow, FloatCurve atmCurve, double atmDensity, FloatCurve velCurve, float machNumber, ref float maxMach) public static float GetFlowModifier(bool atmChangeFlow, FloatCurve atmCurve, double atmDensity, FloatCurve velCurve, float machNumber, ref float maxMach)
{ {
float flowModifier = 1.0f; float flowModifier = 1.0f;
if (atmChangeFlow) if (atmChangeFlow)
{ {
flowModifier = (float)(atmDensity / 1.225); flowModifier = (float)(atmDensity / 1.225);
if (atmCurve != null) if (atmCurve != null)
{ {
flowModifier = atmCurve.Evaluate(flowModifier); flowModifier = atmCurve.Evaluate(flowModifier);
} }
} }
if (velCurve != null) if (velCurve != null)
{ {
flowModifier = flowModifier * velCurve.Evaluate(machNumber); flowModifier = flowModifier * velCurve.Evaluate(machNumber);
maxMach = velCurve.maxTime; maxMach = velCurve.maxTime;
} }
if (flowModifier < float.Epsilon) if (flowModifier < float.Epsilon)
{ {
flowModifier = float.Epsilon; flowModifier = float.Epsilon;
} }
return flowModifier; return flowModifier;
} }
   
public static double GetFlowRate(double thrust, double isp) public static double GetFlowRate(double thrust, double isp)
{ {
return thrust / GetExhaustVelocity(isp); return thrust / GetExhaustVelocity(isp);
} }
   
public static float GetThrottlePercent(float currentThrottle, float thrustPercentage) public static float GetThrottlePercent(float currentThrottle, float thrustPercentage)
{ {
return currentThrottle * GetThrustPercent(thrustPercentage); return currentThrottle * GetThrustPercent(thrustPercentage);
} }
   
public static double GetThrust(double flowRate, double isp) public static double GetThrust(double flowRate, double isp)
{ {
return flowRate * GetExhaustVelocity(isp); return flowRate * GetExhaustVelocity(isp);
} }
   
public static float GetThrustPercent(float thrustPercentage) public static float GetThrustPercent(float thrustPercentage)
{ {
return thrustPercentage * 0.01f; return thrustPercentage * 0.01f;
} }
   
public void DumpEngineToBuffer(StringBuilder buffer, String prefix) public void DumpEngineToBuffer(StringBuilder buffer, String prefix)
{ {
buffer.Append(prefix); buffer.Append(prefix);
buffer.AppendFormat("[thrust = {0:g6}, actual = {1:g6}, isp = {2:g6}\n", thrust, actualThrust, isp); buffer.AppendFormat("[thrust = {0:g6}, actual = {1:g6}, isp = {2:g6}\n", thrust, actualThrust, isp);
} }
   
// A dictionary to hold a set of parts for each resource // A dictionary to hold a set of parts for each resource
Dictionary<int, HashSet<PartSim>> sourcePartSets = new Dictionary<int, HashSet<PartSim>>(); Dictionary<int, HashSet<PartSim>> sourcePartSets = new Dictionary<int, HashSet<PartSim>>();
   
Dictionary<int, HashSet<PartSim>> stagePartSets = new Dictionary<int, HashSet<PartSim>>(); Dictionary<int, HashSet<PartSim>> stagePartSets = new Dictionary<int, HashSet<PartSim>>();
   
HashSet<PartSim> visited = new HashSet<PartSim>(); HashSet<PartSim> visited = new HashSet<PartSim>();
   
public void DumpSourcePartSets(String msg) public void DumpSourcePartSets(String msg)
{ {
MonoBehaviour.print("DumpSourcePartSets " + msg); MonoBehaviour.print("DumpSourcePartSets " + msg);
foreach (int type in sourcePartSets.Keys) foreach (int type in sourcePartSets.Keys)
{ {
MonoBehaviour.print("SourcePartSet for " + ResourceContainer.GetResourceName(type)); MonoBehaviour.print("SourcePartSet for " + ResourceContainer.GetResourceName(type));
HashSet<PartSim> sourcePartSet = sourcePartSets[type]; HashSet<PartSim> sourcePartSet = sourcePartSets[type];
if (sourcePartSet.Count > 0) if (sourcePartSet.Count > 0)
{ {
foreach (PartSim partSim in sourcePartSet) foreach (PartSim partSim in sourcePartSet)
{ {
MonoBehaviour.print("Part " + partSim.name + ":" + partSim.partId); MonoBehaviour.print("Part " + partSim.name + ":" + partSim.partId);
} }
} }
else else
{ {
MonoBehaviour.print("No parts"); MonoBehaviour.print("No parts");
} }
} }
} }
   
public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines, HashSet<PartSim> drainingParts) public bool SetResourceDrains(List<PartSim> allParts, List<PartSim> allFuelLines, HashSet<PartSim> drainingParts)
{ {
LogMsg log = null; LogMsg log = null;
//DumpSourcePartSets("before clear"); //DumpSourcePartSets("before clear");
foreach (HashSet<PartSim> sourcePartSet in sourcePartSets.Values) foreach (HashSet<PartSim> sourcePartSet in sourcePartSets.Values)
{ {
sourcePartSet.Clear(); sourcePartSet.Clear();
} }
//DumpSourcePartSets("after clear"); //DumpSourcePartSets("after clear");
   
for (int index = 0; index < this.resourceConsumptions.Types.Count; index++) for (int index = 0; index < this.resourceConsumptions.Types.Count; index++)
{ {
int type = this.resourceConsumptions.Types[index]; int type = this.resourceConsumptions.Types[index];
   
HashSet<PartSim> sourcePartSet; HashSet<PartSim> sourcePartSet;
if (!sourcePartSets.TryGetValue(type, out sourcePartSet)) if (!sourcePartSets.TryGetValue(type, out sourcePartSet))
{ {
sourcePartSet = new HashSet<PartSim>(); sourcePartSet = new HashSet<PartSim>();
sourcePartSets.Add(type, sourcePartSet); sourcePartSets.Add(type, sourcePartSet);
} }
   
switch ((ResourceFlowMode)this.resourceFlowModes[type]) switch ((ResourceFlowMode)this.resourceFlowModes[type])
{ {
case ResourceFlowMode.NO_FLOW: case ResourceFlowMode.NO_FLOW:
if (partSim.resources[type] > SimManager.RESOURCE_MIN && partSim.resourceFlowStates[type] != 0) if (partSim.resources[type] > SimManager.RESOURCE_MIN && partSim.resourceFlowStates[type] != 0)
{ {
//sourcePartSet = new HashSet<PartSim>(); //sourcePartSet = new HashSet<PartSim>();
//MonoBehaviour.print("SetResourceDrains(" + name + ":" + partId + ") setting sources to just this"); //MonoBehaviour.print("SetResourceDrains(" + name + ":" + partId + ") setting sources to just this");
sourcePartSet.Add(partSim); sourcePartSet.Add(partSim);
} }
break; break;
   
case ResourceFlowMode.ALL_VESSEL: case ResourceFlowMode.ALL_VESSEL:
case ResourceFlowMode.ALL_VESSEL_BALANCE: case ResourceFlowMode.ALL_VESSEL_BALANCE:
for (int i = 0; i < allParts.Count; i++) for (int i = 0; i < allParts.Count; i++)
{ {
PartSim aPartSim = allParts[i]; PartSim aPartSim = allParts[i];
if (aPartSim.resources[type] > SimManager.RESOURCE_MIN && aPartSim.resourceFlowStates[type] != 0) if (aPartSim.resources[type] > SimManager.RESOURCE_MIN && aPartSim.resourceFlowStates[type] != 0)
{ {
sourcePartSet.Add(aPartSim); sourcePartSet.Add(aPartSim);
} }
} }
break; break;
   
case ResourceFlowMode.STAGE_PRIORITY_FLOW: case ResourceFlowMode.STAGE_PRIORITY_FLOW:
case ResourceFlowMode.STAGE_PRIORITY_FLOW_BALANCE: case ResourceFlowMode.STAGE_PRIORITY_FLOW_BALANCE:
   
foreach (HashSet<PartSim> stagePartSet in stagePartSets.Values) foreach (HashSet<PartSim> stagePartSet in stagePartSets.Values)
{ {
stagePartSet.Clear(); stagePartSet.Clear();
} }
var maxStage = -1; var maxStage = -1;
   
//Logger.Log(type); //Logger.Log(type);
for (int i = 0; i < allParts.Count; i++) for (int i = 0; i < allParts.Count; i++)
{ {
var aPartSim = allParts[i]; var aPartSim = allParts[i];
if (aPartSim.resources[type] <= SimManager.RESOURCE_MIN || aPartSim.resourceFlowStates[type] == 0) if (aPartSim.resources[type] <= SimManager.RESOURCE_MIN || aPartSim.resourceFlowStates[type] == 0)
{ {
continue; continue;
} }
   
int stage = aPartSim.DecouplerCount(); int stage = aPartSim.DecouplerCount();
if (stage > maxStage) if (stage > maxStage)
{ {
maxStage = stage; maxStage = stage;
} }
   
HashSet<PartSim> tempPartSet; HashSet<PartSim> tempPartSet;
if (!stagePartSets.TryGetValue(stage, out tempPartSet)) if (!stagePartSets.TryGetValue(stage, out tempPartSet))
{ {
tempPartSet = new HashSet<PartSim>(); tempPartSet = new HashSet<PartSim>();
stagePartSets.Add(stage, tempPartSet); stagePartSets.Add(stage, tempPartSet);
} }
tempPartSet.Add(aPartSim); tempPartSet.Add(aPartSim);
} }
   
for (int j = maxStage; j >= 0; j--) for (int j = maxStage; j >= 0; j--)
{ {
HashSet<PartSim> stagePartSet; HashSet<PartSim> stagePartSet;
if (stagePartSets.TryGetValue(j, out stagePartSet) && stagePartSet.Count > 0) if (stagePartSets.TryGetValue(j, out stagePartSet) && stagePartSet.Count > 0)
{ {
// We have to copy the contents of the set here rather than copying the set reference or // We have to copy the contents of the set here rather than copying the set reference or
// bad things (tm) happen // bad things (tm) happen
foreach (PartSim aPartSim in stagePartSet) foreach (PartSim aPartSim in stagePartSet)
{ {
sourcePartSet.Add(aPartSim); sourcePartSet.Add(aPartSim);
} }
break; break;
} }
} }
break; break;
   
case ResourceFlowMode.STACK_PRIORITY_SEARCH: case ResourceFlowMode.STACK_PRIORITY_SEARCH:
visited.Clear(); visited.Clear();
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
log = new LogMsg(); log = new LogMsg();
log.buf.AppendLine("Find " + ResourceContainer.GetResourceName(type) + " sources for " + partSim.name + ":" + partSim.partId); log.buf.AppendLine("Find " + ResourceContainer.GetResourceName(type) + " sources for " + partSim.name + ":" + partSim.partId);
} }
partSim.GetSourceSet(type, PhysicsGlobals.Stack_PriUsesSurf, allParts, visited, sourcePartSet, log, ""); partSim.GetSourceSet(type, PhysicsGlobals.Stack_PriUsesSurf, allParts, visited, sourcePartSet, log, "");
if (SimManager.logOutput && log != null) if (SimManager.logOutput && log != null)
{ {
MonoBehaviour.print(log.buf); MonoBehaviour.print(log.buf);
} }
break; break;
   
case ResourceFlowMode.STAGE_STACK_FLOW: case ResourceFlowMode.STAGE_STACK_FLOW:
case ResourceFlowMode.STAGE_STACK_FLOW_BALANCE: case ResourceFlowMode.STAGE_STACK_FLOW_BALANCE:
visited.Clear(); visited.Clear();
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
log = new LogMsg(); log = new LogMsg();
log.buf.AppendLine("Find " + ResourceContainer.GetResourceName(type) + " sources for " + partSim.name + ":" + partSim.partId); log.buf.AppendLine("Find " + ResourceContainer.GetResourceName(type) + " sources for " + partSim.name + ":" + partSim.partId);
} }
partSim.GetSourceSet(type, true, allParts, visited, sourcePartSet, log, ""); partSim.GetSourceSet(type, true, allParts, visited, sourcePartSet, log, "");
if (SimManager.logOutput && log != null) if (SimManager.logOutput && log != null)
{ {
MonoBehaviour.print(log.buf); MonoBehaviour.print(log.buf);
} }
break; break;
   
default: default:
MonoBehaviour.print("SetResourceDrains(" + partSim.name + ":" + partSim.partId + ") Unexpected flow type for " + ResourceContainer.GetResourceName(type) + ")"); MonoBehaviour.print("SetResourceDrains(" + partSim.name + ":" + partSim.partId + ") Unexpected flow type for " + ResourceContainer.GetResourceName(type) + ")");
break; break;
} }
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
if (sourcePartSet.Count > 0) if (sourcePartSet.Count > 0)
{ {
log = new LogMsg(); log = new LogMsg();
log.buf.AppendLine("Source parts for " + ResourceContainer.GetResourceName(type) + ":"); log.buf.AppendLine("Source parts for " + ResourceContainer.GetResourceName(type) + ":");
foreach (PartSim partSim in sourcePartSet) foreach (PartSim partSim in sourcePartSet)
{ {
log.buf.AppendLine(partSim.name + ":" + partSim.partId); log.buf.AppendLine(partSim.name + ":" + partSim.partId);
} }
MonoBehaviour.print(log.buf); MonoBehaviour.print(log.buf);
} }
} }
   
//DumpSourcePartSets("after " + ResourceContainer.GetResourceName(type)); //DumpSourcePartSets("after " + ResourceContainer.GetResourceName(type));
} }
// If we don't have sources for all the needed resources then return false without setting up any drains // If we don't have sources for all the needed resources then return false without setting up any drains
for (int i = 0; i < this.resourceConsumptions.Types.Count; i++) for (int i = 0; i < this.resourceConsumptions.Types.Count; i++)
{ {
int type = this.resourceConsumptions.Types[i]; int type = this.resourceConsumptions.Types[i];
HashSet<PartSim> sourcePartSet; HashSet<PartSim> sourcePartSet;
if (!sourcePartSets.TryGetValue(type, out sourcePartSet) || sourcePartSet.Count == 0) if (!sourcePartSets.TryGetValue(type, out sourcePartSet) || sourcePartSet.Count == 0)
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
MonoBehaviour.print("No source of " + ResourceContainer.GetResourceName(type)); MonoBehaviour.print("No source of " + ResourceContainer.GetResourceName(type));
} }
   
isActive = false; isActive = false;
return false; return false;
} }
} }
   
// Now we set the drains on the members of the sets and update the draining parts set // Now we set the drains on the members of the sets and update the draining parts set
for (int i = 0; i < this.resourceConsumptions.Types.Count; i++) for (int i = 0; i < this.resourceConsumptions.Types.Count; i++)
{ {
int type = this.resourceConsumptions.Types[i]; int type = this.resourceConsumptions.Types[i];
HashSet<PartSim> sourcePartSet = sourcePartSets[type]; HashSet<PartSim> sourcePartSet = sourcePartSets[type];
// Loop through the members of the set // Loop through the members of the set
double amount = resourceConsumptions[type] / sourcePartSet.Count; double amount = resourceConsumptions[type] / sourcePartSet.Count;
foreach (PartSim partSim in sourcePartSet) foreach (PartSim partSim in sourcePartSet)
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
MonoBehaviour.print( MonoBehaviour.print(
"Adding drain of " + amount + " " + ResourceContainer.GetResourceName(type) + " to " + partSim.name + ":" + "Adding drain of " + amount + " " + ResourceContainer.GetResourceName(type) + " to " + partSim.name + ":" +
partSim.partId); partSim.partId);
} }
   
partSim.resourceDrains.Add(type, amount); partSim.resourceDrains.Add(type, amount);
drainingParts.Add(partSim); drainingParts.Add(partSim);
} }
} }
return true; return true;
} }
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
   
namespace KerbalEngineer.VesselSimulator namespace KerbalEngineer.VesselSimulator
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using CompoundParts; using CompoundParts;
using Extensions; using Extensions;
using UnityEngine; using UnityEngine;
   
public class PartSim public class PartSim
{ {
private static readonly Pool<PartSim> pool = new Pool<PartSim>(Create, Reset); private static readonly Pool<PartSim> pool = new Pool<PartSim>(Create, Reset);
   
private readonly List<AttachNodeSim> attachNodes = new List<AttachNodeSim>(); private readonly List<AttachNodeSim> attachNodes = new List<AttachNodeSim>();
   
public double realMass; public double realMass;
public double baseMass; public double baseMass;
public double baseMassForCoM; public double baseMassForCoM;
public Vector3d centerOfMass; public Vector3d centerOfMass;
public double baseCost; public double baseCost;
public int decoupledInStage; public int decoupledInStage;
public bool fuelCrossFeed; public bool fuelCrossFeed;
public List<PartSim> fuelTargets = new List<PartSim>(); public List<PartSim> fuelTargets = new List<PartSim>();
public List<PartSim> surfaceMountFuelTargets = new List<PartSim>(); public List<PartSim> surfaceMountFuelTargets = new List<PartSim>();
public bool hasModuleEngines; public bool hasModuleEngines;
public bool hasMultiModeEngine; public bool hasMultiModeEngine;
   
public bool hasVessel; public bool hasVessel;
public String initialVesselName; public String initialVesselName;
public int inverseStage; public int inverseStage;
public bool isDecoupler; public bool isDecoupler;
public bool isEngine; public bool isEngine;
public bool isFuelLine; public bool isFuelLine;
public bool isFuelTank; public bool isFuelTank;
public bool isLanded; public bool isLanded;
public bool isNoPhysics; public bool isNoPhysics;
public bool isSepratron; public bool isSepratron;
//public bool isFairing; //public bool isFairing;
public float postStageMassAdjust; public float postStageMassAdjust;
public int stageIndex; public int stageIndex;
public String name; public String name;
public String noCrossFeedNodeKey; public String noCrossFeedNodeKey;
public PartSim parent; public PartSim parent;
public AttachModes parentAttach; public AttachModes parentAttach;
public Part part; // This is only set while the data structures are being initialised public Part part; // This is only set while the data structures are being initialised
public int partId = 0; public int partId = 0;
public ResourceContainer resourceDrains = new ResourceContainer(); public ResourceContainer resourceDrains = new ResourceContainer();
public ResourceContainer resourceFlowStates = new ResourceContainer(); public ResourceContainer resourceFlowStates = new ResourceContainer();
public ResourceContainer resources = new ResourceContainer(); public ResourceContainer resources = new ResourceContainer();
public double startMass = 0d; public double startMass = 0d;
public String vesselName; public String vesselName;
public VesselType vesselType; public VesselType vesselType;
   
private static PartSim Create() private static PartSim Create()
{ {
return new PartSim(); return new PartSim();
} }
   
private static void Reset(PartSim partSim) private static void Reset(PartSim partSim)
{ {
for (int i = 0; i < partSim.attachNodes.Count; i++) for (int i = 0; i < partSim.attachNodes.Count; i++)
{ {
partSim.attachNodes[i].Release(); partSim.attachNodes[i].Release();
} }
partSim.attachNodes.Clear(); partSim.attachNodes.Clear();
partSim.fuelTargets.Clear(); partSim.fuelTargets.Clear();
partSim.surfaceMountFuelTargets.Clear(); partSim.surfaceMountFuelTargets.Clear();
partSim.resourceDrains.Reset(); partSim.resourceDrains.Reset();
partSim.resourceFlowStates.Reset(); partSim.resourceFlowStates.Reset();
partSim.resources.Reset(); partSim.resources.Reset();
partSim.parent = null; partSim.parent = null;
partSim.baseCost = 0d; partSim.baseCost = 0d;
partSim.baseMass = 0d; partSim.baseMass = 0d;
partSim.baseMassForCoM = 0d; partSim.baseMassForCoM = 0d;
partSim.startMass = 0d; partSim.startMass = 0d;
} }
   
public void Release() public void Release()
{ {
pool.Release(this); pool.Release(this);
} }
   
public static PartSim New(Part p, int id, double atmosphere, LogMsg log) public static PartSim New(Part p, int id, double atmosphere, LogMsg log)
{ {
PartSim partSim = pool.Borrow(); PartSim partSim = pool.Borrow();
   
partSim.part = p; partSim.part = p;
partSim.centerOfMass = p.transform.TransformPoint(p.CoMOffset); partSim.centerOfMass = p.transform.TransformPoint(p.CoMOffset);
partSim.partId = id; partSim.partId = id;
partSim.name = p.partInfo.name; partSim.name = p.partInfo.name;
   
if (log != null) log.buf.AppendLine("Create PartSim for " + partSim.name); if (log != null) log.buf.AppendLine("Create PartSim for " + partSim.name);
   
partSim.parent = null; partSim.parent = null;
partSim.parentAttach = p.attachMode; partSim.parentAttach = p.attachMode;
partSim.fuelCrossFeed = p.fuelCrossFeed; partSim.fuelCrossFeed = p.fuelCrossFeed;
partSim.noCrossFeedNodeKey = p.NoCrossFeedNodeKey; partSim.noCrossFeedNodeKey = p.NoCrossFeedNodeKey;
partSim.decoupledInStage = partSim.DecoupledInStage(p); partSim.decoupledInStage = partSim.DecoupledInStage(p);
partSim.isFuelLine = p.HasModule<CModuleFuelLine>(); partSim.isFuelLine = p.HasModule<CModuleFuelLine>();
partSim.isFuelTank = p is FuelTank; partSim.isFuelTank = p is FuelTank;
partSim.isSepratron = partSim.IsSepratron(); partSim.isSepratron = partSim.IsSepratron();
partSim.inverseStage = p.inverseStage; partSim.inverseStage = p.inverseStage;
//MonoBehaviour.print("inverseStage = " + inverseStage); //MonoBehaviour.print("inverseStage = " + inverseStage);
   
partSim.baseCost = p.GetCostDry(); partSim.baseCost = p.GetCostDry();
   
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Parent part = " + (p.parent == null ? "null" : p.parent.partInfo.name)); log.buf.AppendLine("Parent part = " + (p.parent == null ? "null" : p.parent.partInfo.name));
log.buf.AppendLine("physicalSignificance = " + p.physicalSignificance); log.buf.AppendLine("physicalSignificance = " + p.physicalSignificance);
log.buf.AppendLine("PhysicsSignificance = " + p.PhysicsSignificance); log.buf.AppendLine("PhysicsSignificance = " + p.PhysicsSignificance);
} }
   
// Work out if the part should have no physical significance // Work out if the part should have no physical significance
// The root part is never "no physics" // The root part is never "no physics"
partSim.isNoPhysics = p.physicalSignificance == Part.PhysicalSignificance.NONE || partSim.isNoPhysics = p.physicalSignificance == Part.PhysicalSignificance.NONE ||
p.PhysicsSignificance == 1; p.PhysicsSignificance == 1;
   
if (p.HasModule<LaunchClamp>()) if (p.HasModule<LaunchClamp>())
{ {
partSim.realMass = 0d; partSim.realMass = 0d;
if (log != null) log.buf.AppendLine("Ignoring mass of launch clamp"); if (log != null) log.buf.AppendLine("Ignoring mass of launch clamp");
} }
else else
{ {
partSim.realMass = p.mass; partSim.realMass = p.mass;
if (log != null) log.buf.AppendLine("Using part.mass of " + p.mass); if (log != null) log.buf.AppendLine("Using part.mass of " + p.mass);
} }
   
partSim.postStageMassAdjust = 0f; partSim.postStageMassAdjust = 0f;
if (log != null) log.buf.AppendLine("Calculating postStageMassAdjust, prefabMass = " + p.prefabMass); if (log != null) log.buf.AppendLine("Calculating postStageMassAdjust, prefabMass = " + p.prefabMass);
int count = p.Modules.Count; int count = p.Modules.Count;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
if (log != null) log.buf.AppendLine("Module: " + p.Modules[i].moduleName); if (log != null) log.buf.AppendLine("Module: " + p.Modules[i].moduleName);
IPartMassModifier partMassModifier = p.Modules[i] as IPartMassModifier; IPartMassModifier partMassModifier = p.Modules[i] as IPartMassModifier;
if (partMassModifier != null) if (partMassModifier != null)
{ {
if (log != null) log.buf.AppendLine("ChangeWhen = " + partMassModifier.GetModuleMassChangeWhen()); if (log != null) log.buf.AppendLine("ChangeWhen = " + partMassModifier.GetModuleMassChangeWhen());
if (partMassModifier.GetModuleMassChangeWhen() == ModifierChangeWhen.STAGED) if (partMassModifier.GetModuleMassChangeWhen() == ModifierChangeWhen.STAGED)
{ {
float preStage = partMassModifier.GetModuleMass(p.prefabMass, ModifierStagingSituation.UNSTAGED); float preStage = partMassModifier.GetModuleMass(p.prefabMass, ModifierStagingSituation.UNSTAGED);
float postStage = partMassModifier.GetModuleMass(p.prefabMass, ModifierStagingSituation.STAGED); float postStage = partMassModifier.GetModuleMass(p.prefabMass, ModifierStagingSituation.STAGED);
if (log != null) log.buf.AppendLine("preStage = " + preStage + " postStage = " + postStage); if (log != null) log.buf.AppendLine("preStage = " + preStage + " postStage = " + postStage);
partSim.postStageMassAdjust += (postStage - preStage); partSim.postStageMassAdjust += (postStage - preStage);
} }
} }
} }
if (log != null) log.buf.AppendLine("postStageMassAdjust = " + partSim.postStageMassAdjust); if (log != null) log.buf.AppendLine("postStageMassAdjust = " + partSim.postStageMassAdjust);
   
for (int i = 0; i < p.Resources.Count; i++) for (int i = 0; i < p.Resources.Count; i++)
{ {
PartResource resource = p.Resources[i]; PartResource resource = p.Resources[i];
   
// Make sure it isn't NaN as this messes up the part mass and hence most of the values // Make sure it isn't NaN as this messes up the part mass and hence most of the values
// This can happen if a resource capacity is 0 and tweakable // This can happen if a resource capacity is 0 and tweakable
if (!Double.IsNaN(resource.amount)) if (!Double.IsNaN(resource.amount))
{ {
if (log != null) if (log != null)
log.buf.AppendLine(resource.resourceName + " = " + resource.amount); log.buf.AppendLine(resource.resourceName + " = " + resource.amount);
   
partSim.resources.Add(resource.info.id, resource.amount); partSim.resources.Add(resource.info.id, resource.amount);
partSim.resourceFlowStates.Add(resource.info.id, resource.flowState ? 1 : 0); partSim.resourceFlowStates.Add(resource.info.id, resource.flowState ? 1 : 0);
} }
else else
{ {
if (log != null) log.buf.AppendLine(resource.resourceName + " is NaN. Skipping."); if (log != null) log.buf.AppendLine(resource.resourceName + " is NaN. Skipping.");
} }
} }
   
partSim.hasVessel = (p.vessel != null); partSim.hasVessel = (p.vessel != null);
partSim.isLanded = partSim.hasVessel && p.vessel.Landed; partSim.isLanded = partSim.hasVessel && p.vessel.Landed;
if (partSim.hasVessel) if (partSim.hasVessel)
{ {
partSim.vesselName = p.vessel.vesselName; partSim.vesselName = p.vessel.vesselName;
partSim.vesselType = p.vesselType; partSim.vesselType = p.vesselType;
} }
partSim.initialVesselName = p.initialVesselName; partSim.initialVesselName = p.initialVesselName;
   
partSim.hasMultiModeEngine = p.HasModule<MultiModeEngine>(); partSim.hasMultiModeEngine = p.HasModule<MultiModeEngine>();
partSim.hasModuleEngines = p.HasModule<ModuleEngines>(); partSim.hasModuleEngines = p.HasModule<ModuleEngines>();
   
partSim.isEngine = partSim.hasMultiModeEngine || partSim.hasModuleEngines; partSim.isEngine = partSim.hasMultiModeEngine || partSim.hasModuleEngines;
   
if (log != null) log.buf.AppendLine("Created " + partSim.name + ". Decoupled in stage " + partSim.decoupledInStage); if (log != null) log.buf.AppendLine("Created " + partSim.name + ". Decoupled in stage " + partSim.decoupledInStage);
   
return partSim; return partSim;
} }
   
public ResourceContainer ResourceDrains public ResourceContainer ResourceDrains
{ {
get get
{ {
return resourceDrains; return resourceDrains;
} }
} }
   
public ResourceContainer Resources public ResourceContainer Resources
{ {
get get
{ {
return resources; return resources;
} }
} }
   
public void CreateEngineSims(List<EngineSim> allEngines, double atmosphere, double mach, bool vectoredThrust, bool fullThrust, LogMsg log) public void CreateEngineSims(List<EngineSim> allEngines, double atmosphere, double mach, bool vectoredThrust, bool fullThrust, LogMsg log)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("CreateEngineSims for " + this.name); log.buf.AppendLine("CreateEngineSims for " + this.name);
for (int i = 0; i < this.part.Modules.Count; i++) for (int i = 0; i < this.part.Modules.Count; i++)
{ {
PartModule partMod = this.part.Modules[i]; PartModule partMod = this.part.Modules[i];
log.buf.AppendLine("Module: " + partMod.moduleName); log.buf.AppendLine("Module: " + partMod.moduleName);
} }
} }
   
if (hasMultiModeEngine) if (hasMultiModeEngine)
{ {
// A multi-mode engine has multiple ModuleEngines but only one is active at any point // A multi-mode engine has multiple ModuleEngines but only one is active at any point
// The mode of the engine is the engineID of the ModuleEngines that is active // The mode of the engine is the engineID of the ModuleEngines that is active
string mode = part.GetModule<MultiModeEngine>().mode; string mode = part.GetModule<MultiModeEngine>().mode;
   
List<ModuleEngines> engines = part.GetModules<ModuleEngines>(); List<ModuleEngines> engines = part.GetModules<ModuleEngines>();
for (int i = 0; i < engines.Count; ++i) for (int i = 0; i < engines.Count; ++i)
{ {
ModuleEngines engine = engines[i]; ModuleEngines engine = engines[i];
if (engine.engineID == mode) if (engine.engineID == mode)
{ {
if (log != null) log.buf.AppendLine("Module: " + engine.moduleName); if (log != null) log.buf.AppendLine("Module: " + engine.moduleName);
   
EngineSim engineSim = EngineSim.New( EngineSim engineSim = EngineSim.New(
this, this,
engine, engine,
atmosphere, atmosphere,
(float)mach, (float)mach,
vectoredThrust, vectoredThrust,
fullThrust, fullThrust,
log); log);
allEngines.Add(engineSim); allEngines.Add(engineSim);
} }
} }
} }
else if (hasModuleEngines) else if (hasModuleEngines)
{ {
List<ModuleEngines> engines = part.GetModules<ModuleEngines>(); List<ModuleEngines> engines = part.GetModules<ModuleEngines>();
for (int i = 0; i < engines.Count; ++i) for (int i = 0; i < engines.Count; ++i)
{ {
ModuleEngines engine = engines[i]; ModuleEngines engine = engines[i];
if (log != null) log.buf.AppendLine("Module: " + engine.moduleName); if (log != null) log.buf.AppendLine("Module: " + engine.moduleName);
   
EngineSim engineSim = EngineSim.New( EngineSim engineSim = EngineSim.New(
this, this,
engine, engine,
atmosphere, atmosphere,
(float)mach, (float)mach,
vectoredThrust, vectoredThrust,
fullThrust, fullThrust,
log); log);
allEngines.Add(engineSim); allEngines.Add(engineSim);
} }
} }
   
if (log != null) if (log != null)
{ {
log.Flush(); log.Flush();
} }
} }
   
public int DecouplerCount() public int DecouplerCount()
{ {
int count = 0; int count = 0;
PartSim partSim = this; PartSim partSim = this;
while (partSim != null) while (partSim != null)
{ {
if (partSim.isDecoupler) if (partSim.isDecoupler)
{ {
count++; count++;
} }
   
partSim = partSim.parent; partSim = partSim.parent;
} }
return count; return count;
} }
   
public void DrainResources(double time) public void DrainResources(double time)
{ {
//MonoBehaviour.print("DrainResources(" + name + ":" + partId + ", " + time + ")"); //MonoBehaviour.print("DrainResources(" + name + ":" + partId + ", " + time + ")");
for (int i = 0; i < resourceDrains.Types.Count; ++i) for (int i = 0; i < resourceDrains.Types.Count; ++i)
{ {
int type = resourceDrains.Types[i]; int type = resourceDrains.Types[i];
   
//MonoBehaviour.print("draining " + (time * resourceDrains[type]) + " " + ResourceContainer.GetResourceName(type)); //MonoBehaviour.print("draining " + (time * resourceDrains[type]) + " " + ResourceContainer.GetResourceName(type));
resources.Add(type, -time * resourceDrains[type]); resources.Add(type, -time * resourceDrains[type]);
//MonoBehaviour.print(ResourceContainer.GetResourceName(type) + " left = " + resources[type]); //MonoBehaviour.print(ResourceContainer.GetResourceName(type) + " left = " + resources[type]);
} }
} }
   
public String DumpPartAndParentsToBuffer(StringBuilder buffer, String prefix) public String DumpPartAndParentsToBuffer(StringBuilder buffer, String prefix)
{ {
if (parent != null) if (parent != null)
{ {
prefix = parent.DumpPartAndParentsToBuffer(buffer, prefix) + " "; prefix = parent.DumpPartAndParentsToBuffer(buffer, prefix) + " ";
} }
   
DumpPartToBuffer(buffer, prefix); DumpPartToBuffer(buffer, prefix);
   
return prefix; return prefix;
} }
   
public void DumpPartToBuffer(StringBuilder buffer, String prefix, List<PartSim> allParts = null) public void DumpPartToBuffer(StringBuilder buffer, String prefix, List<PartSim> allParts = null)
{ {
buffer.Append(prefix); buffer.Append(prefix);
buffer.Append(name); buffer.Append(name);
buffer.AppendFormat(":[id = {0:d}, decouple = {1:d}, invstage = {2:d}", partId, decoupledInStage, inverseStage); buffer.AppendFormat(":[id = {0:d}, decouple = {1:d}, invstage = {2:d}", partId, decoupledInStage, inverseStage);
   
//buffer.AppendFormat(", vesselName = '{0}'", vesselName); //buffer.AppendFormat(", vesselName = '{0}'", vesselName);
//buffer.AppendFormat(", vesselType = {0}", SimManager.GetVesselTypeString(vesselType)); //buffer.AppendFormat(", vesselType = {0}", SimManager.GetVesselTypeString(vesselType));
//buffer.AppendFormat(", initialVesselName = '{0}'", initialVesselName); //buffer.AppendFormat(", initialVesselName = '{0}'", initialVesselName);
   
buffer.AppendFormat(", isNoPhys = {0}", isNoPhysics); buffer.AppendFormat(", isNoPhys = {0}", isNoPhysics);
buffer.AppendFormat(", baseMass = {0}", baseMass); buffer.AppendFormat(", baseMass = {0}", baseMass);
buffer.AppendFormat(", baseMassForCoM = {0}", baseMassForCoM); buffer.AppendFormat(", baseMassForCoM = {0}", baseMassForCoM);
   
buffer.AppendFormat(", fuelCF = {0}", fuelCrossFeed); buffer.AppendFormat(", fuelCF = {0}", fuelCrossFeed);
buffer.AppendFormat(", noCFNKey = '{0}'", noCrossFeedNodeKey); buffer.AppendFormat(", noCFNKey = '{0}'", noCrossFeedNodeKey);
   
buffer.AppendFormat(", isSep = {0}", isSepratron); buffer.AppendFormat(", isSep = {0}", isSepratron);
   
for (int i = 0; i < resources.Types.Count; i++) for (int i = 0; i < resources.Types.Count; i++)
{ {
int type = resources.Types[i]; int type = resources.Types[i];
buffer.AppendFormat(", {0} = {1:g6}", ResourceContainer.GetResourceName(type), resources[type]); buffer.AppendFormat(", {0} = {1:g6}", ResourceContainer.GetResourceName(type), resources[type]);
} }
   
if (attachNodes.Count > 0) if (attachNodes.Count > 0)
{ {
buffer.Append(", attached = <"); buffer.Append(", attached = <");
attachNodes[0].DumpToBuffer(buffer); attachNodes[0].DumpToBuffer(buffer);
for (int i = 1; i < attachNodes.Count; i++) for (int i = 1; i < attachNodes.Count; i++)
{ {
buffer.Append(", "); buffer.Append(", ");
attachNodes[i].DumpToBuffer(buffer); attachNodes[i].DumpToBuffer(buffer);
} }
buffer.Append(">"); buffer.Append(">");
} }
   
// Add more info here // Add more info here
   
buffer.Append("]\n"); buffer.Append("]\n");
   
if (allParts != null) if (allParts != null)
{ {
String newPrefix = prefix + " "; String newPrefix = prefix + " ";
for (int i = 0; i < allParts.Count; i++) for (int i = 0; i < allParts.Count; i++)
{ {
PartSim partSim = allParts[i]; PartSim partSim = allParts[i];
if (partSim.parent == this) if (partSim.parent == this)
partSim.DumpPartToBuffer(buffer, newPrefix, allParts); partSim.DumpPartToBuffer(buffer, newPrefix, allParts);
} }
} }
} }
   
public bool EmptyOf(HashSet<int> types) public bool EmptyOf(HashSet<int> types)
{ {
foreach (int type in types) foreach (int type in types)
{ {
if (resources.HasType(type) && resourceFlowStates[type] != 0 && resources[type] > SimManager.RESOURCE_PART_EMPTY_THRESH) if (resources.HasType(type) && resourceFlowStates[type] != 0 && resources[type] > SimManager.RESOURCE_PART_EMPTY_THRESH)
return false; return false;
} }
   
return true; return true;
} }
   
public double GetMass(int currentStage, bool forCoM = false) public double GetMass(int currentStage, bool forCoM = false)
{ {
if (decoupledInStage >= currentStage) if (decoupledInStage >= currentStage)
return 0d; return 0d;
   
double mass = forCoM ? baseMassForCoM : baseMass; double mass = forCoM ? baseMassForCoM : baseMass;
   
for (int i = 0; i < resources.Types.Count; ++i) for (int i = 0; i < resources.Types.Count; ++i)
{ {
mass += resources.GetResourceMass(resources.Types[i]); mass += resources.GetResourceMass(resources.Types[i]);
} }
   
if (postStageMassAdjust != 0.0 && currentStage <= inverseStage) if (postStageMassAdjust != 0.0 && currentStage <= inverseStage)
{ {
mass += postStageMassAdjust; mass += postStageMassAdjust;
} }
   
return mass; return mass;
} }
   
public double GetCost(int currentStage) public double GetCost(int currentStage)
{ {
if (decoupledInStage >= currentStage) if (decoupledInStage >= currentStage)
return 0d; return 0d;
   
double cost = baseCost; double cost = baseCost;
   
for (int i = 0; i < resources.Types.Count; ++i) for (int i = 0; i < resources.Types.Count; ++i)
{ {
cost += resources.GetResourceCost(resources.Types[i]); cost += resources.GetResourceCost(resources.Types[i]);
} }
   
return cost; return cost;
} }
   
public void ReleasePart() public void ReleasePart()
{ {
this.part = null; this.part = null;
} }
   
// All functions below this point must not rely on the part member (it may be null) // All functions below this point must not rely on the part member (it may be null)
// //
   
public void GetSourceSet(int type, bool includeSurfaceMountedParts, List<PartSim> allParts, HashSet<PartSim> visited, HashSet<PartSim> allSources, LogMsg log, String indent) public void GetSourceSet(int type, bool includeSurfaceMountedParts, List<PartSim> allParts, HashSet<PartSim> visited, HashSet<PartSim> allSources, LogMsg log, String indent)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine(indent + "GetSourceSet(" + ResourceContainer.GetResourceName(type) + ") for " + name + ":" + partId); log.buf.AppendLine(indent + "GetSourceSet(" + ResourceContainer.GetResourceName(type) + ") for " + name + ":" + partId);
indent += " "; indent += " ";
} }
   
// Rule 1: Each part can be only visited once, If it is visited for second time in particular search it returns as is. // Rule 1: Each part can be only visited once, If it is visited for second time in particular search it returns as is.
if (visited.Contains(this)) if (visited.Contains(this))
{ {
if (log != null) log.buf.AppendLine(indent + "Returning empty set, already visited (" + name + ":" + partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning empty set, already visited (" + name + ":" + partId + ")");
return; return;
} }
   
if (log != null) log.buf.AppendLine(indent + "Adding this to visited"); if (log != null) log.buf.AppendLine(indent + "Adding this to visited");
   
visited.Add(this); visited.Add(this);
   
// Rule 2: Part performs scan on start of every fuel pipe ending in it. This scan is done in order in which pipes were installed. // Rule 2: Part performs scan on start of every fuel pipe ending in it. This scan is done in order in which pipes were installed.
// Then it makes an union of fuel tank sets each pipe scan returned. If the resulting list is not empty, it is returned as result. // Then it makes an union of fuel tank sets each pipe scan returned. If the resulting list is not empty, it is returned as result.
//MonoBehaviour.print("for each fuel line"); //MonoBehaviour.print("for each fuel line");
   
int lastCount = allSources.Count; int lastCount = allSources.Count;
   
for (int i = 0; i < this.fuelTargets.Count; i++) for (int i = 0; i < this.fuelTargets.Count; i++)
{ {
PartSim partSim = this.fuelTargets[i]; PartSim partSim = this.fuelTargets[i];
if (partSim != null) if (partSim != null)
{ {
if (visited.Contains(partSim)) if (visited.Contains(partSim))
{ {
if (log != null) log.buf.AppendLine(indent + "Fuel target already visited, skipping (" + partSim.name + ":" + partSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Fuel target already visited, skipping (" + partSim.name + ":" + partSim.partId + ")");
} }
else else
{ {
if (log != null) log.buf.AppendLine(indent + "Adding fuel target as source (" + partSim.name + ":" + partSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Adding fuel target as source (" + partSim.name + ":" + partSim.partId + ")");
   
partSim.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent); partSim.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent);
} }
} }
} }
   
// check surface mounted fuel targets // check surface mounted fuel targets
if (includeSurfaceMountedParts) if (includeSurfaceMountedParts)
{ {
for (int i = 0; i < surfaceMountFuelTargets.Count; i++) for (int i = 0; i < surfaceMountFuelTargets.Count; i++)
{ {
PartSim partSim = this.surfaceMountFuelTargets[i]; PartSim partSim = this.surfaceMountFuelTargets[i];
if (partSim != null) if (partSim != null)
{ {
if (visited.Contains(partSim)) if (visited.Contains(partSim))
{ {
if (log != null) log.buf.AppendLine(indent + "Fuel target already visited, skipping (" + partSim.name + ":" + partSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Fuel target already visited, skipping (" + partSim.name + ":" + partSim.partId + ")");
} }
else else
{ {
if (log != null) log.buf.AppendLine(indent + "Adding fuel target as source (" + partSim.name + ":" + partSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Adding fuel target as source (" + partSim.name + ":" + partSim.partId + ")");
   
partSim.GetSourceSet(type, true, allParts, visited, allSources, log, indent); partSim.GetSourceSet(type, true, allParts, visited, allSources, log, indent);
} }
} }
} }
} }
   
if (allSources.Count > lastCount) if (allSources.Count > lastCount)
{ {
if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " fuel target sources (" + this.name + ":" + this.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " fuel target sources (" + this.name + ":" + this.partId + ")");
return; return;
} }
   
   
// Rule 3: This rule has been removed and merged with rules 4 and 7 to fix issue with fuel tanks with disabled crossfeed // Rule 3: This rule has been removed and merged with rules 4 and 7 to fix issue with fuel tanks with disabled crossfeed
   
// Rule 4: Part performs scan on each of its axially mounted neighbors. // Rule 4: Part performs scan on each of its axially mounted neighbors.
// Couplers (bicoupler, tricoupler, ...) are an exception, they only scan one attach point on the single attachment side, // Couplers (bicoupler, tricoupler, ...) are an exception, they only scan one attach point on the single attachment side,
// skip the points on the side where multiple points are. [Experiment] // skip the points on the side where multiple points are. [Experiment]
// Again, the part creates union of scan lists from each of its neighbor and if it is not empty, returns this list. // Again, the part creates union of scan lists from each of its neighbor and if it is not empty, returns this list.
// The order in which mount points of a part are scanned appears to be fixed and defined by the part specification file. [Experiment] // The order in which mount points of a part are scanned appears to be fixed and defined by the part specification file. [Experiment]
if (fuelCrossFeed) if (fuelCrossFeed)
{ {
lastCount = allSources.Count; lastCount = allSources.Count;
//MonoBehaviour.print("for each attach node"); //MonoBehaviour.print("for each attach node");
for (int i = 0; i < this.attachNodes.Count; i++) for (int i = 0; i < this.attachNodes.Count; i++)
{ {
AttachNodeSim attachSim = this.attachNodes[i]; AttachNodeSim attachSim = this.attachNodes[i];
if (attachSim.attachedPartSim != null) if (attachSim.attachedPartSim != null)
{ {
if (attachSim.nodeType == AttachNode.NodeType.Stack) if (attachSim.nodeType == AttachNode.NodeType.Stack)
{ {
if ((string.IsNullOrEmpty(noCrossFeedNodeKey) == false && attachSim.id.Contains(noCrossFeedNodeKey)) == false) if ((string.IsNullOrEmpty(noCrossFeedNodeKey) == false && attachSim.id.Contains(noCrossFeedNodeKey)) == false)
{ {
if (visited.Contains(attachSim.attachedPartSim)) if (visited.Contains(attachSim.attachedPartSim))
{ {
if (log != null) log.buf.AppendLine(indent + "Attached part already visited, skipping (" + attachSim.attachedPartSim.name + ":" + attachSim.attachedPartSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Attached part already visited, skipping (" + attachSim.attachedPartSim.name + ":" + attachSim.attachedPartSim.partId + ")");
} }
else else
{ {
if (log != null) log.buf.AppendLine(indent + "Adding attached part as source (" + attachSim.attachedPartSim.name + ":" + attachSim.attachedPartSim.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Adding attached part as source (" + attachSim.attachedPartSim.name + ":" + attachSim.attachedPartSim.partId + ")");
   
attachSim.attachedPartSim.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent); attachSim.attachedPartSim.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent);
} }
} }
} }
} }
} }
   
if (allSources.Count > lastCount) if (allSources.Count > lastCount)
{ {
if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " attached sources (" + this.name + ":" + this.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " attached sources (" + this.name + ":" + this.partId + ")");
return; return;
} }
} }
   
// Rule 5: If the part is fuel container for searched type of fuel (i.e. it has capability to contain that type of fuel and the fuel // Rule 5: If the part is fuel container for searched type of fuel (i.e. it has capability to contain that type of fuel and the fuel
// type was not disabled [Experiment]) and it contains fuel, it returns itself. // type was not disabled [Experiment]) and it contains fuel, it returns itself.
// Rule 6: If the part is fuel container for searched type of fuel (i.e. it has capability to contain that type of fuel and the fuel // Rule 6: If the part is fuel container for searched type of fuel (i.e. it has capability to contain that type of fuel and the fuel
// type was not disabled) but it does not contain the requested fuel, it returns empty list. [Experiment] // type was not disabled) but it does not contain the requested fuel, it returns empty list. [Experiment]
if (resources.HasType(type) && resourceFlowStates[type] > 0.0) if (resources.HasType(type) && resourceFlowStates[type] > 0.0)
{ {
if (resources[type] > SimManager.RESOURCE_MIN) if (resources[type] > SimManager.RESOURCE_MIN)
{ {
allSources.Add(this); allSources.Add(this);
   
if (log != null) log.buf.AppendLine(indent + "Returning enabled tank as only source (" + name + ":" + partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning enabled tank as only source (" + name + ":" + partId + ")");
} }
   
return; return;
} }
else else
{ {
if (log != null) log.buf.AppendLine(indent + "Not fuel tank or disabled. HasType = " + resources.HasType(type) + " FlowState = " + resourceFlowStates[type]); if (log != null) log.buf.AppendLine(indent + "Not fuel tank or disabled. HasType = " + resources.HasType(type) + " FlowState = " + resourceFlowStates[type]);
} }
   
// Rule 7: If the part is radially attached to another part and it is child of that part in the ship's tree structure, it scans its // Rule 7: If the part is radially attached to another part and it is child of that part in the ship's tree structure, it scans its
// parent and returns whatever the parent scan returned. [Experiment] [Experiment] // parent and returns whatever the parent scan returned. [Experiment] [Experiment]
if (parent != null && parentAttach == AttachModes.SRF_ATTACH) if (parent != null && parentAttach == AttachModes.SRF_ATTACH)
{ {
if (fuelCrossFeed) if (fuelCrossFeed)
{ {
if (visited.Contains(parent)) if (visited.Contains(parent))
{ {
if (log != null) log.buf.AppendLine(indent + "Parent part already visited, skipping (" + parent.name + ":" + parent.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Parent part already visited, skipping (" + parent.name + ":" + parent.partId + ")");
} }
else else
{ {
lastCount = allSources.Count; lastCount = allSources.Count;
this.parent.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent); this.parent.GetSourceSet(type, includeSurfaceMountedParts, allParts, visited, allSources, log, indent);
if (allSources.Count > lastCount) if (allSources.Count > lastCount)
{ {
if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " parent sources (" + this.name + ":" + this.partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning " + (allSources.Count - lastCount) + " parent sources (" + this.name + ":" + this.partId + ")");
return; return;
} }
} }
} }
} }
   
// Rule 8: If all preceding rules failed, part returns empty list. // Rule 8: If all preceding rules failed, part returns empty list.
if (log != null) log.buf.AppendLine(indent + "Returning empty set, no sources found (" + name + ":" + partId + ")"); if (log != null) log.buf.AppendLine(indent + "Returning empty set, no sources found (" + name + ":" + partId + ")");
   
return; return;
} }
   
public double GetStartMass() public double GetStartMass()
{ {
return startMass; return startMass;
} }
   
public void RemoveAttachedParts(HashSet<PartSim> partSims) public void RemoveAttachedParts(HashSet<PartSim> partSims)
{ {
// Loop through the attached parts // Loop through the attached parts
for (int i = 0; i < this.attachNodes.Count; i++) for (int i = 0; i < this.attachNodes.Count; i++)
{ {
AttachNodeSim attachSim = this.attachNodes[i]; AttachNodeSim attachSim = this.attachNodes[i];
// If the part is in the set then "remove" it by clearing the PartSim reference // If the part is in the set then "remove" it by clearing the PartSim reference
if (partSims.Contains(attachSim.attachedPartSim)) if (partSims.Contains(attachSim.attachedPartSim))
{ {
attachSim.attachedPartSim = null; attachSim.attachedPartSim = null;
} }
} }
   
// Loop through the fuel targets (fuel line sources) // Loop through the fuel targets (fuel line sources)
for (int i = 0; i < this.fuelTargets.Count; i++) for (int i = 0; i < this.fuelTargets.Count; i++)
{ {
PartSim fuelTargetSim = this.fuelTargets[i]; PartSim fuelTargetSim = this.fuelTargets[i];
// If the part is in the set then "remove" it by clearing the PartSim reference // If the part is in the set then "remove" it by clearing the PartSim reference
if (fuelTargetSim != null && partSims.Contains(fuelTargetSim)) if (fuelTargetSim != null && partSims.Contains(fuelTargetSim))
{ {
this.fuelTargets[i] = null; this.fuelTargets[i] = null;
} }
} }
   
// Loop through the surface attached fuel targets (surface attached parts for new flow modes) // Loop through the surface attached fuel targets (surface attached parts for new flow modes)
for (int i = 0; i < this.surfaceMountFuelTargets.Count; i++) for (int i = 0; i < this.surfaceMountFuelTargets.Count; i++)
{ {
PartSim fuelTargetSim = this.surfaceMountFuelTargets[i]; PartSim fuelTargetSim = this.surfaceMountFuelTargets[i];
// If the part is in the set then "remove" it by clearing the PartSim reference // If the part is in the set then "remove" it by clearing the PartSim reference
if (fuelTargetSim != null && partSims.Contains(fuelTargetSim)) if (fuelTargetSim != null && partSims.Contains(fuelTargetSim))
{ {
this.surfaceMountFuelTargets[i] = null; this.surfaceMountFuelTargets[i] = null;
} }
} }
} }
   
public void SetupAttachNodes(Dictionary<Part, PartSim> partSimLookup, LogMsg log) public void SetupAttachNodes(Dictionary<Part, PartSim> partSimLookup, LogMsg log)
{ {
if (log != null) log.buf.AppendLine("SetupAttachNodes for " + name + ":" + partId + ""); if (log != null) log.buf.AppendLine("SetupAttachNodes for " + name + ":" + partId + "");
   
attachNodes.Clear(); attachNodes.Clear();
   
for (int i = 0; i < part.attachNodes.Count; ++i) for (int i = 0; i < part.attachNodes.Count; ++i)
{ {
AttachNode attachNode = part.attachNodes[i]; AttachNode attachNode = part.attachNodes[i];
   
if (log != null) log.buf.AppendLine("AttachNode " + attachNode.id + " = " + (attachNode.attachedPart != null ? attachNode.attachedPart.partInfo.name : "null")); if (log != null) log.buf.AppendLine("AttachNode " + attachNode.id + " = " + (attachNode.attachedPart != null ? attachNode.attachedPart.partInfo.name : "null"));
   
if (attachNode.attachedPart != null && attachNode.id != "Strut") if (attachNode.attachedPart != null && attachNode.id != "Strut")
{ {
PartSim attachedSim; PartSim attachedSim;
if (partSimLookup.TryGetValue(attachNode.attachedPart, out attachedSim)) if (partSimLookup.TryGetValue(attachNode.attachedPart, out attachedSim))
{ {
if (log != null) log.buf.AppendLine("Adding attached node " + attachedSim.name + ":" + attachedSim.partId + ""); if (log != null) log.buf.AppendLine("Adding attached node " + attachedSim.name + ":" + attachedSim.partId + "");
   
attachNodes.Add(AttachNodeSim.New(attachedSim, attachNode.id, attachNode.nodeType)); attachNodes.Add(AttachNodeSim.New(attachedSim, attachNode.id, attachNode.nodeType));
} }
else else
{ {
if (log != null) log.buf.AppendLine("No PartSim for attached part (" + attachNode.attachedPart.partInfo.name + ")"); if (log != null) log.buf.AppendLine("No PartSim for attached part (" + attachNode.attachedPart.partInfo.name + ")");
} }
} }
} }
   
for (int i = 0; i < part.fuelLookupTargets.Count; ++i) for (int i = 0; i < part.fuelLookupTargets.Count; ++i)
{ {
Part p = part.fuelLookupTargets[i]; Part p = part.fuelLookupTargets[i];
   
if (p != null) if (p != null)
{ {
PartSim targetSim; PartSim targetSim;
if (partSimLookup.TryGetValue(p, out targetSim)) if (partSimLookup.TryGetValue(p, out targetSim))
{ {
if (log != null) log.buf.AppendLine("Fuel target: " + targetSim.name + ":" + targetSim.partId); if (log != null) log.buf.AppendLine("Fuel target: " + targetSim.name + ":" + targetSim.partId);
   
fuelTargets.Add(targetSim); fuelTargets.Add(targetSim);
} }
else else
{ {
if (log != null) log.buf.AppendLine("No PartSim for fuel target (" + p.name + ")"); if (log != null) log.buf.AppendLine("No PartSim for fuel target (" + p.name + ")");
} }
} }
} }
} }
   
public void SetupParent(Dictionary<Part, PartSim> partSimLookup, LogMsg log) public void SetupParent(Dictionary<Part, PartSim> partSimLookup, LogMsg log)
{ {
if (part.parent != null) if (part.parent != null)
{ {
parent = null; parent = null;
if (partSimLookup.TryGetValue(part.parent, out parent)) if (partSimLookup.TryGetValue(part.parent, out parent))
{ {
if (log != null) log.buf.AppendLine("Parent part is " + parent.name + ":" + parent.partId); if (log != null) log.buf.AppendLine("Parent part is " + parent.name + ":" + parent.partId);
if (part.attachMode == AttachModes.SRF_ATTACH && part.attachRules.srfAttach && part.fuelCrossFeed && part.parent.fuelCrossFeed) if (part.attachMode == AttachModes.SRF_ATTACH && part.attachRules.srfAttach && part.fuelCrossFeed && part.parent.fuelCrossFeed)
{ {
if (log != null) log.buf.AppendLine("Added " + name + " to " + parent.name + " surface mounted fuel targets."); if (log != null) log.buf.AppendLine("Added " + name + " to " + parent.name + " surface mounted fuel targets.");
parent.surfaceMountFuelTargets.Add(this); parent.surfaceMountFuelTargets.Add(this);
} }
} }
else else
{ {
if (log != null) log.buf.AppendLine("No PartSim for parent part (" + part.parent.partInfo.name + ")"); if (log != null) log.buf.AppendLine("No PartSim for parent part (" + part.parent.partInfo.name + ")");
} }
} }
} }
   
public double TimeToDrainResource() public double TimeToDrainResource()
{ {
//MonoBehaviour.print("TimeToDrainResource(" + name + ":" + partId + ")"); //MonoBehaviour.print("TimeToDrainResource(" + name + ":" + partId + ")");
double time = double.MaxValue; double time = double.MaxValue;
   
for (int i = 0; i < resourceDrains.Types.Count; ++i) for (int i = 0; i < resourceDrains.Types.Count; ++i)
{ {
int type = resourceDrains.Types[i]; int type = resourceDrains.Types[i];
   
if (resourceDrains[type] > 0) if (resourceDrains[type] > 0)
{ {
time = Math.Min(time, resources[type] / resourceDrains[type]); time = Math.Min(time, resources[type] / resourceDrains[type]);
//MonoBehaviour.print("type = " + ResourceContainer.GetResourceName(type) + " amount = " + resources[type] + " rate = " + resourceDrains[type] + " time = " + time); //MonoBehaviour.print("type = " + ResourceContainer.GetResourceName(type) + " amount = " + resources[type] + " rate = " + resourceDrains[type] + " time = " + time);
} }
} }
   
//if (time < double.MaxValue) //if (time < double.MaxValue)
// MonoBehaviour.print("TimeToDrainResource(" + name + ":" + partId + ") = " + time); // MonoBehaviour.print("TimeToDrainResource(" + name + ":" + partId + ") = " + time);
return time; return time;
} }
   
private Vector3 CalculateThrustVector(List<Transform> thrustTransforms, LogMsg log) private Vector3 CalculateThrustVector(List<Transform> thrustTransforms, LogMsg log)
{ {
if (thrustTransforms == null) if (thrustTransforms == null)
{ {
return Vector3.forward; return Vector3.forward;
} }
   
Vector3 thrustvec = Vector3.zero; Vector3 thrustvec = Vector3.zero;
for (int i = 0; i < thrustTransforms.Count; ++i) for (int i = 0; i < thrustTransforms.Count; ++i)
{ {
Transform trans = thrustTransforms[i]; Transform trans = thrustTransforms[i];
   
if (log != null) log.buf.AppendFormat("Transform = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", trans.forward.x, trans.forward.y, trans.forward.z, trans.forward.magnitude); if (log != null) log.buf.AppendFormat("Transform = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", trans.forward.x, trans.forward.y, trans.forward.z, trans.forward.magnitude);
   
thrustvec -= trans.forward; thrustvec -= trans.forward;
} }
   
if (log != null) log.buf.AppendFormat("ThrustVec = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude); if (log != null) log.buf.AppendFormat("ThrustVec = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
   
thrustvec.Normalize(); thrustvec.Normalize();
   
if (log != null) log.buf.AppendFormat("ThrustVecN = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude); if (log != null) log.buf.AppendFormat("ThrustVecN = ({0:g6}, {1:g6}, {2:g6}) length = {3:g6}\n", thrustvec.x, thrustvec.y, thrustvec.z, thrustvec.magnitude);
   
return thrustvec; return thrustvec;
} }
   
private int DecoupledInStage(Part thePart, int stage = -1) private int DecoupledInStage(Part thePart, int stage = -1)
{ {
if (IsDecoupler(thePart) && thePart.inverseStage > stage) if (IsDecoupler(thePart) && thePart.inverseStage > stage)
stage = thePart.inverseStage; stage = thePart.inverseStage;
   
if (thePart.parent != null) if (thePart.parent != null)
stage = DecoupledInStage(thePart.parent, stage); stage = DecoupledInStage(thePart.parent, stage);
   
return stage; return stage;
} }
   
private bool IsActiveDecoupler(Part thePart) private bool IsActiveDecoupler(Part thePart)
{ {
return thePart.FindModulesImplementing<ModuleDecouple>().Any(mod => !mod.isDecoupled) || return thePart.FindModulesImplementing<ModuleDecouple>().Any(mod => !mod.isDecoupled) ||
thePart.FindModulesImplementing<ModuleAnchoredDecoupler>().Any(mod => !mod.isDecoupled); thePart.FindModulesImplementing<ModuleAnchoredDecoupler>().Any(mod => !mod.isDecoupled);
} }
   
private bool IsDecoupler(Part thePart) private bool IsDecoupler(Part thePart)
{ {
return thePart.GetProtoModuleDecoupler()?.IsStageEnabled ?? false; PartExtensions.ProtoModuleDecoupler protoDecoupler = thePart.GetProtoModuleDecoupler();
  return protoDecoupler != null && protoDecoupler.IsStageEnabled;
} }
   
private bool IsSepratron() private bool IsSepratron()
{ {
if (!part.ActivatesEvenIfDisconnected) if (!part.ActivatesEvenIfDisconnected)
{ {
return false; return false;
} }
   
if (part is SolidRocket) if (part is SolidRocket)
{ {
return true; return true;
} }
   
IEnumerable<ModuleEngines> modList = part.Modules.OfType<ModuleEngines>(); IEnumerable<ModuleEngines> modList = part.Modules.OfType<ModuleEngines>();
if (modList.Count() == 0) if (modList.Count() == 0)
{ {
return false; return false;
} }
   
if (modList.First().throttleLocked) if (modList.First().throttleLocked)
{ {
return true; return true;
} }
   
return false; return false;
} }
} }
} }
// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2014 CYBUTEK // Copyright (C) 2014 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
#region Using Directives #region Using Directives
   
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
   
using UnityEngine; using UnityEngine;
   
#endregion #endregion
   
namespace KerbalEngineer.VesselSimulator namespace KerbalEngineer.VesselSimulator
{ {
using System.ComponentModel;  
using CompoundParts; using CompoundParts;
using Extensions; using Extensions;
using Helpers; using Helpers;
using KSP.UI.Screens; using KSP.UI.Screens;
   
public class Simulation public class Simulation
{ {
private const double SECONDS_PER_DAY = 86400; private const double SECONDS_PER_DAY = 86400;
private readonly Stopwatch _timer = new Stopwatch(); private readonly Stopwatch _timer = new Stopwatch();
private List<EngineSim> activeEngines = new List<EngineSim>(); private List<EngineSim> activeEngines = new List<EngineSim>();
private List<EngineSim> allEngines = new List<EngineSim>(); private List<EngineSim> allEngines = new List<EngineSim>();
private List<PartSim> allFuelLines = new List<PartSim>(); private List<PartSim> allFuelLines = new List<PartSim>();
private List<PartSim> allParts = new List<PartSim>(); private List<PartSim> allParts = new List<PartSim>();
private double atmosphere; private double atmosphere;
private int currentStage; private int currentStage;
private double currentisp; private double currentisp;
private HashSet<PartSim> decoupledParts = new HashSet<PartSim>(); private HashSet<PartSim> decoupledParts = new HashSet<PartSim>();
private bool doingCurrent; private bool doingCurrent;
private List<PartSim> dontStageParts; private List<PartSim> dontStageParts;
private List<List<PartSim>> dontStagePartsLists = new List<List<PartSim>>(); private List<List<PartSim>> dontStagePartsLists = new List<List<PartSim>>();
private HashSet<PartSim> drainingParts; private HashSet<PartSim> drainingParts;
private HashSet<int> drainingResources; private HashSet<int> drainingResources;
private double gravity; private double gravity;
private Dictionary<Part, PartSim> partSimLookup; private Dictionary<Part, PartSim> partSimLookup;
   
private int lastStage; private int lastStage;
private List<Part> partList = new List<Part>(); private List<Part> partList = new List<Part>();
private double simpleTotalThrust; private double simpleTotalThrust;
private double stageStartMass; private double stageStartMass;
private Vector3d stageStartCom; private Vector3d stageStartCom;
private double stageTime; private double stageTime;
private double stepEndMass; private double stepEndMass;
private double stepStartMass; private double stepStartMass;
private double totalStageActualThrust; private double totalStageActualThrust;
private double totalStageFlowRate; private double totalStageFlowRate;
private double totalStageIspFlowRate; private double totalStageIspFlowRate;
private double totalStageThrust; private double totalStageThrust;
private ForceAccumulator totalStageThrustForce = new ForceAccumulator(); private ForceAccumulator totalStageThrustForce = new ForceAccumulator();
private Vector3 vecActualThrust; private Vector3 vecActualThrust;
private Vector3 vecStageDeltaV; private Vector3 vecStageDeltaV;
private Vector3 vecThrust; private Vector3 vecThrust;
private double mach; private double mach;
private float maxMach; private float maxMach;
public String vesselName; public String vesselName;
public VesselType vesselType; public VesselType vesselType;
private WeightedVectorAverager vectorAverager = new WeightedVectorAverager(); private WeightedVectorAverager vectorAverager = new WeightedVectorAverager();
   
public Simulation() public Simulation()
{ {
this.allParts = new List<PartSim>(); this.allParts = new List<PartSim>();
this.allFuelLines = new List<PartSim>(); this.allFuelLines = new List<PartSim>();
this.drainingParts = new HashSet<PartSim>(); this.drainingParts = new HashSet<PartSim>();
this.allEngines = new List<EngineSim>(); this.allEngines = new List<EngineSim>();
this.activeEngines = new List<EngineSim>(); this.activeEngines = new List<EngineSim>();
this.drainingResources = new HashSet<int>(); this.drainingResources = new HashSet<int>();
this.totalStageThrustForce = new ForceAccumulator(); this.totalStageThrustForce = new ForceAccumulator();
   
// A dictionary for fast lookup of Part->PartSim during the preparation phase // A dictionary for fast lookup of Part->PartSim during the preparation phase
partSimLookup = new Dictionary<Part, PartSim>(); partSimLookup = new Dictionary<Part, PartSim>();
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
MonoBehaviour.print("Simulation created"); MonoBehaviour.print("Simulation created");
} }
} }
   
private double ShipMass private double ShipMass
{ {
get get
{ {
double mass = 0d; double mass = 0d;
   
for (int i = 0; i < allParts.Count; ++i) { for (int i = 0; i < allParts.Count; ++i) {
mass += allParts[i].GetMass(currentStage); mass += allParts[i].GetMass(currentStage);
} }
   
return mass; return mass;
} }
} }
   
private Vector3d ShipCom private Vector3d ShipCom
{ {
get get
{ {
vectorAverager.Reset(); vectorAverager.Reset();
   
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
PartSim partSim = allParts[i]; PartSim partSim = allParts[i];
vectorAverager.Add(partSim.centerOfMass, partSim.GetMass(currentStage, true)); vectorAverager.Add(partSim.centerOfMass, partSim.GetMass(currentStage, true));
} }
   
return vectorAverager.Get(); return vectorAverager.Get();
} }
} }
   
// This function prepares the simulation by creating all the necessary data structures it will // This function prepares the simulation by creating all the necessary data structures it will
// need during the simulation. All required data is copied from the core game data structures // need during the simulation. All required data is copied from the core game data structures
// so that the simulation itself can be run in a background thread without having issues with // so that the simulation itself can be run in a background thread without having issues with
// the core game changing the data while the simulation is running. // the core game changing the data while the simulation is running.
public bool PrepareSimulation(List<Part> parts, double theGravity, double theAtmosphere = 0, double theMach = 0, bool dumpTree = false, bool vectoredThrust = false, bool fullThrust = false) public bool PrepareSimulation(List<Part> parts, double theGravity, double theAtmosphere = 0, double theMach = 0, bool dumpTree = false, bool vectoredThrust = false, bool fullThrust = false)
{ {
LogMsg log = null; LogMsg log = null;
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
log = new LogMsg(); log = new LogMsg();
log.buf.AppendLine("PrepareSimulation started"); log.buf.AppendLine("PrepareSimulation started");
dumpTree = true; dumpTree = true;
} }
this._timer.Reset(); this._timer.Reset();
this._timer.Start(); this._timer.Start();
   
// Store the parameters in members for ease of access in other functions // Store the parameters in members for ease of access in other functions
this.partList = parts; this.partList = parts;
this.gravity = theGravity; this.gravity = theGravity;
this.atmosphere = theAtmosphere; this.atmosphere = theAtmosphere;
this.mach = theMach; this.mach = theMach;
this.lastStage = StageManager.LastStage; this.lastStage = StageManager.LastStage;
this.maxMach = 1.0f; this.maxMach = 1.0f;
//MonoBehaviour.print("lastStage = " + lastStage); //MonoBehaviour.print("lastStage = " + lastStage);
   
// Clear the lists for our simulation parts // Clear the lists for our simulation parts
allParts.Clear(); allParts.Clear();
allFuelLines.Clear(); allFuelLines.Clear();
drainingParts.Clear(); drainingParts.Clear();
allEngines.Clear(); allEngines.Clear();
activeEngines.Clear(); activeEngines.Clear();
drainingResources.Clear(); drainingResources.Clear();
   
// A dictionary for fast lookup of Part->PartSim during the preparation phase // A dictionary for fast lookup of Part->PartSim during the preparation phase
partSimLookup.Clear(); partSimLookup.Clear();
   
if (this.partList.Count > 0 && this.partList[0].vessel != null) if (this.partList.Count > 0 && this.partList[0].vessel != null)
{ {
this.vesselName = this.partList[0].vessel.vesselName; this.vesselName = this.partList[0].vessel.vesselName;
this.vesselType = this.partList[0].vessel.vesselType; this.vesselType = this.partList[0].vessel.vesselType;
} }
//MonoBehaviour.print("PrepareSimulation pool size = " + PartSim.pool.Count()); //MonoBehaviour.print("PrepareSimulation pool size = " + PartSim.pool.Count());
// First we create a PartSim for each Part (giving each a unique id) // First we create a PartSim for each Part (giving each a unique id)
int partId = 1; int partId = 1;
for (int i = 0; i < partList.Count; ++i) for (int i = 0; i < partList.Count; ++i)
{ {
Part part = partList[i]; Part part = partList[i];
   
// If the part is already in the lookup dictionary then log it and skip to the next part // If the part is already in the lookup dictionary then log it and skip to the next part
if (partSimLookup.ContainsKey(part)) if (partSimLookup.ContainsKey(part))
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Part " + part.name + " appears in vessel list more than once"); log.buf.AppendLine("Part " + part.name + " appears in vessel list more than once");
} }
continue; continue;
} }
   
// Create the PartSim // Create the PartSim
PartSim partSim = PartSim.New(part, partId, this.atmosphere, log); PartSim partSim = PartSim.New(part, partId, this.atmosphere, log);
   
// Add it to the Part lookup dictionary and the necessary lists // Add it to the Part lookup dictionary and the necessary lists
partSimLookup.Add(part, partSim); partSimLookup.Add(part, partSim);
this.allParts.Add(partSim); this.allParts.Add(partSim);
if (partSim.isFuelLine) if (partSim.isFuelLine)
{ {
this.allFuelLines.Add(partSim); this.allFuelLines.Add(partSim);
} }
if (partSim.isEngine) if (partSim.isEngine)
{ {
partSim.CreateEngineSims(this.allEngines, this.atmosphere, this.mach, vectoredThrust, fullThrust, log); partSim.CreateEngineSims(this.allEngines, this.atmosphere, this.mach, vectoredThrust, fullThrust, log);
} }
   
partId++; partId++;
} }
   
for (int i = 0; i < allEngines.Count; ++i) for (int i = 0; i < allEngines.Count; ++i)
{ {
maxMach = Mathf.Max(maxMach, allEngines[i].maxMach); maxMach = Mathf.Max(maxMach, allEngines[i].maxMach);
} }
   
this.UpdateActiveEngines(); this.UpdateActiveEngines();
   
// Now that all the PartSims have been created we can do any set up that needs access to other parts // Now that all the PartSims have been created we can do any set up that needs access to other parts
// First we set up all the parent links // First we set up all the parent links
for (int i = 0; i < this.allParts.Count; i++) for (int i = 0; i < this.allParts.Count; i++)
{ {
PartSim partSim = this.allParts[i]; PartSim partSim = this.allParts[i];
partSim.SetupParent(partSimLookup, log); partSim.SetupParent(partSimLookup, log);
} }
   
// Then, in the VAB/SPH, we add the parent of each fuel line to the fuelTargets list of their targets // Then, in the VAB/SPH, we add the parent of each fuel line to the fuelTargets list of their targets
if (HighLogic.LoadedSceneIsEditor) if (HighLogic.LoadedSceneIsEditor)
{ {
for (int i = 0; i < allFuelLines.Count; ++i) for (int i = 0; i < allFuelLines.Count; ++i)
{ {
PartSim partSim = allFuelLines[i]; PartSim partSim = allFuelLines[i];
   
CModuleFuelLine fuelLine = partSim.part.GetModule<CModuleFuelLine>(); CModuleFuelLine fuelLine = partSim.part.GetModule<CModuleFuelLine>();
if (fuelLine.target != null) if (fuelLine.target != null)
{ {
PartSim targetSim; PartSim targetSim;
if (partSimLookup.TryGetValue(fuelLine.target, out targetSim)) if (partSimLookup.TryGetValue(fuelLine.target, out targetSim))
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Fuel line target is " + targetSim.name + ":" + targetSim.partId); log.buf.AppendLine("Fuel line target is " + targetSim.name + ":" + targetSim.partId);
} }
   
targetSim.fuelTargets.Add(partSim.parent); targetSim.fuelTargets.Add(partSim.parent);
} }
else else
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("No PartSim for fuel line target (" + partSim.part.partInfo.name + ")"); log.buf.AppendLine("No PartSim for fuel line target (" + partSim.part.partInfo.name + ")");
} }
} }
} }
else else
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Fuel line target is null"); log.buf.AppendLine("Fuel line target is null");
} }
} }
} }
} }
   
//MonoBehaviour.print("SetupAttachNodes and count stages"); //MonoBehaviour.print("SetupAttachNodes and count stages");
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
PartSim partSim = allParts[i]; PartSim partSim = allParts[i];
   
partSim.SetupAttachNodes(partSimLookup, log); partSim.SetupAttachNodes(partSimLookup, log);
if (partSim.decoupledInStage >= this.lastStage) if (partSim.decoupledInStage >= this.lastStage)
{ {
this.lastStage = partSim.decoupledInStage + 1; this.lastStage = partSim.decoupledInStage + 1;
} }
} }
   
// And finally release the Part references from all the PartSims // And finally release the Part references from all the PartSims
//MonoBehaviour.print("ReleaseParts"); //MonoBehaviour.print("ReleaseParts");
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
allParts[i].ReleasePart(); allParts[i].ReleasePart();
} }
   
// And dereference the core's part list // And dereference the core's part list
this.partList = null; this.partList = null;
   
this._timer.Stop(); this._timer.Stop();
if (log != null) if (log != null)
{ {
log.buf.AppendLine("PrepareSimulation: " + this._timer.ElapsedMilliseconds + "ms"); log.buf.AppendLine("PrepareSimulation: " + this._timer.ElapsedMilliseconds + "ms");
log.Flush(); log.Flush();
} }
   
if (dumpTree) if (dumpTree)
{ {
this.Dump(); this.Dump();
} }
   
return true; return true;
} }
// This function runs the simulation and returns a newly created array of Stage objects // This function runs the simulation and returns a newly created array of Stage objects
public Stage[] RunSimulation() public Stage[] RunSimulation()
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
MonoBehaviour.print("RunSimulation started"); MonoBehaviour.print("RunSimulation started");
} }
   
this._timer.Reset(); this._timer.Reset();
this._timer.Start(); this._timer.Start();
   
LogMsg log = null; LogMsg log = null;
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
log = new LogMsg(); log = new LogMsg();
} }
   
// Start with the last stage to simulate // Start with the last stage to simulate
// (this is in a member variable so it can be accessed by AllowedToStage and ActivateStage) // (this is in a member variable so it can be accessed by AllowedToStage and ActivateStage)
this.currentStage = this.lastStage; this.currentStage = this.lastStage;
// Work out which engines would be active if just doing the staging and if this is different to the // Work out which engines would be active if just doing the staging and if this is different to the
// currently active engines then generate an extra stage // currently active engines then generate an extra stage
// Loop through all the engines // Loop through all the engines
bool anyActive = false; bool anyActive = false;
for (int i = 0; i < allEngines.Count; ++i) for (int i = 0; i < allEngines.Count; ++i)
{ {
EngineSim engine = allEngines[i]; EngineSim engine = allEngines[i];
   
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Testing engine mod of " + engine.partSim.name + ":" + engine.partSim.partId); log.buf.AppendLine("Testing engine mod of " + engine.partSim.name + ":" + engine.partSim.partId);
} }
bool bActive = engine.isActive; bool bActive = engine.isActive;
bool bStage = (engine.partSim.inverseStage >= this.currentStage); bool bStage = (engine.partSim.inverseStage >= this.currentStage);
if (log != null) if (log != null)
{ {
log.buf.AppendLine("bActive = " + bActive + " bStage = " + bStage); log.buf.AppendLine("bActive = " + bActive + " bStage = " + bStage);
} }
if (HighLogic.LoadedSceneIsFlight) if (HighLogic.LoadedSceneIsFlight)
{ {
if (bActive) if (bActive)
{ {
anyActive = true; anyActive = true;
} }
if (bActive != bStage) if (bActive != bStage)
{ {
// If the active state is different to the state due to staging // If the active state is different to the state due to staging
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Need to do current active engines first"); log.buf.AppendLine("Need to do current active engines first");
} }
   
this.doingCurrent = true; this.doingCurrent = true;
} }
} }
else else
{ {
if (bStage) if (bStage)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Marking as active"); log.buf.AppendLine("Marking as active");
} }
   
engine.isActive = true; engine.isActive = true;
} }
} }
} }
   
// If we need to do current because of difference in engine activation and there actually are active engines // If we need to do current because of difference in engine activation and there actually are active engines
// then we do the extra stage otherwise activate the next stage and don't treat it as current // then we do the extra stage otherwise activate the next stage and don't treat it as current
if (this.doingCurrent && anyActive) if (this.doingCurrent && anyActive)
{ {
this.currentStage++; this.currentStage++;
} }
else else
{ {
this.ActivateStage(); this.ActivateStage();
this.doingCurrent = false; this.doingCurrent = false;
} }
   
// Create a list of lists of PartSims that prevent decoupling // Create a list of lists of PartSims that prevent decoupling
BuildDontStageLists(log); BuildDontStageLists(log);
   
if (log != null) if (log != null)
{ {
log.Flush(); log.Flush();
} }
   
// Create the array of stages that will be returned // Create the array of stages that will be returned
Stage[] stages = new Stage[this.currentStage + 1]; Stage[] stages = new Stage[this.currentStage + 1];
   
int startStage = currentStage; int startStage = currentStage;
   
// Loop through the stages // Loop through the stages
while (this.currentStage >= 0) while (this.currentStage >= 0)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Simulating stage " + this.currentStage); log.buf.AppendLine("Simulating stage " + this.currentStage);
log.Flush(); log.Flush();
this._timer.Reset(); this._timer.Reset();
this._timer.Start(); this._timer.Start();
} }
   
// Update active engines and resource drains // Update active engines and resource drains
this.UpdateResourceDrains(); this.UpdateResourceDrains();
   
// Update the masses of the parts to correctly handle "no physics" parts // Update the masses of the parts to correctly handle "no physics" parts
this.stageStartMass = this.UpdatePartMasses(); this.stageStartMass = this.UpdatePartMasses();
   
if (log != null) if (log != null)
this.allParts[0].DumpPartToBuffer(log.buf, "", this.allParts); this.allParts[0].DumpPartToBuffer(log.buf, "", this.allParts);
   
// Create the Stage object for this stage // Create the Stage object for this stage
Stage stage = new Stage(); Stage stage = new Stage();
   
this.stageTime = 0d; this.stageTime = 0d;
this.vecStageDeltaV = Vector3.zero; this.vecStageDeltaV = Vector3.zero;
   
this.stageStartCom = this.ShipCom; this.stageStartCom = this.ShipCom;
   
this.stepStartMass = this.stageStartMass; this.stepStartMass = this.stageStartMass;
this.stepEndMass = 0; this.stepEndMass = 0;
   
this.CalculateThrustAndISP(); this.CalculateThrustAndISP();
   
// Store various things in the Stage object // Store various things in the Stage object
stage.thrust = this.totalStageThrust; stage.thrust = this.totalStageThrust;
if (log != null) log.buf.AppendLine("stage.thrust = " + stage.thrust); if (log != null) log.buf.AppendLine("stage.thrust = " + stage.thrust);
stage.thrustToWeight = this.totalStageThrust / (this.stageStartMass * this.gravity); stage.thrustToWeight = this.totalStageThrust / (this.stageStartMass * this.gravity);
stage.maxThrustToWeight = stage.thrustToWeight; stage.maxThrustToWeight = stage.thrustToWeight;
if (log != null) log.buf.AppendLine("StageMass = " + stageStartMass); if (log != null) log.buf.AppendLine("StageMass = " + stageStartMass);
if (log != null) log.buf.AppendLine("Initial maxTWR = " + stage.maxThrustToWeight); if (log != null) log.buf.AppendLine("Initial maxTWR = " + stage.maxThrustToWeight);
stage.actualThrust = this.totalStageActualThrust; stage.actualThrust = this.totalStageActualThrust;
stage.actualThrustToWeight = this.totalStageActualThrust / (this.stageStartMass * this.gravity); stage.actualThrustToWeight = this.totalStageActualThrust / (this.stageStartMass * this.gravity);
   
// calculate torque and associates // calculate torque and associates
stage.maxThrustTorque = this.totalStageThrustForce.TorqueAt(this.stageStartCom).magnitude; stage.maxThrustTorque = this.totalStageThrustForce.TorqueAt(this.stageStartCom).magnitude;
   
// torque divided by thrust. imagine that all engines are at the end of a lever that tries to turn the ship. // torque divided by thrust. imagine that all engines are at the end of a lever that tries to turn the ship.
// this numerical value, in meters, would represent the length of that lever. // this numerical value, in meters, would represent the length of that lever.
double torqueLeverArmLength = (stage.thrust <= 0) ? 0 : stage.maxThrustTorque / stage.thrust; double torqueLeverArmLength = (stage.thrust <= 0) ? 0 : stage.maxThrustTorque / stage.thrust;
   
// how far away are the engines from the CoM, actually? // how far away are the engines from the CoM, actually?
double thrustDistance = (this.stageStartCom - this.totalStageThrustForce.GetAverageForceApplicationPoint()).magnitude; double thrustDistance = (this.stageStartCom - this.totalStageThrustForce.GetAverageForceApplicationPoint()).magnitude;
   
// the combination of the above two values gives an approximation of the offset angle. // the combination of the above two values gives an approximation of the offset angle.
double sinThrustOffsetAngle = 0; double sinThrustOffsetAngle = 0;
if (thrustDistance > 1e-7) { if (thrustDistance > 1e-7) {
sinThrustOffsetAngle = torqueLeverArmLength / thrustDistance; sinThrustOffsetAngle = torqueLeverArmLength / thrustDistance;
if (sinThrustOffsetAngle > 1) { if (sinThrustOffsetAngle > 1) {
sinThrustOffsetAngle = 1; sinThrustOffsetAngle = 1;
} }
} }
   
stage.thrustOffsetAngle = Math.Asin(sinThrustOffsetAngle) * 180 / Math.PI; stage.thrustOffsetAngle = Math.Asin(sinThrustOffsetAngle) * 180 / Math.PI;
   
// Calculate the total cost of the vessel at this point // Calculate the total cost of the vessel at this point
stage.totalCost = 0d; stage.totalCost = 0d;
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
if (this.currentStage > allParts[i].decoupledInStage) if (this.currentStage > allParts[i].decoupledInStage)
stage.totalCost += allParts[i].GetCost(currentStage); stage.totalCost += allParts[i].GetCost(currentStage);
} }
   
// The total mass is simply the mass at the start of the stage // The total mass is simply the mass at the start of the stage
stage.totalMass = this.stageStartMass; stage.totalMass = this.stageStartMass;
   
// If we have done a previous stage // If we have done a previous stage
if (currentStage < startStage) if (currentStage < startStage)
{ {
// Calculate what the previous stage's mass and cost were by subtraction // Calculate what the previous stage's mass and cost were by subtraction
Stage prev = stages[currentStage + 1]; Stage prev = stages[currentStage + 1];
prev.cost = prev.totalCost - stage.totalCost; prev.cost = prev.totalCost - stage.totalCost;
prev.mass = prev.totalMass - stage.totalMass; prev.mass = prev.totalMass - stage.totalMass;
} }
   
// The above code will never run for the last stage so set those directly // The above code will never run for the last stage so set those directly
if (currentStage == 0) if (currentStage == 0)
{ {
stage.cost = stage.totalCost; stage.cost = stage.totalCost;
stage.mass = stage.totalMass; stage.mass = stage.totalMass;
} }
   
this.dontStageParts = dontStagePartsLists[this.currentStage]; this.dontStageParts = dontStagePartsLists[this.currentStage];
   
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Stage setup took " + this._timer.ElapsedMilliseconds + "ms"); log.buf.AppendLine("Stage setup took " + this._timer.ElapsedMilliseconds + "ms");
   
if (this.dontStageParts.Count > 0) if (this.dontStageParts.Count > 0)
{ {
log.buf.AppendLine("Parts preventing staging:"); log.buf.AppendLine("Parts preventing staging:");
for (int i = 0; i < this.dontStageParts.Count; i++) for (int i = 0; i < this.dontStageParts.Count; i++)
{ {
PartSim partSim = this.dontStageParts[i]; PartSim partSim = this.dontStageParts[i];
partSim.DumpPartToBuffer(log.buf, ""); partSim.DumpPartToBuffer(log.buf, "");
} }
} }
else else
{ {
log.buf.AppendLine("No parts preventing staging"); log.buf.AppendLine("No parts preventing staging");
} }
   
log.Flush(); log.Flush();
} }
   
   
// Now we will loop until we are allowed to stage // Now we will loop until we are allowed to stage
int loopCounter = 0; int loopCounter = 0;
while (!this.AllowedToStage()) while (!this.AllowedToStage())
{ {
loopCounter++; loopCounter++;
//MonoBehaviour.print("loop = " + loopCounter); //MonoBehaviour.print("loop = " + loopCounter);
// Calculate how long each draining tank will take to drain and run for the minimum time // Calculate how long each draining tank will take to drain and run for the minimum time
double resourceDrainTime = double.MaxValue; double resourceDrainTime = double.MaxValue;
PartSim partMinDrain = null; PartSim partMinDrain = null;
foreach (PartSim partSim in this.drainingParts) foreach (PartSim partSim in this.drainingParts)
{ {
double time = partSim.TimeToDrainResource(); double time = partSim.TimeToDrainResource();
if (time < resourceDrainTime) if (time < resourceDrainTime)
{ {
resourceDrainTime = time; resourceDrainTime = time;
partMinDrain = partSim; partMinDrain = partSim;
} }
} }
   
if (log != null) if (log != null)
{ {
MonoBehaviour.print("Drain time = " + resourceDrainTime + " (" + partMinDrain.name + ":" + partMinDrain.partId + ")"); MonoBehaviour.print("Drain time = " + resourceDrainTime + " (" + partMinDrain.name + ":" + partMinDrain.partId + ")");
} }
foreach (PartSim partSim in this.drainingParts) foreach (PartSim partSim in this.drainingParts)
{ {
partSim.DrainResources(resourceDrainTime); partSim.DrainResources(resourceDrainTime);
} }
   
// Get the mass after draining // Get the mass after draining
this.stepEndMass = this.ShipMass; this.stepEndMass = this.ShipMass;
this.stageTime += resourceDrainTime; this.stageTime += resourceDrainTime;
   
double stepEndTWR = this.totalStageThrust / (this.stepEndMass * this.gravity); double stepEndTWR = this.totalStageThrust / (this.stepEndMass * this.gravity);
//MonoBehaviour.print("After drain mass = " + stepEndMass); //MonoBehaviour.print("After drain mass = " + stepEndMass);
//MonoBehaviour.print("currentThrust = " + totalStageThrust); //MonoBehaviour.print("currentThrust = " + totalStageThrust);
//MonoBehaviour.print("currentTWR = " + stepEndTWR); //MonoBehaviour.print("currentTWR = " + stepEndTWR);
if (stepEndTWR > stage.maxThrustToWeight) if (stepEndTWR > stage.maxThrustToWeight)
{ {
stage.maxThrustToWeight = stepEndTWR; stage.maxThrustToWeight = stepEndTWR;
} }
   
//MonoBehaviour.print("newMaxTWR = " + stage.maxThrustToWeight); //MonoBehaviour.print("newMaxTWR = " + stage.maxThrustToWeight);
   
// If we have drained anything and the masses make sense then add this step's deltaV to the stage total // If we have drained anything and the masses make sense then add this step's deltaV to the stage total
if (resourceDrainTime > 0d && this.stepStartMass > this.stepEndMass && this.stepStartMass > 0d && this.stepEndMass > 0d) if (resourceDrainTime > 0d && this.stepStartMass > this.stepEndMass && this.stepStartMass > 0d && this.stepEndMass > 0d)
{ {
this.vecStageDeltaV += this.vecThrust * (float)((this.currentisp * Units.GRAVITY * Math.Log(this.stepStartMass / this.stepEndMass)) / this.simpleTotalThrust); this.vecStageDeltaV += this.vecThrust * (float)((this.currentisp * Units.GRAVITY * Math.Log(this.stepStartMass / this.stepEndMass)) / this.simpleTotalThrust);
} }
   
// Update the active engines and resource drains for the next step // Update the active engines and resource drains for the next step
this.UpdateResourceDrains(); this.UpdateResourceDrains();
   
// Recalculate the current thrust and isp for the next step // Recalculate the current thrust and isp for the next step
this.CalculateThrustAndISP(); this.CalculateThrustAndISP();
// Check if we actually changed anything // Check if we actually changed anything
if (this.stepStartMass == this.stepEndMass) if (this.stepStartMass == this.stepEndMass)
{ {
//MonoBehaviour.print("No change in mass"); //MonoBehaviour.print("No change in mass");
break; break;
} }
   
// Check to stop rampant looping // Check to stop rampant looping
if (loopCounter == 1000) if (loopCounter == 1000)
{ {
MonoBehaviour.print("exceeded loop count"); MonoBehaviour.print("exceeded loop count");
MonoBehaviour.print("stageStartMass = " + this.stageStartMass); MonoBehaviour.print("stageStartMass = " + this.stageStartMass);
MonoBehaviour.print("stepStartMass = " + this.stepStartMass); MonoBehaviour.print("stepStartMass = " + this.stepStartMass);
MonoBehaviour.print("StepEndMass = " + this.stepEndMass); MonoBehaviour.print("StepEndMass = " + this.stepEndMass);
Logger.Log("exceeded loop count"); Logger.Log("exceeded loop count");
Logger.Log("stageStartMass = " + this.stageStartMass); Logger.Log("stageStartMass = " + this.stageStartMass);
Logger.Log("stepStartMass = " + this.stepStartMass); Logger.Log("stepStartMass = " + this.stepStartMass);
Logger.Log("StepEndMass = " + this.stepEndMass); Logger.Log("StepEndMass = " + this.stepEndMass);
break; break;
} }
   
// The next step starts at the mass this one ended at // The next step starts at the mass this one ended at
this.stepStartMass = this.stepEndMass; this.stepStartMass = this.stepEndMass;
} }
   
   
// Store more values in the Stage object and stick it in the array // Store more values in the Stage object and stick it in the array
   
// Store the magnitude of the deltaV vector // Store the magnitude of the deltaV vector
stage.deltaV = this.vecStageDeltaV.magnitude; stage.deltaV = this.vecStageDeltaV.magnitude;
stage.resourceMass = this.stageStartMass - this.stepEndMass; stage.resourceMass = this.stageStartMass - this.stepEndMass;
   
// Recalculate effective stage isp from the stage deltaV (flip the standard deltaV calculation around) // Recalculate effective stage isp from the stage deltaV (flip the standard deltaV calculation around)
// Note: If the mass doesn't change then this is a divide by zero // Note: If the mass doesn't change then this is a divide by zero
if (this.stageStartMass != this.stepStartMass) if (this.stageStartMass != this.stepStartMass)
{ {
stage.isp = stage.deltaV / (Units.GRAVITY * Math.Log(this.stageStartMass / this.stepStartMass)); stage.isp = stage.deltaV / (Units.GRAVITY * Math.Log(this.stageStartMass / this.stepStartMass));
} }
else else
{ {
stage.isp = 0; stage.isp = 0;
} }
   
// Zero stage time if more than a day (this should be moved into the window code) // Zero stage time if more than a day (this should be moved into the window code)
stage.time = (this.stageTime < SECONDS_PER_DAY) ? this.stageTime : 0d; stage.time = (this.stageTime < SECONDS_PER_DAY) ? this.stageTime : 0d;
stage.number = this.doingCurrent ? -1 : this.currentStage; // Set the stage number to -1 if doing current engines stage.number = this.doingCurrent ? -1 : this.currentStage; // Set the stage number to -1 if doing current engines
stage.totalPartCount = this.allParts.Count; stage.totalPartCount = this.allParts.Count;
stage.maxMach = maxMach; stage.maxMach = maxMach;
stages[this.currentStage] = stage; stages[this.currentStage] = stage;
   
// Now activate the next stage // Now activate the next stage
this.currentStage--; this.currentStage--;
this.doingCurrent = false; this.doingCurrent = false;
   
if (log != null) if (log != null)
{ {
// Log how long the stage took // Log how long the stage took
this._timer.Stop(); this._timer.Stop();
MonoBehaviour.print("Simulating stage took " + this._timer.ElapsedMilliseconds + "ms"); MonoBehaviour.print("Simulating stage took " + this._timer.ElapsedMilliseconds + "ms");
stage.Dump(); stage.Dump();
this._timer.Reset(); this._timer.Reset();
this._timer.Start(); this._timer.Start();
} }
   
// Activate the next stage // Activate the next stage
this.ActivateStage(); this.ActivateStage();
   
if (log != null) if (log != null)
{ {
// Log how long it took to activate // Log how long it took to activate
this._timer.Stop(); this._timer.Stop();
MonoBehaviour.print("ActivateStage took " + this._timer.ElapsedMilliseconds + "ms"); MonoBehaviour.print("ActivateStage took " + this._timer.ElapsedMilliseconds + "ms");
} }
} }
   
// Now we add up the various total fields in the stages // Now we add up the various total fields in the stages
for (int i = 0; i < stages.Length; i++) for (int i = 0; i < stages.Length; i++)
{ {
// For each stage we total up the cost, mass, deltaV and time for this stage and all the stages above // For each stage we total up the cost, mass, deltaV and time for this stage and all the stages above
for (int j = i; j >= 0; j--) for (int j = i; j >= 0; j--)
{ {
stages[i].totalDeltaV += stages[j].deltaV; stages[i].totalDeltaV += stages[j].deltaV;
stages[i].totalTime += stages[j].time; stages[i].totalTime += stages[j].time;
stages[i].partCount = i > 0 ? stages[i].totalPartCount - stages[i - 1].totalPartCount : stages[i].totalPartCount; stages[i].partCount = i > 0 ? stages[i].totalPartCount - stages[i - 1].totalPartCount : stages[i].totalPartCount;
} }
// We also total up the deltaV for stage and all stages below // We also total up the deltaV for stage and all stages below
for (int j = i; j < stages.Length; j++) for (int j = i; j < stages.Length; j++)
{ {
stages[i].inverseTotalDeltaV += stages[j].deltaV; stages[i].inverseTotalDeltaV += stages[j].deltaV;
} }
   
// Zero the total time if the value will be huge (24 hours?) to avoid the display going weird // Zero the total time if the value will be huge (24 hours?) to avoid the display going weird
// (this should be moved into the window code) // (this should be moved into the window code)
if (stages[i].totalTime > SECONDS_PER_DAY) if (stages[i].totalTime > SECONDS_PER_DAY)
{ {
stages[i].totalTime = 0d; stages[i].totalTime = 0d;
} }
} }
   
if (log != null) if (log != null)
{ {
this._timer.Stop(); this._timer.Stop();
MonoBehaviour.print("RunSimulation: " + this._timer.ElapsedMilliseconds + "ms"); MonoBehaviour.print("RunSimulation: " + this._timer.ElapsedMilliseconds + "ms");
} }
FreePooledObject(); FreePooledObject();
return stages; return stages;
} }
   
public double UpdatePartMasses() public double UpdatePartMasses()
{ {
for (int i = 0; i < this.allParts.Count; i++) for (int i = 0; i < this.allParts.Count; i++)
{ {
this.allParts[i].baseMass = this.allParts[i].realMass; this.allParts[i].baseMass = this.allParts[i].realMass;
this.allParts[i].baseMassForCoM = this.allParts[i].realMass; this.allParts[i].baseMassForCoM = this.allParts[i].realMass;
} }
   
for (int i = 0; i < this.allParts.Count; i++) for (int i = 0; i < this.allParts.Count; i++)
{ {
PartSim part = this.allParts[i]; PartSim part = this.allParts[i];
   
// Check if part should pass it's mass onto its parent. // Check if part should pass it's mass onto its parent.
if (part.isNoPhysics && part.parent != null) if (part.isNoPhysics && part.parent != null)
{ {
PartSim partParent = part.parent; PartSim partParent = part.parent;
   
// Loop through all parents until a physically significant parent is found. // Loop through all parents until a physically significant parent is found.
while (partParent != null) while (partParent != null)
{ {
// Check if parent is physically significant. // Check if parent is physically significant.
if (partParent.isNoPhysics == false) if (partParent.isNoPhysics == false)
{ {
// Apply the mass to the parent and remove it from the originating part. // Apply the mass to the parent and remove it from the originating part.
partParent.baseMassForCoM += part.baseMassForCoM; partParent.baseMassForCoM += part.baseMassForCoM;
part.baseMassForCoM = 0.0; part.baseMassForCoM = 0.0;
   
// Break out of the recursive loop. // Break out of the recursive loop.
break; break;
} }
   
// Recursively loop through the parent parts. // Recursively loop through the parent parts.
partParent = partParent.parent; partParent = partParent.parent;
} }
} }
} }
   
double totalMass = 0d; double totalMass = 0d;
for (int i = 0; i < this.allParts.Count; i++) for (int i = 0; i < this.allParts.Count; i++)
{ {
totalMass += this.allParts[i].startMass = this.allParts[i].GetMass(currentStage); totalMass += this.allParts[i].startMass = this.allParts[i].GetMass(currentStage);
} }
   
return totalMass; return totalMass;
} }
   
// Make sure we free them all, even if they should all be free already at this point // Make sure we free them all, even if they should all be free already at this point
public void FreePooledObject() public void FreePooledObject()
{ {
//MonoBehaviour.print("FreePooledObject pool size before = " + PartSim.pool.Count() + " for " + allParts.Count + " parts"); //MonoBehaviour.print("FreePooledObject pool size before = " + PartSim.pool.Count() + " for " + allParts.Count + " parts");
foreach (PartSim part in allParts) foreach (PartSim part in allParts)
{ {
part.Release(); part.Release();
} }
//MonoBehaviour.print("FreePooledObject pool size after = " + PartSim.pool.Count()); //MonoBehaviour.print("FreePooledObject pool size after = " + PartSim.pool.Count());
   
//MonoBehaviour.print("FreePooledObject pool size before = " + EngineSim.pool.Count() + " for " + allEngines.Count + " engines"); //MonoBehaviour.print("FreePooledObject pool size before = " + EngineSim.pool.Count() + " for " + allEngines.Count + " engines");
foreach (EngineSim engine in allEngines) foreach (EngineSim engine in allEngines)
{ {
engine.Release(); engine.Release();
} }
//MonoBehaviour.print("FreePooledObject pool size after = " + EngineSim.pool.Count()); //MonoBehaviour.print("FreePooledObject pool size after = " + EngineSim.pool.Count());
} }
   
private void BuildDontStageLists(LogMsg log) private void BuildDontStageLists(LogMsg log)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("Creating list with capacity of " + (this.currentStage + 1)); log.buf.AppendLine("Creating list with capacity of " + (this.currentStage + 1));
} }
   
dontStagePartsLists.Clear(); dontStagePartsLists.Clear();
for (int i = 0; i <= this.currentStage; i++) for (int i = 0; i <= this.currentStage; i++)
{ {
if (i < dontStagePartsLists.Count) if (i < dontStagePartsLists.Count)
{ {
dontStagePartsLists[i].Clear(); dontStagePartsLists[i].Clear();
} }
else else
{ {
dontStagePartsLists.Add(new List<PartSim>()); dontStagePartsLists.Add(new List<PartSim>());
} }
} }
   
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
PartSim partSim = allParts[i]; PartSim partSim = allParts[i];
   
if (partSim.isEngine || !partSim.Resources.Empty) if (partSim.isEngine || !partSim.Resources.Empty)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine( log.buf.AppendLine(
partSim.name + ":" + partSim.partId + " is engine or tank, decoupled = " + partSim.decoupledInStage); partSim.name + ":" + partSim.partId + " is engine or tank, decoupled = " + partSim.decoupledInStage);
} }
   
if (partSim.decoupledInStage < -1 || partSim.decoupledInStage > this.currentStage - 1) if (partSim.decoupledInStage < -1 || partSim.decoupledInStage > this.currentStage - 1)
{ {
if (log != null) if (log != null)
{ {
log.buf.AppendLine("decoupledInStage out of range"); log.buf.AppendLine("decoupledInStage out of range");
} }
} }
else else
{ {
dontStagePartsLists[partSim.decoupledInStage + 1].Add(partSim); dontStagePartsLists[partSim.decoupledInStage + 1].Add(partSim);
} }
} }
} }
   
for (int i = 1; i <= this.lastStage; i++) for (int i = 1; i <= this.lastStage; i++)
{ {
if (dontStagePartsLists[i].Count == 0) if (dontStagePartsLists[i].Count == 0)
{ {
dontStagePartsLists[i] = dontStagePartsLists[i - 1]; dontStagePartsLists[i] = dontStagePartsLists[i - 1];
} }
} }
} }
   
// This function simply rebuilds the active engines by testing the isActive flag of all the engines // This function simply rebuilds the active engines by testing the isActive flag of all the engines
private void UpdateActiveEngines() private void UpdateActiveEngines()
{ {
this.activeEngines.Clear(); this.activeEngines.Clear();
for (int i = 0; i < allEngines.Count; ++i) for (int i = 0; i < allEngines.Count; ++i)
{ {
EngineSim engine = allEngines[i]; EngineSim engine = allEngines[i];
if (engine.isActive) if (engine.isActive && engine.isFlamedOut == false)
{ {
this.activeEngines.Add(engine); this.activeEngines.Add(engine);
} }
} }
} }
   
private void CalculateThrustAndISP() private void CalculateThrustAndISP()
{ {
// Reset all the values // Reset all the values
this.vecThrust = Vector3.zero; this.vecThrust = Vector3.zero;
this.vecActualThrust = Vector3.zero; this.vecActualThrust = Vector3.zero;
this.simpleTotalThrust = 0d; this.simpleTotalThrust = 0d;
this.totalStageThrust = 0d; this.totalStageThrust = 0d;
this.totalStageActualThrust = 0d; this.totalStageActualThrust = 0d;
this.totalStageFlowRate = 0d; this.totalStageFlowRate = 0d;
this.totalStageIspFlowRate = 0d; this.totalStageIspFlowRate = 0d;
this.totalStageThrustForce.Reset(); this.totalStageThrustForce.Reset();
   
// Loop through all the active engines totalling the thrust, actual thrust and mass flow rates // Loop through all the active engines totalling the thrust, actual thrust and mass flow rates
// The thrust is totalled as vectors // The thrust is totalled as vectors
for (int i = 0; i < activeEngines.Count; ++i) for (int i = 0; i < activeEngines.Count; ++i)
{ {
EngineSim engine = activeEngines[i]; EngineSim engine = activeEngines[i];
   
this.simpleTotalThrust += engine.thrust; this.simpleTotalThrust += engine.thrust;
this.vecThrust += ((float)engine.thrust * engine.thrustVec); this.vecThrust += ((float)engine.thrust * engine.thrustVec);
this.vecActualThrust += ((float)engine.actualThrust * engine.thrustVec); this.vecActualThrust += ((float)engine.actualThrust * engine.thrustVec);
   
this.totalStageFlowRate += engine.ResourceConsumptions.Mass; this.totalStageFlowRate += engine.ResourceConsumptions.Mass;
this.totalStageIspFlowRate += engine.ResourceConsumptions.Mass * engine.isp; this.totalStageIspFlowRate += engine.ResourceConsumptions.Mass * engine.isp;
   
for (int j = 0; j < engine.appliedForces.Count; ++j) for (int j = 0; j < engine.appliedForces.Count; ++j)
{ {
this.totalStageThrustForce.AddForce(engine.appliedForces[j]); this.totalStageThrustForce.AddForce(engine.appliedForces[j]);
} }
} }
//MonoBehaviour.print("vecThrust = " + vecThrust.ToString() + " magnitude = " + vecThrust.magnitude); //MonoBehaviour.print("vecThrust = " + vecThrust.ToString() + " magnitude = " + vecThrust.magnitude);
this.totalStageThrust = this.vecThrust.magnitude; this.totalStageThrust = this.vecThrust.magnitude;
this.totalStageActualThrust = this.vecActualThrust.magnitude; this.totalStageActualThrust = this.vecActualThrust.magnitude;
   
// Calculate the effective isp at this point // Calculate the effective isp at this point
if (this.totalStageFlowRate > 0d && this.totalStageIspFlowRate > 0d) if (this.totalStageFlowRate > 0d && this.totalStageIspFlowRate > 0d)
{ {
this.currentisp = this.totalStageIspFlowRate / this.totalStageFlowRate; this.currentisp = this.totalStageIspFlowRate / this.totalStageFlowRate;
} }
else else
{ {
this.currentisp = 0; this.currentisp = 0;
} }
} }
   
// This function does all the hard work of working out which engines are burning, which tanks are being drained // This function does all the hard work of working out which engines are burning, which tanks are being drained
// and setting the drain rates // and setting the drain rates
private void UpdateResourceDrains() private void UpdateResourceDrains()
{ {
// Update the active engines // Update the active engines
this.UpdateActiveEngines(); this.UpdateActiveEngines();
   
// Empty the draining resources set // Empty the draining resources set
this.drainingResources.Clear(); this.drainingResources.Clear();
   
// Reset the resource drains of all draining parts // Reset the resource drains of all draining parts
foreach (PartSim partSim in this.drainingParts) foreach (PartSim partSim in this.drainingParts)
{ {
partSim.ResourceDrains.Reset(); partSim.ResourceDrains.Reset();
} }
   
// Empty the draining parts set // Empty the draining parts set
this.drainingParts.Clear(); this.drainingParts.Clear();
   
// Loop through all the active engine modules // Loop through all the active engine modules
for (int i = 0; i < activeEngines.Count; ++i) for (int i = 0; i < activeEngines.Count; ++i)
{ {
EngineSim engine = activeEngines[i]; EngineSim engine = activeEngines[i];
   
// Set the resource drains for this engine // Set the resource drains for this engine
if (engine.SetResourceDrains(this.allParts, this.allFuelLines, this.drainingParts)) if (engine.SetResourceDrains(this.allParts, this.allFuelLines, this.drainingParts))
{ {
// If it is active then add the consumed resource types to the set // If it is active then add the consumed resource types to the set
for (int j = 0; j < engine.ResourceConsumptions.Types.Count; ++j) for (int j = 0; j < engine.ResourceConsumptions.Types.Count; ++j)
{ {
drainingResources.Add(engine.ResourceConsumptions.Types[j]); drainingResources.Add(engine.ResourceConsumptions.Types[j]);
} }
} }
} }
   
// Update the active engines again to remove any engines that have no fuel supply // Update the active engines again to remove any engines that have no fuel supply
this.UpdateActiveEngines(); this.UpdateActiveEngines();
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
StringBuilder buffer = new StringBuilder(1024); StringBuilder buffer = new StringBuilder(1024);
buffer.AppendFormat("Active engines = {0:d}\n", this.activeEngines.Count); buffer.AppendFormat("Active engines = {0:d}\n", this.activeEngines.Count);
int i = 0; int i = 0;
for (int j = 0; j < this.activeEngines.Count; j++) for (int j = 0; j < this.activeEngines.Count; j++)
{ {
EngineSim engine = this.activeEngines[j]; EngineSim engine = this.activeEngines[j];
engine.DumpEngineToBuffer(buffer, "Engine " + (i++) + ":"); engine.DumpEngineToBuffer(buffer, "Engine " + (i++) + ":");
} }
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
} }
   
// This function works out if it is time to stage // This function works out if it is time to stage
private bool AllowedToStage() private bool AllowedToStage()
{ {
StringBuilder buffer = null; StringBuilder buffer = null;
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
buffer = new StringBuilder(1024); buffer = new StringBuilder(1024);
buffer.AppendLine("AllowedToStage"); buffer.AppendLine("AllowedToStage");
buffer.AppendFormat("currentStage = {0:d}\n", this.currentStage); buffer.AppendFormat("currentStage = {0:d}\n", this.currentStage);
} }
   
if (this.activeEngines.Count > 0) if (this.activeEngines.Count > 0)
{ {
for (int i = 0; i < dontStageParts.Count; ++i) for (int i = 0; i < dontStageParts.Count; ++i)
{ {
PartSim partSim = dontStageParts[i]; PartSim partSim = dontStageParts[i];
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
partSim.DumpPartToBuffer(buffer, "Testing: "); partSim.DumpPartToBuffer(buffer, "Testing: ");
} }
//buffer.AppendFormat("isSepratron = {0}\n", partSim.isSepratron ? "true" : "false"); //buffer.AppendFormat("isSepratron = {0}\n", partSim.isSepratron ? "true" : "false");
   
if (!partSim.isSepratron && !partSim.EmptyOf(this.drainingResources)) if (!partSim.isSepratron && !partSim.EmptyOf(this.drainingResources))
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
partSim.DumpPartToBuffer(buffer, "Decoupled part not empty => false: "); partSim.DumpPartToBuffer(buffer, "Decoupled part not empty => false: ");
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
return false; return false;
} }
   
if (partSim.isEngine) if (partSim.isEngine)
{ {
for (int j = 0; j < activeEngines.Count; ++j) for (int j = 0; j < activeEngines.Count; ++j)
{ {
EngineSim engine = activeEngines[j]; EngineSim engine = activeEngines[j];
   
if (engine.partSim == partSim) if (engine.partSim == partSim)
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
partSim.DumpPartToBuffer(buffer, "Decoupled part is active engine => false: "); partSim.DumpPartToBuffer(buffer, "Decoupled part is active engine => false: ");
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
return false; return false;
} }
} }
} }
} }
} }
   
if (this.currentStage == 0 && this.doingCurrent) if (this.currentStage == 0 && this.doingCurrent)
{ {
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
buffer.AppendLine("Current stage == 0 && doingCurrent => false"); buffer.AppendLine("Current stage == 0 && doingCurrent => false");
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
return false; return false;
} }
   
if (SimManager.logOutput) if (SimManager.logOutput)
{ {
buffer.AppendLine("Returning true"); buffer.AppendLine("Returning true");
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
return true; return true;
} }
   
// This function activates the next stage // This function activates the next stage
// currentStage must be updated before calling this function // currentStage must be updated before calling this function
private void ActivateStage() private void ActivateStage()
{ {
// Build a set of all the parts that will be decoupled // Build a set of all the parts that will be decoupled
decoupledParts.Clear(); decoupledParts.Clear();
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
PartSim partSim = allParts[i]; PartSim partSim = allParts[i];
   
if (partSim.decoupledInStage >= this.currentStage) if (partSim.decoupledInStage >= this.currentStage)
{ {
decoupledParts.Add(partSim); decoupledParts.Add(partSim);
} }
} }
   
foreach (PartSim partSim in decoupledParts) foreach (PartSim partSim in decoupledParts)
{ {
// Remove it from the all parts list // Remove it from the all parts list
this.allParts.Remove(partSim); this.allParts.Remove(partSim);
partSim.Release(); partSim.Release();
if (partSim.isEngine) if (partSim.isEngine)
{ {
// If it is an engine then loop through all the engine modules and remove all the ones from this engine part // If it is an engine then loop through all the engine modules and remove all the ones from this engine part
for (int i = this.allEngines.Count - 1; i >= 0; i--) for (int i = this.allEngines.Count - 1; i >= 0; i--)
{ {
EngineSim engine = this.allEngines[i]; EngineSim engine = this.allEngines[i];
if (engine.partSim == partSim) if (engine.partSim == partSim)
{ {
this.allEngines.RemoveAt(i); this.allEngines.RemoveAt(i);
engine.Release(); engine.Release();
} }
} }
} }
// If it is a fuel line then remove it from the list of all fuel lines // If it is a fuel line then remove it from the list of all fuel lines
if (partSim.isFuelLine) if (partSim.isFuelLine)
{ {
this.allFuelLines.Remove(partSim); this.allFuelLines.Remove(partSim);
} }
} }
   
// Loop through all the (remaining) parts // Loop through all the (remaining) parts
for (int i = 0; i < allParts.Count; ++i) for (int i = 0; i < allParts.Count; ++i)
{ {
// Ask the part to remove all the parts that are decoupled // Ask the part to remove all the parts that are decoupled
allParts[i].RemoveAttachedParts(decoupledParts); allParts[i].RemoveAttachedParts(decoupledParts);
} }
   
// Now we loop through all the engines and activate those that are ignited in this stage // Now we loop through all the engines and activate those that are ignited in this stage
for (int i = 0; i < allEngines.Count; ++i) for (int i = 0; i < allEngines.Count; ++i)
{ {
EngineSim engine = allEngines[i]; EngineSim engine = allEngines[i];
if (engine.partSim.inverseStage == this.currentStage) if (engine.partSim.inverseStage == this.currentStage)
{ {
engine.isActive = true; engine.isActive = true;
} }
} }
} }
   
public void Dump() public void Dump()
{ {
StringBuilder buffer = new StringBuilder(1024); StringBuilder buffer = new StringBuilder(1024);
buffer.AppendFormat("Part count = {0:d}\n", this.allParts.Count); buffer.AppendFormat("Part count = {0:d}\n", this.allParts.Count);
   
// Output a nice tree view of the rocket // Output a nice tree view of the rocket
if (this.allParts.Count > 0) if (this.allParts.Count > 0)
{ {
PartSim root = this.allParts[0]; PartSim root = this.allParts[0];
while (root.parent != null) while (root.parent != null)
{ {
root = root.parent; root = root.parent;
} }
   
if (root.hasVessel) if (root.hasVessel)
{ {
buffer.AppendFormat("vesselName = '{0}' vesselType = {1}\n", this.vesselName, SimManager.GetVesselTypeString(this.vesselType)); buffer.AppendFormat("vesselName = '{0}' vesselType = {1}\n", this.vesselName, SimManager.GetVesselTypeString(this.vesselType));
} }
   
root.DumpPartToBuffer(buffer, "", this.allParts); root.DumpPartToBuffer(buffer, "", this.allParts);
} }
   
MonoBehaviour.print(buffer); MonoBehaviour.print(buffer);
} }
} }
} }
   
 Binary files a/Output/KerbalEngineer/KerbalEngineer.Unity.dll and b/Output/KerbalEngineer/KerbalEngineer.Unity.dll differ
 Binary files a/Output/KerbalEngineer/KerbalEngineer.dll and b/Output/KerbalEngineer/KerbalEngineer.dll differ
{ {
"NAME":"Kerbal Engineer Redux", "NAME":"Kerbal Engineer Redux",
"URL":"http://ksp-avc.cybutek.net/version.php?id=6", "URL":"http://ksp-avc.cybutek.net/version.php?id=6",
"VERSION": "VERSION":
{ {
"MAJOR":1, "MAJOR":1,
"MINOR":1, "MINOR":1,
"PATCH":0, "PATCH":1,
"BUILD":2 "BUILD":0
}, },
"KSP_VERSION": "KSP_VERSION":
{ {
"MAJOR":1, "MAJOR":1,
"MINOR":1, "MINOR":1,
"PATCH":0 "PATCH":1
} }
} }