Blob Blame Raw

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <helianthus.h>


#define WIDTH     285.0
#define HEIGHT    200.0
#define RES        10.0
#define THIKNESS    0.5
#define ALPHA       0.1

#define CELLSIZE   22.5
#define ITEMSIZE   10.0
#define WALLSIZE   15.0
#define WALLJITTER  0.5
#define WALLWIDTH   1.0

#define SRCRES    10.0

#define SCALE      1.0


double itemScale;
double cellScale;


Animation itemTiles[1024];
Animation cellTiles[1024];
int map[256][256];

int itemTilesCount = 0;
int cellTilesCount = 0;

int rows;
int cols;
unsigned int jitterSeed;


Framebuffer framebuffer;
Animation fbAnim;


void shuffle(int *numbers, int count) {
  for(int i = 0; i < count; ++i)
    numbers[i] = i;
  for(int i = 0; i < 2*count; ++i) {
    int a = randomNumber(0, count-1);
    int b = randomNumber(0, count-1);
    int n = numbers[a];
    numbers[a] = numbers[b];
    numbers[b] = n;
  }
}


int lastItem = 1000000;
int itemNumbers[1024];
int genItem() {
  if (lastItem >= itemTilesCount)
    { shuffle(itemNumbers, itemTilesCount); lastItem = 0; }
  return itemNumbers[lastItem++];
}


int lastCell = 1000000;
int cellNumbers[1024];
int genCell() {
  if (lastCell >= cellTilesCount)
    { shuffle(cellNumbers, cellTilesCount); lastCell = 0; }
  return cellNumbers[lastCell++];
}


void generateMap() {
  for(int y = 0; y < rows; ++y)
    for(int x = 0; x < cols; ++x)
      map[y][x] = 1000 + genItem();

  if (rows >= 2 && cols >= 2 && cellTilesCount > 0) {
    int cnt = (rows-1)*(cols-1)/4/10;
    cnt += randomNumber(0, cnt/2) - cnt/4;
    for(int i = 0; i < cnt; ++i) {
      int x = randomNumber(0, cols-2);
      int y = randomNumber(0, rows-2);
      if ( map[y][x] < 2000
        && map[y][x+1] < 2000
        && map[y+1][x] < 2000
        && map[y+1][x+1] < 2000 )
      {
        map[y][x] = 2000 + genCell();
        map[y][x+1] = map[y+1][x] = map[y+1][x+1] = 3000;
      }
    }
  }
}


void jitter(double x, double y, double k, double *outX, double *outY) {
  unsigned int ix = (unsigned int)(int)round(x*2*RES);
  unsigned int iy = (unsigned int)(int)round(y*2*RES);
  unsigned int random = ix ^ ((iy << 16) | (iy >> 16));
  random ^= jitterSeed;

	random = (unsigned int)((unsigned long long)random * 48271 % 0x7fffffff);
  double randomX = random/(double)0x7fffffff;
	random = (unsigned int)((unsigned long long)random * 48271 % 0x7fffffff);
  double randomY = random/(double)0x7fffffff;

	*outX = x + (randomX*2 - 1)*k;
	*outY = y + (randomY*2 - 1)*k;
}


void jitterLine(double x0, double y0, double x1, double y1, double kl, double k) {
  double dx = x1 - x0;
  double dy = y1 - y0;
  double len = sqrt(dx*dx + dy*dy);
  int count = round(len/kl) + 1;
  if (count < 2) count = 2;
  double kx = dx/(count-1);
  double ky = dy/(count-1);
  for(int i = 0; i < count; ++i) {
    double x = x0 + i*kx;
    double y = y0 + i*ky;
    jitter(x, y, k, &x, &y);
    if (i) lineTo(x, y); else moveTo(x, y);
  }
  strokePath();
}


