summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client/tractorbeam.rs8
-rw-r--r--src/constants.rs3
-rw-r--r--src/mass.rs15
-rw-r--r--src/math.rs8
-rw-r--r--src/modules/tractorbeam.rs104
5 files changed, 91 insertions, 47 deletions
diff --git a/src/client/tractorbeam.rs b/src/client/tractorbeam.rs
index 74d912f..c8a6d92 100644
--- a/src/client/tractorbeam.rs
+++ b/src/client/tractorbeam.rs
@@ -27,25 +27,25 @@ pub fn client_tractorbeam(mut stream: TcpStream, mut buff_r: BufReader<TcpStream
match data.status {
tractorbeam::Status::None => write!(
stdout,
- "{}Press o to pull, p to push, t to bring to 5m.",
+ "{}Press o to pull, p to push, b to bring to 5m.",
clear
)
.unwrap(),
tractorbeam::Status::Push => write!(
stdout,
- "{}Press o to pull, p to stop pushing, t to bring to 5m.",
+ "{}Press o to pull, p to stop pushing, b to bring to 5m.",
clear
)
.unwrap(),
tractorbeam::Status::Pull => write!(
stdout,
- "{}Press o to stop pulling, p to push, t to bring to 5m.",
+ "{}Press o to stop pulling, p to push, b to bring to 5m.",
clear
)
.unwrap(),
tractorbeam::Status::Bring => write!(
stdout,
- "{}Press o to pulling, p to push, t to stop bringing to 5m.",
+ "{}Press o to pulling, p to push, b to stop bringing to 5m.",
clear
)
.unwrap(),
diff --git a/src/constants.rs b/src/constants.rs
index a2373ff..4c48cac 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -14,6 +14,8 @@ pub const SHIP_NAVIGATION_TIME: u64 = 3;
pub const SHIP_NAVIGATION_RANGE: f64 = 100.0;
pub const SHIP_REFINERY_TIME: u64 = 5;
pub const SHIP_TRACTORBEAM_STRENGTH: f64 = 0.1;
+pub const SHIP_TRACTORBEAM_RANGE: f64 = 50.0;
+pub const SHIP_TRACTORBEAM_BRING_TO_DISTANCE: f64 = 5.0;
pub const SHIP_ENGINES_FUEL_START: f64 = 100.0;
pub const SHIP_ENGINES_ACCELERATION: f64 = 0.1;
@@ -22,3 +24,4 @@ pub const HYDROGEN_SIZE: usize = 1;
pub const CRUDE_MINERALS_SIZE: usize = 10;
pub const SLEEP_DURATION: u64 = 100;
+pub const FLOAT_PRECISION: f64 = 0.001;
diff --git a/src/mass.rs b/src/mass.rs
index 3d90573..a307295 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -98,16 +98,16 @@ impl Mass {
Mass {
mass_type: astroid,
- position: Vector::new((
+ position: Vector::new(
rng.sample(p_range),
rng.sample(p_range),
rng.sample(p_range),
- )),
- velocity: Vector::new((
+ ),
+ velocity: Vector::new(
rng.sample(v_range),
rng.sample(v_range),
rng.sample(v_range),
- )),
+ ),
effects: Effects::new(),
}
}
@@ -179,10 +179,7 @@ impl Mass {
if let Some(target_name) = &navigation.target_name {
let mut target = masses.remove(target_name).unwrap();
mining.process(self.position.clone(), masses, &mut target, storage);
- tractorbeam.process();
- let acceleration =
- tractorbeam.get_acceleration(self.position.clone(), target.position.clone());
- target.effects.give_acceleration(acceleration);
+ tractorbeam.process(self.position.clone(), &mut target);
masses.insert(target_name.to_string(), target);
}
@@ -193,13 +190,13 @@ impl Mass {
engines.process(self.position.clone(), self.velocity.clone(), target);
refinery.process(storage);
- navigation.process(self.position.clone(), masses);
construction.process(
self.velocity.clone(),
self.position.clone(),
masses,
storage,
);
+ navigation.process(self.position.clone(), masses);
self.effects.give_acceleration(engines.take_acceleration());
}
diff --git a/src/math.rs b/src/math.rs
index 9a9f7b8..e266a88 100644
--- a/src/math.rs
+++ b/src/math.rs
@@ -19,12 +19,8 @@ pub struct Vector {
}
impl Vector {
- pub fn new(v: (f64, f64, f64)) -> Vector {
- Vector {
- x: v.0,
- y: v.1,
- z: v.2,
- }
+ pub fn new(x: f64, y: f64, z: f64) -> Vector {
+ Vector { x, y, z }
}
pub fn distance_from(&self, other: Vector) -> f64 {
diff --git a/src/modules/tractorbeam.rs b/src/modules/tractorbeam.rs
index 4f91292..28aa181 100644
--- a/src/modules/tractorbeam.rs
+++ b/src/modules/tractorbeam.rs
@@ -4,21 +4,89 @@ use crate::math::Vector;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Tractorbeam {
+ pub range: f64,
pub status: Status,
strength: f64,
desired_distance: Option<f64>,
+ control_system: ControlSystem,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
+struct ControlSystem {
+ previous_error: f64,
+ integral: f64,
+ kp: f64,
+ ki: f64,
+ kd: f64,
+ dt: f64,
+}
+
+impl ControlSystem {
+ pub fn new() -> ControlSystem {
+ ControlSystem {
+ previous_error: 0.0,
+ integral: 0.0,
+ kp: 1.0,
+ ki: 0.01,
+ kd: 0.001,
+ dt: 0.0001,
+ }
+ }
+
+ pub fn compute(&mut self, strength: f64, distance: f64, desired_distance: f64) -> f64 {
+ let error = desired_distance - distance;
+ self.integral += error * self.dt;
+ let derivative = (error - self.previous_error) / self.dt;
+ let output = self.kp * error + self.ki * self.integral + self.kd * derivative;
+ self.previous_error = error;
+
+ if output.abs() > strength {
+ if output.is_sign_positive() {
+ strength
+ } else {
+ strength * -1.0
+ }
+ } else {
+ output
+ }
+ }
}
impl Tractorbeam {
pub fn new() -> Tractorbeam {
Tractorbeam {
+ range: constants::SHIP_TRACTORBEAM_RANGE,
status: Status::None,
strength: constants::SHIP_TRACTORBEAM_STRENGTH,
desired_distance: None,
+ control_system: ControlSystem::new(),
}
}
- pub fn process(&mut self) {}
+ pub fn process(&mut self, ship_position: Vector, target: &mut Mass) {
+ let distance = ship_position.distance_from(target.position.clone());
+ if self.range < distance {
+ self.off()
+ } else {
+ let direction = target.position.clone() - ship_position.clone();
+ let acceleration = match self.status {
+ Status::Push => direction.unitize() * self.strength,
+ Status::Pull => direction.unitize() * -1.0 * self.strength,
+ Status::Bring => match self.desired_distance {
+ Some(desired_distance) => {
+ direction.unitize()
+ * self
+ .control_system
+ .compute(self.strength, distance, desired_distance)
+ }
+ None => Vector::default(),
+ },
+ Status::None => Vector::default(),
+ };
+
+ target.effects.give_acceleration(acceleration);
+ }
+ }
pub fn get_client_data(&self, target: Option<&Mass>) -> String {
let client_data = ClientData {
@@ -33,36 +101,11 @@ impl Tractorbeam {
match recv.as_str() {
"o" => self.toggle_pull(),
"p" => self.toggle_push(),
- "b" => self.toggle_bring(5.0),
+ "b" => self.toggle_bring(constants::SHIP_TRACTORBEAM_BRING_TO_DISTANCE),
_ => (),
}
}
- pub fn get_acceleration(&self, ship_position: Vector, target_position: Vector) -> Vector {
- let acceleration = ship_position.clone() - target_position.clone();
- match self.status {
- Status::Push => acceleration.unitize() * -0.05,
- Status::Pull => acceleration.unitize() * 0.05,
- Status::Bring => match self.desired_distance {
- Some(desired_distance) => {
- if desired_distance > ship_position.distance_from(target_position) {
- acceleration.unitize() * -0.05
- //some sort of velocity limiter
- //if target.speed_torwards(ship) < 10.0 {
- // acceleration.unitize() * -0.05
- //} else {
- // Vector::default()
- //}
- } else {
- acceleration.unitize() * 0.05
- }
- }
- None => Vector::default(),
- },
- Status::None => Vector::default(),
- }
- }
-
fn toggle_pull(&mut self) {
self.status = match self.status {
Status::None => Status::Pull,
@@ -83,6 +126,7 @@ impl Tractorbeam {
fn toggle_bring(&mut self, desired_distance: f64) {
self.desired_distance = Some(desired_distance);
+ self.control_system = ControlSystem::new();
self.status = match self.status {
Status::None => Status::Bring,
Status::Pull => Status::Bring,
@@ -90,6 +134,10 @@ impl Tractorbeam {
Status::Bring => Status::None,
}
}
+
+ fn off(&mut self) {
+ self.status = Status::None
+ }
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -98,7 +146,7 @@ pub struct ClientData {
pub status: Status,
}
-#[derive(Serialize, Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Status {
None,
Push,