#! /usr/bin/perl
print "Content-type: text/html\n\n";

if ($ENV{"REQUEST_METHOD"} eq "POST") {
  read(STDIN, $datastring, $ENV{"CONTENT_LENGTH"});		
}
elsif (exists $ENV{"REQUEST_METHOD"}) {		# data from GET transaction (or HEAD or other)
  $datastring = $ENV{"QUERY_STRING"};
}
else {
  print "Offline execution detected\n";
  print "Please enter some data.\n";
  $datastring = <>;
  chomp $datastring;
  print "== data accepted == HTML output follows ==\n\n";
}

###decode######################################################
$datastring =~s/%0D%0A/\n/g;                    		#step to deal with line
																											#breaks in text areas
@nameValuePairs = split(/&/, $datastring);			#step 1
foreach $pair (@nameValuePairs) {
  ($name, $value) = split(/=/, $pair);				#step 2
  
  $name =~tr/+/ /;                                 		#step 3
  $name =~s/%([\da-fA-F]{2})/pack("C",hex($1))/eg; 		#step 3
  $value =~tr/+/ /;                                		#step 3
  $value =~s/%([\da-fA-F]{2})/pack("C",hex($1))/eg;		#step 3
  
  if(exists $formHash{$name}) {					#improved step 4,
    $formHash{$name} = $formHash{$name}.";".$value;		#now handles multiple
  }																										#select menus
  else {
    $formHash{$name} = $value;
  }   	
}

 if($formHash{psquad} eq "squad"){$formHash{p1}=$formHash{squadp1};}
 else{$formHash{p1}=$formHash{herop1};}
 if($formHash{qsquad} eq "squad"){$formHash{q1}=$formHash{squadq1};}
 else{$formHash{q1}=$formHash{heroq1};}

###done decoding###############################################

$note="";
$note=qq^<b><font color="red">Beta Testing Version 6.31 with Wave 13 (D3) figures.
Adding figures as they are revealed in previews.
Please report any errors or suspicious behavior.</font></b>^;

$note2=qq^<b><font color="red">Do not use yet - Alpha Testing Version 6.10 with Wave 12 (D2) figures.
Not yet programmed: Psionic Blast and all the Wyrmling powers.
</font></b>^;

#$note="";

$title=qq^$note<center><big><b>Heroscape One-on-one matchup calculator (Version 6.31, Sep 22, 2010)</b></big></center>
Using math (not simulations), this calculates the exact win probabilities
and average length of battle in number of attacks (which is the same as number of half turns
in the absence of multiple attacks, frenzy, or squads).
The average survivors is the average number of life or figures left assuming that the 
figure or squad wins.
<br>
<a href="$ENV{SCRIPT_NAME}">[Back to intro page]</a>
<a href="$ENV{SCRIPT_NAME}?request=aboutthis" target="newwindow">[About this program]</a>
<a href="$ENV{SCRIPT_NAME}?request=instructions"  target="newwindow">[Detailed instructions]</a>
<a href="$ENV{SCRIPT_NAME}?request=todo"  target="newwindow">[To do list]</a>
<a href="$ENV{SCRIPT_NAME}?request=todo#new"  target="newwindow">[What's new]</a>
<a href="$ENV{SCRIPT_NAME}?request=stats"  target="newwindow">[Access stats]</a>
<br>
<br>
Version 6.20: Added more choices to "Squad size" to include squads of more than 1 common heroes, which is essentially what you get when you use Wyrmling Bonding or Master of the Elements. This only makes a difference for powers that distinguish squad figures from common heros such as Chomp, Poison Acid Breath, Stare of Stone.
Version 6.11: Modified Searing Intensity. 
Version 6.10: Added implementable abilities from D12: the fledgling abilities and Psionic Blast.  Also fixed a typo that would have affected results of ChillingTouch and Trample and made them worse than they should be.  All Chilling Touch, Throw, and Trample numbers before Aug 28, 2010 should be redone.
Version 6.00: New interface improvements, including mouseover activating balloons that describe the abilities.


<br>
<b>Remember, it is up to the user to use this calculator wisely.</b>
This calculator does not take into account of range or movement.
So all battles are either melee vs melee or range vs range.
A better way of thinking of it is you are assuming both sides get to use
the attacks and abilities that you give them.
You must interpret the results appropriately.
Furthermore, it doesn't make sense to matchup certain abilities.
For example, the Ullar Rifle special is only good for ranged duels, so it doesn't make sense to
match that against say melee figures.
And it is up to the user to remember that certain abilities (example: smoke powder, counterstrike, disappearing ninja, tough, etc.) work only against <b>normal</b> attacks.
Remember also, that in addition to using official figures, feel free to customize your own figures (within reason!).
<hr>\n^;


$maxLockFile = 150; # 2.5 minutes max for running a Mathematica calculation.

$color1="#33ff33"; $color2="#ff5550";
$gray="#d0d0d0";

$dataDir = ""; 	
$lockfile=$dataDir."mathlock";
$mafile=$dataDir."mathinput";
$outputfile=$dataDir."mathoutput.ma";
$donefile=$dataDir."mathdone";
$datafile=$dataDir."datafile.txt";

$star=qq^<a href="$ENV{SCRIPT_NAME}?request=instructions"  target="newwindow"> * </a>^;

sub mystar{  ##This will be later when we figure out how to do anchor display when returned as a CGI page
  my ($anchor)=@_;
  return qq^<a href="$ENV{SCRIPT_NAME}?request=instructions\#1"  target="newwindow"> * </a>^;
}

### This is an array of the web displayed names
### and names used in the Mathematica program, in pairs.
### The displayed name may be changed without affecting the Mathematica program.
$nyi=' <font color="red">[not done]</font>';

sub trim{
  my ($x)=@_;
  $x=~s/^\s+//;
  $x=~s/\s+$//;
  return $x;
}

@Abilities=();
@AbilitiesMa=();
@AbilitiesDesc=();
@AbilitiesDescJS=();
%AbilityToN=();
if(open(ABIL, "<abilities.txt")){
  @all=<ABIL>;
  close(ABIL);
  chomp @all;chomp @all;
  $all=join(" ",@all);
  $all=~s/\n/ /g;
  @all=split(/\#\#/, $all);
  $ctr=3;
  for($n=0;$n<@all;$n++){
    $x=$all[$n];
    if($x !~ /\-\-\-\-/){
    ($i, $ma, $a, $desc) = split(/\@/, $x);
    $ctr++;
    if($ctr!=$i){
      &errorPage("ERROR in reading abilities.txt file (unmatched ctr=$ctr, i=$i).");
    }
    push @Abilities, trim($a);
    push @AbilitiesMa, trim($ma);
    $desc=trim($desc);
    push @AbilitiesDesc, $desc;
    $desc=~s/\'/\\\'/g;
    $desc=~s/\"/\\\'/g;
    push @AbilitiesDescJS, $desc;
    $AbilityToN{$ma}=$ctr;   
   }else{
     $n=$#all+10;
   }
  }
}

&setupAbilitiesParrallelArray;
###check consistency with old method###
for($i=0;$i<@AbilitiesOLD;$i++){
 if($AbilitiesMa[$i] ne $AbilitiesMaOLD[$i]){
   $ii=$i+4;
   errorPage("abma:$ii, new:$AbilitiesMa[$i], old:$AbilitiesMaOLD[$i].");
     #.qq^
     #<br>^.join("/",@AbilitiesMa)."<br>".join("/",@AbilitiesMaOLD).
     #"<br>");
 }
}




########### The following @AbilitiesParrallelArray is no longer used ##########
sub setupAbilitiesParrallelArray{
@AbilitiesParrallelArray=(
"Deadly Strike",  'DeadlyStrike',  #4
"Double Shield",  'DoubleShield',  #5
"Double Attack",  'DoubleAttack',  #6
"Counterstrike / Evil Eye / Arcane Riposte",  'CounterStrike', #7
"Wounded Smash",'WoundedAttack',  #8
"Frenzy 16",  'Frenzy',               #9
"Stealth Dodge", 'StealthDodge',    #10
"Mind Shackle 20",  'MindShackle',    #11
"Chomp 0/16           ",  'Chomp',     #12
"Unleashed Fury", 'UnleashedFury', #13
"Paralyzing Stare 16",'ParalyzingGaze',#14
"Crippling Gaze 15", 'CripplingGaze', #15
"Rapid Fire",   'RapidFire',       #16
"Wait Then Fire (see implemntation$star)",  'WaitAndFire',   #17
"Hive Swarm",  'HiveSwarm',        #18
"Zettian Targetting",  'ZettianTargetting',    #19
"Shocking Grasp",  'BonusSkull',    #20
"Tough",  'BonusShield',    #21
"Blood Hungry",  'BloodHungry'  ,    #22
"Stealth Armor 15",  'StealthArmor',    #23
"Shield Wall (modified$star)",  'ShieldWall'    #24
,"Rod of Negation 16",  'RodOfNegation'    #25
,"Marro Plague 16 (simplified$star)",  'MarroPlague'    #26
,"Shaolin Assault (simplified$star) / Whirlwind Attack",  'ShaolinAttack'    #27
,"Whip 12",  'Whip'    #28
,"Stare of Stone 7/17",  'StareOfStone'    #29
,"Sharpshooter 19",  'Sharpshooter'    #30
,"Ullar Rifle",  'UllarRifle'    #31
,"Blood Lust",  'BloodLust'    #32
,"Poison Acid Breath 8/17",  'PoisonAcidBreath'    #33
,"Ice Shard Breath / Shield Throw",  'IceShardBreath'    #34
,"Throw 14",  'Throw14'    #35
,"Gueglix Gun 333",  'Gueglix333'    #36
,"Gueglix Gun 22221",  'Gueglix22221'    #37
,"Gueglix Gun 1x9",  'Gueglix1x9'    #38
,"Gueglix Gun 2223",  'Gueglix2223'    #39
,"Rod of Negation & no attack until negated",  'RodOfNegationVSWS'    #40
,"Arrow Volley / Zombie Onslaught / Encircle",  'ArrowVolley'    #41
,"Blind Rage",  'BlindRage'    #42
,"Stab in the Back",  'StabInTheBack'    #43
,"Maul / Venomous Sting",  'Maul'    #44
,"Disappearing Ninja 12",  'DisappearingNinja'    #45
,"Net Trip 14",  'NetTrip14'    #46
,"One Shield Defense",  'OneShieldDefense'    #47
,"Tough x2",  'Toughx2'    #48
,"Smoke Powder 13",  'SmokePowder'    #49
,"Valkyrie Dice Attack Bonus",  'AttackAura46'    #50
,"Valkyrie Dice Defense Bonus",  'DefenseAura36'    #51
,"Leaf of Home Tree Bonus",  'HomeTree'    #52
,"AutoLoad",  'AutoLoad'    #53
,"Gift of Empress Bonus",  'DefenseAura59'    #54
,"Soul Devour / Mind Shackle 19",  'MindShackle19'    #55
,"Toxic Skin 17",  'ToxicSkin'    #56
,"Toxic Skin 17 vs melee squad",  'ToxicSkinVsMeleeSquad'    #57
,"Helm of Mitonsul",  'HelmOfMitonsul'    #58
,"Helm of Mitonsul vs melee squad",  'HelmOfMitonsulVsMeleeSquad'    #59
,"Zombies Rise Again / Life Drain",  'ZombiesRiseAgain'    #60
,"Web Attack",  'WebAttack'    #61
,"Spidey Sense 11",  'SpideySense'    #62
,"Spider Sense 14",  'SpiderSense'    #63
,"Dust of Death",  'DustOfDeath'    #64
,"Dust of Death vs melee squad",  'DustOfDeathVsMeleeSquad'    #65
,"Vanish 9",  'Vanish9'    #66
,"Rage Smash 5",  'RageSmash5'    #67
,"Stomp 3  / Earth Slam (vs melee squad$star)",  'Stomp'    #68
,"Eternal Hatred",  'EternalHatred'    #69
,"Chilling Touch",  'ChillingTouch'    #70
,"Chilling Touch vs ranged squad",  'ChillingTouchVsRangedSquad'    #71
,"Trample (simplified$star)",  'Trample'    #72
,"Trample vs ranged squad",  'TrampleVsRangedSquad'    #73
,"Stinger Drain",  'StingerDrain'    #74
,"Concentrated Will",  'ConcentratedWill'    #75
,"Machine Pistol 2222",  'Gueglix2222'    #76
,"Quick Release / Wrist Rocket 44",  'Gueglix44'    #77
,"Dragon Heal",  'DragonHeal'    #78
,"Dragon Swoop",  'DragonSwoop'    #79
,"Cosmic Blast",  'CosmicBlast'    #80
,"Lethal Sting",  'LethalSting'    #81
,"Chalice of Fortitude", 'ChaliceOfFortitude'  #82
,"Coil Crush", 'CoilCrush'                     #83
,"Poison Sting/Venom Ray", 'PoisonSting'                 #84
,"Entangling Web", 'EntanglingWeb'             #85
,"Saber Storm 1x6", 'SaberStorm1x6'            #86
,"Saber Storm 222", 'SaberStorm222'            #87
,"Saber Storm 33", 'SaberStorm33'              #88
,"Majestic Fires", 'MajesticFires'             #89
,"Defense Agility (see note$star)", 'DefenseAgility'                           #90
,"Pounce Vs Range (see note$star)", 'PounceVsRange'                            #91
,"Pounce Vs Melee (see note$star)", 'PounceVsMelee'                            #92
,"Concealment 19 at range 2 (see note$star)", 'StealthArmor17'                            #93
,"Concealment 10 at range 2 (see note$star)", 'StealthArmor8'                           #94
,"Cell Divide 17", 'CellDivide17'               #95
,"Circuitry Overload", 'CircuitryOverload'      #96
,"EMP Response", 'EMPResponse'                  #97
,"Combined Arbalest", 'CombinedArbalest'        #98
,"Ullars Bolt 16", 'UllarsBolt'                 #99
,"Ninjutsu Barrage", 'NinjutsuBarrage'          #100
,"Feral Rage (simplified$star)", 'ShaolinAttack3'          #101
,"Blood Frenzy 3", 'BloodFrenzy3'          #102
,"Regenerate / Cold Healing", 'Regenerate'          #103
,"Poison Weapon 12", 'PoisonWeapon'          #104
,"Hide In Darkness/Shadows 16", 'StealthArmor16'          #105
,"Healing From Ana Karithon (see note$star)", 'HealingFromAna'          #106
,"Cleave", 'Cleave'          #107
,"Fire Blast 2", 'FireBlast2'          #108
,"Fire Blast 3", 'FireBlast3'          #109
,"Fire Blast 4", 'FireBlast4'          #110
,"Fire Blast 2 then 4 strategy (see note$star)", 'FireBlast24'          #111
,"Wraith Attack using 1 drow", 'WraithAttack1drow'          #112
,"Wraith Attack using 2 drow", 'WraithAttack2drow'          #113
,"Wraith Attack using 3 drow", 'WraithAttack3drow'          #114
,"Lurking Ambush / First Assault 3 (see implemntation$star)", 'FirstAssault3'          #115
,"Air Mastery (vs flyers only)", 'AirMastery'          #116
,"Searing Intensity 14 (see implemntation$star)", 'SearingIntensity'          #117
,"Hydra Heads", 'HydraHeads'          #118

,"Cloud Of Darkness", 'CloudOfDarkness'          #119
,"Triple Attack", 'TripleAttack'          #120
,"Engagement Strike 15 vs melee using ranged attack ", 'EngagementStrike15usingRange'          #121
,"Engagement Strike 15 vs melee using melee attack", 'EngagementStrike15usingMelee'          #122

### To be implemented: EngagementStrike15usingRange EngagementStrike15usingRange

);
@AbilitiesOLD=();
@AbilitiesMaOLD=();
%AbilityToNOLD=();
$ctr=4;
for($i=0;$i<@AbilitiesParrallelArray;$i+=2){
 push @AbilitiesOLD, $AbilitiesParrallelArray[$i];
 push @AbilitiesMaOLD, $AbilitiesParrallelArray[$i+1];
 $AbilityToNOLD{$AbilitiesParrallelArray[$i+1]}=$ctr;$ctr++;
}

}

$maxD20Bonus=13;
%D20hash=(
0,0,
1,1,
2,2,
3,3,
4,4,
5,5,
6,6,
7,7,
8,8,
9,-1,
10,-2,
11,-3,
12,-4,
13,-5);

$maxSquadSizeIndex=7;
%squadSizeDisplay=(
0,"---",
1, "1 (common hero)",
2, "2 (squad)",
3, "3 (squad)",
4, "4 (squad)",
5, "2 (common heroes)",
6, "3 (common heroes)",
7, "4 (common heroes)"
);
%squadSize=(
0, 0,
1, 1,
2, 2,
3, 3,
4, 4,
5, 2,
6, 3,
7, 4
);
%IsSquadForPowers=(
0, "False",
1, "False",
2, "True",
3, "True",
4, "True",
5, "False",
6, "False",
7, "False"
);


@ForbiddenPairs=( #### Use forbidden.txt instead!!
 (6,19), (10,21), (17,13), (6,27), (42,44), (42,53), (44,53), (11,55),(51,54), (91,92)
 , (91,91),(97,97),(19,98), (107,108), (107,109),(108,109),
 (13,110),(13,111),(13,112), (17,110),(17,111),(17,112)
);
if(open(FORBIDDEN, "<forbidden.txt")){
  @all=<FORBIDDEN>;
  close(FORBIDDEN);
  chomp @all;
  @ForbiddenPairs=();
  foreach $x (@all){
    ($y, $z) = split(/:/, $x);
    @y = $z =~/\d+/g;
    for($i=1; $i<@y; $i++){
      for($j=0; $j<$i; $j++){
        push @ForbiddenPairs, ($y[$i], $y[$j]);
      }
    }
  }
  ### print "(" . join(",", @ForbiddenPairs) .")\n"; <>;
}
if(open(FORBIDDEN, "<forbiddenOpps.txt")){
  @all=<FORBIDDEN>;
  close(FORBIDDEN);
  chomp @all;
  @ForbiddenPairsOpps=();
  foreach $x (@all){
    ($y, $z) = split(/:/, $x);
    @y = $z =~/\d+/g;
    for($i=1; $i<@y; $i++){
      for($j=0; $j<$i; $j++){
        push @ForbiddenPairsOpps, ($y[$i], $y[$j]);
        push @ForbiddenPairsOpps, ($y[$j], $y[$i]);
      }
    }
  }
  ### print "(" . join(",", @ForbiddenPairsOpps) .")\n"; <>;
}

$hitfile="hitcounter.txt";
$HITCOUNTER = "[hit count not available]";
if(open(HIT, "<$hitfile")){
  $HITCOUNTER=<HIT>;
  $HITCOUNTER++;
  close(HIT);
  if(($HITCOUNTER > 1) &&(open(HITW, ">$hitfile"))){  #because sometimes a zero is read from a busy file!
    print HITW $HITCOUNTER, "\n";
    close(HTW);
  }
  #### print "(" . $HITCOUNTER .")\n"; <>;
}



$emailLink=qq^<a href="mailto:yuen\@lakeforest.edu">mathguy</a>^;
$ender=qq^<hr>\n[<a href="/~yuen">To mathguy's homepage</a>]
 [Email $emailLink]^;

$instructions=qq^

^;
@zName=(); @zUC=(); @zHS=(); @zLife=();
@zMove=(); @zRange=(); @zAtt=(); @zDef=(); @zAbilities=();
@zPoints=();
@zSize=();
@zAttEnh=();
@zDefEnh=();
@zInitEnh=();
@zD20Enh=();
%zNameToIndex=();
if(open(F, "<hs.txt")){
 $line=<F>;
 while( $line && ($line !~ /---begin data---/) ){$line=<F>;}
 while($line=<F>){
 if($line=~/\w/){
  chomp $line;
  @a = split(/\s*,\s*/, $line);
  push @zName, $a[0];
  push @zUC, $a[1];
  push @zHS, $a[2];
  push @zLife, $a[3];
  push @zMove, $a[4];
  push @zRange, $a[5];
  push @zAtt, $a[6];
  push @zDef, $a[7];
  push @zAbilities, $a[8];
  push @zSize, $a[9];
  push @zPoints, $a[10];
  if($a[11] eq ""){$a[11]=0;}push @zAttEnh, $a[11];
  if($a[12] eq ""){$a[12]=0;}push @zDefEnh, $a[12];
  if($a[13] eq ""){$a[13]=0;}push @zInitEnh, $a[13];
  if($a[14] eq ""){$a[14]=0;}push @zD20Enh, $a[14];
  $zNameToIndex{$a[0]}=$#zName;
 }
 }
}

&initializeJS;
print qq^	<html><head><title>Heroscape matchup calculator</title>$js

<style type="text/css">

#dhtmltooltip{
position: absolute;
left: -300px;
width: 150px;
border: 1px solid black;
padding: 2px;
background-color: lightyellow;
visibility: hidden;
z-index: 100;
/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
filter: progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135);
}

#dhtmlpointer{
position:absolute;
left: -300px;
z-index: 101;
visibility: hidden;
}

