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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
//! C# reaction effects implementation. If you want to add more, just add more functions,
//! put a serializable definition into enum and pair them in the dispatch function.
//! Try to keep those as close to the game as possible for readadility.
//! This includes also hardconding most values the game hardcodes but excludes compiler warnings,
//! hardcoded values are for whatever reason changed on one server, and to some degree,
//! even performance. If there's a simple `if` that always evaluates to one value (which SS14 coders really love),
//! just leave if there. Whatever. It isn't worth your time proving is is indeed always true.
//! It's fine to skip what's already in YML though, which for some reasons these people also love.

use strum::IntoEnumIterator;

#[cfg(not(feature = "std"))]
use num_traits::Float;

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

use crate::{
    config::{R, T0C},
    gases::{Gas, GasMixture},
    reactions::{GasReactionEffectSignature, ReactionResult},
};

#[derive(Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum GasReactionEffectSerialized {
    AmmoniaOxygen,
    FrezonCoolant,
    FrezonProduction,
    N2ODecomposition,
    PlasmaFire,
    TritiumFire,
    TritiumFireOld,
    N2OFormation,
    /// a.k.a. bz production
    BZFormation,
    HealiumProduction,
    NitriumProduction,
    NitriumDecomposition,
    PluoxiumProduction,
    HydrogenFire,
    HyperNobliumProduction,
    HalonOxygenAbsorbtion,
    ZaukerProduction,
    ZaukerDecomposition,
    ProtoNitrateProduction,
    ProtoNitrateConversion,
    ProtoNitrateBZConversion,
    ZipionFormation,
    ArgonFormation,
    ArgonAbsorbtion,
    #[default]
    #[cfg_attr(feature = "serde", serde(skip_serializing))]
    /// A hack to provide a default value
    NoReaction,
}

pub(crate) const fn dispatch_reaction_effect(
    effect: GasReactionEffectSerialized,
) -> GasReactionEffectSignature {
    match effect {
        GasReactionEffectSerialized::AmmoniaOxygen => ammonia_oxygen_reaction,
        GasReactionEffectSerialized::FrezonCoolant => frezon_coolant_reaction,
        GasReactionEffectSerialized::FrezonProduction => frezon_production_reaction,
        GasReactionEffectSerialized::N2ODecomposition => n2o_decomposition_reaction,
        GasReactionEffectSerialized::PlasmaFire => plasma_fire_reaction,
        GasReactionEffectSerialized::TritiumFire => tritium_fire_reaction,
        GasReactionEffectSerialized::TritiumFireOld => old_tritium_fire_reaction,
        GasReactionEffectSerialized::N2OFormation => n2o_formation_reaction,
        GasReactionEffectSerialized::BZFormation => bz_formation_reaction,
        GasReactionEffectSerialized::HealiumProduction => healium_production_reaction,
        GasReactionEffectSerialized::NitriumProduction => nitrium_production_reaction,
        GasReactionEffectSerialized::NitriumDecomposition => nitrium_decomposition_reaction,
        GasReactionEffectSerialized::PluoxiumProduction => pluoxium_production_reaction,
        GasReactionEffectSerialized::HydrogenFire => hydrogen_fire_reaction,
        GasReactionEffectSerialized::HyperNobliumProduction => hyper_noblium_production_reaction,
        GasReactionEffectSerialized::HalonOxygenAbsorbtion => halon_oxygen_absorbtion_reaction,
        GasReactionEffectSerialized::ZaukerProduction => zauker_production_reaction,
        GasReactionEffectSerialized::ZaukerDecomposition => zauker_decomposition_reaction,
        GasReactionEffectSerialized::ProtoNitrateProduction => proto_nitrate_production_reaction,
        GasReactionEffectSerialized::ProtoNitrateConversion => proto_nitrate_conversion_reaction,
        GasReactionEffectSerialized::ProtoNitrateBZConversion => {
            proto_nitrate_bz_conversion_reaction
        }
        GasReactionEffectSerialized::ZipionFormation => zipion_formation_reaction,
        GasReactionEffectSerialized::ArgonFormation => argon_formation_reaction,
        GasReactionEffectSerialized::ArgonAbsorbtion => argon_absorbtion_reaction,
        // This is perfectly fine because serde won't allow this to be deserialized from user config.
        // And if this is a thing in a built-in preset... Well, we've got bigger problems!
        GasReactionEffectSerialized::NoReaction => panic!("Invalid reaction in the config"),
    }
}

