Guest User

ChiSim.cpp (fixed)

a guest
Feb 26th, 2013
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PROVIDED BY: M. Scott Leuthaeuser
  2. // MODIFIED BY: Sasazuka
  3. // CONTACT E-MAIL: michael (dot) leuthaeuser [at] gmail (dot) com
  4. // FILE: ChiSim.cpp
  5. // VERSION: 0.1.0.B
  6. // PURPOSE: Simulate Enveloping Mist uptime under ideal conditions
  7. //          Includes average Chi generated using new 5.2 model
  8.  
  9. // DIRECTIVES -----------------------------------------------------------------
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <ctime>
  14. #include <string>
  15.  
  16. using namespace std;
  17. // ----------------------------------------------------------------------------
  18.  
  19. // STRUCTURES------------------------------------------------------------------
  20. struct Sim
  21. {
  22.   float time;
  23.   int chiGenerated;
  24.   int chiWasted;
  25.   float downtime;
  26. };
  27. // ----------------------------------------------------------------------------
  28.  
  29. // GLOBAL CONSTANTS -----------------------------------------------------------
  30.  
  31. // Probability of generating 1 Chi each tick
  32. const int G_CHI_PROBABILITY = 15;
  33.  
  34. // Increase in probability when a tick doesn't generate Chi
  35. const int G_CHI_PROBABILITY_MOD = 15;
  36.  
  37. // Number of iterations
  38. const int G_ITERATIONS = 100000;
  39.  
  40. // Average total sim duration
  41. const float G_TIME = 300.0;
  42.  
  43. // Variation on total sim duration as a percent of G_TIME
  44. const float G_VARIANCE = 0.2;
  45.  
  46. // Enveloping duration in ticks
  47. const float G_DURATION = 7.0;
  48.  
  49. // Number of ticks you can fresh early and still get full duration
  50. const float G_REFRESH = 1.0;
  51.  
  52. // Max Chi pool
  53. const int G_CHI_MAX = 4;
  54.  
  55. // How much Chi you start with
  56. const int G_CHI_INIT = 0;
  57.  
  58. // How much Chi Enveloping consumes
  59. const int G_CHI_REQ = 3;
  60. // ----------------------------------------------------------------------------
  61.  
  62. // FUNCTION PROTOTYPES --------------------------------------------------------
  63. Sim simulate( float time, bool useNewModel );
  64. bool generateChi( bool useNewModel );
  65. bool generateChi( bool useNewModel, bool resetProbability );
  66. // ----------------------------------------------------------------------------
  67.  
  68. // ============================================================================
  69. int main( int argc, char* argv[] ) {
  70.   Sim total, iteration;
  71.   bool useNewModel = false;
  72.  
  73.   if ( argc > 1 ) {
  74.     useNewModel = true;
  75.   }
  76.  
  77.   total.time = 0;
  78.   total.chiGenerated = 0;
  79.   total.chiWasted = 0;
  80.   total.downtime = 0;
  81.  
  82.   srand( time(0) );
  83.  
  84.   // Simulate
  85.   for( int c = 0; c < G_ITERATIONS; c++ ) {
  86.     iteration = simulate(
  87.         G_TIME * ( 1.0 + ( G_VARIANCE * 2 * ( ( rand() % 101 ) / 100.0 ) - G_VARIANCE ) ),
  88.         useNewModel );
  89.      
  90.     total.time += iteration.time;
  91.     total.chiGenerated += iteration.chiGenerated;
  92.     total.chiWasted += iteration.chiWasted;
  93.     total.downtime += iteration.downtime;
  94.   }
  95.  
  96.   // Output results
  97.   cout << "AVERAGE DOWNTIME: "
  98.     << total.downtime / G_ITERATIONS
  99.     << " / " << total.time / G_ITERATIONS
  100.     << " (" << total.downtime / total.time * 100 << "%)" << endl;
  101.  
  102.   cout << "AVERAGE CHI WASTED: "
  103.     << (float) total.chiWasted / G_ITERATIONS
  104.     << " / "
  105.     << (float) total.chiGenerated / G_ITERATIONS
  106.     << " (" << (float) total.chiWasted / (float) total.chiGenerated * 100
  107.     << "%)" << endl;
  108.  
  109.   cout << "USING NEW MODEL: " << ( useNewModel ? "true" : "false" ) << endl;
  110.   cout << "AVG CHI GENERATED: " << total.chiGenerated / (float) G_ITERATIONS << endl;
  111.   cout << "AVG TIME: " << total.time / (float) G_ITERATIONS << endl;
  112.  
  113.   // Pause
  114.   cout << endl << "Press any key to continue..." << endl;
  115.   cin.sync();
  116.  
  117.   // Exit
  118.   return 0;
  119. }
  120. // ============================================================================
  121.  
  122. bool generateChi( bool useNewModel ) {
  123.   return generateChi( useNewModel, false );
  124. }
  125.  
  126. bool generateChi( bool useNewModel, bool resetProbability ) {
  127.   static int probability_modifier = 0;
  128.  
  129.   if ( resetProbability ) {
  130.     probability_modifier = 0;
  131.     return false;
  132.   }
  133.  
  134.   if ( useNewModel ) {
  135.     if ( rand() % 100 < ( G_CHI_PROBABILITY + probability_modifier ) ) {
  136.       probability_modifier = 0;
  137.       return true;
  138.     } else {
  139.       probability_modifier += G_CHI_PROBABILITY_MOD;
  140.     }
  141.   } else {
  142.     return ( rand() % 100 < G_CHI_PROBABILITY );
  143.   }
  144.  
  145.  return false;
  146. }
  147.  
  148. Sim simulate( float time, bool useNewModel ) {
  149.   Sim i;
  150.   i.time = 0;
  151.   i.chiGenerated = 0;
  152.   i.chiWasted = 0;
  153.   i.downtime = 0;
  154.  
  155.   int chi = G_CHI_INIT;
  156.   float remDuration = 0;
  157.  
  158.   //cout << "Time: " << time << endl;
  159.  
  160.   for( i.time; i.time < time; i.time++ ) {
  161. //    cout << "TIME: " << i.time << ", CHI: " << chi << ", DURATION: " << remDuration;
  162.     if( remDuration < G_REFRESH && chi >= G_CHI_REQ ) {
  163. //      cout << " REFRESH!";
  164.       chi -= G_CHI_REQ;
  165.       remDuration += G_DURATION;
  166.     }
  167.    
  168.     if( remDuration == 0 ) {
  169. //      cout << " DOWNTIME!";
  170.       i.downtime++;
  171.     }
  172.    
  173.     if ( generateChi( useNewModel ) ) {
  174.       if(chi == G_CHI_MAX) {
  175. //        cout << " CHI WASTED!";
  176.         i.chiWasted++;
  177.       } else {
  178. //        cout << " CHI GENERATED!";
  179.         chi++;
  180.       }
  181.       i.chiGenerated++;
  182.     }
  183.    
  184. //    cout << endl;
  185.     if( remDuration > 1.0 ) {
  186.       remDuration--;
  187.     } else {
  188.       remDuration = 0;
  189.     }
  190.   }
  191.   generateChi( useNewModel, true );
  192.  
  193.   return i;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment