Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

IMyRemoteControl.GetDistanceToSeaLevel #518

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Sandbox.Common/ModAPI/Ingame/IMyRemoteControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ public interface IMyRemoteControl : IMyShipController

// CH: TODO: Uncomment later for drones
Vector3D GetFreeDestination(Vector3D originalDestination, float checkRadius, float shipRadius);

float GetDistanceToSeaLevel();
Vector3D GetNearestCenter();
bool HasPlanetNearby();
}
}
51 changes: 51 additions & 0 deletions Sources/Sandbox.Game/Game/Entities/Blocks/MyRemoteControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,57 @@ Vector3D IMyRemoteControl.GetFreeDestination(Vector3D originalDestination, float

return retval;
}

float IMyRemoteControl.GetDistanceToSeaLevel() {

var controlledEntity = MySession.Static.ControlledEntity as MyCubeBlock;
var controlledEntityPosition = controlledEntity.CubeGrid.Physics.CenterOfMassWorld;

MyPlanet nearestPlanet;
FindDistanceToNearestPlanetSeaLevel(controlledEntityPosition, out nearestPlanet);
if (nearestPlanet == null)
return -1;

Vector3D closestPoint = nearestPlanet.GetClosestSurfacePointGlobal(ref controlledEntityPosition);
float distanceToSeaLevel = (float)Vector3D.Distance(closestPoint, controlledEntityPosition);
return distanceToSeaLevel;
}

Vector3D IMyRemoteControl.GetNearestCenter() {
var controlledEntity = MySession.Static.ControlledEntity as MyCubeBlock;
var controlledEntityPosition = controlledEntity.CubeGrid.Physics.CenterOfMassWorld;

MyPlanet nearestPlanet = MyGravityProviderSystem.GetNearestPlanet(controlledEntityPosition);
if (nearestPlanet == null)
return Vector3D.Zero;
else
return nearestPlanet.PositionComp.GetPosition();
}

bool IMyRemoteControl.HasPlanetNearby() {

var controlledEntity = MySession.Static.ControlledEntity as MyCubeBlock;
var controlledEntityPosition = controlledEntity.CubeGrid.Physics.CenterOfMassWorld;

MyPlanet nearestPlanet = MyGravityProviderSystem.GetNearestPlanet(controlledEntityPosition);
if (nearestPlanet == null)
return false;
else
return true;
}

private float FindDistanceToNearestPlanetSeaLevel(Vector3D worldPoint, out MyPlanet closestPlanet) {
ProfilerShort.Begin("FindNearestPointOnPlanet");
closestPlanet = MyGravityProviderSystem.GetNearestPlanet(worldPoint);
double closestDistance = double.MaxValue;
if (closestPlanet != null) {
closestDistance = ((worldPoint - closestPlanet.PositionComp.GetPosition()).Length() - closestPlanet.AverageRadius);
}
ProfilerShort.End();

return (float)closestDistance;
}

#endregion

private bool TryFindSavedEntity()
Expand Down