horsegen: spam vectors to try to limit type check time

This commit is contained in:
2026-02-16 14:06:51 -05:00
parent 2184515938
commit b0e422ac01
14 changed files with 157 additions and 6644 deletions

View File

@@ -54,7 +54,7 @@ func PairAffinity(a, b CharacterID) int {
if _, ok := Characters[b]; !ok {
return 0
}
return int(pairAffinity[a*{{ $.Count }} + b])
return int(pairAffinity[a*{{ $.CharaCount }} + b])
}
func TrioAffinity(a, b, c CharacterID) int {
@@ -67,6 +67,6 @@ func TrioAffinity(a, b, c CharacterID) int {
if _, ok := Characters[c]; !ok {
return 0
}
return int(trioAffinity[a*{{ $.Count }}*{{ $.Count }} + b*{{ $.Count }} + c])
return int(trioAffinity[a*{{ $.CharaCount }}*{{ $.CharaCount }} + b*{{ $.CharaCount }} + c])
}
{{ end }}

View File

@@ -3,68 +3,55 @@ module horse/{{ $.Region }}/character
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import std/data/rb-map
import horse/game-id
pub import horse/character
// Enumeration of all characters for type-safe programming.
pub type character
{{- range $uma := $.Characters }}
{{ kkenum $uma.Name }}
{{- end }}
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $chara := $.Characters }}{{ $chara.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.CharaCount }}, kk_context())"
js inline "[{{ range $chara := $.Characters }}{{ $chara.ID }},{{ end }}]"
// Vector of all character ID values in order for easy iterating.
pub val all = once(create-id-table)
// Get the character ID for a character.
pub fun character-id(c: character): character-id
match c
{{- range $uma := $.Characters }}
{{ kkenum $uma.Name }} -> Character-id({{ $uma.ID }})
{{- end }}
// List of all characters in ID order for easy iterating.
pub val all = [
{{- range $uma := $.Characters }}
{{ kkenum $uma.Name }},
{{- end }}
]
val name2id: rbmap<string, character-id> = rb-map/empty()
{{- range $uma := $.Characters }}
.set({{ printf "%q" $uma.Name }}, Character-id({{ $uma.ID}}))
{{- end }}
val name2id = once()
var m: rbmap<string, int> := empty()
all().foreach() fn(id) m := m.set(Character-id(id).show, id)
m
// Get the character ID that has the given exact name.
// If no character matches the name, the result is an invalid ID.
pub fun from-name(name: string): character-id
name2id.lookup(name).default(Character-id(0))
Character-id(name2id().rb-map/lookup(name).default(0))
// Get the name for a character.
// If no character matches the ID, the result is the numeric ID.
pub fun show(c: character-id): string
match c.game-id
{{- range $uma := $.Characters }}
{{ $uma.ID }} -> {{ printf "%q" $uma.Name }}
{{- range $chara := $.Characters }}
{{ $chara.ID }} -> {{ printf "%q" $chara.Name }}
{{- end }}
x -> "character " ++ x.show
fun character/index(c: character-id): int
match c.game-id
{{- range $uma := $.Characters }}
{{ $uma.ID }} -> {{ $uma.Index }}
{{- range $chara := $.Characters }}
{{ $chara.ID }} -> {{ $chara.Index }}
{{- end }}
_ -> -99999999
// Create the table of all pair affinities.
// The affinity is the value at a.index*count + b.index.
extern global/create-pair-table(): vector<int>
c inline "kk_intx_t arr[] = {
c inline "int32_t arr[] = {
{{- range $a := $.Characters }}
{{- range $b := $.Characters }}
{{- index $.PairMaps $a.ID $b.ID }},
{{- end }}
{{- end -}}
};\nkk_vector_from_cintarray(arr, (kk_ssize_t){{ $.Count }} * (kk_ssize_t){{ $.Count }}, kk_context())"
};\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.CharaCount }} * (kk_ssize_t){{ $.CharaCount }}, kk_context())"
js inline "[
{{- range $a := $.Characters }}
{{- range $b := $.Characters }}
@@ -76,12 +63,12 @@ val global/pair-table = global/create-pair-table()
// Base affinity between a pair using the global ruleset.
pub fun global/pair-affinity(a: character-id, b: character-id): int
global/pair-table.at(a.index * {{ $.Count }} + b.index).default(0)
global/pair-table.at(a.index * {{ $.CharaCount }} + b.index).default(0)
// Create the table of all trio affinities.
// The affinity is the value at a.index*count*count + b.index*count + c.index.
extern global/create-trio-table(): vector<int>
c inline "kk_intx_t arr[] = {
c inline "int32_t arr[] = {
{{- range $a := $.Characters }}
{{- range $b := $.Characters }}
{{- range $c := $.Characters }}
@@ -89,7 +76,7 @@ extern global/create-trio-table(): vector<int>
{{- end }}
{{- end }}
{{- end -}}
};\nkk_vector_from_cintarray(arr, (kk_ssize_t){{ $.Count }} * (kk_ssize_t){{ $.Count }} * (kk_ssize_t){{ $.Count }}, kk_context())"
};\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.CharaCount }} * (kk_ssize_t){{ $.CharaCount }} * (kk_ssize_t){{ $.CharaCount }}, kk_context())"
js inline "[
{{- range $a := $.Characters }}
{{- range $b := $.Characters }}
@@ -103,6 +90,6 @@ val global/trio-table = global/create-trio-table()
// Base affinity for a trio using the global ruleset.
pub fun global/trio-affinity(a: character-id, b: character-id, c: character-id): int
global/trio-table.at(a.index * {{ $.Count }} * {{ $.Count }} + b.index * {{ $.Count }} + c.index).default(0)
global/trio-table.at(a.index * {{ $.CharaCount }} * {{ $.CharaCount }} + b.index * {{ $.CharaCount }} + c.index).default(0)
{{- end }}