</style>



</head><body>\n

<script type="text/javascript">

/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="arrow2.gif">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

</script>

^;


### app logic #############################################
if($formHash{"request"} eq "findprob") {
  &findprob("");
}
elsif($formHash{"request"} eq "instructions") {
  &instructions("");
}
elsif($formHash{"request"} eq "todo") {
  &todo("");
}
elsif($formHash{"request"} eq "aboutthis") {
  &aboutthis("");
}
elsif($formHash{"request"} eq "stats") {
  &statspage("");
}
else {
  &default_page("");
}
### end app logic #########################################



#################################################################
sub instructions {
  $message = $_[0]; # an optional message
  my $version4=qq^
<li>Select type of battle.
<ul><li>Simple battle: This uses the stats and abilities as entered and disregards range. 
(In other words, this is the original matchup program.)</li>
<li>Ranged vs melee battle:
This takes into account that the ranged units will often get free attacks on the melee units
as the melee units rush the ranged units.
The RANGE stat of the ranged unit and the MOVE stat of the melee unit are used to calculate
how many turns are required for the melee unit to reach the ranged unit.
Examples:
<ul>
<li>RANGE 6 vs MOVE 5. Then the melee unit requires no extra turns to reach the ranged unit.
In effect, the only advantage the ranged unit has in this example is that it always gets to attack
first.
</li>
<li>RANGE 6 vs MOVE 4. The melee unit will require 2 moves to get to the ranged unit.
That is, a melee unit will not be able to attack on its 1st turn, but will be able to attack on its
2nd turn (unless it has the Berserk ability).
</li>
<li>RANGE 9 vs MOVE 4. The melee unit will require 2 moves to reach the ranged unit.</li>  
<li>RANGE 10 vs MOVE 4. The melee unit will require 3 moves to reach the ranged unit.</li>  
</ul>
</li>
</ul>
</li>
^;
$version4="";

$abils="";
for($n=0;$n<@Abilities;$n++){
  $abils.=qq^<li>
$Abilities[$n]:
$AbilitiesDesc[$n]
</li>
^;
}

  print<<WHOLEPAGE;
    <h2>Instructions</h2>
    <b>$message</b>

For each side of the match up, select each of the following:
<ul>


$version4


<li>
You can use the optional drop-down menu to select an army card.  This will automatically select
all the stat numbers and abilities for that army card.
You do not have to use this;
you can customize your own stats and abilities.
But if chosen, the point box will also display point value.
</li>
<li>Choose Hero or Squad(s)
<ul>
<li>If single Hero, select the number of LIFE points.
</li>

<li>
If Squad or common hero, select the number of figures; each figure
automatically has 1 LIFE point.
If the number of figures exceed the squad size, then select the Squad SIZE also.
Note that a squad of size 1 is interpreted as a common hero;
this allows you to model multiple common heros.
</li>
</ul></li>
If you are doing a single figure with just 1 LIFE, then in general it does not matter
whether you select Hero with 1 LIFE or Squad with 1 UNIT; the
only time this matters is if the opponent has the Chomp, Stare of Stone, or any ability
that distinquishes between hero and squad units.</li>
<li>ATTACK:  select the number of attack dice.  Note that at least one
of the two figures must have at least 1 ATTACK.  (Currently, there are no
official figures with 0 attack.  But you might want to compare say, how
Ne-Gok-Sa would do against Krug if Ne-Gok-Sa only used Mind Shackle
and no regular attack.  Also you can imagine in the future where
a figure with 0 attack might be used to slow down attackers.  In that case,
a relevant piece of data might be: what is the expected number of turns
that this figure can survive?)
NOTE: If a figure has a modified attack of 0, then no attack roll is made.
</li>
<li>DEFENSE: select the number of defense dice.</li>


<li>Then select any combination of abilities.
Note that for programming reasons,
certain ability combinations are not permitted;
you will be warned upon execution if such combinations are selected.</li>


$abils

</ul>

<hr>

<a name="results">
<h3>Meaning of the returned data.</h3>
</a>
<br>
<img src="image/example1.gif"></img>
<ul>
<li>The light blue boxes summarizes the stats and abilities.
The example above shows Krug versus Tagawa samurai with 2 experience markers.
</li>
<li>In the table labeled "Battle begins at the beginning of a round, with an initiative roll",
the assumption is the the initiative rolled at the beginning of a round
to determine who goes first.
In the above example, Krug wins 48.210% of the time and the Samurai win
51.790%.
</li>
<li>The average attacks column gives the average length of a battle
in terms of the number of attacks.
So for single figures with no multiple attacks or frenzies,
this would be the same as the number of half-turns.
In the above example, the battle would last 7.07 attacks.  This is not the same
as number of turns because Krug has double attack and also because the Samurai form
a squad.</li>
<li>The average survivors column gives the average number of LIFE or UNITs left
if that player wins.
If the Krug wins, on average he has 2.5 life left.
If the Samurai win, on average they have 2.0 figures left.
</li>
<li>In the bottom table labeled
"If we specify who goes first and battle begins which round",
the scenarios are broken down according to when
the battle begins and who has initiative.
Note that the initiative is still rerolled at the beginning of subsequent rounds.
There are 6 rows of results that correspond to each scenario
whether the melee begins at turn #1 or #2 or #3 of a round,
and whether player #1 or #2 has the initiative on the very first round.
In the above example, note that Krug does best if the battle begins in the first turn
AND he has initiative (win at 51.20%)
or if the battle begins in the 3rd turn of the round and the enemy has initiative
(win at 61.47%).
Krug has a losing percentage in the other 4 scenarios.
I will leave it up to you to reason out why Krug does best if the battle begins the
3rd turn with the enemy having the initiative!
</li>
<li>
The numbers such as transition matrix size and matchcode
are used for diagnostics.
Include this info in any bug report emailed to mathguy.
</li>
<li>
The bottom gives time spent on a calculation.
It might also say the the data was read from a database
because the calculation was already done previously.
</li>
</ul>

 </body>
</html>
WHOLEPAGE

################################# deleted Marro Plague note ######################
#(
#A future feature might try to do attempt the following model:
#Assume each attacker is adjacent to only one defender.
#The number of such adjacency is modeled as [previous adjacent] minus 
#[killed in previous turn] plus [squad size].  This models the optimum strategy of
#moving unadjacent attackers to maximize the number of enemies adjacent. 
#But this model would require keeping track of more information and is likely to take too long 
#in a calculation with large squads.
#)


}
#################################################################
sub todo {
  $message = $_[0]; # an optional message
  print<<WHOLEPAGE;
 <b>$message</b>

 <h2>To do list</h2>

<ul>

<li>Future abilities as they come out.</li>
<li>Adding the option of giving one of the players some free attacks at the beginning to simulate
melee characters getting to the ranged characters.
This might never be implemented because it would increase the size of the calculation too much.
</li>
</ul>

  <h2>Future features?</h2>
<ul>

<li>
The web interface can be improved by adding Javascript features, such as:
<ul>
<li>Hovering over an ability will activate a bubble that describes the ability</li>
<li>A selection box of current figures/squads will allow a select that will
automatically input all the data for that figure/squad.
[DONE!]
</li>
<li>Hitting calculate will bring up a dialog box, estimating the length of time
required for the calculation.  (I'll need more data to be able to guess
the length of time based on size of transition matrix.)</li>
</ul>
</li>
<li>Send suggestions to $emailLink</li>
</ul>

<a name="new"><hr></a>

  <h2>Version history</h2>
<ul>

<li>Version 1.0: the first working version.  
But this had a typo that affected results of squads (those results were a little bit off).</li>
<li> Version 2.0: Corrected typo/bug in Version 1.0 that affected squads.
This version now allows the numbers of units to be higher than the squad size, so that you can
simulate using multiple copies of a squad.
Also implements Rage.</li>
<li>Version 2.1: corrects the typo that had labeled "Tough" in the wrong spot. 
(I accidentally relabeled Bonus Skull to Tough.) 
All Version 2.0: calculations with Tough had wrong answers - sorry! </li>
<li>Version 2.2: Major improvements on efficiency when dealing with multiple common squads.
(This reduced the size of the transition matrix in these cases.)
Version 2.21 made minor improvements.
</li>
<li>
Version 2.3: improvements: Interface renovated to be clearer.  More abilities added: Blood Hungry,.
Also, a minor typo in "Frenzy" ability fixed.
(Calculations done with previous versions involving "Frenzy" might be off by less than 0.1%.)
</li>
<li>
Version 2.31: Implemented Stealth Armor, Shield Wall.  Fixed typo in Crippling Gaze. (Previous Crippling Gaze calculations would just fail because
the transition matrix would be detected to be nonstochastic - a double check that is always done.)
</li>
<li>
Version 2.32: Fixed typo in Unleashed Fury. (Previous calculations would just fail because
the transition matrix would be detected to be nonstochastic.)  Hopefully, these are the last typos
introduced when Version 2.2 efficiency improvements were made.
</li>
<li>
Version 2.40: Interface now has a quick selection box for existing Heroscape characters; selecting one
will fill in all stats & abilities for that army card.
Warnings will sound if certain combinations are selected such as Grimnak versus Charos, in this example, you
should select "Grimnak (vs Large or Huge)" instead of "Grimnak".
And really fixed CripplingGaze this time (hopefully).</li>
<li> Version 2.41: Paralyzing Stare warning added (requires opponent to be small or medium).
</li>
<li>Version 2.42: A textbox that shows point value if official units are chosen (prorated if fractional squads).</li>

<li>Version 3.1: New interface that is more space efficient.
Created Attack Enhancement and Defense Ehancement selection boxes, for abilities 
such as Sword of Reckoning and Melee Defense.
This is now needed because of the Rod of Negation! (It used to be that you could 
just enter a higher Attack of Defense number.)
Abilities from Zanafor's Discovery, Raknar's Vision, and Thaelunk Tundra added.
</li>
<li>Version 3.12: HiveSwarm fixed.</li>
<li>Version 3.2: Radio buttons added for limiting number of attackers when enemy is down to 1 figure.
This is mainly used in conjunction with HiveSwarm. Set this number to 6 if enemy is a one-hex-base figure and
to 8 if enemy is a two-hex-base figure.</li>
<li>Version 3.21: More documentation.</li>

<li>Version 3.30: Some abilities and figures from Thora's Vengeance added.</li>
<li>Version 4.00: Abilities from Thora's Vengeance, Sir Hawthorne, Flagbearers, Dawn of Darkness added.
The Valkyrie dice abilities required revamping the way probabilities were computed using generating functions.
</li>
<li>Version 4.01: Fixed some typos.  
(Previous version crashed on some inputs, but did not return any false results.)
</li>

<li>Version 4.11: Some Marvel characters added. Rest to be added later.
</li>
<li>
Version 4.20: WaitAndFire and UnleashedFury fixed (typos were introduced in Version 4.0).
</li>
<li>
Version 4.21: Fixed Throw14 when vs squads.
</li>
<li>
Version 4.40: Wave 7 and Swarm of the Marro and rest of Marvel characters implemented (except for Silver Surfer).
</li><li>
Version 4.50: Cosmic Blast implemented.
</li><li>
Version 4.60: Lethal Sting implemented.
</li>
<li>
Version 4.70: Updated the database of abilities that should not be used together.  
Fixed database of previous calculations, where Throw14 versus squads, HomeTree vs CounterStrike,
Unleashed Fury with more than 1 squad were done incorrectly.
</li>

<li>
Version 4.80: Smoke Powder fixed so that it occurs before every attack, not just once per turn.
</li>
<li>Version 4.81: Added Master Win Chiu Woo.</li>
<li>Version 4.82: Fixed HomeTree for heros. <u> Any calculations involving heros using the Leaf of HomeTree aura should be recalculated.</u>
(there were 13 of these calculations - they have been purged from the database).
</li>
<li>Version 5.00: Added Wave 8 abilities except for Pounce, which will be added soon, with an explanation of how it is implemented.</li>
<li>Version 5.01: Entangling Web fixed.  Fixed Eternal Hatred.</li>
<li>Version 5.02: The ability Pounce is added.</li>
<li>Version 5.03: Agent Skahen added.</li>
<li>Version 5.04: Fixed stats of Marcu.</li>
<li>Version 5.10: Fixed StingerDrain (StingerDrain calculations prior to 4-26-09 are incorrect.
Those calculations have been expunged from the database.).</li>
<li>Version 5.20: Fixed DoubleShields (Shields of Valor). A typo bug was introduced in
Version 4.00 (May 15, 2007).  D'oh.  Calculations done with DoubleShields between
May 15, 2007 and May 21, 2009 are incorrect.  
</li>
<li>Version 5.30: Added Wave 9. Also fixed a bug in Rod of Negation where the rod was not used if 
it was at the very end of a round (it makes about 0.5-2.5% difference). 
Also fixed Pounce, which was not killing the pouncer sometimes.
It might be worthwhile to redo any previous Rod of Negation or Pounce calculations. (Sorry.)
Any Rod of Negation or Pounce data from the data base has been expunged (88 of these).
</li>
<li>Version 5.40: Added Wave 10 and D&D Master Set figures. 
Also fixed WaitAndFire again. Arg. 
Any WaitThenFire data from the data base has been expunged (690 of these).
Remember the implementation of WaitThenFire is that the figures get +1 attack
except on the very first turn if they go first; this simulates them
being the ones to move into range of the battle.
</li><li>Version 5.45: Fixed BloodFrenzy (Sorry I misread the card to be a Krug-like ability.)
Any calculations done with the Feral troll using testing beta version before Feb 14, 2010 are incorrect.
</li>
<li>
Version 5.50: Added Wave 11 (D1) figures.
Implemented Engagement Strike / Ice cold. 
</li>
<li>
Version 5.60: Implemented Squad of size 1 to mean common heroes. (For example, this makes a difference
for Sudema and Braxas.) Added negative modifiers for D20Enhancement. (Useful for custom figures.)
</li>
<li>
Version 6.00: New interface improvements, including mouseover activating balloons that describe the abilities.
</li>
<li>
Version 6.10: Added implementable abilities from D12: the fledgling abilities and Psionic Blast.  Also fixed a typo that would have affected results of ChillingTouch and Trample and made them worse than they should be.  All Chilling Touch and Trample and Throw numbers before Aug 28, 2010 should be redone.  There were 883 such calculations from before and this data has been expunged from the database.
</li>
<li>
Version 6.11: Modified Searing Intensity. 
</li>
<li>
Version 6.20: Added more choices to "Squad size" to include squads of more than 1 common heroes, which is essentially what you get when you use Wyrmling Bonding or Master of the Elements. This only makes a difference for powers that distinguish squad figures from common heros such as Chomp, Poison Acid Breath, Stare of Stone.
</li>

</ul>


 </body>
</html>
WHOLEPAGE
}
#################################################################
sub statspage {
  $message = $_[0]; # an optional message

my $num="[data unavailable]";
my $avg="[data unavailable]";
my $maxmat="[data unavailable]";
my $maxsizetime="[data unavailable]";
my $ctr=0;
my $sum=0;
my ($in,$x,$y,@x, @z);
if(1){if(open(IN, "<$datafile")){
  while($in=<IN>){
    ($x,$y)=split(/=/,$in);
    if($y!~/custom/){
      $ctr++;
      @x = $y=~/(\d+\.*\d*)/g;
      $sum+=$x[49];
     if($x[24]>$maxmat){$maxmat=$x[24];$maxsizetime=$x[49];}
    }
  }
  close(IN);
  $num=$ctr;
  $avg=sprintf("%.1f",$sum*1.0/$ctr);
}}



#### Version 2.3x, 2.4x had 2350 hits before version 3.x ####

  print<<WHOLEPAGE;
 <b>$message</b>

 <h2>Version 2.3x, 2.4x, 3.xx, 4.xx, 5.xx, 6.xx Access stats</h2>
<h4>From December 9, 2005 to now</h4>

<ul>
<li>
There have been <b>$HITCOUNTER</b> hits to this program. (Remember that successful calculations are stored
in a database, and are NOT recalculated when the same calculation is later requested.  Such a calculation
is counted only once in the number of "successful calculations" below.
</li>
<li>
There have been <b>$num</b> successful calculations,
each taking an average of <b>$avg</b> seconds.
</li>
<li>
The maximum size of a transition matrix in a successful calculation on this server
was <b>$maxmat</b>,
which took <b>$maxsizetime</b> seconds.
</li>
<li>
No data is kept on unsuccessful calculations (those that exceed the imposed
time limit of $maxLockFile seconds).
</li>
<li>
Versions 2.3x, 2.4x had 2350 hits, December 10, 2005 to May 8, 2006. (The new versions are backwards compatible
with old stored data.)
</li>

</ul>

<hr>

<h2>Version 2.2x Access stats</h2>
(November 10, 2005 to December 9, 2005)<br>
(Calculations done with these versions 
involving "Frenzy" might be off by 0.1% to the Frenzier's disadvantage.)
<ul>
<li>
There have been <b>634</b> successful calculations,
each taking an average of <b>4.8</b> seconds.
</li>
<li>
The maximum size of a transition matrix in a successful calculation
was <b>3252</b>,
which took <b>90</b> seconds.
</li>
<li>
No data is kept on unsuccessful calculations (those that exceed the imposed
time limit of 150 seconds).
</li>
</ul>


<hr>
 <h2>Version 2.0 & 2.1 Access stats</h2>
(October 24, 2005 to November 9, 2005)<br>
(Calculations done with these versions involving "Tough" should be ignored.)
<ul>
<li>
There have been <b>454</b> successful calculations,
each taking an average of <b>5.3</b> seconds.
</li>
<li>
The maximum size of a transition matrix in a successful calculation
was <b>4044</b>,
which took <b>144</b> seconds.
</li>
<li>
No data is kept on unsuccessful calculations (those that exceed the imposed
time limit of 150 seconds).
</li>
</ul>



<hr>
 <h2>Version 1.0 Access stats</h2>
(September 16, 2005 to October 23, 2005)

<ul>
<li>
There have been <b>648</b> successful calculations,
each taking an average of <b>7.3</b> seconds.
</li>
<li>
The maximum size of a transition matrix in a successful calculation
was <b>3252</b>,
which took <b>84</b> seconds.
</li>
<li>
No data is kept on unsuccessful calculations (those that exceed the imposed
time limit of 120 seconds).
</li>
</ul>

 </body>
</html>
WHOLEPAGE
}
#################################################################
sub aboutthis {
  $message = $_[0]; # an optional message
  print<<WHOLEPAGE;
 	<h2>About this program...</h2>
<b>$message</b>

Here are the gory details of how math is used to calculate the exact theoretical
probability. (Version 5.40+ version)
<p>
We create a probabilistic finite state machine.
Each state is of the form <br>
<pre>    (life1, life2, halfTurnNumber, attacker, attackersLeft, attacksLeft, otherCrippled, attackDice, targettingSame,
negated, enemiesToAttack, smoked/skipTurn, (blasted1, blasted2), adjacentWolves, (limiteduse1,limiteduse2), numEngaged)
</pre>
So Each state keeps track of:
<ol>
<li>life1: number hit points or units left in player #1</li>
<li>life2: number hit points or units left in player #2</li>
<li>halfTurnNumber: This goes from 1 to 6, the half-turn number</li>
<li>attacker: This is either 1 or 2, which player is attacking.</li>
<li>attackersLeft: This is the number of figures yet to attack in this turn; this
is only relevant for squads</li>
<li>attacksLeft: This is the number of attacks yet to be executed by the current attacking
figure; this is only relevant for Double Attack ability (or Triple Attack).
Note that you cannot lump attackersLeft and attacksLeft together because conceivably
you could have a squad of figures with multiple attacks and it is possible a figure
could be killed in the middle of its first attack.</li>
<li>otherCrippled: This is 0 or 1. It is 1 if the other player has been crippled or EMPResponse activated, 
with order markers removed, or Cloud of Darkness activated with turns skipped until your next turn.</li>
<li>attackDice: This number keeps track of any attack modifier for this turn. For example,
this is used for Unleashed Fury, for Wait And Fire, Stinger Drain.</li>
<li>targettingSame: This is 0 or 1. It is 1 after a figure has fired and the target
has not been destroyed.  This is needed for Zettian Targetting bonus</li>
<li>negated: This is 0, 1, or 2. This keeps track of whether abilities have been negated by the Rod
of negation.  A 1 or 2 refers to which player has been negated.  Note that both players cannot
be negated simultaneously.</li>
<li>enemiesToAttack: This is used for the simplified ShaolinAttack ability. 
This field should have value 0 or x, where x is the number of extra attacks the last
attacker gets; so only needed whether attackersLeft > 0.
</li>
<li>
smoked/skipTurn: For use with Smoke Powder.  0 = normal.  1 = smoked by defender and all normal attacks are skipped.
For use with Eternal Hatred or Stinger Drain, 0 = normal, 1 = skip rest of turn (i.e. no attacks).
</li>
<li>
blasted1 is the number of player2's unrevealed order markers that have been discarded,
blasted2 is the number of player1's unrevealed order markers that have been discarded,
</li>
<li>adjacentWolves is the number of Wolves of Badru that are adjacent; this is used for the Pounce ability.
</li>
<li>
limiteduse1 is the number of limited use weapons left for player1, such as Ullar's Bolt, WraithAttack drows.
limiteduse2 is the number of limited use weapons left for player2, such as Ullar's Bolt, WraithAttack drows.

</li>
<li>numEngaged: Keeps track of number of engaged figures for use with Engagement Strike and Ice Spikes.
</li>


<li>We may need to keep track of more stuff in the future, as more abilities are invented</li>
<li>The states are given an arbitrary indexing.  For efficiency, it is likely best to use
a hash (associative array) to assign a an index to each state.</li>
</ol>

There are also initial states and final states.  There are 12 possible initial states, depending
on which halfTurn and which player starts up.
The final states keep track of who won, with how many hit points or units left.
<p>
After the list of states is made up, 
a big transition matrix is built.
The rows and columns correspond to the states.
So the size is the number of states (which could be well over a thousand).
What is a transition matrix?
<p>
For each state State1, we calculate all the possible states that we can end up in
after one attack (one set of dice rolls, including any Chomp or Gaze).
The probability P that we land in state State2 is stored as an entry in the
transition matrix in row State1 and Column State2.
Call this transition matrix S.  The part where we land in a final state is separated
out into a matrix T.
So S has dimension n x n and T has dimension n x m
where m is the number of final states, and n is the number of non-final states.
<p>
Here is the math theory calculation...
<ul>
<li>So T gives us the matrix of probabilities of starting in any state and landing in a final state
after exactly 1 move.</li>
<li>And  ST (matrix multiplication!) gives us the matrix of probabilities of starting in any state and landing in a final state
after exactly 2 moves.</li>
<li>And SST gives us the matrix of probabilities of starting in any state and landing in a final state
after exactly 3 moves.</li>
<li>So the matrix of probabilities of starting in any state and landing in a final state
after any number of moves is
<pre>    T + ST + SST + SSST + ....</pre>
an infinite sum.</li>
<li>But this infinite sum simplifies into
<pre>    (I + S + S^2 + S^3 +...) T
    = (InverseMatrix(I - S)) T.</pre></li>
<li>Compute <pre>    Q = InverseMatrix(I - S)</pre></li>
<li>Then QT gives the matrix of probabilities of starting in any  particular state
and eventually landing in any final state.
You use QT to compute the probability of a certain player winning, given that any particular
starting state.
You can also use QT to compute the average hit points or units left with a certain player
conditioned upon that player winning.</li>
<li>And finally,
<pre>    Q = I + S + S^2 + S^3 +...</pre>
gives the expected number of visits to each state, so that a sum across a row
would give the average number of moves (attacks) before landing in a final state.
</li>

And there you have it, that's how we compute the exact probabilities.
The tricky part in writing this program is generating the transition matrix.
The potential time consuming part is calculating the Q matrix because it
involves a matrix inversion.
<p>
Hooray for math.
<p>
How does the web interface work?
The Perl-CGI program reads the submitted form data,
and writes a Mathematica program.  The Mathematica program is then
executed on the server and the Perl program sleeps while waiting for the Mathematica
program to finish.
The Perl program will also abort the Mathematica program if it takes too long
(defined to be $maxLockFile seconds).
Only one heroscape Mathematica program is allowed to execute at atime.
 </body>
</html>
WHOLEPAGE
}

#################################################################
sub default_page {
  $message = $_[0]; # an optional message
  print<<WHOLEPAGE;
 	$title<b>$message</b>
<table>
<tr><th>SELECT ABILITIES</th><th></th></tr>
<tr><td align="center">
WHOLEPAGE

&printForm(%formHash);

  print<<WHOLEPAGE;
</td><td>$instructions</td></tr></table>

$ender

 </body>
</html>
WHOLEPAGE
}

#################################################################
sub pInitDisadv{
  my ($diff)=@_;
  my $b=19-$diff;
  return $b*($b+1)/(2*(399-$b));
}
#################################################################
sub pInitProb{
  my ($diff)=@_;
  if($diff>=0){
    return 1-pInitDisadv($diff);
  }else{
    return pInitDisadv(-$diff);
  }
}
#################################################################

sub findprob{
 my  $message = $_[0]; # an optional message
 my ($i,$j,$x,$y,$x2,$y2,$c,$z,$c1,$c2,$fig1spec,$fig2spec,$p, $return, $a1,$a2,$a,$b,$aa,$bb);
 my $fig1="Figure"; my $f1="Fig";
 if($formHash{psquad} eq "squad"){
   $fig1="Squad";$f1="Squ";
   if($formHash{ps}==1){$fig1="Figures";$f1="Figs";}
 }
 my $fig2="Figure";my $f2="Fig";
 if($formHash{qsquad} eq "squad"){
   $fig2="Squad";$f2="Squ";
   if($formHash{qs}==1){$fig2="Figures";$f2="Figs";}
 }

$return = 0;
for($i=0;$i<@ForbiddenPairs;$i+=2){
 foreach $p (("p","q")){
  $j=$i+1;
  $x=$p.($ForbiddenPairs[$i]);
  $y=$p.($ForbiddenPairs[$j]);
  if((exists $formHash{$x})&&(exists $formHash{$y})){
    $a1=$Abilities[$ForbiddenPairs[$i]-4];
    $a2=$Abilities[$ForbiddenPairs[$j]-4];
    $message .= qq^<br>
<table><tr><td bgcolor="red"><font size="18" face="bold">
Giving a figure both $a1 and $a2 is not allowed.
</font></td></tr></table>\n^;
    $return=1;
  }
 }
}
for($i=0;$i<@ForbiddenPairsOpps;$i+=2){
  $j=$i+1;
  $x="p".($ForbiddenPairsOpps[$i]);
  $y="q".($ForbiddenPairsOpps[$j]);
  if((exists $formHash{$x})&&(exists $formHash{$y})){
    $a1=$Abilities[$ForbiddenPairsOpps[$i]-4];
    $a2=$Abilities[$ForbiddenPairsOpps[$j]-4];
    $message .= qq^<br>
<table><tr><td bgcolor="red"><font size="18" face="bold">
Giving opposing figures $a1 and $a2 is not allowed.
</font></td></tr></table>\n^;
    $return=1;
  }
}
if($return){
  &default_page($message);return;
}
if(($formHash{p2}==0) &&($formHash{q2}==0)){
  &default_page("At least one figure must have an attack.");return;
}

if($formHash{psquad} eq "single"){$formHash{ps}=0;}  ## Good!!
if($formHash{qsquad} eq "single"){$formHash{qs}=0;}

  $probs='0,40.17,66,77,12,10,90,12,66,33,45,17';
  $lens='0,3,3,1,1,1,2,2,2,4,4,4';
  $survivors1='1.5,3.2,3,1,1,1,2,2,2,4,4,4';
  $survivors2='1.5,3.2,3,1,1,1,2,2,2,4,4,4';

  @probs=split(/,/,$probs);
  @lens=split(/,/,$lens);
  @survivors1=split(/,/,$survivors1);
  @survivors2=split(/,/,$survivors2);

  $MatrixSize="unknown";
  $timeSpent="";

$matchcode=&encodeMatchup(%formHash);

$code = &goGetProbs(%formHash);

### print "/$code/";

if($code){
  $rTable=$code;
}else{

$fig1spec=&specs("p",%formHash);
$fig2spec=&specs("q",%formHash);

$rTable="";
#$rTable="matchcode = $matchcode <br>\n";

$topgray="#999999";


$rTable.=qq^<table border="2" align="center" bgcolor="cyan"><tr><th>$fig1 #1 specs</th><th>$fig2 #2 specs</th></tr>
<tr><td>$fig1spec</td><td>$fig2spec</td></tr></table>
^;
  $a=$probs[0];$b=$probs[1];$aa=100-$a;$bb=100-$b;
  $x=&myformat(($a+$b)/2);
  $diff=$formHash{pminit}-$formHash{qminit};
  if($diff!=0){
    $ppp=pInitProb($diff);
    $x=&myformat(($ppp*$a+$b*(1-$ppp)));
  }
  $y=&myformat(100-$x);
  $z=&myformat2(($lens[0]+$lens[1])/2);
  if($a+$b==0){
    $x2=&myformat1(0);
  }else{
    $x2=&myformat1(($a*$survivors1[0]+$b*$survivors1[1])/($a+$b));
  }
  if($aa+$bb==0){
    $y2=&myformat1(0);
  }else{
    $y2=&myformat1(($aa*$survivors2[0]+$bb*$survivors2[1])/($aa+$bb));
  }
  $c1="";$c2="";
  if($x>$y){$c1=qq^bgcolor="$color1"^;}else{$c2=qq^bgcolor="$color2"^;}

  $rTable.=qq^<table border="2" align="center" bgcolor="$gray">
<tr><th colspan="5" bgcolor="$topgray">
Battle begins at the beginning of a round, with an initiative roll
</th></tr>
<tr><th colspan="2">Win%</th>
<th rowspan="2">Average<br>attacks</th>
<th colspan="2">Average survivors</th></tr>
<tr>
<th bgcolor="$color1">$f1 #1</th><th bgcolor="$color2">$f2 #2</th>
<th bgcolor="$color1">$f1 #1</th><th bgcolor="$color2">$f2 #2</th>
</tr>
<tr>
<td align="center" $c1>$x\%</td><td align="center" $c2>$y\%</td><td align="center">$z</td>
 <td align="center" bgcolor="$color1">$x2</td><td align="center" bgcolor="$color2">$y2</td>    
</tr>
</table>
^;


  $rTable.=qq^<table border="2" align="center" bgcolor="$gray">
<tr><th colspan="7" bgcolor="$topgray">
If we specify who goes first and battle begins which round...
</th></tr>
<tr><td></td><td></td>
<th colspan="2">Win%</th>
<th rowspan="2">Average<br>attacks</th>
<th colspan="2">Average survivors</th>
</tr>
<tr>
<th colspan="2">Battle begins at...</th>
<th bgcolor="$color1">$f1 #1</th><th bgcolor="$color2">$f2 #2</th>
<th bgcolor="$color1">$f1 #1</th><th bgcolor="$color2">$f2 #2</th>

</tr>
^;
for($i=1;$i<=3;$i++){
  $x=&myformat($probs[4*($i-1)]);
  $y=&myformat(100-$x);
  $z=&myformat2($lens[4*($i-1)]);
  $x2=&myformat1($survivors1[4*($i-1)]);
  $y2=&myformat1($survivors2[4*($i-1)]);
  $c1="";$c2="";
  if($x>$y){$c1=qq^bgcolor="$color1"^;}else{$c2=qq^bgcolor="$color2"^;}
  $rTable.=qq^<tr><th rowspan="2">TURN $i</th>
    <th>$f1 #1 moves first</th><td align="center" $c1>$x\%</td><td align="center" $c2>$y\%</td><td align="center">$z</td>
  <td align="center" bgcolor="$color1">$x2</td><td align="center" bgcolor="$color2">$y2</td>    
  </tr>\n^;
  $x=&myformat($probs[4*($i-1)+1]);
  $y=&myformat(100-$x);
  $z=&myformat2($lens[4*($i-1)+1]);
  $x2=&myformat1($survivors1[4*($i-1)+1]);
  $y2=&myformat1($survivors2[4*($i-1)+1]);
  $c1="";$c2="";
  if($x>$y){$c1=qq^bgcolor="$color1"^;}else{$c2=qq^bgcolor="$color2"^;}
  $rTable.=qq^<tr>
    <th>$f2 #2 moves first</th><td align="center" $c1>$x\%</td><td align="center" $c2>$y\%</td>
<td align="center">$z</td>
<td align="center" bgcolor="$color1">$x2</td><td align="center" bgcolor="$color2">$y2</td>    
</tr>\n^;
}
  $rTable.=qq^</table>\n^;
  
}

$timeSpentNote="";
if($timeSpent){$timeSpentNote=qq^<center>($timeSpent)</center>^;}

  print<<WHOLEPAGE;
 	$title<b>$message</b>
<table>
<tr><th>SELECT ABILITIES</th><th>RESULTS</th></tr>
<tr><td align="center">
WHOLEPAGE

&printForm(%formHash);

$matchcodeA = $matchcode;
$matchcodeA =~s/(\d{10})/$1 /g;

  print<<WHOLEPAGE;
</td><td align="top">$rTable

<center>(transition matrix was $MatrixSize by $MatrixSize)</center>
<center>(Mention the following in any bug report: matchcode = $matchcodeA)</center>
$timeSpentNote
<center>
<a href="$ENV{SCRIPT_NAME}?request=instructions#results"  target="newwindow">[Meaning of returned results]</a>
</center>


</td></tr></table>

$ender
 </body>
</html>
WHOLEPAGE

}

#################################################################
sub myformat{
 my ($x)=@_;
 return sprintf("%.3f",$x);
}
#################################################################
sub myformat1{
 my ($x)=@_;
 return sprintf("%.1f",$x);
}
#################################################################
sub myformat2{
 my ($x)=@_;
 return sprintf("%.2f",$x);
}
#################################################################
sub printForm{
 my %f = @_;
 print qq^
 
 <table border="2" bgcolor="$gray">
  <form action="$ENV{'SCRIPT_NAME'}" name="myform" method="GET">
 <tr><th>Figure(s) #1</th><th>Figure(s) #2</th></tr>
 <tr><td bgcolor="$color1">
^;

&printTopList("p",%f);

 print qq^</td><td bgcolor="$color2">^;

&printTopList("q",%f);

 print qq^
   </td></tr>
   ^;
 
 &printAbilities(%f);

 &printBottomList(%f);
 
 print qq^
   <input type="hidden" name="request" value="findprob"/>
</table>
   <center><input type="submit" value="Calculate"/>
    <input type="reset" value="Reset"/>
</center>
   <center>(this may take several seconds, be patient)</center>
  </form>

<script language="javascript">resetAbilitiesColors();</script>

^;
}
#################################################################
sub printTopList{
 my ($p,%f) = @_;
 my ($i, $checked, $x, $q, $c, $c2, $colh,$cols, $sel);
 ### add color here ###
 if($p eq "p"){
   $c=$color1; $c2="green";
 }else{
   $c=$color2; $c2="red";
 }
 
 $sel=""; if($f{"quick$p"} eq "custom"){$sel='selected="selected"';}
 $q=qq^<option value="custom" $sel>--customize---</option>\n^; 
 for($i=0; $i<@zName; $i++){
   $x = $zName[$i];
   $sel=""; if($f{"quick$p"} eq "$i"){$sel='selected="selected"';}
   $q.=qq^<option value="$i" $sel>$x</option>\n^;
 }
 print qq^<select name="quick$p" onChange="changeQuick$p()">$q</select><br>\n^;
 my $ptp=$f{"pt$p"}; if($ptp eq ""){$ptp="---";}
 print qq^points: <input type="text" name="pt$p" size="5" value="$ptp" readonly><br>\n^;

 $x = $p."squad";

 if($f{$x} ne "squad"){$checked='checked="checked"';$colh=$c2;}else{$checked="";$colh=$c;}
 print qq^<table border="2"><tr><td id="cellhero$p" bgcolor="$colh" align="top">\n^;
 print qq^<input type="radio" name="$x" value="single" onClick="reset$p()" $checked>Hero\n^;

 
 if($f{$x} eq "squad"){$checked='checked="checked"';$cols=$c2;}else{$checked="";$cols=$c;}
 print qq^</td><td id="cellsquad$p" bgcolor="$cols" align="top">\n^;
 print qq^<input type="radio" name="$x" value="squad" onClick="reset$p()" $checked>Squad(s)/Common heroes\n^;

 print qq^</td></tr><tr><td id="cellhero2$p"  bgcolor="$colh" align="top">\n^;


 print qq^LIFE: ^;
 &printMenuReset($p,"hero$p"."1", 1, $f{"hero$p"."1"}, (1..20));
 
 print qq^</td><td id="cellsquad2$p"  bgcolor="$cols" align="top">\n^;

 print qq^FIGURES: ^;
 &printMenuResetFigures($p,"squad$p"."1", 1, $f{"squad$p"."1"}, (1..20));
 print qq^<br>SQUAD SIZE: ^;
 &printMenuReset($p,$p."s", "---", $f{$p."s"}, (0..$maxSquadSizeIndex));  ### " " default says 0 means ---

print qq^</td></tr></table>\n^;

 print qq^\nATTACK: ^;
 &printMenuReset($p,$p."2", 1, $f{$p."2"}, (0..9));
 print qq^<br>\nDEFENSE: ^;
 &printMenuReset($p,$p."3", 1, $f{$p."3"}, (0..12));

}

#################################################################
sub printBottomList{
 my (%f) = @_;

 if(!exists($f{"pattlim"})){$f{"pattlim"}=0;}
 if(!exists($f{"qattlim"})){$f{"qattlim"}=0;}
 print qq^<tr><td colspan="2"><table border="0">\n^;

 my $e='&nbsp;';

 print qq^<tr>^;
 print qq^<td id="apmoff" bgcolor="$color1">$e^;
 &printMenu("pmoff", 0, $f{"pmoff"}, (0..9));
 print qq^$e</td><td id="aqmoff" bgcolor="$color2">$e^;
 &printMenu("qmoff", 0, $f{"qmoff"}, (0..9));
 print qq^$e</td><td>\nAttack enhancement</td>\n^;

 print qq^<td rowspan="3" colspan="3">^;

 print qq^<table border="1">^;
 print qq^<tr><td colspan="3">For use with HiveSwarm melee</td></tr>\n^;

 print qq^<td id="apattlim0" bgcolor="$color1">$e^;
 &printRadio("pattlim", 0, $f{"pattlim"});
 print qq^$e</td><td id="aqattlim0" bgcolor="$color2">$e^;
 &printRadio("qattlim", 0, $f{"qattlim"});
 print qq^$e</td><td>\nNo limit on attackers</td></tr>^;

 print qq^<td id="apattlim1" bgcolor="$color1">$e^;
 &printRadio("pattlim", 6, $f{"pattlim"});
 print qq^$e</td><td id="aqattlim1" bgcolor="$color2">$e^;
 &printRadio("qattlim", 6, $f{"qattlim"});
 print qq^$e</td><td>\nLimit attackers to 6 on each enemy unit</td></tr>^;

 print qq^<td id="apattlim2" bgcolor="$color1">$e^;
 &printRadio("pattlim", 8, $f{"pattlim"});
 print qq^$e</td><td id="aqattlim2" bgcolor="$color2">$e^;
 &printRadio("qattlim", 8, $f{"qattlim"});
 print qq^$e</td><td>\nLimit attackers to 8 on each enemy unit</td></tr>^;

 print qq^</table>^;
 print qq^</td></tr>^;

 print qq^<tr>^;
 print qq^<td id="apmdef" bgcolor="$color1">$e^;
 &printMenu("pmdef", 0, $f{"pmdef"}, (0..9));
 print qq^$e</td><td id="aqmdef" bgcolor="$color2">$e^;
 &printMenu("qmdef", 0, $f{"qmdef"}, (0..9));
 print qq^$e</td><td>\nDefense enhancement</td>^;
 print qq^</tr>\n^;

 print qq^<tr>^;
 print qq^<td  id="ap0" bgcolor="$color1">$e^;
 &printMenu("p0", 0, $f{"p0"}, (0..$maxD20Bonus));
 print qq^$e</td><td id="aq0" bgcolor="$color2">$e^;
 &printMenu("q0", 0, $f{"q0"}, (0..$maxD20Bonus));
 print qq^$e</td><td>\nD20 enhancement</td>^;
 print qq^</tr>\n^;

 print qq^<tr>^;
 print qq^<td id="apminit" bgcolor="$color1">$e^;
 &printMenu("pminit", 0, $f{"pminit"}, (0..18));
 print qq^$e</td><td id="aqminit" bgcolor="$color2">$e^;
 &printMenu("qminit", 0, $f{"qminit"}, (0..18));
 print qq^$e</td><td>\nInitiative enhancement</td>^;
 print qq^</tr>\n^;

 print qq^</table></td></tr>\n^;
}
#################################################################
#################################################################
sub printRadio{
 my ($name, $value, $defValue) = @_;
 my ($checked);
 if($value==$defValue){$checked='checked="checked"';}else{$checked="";}
 print qq^<input type="radio" name="$name" value="$value" onClick="resetBottomColors()" $checked>\n^;
}
#################################################################
sub printAbilities{
 my %f = @_;
 my ($half,$i,$j, $k);
 $numBoxes=0;

 my @sorted = sort {$Abilities[$a] cmp $Abilities[$b]} (0..$#Abilities);
 
 print qq^<tr><td colspan="2"><table border="0" bgcolor="$gray">\n^;
 
 for($k=0;$k<@sorted;$k+=2){
  $i=$sorted[$k];
  $j=$sorted[$k+1];
  print qq^<tr>\n^;
  &printOneAbilityBox("p",$i,%f);
  &printOneAbilityBox("q",$i,%f);
  print qq^<td onMouseover="style.backgroundColor='gray';ddrivetip('$AbilitiesDescJS[$i]', 300)"
onMouseout="style.backgroundColor='$gray';hideddrivetip()">$Abilities[$i]</td>\n^;
  if($j ne ""){
   &printOneAbilityBox("p",$j,%f);
   &printOneAbilityBox("q",$j,%f);
   print qq^<td onMouseover="style.backgroundColor='gray';ddrivetip('$AbilitiesDescJS[$j]', 300)"
onMouseout="style.backgroundColor='$gray';hideddrivetip()">$Abilities[$j]</td>\n^;
  }else{print qq^<td> </td><td> </td><td>&nbsp;</td>^;}
  print qq^</tr>\n^;
 }
 
 print qq^</table></td></tr>\n^;
}
#################################################################
sub printAbilitiesChronological{
 my %f = @_;
 my ($half,$i,$j);
 
 print qq^<tr><td colspan="2"><table border="0" bgcolor="$gray">\n^;
 
 for($i=0;$i<@Abilities;$i+=2){
  $j=$i+1;
  print qq^<tr>\n^;
  &printOneAbilityBox("p",$i,%f);
  &printOneAbilityBox("q",$i,%f);
  print qq^<td>$Abilities[$i]</td>\n^;
  if($j<=$#Abilities){
   &printOneAbilityBox("p",$j,%f);
   &printOneAbilityBox("q",$j,%f);
   print qq^<td>$Abilities[$j]</td>\n^;
  }else{print qq^<td> </td><td> </td><td>&nbsp;</td>^;}
  print qq^</tr>\n^;
 }
 
 print qq^</table></td></tr>\n^;
}


#################################################################
sub printAbilities2{
 my %f = @_;
 my ($half);
 $half=int((1+(scalar @Abilities))/2);
 print qq^<tr><td>\n^;
 &printAbilityTable((0,($half-1)),%f);
 print qq^</td><td>\n^;
 &printAbilityTable(($half,($#Abilities)),%f);
 print qq^</td></tr>\n^;
 
}
#################################################################
sub printAbilityTable{
 my ($start,$end,%f)=@_;
 my ($i);
 ####print qq^HERE: $start, $end^;
 print qq^<table border="0" bgcolor="$gray">\n^;
 for($i=$start;$i<=$end;$i++){
   print qq^<tr>^;
   &printOneAbilityBox("p",$i,%f);
   &printOneAbilityBox("q",$i,%f);
   print qq^<td>$Abilities[$i]</td>\n^;
   print qq^</tr>^;
 }
 if(($start>0)&&(2*$start>$end+1)){print qq^<tr><td> </td><td> </td><td>&nbsp;</td></tr>\n^;}
 print qq^</table>^;

}
#################################################################
sub printOneAbilityBox{
 my ($p,$i,%f) = @_;
 my ($x,$color,$checked,$more);
 $x = $p.($i+4);   
 if($p eq "p"){$color=$color1;
   $more=qq^setWhichColor(myform.$x, 'a$x','green','$color1');^;}
 else{$color=$color2;
   $more=qq^setWhichColor(myform.$x, 'a$x','red','$color2');^;}

   if(exists $f{$x}){$checked='checked="checked"';}else{$checked="";}
   $reset="reset";
   if($x eq "$p$AbilityToN{Chomp}"){$reset="resetChomp";}
   if($x eq "$p$AbilityToN{MindShackle}"){$reset="resetMindShackle";}
   if($x eq "$p$AbilityToN{ParalyzingGaze}"){$reset="resetParalyzingGaze";}
   print qq^<td bgcolor="$color" id="a$x">
   <input type="checkbox" onClick="$more$reset$p()" name="$x" value="1" $checked>
   </td>\n^;

}
#################################################################
sub printMenuResetFigures{
 my ($p,$x,$def,$v,@a)=@_;
 my ($y, $s, $yy);
 if($v eq ""){$v=$def;}
 if($v eq "---"){$v=0;}
 print qq^<select name="$x" onChange="resetFigures$p()">\n^;
 foreach $y (@a){
  $s=""; if($y eq $v){$s='selected="selected"';}
  $yy=$y;
  if(($def eq "---")&&($y eq "0")){$yy="---";}
  print qq^<option value="$y" $s>$yy</option>\n^;
 }
 print qq^</select>\n^;
}
#################################################################
sub printMenuReset{
 my ($p,$x,$def,$v,@a)=@_;
 my ($y, $s, $yy);
 if($v eq ""){$v=$def;}
 if($v eq "---"){$v=0;}
 my $extraCode="";
 if($x eq $p."s"){
   $extraCode="myform.${p}squad[1].checked=true;"
 }
 if($x eq "hero".$p."1"){
   $extraCode="myform.${p}squad[0].checked=true;"
 }

 print qq^<select name="$x" onChange="${extraCode}reset$p()">\n^;
 foreach $y (@a){
  $s=""; if($y eq $v){$s='selected="selected"';}
  $yy=$y;
  if(($def eq "---")&&($y eq "0")){$yy="---";}
  if(($x eq $p."s")){
    $yy = $squadSizeDisplay{$y};
    #if($y==1){ $yy="1 (common hero)";}
    #elsif($y>0){ $yy="$y (squad)";}
  }
  print qq^<option value="$y" $s>$yy</option>\n^;
 }
 print qq^</select>\n^;
}
#################################################################
sub printMenu{
 my ($x,$def,$v,@a)=@_;
 my ($y, $s, $yy);
 if($v eq ""){$v=$def;}
 if($v eq "---"){$v=0;}
 print qq^<select name="$x" onClick="resetBottomColors()" >\n^;
 foreach $y (@a){
  $s=""; if($y eq $v){$s='selected="selected"';}
  $yy=$y;
  if(($def eq "---")&&($y eq "0")){$yy="---";}
  if(($x eq "p0")||($x eq "q0")){
    $yy=$D20hash{$y};
  }
  print qq^<option value="$y" $s>$yy</option>\n^;
 }
 print qq^</select>\n^;
}
#################################################################
sub specs{
 my ($p,%f)=@_;
 my ($i,$x,$d,@a);
 my $ans="";
 if($f{$p."squad"}eq "squad"){
   $ans.=qq^$f{$p."1"} FIGUREs<br>\n(1 LIFE each)<br>\n^;
 }else{
   $ans.=qq^LIFE = $f{$p."1"}<br>\n^;
 }
 $ans.=qq^ATTACK = $f{$p."2"}<br>\n^;
 $ans.=qq^DEFENSE = $f{$p."3"}<br>\n^;

 if($f{$p."s"}>0){
   if(($IsSquadForPowers{$f{$p."s"}} eq "False")){
     $ans.=qq^COMMON HEROES squad size $squadSize{$f{$p."s"}}<br>\n^;
   }else{
     $ans.=qq^SQUAD SIZE = $squadSize{$f{$p."s"}}<br>\n^;
   }
 }

 if($f{$p."0"}>0){
  $d=$D20hash{$f{$p."0"}};
  if($d>0){$d = "+".$d;}
  $ans.=qq^D20 enhancement = $d<br>\n^;
 }
 if($f{$p."moff"}>0){$ans.=qq^Attack enhancement = +$f{$p."moff"}<br>\n^;}
 if($f{$p."mdef"}>0){$ans.=qq^Defense enhancement = +$f{$p."mdef"}<br>\n^;}
 if($f{$p."minit"}>0){$ans.=qq^Initiative enhancement = +$f{$p."minit"}<br>\n^;}
 if($f{$p."attlim"}>0){$ans.=qq^Limit attackers to $f{$p."attlim"} on each defender<br>\n^;}


 $ans.=qq^^;
 @a=();
 for($i=0;$i<@Abilities;$i++){
  $x=$p.($i+4);
  if(exists $f{$x}){push @a, $Abilities[$i];}
 }
 $ans.=join("<br>\n",@a);


}
#################################################################
sub goGetProbs{
 my %f=@_;

#### check if result already exists ####
 my ($line,$x,$y, $z, @x, $note);
 my $want = qq^res["$matchcode"]^;
 my $errcode;
 if(open(IN, "$datafile")){
   while($line=<IN>){
     ($x,$y)=split(/=/,$line);
     if($x eq $want){
       @x = $y=~/(\d+\.*\d*)/g;
       @probs=@x[0..11];
       @lens=@x[12..23];
       $MatrixSize=$x[24];
       @survivors1=@x[25..36];
       @survivors2=@x[37..48];
       $tsp=$x[49];
       $note=""; if($y=~/custom/){$note="done on mathguy's home computer";}
#diagnostics
#print "A: ", scalar @x, " : ", join("/",@x), "<br>\n";
#print "B: ", scalar @probs, " : ", join("/",@probs), "<br>\n";
#print "C: ", scalar @lens, " : ", join("/",@lens), "<br>\n";
      $timeSpent="Data read from saved calculations file [original calc time $note was $tsp seconds].";
       return;
     }
   }
 } ###else{print "File open error ($!)<br>\n";}
 #### otherwise use mathematica ####

 #### check if mathematica already running via $mathlock
 if((-e $lockfile) && ((-C $lockfile) > $maxLockFile/(24.*3600))){
   ### Abort mathematica background process
    system("./findAndAbortMath");
 }
 if(-e $lockfile){
   return qq^Site busy (we can only run one copy of Mathematica at a time, 
someone else might be coincidentally using this program), 
please try again, or notify $emailLink of a potential problem if this persists, especially
if the following number (the number of seconds someone else is executing the program) is greater than $maxLockFile.^
."  (busy time: ".((-C $lockfile)*(24.*3600)).")";
 }

 #### set mathlock, delete mathoutput and mathdone
 #### if mathlock existed for more than 10 minutes, abort it?
 open(OUT, ">$lockfile"); close(OUT);
 if(-e $outputfile){unlink $outputfile;}
 if(-e $donefile){unlink $donefile;}
 if(-e "report1.txt"){unlink "report1.txt";} 
 if(-e "report2.txt"){unlink "report2.txt";}

 #### create mathematica input file
 $errcode= &makeMathInput(%f);
 if($errcode){
   return qq^$errcode
Please try again, or notify $emailLink of a potential problem if this persists.^;
;}

 #### run mathematica, checking every 3 seconds until mathdone exists
 #### abort if takes too long?

 if(-e "log.txt"){unlink "log.txt";}
 system("math < $mafile  > log.txt &");

 $timelimit = $maxLockFile; ## 2 minutes
 $done=0;
 $repRed1=0;$toPrint1=0;
 $repRed2=0;$toPrint2=0;
 while(($timelimit >=0)&&(!$done)){
   $toPrint1="";$toPrint2="";
   sleep 2; $timelimit-=2;
   if((!$repRed1)&&(-e "report1.txt")){ ## read data such as size of matrix...
     if(open(INREP, "<report1.txt")){
       $repRed1=1;
       my @repall=<INREP>;
       close(INREP);
       chomp @repall;
       my $repall = join(" ",@repall);
       $repall=~/=.*\"(.*)\"/;
       $toPrint1="$1\n";
       $toPrint1=~s/\\//g;
     }
   }
   if((!$repRed2)&&(-e "report2.txt")){ ## read data such as size of matrix...
     if(open(INREP, "<report2.txt")){
       $repRed2=1;
       my @repall=<INREP>;
       close(INREP);
       chomp @repall;
       my $repall = join(" ",@repall);
       $repall=~/=.*\"(.*)\"/;
       $toPrint2=" $1\n";
       $toPrint2=~s/\\//g;
     }
   }
   if(-e $donefile){
     $done="success";
     ### check to abort mathematica program...
   }else{
   #  if($toPrint1){print "<p>", $toPrint1, "</p>\n"; system("./myflush");}
   #  if($toPrint2){print "<p>", $toPrint2, "</p>\n"; system("./myflush");}
   }
 }
 if($timelimit<0){ #### need to abort mathematica program....
    system("./findAndAbortMath");
    unlink $lockfile;
    return "Mathematica program aborted.  Time limit of $maxLockFile seconds exceeded.
             (Either the calculation was too big, or there was a problem with the calculation.
              If you suspect there is a problem, please report this problem to $emailLink.)
             (or try again.  If successful after several tries, and if the calculation is
              important to you, you may ask $emailLink to do the calculation for you
              on a bigger faster machine [this webserver is a few years old])";
 }

 ###system("./math < $mafile");  ## for diagnostics of subroutine logic
 ###system("sleep 2");  ## simulation only

 #### Run mathematica in background.


 #### read mathoutput file, delete mathlock, and parse
 if(!open(IN, "<$outputfile")){
   unlink $lockfile;
   return "Error in reading output of mathematica program. Report this problem to $emailLink.".
     "(matchcode = $matchcode)";
 }
 my @all = <IN>;
 close(IN);
 ##$timeSpent="Time spent on calculation: ".(int(24*3600*(-M $lockfile)))." seconds.";
 ##$timeSpent="Time spent on calculation: ".(int(24*3600*(-C $lockfile)))." seconds.". 
     ###"diag: ".$lockfile." ".(-e $lockfile)."/".(-C $lockfile)."/".$timelimit."/";
 $timeSpent="Time spent on calculation: ".($maxLockFile-$timelimit)." seconds.";
 unlink $lockfile;
 if(-e $lockfile){sleep 1; unlink $lockfile;}
 chomp @all;
 my $all = join(" ", @all);
 $all=~s/\\ //g;  ### Delete \\ in case of input spread over multiple lines
 if($all =~/error/i){  ###

  $all=~/=.*\"(.*)\"/;
  return $1;
 
 }else{
 $tsp=($maxLockFile-$timelimit);
 ($x,$z,$y) = split(/=/, $all);
 $y=~s/\}/ \,$tsp\}/;

    $z=~/\"(.*)\"/;
    $redMatchcode=$1;
    if($redMatchcode eq $matchcode){
       @x = $y=~/(\d+\.*\d*)/g;
       @probs=@x[0..11];
       @lens=@x[12..23];
       $MatrixSize=$x[24];
       @survivors1=@x[25..36];
       @survivors2=@x[37..48];


       #### append result to datafile
       if(open(OUT, ">>$datafile")){
         print OUT qq^res["$matchcode"]=$y;\n^;
         close(OUT);
       }
    }else{
      return "Error in processing output.  Coincidental error because two users interfered with each other?".
        " Please try again or report this error to $emailLink. ".
     "(matchcode1 = $redMatchcode), (matchcode2 = $matchcode)";

    }

 return "";

 }
}
#################################################################
sub encodeMatchup{
 my %f=@_;
 my $h1="H"; if($formHash{psquad} eq "squad"){$h1="S";}
 my $h2="H"; if($formHash{qsquad} eq "squad"){$h2="S";}

 my $ans = "$h1$h2$f{p1}-$f{q1}-$f{p2}-$f{q2}-$f{p3}-$f{q3}-$f{ps}-$f{qs}-";
 my @y=();
 my $i; my $x;
 for($i=0;$i<@Abilities;$i++){
  $x="p".($i+4); if(exists $f{$x}){push @y, 1;}else{push @y, 0;}
  $x="q".($i+4); if(exists $f{$x}){push @y, 1;}else{push @y, 0;}
 }
 my $y =join("",@y);
 $y=~s/0+$//;  # Truncate last zeroes.  This will make for backwards compatibility in the future.
 $ans .= $y."-$f{p0}-$f{q0}";

 my $mel = "-$f{pmoff}-$f{qmoff}-$f{pmdef}-$f{qmdef}";

  ## append initiative enhancements .............................................
 my $initEnh="-$f{pminit}-$f{qminit}";
 if($initEnh eq "-0-0"){$initEnh="";}

 my $attLim="-$f{pattlim}-$f{qattlim}".$initEnh;
 if($attLim eq "-0-0"){$attLim="";}
 $mel.=$attLim;

 if($mel eq "-0-0-0-0"){$mel="";}
 return $ans.$mel;


}
#################################################################
sub convertNN{
 my ($dp)=@_;
 if($dp=~/n/){$dp=~/(\d+)/;$dp=-$1;}
 return $dp;
}

sub makeMathInput{
 my %f = @_;
 my $all="";
 my $v; my $i; my ($x,$y);
 $v="False"; if($f{psquad} eq "squad"){$v="True";}
 $all.=qq^IsSquad[1]=$v;\n^;
 $v="False"; if($f{qsquad} eq "squad"){$v="True";}
 $all.=qq^IsSquad[2]=$v;\n^;

 $v="False"; if($f{psquad} eq "squad"){
   $v=$IsSquadForPowers{$f{ps}};
 }
 $all.=qq^IsSquadForPowers[1]=$v;\n^;
 $v="False"; if($f{qsquad} eq "squad"){
   $v=$IsSquadForPowers{$f{qs}};
 }
 $all.=qq^IsSquadForPowers[2]=$v;\n^;

 my($dp,$dq)=($f{p0},$f{q0});
 $dp=$D20hash{$dp};
 $dq=$D20hash{$dq};
 

$all.=qq^
Life[1]=$f{p1};
Life[2]=$f{q1};
Attack[1]=$f{p2};
Attack[2]=$f{q2};
Defense[1]=$f{p3};
Defense[2]=$f{q3};
SquadSize[1]=$squadSize{$f{ps}};
SquadSize[2]=$squadSize{$f{qs}};
D20Enhancement[1]=$dp;
D20Enhancement[2]=$dq;
AttackEnhancement[1]=$f{pmoff};
AttackEnhancement[2]=$f{qmoff};
DefenseEnhancement[1]=$f{pmdef};
DefenseEnhancement[2]=$f{qmdef};
AttackerLimit[1]=$f{pattlim};
AttackerLimit[2]=$f{qattlim};
InitiativeEnhancement[1]=$f{pminit};
InitiativeEnhancement[2]=$f{qminit};
matchcode="$matchcode";
^;
 for($i=0;$i<@Abilities;$i++){
  $y=$AbilitiesMa[$i];
  $x="p".($i+4);$v="False"; if(exists $f{$x}){$v="True";}
  $all.=qq^${y}[1]=$v;\n^;
  $x="q".($i+4);$v="False"; if(exists $f{$x}){$v="True";}
  $all.=qq^${y}[2]=$v;\n^;
 }
 $all.=qq^Get["hero-in.ma"];
DoTheCalculation;
Quit
^;
 if(open(OUT, ">$mafile")){
  print OUT $all;
  close(OUT);
  return "";
 }else{
   return "Error making Mathematica input file.";
 }
}
#################################################################
sub initializeJS{
  $js=qq^<script language="javascript">\n^;
  my $isH = "";
  my $isSM="";
  my $isU="";
  my $indexToName="";
  my $pts="";
  my $i;

  for($i=0;$i<@zSize;$i++){
    if($zSize[$i] eq "H"){$isH.="isH[$i]=1;\n";}else{$isH.="isH[$i]=0;\n";}
    if(($zSize[$i] eq "S")||($zSize[$i] eq "M")){$isSM.="isSM[$i]=1;\n";}else{$isSM.="isSM[$i]=0;\n";}
    if($zUC[$i] eq "unique"){$isU.="isU[$i]=1;\n";}else{$isU.="isU[$i]=0;\n";}
    $indexToName.=qq^name[$i]="$zName[$i]";\n^;
    $pts.=qq^pts[$i]=$zPoints[$i];\n^;
  }
  $js.=qq^
 var isSM = new Array();
 var isH = new Array();
 var isU = new Array();
 var name = new Array();
 var pts = new Array();
 var i;
 $isH
 $isSM
 $isU
 $pts
 $indexToName
^;
  $js.=qq^function resetp(){myform.quickp.selectedIndex=0;myform.ptp.value="---";resetcolors();}\n^;
  $js.=qq^function resetq(){myform.quickq.selectedIndex=0;myform.ptq.value="---";resetcolors();}\n^;
  $js.=qq^function setColor(id, color){
    document.getElementById(id).style.backgroundColor=color;
  }
  ^;

  $resetAbilitiesColorCode="";
  for($i=4;$i<4+@Abilities;$i++){
    $resetAbilitiesColorCode.=qq^
    if(myform.p$i.checked){
      setColor("ap$i","green");
    }else{
      setColor("ap$i","#33ff33");
    }
    if(myform.q$i.checked){
      setColor("aq$i","red");
    }else{
      setColor("aq$i","#ff5550");
    }
    ^;
  }
  $js.=qq^function resetAbilitiesColors(){
    $resetAbilitiesColorCode
    resetBottomColors();
  }
  function resetBottomColors(){
    if(myform.pmoff.value>0){setColor("apmoff","green");}else{setColor("apmoff", "$color1");}
    if(myform.qmoff.value>0){setColor("aqmoff","red");}else{setColor("aqmoff", "$color2");}
    if(myform.pmdef.value>0){setColor("apmdef","green");}else{setColor("apmdef", "$color1");}
    if(myform.qmdef.value>0){setColor("aqmdef","red");}else{setColor("aqmdef", "$color2");}
    if(myform.pminit.value>0){setColor("apminit","green");}else{setColor("apminit", "$color1");}
    if(myform.qminit.value>0){setColor("aqminit","red");}else{setColor("aqminit", "$color2");}
    if(myform.p0.value>0){setColor("ap0","green");}else{setColor("ap0", "$color1");}
    if(myform.q0.value>0){setColor("aq0","red");}else{setColor("aq0", "$color2");}
    if(myform.pattlim[0].checked){setColor("apattlim0","green");}else{setColor("apattlim0", "$color1");}
    if(myform.qattlim[0].checked){setColor("aqattlim0","red");}else{setColor("aqattlim0", "$color2");}
    if(myform.pattlim[1].checked){setColor("apattlim1","green");}else{setColor("apattlim1", "$color1");}
    if(myform.qattlim[1].checked){setColor("aqattlim1","red");}else{setColor("aqattlim1", "$color2");}
    if(myform.pattlim[2].checked){setColor("apattlim2","green");}else{setColor("apattlim2", "$color1");}
    if(myform.qattlim[2].checked){setColor("aqattlim2","red");}else{setColor("aqattlim2", "$color2");}
  }
  function setWhichColor(ch, id, c1, c2){
    if(ch.checked){setColor(id,c1);}else{setColor(id,c2);}
  }
  ^;
  $js.=qq^function resetcolors(){
    if(myform.psquad[0].checked){
      document.getElementById("cellherop").style.backgroundColor="green";
      document.getElementById("cellsquadp").style.backgroundColor="#33ff33";
      document.getElementById("cellhero2p").style.backgroundColor="green";
      document.getElementById("cellsquad2p").style.backgroundColor="#33ff33";
    }else{
      document.getElementById("cellherop").style.backgroundColor="33ff33";
      document.getElementById("cellsquadp").style.backgroundColor="green";
      document.getElementById("cellhero2p").style.backgroundColor="33ff33";
      document.getElementById("cellsquad2p").style.backgroundColor="green";
    };
    if(myform.qsquad[0].checked){
      document.getElementById("cellheroq").style.backgroundColor="red";
      document.getElementById("cellsquadq").style.backgroundColor="#ff5550";
      document.getElementById("cellhero2q").style.backgroundColor="red";
      document.getElementById("cellsquad2q").style.backgroundColor="#ff5550";
    }else{
      document.getElementById("cellheroq").style.backgroundColor="#ff5550";
      document.getElementById("cellsquadq").style.backgroundColor="red";
      document.getElementById("cellhero2q").style.backgroundColor="#ff5550";
      document.getElementById("cellsquad2q").style.backgroundColor="red";
    };
}^;

  $js.=makeResetFunction("p","q");
  $js.=makeResetFunction("q","p");
  $js.=makeClearQuickFunction("p");
  $js.=makeClearQuickFunction("q");
  $js.=makeChangeQuickFunction("p","q");
  $js.=makeChangeQuickFunction("q","p");
  $js.=qq^
function checkSpecial(){
 checkChompp();
 checkChompq();
 checkParalyzingGazep();
 checkParalyzingGazeq();
 checkMindShacklep();
 checkMindShackleq();
 checkDenrickp();
 checkDenrickq();
}
^;
  $js.=qq^</script>\n^;
}

sub makeClearQuickFunction{
 my ($p)=@_;
 my ($i, $x);
 my $ans=qq^function clearQuick$p(){\n^;
 foreach $x (@AbilitiesMa){
   $ans.=qq^myform.$p$AbilityToN{$x}.checked=false;\n^;
 }
 return $ans."}\n";
}

sub makeResetFunction{
 my ($p,$q)=@_;
 my ($i, $x);
 my $ans=qq^

function resetChomp$p(){
  reset$p();
  checkChomp$p();
}
function checkChomp$p(){
 var n;
 if(myform.$p$AbilityToN{"Chomp"}.checked){
   n = myform.quick$q.selectedIndex-1;
   var morestr="";
   if(n>=0)if(!isSM[n]){
     if((myform.quick$p.selectedIndex-1)==$zNameToIndex{Grimnak})
       {morestr=" You might change \\"Grimnak\\" to  \\"Grimnak (vs Large or Huge)\\".";}
     alert("Warning: Chomp has been selected but "+
      name[n]+" is not small or medium."+morestr);
   }
 }else if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Grimnak (vs Large or Huge)"}){
   n = myform.quick$q.selectedIndex-1;   
   if(n>=0)if(isSM[n]){
     alert("Warning: \\"Grimnak (vs Large or Huge)\\" has been selected but "+
      name[n]+" is not Large or Huge, so you should select plain \\"Grimnak\\".");
   }
 }
}

function resetParalyzingGaze$p(){
  reset$p();
  checkParalyzingGaze$p();
}
function checkParalyzingGaze$p(){
 var n;
 if(myform.$p$AbilityToN{"ParalyzingGaze"}.checked){
   n = myform.quick$q.selectedIndex-1;
   var morestr="";
   if(n>=0)if(!isSM[n]){
     if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Me-Burq-Sa"})
       {morestr=" You might change \\"Me-Burq-Sa\\" to  \\"Me-Burq-Sa (vs Large or Huge)\\".";}
     alert("Warning: Paralyzing Stare has been selected but "+
      name[n]+" is not small or medium."+morestr);
   }
 }else if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Me-Burq-Sa (vs Large or Huge)"}){
   n = myform.quick$q.selectedIndex-1;   
   if(n>=0)if(isSM[n]){
     alert("Warning: \\"Me-Burq-Sa (vs Large or Huge)\\" has been selected but "+
      name[n]+" is not Large or Huge, so you should select plain \\"Me-Burq-Sa\\".");
   }
 }
}



function resetMindShackle$p(){
  reset$p();
  checkMindShackle$p();
}

function checkMindShackle$p(){
 var n;
 if(myform.$p$AbilityToN{"MindShackle"}.checked){
   n = myform.quick$q.selectedIndex-1;
   var morestr="";
   if(n>=0)if(!isU[n]){
     if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Ne-Gok-Sa"})
       {morestr=" You might change \\"Ne-Gok-Sa\\" to  \\"Ne-Gok-Sa (vs common)\\"";}
     alert("Warning: Mind Shackle has been selected but "+
      name[n]+" is not unique.");
   }
 }else if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Ne-Gok-Sa (vs common)"}){
   n = myform.quick$q.selectedIndex-1;   
   if(n>=0)if(isU[n]){
     alert("Warning: \\"Ne-Gok-Sa (vs common)\\" has been selected but "+
      name[n]+" is unique, so you should select plain \\"Ne-Gok-Sa\\".");
   }
 }

}

function checkDenrick$p(){
 var n;
 if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Sir Denrick"}){
   n = myform.quick$q.selectedIndex-1;   
   if(n>=0)if(isH[n]){
     alert("Warning: \\"Sir Denrick\\" has been selected but "+
      name[n]+" is Huge, so you should select \\"Sir Denrick (vs Huge)\\".");
   }
 }else if((myform.quick$p.selectedIndex-1)==$zNameToIndex{"Sir Denrick (vs Huge)"}){
   n = myform.quick$q.selectedIndex-1;   
   if(n>=0)if(!isH[n]){
     alert("Warning: \\"Sir Denrick (vs Huge)\\" has been selected but "+
      name[n]+" is not Huge, so you should select plain \\"Sir Denrick\\".");
   }
 }

}

function resetFigures$p(){
 var n;
 n = myform.quick$p.selectedIndex-1;
 if(n>=0)if(myform.${p}squad[1].checked){
   var p = Math.round(pts[n]*parseFloat(myform.squad${p}1.value)/parseFloat(myform.${p}s.value));
   myform.pt$p.value=p;
 }
 if(myform.${p}squad[0].checked){
   myform.${p}squad[1].checked=true;
   reset$p();
 }
}


^;

 return $ans;
}

sub makeChangeQuickFunction{
 my ($p,$q)=@_;
 my ($i, $x);
 my $ans=qq^\nfunction changeQuick$p(){\n^;
 $ans.=qq^var n = myform.quick$p.selectedIndex-1;if(n>=0){clearQuick$p();}\n^;
 for($i=0;$i<@zName;$i++){
  $ans.=qq^if(n==$i){^;
  foreach $x (split(/:/, $zAbilities[$i])){
    if(exists $AbilityToN{$x}){
      $ans.=qq^myform.$p$AbilityToN{$x}.checked=true;^;
    }
  }

  if($zHS[$i] eq "hero"){
    $ans.=qq^myform.${p}squad[0].checked=true;^;
    $ans.=qq^myform.hero${p}1.value="$zLife[$i]";^;
  }
  else{
   $ans.=qq^myform.${p}squad[1].checked=true;^;
   $ans.=qq^myform.squad${p}1.value="$zLife[$i]";^;
   $ans.=qq^myform.${p}s.value="$zLife[$i]";^;
  }
  $ans.=qq^myform.${p}2.value="$zAtt[$i]";^;
  $ans.=qq^myform.${p}3.value="$zDef[$i]";^;
  $ans.=qq^myform.pt${p}.value="$zPoints[$i]";^;

  $ans.=qq^myform.${p}moff.value="0";^;
  $ans.=qq^myform.${p}mdef.value="0";^;
  $ans.=qq^myform.${p}minit.value="0";^;
  $ans.=qq^myform.${p}0.value="0";^;
  $ans.=qq^myform.${p}moff.value="$zAttEnh[$i]";^;
  $ans.=qq^myform.${p}mdef.value="$zDefEnh[$i]";^;
  $ans.=qq^myform.${p}minit.value="$zInitEnh[$i]";^;
  $ans.=qq^myform.${p}0.value="$zD20Enh[$i]";^;
  $ans.=qq^resetcolors();resetAbilitiesColors();^;

  $ans.=qq^}\n^;
 }
 return $ans."if(n>=0){checkSpecial();}\n}\n";
}
#################################################################
#################################################################
# end app logic functions
# begin toolkit functions
#################################################################
#################################################################


#################################################################
sub errorPage {
 my $message = $_[0];  # optional message parameter
 
 print<<ALL;
 <html><head><title>Server Error</title></head><body>
  <h2>Server Error Encountered</h2>
  $message 
  
  If the problem persists, please notify $emailLink
 </body></html>
ALL
 
	exit;   
}

##################################################################
# OLD SUBROUTINES no longer used, will eventually delete
#################################################################





