#!/usr/bin/perl
#use Fcntl q{:flock};
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;
	%_=@_;
	my $avatar=$_{avatar};

	my $leave='leave';

	# An exit can only be used if it is in an exit_ok room, or if it is
	# owned by the same person who owns its room. However, the owner of
	# an exit, and the owner of the room it's in, and the admin, can
	# always use it.
	my $toproom = $this->location;
	my $location;
	while ($location = $toproom->location) {
		$toproom = $location;
	}
	if ($this->owner != $toproom->owner && ! $toproom->exit_ok) {
		if ($this->owner == $avatar ||
		    $avatar == $Mooix::Root->system->admin ||
	    	    $toproom->owner == $avatar) {
		    	# Use a different message.
			$leave = "leave_not_exit_ok";
		}
		else {
			fail "You can't go that way.";
		}
	}
	
	my %substs=(
		%_,
		avatar => $avatar,
		destination => $this->destination,
	);
	
	# lock the avatar for movement
	my $lock = $avatar->getlock(LOCK_EX) unless $_{avatar_is_locked};
	$this->msg($leave, %_);
	if ($avatar->physics->move(object => $avatar, to => $this->destination)) {
		$this->destination->msg('arrive', %_,
			originator => $this,
			skip => $avatar,
		);
	}
	else {
		$this->msg('leave_fail', %_);
		fail "You can't go that way.";
	}
}
