diff options
| author | 2026-02-16 16:31:54 -0500 | |
|---|---|---|
| committer | 2026-02-16 16:31:54 -0500 | |
| commit | ca90ebdfa8789654766c5d7969baa7afacd9ebd2 (patch) | |
| tree | 9693e0c7a5af6713f4c5e39372dcf22d05844ec3 /common/loc | |
Diffstat (limited to '')
| -rw-r--r-- | common/loc/loc.go | 82 | ||||
| -rw-r--r-- | common/loc/loc_test.go | 24 |
2 files changed, 106 insertions, 0 deletions
diff --git a/common/loc/loc.go b/common/loc/loc.go new file mode 100644 index 0000000..c82c816 --- /dev/null +++ b/common/loc/loc.go @@ -0,0 +1,82 @@ +package loc + +import ( + "fmt" + "strings" +) + +type Locations struct { + locations []Location +} + +type Location struct { + Coord [3]float32 + Name string +} + +func Parse(data []byte) (*Locations, error) { + var locs []Location + + for _, line := range strings.Split(string(data), "\n") { + line = strings.TrimSpace(line) + if len(line) == 0 { + continue + } + + var x, y, z float64 + var name string + _, err := fmt.Sscanf(line, "%f %f %f %s", &x, &y, &z, &name) + if err != nil { + return nil, fmt.Errorf("error parsing line '%v'", err) + } + + locs = append(locs, Location{ + Coord: [3]float32{ + float32(x / 8.0), + float32(y / 8.0), + float32(z / 8.0), + }, + Name: trim(name), + }) + } + + return &Locations{locs}, nil +} + +func trim(input string) string { + m := map[string]string{ + "$loc_name_ga": "ga", + "$loc_name_mh": "mh", + "$loc_name_pent": "pent", + "$loc_name_quad": "quad", + "$loc_name_ra": "ra", + "$loc_name_ring": "ring", + "$loc_name_separator": "-", + "$loc_name_ya": "ya", + } + + for x, y := range m { + input = strings.Replace(input, x, y, -1) + } + + return input +} + +func (l *Locations) Get(coord [3]float32) *Location { + var loc *Location + var min float32 + + for _, n := range l.locations { + x := coord[0] - n.Coord[0] + y := coord[1] - n.Coord[1] + z := coord[2] - n.Coord[2] + d := x*x + y*y + z*z + + if loc == nil || d < min { + loc = &n + min = d + } + } + + return loc +} diff --git a/common/loc/loc_test.go b/common/loc/loc_test.go new file mode 100644 index 0000000..5b68cc8 --- /dev/null +++ b/common/loc/loc_test.go @@ -0,0 +1,24 @@ +package loc + +import ( + "testing" +) + +func TestLoc(t *testing.T) { + dm3Loc := []byte(`-7040 -1856 -128 sng-mega +1536 -1664 -1408 ra-tunnel +11776 -7424 -192 ya +12160 3456 -704 rl +-5056 -5440 -128 sng-ra +4096 6144 1728 lifts`) + + loc, err := Parse(dm3Loc) + if err != nil { + t.Errorf("error when parsing loc data, %v", err) + } + + l := loc.Get([3]float32{1300, -700, -24}) + if l.Name != "ya" { + t.Errorf("expected to be at ya, got %v", l.Name) + } +} |
