-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
327 lines (303 loc) · 11 KB
/
environment.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"sync"
"math"
"math/rand"
"github.com/nsf/termbox-go"
"github.com/mwindels/go-rogue/adt"
"github.com/mwindels/go-rogue/geom"
)
const (
roomCreateLow float64 = 0.2
roomCreateHigh float64 = 1.0
roomDestroyLow float64 = 0.5
roomDestroyHigh float64 = 1.0
)
var (
floorTile termbox.Cell = termbox.Cell{Ch: ' ', Fg: termbox.ColorDefault, Bg: termbox.ColorBlack}
wallTile termbox.Cell = termbox.Cell{Ch: '░', Fg: termbox.ColorDefault, Bg: termbox.ColorBlack}
)
var (
neighbourPoints [8]geom.Point = [8]geom.Point{
geom.InitPoint(-1.0, -1.0),
geom.InitPoint(0.0, -1.0),
geom.InitPoint(1.0, -1.0),
geom.InitPoint(1.0, 0.0),
geom.InitPoint(1.0, 1.0),
geom.InitPoint(0.0, 1.0),
geom.InitPoint(-1.0, 1.0),
geom.InitPoint(-1.0, 0.0),
}
)
type environment struct {
mutex sync.RWMutex
paused adt.Barrier
//Immutable
width int
height int
tiles [][]termbox.Cell
rooms adt.BSPTree
//Mutable
entities [][](*entity)
player *entity
}
func generatePartitions(w, h int, partitionCount int) []geom.Point {
partitions := make([]geom.Point, partitionCount, partitionCount)
for i := 0; i < partitionCount; i++ {
partitions[i] = geom.InitPoint(math.Trunc(rand.Float64() * float64(w)), math.Trunc(rand.Float64() * float64(h)))
}
return partitions
}
func initEnvironment(w, h int) environment {
env := environment{
mutex: sync.RWMutex{},
paused: adt.InitBarrier(true),
width: w,
height: h,
tiles: make([][]termbox.Cell, w),
rooms: adt.InitBSPTree(geom.InitRectangle(0.0, 0.0, float64(w), float64(h)), generatePartitions(w, h, w * h)), //w * h is just a stand-in for a more complicated partition count function
entities: make([][](*entity), w),
player: nil,
}
for row := 0; row < int(w); row++ {
env.tiles[row] = make([]termbox.Cell, h)
env.entities[row] = make([](*entity), h)
}
return env
}
func createRoomPredicate(relativeDepth float64) bool {
return roomCreateLow <= relativeDepth && relativeDepth <= roomCreateHigh && rand.Float64() * (roomCreateHigh - roomCreateLow) <= 0.5 * (relativeDepth - roomCreateLow)
}
func destroyRoomPredicate(relativeDepth float64) bool {
return roomDestroyLow <= relativeDepth && relativeDepth <= roomDestroyHigh && rand.Float64() * (roomDestroyHigh - roomDestroyLow) <= 0.75 * (relativeDepth - roomDestroyLow)
}
func (e *environment) generateRooms(randomizations int) {
for i := 0; i < randomizations; i++ {
e.rooms.RandomizeTraversability(createRoomPredicate, destroyRoomPredicate)
}
var stk adt.Stack
func() {
defer func() {recover()}()
stk.Push(e.rooms.Root())
}()
for !(stk.IsEmpty()) {
node, valid := stk.Pop().(adt.BSPNode)
if !valid {
panic("Popped an element from the tile-filling stack that wasn't a BSPNode!")
}
if node.Traversability() != adt.SemiOpen {
for x := int(node.Area().UpperLeft().X); x < int(node.Area().UpperRight().X); x++ {
for y := int(node.Area().UpperLeft().Y); y < int(node.Area().LowerLeft().Y); y++ {
if node.Traversability() == adt.Open {
e.tiles[x][y] = floorTile
}else if node.Traversability() == adt.Closed {
e.tiles[x][y] = wallTile
}
}
}
}else{ //A semi-open node implies that the node is not a leaf, so no need to recover from Left() or Right().
stk.Push(node.Right())
stk.Push(node.Left())
}
}
}
func nearestRoomPairs(leftRooms, rightRooms []geom.Rectangle) ([][2]geom.Rectangle, float64) {
distance := math.Inf(1)
var nearest [][2]geom.Rectangle
for _, current := range leftRooms {
currentNearest, currentDistance := geom.NearestRectangles(current, rightRooms...)
if float32(currentDistance) <= float32(distance) { //cuts them down to fewer bits to reduce the effects of round-off error
if float32(currentDistance) < float32(distance) {
distance = currentDistance
nearest = make([][2]geom.Rectangle, 0, len(currentNearest))
}
for i := 0; i < len(currentNearest); i++ {
nearest = append(nearest, [2]geom.Rectangle{current, currentNearest[i]})
}
}
}
return nearest, distance
}
func twoSegmentHall(left, right geom.Rectangle, partitionDimension uint) []geom.Point {
var path []geom.Point
var start, end geom.Point
if partitionDimension % 2 == 0 { //partition is in the x dimension.
start = geom.InitPoint(left.UpperRight().X - math.Trunc(rand.Float64() * (adt.MinPartitionWidth - 2.0)) - 2.0, left.UpperLeft().Y + math.Trunc(rand.Float64() * (left.Height() - 2.0)) + 1.0)
end = geom.InitPoint(right.UpperLeft().X + math.Trunc(rand.Float64() * (adt.MinPartitionWidth - 2.0)) + 1.0, right.UpperLeft().Y + math.Trunc(rand.Float64() * (right.Height() - 2.0)) + 1.0)
}else{ //partition is in the y dimension.
start = geom.InitPoint(left.UpperLeft().X + math.Trunc(rand.Float64() * (left.Width() - 2.0)) + 1.0, left.LowerLeft().Y - math.Trunc(rand.Float64() * (adt.MinPartitionHeight - 2.0)) - 2.0)
end = geom.InitPoint(right.UpperLeft().X + math.Trunc(rand.Float64() * (right.Width() - 2.0)) + 1.0, right.UpperLeft().Y + math.Trunc(rand.Float64() * (adt.MinPartitionHeight - 2.0)) + 1.0)
}
//start := geom.InitPoint(left.UpperLeft().X + math.Trunc(rand.Float64() * (left.Width() - 2.0)) + 1.0, left.UpperLeft().Y + math.Trunc(rand.Float64() * (left.Height() - 2.0)) + 1.0)
//end := geom.InitPoint(right.UpperLeft().X + math.Trunc(rand.Float64() * (right.Width() - 2.0)) + 1.0, right.UpperLeft().Y + math.Trunc(rand.Float64() * (right.Height() - 2.0)) + 1.0)
current := geom.InitPoint(start.X, start.Y)
moveX := func () {
for current.X != end.X {
path = append(path, current)
if start.X < end.X {
current.X += 1
}else{
current.X -= 1
}
}
}
moveY := func() {
for current.Y != end.Y {
path = append(path, current)
if start.Y < end.Y {
current.Y += 1
}else{
current.Y -= 1
}
}
}
if rand.Intn(2) == 0 {
moveX()
moveY()
}else{
moveY()
moveX()
}
return path
}
func threeSegmentHall(left, right geom.Rectangle, partitionDimension uint) []geom.Point {
var path []geom.Point
start := geom.InitPoint(left.UpperLeft().X + math.Trunc(rand.Float64() * (left.Width() - 2.0)) + 1.0, left.UpperLeft().Y + math.Trunc(rand.Float64() * (left.Height() - 2.0)) + 1.0)
end := geom.InitPoint(right.UpperLeft().X + math.Trunc(rand.Float64() * (right.Width() - 2.0)) + 1.0, right.UpperLeft().Y + math.Trunc(rand.Float64() * (right.Height() - 2.0)) + 1.0)
current := geom.InitPoint(start.X, start.Y)
if partitionDimension % 2 == 0 { //partition is in the x dimension, just like in the BSPTree.
cornerPoint := geom.InitPoint(left.UpperRight().X + math.Trunc(rand.Float64() * (right.UpperLeft().X - left.UpperRight().X - 2.0)) + 1.0, start.Y)
for !geom.PointsEqual(current, cornerPoint) {
path = append(path, current, cornerPoint)
current.X += 1
}
for current.Y != end.Y {
path = append(path, current)
if start.Y < end.Y {
current.Y += 1
}else{
current.Y -= 1
}
}
for !geom.PointsEqual(current, end) {
path = append(path, current)
current.X += 1
}
}else{ //partition is in the y dimension (like BSPTree).
cornerPoint := geom.InitPoint(start.X, left.LowerLeft().Y + math.Trunc(rand.Float64() * (right.UpperLeft().Y - left.LowerLeft().Y - 2.0)) + 1.0)
for !geom.PointsEqual(current, cornerPoint) {
path = append(path, current)
current.Y += 1
}
for current.X != end.X {
path = append(path, current)
if start.X < end.X {
current.X += 1
}else{
current.X -= 1
}
}
for !geom.PointsEqual(current, end) {
path = append(path, current)
current.Y += 1
}
}
return path
}
func (e *environment) connectRooms(node adt.BSPNode, depth uint) []geom.Rectangle {
if node.Traversability() == adt.Closed {
return []geom.Rectangle{}
}else if node.Traversability() == adt.Open {
return []geom.Rectangle{node.Area()}
}
leftRooms := e.connectRooms(node.Left(), depth + 1) //no need to recover, since the node is implicitly semi-open, hence not a leaf.
rightRooms := e.connectRooms(node.Right(), depth + 1) //likewise.
if len(leftRooms) > 0 && len(rightRooms) > 0 {
var hall []geom.Point
nearestLeft, leftDistance := geom.NearestRectangles(node.Right().Area(), leftRooms...)
nearestRight, rightDistance := geom.NearestRectangles(node.Left().Area(), rightRooms...)
if leftDistance == 0.0 && rightDistance == 0.0 {
nearestPairs, nearestDistance := nearestRoomPairs(nearestLeft, nearestRight)
randomPair := nearestPairs[rand.Intn(len(nearestPairs))]
if nearestDistance > 0.0 { //may need to prefer room pairs which share edges over those which only share corners (but both have a distance of 0).
hall = twoSegmentHall(randomPair[0], randomPair[1], depth % 2)
}else{
pairSharesEdge := false
for _, pair := range nearestPairs {
intersection := geom.RectanglesIntersection(pair[0], pair[1])
if intersection.Width() > 0.0 || intersection.Height() > 0.0 {
pairSharesEdge = true
break
}
}
if !pairSharesEdge {
hall = twoSegmentHall(randomPair[0], randomPair[1], depth % 2)
}
}
}else{
hall = threeSegmentHall(nearestLeft[rand.Intn(len(nearestLeft))], nearestRight[rand.Intn(len(nearestRight))], depth % 2)
}
start, end := 0, len(hall)
for i, tile := range hall {
for j := 0; j < 4; j++ {
neighbour := geom.InitPoint(tile.X + neighbourPoints[2 * j + 1].X, tile.Y + neighbourPoints[2 * j + 1].Y)
if geom.RectangleContainsInclusive(node.Area(), neighbour) {
if e.tiles[int(neighbour.X)][int(neighbour.Y)] == floorTile {
if geom.RectangleContainsInclusive(node.Right().Area(), neighbour) {
end = i + 1
}else{
start = i
}
}
}
}
if end < len(hall) {
break
}
}
hall = hall[start:end]
for _, tile := range hall {
e.tiles[int(tile.X)][int(tile.Y)] = floorTile
}
}
return append(leftRooms, rightRooms...)
}
func generateEnvironment(e environment) environment {
e.generateRooms(1)
e.connectRooms(e.rooms.Root(), 0)
return e
}
func (e *environment) populateRooms(envRcv <-chan *environment, envRqst chan<- bool) {
var stk adt.Stack
func() {
defer func() {recover()}()
stk.Push(e.rooms.Root())
}()
for !(stk.IsEmpty()) {
node, valid := stk.Pop().(adt.BSPNode)
if !valid {
panic("Popped an element from the room-populating stack that wasn't a BSPNode!")
}
if node.Traversability() == adt.SemiOpen { //no recover required, since the calls below won't panic
stk.Push(node.Right())
stk.Push(node.Left())
}else if node.Traversability() == adt.Open {
/*for x := 0; x < int(node.Area().Width()); x++ {
for y := 0; y < int(node.Area().Height()); y++ {
go runEntity(envRcv, envRqst, initEntity('c', termbox.ColorRed, 500, int(node.Area().UpperLeft().X) + x, int(node.Area().UpperLeft().Y) + y))
}
}*/
go runEntity(envRcv, envRqst, initEntity('c', termbox.ColorRed, 500, int(node.Area().UpperLeft().X + rand.Float64() * node.Area().Width()), int(node.Area().UpperLeft().Y + rand.Float64() * node.Area().Height())))
}
}
}
func runEnvController(envSnd chan *environment, envRqst chan bool) {
env := generateEnvironment(initEnvironment(150, 150))
env.populateRooms(envSnd, envRqst)
for {
select{
case <-envRqst:
envSnd <- &env
}
}
}