horsegen: generate skill groups

This commit is contained in:
2026-01-09 14:24:31 -05:00
parent 024742c053
commit 7d8c6ae82e
6 changed files with 1207 additions and 17 deletions

1025
horse/skill.kk Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,15 +1,17 @@
package main
import (
_ "embed"
"embed"
"fmt"
"io"
"regexp"
"strings"
"text/template"
"unicode"
)
//go:embed character.kk.template
var characterKK string
//go:embed character.kk.template skill.kk.template
var templates embed.FS
// LoadTemplates sets up templates to render game data to source code.
func LoadTemplates() (*template.Template, error) {
@@ -17,15 +19,11 @@ func LoadTemplates() (*template.Template, error) {
t.Funcs(template.FuncMap{
"kkenum": kkenum,
})
t, err := t.Parse(characterKK)
if err != nil {
return nil, fmt.Errorf("parsing characterKK: %w", err)
}
return t, nil
return t.ParseFS(templates, "*")
}
// ExecCharacterKK renders the Koka character module to w.
func ExecCharacterKK(t *template.Template, w io.Writer, c []Character, pairs, trios []AffinityRelation) error {
func ExecCharacterKK(t *template.Template, w io.Writer, c []NamedID[Character], pairs, trios []AffinityRelation) error {
if len(pairs) != len(c)*len(c) {
return fmt.Errorf("there are %d pairs but there must be %d for %d characters", len(pairs), len(c)*len(c), len(c))
}
@@ -52,7 +50,7 @@ func ExecCharacterKK(t *template.Template, w io.Writer, c []Character, pairs, tr
}
data := struct {
Characters []Character
Characters []NamedID[Character]
Pairs []AffinityRelation
Trios []AffinityRelation
PairMaps map[int]map[int]int
@@ -63,8 +61,56 @@ func ExecCharacterKK(t *template.Template, w io.Writer, c []Character, pairs, tr
return t.ExecuteTemplate(w, "koka-character", &data)
}
func ExecSkillKK(t *template.Template, w io.Writer, g []NamedID[SkillGroup]) error {
data := struct {
Groups []NamedID[SkillGroup]
}{g}
return t.ExecuteTemplate(w, "koka-skill", &data)
}
const replaceDash = " ,!?/+();#○◎☆♡'&=♪∀゚∴"
var (
kkReplace = func() *strings.Replacer {
r := []string{
"Triple 7s", "Triple-Sevens", // hard to replace with the right thing automatically
"1,500,000 CC", "Million-CC",
"1st", "First",
".", "",
"'s", "s",
"ó", "o",
"∞", "Infinity",
}
for _, c := range replaceDash {
r = append(r, string(c), "-")
}
return strings.NewReplacer(r...)
}()
kkMultidash = regexp.MustCompile(`-+`)
kkDashNonletter = regexp.MustCompile(`-[^A-Za-z]`)
)
func kkenum(name string) string {
name = strings.ReplaceAll(name, ".", "")
name = strings.ReplaceAll(name, " ", "-")
orig := name
name = kkReplace.Replace(name)
name = kkMultidash.ReplaceAllLiteralString(name, "-")
name = strings.Trim(name, "-")
if len(name) == 0 {
panic(fmt.Errorf("%q became empty as Koka enum variant", orig))
}
name = strings.ToUpper(name[:1]) + name[1:]
if !unicode.IsLetter(rune(name[0])) {
panic(fmt.Errorf("Koka enum variant %q (from %q) starts with a non-letter", name, orig))
}
for _, c := range name {
if c > 127 {
// Koka does not allow non-ASCII characters in source code.
// Don't proceed if we've missed one.
panic(fmt.Errorf("non-ASCII character %q (%[1]U) in Koka enum variant %q (from %q)", c, name, orig))
}
}
if kkDashNonletter.MatchString(name) {
panic(fmt.Errorf("non-letter character after a dash in Koka enum variant %q (from %q)", name, orig))
}
return name
}

View File

@@ -17,15 +17,26 @@ var characterAffinity2SQL string
//go:embed character.affinity3.sql
var characterAffinity3SQL string
type Character struct {
//go:embed skill-group.sql
var skillGroupSQL string
type (
Character struct{}
SkillGroup struct{}
)
type NamedID[T any] struct {
// Disallow conversions between NamedID types.
_ [0]*T
ID int
Name string
// For internal use, the index of the character.
// For internal use, the index of the identity, when it's needed.
// We don't show this in public API, but it lets us use vectors for lookups.
Index int
}
func Characters(ctx context.Context, db *sqlitex.Pool) ([]Character, error) {
func Characters(ctx context.Context, db *sqlitex.Pool) ([]NamedID[Character], error) {
conn, err := db.Take(ctx)
defer db.Put(conn)
if err != nil {
@@ -37,7 +48,7 @@ func Characters(ctx context.Context, db *sqlitex.Pool) ([]Character, error) {
}
defer stmt.Finalize()
var r []Character
var r []NamedID[Character]
for {
ok, err := stmt.Step()
if err != nil {
@@ -46,7 +57,7 @@ func Characters(ctx context.Context, db *sqlitex.Pool) ([]Character, error) {
if !ok {
break
}
c := Character{
c := NamedID[Character]{
ID: stmt.ColumnInt(0),
Name: stmt.ColumnText(1),
Index: stmt.ColumnInt(2),
@@ -133,3 +144,33 @@ func CharacterTrios(ctx context.Context, db *sqlitex.Pool) ([]AffinityRelation,
}
return r, nil
}
func SkillGroups(ctx context.Context, db *sqlitex.Pool) ([]NamedID[SkillGroup], error) {
conn, err := db.Take(ctx)
defer db.Put(conn)
if err != nil {
return nil, fmt.Errorf("couldn't get connection for skill groups: %w", err)
}
stmt, _, err := conn.PrepareTransient(skillGroupSQL)
if err != nil {
return nil, fmt.Errorf("couldn't prepare statement for skill groups: %w", err)
}
defer stmt.Finalize()
var r []NamedID[SkillGroup]
for {
ok, err := stmt.Step()
if err != nil {
return nil, fmt.Errorf("error stepping skill groups: %w", err)
}
if !ok {
break
}
g := NamedID[SkillGroup]{
ID: stmt.ColumnInt(0),
Name: stmt.ColumnText(1),
}
r = append(r, g)
}
return r, nil
}

View File

@@ -58,6 +58,12 @@ func main() {
slog.Error("getting trios", slog.Any("err", err))
os.Exit(1)
}
slog.Info("get skill groups")
sg, err := SkillGroups(ctx, db)
if err != nil {
slog.Error("getting skill groups", slog.Any("err", err))
os.Exit(1)
}
cf, err := os.Create(filepath.Join(out, "character.kk"))
if err != nil {
@@ -69,4 +75,14 @@ func main() {
slog.Error("writing character.kk", slog.Any("err", err))
// continue on
}
sf, err := os.Create(filepath.Join(out, "skill.kk"))
if err != nil {
slog.Error("creating skill.kk", slog.Any("err", err))
os.Exit(1)
}
slog.Info("write skills")
if err := ExecSkillKK(t, sf, sg); err != nil {
slog.Error("writing skill.kk", slog.Any("err", err))
// continue on
}
}

14
horsegen/skill-group.sql Normal file
View File

@@ -0,0 +1,14 @@
WITH skill_names AS (
SELECT
"index" AS "id",
"text" AS "name"
FROM text_data
WHERE category = 47
)
SELECT
group_id,
name
FROM skill_data d
JOIN skill_names n ON d.id = n.id
WHERE d.group_rate = 1
ORDER BY group_id

View File

@@ -0,0 +1,48 @@
{{ define "koka-skill" -}}
module horse/skill
// Automatically generated with the horsegen tool; DO NOT EDIT
// Skill groups.
// A skill group may contain white, circle, double-circle, gold, and purple skills
// for the same effect.
// Sparks that grant skills refer to a skill group.
pub type skill-group
{{- range $g := $.Groups }}
{{ kkenum $g.Name }}
{{- end }}
// Map a skill group to its ID.
pub fip fun skill-group/group-id(^sg: skill-group): int
match sg
{{- range $g := $.Groups }}
{{ kkenum $g.Name }} -> {{ $g.ID }}
{{- end }}
// Get the skill group for an ID.
pub fip(1) fun skill-group/from-id(^id: int): maybe<skill-group>
match id
{{- range $g := $.Groups }}
{{ $g.ID }} -> Just( {{- kkenum $g.Name -}} )
{{- end }}
_ -> Nothing
// Get names for skill groups.
// Skill group names are the name of the base skill in the group.
pub fun skill-group/show(sg: skill-group): string
match sg
{{- range $g := $.Groups }}
{{ kkenum $g.Name }} -> {{ printf "%q" $g.Name }}
{{- end }}
// Compare two skill groups by ID order.
pub fip fun skill-group/order2(a: skill-group, b: skill-group): order2<skill-group>
match cmp(a.group-id, b.group-id)
Lt -> Lt2(a, b)
Eq -> Eq2(a)
Gt -> Gt2(a, b)
pub fun skill-group/(==)(a: skill-group, b: skill-group): bool
a.group-id == b.group-id
{{- end }}