forked from logicchains/Levgen-Parallel-Benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCPP.cpp
160 lines (142 loc) · 4.95 KB
/
PCPP.cpp
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
152
153
154
155
156
157
158
159
160
#include <inttypes.h>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <thread>
const uint32_t TILE_DIM = 50;
const uint32_t WIDMIN = 2;
const uint32_t RESTWIDMAX = 8;
const uint32_t NUM_LEVS = 800;
const uint32_t NUM_THREADS = 4;
const uint32_t NUM_LEVS_PER_THREAD = NUM_LEVS / NUM_THREADS;
struct GenRandGenerator {
uint32_t operator()( uint32_t & gen ) {
gen += gen;
gen ^= 1;
int32_t tgen=gen;
if ( tgen < 0) {
gen ^= 0x88888eef;
}
return gen;
}
};
struct Tile {
uint32_t X, Y;
bool T;
};
struct Room {
uint32_t x, y, w, h, roomNum;
};
struct Level {
Level() : tiles( 2500 ) {
for( uint32_t t = 0 ; t < 2500 ; t++ ) {
tiles[t].X = t % TILE_DIM;
tiles[t].Y = t / TILE_DIM;
tiles[t].T = false;
}
rooms.reserve( 100 );
}
void fillTiles() {
for( Room & r : rooms ) {
for( uint32_t yi = r.y ; yi <= ( r.y + r.h ) ; ++yi ) {
for( uint32_t xi = r.x ; xi <= ( r.x + r.w ) ; ++xi ) {
tiles[ yi * TILE_DIM + xi ].T = true;
}
}
}
}
std::vector<Tile> tiles;
std::vector<Room> rooms;
};
template <typename RandomGenerator>
class LevelGenerator {
void makeRoomSilentlyFail( Level & level, uint32_t & gen ) {
uint32_t x( nextRandomGenerator_( gen ) % TILE_DIM );
uint32_t y( nextRandomGenerator_( gen ) % TILE_DIM );
uint32_t w( (nextRandomGenerator_( gen ) % RESTWIDMAX) + WIDMIN );
uint32_t h( (nextRandomGenerator_( gen ) % RESTWIDMAX) + WIDMIN );
if( (x+w) < TILE_DIM && (y+h) < TILE_DIM && x != 0 && y != 0 &&
!isCollision( level.rooms, x, y, w, h ) ) {
level.rooms.push_back( Room { x, y, w, h, (uint32_t)(level.rooms.size() + 1) } );
}
}
bool isCollision( std::vector<Room> & rooms, const uint32_t x,
const uint32_t y, const uint32_t w, const uint32_t h ) {
for( Room & r : rooms ) {
if( !( ((r.x + r.w + 1 ) < x || r.x > (x + w + 1 )) ||
((r.y + r.h + 1) < y || r.y > (y + h + 1 )) ) ) {
return true;
}
}
return false;
}
RandomGenerator nextRandomGenerator_;
std::vector<Level> levels_;
public:
LevelGenerator( RandomGenerator randomGenerator, const uint32_t numLevels ) :
nextRandomGenerator_( randomGenerator ), levels_( numLevels, Level() ) {}
void partitionedGenerateLevels( uint32_t seed, const uint32_t partitionStartIndex,
const uint32_t partitionEndIndex ) {
for( uint32_t i = partitionStartIndex ; i < partitionEndIndex ; ++i ) {
for( uint32_t ii = 0 ; ii < 50000 ; ii++ ) {
makeRoomSilentlyFail( levels_[i], seed );
if( levels_[i].rooms.size() == 99 ) {
break;
}
}
levels_[i].fillTiles();
}
}
template <typename LevelMetric>
Level & pickLevelByCriteria( LevelMetric levelMetric ) {
auto lIter = levels_.begin(), lEnd = levels_.end();
Level & result( *lIter++ );
for( ; lIter != lEnd ; ++lIter ) {
if( levelMetric.isBetterLevel( result, *lIter ) ) {
result = *lIter;
}
}
return result;
}
};
struct NumRoomsMetric {
bool isBetterLevel( const Level & x, const Level & y ) {
return y.rooms.size() > x.rooms.size();
}
};
void printLevel( const Level & level ) {
for( uint32_t i = 0 ; i < 2500 ; i++ ) {
printf( "%d", (level.tiles[i].T ? 1 : 0 ) );
if( i % ( TILE_DIM ) == 49 && i != 0 ) printf( "\n" );
}
}
int main(int argc, char* argv[]) {
clock_t start, stop;
start = clock();
int v = atoi(argv[1]);
printf("The random seed is: %d \n", v);
srand(v);
GenRandGenerator randGenerator;
LevelGenerator<GenRandGenerator> levelGenerator( randGenerator, NUM_LEVS );
std::vector<std::thread> threads( NUM_THREADS );
for( uint32_t i = 0 ; i < NUM_THREADS ; ++i ) {
uint32_t threadSeed = v * ((i+1)*(i+1));
printf("The seed of thread %d is: %d\n", i + 1, threadSeed );
uint32_t partitionStartIndex( i * NUM_LEVS_PER_THREAD );
uint32_t partitionEndIndex( partitionStartIndex + NUM_LEVS_PER_THREAD );
threads[i] = std::move( std::thread {
std::bind( &LevelGenerator<GenRandGenerator>::partitionedGenerateLevels,
&levelGenerator, threadSeed, partitionStartIndex, partitionEndIndex ) } );
}
for( uint32_t i = 0 ; i < NUM_THREADS ; ++i ) {
threads[i].join();
}
NumRoomsMetric numRoomsMetric;
Level & l( levelGenerator.pickLevelByCriteria( numRoomsMetric ) );
printLevel( l );
stop = clock();
long clocks_per_ms = CLOCKS_PER_SEC/1000;
printf("%ld\n", (stop - start)/clocks_per_ms);
return 0;
}