#!/usr/bin/perl
no warnings; # in case of non-numeric comparisons
$SIG{INT} = sub { exit 1; };
$SIG{TERM} = sub { exit 1; };
srand;
my $strength = 0;
my $enemy = 1 + int(rand 20);
my $crushed = 0;
if (-e $ENV{HOME} . "/.soy") {
	open(SOY, $ENV{HOME} . "/.soy");
	chomp($strength = (<SOY> + 0 || 0));
	chomp($enemy = (<SOY> + 0 || 0));
	chomp($crushed = (<SOY> + 0 || 0));
	close SOY;
}
my $op = $ARGV[0] || "";
if (($op eq "-e") || ($op eq "--eat")) {
	if (rand(30) < 1) {
		if (rand(30) < 1) {
			$strength += 500 + int(rand 1000);
			print "You receive INCREDIBLE SOY POWERS raising your strength to $strength!\n";
		} else {
			$strength += 20 + int(rand 80);
			print "The AWESOME power of SOY! raises your strength to $strength!\n";
		}
	} else {
		$strength += 1 + int(rand 3);
		print "By the power of SOY! you raise your strength to $strength!\n";
	}

} elsif (($op eq "-c") || ($op eq "--crush")) {
	if ($strength >= $enemy) {
		my $i = 1;
		$strength -= $enemy;
		$enemy = 1 + int(rand 20);
		
		if (($strength >= $enemy) && (rand 30 < 1)) {
			while ($strength >= $enemy) {
				$i++;
				$strength -= $enemy;
				$enemy = 1 + int(rand 20);
			}
			print "With an ALMIGHTY blow you crush $i enemies!\n";
		} else {
			print "You smite your enemy with a crushing blow!\n";
		}
		$crushed += $i;
		print "You have now crushed a total of $crushed " . (($crushed == 1) ? "enemy" : "enemies") . ".\n";
	} else {
		print "Your strength is not enough. Try again.\n";
	}
} elsif (($op eq "-s") || ($op eq "--status")) {
	print "Your strength is $strength\n";
	print "Your enemy's strength is $enemy\n";
	print "You have crushed a total of $crushed " . (($crushed == 1) ? "enemy" : "enemies") . "\n";
} else {
	my $me = $0;
	$me =~ s#.*/##;
	print <<END;
SOY! SOY! SOY! SOY makes you strong! Strength crushes enemies! SOY!
Usage: $me <option>
	Options:
		-e --eat
			Eat SOY!
		-c --crush
			Crush enemies!
		-s --status
			Status
END
}
open(SOY, ">", $ENV{HOME} . "/.soy");
print SOY "$strength\n$enemy\n$crushed\n";
close SOY;