// source: wizden

fn ammonia_oxygen_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let n_ammonia = mixture.get_moles(Gas::Ammonia);
    let n_oxygen = mixture.get_moles(Gas::Oxygen);
    let n_total = mixture.total_moles();

    let rate = (n_ammonia * n_oxygen).powi(2) / n_total.powi(4);

    let delta_moles = n_ammonia * 2. / g.atmospherics.ammonia_oxygen_reaction_rate * rate;

    if delta_moles <= 0. || n_ammonia - delta_moles < 0. {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Ammonia, -delta_moles);
    mixture.adjust_moles(Gas::Oxygen, -delta_moles);
    mixture.adjust_moles(Gas::NitrousOxide, delta_moles * 0.5);
    mixture.adjust_moles(Gas::WaterVapor, delta_moles * 1.5);

    ReactionResult::Reacting
}

fn frezon_coolant_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;
    let i = &mixture.engine.inverses;

    let old_geat_capacity = mixture.get_heat_capacity();
    let temperature = mixture.temperature();

    let mut energy_modifier = 1.;
    let mut scale = (temperature - g.atmospherics.frezon_cool_lower_temperature)
        * i.frez_cool_mid_minus_lower_temp;
    if scale > 1. {
        energy_modifier = scale.min(g.atmospherics.frezon_cool_maximum_energy_modifier);
        scale = 1.;
    }

    if scale <= 0. {
        return ReactionResult::NoReaction;
    }

    let initial_nit = mixture.get_moles(Gas::Nitrogen);
    let initial_frezon = mixture.get_moles(Gas::Frezon);

    let burn_rate = initial_frezon * scale / g.atmospherics.frezon_cool_rate_modifier;

    let mut energy_released = 0.;
    if burn_rate > g.atmospherics.minimum_heat_capacity {
        let nit_amt = (burn_rate * g.atmospherics.frezon_nitrogen_cool_ratio).min(initial_nit);
        let frezon_amt = burn_rate.min(initial_frezon);
        mixture.adjust_moles(Gas::Nitrogen, -nit_amt);
        mixture.adjust_moles(Gas::Frezon, -frezon_amt);
        mixture.adjust_moles(Gas::NitrousOxide, nit_amt + frezon_amt);
        energy_released = burn_rate * g.atmospherics.frezon_cool_energy_released * energy_modifier;
    }

    energy_released *= i.heat_scale;
    if energy_released >= 0. {
        return ReactionResult::NoReaction;
    }

    let new_heat_capacity = mixture.get_heat_capacity();
    if new_heat_capacity > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature(
            (temperature * old_geat_capacity + energy_released) / new_heat_capacity,
        );
    }

    ReactionResult::Reacting
}

fn frezon_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let initial_n2 = mixture.get_moles(Gas::Nitrogen);
    let initial_oxy = mixture.get_moles(Gas::Oxygen);
    let initial_trit = mixture.get_moles(Gas::Tritium);

    let efficiency =
        mixture.temperature() / g.atmospherics.frezon_production_max_efficiency_temperature;
    let loss = 1. - efficiency;

    let catalyst_limit =
        initial_n2 * (g.atmospherics.frezon_production_nitrogen_ratio / efficiency);
    let oxy_limit = initial_oxy.min(catalyst_limit) / g.atmospherics.frezon_production_trit_ratio;

    let trit_burned = oxy_limit.min(initial_trit);
    let oxy_burned = trit_burned * g.atmospherics.frezon_production_trit_ratio;

    let oxy_conversion = oxy_burned / g.atmospherics.frezon_production_conversion_rate;
    let trit_conversion = trit_burned / g.atmospherics.frezon_production_conversion_rate;
    let total = oxy_conversion + trit_conversion;

    mixture.adjust_moles(Gas::Oxygen, -oxy_conversion);
    mixture.adjust_moles(Gas::Tritium, -trit_conversion);
    mixture.adjust_moles(Gas::Frezon, total * efficiency);
    mixture.adjust_moles(Gas::Nitrogen, total * loss);

    ReactionResult::Reacting
}

