title: Targeting logic for 1.000.000 units army
body: <p>I am making an army fight for my website <a href="http://www.armyfight.com" rel="nofollow">armyfight</a>. There can be fights of million units. I am looking for better unit targeting. in <a href="https://jsfiddle.net/mansim/a2cmmr1m/18/" rel="nofollow">jsfiddle</a> is an example.<br></p>
  
  <pre><code>var enemy = {
      2: { 
          3: {}  // object inside with unit data
      },
        5: { 
          6: {} 
      }
  }
  
  var ally = {
      4: { 
          3: {}
      }
  }
  
  function unit_searches_for_target(y, x) {
      var closest_y   = iterate_Y(y);
      var unit_target = iterate_X(closest_y, x);
    return unit_target;
  }
  
  // Searches for closest target at Y axis
  function iterate_Y(y) {
      if (_.has(target, y)) {
          return y;
      } else {
        var y = parseInt(y);
          for (var i = 1; i &lt; 300; i++) {
              // If y == 4, it checks 4+1=5 (positive side)
              if (_.has(target, String(y + i))) {
                  return (y+i);
              // Else if y == 4, it checks 4-1=3 (negative side)
              } else if (_.has(target, String(y - i))) {
                  return (y-i);
              }
          }
      }
      // if didn't find anything, just returns same y as given
      return y;
  };
  
  // Searches for closest target at X axis
  function iterate_X(y, x) {
      if (_.has(target[y], x)) {
          return {y: y, x: x};
      } else {
        var x = parseInt(x);
          for (var i = 1; i &lt; 300; i++) {
              // If x == 4, it checks 4+1=5 (positive side)
              if (_.has(target[y], String(x + i))) {
              return {y: y, x: x + i};
              // Else if x == 4, it checks 4-1=3 (negative side)
              } else if (_.has(target[y], String(x - i))) {
                return {y: y, x: x - i};
              }
          }
      }
      // if didn't find anything, just returns same x as given
      return {y: y, x: x};
  };
  </code></pre>
  
  <p>Here first i iterate on Y axis, later on X axis. But on different army positions it gives wrong results.
  <br><br> 
  In this example, units are looking for targets by simply iterating. It is not very good for a huge army fight of million units.<br>
  Best would be to iterate like here, when iteration process is much faster leading to first-hit success:</p>
  
  <pre><code>function iterate_X(y, x) {
    return _.find(enemy[y], function (value, key) { return parseInt(key) &gt;= x });
  }
  </code></pre>
  
  <p>But this iteration works only 0..100, or 99..100, but not 100..0, so i can't iterate objects in reverse mode.<br> So if ally is <code>{5: {10: {}, 99: {}}</code> and enemy is at <code>{5: {100: {}}}</code>, enemy unit would find closest ally as [5, 10] instead of [5, 99]. If it would be an array iterating backwards from 100 to 0, so it would be first-hit success. But then i have problems with assotiative array, can it be reverse-iterated? <br><br>
  Any suggestions please on structure? Maybe change units structure from objects to arrays or mix?  But then I have problems with associative arrays. I am looking for an explanation or example about the structure and for a faster iteration for units targeting.<br><br>
  And i am also looking for websites, where i could ask these heavy logic questions? I have more of them.</p>