Keep track of circuit/net states.

This commit is contained in:
Adam Gausmann 2022-11-09 21:47:50 -06:00
parent 935523a414
commit c6d7e57c59
4 changed files with 37 additions and 4 deletions

View file

@ -5,4 +5,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dependencies]
bitvec = "1.0"

View file

@ -13,6 +13,10 @@ impl<T> DenseStore<T> {
Self { items: Vec::new() }
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn get(&self, index: Index<T>) -> Option<&T> {
self.items.get(index.into_raw())
}

View file

@ -2,6 +2,8 @@
mod serialize;
use bitvec::vec::BitVec;
use crate::{
latest::ModInfo,
misc::{
@ -20,6 +22,7 @@ pub struct Sandbox {
components: ObjectStore<ComponentInfo>,
wires: ObjectStore<WireInfo>,
nets: DenseStore<NetInfo>,
net_states: BitVec<u8>,
next_type: u16,
component_types: HashMap<String, u16>,
@ -40,6 +43,7 @@ impl Sandbox {
components: ObjectStore::new(),
wires: ObjectStore::new(),
nets: DenseStore::new(),
net_states: BitVec::new(),
next_type: component_types.values().copied().max().unwrap_or(0),
component_types,
@ -270,6 +274,9 @@ impl Sandbox {
}
fn make_net(&mut self) -> NetId {
assert_eq!(self.nets.len(), self.net_states.len());
self.net_states.push(false);
NetId(self.nets.insert(NetInfo {
wires: HashSet::new(),
pegs: HashSet::new(),
@ -287,6 +294,11 @@ impl Sandbox {
for peg_id in &renamed.pegs.clone() {
self.get_peg_mut(peg_id).unwrap().net_id = NetId(rename.dest);
}
// Perform the same swap-remove in net_states:
self.net_states.swap_remove(rename.dest.into_raw());
assert_eq!(self.nets.len(), self.net_states.len());
Some(net)
} else {
None

View file

@ -1,5 +1,7 @@
//! Conversions between the Sandbox editor and Blotter serialization types.
use bitvec::vec::BitVec;
use crate::latest as blotter;
use std::collections::{HashMap, HashSet};
@ -101,6 +103,9 @@ impl From<&super::Sandbox> for blotter::BlotterFile {
components.push(ser.serialize_component(component_id, component))
}
let mut states = sandbox.net_states.clone();
states.set_uninitialized(false);
Self {
game_version: GAME_VERSION,
save_type: blotter::SaveType::World,
@ -120,7 +125,7 @@ impl From<&super::Sandbox> for blotter::BlotterFile {
.map(|(_id, wire)| ser.serialize_wire(wire))
.collect(),
circuit_states: blotter::CircuitStates::WorldFormat {
circuit_states: vec![], //TODO
circuit_states: states.into_vec(),
},
}
}
@ -218,6 +223,19 @@ impl From<&blotter::BlotterFile> for super::Sandbox {
file.mods.clone(),
);
match &file.circuit_states {
blotter::CircuitStates::WorldFormat { circuit_states } => {
sandbox.net_states = BitVec::from_slice(circuit_states);
for _ in 0..8 * circuit_states.len() {
sandbox.nets.insert(super::NetInfo {
wires: HashSet::new(),
pegs: HashSet::new(),
});
}
}
_ => panic!("unsupported circuit state format"),
}
for component in &file.components {
let id = sandbox.insert_component(de.deserialize_component(component));
de.register_component(component.address, id);
@ -230,8 +248,6 @@ impl From<&blotter::BlotterFile> for super::Sandbox {
.unwrap();
}
//TODO load circuit states
sandbox
}
}