title: Reverse engineering - Is this a cheap 3D distance function?
body: <p>I am reverse engineering a game from 1999 and I came across a function which looks to be checking if the player is within range of a 3d point for the triggering of audio sources. The decompiler mangles the code pretty bad but I think I understand it.</p>
  
  <pre><code>// Position Y delta
  v1 = * (float * )(this + 16) - LocalPlayerZoneEntry - &gt; y;
  
  // Position X delta
  v2 = * (float * )(this + 20) - LocalPlayerZoneEntry - &gt; x;
  
  // Absolute value
  if (v1 &lt; 0.0)
      v1 = -v1;
  
  // Absolute value
  if (v2 &lt; 0.0)
      v2 = -v2;
  
  // What is going on here?
  if (v1 &lt;= v2)
      v1 = v1 * 0.5;
  else
      v2 = v2 * 0.5;
  
  // Z position delta
  v3 = * (float * )(this + 24) - LocalPlayerZoneEntry - &gt; z;
  
  // Absolute value
  if (v3 &lt; 0.0)
      v3 = -v3;
  
  result = v3 + v2 + v1;
  
  // Radius
  if (result &gt; * (float * )(this + 28))
      return 0.0;
  
  return result;
  </code></pre>
  
  <p>Interestingly enough, when in game, it seemed like the triggering was pretty inconsistent and would sometimes be quite a bit off depending on from which side I approached the trigger.</p>
  
  <p>Does anyone have any idea if this was a common algorithm used back in the day?</p>
  
  <p>Note: The types were all added by me so they may be incorrect. I assume that this is a function of type bool.</p>