private static Point findLocation(final StendhalRPZone zone,final Entity entity,final Shape allowedArea,final int x,final int y,final boolean checkPath){
  for (int totalShift=1; totalShift <= maxDisplacement; totalShift++) {
    for (int tilt=(totalShift + 1) / 2; tilt > 0; tilt--) {
      final int spread=totalShift - tilt;
      int tmpx=x - tilt;
      int tmpy=y - spread;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpx=x + tilt;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpy=y + spread;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpx=x - tilt;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      if (spread == tilt) {
        continue;
      }
      tmpx=x - spread;
      tmpy=y - tilt;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpx=x + spread;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpy=y + tilt;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
      tmpx=x - spread;
      if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
        return new Point(tmpx,tmpy);
      }
    }
    int tmpx=x;
    int tmpy=y - totalShift;
    if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
      return new Point(tmpx,tmpy);
    }
    tmpy=y + totalShift;
    if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
      return new Point(tmpx,tmpy);
    }
    tmpy=y;
    tmpx=x - totalShift;
    if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
      return new Point(tmpx,tmpy);
    }
    tmpx=x + totalShift;
    if (isValidPlacement(zone,entity,allowedArea,x,y,tmpx,tmpy,checkPath)) {
      return new Point(tmpx,tmpy);
    }
  }
  return null;
}

private static Point findLocation ( final StendhalRPZone zone , final Entity entity , final Shape allowedArea , final int x , final int y , final boolean checkPath ) { for ( int totalShift = 1 ; totalShift <= maxDisplacement ; totalShift ++ ) { for ( int tilt = ( totalShift + 1 ) / 2 ; tilt > 0 ; tilt -- ) { final int spread = totalShift - tilt ; int tmpx = x - tilt ; int tmpy = y - spread ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpx = x + tilt ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpy = y + spread ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpx = x - tilt ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } if ( spread == tilt ) { continue ; } tmpx = x - spread ; tmpy = y - tilt ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpx = x + spread ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpy = y + tilt ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpx = x - spread ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } } int tmpx = x ; int tmpy = y - totalShift ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpy = y + totalShift ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpy = y ; tmpx = x - totalShift ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } tmpx = x + totalShift ; if ( isValidPlacement ( zone , entity , allowedArea , x , y , tmpx , tmpy , checkPath ) ) { return new Point ( tmpx , tmpy ) ; } } return null ; }
finds a new place for entity .
