Skip to content

Commit 069171f

Browse files
committed
Add function to calculate surface gravity using fixed point
1 parent 73646c2 commit 069171f

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/galaxy/StarSystemGenerator.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ static fixed CalculateHabitability(SystemBody *sbody)
14811481
// Surface gravity - mean at 7.5 with standard deviation of 7.5, so slightly less than Earth gravity is a
14821482
// pleasure to work in, zero gravity is still habitable, but has a penalty, while heavier than Earth gravity
14831483
// gets progressively worse. Multiply the result a small amount so that Earth itself is back up to 1.0
1484-
const fixed gravity = fixed::FromDouble(sbody->CalcSurfaceGravity());
1484+
const fixed gravity = sbody->CalcSurfaceGravityAsFixed();
14851485
habitability *= FalloffFunction(gravity, fixed(15, 2), fixed(15, 2)) * fixed(106, 100);
14861486

14871487
// If the entire planet is covered in ice (ice coverage = 1) then that's hugely inconvenient for industry and
@@ -1502,20 +1502,21 @@ static fixed CalculateHabitability(SystemBody *sbody)
15021502
// too heavily. Fundamentally, if there is very low air pressure, then the temperature and composition don't matter.
15031503
// So first check air pressure, and if it's below a threshold then skip over the other two.
15041504

1505-
// Atmospheric pressure. 1.0 is Earth atmosphere density, so that's our baseline, with medium fall-off.
1506-
const fixed pressure = fixed::FromDouble(sbody->GetAtmSurfacePressure());
1507-
habitability *= FalloffFunction(pressure, fixed(1, 1), fixed(9, 10));
1505+
// For atmospheric pressure, we'll use the gas density, since it's already in fixed point and is good enough for this
1506+
// rough habitability calculation. 1.225 is Earth atmosphere density, so that's our baseline, with medium fall-off.
1507+
const fixed density = sbody->GetVolatileGasAsFixed();
1508+
habitability *= FalloffFunction(density, fixed(1225, 1000), fixed(11, 10));
15081509

1509-
if (pressure > 0.5) {
1510+
if (density > fixed(6, 10)) {
15101511
// Temperature is in Kelvin. Average temperature on Earth's service is 15 Celsius, so 273+15 is our mean.
15111512
// Humans live at extremes on Earth, plus we're dealing with the future, so 50 for a standard deviation seems
15121513
// reasonable, though narrow in astrophysical terms
1513-
const fixed temperature = fixed::FromDouble(sbody->GetAverageTemp());
1514+
const fixed temperature = fixed(sbody->GetAverageTemp(), 1);
15141515
habitability *= FalloffFunction(temperature, fixed(288, 1), fixed(50, 1));
15151516

15161517
// Breathable air is super convenient, while air that actively kills you is even worse than a vacuum. So let's heavily
15171518
// favour oxidising atmospheres over reducing onces.
1518-
const fixed air_type = fixed::FromDouble(sbody->GetAtmosOxidizing());
1519+
const fixed air_type = sbody->GetAtmosOxidizingAsFixed();
15191520
habitability *= FalloffFunction(air_type, fixed(1, 1), fixed(3, 10));
15201521
}
15211522

src/galaxy/SystemBody.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,17 @@ double SystemBody::CalcSurfaceGravity() const
969969
}
970970
}
971971

972+
fixed SystemBody::CalcSurfaceGravityAsFixed() const
973+
{
974+
if (m_radius <= fixed(0, 1))
975+
return fixed(0, 1);
976+
// m_radius and m_mass are already in native body units
977+
// (Earth radius/mass for planets, Sol radius/mass for stars)
978+
// so we don't need big G here - just multiply by the relevant value of g.
979+
const fixed unitG = (GetSuperType() == SUPERTYPE_STAR) ? G_SOL : G_EARTH;
980+
return unitG * ((m_mass / m_radius) / m_radius);
981+
}
982+
972983
double SystemBody::CalcEscapeVelocity() const
973984
{
974985
double r = GetRadius();

src/galaxy/SystemBody.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include "galaxy/SystemPath.h"
1515
#include "gameconsts.h"
1616

17+
static const fixed G_EARTH = fixed(980665, 100000); // 9.80665 m/s^2
18+
static const fixed G_SOL = fixed(2742, 10); // 274.2 m/s^2
19+
1720
class StarSystem;
1821

1922
struct AtmosphereParameters;
@@ -261,6 +264,7 @@ class SystemBody : public RefCounted, public SystemBodyType, protected SystemBod
261264
double GetVolatileIces() const { return m_volatileIces.ToDouble(); }
262265
fixed GetVolcanicityAsFixed() const { return m_volcanicity; }
263266
double GetVolcanicity() const { return m_volcanicity.ToDouble(); }
267+
fixed GetAtmosOxidizingAsFixed() const { return m_atmosOxidizing; }
264268
double GetAtmosOxidizing() const { return m_atmosOxidizing.ToDouble(); }
265269
fixed GetLifeAsFixed() const { return m_life; }
266270
double GetLife() const { return m_life.ToDouble(); }
@@ -270,6 +274,7 @@ class SystemBody : public RefCounted, public SystemBodyType, protected SystemBod
270274
fixed GetPopulationAsFixed() const { return m_population; }
271275
double GetPopulation() const { return m_population.ToDouble(); }
272276

277+
fixed CalcSurfaceGravityAsFixed() const;
273278
double CalcSurfaceGravity() const;
274279
double CalcEscapeVelocity() const;
275280
double CalcMeanDensity() const;

0 commit comments

Comments
 (0)