View File

@@ -59,7 +59,7 @@ func ExecCharacter(t *template.Template, region string, kk, g io.Writer, c []Nam
Trios []AffinityRelation
PairMaps map[int]map[int]int
TrioMaps map[int]map[int]map[int]int
Count int
CharaCount int
MaxID int
}{region, c, pairs, trios, pm, tm, len(c), maxid}
var err error
@@ -78,11 +78,12 @@ func ExecSkill(t *template.Template, region string, kk, g io.Writer, groups []Na
m[t.GroupID] = append(m[t.GroupID], t)
}
data := struct {
Region string
Groups []NamedID[SkillGroup]
Skills []Skill
Related map[int][]Skill
}{region, groups, skills, m}
Region string
Groups []NamedID[SkillGroup]
Skills []Skill
Related map[int][]Skill
SkillCount int
}{region, groups, skills, m, len(skills)}
var err error
if kk != nil {
err = errors.Join(err, t.ExecuteTemplate(kk, "koka-skill", &data))
@@ -95,9 +96,10 @@ func ExecSkill(t *template.Template, region string, kk, g io.Writer, groups []Na
func ExecRace(t *template.Template, region string, kk, g io.Writer, races []Race) error {
data := struct {
Region string
Races []Race
}{region, races}
Region string
Races []Race
RaceCount int
}{region, races, len(races)}
var err error
if kk != nil {
err = errors.Join(err, t.ExecuteTemplate(kk, "koka-race", &data))
@@ -110,9 +112,10 @@ func ExecRace(t *template.Template, region string, kk, g io.Writer, races []Race
func ExecSaddle(t *template.Template, region string, kk, g io.Writer, saddles []Saddle) error {
data := struct {
Region string
Saddles []Saddle
}{region, saddles}
Region string
Saddles []Saddle
SaddleCount int
}{region, saddles, len(saddles)}
var err error
if kk != nil {
err = errors.Join(err, t.ExecuteTemplate(kk, "koka-saddle", &data))
@@ -125,9 +128,10 @@ func ExecSaddle(t *template.Template, region string, kk, g io.Writer, saddles []
func ExecScenario(t *template.Template, region string, kk, g io.Writer, scen []Scenario) error {
data := struct {
Region string
Scenarios []Scenario
}{region, scen}
Region string
Scenarios []Scenario
ScenarioCount int
}{region, scen, len(scen)}
var err error
if kk != nil {
err = errors.Join(err, t.ExecuteTemplate(kk, "koka-scenario", &data))
@@ -143,7 +147,8 @@ func ExecSparks(t *template.Template, region string, kk, g io.Writer, sparks []S
Region string
Sparks []Spark
SparkEffects map[int]map[int][]SparkEffect
}{region, sparks, effects}
SparkCount int
}{region, sparks, effects, len(sparks)}
var err error
if kk != nil {
err = errors.Join(err, t.ExecuteTemplate(kk, "koka-spark", &data))

View File

@@ -3,40 +3,29 @@ module horse/{{ $.Region }}/race
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import std/data/rb-map
import horse/game-id
pub import horse/race
// Enumeration of all races for type-safe programming.
pub type race
{{- range $r := $.Races }}
{{ kkenum $r.Name }}{{ if $r.Alternate }}-Alt{{ $r.ID }}{{ end }}
{{- end }}
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $r := $.Races }}{{ $r.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.RaceCount }}, kk_context())"
js inline "[{{ range $r := $.Races }}{{ $r.ID }},{{ end }}]"
// Vector of all race IDs in order for easy iterating.
val all = once(create-id-table)
// Get the race ID for a race.
pub fun race-id(r: race): race-id
match r
{{- range $r := $.Races }}
{{ kkenum $r.Name }}{{ if $r.Alternate }}-Alt{{ $r.ID }}{{ end }} -> Race-id({{ $r.ID }})
{{- end }}
// List of all races in ID order for easy iterating.
pub val all = [
{{- range $r := $.Races }}
{{ kkenum $r.Name }}{{ if $r.Alternate }}-Alt{{ $r.ID }}{{ end }},
{{- end }}
]
val name2id: rbmap<string, race-id> = rb-map/empty()
{{- range $r := $.Races }}
.set({{ printf "%q" $r.Name }}{{ if $r.Alternate }} ++ " (Alternate {{ $r.ID }})"{{ end }}, Race-id({{ $r.ID }}))
{{- end }}
val name2id = once()
var m: rbmap<string, int> := empty()
all().foreach() fn(id) m := m.set(Race-id(id).show, id)
m
// Get the race ID that has the given exact name.
// Alternate versions of races have an indication of their ID in their names.
// If no race matches the name, the result is an invalid ID.
pub fun from-name(name: string): race-id
name2id.lookup(name).default(Race-id(0))
Race-id(name2id().rb-map/lookup(name).default(0))
// Get the name for a race.
// Alternate versions of races have an indication of their ID in their names.

View File

@@ -3,29 +3,18 @@ module horse/{{ $.Region }}/saddle
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import horse/game-id
pub import horse/race
pub import horse/{{ $.Region }}/race
// Enumeration of all saddles for type-safe programming.
pub type saddle
{{- range $s := $.Saddles }}
{{ kkenum $s.Name }}{{ if $s.Alternate }}-Alt{{ $s.ID }}{{ end }}
{{- end }}
// Get the saddle ID for a saddle.
pub fun saddle-id(s: saddle): saddle-id
match s
{{- range $s := $.Saddles }}
{{ kkenum $s.Name }}{{ if $s.Alternate }}-Alt{{ $s.ID }}{{ end }} -> Saddle-id({{ $s.ID }})
{{- end }}
// List of all saddles in ID order for easy iterating.
pub val all = [
{{- range $s := $.Saddles }}
{{ kkenum $s.Name }}{{ if $s.Alternate }}-Alt{{ $s.ID }}{{ end }},
{{- end }}
]
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $s := $.Saddles }}{{ $s.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.SaddleCount }}, kk_context())"
js inline "[{{ range $s := $.Saddles }}{{ $s.ID }},{{ end }}]"
// Vector of all saddle IDs in order for easy iterating.
val all = once(create-id-table)
// Get the name for a saddle.
// Alternate versions of saddles have an indication of their ID in their names.

View File

@@ -3,27 +3,16 @@ module horse/{{ $.Region }}/scenario
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import horse/game-id
// Enumeration of all scenarios for type-safe programming.
pub type scenario
{{- range $s := $.Scenarios }}
{{ kkenum $s.Name }}
{{- end }}
// Get the scenario ID for a scenario.
pub fun scenario-id(s: scenario): scenario-id
match s
{{- range $s := $.Scenarios }}
{{ kkenum $s.Name }} -> Scenario-id({{ $s.ID }})
{{- end }}
// List of all scenarios in ID order for easy iterating.
pub val all = [
{{- range $s := $.Scenarios }}
{{ kkenum $s.Name }},
{{- end }}
]
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $s := $.Scenarios }}{{ $s.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.ScenarioCount }}, kk_context())"
js inline "[{{ range $s := $.Scenarios }}{{ $s.ID }},{{ end }}]"
// Vector of all scenario IDs in order for easy iterating.
val all = once(create-id-table)
// Get the name for a scenario.
// If no scenario matches the ID, the result contains the numeric ID.

