key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
*/

package key.commands;

import key.*;

import java.io.*;
import java.util.StringTokenizer;
import java.util.Hashtable;
import java.util.Enumeration;

public class Travel extends Command
{
	public static final AtomicElement[] ELEMENTS =
	{
		AtomicElement.construct( Trans.class, String.class, "leave",
			AtomicElement.PUBLIC_FIELD,
			"the message sent to the room when a player leaves" ),
		AtomicElement.construct( Trans.class, String.class, "enter",
			AtomicElement.PUBLIC_FIELD,
			"the message sent to the room when a player enters" )
	};

	public static final AtomicStructure STRUCTURE = new AtomicStructure( Command.STRUCTURE, ELEMENTS );
	
	public static final char playerCode = 'o';
	
	public String enter = "A shimmering rift appears before you as %o steps out.";
	public String leave = "A shimmering rift appears before you as %o steps in.";
	
	public Travel()
	{
		setKey( "travel" );
		usage = "<location>";
	}
	
	public AtomicStructure getDeclaredStructure()
	{
		return( STRUCTURE );
	}
	
	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		if( !args.hasMoreTokens() )
			usage( ic );
		else
		{
			String id = args.nextToken();
			Object l = null;
			
			if( id.indexOf( "/" ) == -1 )
			{
				l = new Search( id, p.getContext() ).result;

				if( l == null )
				{
					ic.sendError( "Could not locate '" + id + "' in " + p.getContext().getId() );
					return;
				}
			}
			else
			{
				l = new Search( id, Key.shortcuts() ).result;
				
				if( l == null )
				{
					ic.sendError( "Could not locate '" + id + "'" );
					return;
				}
			}
			
			if( l instanceof Room )
			{
				Room wanted = (Room) l;
				Room current = p.getLocation();
				
					//  we're nice: if there is an exit
					//  to this room in the current room,
					//  we can use that instead.
				{
					Reference r = wanted.getThis();
					
					for( Enumeration e = current.elements(); e.hasMoreElements(); )
					{
						Object o = e.nextElement();
						
						if( o instanceof Exit )
						{
							Exit x = ((Exit)o);
							
							if( x.to.equals( r ) )
							{
								x.use( p, args, ic, flags, null );
								return;
							}
						}
					}
				}
				
					//  set the codes
				p.putCode( playerCode, p.getName() );
				
					//  substitute name for %o
				String e;
				String lm;
				
				p.putCode( playerCode, p.getName() );
				
				e = Grammar.substitute( enter, p.getCodes() );
				lm = Grammar.substitute( leave, p.getCodes() );
				
				Effect ee = new key.effect.Enter( p, wanted, e );
				Effect le = null;
				
				if( current != null )
					le = new key.effect.Leave( p, current, lm );
				
				p.moveTo( wanted, le, ee );
				
					//  show them where they are
				p.roomLook();
				
					//  tell them
				ic.sendFeedback( "You have travelled to " + ( (Room) l).getId() + "." );
			}
			else if( l instanceof Atom )
				ic.sendError( "'" + ((Atom)l).getId() + "' is not a room." );
			else
				ic.sendError( "Object of type " + Type.typeOf( l ).getName() + " is not a room" );
		}
	}
}