Skip to content

Commit

Permalink
CustomSystem: clean round-trip to Json for bodies
Browse files Browse the repository at this point in the history
- Ensure bodies are loaded with exactly the same values as they were serialized with
- Remove latitude/longitude fields from CustomSystemBody
  • Loading branch information
sturnclaw committed Sep 6, 2023
1 parent 6754490 commit 4c0ba5b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/galaxy/CustomSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ CSB_FIELD_SETTER_FIXED(mass, mass)
CSB_FIELD_SETTER_INT(temp, averageTemp)
CSB_FIELD_SETTER_FIXED(semi_major_axis, semiMajorAxis)
CSB_FIELD_SETTER_FIXED(eccentricity, eccentricity)
CSB_FIELD_SETTER_REAL(latitude, latitude)
CSB_FIELD_SETTER_REAL(longitude, longitude)
CSB_FIELD_SETTER_FIXED(inclination, inclination)
CSB_FIELD_SETTER_FIXED(rotation_period, rotationPeriod)
CSB_FIELD_SETTER_FIXED(axial_tilt, axialTilt)
CSB_FIELD_SETTER_FIXED(metallicity, metallicity)
Expand Down Expand Up @@ -298,10 +297,10 @@ static luaL_Reg LuaCustomSystemBody_meta[] = {
{ "eccentricity", &l_csb_eccentricity },
{ "orbital_offset", &l_csb_orbital_offset },
{ "orbital_phase_at_start", &l_csb_orbital_phase_at_start },
{ "latitude", &l_csb_latitude },
{ "latitude", &l_csb_inclination },
// latitude is for surface bodies, inclination is for orbiting bodies (but they're the same field)
{ "inclination", &l_csb_latitude },
{ "longitude", &l_csb_longitude },
{ "inclination", &l_csb_inclination },
{ "longitude", &l_csb_orbital_offset },
{ "rotation_period", &l_csb_rotation_period },
{ "rotational_phase_at_start", &l_csb_rotational_phase_at_start }, // 0 to 2 pi
{ "axial_tilt", &l_csb_axial_tilt },
Expand Down Expand Up @@ -332,13 +331,14 @@ void CustomSystemBody::LoadFromJson(const Json &obj)
aspectRatio = obj.value<fixed>("aspectRatio", 0);
mass = obj.value<fixed>("mass", 0);
rotationPeriod = obj.value<fixed>("rotationPeriod", 0);
rotationalPhaseAtStart = obj.value<fixed>("rotationPhase", 0);
// humanActivity = obj.value<fixed>("humanActivity", 0);
semiMajorAxis = obj.value<fixed>("semiMajorAxis", 0);
eccentricity = obj.value<fixed>("eccentricity", 0);
orbitalOffset = obj.value<fixed>("orbitalOffset", 0);
orbitalPhaseAtStart = obj.value<fixed>("orbitalPhase", 0);
axialTilt = obj.value<fixed>("axialTilt", 0);
latitude = obj.value<fixed>("inclination", 0).ToDouble();
inclination = obj.value<fixed>("inclination", 0);
argOfPeriapsis = obj.value<fixed>("argOfPeriapsis", 0);
averageTemp = obj.value<uint32_t>("averageTemp", 0);
// isCustomBody = obj.value<bool>("isCustom", false);
Expand Down Expand Up @@ -1013,7 +1013,7 @@ CustomSystemBody::CustomSystemBody() :
want_rand_offset(true),
want_rand_arg_periapsis(true),
want_rand_phase(true),
latitude(0.0),
inclination(0.0),
longitude(0.0),
volatileGas(0),
ringStatus(WANT_RANDOM_RINGS),
Expand Down
3 changes: 2 additions & 1 deletion src/galaxy/CustomSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class CustomSystemBody {
bool want_rand_phase;
bool want_rand_arg_periapsis;
// for orbiting things, latitude = inclination
float latitude, longitude; // radians
fixed inclination; // radians
fixed longitude; // radians
fixed rotationPeriod; // in days
fixed rotationalPhaseAtStart; // 0 to 2 pi
fixed axialTilt; // in radians
Expand Down
4 changes: 1 addition & 3 deletions src/galaxy/StarSystemGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,7 @@ void StarSystemCustomGenerator::CustomGetKidsOf(RefCountedPtr<StarSystem::Genera
kid->m_orbitalPhaseAtStart = csbody->orbitalPhaseAtStart;
kid->m_argOfPeriapsis = csbody->argOfPeriapsis;
kid->m_axialTilt = csbody->axialTilt;
kid->m_inclination = fixed(csbody->latitude * 10000, 10000);
if (kid->GetType() == SystemBody::TYPE_STARPORT_SURFACE)
kid->m_orbitalOffset = fixed(csbody->longitude * 10000, 10000);
kid->m_inclination = csbody->inclination;
kid->m_semiMajorAxis = csbody->semiMajorAxis;

if (csbody->heightMapFilename.length() > 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/galaxy/SystemBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void SystemBody::SaveToJson(Json &out)
out["spaceStationType"] = m_space_station_type;

if (!m_heightMapFilename.empty()) {
out["heightMapFileName"] = m_heightMapFilename;
out["heightMapFilename"] = m_heightMapFilename;
out["heightMapFractal"] = m_heightMapFractal;
}
}
Expand All @@ -96,6 +96,7 @@ void SystemBody::LoadFromJson(const Json &obj)
m_aspectRatio = obj.value<fixed>("aspectRatio", 0);
m_mass = obj.value<fixed>("mass", 0);
m_rotationPeriod = obj.value<fixed>("rotationPeriod", 0);
m_rotationalPhaseAtStart = obj.value<fixed>("rotationPhase", 0);
m_humanActivity = obj.value<fixed>("humanActivity", 0);
m_semiMajorAxis = obj.value<fixed>("semiMajorAxis", 0);
m_eccentricity = obj.value<fixed>("eccentricity", 0);
Expand Down

0 comments on commit 4c0ba5b

Please sign in to comment.