naev 0.10.4
pilot_heat.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
4
5
14#include <math.h>
15
16#include "naev.h"
19#include "pilot_heat.h"
20
21#include "array.h"
22#include "log.h"
23
24
25/*
26 * Prototypes.
27 */
28static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o );
29
30
39{
40 double mass_kg;
41 mass_kg = 1000. * p->base_mass;
42 p->heat_emis = 0.8;
43 p->heat_cond = STEEL_HEAT_CONDUCTIVITY;
44 p->heat_C = STEEL_HEAT_CAPACITY * mass_kg;
45
46 /* We'll approximate area for a sphere.
47 *
48 * Sphere:
49 * V = 4/3*pi*r^3
50 * A = 4*pi*r^2
51 *
52 * Ship:
53 * V = mass/density
54 *
55 * We then equal the ship V and sphere V to obtain r:
56 * r = (3*mass)/(4*pi*density))^(1/3)
57 *
58 * Substituting r in A we get:
59 * A = 4*pi*((3*mass)/(4*pi*density))^(2/3)
60 * */
61 p->heat_area = 4.*M_PI*pow( 3./4.*mass_kg/STEEL_DENSITY/M_PI, 2./3. ) * p->stats.heat_dissipation;
62}
63
64
68double pilot_heatCalcOutfitC( const Outfit *o )
69{
70 /* Simple thermal mass. Put a floor on it so we get zero heat flux in case of NaN for massless outfits. */
71 return STEEL_HEAT_CAPACITY * MAX( 1000. * o->mass, 1. );
72}
73
74
81{
82 double mass_kg = MAX( 1000. * o->mass, 1. ); /* Avoid it being 0. */
83 /* We consider the effective area of outfits to be half of a sphere. */
84 return 2.*M_PI*pow( 3./4.*mass_kg/STEEL_DENSITY/M_PI, 2./3. );
85}
86
87
92{
93 o->heat_T = CONST_SPACE_STAR_TEMP; /* Reset temperature. */
94 o->heat_start = CONST_SPACE_STAR_TEMP; /* For cooldown purposes. */
95 if (o->outfit == NULL) {
96 o->heat_C = 1.;
97 o->heat_area = 0.;
98 return;
99 }
102}
103
104
111{
112 p->heat_T = CONST_SPACE_STAR_TEMP;
113 for (int i=0; i<array_size(p->outfits); i++)
114 p->outfits[i]->heat_T = CONST_SPACE_STAR_TEMP;
115}
116
117
121static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o )
122{
123 switch (o->type) {
124 case OUTFIT_TYPE_BOLT:
125 case OUTFIT_TYPE_BEAM:
126 return p->stats.fwd_heat;
127
128 case OUTFIT_TYPE_TURRET_BOLT:
129 case OUTFIT_TYPE_TURRET_BEAM:
130 return p->stats.tur_heat;
131
132 default:
133 return 1;
134 }
135}
136
137
145{
146 /* We consider that only 1% of the energy is lost in the form of heat,
147 * this keeps numbers safe. */
148 double hmod = pilot_heatOutfitMod( p, o->outfit );
149
150 o->heat_T += hmod * outfit_heat(o->outfit) / o->heat_C;
151
152 /* Enforce a minimum value as a safety measure. */
153 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
154}
155
156
164void pilot_heatAddSlotTime( const Pilot *p, PilotOutfitSlot *o, double dt )
165{
166 double hmod = pilot_heatOutfitMod( p, o->outfit );
167
168 o->heat_T += (hmod * outfit_heat(o->outfit) / o->heat_C) * dt;
169
170 /* Enforce a minimum value as a safety measure. */
171 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
172}
173
174
193double pilot_heatUpdateSlot( const Pilot *p, PilotOutfitSlot *o, double dt )
194{
195 double Q;
196
197 /* Calculate energy leaving/entering ship chassis. */
198 Q = -p->heat_cond * (o->heat_T - p->heat_T) * o->heat_area * dt;
199
200 /* Update current temperature. */
201 o->heat_T += Q / o->heat_C;
202
203 /* Return energy moved. */
204 return Q;
205}
206
207
229void pilot_heatUpdateShip( Pilot *p, double Q_cond, double dt )
230{
231 double Q, Q_rad;
232
233 /* Calculate radiation. */
234 Q_rad = CONST_STEFAN_BOLTZMANN * p->heat_area * p->heat_emis *
235 (CONST_SPACE_STAR_TEMP_4 - pow(p->heat_T,4.)) * dt;
236
237 /* Total heat movement. */
238 Q = Q_rad - Q_cond;
239
240 /* Update ship temperature. */
241 p->heat_T += Q / p->heat_C;
242}
243
244
252double pilot_heatEfficiencyMod( double T, double Tb, double Tc )
253{
254 return CLAMP( 0., 1., 1 - (T - Tb) / Tc );
255}
256
263{
264 double t;
265
266 t = pow2( 1. - p->ctimer / p->cdelay );
267 p->heat_T = p->heat_start - CONST_SPACE_STAR_TEMP - (p->heat_start -
268 CONST_SPACE_STAR_TEMP) * t + CONST_SPACE_STAR_TEMP;
269
270 for (int i=0; i<array_size(p->outfits); i++) {
271 int ammo_threshold;
272 PilotOutfitSlot *o = p->outfits[i];
273 o->heat_T = o->heat_start - CONST_SPACE_STAR_TEMP - (o->heat_start -
274 CONST_SPACE_STAR_TEMP) * t + CONST_SPACE_STAR_TEMP;
275
276 /* Refill ammo too (also part of Active Cooldown) */
277 /* Must be valid outfit. */
278 if (o->outfit == NULL)
279 continue;
280
281 /* Initial (raw) ammo threshold */
282 ammo_threshold = round(t * pilot_maxAmmoO(p,o->outfit));
283
284 /* Adjust for deployed fighters if needed */
285 if ( outfit_isFighterBay( o->outfit ) )
286 ammo_threshold -= o->u.ammo.deployed;
287
288 if ( o->u.ammo.quantity < ammo_threshold )
289 pilot_addAmmo( p, p->outfits[i], ammo_threshold - o->u.ammo.quantity );
290 }
291}
292
296double pilot_heatAccuracyMod( double T )
297{
298 return CLAMP( 0., 1., (T-500.)/600. );
299}
300
301
305double pilot_heatFireRateMod( double T )
306{
307 return CLAMP( 0., 1., (1100.-T)/300. );
308}
309
310
314double pilot_heatFirePercent( double T )
315{
316 return 2.*pilot_heatAccuracyMod(T);
317}
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition: naev.h:41
#define pow2(x)
Definition: naev.h:46
#define MAX(x, y)
Definition: naev.h:39
int outfit_isFighterBay(const Outfit *o)
Checks if outfit is a fighter bay.
Definition: outfit.c:550
double outfit_heat(const Outfit *o)
Gets the outfit's heat generation.
Definition: outfit.c:692
void pilot_heatAddSlot(const Pilot *p, PilotOutfitSlot *o)
Adds heat to an outfit slot.
Definition: pilot_heat.c:144
double pilot_heatEfficiencyMod(double T, double Tb, double Tc)
Returns a 0:1 modifier representing efficiency (1. being normal).
Definition: pilot_heat.c:252
void pilot_heatUpdateCooldown(Pilot *p)
Overrides the usual heat model during active cooldown.
Definition: pilot_heat.c:262
double pilot_heatFireRateMod(double T)
Returns a 0:1 modifier representing fire rate (1. being normal).
Definition: pilot_heat.c:305
void pilot_heatCalcSlot(PilotOutfitSlot *o)
Calculates the heat parameters for a pilot's slot.
Definition: pilot_heat.c:91
static double pilot_heatOutfitMod(const Pilot *p, const Outfit *o)
Gets the heat mod for an outfit.
Definition: pilot_heat.c:121
void pilot_heatReset(Pilot *p)
Resets a pilot's heat.
Definition: pilot_heat.c:110
double pilot_heatAccuracyMod(double T)
Returns a 0:1 modifier representing accuracy (0. being normal).
Definition: pilot_heat.c:296
void pilot_heatUpdateShip(Pilot *p, double Q_cond, double dt)
Heats the pilot's ship.
Definition: pilot_heat.c:229
double pilot_heatCalcOutfitC(const Outfit *o)
Calculates the thermal mass of an outfit.
Definition: pilot_heat.c:68
void pilot_heatCalc(Pilot *p)
Calculates the heat parameters for a pilot.
Definition: pilot_heat.c:38
double pilot_heatCalcOutfitArea(const Outfit *o)
Calculates the effective transfer area of an outfit.
Definition: pilot_heat.c:80
double pilot_heatUpdateSlot(const Pilot *p, PilotOutfitSlot *o, double dt)
Heats the pilot's slot.
Definition: pilot_heat.c:193
double pilot_heatFirePercent(double T)
Returns a 0:2 level of fire, 0:1 is the accuracy point, 1:2 is fire rate point.
Definition: pilot_heat.c:314
void pilot_heatAddSlotTime(const Pilot *p, PilotOutfitSlot *o, double dt)
Adds heat to an outfit slot over a period of time.
Definition: pilot_heat.c:164
int pilot_maxAmmoO(const Pilot *p, const Outfit *o)
Gets the maximum available ammo for a pilot for a specific outfit.
Definition: pilot_outfit.c:767
int pilot_addAmmo(Pilot *pilot, PilotOutfitSlot *s, int quantity)
Adds some ammo to the pilot stock.
Definition: pilot_outfit.c:661
A ship outfit, depends radically on the type.
Definition: outfit.h:304
OutfitType type
Definition: outfit.h:369
double mass
Definition: outfit.h:315
int deployed
Definition: pilot.h:100
int quantity
Definition: pilot.h:99
Stores an outfit the pilot has.
Definition: pilot.h:108
PilotOutfitAmmo ammo
Definition: pilot.h:135
double heat_C
Definition: pilot.h:118
double heat_start
Definition: pilot.h:120
double heat_T
Definition: pilot.h:117
double heat_area
Definition: pilot.h:119
const Outfit * outfit
Definition: pilot.h:112
The representation of an in-game pilot.
Definition: pilot.h:210