horsegen: also generate go
This commit is contained in:
145
horsegen/character.go.template
Normal file
145
horsegen/character.go.template
Normal file
@@ -0,0 +1,145 @@
|
||||
{{ define "go-character" -}}
|
||||
package horse
|
||||
|
||||
// Automatically generated with horsegen; DO NOT EDIT
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
ID int16
|
||||
Name string
|
||||
}
|
||||
|
||||
var characterIDs = []int16{
|
||||
{{- range $c := $.Characters }}
|
||||
{{ $c.ID }}, // {{ $c.Name }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
var characterNames = []string{
|
||||
{{- range $c := $.Characters }}
|
||||
{{ printf "%q" $c.Name }},
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
var characterNameToID = map[string]int16{
|
||||
{{- range $c := $.Characters }}
|
||||
{{ printf "%q" $c.Name }}: {{ $c.ID }},
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func characterIndex(id int16) (int, bool) {
|
||||
return slices.BinarySearch(characterIDs, id)
|
||||
}
|
||||
|
||||
func CharacterForID(id int16) Character {
|
||||
i, ok := characterIndex(id)
|
||||
if !ok {
|
||||
return Character{}
|
||||
}
|
||||
return Character{
|
||||
ID: id,
|
||||
Name: characterNames[i],
|
||||
}
|
||||
}
|
||||
|
||||
func CharacterForName(name string) Character {
|
||||
id, ok := characterNameToID[name]
|
||||
if !ok {
|
||||
return Character{}
|
||||
}
|
||||
return Character{
|
||||
ID: id,
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Character) MarshalJSON() ([]byte, error) {
|
||||
// Only marshal legal or empty characters.
|
||||
if c.ID == 0 {
|
||||
return []byte{'0'}, nil
|
||||
}
|
||||
i, ok := characterIndex(c.ID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("marshaling character %q with invalid ID %d", c.Name, c.ID)
|
||||
}
|
||||
if characterNames[i] != c.Name {
|
||||
return nil, fmt.Errorf("marshaling character with ID %d: name is %q but should be %q", c.ID, c.Name, characterNames[i])
|
||||
}
|
||||
return strconv.AppendInt(nil, int64(c.ID), 10), nil
|
||||
}
|
||||
|
||||
func (c *Character) UnmarshalJSON(b []byte) error {
|
||||
if string(b) == "null" {
|
||||
return nil
|
||||
}
|
||||
id, err := strconv.ParseInt(string(b), 10, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshaling invalid character ID %q: %w", b, err)
|
||||
}
|
||||
if id == 0 {
|
||||
*c = Character{}
|
||||
return nil
|
||||
}
|
||||
i, ok := characterIndex(int16(id))
|
||||
if !ok {
|
||||
return fmt.Errorf("unmarshaling unrecognized character ID %d", id)
|
||||
}
|
||||
*c = Character{
|
||||
ID: int16(id),
|
||||
Name: characterNames[i],
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var pairAffinity = []int8{
|
||||
{{- range $a := $.Characters -}}
|
||||
{{- range $b := $.Characters -}}
|
||||
{{- index $.PairMaps $a.ID $b.ID -}},
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
}
|
||||
|
||||
var trioAffinity = []int8{
|
||||
{{- range $a := $.Characters -}}
|
||||
{{- range $b := $.Characters -}}
|
||||
{{- range $c := $.Characters -}}
|
||||
{{- index $.TrioMaps $a.ID $b.ID $c.ID -}},
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
}
|
||||
|
||||
func PairAffinity(a, b Character) int {
|
||||
i, ok := characterIndex(a.ID)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
j, ok := characterIndex(b.ID)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return int(pairAffinity[i*{{ $.Count }} + j])
|
||||
}
|
||||
|
||||
func TrioAffinity(a, b, c Character) int {
|
||||
i, ok := characterIndex(a.ID)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
j, ok := characterIndex(b.ID)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
k, ok := characterIndex(c.ID)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return int(trioAffinity[i*{{ $.Count }}*{{ $.Count }} + j*{{ $.Count }} + k])
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user