/
area/
classes/net/sourceforge/pain/logic/
classes/net/sourceforge/pain/logic/event/
classes/net/sourceforge/pain/logic/fn/util/
classes/net/sourceforge/pain/network/console/
classes/net/sourceforge/pain/plugin/
classes/net/sourceforge/pain/plugin/reset/
classes/net/sourceforge/pain/plugin/shutdown/
classes/net/sourceforge/pain/plugin/social/
classest/net/sourceforge/pain/db/data/
doc/
doc/paindb/resources/
src/net/sourceforge/pain/logic/
src/net/sourceforge/pain/logic/event/
src/net/sourceforge/pain/logic/fn/util/
src/net/sourceforge/pain/network/console/
src/net/sourceforge/pain/network/console/telnet/
src/net/sourceforge/pain/plugin/
src/net/sourceforge/pain/plugin/command/
src/net/sourceforge/pain/plugin/reset/
src/net/sourceforge/pain/plugin/shutdown/
src/net/sourceforge/pain/plugin/social/
src/net/sourceforge/pain/util/
tests/
tests/net/sourceforge/pain/db/data/
package net.sourceforge.pain.logic.event.console.command;

import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.logic.event.console.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.logic.fn.util.*;
import net.sourceforge.pain.util.*;


public final class Scan extends CommandHandler {

	private final static int range = 4;

	public boolean isAccessible() {
	    return super.isAccessible() && player.asReceptive().canSee();
	}

	public void processNotAccessible() {
		MessageOutFn.outln(console, "You can't see anything!");
	}

	public void processCommand() throws Exception {
		int dir = -1;
		String dirStr = commandParams;
		if (dirStr != null) {
			dirStr = dirStr.toLowerCase().trim();
			if ("north".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_NORTH;
			} else if ("east".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_EAST;
			} else if ("south".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_SOUTH;
			} else if ("west".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_WEST;
			} else if ("up".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_UP;
			} else if ("down".startsWith(dirStr)) {
				dir = LinkedSpace.DIR_DOWN;
			} else {
				MessageOutFn.outln(console, "Wrong direction.");
			}
		}
		if (dir == -1) {
			lookAround();
		} else {
			lookDir(dir);
		}
	}

	private void lookDir(int dir) {
		MessageOutFn.outSpace(player, "$n scans $T.", player, LangUtil.exitName[dir]);
		MessageOutFn.outOne(player, "You scan $t.", LangUtil.exitName[dir]);
		Space space = player.getLocation();
		LinkedSpace link = (LinkedSpace) space.getRole(LinkedSpace.class);
		if (link == null) {
			return;
		}

		for (int i = 1; i <= range; i++) {
			Exit exit = link.getExit(dir);
			if (exit == null) {
				break;
			}
			space = exit.getTargetSpace();
			if (space == null) {
			    Log.debug("Error: exit without space!");
				break;
			}
			boolean rangeTitleShown = false;
			for (Located obj = space.getFirstInSpace(); obj != null; obj = obj.getNextInSpace()) {
				Interactive interactive = (Interactive) obj.getRole(Interactive.class);
				if (interactive == null) {
					continue;
				}
				if (!rangeTitleShown) {
					MessageOutFn.outln(console, "***** {cRange " + i + "{x *****");
					rangeTitleShown = true;
				}
				MessageOutFn.outln(console, interactive.getName());
			}
			link = (LinkedSpace) space.getRole(LinkedSpace.class);
			if (link == null) {
				break;
			}
		}
	}

	private void lookAround() {
		MessageOutFn.outSpace(player, "$n looks all around.", player, null);
		MessageOutFn.outln(console, "Looking around you see:");
		MessageOutFn.outln(console, "{Chere{x:");
		Space space = player.getLocation();
		scanSpace(space);

		LinkedSpace link = (LinkedSpace) space.getRole(LinkedSpace.class);
		if (link == null) {
			return;
		}

		for (int dir = LinkedSpace.DIR_NORTH; dir <= LinkedSpace.DIR_DOWN; dir++) {
			Exit exit = link.getExit(dir);
			if (exit == null) {
				continue;
			}
			space = exit.getTargetSpace();
			MessageOutFn.outln(console, "{C" + LangUtil.exitName[dir] + "{x:");
			scanSpace(space);
		}
	}

	private void scanSpace(Space space) {
		Interactive pli = player.asInteractive();
		for (Located obj = space.getFirstInSpace(); obj != null; obj = obj.getNextInSpace()) {
			Interactive i = (Interactive) obj.getRole(Interactive.class);
			if (i == null) {
				continue;
			}
			if (i != pli) {
				MessageOutFn.outln(console, "    " + i.getName());
			}
		}
	}

	public void showHelp() {
		MessageOutFn.outln(console, command.name+": scans the specified direction.");
	}

}