-
Notifications
You must be signed in to change notification settings - Fork 11
/
Go.go
144 lines (131 loc) · 2.25 KB
/
Go.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
package main
import (
"flag"
"fmt"
"time"
)
const (
TileDim = 50
MinWid = 2
MaxWid = 8
)
type Tile struct {
X uint32
Y uint32
T uint32
}
type Room struct {
X uint32
Y uint32
W uint32
H uint32
N uint32
}
type Lev struct {
ts []Tile
rs []Room
}
var seed uint32
func GenRand() uint32 {
seed <<= 1
sext := uint32(int32(seed)>>31) & 0x88888eef
seed ^= sext ^ 1
return seed
}
func CheckColl(x, y, w, h uint32, rs []Room) bool {
var r *Room
x_m1, xw_p1 := x-1, x+w+1
y_m1, yh_p1 := y-1, y+h+1
for i := range rs {
r = &rs[i]
switch{
case r.X > xw_p1:
continue
case r.Y > yh_p1 :
continue
case (r.X + r.W) < x_m1:
continue
case (r.Y + r.H) < y_m1:
continue
}
return true
}
return false
}
func MakeRoom(count uint32) *[]Room {
rs := make([]Room, 100)
counter := uint32(0)
for i := uint32(0); i < count; i++ {
r1 := GenRand()
r2 := GenRand()
x := r1 % TileDim
y := r2 % TileDim
if x*y == 0 {
continue
}
w := r1 % MaxWid + MinWid
h := r2 % MaxWid + MinWid
if x+w >= TileDim || y+h >= TileDim {
continue
}
iscrash := CheckColl(x, y, w, h, rs[0:counter])
if iscrash == false {
rs[counter] = Room{x,y,w,h,counter}
counter++
}
if counter == 99 {
break
}
}
x := rs[0:counter]
return &x
}
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].T = 1
}
}
}
func PrintLev(l *Lev) {
for i, t := range l.ts {
fmt.Printf("%v", t.T)
if i%(TileDim) == 49 && i != 0 {
fmt.Print("\n")
}
}
}
var vflag = flag.Int("v", 18, "Random Seed")
func main() {
start := time.Now()
flag.Parse()
var v int = *vflag
fmt.Printf("Random seed: %v\n", v)
seed = ^uint32(v)
ls := make([]Lev, 100)
for i := 0; i < 100; i++ {
rs := MakeRoom(50000)
ts := make([]Tile, 2500)
for ii := uint32(0); ii < 2500; ii++ {
ts[ii] = Tile{X: ii % TileDim, Y: ii / TileDim, T: 0}
}
for _, r := range *rs {
Room2Tiles(&r, &ts)
}
ls[i] = Lev{rs: *rs, ts: ts}
}
templ := Lev{}
for i := 0; i < 100; i++ {
if len(ls[i].rs) > len(templ.rs) {
templ = ls[i]
}
}
PrintLev(&templ)
end := time.Now()
fmt.Printf("Time in ms: %d\n", (end.Sub(start) / 1000000))
}