generated from minecrafthome/template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kaktwoos.cl
148 lines (123 loc) · 4.02 KB
/
kaktwoos.cl
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
int nextInt(ulong* seed, short bound);
int next(ulong* seed, short bits);
int nextIntUnknown(ulong* seed, short bound);
unsigned char extract(const unsigned int heightMap[], int id);
void increase(unsigned int heightMap[], int id, int val);
#define WANTED_CACTUS_HEIGHT 22
kernel void crack(global int *data, global ulong* answer)
{
int id = get_global_id(0);
ulong originalSeed = (((ulong)data[0] * (ulong)data[1] + (ulong)id) << 4) | data[8];
ulong seed = originalSeed;
short position = -1;
short posMap;
short posX, posY, posZ;
short initialPosX, initialPosY, initialPosZ, initialPos;
short top = data[7];
uint heightMap[205];
for (short i = 0; i < 205; i++) {
heightMap[i] = 0;
}
for (short i = 0; i < 10; i++) {
if (WANTED_CACTUS_HEIGHT - top > 9 * (10 - i)) {
return;
}
initialPosX = next(&seed, 4) + 8;
initialPosZ = next(&seed, 4) + 8;
initialPos = initialPosX + initialPosZ * 32;
short terrainHeight = (extract(heightMap, initialPos) + FLOOR_LEVEL + 1) * 2;
initialPosY = nextIntUnknown(&seed, terrainHeight);
if (initialPosY + 3 <= FLOOR_LEVEL && initialPosY - 3 >= 0) {
seed = (seed * 256682821044977UL + 233843537749372UL) & ((1UL << 48) - 1);
continue;
}
if (initialPosY - 3 > top + FLOOR_LEVEL + 1) {
for (int j = 0; j < 10; j++) {
seed = (seed * 76790647859193UL + 25707281917278UL) & ((1UL << 48) - 1);
nextIntUnknown(&seed, nextInt(&seed, 3) + 1);
}
continue;
}
for (short a = 0; a < 10; a++) {
posX = initialPosX + next(&seed, 3) - next(&seed, 3);
posY = initialPosY + next(&seed, 2) - next(&seed, 2);
posZ = initialPosZ + next(&seed, 3) - next(&seed, 3);
posMap = posX + posZ * 32;
if (position == -1 && posY > FLOOR_LEVEL && posY <= FLOOR_LEVEL + data[7] + 1) {
if (posMap == data[3]) {
position = 0;
} else if (posMap == data[4]) {
position = 1;
} else if (posMap == data[5]) {
position = 2;
}
if (position != -1) {
int bit = (int)((originalSeed >> 4) & 1);
if (data[6] != position) {
if (bit == data[9]) return;
} else {
if (bit != data[9]) return;
}
increase(heightMap, posMap, data[7]);
top = data[7];
}
}
if (posY <= extract(heightMap, posMap) + FLOOR_LEVEL) continue;
short offset = 1 + nextIntUnknown(&seed, nextInt(&seed, 3) + 1);
for (short j = 0; j < offset; j++) {
if ((posY + j - 1) > extract(heightMap, posX + posZ * 32) + FLOOR_LEVEL || posY < 0) continue;
if ((posY + j) <= extract(heightMap, (posX + 1) + posZ * 32) + FLOOR_LEVEL) continue;
if ((posY + j) <= extract(heightMap, (posX - 1) + posZ * 32) + FLOOR_LEVEL) continue;
if ((posY + j) <= extract(heightMap, posX + (posZ + 1) * 32) + FLOOR_LEVEL) continue;
if ((posY + j) <= extract(heightMap, posX + (posZ - 1) * 32) + FLOOR_LEVEL) continue;
increase(heightMap, posMap, 1);
if (top < extract(heightMap, posMap)) {
top = extract(heightMap, posMap);
}
}
}
}
if (top >= WANTED_CACTUS_HEIGHT) {
answer[atomic_add(&data[2], 1)] =
((ulong)top) << 58UL |
(((ulong)data[position + 3]) << 48UL) |
originalSeed;
}
}
int next(ulong* seed, short bits)
{
*seed = (*seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return *seed >> (48 - bits);
}
int nextInt(ulong* seed, short bound)
{
int bits, value;
do {
*seed = (*seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
bits = *seed >> 17;
value = bits % bound;
} while(bits - value + (bound - 1) < 0);
return value;
}
int nextIntUnknown(ulong* seed, short bound)
{
if((bound & -bound) == bound) {
*seed = (*seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)((bound * (*seed >> 17)) >> 31);
}
int bits, value;
do {
*seed = (*seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
bits = *seed >> 17;
value = bits % bound;
} while(bits - value + (bound - 1) < 0);
return value;
}
unsigned char extract(const unsigned int heightMap[], int id)
{
return (heightMap[id / 5] >> ((id % 5) * 6)) & 0b111111U;
}
void increase(unsigned int heightMap[], int id, int val)
{
heightMap[id / 5] += val << ((id % 5) * 6);
}