Skip to content

Commit e1381c5

Browse files
committed
experimental graph data structure wrapping components
1 parent 5a957b2 commit e1381c5

File tree

11 files changed

+239
-299
lines changed

11 files changed

+239
-299
lines changed

.idea/workspace.xml

Lines changed: 149 additions & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bin/gen_typescript.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use population_simulation::pop::components::RegionPop;
33

44
fn main() {
55
println!("hi");
6-
let x = RegionPop::new(&[1./17.;17], 200);
6+
//let x = RegionPop::new(&[1./17.;17], 200);
77

88
dbg!(RegionPop::type_script_ify());
99

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use crate::{
7777
core_loop::game_loop,
7878
},
7979
};
80+
use crate::misc::graph_ds::test_graph_ds;
8081

8182
//lazy_static! {
8283
// pub static ref SETTINGS : RwLock<config::Config> = RwLock::new(config::Config::default());
@@ -90,7 +91,7 @@ pub fn setup() -> Result<(), Error> {
9091

9192
let mut world = setup_world()?;
9293

93-
let foo = Some("hi");
94+
test_graph_ds(&mut world);
9495
// blocks until connection established to a client
9596
let WsReturn { server_thread, out, sub_req_recv} = wait_client()?;
9697
trace!("after waiting for connect");

src/misc/graph_ds.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::prelude::*;
2+
use specs::join::JoinIter;
3+
use serde_json::value::Value::Array;
4+
5+
pub trait AdjList {
6+
fn get_nbs<'a>(&self, e: Entity) -> ArrayVec<[usize; 256]>;
7+
//fn nbs_ent_u(&self, id: usize, map: &[Entity]) -> Vec<Entity>;
8+
//fn nbs_ent(&self, e: &Entity) -> ArrayVec<[Entity; 256]>;
9+
}
10+
11+
pub struct Graph<'a, A: AdjList> {
12+
adj: A,
13+
id_map: Read<'a, VecMap<Entity>>,
14+
ents: Entities<'a>,
15+
}
16+
17+
impl<'a, A: AdjList> Graph<'a, A> {
18+
pub fn new(adj: A, id_map: Read<'a, VecMap<Entity>>, ents: Entities<'a>) -> Self {
19+
Graph { adj, id_map, ents }
20+
}
21+
22+
pub fn nbs_iter<T: Join>(&mut self, res: &mut JoinIter<T>, id: usize) -> impl Iterator<Item=<T as Join>::Type> {
23+
dbg!(id);
24+
let e: Entity = self.id_map[id];
25+
26+
let nbs = self.adj.get_nbs(e);
27+
28+
let v: Vec<_> = nbs.iter()
29+
.map(|&id| self.id_map.get(id).expect("boom2"))
30+
.filter_map(|&e| res.get(e, &self.ents)).collect();
31+
v.into_iter()
32+
}
33+
}
34+
35+
impl<'a> AdjList for ReadStorage<'a, RegionAdjacency> {
36+
fn get_nbs(&self, e: Entity) -> ArrayVec<[usize; 256]> {
37+
let ra = self.get(e).expect("boom");
38+
ArrayVec::from_iter(ra.nbs.iter().map(|&i| i))
39+
}
40+
}
41+
42+
pub fn test_graph_ds(world: &mut World) {
43+
println!("in test graph ds");
44+
let (r, rp, rfd): (ReadStorage<Region>, ReadStorage<RegionPop>, ReadStorage<RegBaseFarmData>) = world.system_data();
45+
let adj: ReadStorage<RegionAdjacency> = world.system_data();
46+
let (r2e, ents): (Read<Region2Entity>, Entities) = world.system_data();
47+
48+
let mut graph = Graph::new(adj, r2e, ents);
49+
let mut res = (&rp, &rfd).join();
50+
51+
52+
for Region { id, .. } in r.join() {
53+
dbg!(id);
54+
graph.nbs_iter(&mut res, *id).for_each(|(rp, rfd)| { dbg!(rfd); });
55+
}
56+
}

src/misc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod core_loop;
33
pub mod normalize;
44
pub mod time;
55
pub mod systems;
6+
pub mod graph_ds;
67

