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