forked from logicchains/levgen-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.d
More file actions
151 lines (136 loc) · 3.37 KB
/
D.d
File metadata and controls
151 lines (136 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Compile with:
// ldmd2 -noruntime -release -inline -O -noboundscheck bench2.d
import core.stdc.stdio, core.stdc.stdlib;
enum int TileDim = 50;
enum int Miw = 2;
enum int MaxWid = 8;
struct Tile {
int X = void;
int Y = void;
int T = void;
}
struct Room {
int X = void;
int Y = void;
int W = void;
int H = void;
int N = void;
}
struct Lev {
Tile[2500] ts = void;
Room[100] rs = void;
int lenrs = void;
}
int GenRand(uint* gen) pure nothrow {
*gen += *gen;
*gen ^= 1;
int tgen = *gen;
if (tgen < 0) {
*gen ^= 0x88888eef;
}
int a = *gen;
return a;
}
int CheckColl(in int x, in int y, in int w, in int h,
in ref Room[100] rs, in int lenrs) pure nothrow {
for (int i = 0; i < lenrs; i++) {
int rx = rs[i].X;
int ry = rs[i].Y;
int rw = rs[i].W;
int rh = rs[i].H;
int RoomOkay = void;
if ((rx + rw + 1) < x || rx > (x + w + 1)) {
RoomOkay = 1;
} else if ((ry + rh + 1) < y || ry > (y + h + 1)) {
RoomOkay = 1;
} else {
RoomOkay = 0;
}
if (RoomOkay == 0)
return 1;
}
return 0;
}
void MakeRoom(ref Room rs[100], int* lenrs, uint* gen) pure nothrow {
immutable int r1 = GenRand(gen);
immutable int r2 = GenRand(gen);
immutable int x = r1 % TileDim;
immutable int y = r2 % TileDim;
immutable int w = r1 % MaxWid+Miw;
immutable int h = r2 % MaxWid+Miw;
if (x + w >= TileDim || y + h >= TileDim || x == 0 || y == 0)
return;
immutable int nocrash = CheckColl(x, y, w, h, rs, *lenrs);
if (nocrash == 0) {
Room r = void;
r.X = x;
r.Y = y;
r.W = w;
r.H = h;
r.N = *lenrs;
rs[*lenrs] = r;
*lenrs = *lenrs + 1;
}
}
void Room2Tiles(in Room* r, ref Tile ts[2500]) pure nothrow {
immutable x = r.X;
immutable y = r.Y;
immutable w = r.W;
immutable h = r.H;
for (int xi = x; xi <= x + w; xi++) {
for (int yi = y; yi <= y + h; yi++) {
int num = yi * TileDim + xi;
ts[num].T = 1;
}
}
}
void PrintLev(in Lev* l) nothrow {
for (int i = 0; i < 2500; i++) {
printf("%d", l.ts[i].T);
if (i % (TileDim) == 49 && i != 0)
printf("\n");
}
}
void main(string[] args) nothrow {
int v = atoi(args[1].ptr);
printf("The random seed is: %d \n", v);
srand(v);
uint gen = v;
uint *Pgen = &gen;
Lev[100] ls = void;
int lenLS = 0;
for (int i = 0; i < 100; i++) {
Room rs[100] = void;
int lenrs = 0;
int *Plenrs = &lenrs;
int ii = void;
for (ii = 0; ii < 50000; ii++) {
MakeRoom(rs, Plenrs, Pgen);
if (lenrs == 99) {
break;
}
}
Tile[2500] ts = void;
for (ii = 0; ii < 2500; ii++) {
ts[ii].X = ii % TileDim;
ts[ii].Y = ii / TileDim;
ts[ii].T = 0;
}
for (ii = 0; ii < lenrs; ii++) {
Room2Tiles(&(rs[ii]), ts);
}
Lev l = void;
l.rs = rs;
l.ts = ts;
l.lenrs = lenrs;
ls[lenLS] = l;
lenLS++;
}
Lev templ = void;
templ.lenrs = 0;
for (int i = 0; i < 100; i++) {
if (ls[i].lenrs > templ.lenrs)
templ = ls[i];
}
PrintLev(&templ);
}