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()); }
// getPlayer().sendServerInfo("You make progress on the quest " + // getName() + ". You have " + status.currentCount + "/" + status.targetCount + " " + // status.template.getName()); // } // return; // } // else { // Log.debug("CollectionGoalStatus.handleAcquire: no match"); // } // } // } // finally { // lock.unlock(); // } // } public void handleInvUpdate() { if (Log.loggingDebug) if (Log.loggingDebug) Log.debug("CollectionQuestState.handleAcquire: quest=" + getName()); lock.lock(); try { if (getConcluded()) { return; } // MarsMob player = getPlayer(); // Log.debug("CollectionQuestState.handleInvUpdate: player=" + // player.getName() + // ", questState=" + this.getName() + // ", numGoals=" + // goalsStatus.size()); // Iterator<CollectionGoalStatus> iter = goalsStatus.iterator(); // while(iter.hasNext()) { // CollectionGoalStatus status = iter.next(); // int invCount = player.getInvItemCount(status.template); // Log.debug("CollectionQuestState.handleInvUpdate: checking itemTemplate=" + status.template // + ", invCount=" + invCount); // if (getCompleted()) { // Log.debug("CollectionQuestState.handleInvUpdate: completed"); // // the quest was completed prior to getting this item // if (invCount > status.currentCount) { // // we must have just picked up a quest item // // but we dont need it // continue; // } // else if (invCount == status.currentCount) { // // quest is still completed - you can ignore // continue; // } // // // invCount < status.currentCount // // we must have just dropped an item, and // // the quest is no longer complete // setCompleted(false); // status.currentCount = invCount; // // // send a QuestCompleted event to mobs that perceive us // // so that if one of them can accept this quest for // // completion, it can update its state // QuestCompleted completeEvent = // new QuestCompleted(player, getQuest()); // Engine.getMessageServer().sendToPerceivers(player, // completeEvent, // RDPMessageServer.NON_USERS); // // // send a log update // Event questStateInfo = new QuestStateInfo(player, this); // player.sendEvent(questStateInfo); // // // send text message // player.sendServerInfo("Your quest " + getName() + " is no longer // completed. You have " + status.currentCount + "/" + status.targetCount + " " + // status.template.getName()); // continue; // } // // // quest is not completed // Log.debug("CollectionQuestState.handleInvUpdate: not completed"); // // if (invCount < status.currentCount) { // Log.debug("CollectionQuestState.handleInvUpdate: lost item"); // // we lost an item // status.currentCount = invCount; // // // send a log update // Event questStateInfo = new QuestStateInfo(player, this); // player.sendEvent(questStateInfo); // // // send text message // player.sendServerInfo("You lost an item for quest " + getName() + ". // You have " + status.currentCount + "/" + status.targetCount + " " + // status.template.getName()); // continue; // } // else if (invCount == status.currentCount) { // // we didnt get a new quest item // Log.debug("CollectionQuestState.handleInvUpdate: not quest item"); // continue; // } // if (invCount > status.currentCount) { // // we just got a new needed quest item // Log.debug("CollectionQuestState.handleInvUpdate: got new quest item"); // if (invCount > status.targetCount) { // // we have more than we need // status.currentCount = status.targetCount; // } // else { // status.currentCount = invCount; // } // // // send a log update // Event questStateInfo = new QuestStateInfo(player, this); // player.sendEvent(questStateInfo); // // // send text message // player.sendServerInfo("You make progress on the quest " + getName() + ". // You have " + status.currentCount + "/" + status.targetCount + " " + // status.template.getName()); // // // check if completed and handle if it is // // (sending message to user and also notifying // // mobs nearby in case one of them is a quest // // completer) // completeHandler(); // } // } } finally { lock.unlock(); } }