Spaces:
Sleeping
Sleeping
| use crate::vm::BCNVM; | |
| use crate::programs::CREDIT_REQUEST; | |
| use rand::Rng; | |
| pub struct World { | |
| pub agents: Vec<BCNVM>, | |
| pub tick: u64, | |
| } | |
| impl World { | |
| pub fn new(n: usize) -> Self { | |
| let mut agents = Vec::new(); | |
| for i in 0..n { | |
| let id = format!("node-{}", i); | |
| let vm = BCNVM::new(CREDIT_REQUEST.to_vec(), id); | |
| agents.push(vm); | |
| } | |
| Self { agents, tick: 0 } | |
| } | |
| pub fn step(&mut self) { | |
| self.tick += 1; | |
| let mut rng = rand::thread_rng(); | |
| for agent in self.agents.iter_mut() { | |
| let _ = agent.run(); | |
| // Crisis injection | |
| if rng.gen_bool(0.01) { | |
| agent.state.trust -= 20; | |
| agent.state.default_risk += 0.2; | |
| } | |
| // Productivity growth | |
| agent.state.productivity += rng.gen_range(0..3); | |
| // Flow redistribution | |
| agent.state.flow_total += rng.gen_range(0..50); | |
| } | |
| // Peer mesh gossip | |
| for i in 0..self.agents.len() { | |
| let snapshot = self.agents[i] | |
| .mesh | |
| .local_snapshot(&self.agents[i].state); | |
| for j in 0..self.agents.len() { | |
| if i != j { | |
| self.agents[j].mesh.ingest_peer(snapshot.clone()); | |
| } | |
| } | |
| } | |
| for agent in self.agents.iter_mut() { | |
| agent.mesh.reconcile(&mut agent.state); | |
| } | |
| } | |
| } |