Skip to content

Commit ddbc21e

Browse files
Michael Bangmarkus-wa
authored andcommitted
metadata: add and use generic map translation and scaling. (#37)
1 parent b0fb87f commit ddbc21e

21 files changed

+156
-36
lines changed

examples/de_cache.jpg

-223 KB
Binary file not shown.

examples/nade-trajectories/nade_trajectories.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
common "github.com/markus-wa/demoinfocs-golang/common"
1919
events "github.com/markus-wa/demoinfocs-golang/events"
2020
ex "github.com/markus-wa/demoinfocs-golang/examples"
21+
"github.com/markus-wa/demoinfocs-golang/metadata"
2122
st "github.com/markus-wa/demoinfocs-golang/sendtables"
2223
)
2324

@@ -35,6 +36,7 @@ var (
3536
colorFlash color.Color = color.RGBA{0x00, 0x00, 0xff, 0xff} // Blue, because of the color on the nade
3637
colorSmoke color.Color = color.RGBA{0xbe, 0xbe, 0xbe, 0xff} // Light gray
3738
colorDecoy color.Color = color.RGBA{0x96, 0x4b, 0x00, 0xff} // Brown, because it's shit :)
39+
curMap metadata.Map
3840
)
3941

4042
type inferno []r3.Vector
@@ -55,9 +57,11 @@ func main() {
5557

5658
p := dem.NewParser(f)
5759

58-
_, err = p.ParseHeader()
60+
hd, err := p.ParseHeader()
5961
checkError(err)
6062

63+
curMap = metadata.MapNameToMap[hd.MapName]
64+
6165
nadeTrajectories := make(map[int64]*nadePath) // Trajectories of all destroyed nades
6266

6367
p.RegisterEventHandler(func(e events.NadeProjectileDestroyedEvent) {
@@ -122,18 +126,18 @@ func main() {
122126
err = p.ParseToEnd()
123127
checkError(err)
124128

125-
// Draw image
126-
127-
// Create output canvas
128-
dest := image.NewRGBA(image.Rect(0, 0, 1024, 1024))
129-
130129
// Use cache map overview as base
131-
fCache, err := os.Open("../de_cache.jpg")
130+
fMap, err := os.Open(fmt.Sprintf("../../metadata/maps/%s.jpg", hd.MapName))
132131
checkError(err)
133132

134-
imgCache, _, err := image.Decode(fCache)
133+
imgMap, _, err := image.Decode(fMap)
135134
checkError(err)
136-
draw.Draw(dest, dest.Bounds(), imgCache, image.Point{0, 0}, draw.Src)
135+
136+
// Create output canvas
137+
dest := image.NewRGBA(imgMap.Bounds())
138+
139+
// Draw image
140+
draw.Draw(dest, dest.Bounds(), imgMap, image.Point{0, 0}, draw.Src)
137141

138142
// Initialize the graphic context
139143
gc := draw2dimg.NewGraphicContext(dest)
@@ -171,11 +175,15 @@ func drawInfernos(gc *draw2dimg.GraphicContext, hulls []*s2.Loop) {
171175

172176
func buildInfernoPath(gc *draw2dimg.GraphicContext, hull *s2.Loop) {
173177
vertices := hull.Vertices()
174-
gc.MoveTo(translateX(vertices[0].X), translateY(vertices[0].Y))
178+
x, y, _ := curMap.TranslateScale(vertices[0].X, vertices[0].Y, 0)
179+
gc.MoveTo(x, y)
180+
175181
for _, fire := range vertices[1:] {
176-
gc.LineTo(translateX(fire.X), translateY(fire.Y))
182+
x, y, _ := curMap.TranslateScale(fire.X, fire.Y, 0)
183+
gc.LineTo(x, y)
177184
}
178-
gc.LineTo(translateX(vertices[0].X), translateY(vertices[0].Y))
185+
186+
gc.LineTo(x, y)
179187
}
180188

181189
func drawTrajectories(gc *draw2dimg.GraphicContext, trajectories []*nadePath) {
@@ -209,38 +217,18 @@ func drawTrajectories(gc *draw2dimg.GraphicContext, trajectories []*nadePath) {
209217
}
210218

211219
// Draw path
212-
gc.MoveTo(translateX(np.path[0].X), translateY(np.path[0].Y)) // Move to a position to start the new path
220+
x, y, _ := curMap.TranslateScale(np.path[0].X, np.path[0].Y, 0)
221+
gc.MoveTo(x, y) // Move to a position to start the new path
213222

214223
for _, pos := range np.path[1:] {
215-
gc.LineTo(translateX(pos.X), translateY(pos.Y))
224+
x, y, _ := curMap.TranslateScale(pos.X, pos.Y, 0)
225+
gc.LineTo(x, y)
216226
}
217227

218228
gc.FillStroke()
219229
}
220230
}
221231

222-
// Rough translations for x & y coordinates from de_cache to 1024x1024 px.
223-
// This could be done nicer by only having to provide the mapping between two source & target coordinates and the max size.
224-
// Then we could calculate the correct stretch & offset automatically.
225-
226-
func translateX(x float64) float64 {
227-
const (
228-
stretch = 0.18
229-
offset = 414
230-
)
231-
232-
return x*stretch + offset
233-
}
234-
235-
func translateY(y float64) float64 {
236-
const (
237-
stretch = -0.18
238-
offset = 630
239-
)
240-
241-
return y*stretch + offset
242-
}
243-
244232
func checkError(err error) {
245233
if err != nil {
246234
log.Fatal(err)

metadata/maps.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package metadata
2+
3+
// Map represents a CS:GO map. It contains information required to translate
4+
// in-game world coordinates to coordinates relative to (0, 0)
5+
type Map struct {
6+
Name string
7+
8+
PosX float64
9+
PosY float64
10+
11+
Scale float64
12+
Rotate float64
13+
Zoom float64
14+
}
15+
16+
// Translate translates in-game world-relative coordinates to (0, 0) relative coordinates
17+
func (m Map) Translate(x, y, z float64) (float64, float64, float64) {
18+
return x - m.PosX, m.PosY - y, z
19+
}
20+
21+
// TranslateScale translates and scales in-game world-relative coordinates to (0, 0) relative coordinates
22+
func (m Map) TranslateScale(x, y, z float64) (float64, float64, float64) {
23+
x, y, z = m.Translate(x, y, z)
24+
return x / m.Scale, y / m.Scale, z
25+
}
26+
27+
var (
28+
MapDeCache = Map{
29+
Name: "de_cache",
30+
PosX: -2000,
31+
PosY: 3250,
32+
Scale: 5.5,
33+
}
34+
35+
MapDeCanals = Map{
36+
Name: "de_canals",
37+
PosX: -2496,
38+
PosY: 1792,
39+
Scale: 4,
40+
}
41+
42+
MapDeCbble = Map{
43+
Name: "de_cbble",
44+
PosX: -3840,
45+
PosY: 3072,
46+
Scale: 6,
47+
}
48+
49+
MapDeDust2 = Map{
50+
Name: "de_dust2",
51+
PosX: -2400,
52+
PosY: 3383,
53+
Scale: 4.4,
54+
Rotate: 1,
55+
Zoom: 1.1,
56+
}
57+
58+
MapDeInferno = Map{
59+
Name: "de_inferno",
60+
PosX: -2087,
61+
PosY: 3870,
62+
Scale: 4.9,
63+
}
64+
65+
MapDeMirage = Map{
66+
Name: "de_mirage",
67+
PosX: -3230,
68+
PosY: 1713,
69+
Scale: 5,
70+
Rotate: 0,
71+
Zoom: 0,
72+
}
73+
74+
MapDeNuke = Map{
75+
Name: "de_nuke",
76+
PosX: -3453,
77+
PosY: 2887,
78+
Scale: 7,
79+
}
80+
81+
MapDeOverpass = Map{
82+
Name: "de_overpass",
83+
PosX: -4831,
84+
PosY: 1781,
85+
Scale: 5.2,
86+
Rotate: 0,
87+
Zoom: 0,
88+
}
89+
90+
MapDeTrain = Map{
91+
Name: "de_train",
92+
PosX: -2477,
93+
PosY: 2392,
94+
Scale: 4.7,
95+
}
96+
)
97+
98+
// MapNameToMap translates a map name to a Map.
99+
var MapNameToMap = map[string]Map{
100+
"de_cache": MapDeCache,
101+
"de_canals": MapDeCanals,
102+
"de_cbble": MapDeCbble,
103+
"de_dust2": MapDeDust2,
104+
"de_inferno": MapDeInferno,
105+
"de_mirage": MapDeMirage,
106+
"de_nuke": MapDeNuke,
107+
"de_overpass": MapDeOverpass,
108+
"de_train": MapDeTrain,
109+
}

metadata/maps/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Map files courtesy of @zoidbergwill/csgo-overviews, granted under the MIT license:
2+
3+
> The MIT License (MIT)
4+
>
5+
> Copyright (c) 2015 William Martin Stewart
6+
>
7+
> Permission is hereby granted, free of charge, to any person obtaining a copy
8+
> of this software and associated documentation files (the "Software"), to deal
9+
> in the Software without restriction, including without limitation the rights
10+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
> copies of the Software, and to permit persons to whom the Software is
12+
> furnished to do so, subject to the following conditions:
13+
>
14+
> The above copyright notice and this permission notice shall be included in all
15+
> copies or substantial portions of the Software.
16+
>
17+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
> SOFTWARE.

metadata/maps/de_cache.jpg

83.6 KB
Loading

metadata/maps/de_canals.jpg

79.6 KB
Loading

metadata/maps/de_cbble.jpg

62.3 KB
Loading

metadata/maps/de_dust.jpg

61.8 KB
Loading

metadata/maps/de_dust2.jpg

82.6 KB
Loading

metadata/maps/de_inferno.jpg

93.2 KB
Loading

0 commit comments

Comments
 (0)