-
Notifications
You must be signed in to change notification settings - Fork 18
/
grid.go
619 lines (480 loc) · 15.8 KB
/
grid.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package tetra3d
import (
"math"
"math/rand"
"sort"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
// A GridConnection represents a one-way connection from one GridPoint to another.
type GridConnection struct {
To *GridPoint
Passable bool // Whether the connection should be considered as passable when performing pathfinding.
Cost float64 // The cost of the jump, from one grid point to another. Defaults to 0.
length float64 // the length of the jump from one GridPoint to another.
}
// Clone the GridConnection.
func (c *GridConnection) Clone() *GridConnection {
return &GridConnection{
To: c.To,
Passable: c.Passable,
Cost: c.Cost,
}
}
// GridPoint represents a point on a Grid, used for pathfinding or connecting points in space.
// GridPoints are parented to a Grid and the connections are created seperate from their positions,
// which means you can move GridPoints freely after creation. Note that GridPoints consider themselves
// to be in the same Grid only if they have the same direct parent (being the Grid).
type GridPoint struct {
*Node
Connections []*GridConnection
sortedConnections []*GridConnection
prevLink *GridPoint
costSoFar float64
}
// NewGridPoint creates a new GridPoint.
func NewGridPoint(name string) *GridPoint {
return NewGridPointFromNode(NewNode(name))
}
// NewGridPoint creates a new GridPoint from an existing Node.
func NewGridPointFromNode(node *Node) *GridPoint {
gridPoint := &GridPoint{
Node: node,
Connections: []*GridConnection{},
}
gridPoint.owner = gridPoint
return gridPoint
}
// Clone clones the given GridPoint.
func (point *GridPoint) Clone() INode {
newPoint := &GridPoint{}
newPoint.Node = point.Node.clone(point).(*Node)
newPoint.Connections = append([]*GridConnection{}, point.Connections...)
newPoint.sortedConnections = append([]*GridConnection{}, point.Connections...)
if newPoint.Callbacks() != nil && newPoint.Callbacks().OnClone != nil {
newPoint.Callbacks().OnClone(newPoint)
}
return newPoint
}
// IsConnected returns if the provided GridPoint is connected to the given other GridPoint.
func (point *GridPoint) IsConnected(other *GridPoint) bool {
for _, c := range point.Connections {
if c.To == other {
return true
}
}
return false
}
// IsOnSameGrid returns if the grid point is on the same grid as the other given GridPoint.
func (point *GridPoint) IsOnSameGrid(other *GridPoint) bool {
return point.parent == other.parent
}
// Connect connects the GridPoint to the given other GridPoint.
func (point *GridPoint) Connect(other *GridPoint) (aToB, bToA *GridConnection) {
if point == other {
return
}
if !point.IsConnected(other) {
aToB = &GridConnection{
To: other,
Passable: true,
length: point.DistanceTo(other),
}
point.Connections = append(point.Connections, aToB)
point.sortedConnections = append(point.sortedConnections, aToB)
}
if !other.IsConnected(point) {
bToA = &GridConnection{
To: point,
Passable: true,
length: other.DistanceTo(point),
}
other.Connections = append(other.Connections, bToA)
other.sortedConnections = append(other.sortedConnections, bToA)
}
return
}
// Connection returns the GridConnection from one GridPoint to another.
// If they aren't connected, this function will return nil.
// Note that a connection between GridPoints goes both ways (so there are
// two connections between two GridPoints - one from either direction).
func (point *GridPoint) Connection(other *GridPoint) *GridConnection {
for _, c := range point.Connections {
if c.To == other {
return c
}
}
return nil
}
// Disconnect disconnects the GridPoint from the given other GridPoint.
func (point *GridPoint) Disconnect(other *GridPoint) {
if point == other {
return
}
for i := len(point.Connections) - 1; i >= 0; i-- {
for si := range point.sortedConnections {
if point.sortedConnections[si] == point.Connections[i] {
point.sortedConnections[si] = nil
point.sortedConnections = append(point.sortedConnections[:si], point.sortedConnections[si+1:]...)
}
}
if point.Connections[i].To == other {
point.Connections[i] = nil
point.Connections = append(point.Connections[:i], point.Connections[i+1:]...)
}
}
for i := len(other.Connections) - 1; i >= 0; i-- {
for si := range point.sortedConnections {
if point.sortedConnections[si] == point.Connections[i] {
point.sortedConnections[si] = nil
point.sortedConnections = append(point.sortedConnections[:si], point.sortedConnections[si+1:]...)
}
}
if other.Connections[i].To == point {
other.Connections[i] = nil
other.Connections = append(other.Connections[:i], other.Connections[i+1:]...)
}
}
}
// DisconnectAll disconnects the GridPoint from all other GridPoints.
func (point *GridPoint) DisconnectAll() {
for i := len(point.Connections) - 1; i >= 0; i-- {
point.Disconnect(point.Connections[i].To)
}
}
// PathTo creates a path going from the GridPoint to the given other GridPoint. The path generated
// should be the shortest-possible route, taking into account both the cumulative lengths (in units)
// and costs of individual hops.
// If a path is not possible from the starting point to the end point, then PathTo will return nil.
func (point *GridPoint) PathTo(goal *GridPoint) *GridPath {
if point.parent == nil || point.parent.Type() != NodeTypeGrid || !point.IsOnSameGrid(goal) {
return nil
}
if point == goal {
return &GridPath{
GridPoints: []Vector{point.WorldPosition()},
}
}
point.parent.(*Grid).ForEachPoint(func(gridPoint *GridPoint) {
gridPoint.prevLink = nil
gridPoint.costSoFar = 0
})
path := &GridPath{
GridPoints: []Vector{},
}
toCheck := []*GridPoint{point}
goal.prevLink = nil
point.prevLink = nil
var next *GridPoint
// finishedForLoop:
for {
if next == goal {
break
}
if len(toCheck) == 0 {
return nil
}
next = toCheck[0]
toCheck = toCheck[1:]
for _, c := range next.Connections {
if c.Passable {
nextCost := next.costSoFar + c.Cost + c.length
if c.To != point && (c.To.costSoFar == 0 || c.To.costSoFar > nextCost) {
c.To.costSoFar = nextCost
c.To.prevLink = next
toCheck = append(toCheck, c.To)
}
}
}
sort.Slice(toCheck, func(i, j int) bool { return toCheck[i].costSoFar < toCheck[j].costSoFar })
}
for next.prevLink != nil {
path.GridPoints = append(path.GridPoints, next.WorldPosition())
next = next.prevLink
}
for i, j := 0, len(path.GridPoints)-1; i < j; i, j = i+1, j-1 {
path.GridPoints[i], path.GridPoints[j] = path.GridPoints[j], path.GridPoints[i]
}
return path
}
func (point *GridPoint) insertIntoSlice(slice []*GridPoint, index int, value *GridPoint) []*GridPoint {
if len(slice) == index { // nil or empty slice or after last element
return append(slice, value)
}
slice = append(slice[:index+1], slice[index:]...) // index < len(a)
slice[index] = value
return slice
}
////////////
// Type returns the NodeType for this object.
func (point *GridPoint) Type() NodeType {
return NodeTypeGridPoint
}
// Grid represents a collection of points and the connections between them. A Grid can be used for pathfinding
// or simply for connecting points in space (like for a world map in a level-based game, for example).
type Grid struct {
*Node
}
// NewGrid creates a new Grid.
func NewGrid(name string) *Grid {
g := &Grid{Node: NewNode(name)}
g.owner = g
return g
}
// Clone creates a clone of this GridPoint.
func (grid *Grid) Clone() INode {
newGrid := &Grid{}
newGrid.Node = grid.Node.clone(newGrid).(*Node)
for _, child := range newGrid.children {
child.setParent(newGrid)
}
for _, c := range newGrid.Points() {
c.Connections = []*GridConnection{}
}
for _, c := range grid.Points() {
start := newGrid.ClosestGridPoint(c.LocalPosition())
for _, connect := range c.Connections {
end := newGrid.ClosestGridPoint(connect.To.LocalPosition())
start.Connect(end)
}
}
if newGrid.Callbacks() != nil && newGrid.Callbacks().OnClone != nil {
newGrid.Callbacks().OnClone(newGrid)
}
return newGrid
}
// Points returns a slice of the children nodes that constitute this Grid's GridPoints.
func (grid *Grid) Points() []*GridPoint {
points := make([]*GridPoint, 0, len(grid.children))
for _, n := range grid.children {
if gp, ok := n.(*GridPoint); ok {
points = append(points, gp)
}
}
return points
}
// ForEachPoint returns a slice of the children nodes that constitute this Grid's GridPoints.
func (grid *Grid) ForEachPoint(forEach func(gridPoint *GridPoint)) {
for _, n := range grid.children {
if gp, ok := n.(*GridPoint); ok {
forEach(gp)
}
}
}
// DisconnectAllPoints disconnects all points from each other in the Grid.
func (grid *Grid) DisconnectAllPoints() {
for _, point := range grid.Points() {
point.DisconnectAll()
}
}
func (grid *Grid) MergeDuplicatePoints(margin float64) {
grid.ForEachPoint(func(point *GridPoint) {
grid.ForEachPoint(func(point2 *GridPoint) {
if point == point2 {
return
}
if point.WorldPosition().DistanceSquared(point2.WorldPosition()) < margin {
for _, c := range point2.Connections {
nc := c.Clone()
point.Connections = append(point.Connections, nc)
nc2 := c.To.Connection(point2).Clone()
nc2.To = point
}
point2.DisconnectAll()
point2.Unparent()
}
})
})
}
// HopCount indicates an instances of how many hops it takes to get to the specified GridPoint from the starting GridPoint.
type HopCount struct {
Start *GridPoint // The start of the path
Destination *GridPoint // The end of the path
HopCount int // How many hops it takes to get there (i.e. length of the path between the two points minus one)
}
// HopCounts returns the number of hops from the closest grid point to the starting position (from)
// to the closest grid points to all other provided positions.
func (grid *Grid) HopCounts(from Vector, targetPositions ...Vector) []HopCount {
start := grid.ClosestGridPoint(from)
hopCounts := []HopCount{}
for _, point := range targetPositions {
hc := HopCount{
Start: start,
Destination: grid.ClosestGridPoint(point),
}
hc.HopCount = len(start.PathTo(hc.Destination).GridPoints) - 1
hopCounts = append(hopCounts, hc)
}
sort.Slice(hopCounts, func(i, j int) bool {
return hopCounts[i].HopCount < hopCounts[j].HopCount
})
return hopCounts
}
// ClosestPositionOnGrid returns the nearest world position on the Grid to the given world position.
// This position can be directly on a GridPoint, or on a connection between GridPoints.
func (grid *Grid) ClosestPositionOnGrid(position Vector) Vector {
nearestPoint := grid.ClosestGridPoint(position)
start := nearestPoint.WorldPosition()
dist := math.MaxFloat64
endPos := position
for _, connection := range nearestPoint.Connections {
// diff := connection.WorldPosition().Sub(pos)
end := connection.To.WorldPosition()
segment := end.Sub(start)
newPos := position.Sub(start)
t := newPos.Dot(segment) / segment.Dot(segment)
if t > 1 {
t = 1
} else if t < 0 {
t = 0
}
newPos.X = start.X + segment.X*t
newPos.Y = start.Y + segment.Y*t
newPos.Z = start.Z + segment.Z*t
nd := newPos.DistanceSquared(position)
if nd < dist {
dist = nd
endPos = newPos
}
}
return endPos
}
// ClosestGridPoint returns the nearest grid point to the given world position.
func (grid *Grid) ClosestGridPoint(position Vector) *GridPoint {
points := grid.Points()
sort.Slice(points, func(i, j int) bool {
return points[i].WorldPosition().Sub(position).MagnitudeSquared() < points[j].WorldPosition().Sub(position).MagnitudeSquared()
})
return points[0]
}
// FurthestGridPoint returns the furthest grid point to the given world position.
func (grid *Grid) FurthestGridPoint(position Vector) *GridPoint {
points := grid.Points()
sort.Slice(points, func(i, j int) bool {
return points[i].WorldPosition().Sub(position).MagnitudeSquared() < points[j].WorldPosition().Sub(position).MagnitudeSquared()
})
return points[len(points)-1]
}
// RandomPoint returns a random grid point in the grid.
func (grid *Grid) RandomPoint() *GridPoint {
gridPoints := grid.Points()
return gridPoints[rand.Intn(len(gridPoints))]
}
// FirstPoint returns the first point out of the Grid's GridPoints.
// If the Grid has no GridPoints, then it will return nil.
func (grid *Grid) FirstPoint() *GridPoint {
gridPoints := grid.Points()
if len(gridPoints) == 0 {
return nil
}
return gridPoints[0]
}
// LastPoint returns the last point out of the Grid's GridPoints.
// If the Grid has no GridPoints, then it will return nil.
func (grid *Grid) LastPoint() *GridPoint {
gridPoints := grid.Points()
if len(gridPoints) == 0 {
return nil
}
return gridPoints[len(gridPoints)-1]
}
// Combine combines the Grid with the other Grids provided. This reparents the other' Grid's GridPoints (and other children)
// to be under the calling Grid's. If two GridPoints share the same position, they will be merged together.
// After combining a Grid with others, the other Grids will automatically be unparented from the scene nodegraph tree
// (as their GridPoints will have been absorbed).
func (grid *Grid) Combine(others ...*Grid) {
for _, other := range others {
if grid == other {
continue
}
for _, p := range other.Children() {
pos := p.WorldPosition()
grid.AddChildren(p)
p.SetWorldPositionVec(pos)
}
// for _, p := range grid.Points() {
// for _, p2 := range grid.Points() {
// if p == p2 {
// continue
// }
// if p.WorldPosition().Equals(p2.WorldPosition()) {
// for _, connect := range p2.Connections {
// p.Connect(connect.To)
// connect.To.Disconnect(p2)
// }
// p2.Unparent()
// }
// }
// }
grid.MergeDuplicatePoints(0.001)
other.Unparent()
}
}
// Center returns the center point of the Grid, given the positions of its GridPoints.
func (grid *Grid) Center() Vector {
pos := Vector{0, 0, 0, 0}
points := grid.Points()
for _, p := range points {
pos = pos.Add(p.WorldPosition())
}
pos = pos.Divide(float64(len(points)))
return pos
}
// Dimensions returns a Dimensions struct, indicating the overall "spread" of the GridPoints composing the Grid.
func (grid *Grid) Dimensions() Dimensions {
gridPoints := grid.Points()
points := make([]Vector, 0, len(gridPoints))
for _, p := range gridPoints {
points = append(points, p.WorldPosition())
}
return NewDimensionsFromPoints(points...)
}
////////
// Type returns the NodeType for this object.
func (grid *Grid) Type() NodeType {
return NodeTypeGrid
}
// GridPath represents a sequence of grid points, used to traverse a path.
// GridPath implements IPath.
type GridPath struct {
GridPoints []Vector
}
// Length returns the length of the overall path.
func (gp *GridPath) Length() float64 {
dist := 0.0
if len(gp.GridPoints) <= 1 {
return 0
}
start := gp.GridPoints[0]
for i := 1; i < len(gp.GridPoints); i++ {
next := gp.GridPoints[i]
dist += next.Sub(start).Magnitude()
start = next
}
return dist
}
// Points returns the points of the GridPath in a slice.
func (gp *GridPath) Points() []Vector {
points := append(make([]Vector, 0, len(gp.GridPoints)), gp.GridPoints...)
return points
}
// HopCount returns the number of hops in the path (i.e. the number of nodes - 1).
func (gp *GridPath) HopCount() int {
return len(gp.GridPoints) - 1
}
func (gp *GridPath) isClosed() bool {
return false
}
func (gp *GridPath) DebugDraw(screen *ebiten.Image, camera *Camera, color Color) {
points := gp.Points()
for i := 0; i < len(points)-1; i++ {
p1 := camera.WorldToScreenPixels(points[i])
p2 := camera.WorldToScreenPixels(points[i+1])
ebitenutil.DrawLine(screen, p1.X, p1.Y, p2.X, p2.Y, color.ToRGBA64())
ebitenutil.DrawCircle(screen, p1.X, p1.Y, 8, color.ToRGBA64())
if i == len(points)-2 {
ebitenutil.DrawCircle(screen, p2.X, p2.Y, 8, color.ToRGBA64())
}
}
}
var _ IPath = &GridPath{} // Sanity check to ensure GridPaths implement paths