View File

@@ -3,42 +3,31 @@ module horse/{{ $.Region }}/skill
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import std/data/rb-map
import std/num/decimal
import horse/game-id
import horse/movement
pub import horse/skill
// Enumeration of all skills for type-safe programming.
pub type skill
{{- range $s := $.Skills }}
{{ kkenum $s.Name }}{{ if $s.InheritID }}-Inherit{{ end }}
{{- end }}
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $s := $.Skills }}{{ $s.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.SkillCount }}, kk_context())"
js inline "[{{ range $s := $.Skills }}{{ $s.ID }},{{ end }}]"
// Vector of all skill ID values in order for easy iterating.
val all = once(create-id-table)
// Get the skill ID for a skill.
pub fun skill-id(s: skill): skill-id
match s
{{- range $s := $.Skills }}
{{ kkenum $s.Name }}{{ if $s.InheritID }}-Inherit{{ end }} -> Skill-id({{ $s.ID }})
{{- end }}
// List of all skills in ID order for easy iterating.
pub val all = [
{{- range $s := $.Skills }}
{{ kkenum $s.Name }}{{ if $s.InheritID }}-Inherit{{ end }},
{{- end }}
]
val name2id: rbmap<string, skill-id> = rb-map/empty()
{{- range $s := $.Skills }}
.set({{ printf "%q" $s.Name }}{{ if $s.InheritID }} ++ " (Inherited)"{{ end }}, Skill-id({{ $s.ID }}))
{{- end }}
val name2id = once()
var m: rbmap<string, int> := empty()
all().foreach() fn(id) m := m.set(Skill-id(id).show, id)
m
// Get the skill ID that has the given exact name.
// Inherited skills have `" (Inherited)"` appended to their names.
// If no skill matches the name, the result is an invalid ID.
pub fun from-name(name: string): skill-id
name2id.lookup(name).default(Skill-id(0))
Skill-id(name2id().rb-map/lookup(name).default(0))
// Get the name for a skill.
// Inherited skills have `" (Inherited)"` appended to their names.

