Don't like ads? PRO users don't see any ads ;-)

generate 4

By: Bond697 on Mar 17th, 2012  |  syntax: C  |  size: 7.34 KB  |  hits: 58  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* no idea whose this is.  if whoever made it wants to say something and claim credit, please let me know. */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include pidiv.h
  6. #include gender.h
  7.  
  8. #define DEFAULT_MODE CLI
  9. #define NUM_POKEMON 493
  10. #define IV_MAX 31
  11.  
  12. #define TEST 29
  13.  
  14. #define PRNG(x) (0x41c64E6D*(x)+0x6073)
  15. #define PRNG_REV(x) (0xEEB9EB65*(x)+0xA3561A1)
  16. #define APRNG(x) (0x6C078965*(x)+0x1)
  17. #define APRNG_REV(x) (0x9638806D*(x)+0x69c77F93)
  18.  
  19. // High and low functions for u32
  20. #define HIGH(x) ((x)>>16&0xFFFF)
  21. #define LOW(x) ((x)&0xFFFF)
  22.  
  23. #define RAND8 random(0x100)
  24. #define RAND16 random(0x10000)
  25.  
  26. typedef unsigned char u8;                       // 0 to 255
  27. typedef unsigned short u16;                     // 0 to 65535
  28. typedef unsigned long u32;                      // 0 to 4294967295
  29. typedef unsigned long long u64;         // 0 to 1.8*10^19
  30. typedef signed char s8;                         // -127 to 127 (usually -128)
  31. typedef signed short s16;                       // -32767 to 32767 (usually -32768)
  32. typedef signed long s32;                        // +/- 2147483647
  33. typedef signed long long s64;           // +/- 9.2*10^18
  34.  
  35. int mode;
  36. s32 * pref_stats;
  37. s32 * stats;
  38. u8 * pref_ivs;
  39. u8 * ivs;
  40. u8 * algorithms;
  41. u8 * ic_log;
  42. u8 gender_threshold;
  43. u64 num_results;
  44. u64 found;
  45. u8 log_length;
  46. u8 tail;
  47.  
  48. enum {CLI, GUI, NUM_MODES};
  49. enum {HP, ATK, DEF, SPD, SATK, SDEF, NUM_IVS};
  50. enum {NORMAL, WILD, UNCOMMON, RARE, VERY_RARE, SUPER_RARE, NUM_ALGORITHMS};
  51. enum {NATURE, CLASS, GENDER, SHINY, HP_TYPE, HP_POWER, TID, SID, NUM_STATS};
  52.  
  53. u8 get_gender_threshold(int pokemon)
  54. {
  55.         return (pokemon <= 0 || pokemon > NUM_POKEMON) ? gender[0] : gender[pokemon];
  56. }
  57.  
  58. int cli_input()
  59. {
  60.         // temp code
  61.         int i;
  62.         for(i = 0; i < NUM_IVS; i++) {
  63.                 pref_ivs[i]=TEST;
  64.         }
  65.         for(i = 0; i< NUM_ALGORITHMS; i++) {
  66.                 algorithms[i] = 1;
  67.         }
  68.         pref_stats[NATURE] = -1;
  69.         pref_stats[CLASS] = -1;
  70.         pref_stats[GENDER] = -1;
  71.         pref_stats[SHINY] = 0;
  72.         pref_stats[HP_TYPE] = -1;
  73.         pref_stats[HP_POWER] = 30;
  74.         pref_stats[TID] = -1;
  75.         pref_stats[SID] = -1;
  76.  
  77.         /* TODO: cli_input */
  78.         return 0;
  79. }
  80.  
  81. int gui_input()
  82. {
  83.         /* TODO: gui_input */
  84.         return 0;
  85. }
  86.  
  87. int get_input()
  88. {
  89.         if(mode == CLI) {
  90.                 return cli_input();
  91.         } else if(mode == GUI) {
  92.                 return gui_input();
  93.         }
  94.         return 1;
  95. }
  96.  
  97. void init_all()
  98. {
  99.         int i;
  100.         pref_stats = (s32*) calloc(NUM_STATS, sizeof(s32));
  101.         if(pref_stats==NULL) exit (1);
  102.         stats = (s32*) calloc(NUM_STATS, sizeof(s32));
  103.         if(stats==NULL) exit (1);
  104.         pref_ivs = (u8*) calloc(NUM_IVS, sizeof(u8));
  105.         if(pref_ivs==NULL) exit (1);
  106.         ivs = (u8*) calloc(NUM_IVS, sizeof(u8));
  107.         if(ivs==NULL) exit (1);
  108.         algorithms = (u8*) calloc(NUM_ALGORITHMS, sizeof(u8));
  109.         if(algorithms==NULL) exit (1);
  110.         ic_log = NULL;
  111.         mode = DEFAULT_MODE;
  112.         gender_threshold = 255;
  113.         num_results = 0;
  114.         found = 0;
  115.         log_lenghth = 0;
  116.         tail = -1;
  117.         return;
  118. }
  119.  
  120. void init_ic()
  121. {
  122.         for(i = 0; i < NUM_IVS; i++) {
  123.                 log_length += IV_MAX-pref_ivs[i];
  124.         }
  125.         if(algorithms==NULL) exit (1);
  126.         ic_log = (u8*) calloc(log_length, sizeof(u8));
  127. }
  128.  
  129. void free_all()
  130. {
  131.         free(pref_stats);
  132.         free(stats);
  133.         free(pref_ivs);
  134.         free(ivs);
  135.         free(algorithms);
  136.         if(ic_log) free(ic_log);
  137.         return;
  138. }
  139.  
  140. int cli_print_data(u32 pid, u8 algorithm) {
  141.         // temp code
  142.         printf("%010u %2d %2d %2d %2d %2d %2d %1d %2d %1d %1d %1d %2d %2d %05u %05u\n", pid, ivs[HP], ivs[ATK], ivs[DEF], ivs[SPD], ivs[SATK], ivs[SDEF], algorithm, stats[NATURE], stats[CLASS], stats[GENDER], stats[SHINY], stats[HP_TYPE], stats[HP_POWER], stats[TID], stats[SID]);
  143.        
  144.         /* TODO: real cli_print_data */
  145.         return 0;
  146. }
  147.  
  148. int gui_print_data(u32 pid, u8 algorithm)
  149. {
  150.         /* TODO: gui_print_data */
  151.         return 0;
  152. }
  153.  
  154. int print_data(u32 pid, u8 algorithm)
  155. {
  156.         if(mode == CLI) {
  157.                 return cli_print_data(pid, algorithm);
  158.         } else if(mode == GUI) {
  159.                 return gui_print_data(pid, algorithm);
  160.         }
  161.         return 1;
  162. }
  163.  
  164. int check_pid(u32 pid, u8 algorithm)
  165. {
  166.         int ret;
  167.         stats[NATURE] = (s32) pid%25;
  168.         stats[CLASS] = (s32) pid&0x1;
  169.         stats[HP_TYPE] = (s32) ((ivs[5]&0x1)<<5|(ivs[4]&0x1)<<4|(ivs[3]&0x1)<<3|(ivs[2]&0x1)<<2|(ivs[1]&0x1)<<1|ivs[0]&0x1)*15/63;
  170.         stats[HP_POWER] = (s32) ((ivs[5]&0x2)<<4|(ivs[4]&0x2)<<3|(ivs[3]&0x2)<<2|(ivs[2]&0x2)<<1|(ivs[2]&0x2)|(ivs[2]&0x2)>>1)*40/63+30;
  171.         stats[TID] = (s32) (prefs[TID]>=0 ? prefs[TID] : (prefs[SID]<0 ? RAND16 : (prefs[SHINY] ? HIGH(pid)^LOW(pid)^prefs[SID] : RAND16)));
  172.         stats[SID] = (s32) (prefs[SID]>=0 ? prefs[SID] : (prefs[SHINY] ? HIGH(pid)^LOW(pid)^stats[TID] : RAND16));
  173.         stats[SHINY] = (HIGH(pid)^LOW(pid)^stats[TID]^stats[SID]) < 8 ? 1 : 0;
  174.         stats[GENDER] = gender_threshold==0 ? 0 : (gender_threshold==254 ? 1 : (gender_threshold==255 ? 2 : ((pid&0xFF) > gender_threshold ? 0 : 1)));
  175.         if(!(1<<stats[NATURE]&prefs[NATURE]))
  176.                 return 0;
  177.         if(stats[CLASS] != prefs[CLASS] && prefs[CLASS] >= 0)
  178.                 return 0;
  179.         if(stats[GENDER] != prefs[GENDER] && prefs[GENDER] >= 0)
  180.                 return 0;
  181.         if(!stats[SHINY] && prefs[SHINY])
  182.                 return 0;
  183.         if(!(1<<stats[HP_TYPE]&prefs[HP_TYPE]))
  184.                 return 0;
  185.         if(stats[HP_POWER] < prefs[HP_POWER])
  186.                 return 0;
  187.         ret = print_data(pid, algorithm);
  188.         return ret;
  189. }
  190.  
  191. int ivs_to_pid(u32 root, int iv1)
  192. {
  193.         int ret;
  194.         u32 x[5];
  195.         u8 found[] = {0, 0, 0};
  196.         x[0] = PRNG_REV(root);
  197.         if(algorithms[NORMAL] || algorithms[WILD] || algorithms[SUPER_RARE]) {
  198.                 if ((HIGH(x[0])&0x7FFF) == iv1)
  199.                         found[0] = 1;
  200.         }
  201.         x[1] = PRNG_REV(x[0]);
  202.         if(algorithms[UNCOMMON] || algorithms[RARE]) {
  203.                 if ((HIGH(x[1])&0x7FFF) == iv1)
  204.                         found[1] = 1;
  205.         }
  206.         x[2] = rev(x[1]);
  207.         if(algorithms[VERY_RARE]) {
  208.                 if ((HIGH(x[2])&0x7FFF) == iv1)
  209.                         found[2] = 1;
  210.         }
  211.         if(found[0]) {
  212.                 ret = check_pid(((x[1]&0xFFFF0000)|HIGH(x[2]), NORMAL);
  213.                 if(ret) return ret;
  214.         }
  215.         if(!algorithms[WILD] && !algorithms[UNCOMMON] && !algorithms[RARE] && !algorithms[VERY_RARE] && !algorithms[SUPER_RARE])
  216.                 return 0;
  217.         x[3] = rev(x[2]);
  218.         if(found[0] || found[1]) {
  219.                 ret = check_pid(((x[2]&0xFFFF0000)|HIGH(x[3]), found[0] ? WILD : UNCOMMON);
  220.                 if(ret) return ret;
  221.         }
  222.         if(!algorithms[RARE] && !algorithms[VERY_RARE] && !algorithms[SUPER_RARE])
  223.                 return 0;
  224.         x[4] = rev(x[3]);
  225.         if(found[0] || found[1] || found[2]) {
  226.                 ret = check_pid(((x[3]&0xFFFF0000)|HIGH(x[4])), found[1] ? RARE : (found[2] ? VERY_RARE : SUPER_RARE));
  227.                 if(ret) return ret;
  228.         }
  229.         return 0;
  230. }
  231.  
  232. int root_loop()
  233. {
  234.         int ret;
  235.         u32 i;
  236.         u32 j;
  237.         u32 iv1 = ivs[HP]|ivs[ATK]<<5|ivs[DEF]<<10;
  238.         u32 iv2 = ivs[SPD]|ivs[SATK]<<5|ivs[SDEF]<<10;
  239.         for(i = 0; i < 2; i++) {
  240.                 for(j = 0; j < 0x10000; j++) {
  241.                         ret = ivs_to_pid(i<<31|iv2<<16|j, iv1);
  242.                         if(ret) return ret;
  243.                 }
  244.         }
  245.         return 0;
  246. }
  247.  
  248. int flip(int i)
  249. {
  250.         int ret;
  251.         ivs[ic_log[i]]++;
  252.         ic_log[i]++;
  253.         if((i != 0 && ic_log[i] > ic_log[i-1]) || ic_log[i] >= NUM_IVS) {
  254.                 ic_log[i] = 0;
  255.                 ivs[0]--;
  256.                 if(i>0) {
  257.                         ret = flip(i-1);
  258.                         if(ret) return ret;
  259.                 } else {
  260.                         ret = exp_log();
  261.                         if(ret) return ret;
  262.                 }
  263.         } else {
  264.                 ivs[ic_log[i]]--;
  265.         }
  266.         if(ivs[ic_log[i]] < pref_ivs[ic_log[i]] || ivs[ic_log[i]] > IV_MAX) return flip(i);
  267.         return 0;
  268. }
  269.  
  270. int exp_log()
  271. {
  272.         tail++;
  273.         if(tail > log_length) return 1;
  274.         ic_log[tail]=0;
  275.         ivs[0]--;
  276.         if(ivs[0]<pref_ivs[0]) return flip(tail);
  277.         return 0;
  278. }
  279.  
  280. int iv_loop()
  281. {
  282.         int i;
  283.         int ret;
  284.         int stop = 0;
  285.         for(i = 0; i < NUM_IVS; i++) {
  286.                 ivs[i] = IV_MAX;
  287.         }
  288.         ret = root_loop();
  289.         if(ret) return ret;
  290.         stop = exp_log();
  291.         while(!stop) {
  292.                 ret = root_loop();
  293.                 if(ret) return ret;
  294.                 stop = flip(tail);
  295.         }
  296. }
  297.  
  298. int main()
  299. {
  300.         int ret;
  301.         mode = CLI;
  302.         init_all();
  303.         ret = get_input();
  304.         if(ret) {
  305.                 free_all();
  306.                 return ret;
  307.         }
  308.         init_ic();
  309.         ret = iv_loop();
  310.         free_all();
  311.         return ret;
  312. }