/
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.builder;

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

import java.util.*;

/**
 * PAiN  Date: 05.06.2003  Time: 1:40:02
 */
public final class BC_Link extends BuilderCommand {

	public void processBuilderCommand(BuilderShell p, String args) throws Exception {
		boolean failed = true;
		final Space space = p.player.asLocated().getLocation();
		LinkedSpace link = (LinkedSpace) space.getRole(LinkedSpace.class);
		if (args != null) {
			StringTokenizer st = new StringTokenizer(args, " ");
			final String directionStr = st.nextToken();
			if (directionStr.length() != 1) {
				MessageOutFn.outln(p.console, "ERROR:Illegal direction:" + directionStr);
				return;
			}
			final int dir;
			try {
				dir = Utils.exitCharToDir(directionStr.charAt(0));
			} catch (Exception e) {
				MessageOutFn.outln(p.console, "ERROR:Illegal direction:" + directionStr);
				return;
			}
			Exit e = link == null ? null : link.getExit(dir);
			if (e != null) {
				MessageOutFn.outln(p.console, "ERROR:Direction is busy:" + e.getTargetSpace().getName());
				return;
			}
			boolean single = false;
			if (st.hasMoreTokens()) {
				String uniqueName = null;
				String token = st.nextToken();
				if ("SINGLE".equalsIgnoreCase(token)) {
					single = true;
					if (st.hasMoreTokens()) {
						uniqueName = st.nextToken("\n").trim();
					}
				} else {
					uniqueName = token;
				}
				if (uniqueName != null) {
					IndexedSpace targetIndexedSpace = Core.getWorld().getIndexedSpacesRegistry().getSpace(uniqueName);
					if (targetIndexedSpace == null) {
						MessageOutFn.outln(p.console, "ERROR: no named space found with name:" + uniqueName);
						return;
					}
					LinkedSpace targetSixWayLink = single ? null : (LinkedSpace) targetIndexedSpace.getRole(LinkedSpace.class);
					int reverseDir = LinkedSpace.reverseDir[dir];
					if (targetSixWayLink != null && targetSixWayLink.getExit(reverseDir) != null) {
						MessageOutFn.outln(p.console, "ERROR: reverse dir is used, use SINGLE option to create single way link");
						return;
					}
					// ok, creating!
					failed = false;
					link(p.console, space, dir, targetIndexedSpace.asSpace());
					if (!single) {
						link(p.console, targetIndexedSpace.asSpace(), reverseDir, space);
					}


				}
			}

		}
		if (failed) {
			showUsage(p.console);
		}
	}

	private static void link(Console console, Space space, int dir, Space targetSpace) throws Exception {
		MessageOutFn.outln(console, "Creating link from {Y" + space.getName() + "{x direction:{C" + LangUtil.exitName[dir] + "{x target space:{Y" + targetSpace.getName() + "{x");
		LinkedSpace link = (LinkedSpace) space.getRole(LinkedSpace.class);
		if (link == null) {
			link = (LinkedSpace) space.addRole(LinkedSpace.class);
		}
		Exit e = (Exit) ObjectFactory.create(Exit.class);
		e.setTargetSpace(targetSpace);
		link.setExit(dir, e);
	}


	public void showUsage(Console console) {
		MessageOutFn.outln(console, "LINK:Command syntax <DIRECTION> [SINGLE] <UNIQUE_ROOM_ID>\n");
	}

	public void showHelp(Console console) {
		MessageOutFn.outln(console, "Builder command LINK creates exit from current space to other.");
		MessageOutFn.outln(console, "Usage: link <DIRECTION> [SINGLE] <UNIQUE_ROOM_ID>");
		MessageOutFn.outln(console, "{wDIRECTION{x is a letter from (N,E,S,W,U,D)");
		MessageOutFn.outln(console, "{wSINGLE{x is optional param. Used when single way link should be created");
		MessageOutFn.outln(console, "{wUNIQUE_ROOM_ID{x is unique name of the target space");
		MessageOutFn.outln(console, "NOTE: LinkedSpace will be added to current or target space if needed");
	}

}