conspire/
lib.rs

1#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
2#![doc = include_str!("../README.md")]
3
4#[cfg(feature = "constitutive")]
5pub mod constitutive;
6
7#[cfg(feature = "fem")]
8#[path = "domain/fem/mod.rs"]
9pub mod fem;
10
11#[cfg(feature = "math")]
12pub mod math;
13
14#[cfg(feature = "mechanics")]
15pub mod mechanics;
16
17#[cfg(feature = "vem")]
18#[path = "domain/vem/mod.rs"]
19pub mod vem;
20
21#[cfg(test)]
22mod test;
23
24use std::{
25    sync::atomic::{AtomicU64, Ordering},
26    time::{SystemTime, UNIX_EPOCH},
27};
28
29/// Absolute tolerance.
30pub const ABS_TOL: f64 = 1e-12;
31
32/// Relative tolerance.
33pub const REL_TOL: f64 = 1e-12;
34
35#[cfg(test)]
36/// A perturbation.
37pub const EPSILON: f64 = 1e-6;
38
39#[allow(dead_code)]
40#[cfg_attr(coverage_nightly, coverage(off))]
41fn defeat_message<'a>() -> &'a str {
42    match random_u8(14) {
43        0 => "Game over.",
44        1 => "I am Error.",
45        2 => "Insert coin to continue.",
46        3 => "Now let's all agree to never be creative again.",
47        4 => "Oh dear, you are dead!",
48        5 => "Press F to pay respects.",
49        6 => "Surprise! You're dead!",
50        7 => "Task failed successfully.",
51        8 => "This is not your grave, but you are welcome in it.",
52        9 => "To be continued...",
53        10 => "What a horrible night to have a curse.",
54        11 => "You cannot give up just yet.",
55        12 => "You have died of dysentery.",
56        13 => "You lost the game.",
57        14.. => "You've met with a terrible fate, haven't you?",
58    }
59}
60
61#[allow(dead_code)]
62#[cfg_attr(coverage_nightly, coverage(off))]
63fn victory_message<'a>() -> &'a str {
64    match random_u8(7) {
65        0 => "A winner is you!",
66        1 => "Bird up!",
67        2 => "Congraturation, this story is happy end!",
68        3 => "Flawless victory.",
69        4 => "Hey, that's pretty good!",
70        5 => "Nice work, bone daddy.",
71        6 => "That's Numberwang!",
72        7.. => "That was totes yeet, yo!",
73    }
74}
75
76fn random_u8(max: u8) -> u8 {
77    if max == u8::MAX {
78        return get_random();
79    }
80    // let range = (max as u16) + 1;
81    // let threshold = ((256_u16 / range) * range) as u8;
82    let mut attempts = 0;
83    loop {
84        let val = get_random();
85        // if val < threshold {
86        //     return val % (max + 1);
87        // }
88        attempts += 1;
89        if attempts > 10 {
90            return val % (max + 1);
91        }
92    }
93}
94
95fn get_random() -> u8 {
96    static STATE: AtomicU64 = AtomicU64::new(0);
97    let mut s = STATE.load(Ordering::Relaxed);
98    if s == 0 {
99        let now = SystemTime::now()
100            .duration_since(UNIX_EPOCH)
101            .unwrap_or_default();
102        s = 1 + now.as_nanos() as u64;
103    }
104    s ^= s << 13;
105    s ^= s >> 7;
106    s ^= s << 17;
107    STATE.store(s, Ordering::Relaxed);
108    (s >> 56) as u8
109}