This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
/
calculation.go
416 lines (333 loc) · 10.1 KB
/
calculation.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
package main
import (
"context"
"encoding/gob"
"errors"
"fmt"
"log"
"math"
"os"
"path"
"strings"
"github.com/cheggaaa/pb"
)
const (
traceTypeFlightStop = "flight_stop"
traceTypeProgress = "trace_progress"
numberOfBucketLayers = 6
)
func init() {
gob.Register(starSystemDatabase{})
}
type traceResult struct {
TraceType string `json:"trace_type"`
FlightDistance float64 `json:"flight_distance"`
Progress float64 `json:"progress"`
Requested bool `json:"requested"`
StarSystem *starSystem `json:"star_system"`
TotalFlightDistance float64 `json:"total_flight_distance"`
}
type coordinateBucket struct {
Parent *coordinateBucket
Bounds [2]starCoordinate
Buckets []*coordinateBucket
Systems []int64
}
func (c *coordinateBucket) CreateSubBuckets(depth int) {
if depth == 0 {
return
}
minC := c.Bounds[0]
maxC := c.Bounds[1]
midX := c.Bounds[0].X + (c.Bounds[1].X-c.Bounds[0].X)/2
midY := c.Bounds[0].Y + (c.Bounds[1].Y-c.Bounds[0].Y)/2
midZ := c.Bounds[0].Z + (c.Bounds[1].Z-c.Bounds[0].Z)/2
c.Buckets = []*coordinateBucket{
{Parent: c, Bounds: [2]starCoordinate{{minC.X, minC.Y, minC.Z}, {midX, midY, midZ}}},
{Parent: c, Bounds: [2]starCoordinate{{midX, minC.Y, minC.Z}, {maxC.X, midY, midZ}}},
{Parent: c, Bounds: [2]starCoordinate{{minC.X, midY, minC.Z}, {midX, maxC.Y, midZ}}},
{Parent: c, Bounds: [2]starCoordinate{{midX, midY, minC.Z}, {maxC.X, maxC.Y, midZ}}},
{Parent: c, Bounds: [2]starCoordinate{{minC.X, minC.Y, midZ}, {midX, midY, maxC.Z}}},
{Parent: c, Bounds: [2]starCoordinate{{midX, minC.Y, midZ}, {maxC.X, midY, maxC.Z}}},
{Parent: c, Bounds: [2]starCoordinate{{minC.X, midY, midZ}, {midX, maxC.Y, maxC.Z}}},
{Parent: c, Bounds: [2]starCoordinate{{midX, midY, midZ}, {maxC.X, maxC.Y, maxC.Z}}},
}
for i := range c.Buckets {
c.Buckets[i].CreateSubBuckets(depth - 1)
}
}
func (c coordinateBucket) ContainsCoordinate(in starCoordinate) bool {
return c.Bounds[0].X <= in.X &&
c.Bounds[0].Y <= in.Y &&
c.Bounds[0].Z <= in.Z &&
c.Bounds[1].X >= in.X &&
c.Bounds[1].Y >= in.Y &&
c.Bounds[1].Z >= in.Z
}
func (c *coordinateBucket) Add(in starCoordinate, id int64) bool {
if !c.ContainsCoordinate(in) {
return false
}
if len(c.Buckets) > 0 {
for _, b := range c.Buckets {
if ok := b.Add(in, id); ok {
return true
}
}
}
c.Systems = append(c.Systems, id)
return true
}
func (c coordinateBucket) GetByCoordinate(in starCoordinate) *coordinateBucket {
if !c.ContainsCoordinate(in) {
return nil
}
for _, b := range c.Buckets {
if ret := b.GetByCoordinate(in); ret != nil {
return ret
}
}
return &c
}
func (c coordinateBucket) GetSystems() []int64 {
out := c.Systems
for _, b := range c.Buckets {
out = append(out, b.GetSystems()...)
}
return out
}
func (c coordinateBucket) InSphere(center starCoordinate, radius float64) bool {
min, max := c.Bounds[0], c.Bounds[1]
return c.ContainsCoordinate(center) ||
center.DistanceLy(min) <= radius ||
center.DistanceLy(starCoordinate{min.X, min.Y, max.Z}) <= radius ||
center.DistanceLy(starCoordinate{min.X, max.Y, max.Z}) <= radius ||
center.DistanceLy(starCoordinate{max.X, min.Y, max.Z}) <= radius ||
center.DistanceLy(starCoordinate{max.X, max.Y, min.Z}) <= radius ||
center.DistanceLy(starCoordinate{max.X, min.Y, min.Z}) <= radius ||
center.DistanceLy(starCoordinate{min.X, max.Y, min.Z}) <= radius ||
center.DistanceLy(max) <= radius
}
func (c coordinateBucket) GetFilledBucketsBySphere(center starCoordinate, radius float64) []*coordinateBucket {
out := []*coordinateBucket{}
if c.InSphere(center, radius) && len(c.Systems) > 0 {
out = append(out, &c)
}
for _, b := range c.Buckets {
out = append(out, b.GetFilledBucketsBySphere(center, radius)...)
}
return out
}
type starSystemDatabase struct {
Min, Max starCoordinate
Systems map[int64]*starSystem
NameDB map[string]int64
coordinateBucket *coordinateBucket
}
func loadStarSystems() (*starSystemDatabase, error) {
starSystems := newStarSystemDatabase()
dump, err := os.Open(path.Join(cfg.EDSMDumpPath, readableDumpName))
if err != nil {
return starSystems, err
}
defer dump.Close()
if err := gob.NewDecoder(dump).Decode(&starSystems); err != nil {
return nil, err
}
starSystems.GenerateCoordinateBuckets(starSystems.Min, starSystems.Max)
bar := pb.New(len(starSystems.Systems))
if !cfg.Silent {
bar.Start()
}
for id, sys := range starSystems.Systems {
starSystems.coordinateBucket.Add(sys.Coords, id)
bar.Increment()
}
bar.Finish()
verboseLog("Database loaded successfully.")
return starSystems, nil
}
func newStarSystemDatabase() *starSystemDatabase {
return &starSystemDatabase{
Systems: make(map[int64]*starSystem),
NameDB: make(map[string]int64),
}
}
func (systems *starSystemDatabase) GenerateCoordinateBuckets(min, max starCoordinate) error {
startBucket := &coordinateBucket{
Bounds: [2]starCoordinate{min, max},
Buckets: []*coordinateBucket{},
Systems: []int64{},
}
startBucket.CreateSubBuckets(numberOfBucketLayers - 1)
systems.coordinateBucket = startBucket
return nil
}
func (systems *starSystemDatabase) AddSystem(s *starSystem) error {
systems.Systems[s.ID] = s
systems.NameDB[strings.ToLower(s.Name)] = s.ID
return nil
}
func (systems starSystemDatabase) CalculateRoute(ctx context.Context, a, b *starSystem, stopDistance float64) (<-chan traceResult, <-chan error) {
out := make(chan traceResult, 100)
err := make(chan error, 10)
if a == nil || b == nil {
close(out)
err <- errors.New("Systems must not be nil")
} else {
go systems.startRouteTracer(ctx, out, err, a, b, stopDistance)
}
return out, err
}
func (systems starSystemDatabase) startRouteTracer(ctx context.Context, rChan chan traceResult, eChan chan error, a, b *starSystem, stopDistance float64) {
doneChan := make(chan struct{})
defer close(doneChan)
starSystemsLock.RLock()
defer starSystemsLock.RUnlock()
keepRunning := true
go func() {
defer close(rChan)
defer close(eChan)
for {
select {
case <-doneChan:
return
case <-ctx.Done():
keepRunning = false
eChan <- fmt.Errorf("Calculation deadline of %s exceeded.", cfg.WebRouteTimeout)
}
}
}()
if route, err := cache.GetRoute(*a, *b, int64(stopDistance)); err == nil {
for _, tr := range route {
rChan <- tr
if !keepRunning {
break
}
}
return
}
forbiddenSystemIDs := []int64{}
flightPlan := []*starSystem{}
oldStop := a
for oldStop.Coords.DistanceLy(b.Coords) > 0 && keepRunning {
forbiddenSystemIDs = append(forbiddenSystemIDs, oldStop.ID)
idealTargetCoordinate := oldStop.Coords.PartVectorTarget(b.Coords, stopDistance)
sphericMatchSystems := bucketListToSystemList(systems.coordinateBucket.GetFilledBucketsBySphere(idealTargetCoordinate, stopDistance*.1))
sphericMatchSystems = sphericMatchSystems.
Filter(func(s *starSystem) bool { return s.Coords.DistanceLy(oldStop.Coords) <= stopDistance }).
Filter(func(s *starSystem) bool {
for _, id := range forbiddenSystemIDs {
if s.ID == id {
return false
}
}
return true
})
stop := sphericMatchSystems.GetNearestSystem(idealTargetCoordinate)
if stop == nil {
if len(flightPlan) <= 1 {
eChan <- errors.New("Was not able to calculate first stop, canceling calculation now.")
return
}
flightPlan = flightPlan[0 : len(flightPlan)-1]
oldStop = flightPlan[len(flightPlan)-1]
continue
}
flightPlan = append(flightPlan, stop)
oldStop = stop
rChan <- traceResult{
TraceType: traceTypeProgress,
Progress: (a.Coords.DistanceLy(b.Coords) - stop.Coords.DistanceLy(b.Coords)) / a.Coords.DistanceLy(b.Coords),
}
}
cachedRoute := []traceResult{}
t := traceResult{
TraceType: traceTypeFlightStop,
StarSystem: a,
}
rChan <- t
cachedRoute = append(cachedRoute, t)
totalFlight := 0.0
oldStop = a
for _, s := range flightPlan {
totalFlight += oldStop.Coords.DistanceLy(s.Coords)
t = traceResult{
TraceType: traceTypeFlightStop,
FlightDistance: oldStop.Coords.DistanceLy(s.Coords),
Progress: (a.Coords.DistanceLy(b.Coords) - s.Coords.DistanceLy(b.Coords)) / a.Coords.DistanceLy(b.Coords),
StarSystem: s,
TotalFlightDistance: totalFlight,
}
rChan <- t
cachedRoute = append(cachedRoute, t)
oldStop = s
}
if err := cache.StoreRoute(*a, *b, int64(stopDistance), cachedRoute); err != nil {
log.Printf("Could not cache route: %s", err)
}
doneChan <- struct{}{}
}
func (systems starSystemDatabase) GetSystemByNearestCoordinate(coords starCoordinate, skipSystem starSystem) *starSystem {
var storedSystem *starSystem
bucket := systems.coordinateBucket.GetByCoordinate(coords)
if bucket == nil {
log.Printf("Could not find bucket for coordinate %#v (outer bounds: %#v / %#v)", coords, systems.Min, systems.Max)
return nil
}
for storedSystem == nil {
dist := math.MaxFloat64
for _, i := range bucket.GetSystems() {
if d := systems.Systems[i].Coords.DistanceLy(coords); d < dist && systems.Systems[i].ID != skipSystem.ID {
dist = d
storedSystem = systems.Systems[i]
}
}
bucket = bucket.Parent
if bucket == nil {
log.Printf("Parent was nil.")
break
}
}
return storedSystem
}
func (systems starSystemDatabase) GetSystemByName(name string) *starSystem {
if systemID, ok := systems.NameDB[strings.ToLower(name)]; ok {
return systems.Systems[systemID]
}
return nil
}
func (systems starSystemDatabase) GetSystemByID(id int64) *starSystem {
return systems.Systems[id]
}
type systemList []*starSystem
func (s systemList) Filter(f func(system *starSystem) bool) systemList {
out := systemList{}
for _, sys := range s {
if f(sys) {
out = append(out, sys)
}
}
return out
}
func (s systemList) GetNearestSystem(coords starCoordinate) *starSystem {
lowestDist := math.MaxFloat64
var result *starSystem
for i := range s {
sys := s[i]
if dist := sys.Coords.DistanceLy(coords); dist < lowestDist {
result = sys
lowestDist = dist
}
}
return result
}
func bucketListToSystemList(c []*coordinateBucket) systemList {
out := systemList{}
for _, cb := range c {
for _, id := range cb.GetSystems() {
out = append(out, starSystems.GetSystemByID(id))
}
}
return out
}