From cdd25d9e351c3c19a3154ba22245aa099242259b Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Thu, 29 Jun 2023 18:43:01 -0500 Subject: [PATCH] module breaking added. TUNING NECESSARY --- server/src/entity.rs | 10 ++++++- server/src/timer.rs | 67 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/server/src/entity.rs b/server/src/entity.rs index 3ec683bc9b903f05f8f2e9980faf7c830fee0cbf..14d3915f7d7162cfb3432305b95ca1e0821c01f2 100644 --- a/server/src/entity.rs +++ b/server/src/entity.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, net::SocketAddr, sync::atomic::AtomicU32}; -use log::debug; use nalgebra::Vector2; use protobuf::SpecialFields; use starkingdoms_protocol::planet::PlanetType; @@ -139,6 +138,15 @@ impl EntityHandler { } modules } + pub fn get_all_attached_id(&self) -> Vec<(EntityId, AttachedModule)> { + let mut modules = Vec::new(); + for (id, entity) in self.entities.clone() { + if let Entity::AttachedModule(module) = entity { + modules.push((id, module.clone())); + } + } + modules + } pub fn get_attached_from_id(&self, id: EntityId) -> Option { if let Some(Entity::AttachedModule(module)) = self.entities.get(&id) { return Some(module.clone()); diff --git a/server/src/timer.rs b/server/src/timer.rs index d2758dbab6628bbc3d122f1ad78ec03c138af36e..b77b79864e548f16568e875f6cfc60fdc7478fe9 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -1,16 +1,16 @@ use crate::entity::EntityHandler; -use crate::module::Module; +use crate::module::{Module, AttachedModule}; use crate::orbit::constants::{GAME_ORBITS_ENABLED, MARS_APHELION, MARS_ORBIT_TIME, MARS_PERIHELION, MOON_APOAPSIS, MOON_ORBIT_TIME, MOON_PERIAPSIS}; use crate::orbit::orbit::{calculate_point_on_orbit, calculate_world_position_of_orbit}; use crate::{ entity::{get_entity_id, Entity}, manager::{ClientHandlerMessage, ClientManager, PhysicsData}, - planet::{Planet, Planets}, + planet::Planets, SCALE, }; use async_std::sync::RwLock; use async_std::task::sleep; -use log::{warn, debug}; +use log::warn; use nalgebra::{point, vector}; use protobuf::SpecialFields; use rand::Rng; @@ -95,7 +95,7 @@ pub async fn timer_main( ); // update mars - let mars_id = planets.get_planet_id(PlanetType::Mars).ok_or("moon does not exist")?; + let mars_id = planets.get_planet_id(PlanetType::Mars).ok_or("mars does not exist")?; if let Entity::Planet(mars) = map .get_mut(&mars_id) .ok_or("mars does not exist")? { @@ -226,11 +226,37 @@ pub async fn timer_main( module_body.mass(), ); module_body.apply_impulse(vector![grav_force.0, grav_force.1], true); + + for child in module.children.clone() { + if let Some(child) = child { + let joint = physics_data.impulse_joint_set + .get(child.connection).ok_or("module joint does not exist")?; + if joint.impulses.magnitude() > 0.00006 { + let module: Option; + if let Some(Entity::AttachedModule(p_module)) = + entities.entities.get_mut(&child.child) + { + module = Some(p_module.clone()); + } else { + warn!("attempted to detach nonexistent module"); + continue; + } + let player_id = module.as_ref().ok_or("cannot detach module that doesn't exist")?.player_id; + AttachedModule::detach( + &mut physics_data, + &mut entities, + player_id, + &module.ok_or("cannot detach module that doesn't exist")?, + ); + } + } + } } } { - for (player_id, player) in &entities.read().await.get_players() { + let mut entities = entities.write().await; + for (player_id, player) in &entities.get_players() { let player_handle = player.handle; let player_body = physics_data .rigid_body_set @@ -238,8 +264,7 @@ pub async fn timer_main( .ok_or("player body does not exist")?; player_body.reset_forces(true); player_body.reset_torques(true); - let planets = entities.read().await; - let grav_force = planets.gravity( + let grav_force = entities.gravity( (player_body.translation().x, player_body.translation().y), player_body.mass(), ); @@ -330,6 +355,34 @@ pub async fn timer_main( username, special_fields: SpecialFields::default(), }); + + for child in player.children.clone() { + if let Some(child) = child { + let joint = physics_data.impulse_joint_set + .get(child.connection).ok_or("module joint does not exist")?; + // displays impulse on joint * 10000 so its visible, use to tune breaking + // force + //debug!("impulse: {}", joint.impulses.magnitude() * 10000.); + if joint.impulses.magnitude() > 0.00006 { + let module: Option; + if let Some(Entity::AttachedModule(p_module)) = + entities.entities.get_mut(&child.child) + { + module = Some(p_module.clone()); + } else { + warn!("attempted to detach nonexistent module"); + continue; + } + let player_id = module.as_ref().ok_or("cannot detach module that doesn't exist")?.player_id; + AttachedModule::detach( + &mut physics_data, + &mut entities, + player_id, + &module.ok_or("cannot detach module that doesn't exist")?, + ); + } + } + } } }