Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 36e52307b5bc
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d277fba74680
Choose a head ref
  • 8 commits
  • 4 files changed
  • 2 contributors

Commits on Mar 13, 2017

  1. Copy the full SHA
    0f37c8f View commit details
  2. correctness

    eggrobin committed Mar 13, 2017
    Copy the full SHA
    b2e4c82 View commit details
  3. Copy the full SHA
    661fb66 View commit details
  4. spaaace

    eggrobin committed Mar 13, 2017
    Copy the full SHA
    56d7e02 View commit details
  5. traces

    eggrobin committed Mar 13, 2017
    Copy the full SHA
    0cb1c87 View commit details
  6. better time handling

    eggrobin committed Mar 13, 2017
    Copy the full SHA
    99f7ac4 View commit details
  7. remove traces

    eggrobin committed Mar 13, 2017
    Copy the full SHA
    a9384f9 View commit details
  8. Merge pull request #1255 from eggrobin/recomputing-rotations

    Timing and miscellaneous C# issues
    pleroy authored Mar 13, 2017
    Copy the full SHA
    d277fba View commit details
Showing with 85 additions and 52 deletions.
  1. +7 −0 ksp_plugin/interface.cpp
  2. +3 −2 ksp_plugin/plugin.hpp
  3. +58 −49 ksp_plugin_adapter/ksp_plugin_adapter.cs
  4. +17 −1 serialization/journal.proto
7 changes: 7 additions & 0 deletions ksp_plugin/interface.cpp
Original file line number Diff line number Diff line change
@@ -626,6 +626,13 @@ bool principia__IsKspStockSystem(Plugin* const plugin) {
return m.Return(plugin->IsKspStockSystem());
}

bool principia__IsLoaded(Plugin const* const plugin,
char const* const vessel_guid) {
journal::Method<journal::IsLoaded> m({plugin, vessel_guid});
CHECK_NOTNULL(plugin);
return m.Return(plugin->is_loaded(GetVessel(*plugin, vessel_guid)));
}

// Exports |LOG(SEVERITY) << text| for fast logging from the C# adapter.
// This will always evaluate its argument even if the corresponding log severity
// is disabled, so it is less efficient than LOG(INFO). It will not report the
5 changes: 3 additions & 2 deletions ksp_plugin/plugin.hpp
Original file line number Diff line number Diff line change
@@ -156,6 +156,9 @@ class Plugin {
bool loaded,
bool& inserted);

// Whether |loaded_vessels_| contains |vessel|.
bool is_loaded(not_null<Vessel*> vessel) const;

// Adds a part with the given |part_id| to the vessel with the given |GUID|,
// which must be unloaded, putting the part at the given offset from the
// parent body of the vessel. The part is given unit mass; this does not
@@ -436,8 +439,6 @@ class Plugin {
Mass mass,
DegreesOfFreedom<Barycentric> const& degrees_of_freedom);

// Whether |loaded_vessels_| contains |vessel|.
bool is_loaded(not_null<Vessel*> vessel) const;
// Whether |new_unloaded_vessels_| contains |vessel|.
bool is_new_unloaded(not_null<Vessel*> vessel) const;

107 changes: 58 additions & 49 deletions ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
@@ -88,6 +88,11 @@ public partial class PrincipiaPluginAdapter
[KSPField(isPersistant = true)]
private bool show_crash_options_ = false;
#endif
// Timing diagnostics.
private System.Diagnostics.Stopwatch stopwatch_ =
new System.Diagnostics.Stopwatch();
private double last_universal_time_;
private double slowdown_;

private bool time_is_advancing_;

