naev 0.11.5
pilot_heat.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
11#include <math.h>
12
13#include "naev.h"
16#include "pilot_heat.h"
17
18#include "array.h"
19#include "log.h"
20
21/*
22 * Prototypes.
23 */
24static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o );
25
34{
35 double mass_kg;
36 mass_kg = 1000. * p->base_mass;
37 p->heat_emis = 0.8;
38 p->heat_cond = STEEL_HEAT_CONDUCTIVITY;
39 p->heat_C = STEEL_HEAT_CAPACITY * mass_kg;
40
41 /* We'll approximate area for a sphere.
42 *
43 * Sphere:
44 * V = 4/3*pi*r^3
45 * A = 4*pi*r^2
46 *
47 * Ship:
48 * V = mass/density
49 *
50 * We then equal the ship V and sphere V to obtain r:
51 * r = (3*mass)/(4*pi*density))^(1/3)
52 *
53 * Substituting r in A we get:
54 * A = 4*pi*((3*mass)/(4*pi*density))^(2/3)
55 * */
56 p->heat_area = 4.*M_PI*pow( 3./4.*mass_kg/STEEL_DENSITY/M_PI, 2./3. ) * p->stats.heat_dissipation;
57}
58
62double pilot_heatCalcOutfitC( const Outfit *o )
63{
64 /* Simple thermal mass. Put a floor on it so we get zero heat flux in case of NaN for massless outfits. */
65 return STEEL_HEAT_CAPACITY * MAX( 1000. * o->mass, 1. );
66}
67
74{
75 double mass_kg = MAX( 1000. * o->mass, 1. ); /* Avoid it being 0. */
76 /* We consider the effective area of outfits to be half of a sphere. */
77 return 2.*M_PI*pow( 3./4.*mass_kg/STEEL_DENSITY/M_PI, 2./3. );
78}
79
84{
85 o->heat_T = CONST_SPACE_STAR_TEMP; /* Reset temperature. */
86 o->heat_start = CONST_SPACE_STAR_TEMP; /* For cooldown purposes. */
87 if (o->outfit == NULL) {
88 o->heat_C = 1.;
89 o->heat_area = 0.;
90 return;
91 }
94}
95
102{
103 p->heat_T = CONST_SPACE_STAR_TEMP;
104 for (int i=0; i<array_size(p->outfits); i++)
105 p->outfits[i]->heat_T = CONST_SPACE_STAR_TEMP;
106}
107
111static double pilot_heatOutfitMod( const Pilot *p, const Outfit *o )
112{
113 switch (o->type) {
114 case OUTFIT_TYPE_BOLT:
115 case OUTFIT_TYPE_BEAM:
116 return p->stats.fwd_heat;
117
118 case OUTFIT_TYPE_TURRET_BOLT:
119 case OUTFIT_TYPE_TURRET_BEAM:
120 return p->stats.tur_heat;
121
122 default:
123 return 1.;
124 }
125}
126
134{
135 /* We consider that only 1% of the energy is lost in the form of heat,
136 * this keeps numbers safe. */
137 double hmod = pilot_heatOutfitMod( p, o->outfit );
138
139 o->heat_T += hmod * outfit_heat(o->outfit) / o->heat_C;
140
141 /* Enforce a minimum value as a safety measure. */
142 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
143}
144
152void pilot_heatAddSlotTime( const Pilot *p, PilotOutfitSlot *o, double dt )
153{
154 double hmod = pilot_heatOutfitMod( p, o->outfit );
155
156 o->heat_T += (hmod * outfit_heat(o->outfit) / o->heat_C) * dt;
157
158 /* Enforce a minimum value as a safety measure. */
159 o->heat_T = MAX( o->heat_T, CONST_SPACE_STAR_TEMP );
160}
161
180double pilot_heatUpdateSlot( const Pilot *p, PilotOutfitSlot *o, double dt )
181{
182 /* Calculate energy leaving/entering ship chassis. */
183 double Q = -p->heat_cond * (o->heat_T - p->heat_T) * o->heat_area * dt;
184
185 /* Update current temperature. */
186 o->heat_T += Q / o->heat_C;
187
188 /* Return energy moved. */
189 return Q;
190}
191
213void pilot_heatUpdateShip( Pilot *p, double Q_cond, double dt )
214{
215 double Q, Q_rad;
216
217 /* Calculate radiation. */
218 Q_rad = CONST_STEFAN_BOLTZMANN * p->heat_area * p->heat_emis *
219 (CONST_SPACE_STAR_TEMP_4 - pow(p->heat_T,4.)) * dt;
220
221 /* Total heat movement. */
222 Q = Q_rad - Q_cond;
223
224 /* Update ship temperature. */
225 p->heat_T += Q / p->heat_C;
226}
227
235double pilot_heatEfficiencyMod( double T, double Tb, double Tc )
236{
237 return CLAMP( 0., 1., 1. - (T - Tb) / Tc );
238}
239
246{
247 double t = pow2( 1. - p->ctimer / p->cdelay );
248 p->heat_T = p->heat_start - CONST_SPACE_STAR_TEMP - (p->heat_start -
249 CONST_SPACE_STAR_TEMP) * t + CONST_SPACE_STAR_TEMP;
250
251 for (int i=0; i<array_size(p->outfits); i++) {
252 int ammo_threshold;
253 PilotOutfitSlot *o = p->outfits[i];
254 o->heat_T = o->heat_start - CONST_SPACE_STAR_TEMP - (o->heat_start -
255 CONST_SPACE_STAR_TEMP) * t + CONST_SPACE_STAR_TEMP;
256
257 /* Refill ammo too (also part of Active Cooldown) */
258 /* Must be valid outfit. */
259 if (o->outfit == NULL)
260 continue;
261
262 /* Initial (raw) ammo threshold */
263 ammo_threshold = round(t * pilot_maxAmmoO(p,o->outfit));
264
265 /* Adjust for deployed fighters if needed */
266 if ( outfit_isFighterBay( o->outfit ) )
267 ammo_threshold -= o->u.ammo.deployed;
268
269 if ( o->u.ammo.quantity < ammo_threshold )
270 pilot_addAmmo( p, p->outfits[i], ammo_threshold - o->u.ammo.quantity );
271 }
272}
273
277double pilot_heatAccuracyMod( double T )
278{
279 return CLAMP( 0., 1., (T-500.)/600. );
280}
281
285double pilot_heatFireRateMod( double T )
286{
287 return CLAMP( 0., 1., (1100.-T)/300. );
288}
289
293double pilot_heatFirePercent( double T )
294{
295 return 2.*pilot_heatAccuracyMod(T);
296}
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:616
double outfit_heat(const Outfit *o)
Gets the outfit's heat generation.
Definition outfit.c:768
void pilot_heatAddSlot(const Pilot *p, PilotOutfitSlot *o)
Adds heat to an outfit slot.
Definition pilot_heat.c:133
double pilot_heatEfficiencyMod(double T, double Tb, double Tc)
Returns a 0:1 modifier representing efficiency (1. being normal).
Definition pilot_heat.c:235
void pilot_heatUpdateCooldown(Pilot *p)
Overrides the usual heat model during active cooldown.
Definition pilot_heat.c:245
double pilot_heatFireRateMod(double T)
Returns a 0:1 modifier representing fire rate (1. being normal).
Definition pilot_heat.c:285
void pilot_heatCalcSlot(PilotOutfitSlot *o)
Calculates the heat parameters for a pilot's slot.
Definition pilot_heat.c:83
static double pilot_heatOutfitMod(const Pilot *p, const Outfit *o)
Gets the heat mod for an outfit.
Definition pilot_heat.c:111
void pilot_heatReset(Pilot *p)
Resets a pilot's heat.
Definition pilot_heat.c:101
double pilot_heatAccuracyMod(double T)
Returns a 0:1 modifier representing accuracy (0. being normal).
Definition pilot_heat.c:277
void pilot_heatUpdateShip(Pilot *p, double Q_cond, double dt)
Heats the pilot's ship.
Definition pilot_heat.c:213
double pilot_heatCalcOutfitC(const Outfit *o)
Calculates the thermal mass of an outfit.
Definition pilot_heat.c:62
void pilot_heatCalc(Pilot *p)
Calculates the heat parameters for a pilot.
Definition pilot_heat.c:33
double pilot_heatCalcOutfitArea(const Outfit *o)
Calculates the effective transfer area of an outfit.
Definition pilot_heat.c:73
double pilot_heatUpdateSlot(const Pilot *p, PilotOutfitSlot *o, double dt)
Heats the pilot's slot.
Definition pilot_heat.c:180
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:293
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:152
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:328
OutfitType type
Definition outfit.h:402
double mass
Definition outfit.h:340
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:217