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: a8a2e244feac
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a75e9890aa89
Choose a head ref
  • 8 commits
  • 2 files changed
  • 1 contributor

Commits on Mar 24, 2019

  1. Copy the full SHA
    0ab80e2 View commit details
  2. Copy the full SHA
    9bcad05 View commit details
  3. comment

    eggrobin committed Mar 24, 2019
    Copy the full SHA
    64df1eb View commit details
  4. limit 64

    eggrobin committed Mar 24, 2019
    Copy the full SHA
    ae6d586 View commit details
  5. Copy the full SHA
    e5dc6e7 View commit details
  6. after pleroy's review

    eggrobin committed Mar 24, 2019
    Copy the full SHA
    fa6c1bc View commit details
  7. Copy the full SHA
    78b3da2 View commit details
  8. Merge pull request #2108 from eggrobin/map-node-pool

    Map node pool improvements
    eggrobin authored Mar 24, 2019
    Copy the full SHA
    a75e989 View commit details
Showing with 72 additions and 29 deletions.
  1. +33 −7 ksp_plugin_adapter/ksp_plugin_adapter.cs
  2. +39 −22 ksp_plugin_adapter/map_node_pool.cs
40 changes: 33 additions & 7 deletions ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
@@ -546,6 +546,9 @@ public override void OnAwake() {
// Timing5, 8008.
TimingManager.FixedUpdateAdd(TimingManager.TimingStage.BetterLateThanNever,
BetterLateThanNever);
TimingManager.LateUpdateAdd(
TimingManager.TimingStage.BetterLateThanNever,
BetterLateThanNeverLateUpdate);
}

public override void OnSave(ConfigNode node) {
@@ -966,6 +969,9 @@ private void OnDisable() {
TimingManager.FixedUpdateRemove(
TimingManager.TimingStage.BetterLateThanNever,
BetterLateThanNever);
TimingManager.LateUpdateRemove(
TimingManager.TimingStage.BetterLateThanNever,
BetterLateThanNeverLateUpdate);
}

#endregion
@@ -1475,6 +1481,33 @@ private void BetterLateThanNever() {
}
}

private void BetterLateThanNeverLateUpdate() {
// While we draw the trajectories directly (and thus do so after everything
// else has been rendered), we rely on the game to render its map nodes.
// Since the screen position is determined in |MapNode.NodeUpdate|, it must
// be called before rendering occurs, but after the cameras have moved;
// otherwise, the map nodes will lag behind when the camera is moved.
// The only timing that satisfies these constraints is BetterLateThanNever
// in LateUpdate.
string main_vessel_guid = PredictedVessel()?.id.ToString();
if (MapView.MapIsEnabled && main_vessel_guid != null &&
PluginRunning() && plugin_.HasVessel(main_vessel_guid)) {
XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position;
RenderPredictionMarkers(main_vessel_guid, sun_world_position);
string target_id =
FlightGlobals.fetch.VesselTarget?.GetVessel()?.id.ToString();
if (FlightGlobals.ActiveVessel != null &&
!plotting_frame_selector_.target_override && target_id != null &&
plugin_.HasVessel(target_id)) {
RenderPredictionMarkers(target_id, sun_world_position);
}
if (plugin_.FlightPlanExists(main_vessel_guid)) {
RenderFlightPlanMarkers(main_vessel_guid, sun_world_position);
}
}
map_node_pool_.Update();
}

