@wildshard/shard-format 0.5.10 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.5.10",
3
+ "version": "0.7.0",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -94,6 +94,8 @@
94
94
  "./contracts": "./src/format/contracts.ts",
95
95
  "./coverAppearance": "./src/format/coverAppearance.ts",
96
96
  "./creatures": "./src/format/creatures.ts",
97
+ "./creatureLayers": "./src/format/creature-layers.ts",
98
+ "./creatureSkeletons": "./src/format/creature-skeletons.ts",
97
99
  "./crowdPresentation": "./src/format/crowdPresentation.ts",
98
100
  "./crowds": "./src/format/crowds.ts",
99
101
  "./cueDirector": "./src/format/cueDirector.ts",
@@ -245,6 +247,7 @@
245
247
  "./postGraphDocument": "./src/format/postGraphDocument.ts",
246
248
  "./postGraphLook": "./src/format/postGraphLook.ts",
247
249
  "./postStack": "./src/format/postStack.ts",
250
+ "./look-details": "./src/format/look-details.ts",
248
251
  "./look-light": "./src/format/look-light.ts",
249
252
  "./look-render": "./src/format/look-render.ts",
250
253
  "./pool-surface": "./src/format/pool-surface.ts",
@@ -0,0 +1,262 @@
1
+ import {
2
+ array,
3
+ boolean,
4
+ check,
5
+ exactOptional,
6
+ finite,
7
+ maxLength,
8
+ maxEntries,
9
+ maxValue,
10
+ minLength,
11
+ minValue,
12
+ number,
13
+ picklist,
14
+ pipe,
15
+ regex,
16
+ record,
17
+ strictObject,
18
+ string,
19
+ tuple,
20
+ union,
21
+ } from 'valibot'
22
+
23
+ import { HuntTuningSchema } from './speciesPrograms.ts'
24
+
25
+ import type { SpeciesData } from './speciesPrograms.ts'
26
+ import type { GenericSchema } from 'valibot'
27
+
28
+ /** Labels for quest and journal filters; these never choose mechanics. */
29
+ type CreatureType =
30
+ | 'beast'
31
+ | 'humanoid'
32
+ | 'giant'
33
+ | 'dragonkin'
34
+ | 'undead'
35
+ | 'elemental'
36
+ | 'construct'
37
+ | 'insect'
38
+ | 'aquatic'
39
+ | 'critter'
40
+ | 'aberration'
41
+ type CreatureSkeleton = 'quadruped' | 'biped'
42
+ type CreatureSize = 'tiny' | 'small' | 'medium' | 'large' | 'giant'
43
+ type CreatureReaction = 'passive' | 'skittish' | 'neutral' | 'aggressive'
44
+ type CreatureTuningScalar = number | boolean | string
45
+ type CreatureTuningValue =
46
+ | CreatureTuningScalar
47
+ | CreatureTuningScalar[]
48
+ | Record<string, CreatureTuningScalar>
49
+ type CreatureTuning = Record<string, CreatureTuningValue>
50
+
51
+ interface CreatureBody {
52
+ /** Omit for a GLB with its own rig; a kit poser names its contract explicitly. */
53
+ skeleton?: CreatureSkeleton
54
+ size: CreatureSize
55
+ scale: number
56
+ look: string
57
+ }
58
+ /** The spawn supplies home position; these numbers control its return policy. */
59
+ interface CreatureTemperament {
60
+ brain: string
61
+ reaction: CreatureReaction
62
+ leash: number
63
+ assist: number
64
+ home: { speed: number; stop: number }
65
+ pack?: boolean
66
+ territorial?: number
67
+ ambush?: boolean
68
+ flee?: number
69
+ tuning?: NonNullable<SpeciesData['tuning']> | CreatureTuning
70
+ }
71
+ interface CreatureSenses {
72
+ notice: number
73
+ sight?: { range: number; cone: number; graze?: number }
74
+ hearing?: { still: number; crouch: number; walk: number; sprint: number }
75
+ reveal?: number
76
+ }
77
+ /** Ordered strike-row references. A scalar range means zero through that radius. */
78
+ interface CreatureAbility {
79
+ use: string
80
+ range: number | [number, number]
81
+ cooldown: number
82
+ weight: number
83
+ hpBelow?: number
84
+ damage?: number
85
+ speed?: number
86
+ windup?: number
87
+ duration?: number
88
+ }
89
+ interface CreaturePresentation {
90
+ blood: boolean
91
+ sounds: {
92
+ call?: string
93
+ hurt?: string
94
+ callVariants?: string[]
95
+ callEvery?: [number, number]
96
+ }
97
+ }
98
+ /** Additive fields: absence is preserved without injecting a default layer. */
99
+ interface CreatureLayers {
100
+ type?: CreatureType
101
+ body?: CreatureBody
102
+ temperament?: CreatureTemperament
103
+ senses?: CreatureSenses
104
+ abilities?: CreatureAbility[]
105
+ presentation?: CreaturePresentation
106
+ }
107
+
108
+ const ZERO = 0
109
+ const ONE = 1
110
+ const MAX_ID = 128
111
+ const MAX_SOUND = 4096
112
+ const MAX_RADIUS = 500
113
+ const MAX_SPEED = 15
114
+ const MAX_TIME = 86_400
115
+ const MAX_AMOUNT = 10_000_000
116
+ const MAX_SCALE = 10
117
+ const MAX_CALLS = 32
118
+ const MAX_TUNING_FIELDS = 64
119
+
120
+ const id = pipe(string(), regex(/^[a-z][a-zA-Z0-9.:-]*$/u), maxLength(MAX_ID))
121
+ const sound = pipe(string(), minLength(ONE), maxLength(MAX_SOUND))
122
+ const scalar = pipe(number(), finite())
123
+ const radius = pipe(scalar, minValue(ZERO), maxValue(MAX_RADIUS))
124
+ const speed = pipe(scalar, minValue(ZERO), maxValue(MAX_SPEED))
125
+ const unit = pipe(scalar, minValue(ZERO), maxValue(ONE))
126
+ const time = pipe(scalar, minValue(ZERO), maxValue(MAX_TIME))
127
+ const amount = pipe(scalar, minValue(ZERO), maxValue(MAX_AMOUNT))
128
+ const orderedRadius = pipe(
129
+ tuple([radius, radius]),
130
+ check<[number, number], 'Ordered creature ability range'>(
131
+ ([near, far]: readonly [number, number]): boolean => near <= far,
132
+ 'Ordered creature ability range',
133
+ ),
134
+ )
135
+ const orderedTime = pipe(
136
+ tuple([time, time]),
137
+ check<[number, number], 'Ordered creature call interval'>(
138
+ ([near, far]: readonly [number, number]): boolean => near <= far,
139
+ 'Ordered creature call interval',
140
+ ),
141
+ )
142
+ const tuningNumber = pipe(scalar, minValue(-MAX_AMOUNT), maxValue(MAX_AMOUNT))
143
+ const tuningScalar = union([tuningNumber, boolean(), sound])
144
+ const tuningList = pipe(array(tuningScalar), maxLength(MAX_CALLS))
145
+ const tuningObject = pipe(
146
+ record(id, tuningScalar),
147
+ maxEntries(MAX_TUNING_FIELDS),
148
+ )
149
+ const tuningValue = union([tuningScalar, tuningList, tuningObject])
150
+ const brainTuning = pipe(record(id, tuningValue), maxEntries(MAX_TUNING_FIELDS))
151
+ const tuning = union([HuntTuningSchema, brainTuning])
152
+
153
+ /** Schemas are separately bounded so their defining declaration stays small. */
154
+ const CreatureTypeSchema: GenericSchema<CreatureType> = picklist(
155
+ [
156
+ 'beast',
157
+ 'humanoid',
158
+ 'giant',
159
+ 'dragonkin',
160
+ 'undead',
161
+ 'elemental',
162
+ 'construct',
163
+ 'insect',
164
+ 'aquatic',
165
+ 'critter',
166
+ 'aberration',
167
+ ],
168
+ 'Unknown creature type',
169
+ )
170
+ const CreatureBodySchema: GenericSchema<CreatureBody> = strictObject({
171
+ skeleton: exactOptional(
172
+ picklist(['quadruped', 'biped'], 'Unknown creature skeleton'),
173
+ ),
174
+ size: picklist(
175
+ ['tiny', 'small', 'medium', 'large', 'giant'],
176
+ 'Unknown creature size',
177
+ ),
178
+ scale: pipe(scalar, minValue(Number.MIN_VALUE), maxValue(MAX_SCALE)),
179
+ look: id,
180
+ })
181
+ const CreatureTemperamentSchema: GenericSchema<CreatureTemperament> =
182
+ strictObject({
183
+ brain: id,
184
+ reaction: picklist(
185
+ ['passive', 'skittish', 'neutral', 'aggressive'],
186
+ 'Unknown creature reaction',
187
+ ),
188
+ leash: radius,
189
+ assist: radius,
190
+ home: strictObject({ speed, stop: radius }),
191
+ pack: exactOptional(boolean()),
192
+ territorial: exactOptional(radius),
193
+ ambush: exactOptional(boolean()),
194
+ flee: exactOptional(unit),
195
+ tuning: exactOptional(tuning),
196
+ })
197
+ const sight = strictObject({
198
+ range: radius,
199
+ cone: pipe(scalar, minValue(ZERO), maxValue(Math.PI)),
200
+ graze: exactOptional(radius),
201
+ })
202
+ const hearing = strictObject({
203
+ still: radius,
204
+ crouch: radius,
205
+ walk: radius,
206
+ sprint: radius,
207
+ })
208
+ const CreatureSensesSchema: GenericSchema<CreatureSenses> = strictObject(
209
+ {
210
+ notice: radius,
211
+ sight: exactOptional(sight),
212
+ hearing: exactOptional(hearing),
213
+ reveal: exactOptional(radius),
214
+ },
215
+ 'Unknown creature sense',
216
+ )
217
+ const CreatureAbilitySchema: GenericSchema<CreatureAbility> = strictObject({
218
+ use: id,
219
+ range: union([radius, orderedRadius]),
220
+ cooldown: time,
221
+ weight: amount,
222
+ hpBelow: exactOptional(unit),
223
+ damage: exactOptional(amount),
224
+ speed: exactOptional(amount),
225
+ windup: exactOptional(time),
226
+ duration: exactOptional(time),
227
+ })
228
+ const callVariants = pipe(array(sound), maxLength(MAX_CALLS))
229
+ const CreaturePresentationSchema: GenericSchema<CreaturePresentation> =
230
+ strictObject({
231
+ blood: boolean(),
232
+ sounds: strictObject({
233
+ call: exactOptional(sound),
234
+ hurt: exactOptional(sound),
235
+ callVariants: exactOptional(callVariants),
236
+ callEvery: exactOptional(orderedTime),
237
+ }),
238
+ })
239
+
240
+ export {
241
+ CreatureTypeSchema,
242
+ CreatureBodySchema,
243
+ CreatureTemperamentSchema,
244
+ CreatureSensesSchema,
245
+ CreatureAbilitySchema,
246
+ CreaturePresentationSchema,
247
+ }
248
+ export type {
249
+ CreatureType,
250
+ CreatureSkeleton,
251
+ CreatureSize,
252
+ CreatureReaction,
253
+ CreatureBody,
254
+ CreatureTemperament,
255
+ CreatureSenses,
256
+ CreatureAbility,
257
+ CreaturePresentation,
258
+ CreatureLayers,
259
+ CreatureTuningScalar,
260
+ CreatureTuningValue,
261
+ CreatureTuning,
262
+ }
@@ -0,0 +1,137 @@
1
+ import type { CreatureSkeleton } from './creature-layers.ts'
2
+
3
+ interface SkeletonChain {
4
+ readonly parent: string
5
+ readonly bones: readonly string[]
6
+ }
7
+ interface SkeletonSocket {
8
+ readonly bone: string
9
+ readonly at: readonly [number, number, number]
10
+ }
11
+ interface SkeletonVolume {
12
+ readonly part: string
13
+ readonly bone: string
14
+ readonly shape: 'sphere' | 'capsule'
15
+ readonly radius: number
16
+ readonly halfLength: number
17
+ readonly axis: 'y' | 'z'
18
+ readonly multiplier: number
19
+ }
20
+ /** Bone names are wire-format names after the SDK's import mapping. */
21
+ interface SkeletonContract {
22
+ readonly root: string
23
+ readonly chains: Readonly<Record<string, SkeletonChain>>
24
+ readonly sockets: Readonly<Record<string, SkeletonSocket>>
25
+ readonly lock: string
26
+ readonly volumes: readonly SkeletonVolume[]
27
+ readonly clips: readonly string[]
28
+ }
29
+ const ZERO = 0
30
+ const ONE = 1
31
+ const HEAD_MULTIPLIER = 2
32
+ const BODY_RADIUS = 0.35
33
+ const BODY_HALF_LENGTH = 0.5
34
+ const MIN_VOLUME_RADIUS = 0.3
35
+ const MOUTH_FORWARD = 0.2
36
+ const SEAT_HEIGHT = 0.15
37
+ const ORIGIN: readonly [number, number, number] = [ZERO, ZERO, ZERO]
38
+ const CLIPS: readonly string[] = ['idle', 'walk', 'run', 'attack', 'hit', 'die']
39
+
40
+ function socket(bone: string): SkeletonSocket {
41
+ return { bone, at: ORIGIN }
42
+ }
43
+ function bodyVolume(bone: string, axis: 'y' | 'z'): SkeletonVolume {
44
+ return {
45
+ part: 'body',
46
+ bone,
47
+ shape: 'capsule',
48
+ radius: BODY_RADIUS,
49
+ halfLength: BODY_HALF_LENGTH,
50
+ axis,
51
+ multiplier: ONE,
52
+ }
53
+ }
54
+ const HEAD: SkeletonVolume = {
55
+ part: 'head',
56
+ bone: 'head',
57
+ shape: 'sphere',
58
+ radius: MIN_VOLUME_RADIUS,
59
+ halfLength: ZERO,
60
+ axis: 'y',
61
+ multiplier: HEAD_MULTIPLIER,
62
+ }
63
+
64
+ /** The quadruped keeps today's engine gait names, including front/back leg joints. */
65
+ const QUADRUPED: SkeletonContract = {
66
+ root: 'body',
67
+ chains: {
68
+ neck: { parent: 'body', bones: ['neck1', 'neck2', 'head'] },
69
+ tail: { parent: 'body', bones: ['tail'] },
70
+ 'front.L': {
71
+ parent: 'body',
72
+ bones: ['FL_shoulder', 'FL_carpus', 'FL_fetlock'],
73
+ },
74
+ 'front.R': {
75
+ parent: 'body',
76
+ bones: ['FR_shoulder', 'FR_carpus', 'FR_fetlock'],
77
+ },
78
+ 'back.L': { parent: 'body', bones: ['BL_hip', 'BL_stifle', 'BL_hock'] },
79
+ 'back.R': { parent: 'body', bones: ['BR_hip', 'BR_stifle', 'BR_hock'] },
80
+ },
81
+ sockets: {
82
+ head: socket('head'),
83
+ chest: socket('body'),
84
+ back: socket('body'),
85
+ mouth: { bone: 'head', at: [ZERO, ZERO, MOUTH_FORWARD] },
86
+ seat: { bone: 'body', at: [ZERO, SEAT_HEIGHT, ZERO] },
87
+ tail: socket('tail'),
88
+ },
89
+ lock: 'chest',
90
+ volumes: [bodyVolume('body', 'z'), HEAD],
91
+ clips: CLIPS,
92
+ }
93
+ /** An identity body root retains the existing actor root contract. */
94
+ const BIPED: SkeletonContract = {
95
+ root: 'body',
96
+ chains: {
97
+ spine: {
98
+ parent: 'body',
99
+ bones: ['hips', 'spine', 'chest', 'neck', 'head'],
100
+ },
101
+ 'arm.L': { parent: 'chest', bones: ['upperArm.L', 'forearm.L', 'hand.L'] },
102
+ 'arm.R': { parent: 'chest', bones: ['upperArm.R', 'forearm.R', 'hand.R'] },
103
+ 'leg.L': { parent: 'hips', bones: ['thigh.L', 'shin.L', 'foot.L'] },
104
+ 'leg.R': { parent: 'hips', bones: ['thigh.R', 'shin.R', 'foot.R'] },
105
+ },
106
+ sockets: {
107
+ head: socket('head'),
108
+ chest: socket('chest'),
109
+ back: socket('chest'),
110
+ mouth: { bone: 'head', at: [ZERO, ZERO, MOUTH_FORWARD] },
111
+ 'hand.L': socket('hand.L'),
112
+ 'hand.R': socket('hand.R'),
113
+ },
114
+ lock: 'chest',
115
+ volumes: [bodyVolume('chest', 'y'), HEAD],
116
+ clips: CLIPS,
117
+ }
118
+ const CREATURE_SKELETONS: Readonly<Record<CreatureSkeleton, SkeletonContract>> =
119
+ {
120
+ quadruped: QUADRUPED,
121
+ biped: BIPED,
122
+ }
123
+
124
+ /** Importers and posers share this list, rather than inferring a creature kind. */
125
+ function skeletonBones(
126
+ contract: Readonly<SkeletonContract>,
127
+ ): readonly string[] {
128
+ return [
129
+ contract.root,
130
+ ...Object.values(contract.chains).flatMap(
131
+ (chain: Readonly<SkeletonChain>): readonly string[] => chain.bones,
132
+ ),
133
+ ]
134
+ }
135
+
136
+ export { CREATURE_SKELETONS, MIN_VOLUME_RADIUS, skeletonBones }
137
+ export type { SkeletonChain, SkeletonSocket, SkeletonVolume, SkeletonContract }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * The binary files behind `look.details` (meta SHARD-SOURCE AP32a), kept binary (engine G649: never base64). Plain
3
+ * arithmetic over bytes, so the same checks run in the browser, the Worker, the cook and Node.
4
+ *
5
+ * - A card mesh file: little-endian, no header: `vertices` × f32 xyz positions, `vertices` × f32 xyz normals, `vertices` ×
6
+ * f32 uv, then `triangles` × three u16 indices. Its length is exactly `32 × vertices + 6 × triangles` bytes.
7
+ * - A mask file: one u8 per region cell, row by row from the low corner.
8
+ * - A heights file: one u16 little-endian per region cell, the same order.
9
+ */
10
+
11
+ /** A layer's card mesh: a binary file (by hash) and its counts. */
12
+ interface LookDetailMesh {
13
+ readonly file: string
14
+ readonly vertices: number
15
+ readonly triangles: number
16
+ }
17
+ /** A layer's three files, as bytes. */
18
+ interface LookDetailFiles {
19
+ readonly mesh: Readonly<Uint8Array>
20
+ readonly mask: Readonly<Uint8Array>
21
+ readonly heights: Readonly<Uint8Array>
22
+ }
23
+ /** The parts of a layer row its files are checked against. */
24
+ interface DetailFileRow {
25
+ readonly id: string
26
+ readonly mesh: LookDetailMesh
27
+ readonly region: { readonly columns: number; readonly rows: number }
28
+ }
29
+ /** A card mesh's attribute views over its file's bytes. */
30
+ interface LookDetailMeshViews {
31
+ readonly positions: Float32Array
32
+ readonly normals: Float32Array
33
+ readonly uvs: Float32Array
34
+ readonly indices: Uint16Array
35
+ }
36
+
37
+ /** At most this many triangles in one layer's card mesh. */
38
+ const DETAIL_MESH_TRIANGLES_MAX = 2000
39
+ /** At most this many vertices in one layer's card mesh. */
40
+ const DETAIL_MESH_VERTICES_MAX = 6000
41
+ const NONE = 0
42
+ const ONE = 1
43
+ const XYZ = 3
44
+ const UV = 2
45
+ const TRIANGLE = 3
46
+ const F32 = 4
47
+ const U16 = 2
48
+
49
+ /** The exact byte length of a card mesh file with these counts. */
50
+ function detailMeshBytes(vertices: number, triangles: number): number {
51
+ return vertices * (XYZ + XYZ + UV) * F32 + triangles * TRIANGLE * U16
52
+ }
53
+
54
+ /** The messages whose condition holds. */
55
+ function faultsOf(checks: readonly (readonly [boolean, string])[]): string[] {
56
+ return checks
57
+ .filter((entry: readonly [boolean, string]): boolean => entry[NONE])
58
+ .map((entry: readonly [boolean, string]): string => entry[ONE])
59
+ }
60
+
61
+ /** A fresh aligned copy of `length` bytes from `offset`. */
62
+ function aligned(
63
+ bytes: Readonly<Uint8Array>,
64
+ offset: number,
65
+ length: number,
66
+ ): ArrayBuffer {
67
+ return bytes.slice(offset, offset + length).buffer
68
+ }
69
+
70
+ /** The card mesh's attribute views (copied, so any byte offset works). */
71
+ function detailMeshViews(
72
+ bytes: Readonly<Uint8Array>,
73
+ mesh: LookDetailMesh,
74
+ ): LookDetailMeshViews {
75
+ const xyz = mesh.vertices * XYZ * F32
76
+ const uv = mesh.vertices * UV * F32
77
+ return {
78
+ indices: new Uint16Array(
79
+ aligned(bytes, xyz + xyz + uv, mesh.triangles * TRIANGLE * U16),
80
+ ),
81
+ normals: new Float32Array(aligned(bytes, xyz, xyz)),
82
+ positions: new Float32Array(aligned(bytes, NONE, xyz)),
83
+ uvs: new Float32Array(aligned(bytes, xyz + xyz, uv)),
84
+ }
85
+ }
86
+
87
+ /** True when any index is `vertices` or more. */
88
+ function indexPast(indices: Readonly<Uint16Array>, vertices: number): boolean {
89
+ return indices.some((index: number): boolean => index >= vertices)
90
+ }
91
+
92
+ /** The faults in one layer's files against its mesh counts and region grid, or none. */
93
+ function detailFileFaults(
94
+ row: DetailFileRow,
95
+ files: LookDetailFiles,
96
+ ): string[] {
97
+ const { id, mesh } = row
98
+ const cells = row.region.columns * row.region.rows
99
+ const meshBytes = detailMeshBytes(mesh.vertices, mesh.triangles)
100
+ const sized = files.mesh.length === meshBytes
101
+ const past =
102
+ sized && indexPast(detailMeshViews(files.mesh, mesh).indices, mesh.vertices)
103
+ return faultsOf([
104
+ [
105
+ !sized,
106
+ `detail ${id}: mesh file holds ${String(files.mesh.length)} bytes, its counts need ${String(meshBytes)}`,
107
+ ],
108
+ [past, `detail ${id}: an index past the last vertex`],
109
+ [
110
+ files.mask.length !== cells,
111
+ `detail ${id}: mask holds ${String(files.mask.length)} bytes, the region has ${String(cells)} cells`,
112
+ ],
113
+ [
114
+ files.heights.length !== cells * U16,
115
+ `detail ${id}: heights hold ${String(files.heights.length)} bytes, the region needs ${String(cells * U16)}`,
116
+ ],
117
+ ])
118
+ }
119
+
120
+ export {
121
+ DETAIL_MESH_TRIANGLES_MAX,
122
+ DETAIL_MESH_VERTICES_MAX,
123
+ detailFileFaults,
124
+ detailMeshBytes,
125
+ detailMeshViews,
126
+ faultsOf,
127
+ }
128
+ export type {
129
+ DetailFileRow,
130
+ LookDetailFiles,
131
+ LookDetailMesh,
132
+ LookDetailMeshViews,
133
+ }