changeset 1513:4055b09160ae

Clean and decouple. (2)
author yoctozepto
date Mon, 02 Sep 2013 22:08:21 +0200
parents af57b3b76fe4
children 965af46e8793
files Level/Decoration.h VectorTypes.cpp VectorTypes.h mm7_2.cpp mm7_4.cpp mm7_data.h
diffstat 6 files changed, 87 insertions(+), 247 deletions(-) [+]
line wrap: on
line diff
--- a/Level/Decoration.h	Mon Sep 02 15:23:09 2013 +0200
+++ b/Level/Decoration.h	Mon Sep 02 22:08:21 2013 +0200
@@ -30,7 +30,7 @@
   int32_t field_10_y_rot;
   uint16_t uCog;
   int16_t field_16_event_id;
-  int16_t field_18;
+  uint16_t uTriggerRange;
   int16_t field_1A;
   int16_t _idx_in_stru123;
   int16_t field_1E;
--- a/VectorTypes.cpp	Mon Sep 02 15:23:09 2013 +0200
+++ b/VectorTypes.cpp	Mon Sep 02 22:08:21 2013 +0200
@@ -1,45 +1,43 @@
+#include <utility>
+
 #include "mm7_data.h"
 
+//----- (004621DA) --------------------------------------------------------
+uint32_t int_get_vector_length(int32_t x, int32_t y, int32_t z)
+{
+  if (x < y)
+  {
+    std::swap(x, y);
+  }
+  if (x < z)
+  {
+    std::swap(x, z);
+  }
+  if (y < z)
+  {
+    std::swap(y, z);
+  }
+
+  return x + (11 * y >> 5) + (z >> 2);
+}
+
 //----- (0044C362) --------------------------------------------------------
-void Vec3_int_::Normalize_float()
+template <class T>
+void Vec3<T>::Normalize_float()
 {
-  //Vec3_int_ *v1; // esi@1
-  double v2; // st6@1
-  float v3; // ST20_4@1
-  double v4; // st5@1
-  float v5; // ST18_4@1
-  double v6; // st4@1
-  float v7; // ST14_4@1
-  float v8; // ST24_4@1
-  float v9; // ST20_4@1
-  double v10; // ST0C_8@1
-  float v11; // ST18_4@1
-  double v12; // ST0C_8@1
-  float v13; // ST14_4@1
-  double v14; // ST0C_8@1
+  long double x = this->x;
+  long double y = this->y;
+  long double z = this->z;
+  long double s = sqrtl(x * x + y * y + z * z);
 
-  assert(false);
-  //v1 = this;
-  v2 = (double)this->x * 0.000015258789;
-  v3 = v2;
-  v4 = (double)this->y * 0.000015258789;
-  v5 = v4;
-  v6 = (double)this->z * 0.000015258789;
-  v7 = v6;
-  v8 = 1.0 / sqrt(v6 * v6 + v4 * v4 + v2 * v2);
-  v9 = v8 * v3 * 65536.0;
-  v10 = v9 + 6.7553994e15;
-  this->x = LODWORD(v10);
-  v11 = v8 * v5 * 65536.0;
-  v12 = v11 + 6.7553994e15;
-  this->y = LODWORD(v12);
-  v13 = v8 * v7 * 65536.0;
-  v14 = v13 + 6.7553994e15;
-  this->z = LODWORD(v14);
+  this->x = (T)(x / s);
+  this->y = (T)(y / s);
+  this->z = (T)(z / s);
 }
 
 //----- (0043AA99) --------------------------------------------------------
-void __fastcall Vec3_int_::Rotate(int sDepth, int sRotY, int sRotX, Vec3_int_ v, int *outx, int *outy, int *outz)
+template <class T>
+void __fastcall Vec3<T>::Rotate(T sDepth, T sRotY, T sRotX, Vec3<T> v, T *outx, T *outy, T *outz)
 {
  float cosf_x = cosf(3.14159265f * sRotX / 1024.0f),
        sinf_x = sinf(3.14159265f * sRotX / 1024.0f),
@@ -52,9 +50,12 @@
 }
 
 //----- (0043AB61) --------------------------------------------------------
