Skip to content

Commit

Permalink
wip: fix examples with map radars for s2
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-wa committed Sep 4, 2024
1 parent 67ecfa4 commit b92f2cc
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 46 deletions.
Binary file added assets/de_inferno.webp
Binary file not shown.
Binary file added assets/de_nuke.webp
Binary file not shown.
Binary file added assets/de_vertigo.webp
Binary file not shown.
16 changes: 8 additions & 8 deletions examples/heatmap/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
ex "github.com/markus-wa/demoinfocs-golang/v4/examples"
demoinfocs "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
events "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/events"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/msg"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/msgs2"
)

const (
Expand All @@ -36,27 +36,27 @@ func main() {
p := demoinfocs.NewParser(f)
defer p.Close()

// Parse header (contains map-name etc.)
header, err := p.ParseHeader()
checkError(err)

var (
mapMetadata ex.Map
mapRadarImg image.Image
)

p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_ServerInfo) {
p.RegisterNetMessageHandler(func(msg *msgs2.CSVCMsg_ServerInfo) {
// Get metadata for the map that the game was played on for coordinate translations
mapMetadata = ex.GetMapMetadata(header.MapName, msg.GetMapCrc())
mapMetadata = ex.GetMapMetadata(msg.GetMapName(), 0)

// Load map overview image
mapRadarImg = ex.GetMapRadar(header.MapName, msg.GetMapCrc())
mapRadarImg = ex.GetMapRadar(msg.GetMapName(), 0)
})

// Register handler for WeaponFire, triggered every time a shot is fired
var points []r2.Point

p.RegisterEventHandler(func(e events.WeaponFire) {
if e.Shooter == nil {
return
}

// Translate positions from in-game coordinates to radar overview image pixels
x, y := mapMetadata.TranslateScale(e.Shooter.Position().X, e.Shooter.Position().Y)

Expand Down
49 changes: 23 additions & 26 deletions examples/map_metadata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package examples

import (
"encoding/json"
"fmt"
"image"
"net/http"
"os"

"github.com/chai2010/webp"
)

// Map represents a CS:GO map. It contains information required to translate
Expand All @@ -31,38 +31,35 @@ func (m Map) TranslateScale(x, y float64) (float64, float64) {
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
// Panics if any error occurs.
func GetMapMetadata(name string, crc uint32) Map {
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/info.json", name, crc)

resp, err := http.Get(url)
checkError(err)

defer resp.Body.Close()

var data map[string]Map

err = json.NewDecoder(resp.Body).Decode(&data)
checkError(err)

mapInfo, ok := data[name]
if !ok {
panic(fmt.Sprintf("failed to get map info.json entry for %q", name))
}

return mapInfo
return map[string]Map{
"ar_baggage": {
PosX: -1316,
PosY: 1288,
Scale: 2.539062,
},
"de_nuke": {
PosX: -3453,
PosY: 2887,
Scale: 7,
},
"de_vertigo": {
PosX: -3168,
PosY: 1762,
Scale: 4,
},
}[name]
}

// 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, crc uint32) image.Image {
url := fmt.Sprintf("https://radar-overviews.csgo.saiko.tech/%s/%d/radar.png", name, crc)

resp, err := http.Get(url)
f, err := os.Open("assets/" + name + ".webp")
checkError(err)

defer resp.Body.Close()
defer f.Close()

img, _, err := image.Decode(resp.Body)
img, err := webp.Decode(f)
checkError(err)

return img
Expand Down
26 changes: 14 additions & 12 deletions examples/nade-trajectories/nade_trajectories.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import (
"os"

"github.com/golang/geo/r2"
"github.com/golang/geo/r3"
"github.com/llgcode/draw2d/draw2dimg"

ex "github.com/markus-wa/demoinfocs-golang/v4/examples"
demoinfocs "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
common "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/common"
events "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/events"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/msg"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/msgs2"
)

type nadePath struct {
wep common.EquipmentType
path []r3.Vector
path []common.TrajectoryEntry
team common.Team
}

Expand All @@ -48,19 +47,16 @@ func main() {
p := demoinfocs.NewParser(f)
defer p.Close()

header, err := p.ParseHeader()
checkError(err)

var (
mapRadarImg image.Image
)

p.RegisterNetMessageHandler(func(msg *msg.CSVCMsg_ServerInfo) {
p.RegisterNetMessageHandler(func(msg *msgs2.CSVCMsg_ServerInfo) {
// Get metadata for the map that the game was played on for coordinate translations
curMap = ex.GetMapMetadata(header.MapName, msg.GetMapCrc())
curMap = ex.GetMapMetadata(msg.GetMapName(), 0)

// Load map overview image
mapRadarImg = ex.GetMapRadar(header.MapName, msg.GetMapCrc())
mapRadarImg = ex.GetMapRadar(msg.GetMapName(), 0)
})

nadeTrajectories := make(map[int64]*nadePath) // Trajectories of all destroyed nades
Expand All @@ -81,7 +77,7 @@ func main() {
}
}

nadeTrajectories[id].path = e.Projectile.Trajectory
nadeTrajectories[id].path = e.Projectile.Trajectory2
})

var infernos []*common.Inferno
Expand Down Expand Up @@ -181,6 +177,12 @@ func drawTrajectories(gc *draw2dimg.GraphicContext, trajectories []*nadePath) {
gc.SetFillColor(color.RGBA{0, 0, 0, 0}) // No fill, alpha 0

for _, np := range trajectories {
if len(np.path) == 0 {
fmt.Fprintf(os.Stderr, "No path for nade trajectory of type %v\n", np.wep)

continue
}

// Set colors
switch np.wep {
case common.EqMolotov:
Expand All @@ -207,11 +209,11 @@ func drawTrajectories(gc *draw2dimg.GraphicContext, trajectories []*nadePath) {
}

// Draw path
x, y := curMap.TranslateScale(np.path[0].X, np.path[0].Y)
x, y := curMap.TranslateScale(np.path[0].Position.X, np.path[0].Position.Y)
gc.MoveTo(x, y) // Move to a position to start the new path

for _, pos := range np.path[1:] {
x, y := curMap.TranslateScale(pos.X, pos.Y)
x, y := curMap.TranslateScale(pos.Position.X, pos.Position.Y)
gc.LineTo(x, y)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
)

require (
github.com/chai2010/webp v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit b92f2cc

Please sign in to comment.