Skip to content

Commit 65f7318

Browse files
Start bucket consolidation refactor
1 parent 347c3a5 commit 65f7318

4 files changed

Lines changed: 230 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
pub(crate) struct ConstReinsertionBucketStore<
2+
const STATE_COUNT: usize,
3+
Reinsertion: super::Reinsertion,
4+
> {
5+
pub reinsertion_time: u32,
6+
7+
states: [State<Reinsertion>; STATE_COUNT],
8+
}
9+
10+
struct State<Reinsertion: super::Reinsertion> {
11+
moving_time_counts: Box<[u32]>,
12+
moving_values: Vec<Reinsertion::MovingValue>,
13+
ticking_values: Vec<Reinsertion::TickingValue>,
14+
}
15+
16+
impl<const STATE_COUNT: usize, Reinsertion: super::Reinsertion>
17+
ConstReinsertionBucketStore<STATE_COUNT, Reinsertion>
18+
{
19+
pub fn new(reinsertion_time: u32) -> Self {
20+
assert_eq!(Reinsertion::STATE_COUNT, STATE_COUNT);
21+
assert!(Reinsertion::STATE_COUNT > 0);
22+
assert!(reinsertion_time > 0);
23+
Self {
24+
reinsertion_time,
25+
states: std::array::from_fn(|_| State {
26+
moving_time_counts: vec![0; reinsertion_time as usize].into_boxed_slice(),
27+
moving_values: vec![],
28+
ticking_values: vec![],
29+
}),
30+
}
31+
}
32+
33+
pub fn update<'a>(&mut self, world_state: &mut Reinsertion::WorldState<'a>) {
34+
for state in &mut self.states {
35+
// Done moving
36+
let (ticking, (moving, moving_time_counts)) = {
37+
(
38+
&mut state.ticking_values,
39+
(&mut state.moving_values, &mut state.moving_time_counts),
40+
)
41+
};
42+
43+
let amount_done_moving = moving_time_counts[0];
44+
let extract_range = 0..(amount_done_moving as usize);
45+
46+
let removed_moving = moving.drain(extract_range);
47+
let added_ticking = removed_moving.map(|moving| Reinsertion::moving_to_ticking(moving));
48+
49+
ticking.extend(added_ticking);
50+
51+
moving_time_counts.rotate_left(1);
52+
assert_eq!(
53+
amount_done_moving,
54+
moving_time_counts[self.reinsertion_time as usize - 1]
55+
);
56+
moving_time_counts[self.reinsertion_time as usize - 1] = 0;
57+
}
58+
59+
for state in 0..STATE_COUNT {
60+
let next_state = (state + 1) % STATE_COUNT;
61+
62+
// Start Moving
63+
let (ticking, (moving, moving_time_counts)) = if state == next_state {
64+
assert!(STATE_COUNT == 1);
65+
let state = &mut self.states[state];
66+
(
67+
&mut state.ticking_values,
68+
(&mut state.moving_values, &mut state.moving_time_counts),
69+
)
70+
} else {
71+
assert!(STATE_COUNT > 1);
72+
let [this, next] = self.states.get_disjoint_mut([state, next_state]).unwrap();
73+
74+
(
75+
&mut this.ticking_values,
76+
(&mut next.moving_values, &mut next.moving_time_counts),
77+
)
78+
};
79+
80+
let extracted_ticking =
81+
ticking.extract_if(.., |ticking| Reinsertion::tick(state, ticking, world_state));
82+
83+
let reinserted_moving =
84+
extracted_ticking.filter_map(|ticking| Reinsertion::ticking_to_moving(ticking));
85+
86+
let moving_old_len = moving.len();
87+
moving.extend(reinserted_moving);
88+
let added = moving.len() - moving_old_len;
89+
90+
moving_time_counts[self.reinsertion_time as usize - 1] += added as u32;
91+
}
92+
}
93+
}