private void SetBodyFrames() {
if (PluginRunning()) {
if (FlightGlobals.currentMainBody != null) {
@@ -1657,7 +1690,6 @@ private void RenderTrajectories() {
XKCDColors.Lime,
GLLines.Style.FADED);
}
RenderPredictionMarkers(main_vessel_guid, sun_world_position);
using (DisposableIterator rp2_lines_iterator =
planetarium.PlanetariumPlotPrediction(
plugin_,
@@ -1681,7 +1713,6 @@ private void RenderTrajectories() {
XKCDColors.Goldenrod,
GLLines.Style.FADED);
}
RenderPredictionMarkers(target_id, sun_world_position);
using (DisposableIterator rp2_lines_iterator =
planetarium.PlanetariumPlotPrediction(
plugin_,
@@ -1693,8 +1724,6 @@ private void RenderTrajectories() {
}
}
if (plugin_.FlightPlanExists(main_vessel_guid)) {
RenderFlightPlanMarkers(main_vessel_guid, sun_world_position);

int number_of_segments =
plugin_.FlightPlanNumberOfSegments(main_vessel_guid);
for (int i = 0; i < number_of_segments; ++i) {
@@ -1747,9 +1776,6 @@ private void RenderTrajectories() {
}
});
}
map_node_pool_.Update();
} else {
map_node_pool_.Clear();
}
}

61 changes: 39 additions & 22 deletions ksp_plugin_adapter/map_node_pool.cs
Original file line number Diff line number Diff line change
@@ -31,12 +31,13 @@ public void Clear() {

public void Update() {
for (int i = pool_index_; i < nodes_.Count; ++i) {
nodes_[i].Terminate();
properties_.Remove(nodes_[i]);
if (properties_[nodes_[i]].visible) {
properties_[nodes_[i]].visible = false;
nodes_[i].NodeUpdate();
}
}
nodes_.RemoveRange(index : pool_index_, count : nodes_.Count - pool_index_);
foreach (var node in nodes_) {
node.NodeUpdate();
for (int i = 0; i < pool_index_; ++i) {
nodes_[i].NodeUpdate();
}
pool_index_ = 0;
}
@@ -46,27 +47,38 @@ public void RenderMarkers(DisposableIterator apsis_iterator,
NodeSource source,
Vessel vessel,
CelestialBody celestial) {
for (; !apsis_iterator.IteratorAtEnd();
apsis_iterator.IteratorIncrement()) {
// We render at most 64 markers of one type and one provenance (e.g., at
// most 64 perilunes for the prediction of the active vessel). This is
// more than is readable, and keeps the size of the map node pool under
// control.
for (int i = 0; i < 64 && !apsis_iterator.IteratorAtEnd();
++i, apsis_iterator.IteratorIncrement()) {
QP apsis = apsis_iterator.IteratorGetDiscreteTrajectoryQP();
MapNodeProperties node_properties;
node_properties.object_type = type;
node_properties.vessel = vessel;
node_properties.celestial = celestial;
node_properties.world_position = (Vector3d)apsis.q;
node_properties.velocity = (Vector3d)apsis.p;
node_properties.source = source;
node_properties.time = apsis_iterator.IteratorGetDiscreteTrajectoryTime();
MapNodeProperties node_properties = new MapNodeProperties {
visible = true,
object_type = type,
vessel = vessel,
celestial = celestial,
world_position = (Vector3d)apsis.q,
velocity = (Vector3d)apsis.p,
source = source,
time = apsis_iterator.IteratorGetDiscreteTrajectoryTime()
};

if (pool_index_ == nodes_.Count) {
nodes_.Add(MakePoolNode());
} else if (properties_[nodes_[pool_index_]].object_type != type) {
// Do not reuse a node for different types, as this results in
// overlapping labels on KSP 1.3, e.g. a closest approach marker that
// also says "Ap" and "DN".
nodes_[pool_index_].Terminate();
properties_.Remove(nodes_[pool_index_]);
nodes_[pool_index_] = MakePoolNode();
// KSP attaches labels to its map nodes, but never detaches them.
// If the node changes type, we end up with an arbitrary combination of
// labels Ap, Pe, AN, DN.
// Recreating the node entirely takes a long time (approximately 50 ns *
// 𝑁, where 𝑁 is the total number of map nodes in existence), instead
// we manually get rid of the labels.
foreach (var component in
nodes_[pool_index_].transform.GetComponentsInChildren<
TMPro.TextMeshProUGUI>()) {
UnityEngine.Object.Destroy(component.gameObject);
}
}
properties_[nodes_[pool_index_++]] = node_properties;
}
@@ -101,6 +113,10 @@ private KSP.UI.Screens.Mapview.MapNode MakePoolNode() {
new_node.OnUpdateVisible +=
(KSP.UI.Screens.Mapview.MapNode node,
KSP.UI.Screens.Mapview.MapNode.IconData icon) => {
if (!properties_[node].visible) {
icon.visible = false;
return;
}
CelestialBody celestial = properties_[node].celestial;
UnityEngine.Color colour =
celestial.orbit == null
@@ -215,7 +231,8 @@ private KSP.UI.Screens.Mapview.MapNode MakePoolNode() {
return new_node;
}

private struct MapNodeProperties {
private class MapNodeProperties {
public bool visible;
public MapObject.ObjectType object_type;
public Vector3d world_position;
// Velocity in the plotting frame. Note that the handedness is