-void Vec3_int_::Normalize(int *x, int *y, int *z)
+template <class T>
+void Vec3<T>::Normalize(T *x, T *y, T *z)
 {
   *x *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
   *y *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
   *z *= 65536 / (integer_sqrt(*y * *y + *z * *z + *x * *x) | 1);
 }
+
+template Vec3<int32_t>;
--- a/VectorTypes.h	Mon Sep 02 15:23:09 2013 +0200
+++ b/VectorTypes.h	Mon Sep 02 22:08:21 2013 +0200
@@ -1,55 +1,53 @@
 #pragma once
 
-#pragma pack(push, 1)
-struct Vec2_short_
-{
-  __int16 x;
-  __int16 y;
-};
-#pragma pack(pop)
+#include <cstdint>
+
+uint32_t int_get_vector_length(int32_t x, int32_t y, int32_t z);
 
 #pragma pack(push, 1)
-struct Vec2_int_
+template <class T>
+struct Vec2
 {
-  int x;
-  int y;
-};
-#pragma pack(pop)
+  T x;
+  T y;
 
-#pragma pack(push, 1)
-struct Vec2_float_
-{
-  float x;
-  float y;
+  inline Vec2(T a = 0, T b = 0):
+    x(a), y(b)
+  {}
 };
 #pragma pack(pop)
 
+#define Vec2_int_ Vec2<int32_t>
+#define Vec2_float_ Vec2<float>
+
 #pragma pack(push, 1)
-struct Vec3_short_
+template <class T>
+struct Vec3: public Vec2<T>
 {
-  __int16 x;
-  __int16 y;
-  __int16 z;
+  T z;
+
+  inline Vec3(T a = 0, T b = 0, T c = 0):
+    Vec2(a, b), z(c)
+  {}
+
+  void Normalize_float();
+  template <class U>
+  inline uint32_t GetDistanceTo(Vec3<U> &o)
+  {
+    return int_get_vector_length(
+      abs(this->x - o.x),
+      abs(this->y - o.y),
+      abs(this->z - o.z)
+    );
+  }
+
+  static void __fastcall Rotate(T sDepth, T sRotY, T sRotX, Vec3<T> v, T *outx, T *outy, T *outz);
+  static void Normalize(T *x, T *y, T *z);
 };
 #pragma pack(pop)
 
-#pragma pack(push, 1)
-struct Vec3_int_
-{
-  inline Vec3_int_(int a = 0, int b = 0, int c = 0):
-    x(a), y(b), z(c)
-  {}
-
-  void Normalize_float();
-
-  static void __fastcall Rotate(int sDepth, int sRotY, int sRotX, Vec3_int_ v, int *outx, int *outy, int *outz);
-  static void Normalize(int *x, int *y, int *z);
-
-  int x;
-  int y;
-  int z;
-};
-#pragma pack(pop)
+#define Vec3_short_ Vec3<int16_t>
+#define Vec3_int_ Vec3<int32_t>
 
 #pragma pack(push, 1)
 struct Vec3_float_
--- a/mm7_2.cpp	Mon Sep 02 15:23:09 2013 +0200
+++ b/mm7_2.cpp	Mon Sep 02 22:08:21 2013 +0200
@@ -3113,32 +3113,6 @@
       event_triggers[num_event_triggers++] = i;
 }
 
-//----- (004621DA) --------------------------------------------------------
-int int_get_vector_length(signed int x, signed int y, signed int z)
-{
-  signed int v3; // eax@2
-
-  if ( x < y )
-  {
-    v3 = x;
-    x = y;
-    y = v3;
-  }
-  if ( x < z )
-  {
-    v3 = x;
-    x = z;
-    z = v3;
-  }
-  if ( y < z )
-  {
-    v3 = y;
-    y = z;
-    z = v3;
-  }
-  return (11 * y >> 5) + x + (z >> 2);
-}
-
 OPENFILENAMEA ofn;
 //----- (0046271C) --------------------------------------------------------
 void CreateDefaultBLVLevel()
