From 2c018c117c1968cb933927046108e0067adeb7fb Mon Sep 17 00:00:00 2001
From: Nathan M Gilbert <tahgtahv@gmail.com>
Date: Thu, 21 Dec 2023 10:59:26 -0500
Subject: [PATCH] Implement Helicopter subclass (#336)

* Implement Helicopter subclass

* Minor fixes

---------

Co-authored-by: Christian Semmler <mail@csemmler.com>
---
 LEGO1/helicopter.cpp    | 48 ++++++++++++++++++++++++++++++++++++++---
 LEGO1/realtime/vector.h |  4 +++-
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/LEGO1/helicopter.cpp b/LEGO1/helicopter.cpp
index 2d13e011..87ee1dcb 100644
--- a/LEGO1/helicopter.cpp
+++ b/LEGO1/helicopter.cpp
@@ -233,7 +233,7 @@ MxU32 Helicopter::VTable0xd8(MxType18NotificationParam& p_param)
 		Matrix4 mat2 = mat.GetMatrix();
 		float s = sin(0.5235987901687622); // PI / 6, 30 deg
 		float c = cos(0.5235987901687622); // PI / 6, 30 deg
-		for (int i = 0; i < 4; i++) {
+		for (MxS32 i = 0; i < 4; i++) {
 			mat.GetMatrix()[i][1] = mat2[i][1] * c - mat2[i][2] * s;
 			mat.GetMatrix()[i][2] = mat2[i][2] * c + mat2[i][1] * s;
 		}
@@ -321,8 +321,50 @@ void Helicopter::VTable0x70(float p_float)
 	}
 }
 
-// STUB: LEGO1 0x100040a0
+// FUNCTION: LEGO1 0x100040a0
 MxResult HelicopterSubclass::FUN_100040a0(Vector4Impl& p_v, float p_f)
 {
-	return SUCCESS;
+	MxU32 state = m_unk0x30;
+	if (state == 1) {
+		p_v.EqualsImpl(m_unk0x0.GetVector().elements);
+		p_v[3] = acos(p_v[3]) * (1 - p_f) * 2.0;
+		return p_v.NormalizeQuaternion();
+	}
+	else if (state == 2) {
+		p_v.EqualsImpl(m_unk0x18.GetVector().elements);
+		p_v[3] = acos(p_v[3]) * p_f * 2.0;
+		p_v.NormalizeQuaternion();
+		return p_v.NormalizeQuaternion();
+	}
+	else if (state == 3) {
+		double d1 = p_v.Dot(&m_unk0x0, &m_unk0x18), d2;
+		if (d1 + 1 > 0.00001) {
+			if (1 - d1 > 0.00001) {
+				double d = acos(d1);
+				sin(d);
+				d1 = sin((1 - p_f) * d) / sin(d);
+				d2 = sin(p_f * d) / sin(d);
+			}
+			else {
+				d1 = 1 - p_f;
+				d2 = p_f;
+			}
+			for (MxS32 i = 0; i < 4; i++) {
+				p_v[i] = m_unk0x18[i] * d2 + m_unk0x0[i] * d1;
+			}
+			return SUCCESS;
+		}
+		p_v[0] = -m_unk0x0[1];
+		p_v[1] = m_unk0x0[1];
+		p_v[2] = -m_unk0x0[3];
+		p_v[3] = m_unk0x0[2];
+		d1 = sin((1 - p_f) * 1.570796326794895);
+		d2 = sin(p_f * 1.570796326794895);
+		for (MxS32 i = 0; i < 3; i++) {
+			p_v[i] = m_unk0x0[i] * d1 + p_v[i] * d2;
+		}
+		return SUCCESS;
+	}
+	else
+		return FAILURE;
 }
diff --git a/LEGO1/realtime/vector.h b/LEGO1/realtime/vector.h
index 3f61fb9a..5c31f24e 100644
--- a/LEGO1/realtime/vector.h
+++ b/LEGO1/realtime/vector.h
@@ -174,11 +174,13 @@ public:
 
 	void EqualsScalar(float* p_value);
 
-	// vtable + 0x84
+	// vtable + 0x88
 	virtual void SetMatrixProduct(Vector4Impl* p_a, float* p_b);
 	virtual void SetMatrixProductImpl(float* p_vec, float* p_mat);
 	virtual int NormalizeQuaternion();
 	virtual void UnknownQuaternionOp(Vector4Impl* p_a, Vector4Impl* p_b);
+
+	inline Vector4& GetVector() { return *((Vector4*) m_data); }
 };
 
 // VTABLE: LEGO1 0x100d4488