[ create a new paste ] login | about

Link: http://codepad.org/yxsMhYOu    [ raw code | fork ]

D, pasted on Jul 24:
// Compile with:
//   ldmd2 -O -release -inline -noboundscheck bench.d
//   Then if you want strip the binary.

import std.stdio, std.random, std.getopt;

enum uint tileDim = 50,
          minWid = 2,
          maxWid = 8;

struct Tile { uint x, y, t; }
struct Room { uint x, y, w, h, n; }

struct Lev {
    Tile[2_500] ts;
    Room[100] rs;
    uint lenRs;
}

bool checkColl(size_t N)(in uint x, in uint y, in uint w, in uint h,
                         in ref Room[N] rs)
pure nothrow {
    foreach (immutable(Room) /*ref*/ r; rs) {
        bool roomOK = false;
        if ((r.x + r.w + 1) < x || r.x > (x + w + 1))
            roomOK = true;
        else if ((r.y + r.h + 1) < y || r.y > (y + h + 1))
            roomOK = true;
        else
            roomOK = false;
        if (!roomOK)
            return true;
    }

    return false;
}

void makeRoom(size_t N)(ref Room[N] rs, ref uint lenRs, ref Xorshift rng) {
    immutable x = uniform(0, tileDim, rng);
    immutable y = uniform(0, tileDim, rng);
    immutable w = uniform(minWid, maxWid, rng);
    immutable h = uniform(minWid, maxWid, rng);

    if (x + w >= tileDim || y + h >= tileDim || x == 0 || y == 0)
        return;
    if (!checkColl(x, y, w, h, rs)) {
        rs[lenRs] = Room(x, y, w, h, lenRs);
        lenRs++;
    }
}

void room2Tiles(size_t N)(in ref Room r, ref Tile[N] ts) pure nothrow {
    foreach (xi; r.x .. r.x + r.w + 1)
        foreach (yi; r.y .. r.y + r.h + 1)
            ts[yi * tileDim + xi].t = 1;
}

void printLev(in ref Lev l) nothrow {
    foreach (i, ref const(Tile) tsi; l.ts) {
        printf("%d", tsi.t);
        if (i % tileDim == tileDim - 1)
            printf("\n");
    }
}

void main(string[] args) {
    enum size_t nLoops = 50_000;

    if (args.length <= 1)
        return writeln("Integer seed argument required.");
    uint v;
    getopt(args, "v", &v);

    writeln("The random seed is: ", v);
    auto rng = v.Xorshift;
    Lev[Lev.rs.length] ls = void;

    uint lenLs = 0;
    foreach (i; 0 .. Lev.rs.length) {
        Room[Lev.rs.length] rs;
        uint lenRs = 0;
        foreach (_; 0 .. nLoops) {
            makeRoom(rs, lenRs, rng);
            if (lenRs == (Lev.rs.length - 1))
                break;
        }

        Tile[Lev.ts.length] ts = void;
        foreach (uint j, ref tsj; ts)
            tsj = Tile(j % tileDim, j / tileDim, 0);

        foreach (j; 0 .. lenRs)
            room2Tiles(rs[j], ts);

        ls[lenLs] = Lev(ts, rs, lenRs);
        lenLs++;
    }

    Lev templ = void;
    templ.lenRs = 0;
    foreach (ref const(Lev) lsi; ls)
        if (lsi.lenRs > templ.lenRs)
            templ = lsi;

    templ.printLev;
}


Create a new paste based on this one


Comments: