Advertisement
Kirkq

DQ3 Simulator v3

Mar 5th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #include <windows.h>
  7. #include <unistd.h>
  8. using namespace std;
  9.  
  10. #define FIELD 256
  11. #define ICE 256
  12. #define DESERT 384
  13. #define FOREST 460
  14. #define SWAMP 460
  15. #define MOUNTAIN 640
  16.  
  17. int ec;
  18.  
  19. //Add lamp of darkness later.
  20.  
  21. typedef enum PathType
  22. {
  23.     PATH_STANDARD = 0,
  24.     PATH_ALTERNATE = 1
  25. } PathType;
  26.  
  27.  
  28. #define ASSERT printf("ASSERT %d\n", __LINE__);cout << "To close type something and hit enter: ";cin >> dumpchar;exit(-1);
  29. //0 to 12288
  30. #define EC_ARRAY_SIZE (49)
  31. #define EC_SAMPLE_SIZE (32768)
  32. #define MAX_NUM_TILES (100)
  33.  
  34. #define ALIAHAN_ENCOUNTER (60 * 15 + 0)  //In frames
  35. #define MAGIC_BALL_ENCOUNTER (60 * 30 + 0)  //In frames
  36. #define ALIAHAN_ALT_PATH_STATIC_TIME (278)  //In frames
  37. #define MAGIC_BALL_ALT_PATH_STATIC_TIME (273)  //In frames
  38. #define OUTDOOR_STEP_TIME (16)  //In frames
  39. #define STEP_TIME (OUTDOOR_STEP_TIME)  //In frames
  40.  
  41. #define ALIAHAN_TEST
  42. //#define MAGIC_BALL_TEST
  43.  
  44. //#define DEBUG
  45.  
  46. static int SpreadArray[EC_ARRAY_SIZE] =
  47. {  9, 9, 42, 69, 105, 171, 208, 253, 317, 362,
  48. 456, 533, 613, 685, 780, 936, 1032, 1065, 1220, 1245,
  49. 1378, 1359, 1462, 1449, 1454, 1351, 1343, 1380, 1401, 1277,
  50. 1220, 1126, 996, 908, 793, 716, 615, 528, 443, 390,
  51. 279, 240, 167, 142, 106, 64, 46, 20, 5};
  52. int ECArray[EC_SAMPLE_SIZE];
  53. int dumpchar;
  54.  
  55. class Tile
  56. {
  57.     public:
  58.         int ec_decrement;
  59.         int encounter_time_loss;
  60.         bool make_break_decision;
  61.         Tile();
  62. };
  63. Tile::Tile(void)
  64. {
  65.     ec_decrement = 0;
  66.     encounter_time_loss = 0;
  67.     make_break_decision = false;
  68. }
  69. void initTile (Tile &Tile, int type, int enc)
  70. {
  71.     Tile.ec_decrement = type;
  72.     Tile.encounter_time_loss = enc;
  73. }
  74.  
  75. Tile TileArray[MAX_NUM_TILES];
  76. Tile TileArrayAlt[MAX_NUM_TILES];   //Tile break attaches to TileArrayAlt[0];
  77. int NumSteps = 0;
  78. int NumAltSteps = 0;
  79. int AltPathTime = 0;
  80.  
  81. void SetupAllTiles()
  82. {
  83.  
  84. #ifdef ALIAHAN_TEST
  85. //Aliahan to Reeve
  86.     int k = 0;
  87.     //The first tile is the first enemy tile
  88.     //The last tile is the last enemy tile (one step from destination)
  89.  
  90.     //MAIN_PATH
  91.     for(int i = 0; i<21; i++)
  92.     {
  93.         if(k >= MAX_NUM_TILES)
  94.         {
  95.             ASSERT;
  96.         }
  97.         initTile(TileArray[k++], FIELD, ALIAHAN_ENCOUNTER);
  98.     }
  99.     for(int i = 0; i<12; i++)
  100.     {
  101.         if(k >= MAX_NUM_TILES)
  102.         {
  103.             ASSERT;
  104.         }
  105.         if(i == 8)
  106.         {
  107.             TileArray[k].make_break_decision = true;
  108.         }
  109.         initTile(TileArray[k++], FOREST, ALIAHAN_ENCOUNTER);
  110.     }
  111.     for(int i = 0; i<4; i++)
  112.     {
  113.         if(k >= MAX_NUM_TILES)
  114.         {
  115.             ASSERT;
  116.         }
  117.         initTile(TileArray[k++], FIELD, ALIAHAN_ENCOUNTER);
  118.     }
  119.     NumSteps = k;
  120.  
  121.     //ALT_PATH
  122.     k = 0;
  123.     for(int i = 0; i<2; i++)
  124.     {
  125.         if(k >= MAX_NUM_TILES)
  126.         {
  127.             ASSERT;
  128.         }
  129.         initTile(TileArrayAlt[k++], FOREST, ALIAHAN_ENCOUNTER);
  130.     }
  131.     NumAltSteps = k;
  132.     AltPathTime = ALIAHAN_ALT_PATH_STATIC_TIME;
  133. #endif
  134. #ifdef MAGIC_BALL_TEST
  135.     //Reeve to Magic Ball Cave
  136.     int k = 0;
  137.     //The first tile is the first enemy tile
  138.     //The last tile is the last enemy tile (one step from destination)
  139.  
  140.     //MAIN_PATH
  141.     for(int i = 0; i<21; i++)
  142.     {
  143.         if(k >= MAX_NUM_TILES)
  144.         {
  145.             ASSERT;
  146.         }
  147.         initTile(TileArray[k++], FIELD, MAGIC_BALL_ENCOUNTER);
  148.     }
  149.     for(int i = 0; i<3; i++)
  150.     {
  151.         if(k >= MAX_NUM_TILES)
  152.         {
  153.             ASSERT;
  154.         }
  155.         initTile(TileArray[k++], FOREST, MAGIC_BALL_ENCOUNTER);
  156.     }
  157.     for(int i = 0; i<1; i++)
  158.     {
  159.         if(k >= MAX_NUM_TILES)
  160.         {
  161.             ASSERT;
  162.         }
  163.         initTile(TileArray[k++], MOUNTAIN, MAGIC_BALL_ENCOUNTER);
  164.     }
  165.     for(int i = 0; i<3; i++)
  166.     {
  167.         if(k >= MAX_NUM_TILES)
  168.         {
  169.             ASSERT;
  170.         }
  171.         initTile(TileArray[k++], FOREST, MAGIC_BALL_ENCOUNTER);
  172.     }
  173.     for(int i = 0; i<9; i++)
  174.     {
  175.         if(k >= MAX_NUM_TILES)
  176.         {
  177.             ASSERT;
  178.         }
  179.         initTile(TileArray[k++], MOUNTAIN, MAGIC_BALL_ENCOUNTER);
  180.     }
  181.     for(int i = 0; i<3; i++)
  182.     {
  183.         if(k >= MAX_NUM_TILES)
  184.         {
  185.             ASSERT;
  186.         }
  187.         initTile(TileArray[k++], FOREST, MAGIC_BALL_ENCOUNTER);
  188.     }
  189.     for(int i = 0; i<7; i++)
  190.     {
  191.         if(k >= MAX_NUM_TILES)
  192.         {
  193.             ASSERT;
  194.         }
  195.         initTile(TileArray[k++], MOUNTAIN, MAGIC_BALL_ENCOUNTER);
  196.     }
  197.     for(int i = 0; i<2; i++)
  198.     {
  199.         if(k >= MAX_NUM_TILES)
  200.         {
  201.             ASSERT;
  202.         }
  203.         if(i == 0)
  204.         {
  205.             TileArray[k].make_break_decision = true;
  206.         }
  207.         initTile(TileArray[k++], FIELD, MAGIC_BALL_ENCOUNTER);
  208.     }
  209.     for(int i = 0; i<3; i++)
  210.     {
  211.         if(k >= MAX_NUM_TILES)
  212.         {
  213.             ASSERT;
  214.         }
  215.         initTile(TileArray[k++], DESERT, MAGIC_BALL_ENCOUNTER);
  216.     }
  217.     NumSteps = k;
  218.  
  219.     //ALT_PATH
  220.     k = 0;
  221.     for(int i = 0; i<1; i++)
  222.     {
  223.         if(k >= MAX_NUM_TILES)
  224.         {
  225.             ASSERT;
  226.         }
  227.         initTile(TileArrayAlt[k++], FIELD, ALIAHAN_ENCOUNTER);
  228.     }
  229.     NumAltSteps = k;
  230.     AltPathTime = MAGIC_BALL_ALT_PATH_STATIC_TIME;
  231. #endif
  232.  
  233.  
  234. }
  235.  
  236. void InitArrays()
  237. {
  238.     int k;
  239.     for(int i = 0; i<EC_ARRAY_SIZE; i++)
  240.     {
  241.         for(int j = 0; j < SpreadArray[i]; j++)
  242.         {
  243.             if(k >= EC_SAMPLE_SIZE)
  244.             {
  245.                 ASSERT;
  246.             }
  247.             ECArray[k++] = i*256;
  248.         }
  249.     }
  250.     //Sanity checks
  251.     if(k != EC_SAMPLE_SIZE || ECArray[0] != 0 || ECArray[EC_SAMPLE_SIZE-1] != 12288)
  252.     {
  253.         ASSERT;
  254.     }
  255.  
  256. #ifdef DEBUG
  257.     for(int i = 0; i<100; i++)
  258.     {
  259.         printf("ECArray[i] = %d\n", ECArray[i]);
  260.     }
  261. #endif
  262. }
  263.  
  264. int GetRandomEC()
  265. {
  266.     int ret = ECArray[rand() % EC_SAMPLE_SIZE];
  267.  
  268.     return ret;
  269. }
  270.  
  271. int Simulate(void)
  272. {
  273.     int last_encounter_tile = -1;
  274.     int ec = GetRandomEC(); //Start
  275.     bool alt_path_taken = false;
  276.     bool printenable = false;
  277.     int time = 0;
  278.     int num_encounters = 0;
  279.    
  280.     //if(ec < 9000) //HACK TEST
  281.     //{
  282.     //    return 0;
  283.     //}
  284.  
  285. //
  286.     ////0 to 12288
  287.     //ec = 512;
  288.     printenable = true;
  289. //
  290.     if(ec == 256 * 48)
  291.     {
  292.         //printf("ec = %d\n", ec);
  293.         //printenable = true;
  294.     }
  295.     if(printenable)
  296.     {
  297.         printf("START ec %d time %d steps 0\n", ec, time);
  298.     }
  299.  
  300.  
  301.     for(int steps = 0; steps < NumSteps; steps++)//Main path steps.
  302.     {
  303.         ec -= TileArray[steps].ec_decrement;  //Take a step
  304.         time += STEP_TIME;
  305.         if(ec <= 0)   //See if we got into an encounter.
  306.         {
  307.             time += TileArray[steps].encounter_time_loss;  //Battle time loss
  308.             ec = GetRandomEC();
  309.             last_encounter_tile = steps;   //This really should be converted to distance covered.
  310.             num_encounters++;
  311.         }
  312.         if(printenable)
  313.         {
  314.             printf("ec %d time %d steps %d\n", ec, time, steps+1);
  315.         }
  316.         //See if we're going onto the alt path.
  317. #if 1
  318.         if(TileArray[steps].make_break_decision == true && alt_path_taken == false)
  319.         {
  320.             alt_path_taken = true;
  321.             if(last_encounter_tile <= 50)  //This is the break decision
  322.             {
  323.                 int alt_steps = 0;
  324.                 for(alt_steps = 0; alt_steps < NumAltSteps; alt_steps++)//Alt path steps forwards.
  325.                 {  
  326.                     ec -= TileArrayAlt[alt_steps].ec_decrement;  //Take a step
  327.                     time += STEP_TIME;
  328.                     if(ec <= 0)   //See if we got into an encounter.
  329.                     {
  330.                         time += TileArrayAlt[alt_steps].encounter_time_loss;  //FIXME For now...
  331.                         ec = GetRandomEC();
  332.                         num_encounters++;
  333.                         //FIXME Don't mark last encounter here yet.
  334.                         break;  //Start walking back.
  335.                     }
  336.                     if(printenable)
  337.                     {
  338.                         printf("ec %d time %d alt_steps %d\n", ec, time, alt_steps+1);
  339.                     }
  340.                 }
  341.                 if(alt_steps == NumAltSteps)  //If we made it to the reset.
  342.                 {
  343.                     time += AltPathTime;
  344.                     ec = GetRandomEC();
  345.                 }
  346.                 for(; alt_steps >= 0; alt_steps--)//Alt path steps backwards.   //FIXME There's an issue with an extra step here.
  347.                 {  
  348.                     ec -= TileArrayAlt[alt_steps].ec_decrement;  //Take a step
  349.                     time += STEP_TIME;
  350.                     if(ec <= 0)   //See if we got into an encounter.
  351.                     {
  352.                         time += TileArrayAlt[alt_steps].encounter_time_loss;
  353.                         ec = GetRandomEC();
  354.                         num_encounters++;
  355.                         //FIXME Don't mark last encounter here yet.
  356.                     }
  357.                     if(printenable)
  358.                     {
  359.                         printf("ec %d time %d alt_steps %d\n", ec, time, alt_steps+1);
  360.                     }
  361.                 }
  362.                 steps -= 1;
  363.             }
  364.            
  365.         }
  366. #endif
  367.     }
  368.  
  369.  
  370.  
  371.  
  372.     if(printenable)
  373.     {
  374.         printf("Final Time %d num_encounters %d\n", time, num_encounters);
  375.     }
  376.     return time;
  377. }
  378.  
  379. int main ()
  380. {  
  381.     int this_time;
  382.     long long total_time = 0;
  383.     double average_time = 0;
  384.     int iterations = 1;
  385.     int count = 0;
  386.  
  387.     //int mintime = 60 * 100;
  388.     srand(time(NULL));
  389.     InitArrays();
  390.     SetupAllTiles();
  391.  
  392.     for(int i=0; i<iterations; i++)
  393.     {
  394.         this_time = Simulate();
  395.         if(this_time > 0)
  396.         {
  397.             total_time += this_time;
  398.             count++;
  399.         }
  400.     }
  401.     average_time = double(total_time)/double(count);
  402.     printf("average_num_frames = %.4f or %.4f seconds\n", average_time, average_time/60);
  403.  
  404.    
  405.  
  406.     /*for(int i=0; i<40; i++)
  407.     {
  408.         printf("%d\n",TileArray[i].ec_decrement);
  409.     }*/
  410.  
  411.     cout << "To close type something and hit enter: ";
  412.     cin >> dumpchar;
  413.     return 0;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement