banished_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 if c.school {
48 result.students += 1;
49 } else if c.age < c.adult_age {
50 result.children += 1;
51 } else {
52 result.adults += 1;
53 }
54 for (i, n) in result.counts.iter_mut().enumerate() {
55 *n += (c.status >> i) & 1;
56 }
57 result.health += c.health;
58 result.happiness += c.happiness;
59 count += 1;
60 }
61 if count != 0 {
62 result.health /= count as f32;
63 result.happiness /= count as f32;
64 }
65 result
66}
67
68pub fn meter(value: f32, maximum: f32, icons: usize) -> Vec<(bool, bool)> {
73 let halves = ((value / maximum) * (icons as i32 * 2) as f32 + 0.5) as i32;
74 (0..icons)
75 .map(|i| {
76 let boundary = i as i32 * 2 + 1;
77 (boundary < halves, boundary == halves)
78 })
79 .collect()
80}
81
82pub fn set_icon(mask: &mut u32, bit: u8, on: bool, owner_disabled: bool) {
86 if !on {
87 *mask &= !(1 << (bit & 31));
88 } else if !owner_disabled {
89 *mask |= 1 << (bit & 31);
90 }
91}