From c15fb2b68fd6b8f2d0ae9505c11b659e1f6cad49 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 20 Jan 2022 19:56:27 -0500 Subject: [PATCH] Better mugger AI --- Cargo.lock | 1 + server/Cargo.toml | 2 +- server/src/sys/agent/attack.rs | 16 +++++++--- world/src/civ/mod.rs | 55 +++++++++++++++----------------- world/src/site2/plot/gnarling.rs | 1 + 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4455c5d68..d2c0880804 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6439,6 +6439,7 @@ dependencies = [ "futures-util", "hashbrown 0.11.2", "humantime", + "inline_tweak", "itertools", "lazy_static", "num_cpus", diff --git a/server/Cargo.toml b/server/Cargo.toml index 41a40d8bcb..fd11b20289 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -23,7 +23,7 @@ common-net = { package = "veloren-common-net", path = "../common/net" } world = { package = "veloren-world", path = "../world" } network = { package = "veloren-network", path = "../network", features = ["metrics", "compression", "quic"], default-features = false } -# inline_tweak = "1.0.8" +inline_tweak = "1.0.8" specs = { git = "https://github.com/amethyst/specs.git", features = ["shred-derive"], rev = "f985bec5d456f7b0dd8aae99848f9473c2cd9d46" } specs-idvs = { git = "https://gitlab.com/veloren/specs-idvs.git", rev = "8be2abcddf8f524cb5876e8dd20a7e47cfaf7573" } diff --git a/server/src/sys/agent/attack.rs b/server/src/sys/agent/attack.rs index 498fdc3162..a6474774ea 100644 --- a/server/src/sys/agent/attack.rs +++ b/server/src/sys/agent/attack.rs @@ -71,20 +71,23 @@ impl<'a> AgentData<'a> { .and_then(|t| read_data.orientations.get(t.target)) .map(|ori| ori.look_vec()) .unwrap_or_default(); - let vec_to_target = (tgt_data.pos.0 - self.pos.0).xy(); + let dist = attack_data.dist_sqrd.sqrt(); + let in_front_of_target = target_ori.dot(self.pos.0 - tgt_data.pos.0) > 0.0; if attack_data.dist_sqrd < MAX_PATH_DIST.powi(2) { // If in front of the target, circle to try and get behind, else just make a // beeline for the back of the agent + let vec_to_target = (tgt_data.pos.0 - self.pos.0).xy(); if in_front_of_target { + let theta = (PI / 2. - dist * 0.1).max(0.0); // Checks both CW and CCW rotation let potential_move_dirs = [ vec_to_target - .rotated_z(PI / 2.) + .rotated_z(theta) .try_normalized() .unwrap_or_default(), vec_to_target - .rotated_z(-PI / 2.) + .rotated_z(-theta) .try_normalized() .unwrap_or_default(), ]; @@ -96,7 +99,12 @@ impl<'a> AgentData<'a> { controller.inputs.move_dir = *move_dir; } } else { - controller.inputs.move_dir = vec_to_target.try_normalized().unwrap_or_default(); + // Aim for a point a given distance behind the target to prevent sideways + // movement + let move_target = tgt_data.pos.0.xy() - dist / 2. * target_ori.xy(); + controller.inputs.move_dir = (move_target - self.pos.0) + .try_normalized() + .unwrap_or_default(); } } else { self.path_toward_target(agent, controller, tgt_data, read_data, false, true, None); diff --git a/world/src/civ/mod.rs b/world/src/civ/mod.rs index 3a35ca5147..cd76ef3b56 100644 --- a/world/src/civ/mod.rs +++ b/world/src/civ/mod.rs @@ -197,36 +197,31 @@ impl Civs { let mut rng = ctx.reseed().rng; let site = index.sites.insert(match &sim_site.kind { - //SiteKind::Settlement => { - // WorldSite::settlement(Settlement::generate(wpos, Some(ctx.sim), &mut rng)) - //}, - //SiteKind::Dungeon => WorldSite::dungeon(site2::Site::generate_dungeon( - // &Land::from_sim(ctx.sim), - // &mut rng, - // wpos, - //)), - //SiteKind::Castle => { - // WorldSite::castle(Castle::generate(wpos, Some(ctx.sim), &mut rng)) - //}, - //SiteKind::Refactor => WorldSite::refactor(site2::Site::generate_city( - // &Land::from_sim(ctx.sim), - // &mut rng, - // wpos, - //)), - //SiteKind::Tree => { - // WorldSite::tree(Tree::generate(wpos, &Land::from_sim(ctx.sim), &mut rng)) - //}, - //SiteKind::GiantTree => WorldSite::giant_tree(site2::Site::generate_giant_tree( - // &Land::from_sim(ctx.sim), - // &mut rng, - // wpos, - //)), - //SiteKind::Gnarling => WorldSite::gnarling(site2::Site::generate_gnarling( - // &Land::from_sim(ctx.sim), - // &mut rng, - // wpos, - //)), - _ => WorldSite::gnarling(site2::Site::generate_gnarling( + SiteKind::Settlement => { + WorldSite::settlement(Settlement::generate(wpos, Some(ctx.sim), &mut rng)) + }, + SiteKind::Dungeon => WorldSite::dungeon(site2::Site::generate_dungeon( + &Land::from_sim(ctx.sim), + &mut rng, + wpos, + )), + SiteKind::Castle => { + WorldSite::castle(Castle::generate(wpos, Some(ctx.sim), &mut rng)) + }, + SiteKind::Refactor => WorldSite::refactor(site2::Site::generate_city( + &Land::from_sim(ctx.sim), + &mut rng, + wpos, + )), + SiteKind::Tree => { + WorldSite::tree(Tree::generate(wpos, &Land::from_sim(ctx.sim), &mut rng)) + }, + SiteKind::GiantTree => WorldSite::giant_tree(site2::Site::generate_giant_tree( + &Land::from_sim(ctx.sim), + &mut rng, + wpos, + )), + SiteKind::Gnarling => WorldSite::gnarling(site2::Site::generate_gnarling( &Land::from_sim(ctx.sim), &mut rng, wpos, diff --git a/world/src/site2/plot/gnarling.rs b/world/src/site2/plot/gnarling.rs index 8d1e6622c7..fdfd975bd3 100644 --- a/world/src/site2/plot/gnarling.rs +++ b/world/src/site2/plot/gnarling.rs @@ -1963,6 +1963,7 @@ fn harvester_boss(pos: Vec3, rng: &mut R) -> EntityInfo { .with_asset_expect("common.entity.dungeon.gnarling.harvester", rng) } +#[allow(clippy::type_complexity)] fn rrt( start: Vec3, end: Vec3,