-
Notifications
You must be signed in to change notification settings - Fork 16
/
PR.rs
147 lines (129 loc) · 3.47 KB
/
PR.rs
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
extern mod extra;
use std::{os, int, uint, u32, vec, comm};
use extra::time;
static TileDim: u32 = 50;
static WidMin: u32 = 2;
static RestWidMax: u32 = 8;
static NumLevs: uint= 800;
static NumCores: uint = 4;
static NumTries: uint = 50000;
fn main() {
let start = time::precise_time_ns();
let args = os::args();
let str = (args[1]);
let v = int::from_str(str).get_or_default(18);
println(fmt!("The random seed is: %?",v));
let prng: u32 = v.to_u32();
let (port, chan) = stream();
let chan = comm::SharedChan::new(chan);
for uint::range(0,NumCores)|val| {
let child_chan = chan.clone();
do spawn {
let thisnum = val.to_u32()+1;
let mut newprng = prng.clone()*thisnum*thisnum;
child_chan.send(MakeLevs(NumLevs/NumCores,&mut newprng));
}
}
let mut ls: ~[Lev] = ~[];
let mut ii :uint = 0;
while ii < NumCores{
ls = vec::append(ls,port.recv() );
ii+=1;
}
let biggest_lev = find_most_rooms(ls);
print_lev(biggest_lev);
let end = time::precise_time_ns();
let elapsed = end - start;
println(fmt!("Time in ms: %f", (((elapsed as f64)/1000.0/1000.0)) as float));
}
fn MakeLevs(n: uint, gen:&mut u32) -> ~[Lev] {
let ls: ~[Lev] = do vec::from_fn(n) |_| {
let rs = rooms(99, gen);
let mut ts: ~[Tile] = do vec::from_fn( (TileDim * TileDim).to_uint() ) |ii| {
Tile {
x: ii.to_u32() % TileDim,
y: ii.to_u32() / TileDim,
t: false
}
};
for rs.iter().advance |r| {
room_to_tiles(r, &mut ts);
}
Lev { tiles: ts, rooms: rs }
};
ls
}
struct Tile {
x: u32,
y: u32,
t: bool
}
struct Room {
x: u32,
y: u32,
w: u32,
h: u32,
n: u32
}
struct Lev {
tiles: ~[Tile],
rooms: ~[Room],
}
fn find_most_rooms<'a>(ls: &'a [Lev]) -> &'a Lev {
do ls.iter().max_by |lev| {
lev.rooms.len()
}.expect("oops, no levels")
}
fn rooms(n: uint,gen:&mut u32) -> ~[Room] {
let mut rooms = vec::with_capacity(n);
for NumTries.times {
let x = GenRand(gen) % TileDim;
let y = GenRand(gen) % TileDim;
let w = GenRand(gen) % RestWidMax+WidMin;
let h = GenRand(gen) % RestWidMax+WidMin;
if x + w < TileDim &&
y + h < TileDim &&
x != 0 &&
y != 0 &&
not_crash(x, y, w, h, rooms) {
let r = Room { x: x, y: y, w: w, h: h, n: rooms.len().to_u32() };
rooms.push(r);
if rooms.len() == n { break }
}
}
rooms
}
fn not_crash(new_x: u32, new_y: u32, new_w: u32, new_h: u32, rs: &[Room]) -> bool {
do rs.iter().all |r| {
let Room { x, y, w, h, _ } = *r;
((x + w + 1) < new_x ||
x > (new_x + new_w + 1) ||
(y + h + 1) < new_y ||
y > (new_y + new_h + 1))
}
}
fn room_to_tiles(r: &Room, ts: &mut ~[Tile]) {
let Room { x, y, w, h, _ } = *r;
for u32::range(y, y + h + 1) |yi| {
for u32::range(x, x + w + 1) |xi| {
let num = yi * TileDim + xi;
ts[num].t = true;
}
}
}
fn print_lev(l: &Lev) {
for l.tiles.iter().enumerate().advance |(i, tile)| {
print(if tile.t {"1"} else {"0"});
if (i).to_u32() % TileDim == 49 {
print("\n");
}
}
}
fn GenRand(gen:&mut u32) ->u32 {
*gen += *gen;
*gen ^= 1;
if (*gen).to_i32() < 0 {
*gen ^= 0x88888eef;
}
return *gen
}