Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inserter/src/algorithm/data/planet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ impl<'a> Planet<'a> {
}
}

#[allow(dead_code)]
pub fn is_tidal_locked(&self) -> bool {
self.get_rotation_period() == self.get_orbital_period()
}
Expand Down
11 changes: 11 additions & 0 deletions inserter/src/algorithm/data/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub struct Quaternion {
}

impl Quaternion {
#[inline]
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
Self { x, y, z, w }
}

#[inline]
pub fn identity() -> Self {
Self {
x: 0.0,
Expand All @@ -26,6 +28,7 @@ impl Quaternion {

/// Port of Unity's `Quaternion.AngleAxis(angle_degrees, axis)`.
/// `angle_degrees` is in degrees (same as Unity's AngleAxis).
#[inline]
pub fn angle_axis(angle_degrees: f32, axis: &VectorF3) -> Self {
let half_rad = (angle_degrees * std::f32::consts::PI / 180.0) * 0.5;
let s = half_rad.sin();
Expand All @@ -40,6 +43,7 @@ impl Quaternion {

/// Port of `Maths.QInvRotateLF(Quaternion, VectorLF3)` — inverse rotates a VectorF3.
/// Since the user specified VectorLF3 maps to VectorF3 in Rust, this uses f32.
#[inline]
pub fn q_inv_rotate_lf(&self, v: &VectorF3) -> VectorF3 {
let vx = v.0 * 2.0;
let vy = v.1 * 2.0;
Expand All @@ -56,6 +60,7 @@ impl Quaternion {
/// Port of Unity's `Quaternion.FromToRotation(fromDirection, toDirection)`.
///
/// Creates a rotation that rotates `from` to align with `to`.
#[inline]
pub fn from_to_rotation(from: &VectorF3, to: &VectorF3) -> Self {
let from_mag_sq = from.magnitude_sq();
let to_mag_sq = to.magnitude_sq();
Expand Down Expand Up @@ -113,6 +118,7 @@ impl Quaternion {
}

/// Port of `Maths.QRotateLF(Quaternion, VectorLF3)` — rotates a VectorF3.
#[inline]
pub fn q_rotate_lf(&self, v: &VectorF3) -> VectorF3 {
let vx = v.0 * 2.0;
let vy = v.1 * 2.0;
Expand All @@ -134,6 +140,7 @@ impl Quaternion {
impl Mul<&VectorF3> for &Quaternion {
type Output = VectorF3;

#[inline]
fn mul(self, point: &VectorF3) -> VectorF3 {
let num1 = self.x * 2.0;
let num2 = self.y * 2.0;
Expand All @@ -159,6 +166,7 @@ impl Mul<&VectorF3> for &Quaternion {
impl Mul<Quaternion> for Quaternion {
type Output = Quaternion;

#[inline]
fn mul(self, rhs: Quaternion) -> Quaternion {
Quaternion {
x: self.x * rhs.w + self.y * rhs.z - self.z * rhs.y + self.w * rhs.x,
Expand All @@ -172,6 +180,7 @@ impl Mul<Quaternion> for Quaternion {
impl Mul<&Quaternion> for Quaternion {
type Output = Quaternion;

#[inline]
fn mul(self, rhs: &Quaternion) -> Quaternion {
Quaternion {
x: self.x * rhs.w + self.y * rhs.z - self.z * rhs.y + self.w * rhs.x,
Expand All @@ -185,6 +194,7 @@ impl Mul<&Quaternion> for Quaternion {
impl Mul<Quaternion> for &Quaternion {
type Output = Quaternion;

#[inline]
fn mul(self, rhs: Quaternion) -> Quaternion {
Quaternion {
x: self.x * rhs.w + self.y * rhs.z - self.z * rhs.y + self.w * rhs.x,
Expand All @@ -198,6 +208,7 @@ impl Mul<Quaternion> for &Quaternion {
impl Mul<&Quaternion> for &Quaternion {
type Output = Quaternion;

#[inline]
fn mul(self, rhs: &Quaternion) -> Quaternion {
Quaternion {
x: self.x * rhs.w + self.y * rhs.z - self.z * rhs.y + self.w * rhs.x,
Expand Down
38 changes: 18 additions & 20 deletions inserter/src/algorithm/data/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@ impl DspRandom {
num1 = seed_array[index2]
}

let ptr = seed_array.as_mut_ptr();

let (chunk1_lhs, chunk1_rhs, chunk2_lhs, chunk2_rhs) = unsafe {
(
&mut *ptr.add(1).cast::<[i32; 24]>(),
&*ptr.add(32).cast::<[i32; 24]>(),
&mut *ptr.add(25).cast::<[i32; 31]>(),
&*ptr.add(1).cast::<[i32; 31]>(),
)
};

let update = |(lhs, rhs): (&mut i32, &i32)| {
*lhs = lhs.wrapping_sub(*rhs);
if lhs.is_negative() {
*lhs += i32::MAX;
}
};

for _ in 1..5 {
chunk1_lhs.iter_mut().zip(chunk1_rhs).for_each(update);
chunk2_lhs.iter_mut().zip(chunk2_rhs).for_each(update);
for i in 0..24 {
let lhs = seed_array[1 + i];
let rhs = seed_array[32 + i];
let mut val = lhs.wrapping_sub(rhs);
if val < 0 {
val += i32::MAX;
}
seed_array[1 + i] = val;
}
for i in 0..31 {
let lhs = seed_array[25 + i];
let rhs = seed_array[1 + i];
let mut val = lhs.wrapping_sub(rhs);
if val < 0 {
val += i32::MAX;
}
seed_array[25 + i] = val;
}
}

Self {
Expand Down
1 change: 1 addition & 0 deletions inserter/src/algorithm/data/rule.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
use super::galaxy::Galaxy;
use serde::{Deserialize, Serialize};

Expand Down
12 changes: 6 additions & 6 deletions inserter/src/algorithm/data/star_planets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ pub struct StarWithPlanets<'a> {
planets: UnsafeCell<Vec<Planet<'a>>>,

#[serde(skip)]
safe: UnsafeCell<bool>,
safe: Cell<bool>,
#[serde(skip)]
avg_veins: UnsafeCell<[f32; MAX_VEIN_COUNT]>,
#[serde(skip)]
#[allow(dead_code)]
actual_veins: UnsafeCell<[f32; MAX_VEIN_COUNT]>,
#[serde(skip)]
game_desc: &'a GameDesc,
Expand All @@ -50,7 +51,7 @@ impl<'a> StarWithPlanets<'a> {
Self {
star,
planets: UnsafeCell::new(Vec::with_capacity(6)),
safe: UnsafeCell::new(false),
safe: Cell::new(false),
avg_veins: UnsafeCell::new([f32::NAN; MAX_VEIN_COUNT]),
actual_veins: UnsafeCell::new([f32::NAN; MAX_VEIN_COUNT]),
name: Default::default(),
Expand All @@ -60,13 +61,11 @@ impl<'a> StarWithPlanets<'a> {
}

pub fn is_safe(&self) -> bool {
unsafe { *self.safe.get() }
self.safe.get()
}

pub fn mark_safe(&self) {
unsafe {
*self.safe.get() = true;
}
self.safe.set(true);
}

pub fn load_planets(&self) {
Expand Down Expand Up @@ -123,6 +122,7 @@ impl<'a> StarWithPlanets<'a> {
count
}

#[allow(dead_code)]
pub fn get_actual_vein(&self, vein_type: &VeinType) -> f32 {
if vein_type == &VeinType::Mag
&& self.star.star_type != StarType::BlackHole
Expand Down
31 changes: 24 additions & 7 deletions inserter/src/algorithm/data/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,51 @@ use serde::Serialize;
pub struct Vector3(pub f64, pub f64, pub f64);

impl Vector3 {
#[inline]
pub fn zero() -> Self {
Self(0.0, 0.0, 0.0)
}

#[inline]
pub fn distance_sq_from(&self, pt: &Self) -> f64 {
let dx = pt.0 - self.0;
let dy = pt.1 - self.1;
let dz = pt.2 - self.2;
dx * dx + dy * dy + dz * dz
}

#[inline]
pub fn distance_from(&self, pt: &Self) -> f64 {
self.distance_sq_from(pt).sqrt()
}

#[inline]
pub fn magnitude_sq(&self) -> f64 {
self.0 * self.0 + self.1 * self.1 + self.2 * self.2
}

#[inline]
pub fn magnitude(&self) -> f64 {
self.magnitude_sq().sqrt()
}

#[inline]
pub fn normalize(&mut self) -> &mut Self {
let num = self.magnitude();
if num > 1e-5 {
*self /= num;
} else {
*self = Self::zero();
}
return self;
self
}

#[inline]
pub fn dot(&self, rhs: &Vector3) -> f64 {
return self.0 * rhs.0 + self.1 * rhs.1 + self.2 * rhs.2;
self.0 * rhs.0 + self.1 * rhs.1 + self.2 * rhs.2
}

#[inline]
pub fn slerp(lhs: &Vector3, rhs: &Vector3, percent: f64) -> Vector3 {
let dot = lhs.dot(rhs).clamp(-1.0, 1.0);
let theta = dot.acos() * percent;
Expand All @@ -53,12 +61,14 @@ impl Vector3 {
impl std::ops::Add<&Vector3> for &Vector3 {
type Output = Vector3;

#[inline]
fn add(self, rhs: &Vector3) -> Vector3 {
return Vector3(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2);
Vector3(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2)
}
}

impl std::ops::AddAssign<&Vector3> for Vector3 {
#[inline]
fn add_assign(&mut self, rhs: &Vector3) {
self.0 += rhs.0;
self.1 += rhs.1;
Expand All @@ -69,12 +79,14 @@ impl std::ops::AddAssign<&Vector3> for Vector3 {
impl std::ops::Sub<&Vector3> for &Vector3 {
type Output = Vector3;

#[inline]
fn sub(self, rhs: &Vector3) -> Vector3 {
return Vector3(self.0 - rhs.0, self.1 - rhs.1, self.2 - rhs.2);
Vector3(self.0 - rhs.0, self.1 - rhs.1, self.2 - rhs.2)
}
}

impl std::ops::SubAssign<&Vector3> for Vector3 {
#[inline]
fn sub_assign(&mut self, rhs: &Vector3) {
self.0 -= rhs.0;
self.1 -= rhs.1;
Expand All @@ -85,20 +97,23 @@ impl std::ops::SubAssign<&Vector3> for Vector3 {
impl std::ops::Mul<f64> for Vector3 {
type Output = Vector3;

#[inline]
fn mul(self, rhs: f64) -> Vector3 {
return Vector3(self.0 * rhs, self.1 * rhs, self.2 * rhs);
Vector3(self.0 * rhs, self.1 * rhs, self.2 * rhs)
}
}

impl std::ops::Mul<f64> for &Vector3 {
type Output = Vector3;

#[inline]
fn mul(self, rhs: f64) -> Vector3 {
return Vector3(self.0 * rhs, self.1 * rhs, self.2 * rhs);
Vector3(self.0 * rhs, self.1 * rhs, self.2 * rhs)
}
}

impl std::ops::MulAssign<f64> for Vector3 {
#[inline]
fn mul_assign(&mut self, rhs: f64) {
self.0 *= rhs;
self.1 *= rhs;
Expand All @@ -109,12 +124,14 @@ impl std::ops::MulAssign<f64> for Vector3 {
impl std::ops::Div<f64> for &Vector3 {
type Output = Vector3;

#[inline]
fn div(self, rhs: f64) -> Vector3 {
return Vector3(self.0 / rhs, self.1 / rhs, self.2 / rhs);
Vector3(self.0 / rhs, self.1 / rhs, self.2 / rhs)
}
}

impl std::ops::DivAssign<f64> for Vector3 {
#[inline]
fn div_assign(&mut self, rhs: f64) {
self.0 /= rhs;
self.1 /= rhs;
Expand Down
Loading
Loading