-
Notifications
You must be signed in to change notification settings - Fork 16
/
PGo.go
145 lines (131 loc) · 2.41 KB
/
PGo.go
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
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"time"
)
const (
TileDim = 50
WidMin = 2
RestWidMax = 8
NumLevs = 800
NumTries = 50000
DefaultSeed = "18"
)
type Tile uint8
type Room struct {
X, Y, W, H, N uint32
}
type Lev struct {
ts []Tile
rs []*Room
}
var seed uint32
func Rand(seed *uint32) (s uint32) {
s = *seed
s <<= 1
sext := uint32(int32(s)>>31) & 0x88888eef
s ^= sext ^ 1
*seed = s
return
}
func CheckColl(x, y, w, h uint32, rs []*Room) bool {
for _, r := range rs {
if (r.X+r.W+1) < x || r.X > (x+w+1) {
continue
}
if (r.Y+r.H+1) < y || r.Y > (y+h+1) {
continue
}
return true
}
return false
}
func MakeRoom(count uint32, seed *uint32) []*Room {
rs := make([]*Room, 0, 100)
for i := uint32(0); i < count; i++ {
x := Rand(seed) % TileDim
y := Rand(seed) % TileDim
w := Rand(seed)%RestWidMax + WidMin
h := Rand(seed)%RestWidMax + WidMin
if x+w >= TileDim || y+h >= TileDim || x*y == 0 {
continue
}
iscrash := CheckColl(x, y, w, h, rs)
if !iscrash {
rs = append(rs, &Room{x, y, w, h, uint32(len(rs))})
}
if len(rs) == 99 {
break
}
}
return rs
}
func Room2Tiles(r *Room, ts []Tile) {
x := r.X
y := r.Y
w := r.W
h := r.H
for xi := x; xi <= x+w; xi++ {
for yi := y; yi <= y+h; yi++ {
num := yi*TileDim + xi
ts[num] = 1
}
}
}
func PrintLev(l *Lev) {
for i, t := range l.ts {
fmt.Printf("%v", t)
if i%(TileDim) == 49 && i != 0 {
fmt.Print("\n")
}
}
}
func godo(levchan chan<- *Lev, seeds <-chan uint32) {
for seed := range seeds {
rs := MakeRoom(NumTries, &seed)
ts := make([]Tile, 2500)
for _, r := range rs {
Room2Tiles(r, ts)
}
levchan <- &Lev{ts, rs}
}
}
func getSeed() uint32 {
arg0 := DefaultSeed
if len(os.Args) > 1 {
arg0 = os.Args[1]
}
val, err := strconv.ParseUint(arg0, 10, 32)
if err != nil {
panic(err)
}
return uint32(val)
}
func main() {
start := time.Now()
nc := runtime.NumCPU()
runtime.GOMAXPROCS(nc)
v := getSeed()
fmt.Printf("Random seed: %v\n", v)
seed = ^uint32(v)
levchan := make(chan *Lev, NumLevs)
seeds := make(chan uint32, NumLevs)
for i := 0; i < nc; i++ {
go godo(levchan, seeds)
}
for i := uint32(0); i < NumLevs; i++ {
seeds <- seed * (i + 1) * (i + 1)
}
var templ Lev
for i := 0; i < NumLevs; i++ {
x := <-levchan
if len(x.rs) > len(templ.rs) {
templ = *x
}
}
PrintLev(&templ)
fmt.Printf("Time in ms: %d\n", (time.Since(start) / 1000000))
}