unbanished_game/people/
status.rs1#[derive(Debug, Clone, Copy, Default)]
7pub struct CitizenValue {
8 pub age: f32,
10 pub adult_age: f32,
12 pub school: bool,
14 pub health: f32,
16 pub happiness: f32,
18 pub status: u32,
21}
22
23#[derive(Debug, Clone, Copy, Default, PartialEq)]
25pub struct TownStatus {
26 pub adults: u32,
28 pub students: u32,
30 pub children: u32,
32 pub counts: [u32; 6],
34 pub health: f32,
36 pub happiness: f32,
38}
39
40pub fn aggregate(citizens: impl IntoIterator<Item = CitizenValue>) -> TownStatus {
44 let mut result = TownStatus::default();
45 let mut count = 0;
46 for c in citizens {
47 #[allow(clippy::neg_cmp_op_on_partial_ord)]
49 let child = !(c.adult_age <= c.age);
50 if c.school {
51 result.students += 1;
52 } else if child {
53 result.children += 1;
54 } else {
55 result.adults += 1;
56 }
57 for (i, n) in result.counts.iter_mut().enumerate() {
58 *n += (c.status >> i) & 1;
59 }
60 result.health += c.health;
61 result.happiness += c.happiness;
62 count += 1;
63 }
64 if count != 0 {
65 result.health /= count as f32;
66 result.happiness /= count as f32;
67 }
68 result
69}
70
71pub fn meter(value: f32, maximum: f32, icons: usize) -> Vec<(bool, bool)> {
76 let halves = ((value / maximum) * (icons as i32 * 2) as f32 + 0.5) as i32;
77 (0..icons)
78 .map(|i| {
79 let boundary = i as i32 * 2 + 1;
80 (boundary < halves, boundary == halves)
81 })
82 .collect()
83}
84
85pub fn set_icon(mask: &mut u32, bit: u8, on: bool, owner_disabled: bool) {
89 if !on {
90 *mask &= !(1 << (bit & 31));
91 } else if !owner_disabled {
92 *mask |= 1 << (bit & 31);
93 }
94}