Hello i want to iterate over an 2d ndarray filled with one vector for each field.
But i am having trouble making a useful iteration because of the nested vectors.
here is how i declared the ndarray:
pub chunks_prey:Array2::<Vec<Entity>>,
pub chunks_hunt:Array2::<Vec<Entity>>,
(there are hunter entities and prey entities, hunter follow prey. and prey runs away. similiar to boids)
And here is how i am trying to iterate over it
pub fn update_entities_chunked(&mut self){
//random spawn location for all enties that will die this frame
let spawn_x = rand::thread_rng().gen_range(0..self.w-1) as f32;
let spawn_y = rand::thread_rng().gen_range(0..self.h-1) as f32;
//for each entity in the chunk
/* HOW TO ITEREATE CORRECTLY HERE??*/
self.chunks_prey.par_iter().for_each(|v|{ //in the nd array is a Vec<Entity>
for e in v{ //i want to edit the entities
//check against own chunk
let base_chunk_x = e.pos_x as usize/self.chunk_size;
let base_chunk_y = e.pos_y as usize/self.chunk_size;
// get the closest other hunter entity. we want to run away from the closest one
let others = &self.chunks_hunt[[base_chunk_x, base_chunk_y]];
let other = e.closest(others, self.w, self.h);
//TODO check other possible chunks //not important in this forum post
//react to the closest other
let dis = other.2; //other is a tuple containing the position and distance (other_x, other_y, distance)
//if a hunter is too close we got eaten
if dis < 1.0 {
//respawn at a random entity
e.set_pos(spawn_x, spawn_y); //error here
} else if dis < e.view { //we see the hunter and run away
e.move_away(other.0, other.1);
}
//random movement
let step = 4.5;
let rand_x = rand::thread_rng().gen_range(-step..step);
let rand_y = rand::thread_rng().gen_range(-step..step);
e.move_by(rand_x, rand_y);
//make sure the entity doesn't walk offscreen
e.set_pos(warp(e.pos_x, self.w), warp(e.pos_y, self.h));
}
});
}
I feel like i am doing this completely wrong. I want to edit entity e by moving it around etc. so e should be a mut. however i don;t know how to tell this the compiler. Do i have to par_iter_mut the nd array too?
Here is the error i am getting.
error[E0596]: cannot borrow `*e` as mutable, as it is behind a `&` reference
--> src/world.rs:221:11
|
205 | for e in v{
| - this iterator yields `&` references
...
221 | e.set_pos(spawn_x, spawn_y);
| ^ `e` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*e` as mutable, as it is behind a `&` reference
--> src/world.rs:223:11
|
205 | for e in v{
| - this iterator yields `&` references
...
223 | e.move_away(other.0, other.1);
| ^ `e` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*e` as mutable, as it is behind a `&` reference
--> src/world.rs:229:11
|
205 | for e in v{
| - this iterator yields `&` references
...
229 | e.move_by(rand_x, rand_y);
| ^ `e` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*e` as mutable, as it is behind a `&` reference
--> src/world.rs:237:9
|
205 | for e in v{
| - this iterator yields `&` references
...
237 | e.move_by(rand_x, rand_y);
| ^ `e` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*e` as mutable, as it is behind a `&` reference
--> src/world.rs:239:9
|
205 | for e in v{
| - this iterator yields `&` references
...
239 | e.set_pos(warp(e.pos_x, self.w), warp(e.pos_y, self.h));
| ^ `e` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error: aborting due to 5 previous errors; 4 warnings emitted
For more information about this error, try `rustc --explain E0596`.
error: could not compile `pixels`
To learn more, run the command again with --verbose.