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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Reaction logic and gas reaction PROTOTYPE definitions. For C# side of reacions, check effects.rs
//! If you want to add new reaction, simply add it to impl `GasReactionPrototype`

use arrayvec::ArrayVec;
use log::trace;

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

use crate::{
    config::TCMB,
    effects::{GasReactionEffectSerialized, dispatch_reaction_effect},
    gases::{Gas, GasMixture, Moles, MolesWrapper, gases},
};

// Funky has 20 reactions if I've counted correctly 2 of which we don't use
// I doubt any fork has more, so 18 should just be enough
const REACTION_BUFFER_SIZE: usize = 18;
pub(crate) const ATMOS_TICKRATE: f32 = 0.5;

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ReactionBuffer {
    #[cfg_attr(feature = "serde", serde(rename = "reactions"))]
    Protos(ArrayVec<GasReactionPrototype, REACTION_BUFFER_SIZE>),
    #[cfg_attr(feature = "serde", serde(skip))]
    Inited(ArrayVec<GasReaction, REACTION_BUFFER_SIZE>),
}

impl Default for GasReactionPrototype {
    fn default() -> Self {
        Self {
            priority: 0,
            minimum_temperature: TCMB,
            maximum_temperature: f32::MAX,
            minimum_requirements: MolesWrapper::default(),
            minimum_energy: 0.,
            effect: GasReactionEffectSerialized::default(),
        }
    }
}

impl GasReactionPrototype {
    #[must_use]
    pub fn new(effect: GasReactionEffectSerialized) -> Self {
        Self {
            effect,
            ..Default::default()
        }
    }

