-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmap_metadata.go
76 lines (58 loc) · 1.91 KB
/
map_metadata.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
package examples
import (
"embed"
"encoding/json"
"fmt"
"image"
"github.com/andygrunwald/vdf"
)
// Map represents a CS:GO map. It contains information required to translate
// in-game world coordinates to coordinates relative to (0, 0) on the provided map-overviews (radar images).
type Map struct {
PosX float64 `json:"pos_x,string"`
PosY float64 `json:"pos_y,string"`
Scale float64 `json:"scale,string"`
}
// Translate translates in-game world-relative coordinates to (0, 0) relative coordinates.
func (m Map) Translate(x, y float64) (float64, float64) {
return x - m.PosX, m.PosY - y
}
// TranslateScale translates and scales in-game world-relative coordinates to (0, 0) relative coordinates.
// The outputs are pixel coordinates for the radar images found in the maps folder.
func (m Map) TranslateScale(x, y float64) (float64, float64) {
x, y = m.Translate(x, y)
return x / m.Scale, y / m.Scale
}
//go:embed _assets/*
var fs embed.FS
// GetMapMetadata fetches metadata for a specific map version from
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
// Panics if any error occurs.
func GetMapMetadata(name string) Map {
f, err := fs.Open(fmt.Sprintf("_assets/metadata/%s.txt", name))
checkError(err)
defer f.Close()
m, err := vdf.NewParser(f).Parse()
checkError(err)
b, err := json.Marshal(m)
checkError(err)
var data map[string]Map
err = json.Unmarshal(b, &data)
checkError(err)
mapInfo, ok := data[name]
if !ok {
panic(fmt.Sprintf("failed to get map info.json entry for %q", name))
}
return mapInfo
}
// GetMapRadar fetches the radar image for a specific map version from
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/radar.png`.
// Panics if any error occurs.
func GetMapRadar(name string) image.Image {
f, err := fs.Open(fmt.Sprintf("_assets/radar/%s_radar_psd.png", name))
checkError(err)
defer f.Close()
img, _, err := image.Decode(f)
checkError(err)
return img
}