atmosim 0.1.0

A library for calculating most efficient gas bombs in Space Station 14 game
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Handles game config. If you want to add new variable, put it into `GameConfig` struct,
//! then put its Wizden (or whatever server uses it) value into `GameConfig`'s Default implementation
//! Oh, and if the variable you add can be transformed for performance, add it into `ConfigCache`
//! and its Default impl. Though do it wisely as extra memory usage can degrade performance.
// Ideally we can make yet another struct that drops unneeded variables but lazy ^
//! Finally, if you want to create/edit a preset, just create/edit its function in impl `GameConfig`.

use core::cmp::Reverse;

use arrayvec::ArrayVec;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{
    Atmosim,
    gases::{Gas, GasProperties, MoleProperties, gases},
    maxcap::GasContainerPrototype,
    reactions::{GasReaction, GasReactionPrototype, ReactionBuffer},
};

// Basic atmos constants no server would change
pub(crate) const R: f32 = 8.314_463; // anything more precise is just truncated anyway
pub(crate) const ONE_ATMOSPHERE: f32 = 101.325;
pub(crate) const TCMB: f32 = 2.7;
pub(crate) const T0C: f32 = 273.15;
pub(crate) const MIN_MOLES: f32 = 0.000_000_05;

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct GameConfig {
    pub atmospherics: Atmospherics,

    // CVars
    pub heat_scale: f32,

    /// goob and mono have some reactions from funky that should produce hydrogen.
    /// they, however, don't have hydrogen. so they produce water vapour instead.
    /// yes, this field is giga spaghetti but still better than copy-pasting.
    pub hydrogen_substitute: Gas,

    pub gas_properties: GasProperties,
    pub container: GasContainerPrototype,
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub reactions: ReactionBuffer,
}

