1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::fmt::{self, Debug, Display, Formatter};
use std::marker::PhantomData;
use std::usize;
use uuid::Uuid;
use container::EntityContainer;
use cell::{Cell, CellState};
use entity::{Entity, EntityState, MutEntityState};
use generator::Generator;
use action::{CellAction, SelfAction, EntityAction};
#[derive(Clone)]
pub struct UniverseConf {
pub view_distance: usize,
pub size: usize,
pub iter_cells: bool,
}
impl Default for UniverseConf {
fn default() -> UniverseConf {
UniverseConf {
view_distance: 1,
size: 8000,
iter_cells: false,
}
}
}
pub struct Universe<C: CellState, E: EntityState<C>, M: MutEntityState, CA: CellAction<C>, EA: EntityAction<C, E>> {
pub conf: UniverseConf,
pub cell_mutator: fn(usize, &[Cell<C>]) -> Option<C>,
pub entity_driver: fn(
universe_index: usize,
entity: &Entity<C, E, M>,
entities: &EntityContainer<C, E, M>,
cells: &[Cell<C>],
cell_action_executor: &mut FnMut(CA, usize),
self_action_executor: &mut FnMut(SelfAction<C, E, EA>),
entity_action_executor: &mut FnMut(EA, usize, Uuid)
),
pub seq: usize,
pub cells: Vec<Cell<C>>,
pub entities: EntityContainer<C, E, M>,
pub average_actions_per_cycle: usize,
pub total_actions: usize,
pub average_unique_entities_modified_per_cycle: usize,
pub total_entity_modifications: usize,
__phantom_ca: PhantomData<CA>,
__phantom_ea: PhantomData<EA>,
}
impl<C: CellState, E: EntityState<C>, M: MutEntityState, CA: CellAction<C>, EA: EntityAction<C, E>> Universe<C, E, M, CA, EA> {
pub fn new(
conf: UniverseConf,
gen: &mut Generator<C, E, M, CA, EA>,
cell_mutator: fn(usize, &[Cell<C>]) -> Option<C>,
entity_driver: fn(
universe_index: usize,
entity: &Entity<C, E, M>,
entities: &EntityContainer<C, E, M>,
cells: &[Cell<C>],
cell_action_executor: &mut FnMut(CA, usize),
self_action_executor: &mut FnMut(SelfAction<C, E, EA>),
entity_action_executor: &mut FnMut(EA, usize, Uuid)
)
) -> Universe<C, E, M, CA, EA> {
assert!(conf.size > 0);
let universe_size = conf.size;
let mut universe = Universe {
conf: conf,
cell_mutator: cell_mutator,
entity_driver: entity_driver,
seq: 0,
cells: Vec::new(),
entities: EntityContainer::new(universe_size),
average_actions_per_cycle: 0,
total_actions: 0,
average_unique_entities_modified_per_cycle: 0,
total_entity_modifications: 0,
__phantom_ca: PhantomData,
__phantom_ea: PhantomData,
};
let (cells, entities) = gen.gen(&universe.conf);
universe.cells = cells;
for (universe_index, entity_vec) in entities.into_iter().enumerate() {
for entity in entity_vec {
universe.entities.insert(entity, universe_index);
}
}
universe
}
pub fn uninitialized(universe_size: usize) -> Self {
let dummy_driver = |
_: usize,
_: &Entity<C, E, M>,
_: &EntityContainer<C, E, M>,
_: &[Cell<C>],
_: &mut FnMut(CA, usize),
_: &mut FnMut(SelfAction<C, E, EA>),
_: &mut FnMut(EA, usize, Uuid)
| {};
Universe {
conf: UniverseConf::default(),
cell_mutator: |_: usize, _: &[Cell<C>]| -> Option<C> { None },
entity_driver: dummy_driver,
seq: 0,
cells: Vec::new(),
entities: EntityContainer::new(universe_size),
average_actions_per_cycle: 0,
total_actions: 0,
average_unique_entities_modified_per_cycle: 0,
total_entity_modifications: 0,
__phantom_ca: PhantomData,
__phantom_ea: PhantomData,
}
}
}
impl<
C: CellState, E: EntityState<C>, M: MutEntityState, CA: CellAction<C>, EA: EntityAction<C, E>
> Debug for Universe<C, E, M, CA, EA> where C:Display, E:Display {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
let length = self.conf.size * self.conf.size;
let mut buf = String::with_capacity(length);
buf.push_str(&format!("{}", self.seq));
for i in 0..length {
if i % self.conf.size == 0 {
buf.push('\n');
}
let entities = self.entities.get_entities_at(i);
if entities.len() > 0 {
let entity_index = entities[0];
let entity_state = unsafe { &self.entities.get(entity_index).state };
buf.push_str(&format!("{}", entity_state));
} else {
buf.push_str(&format!("{}", self.cells[i].state));
}
}
buf.push('\n');
write!(formatter, "{}", buf)
}
}