####CORE#### ############ ##NOTE : #### # #The Core Calculates the probability the Attacker wins. #Unless flag is defined. In which case the defender probability is calculated. # #Core :: Does the Core Probability Computation sub core { my ($D, $A, $T, $HD, $HA, $flag) = @_; #Def. Attack. Terrain. HitDef. HitAtt. ($HD, $HA) = ($HA, $HD) if $flag; #For Defender [$flag] Switch HP in neg_bin; #E[P] P = DT/A * Uniform(0,1) or flipped otherwise. my $p = (($A > $D*$T) ? $D*$T/($A*2) : $A/ ($D*$T* 2)); $p = 1 - $p if ((!$flag && $A>$D*$T) || ($flag && $A<=$D*$T)); #If Attacker, and A>DT. Or Defender and DT>A print "Probability of individual hp conflict being won by ".(($flag) ? "Defender" : "Attacker")." is $p\n"; return &neg_bin($p, $HD, $HD + $HA - 1); } #Core::Neg_Binomial -- The Neg. Bin. distribution at the very core of the calc. #Neg Binomial of X (r,p) [Frequency Function] = Sum { [x-1 C r- 1] * p^r * (1-p) ^ (x-r) } # sub neg_bin { my ($p, $r, $limit)=@_; my $float = 6;my $freq=0; my (@Sum,$chr); for (my $x=$r; $x<=$limit;$x++) { $freq = &choose($x-1, $r-1) * ($p ** ($r)) * ((1- $p)**($x - $r))*100; #Freq function $freq = sprintf( "%.".$float."g" , $freq); #g rounds to maximum # of $float #'s in $freq push (@Sum, $freq); } return (&add(@Sum), \@Sum); } #Core::Choose -- Combination. Nothing Special. sub choose { return &pi($_[0]-$_[1]+1, $_[0])/(&pi(1, $_[1])); #Product (k-n+1 to n) / (k!) } #Core::Pi (Product Counter) sub pi { ### NOTE #### The counting inclues the term specified #### my $pi=1; for ($_[0]..$_[1]) {$pi*=$_;} #Normal factorial would be factorial(1,n); return $pi; } #Core::Add --- ???? sub add { my $sum; $sum+=$_ foreach (@_); return $sum; } 1;