#!/usr/bin/perl
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;
	%_=@_;
	
	# Make sure that this command is not spoofed, just in case.
        if ($_{avatar} != $this) {
		fail "No!"; 
	}
	
	# This verb can be called several ways; if an indirect object is
	# omiitted it is a self teleport to the direct object.
	my ($object, $destination, $preposition);
	if ($_{do_preposition} eq 'away' || $_{do_preposition} eq 'out') {
		$object = $_{direct_object};
		$destination = undef;
		$preposition = undef;
	}
	elsif ($_{do_preposition} eq 'in') {
		$object = $_{direct_object};
		$destination=$this->location;
		$preposition = $_{do_preposition};
	}
	elsif ($_{indirect_object}) {
		$object = $_{direct_object};
		$destination = $_{indirect_object};
		$preposition = $_{io_preposition};
	}
	else {
		$object = $this;
		$destination = $_{direct_object};
	}
	my $oldloc = $object->location;

	# Teleport to an exit goes to the exit's destination.
	if ($destination && $destination->isa($Mooix::Root->concrete->exit)) {
		$destination=$destination->destination;
	}
	
	# TODO some rooms may want to disallow teleports into/out of them.
	
	if ($oldloc == $destination) {
		if ($object == $this) {
			fail "You're already there!";
		}
		else {
			fail ucfirst $object->gender_subject_pronoun."'s already there!";
		}
	}

	# Note the use of the teleport flag, to let move know that even
	# normally immobile objects should be 'ported, if possible.
	$object->msg('teleport', %_, (destination => $destination ? $destination : 'limbo'));
	if ($object->physics->move(object => $object, to => $destination, teleport => 1, preposition => $preposition)) {
		$oldloc->msg('teleport_leave', %_, originator => $object)
			if $oldloc;
		$destination->msg('teleport_arrive', %_, skip => $object, originator => $object)
			if $destination;
	}
	else {
		fail "You can't do that.";
	}
}
