/** * Returns neighbor units in given range (radius). * For example, given a Unit X and 1 as range, will return unit * to the left of X AND to the right of X, if they exist (even if dead). * If you provide a side argument, only that side is returned. * So for Unit X, range 1, side left, only the ONE unit to the left of X is returned. * * @param Unit $oUnit * @param int $iRange * @param string $sSide * @return array */ public function getAdjacentUnits(Unit $oUnit, $iRange = 1, $sSide = 'both') { $aAdjacent = array(); while ($iRange > 0) { if ($sSide == 'both' || $sSide == 'left') { if (isset($this->aUnits[$oUnit->getIndex() - $iRange])) { $aAdjacent[] = $this->aUnits[$oUnit->getIndex() - $iRange]; } } if ($sSide == 'both' || $sSide == 'right') { if (isset($this->aUnits[$oUnit->getIndex() + $iRange])) { $aAdjacent[] = $this->aUnits[$oUnit->getIndex() + $iRange]; } } $iRange--; } return array_reverse($aAdjacent); }