--- a/mm7_4.cpp	Mon Sep 02 15:23:09 2013 +0200
+++ b/mm7_4.cpp	Mon Sep 02 22:08:21 2013 +0200
@@ -49,165 +49,33 @@
 //----- (0046CC4B) --------------------------------------------------------
 void check_event_triggers()
 {
-  int v0; // eax@1
   LevelDecoration *v1; // esi@2
-  signed int v2; // edi@2
-  int v3; // ebx@2
-  int v4; // eax@3
-  int v5; // ebx@3
-  unsigned int v6; // ecx@3
-  unsigned int v7; // edx@6
-  unsigned int v8; // edx@8
-  Actor *v9; // edi@13
-  int v10; // ebx@14
-  int v11; // eax@14
-  int v12; // ebx@14
-  unsigned int v13; // ecx@14
-  int v14; // edx@15
-  unsigned int v15; // edx@17
-  unsigned int v16; // edx@19
-  char *v17; // edi@25
-  int v18; // ebx@26
-  int v19; // eax@26
-  int v20; // ebx@26
-  unsigned int v21; // ecx@26
-  int v22; // edx@27
-  unsigned int v23; // edx@29
-  unsigned int v24; // edx@31
-  int v25; // [sp+0h] [bp-24h]@3
-  int v26; // [sp+0h] [bp-24h]@14
-  int v27; // [sp+0h] [bp-24h]@26
-  int v28; // [sp+4h] [bp-20h]@3
-  int v29; // [sp+4h] [bp-20h]@14
-  int v30; // [sp+4h] [bp-20h]@26
-  signed int v31; // [sp+8h] [bp-1Ch]@2
-  int v32; // [sp+Ch] [bp-18h]@2
-  int v33; // [sp+10h] [bp-14h]@2
-  int i; // [sp+14h] [bp-10h]@1
-  int v35; // [sp+18h] [bp-Ch]@2
-  int v36; // [sp+1Ch] [bp-8h]@3
-  signed int v37; // [sp+1Ch] [bp-8h]@12
-  signed int v38; // [sp+20h] [bp-4h]@24
 
-  v0 = 0;
-  for ( i = 0; i < num_event_triggers; ++i )
+  for (size_t i = 0; i < num_event_triggers; i++)
   {
-    v1 = &pLevelDecorations[event_triggers[v0]];
-    v2 = v1->field_18;
-    v3 = v1->vPosition.y;
-    v33 = v1->vPosition.x;
-    v32 = v1->vPosition.y;
-    v35 = v1->vPosition.z;
-    v31 = v1->field_18;
-    if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_TOUCH)
+    v1 = &pLevelDecorations[event_triggers[i]];
+
+    if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_TOUCH
+        && v1->vPosition.GetDistanceTo(pParty->vPosition) < v1->uTriggerRange)
     {
-      v36 = abs(v1->vPosition.x - pParty->vPosition.x);
-      v25 = abs(v3 - pParty->vPosition.y);
-      v28 = abs(v35 - pParty->vPosition.z);
-      v4 = v36;
-      v5 = v25;
-      v6 = v28;
-      if ( v36 < v25 )
-      {
-        v4 = v25;
-        v5 = v36;
-      }
-      if ( v4 < v28 )
-      {
-        v7 = v4;
-        v4 = v28;
-        v6 = v7;
-      }
-      if ( v5 < (signed int)v6 )
-      {
-        v8 = v6;
-        v6 = v5;
-        v5 = v8;
-      }
-      if ( (signed int)(((unsigned int)(11 * v5) >> 5) + (v6 >> 2) + v4) < v2 )
-        EventProcessor(v1->field_16_event_id, PID(OBJECT_Decoration,i), 1);
+      EventProcessor(v1->field_16_event_id, PID(OBJECT_Decoration,i), 1);
     }
