#!/usr/bin/perl 
# This should be stackless so anything can call it. Which is 100% safe.
#use Mooix::Thing;
run sub {
	my $this=shift;
	
	my $orig = $this->hitpoints;

	# The dead don't heal.
	return if $orig <= $this->minhitpoints;
	
	my $mtime = (stat($this->fieldfile('hitpoints')))[9];
	my $modifier = int((time - $mtime) / $this->hp_regen_rate);

	my $max = $this->maxhitpoints;

	if ($modifier != 0 && $orig < $max) {
		my $new = $orig + $modifier;
		$new = $max if $new > $max;
		$this->hitpoints($new);

		# No longer unconscious.
		if ($orig <= 0 && $new > 0) {
			$this->recover;
		}
	}
}
