#!/usr/bin/perl
#use Mooix::Thing;

# Does it like this food?
sub like {
	my $this=shift;
	my $food=shift;

	foreach my $obj ($this->consumables) {
		return 1 if $food->isa($obj);
	}
	return 0;
}

run sub {
	my $this=shift;
	%_=@_;

	my $food = ($_{direct_object} == $this) ? 
		$_{indirect_object} : $_{direct_object};
	
	if ($food == $_{avatar} || $food == $this) {
		fail "Surely you're kidding!";
	}
		
	if (! $food) {
		# Well, try to find some food being held by the avatar.
		foreach my $obj ($_{avatar}->contents->list) {
			next if $obj == $this;
			if (like($this, $obj)) {
				$food=$obj;
				last;
			}
		}
		if (! $food) {
			fail "Feed it what?";
			return;
		}
	}
	else {
		if (! like($this, $food)) {
			$this->msg("feed_ignore", food => $food, %_);
			return;
		}
	}
	
	# Avatar must pick up the food.
	if ($food->location != $_{avatar}) {
		$food->take_verb(avatar => $_{avatar}, 
			direct_object => $food, session => $_{session});
		if ($food->location != $_{avatar}) {
			fail "You're not holding that.";
		}
	}

	$this->msg("feed", food => $food, %_);
	# Call the consume_verb to eat it. Note use of special notake
	# parameter to prevent it from picking up the food.
	$food->consume_verb(avatar => $this, direct_object => $food, 
		notake => 1);
}
