server/model_sphere.go (1500B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 ) 7 8 type collisionSphere struct { 9 offset [3]float32 10 radius float32 11 } 12 13 func decodeModelSphere(data []byte, kind uint32, scale float32) (collisionSphere, error) { 14 var offset uint64 15 switch kind { 16 case 1010001: 17 if len(data) < 8 || binary.LittleEndian.Uint32(data[:4]) != 3 { 18 return collisionSphere{}, fmt.Errorf("invalid model sphere mesh header") 19 } 20 offset = 8 + uint64(binary.LittleEndian.Uint32(data[4:8]))*36 21 case 1010002: 22 if len(data) < 36 || binary.LittleEndian.Uint32(data[32:36]) < 1009 { 23 return collisionSphere{}, fmt.Errorf("invalid model sphere CIR header") 24 } 25 offset = 36 + (uint64(binary.LittleEndian.Uint32(data[32:36]))/1009-1)*44 26 default: 27 return collisionSphere{}, fmt.Errorf("invalid model sphere resource kind %d", kind) 28 } 29 if offset+4 > uint64(len(data)) { 30 return collisionSphere{}, fmt.Errorf("truncated model sphere metadata") 31 } 32 33 // Sources: client/game/bbr.dll/lifted-0030.c, sub_250911D0; 34 // client/game/bbr.dll/lifted-0036.c, sub_250A2290. Store after wide arithmetic. 35 var torso [4]float32 36 for axis, packed := range data[offset : offset+4] { 37 value := float64(int8(packed)) 38 torso[axis] = float32(value * (value*value*float64(float32(.000006)) + float64(float32(.05)))) 39 } 40 // client/game/llr.dll/lifted-0024.c, sub_3803DD90: fallback, drop X/Z, then scale. 41 if torso[3] < 0 { 42 torso[3] = .5 43 } 44 return collisionSphere{offset: [3]float32{0, torso[1] * scale, 0}, radius: torso[3] * scale}, nil 45 }