Merge branch 'master' into 'master'

Added basic agent AI

See merge request veloren/veloren!36

Former-commit-id: 384f3be090870a308d82d543797e6c84064ccc84
This commit is contained in:
Joshua Barretto 2019-04-17 09:00:30 +00:00
commit 328a11a4af
11 changed files with 143 additions and 25 deletions

1
Cargo.lock generated
View File

@ -1902,6 +1902,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)",
"mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
"shred 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "shred 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -148,21 +148,9 @@ impl Client {
println!("Chunk at {:?}", k); println!("Chunk at {:?}", k);
}); });
// Step 1 self.state.write_component(self.player, comp::Control {
if let (Some(_), Some(vel), Some(_)) = ( move_dir: input.move_dir,
self.state.read_component_cloned::<comp::phys::Pos>(self.player), });
self.state.read_component_cloned::<comp::phys::Vel>(self.player),
self.state.read_component_cloned::<comp::phys::Dir>(self.player),
) {
self.state.write_component(self.player, comp::phys::Vel(vel.0 + input.move_dir * 2.0 - vel.0.map(|e| e * e.abs() + e) * 0.03));
if input.move_dir.magnitude() > 0.01 {
self.state.write_component(self.player, comp::phys::Dir(vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0)));
self.state.write_component(self.player, comp::Animation::Run);
} else {
self.state.write_component(self.player, comp::Animation::Idle);
}
}
// Tick the client's LocalState (step 3) // Tick the client's LocalState (step 3)
self.state.tick(dt); self.state.tick(dt);

View File

@ -18,3 +18,4 @@ serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
bincode = "1.0" bincode = "1.0"
log = "0.4" log = "0.4"
rand = "0.5"

28
common/src/comp/agent.rs Normal file
View File

@ -0,0 +1,28 @@
use specs::{Component, VecStorage};
use vek::*;
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Agent {
Wanderer(Vec2<f32>),
}
impl Component for Agent {
type Storage = VecStorage<Self>;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Control {
pub move_dir: Vec2<f32>,
}
impl Default for Control {
fn default() -> Self {
Self {
move_dir: Vec2::zero(),
}
}
}
impl Component for Control {
type Storage = VecStorage<Self>;
}

View File

@ -1,8 +1,10 @@
pub mod agent;
pub mod character; pub mod character;
pub mod player; pub mod player;
pub mod phys; pub mod phys;
// Reexports // Reexports
pub use agent::{Agent, Control};
pub use character::Character; pub use character::Character;
pub use player::Player; pub use player::Player;
pub use character::Animation; pub use character::Animation;

View File

@ -101,6 +101,8 @@ impl State {
ecs.internal_mut().register::<comp::phys::Vel>(); ecs.internal_mut().register::<comp::phys::Vel>();
ecs.internal_mut().register::<comp::phys::Dir>(); ecs.internal_mut().register::<comp::phys::Dir>();
ecs.internal_mut().register::<comp::Animation>(); ecs.internal_mut().register::<comp::Animation>();
ecs.internal_mut().register::<comp::Agent>();
ecs.internal_mut().register::<comp::Control>();
// Register resources used by the ECS // Register resources used by the ECS
ecs.internal_mut().add_resource(TimeOfDay(0.0)); ecs.internal_mut().add_resource(TimeOfDay(0.0));

34
common/src/sys/agent.rs Normal file
View File

@ -0,0 +1,34 @@
// Library
use specs::{Join, Read, ReadStorage, System, WriteStorage};
use vek::*;
// Crate
use crate::comp::{Agent, Control, phys::Pos};
// Basic ECS AI agent system
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
WriteStorage<'a, Agent>,
ReadStorage<'a, Pos>,
WriteStorage<'a, Control>,
);
fn run(&mut self, (mut agents, pos, mut controls): Self::SystemData) {
for (mut agent, pos, mut control) in (&mut agents, &pos, &mut controls).join() {
match agent {
Agent::Wanderer(bearing) => {
*bearing += Vec2::new(
rand::random::<f32>().fract() - 0.5,
rand::random::<f32>().fract() - 0.5,
) - *bearing * 0.05 - pos.0 * 0.001;
if bearing.magnitude_squared() != 0.0 {
control.move_dir = bearing.normalized();
}
},
}
}
}
}

34
common/src/sys/control.rs Normal file
View File

@ -0,0 +1,34 @@
// Library
use specs::{Join, Read, ReadStorage, System, WriteStorage, Entities};
use vek::*;
// Crate
use crate::comp::{Control, Animation, phys::{Pos, Vel, Dir}};
// Basic ECS AI agent system
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Dir>,
WriteStorage<'a, Animation>,
ReadStorage<'a, Control>,
);
fn run(&mut self, (entities, mut vels, mut dirs, mut anims, controls): Self::SystemData) {
for (entity, mut vel, mut dir, control) in (&entities, &mut vels, &mut dirs, &controls).join() {
// TODO: Don't hard-code this
// Apply physics to the player: acceleration and non-linear decceleration
vel.0 += control.move_dir * 2.0 - vel.0.map(|e| e * e.abs() + e) * 0.03;
if control.move_dir.magnitude() > 0.01 {
dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
anims.insert(entity, Animation::Run);
} else {
anims.insert(entity, Animation::Run);
}
}
}
}

