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   10.0
#define WALLJITTER  1.0
#define WALLBORDER  2.0

#define SRCRES    10.0

#define SCALE      1.0


double itemScale;
double cellScale;


Animation itemTiles[1024];
Animation cellTiles[1024];

int itemTilesCount = 0;
int cellTilesCount = 0;

int rows;
int cols;

Framebuffer framebuffer;
Animation fbAnim;


void shuffle(Animation *anims, int count) {
  for(int i = 0; i < 2*count; ++i) {
    int a = randomNumber(0, count-1);
    int b = randomNumber(0, count-1);
    if (a != b) {
      Animation anim = anims[a];
      anims[a] = anims[b];
      anims[b] = anim;
    }
  }
}


void groundLine(double len, double k) {
  double x = 0;
  double y = 0;
  moveTo(x, y);
  while(x < len) {
    double ny;
    do { ny = (randomFloat()*2 - 1)*k; } while(fabs(y + ny) > k);
    y += ny;
    x += (1 + (randomFloat()*2 - 1)*0.5)*2*k;
    if (x >= len) { x = len; y = 0; }
    lineTo(x, y);
  }
  strokePath();
}


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

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

  translate(fw/2, fh/2);
  zoom(RES);
  fill(COLOR_WHITE);
  noStroke();

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

  double itemStep = (CELLSIZE - ITEMSIZE)/2;
  double cellSize = CELLSIZE*cellScale;
  double itemSize = ITEMSIZE*itemScale;

  int cellId = cellTilesCount;
  int itemId = itemTilesCount;
  for(int y = 0; y < rows; ++y) {
    for(int x = 0; x < cols; ++x) {
      saveState();
      translate(x*step, y*step);
      if (cellTilesCount > 0 && randomFloat()*100 < 10) {
        if (cellId >= cellTilesCount) {
          shuffle(cellTiles, cellTilesCount);
          cellId = 0;
        }
        rectTextured(cellTiles[cellId++], -cellSize/2, -cellSize/2, cellSize, cellSize);
      } else
      if (itemTilesCount > 0) {
        int cnt = 4;
        if (cnt < itemTilesCount) cnt = rand()%itemTilesCount;
        for(int i = 0; i < 4; ++i) {
          saveState();
          translate(i%2 ? itemStep : -itemStep, i/2 ? itemStep : -itemStep);
          if (itemId >= itemTilesCount) {
            shuffle(itemTiles, itemTilesCount);
            itemId = 0;
          }
          rectTextured(itemTiles[itemId++], -itemSize/2, -itemSize/2, itemSize, itemSize);
          restoreState();
        }
      }
      restoreState();
    }
  }

  noFill();
  stroke(COLOR_BLACK);
  strokeWidth(THIKNESS);
  double k = WALLJITTER;
  double wallStep = CELLSIZE/2 + k + WALLBORDER;
  saveState();
    translate(-wallStep, 0);
    double len = (cols-1)*step + 2*wallStep;
    for(int i = 0; i < rows; ++i) {
      saveState();
      translate(0, i*step - wallStep);
      groundLine(len, k);
      translate(0, 2*wallStep);
      groundLine(len, k);
      restoreState();
    }
  restoreState();
  saveState();
    rotate(90);
    scale(1, -1);
    translate(-wallStep, 0);
    len = (rows-1)*step + 2*wallStep;
    for(int i = 0; i < cols; ++i) {
      saveState();
      translate(0, i*step - wallStep);
      groundLine(len, k);
      translate(0, 2*wallStep);
      groundLine(len, k);
      restoreState();
    }
  restoreState();

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


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/dungeon2.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 < 5; ++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);

  rows = HEIGHT/(CELLSIZE + WALLSIZE);
  cols = WIDTH/(CELLSIZE + WALLSIZE);

  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();
}