void drawMap() {
  int fw = framebufferGetWidth(framebuffer);
  int fh = framebufferGetHeight(framebuffer);

  saveState();
  target(framebuffer);
  background(COLOR_TRANSPARENT);
  clear();

  translate(fw/2, fh/2);
  zoom(RES);

  double step = WALLSIZE;
  translate(-(cols-1)*step/2, -(rows-1)*step/2);

  double cellSize = CELLSIZE*cellScale;
  double itemSize = ITEMSIZE*itemScale;
  fill(COLOR_WHITE);
  noStroke();
  for(int y = 0; y < rows; ++y) {
    for(int x = 0; x < cols; ++x) {
      int m = map[y][x];
      if (m < 1000) {
      } else
      if (m < 2000) {
        double xx = x*step - itemSize/2;
        double yy = y*step - itemSize/2;
        rectTextured(itemTiles[m - 1000], xx, yy, itemSize, itemSize);
      } else
      if (m < 3000) {
        double xx = x*step + step/2 - cellSize/2;
        double yy = y*step + step/2 - cellSize/2;
        rectTextured(cellTiles[m - 2000], xx, yy, cellSize, cellSize);
      }
    }
  }

  noFill();
  stroke(COLOR_BLACK);
  strokeWidth(THIKNESS);
  jitterSeed = randomNumber(0, 0x7fffffff);
  double t = WALLWIDTH/2.0;
  double k = WALLJITTER;
  double kl = 2*k;
  for(int y = 0; y < rows; ++y) {
    for(int x = 0; x <= cols; ++x) {
      if (x > 0 && x < cols) {
        if (map[y][x-1] >= 2000 && map[y][x-1] < 3000) continue;
        if (y > 0 && map[y][x-1] >= 3000 && map[y-1][x-1] >= 2000 && map[y-1][x-1] < 3000) continue;
      }
      double xx = (x - 0.5)*step;
      double yy = (y - 0.5)*step;
      jitterLine(xx-t, yy, xx-t, yy+step, kl, k);
      jitterLine(xx+t, yy, xx+t, yy+step, kl, k);
    }
  }
  for(int x = 0; x < cols; ++x) {
    for(int y = 0; y <= rows; ++y) {
      if (y > 0 && y < rows) {
        if (map[y-1][x] >= 2000 && map[y-1][x] < 3000) continue;
        if (x > 0 && map[y-1][x] >= 3000 && map[y-1][x-1] >= 2000 && map[y-1][x-1] < 3000) continue;
      }
      double xx = (x - 0.5)*step;
      double yy = (y - 0.5)*step;
      jitterLine(xx, yy-t, xx+step, yy-t, kl, k);
      jitterLine(xx, yy+t, xx+step, yy+t, kl, k);
    }
  }

  viewportSave("data/output/generated-dungeon3.png");
  restoreState();
}


void generate() {
  generateMap();
  drawMap();
}


Animation subImage(int width, int height, unsigned char *pixels, int x, int y, int w, int h) {
  if (x < 0 || y < 0 || x + w > width || y + h > height || !pixels)
    return NULL;
  unsigned char *px = calloc(4, w*h);
  for(int i = 0; i < h; ++i)
    memcpy(px + i*w*4, pixels + ((y+i)*width + x)*4, w*4);
  Animation anim = createAnimationFromImage(w, h, px, FALSE);
  free(px);
  return anim;
}


void init() {
  int w = 0;
  int h = 0;
  unsigned char *px = NULL;
  imageLoad("data/dungeon3.png", &w, &h, &px);

  int is = 128;
  int cs = 256;
  itemScale = is/(ITEMSIZE*SRCRES);
  cellScale = cs/(CELLSIZE*SRCRES);

  int ix0 = (int)round((1*ITEMSIZE + (1 - itemScale)*ITEMSIZE/2)*SRCRES);
  int iy0 = ix0;
  int idx = (int)round(2*ITEMSIZE*SRCRES);
  int idy = idx;

  int cx0 = (int)round((1*ITEMSIZE + (1 - cellScale)*CELLSIZE/2)*SRCRES);
  int cy0 = (int)round((5*ITEMSIZE + (1 - cellScale)*CELLSIZE/2)*SRCRES);
  int cdx = (int)round(3*ITEMSIZE*SRCRES);

  int y = iy0;
  for(int i = 0; i < 6; ++i)
      if ((itemTiles[itemTilesCount] = subImage(w, h, px, ix0 + i*idx, y, is, is)))
        ++itemTilesCount;
  y += idy;
  for(int i = 0; i < 6; ++i)
      if ((itemTiles[itemTilesCount] = subImage(w, h, px, ix0 + i*idx, y, is, is)))
        ++itemTilesCount;
  y = cy0;
  for(int i = 0; i < 3; ++i)
      if ((cellTiles[cellTilesCount] = subImage(w, h, px, cx0 + i*cdx, y, cs, cs)))
        ++cellTilesCount;
  free(px);

  cols = (int)floor(WIDTH/WALLSIZE + 0.001) - 1;
  rows = (int)floor(HEIGHT/WALLSIZE + 0.001) - 1;

  int fw = ceil(WIDTH*RES - 0.001);
  int fh = ceil(HEIGHT*RES - 0.001);
  framebuffer = createFramebuffer(fw, fh);
  fbAnim = createAnimationFromFramebuffer(framebuffer);

  generate();
}


void draw() {
  background(COLOR_WHITE);

  double w = windowGetWidth();
  double h = windowGetHeight();

  if (keyWentDown("space")) generate();

  saveState();
  translate(w/2, h/2);
  zoom(w/WIDTH);
  fill(COLOR_WHITE);
  noStroke();
  rectTextured(fbAnim, -WIDTH/2.0, -HEIGHT/2.0, WIDTH, HEIGHT);
  restoreState();
}


int main() {
  windowSetVariableFrameRate();
  windowSetResizable(TRUE);
  windowSetInit(&init);
  windowSetDraw(&draw);
  windowRun();
}