View File

@@ -3,28 +3,17 @@ module horse/{{ $.Region }}/spark
// Automatically generated with horsegen; DO NOT EDIT
import std/core/delayed
import std/core/vector
import std/core-extras
import horse/game-id
pub import horse/spark
// Enumeration of all sparks for type-safe programming.
pub type spark
{{- range $s := $.Sparks }}
{{ kkenum $s.Name }}-Lv{{ $s.Rarity }}
{{- end }}
// Get the ID for a spark.
pub fun spark-id(s: spark): spark-id
match s
{{- range $s := $.Sparks }}
{{ kkenum $s.Name }}-Lv{{ $s.Rarity }} -> Spark-id({{ $s.ID }})
{{- end }}
// List of all sparks in ID order for easy iterating.
pub val all = [
{{- range $s := $.Sparks }}
{{ kkenum $s.Name }}-Lv{{ $s.Rarity }},
{{- end }}
]
extern create-id-table(): vector<int>
c inline "int32_t arr[] = { {{- range $s := $.Sparks }}{{ $s.ID }},{{ end -}} };\nkk_vector_from_cint32array(arr, (kk_ssize_t){{ $.SparkCount }}, kk_context())"
js inline "[{{ range $s := $.Sparks }}{{ $s.ID }},{{ end }}]"
// Vector of all spark IDs in order for easy iterating.
val all = once(create-id-table)
// Get the name for a spark.
// The name does not indicate the spark level.