78
use crate::prelude::*;
89
pub use self::{

src/terrain/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use vec_map::VecMap;
1010

1111
// resource for going the other way
1212
pub type Tile2Entity = Vec<Entity>;
13-
pub type Region2Entity = Vec<Entity>;
13+
pub type Region2Entity = VecMap<Entity>;
1414

1515
#[derive(Component, Copy, Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Ord, Eq)]
1616
pub struct TileID {

src/worldbuilding/agr_init.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ pub fn init_farm_data((base, reg, topo, pop, mut farm, entities): (ReadStorage<R
146146
let cleared = (needed_area * rng.gen_range(1.01, 1.5)).min(arable);
147147
let auc = needed_area.min(arable);
148148

149-
dbg!(needed_grain / high_yield);
150-
dbg!(needed_grain);
151-
dbg!(high_yield);
152-
dbg!(pop.pop());
153-
dbg!(arable);
154-
dbg!(topo.area);
155-
dbg!(fertility);
156-
println!("");
149+
// dbg!(needed_grain / high_yield);
150+
// dbg!(needed_grain);
151+
// dbg!(high_yield);
152+
// dbg!(pop.pop());
153+
// dbg!(arable);
154+
// dbg!(topo.area);
155+
// dbg!(fertility);
156+
// println!("");
157157
farm.insert(e, FarmData {auc, cleared}).unwrap();
158158
}
159159

160-
dbg!(base_yield(0, 1., 1.));
161-
162-
dbg!(count);
160+
// dbg!(base_yield(0, 1., 1.));
161+
//
162+
// dbg!(count);
163163
}
164164

165165

src/worldbuilding/pop_init.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn register_pop_ecs(world: &mut World) {
2727
let total_est = region.tiles.iter().filter_map(|id| est.get(t2e[id.id]))
2828
.fold(0, |a, est| a + est.0);
2929

30-
dbg!(total_est);
30+
//dbg!(total_est);
3131
let rp = RegionPop::new(&dist, total_est);
3232
//dbg!(&rp);
3333
updater.insert(e, rp);
@@ -49,9 +49,7 @@ pub fn init_pop_est(world: &mut World) {
4949
let keys: Vec<_> = v.keys().collect();
5050

5151
let dist = WeightedIndex::new(goodness.iter()).unwrap();
52-
5352
let mut rng = SmallRng::from_entropy();
54-
5553
let mut arr = ArrayVec::<[usize; 30]>::new();
5654
for i in 0..10 {
5755
arr.push(dist.sample(&mut rng));
@@ -62,8 +60,8 @@ pub fn init_pop_est(world: &mut World) {
6260
let g = goodness[i];
6361
WeightedNode { weight: ((g as f32).sqrt() as i32) * 10, inner: &v[keys[i]] }
6462
}));
65-
let mut seen = VecMap::with_capacity(goodness.len());
6663

64+
let mut seen = VecMap::with_capacity(goodness.len());
6765
let mut pop_est = VecMap::with_capacity(v.len());
6866
while let Some(WeightedNode { weight, inner: (id, t, b, a) }) = heap.pop() {
6967
let coef = ((t.hillratio / 2. + b.fertility) as f32 / 2.).sqrt();
@@ -72,7 +70,7 @@ pub fn init_pop_est(world: &mut World) {
7270

7371
seen.insert(id.id, ());
7472
for &n in a.nbs.iter().filter(|&&n| !seen.contains_key(n)) {
75-
let e = t2e.get(id.id).unwrap();
73+
//let e = t2e.get(id.id).unwrap();
7674

7775
if let Some(x) = v.get(n) {
7876
heap.push(WeightedNode {
@@ -84,7 +82,7 @@ pub fn init_pop_est(world: &mut World) {
8482
}
8583

8684

87-
dbg!(&pop_est);
85+
//dbg!(&pop_est);
8886
for (k, p) in pop_est {
8987
pop.insert(t2e[k], PopEst(p as usize)).unwrap();
9088
}

src/worldbuilding/terrain_init.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn register_terrain_ecs(mesh: &Mesh, world: &mut World) {
7979
}
8080
world.maintain();
8181
world.add_resource(tile2entity);
82+
world.add_resource(Region2Entity::default());
8283
}
8384

8485
pub const RIVER_FLUX_THRESH: f32 = 0.006;
@@ -129,6 +130,7 @@ type ConsRegionData<'a> = (ReadStorage<'a, TileTopography>,
129130
WriteStorage<'a, Region>,
130131
WriteStorage<'a, RegionID>,
131132
Read<'a, Tile2Entity>,
133+
Write<'a, Region2Entity>,
132134
Entities<'a>,
133135
Write<'a, LazyUpdate>);
134136

@@ -141,7 +143,7 @@ pub const MAX_TILES_PER_REGION: u8 = 12;
141143
pub const REGION_BASE_FOOD_MAX: f32 = 30.;// too high, bring down eventually
142144

143145
// TODO refactor and simplify
144-
fn construct_regions_inner((tile_topo, tile_id, tile_adj, farm, region, region_id, t2e, entities, updater): ConsRegionData) -> Option<()> {
146+
fn construct_regions_inner((tile_topo, tile_id, tile_adj, farm, region, region_id, t2e, mut r2e, entities, updater): ConsRegionData) -> Option<()> {
145147
// N: num tiles
146148
// R: num regions
147149
// A: tiles per region; N / R
@@ -265,7 +267,7 @@ fn construct_regions_inner((tile_topo, tile_id, tile_adj, farm, region, region_i
265267
region_pool.swap_remove(i);
266268
} else {
267269
error!("Should have removed max");
268-
dbg!( &region_pool);
270+
dbg!(&region_pool);
269271
dbg!(max);
270272
}
271273

@@ -290,19 +292,24 @@ fn construct_regions_inner((tile_topo, tile_id, tile_adj, farm, region, region_i
290292
}
291293
}
292294

295+
293296
debug!("num loops: {}", loops);
294297
debug!("num_regions: {}", region_map.len());
295298

299+
296300
for (i, r) in region_map.drain() {
301+
let id = r.id;
297302
let b = updater.create_entity(&entities)
298303
.with(r)
299304
.with(reg_topo.remove(i).unwrap())
300305
.with(reg_adj.remove(i).unwrap());
301-
if let Some(farm) = reg_agr.remove(i) {
302-
b.with(farm).build();
306+
let e = if let Some(farm) = reg_agr.remove(i) {
307+
b.with(farm).build()
303308
} else {
304-
b.build();
305-
}
309+
b.build()
310+
};
311+
312+
r2e.insert(id, e);
306313
}
307314

308315
Some(())

vue-client/src/components/panels/DataSubPicker.vue.js

Lines changed: 0 additions & 137 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)