fn n2o_decomposition_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let cache_n2o = mixture.get_moles(Gas::NitrousOxide);

    let burned_fuel = cache_n2o / g.atmospherics.n2o_decomposition_rate;

    if burned_fuel <= 0. || cache_n2o - burned_fuel < 0. {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::NitrousOxide, -burned_fuel);
    mixture.adjust_moles(Gas::Nitrogen, burned_fuel);
    mixture.adjust_moles(Gas::Oxygen, burned_fuel * 0.5);

    ReactionResult::Reacting
}

fn plasma_fire_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;
    let i = &mixture.engine.inverses;

    let mut energy_released = 0.;
    let old_heat_capacity = mixture.get_heat_capacity();
    let temperature = mixture.temperature();
    let mut fire = false;

    let temperature_scale = if temperature > g.atmospherics.plasma_upper_temperature {
        1.
    } else {
        (temperature - g.atmospherics.plasma_minimum_burn_temperature)
            * i.plasma_upper_minus_min_burn_temp
    };

    if temperature_scale > 0. {
        let oxygen_burn_rate = g.atmospherics.oxygen_burn_rate_base - temperature_scale;
        let mut plasma_burn_rate;

        let initial_oxygen_moles = mixture.get_moles(Gas::Oxygen);
        let initial_plasma_moles = mixture.get_moles(Gas::Plasma);

        let oxy_ratio = initial_oxygen_moles / initial_plasma_moles;
        let supersaturation = ((oxy_ratio - g.atmospherics.plasma_super_saturation_ends)
            * i.plasma_supsat_thres_minus_ends)
            .clamp(0., 1.);

        if initial_oxygen_moles > initial_plasma_moles * g.atmospherics.plasma_oxygen_fullburn {
            plasma_burn_rate = initial_plasma_moles * temperature_scale * i.plasma_burn_rate_delta;
        } else {
            plasma_burn_rate = temperature_scale
                * (initial_oxygen_moles * i.plasma_o2_full_burn)
                * i.plasma_burn_rate_delta;
        }

        if plasma_burn_rate > g.atmospherics.minimum_heat_capacity {
            plasma_burn_rate = plasma_burn_rate
                .min(initial_plasma_moles.min(initial_oxygen_moles / oxygen_burn_rate));
            mixture.set_moles(Gas::Plasma, initial_plasma_moles - plasma_burn_rate);
            mixture.set_moles(
                Gas::Oxygen,
                initial_oxygen_moles - plasma_burn_rate * oxygen_burn_rate,
            );

            mixture.adjust_moles(Gas::Tritium, plasma_burn_rate * supersaturation);
            mixture.adjust_moles(
                Gas::CarbonDioxide,
                plasma_burn_rate * (1. - supersaturation),
            );

            energy_released += g.atmospherics.plasma_fire_energy_released * plasma_burn_rate;
            energy_released *= i.heat_scale;
            fire = true;
        }

        if energy_released > 0. {
            let new_heat_capacity = mixture.get_heat_capacity();
            if new_heat_capacity > g.atmospherics.minimum_heat_capacity {
                mixture.set_temperature(
                    (temperature * old_heat_capacity + energy_released) / new_heat_capacity,
                );
            }
        }
    }

    if fire {
        ReactionResult::Reacting
    } else {
        ReactionResult::NoReaction
    }
}

