Add anatomical character customization at initial creation
This commit is contained in:
@@ -24,12 +24,41 @@ func ValidateAppearance(a *Appearance, families ...string) error {
|
||||
if a == nil {
|
||||
return nil
|
||||
} // Existing worlds retain their original models.
|
||||
if !slices.Contains(families, a.Family) || a.Version != 1 || a.Variant < 0 || a.Variant > 3 || a.Seed < 1 || a.Seed > 65535 || !finiteRange(a.Height, .75, 1.25) || !finiteRange(a.Width, .75, 1.25) || !finiteRange(a.Detail, 0, 1) || !appearanceColor.MatchString(a.Primary) || !appearanceColor.MatchString(a.Secondary) || !appearanceColor.MatchString(a.Accent) {
|
||||
if !slices.Contains(families, a.Family) || (a.Version != 1 && a.Version != 2) || a.Variant < 0 || a.Variant > 3 || a.Seed < 1 || a.Seed > 65535 || !finiteRange(a.Height, .75, 1.25) || !finiteRange(a.Width, .75, 1.25) || !finiteRange(a.Detail, 0, 1) || !appearanceColor.MatchString(a.Primary) || !appearanceColor.MatchString(a.Secondary) || !appearanceColor.MatchString(a.Accent) {
|
||||
return fmt.Errorf("invalid appearance: choose a supported form, bounded proportions, and six-digit colors")
|
||||
}
|
||||
if a.Version == 1 {
|
||||
if a.Body != nil {
|
||||
return fmt.Errorf("legacy appearance cannot include anatomy")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
b := a.Body
|
||||
if a.Family != "human" || b == nil || !finiteRange(a.Height, .9, 1.1) || a.Width != 1 || !appearanceColor.MatchString(b.EyeColor) || !slices.Contains([]string{"none", "thin", "bushy"}, b.Eyebrows) || !slices.Contains([]string{"neutral", "smile", "stern"}, b.MouthStyle) || !slices.Contains([]string{"male", "female"}, b.Chest) {
|
||||
return fmt.Errorf("invalid humanoid anatomy")
|
||||
}
|
||||
for _, v := range []float64{b.HeadLength, b.HeadWidth, b.HeadDepth, b.ChestWidth, b.HipsWidth, b.FootSize} {
|
||||
if !finiteRange(v, .85, 1.15) {
|
||||
return fmt.Errorf("body proportion outside supported range")
|
||||
}
|
||||
}
|
||||
for _, v := range []float64{b.EyeSpacing, b.ArmWidth, b.LegWidth} {
|
||||
if !finiteRange(v, .8, 1.2) {
|
||||
return fmt.Errorf("body width outside supported range")
|
||||
}
|
||||
}
|
||||
if !finiteRange(b.ArmLength, .9, 1.1) || !finiteRange(b.LegLength, .9, 1.1) || !finiteRange(b.NoseScale, .75, 1.25) || !finiteRange(b.EyeHeight, -.035, .035) || !finiteRange(b.NoseHeight, -.035, .035) || !finiteRange(b.MouthHeight, -.025, .025) {
|
||||
return fmt.Errorf("facial feature or limb outside supported range")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultCharacterAppearance() *Appearance {
|
||||
return &Appearance{Version: 2, Family: "human", Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#c99573", Secondary: "#392b28", Accent: "#456c70", Body: &p.HumanoidBody{
|
||||
HeadLength: 1, HeadWidth: 1, HeadDepth: 1, EyeSpacing: 1, EyeColor: "#547d75", Eyebrows: "thin", NoseScale: 1, MouthStyle: "neutral", Chest: "male", ChestWidth: 1, HipsWidth: 1, ArmLength: 1, ArmWidth: 1, LegLength: 1, LegWidth: 1, FootSize: 1,
|
||||
}}
|
||||
}
|
||||
|
||||
func (c Catalog) validateVisuals() error {
|
||||
if len(c.Visuals) > 1024 {
|
||||
return fmt.Errorf("visual library is full")
|
||||
|
||||
@@ -23,3 +23,29 @@ func TestAppearanceValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanoidAppearanceBounds(t *testing.T) {
|
||||
base := DefaultCharacterAppearance()
|
||||
if err := ValidateAppearance(base, "human"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, mutate := range []func(*Appearance){
|
||||
func(a *Appearance) { a.Body.HeadWidth = math.NaN() },
|
||||
func(a *Appearance) { a.Body.LegLength = 1.2 },
|
||||
func(a *Appearance) { a.Body.NoseHeight = .1 },
|
||||
func(a *Appearance) { a.Body.EyeColor = "red" },
|
||||
func(a *Appearance) { a.Body.Eyebrows = "unknown" },
|
||||
func(a *Appearance) { a.Body.Chest = "unknown" },
|
||||
func(a *Appearance) { a.Body.MouthStyle = "unknown" },
|
||||
func(a *Appearance) { a.Family = "creature" },
|
||||
func(a *Appearance) { a.Width = 1.1 },
|
||||
func(a *Appearance) { a.Body = nil },
|
||||
func(a *Appearance) { a.Version = 1 },
|
||||
} {
|
||||
a := DefaultCharacterAppearance()
|
||||
mutate(a)
|
||||
if ValidateAppearance(a, "human", "creature") == nil {
|
||||
t.Fatal("invalid anatomy accepted", a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,42 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppearanceRestartAndReplacement(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
a := p.Appearance{Version: 1, Family: "human", Variant: 1, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
c.View.Entity.Appearance = &a
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
life := restored.Characters[c.View.Entity.ID]
|
||||
if life.View.Entity.Appearance == nil || *life.View.Entity.Appearance != a {
|
||||
t.Fatal("appearance lost on restart")
|
||||
}
|
||||
restored.kill(life, "test replacement")
|
||||
replacement, err := restored.Join(c.Account, "Replacement")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if replacement.View.Entity.ID == life.View.Entity.ID || replacement.View.Entity.Appearance != nil {
|
||||
t.Fatal("replacement inherited prior life's appearance")
|
||||
for _, a := range []*p.Appearance{
|
||||
{Version: 1, Family: "human", Variant: 1, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"},
|
||||
content.DefaultCharacterAppearance(),
|
||||
} {
|
||||
w, c := fixture(t)
|
||||
c.View.Entity.Appearance = a
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
life := restored.Characters[c.View.Entity.ID]
|
||||
if !reflect.DeepEqual(life.View.Entity.Appearance, a) {
|
||||
t.Fatal("appearance lost on restart")
|
||||
}
|
||||
restored.kill(life, "test replacement")
|
||||
replacement, err := restored.Join(c.Account, "Replacement")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if replacement.View.Entity.ID == life.View.Entity.ID || replacement.View.Entity.Appearance != nil {
|
||||
t.Fatal("replacement inherited prior life's appearance")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"aeternalis/server/content"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -153,7 +153,7 @@ func TestCastingInterruptMissAndWeaponUnlock(t *testing.T) {
|
||||
|
||||
func TestCorpseRevivalKeepsLootAndDailyBudget(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
appearance := p.Appearance{Version: 1, Family: "human", Variant: 2, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
appearance := *content.DefaultCharacterAppearance()
|
||||
c.View.Entity.Appearance = &appearance
|
||||
rescuer, e := w.Join("rescuer", "Rescuer")
|
||||
if e != nil {
|
||||
|
||||
@@ -115,16 +115,39 @@ type MobDefinition struct {
|
||||
}
|
||||
|
||||
type Appearance struct {
|
||||
Accent string `json:"accent"`
|
||||
Detail float64 `json:"detail"`
|
||||
Family string `json:"family"`
|
||||
Height float64 `json:"height"`
|
||||
Primary string `json:"primary"`
|
||||
Secondary string `json:"secondary"`
|
||||
Seed int64 `json:"seed"`
|
||||
Variant int64 `json:"variant"`
|
||||
Version int64 `json:"version"`
|
||||
Width float64 `json:"width"`
|
||||
Accent string `json:"accent"`
|
||||
Body *HumanoidBody `json:"body,omitempty"`
|
||||
Detail float64 `json:"detail"`
|
||||
Family string `json:"family"`
|
||||
Height float64 `json:"height"`
|
||||
Primary string `json:"primary"`
|
||||
Secondary string `json:"secondary"`
|
||||
Seed int64 `json:"seed"`
|
||||
Variant int64 `json:"variant"`
|
||||
Version int64 `json:"version"`
|
||||
Width float64 `json:"width"`
|
||||
}
|
||||
|
||||
type HumanoidBody struct {
|
||||
ArmLength float64 `json:"armLength"`
|
||||
ArmWidth float64 `json:"armWidth"`
|
||||
Chest string `json:"chest"`
|
||||
ChestWidth float64 `json:"chestWidth"`
|
||||
EyeColor string `json:"eyeColor"`
|
||||
EyeHeight float64 `json:"eyeHeight"`
|
||||
EyeSpacing float64 `json:"eyeSpacing"`
|
||||
Eyebrows string `json:"eyebrows"`
|
||||
FootSize float64 `json:"footSize"`
|
||||
HeadDepth float64 `json:"headDepth"`
|
||||
HeadLength float64 `json:"headLength"`
|
||||
HeadWidth float64 `json:"headWidth"`
|
||||
HipsWidth float64 `json:"hipsWidth"`
|
||||
LegLength float64 `json:"legLength"`
|
||||
LegWidth float64 `json:"legWidth"`
|
||||
MouthHeight float64 `json:"mouthHeight"`
|
||||
MouthStyle string `json:"mouthStyle"`
|
||||
NoseHeight float64 `json:"noseHeight"`
|
||||
NoseScale float64 `json:"noseScale"`
|
||||
}
|
||||
|
||||
type MobSpawn struct {
|
||||
|
||||
@@ -64,7 +64,7 @@ func runTravelTests(t *testing.T, dsn string) {
|
||||
c.View.Experience = 7
|
||||
c.View.StatPoints = 20
|
||||
c.DailySeconds = 123
|
||||
c.View.Entity.Appearance = &content.Appearance{Version: 1, Family: "human", Variant: 2, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
c.View.Entity.Appearance = content.DefaultCharacterAppearance()
|
||||
c.WorldsSurvived = 2
|
||||
if e = source.AdminItems(c, "grant-item", "spear:1"); e != nil {
|
||||
t.Fatal(e)
|
||||
|
||||
@@ -1,54 +1,15 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
import "net/http"
|
||||
|
||||
// Looks are committed together with the name on initial join. Keep the old route
|
||||
// explicit so a stale client cannot silently offer a free appearance change.
|
||||
func (h *HTTP) appearanceRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("POST /api/appearance", func(w http.ResponseWriter, r *http.Request) {
|
||||
account, err := h.auth.account(r)
|
||||
if err != nil {
|
||||
if _, err := h.auth.account(r); err != nil {
|
||||
fail(w, 401, "sign in required")
|
||||
return
|
||||
}
|
||||
var appearance p.Appearance
|
||||
if decode(w, r, &appearance) != nil || content.ValidateAppearance(&appearance, "human") != nil {
|
||||
fail(w, 400, "invalid character appearance")
|
||||
return
|
||||
}
|
||||
h.s.mu.Lock()
|
||||
defer h.s.mu.Unlock()
|
||||
if !h.s.ready.Load() {
|
||||
fail(w, 503, "world unavailable")
|
||||
return
|
||||
}
|
||||
c := h.s.world.ForAccount(account)
|
||||
if c == nil || (c.View.Entity.State != "alive" && c.View.Entity.State != "eligible") {
|
||||
fail(w, 409, "no living character in this world")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
active, err := h.auth.db.ActiveCharacter(ctx, account)
|
||||
if err != nil {
|
||||
fail(w, 503, "character registry unavailable")
|
||||
return
|
||||
}
|
||||
if active == nil || active.WorldID != h.s.world.ID || active.ID != c.View.Entity.ID || active.State == "transit" {
|
||||
fail(w, 409, "character belongs to another world or is traveling")
|
||||
return
|
||||
}
|
||||
previous := c.View.Entity.Appearance
|
||||
c.View.Entity.Appearance = &appearance
|
||||
if err := h.s.persist(ctx, ""); err != nil {
|
||||
c.View.Entity.Appearance = previous
|
||||
fail(w, 503, "appearance could not be saved")
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]bool{"saved": true})
|
||||
fail(w, 403, "appearance can only be chosen when creating a character")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -28,12 +29,14 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc := NewService(world, lease)
|
||||
a := content.Appearance{Version: 1, Family: "human", Variant: 3, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
a := *content.DefaultCharacterAppearance()
|
||||
a.Body.HeadWidth = 1.1
|
||||
a.Body.Chest = "female"
|
||||
view, err := svc.join(ctx, "artist", "Artist", "", &a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if view.Entity.Appearance == nil || *view.Entity.Appearance != a {
|
||||
if !reflect.DeepEqual(view.Entity.Appearance, &a) {
|
||||
t.Fatal("new character appearance missing")
|
||||
}
|
||||
other := a
|
||||
@@ -41,7 +44,7 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
|
||||
if _, err = svc.join(ctx, "artist", "Artist", "", &other); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *world.ForAccount("artist").View.Entity.Appearance != a {
|
||||
if !reflect.DeepEqual(world.ForAccount("artist").View.Entity.Appearance, &a) {
|
||||
t.Fatal("join retry overwrote appearance")
|
||||
}
|
||||
auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "")
|
||||
@@ -72,15 +75,24 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
|
||||
}
|
||||
bad := other
|
||||
bad.Family = "creature"
|
||||
if request(bad, token, server.URL) != 400 {
|
||||
if request(bad, token, server.URL) != 403 {
|
||||
t.Fatal("player could choose creature")
|
||||
}
|
||||
before := world.ForAccount("artist").View.Stats["movementSpeed"]
|
||||
if request(other, token, server.URL) != 200 {
|
||||
t.Fatal("appearance save rejected")
|
||||
before := world.ForAccount("artist").View.Stats["moveSpeed"]
|
||||
if request(other, token, server.URL) != 403 {
|
||||
t.Fatal("existing character appearance could be changed")
|
||||
}
|
||||
record, err := db.ActiveCharacter(ctx, "artist")
|
||||
if err != nil || *record.Character.View.Entity.Appearance != other || record.Character.View.Stats["movementSpeed"] != before {
|
||||
if err != nil || !reflect.DeepEqual(record.Character.View.Entity.Appearance, &a) || record.Character.View.Stats["moveSpeed"] != before {
|
||||
t.Fatal("appearance not durable or changed stats", err)
|
||||
}
|
||||
invalid := *content.DefaultCharacterAppearance()
|
||||
invalid.Body.NoseScale = 200
|
||||
if _, err := svc.join(ctx, "invalid", "Invalid", "", &invalid); err == nil || world.ForAccount("invalid") != nil {
|
||||
t.Fatal("invalid initial appearance created a character")
|
||||
}
|
||||
plain, err := svc.join(ctx, "plain", "Plain", "")
|
||||
if err != nil || !reflect.DeepEqual(plain.Entity.Appearance, content.DefaultCharacterAppearance()) {
|
||||
t.Fatal("creation without customization did not get the default anatomy", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,8 +159,15 @@ func (s *Service) join(ctx context.Context, account, name, claim string, appeara
|
||||
return p.Character{}, err
|
||||
}
|
||||
copy := *appearance[0]
|
||||
if copy.Body != nil {
|
||||
body := *copy.Body
|
||||
copy.Body = &body
|
||||
}
|
||||
chosen = ©
|
||||
}
|
||||
if existing == nil && chosen == nil {
|
||||
chosen = content.DefaultCharacterAppearance()
|
||||
}
|
||||
if claim != "" {
|
||||
c, err = s.lease.DB.ReadPassage(ctx, claim, account)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user