Advertisement
syntax53

Untitled

Apr 4th, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.44 KB | None | 0 0
  1. <?php
  2.  
  3. function p($left,$right) {
  4.     echo $left;
  5.     if (!empty($right)) echo str_repeat(' ', 20-strlen(strip_tags($left))).$right;
  6.     echo "\r\n";
  7. }
  8.  
  9. function damage($d) {
  10.     return '<span style="color:#FD0004;">'.$d.'</span>';
  11. }
  12.  
  13. function AddAttack($name, $type, $energy, $min, $max, $attack_chance, $success_chance) {
  14.     global $attacks;
  15.     $i = count($attacks)+1;
  16.     $attacks[$i]['name'] = $name;
  17.     $attacks[$i]['type'] = $type;
  18.     $attacks[$i]['energy'] = $energy;
  19.     $attacks[$i]['min'] = $min;
  20.     $attacks[$i]['max'] = $max;
  21.     $attacks[$i]['attack_chance'] = $attack_chance;
  22.     $attacks[$i]['success_chance'] = $success_chance;
  23.     $attacks[$i]['used'] = 0;
  24.     $attacks[$i]['hits'] = 0;
  25.     $attacks[$i]['no_energy'] = 0;
  26.     $attacks[$i]['remaining_energy_yes'] = 0;
  27.     $attacks[$i]['remaining_energy_no'] = 0;
  28.     $attacks[$i]['total_damage'] = 0;
  29. }
  30.  
  31. $energy_per_round = 1000;
  32. $remaining_energy = 0;
  33. $number_of_rounds = 100;
  34. $attacks = array();
  35. $max_round = 0;
  36. $character_ac = 0;
  37. $character_dr = 0;
  38.  
  39. if (isset($_POST['name'])) {
  40.     foreach ($_POST['name'] as $index => $value) {
  41.         $name = (string)preg_replace("/[^A-Za-z0-9]/", '', $_POST['name'][$index]);
  42.         $type = (string)$_POST['type'][$index] == 'spell' ? 'spell' : 'physical';
  43.         $energy = (int)preg_replace("/[^0-9]/", '', $_POST['energy'][$index]);
  44.         $min = (int)preg_replace("/[^0-9]/", '', $_POST['min'][$index]);
  45.         $max = (int)preg_replace("/[^0-9]/", '', $_POST['max'][$index]);
  46.         $attack_chance = (int)preg_replace("/[^0-9]/", '', $_POST['attack_chance'][$index]);
  47.         $success_chance = (int)preg_replace("/[^0-9]/", '', $_POST['success_chance'][$index]);
  48.         if (!empty($name) && $energy > 0 && $max > 0 && $attack_chance > 0 && $success_chance > 0) {
  49.             AddAttack($name,$type,$energy,$min,$max,$attack_chance,$success_chance);
  50.         }
  51.     }
  52.    
  53.     $energy_per_round = (int)preg_replace("/[^0-9]/", '', $_POST['energy_per_round']);
  54.     if ($energy_per_round > 10000) $energy_per_round = 10000;
  55.     if ($energy_per_round < 1) $energy_per_round = 1;
  56.    
  57.     $number_of_rounds = (int)preg_replace("/[^0-9]/", '', $_POST['number_of_rounds']);
  58.     if ($number_of_rounds > 100000) $number_of_rounds = 50000;
  59.     if ($number_of_rounds < 1) $number_of_rounds = 1;
  60.    
  61.     $character_ac = (int)preg_replace("/[^0-9]/", '', $_POST['character_ac']);
  62.     if ($character_ac > 10000) $character_ac = 10000;
  63.     if ($character_ac < 0) $character_ac = 0;
  64.    
  65.     $character_dr = (int)preg_replace("/[^0-9]/", '', $_POST['character_dr']);
  66.     if ($character_dr > 10000) $character_dr = 10000;
  67.     if ($character_dr < 0) $character_dr = 0;
  68.    
  69. } else {
  70.     //          name        type        energy  min max attack% success%
  71.     AddAttack(  'physical', 'physical', 200,    10, 20, 45,     99);
  72.     AddAttack(  'lbol',     'spell',    400,    5,  12, 35,     75);
  73.     AddAttack(  'sbol',     'spell',    600,    10, 22, 20,     50);
  74. }
  75.  
  76. ?>
  77. <HTML>
  78. <HEAD>
  79. <style>
  80. input[type="number"] {
  81.     width:50px;
  82. }
  83. label {
  84.     font-size:0.8em;
  85. }
  86. </style>
  87. </HEAD>
  88. <BODY>
  89. <form action="monster.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  90. <?php for ($x = 1; $x <= 5; $x++):
  91.     $name = empty($attacks[$x]['name']) ? '' : $attacks[$x]['name'];
  92.     $type = empty($attacks[$x]['type']) ? '' : $attacks[$x]['type'];
  93. ?>
  94.    
  95.     <strong>Attack <?php echo $x; ?>:</strong>
  96.   <label for="name<?php echo $x; ?>">Name:</label><input name="name[]" type="text" id="name<?php echo $x; ?>" size="7" value="<?php echo $name; ?>">
  97.   <label for="type<?php echo $x; ?>">Type:</label>
  98.   <select name="type[]" id="type<?php echo $x; ?>">
  99.     <option value="spell" <?php if ($type=='spell') echo 'selected'; ?>>Spell</option>
  100.     <option value="physical" <?php if ($type=='physical') echo 'selected'; ?>>Physical</option>
  101.   </select>
  102.     <?php foreach (array('energy', 'min', 'max', 'attack_chance', 'success_chance') as $var):
  103.         $$var = empty($attacks[$x][$var]) ? '' : $attacks[$x][$var];
  104.     ?>
  105.   <label for="<?php echo $var.$x; ?>[]"><?php echo $var == 'success_chance' ? 'Accy/Cast%' : $var; ?>:</label> <input name="<?php echo $var; ?>[]" type="number" id="<?php echo $var.$x; ?>" value="<?php echo $$var; ?>">
  106.     <?php endforeach; ?>
  107.     <br>
  108. <?php endfor; ?>
  109. <br>
  110. <label for="energy_per_round">Monster's Energy per Round:</label>
  111. <input type="text" name="energy_per_round" id="energy_per_round" size="5" value="<?php echo $energy_per_round; ?>"> &nbsp;
  112.  
  113. <label for="character_ac">Character AC:</label>
  114. <input type="text" name="character_ac" id="character_ac" size="5" value="<?php echo $character_ac; ?>"> &nbsp;
  115.  
  116. <label for="character_dr">Character DR:</label>
  117. <input type="text" name="character_dr" id="character_dr" size="5" value="<?php echo $character_dr; ?>"> &nbsp;
  118.  
  119. <br><br>
  120. <label for="number_of_rounds"># Rounds:</label>
  121. <input type="text" name="number_of_rounds" id="number_of_rounds" size="5" value="<?php echo $number_of_rounds; ?>"> &nbsp;
  122.  
  123. <input type="submit" name="submit_hide_rounds" id="submit_hide_rounds" value="Execute &amp; Show Stats Only"> &nbsp; <input type="submit" name="submit" id="submit" value="Execute &amp; Show Rounds"> (Under 5,000 rounds)
  124.  
  125. </form>
  126. <?php
  127. //normalize attack % for rand
  128. for ($x = 2; $x <= count($attacks); $x++) {
  129.     $attacks[$x]['attack_chance'] += $attacks[$x-1]['attack_chance'];
  130.     if ($x == count($attacks) && $attacks[$x]['attack_chance'] != 100) p('<span style="font-size:1.5em;font-weight:bold;color:#FF9600;">Attack chances do not total 100%!</span>');
  131. }
  132. ?>
  133. <PRE>
  134. <?php
  135.  
  136. ob_start();
  137. p('==============================================');
  138.  
  139. $total_damage = 0; $total_attacks = 0;
  140. $total_energy_remaining = 0; $max_energy_remaining = 0; $total_energy_used = 0;
  141. $last_attack_energy = 0; $last_attack_type = '';
  142. $energy_remaining_per_attack = 0; $energy_remaining_after_attack = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
  143. for ($round = 1; $round <= $number_of_rounds && !empty($attacks); $round++) {
  144.     p('<strong>ROUND '.$round.'</strong> / Energy: '.$remaining_energy.' + '.$energy_per_round.' = '.($remaining_energy+$energy_per_round));
  145.     $remaining_energy += $energy_per_round;
  146.    
  147.     $x = 1;
  148.     $round_damage = 0;
  149.     while ($x <= 6 /*&& $remaining_energy > 0*/) {
  150.         $attack_chance = mt_rand(1,100);
  151.         $last_attack_energy = 0;
  152.         $last_attack_type = '';
  153.         foreach ($attacks as $attack_num => $attack) {
  154.             if ($attack_chance <= $attack['attack_chance']) {
  155.                 $last_attack_energy = $attack['energy'];
  156.                 $last_attack_type = $attack['type'];
  157.                 if ($remaining_energy < $attack['energy']) {
  158.                    
  159.                     p($attack['name'],'Not enough energy.');
  160.                     $attacks[$attack_num]['no_energy']++;
  161.                     $attacks[$attack_num]['remaining_energy_no'] += $remaining_energy;
  162.                    
  163.                 } else {
  164.                    
  165.                     $attacks[$attack_num]['used']++;
  166.                     $attacks[$attack_num]['remaining_energy_yes'] += $remaining_energy;
  167.                     $total_attacks++;
  168.                    
  169.                     $success_chance = $attack['success_chance'];
  170.                     if ($attack['type'] != 'spell' && $character_ac > 0) {
  171.                         //=((AC*AC)/100)/((ACCY*ACCY)/140)=fail %
  172.                         $success_chance = round(1-(($character_ac*$character_ac)/100)/(($success_chance*$success_chance)/140),2)*100;
  173.                         if ($success_chance < 9) $success_chance = 9;
  174.                         if ($success_chance > 99) $success_chance = 99;
  175.                     }
  176.                    
  177.                     $cast_chance = mt_rand(1,100);
  178.                     if ($cast_chance <= $success_chance) {
  179.                        
  180.                         $damage = mt_rand($attack['min'],$attack['max']);
  181.                         if ($attack['type'] != 'spell') $damage -= $character_dr;
  182.                         if ($damage < 0) $damage = 0;
  183.                        
  184.                         $total_damage += $damage;
  185.                         $round_damage += $damage;
  186.                        
  187.                         $remaining_energy -= $attack['energy'];
  188.                         $total_energy_used += $attack['energy'];
  189.                        
  190.                         $attacks[$attack_num]['hits']++;
  191.                         $attacks[$attack_num]['total_damage'] += $damage;
  192.                         p(damage($attack['name'].' for '.$damage),'Energy used: '.$attack['energy'].' ... Energy remaining: '.$remaining_energy);
  193.                        
  194.                     } else {
  195.                         if ($attack['type'] == 'spell') {
  196.                             $total_energy_used += round($attack['energy']/2,0);
  197.                             $remaining_energy -= round($attack['energy']/2,0);
  198.                            
  199.                             p($attack['name'].' (FAIL)','Energy used: '.round($attack['energy']/2,0).' ... Energy remaining: '.$remaining_energy);
  200.                         } else {
  201.                             $total_energy_used += $attack['energy'];
  202.                             $remaining_energy -= $attack['energy'];
  203.                            
  204.                             p($attack['name'].' (MISS)','Energy used: '.$attack['energy'].' ... Energy remaining: '.$remaining_energy);
  205.                         }
  206.                     }
  207.                 }
  208.                
  209.                 break 1;
  210.             }
  211.         }
  212.         $energy_remaining_per_attack += $remaining_energy;
  213.         $energy_remaining_after_attack[$x] += $remaining_energy;
  214.         if ($last_attack_type != 'spell') {
  215.             if ($remaining_energy < $last_attack_energy) break 1;
  216.         }
  217.         $x++;
  218.     }
  219.    
  220.     p('Damage for round: '.$round_damage.', Energy Remaining: '.$remaining_energy);
  221.     if ($round_damage > $max_round) $max_round = $round_damage;
  222.    
  223.     if ($remaining_energy > $max_energy_remaining) $max_energy_remaining = $remaining_energy;
  224.     $total_energy_remaining += $remaining_energy;
  225.    
  226.     p('==============================================');
  227. }
  228. $attack_html = ob_get_clean();
  229.  
  230. if (!empty($attacks)) {
  231.     p('');
  232.     p('Total Attacks: '.$total_attacks);
  233. }
  234.  
  235. $last_cast_percent = 0;
  236. foreach ($attacks as $attack) {
  237.     p('');
  238.     p('<strong>'.$attack['name'].'--</strong>');
  239.     p('Hits: '.$attack['hits'].', '.($attack['type'] == 'spell' ? 'Fails' : 'Misses').': '.($attack['used']-$attack['hits']).', Success %: '.(round($attack['hits']/$attack['used'],3)*100));
  240.    
  241.     if ($attack['no_energy'] > 0) {
  242.         p('');
  243.         p('Attack chosen but not enough energy: '.(round($attack['no_energy']/($attack['no_energy']+$attack['used']), 4)*100).'%');
  244.         p('Average energy available when attack used: '.(round($attack['remaining_energy_yes']/$attack['used'], 0)));
  245.         p('Average energy available when attack could not be used: '.(round($attack['remaining_energy_no']/$attack['no_energy'], 0)));
  246.     }
  247.    
  248.     p('');
  249.     p('Total Damage: '.$attack['total_damage']);
  250.     p(damage('Average Damage/'.($attack['type'] == 'spell' ? (($attack['used'] > $attack['hits']) ? 'Cast (including fails)':'Cast') : 'Swing').': '.round($attack['total_damage']/$attack['used'],2)));
  251.     p('Average '.($attack['type'] == 'spell' ? 'Casts' : 'Swings').'/Round: '.round($attack['used']/$number_of_rounds,2));
  252.     p(damage('Average Damage/Round: '.round($attack['total_damage']/$number_of_rounds,2)));
  253.     p('Initial Attack Chance: '.($attack['attack_chance']-$last_cast_percent).'%');
  254.     p('<strong><span style="color:#F006FF;">True Attack Chance: '.(round($attack['used']/$total_attacks,3)*100).'%</span></strong>');
  255.     $last_cast_percent = $attack['attack_chance'];
  256. }
  257.  
  258. if (!empty($attacks)) {
  259.     p('');
  260.     p('Total Rounds: '.$number_of_rounds);
  261.     p('Total Damage: '.$total_damage);
  262.    
  263.     p('<h3>'.damage('Total AVG Damage / Round: '.round($total_damage/$number_of_rounds,2)).'</h3>');
  264.    
  265.     p('Max Round Seen: '.$max_round);
  266.    
  267.     p('');
  268.     p('Average Energy Used/Attack: '.round($total_energy_used/$total_attacks,2));
  269.     p('Average Energy Used/Round: '.round($total_energy_used/$number_of_rounds,2));
  270.     p('Average Energy Remaining/Attack: '.round($energy_remaining_per_attack/$total_attacks,2));
  271.     p('Average Energy Remaining/Round: '.round($total_energy_remaining/$number_of_rounds,2));
  272.     p('Max Energy Remaining Seen at End of Round: '.$max_energy_remaining);
  273.    
  274.     p('');
  275.     for ($x = 1; $x <= 6; $x++) {
  276.         p('Average Energy Remaining after atack '.$x.': '.round($energy_remaining_after_attack[$x]/$number_of_rounds,2));
  277.     }
  278.    
  279.     if (!isset($_POST['submit_hide_rounds'])) {
  280.         p('');
  281.         echo var_export($attacks, true)."\r\n";
  282.         if ($number_of_rounds <= 5000) {
  283.             p('');
  284.             echo $attack_html;
  285.         }
  286.     }
  287. }
  288. ?>
  289. </PRE>
  290. </BODY></HTML>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement