function showaverages($results) { global $forces, $options; //set colors and units in the next if-then for rulesets on PHP side...uses the CSS $className = ''; if (isset($_REQUEST['ruleset'])) { if ($_REQUEST['ruleset'] == 'Classic') { $className = 'cla'; } if ($_REQUEST['ruleset'] == 'Revised') { $className = 'rev'; } if ($_REQUEST['ruleset'] == 'AA50') { $className = 'aa50'; } } $output = ''; $sides = array('att' => 'Attacker', 'def' => 'Defender'); $statkeys = array('count', 'cost', 'punch'); $output .= '<h2>Average outcome of ' . number_format($_REQUEST['reps']) . ' battles</h2><b>Attacker: </b>' . showforce($forces['att']) . '<b> v. Defender: </b>' . showforce($forces['def']) . ' ' . scenariolink('web'); $output .= '<p>Average battle duration: <b>' . $results['duration'] . '</b> rounds of combat</p>'; $breaks = array('att' => array(), 'def' => array()); if (!has_sea($forces['def']) && can_bombard($forces['att'])) { //Amphibious assault unset($forces['att']['Des']); unset($forces['att']['Cru']); unset($forces['att']['Bat']); $output .= "<b>Note:</b> Bombarding units are included in the statistics, although not reported in count/cost/punch/units.<br />"; } $benchmarks = calibrate($forces); $graphstyles = ' display: block; float: left; font-size: 60%; text-align: center; padding: 0; margin: 0; height:15px; border: 1px solid grey; '; $output .= '<table border=0><tr><th> </th> <th>avg. # units left<!--<div style="font-size: 60%">scale: ' . $benchmarks['count'] . '</div>--></th> <th>IPC value <!--<div style="font-size: 60%">scale: ' . $benchmarks['cost'] . '</div>--></th> <th>Punch <!--<div style="font-size: 60%">scale: ' . $benchmarks['punch'] . '</div>--></th> </tr>'; $gross = array(); #debugarray($results); $stats = array(); foreach ($sides as $side => $name) { // show both sides arsort($results[$side]); $totals = array('count' => 0, 'cost' => 0, 'punch' => 0, 'wins' => 0); #debugarray ($results[$side]); $percentpoint = 0; foreach ($results[$side] as $index => $data) { $wins = round($data['total'] / $_REQUEST['reps'] * 100, 2); $lastpercent = $percentpoint; $percentpoint += $wins; $units = $data['stats']['count']; $lossdata = assess($data['lost'], substr(strtolower($name), 0, -2)); #debug ("$wins, total $percentpoint, $index"); #debug($percentpoint); //the 1st SD includes 68% of the results centered about the mean or median $stdev1 = $percentpoint >= 84 && $lastpercent < 84 || $percentpoint >= 16 && $lastpercent < 16; //the 2nd SD includes 94% of the results centered about the mean or median $stdev2 = $percentpoint >= 97 && $lastpercent < 97 || $percentpoint >= 3 && $lastpercent < 3; $median = $percentpoint >= 50 && $lastpercent < 50; if ($wins > 0) { $breaks[$side][$index] = probgraph($wins, showforce($data['force']), $units, showforce($data['lost']), $lossdata['cost'], $median, $stdev1, $stdev2); } foreach ($statkeys as $key) { $totals[$key] += $data['stats'][$key] * $wins; } $totals['wins'] += $wins; } #debugarray ($breaks[$side]); $astats = array(); foreach ($statkeys as $key) { if ($totals['wins'] !== 0) { $astats[$key] = round($totals[$key] / 100, 1); # if ($side=='att'){$astats['punch']=$astats['opunch'];}else{$astats['punch']=$astats['dpunch'];} } else { $astats[$key] = 0; } } $stats[$side] = $astats; //save for later $mode = strtolower(substr($sides[$side], 0, -2)); //gets 'attack' or 'defend' from "Attacker" or "Defender" $astartstats = assess($forces[$side], $mode); #if ($side=='att'){$astartstats['punch']=$astartstats['opunch'];}else{$astartstats['punch']=$astartstats['dpunch'];} $aloststats = array(); foreach ($statkeys as $key) { $aloststats[$key] = $astartstats[$key] - $astats[$key]; } if ($totals['wins'] > 100) { $totals['wins'] = 100; } $gross[$side] = round($totals['wins'], 1); $survive = '<th class="' . $side . $className . '">' . $name . ':</th>'; if ($totals['wins'] < 99) { $losses = 100 - $totals['wins']; $stdev2 = $losses >= 3; //2nd standard deviation contains about 95% of the results. thus, 50-(95/2) = 3% when rounded up $stdev1 = $losses >= 16; //1st standard deviation contains about 68% of the results. thus, 50-(68/2) = 16% $median = $losses >= 50; $width = width($losses, 50); $lossdata = assess($forces[$side], substr(strtolower($name), 0, -2)); $breaks[$side][0] = probgraph($losses, 'no units.', 0, showforce($forces[$side]), $lossdata['cost'], $median, $stdev1, $stdev2); #'<!--0--> #<span style="background: grey; width: '.$width.'px; display: block; float: left; border: 1px solid black; margin: 3px 3px 0 0; font-size: 60%;"> </span> #0 units: '.$losses.'%: left with <b>nothing</b>.'; } foreach ($benchmarks as $stat => $bmark) { $afw = width($astats[$stat], $bmark); $alw = width($aloststats[$stat], $bmark); $alossw = width($astartstats[$stat], $bmark); $survive .= '<td class="graph">'; if ($astats[$stat] > 0) { $survive .= '<span class="' . $side . $className . '"style="width:' . $afw . 'px; ' . $graphstyles . '">' . $astats[$stat] . '</span>'; } #<span class="noshow">Left:</span> '; if ($aloststats[$stat] > 0) { $survive .= '<span class="lost" style="' . $graphstyles . ' border: none !important; padding: 1px 0px !important; width:' . ($alw - 1) . 'px;">' . $aloststats[$stat] . '</span>'; } $survive .= '</td>'; } #if ($totals['wins'] > 0) # $ics=$astartstats['count']*$astartstats['punch']; # $survive.= "<td>count: ".$astartstats['count']." punch: ".$astartstats['punch']. "ICS: $ics</td>"; $output .= '<tr>' . $survive . '</tr>'; } $output .= ' <tr><td> </td><td><span class="att' . $className . '" style="display: block; float: left; border: 1px solid black; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%;">#</span> Surviving Attackers <br /> </td> <td valign="top"><span class="def' . $className . '" style="display: block; float: left; border: 1px solid black; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%;">#</span> Surviving Defenders</td><td><span class="lost" style="display: block; float: left; border: 1px solid grey; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%; ">#</span>Casualties</td></tr> <tr><td colspan="4"><hr /></td></tr> <tr style="font-weight: bold;"> <th>Overall %*:</th><td class="att' . $className . '" style="font-weight: bold; padding: 0px 4px;">A. survives: ' . $gross['att'] . '%</td> <td class="def' . $className . '" style="font-weight: bold; padding: 0px 4px;">D. survives: ' . $gross['def'] . '%</td> <td class="lost" style="font-weight: bold; padding: 0px 4px;">No one survives: ' . round($results['draw'] / $_REQUEST['reps'] * 100, 1) . '%</td></tr> </table> <em>* percentages may not total 100 due to rounding. The average results from above are<span style="background: yellow;"> highlighted </span>in charts below, while the median result (equal odds of getting a worse or better result) is written in <span style="color: red">red</span>. If shown, the 1st and 2nd standard deviations about the mean are represented in <span style="color: blue">blue</span> and <span style="color: dodgerblue">light blue</span>.</em> <table>'; #debugarray ($results); foreach ($sides as $side => $name) { $output .= '<tr><th colspan="4">' . $name . ' results:<hr /></td></tr> <tr><th style="text-align: right;">Probability</th><th style="text-align: left;">%</th><th style="text-align: right;">#</th><th style="text-align: left">units</th><th>/ losses</th></tr> '; # <tr><td>'; # foreach ($breaks[$side] as $line) $output .= "<tr>$line</tr>"; #$output .= '</ul></td><td><h3>By remaining count</h3><ul>'; natcasesort($breaks[$side]); #debug ('Stats:'); #debugarray ($stats[$side]); #debug ('Avg. Count for '.$side.': '.$stats[$side]['count']); $breaks[$side] = array_reverse($breaks[$side]); #debugarray ($r); #debug (count($breaks[$side])); foreach ($breaks[$side] as $index => $line) { $color = 'background: #eef;'; $avgcount = round($stats[$side]['count']); $count = 0; if ($index !== 0) { $count = $results[$side][$index]['stats']['count']; } if ($count > $avgcount - 1.0 and $count < $avgcount + 1.0) { $color = 'background: yellow;'; } if ($count !== 0 && $color == '') { $countratio = $avgcount / $count; if ($countratio >= 0.9 and $countratio <= 1.1) { $color = 'background: yellow;'; } } $output .= "<tr style=\"font-size: 60%; {$color}\">{$line}</tr>\n\t\t\t"; } $output .= '<tr><td colspan=4><hr /></td></tr>'; } $output .= '</table>'; global $rounds; #if ($rounds == '') $output .= showipcdiffs($results['ipcdiff']); #debug ($rounds); $output .= scenariolink('mail'); return $output; }
} //condition to void subs dice rolls for an only sub attack force vs an only air defense force. if (has_sub($att[$t]['force']) && !nonsubs($att[$t]['force']) && has_air($def[$t]['force']) && !nonair($def[$t]['force'])) { $outcome['att']['vals'] = array(); } //condition to void subs dice rolls for an only air attack force vs an only sub defense force. if (has_sub($def[$t]['force']) && !nonsubs($def[$t]['force']) && has_air($att[$t]['force']) && !nonair($att[$t]['force'])) { $outcome['def']['vals'] = array(); } $history[$t] = $outcome; $stalemate = $_REQUEST['luck'] == 'none' && $t > 0 && $outcome['att']['vals']['totalhits'] + $outcome['def']['vals']['totalhits'] == 0; $t++; $att[$t]['force'] = $outcome['att']['force']; $def[$t]['force'] = $outcome['def']['force']; $astats = assess($att[$t]['force'], 'attack'); $dstats = assess($def[$t]['force'], 'defend'); $abort = ($astats['count'] - round(($dstats['punch'] + 3) / 6) < $saveunits && $saveunits > 0 or $dstats['count'] - round(($astats['punch'] + 3) / 6) < $strafeunits && $strafeunits > 0 or $dstats['punch'] > 0 and $astats['punch'] / $dstats['punch'] * 100 < $abortratio); if (!$stalemate) { if (!has_sea($def[$t]['force']) && (!nonsubs($att[$t]['force']) or isset($_REQUEST['asubschicken'])) && has_sub($att[$t]['force'])) { $submerged['att'] = array(); if (isset($att[$t]['force']['Sub'])) { $submerged['att']['Sub'] = $att[$t]['force']['Sub']; } if (isset($att[$t]['force']['SSub'])) { $submerged['att']['SSub'] = $att[$t]['force']['SSub']; } $att[$t]['force'] = submerge($att[$t]['force']); } else { if (!has_sea($att[$t]['force']) && (!nonsubs($def[$t]['force']) or isset($_REQUEST['dsubschicken'])) && has_sub($def[$t]['force'])) { $submerged['def'] = array(); if (isset($def[$t]['force']['Sub'])) {