src/bucket_store/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub(crate) mod const_reinsertion;
2+
3+
mod storage_storage_inserter_store;
4+
5+
pub(crate) trait Reinsertion {
6+
const STATE_COUNT: usize;
7+
8+
type MovingValue: Copy;
9+
type TickingValue: Copy;
10+
11+
type WorldState<'a>;
12+
13+
fn tick<'a>(
14+
state: usize,
15+
value: &mut Self::TickingValue,
16+
world_state: &mut Self::WorldState<'a>,
17+
) -> bool;
18+
fn ticking_to_moving(value: Self::TickingValue) -> Option<Self::MovingValue>;
19+
20+
fn moving_to_ticking(value: Self::MovingValue) -> Self::TickingValue;
21+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::num::NonZero;
2+
3+
use enum_map::Enum;
4+
5+
use crate::{bucket_store::Reinsertion, item::ITEMCOUNTTYPE};
6+
7+
struct StorageStorageInserterStore {}
8+
9+
struct Info {
10+
last_ticked: u16,
11+
state_at_last_tick: ImplicitState,
12+
}
13+
14+
enum ImplicitState {
15+
WaitingForSourceItems(ITEMCOUNTTYPE),
16+
WaitingForSpaceInDestination(ITEMCOUNTTYPE),
17+
}
18+
19+
struct StorageStorageInserterReinsertion;
20+
21+
#[derive(Debug, Enum)]
22+
enum State {
23+
PickingUpItems,
24+
DroppingOffItems,
25+
}
26+
27+
#[derive(Debug, Clone, Copy)]
28+
struct MovingInserter {
29+
id: u32,
30+
todo: !,
31+
}
32+
33+
#[derive(Debug, Clone, Copy)]
34+
struct TickingInserter {
35+
id: u32,
36+
hand: ITEMCOUNTTYPE,
37+
todo: !,
38+
}
39+
40+
struct WorldState<'a> {
41+
max_hand_size: ITEMCOUNTTYPE,
42+
43+
current_tick: u32,
44+
infos: &'a mut [Info],
45+
}
46+
47+
impl Reinsertion for StorageStorageInserterReinsertion {
48+
const STATE_COUNT: usize = State::LENGTH;
49+
50+
type MovingValue = MovingInserter;
51+
52+
type TickingValue = TickingInserter;
53+
54+
type WorldState<'a> = WorldState<'a>;
55+
56+
#[inline]
57+
fn tick<'a>(
58+
state: usize,
59+
value: &mut Self::TickingValue,
60+
world_state: &mut Self::WorldState<'a>,
61+
) -> bool {
62+
let WorldState {
63+
current_tick,
64+
infos,
65+
max_hand_size,
66+
} = world_state;
67+
68+
infos[value.id as usize].last_ticked = *current_tick as u16;
69+
70+
match State::from_usize(state) {
71+
State::PickingUpItems => {
72+
let amount_picked_up: ITEMCOUNTTYPE = todo!("Pick up actually");
73+
74+
if amount_picked_up > 0 {
75+
value.hand += amount_picked_up;
76+
infos[value.id as usize].state_at_last_tick =
77+
ImplicitState::WaitingForSourceItems(value.hand);
78+
if value.hand == *max_hand_size {
79+
todo!("Try to put it in waitlist");
80+
true
81+
} else {
82+
false
83+
}
84+
} else {
85+
false
86+
}
87+
},
88+
State::DroppingOffItems => {
89+
let amount_dropped_off: ITEMCOUNTTYPE = todo!("Drop off actually");
90+
91+
if amount_dropped_off > 0 {
92+
value.hand -= amount_dropped_off;
93+
infos[value.id as usize].state_at_last_tick =
94+
ImplicitState::WaitingForSpaceInDestination(value.hand);
95+
if value.hand == 0 {
96+
todo!("Try to put it in waitlist");
97+
true
98+
} else {
99+
false
100+
}
101+
} else {
102+
false
103+
}
104+
},
105+
}
106+
}
107+
108+
fn ticking_to_moving(value: Self::TickingValue) -> Option<Self::MovingValue> {
109+
todo!()
110+
}
111+
112+
fn moving_to_ticking(value: Self::MovingValue) -> Self::TickingValue {
113+
todo!()
114+
}
115+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub mod liquid;
119119

120120
mod par_generation;
121121

122+
mod bucket_store;
122123
mod lockfile;
123124

124125
impl WeakIdxTrait for u8 {}

0 commit comments

Comments
 (0)