@@ -353,6 +358,9 @@ public override void OnAwake() {
// TimingPre, -101 on the script execution order page.
TimingManager.FixedUpdateAdd(TimingManager.TimingStage.Precalc,
SetBodyFramesAndPrecalculateVessels);
// Timing1, -99 on the script execution order page.
TimingManager.FixedUpdateAdd(TimingManager.TimingStage.Early,
UpdateVesselOrbits);
// Timing3, 7.
TimingManager.FixedUpdateAdd(TimingManager.TimingStage.FashionablyLate,
ReportVesselsAndParts);
@@ -641,8 +649,15 @@ private void FixedUpdate() {
Log.Info("Setting GameSettings.ORBIT_WARP_DOWN_AT_SOI to false");
GameSettings.ORBIT_WARP_DOWN_AT_SOI = false;
}

if (PluginRunning()) {
double universal_time = Planetarium.GetUniversalTime();
slowdown_ = stopwatch_.ElapsedMilliseconds /
((universal_time - last_universal_time_) * 1000.0);
last_universal_time_ = universal_time;
stopwatch_.Reset();
stopwatch_.Start();

plugin_.SetMainBody(
FlightGlobals.currentMainBody.GetValueOrDefault(
FlightGlobals.GetHomeBody()).flightGlobalsIndex);
@@ -651,7 +666,7 @@ private void FixedUpdate() {
bool ready_to_draw_active_vessel_trajectory =
draw_active_vessel_trajectory() &&
plugin_.HasVessel(active_vessel.id.ToString());

if (ready_to_draw_active_vessel_trajectory) {
// TODO(egg): make the speed tolerance independent. Also max_steps.
AdaptiveStepParameters adaptive_step_parameters =
@@ -673,26 +688,13 @@ private void FixedUpdate() {
}
plugin_.ForgetAllHistoriesBefore(universal_time -
history_lengths_[history_length_index_]);
if (FlightGlobals.currentMainBody != null) {
FlightGlobals.currentMainBody.rotationPeriod =
plugin_.CelestialRotationPeriod(
FlightGlobals.currentMainBody.flightGlobalsIndex);
FlightGlobals.currentMainBody.initialRotation =
plugin_.CelestialInitialRotationInDegrees(
FlightGlobals.currentMainBody.flightGlobalsIndex);
}
ApplyToBodyTree(body => UpdateBody(body, universal_time));
SetBodyFrames();
ApplyToVesselsOnRails(vessel => UpdateVessel(vessel, universal_time));
// TODO(egg): Set the degrees of freedom of the origin of |World| (by
// toying with Krakensbane and FloatingOrigin) here.

// Now we let the game and Unity do their thing. among other things,
// the FashionablyLate callbacks, including ReportNonConservativeForces,
// then the FlightIntegrator's FixedUpdate will run, then the Vessel's,
// and eventually the physics simulation.
StartCoroutine(
AdvanceTimeAndNudgeVesselsAfterPhysicsSimulation(universal_time));
}
}

@@ -710,20 +712,16 @@ private void OnDisable() {
DisableVesselPrecalculate);
TimingManager.FixedUpdateRemove(TimingManager.TimingStage.Precalc,
SetBodyFramesAndPrecalculateVessels);
TimingManager.FixedUpdateRemove(TimingManager.TimingStage.Early,
UpdateVesselOrbits);
TimingManager.FixedUpdateRemove(TimingManager.TimingStage.FashionablyLate,
ReportVesselsAndParts);
}

#endregion

private System.Collections.IEnumerator
AdvanceTimeAndNudgeVesselsAfterPhysicsSimulation(double universal_time) {
yield return new UnityEngine.WaitForFixedUpdate();
// Unity's physics has just finished doing its thing. If we correct the
// positions here, nobody will know that they're not the ones obtained by
// Unity. Careful however: while the positions here are those of the next
// step, the Planetarium hasn't run yet, and still has its old time.
universal_time += Planetarium.TimeScale * Planetarium.fetch.fixedDeltaTime;
private void AdvanceTimeAndNudgeVesselsAfterPhysicsSimulation() {
double universal_time = Planetarium.GetUniversalTime();

plugin_.PrepareToReportCollisions();

@@ -733,7 +731,9 @@ private System.Collections.IEnumerator
// physics simulation, which is why we report them before calling
// |AdvanceTime|.
foreach (Vessel vessel1 in FlightGlobals.VesselsLoaded) {
if (plugin_.HasVessel(vessel1.id.ToString()) && !vessel1.packed) {
if (plugin_.HasVessel(vessel1.id.ToString()) &&
plugin_.IsLoaded(vessel1.id.ToString()) &&
!vessel1.packed) {
if (vessel1.isEVA && vessel1.evaController.OnALadder) {
var vessel2 = vessel1.evaController.LadderPart.vessel;
if (vessel2 != null && plugin_.HasVessel(vessel2.id.ToString()) &&
@@ -747,7 +747,9 @@ private System.Collections.IEnumerator
var part2 =
collider.gameObject.GetComponentUpwards<Part>();
var vessel2 = part2.vessel;
if (vessel2 != null && plugin_.HasVessel(vessel2.id.ToString())) {
if (vessel2 != null &&
plugin_.HasVessel(vessel2.id.ToString()) &&
plugin_.IsLoaded(vessel1.id.ToString())) {
plugin_.ReportCollision(part1.flightID, part2.flightID);
}
}
@@ -763,17 +765,19 @@ private System.Collections.IEnumerator
universal_time + " plugin-universal=" +
(plugin_time - universal_time));
time_is_advancing_ = false;
yield break;
return;
} else if (plugin_time == universal_time) {
time_is_advancing_ = false;
yield break;
return;
}
time_is_advancing_ = true;

plugin_.FreeVesselsAndPartsAndCollectPileUps();

foreach (Vessel vessel in FlightGlobals.VesselsLoaded) {
if (vessel.packed || !plugin_.HasVessel(vessel.id.ToString())) {
if (vessel.packed ||
!plugin_.HasVessel(vessel.id.ToString()) ||
!plugin_.IsLoaded(vessel.id.ToString())) {
continue;
}
foreach (Part part in vessel.parts) {
@@ -801,7 +805,9 @@ private System.Collections.IEnumerator
// TODO(egg): if I understand anything, there should probably be a
// special treatment for loaded packed vessels. I don't understand
// anything though.
if (vessel.packed || !plugin_.HasVessel(vessel.id.ToString())) {
if (vessel.packed ||
!plugin_.HasVessel(vessel.id.ToString()) ||
!plugin_.IsLoaded(vessel.id.ToString())) {
continue;
}
foreach (Part part in vessel.parts) {
@@ -846,17 +852,28 @@ private void DisableVesselPrecalculate() {
}

private void SetBodyFramesAndPrecalculateVessels() {
AdvanceTimeAndNudgeVesselsAfterPhysicsSimulation();
SetBodyFrames();
// Unfortunately there is no way to get scheduled between Planetarium and
// VesselPrecalculate, so we get scheduled after VesselPrecalculate, set the
// body frames for our weird tilt, and run VesselPrecalculate manually.
// Sob.
foreach (var vessel in FlightGlobals.Vessels) {
// NOTE(egg): we cannot use foreach here, and we must iterate downwards,
// since vessel.precalc.FixedUpdate may remove its vessel.
for (int i = FlightGlobals.Vessels.Count - 1; i >= 0; --i) {
var vessel = FlightGlobals.Vessels[i];
vessel.precalc.enabled = true;
vessel.precalc.FixedUpdate();
}
}

private void UpdateVesselOrbits() {
if (PluginRunning()) {
ApplyToVesselsOnRails(
vessel => UpdateVessel(vessel, Planetarium.GetUniversalTime()));
}
}

private void ReportVesselsAndParts() {
// We fetch the forces from the census of nonconservatives here;
// part.forces, part.force, and part.torque are cleared by the/
@@ -915,6 +932,15 @@ private void ReportVesselsAndParts() {

private void SetBodyFrames() {
if (PluginRunning()) {
if (FlightGlobals.currentMainBody != null) {
FlightGlobals.currentMainBody.rotationPeriod =
plugin_.CelestialRotationPeriod(
FlightGlobals.currentMainBody.flightGlobalsIndex);
FlightGlobals.currentMainBody.initialRotation =
plugin_.CelestialInitialRotationInDegrees(
FlightGlobals.currentMainBody.flightGlobalsIndex);
}
ApplyToBodyTree(body => UpdateBody(body, Planetarium.GetUniversalTime()));
foreach (var body in FlightGlobals.Bodies) {
// TODO(egg): I have no idea why this |swizzle| thing makes things work.
// This probably really means something in terms of frames that should
@@ -1144,26 +1170,9 @@ private void DrawMainWindow(int window_id) {
plugin_state = "running";
}
UnityEngine.GUILayout.TextArea(text : "Plugin is " + plugin_state);
// TODO(egg): remove this diagnosis when we have proper collision handling.
if (FlightGlobals.ActiveVessel != null) {
int collisions = 0;
int part_collisions = 0;
foreach (var part in FlightGlobals.ActiveVessel.parts) {
collisions += part.currentCollisions.Count;
foreach (var collider in part.currentCollisions) {
var collidee = collider.gameObject.GetComponentUpwards<Part>();
if (collidee != null &&
collidee.vessel != FlightGlobals.ActiveVessel) {
++part_collisions;
}
}
}
UnityEngine.GUILayout.TextArea(
text
: "Active vessel is involved in " + collisions +
" collision(s) including " + part_collisions +
" with another vessel.");
}
UnityEngine.GUILayout.TextArea(
"Time runs slowed by " +
slowdown_);
if (FlightGlobals.ActiveVessel != null) {
UnityEngine.GUILayout.TextArea(FlightGlobals.ActiveVessel.geeForce +
" g0");
18 changes: 17 additions & 1 deletion serialization/journal.proto
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ message WXYZ {
}

message Method {
extensions 5000 to 5999; // Last used: 5118.
extensions 5000 to 5999; // Last used: 5119.
}

message AdvanceTime {
@@ -816,6 +816,22 @@ message IsKspStockSystem {
optional Return return = 3;
}

message IsLoaded {
extend Method {
optional IsLoaded extension = 5119;
}
message In {
required fixed64 plugin = 1 [(pointer_to) = "Plugin const",
(is_subject) = true];
required string vessel_guid = 2;
}
message Return {
required bool result = 1;
}
optional In in = 1;
optional Return return = 3;
}

message IteratorAtEnd {
extend Method {
optional IteratorAtEnd extension = 5083;