Advertisement
Guest User

Blue Sphere "Code to Level" Algorithm (codtolev)

a guest
Mar 4th, 2015
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function div(a, b) {
  2.     return Math.floor(a / b);
  3. }
  4.  
  5. // this function returns the level number (1 - 134217728) for a given level password, or -1 if the password is invalid
  6. function codtolev(cod) {
  7.     // cod is a password string in the format "1234 1234 1234"
  8.     var split = parseInt(cod.substring(0, 4) + cod.substring(5, 9) + cod.substring(10));
  9.     var cx = new Array(39);
  10.     var cz = new Array(28);
  11.  
  12.     // create array of cod's binary digits
  13.     for (i = 0; i < 39; i++) {
  14.         cx[i] = split % 2;
  15.         split = div(split, 2);
  16.     }
  17.  
  18.     // invert the bits at odd indices
  19.     for (i = 1; i < 39; i += 2) {
  20.         cx[i] = 1 - cx[i];
  21.     }
  22.    
  23.     // create array from the bits of cx in a certain order
  24.     cz[27] = 0;
  25.     for (i = 0; i < 6; i++) {
  26.         cz[i] = cx[i + 26];
  27.     }
  28.     for (i = 6; i < 27; i++) {
  29.         cz[i] = cx[i - 6];
  30.     }
  31.    
  32.     // convert the binary number represented by cz to decimal
  33.     var sum = 0;
  34.     for (i = 26; i > -1; i--) {
  35.         sum += (cz[i] * Math.pow(2, i));
  36.     }
  37.    
  38.     // subtract a constant, then fiddle with the number a bit
  39.     sum -= 19088742;
  40.     if (sum < 1 && sum > -19088743) {
  41.         sum = 134217728 + sum;
  42.     }
  43.  
  44.     // sum should now be the level number; now we need to make sure the rest of the bit pattern is valid
  45.  
  46.     var tmp = sum - 1;    
  47.  
  48.     // is this bit is 0, the code was generated with the "not sonic 1 cart" flag, so to validate we carry out the extra operation that entails
  49.     if (cx[38] == 0) {
  50.         tmp += 0x07654321;
  51.         tmp &= 0x07FFFFFF;
  52.     }
  53.    
  54.     // create array of tmp's binary digits
  55.     for (i = 0; i <= 27; i++) {
  56.         cz[i] = tmp % 2;
  57.         tmp = div(tmp, 2);
  58.     }
  59.    
  60.     // validate the bit pattern
  61.     if (cx[37] == (1 + cz[6] + cz[23]) % 2 && cx[36] == (1 + cz[5] + cz[22] + cz[17] + cz[0]) % 2 && cx[35] == (1 + cz[4] + cz[21] + cz[16]) % 2 && cx[34] == (1 + cz[3] + cz[20]) % 2 && cx[33] == (1 + cz[2] + cz[19]) % 2 && cx[32] == (1 + cz[1] + cz[18]) % 2 && cx[25] == (0 + cz[11] + cz[16]) % 2 && cx[24] == (0 + cz[10] ) % 2 && cx[23] == (0 + cz[9] + cz[26]) % 2 && cx[22] == (0 + cz[8] + cz[25]) % 2 && cx[21] == (0 + cz[7] + cz[24]) % 2) {
  62.         // in game, the "sonic 1 cart" flag should be set here according to the value of cx[38]
  63.         return sum;
  64.     }
  65.     else {
  66.         return -1;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement