Skip to content

Commit 7aff9e0

Browse files
committed
Reduced sloc
1 parent 4730896 commit 7aff9e0

File tree

1 file changed

+102
-117
lines changed

1 file changed

+102
-117
lines changed

PD.d

Lines changed: 102 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,111 @@
11
// Compile with:
2-
// ldc2 -O5 -check-printf-calls -fdata-sections -ffunction-sections -release -singleobj -strip-debug -wi -disable-boundscheck -L=--gc-sections -L=-s D.d
3-
4-
@safe:
2+
// ldc2 -O5 -check-printf-calls -fdata-sections -ffunction-sections -release -singleobj -strip-debug -wi -disable-boundscheck -L=--gc-sections -L=-s PD.d
3+
54
import std.conv, std.stdio, std.parallelism;
6-
7-
enum LEVEL_SIZE = 50; /// Width and height of a level
8-
enum ROOMS = 100; /// Maximum number of rooms in a level
9-
enum ROOM_SIZE_BASE = 2; /// Rooms will be at least this value plus one in size.
10-
enum ROOM_SIZE_MOD = 8; /// Random additional room size: [0 .. ROOM_SIZE_MOD)
11-
12-
enum NUM_LEVS = 800;
13-
enum NUM_TRIES = 50000;
14-
15-
struct Tile {
16-
uint x;
17-
uint y;
18-
uint t;
19-
}
20-
5+
6+
@safe:
7+
8+
enum levelSize = 50; /// Width and height of a level.
9+
enum nRooms = 100; /// Maximum number of rooms in a level.
10+
enum roomSizeBase = 2; /// Rooms will be at least this value plus one in size.
11+
enum roomSizeMod = 8; /// Random additional room size: [0 .. roomSizeMod).
12+
13+
enum nLevels = 800;
14+
enum nTries = 50_000;
15+
16+
struct Tile { uint x, y, t; }
17+
2118
struct Room {
22-
uint x;
23-
uint y;
24-
uint w;
25-
uint h;
26-
size_t number;
19+
uint x, y, w, h;
20+
size_t number;
2721
}
28-
22+
2923
struct Level {
30-
Tile[LEVEL_SIZE ^^ 2] tiles = void;
31-
Room[ROOMS] rooms = void;
32-
size_t roomCnt = 0;
33-
34-
void makeRoom(ref Random rnd) nothrow {
35-
immutable x = rnd.next() % LEVEL_SIZE;
36-
immutable y = rnd.next() % LEVEL_SIZE;
37-
if (x == 0 || y == 0) return;
38-
39-
immutable w = ROOM_SIZE_BASE + rnd.next() % ROOM_SIZE_MOD;
40-
immutable h = ROOM_SIZE_BASE + rnd.next() % ROOM_SIZE_MOD;
41-
if (x + w >= LEVEL_SIZE || y + h >= LEVEL_SIZE) return;
42-
if (checkColl( x, y, w, h )) return;
43-
44-
Room* r = &this.rooms[this.roomCnt];
45-
r.number = this.roomCnt++;
46-
r.x = x;
47-
r.y = y;
48-
r.w = w;
49-
r.h = h;
50-
}
51-
52-
/// Returns true, when the given area collides with existing rooms.
53-
bool checkColl(in uint x, in uint y, in uint w, in uint h) const pure nothrow {
54-
foreach (ref r; this.rooms[0 .. this.roomCnt]) {
55-
if (r.x + r.w + 1 >= x && r.x <= x + w + 1 &&
56-
r.y + r.h + 1 >= y && r.y <= y + h + 1) {
57-
return true;
58-
}
59-
}
60-
return false;
61-
}
62-
63-
/// Initializes and then builds the tiles from the room definitions.
64-
void buildTiles() pure nothrow {
65-
foreach (uint i; 0 .. this.tiles.length) {
66-
this.tiles[i].x = i % LEVEL_SIZE;
67-
this.tiles[i].y = i / LEVEL_SIZE;
68-
this.tiles[i].t = 0;
69-
}
70-
foreach (ref r; this.rooms[0 .. this.roomCnt]) {
71-
foreach (xi; r.x .. r.x + r.w + 1)
72-
foreach (yi; r.y .. r.y + r.h + 1) {
73-
this.tiles[yi * LEVEL_SIZE + xi].t = 1;
74-
}
75-
}
76-
}
77-
78-
void dump() const @trusted {
79-
foreach (row; 0 .. LEVEL_SIZE) {
80-
immutable offset = LEVEL_SIZE * row;
81-
foreach (col; 0 .. LEVEL_SIZE) {
82-
write( this.tiles[offset + col].t );
83-
}
84-
writeln();
85-
}
86-
}
24+
Tile[levelSize ^^ 2] tiles = void;
25+
Room[nRooms] rooms = void;
26+
size_t roomCnt = 0;
27+
28+
void makeRoom(ref Random rnd) nothrow {
29+
immutable x = rnd.next % levelSize;
30+
if (x == 0)
31+
return;
32+
immutable y = rnd.next % levelSize;
33+
if (y == 0)
34+
return;
35+
36+
immutable w = roomSizeBase + rnd.next % roomSizeMod;
37+
immutable h = roomSizeBase + rnd.next % roomSizeMod;
38+
if (x + w >= levelSize || y + h >= levelSize)
39+
return;
40+
if (checkColl(x, y, w, h))
41+
return;
42+
43+
this.rooms[this.roomCnt] = Room(x, y, w, h, this.roomCnt++);
44+
}
45+
46+
/// Returns true, when the given area collides with existing rooms.
47+
bool checkColl(in uint x, in uint y, in uint w, in uint h) const pure nothrow {
48+
foreach (const ref r; this.rooms[0 .. this.roomCnt])
49+
if (r.x + r.w + 1 >= x && r.x <= x + w + 1 &&
50+
r.y + r.h + 1 >= y && r.y <= y + h + 1)
51+
return true;
52+
return false;
53+
}
54+
55+
/// Initializes and then builds the tiles from the room definitions.
56+
void buildTiles() pure nothrow {
57+
foreach (immutable uint i, ref t; this.tiles)
58+
t = Tile(i % levelSize, i / levelSize, 0);
59+
60+
foreach (const ref r; this.rooms[0 .. this.roomCnt])
61+
foreach (immutable xi; r.x .. r.x + r.w + 1)
62+
foreach (immutable yi; r.y .. r.y + r.h + 1)
63+
this.tiles[yi * levelSize + xi].t = 1;
64+
}
65+
66+
void dump() @system const {
67+
foreach (immutable row; 0 .. levelSize) {
68+
immutable offset = levelSize * row;
69+
foreach (immutable col; 0 .. levelSize)
70+
this.tiles[offset + col].t.write;
71+
writeln;
72+
}
73+
}
8774
}
88-
75+
8976
struct Random {
90-
uint current;
91-
92-
uint next() nothrow {
93-
current += current;
94-
current ^= (current > int.max) ? 0x88888eee : 1;
95-
return current;
96-
}
77+
uint current;
78+
79+
uint next() pure nothrow {
80+
current += current;
81+
current ^= (current > int.max) ? 0x88888eee : 1;
82+
return current;
83+
}
9784
}
98-
99-
__gshared Level[NUM_LEVS] levels;
100-
101-
void main(string[] args) @system {
102-
// Create a local random number generator
103-
immutable seed = (args.length > 1) ? args[1].to!uint() : 123;
104-
writefln( "The random seed is: %s", seed );
105-
106-
// Create several levels for benchmarking purposes
107-
foreach (levelIdx, ref level; parallel( levels[] )) {
108-
auto rnd = Random( cast(uint) (seed * (levelIdx+1) * (levelIdx+1)) );
109-
foreach (i; 0 .. NUM_TRIES) {
110-
level.makeRoom(rnd);
111-
if (level.roomCnt == ROOMS) {
112-
break;
113-
}
114-
}
115-
level.buildTiles();
116-
}
117-
118-
// Select the level with the most rooms for printing
119-
Level* levelToPrint = &levels[0];
120-
foreach (ref level; levels[1 .. $]) {
121-
if (level.roomCnt > levelToPrint.roomCnt) {
122-
levelToPrint = &level;
123-
}
124-
}
125-
levelToPrint.dump();
85+
86+
87+
void main(in string[] args) @system {
88+
static __gshared Level[nLevels] levels;
89+
90+
// Create a local random number generator.
91+
immutable seed = (args.length > 1) ? args[1].to!uint : 123;
92+
writefln("The random seed is: %s", seed);
93+
94+
// Create several levels for benchmarking purposes.
95+
foreach (levelIdx, ref level; levels[].parallel) {
96+
auto rnd = Random(cast(uint)(seed * (levelIdx + 1) * (levelIdx + 1)));
97+
foreach (immutable i; 0 .. nTries) {
98+
level.makeRoom(rnd);
99+
if (level.roomCnt == nRooms)
100+
break;
101+
}
102+
level.buildTiles;
103+
}
104+
105+
// Select the level with the most rooms for printing.
106+
auto levelToPrint = &levels[0];
107+
foreach (ref level; levels[1 .. $])
108+
if (level.roomCnt > levelToPrint.roomCnt)
109+
levelToPrint = &level;
110+
levelToPrint.dump;
126111
}

0 commit comments

Comments
 (0)