orient airships correctly on load

This commit is contained in:
Isse 2023-04-14 11:15:50 +02:00
parent 2208a3037e
commit 3548b464da
9 changed files with 45 additions and 45 deletions

View File

@ -227,6 +227,7 @@ pub enum ServerEvent {
}, },
CreateShip { CreateShip {
pos: Pos, pos: Pos,
ori: Ori,
ship: comp::ship::Body, ship: comp::ship::Body,
rtsim_entity: Option<RtSimVehicle>, rtsim_entity: Option<RtSimVehicle>,
driver: Option<NpcBuilder>, driver: Option<NpcBuilder>,

View File

@ -262,6 +262,7 @@ pub enum VehicleKind {
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
pub struct Vehicle { pub struct Vehicle {
pub wpos: Vec3<f32>, pub wpos: Vec3<f32>,
pub dir: Vec2<f32>,
pub body: comp::ship::Body, pub body: comp::ship::Body,
@ -283,6 +284,7 @@ impl Vehicle {
pub fn new(wpos: Vec3<f32>, body: comp::ship::Body) -> Self { pub fn new(wpos: Vec3<f32>, body: comp::ship::Body) -> Self {
Self { Self {
wpos, wpos,
dir: Vec2::unit_y(),
body, body,
chunk_pos: None, chunk_pos: None,
driver: None, driver: None,

View File

@ -301,7 +301,6 @@ fn goto(wpos: Vec3<f32>, speed_factor: f32, goal_dist: f32) -> impl Action {
ctx.world ctx.world
.sim() .sim()
.get_surface_alt_approx(wpos.xy().as_()) .get_surface_alt_approx(wpos.xy().as_())
.map(|alt| alt)
.unwrap_or(wpos.z), .unwrap_or(wpos.z),
) )
}); });
@ -314,7 +313,6 @@ fn goto(wpos: Vec3<f32>, speed_factor: f32, goal_dist: f32) -> impl Action {
.map(|_| {}) .map(|_| {})
} }
fn goto_flying( fn goto_flying(
wpos: Vec3<f32>, wpos: Vec3<f32>,
speed_factor: f32, speed_factor: f32,
@ -902,21 +900,17 @@ fn pilot(ship: common::comp::ship::Body) -> impl Action {
}) })
.choose(&mut ctx.rng); .choose(&mut ctx.rng);
if let Some((_id, site)) = site { if let Some((_id, site)) = site {
Either::Right(goto_2d_flying( Either::Right(
site.wpos.as_(), goto_2d_flying(
1.0, site.wpos.as_(),
50.0, 1.0,
150.0, 50.0,
110.0, 150.0,
ship.flying_height(), 110.0,
).then(goto_2d_flying( ship.flying_height(),
site.wpos.as_(), )
1.0, .then(goto_2d_flying(site.wpos.as_(), 1.0, 10.0, 32.0, 16.0, 10.0)),
10.0, )
32.0,
16.0,
10.0,
)))
} else { } else {
Either::Left(finish()) Either::Left(finish())
} }

View File

@ -11,6 +11,7 @@ use common::{
use rand::prelude::*; use rand::prelude::*;
use rand_chacha::ChaChaRng; use rand_chacha::ChaChaRng;
use tracing::{error, warn}; use tracing::{error, warn};
use vek::Vec2;
use world::site::SiteKind; use world::site::SiteKind;
pub struct SimulateNpcs; pub struct SimulateNpcs;
@ -167,10 +168,10 @@ fn on_tick(ctx: EventCtx<SimulateNpcs, OnTick>) {
if dist2 > 0.5f32.powi(2) { if dist2 > 0.5f32.powi(2) {
let wpos = vehicle.wpos let wpos = vehicle.wpos
+ (diff + (diff
* (vehicle.get_speed() * speed_factor * ctx.event.dt * (vehicle.get_speed() * speed_factor * ctx.event.dt
/ dist2.sqrt()) / dist2.sqrt())
.min(1.0)); .min(1.0));
let is_valid = match vehicle.body { let is_valid = match vehicle.body {
common::comp::ship::Body::DefaultAirship common::comp::ship::Body::DefaultAirship
@ -189,6 +190,9 @@ fn on_tick(ctx: EventCtx<SimulateNpcs, OnTick>) {
if is_valid { if is_valid {
vehicle.wpos = wpos; vehicle.wpos = wpos;
} }
vehicle.dir = (target.xy() - vehicle.wpos.xy())
.try_normalized()
.unwrap_or(Vec2::unit_y());
} }
}, },
// When riding, other actions are disabled // When riding, other actions are disabled

View File

@ -1572,19 +1572,14 @@ fn handle_spawn_airship(
pos.0.z += 50.0; pos.0.z += 50.0;
const DESTINATION_RADIUS: f32 = 2000.0; const DESTINATION_RADIUS: f32 = 2000.0;
let angle = angle.map(|a| a * std::f32::consts::PI / 180.0); let angle = angle.map(|a| a * std::f32::consts::PI / 180.0);
let destination = angle.map(|a| { let dir = angle.map(|a| Vec3::new(a.cos(), a.sin(), 0.0));
pos.0 let destination = dir.map(|dir| pos.0 + dir * DESTINATION_RADIUS + Vec3::new(0.0, 0.0, 200.0));
+ Vec3::new(
DESTINATION_RADIUS * a.cos(),
DESTINATION_RADIUS * a.sin(),
200.0,
)
});
let mut rng = thread_rng(); let mut rng = thread_rng();
let ship = comp::ship::Body::random_airship_with(&mut rng); let ship = comp::ship::Body::random_airship_with(&mut rng);
let ori = comp::Ori::from(common::util::Dir::new(dir.unwrap_or(Vec3::unit_y())));
let mut builder = server let mut builder = server
.state .state
.create_ship(pos, ship, |ship| ship.make_collider()) .create_ship(pos, ori, ship, |ship| ship.make_collider())
.with(LightEmitter { .with(LightEmitter {
col: Rgb::new(1.0, 0.65, 0.2), col: Rgb::new(1.0, 0.65, 0.2),
strength: 2.0, strength: 2.0,
@ -1621,19 +1616,14 @@ fn handle_spawn_ship(
pos.0.z += 50.0; pos.0.z += 50.0;
const DESTINATION_RADIUS: f32 = 2000.0; const DESTINATION_RADIUS: f32 = 2000.0;
let angle = angle.map(|a| a * std::f32::consts::PI / 180.0); let angle = angle.map(|a| a * std::f32::consts::PI / 180.0);
let destination = angle.map(|a| { let dir = angle.map(|a| Vec3::new(a.cos(), a.sin(), 0.0));
pos.0 let destination = dir.map(|dir| pos.0 + dir * DESTINATION_RADIUS + Vec3::new(0.0, 0.0, 200.0));
+ Vec3::new(
DESTINATION_RADIUS * a.cos(),
DESTINATION_RADIUS * a.sin(),
200.0,
)
});
let mut rng = thread_rng(); let mut rng = thread_rng();
let ship = comp::ship::Body::random_ship_with(&mut rng); let ship = comp::ship::Body::random_ship_with(&mut rng);
let ori = comp::Ori::from(common::util::Dir::new(dir.unwrap_or(Vec3::unit_y())));
let mut builder = server let mut builder = server
.state .state
.create_ship(pos, ship, |ship| ship.make_collider()) .create_ship(pos, ori, ship, |ship| ship.make_collider())
.with(LightEmitter { .with(LightEmitter {
col: Rgb::new(1.0, 0.65, 0.2), col: Rgb::new(1.0, 0.65, 0.2),
strength: 2.0, strength: 2.0,
@ -1683,9 +1673,12 @@ fn handle_make_volume(
}; };
server server
.state .state
.create_ship(comp::Pos(pos.0 + Vec3::unit_z() * 50.0), ship, move |_| { .create_ship(
collider comp::Pos(pos.0 + Vec3::unit_z() * 50.0),
}) comp::Ori::default(),
ship,
move |_| collider,
)
.build(); .build();
server.notify_client( server.notify_client(

View File

@ -194,6 +194,7 @@ pub fn handle_create_npc(server: &mut Server, pos: Pos, mut npc: NpcBuilder) ->
pub fn handle_create_ship( pub fn handle_create_ship(
server: &mut Server, server: &mut Server,
pos: Pos, pos: Pos,
ori: Ori,
ship: comp::ship::Body, ship: comp::ship::Body,
rtsim_vehicle: Option<RtSimVehicle>, rtsim_vehicle: Option<RtSimVehicle>,
driver: Option<NpcBuilder>, driver: Option<NpcBuilder>,
@ -201,7 +202,7 @@ pub fn handle_create_ship(
) { ) {
let mut entity = server let mut entity = server
.state .state
.create_ship(pos, ship, |ship| ship.make_collider()); .create_ship(pos, ori, ship, |ship| ship.make_collider());
/* /*
if let Some(mut agent) = agent { if let Some(mut agent) = agent {
let (kp, ki, kd) = pid_coefficients(&Body::Ship(ship)); let (kp, ki, kd) = pid_coefficients(&Body::Ship(ship));

View File

@ -193,10 +193,11 @@ impl Server {
}, },
ServerEvent::CreateShip { ServerEvent::CreateShip {
pos, pos,
ori,
ship, ship,
rtsim_entity, rtsim_entity,
driver, driver,
} => handle_create_ship(self, pos, ship, rtsim_entity, driver, Vec::new()), } => handle_create_ship(self, pos, ori, ship, rtsim_entity, driver, Vec::new()),
ServerEvent::CreateWaypoint(pos) => handle_create_waypoint(self, pos), ServerEvent::CreateWaypoint(pos) => handle_create_waypoint(self, pos),
ServerEvent::ClientDisconnect(entity, reason) => { ServerEvent::ClientDisconnect(entity, reason) => {
frontend_events.push(handle_client_disconnect(self, entity, reason, false)) frontend_events.push(handle_client_disconnect(self, entity, reason, false))

View File

@ -11,6 +11,7 @@ use common::{
slowjob::SlowJobPool, slowjob::SlowJobPool,
terrain::CoordinateConversions, terrain::CoordinateConversions,
trade::{Good, SiteInformation}, trade::{Good, SiteInformation},
util::Dir,
LoadoutBuilder, LoadoutBuilder,
}; };
use common_ecs::{Job, Origin, Phase, System}; use common_ecs::{Job, Origin, Phase, System};
@ -315,6 +316,7 @@ impl<'a> System<'a> for Sys {
emitter.emit(ServerEvent::CreateShip { emitter.emit(ServerEvent::CreateShip {
pos: comp::Pos(vehicle.wpos), pos: comp::Pos(vehicle.wpos),
ori: comp::Ori::from(Dir::new(vehicle.dir.with_z(0.0))),
ship: vehicle.body, ship: vehicle.body,
rtsim_entity: Some(RtSimVehicle(vehicle_id)), rtsim_entity: Some(RtSimVehicle(vehicle_id)),
driver: vehicle.driver.and_then(&mut actor_info), driver: vehicle.driver.and_then(&mut actor_info),

View File

@ -64,6 +64,7 @@ pub trait StateExt {
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>( fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
&mut self, &mut self,
pos: comp::Pos, pos: comp::Pos,
ori: comp::Ori,
ship: comp::ship::Body, ship: comp::ship::Body,
make_collider: F, make_collider: F,
) -> EcsEntityBuilder; ) -> EcsEntityBuilder;
@ -338,6 +339,7 @@ impl StateExt for State {
fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>( fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
&mut self, &mut self,
pos: comp::Pos, pos: comp::Pos,
ori: comp::Ori,
ship: comp::ship::Body, ship: comp::ship::Body,
make_collider: F, make_collider: F,
) -> EcsEntityBuilder { ) -> EcsEntityBuilder {
@ -347,7 +349,7 @@ impl StateExt for State {
.create_entity_synced() .create_entity_synced()
.with(pos) .with(pos)
.with(comp::Vel(Vec3::zero())) .with(comp::Vel(Vec3::zero()))
.with(comp::Ori::default()) .with(ori)
.with(body.mass()) .with(body.mass())
.with(body.density()) .with(body.density())
.with(make_collider(ship)) .with(make_collider(ship))