#include "types.h"
#include "user.h"

// User app for generating a simple maze similar to the c64 version using the border ascii characters
// This doesnt seem to work on the azure server but left in since i did the work on it...
uint rngnumber(void)
{
  // Taken from http://stackoverflow.com/questions/1167253/implementation-of-rand
  static unsigned int z1 = 12345, z2 = 12345, z3 = 12345, z4 = 12345;
  unsigned int b;
  b  = ((z1 << 6) ^ z1) >> 13;
  z1 = ((z1 & 4294967294U) << 18) ^ b;
  b  = ((z2 << 2) ^ z2) >> 27; 
  z2 = ((z2 & 4294967288U) << 2) ^ b;
  b  = ((z3 << 13) ^ z3) >> 21;
  z3 = ((z3 & 4294967280U) << 7) ^ b;
  b  = ((z4 << 3) ^ z4) >> 12;
  z4 = ((z4 & 4294967168U) << 13) ^ b;

  return (z1 ^ z2 ^ z3 ^ z4) / 2;
}

int rngrange(int start, int end, int seed)
{
    if (end < start)
    {
        int swtmp = start;
        start = end;
        end = swtmp;
    }

    int range = end - start + 1;
    return (rngnumber() + seed) % (range) + start;
}

int main(int argc, char *argv[]) {
    int lines = 1000;    
    int seed = getpid();
        
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i],  "-l") == 0) 
        {
            lines = atoi(argv[i + 1]);            
        }
        else if (strcmp(argv[i], "-s") == 0) 
        {
            seed = atoi(argv[i + 1]);
        }
        else if (strcmp(argv[i],  "-help") == 0)
        {
            printf(1, "Generates X lines of random maze.\n");
            printf(1, "Options:\n");
            printf(1, "-l [Number] : Sets number of lines to generate for the maze.\n");
            printf(1, "-s [Number] : Sets the current seed for the random function.\n");
            exit();
            return 0;
        }
    }
    
    cls();
    
    printf(1, "Start Maze (DISABLED)\n");
    printf(1, "This does not work on the azure servers, but it would generate a random x lines of characters to look like a maze, similar to the old c64 basic program.\n");
    printf(1, "I could have used the slashes but they look aweful in this font so use the count app instead.\n\n");
    exit();
    
    char mazeline[80];
    for (int y = 0; y < lines; y++)
    {        
        for (int x = 0; x < 79; x++)
        {
            if (x == 0)
            {
                mazeline[x] = ' ';
                continue;
            }

            int rndnum = rngrange(188, 197, seed);
            switch (rndnum)
            {
            case 188:
                rndnum = 32;
                break;
            case 189:
                rndnum = 179;
                break;
            case 190:
                rndnum = 180;
                break;
            }
            // This is causing huge lock ups due to having to lock the console for each character
            // is what im guessing so instead write it to a per line buffer then print
            //printf(1, "%c", rndnum);
            mazeline[x] = rndnum;
        }        
        printf(1, "%s\n", mazeline);
        sleep(10);
    }
    printf(1, "Maze Ended\n");
    
    exit();
} 