public void handleMessage(Message msg, int flags) {
   try {
     lock.lock();
     if (activated == false) return; // return true;
     if (msg.getMsgType() == Behavior.MSG_TYPE_COMMAND) {
       Behavior.CommandMessage cmdMsg = (Behavior.CommandMessage) msg;
       String command = cmdMsg.getCmd();
       // Remove the executor, because anything we do will
       // end the current execution.
       Engine.getExecutor().remove(this);
       if (Log.loggingDebug)
         Log.debug(
             "BaseBehavior.onMessage: command = "
                 + command
                 + "; oid = "
                 + obj.getOid()
                 + "; name "
                 + obj.getName());
       if (command.equals(MSG_CMD_TYPE_GOTO)) {
         GotoCommandMessage gotoMsg = (GotoCommandMessage) msg;
         Point destination = gotoMsg.getDestination();
         mode = MSG_CMD_TYPE_GOTO;
         roamingBehavior = true;
         gotoSetup(destination, gotoMsg.getSpeed());
       } else if (command.equals(MSG_CMD_TYPE_STOP)) {
         followTarget = null;
         pathState.clear();
         obj.getWorldNode().setDir(new MVVector(0, 0, 0));
         obj.updateWorldNode();
         mode = MSG_CMD_TYPE_STOP;
         // If roamingBehavior is set, that means that we
         // used formerly had a roaming behavior, so send
         // an ArrivedEventMessage so that the other
         // behavior starts up again.
         if (roamingBehavior) {
           try {
             Engine.getAgent().sendBroadcast(new ArrivedEventMessage(obj));
           } catch (Exception e) {
             Log.error(
                 "BaseBehavior.onMessage: Error sending ArrivedEventMessage, error was '"
                     + e.getMessage()
                     + "'");
             throw new RuntimeException(e);
           }
         }
       } else if (command.equals(BaseBehavior.MSG_CMD_TYPE_FOLLOW)) {
         FollowCommandMessage followMsg = (FollowCommandMessage) msg;
         mode = MSG_CMD_TYPE_FOLLOW;
         followSetup(followMsg.getTarget(), followMsg.getSpeed());
       }
     } else if (msg.getMsgType() == WorldManagerClient.MSG_TYPE_MOB_PATH_CORRECTION) {
       Engine.getExecutor().remove(this);
       interpolatePath();
       interpolatingPath = false;
     }
     // return true;
   } finally {
     lock.unlock();
   }
 }
 protected boolean interpolatePath() {
   long timeNow = System.currentTimeMillis();
   PathLocAndDir locAndDir = pathState.interpolatePath(timeNow);
   long oid = obj.getOid();
   //         if (locAndDir != null) {
   //             if (Log.loggingDebug) {
   //                 Log.debug("BaseBehavior.interpolatePath: oid = " + oid + "; loc = " +
   // locAndDir.getLoc() + "; dir = " + locAndDir.getDir());
   //             }
   //         }
   //         else {
   //             if (Log.loggingDebug)
   //                 Log.debug("BaseBehavior.interpolatePath: oid = " + oid + "; locAndDir is
   // null");
   //         }
   if (locAndDir == null) {
     // We have arrived - - turn off interpolation, and cancel that path
     interpolatingPath = false;
     if (Log.loggingDebug)
       Log.debug(
           "BaseBehavior.interpolatePath: cancelling path: oid = "
               + oid
               + "; myLoc = "
               + obj.getWorldNode().getLoc());
     cancelPathInterpolator(oid);
     obj.getWorldNode().setDir(new MVVector(0, 0, 0));
   } else {
     obj.getWorldNode()
         .setPathInterpolatorValues(
             timeNow, locAndDir.getDir(), locAndDir.getLoc(), locAndDir.getOrientation());
     MobManagerPlugin.getTracker(obj.getInstanceOid()).updateEntity(obj);
   }
   return interpolatingPath;
 }
 public void gotoSetup(Point dest, int speed) {
   // calculate the vector to the destination
   destLoc = dest;
   mobSpeed = speed;
   Point myLoc = obj.getWorldNode().getLoc();
   long oid = obj.getOid();
   if (Log.loggingDebug)
     Log.debug("BaseBehavior.gotoSetup: oid = " + oid + "; myLoc = " + myLoc + "; dest = " + dest);
   scheduleMe(
       setupPathInterpolator(oid, myLoc, dest, false, obj.getWorldNode().getFollowsTerrain()));
 }
 public void followUpdate() {
   ObjectStub followObj = (ObjectStub) followTarget.getEntity(Namespace.MOB);
   Point followLoc = followObj.getWorldNode().getLoc();
   InterpolatedWorldNode node = obj.getWorldNode();
   Point myLoc = node.getLoc();
   long oid = obj.getOid();
   float fdist = Point.distanceTo(followLoc, destLoc);
   float dist = Point.distanceTo(followLoc, myLoc);
   if (Log.loggingDebug)
     Log.debug(
         "BaseBehavior.followUpdate: oid = "
             + oid
             + "; myLoc = "
             + myLoc
             + "; followLoc = "
             + followLoc
             + "; fdist = "
             + fdist
             + "; dist = "
             + dist);
   long msToSleep = (long) 500;
   // If the new target location is more than a meter from
   // the old one, create a new path.
   if (fdist > 1000) {
     long msToDest = setupPathInterpolator(oid, myLoc, followLoc, true, node.getFollowsTerrain());
     destLoc = followLoc;
     msToSleep = msToDest == 0 ? (long) 500 : Math.min((long) 500, msToDest);
   }
   // Else if we're interpolating, interpolate the current path
   else if (interpolatingPath) {
     interpolatePath();
     if (Log.loggingDebug)
       Log.debug(
           "baseBehavior.followUpdate: oid = "
               + oid
               + "; interpolated myLoc = "
               + obj.getWorldNode().getLoc());
   }
   scheduleMe(interpolatingPath ? msToSleep : pathState.pathTimeRemaining());
 }
 public void run() {
   try {
     lock.lock();
     if (activated == false) {
       return;
     }
     try {
       if (mode == MSG_CMD_TYPE_GOTO) {
         gotoUpdate();
       } else if (mode == MSG_CMD_TYPE_FOLLOW) {
         followUpdate();
       } else if (mode == MSG_CMD_TYPE_STOP) {
       } else {
         Log.error("BaseBehavior.run: invalid mode");
       }
     } catch (Exception e) {
       Log.exception("BaseBehavior.run caught exception raised during run for mode = " + mode, e);
       throw new RuntimeException(e);
     }
   } finally {
     lock.unlock();
   }
 }
 protected long setupPathInterpolator(
     long oid, Point myLoc, Point dest, boolean follow, boolean followsTerrain) {
   long timeNow = System.currentTimeMillis();
   WorldManagerClient.MobPathReqMessage reqMsg =
       pathState.setupPathInterpolator(timeNow, myLoc, dest, mobSpeed, follow, followsTerrain);
   if (reqMsg != null) {
     try {
       Engine.getAgent().sendBroadcast(reqMsg);
       if (Log.loggingDebug)
         Log.debug("BaseBehavior.setupPathInterpolator: send MobPathReqMessage " + reqMsg);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
     interpolatingPath = true;
     return pathState.pathTimeRemaining();
   } else {
     interpolatingPath = false;
     return 0;
   }
 }
 public void gotoUpdate() {
   Point myLoc = obj.getWorldNode().getLoc();
   long oid = obj.getOid();
   if (interpolatingPath) {
     interpolatePath();
     if (!interpolatingPath) {
       Engine.getAgent().sendBroadcast(new ArrivedEventMessage(obj));
       if (Log.loggingDebug)
         Log.debug(
             "BaseBehavior.gotoUpdate sending ArrivedEventMessage: oid = "
                 + oid
                 + "; myLoc = "
                 + myLoc
                 + "; destLoc = "
                 + destLoc);
       mode = MSG_CMD_TYPE_STOP;
     }
   }
   if (interpolatingPath) scheduleMe(pathState.pathTimeRemaining());
 }