fn tritium_fire_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;
    let i = &mixture.engine.inverses;

    let mut energy_released = 0.;
    let old_heat_capacity = mixture.get_heat_capacity();
    let temperature = mixture.temperature();
    let mut fire = ReactionResult::NoReaction;
    let mut burned_fuel;
    let initial_trit = mixture.get_moles(Gas::Tritium);

    if mixture.get_moles(Gas::Oxygen) < initial_trit
        || g.atmospherics.tritium_minimum_oxyburn_energy
            > (temperature * old_heat_capacity * g.heat_scale)
    {
        burned_fuel = mixture.get_moles(Gas::Oxygen) * i.tritium_burn_oxy_factor;
        if burned_fuel > initial_trit {
            burned_fuel = initial_trit;
        }

        mixture.adjust_moles(Gas::Tritium, -burned_fuel);
        mixture.adjust_moles(Gas::Oxygen, -burned_fuel * i.tritium_burn_fuel_ratio);
    } else {
        burned_fuel = initial_trit.min(mixture.get_moles(Gas::Oxygen) * i.tritium_burn_fuel_ratio)
            * i.tritium_burn_trit_factor;
        mixture.adjust_moles(Gas::Tritium, -burned_fuel);
        mixture.adjust_moles(Gas::Oxygen, -burned_fuel * i.tritium_burn_fuel_ratio);
        energy_released += g.atmospherics.hydrogen_fire_energy_released
            * burned_fuel
            * (g.atmospherics.tritium_burn_trit_factor - 1.);
    }

    if burned_fuel > 0. {
        energy_released += g.atmospherics.hydrogen_fire_energy_released * burned_fuel;

        mixture.adjust_moles(Gas::WaterVapor, burned_fuel);

        fire = ReactionResult::Reacting;
    }

    energy_released *= i.heat_scale;
    if energy_released > 0. {
        let new_heat_capacity = mixture.get_heat_capacity();
        if new_heat_capacity > g.atmospherics.minimum_heat_capacity {
            mixture.set_temperature(
                (temperature * old_heat_capacity + energy_released) / new_heat_capacity,
            );
        }
    }

    fire
}

// source: goob
fn old_tritium_fire_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;
    let i = &mixture.engine.inverses;

    let mut energy_released = 0.;
    let old_heat_capacity = mixture.get_heat_capacity();
    let temperature = mixture.temperature();
    let mut fire = ReactionResult::NoReaction;
    let mut burned_fuel;
    let initial_trit = mixture.get_moles(Gas::Tritium);

    if mixture.get_moles(Gas::Oxygen) < initial_trit
        || g.atmospherics.tritium_minimum_oxyburn_energy
            > (temperature * old_heat_capacity * g.heat_scale)
    {
        burned_fuel = mixture.get_moles(Gas::Oxygen) * i.tritium_burn_oxy_factor;
        if burned_fuel > initial_trit {
            burned_fuel = initial_trit;
        }

        mixture.adjust_moles(Gas::Tritium, -burned_fuel);
    } else {
        burned_fuel = initial_trit;
        mixture.set_moles(
            Gas::Tritium,
            mixture.get_moles(Gas::Tritium) * (1. - i.tritium_burn_trit_factor),
        );
        mixture.adjust_moles(Gas::Oxygen, -mixture.get_moles(Gas::Tritium));
        energy_released += g.atmospherics.hydrogen_fire_energy_released
            * burned_fuel
            * (g.atmospherics.tritium_burn_trit_factor - 1.);
    }

    if burned_fuel > 0. {
        energy_released += g.atmospherics.hydrogen_fire_energy_released * burned_fuel;

        mixture.adjust_moles(Gas::WaterVapor, burned_fuel);

        fire = ReactionResult::Reacting;
    }

    energy_released *= i.heat_scale;

    if energy_released > 0. {
        let new_heat_capacity = mixture.get_heat_capacity();
        if new_heat_capacity > g.atmospherics.minimum_heat_capacity {
            mixture.set_temperature(
                (temperature * old_heat_capacity + energy_released) / new_heat_capacity,
            );
        }
    }

    fire
}

fn n2o_formation_reaction(mixture: &mut GasMixture) -> ReactionResult {
    // goob only reaction, goob has no hypernob, hence no check
    let g = &mixture.engine.game;

    let init_oxygen = mixture.get_moles(Gas::Oxygen);
    let init_nitrogen = mixture.get_moles(Gas::Nitrogen);

    let n2o_added = (init_oxygen * 0.5).min(init_nitrogen);
    if init_nitrogen < n2o_added || init_oxygen * 0.5 < n2o_added {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::NitrousOxide, n2o_added);
    mixture.adjust_moles(Gas::Nitrogen, -n2o_added);
    mixture.adjust_moles(Gas::Oxygen, -0.5 * n2o_added);

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature(
            mixture.temperature() + (n2o_added * g.atmospherics.n2o_formation_energy),
        );
    }

    ReactionResult::Reacting
}

