Pioneer
Propulsion.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef PROPULSION_H
5 #define PROPULSION_H
6 
7 #include "DynamicBody.h"
8 #include "JsonUtils.h"
9 #include "scenegraph/Model.h"
10 #include "vector3.h"
11 
12 class Camera;
13 class Space;
14 
15 enum Thruster { // <enum scope='Thruster' name=ShipTypeThruster prefix=THRUSTER_ public>
22  THRUSTER_MAX // <enum skip>
23 };
24 
25 class Propulsion : public RefCounted {
26 public:
27  // Inits:
28  Propulsion();
29  virtual ~Propulsion(){};
30  // Acceleration cap is infinite
31  void Init(DynamicBody *b, SceneGraph::Model *m, const int tank_mass, const double effExVel, const float lin_Thrust[], const float ang_Thrust);
32  void Init(DynamicBody *b, SceneGraph::Model *m, const int tank_mass, const double effExVel, const float lin_Thrust[], const float ang_Thrust, const float lin_AccelerationCap[]);
33 
34  virtual void SaveToJson(Json &jsonObj, Space *space);
35  virtual void LoadFromJson(const Json &jsonObj, Space *space);
36 
37  // Bonus:
38  void SetThrustPowerMult(double p, const float lin_Thrust[], const float ang_Thrust);
39  void SetAccelerationCapMult(double p, const float lin_AccelerationCap[]);
40 
41  // Thrust and thruster functions
42  // Everything's capped unless specified otherwise.
43  double GetThrust(Thruster thruster) const; // Maximum thrust possible within acceleration cap
44  vector3d GetThrust(const vector3d &dir) const;
45  inline double GetThrustFwd() const { return GetThrust(THRUSTER_FORWARD); }
46  inline double GetThrustRev() const { return GetThrust(THRUSTER_REVERSE); }
47  inline double GetThrustUp() const { return GetThrust(THRUSTER_UP); }
48  double GetThrustMin() const;
49 
50  vector3d GetThrustUncapped(const vector3d &dir) const;
51 
52  inline double GetAccel(Thruster thruster) const { return GetThrust(thruster) / m_dBody->GetMass(); }
53  inline double GetAccelFwd() const { return GetAccel(THRUSTER_FORWARD); }
54  inline double GetAccelRev() const { return GetAccel(THRUSTER_REVERSE); }
55  inline double GetAccelUp() const { return GetAccel(THRUSTER_UP); }
56  inline double GetAccelMin() const { return GetThrustMin() / m_dBody->GetMass(); }
57 
58  // Clamp thruster levels and scale them down so that a level of 1
59  // corresponds to the thrust from GetThrust().
60  double ClampLinThrusterState(int axis, double level) const;
61  vector3d ClampLinThrusterState(const vector3d &levels) const;
62 
63  // A level of 1 corresponds to the thrust from GetThrust().
64  void SetLinThrusterState(int axis, double level);
65  void SetLinThrusterState(const vector3d &levels);
66 
67  inline void SetAngThrusterState(int axis, double level) { m_angThrusters[axis] = Clamp(level, -1.0, 1.0); }
68  void SetAngThrusterState(const vector3d &levels);
69 
70  inline vector3d GetLinThrusterState() const { return m_linThrusters; };
71  inline vector3d GetAngThrusterState() const { return m_angThrusters; }
72 
73  inline void ClearLinThrusterState() { m_linThrusters = vector3d(0, 0, 0); }
74  inline void ClearAngThrusterState() { m_angThrusters = vector3d(0, 0, 0); }
75 
76  inline vector3d GetActualLinThrust() const { return m_linThrusters * GetThrustUncapped(m_linThrusters); }
77  inline vector3d GetActualAngThrust() const { return m_angThrusters * m_angThrust; }
78 
79  // Fuel
80  enum FuelState { // <enum scope='Propulsion' name=PropulsionFuelStatus prefix=FUEL_ public>
84  };
85 
86  inline FuelState GetFuelState() const
87  {
88  return (m_thrusterFuel > 0.05f) ?
89  FUEL_OK :
90  (m_thrusterFuel > 0.0f) ?
91  FUEL_WARNING :
92  FUEL_EMPTY;
93  }
94  // fuel left, 0.0-1.0
95  inline double GetFuel() const { return m_thrusterFuel; }
96  inline double GetFuelReserve() const { return m_reserveFuel; }
97  inline void SetFuel(const double f) { m_thrusterFuel = Clamp(f, 0.0, 1.0); }
98  inline void SetFuelReserve(const double f) { m_reserveFuel = Clamp(f, 0.0, 1.0); }
99  float GetFuelUseRate();
100  // available delta-V given the ship's current fuel minus reserve according to the Tsiolkovsky equation
101  double GetSpeedReachedWithFuel() const;
102  /* TODO: These are needed to avoid savegamebumps:
103  * are used to pass things to/from shipStats;
104  * may be better if you not expose these fields
105  */
106  inline float FuelTankMassLeft() { return m_fuelTankMass * m_thrusterFuel; }
107  inline void SetFuelTankMass(int fTank) { m_fuelTankMass = fTank; }
108  void UpdateFuel(const float timeStep);
109  inline bool IsFuelStateChanged() { return m_fuelStateChange; }
110 
111  void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform);
112 
113  // AI on Propulsion
114  void AIModelCoordsMatchSpeedRelTo(const vector3d &v, const DynamicBody *other);
116  bool AIMatchVel(const vector3d &vel, const vector3d &powerLimit = vector3d(1.0));
117  bool AIChangeVelBy(const vector3d &diffvel, const vector3d &powerLimit = vector3d(1.0)); // acts in object space
118  vector3d AIChangeVelDir(const vector3d &diffvel); // object space, maintain direction
119  void AIMatchAngVelObjSpace(const vector3d &angvel, const vector3d &powerLimit = vector3d(1.0), bool ignoreZeroValues = false);
120  double AIFaceUpdir(const vector3d &updir, double av = 0);
121  double AIFaceDirection(const vector3d &dir, double av = 0);
122  vector3d AIGetLeadDir(const Body *target, const vector3d &targaccel, double projspeed);
123 
124 private:
125  // Thrust and thrusters
126  float m_linThrust[THRUSTER_MAX];
127  float m_angThrust;
128  vector3d m_linThrusters; // 0.0-1.0, thruster levels
129  vector3d m_angThrusters; // 0.0-1.0
130  // Used to calculate max linear thrust by limiting the thruster levels
131  float m_linAccelerationCap[THRUSTER_MAX];
132 
133  // Fuel
134  int m_fuelTankMass;
135  double m_thrusterFuel; // 0.0-1.0, remaining fuel
136  double m_reserveFuel; // 0.0-1.0, fuel not to touch for the current AI program
137  double m_effectiveExhaustVelocity;
138  bool m_fuelStateChange;
139 
140  const DynamicBody *m_dBody;
141  SceneGraph::Model *m_smodel;
142 };
143 
144 #endif // PROPULSION_H
nlohmann::json Json
Definition: Json.h:8
Thruster
Definition: Propulsion.h:15
@ THRUSTER_UP
Definition: Propulsion.h:18
@ THRUSTER_REVERSE
Definition: Propulsion.h:16
@ THRUSTER_LEFT
Definition: Propulsion.h:20
@ THRUSTER_RIGHT
Definition: Propulsion.h:21
@ THRUSTER_FORWARD
Definition: Propulsion.h:17
@ THRUSTER_MAX
Definition: Propulsion.h:22
@ THRUSTER_DOWN
Definition: Propulsion.h:19
Definition: Body.h:57
Definition: Camera.h:80
Definition: DynamicBody.h:15
virtual double GetMass() const override
Definition: DynamicBody.h:37
Definition: Renderer.h:44
Definition: Propulsion.h:25
double GetAccelRev() const
Definition: Propulsion.h:54
virtual ~Propulsion()
Definition: Propulsion.h:29
vector3d AIGetLeadDir(const Body *target, const vector3d &targaccel, double projspeed)
Definition: Propulsion.cpp:456
bool IsFuelStateChanged()
Definition: Propulsion.h:109
double GetThrustMin() const
Definition: Propulsion.cpp:185
void ClearAngThrusterState()
Definition: Propulsion.h:74
void SetFuelReserve(const double f)
Definition: Propulsion.h:98
vector3d GetActualLinThrust() const
Definition: Propulsion.h:76
double ClampLinThrusterState(int axis, double level) const
Definition: Propulsion.cpp:113
double GetAccelUp() const
Definition: Propulsion.h:55
void SetFuel(const double f)
Definition: Propulsion.h:97
void SetAccelerationCapMult(double p, const float lin_AccelerationCap[])
Definition: Propulsion.cpp:96
double GetFuelReserve() const
Definition: Propulsion.h:96
void ClearLinThrusterState()
Definition: Propulsion.h:73
void SetAngThrusterState(int axis, double level)
Definition: Propulsion.h:67
double GetThrustUp() const
Definition: Propulsion.h:47
double GetFuel() const
Definition: Propulsion.h:95
FuelState
Definition: Propulsion.h:80
@ FUEL_OK
Definition: Propulsion.h:81
@ FUEL_EMPTY
Definition: Propulsion.h:83
@ FUEL_WARNING
Definition: Propulsion.h:82
double AIFaceUpdir(const vector3d &updir, double av=0)
Definition: Propulsion.cpp:388
void SetFuelTankMass(int fTank)
Definition: Propulsion.h:107
double GetAccelMin() const
Definition: Propulsion.h:56
vector3d AIChangeVelDir(const vector3d &diffvel)
Definition: Propulsion.cpp:345
double GetThrust(Thruster thruster) const
Definition: Propulsion.cpp:164
vector3d GetThrustUncapped(const vector3d &dir) const
Definition: Propulsion.cpp:194
void AIMatchAngVelObjSpace(const vector3d &angvel, const vector3d &powerLimit=vector3d(1.0), bool ignoreZeroValues=false)
Definition: Propulsion.cpp:366
void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)
Definition: Propulsion.cpp:236
void Init(DynamicBody *b, SceneGraph::Model *m, const int tank_mass, const double effExVel, const float lin_Thrust[], const float ang_Thrust)
Definition: Propulsion.cpp:68
void SetLinThrusterState(int axis, double level)
Definition: Propulsion.cpp:149
virtual void SaveToJson(Json &jsonObj, Space *space)
Definition: Propulsion.cpp:22
double GetThrustFwd() const
Definition: Propulsion.h:45
double GetSpeedReachedWithFuel() const
Definition: Propulsion.cpp:226
vector3d GetActualAngThrust() const
Definition: Propulsion.h:77
bool AIMatchVel(const vector3d &vel, const vector3d &powerLimit=vector3d(1.0))
Definition: Propulsion.cpp:319
double GetAccelFwd() const
Definition: Propulsion.h:53
double AIFaceDirection(const vector3d &dir, double av=0)
Definition: Propulsion.cpp:417
void UpdateFuel(const float timeStep)
Definition: Propulsion.cpp:211
void AIModelCoordsMatchSpeedRelTo(const vector3d &v, const DynamicBody *other)
Definition: Propulsion.cpp:246
void SetThrustPowerMult(double p, const float lin_Thrust[], const float ang_Thrust)
Definition: Propulsion.cpp:88
float GetFuelUseRate()
Definition: Propulsion.cpp:205
float FuelTankMassLeft()
Definition: Propulsion.h:106
double GetAccel(Thruster thruster) const
Definition: Propulsion.h:52
void AIAccelToModelRelativeVelocity(const vector3d &v)
Definition: Propulsion.cpp:255
Propulsion()
Definition: Propulsion.cpp:50
virtual void LoadFromJson(const Json &jsonObj, Space *space)
Definition: Propulsion.cpp:34
vector3d GetLinThrusterState() const
Definition: Propulsion.h:70
bool AIChangeVelBy(const vector3d &diffvel, const vector3d &powerLimit=vector3d(1.0))
Definition: Propulsion.cpp:327
FuelState GetFuelState() const
Definition: Propulsion.h:86
vector3d GetAngThrusterState() const
Definition: Propulsion.h:71
double GetThrustRev() const
Definition: Propulsion.h:46
Definition: RefCounted.h:11
Definition: Model.h:88
Definition: Space.h:19
const T & Clamp(const T &x, const T &min, const T &max)
Definition: libs.h:69
vector3< double > vector3d
Definition: vector3.h:290