package net.sourceforge.pain.logic.fn;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.data.*;
import net.sourceforge.pain.logic.trigger.*;
import net.sourceforge.pain.util.*;
import java.util.*;
public class MoveFn {
public static final int ERR_MOVE_NO_EXITS_FOUND = 1;
public static final int ERR_MOVE_NOT_ENOUGH_MOVE_POINTS = 2;
public static final int ERR_MOVE_ILLEGAL_MOB_STATE = 3;//for future
public static void move(Mobile mob, int direction, boolean affectMoves) {
Located mobLocated = mob.asLocated();
Room fromSpace = (Room) mobLocated.getLocation().getRole(Room.class);
if (fromSpace == null) { // not Room -> can't move
throw new LogicException(ERR_MOVE_NO_EXITS_FOUND, "Not a linked space");
}
Exit exit = fromSpace.getExit(direction);
if (exit == null) { // no exit found by direction
throw new LogicException(ERR_MOVE_NO_EXITS_FOUND, "Exit not found");
}
// place to call onBeforeMoveDone triggers
if (affectMoves) {
int moveCost = exit.getMoveCost();
int movesPoints = mob.getMoves();
if (moveCost > movesPoints) {
throw new LogicException(ERR_MOVE_NOT_ENOUGH_MOVE_POINTS, "Not enough move points");
}
mob.setMoves(movesPoints - moveCost);
}
final Room toSpace = exit.getTargetRoom();
RelocateFn.relocate(mobLocated, toSpace.asSpace());
// place to call onAfterMoveDone triggers
triggerMovedIn(mob, fromSpace, toSpace);
}
private static void triggerMovedIn(Mobile mob, Room srcSpace, Room dstSpace) {
for (Iterator it = dstSpace.getTriggersByEventType(TriggerType.TRIGGER_AFTER_MOVED_IN); it.hasNext();) {
try {
AfterMovedInTrigger t = (AfterMovedInTrigger )TriggerFactory.provideTrigger((TriggerData) it.next());
t.onAfterMoveIn(mob, srcSpace);
} catch (Exception e) {
Log.error(e);
}
}
}
}