// source: funky
fn bz_formation_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_n2o = mixture.get_moles(Gas::NitrousOxide);
    let init_plasma = mixture.get_moles(Gas::Plasma);

    // a bit of different formula but same thing
    let environment_efficiency = mixture.engine.container().volume.powi(2)
        / (mixture.total_moles() * R * mixture.temperature());

    let ratio_efficiency = (init_n2o / init_plasma).min(1.0);
    let bz_formed = (0.01 * ratio_efficiency * environment_efficiency)
        .min(init_n2o * 2.5)
        .min(init_plasma * 1.25);

    let nitrous_oxide_decomposed = (4. * (init_plasma / (init_n2o + init_plasma) - 0.75)).max(0.);
    let mut nitrogen_added = 0.;
    let mut oxygen_added = 0.;
    if nitrous_oxide_decomposed > 0. {
        let amount_decomposed = 0.4 * bz_formed * nitrous_oxide_decomposed;
        nitrogen_added = amount_decomposed;
        oxygen_added = 0.5 * amount_decomposed;
    }
    let bz_added = bz_formed * (1. - nitrous_oxide_decomposed);
    let n2o_removed = 0.4 * bz_formed;
    let plasma_removed = 0.8 * bz_formed * (1. - nitrous_oxide_decomposed);

    if n2o_removed > init_n2o || plasma_removed > init_plasma {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::NitrousOxide, -n2o_removed);
    mixture.adjust_moles(Gas::Plasma, -plasma_removed);
    mixture.adjust_moles(Gas::Nitrogen, nitrogen_added);
    mixture.adjust_moles(Gas::Oxygen, oxygen_added);
    mixture.adjust_moles(Gas::BZ, bz_added);

    let energy_released =
        bz_formed * (g.atmospherics.bz_production_energy + nitrous_oxide_decomposed);
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn healium_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_bz = mixture.get_moles(Gas::BZ);
    let init_frezon = mixture.get_moles(Gas::Frezon);

    let efficiency = (mixture.temperature() * 0.3)
        .min(init_frezon * 0.36)
        .min(init_bz * 4.);

    let bz_removed = efficiency * 0.25;
    let frezon_removed = efficiency * 2.75;
    let healium_produced = efficiency * 3.;

    if efficiency <= 0. || init_frezon < frezon_removed || init_bz < bz_removed {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::BZ, -bz_removed);
    mixture.adjust_moles(Gas::Frezon, -frezon_removed);
    mixture.adjust_moles(Gas::Healium, healium_produced);

    let energy_released = efficiency * g.atmospherics.healium_production_energy;
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn nitrium_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_tritium = mixture.get_moles(Gas::Tritium);
    let init_nitrogen = mixture.get_moles(Gas::Nitrogen);
    let init_bz = mixture.get_moles(Gas::BZ);

    let efficiency = (mixture.temperature() * (1. / 2984.))
        .min(init_bz * 20.)
        .min(init_tritium)
        .min(init_nitrogen);

    let tritium_removed = efficiency;
    let nitrogen_removed = efficiency;
    let bz_removed = efficiency * 0.05;
    let nitrium_produced = efficiency;

    if efficiency <= 0.
        || init_tritium < tritium_removed
        || init_nitrogen < nitrogen_removed
        || init_bz < bz_removed
    {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Tritium, -tritium_removed);
    mixture.adjust_moles(Gas::Nitrogen, -nitrogen_removed);
    mixture.adjust_moles(Gas::BZ, -bz_removed);
    mixture.adjust_moles(Gas::Nitrium, nitrium_produced);

    let energy_released = efficiency * g.atmospherics.nitrium_production_energy;
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn nitrium_decomposition_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_nitrium = mixture.get_moles(Gas::Nitrium);
    // init_oxygen - not used in the original

    let efficiency = (mixture.temperature() * (1. / 2984.)).min(init_nitrium);

    let nitrium_removed = efficiency;
    let h2_produced = efficiency;
    let nitrogen_produced = efficiency;

    if efficiency <= 0. || init_nitrium < nitrium_removed {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Nitrium, -nitrium_removed);
    mixture.adjust_moles(g.hydrogen_substitute, h2_produced);
    mixture.adjust_moles(Gas::Nitrogen, nitrogen_produced);

    let energy_released = efficiency * g.atmospherics.nitrium_decomposition_energy;
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature(
            ((mixture.temperature() * heat_cap + energy_released) / heat_cap).min(T0C + 98.8),
        );
    }

    ReactionResult::Reacting
}

