# Quest, a computer role playing game system # Copyright (C) 2002 Willem Robert van Hage, details in the COPYING file. package misc; use strict; use tree; use Data::Dumper; $Data::Dumper::Indent = 1; sub random { my ($min,$max) = @_; return int(rand($max-$min+1))+$min } sub evaluate { my ($player,$context,$str) = @_; my @tokens = split(" ",$str); foreach (@tokens) { if (/\/?player((\/+[a-z*]+)*)/) { $_ = tree::query($player,$1); return unless $_; return $_ if ref($_) } if (/\/?self((\/+[a-z*]+)*)/) { $_ = tree::query($context,$1); return unless $_; return $_ if ref($_) } $_ = random($1,$2) if /\[(\d+)~(\d+)\]/; } return eval join(" ",@tokens) } sub condition { my ($pl,$context,$cond) = @_; my $rv = 1; my $msg = ""; return ($rv,$msg) unless $cond; if ($cond->{bool}) { foreach (@{$cond->{bool}}) { my $value = evaluate($pl,$context,$_); $rv = 0, $msg .= xml::display("$_ is false") unless $value } } if ($cond->{check}) { foreach (@{$cond->{check}}) { my $roll = rand(20)+1; my $value = evaluate($pl,$context,$_); $rv = 0, $msg .= xml::display("$_ < $roll") unless $roll >= $value } } if ($cond->{exists}) { foreach (@{$cond->{exists}}) { my $value = defined evaluate($pl,$context,$_); $rv = 0, $msg .= xml::display("$_ doesn't exist") unless $value } } if ($cond->{then} and $rv) { foreach (@{$cond->{then}}) { if ($_->{display}) { $msg .= xml::display("$_->{display}") } } } if ($cond->{else} and not $rv) { foreach (@{$cond->{else}}) { if ($_->{display}) { $msg .= xml::display("$_->{display}") } } } return ($rv,$msg) } 1;