Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in l_body_get_ground_position if there is no body to get po… #5665

Merged
merged 3 commits into from
Nov 20, 2023
Merged
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
1 change: 1 addition & 0 deletions data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ end
-- experimental
--
function Ship:GetGPS()
if not self.frameBody then return end
local lat, lon, altitude = self:GetGroundPosition()
local vspd = self:GetVelocityRelTo(self.frameBody):dot(self:GetPositionRelTo(self.frameBody):normalized())
lat = math.rad2deg(lat)
Expand Down
23 changes: 11 additions & 12 deletions src/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "CargoBody.h"
#include "Frame.h"
#include "GameSaveError.h"
#include "JsonUtils.h"
#include "HyperspaceCloud.h"
#include "JsonUtils.h"
#include "Missile.h"
#include "Planet.h"
#include "Player.h"
Expand Down Expand Up @@ -259,21 +259,21 @@ vector3d Body::GetVelocityRelTo(const Body *relTo) const
return GetVelocityRelTo(relTo->m_frame) - relTo->GetVelocityRelTo(relTo->m_frame);
}

double Body::GetAltitudeRelTo(const Body* relTo, AltitudeType altType)
double Body::GetAltitudeRelTo(const Body *relTo, AltitudeType altType)
{
if (!relTo) {
return 0.0;
}
vector3d pos = GetPositionRelTo(relTo);
double center_dist = pos.Length();
if (relTo && relTo->IsType(ObjectType::TERRAINBODY)) {
const TerrainBody* terrain = static_cast<const TerrainBody*>(relTo);
if (relTo->IsType(ObjectType::TERRAINBODY)) {
const TerrainBody *terrain = static_cast<const TerrainBody *>(relTo);
vector3d surface_pos = pos.Normalized();
double radius;
if (altType != AltitudeType::DEFAULT)
{
if (altType != AltitudeType::DEFAULT) {
radius = altType == AltitudeType::SEA_LEVEL ? terrain->GetSystemBody()->GetRadius() :
terrain->GetTerrainHeight(surface_pos);
}
else
{
terrain->GetTerrainHeight(surface_pos);
} else {
radius = terrain->GetSystemBody()->GetRadius();
if (center_dist <= 3.0 * terrain->GetMaxFeatureRadius()) {
radius = terrain->GetTerrainHeight(surface_pos);
Expand All @@ -283,8 +283,7 @@ double Body::GetAltitudeRelTo(const Body* relTo, AltitudeType altType)
if (altitude < 0)
altitude = 0;
return altitude;
}
else {
} else {
return center_dist;
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/lua/LuaBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ static int l_body_get_altitude_rel_to(lua_State *l)
Body *b = LuaObject<Body>::CheckFromLua(1);
const Body *other = LuaObject<Body>::CheckFromLua(2);
AltitudeType altType = AltitudeType::DEFAULT;
if (!lua_isnoneornil(l, 3))
{
if (!lua_isnoneornil(l, 3)) {
bool terrainRelative = lua_toboolean(l, 3);
altType = terrainRelative ? AltitudeType::ABOVE_TERRAIN : AltitudeType::SEA_LEVEL;
}
Expand Down Expand Up @@ -491,8 +490,7 @@ static int l_body_attr_frame_body(lua_State *l)

Frame *f = Frame::GetFrame(b->GetFrame());

if (!f)
{
if (!f) {
lua_pushnil(l);
return 1;
}
Expand Down Expand Up @@ -526,8 +524,7 @@ static int l_body_attr_frame_rotating(lua_State *l)
}

Frame *f = Frame::GetFrame(b->GetFrame());
if (!f)
{
if (!f) {
lua_pushnil(l);
return 1;
}
Expand Down Expand Up @@ -650,8 +647,7 @@ static int l_body_get_ground_position(lua_State *l)
{
Body *b = LuaObject<Body>::CheckFromLua(1);
AltitudeType altType = AltitudeType::DEFAULT;
if (!lua_isnoneornil(l, 2))
{
if (!lua_isnoneornil(l, 2)) {
bool terrainRelative = lua_toboolean(l, 2);
altType = terrainRelative ? AltitudeType::ABOVE_TERRAIN : AltitudeType::SEA_LEVEL;
}
Expand All @@ -663,17 +659,21 @@ static int l_body_get_ground_position(lua_State *l)

Frame *f = Frame::GetFrame(b->GetFrame());

if (!f)
{
if (!f) {
lua_pushnil(l);
return 1;
}
Body *astro = f->GetBody();
if (!astro) {
lua_pushnil(l);
return 1;
}

vector3d pos = b->GetPosition();
double latitude = atan2(pos.y, sqrt(pos.x * pos.x + pos.z * pos.z));
double longitude = atan2(pos.x, pos.z);
lua_pushnumber(l, latitude);
lua_pushnumber(l, longitude);
Body *astro = f->GetBody();
double altitude = b->GetAltitudeRelTo(astro, altType);
lua_pushnumber(l, altitude);
return 3;
Expand Down