fn pluoxium_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_o2 = mixture.get_moles(Gas::Oxygen);
    let init_co2 = mixture.get_moles(Gas::CarbonDioxide);
    let init_trit = mixture.get_moles(Gas::Tritium);

    let produced_amount = 5f32.min(init_co2).min(init_o2 * 2.).min(init_trit * 100.);

    if produced_amount <= 0. {
        return ReactionResult::NoReaction;
    }

    let co2_removed = produced_amount;
    let oxy_removed = produced_amount * 0.5;
    let trit_removed = produced_amount * 0.01;
    let pluox_produced = produced_amount;
    let hydro_produced = produced_amount * 0.01;

    mixture.adjust_moles(Gas::CarbonDioxide, -co2_removed);
    mixture.adjust_moles(Gas::Oxygen, -oxy_removed);
    mixture.adjust_moles(Gas::Tritium, -trit_removed);
    mixture.adjust_moles(Gas::Pluoxium, pluox_produced);
    mixture.adjust_moles(g.hydrogen_substitute, hydro_produced);

    let energy_released = produced_amount * g.atmospherics.pluoxium_production_energy;
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

// those and past are funky exclusive

fn hydrogen_fire_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;
    let i = &mixture.engine.inverses;

    let mut energy_released = 0.;
    let old_heat_capacity = mixture.get_heat_capacity();
    let temperature = mixture.temperature();
    let mut fire = ReactionResult::NoReaction;
    let mut burned_fuel;
    let initial_h2 = mixture.get_moles(Gas::Hydrogen);
    let initial_o2 = mixture.get_moles(Gas::Oxygen);

    if initial_o2 < initial_h2
        || g.atmospherics.minimum_hydrogen_oxyburn_energy
            > (temperature * old_heat_capacity * g.heat_scale)
    {
        burned_fuel = initial_o2 / g.atmospherics.hydrogen_burn_oxy_factor;
        if burned_fuel > initial_h2 {
            burned_fuel = initial_h2;
        }
    } else {
        burned_fuel =
            initial_h2.min(initial_o2 * i.tritium_burn_fuel_ratio) * i.tritium_burn_trit_factor;
    }

    if burned_fuel <= 0. {
        return ReactionResult::NoReaction;
    }

    let oxygen_consumed = burned_fuel * i.tritium_burn_fuel_ratio;
    if initial_h2 < burned_fuel || initial_o2 < oxygen_consumed {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Hydrogen, -burned_fuel);
    mixture.adjust_moles(Gas::Oxygen, -oxygen_consumed);

    if burned_fuel > 0. {
        energy_released += g.atmospherics.hydrogen_fire_energy_released * burned_fuel;

        mixture.adjust_moles(Gas::WaterVapor, burned_fuel);
        fire = ReactionResult::Reacting;
    }

    energy_released *= i.heat_scale;
    if energy_released > 0. {
        let new_heat_capacity = mixture.get_heat_capacity();
        if new_heat_capacity > g.atmospherics.minimum_heat_capacity {
            mixture.set_temperature(
                (mixture.temperature() * new_heat_capacity + energy_released) / new_heat_capacity,
            );
        }
    }

    fire
}

