mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Parallelised rtsim NPC AI
This commit is contained in:
parent
077da13a5f
commit
c4032ee024
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -6952,6 +6952,7 @@ dependencies = [
|
|||||||
"hashbrown 0.12.3",
|
"hashbrown 0.12.3",
|
||||||
"itertools",
|
"itertools",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
|
"rayon",
|
||||||
"rmp-serde",
|
"rmp-serde",
|
||||||
"ron 0.8.0",
|
"ron 0.8.0",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -19,3 +19,4 @@ slotmap = { version = "1.0.6", features = ["serde"] }
|
|||||||
rand = { version = "0.8", features = ["small_rng"] }
|
rand = { version = "0.8", features = ["small_rng"] }
|
||||||
fxhash = "0.2.1"
|
fxhash = "0.2.1"
|
||||||
itertools = "0.10.3"
|
itertools = "0.10.3"
|
||||||
|
rayon = "1.5"
|
||||||
|
@ -89,7 +89,7 @@ impl Data {
|
|||||||
.with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0))
|
.with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0))
|
||||||
};
|
};
|
||||||
if good_or_evil {
|
if good_or_evil {
|
||||||
for _ in 0..32 {
|
for _ in 0..250 {
|
||||||
this.npcs.create(
|
this.npcs.create(
|
||||||
Npc::new(rng.gen(), rand_wpos(&mut rng))
|
Npc::new(rng.gen(), rand_wpos(&mut rng))
|
||||||
.with_faction(site.faction)
|
.with_faction(site.faction)
|
||||||
@ -107,7 +107,7 @@ impl Data {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _ in 0..5 {
|
for _ in 0..15 {
|
||||||
this.npcs.create(
|
this.npcs.create(
|
||||||
Npc::new(rng.gen(), rand_wpos(&mut rng))
|
Npc::new(rng.gen(), rand_wpos(&mut rng))
|
||||||
.with_faction(site.faction)
|
.with_faction(site.faction)
|
||||||
|
@ -21,6 +21,7 @@ use common::{
|
|||||||
use fxhash::FxHasher64;
|
use fxhash::FxHasher64;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
@ -230,21 +231,27 @@ const MAX_STEP: f32 = 32.0;
|
|||||||
impl Rule for NpcAi {
|
impl Rule for NpcAi {
|
||||||
fn start(rtstate: &mut RtState) -> Result<Self, RuleError> {
|
fn start(rtstate: &mut RtState) -> Result<Self, RuleError> {
|
||||||
rtstate.bind::<Self, OnTick>(|mut ctx| {
|
rtstate.bind::<Self, OnTick>(|mut ctx| {
|
||||||
let npc_ids = ctx.state.data().npcs.keys().collect::<Vec<_>>();
|
let mut npc_data = {
|
||||||
|
let mut data = ctx.state.data_mut();
|
||||||
for npc_id in npc_ids {
|
data.npcs
|
||||||
let mut brain = ctx.state.data_mut().npcs[npc_id]
|
.iter_mut()
|
||||||
.brain
|
.map(|(npc_id, npc)| {
|
||||||
.take()
|
let controller = Controller { goto: npc.goto };
|
||||||
.unwrap_or_else(|| Brain {
|
let brain = npc.brain.take().unwrap_or_else(|| Brain {
|
||||||
action: Box::new(think().repeat()),
|
action: Box::new(think().repeat()),
|
||||||
});
|
});
|
||||||
|
(npc_id, controller, brain)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
|
||||||
let controller = {
|
{
|
||||||
let data = &*ctx.state.data();
|
let data = &*ctx.state.data();
|
||||||
let npc = &data.npcs[npc_id];
|
|
||||||
|
|
||||||
let mut controller = Controller { goto: npc.goto };
|
npc_data
|
||||||
|
.par_iter_mut()
|
||||||
|
.for_each(|(npc_id, controller, brain)| {
|
||||||
|
let npc = &data.npcs[*npc_id];
|
||||||
|
|
||||||
brain.action.tick(&mut NpcCtx {
|
brain.action.tick(&mut NpcCtx {
|
||||||
state: ctx.state,
|
state: ctx.state,
|
||||||
@ -253,9 +260,17 @@ impl Rule for NpcAi {
|
|||||||
time_of_day: ctx.event.time_of_day,
|
time_of_day: ctx.event.time_of_day,
|
||||||
time: ctx.event.time,
|
time: ctx.event.time,
|
||||||
npc,
|
npc,
|
||||||
npc_id,
|
npc_id: *npc_id,
|
||||||
controller: &mut controller,
|
controller,
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut data = ctx.state.data_mut();
|
||||||
|
for (npc_id, controller, brain) in npc_data {
|
||||||
|
data.npcs[npc_id].goto = controller.goto;
|
||||||
|
data.npcs[npc_id].brain = Some(brain);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let action: ControlFlow<()> = try {
|
let action: ControlFlow<()> = try {
|
||||||
@ -301,13 +316,6 @@ impl Rule for NpcAi {
|
|||||||
*/
|
*/
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
controller
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.state.data_mut().npcs[npc_id].goto = controller.goto;
|
|
||||||
ctx.state.data_mut().npcs[npc_id].brain = Some(brain);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Self)
|
Ok(Self)
|
||||||
|
Loading…
Reference in New Issue
Block a user