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

sub addadjective {
	my $this=shift;
	my $adjective=shift;
	my @adjectives=$this->adjective;
	if (! grep { $adjective eq $_ } @adjectives) {
		$this->adjective(@adjectives, $adjective);
	}
}

run sub {
	my $this=shift;
	my $damage=shift;
	
	return if $this->unbreakable;
	
	my $hitpoints = $this->hitpoints($this->hitpoints - $damage);

	if ($hitpoints > 0) {
		#the object is only partially broken
		$this->cracked(1);
		$this->msg('crack', %_);
		addadjective($this, "cracked");
	}
	else {
		# The object is totally broken
		if (! $this->break_destroy ) {
			# If break_destroy unset, break the object
			$this->msg('break', %_);
			my $name=$this->name;
			# Add old name to aliases.
			my @aliases=$this->alias;
			if (! grep { $name eq $_ } @aliases) {
				$this->alias(@aliases, $name);
			}
			if ($this->visible_break) {
				$this->name("broken ".$this->name);
			}
			addadjective($this, "broken");
		}
		else {
			# If break_destroy set, the object is destroyed
			# when it breaks.
			$this->destroy(@_);
		}
	}
	return;
}
