Pioneer
Propulsion.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 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); } //GetThrustFwd() / m_dBody->GetMass(); }
54  inline double GetAccelRev() const { return GetAccel(THRUSTER_REVERSE); } //GetThrustRev() / m_dBody->GetMass(); }
55  inline double GetAccelUp() const { return GetAccel(THRUSTER_UP); } //GetThrustUp() / m_dBody->GetMass(); }
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 { return (m_thrusterFuel > 0.05f) ? FUEL_OK : (m_thrusterFuel > 0.0f) ? FUEL_WARNING : FUEL_EMPTY; }
87  // fuel left, 0.0-1.0
88  inline double GetFuel() const { return m_thrusterFuel; }
89  inline double GetFuelReserve() const { return m_reserveFuel; }
90  inline void SetFuel(const double f) { m_thrusterFuel = Clamp(f, 0.0, 1.0); }
91  inline void SetFuelReserve(const double f) { m_reserveFuel = Clamp(f, 0.0, 1.0); }
92  float GetFuelUseRate();
93  // available delta-V given the ship's current fuel minus reserve according to the Tsiolkovsky equation
94  double GetSpeedReachedWithFuel() const;
95  /* TODO: These are needed to avoid savegamebumps:
96  * are used to pass things to/from shipStats;
97  * may be better if you not expose these fields
98  */
99  inline float FuelTankMassLeft() { return m_fuelTankMass * m_thrusterFuel; }
100  inline void SetFuelTankMass(int fTank) { m_fuelTankMass = fTank; }
101  void UpdateFuel(const float timeStep);
102  inline bool IsFuelStateChanged() { return m_fuelStateChange; }
103 
104  void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform);
105 
106  // AI on Propulsion
107  void AIModelCoordsMatchAngVel(const vector3d &desiredAngVel, double softness);
108  void AIModelCoordsMatchSpeedRelTo(const vector3d &v, const DynamicBody *other);
110 
111  bool AIMatchVel(const vector3d &vel);
112  bool AIChangeVelBy(const vector3d &diffvel); // acts in object space
113  vector3d AIChangeVelDir(const vector3d &diffvel); // object space, maintain direction
114  void AIMatchAngVelObjSpace(const vector3d &angvel);
115  double AIFaceUpdir(const vector3d &updir, double av = 0);
116  double AIFaceDirection(const vector3d &dir, double av = 0);
117  vector3d AIGetLeadDir(const Body *target, const vector3d &targaccel, double projspeed);
118 
119 private:
120  // Thrust and thrusters
121  float m_linThrust[THRUSTER_MAX];
122  float m_angThrust;
123  vector3d m_linThrusters; // 0.0-1.0, thruster levels
124  vector3d m_angThrusters; // 0.0-1.0
125  // Used to calculate max linear thrust by limiting the thruster levels
126  float m_linAccelerationCap[THRUSTER_MAX];
127 
128  // Fuel
129  int m_fuelTankMass;
130  double m_thrusterFuel; // 0.0-1.0, remaining fuel
131  double m_reserveFuel; // 0.0-1.0, fuel not to touch for the current AI program
132  double m_effectiveExhaustVelocity;
133  bool m_fuelStateChange;
134 
135  const DynamicBody *m_dBody;
136  SceneGraph::Model *m_smodel;
137 };
138 
139 #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:54
Definition: Camera.h:76
Definition: DynamicBody.h:15
virtual double GetMass() const override
Definition: DynamicBody.h:37
Definition: Renderer.h:39
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:450
bool IsFuelStateChanged()
Definition: Propulsion.h:102
double GetThrustMin() const
Definition: Propulsion.cpp:175
void ClearAngThrusterState()
Definition: Propulsion.h:74
void SetFuelReserve(const double f)
Definition: Propulsion.h:91
vector3d GetActualLinThrust() const
Definition: Propulsion.h:76
double ClampLinThrusterState(int axis, double level) const
Definition: Propulsion.cpp:103
double GetAccelUp() const
Definition: Propulsion.h:55
void AIMatchAngVelObjSpace(const vector3d &angvel)
Definition: Propulsion.cpp:372
void SetFuel(const double f)
Definition: Propulsion.h:90
void SetAccelerationCapMult(double p, const float lin_AccelerationCap[])
Definition: Propulsion.cpp:86
void AIModelCoordsMatchAngVel(const vector3d &desiredAngVel, double softness)
Definition: Propulsion.cpp:236
double GetFuelReserve() const
Definition: Propulsion.h:89
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:88
FuelState
Definition: Propulsion.h:80
@ FUEL_OK
Definition: Propulsion.h:81
@ FUEL_EMPTY
Definition: Propulsion.h:83
@ FUEL_WARNING
Definition: Propulsion.h:82
bool AIMatchVel(const vector3d &vel)
Definition: Propulsion.cpp:326
double AIFaceUpdir(const vector3d &updir, double av=0)
Definition: Propulsion.cpp:382
void SetFuelTankMass(int fTank)
Definition: Propulsion.h:100
double GetAccelMin() const
Definition: Propulsion.h:56
vector3d AIChangeVelDir(const vector3d &diffvel)
Definition: Propulsion.cpp:351
double GetThrust(Thruster thruster) const
Definition: Propulsion.cpp:154
vector3d GetThrustUncapped(const vector3d &dir) const
Definition: Propulsion.cpp:184
void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)
Definition: Propulsion.cpp:226
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:58
void SetLinThrusterState(int axis, double level)
Definition: Propulsion.cpp:139
virtual void SaveToJson(Json &jsonObj, Space *space)
Definition: Propulsion.cpp:12
double GetThrustFwd() const
Definition: Propulsion.h:45
double GetSpeedReachedWithFuel() const
Definition: Propulsion.cpp:216
vector3d GetActualAngThrust() const
Definition: Propulsion.h:77
double GetAccelFwd() const
Definition: Propulsion.h:53
double AIFaceDirection(const vector3d &dir, double av=0)
Definition: Propulsion.cpp:411
void UpdateFuel(const float timeStep)
Definition: Propulsion.cpp:201
bool AIChangeVelBy(const vector3d &diffvel)
Definition: Propulsion.cpp:334
void AIModelCoordsMatchSpeedRelTo(const vector3d &v, const DynamicBody *other)
Definition: Propulsion.cpp:253
void SetThrustPowerMult(double p, const float lin_Thrust[], const float ang_Thrust)
Definition: Propulsion.cpp:78
float GetFuelUseRate()
Definition: Propulsion.cpp:195
float FuelTankMassLeft()
Definition: Propulsion.h:99
double GetAccel(Thruster thruster) const
Definition: Propulsion.h:52
void AIAccelToModelRelativeVelocity(const vector3d &v)
Definition: Propulsion.cpp:262
Propulsion()
Definition: Propulsion.cpp:40
virtual void LoadFromJson(const Json &jsonObj, Space *space)
Definition: Propulsion.cpp:24
vector3d GetLinThrusterState() const
Definition: Propulsion.h:70
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:94
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:286