    /// Found everywhere
    #[must_use]
    pub fn plasma_fire() -> Self {
        Self {
            priority: -2,
            minimum_temperature: 373.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Plasma => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::PlasmaFire)
        }
    }

    /// Found everywhere but frontier, mono and goob (they never merge upstream)
    #[must_use]
    pub fn tritium_fire() -> Self {
        Self {
            priority: -1,
            minimum_temperature: 373.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Tritium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::TritiumFire)
        }
    }

    /// Found everywhere
    #[must_use]
    pub fn frezon_coolant() -> Self {
        Self {
            priority: 1,
            minimum_temperature: 23.15,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrogen => 0.01,
                Gas::Frezon => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::FrezonCoolant)
        }
    }

    /// Found everywhere
    #[must_use]
    pub fn frezon_production() -> Self {
        Self {
            priority: 2,
            maximum_temperature: 73.15,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Nitrogen => 0.01,
                Gas::Tritium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::FrezonProduction)
        }
    }

    /// Found everywhere
    #[must_use]
    pub fn ammonia_oxygen() -> Self {
        Self {
            priority: 2,
            minimum_temperature: 323.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Ammonia => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::AmmoniaOxygen)
        }
    }

    /// Found everywhere
    #[must_use]
    pub fn n2o_decomposition() -> Self {
        Self {
            priority: 0,
            minimum_temperature: 850.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::NitrousOxide => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::N2ODecomposition)
        }
    }

    /// Found on servers that don't merge wizden upstream
    #[must_use]
    pub fn old_tritium_fire() -> Self {
        Self {
            priority: -1,
            minimum_temperature: 373.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Tritium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::TritiumFireOld)
        }
    }

    /// Frontier, mono
    #[must_use]
    pub fn frontier_tritium_fire() -> Self {
        Self {
            minimum_temperature: 700.,
            ..Self::old_tritium_fire()
        }
    }

    /// Goob
    #[must_use]
    pub fn goob_n2o_formation() -> Self {
        Self {
            priority: 2,
            minimum_temperature: 200.,
            maximum_temperature: 250., // it's hardcoded in the reaction itself BRUH
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Nitrogen => 0.01,
                Gas::BZ => 5., // hardcoded too, award
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::N2OFormation)
        }
    }

    /// Funky, goob, mono
    #[must_use]
    pub fn funky_bz_formation() -> Self {
        Self {
            priority: 2,
            minimum_temperature: 313.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Plasma => 10.,
                Gas::NitrousOxide => 10.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::BZFormation)
        }
    }

    /// Funky, goob
    #[must_use]
    pub fn funky_healium_production() -> Self {
        Self {
            // mono has no temp constaints for some reason
            maximum_temperature: 300.,
            minimum_temperature: 22.,
            ..Self::mono_healium_production()
        }
    }

    /// Mono
    #[must_use]
    pub fn mono_healium_production() -> Self {
        Self {
            priority: 2,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Frezon => 5.,
                Gas::BZ => 5.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::HealiumProduction)
        }
    }

    /// Goob, funky, for mono check `mono_nitrium_production()`
    #[must_use]
    pub fn funky_nitrium_production() -> Self {
        Self {
            priority: 2,
            minimum_temperature: 1500.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrogen => 10.,
                Gas::Tritium => 20.,
                Gas::BZ => 5.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::NitriumProduction)
        }
    }

    /// Mono
    #[must_use]
    pub fn mono_nitrium_production() -> Self {
        Self {
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrogen => 20., // MONO
                Gas::Tritium => 20.,
                Gas::BZ => 5.,
                _ => 0.,
            }),
            ..Self::funky_nitrium_production()
        }
    }

    /// Funky, goob, mono
    #[must_use]
    pub fn funky_nitrium_decomposition() -> Self {
        Self {
            priority: 2,
            maximum_temperature: 343.15, // both goob and mono have this hardcoded (AWARD!!)
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Nitrium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::NitriumDecomposition)
        }
    }

    /// Funky, goob, mono
    #[must_use]
    pub fn funky_pluoxium_production() -> Self {
        Self {
            priority: 3,
            minimum_temperature: 50.,
            maximum_temperature: 273.15,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::CarbonDioxide => 0.01,
                Gas::Tritium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::PluoxiumProduction)
        }
    }

    // funky pluoxium radiation production and metal hydrogen reactions are not supported
    // first one requires, well, radiation, second one only works in the atmosphere

    /// Funky
    #[must_use]
    pub fn funky_hydrogen_fire() -> Self {
        Self {
            priority: -1,
            minimum_temperature: 373.149,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Hydrogen => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::HydrogenFire)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_hyper_noblium_production() -> Self {
        Self {
            priority: 7,
            minimum_temperature: 2.7,
            maximum_temperature: 15.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrogen => 10.,
                Gas::Tritium => 5.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::HyperNobliumProduction)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_halon_oxygen_absorbtion() -> Self {
        Self {
            priority: -4,
            minimum_temperature: 373.15,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Oxygen => 0.01,
                Gas::Halon => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::HalonOxygenAbsorbtion)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_zauker_production() -> Self {
        Self {
            priority: 9,
            minimum_temperature: 50000.,
            maximum_temperature: 75000.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrium => 0.01,
                Gas::HyperNoblium => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ZaukerProduction)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_zauker_decomposition() -> Self {
        Self {
            priority: 10,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Nitrogen => 0.01,
                Gas::Zauker => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ZaukerDecomposition)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_proto_nitrate_production() -> Self {
        Self {
            priority: 11,
            minimum_temperature: 5000.,
            maximum_temperature: 10000.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Pluoxium => 0.01,
                Gas::Hydrogen => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ProtoNitrateProduction)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_proto_nitrate_conversion() -> Self {
        Self {
            priority: 12,
            minimum_requirements: MolesWrapper(gases! {
                Gas::ProtoNitrate => 100.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ProtoNitrateConversion)
        }
    }

    /// Funky
    #[must_use]
    pub fn funky_proto_nitrate_bz_conversion() -> Self {
        Self {
            priority: 13,
            minimum_temperature: 260.,
            maximum_temperature: 280.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::BZ => 0.01,
                Gas::ProtoNitrate => 0.01,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ProtoNitrateBZConversion)
        }
    }

    /// Klovn
    #[must_use]
    pub fn klovn_zipion_formation() -> Self {
        Self {
            priority: 2,
            maximum_temperature: 200.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Plasma => 1.,
                Gas::WaterVapor => 9.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ZipionFormation)
        }
    }

    /// Klovn
    #[must_use]
    pub fn klovn_argon_formation() -> Self {
        Self {
            priority: 2,
            maximum_temperature: 589.,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Frezon => 1.,
                Gas::Plasma => 2.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ArgonFormation)
        }
    }

    /// Klovn
    #[must_use]
    pub fn klovn_argon_absorbtion() -> Self {
        Self {
            priority: 3,
            minimum_requirements: MolesWrapper(gases! {
                Gas::Argon => 80.,
                _ => 0.,
            }),
            ..Self::new(GasReactionEffectSerialized::ArgonAbsorbtion)
        }
    }
}

impl Default for ReactionBuffer {
    fn default() -> Self {
        Self::Protos(
            [
                GasReactionPrototype::plasma_fire(),
                GasReactionPrototype::tritium_fire(),
                GasReactionPrototype::frezon_coolant(),
                GasReactionPrototype::frezon_production(),
                GasReactionPrototype::ammonia_oxygen(),
                GasReactionPrototype::n2o_decomposition(),
            ]
            .into_iter()
            .collect(),
        )
    }
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct GasReactionPrototype {
    pub effect: GasReactionEffectSerialized,
    /// Higher = happens first. If priorities are the same order is random
    pub priority: i8,
    pub minimum_temperature: f32,
    pub maximum_temperature: f32,
    pub minimum_energy: f32,
    pub minimum_requirements: MolesWrapper,
}

pub enum ReactionResult {
    NoReaction,
    Reacting,
    StopReactions,
}

pub type GasReactionEffectSignature = fn(&mut GasMixture) -> ReactionResult;

#[derive(Debug, Clone, Copy)]
pub struct GasReaction {
    pub minimum_temperature: f32,
    pub maximum_temperature: f32,
    pub minimum_requirements: Moles,
    pub minimum_energy: f32,
    pub priority: i8,
    pub effect: GasReactionEffectSignature,
}

impl GasReaction {
    pub(crate) fn cache(proto: GasReactionPrototype) -> Self {
        Self {
            priority: proto.priority,
            maximum_temperature: proto.maximum_temperature,
            minimum_temperature: proto.minimum_temperature,
            minimum_requirements: proto.minimum_requirements.0,
            minimum_energy: proto.minimum_energy,
            effect: dispatch_reaction_effect(proto.effect),
        }
    }
}

impl GasMixture<'_> {
    /// You probably shouldn't make a spherical gas mixture react in a vacuum,
    /// and instead put it in a container first, but you can do your thing
    /// Returns whether any reaction ran
    pub fn react(&mut self) -> bool {
        let mut reacted = false;
        let mut previous_priority = None;
        if let ReactionBuffer::Inited(reactions) = &self.engine.game.reactions {
            for reaction in reactions {
                // only leave those that fit the reqs
                if self.get_thermal_energy() >= reaction.minimum_energy
                    && self.temperature() >= reaction.minimum_temperature
                    && reaction.maximum_temperature >= self.temperature()
                    && reaction
                        .minimum_requirements
                        .iter()
                        .all(|(gas, required)| &self.get_moles(gas) >= required)
                {
                    // finally run the reaction
                    match (reaction.effect)(self) {
                        ReactionResult::Reacting => {
                            reacted = true;
                            previous_priority = priority_warn(previous_priority, reaction.priority);
                        }
                        ReactionResult::NoReaction => (),
                        ReactionResult::StopReactions => {
                            reacted = true;
                            priority_warn(previous_priority, reaction.priority);
                            break;
                        }
                    }
                }
            }
        } else {
            unreachable!("Engine should've been inited")
        }
        reacted
    }
}

fn priority_warn(previous: Option<i8>, current: i8) -> Option<i8> {
    let current_priority = Some(current);
    if previous == current_priority {
        trace!(
            "Two reactions with the same priority {current_priority:?} ran in a row, the result might be unpredictable"
        );
    }
    current_priority
}