unbanished_game/nature/wildlife/
population.rs1use super::*;
4
5impl Wild<'_, '_> {
6 pub fn start_population(&mut self, template: usize) {
10 let Some(kind) = self.wildlife.population_kinds.get(&template).cloned() else {
11 return;
12 };
13 let d = &kind.desc;
14 let entity = self.create(template, [0.0; 3]);
15 let n = self.n();
16 let width = n / d.cell.max(1);
17 let mut p = Population {
18 entity,
19 template,
20 width,
21 cells: vec![[0; 3]; (width * width) as usize],
22 herd_width: n / d.herd_block.max(1),
23 clock: 0,
24 cursor: 0,
25 };
26 for i in 0..width * width {
27 let c = p.capacity(d, self.cells, i);
28 p.cells[i as usize] = [c, c, c];
29 }
30 let index = self.wildlife.populations.len();
31 let herds = p.herd_width * p.herd_width;
32 self.wildlife.populations.push(p);
33 self.wildlife.kinds.insert(entity, Kind::Population(index));
34 let Some(herd_t) = kind.herd else { return };
35 let Some(hk) = self.wildlife.herd_kinds.get(&herd_t).cloned() else {
36 return;
37 };
38 for i in 0..herds {
39 let h = self.wildlife.herds.len();
40 self.wildlife.herds.push(Herd {
41 entity: usize::MAX,
42 template: herd_t,
43 population: index,
44 index: i,
45 animals: Vec::new(),
46 moving: false,
47 target: -1,
48 rest: 0,
49 regrow: 0,
50 });
51 let count =
52 self.rng.rand_below((hk.desc.max - hk.desc.min) as u32) as i32 + hk.desc.min;
53 self.spawn_herd(h, count);
54 let e = self.create(herd_t, [0.0; 3]);
56 self.wildlife.herds[h].entity = e;
57 self.wildlife.kinds.insert(e, Kind::Herd(h));
58 }
59 }
60
61 pub(super) fn update_population(&mut self, p: usize, dt: i64) {
63 let template = self.wildlife.populations[p].template;
64 let d = self.wildlife.population_kinds[&template].desc.clone();
65 let period = ticks(PERIOD);
66 let pop = &mut self.wildlife.populations[p];
67 pop.clock += dt;
68 while period < pop.clock {
69 pop.regrow(&d);
70 pop.clock -= period;
71 }
72 if !d.refresh {
73 return;
74 }
75 let total = pop.width * pop.width;
76 for _ in 0..total / 16 {
77 let i = pop.cursor;
78 let cap = pop.capacity(&d, self.cells, i);
79 let c = &mut pop.cells[i as usize];
80 c[0] = cap;
81 if cap < c[1] {
82 c[1] = cap;
83 }
84 if cap < c[2] {
85 c[2] = cap;
86 }
87 pop.cursor = if i + 1 == total { 0 } else { i + 1 };
88 }
89 }
90}
91
92impl Population {
93 pub(super) fn capacity(&self, d: &PopulationDescription, cells: &Cells, i: i32) -> i32 {
96 let n = cells.size as i32;
97 let cs = d.cell;
98 let (y0, x0) = ((i / self.width) * cs, (i % self.width) * cs);
99 let (mut habitat, mut forbid) = (0, 0u32);
100 for y in y0..y0 + cs {
101 for x in x0..x0 + cs {
102 let c = (n * y + x) as usize;
103 let bit = 1u32 << (cells.types[c].0 & 0x1f);
104 if bit & d.habitat != 0 {
105 habitat += 1;
106 }
107 forbid |= d.forbidden & bit;
108 if d.no_occupants && cells.occupants[c].is_some() {
109 forbid |= 1;
110 }
111 }
112 }
113 if forbid == 0 && habitat > 0 {
114 d.capacity
115 } else {
116 0
117 }
118 }
119
120 pub(super) fn block(&self) -> i32 {
122 self.width / self.herd_width.max(1)
123 }
124
125 pub(super) fn habitable_share(&self, index: i32) -> f32 {
127 let bs = self.block();
128 let (row0, col0) = (
129 (index / self.herd_width) * bs,
130 (index % self.herd_width) * bs,
131 );
132 let mut n = 0;
133 for r in 0..bs {
134 for c in 0..bs {
135 if self.cells[(self.width * (row0 + r) + col0 + c) as usize][0] > 0 {
136 n += 1;
137 }
138 }
139 }
140 n as f32 / (bs * bs) as f32
141 }
142
143 pub(super) fn pick(
146 &self,
147 d: &PopulationDescription,
148 index: i32,
149 n: i32,
150 rng: &mut Random,
151 ) -> i32 {
152 let bs = self.block();
153 let (row0, col0) = (
154 (index / self.herd_width) * bs,
155 (index % self.herd_width) * bs,
156 );
157 let mut found = Vec::new();
158 for r in 0..bs {
159 for c in 0..bs {
160 let i = self.width * (row0 + r) + col0 + c;
161 if self.cells[i as usize][0] > 0 {
162 found.push(i);
163 }
164 }
165 }
166 if found.is_empty() {
167 return -1;
168 }
169 let i = found[rng.rand_below(found.len() as u32) as usize];
170 let cs = d.cell;
171 n * (cs * (i / self.width) + (cs >> 1)) + cs * (i % self.width) + (cs >> 1)
172 }
173
174 pub(super) fn capacity_at(&self, d: &PopulationDescription, n: i32, cell: i32) -> i32 {
176 if cell < 0 {
177 return 0;
178 }
179 let (y, x) = (cell / n / d.cell, cell % n / d.cell);
180 self.cells
181 .get((y * self.width + x) as usize)
182 .map_or(0, |c| c[0])
183 }
184
185 pub(super) fn regrow(&mut self, d: &PopulationDescription) {
188 for c in &mut self.cells {
189 c[1] += d.regrowth;
190 if c[0] < c[1] {
191 c[1] = c[0];
192 }
193 }
194 let w = self.width;
195 let mut mean = vec![0; self.cells.len()];
196 for gy in 0..w {
197 for gx in 0..w {
198 let (mut sum, mut weights) = (0, 0);
199 for dy in -2..3 {
200 for dx in -2..3 {
201 let (x, y) = (gx + dx, gy + dy);
202 if x < 0 || y < 0 || x >= w || y >= w {
203 continue;
204 }
205 let c = self.cells[(w * y + x) as usize];
206 if c[0] == 0 {
207 continue;
208 }
209 let k = 16 - dx * dx - dy * dy;
210 if k >= 0 {
211 sum += c[1] * k;
212 weights += k;
213 }
214 }
215 }
216 mean[(w * gy + gx) as usize] = if weights == 0 { 0 } else { sum / weights };
217 }
218 }
219 for (c, m) in self.cells.iter_mut().zip(mean) {
220 c[1] = m;
221 c[2] = m;
222 }
223 }
224}