public double getDistanceToTarget() {
   if (isFollowTarget) {
     VisibleObject target = owner.getTarget();
     if (target != null)
       return MathUtil.getDistance(
           owner.getX(), owner.getY(), owner.getZ(), target.getX(), target.getY(), target.getZ());
   }
   return MathUtil.getDistance(
       owner.getX(), owner.getY(), owner.getZ(), targetX, targetY, targetZ);
 }
 /**
  * Returns MapRegion that contains coordinates of VisibleObject. If the region doesn't exist, it's
  * created.
  *
  * @param object
  * @return a MapRegion
  */
 MapRegion getRegion(VisibleObject object) {
   return getRegion(object.getX(), object.getY(), object.getZ());
 }
  private void move() {
    /** Demo npc skills - prevent movement while casting */
    if (!owner.canPerformMove() || owner.isCasting()) {
      if (!isStopped) {
        isStopped = true;
        owner.getController().stopMoving();
      }
      return;
    }

    VisibleObject target = owner.getTarget();

    if (isFollowTarget && target != null) {
      setNewDirection(target.getX(), target.getY(), target.getZ());
    }

    float ownerX = owner.getX();
    float ownerY = owner.getY();
    float ownerZ = owner.getZ();

    double dist = MathUtil.getDistance(ownerX, ownerY, ownerZ, targetX, targetY, targetZ);
    if (dist > this.distance) {
      isStopped = false;

      float x2;
      float y2;
      float z2;
      float mod = 1;
      if (dist < speed * 0.5) {
        x2 = (targetX - ownerX);
        y2 = (targetY - ownerY);
        z2 = (targetZ - ownerZ);
      } else {
        x2 = (float) (((targetX - ownerX) / dist) * speed * 0.5);
        y2 = (float) (((targetY - ownerY) / dist) * speed * 0.5);
        z2 = (float) (((targetZ - ownerZ) / dist) * speed * 0.5);
        mod = 0.5f;
      }

      byte heading2 = (byte) (Math.toDegrees(Math.atan2(y2, x2)) / 3);

      if (directionChanged) {
        PacketSendUtility.broadcastPacket(
            owner,
            new SM_MOVE(
                owner.getObjectId(),
                ownerX,
                ownerY,
                ownerZ,
                (x2 / mod),
                (y2 / mod),
                (z2 / mod),
                heading2,
                MovementType.MOVEMENT_START_KEYBOARD));
        directionChanged = false;
      }

      moveCounter++;
      World.getInstance()
          .updatePosition(
              owner, ownerX + x2, ownerY + y2, ownerZ + z2, heading2, moveCounter % 5 == 0);
    } else {
      if (!isStopped) {
        isStopped = true;
        owner.getController().stopMoving();
      }
    }
  }