// Default fields in structs when
impl Default for GameConfig {
    fn default() -> Self {
        Self {
            atmospherics: Atmospherics::default(),
            // cvars
            heat_scale: 8.,

            hydrogen_substitute: Gas::Hydrogen,

            gas_properties: GasProperties::default(),
            reactions: ReactionBuffer::default(),
            container: GasContainerPrototype::default(),
        }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Atmospherics {
    pub tmax: f32,
    pub minimum_heat_capacity: f32,

    pub ammonia_oxygen_reaction_rate: f32,

    pub n2o_decomposition_rate: f32,

    pub frezon_cool_lower_temperature: f32,
    pub frezon_cool_mid_temperature: f32,
    pub frezon_cool_maximum_energy_modifier: f32,
    pub frezon_cool_rate_modifier: f32,
    pub frezon_nitrogen_cool_ratio: f32,
    pub frezon_cool_energy_released: f32,

    pub frezon_production_max_efficiency_temperature: f32,
    pub frezon_production_nitrogen_ratio: f32,
    pub frezon_production_trit_ratio: f32,
    pub frezon_production_conversion_rate: f32,

    pub fire_minimum_temperature_to_exist: f32,
    pub oxygen_burn_rate_base: f32,

    pub plasma_upper_temperature: f32,
    pub plasma_minimum_burn_temperature: f32,
    pub plasma_super_saturation_ends: f32,
    pub plasma_super_saturation_threshold: f32,
    pub plasma_oxygen_fullburn: f32,
    pub plasma_burn_rate_delta: f32,
    pub plasma_fire_energy_released: f32,

    pub tritium_minimum_oxyburn_energy: f32,
    pub tritium_burn_oxy_factor: f32,
    pub tritium_burn_fuel_ratio: f32,
    pub tritium_burn_trit_factor: f32,
    pub hydrogen_fire_energy_released: f32,

    // goob
    pub n2o_formation_energy: f32,

    // funky and others
    pub bz_production_energy: f32,
    pub healium_production_energy: f32,
    pub nitrium_production_energy: f32,
    pub nitrium_decomposition_energy: f32,
    pub pluoxium_production_energy: f32,

    // funky only
    pub minimum_hydrogen_oxyburn_energy: f32,
    pub hydrogen_burn_oxy_factor: f32,
    pub hyper_noblium_production_energy: f32,
    pub halon_combustion_energy: f32,
    pub zauker_production_energy: f32,
    pub zauker_temperature_scale: f32,
    pub zauker_decomposition_energy: f32,
    pub zauker_decomposition_max_rate: f32,
    pub proto_nitrate_production_energy: f32,
    pub proto_nitrate_conversion_energy: f32,
    pub proto_nitrate_bz_conversion_energy: f32,

    // klovn
    pub zipion_production_conversion_rate: f32,
}

impl Default for Atmospherics {
    fn default() -> Self {
        let sup_sat_thr = 96.;
        Self {
            tmax: 262_144.,
            minimum_heat_capacity: 0.0003,

            ammonia_oxygen_reaction_rate: 2.,

            n2o_decomposition_rate: 2.,

            frezon_cool_lower_temperature: 23.15,
            frezon_cool_mid_temperature: 373.15,

            frezon_cool_maximum_energy_modifier: 10.,
            frezon_cool_rate_modifier: 20.,
            frezon_nitrogen_cool_ratio: 5.,
            frezon_cool_energy_released: -600e3,
            frezon_production_max_efficiency_temperature: 73.15,
            frezon_production_nitrogen_ratio: 10.,
            frezon_production_trit_ratio: 50.,
            frezon_production_conversion_rate: 50.,
            fire_minimum_temperature_to_exist: T0C + 100.,
            oxygen_burn_rate_base: 1.4,

            plasma_upper_temperature: 1370. + T0C,
            plasma_minimum_burn_temperature: 100. + T0C,
            plasma_super_saturation_ends: sup_sat_thr / 3.,
            plasma_super_saturation_threshold: sup_sat_thr,
            plasma_oxygen_fullburn: 10.,
            plasma_burn_rate_delta: 9.,
            plasma_fire_energy_released: 160e3,

            tritium_minimum_oxyburn_energy: 143_000.,
            tritium_burn_oxy_factor: 100.,
            tritium_burn_fuel_ratio: 2.,
            tritium_burn_trit_factor: 10.,
            hydrogen_fire_energy_released: 284e3,

            n2o_formation_energy: 10e3,
            bz_production_energy: 80e3,
            healium_production_energy: 9e3,

            nitrium_production_energy: -100e3,
            nitrium_decomposition_energy: 30e3,

            pluoxium_production_energy: 250.,

            minimum_hydrogen_oxyburn_energy: 143_000.,
            hydrogen_burn_oxy_factor: 100.,

            hyper_noblium_production_energy: 2e7,

            halon_combustion_energy: -2500.,
            zauker_production_energy: 5000.,
            zauker_temperature_scale: 5e-6,
            zauker_decomposition_energy: 460.,
            zauker_decomposition_max_rate: 20.,

            proto_nitrate_production_energy: 650.,
            proto_nitrate_conversion_energy: 2000.,
            proto_nitrate_bz_conversion_energy: -10_000.,

            zipion_production_conversion_rate: 40.,
        }
    }
}

impl Atmosim {
    #[must_use]
    pub fn new(mut game: GameConfig) -> Self {
        // init reactions
        if let ReactionBuffer::Protos(protos) = game.reactions {
            let mut reactions = protos
                .iter()
                .map(|&proto| GasReaction::cache(proto))
                .collect::<ArrayVec<_, _>>();

            reactions.sort_unstable_by_key(|reaction| Reverse(reaction.priority)); // probably needs some tweaking to perfectly emulate C#
            game.reactions = ReactionBuffer::Inited(reactions);
        }

        // inverse stuff
        let inverses = Inverses {
            heat_scale: 1. / game.heat_scale,
            frez_cool_mid_minus_lower_temp: 1.
                / (game.atmospherics.frezon_cool_mid_temperature
                    - game.atmospherics.frezon_cool_lower_temperature),
            plasma_upper_minus_min_burn_temp: 1.
                / (game.atmospherics.plasma_upper_temperature
                    - game.atmospherics.plasma_minimum_burn_temperature),
            plasma_supsat_thres_minus_ends: 1.
                / (game.atmospherics.plasma_super_saturation_threshold
                    - game.atmospherics.plasma_super_saturation_ends),
            plasma_burn_rate_delta: 1. / game.atmospherics.plasma_burn_rate_delta,
            plasma_o2_full_burn: 1. / game.atmospherics.plasma_oxygen_fullburn,
            tritium_burn_fuel_ratio: 1. / game.atmospherics.tritium_burn_fuel_ratio,
            tritium_burn_trit_factor: 1. / game.atmospherics.tritium_burn_trit_factor,
            tritium_burn_oxy_factor: 1. / game.atmospherics.tritium_burn_oxy_factor,
        };

        // divide all HCs by heat scale
        for (_, property) in &mut game.gas_properties.0 {
            property.specific_heat *= inverses.heat_scale;
        }

        Self { inverses, game }
    }
}

impl GameConfig {
    #[must_use]
    pub fn wizden_canister() -> Self {
        Self {
            atmospherics: Atmospherics {
                hydrogen_fire_energy_released: 284e4,
                ..Default::default()
            },
            container: GasContainerPrototype::default_canister(),
            ..Default::default()
        }
    }

    #[must_use]
    pub fn wizden_pmr() -> Self {
        Self {
            atmospherics: Atmospherics {
                hydrogen_fire_energy_released: 284e4,
                ..Default::default()
            },
            container: GasContainerPrototype::old_default_tank(),
            ..Default::default()
        }
    }

    #[must_use]
    pub fn goob() -> Self {
        Self {
            atmospherics: Atmospherics {
                tmax: 262_144_000_000.,
                ..Default::default()
            },
            hydrogen_substitute: Gas::WaterVapor,
            reactions: ReactionBuffer::Protos(
                [
                    GasReactionPrototype::plasma_fire(),
                    GasReactionPrototype::old_tritium_fire(),
                    GasReactionPrototype::frezon_coolant(),
                    GasReactionPrototype::frezon_production(),
                    GasReactionPrototype::ammonia_oxygen(),
                    GasReactionPrototype::n2o_decomposition(),
                    GasReactionPrototype::funky_bz_formation(),
                    GasReactionPrototype::goob_n2o_formation(),
                    GasReactionPrototype::funky_nitrium_production(),
                    GasReactionPrototype::funky_nitrium_decomposition(),
                    GasReactionPrototype::funky_pluoxium_production(),
                ]
                .into_iter()
                .collect(),
            ),
            container: GasContainerPrototype::old_default_tank(),
            ..Default::default()
        }
    }

    #[must_use]
    pub fn frontier() -> Self {
        let sup_sat_thr = 30.;
        Self {
            atmospherics: Atmospherics {
                plasma_super_saturation_threshold: sup_sat_thr,
                plasma_super_saturation_ends: sup_sat_thr / 3.,
                plasma_upper_temperature: 700.,
                ..Default::default()
            },
            reactions: ReactionBuffer::Protos(
                [
                    GasReactionPrototype::plasma_fire(),
                    GasReactionPrototype::frontier_tritium_fire(),
                    GasReactionPrototype::frezon_coolant(),
                    GasReactionPrototype::frezon_production(),
                    GasReactionPrototype::ammonia_oxygen(),
                    GasReactionPrototype::n2o_decomposition(),
                ]
                .into_iter()
                .collect(),
            ),
            container: GasContainerPrototype::old_default_tank(),
            ..Default::default()
        }
    }

    #[must_use]
    pub fn monolith() -> Self {
        Self {
            hydrogen_substitute: Gas::WaterVapor,
            reactions: ReactionBuffer::Protos(
                [
                    GasReactionPrototype::plasma_fire(),
                    GasReactionPrototype::frontier_tritium_fire(),
                    GasReactionPrototype::frezon_coolant(),
                    GasReactionPrototype::frezon_production(),
                    GasReactionPrototype::ammonia_oxygen(),
                    GasReactionPrototype::n2o_decomposition(),
                    GasReactionPrototype::funky_bz_formation(),
                    GasReactionPrototype::mono_nitrium_production(),
                    GasReactionPrototype::funky_nitrium_decomposition(),
                    GasReactionPrototype::funky_pluoxium_production(),
                ]
                .into_iter()
                .collect(),
            ),
            gas_properties: GasProperties(gases! {
                // for some reason on mono healium's specific heat is 20
                Gas::Healium => MoleProperties { specific_heat: 20., mass: 15. },
                other => GasProperties::default().0[other] // other gases are the same
            }),
            container: GasContainerPrototype {
                volume: 10.,
                ..GasContainerPrototype::old_default_tank()
            },
            ..Self::frontier() // inherits frontier
        }
    }
    #[must_use]
    pub fn funky() -> Self {
        Self {
            atmospherics: Atmospherics {
                tmax: 262_144_000.,
                ..Default::default()
            },
            reactions: ReactionBuffer::Protos(
                [
                    GasReactionPrototype::plasma_fire(),
                    GasReactionPrototype::tritium_fire(),
                    GasReactionPrototype::frezon_coolant(),
                    GasReactionPrototype::frezon_production(),
                    GasReactionPrototype::ammonia_oxygen(),
                    GasReactionPrototype::n2o_decomposition(),
                    GasReactionPrototype::funky_bz_formation(),
                    GasReactionPrototype::funky_nitrium_production(),
                    GasReactionPrototype::funky_nitrium_decomposition(),
                    GasReactionPrototype::funky_pluoxium_production(),
                    GasReactionPrototype::funky_hydrogen_fire(),
                    GasReactionPrototype::funky_hyper_noblium_production(),
                    GasReactionPrototype::funky_halon_oxygen_absorbtion(),
                    GasReactionPrototype::funky_zauker_production(),
                    GasReactionPrototype::funky_zauker_decomposition(),
                    GasReactionPrototype::funky_proto_nitrate_production(),
                    GasReactionPrototype::funky_proto_nitrate_conversion(),
                    GasReactionPrototype::funky_proto_nitrate_bz_conversion(),
                ]
                .into_iter()
                .collect(),
            ),
            container: GasContainerPrototype::old_default_tank(),
            ..Default::default()
        }
    }
    // just funky as far as i can tell, except one number
    #[must_use]
    pub fn starlight() -> Self {
        Self {
            atmospherics: Atmospherics {
                hydrogen_fire_energy_released: 284e4,
                ..Self::funky().atmospherics
            },
            ..Self::funky()
        }
    }
    /// basically wizden PMR but with extra reactions
    #[must_use]
    pub fn klovn() -> Self {
        Self {
            reactions: ReactionBuffer::Protos(
                [
                    GasReactionPrototype::plasma_fire(),
                    GasReactionPrototype::tritium_fire(),
                    GasReactionPrototype::frezon_coolant(),
                    GasReactionPrototype::frezon_production(),
                    GasReactionPrototype::ammonia_oxygen(),
                    GasReactionPrototype::n2o_decomposition(),
                    GasReactionPrototype::klovn_zipion_formation(),
                    GasReactionPrototype::klovn_argon_formation(),
                    GasReactionPrototype::klovn_argon_absorbtion(),
                ]
                .into_iter()
                .collect(),
            ),
            ..Self::wizden_pmr()
        }
    }
}

// Stuff that is commonly used as denominator, because division is expensive
#[derive(Debug)]
pub(crate) struct Inverses {
    pub heat_scale: f32,

    pub frez_cool_mid_minus_lower_temp: f32,

    pub plasma_upper_minus_min_burn_temp: f32,
    pub plasma_supsat_thres_minus_ends: f32,
    pub plasma_burn_rate_delta: f32,
    pub plasma_o2_full_burn: f32,

    pub tritium_burn_fuel_ratio: f32,
    pub tritium_burn_trit_factor: f32,
    pub tritium_burn_oxy_factor: f32,
}