fn hyper_noblium_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_n2 = mixture.get_moles(Gas::Nitrogen);
    let init_trit = mixture.get_moles(Gas::Tritium);
    let init_bz = mixture.get_moles(Gas::BZ);

    let reduction_factor = (init_trit / (init_trit + init_bz)).clamp(0.01, 1.);

    let nob_formed = ((init_n2 + init_trit) * 0.01)
        .min(init_trit * 1. / (5. * reduction_factor))
        .min(init_n2 * 0.1);
    if nob_formed < 0.
        || init_trit < 5. * nob_formed * reduction_factor
        || init_n2 < 10. * nob_formed
    {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Tritium, -5. * nob_formed * reduction_factor);
    mixture.adjust_moles(Gas::Nitrogen, -10. * nob_formed);
    mixture.adjust_moles(Gas::HyperNoblium, nob_formed);

    let energy_released =
        nob_formed * (g.atmospherics.hyper_noblium_production_energy / init_bz.max(1.));

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn halon_oxygen_absorbtion_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_halon = mixture.get_moles(Gas::Halon);
    let init_oxy = mixture.get_moles(Gas::Oxygen);

    let temperature = mixture.temperature();

    let heat_efficiency = (temperature / (g.atmospherics.fire_minimum_temperature_to_exist * 10.))
        .min(init_halon)
        .min(init_oxy * 0.05);

    if heat_efficiency < 0. {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Halon, -heat_efficiency);
    mixture.adjust_moles(Gas::Oxygen, -heat_efficiency * 20.);
    mixture.adjust_moles(Gas::Halon, heat_efficiency * 2.5);

    let energy_released = heat_efficiency * g.atmospherics.halon_combustion_energy;
    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn zauker_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_hyper_nob = mixture.get_moles(Gas::HyperNoblium);
    let init_nitrium = mixture.get_moles(Gas::Nitrium);

    let temperature = mixture.temperature();
    let heat_efficiency = (temperature * g.atmospherics.zauker_temperature_scale)
        .min(init_hyper_nob * 100.)
        .min(init_nitrium * 20.);

    if heat_efficiency < 0.
        || init_hyper_nob < heat_efficiency * 0.01
        || init_nitrium < heat_efficiency * 0.5
    {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::HyperNoblium, -heat_efficiency * 0.01);
    mixture.adjust_moles(Gas::Nitrium, -heat_efficiency * 0.5);
    mixture.adjust_moles(Gas::Zauker, heat_efficiency * 0.5);

    let energy_released = heat_efficiency * g.atmospherics.zauker_production_energy;

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn zauker_decomposition_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_zauker = mixture.get_moles(Gas::Zauker);
    let init_n2 = mixture.get_moles(Gas::Nitrogen);

    let burned_fuel = g
        .atmospherics
        .zauker_decomposition_max_rate
        .min(init_n2)
        .min(init_zauker);

    if burned_fuel <= 0. || init_zauker < burned_fuel {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Zauker, -burned_fuel);
    mixture.adjust_moles(Gas::Oxygen, burned_fuel * 0.3);
    mixture.adjust_moles(Gas::Nitrogen, burned_fuel * 0.7);

    let energy_released = burned_fuel * g.atmospherics.zauker_decomposition_energy;

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn proto_nitrate_production_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_pluox = mixture.get_moles(Gas::Pluoxium);
    let init_h2 = mixture.get_moles(Gas::Hydrogen);

    let temperature = mixture.temperature();
    let heat_efficiency = (temperature * 0.005)
        .min(init_pluox * 5.)
        .min(init_h2 * 0.5);

    if heat_efficiency <= 0. || init_pluox < heat_efficiency * 0.2 || init_h2 < heat_efficiency * 2.
    {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::Hydrogen, -heat_efficiency * 2.);
    mixture.adjust_moles(Gas::Pluoxium, -heat_efficiency * 0.2);
    mixture.adjust_moles(Gas::ProtoNitrate, heat_efficiency * 2.2);

    let energy_released = heat_efficiency * g.atmospherics.proto_nitrate_production_energy;

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn proto_nitrate_conversion_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_pn = mixture.get_moles(Gas::ProtoNitrate);
    let init_trit = mixture.get_moles(Gas::Tritium);
    let init_h2 = mixture.get_moles(Gas::Hydrogen);

    let temperature = mixture.temperature();

    let common_factor = 0.25 * (temperature * (1. / 250.) - 1.2).sin();
    let burned_h2 = init_h2 * common_factor;
    let burned_trit = init_trit * -common_factor;
    let produced_amount = burned_h2 + burned_trit;

    if init_trit < burned_trit && init_h2 < burned_h2 || init_pn < produced_amount * 0.01 {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::ProtoNitrate, -produced_amount * 0.01);
    mixture.adjust_moles(Gas::Plasma, produced_amount * 0.05);
    mixture.adjust_moles(Gas::Tritium, burned_h2 * 0.95 - burned_trit);
    mixture.adjust_moles(Gas::Hydrogen, burned_trit * 0.95 - burned_h2);

    let energy_released = (burned_trit * 0.95 - burned_h2 * 0.95)
        * g.atmospherics.proto_nitrate_conversion_energy
        + produced_amount * 2.5;

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