View File

@ -1,11 +1,17 @@
pub mod agent;
pub mod control;
pub mod phys; pub mod phys;
// External // External
use specs::DispatcherBuilder; use specs::DispatcherBuilder;
// System names // System names
const AGENT_SYS: &str = "agent_sys";
const CONTROL_SYS: &str = "control_sys";
const MOVEMENT_SYS: &str = "movement_sys"; const MOVEMENT_SYS: &str = "movement_sys";
pub fn add_local_systems(dispatch_builder: &mut DispatcherBuilder) { pub fn add_local_systems(dispatch_builder: &mut DispatcherBuilder) {
dispatch_builder.add(phys::MovementSys, MOVEMENT_SYS, &[]); dispatch_builder.add(agent::Sys, AGENT_SYS, &[]);
dispatch_builder.add(control::Sys, CONTROL_SYS, &[]);
dispatch_builder.add(phys::Sys, MOVEMENT_SYS, &[]);
} }

View File

@ -8,9 +8,9 @@ use crate::{
}; };
// Basic ECS physics system // Basic ECS physics system
pub struct MovementSys; pub struct Sys;
impl<'a> System<'a> for MovementSys { impl<'a> System<'a> for Sys {
type SystemData = ( type SystemData = (
WriteStorage<'a, Pos>, WriteStorage<'a, Pos>,
ReadStorage<'a, Vel>, ReadStorage<'a, Vel>,

View File

@ -13,7 +13,7 @@ use common::{
comp, comp,
msg::{ClientMsg, ServerMsg}, msg::{ClientMsg, ServerMsg},
net::PostOffice, net::PostOffice,
state::State, state::{State, Uid},
terrain::TerrainChunk, terrain::TerrainChunk,
}; };
use specs::{ use specs::{
@ -52,8 +52,11 @@ impl Server {
pub fn new() -> Result<Self, Error> { pub fn new() -> Result<Self, Error> {
let (chunk_tx, chunk_rx) = mpsc::channel(); let (chunk_tx, chunk_rx) = mpsc::channel();
Ok(Self { let mut state = State::new();
state: State::new(), state.ecs_mut().internal_mut().register::<comp::phys::ForceUpdate>();
let mut this = Self {
state,
world: World::new(), world: World::new(),
postoffice: PostOffice::bind(SocketAddr::from(([0; 4], 59003)))?, postoffice: PostOffice::bind(SocketAddr::from(([0; 4], 59003)))?,
@ -65,7 +68,16 @@ impl Server {
chunk_tx, chunk_tx,
chunk_rx, chunk_rx,
pending_chunks: HashSet::new(), pending_chunks: HashSet::new(),
}) };
for i in 0..4 {
this.create_character(comp::Character::test())
.with(comp::Agent::Wanderer(Vec2::zero()))
.with(comp::Control::default())
.build();
}
Ok(this)
} }
/// Get a reference to the server's game state. /// Get a reference to the server's game state.
@ -90,6 +102,18 @@ impl Server {
&mut self.world &mut self.world
} }
/// Build a non-player character
#[allow(dead_code)]
pub fn create_character(&mut self, character: comp::Character) -> EcsEntityBuilder {
self.state
.ecs_mut()
.create_entity_synced()
.with(comp::phys::Pos(Vec3::zero()))
.with(comp::phys::Vel(Vec3::zero()))
.with(comp::phys::Dir(Vec3::unit_y()))
.with(character)
}
/// Execute a single server tick, handle input and update the game state by the given duration /// Execute a single server tick, handle input and update the game state by the given duration
#[allow(dead_code)] #[allow(dead_code)]
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> { pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
@ -219,8 +243,8 @@ impl Server {
state.write_component(entity, comp::phys::Dir(Vec3::unit_y())); state.write_component(entity, comp::phys::Dir(Vec3::unit_y()));
if let Some(character) = character { if let Some(character) = character {
state.write_component(entity, character); state.write_component(entity, character);
} }
state.write_component(entity, comp::phys::ForceUpdate);
client.state = ClientState::Connected; client.state = ClientState::Connected;
@ -339,8 +363,6 @@ impl Server {
}; };
match force_update { match force_update {
Some(_) => self.clients.notify_connected(msg), Some(_) => self.clients.notify_connected(msg),
None => self.clients.notify_connected_except(entity, msg), None => self.clients.notify_connected_except(entity, msg),
} }