server/appearance.go (2129B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "fmt" 6 ) 7 8 func (character *character) baseScale() uint16 { 9 if character.height == 0 { 10 return 100 11 } 12 return max(uint16(20), character.height) 13 } 14 15 func (character *character) resolvedAppearance() appearanceFields { 16 // Unspecified player templates use the original Solitus male defaults. 17 appearance := unpackAppearance(character.appearance) 18 if appearance.Breed < 1 || appearance.Breed > 4 { 19 appearance.Breed = 1 20 } 21 if appearance.Breed == 4 { 22 appearance.Sex = 1 23 } else if appearance.Sex != 3 { 24 appearance.Sex = 2 25 } 26 if appearance.Race == 0 || appearance.Breed != 1 { 27 appearance.Race = 1 28 } 29 return appearance 30 } 31 32 func (character *character) appearanceAssets() (uint32, [5]uint32) { 33 appearance := character.resolvedAppearance() 34 35 // Explicit head identity supports all native choices, independently of face bits. 36 head := character.headMesh 37 if head == 0 { 38 for _, choice := range gamedata.PlayerAppearances.Heads { 39 if choice.Breed == appearance.Breed && choice.Sex == appearance.Sex && choice.Race == appearance.Race { 40 head = choice.Mesh 41 break 42 } 43 } 44 } 45 for _, body := range gamedata.PlayerAppearances.Bodies { 46 if body.Breed == appearance.Breed && body.Sex == appearance.Sex { 47 return head, body.Textures[appearance.Race] 48 } 49 } 50 panic("missing player body assets") 51 } 52 53 type appearanceFields struct { 54 Race uint16 55 Fatness uint16 56 Breed uint16 57 Sex uint16 58 Side uint16 59 Head uint16 60 } 61 62 func unpackAppearance(value uint16) appearanceFields { 63 return appearanceFields{value & 3, value >> 2 & 3, value >> 4 & 7, value >> 7 & 3, value >> 9 & 3, value >> 11 & 31} 64 } 65 66 func (appearance appearanceFields) packed() uint16 { 67 return appearance.Race | appearance.Fatness<<2 | appearance.Breed<<4 | appearance.Sex<<7 | appearance.Side<<9 | appearance.Head<<11 68 } 69 70 func (appearance appearanceFields) validate() error { 71 if appearance.Race > 3 || appearance.Fatness > 3 || appearance.Breed > 7 || appearance.Sex > 3 || appearance.Side > 3 || appearance.Head > 31 { 72 return fmt.Errorf("appearance fields exceed their client bit ranges") 73 } 74 return nil 75 }