fn proto_nitrate_bz_conversion_reaction(mixture: &mut GasMixture) -> ReactionResult {
    if check_hyper_noblium(mixture) {
        return ReactionResult::NoReaction;
    }
    let g = &mixture.engine.game;

    let init_pn = mixture.get_moles(Gas::ProtoNitrate);
    let init_bz = mixture.get_moles(Gas::BZ);

    let temperature = mixture.temperature();
    let consumed_amount = (temperature / 2240. * init_bz * init_pn / (init_bz + init_pn))
        .min(init_bz)
        .min(init_pn);

    if consumed_amount <= 0. || init_bz < consumed_amount {
        return ReactionResult::NoReaction;
    }

    mixture.adjust_moles(Gas::BZ, -consumed_amount);
    mixture.adjust_moles(Gas::Nitrogen, consumed_amount * 0.4);
    mixture.adjust_moles(Gas::Helium, consumed_amount * 1.6);
    mixture.adjust_moles(Gas::Plasma, consumed_amount * 0.8);

    let energy_released = consumed_amount * g.atmospherics.proto_nitrate_bz_conversion_energy;

    let heat_cap = mixture.get_heat_capacity();
    if heat_cap > g.atmospherics.minimum_heat_capacity {
        mixture.set_temperature((mixture.temperature() * heat_cap + energy_released) / heat_cap);
    }

    ReactionResult::Reacting
}

// klovn reactions

fn zipion_formation_reaction(mixture: &mut GasMixture) -> ReactionResult {
    let g = &mixture.engine.game;

    let initial_vapor = mixture.get_moles(Gas::WaterVapor);
    let initial_plasma = mixture.get_moles(Gas::Plasma);

    let consumed_plasma = (initial_plasma / g.atmospherics.zipion_production_conversion_rate)
        .min(initial_vapor - (initial_vapor % 9.) / 9.);
    let consumed_vapor = consumed_plasma * 9.;
    let produced_zipion = consumed_plasma + consumed_vapor;

    mixture.adjust_moles(Gas::Plasma, -consumed_plasma);
    mixture.adjust_moles(Gas::WaterVapor, -consumed_vapor);
    mixture.adjust_moles(Gas::Zipion, produced_zipion);

    ReactionResult::Reacting
}

fn argon_formation_reaction(mixture: &mut GasMixture) -> ReactionResult {
    let initial_frezon = mixture.get_moles(Gas::Frezon);
    let initial_plasma = mixture.get_moles(Gas::Plasma);

    let consumed_frezon = initial_frezon.min(initial_plasma * 0.5);
    let consumed_plasma = consumed_frezon * 2.;
    let produced_argon = consumed_frezon * 3.;

    mixture.adjust_moles(Gas::Frezon, -consumed_frezon);
    mixture.adjust_moles(Gas::Plasma, -consumed_plasma);
    mixture.adjust_moles(Gas::Argon, produced_argon);

    ReactionResult::Reacting
}

fn argon_absorbtion_reaction(mixture: &mut GasMixture) -> ReactionResult {
    const ABSORBTION_RATE: f32 = 2.; // :face_holding_back_tears:

    let mut total_absorbed = 0.;

    for gas in Gas::iter() {
        if gas == Gas::Argon {
            continue;
        }

        let moles = mixture.get_moles(gas);
        if moles <= 0. {
            continue;
        }

        let absorb = ABSORBTION_RATE.min(moles);
        mixture.adjust_moles(gas, -absorb);
        total_absorbed += absorb;
    }

    if total_absorbed > 0. {
        ReactionResult::StopReactions
    } else {
        ReactionResult::NoReaction // no other gases anyway
    }
}

/// hypernoblium presence stops all reactions including its own production
/// this is funky specific but so is the gas so it's fine to just slap this everywhere
/// btw why the fuck is this just not its own reaction with high priority retuning `StopReactions`
fn check_hyper_noblium(mixture: &GasMixture) -> bool {
    mixture.temperature() > 20. && mixture.get_moles(Gas::HyperNoblium) >= 5.
}