naev 0.12.5
pilot_heat.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
9
11#include <math.h>
12
13#include "naev.h"
15
16#include "pilot_heat.h"
17
18#include "array.h"
19
20/*
21 * Prototypes.
22 */
23static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o );
24
33{
34 double mass_kg;
35 mass_kg = 1000. * p->base_mass;
36 p->heat_emis = 0.8;
37 p->heat_cond = STEEL_HEAT_CONDUCTIVITY;
38 p->heat_C = STEEL_HEAT_CAPACITY * mass_kg;
39
40 /* We'll approximate area for a sphere.
41 *
42 * Sphere:
43 * V = 4/3*pi*r^3
44 * A = 4*pi*r^2
45 *
46 * Ship:
47 * V = mass/density
48 *
49 * We then equal the ship V and sphere V to obtain r:
50 * r = (3*mass)/(4*pi*density))^(1/3)
51 *
52 * Substituting r in A we get:
53 * A = 4*pi*((3*mass)/(4*pi*density))^(2/3)
54 * */
55 p->heat_area = 4. * M_PI *
56 pow( 3. / 4. * mass_kg / STEEL_DENSITY / M_PI, 2. / 3. ) *
57 p->stats.heat_dissipation;
58}
59
63double pilot_heatCalcOutfitC( const Outfit *o )
64{
65 /* Simple thermal mass. Put a floor on it so we get zero heat flux in case of
66 * NaN for massless outfits. */
67 return STEEL_HEAT_CAPACITY * MAX( 1000. * o->mass, 1. );
68}
69
76{
77 double mass_kg = MAX( 1000. * o->mass, 1. ); /* Avoid it being 0. */
78 /* We consider the effective area of outfits to be half of a sphere. */
79 return 2. * M_PI * pow( 3. / 4. * mass_kg / STEEL_DENSITY / M_PI, 2. / 3. );
80}
81
86{
87 o->heat_T = CONST_SPACE_STAR_TEMP; /* Reset temperature. */
88 o->heat_start = CONST_SPACE_STAR_TEMP; /* For cooldown purposes. */
89 if ( o->outfit == NULL ) {
90 o->heat_C = 1.;
91 o->heat_area = 0.;
92 return;
93 }
96}
97
104{
105 p->heat_T = CONST_SPACE_STAR_TEMP;
106 for ( int i = 0; i < array_size( p->outfits ); i++ )
107 p->outfits[i]->heat_T = CONST_SPACE_STAR_TEMP;
108}
109
113static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o )
114{
115 switch ( o->type ) {
116 case OUTFIT_TYPE_BOLT:
117 case OUTFIT_TYPE_BEAM:
118 return p->stats.fwd_heat;
119
120 case OUTFIT_TYPE_TURRET_BOLT:
121 case OUTFIT_TYPE_TURRET_BEAM:
122 return p->stats.tur_heat;
123
124 default:
125 return 1.;
126 }
127}
128
136{
137 /* We consider that only 1% of the energy is lost in the form of heat,
138 * this keeps numbers safe. */
139 double hmod = pilot_heatOutfitMod( p, o->outfit );
140
141 o->heat_T += hmod * outfit_heat( o->outfit ) / o->heat_C;
142
143 /* Enforce a minimum value as a safety measure. */
144 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
145}
146
154void pilot_heatAddSlotTime( const Pilot *p, PilotOutfitSlot *o, double dt )
155{
156 double hmod = pilot_heatOutfitMod( p, o->outfit );
157
158 o->heat_T += ( hmod * outfit_heat( o->outfit ) / o->heat_C ) * dt;
159
160 /* Enforce a minimum value as a safety measure. */
161 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
162}
163
182double pilot_heatUpdateSlot( const Pilot *p, PilotOutfitSlot *o, double dt )
183{
184 /* Calculate energy leaving/entering ship chassis. */
185 double Q = -p->heat_cond * ( o->heat_T - p->heat_T ) * o->heat_area * dt;
186
187 /* Update current temperature. */
188 o->heat_T += Q / o->heat_C;
189
190 /* Return energy moved. */
191 return Q;
192}
193
215void pilot_heatUpdateShip( Pilot *p, double Q_cond, double dt )
216{
217 double Q, Q_rad;
218
219 /* Calculate radiation. */
220 Q_rad = CONST_STEFAN_BOLTZMANN * p->heat_area * p->heat_emis *
221 ( CONST_SPACE_STAR_TEMP_4 - pow( p->heat_T, 4. ) ) * dt;
222
223 /* Total heat movement. */
224 Q = Q_rad - Q_cond;
225
226 /* Update ship temperature. */
227 p->heat_T += Q / p->heat_C;
228}
229
237double pilot_heatEfficiencyMod( double T, double Tb, double Tc )
238{
239 return CLAMP( 0., 1., 1. - ( T - Tb ) / Tc );
240}
241
248{
249 double t = pow2( 1. - p->ctimer / p->cdelay );
250 p->heat_T = p->heat_start - CONST_SPACE_STAR_TEMP -
251 ( p->heat_start - CONST_SPACE_STAR_TEMP ) * t +
252 CONST_SPACE_STAR_TEMP;
253
254 for ( int i = 0; i < array_size( p->outfits ); i++ ) {
255 int ammo_threshold;
256 PilotOutfitSlot *o = p->outfits[i];
257 o->heat_T = o->heat_start - CONST_SPACE_STAR_TEMP -
258 ( o->heat_start - CONST_SPACE_STAR_TEMP ) * t +
259 CONST_SPACE_STAR_TEMP;
260
261 /* Refill ammo too (also part of Active Cooldown) */
262 /* Must be valid outfit. */
263 if ( o->outfit == NULL )
264 continue;
265
266 /* Initial (raw) ammo threshold */
267 ammo_threshold = round( t * pilot_maxAmmoO( p, o->outfit ) );
268
269 /* Adjust for deployed fighters if needed */
270 if ( outfit_isFighterBay( o->outfit ) )
271 ammo_threshold -= o->u.ammo.deployed;
272
273 if ( o->u.ammo.quantity < ammo_threshold )
274 pilot_addAmmo( p, p->outfits[i], ammo_threshold - o->u.ammo.quantity );
275 }
276}
277
281double pilot_heatAccuracyMod( double T )
282{
283 return CLAMP( 0., 1., ( T - 500. ) / 600. );
284}
285
289double pilot_heatFireRateMod( double T )
290{
291 return CLAMP( 0., 1., ( 1100. - T ) / 300. );
292}
293
298double pilot_heatFirePercent( double T )
299{
300 return 2. * pilot_heatAccuracyMod( T );
301}
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:179
Header file with generic functions and naev-specifics.
#define CLAMP(a, b, x)
Definition naev.h:41
#define pow2(x)
Definition naev.h:53
#define MAX(x, y)
Definition naev.h:37
int outfit_isFighterBay(const Outfit *o)
Checks if outfit is a fighter bay.
Definition outfit.c:701
double outfit_heat(const Outfit *o)
Gets the outfit's heat generation.
Definition outfit.c:877
void pilot_heatAddSlot(const Pilot *p, PilotOutfitSlot *o)
Adds heat to an outfit slot.
Definition pilot_heat.c:135
double pilot_heatEfficiencyMod(double T, double Tb, double Tc)
Returns a 0:1 modifier representing efficiency (1. being normal).
Definition pilot_heat.c:237
void pilot_heatUpdateCooldown(Pilot *p)
Overrides the usual heat model during active cooldown.
Definition pilot_heat.c:247
double pilot_heatFireRateMod(double T)
Returns a 0:1 modifier representing fire rate (1. being normal).
Definition pilot_heat.c:289
void pilot_heatCalcSlot(PilotOutfitSlot *o)
Calculates the heat parameters for a pilot's slot.
Definition pilot_heat.c:85
static double pilot_heatOutfitMod(const Pilot *p, const Outfit *o)
Gets the heat mod for an outfit.
Definition pilot_heat.c:113
void pilot_heatReset(Pilot *p)
Resets a pilot's heat.
Definition pilot_heat.c:103
double pilot_heatAccuracyMod(double T)
Returns a 0:1 modifier representing accuracy (0. being normal).
Definition pilot_heat.c:281
void pilot_heatUpdateShip(Pilot *p, double Q_cond, double dt)
Heats the pilot's ship.
Definition pilot_heat.c:215
double pilot_heatCalcOutfitC(const Outfit *o)
Calculates the thermal mass of an outfit.
Definition pilot_heat.c:63
void pilot_heatCalc(Pilot *p)
Calculates the heat parameters for a pilot.
Definition pilot_heat.c:32
double pilot_heatCalcOutfitArea(const Outfit *o)
Calculates the effective transfer area of an outfit.
Definition pilot_heat.c:75
double pilot_heatUpdateSlot(const Pilot *p, PilotOutfitSlot *o, double dt)
Heats the pilot's slot.
Definition pilot_heat.c:182
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:298
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:154
int pilot_maxAmmoO(const Pilot *p, const Outfit *o)
Gets the maximum available ammo for a pilot for a specific outfit.
int pilot_addAmmo(Pilot *pilot, PilotOutfitSlot *s, int quantity)
Adds some ammo to the pilot stock.
A ship outfit, depends radically on the type.
Definition outfit.h:372
OutfitType type
Definition outfit.h:452
double mass
Definition outfit.h:384
Stores an outfit the pilot has.
Definition pilot.h:145
PilotOutfitAmmo ammo
Definition pilot.h:174
double heat_C
Definition pilot.h:155
double heat_start
Definition pilot.h:157
double heat_T
Definition pilot.h:154
double heat_area
Definition pilot.h:156
const Outfit * outfit
Definition pilot.h:149
The representation of an in-game pilot.
Definition pilot.h:263