-    if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_MONSTER)
+    else if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_MONSTER)
     {
-      v37 = 0;
-      if ( (signed int)uNumActors > 0 )
+      for (size_t j = 0; j < uNumActors; j++)
       {
-        v9 = pActors.data();//[0].vPosition.y;
-        do
-        {
-		  v10 = abs(v33 - v9->vPosition.x);
-          v29 = abs(v32 - v9->vPosition.y);
-          v26 = abs(v35 - v9->vPosition.z);
-          v11 = v10;
-          v12 = v29;
-          v13 = v26;
-          if ( v11 < v29 )
-          {
-            v14 = v11;
-            v11 = v29;
-            v12 = v14;
-          }
-          if ( v11 < v26 )
-          {
-            v15 = v11;
-            v11 = v26;
-            v13 = v15;
-          }
-          if ( v12 < (signed int)v13 )
-          {
-            v16 = v13;
-            v13 = v12;
-            v12 = v16;
-          }
-          if ( (signed int)(((unsigned int)(11 * v12) >> 5) + (v13 >> 2) + v11) < v31 )
-            EventProcessor(v1->field_16_event_id, 0, 1);
-          ++v37;
-          ++v9;
-        }
-        while ( v37 < (signed int)uNumActors );
+        if (v1->vPosition.GetDistanceTo(pActors[j].vPosition) < v1->uTriggerRange)
+          EventProcessor(v1->field_16_event_id, 0, 1);
       }
     }
-    if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_OBJECT)
+    else if (v1->uFlags & LEVEL_DECORATION_TRIGGERED_BY_OBJECT)
     {
-      v38 = 0;
-      if ( (signed int)uNumSpriteObjects > 0 )
+      for (size_t j = 0; j < uNumSpriteObjects; j++)
       {
-        v17 = (char *)&pSpriteObjects[0].vPosition.y;
-        do
-        {
-          v18 = abs(v33 - *((int *)v17 - 1));
-          v30 = abs(v32 - *(int *)v17);
-          v27 = abs(v35 - *((int *)v17 + 1));
-          v19 = v18;
-          v20 = v30;
-          v21 = v27;
-          if ( v19 < v30 )
-          {
-            v22 = v19;
-            v19 = v30;
-            v20 = v22;
-          }
-          if ( v19 < v27 )
-          {
-            v23 = v19;
-            v19 = v27;
-            v21 = v23;
-          }
-          if ( v20 < (signed int)v21 )
-          {
-            v24 = v21;
-            v21 = v20;
-            v20 = v24;
-          }
-          if ( (signed int)(((unsigned int)(11 * v20) >> 5) + (v21 >> 2) + v19) < v31 )
-            EventProcessor(v1->field_16_event_id, 0, 1);
-          ++v38;
-          v17 += 112;
-        }
-        while ( v38 < (signed int)uNumSpriteObjects );
+        if (v1->vPosition.GetDistanceTo(pSpriteObjects[j].vPosition) < v1->uTriggerRange)
+          EventProcessor(v1->field_16_event_id, 0, 1);
       }
     }
-    v0 = i + 1;
   }
 }
 // 6836C8: using guessed type int 6836C8_num_decorations_6807E8;
--- a/mm7_data.h	Mon Sep 02 15:23:09 2013 +0200
+++ b/mm7_data.h	Mon Sep 02 22:08:21 2013 +0200
@@ -1199,7 +1199,6 @@
 void PrepareToLoadBLV(unsigned int bLoading);
 void __fastcall PrepareToLoadODM(unsigned int bLoading, struct OutdoorCamera *a2);
 void _461103_load_level_sub();
-int int_get_vector_length(signed int x, signed int y, signed int z);
 void MainMenu_Loop();
 char sub_4637E0_is_there_popup_onscreen();
 void ResetCursor_Palettes_LODs_Level_Audio_SFT_Windows();