#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#define LINUX 1
#define uint8 unsigned char
#include "games.h"

// TO DO: after converting outside border, run repeated passes
// to detect and convert internal blocks.

// smallest block possible:
//
//   ++
//   ++
//

// Not yet decided how to handle single-block-wide walls with branches.
// Leave as blocks?  Draw centerlines?  Outline like the others?
//
//        #
//    #####
//      #
//      ###
//

// need to be careful and do change of direction when hitting + otherwise
// some cases may be ambiguous. Rule needs to understand clockwise traversal.
//
// +--++--+
// |  ++  |
// ++    ++
// ++    ++
// |  ++  |
// +--++--+

// +--+ +--+
// |  +_+  |
// ++     ++
//  |     |
// ++     ++
// |  +-+  |
// +--+ +--+

int map[18][18];

void Convert(int x, int y, int lastx, int lasty) {
  map[x][y] = 'x';
  if (map[x+1][y] == '-') {
    assert(lasty == 0);
    Convert(x+1,y, lastx+1,0);
  } else if (map[x+1][y] == '+') {
    fprintf(stdout, "  -1, 0, W*%d,\n", lastx+1);
    Convert(x+1,y, 0,0);

  } else if (map[x][y+1] == '|') {
    assert(lastx == 0);
    Convert(x,y+1, 0,lasty+1);
  } else if (map[x][y+1] == '+') {
    fprintf(stdout, "  -1, H*%d, 0,\n", lasty+1);
    Convert(x,y+1, 0,0);

  } else if (map[x-1][y] == '-') {
    assert(lasty == 0);
    Convert(x-1,y, lastx-1,0);
  } else if (map[x-1][y] == '+') {
    fprintf(stdout, "  -1, 0, W*%d,\n", lastx-1);
    Convert(x-1,y, 0,0);

  } else if (map[x][y-1] == '|') {
    assert(lastx == 0);
    Convert(x,y-1, 0,lasty-1);
  } else if (map[x][y-1] == '+') {
    fprintf(stdout, "  -1, H*%d, 0,\n", lasty-1);
    Convert(x,y-1, 0,0);
  }
  return; // recursion stopper.
}

void gen_outline(int gameno, char **game, int lines) {
  int i, x, y;
  for (y = 0; y < 18; y++) {
    for (x = 0; x < 18; x++) {
      map[x][y] = ' '; if (y == 0 || y == 17 ||
                           x == 0 || x == 17) map[x][y] = 'x';
    }
  }
  for (i = 0; i < lines; i++) {
    for (x = 0; x < strlen(game[i]); x++) {
      map[(17-strlen(game[0]))/2+x+1][(17-lines)/2+i+1] = game[i][x];
      if (game[i][x] == '@') {
        fprintf(stdout, "\n// Player start pos is y=%d x=%d\n", (17-lines)/2+i+1, (17-strlen(game[0]))/2+x+1);
      }
    }
  }
  fprintf(stdout, "/*\n");
  for (y = 0; y < 18; y++) {
    for (x = 0; x < 18; x++) {
      fputc(map[x][y], stdout);
    }
    fputc('\n', stdout);
  }
  fprintf(stdout, " */\n");
  fputc('\n', stdout);
  for (y = 0; y < 18; y++) {
    for (x = 0; x < 18; x++) {
      if (map[x][y] == '+') {
        fprintf(stdout, "const int wall%0d[] = {\n", gameno);
        Convert(x,y, 0,0);
        fprintf(stdout, "  2,\n");
        fprintf(stdout, "};\n");
        return;
      }
    }
  }
}

int main(int argc, char **argv) {
  int i;
  for (i = 0; i < NUM_GAMES; i++) {
    gen_outline(i, (char **)game[i], game_height[i]);
  }
  fprintf(stdout, "const int const *wall[MAX_GAMES+1] = {\n");
  for (i = 0; i < NUM_GAMES; i++) {
    fprintf(stdout, "  wall%0d,\n", i);
  }
  fprintf(stdout, "  NULL,\n}\n");
}
