@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 +4 -1
- package/src/format/creature-layers.ts +262 -0
- package/src/format/creature-skeletons.ts +137 -0
- package/src/format/look-details-bytes.ts +133 -0
- package/src/format/look-details.ts +255 -0
- package/src/format/rows.ts +36 -8
- package/src/format/shardSchema.ts +7 -1
- package/src/format/skin-file.ts +64 -0
- package/src/format/skin-variants.ts +190 -0
- package/src/format/skinAssets.ts +31 -37
- package/src/format/skins.ts +26 -12
- package/src/format/speciesPrograms.ts +22 -17
- package/src/format/view-draws.ts +15 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detail layers (meta SHARD-SOURCE AP32a): grass, ferns and other small clutter as a card mesh plus a density mask, scattered
|
|
3
|
+
* by the engine in a ring round the camera (its grass-carpet pattern, made general) instead of baked as thousands of instance
|
|
4
|
+
* matrices. Absent: no layers (no default is filled, so older shard files keep their bytes).
|
|
5
|
+
*
|
|
6
|
+
* One `look.details` row:
|
|
7
|
+
* - `id`: unique among the layers; it also seeds the scatter, so the same square metre always grows the same clumps.
|
|
8
|
+
* - `material`: a `look.materials` id (or a family) the cards draw with.
|
|
9
|
+
* - `mesh`: the card: a binary `file` (by hash) and its `vertices` and `triangles` counts (layout in
|
|
10
|
+
* `look-details-bytes.ts`). At most `DETAIL_MESH_TRIANGLES_MAX` triangles.
|
|
11
|
+
* - `region`: the mask's grid: its low `corner` ([x, z] metres), `cell` size, `columns` (along x), `rows` (along z), and
|
|
12
|
+
* the `span` of y its heights cover.
|
|
13
|
+
* - `mask`: a binary file, one u8 per cell, row by row from the low corner: 0 grows nothing, 255 grows `clumps` clumps.
|
|
14
|
+
* - `heights`: a binary file, one u16 per cell: the ground under the cell's centre, `span[0] + h / 65535 × (span[1] -
|
|
15
|
+
* span[0])`.
|
|
16
|
+
* - `clumps`: clumps in a full cell; `scale`, `tilt` (the most a clump leans, radians) and `tint` (two sRGB multipliers a
|
|
17
|
+
* clump's colour is picked between) vary each clump.
|
|
18
|
+
* - `sway`: wind on the cards (`amount` of lean, `rate` in cycles a second); absent: still.
|
|
19
|
+
* - `radius`: metres round the camera that grow the layer on desktop; phones grow half the radius (the engine's rule).
|
|
20
|
+
* - `shadow`: the cards cast shadows.
|
|
21
|
+
*
|
|
22
|
+
* The files stay binary (engine G649). Their sizes are checked against the row where the bytes are at hand
|
|
23
|
+
* (`detailFileFaults`); the shardfile rule checks they are declared binary files with no dependencies.
|
|
24
|
+
*/
|
|
25
|
+
import {
|
|
26
|
+
array,
|
|
27
|
+
boolean,
|
|
28
|
+
check,
|
|
29
|
+
exactOptional,
|
|
30
|
+
finite,
|
|
31
|
+
integer,
|
|
32
|
+
maxLength,
|
|
33
|
+
maxValue,
|
|
34
|
+
minValue,
|
|
35
|
+
number,
|
|
36
|
+
pipe,
|
|
37
|
+
regex,
|
|
38
|
+
string,
|
|
39
|
+
strictObject,
|
|
40
|
+
tuple,
|
|
41
|
+
} from 'valibot'
|
|
42
|
+
|
|
43
|
+
import {
|
|
44
|
+
DETAIL_MESH_TRIANGLES_MAX,
|
|
45
|
+
DETAIL_MESH_VERTICES_MAX,
|
|
46
|
+
} from './look-details-bytes.ts'
|
|
47
|
+
import { materialExists } from './materials.ts'
|
|
48
|
+
|
|
49
|
+
import type { LookDetailMesh } from './look-details-bytes.ts'
|
|
50
|
+
import type { GenericSchema } from 'valibot'
|
|
51
|
+
|
|
52
|
+
type Rgb = readonly [number, number, number]
|
|
53
|
+
type Pair = readonly [number, number]
|
|
54
|
+
|
|
55
|
+
/** A layer's mask grid. */
|
|
56
|
+
interface LookDetailRegion {
|
|
57
|
+
readonly corner: Pair
|
|
58
|
+
readonly cell: number
|
|
59
|
+
readonly columns: number
|
|
60
|
+
readonly rows: number
|
|
61
|
+
readonly span: Pair
|
|
62
|
+
}
|
|
63
|
+
/** Wind on a layer's cards. */
|
|
64
|
+
interface LookDetailSway {
|
|
65
|
+
readonly amount: number
|
|
66
|
+
readonly rate: number
|
|
67
|
+
}
|
|
68
|
+
/** One detail layer. */
|
|
69
|
+
interface LookDetail {
|
|
70
|
+
readonly id: string
|
|
71
|
+
readonly material: string
|
|
72
|
+
readonly mesh: LookDetailMesh
|
|
73
|
+
readonly region: LookDetailRegion
|
|
74
|
+
readonly mask: string
|
|
75
|
+
readonly heights: string
|
|
76
|
+
readonly clumps: number
|
|
77
|
+
readonly scale: Pair
|
|
78
|
+
readonly tilt: number
|
|
79
|
+
readonly tint: readonly [Rgb, Rgb]
|
|
80
|
+
readonly sway?: LookDetailSway
|
|
81
|
+
readonly radius: number
|
|
82
|
+
readonly shadow: boolean
|
|
83
|
+
}
|
|
84
|
+
/** A shardfile file entry, as the rule reads it. */
|
|
85
|
+
interface DetailFileEntry {
|
|
86
|
+
readonly hash: string
|
|
87
|
+
readonly kind: string
|
|
88
|
+
readonly dependencies: readonly string[]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** At most this many detail layers in one look. */
|
|
92
|
+
const DETAIL_LAYERS_MAX = 8
|
|
93
|
+
/** At most this many mask columns or rows (512 × 512 cells). */
|
|
94
|
+
const DETAIL_SIDE_MAX = 512
|
|
95
|
+
/** At most this many clumps in a full cell. */
|
|
96
|
+
const DETAIL_CLUMPS_MAX = 64
|
|
97
|
+
const NONE = 0
|
|
98
|
+
const FULL = 1
|
|
99
|
+
const TRIANGLE = 3
|
|
100
|
+
const CELL_MIN = 0.5
|
|
101
|
+
const CELL_MAX = 8
|
|
102
|
+
const SCALE_MIN = 0.05
|
|
103
|
+
const SCALE_MAX = 20
|
|
104
|
+
const RATE_MIN = 0.05
|
|
105
|
+
const RATE_MAX = 10
|
|
106
|
+
const RADIUS_MIN = 4
|
|
107
|
+
const RADIUS_MAX = 200
|
|
108
|
+
|
|
109
|
+
/** The layers whose material names no `look.materials` entry or family. */
|
|
110
|
+
function detailMaterialFaults(
|
|
111
|
+
rows: readonly LookDetail[],
|
|
112
|
+
materials: Readonly<Record<string, unknown>>,
|
|
113
|
+
): string[] {
|
|
114
|
+
const missing = rows.filter(
|
|
115
|
+
(row: LookDetail): boolean => !materialExists(materials, row.material),
|
|
116
|
+
)
|
|
117
|
+
return missing.map(
|
|
118
|
+
(row: LookDetail): string =>
|
|
119
|
+
`detail ${row.id}: material ${row.material} is not declared`,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** True when `hash` is a declared binary file with no dependencies. */
|
|
124
|
+
function plainBinary(
|
|
125
|
+
files: ReadonlyMap<string, DetailFileEntry>,
|
|
126
|
+
hash: string,
|
|
127
|
+
): boolean {
|
|
128
|
+
const file = files.get(hash)
|
|
129
|
+
return file?.kind === 'binary' && file.dependencies.length === NONE
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The shardfile rules: every layer's material is declared and its three files are plain binaries. */
|
|
133
|
+
function detailLayerRules(
|
|
134
|
+
rows: readonly LookDetail[] | undefined,
|
|
135
|
+
materials: Readonly<Record<string, unknown>>,
|
|
136
|
+
files: readonly DetailFileEntry[],
|
|
137
|
+
): string[] {
|
|
138
|
+
const layers = rows ?? []
|
|
139
|
+
const byHash = new Map(
|
|
140
|
+
files.map((file: DetailFileEntry): [string, DetailFileEntry] => [
|
|
141
|
+
file.hash,
|
|
142
|
+
file,
|
|
143
|
+
]),
|
|
144
|
+
)
|
|
145
|
+
const refs = layers.flatMap((row: LookDetail): string[] => [
|
|
146
|
+
row.mesh.file,
|
|
147
|
+
row.mask,
|
|
148
|
+
row.heights,
|
|
149
|
+
])
|
|
150
|
+
const errors: string[] = []
|
|
151
|
+
if (detailMaterialFaults(layers, materials).length > NONE) {
|
|
152
|
+
errors.push('declared detail layer material')
|
|
153
|
+
}
|
|
154
|
+
if (!refs.every((hash: string): boolean => plainBinary(byHash, hash))) {
|
|
155
|
+
errors.push('detail layer files are declared binaries with no dependencies')
|
|
156
|
+
}
|
|
157
|
+
return errors
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** True when a layer's ranges run low to high and its mesh can index every triangle. */
|
|
161
|
+
function ordered(row: LookDetail): boolean {
|
|
162
|
+
const spans =
|
|
163
|
+
row.region.span[NONE] <= row.region.span[FULL] &&
|
|
164
|
+
row.scale[NONE] <= row.scale[FULL]
|
|
165
|
+
return spans && row.mesh.vertices >= TRIANGLE
|
|
166
|
+
}
|
|
167
|
+
/** True when no two layers share an id. */
|
|
168
|
+
function uniqueIds(rows: readonly LookDetail[]): boolean {
|
|
169
|
+
return (
|
|
170
|
+
new Set(rows.map((row: LookDetail): string => row.id)).size === rows.length
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function range(low: number, high: number): GenericSchema<number, number> {
|
|
175
|
+
return pipe(number(), finite(), minValue(low), maxValue(high))
|
|
176
|
+
}
|
|
177
|
+
function count(low: number, high: number): GenericSchema<number, number> {
|
|
178
|
+
return pipe(number(), integer(), minValue(low), maxValue(high))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const real = pipe(number(), finite())
|
|
182
|
+
const unit = range(NONE, FULL)
|
|
183
|
+
const rgb = tuple([unit, unit, unit])
|
|
184
|
+
const identity = pipe(string(), regex(/^[a-z0-9][a-z0-9._-]{0,63}$/u))
|
|
185
|
+
const hash = pipe(string(), regex(/^[a-f0-9]{64}$/u))
|
|
186
|
+
const side = count(FULL, DETAIL_SIDE_MAX)
|
|
187
|
+
const size = range(SCALE_MIN, SCALE_MAX)
|
|
188
|
+
const pair = tuple([real, real])
|
|
189
|
+
const MeshSchema = strictObject({
|
|
190
|
+
file: hash,
|
|
191
|
+
triangles: count(FULL, DETAIL_MESH_TRIANGLES_MAX),
|
|
192
|
+
vertices: count(FULL, DETAIL_MESH_VERTICES_MAX),
|
|
193
|
+
})
|
|
194
|
+
const RegionSchema = strictObject({
|
|
195
|
+
cell: range(CELL_MIN, CELL_MAX),
|
|
196
|
+
columns: side,
|
|
197
|
+
corner: pair,
|
|
198
|
+
rows: side,
|
|
199
|
+
span: pair,
|
|
200
|
+
})
|
|
201
|
+
const SwaySchema = strictObject({
|
|
202
|
+
amount: unit,
|
|
203
|
+
rate: range(RATE_MIN, RATE_MAX),
|
|
204
|
+
})
|
|
205
|
+
const DetailRowSchema: GenericSchema<LookDetail, LookDetail> = strictObject({
|
|
206
|
+
clumps: count(FULL, DETAIL_CLUMPS_MAX),
|
|
207
|
+
heights: hash,
|
|
208
|
+
id: identity,
|
|
209
|
+
mask: hash,
|
|
210
|
+
material: identity,
|
|
211
|
+
mesh: MeshSchema,
|
|
212
|
+
radius: range(RADIUS_MIN, RADIUS_MAX),
|
|
213
|
+
region: RegionSchema,
|
|
214
|
+
scale: tuple([size, size]),
|
|
215
|
+
shadow: boolean(),
|
|
216
|
+
sway: exactOptional(SwaySchema),
|
|
217
|
+
tilt: unit,
|
|
218
|
+
tint: tuple([rgb, rgb]),
|
|
219
|
+
})
|
|
220
|
+
const LookDetailSchema: GenericSchema<LookDetail, LookDetail> = pipe(
|
|
221
|
+
DetailRowSchema,
|
|
222
|
+
check<LookDetail, string>(
|
|
223
|
+
ordered,
|
|
224
|
+
'detail layer ranges run low to high and its mesh has a triangle',
|
|
225
|
+
),
|
|
226
|
+
)
|
|
227
|
+
/** `look.details`: at most `DETAIL_LAYERS_MAX` layers with unique ids. */
|
|
228
|
+
const LookDetailsSchema: GenericSchema<LookDetail[], LookDetail[]> = pipe(
|
|
229
|
+
array(LookDetailSchema),
|
|
230
|
+
maxLength(DETAIL_LAYERS_MAX),
|
|
231
|
+
check<LookDetail[], string>(uniqueIds, 'detail layer ids are unique'),
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
export {
|
|
235
|
+
DETAIL_CLUMPS_MAX,
|
|
236
|
+
DETAIL_LAYERS_MAX,
|
|
237
|
+
DETAIL_SIDE_MAX,
|
|
238
|
+
LookDetailsSchema,
|
|
239
|
+
detailLayerRules,
|
|
240
|
+
detailMaterialFaults,
|
|
241
|
+
}
|
|
242
|
+
export {
|
|
243
|
+
DETAIL_MESH_TRIANGLES_MAX,
|
|
244
|
+
DETAIL_MESH_VERTICES_MAX,
|
|
245
|
+
detailFileFaults,
|
|
246
|
+
detailMeshBytes,
|
|
247
|
+
detailMeshViews,
|
|
248
|
+
} from './look-details-bytes.ts'
|
|
249
|
+
export type {
|
|
250
|
+
DetailFileRow,
|
|
251
|
+
LookDetailFiles,
|
|
252
|
+
LookDetailMesh,
|
|
253
|
+
LookDetailMeshViews,
|
|
254
|
+
} from './look-details-bytes.ts'
|
|
255
|
+
export type { DetailFileEntry, LookDetail, LookDetailRegion, LookDetailSway }
|
package/src/format/rows.ts
CHANGED
|
@@ -3,6 +3,8 @@ import * as v from 'valibot';
|
|
|
3
3
|
import { isJsonData } from './json.ts';
|
|
4
4
|
import { sketchReference } from './sketch.ts';
|
|
5
5
|
import { JournalRowSchema, journalRowRules } from './compendiumRows.ts';
|
|
6
|
+
import { CreatureTypeSchema, CreatureBodySchema, CreatureTemperamentSchema, CreatureSensesSchema, CreatureAbilitySchema, CreaturePresentationSchema } from './creature-layers.ts';
|
|
7
|
+
import type { CreatureLayers } from './creature-layers.ts';
|
|
6
8
|
|
|
7
9
|
const id = v.pipe(v.string(), v.regex(/^[a-z][a-zA-Z0-9.:-]*$/u), v.maxLength(128));
|
|
8
10
|
const text = v.pipe(v.string(), v.maxLength(4096));
|
|
@@ -37,8 +39,13 @@ const variant = v.strictObject({ id, label: text, rarity: v.picklist(['common',
|
|
|
37
39
|
const flight = v.strictObject({ altitude: v.pipe(finite, v.minValue(-250), v.maxValue(250)),
|
|
38
40
|
above: v.exactOptional(v.picklist(['ground', 'world'])), climbRate: v.pipe(positive, v.maxValue(30)), diveRate: v.pipe(positive, v.maxValue(30)),
|
|
39
41
|
lockRange: v.exactOptional(v.pipe(positive, v.maxValue(600))), bank: v.exactOptional(v.pipe(finite, v.check(n => n > 0 && n < Math.PI / 2, 'flight bank between 0 and pi/2'))) });
|
|
42
|
+
const MAX_ABILITIES = 32;
|
|
43
|
+
const abilities = v.pipe(v.array(CreatureAbilitySchema), v.maxLength(MAX_ABILITIES));
|
|
40
44
|
const species = v.strictObject({ id, kind: id, label: text, aggressive: v.boolean(), lockable: v.boolean(), dims, flight: v.exactOptional(flight),
|
|
41
|
-
variants: v.pipe(v.array(variant), v.minLength(1), v.maxLength(64))
|
|
45
|
+
variants: v.pipe(v.array(variant), v.minLength(1), v.maxLength(64)),
|
|
46
|
+
type: v.exactOptional(CreatureTypeSchema), body: v.exactOptional(CreatureBodySchema),
|
|
47
|
+
temperament: v.exactOptional(CreatureTemperamentSchema), senses: v.exactOptional(CreatureSensesSchema),
|
|
48
|
+
abilities: v.exactOptional(abilities), presentation: v.exactOptional(CreaturePresentationSchema) });
|
|
42
49
|
const parameter = v.union([finite, v.boolean(), text, v.pipe(v.array(finite), v.maxLength(16))]);
|
|
43
50
|
const look = v.strictObject({ id, species: id, recipe: id, material: v.optional(v.nullable(id), null), parameters: v.record(id, parameter),
|
|
44
51
|
animation: v.strictObject({ recipe: id, parameters: v.record(id, parameter) }) });
|
|
@@ -54,7 +61,7 @@ const raw: v.StrictObjectSchema<{ readonly strikes: v.SchemaWithPipe<readonly [v
|
|
|
54
61
|
/** Pure author rows: numeric weights, state output tables, clocks and registered view/presentation recipes. */
|
|
55
62
|
|
|
56
63
|
/** Reference, uniqueness and complete clock/weather transition rules for data rows. */
|
|
57
|
-
|
|
64
|
+
function rowRules(rows: ShardRows): string[] {
|
|
58
65
|
const errors: string[] = [];
|
|
59
66
|
for (const list of Object.values(rows)) if (new Set(list.map((r) => r.id)).size !== list.length) errors.push('unique row ids');
|
|
60
67
|
for (const row of rows.loot) if (row.economy !== undefined && !('kind' in row.economy)) errors.push(...lootEconomyRules(row.economy));
|
|
@@ -65,6 +72,16 @@ export function rowRules(rows: ShardRows): string[] {
|
|
|
65
72
|
for (const d of rows.days) if (d.schedule[0]?.from !== 0 || d.schedule.at(-1)?.to !== 24 || d.schedule.some((s, i) => s.from >= s.to || (i > 0 && d.schedule[i - 1]?.to !== s.from))) errors.push('complete ordered day schedule');
|
|
66
73
|
const speciesIds = new Set(rows.species.map((s) => s.id));
|
|
67
74
|
for (const s of rows.species) if (new Set(s.variants.map((r) => r.id)).size !== s.variants.length || s.variants.some((r) => r.scale[1] > 10)) errors.push('species variants');
|
|
75
|
+
const strikeIds = new Set<string>();
|
|
76
|
+
for (const strikeRow of rows.strikes) { strikeIds.add(strikeRow.id); }
|
|
77
|
+
const lookIds = new Set<string>();
|
|
78
|
+
for (const lookRow of rows.looks) { lookIds.add(lookRow.id); }
|
|
79
|
+
for (const speciesRow of rows.species) {
|
|
80
|
+
for (const ability of speciesRow.abilities ?? []) {
|
|
81
|
+
if (!strikeIds.has(ability.use)) { errors.push(`Unknown creature ability: ${ability.use}`); }
|
|
82
|
+
}
|
|
83
|
+
if (speciesRow.body !== undefined && !lookIds.has(speciesRow.body.look)) { errors.push(`Unknown creature look: ${speciesRow.body.look}`); }
|
|
84
|
+
}
|
|
68
85
|
for (const l of rows.looks) if (!speciesIds.has(l.species)) errors.push('look species');
|
|
69
86
|
for (const c of rows.compendiums) {
|
|
70
87
|
if ('kind' in c) { errors.push(...journalRowRules(c)); continue; }
|
|
@@ -73,14 +90,25 @@ export function rowRules(rows: ShardRows): string[] {
|
|
|
73
90
|
}
|
|
74
91
|
return errors;
|
|
75
92
|
}
|
|
93
|
+
function rowMessage(issue: Readonly<{ input: unknown }>): string {
|
|
94
|
+
const data = v.parse(raw, issue.input);
|
|
95
|
+
const reasons = rowRules(data);
|
|
96
|
+
return `row reference rules: ${reasons.join('; ')}`;
|
|
97
|
+
}
|
|
76
98
|
/** The independent JSON check runs before strict row schemas; functions/accessors/cycles never become data. */
|
|
77
|
-
|
|
99
|
+
const RowsSchema: v.SchemaWithPipe<readonly [v.UnknownSchema, v.CheckAction<unknown, 'JSON-only rows'>, typeof raw, v.CheckAction<ShardRows, typeof rowMessage>]> = v.pipe(v.unknown(), v.check(isJsonData, 'JSON-only rows'), raw, v.check((rows) => rowRules(rows).length === 0, rowMessage));
|
|
78
100
|
/** Validate rows at the author boundary, before JSON serialization can erase closures. */
|
|
79
|
-
|
|
101
|
+
function parseRows(input: unknown): ShardRows { return v.parse(RowsSchema, input); }
|
|
80
102
|
|
|
81
103
|
/** Admitted author data independent of validator implementation metadata. */
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
104
|
+
type StrikeRowsInput = StrikeRows;
|
|
105
|
+
interface StrikeRows { id: string; shape: ({ kind: 'point'; radius: number; }) | ({ kind: 'sphere'; radius: number; }) | ({ kind: 'lane'; length: number; width: number; }) | ({ kind: 'arc'; radius: number; halfAngle: number; }) | ({ kind: 'wedge'; length: number; halfAngle: number; }) | ({ kind: 'ring'; inner: number; outer: number; }); windup: number; active: number; recover: number; cooldown: number; range: number; damage: number; tags: (`${string}.${string}`)[]; weight: number; speed: number; }
|
|
106
|
+
interface ShardRowsInput { strikes: (StrikeRowsInput)[]; weather: ({ id: string; states: ({ id: string; next: string; length: [number, number]; numbers: { overcast: number; rain: number; wet: number; wind: number; fog: number; }; })[]; initial: { overcast: number; rain: number; wet: number; wind: number; fog: number; }; soak: number; dry: number; modes: ({ id: string; hold: (string) | null; at: number; dry: boolean; })[]; })[]; days: ({ id: string; units: 'hour'; start: number; schedule: ({ phase: 'dawn' | 'day' | 'golden' | 'dusk' | 'night'; from: number; to: number; minutes: number; })[]; sun: { maxElevation: number; azimuthOffset: number; }; fixed: { midday: number; golden: number; sunset: number; night: number; }; presets: { dawn: number; noon: number; dusk: number; night: number; }; })[]; species: SpeciesRow[]; looks: ({ id: string; species: string; recipe: string; material?: (string) | null | undefined; parameters: Record<string, (number) | (boolean) | (string) | ((number)[])>; animation: { recipe: string; parameters: Record<string, (number) | (boolean) | (string) | ((number)[])>; }; })[]; compendiums: (({ id: string; className: string; title: string; tabs: ({ id: string; label: string; })[]; stamp: string; stats: ({ label: string; value: string; })[]; entries: ({ id: string; kind: 'species'; tab: string; name: string; notes: string; sketch: string; species: string; })[]; }) | (v.InferInput<typeof JournalRowSchema>))[]; loot: (({ id: string; gear: 'purse.coins'; finds: null; marks: null; charted: boolean; chime: string; economy?: v.InferInput<typeof LootEconomySchema>; }) | ({ id: string; economy: v.InferInput<typeof BarterEconomySchema>; }))[]; }
|
|
85
107
|
// oxlint-disable-next-line typescript/consistent-type-definitions -- Object.values needs the original implicit dictionary contract for row validation.
|
|
86
|
-
|
|
108
|
+
type ShardRows = { strikes: (StrikeRows)[]; weather: ({ id: string; states: ({ id: string; next: string; length: [number, number]; numbers: { overcast: number; rain: number; wet: number; wind: number; fog: number; }; })[]; initial: { overcast: number; rain: number; wet: number; wind: number; fog: number; }; soak: number; dry: number; modes: ({ id: string; hold: (string) | null; at: number; dry: boolean; })[]; })[]; days: ({ id: string; units: 'hour'; start: number; schedule: ({ phase: 'dawn' | 'day' | 'golden' | 'dusk' | 'night'; from: number; to: number; minutes: number; })[]; sun: { maxElevation: number; azimuthOffset: number; }; fixed: { midday: number; golden: number; sunset: number; night: number; }; presets: { dawn: number; noon: number; dusk: number; night: number; }; })[]; species: SpeciesRow[]; looks: ({ id: string; species: string; recipe: string; material: (string) | null; parameters: Record<string, (number) | (boolean) | (string) | ((number)[])>; animation: { recipe: string; parameters: Record<string, (number) | (boolean) | (string) | ((number)[])>; }; })[]; compendiums: (({ id: string; className: string; title: string; tabs: ({ id: string; label: string; })[]; stamp: string; stats: ({ label: string; value: string; })[]; entries: ({ id: string; kind: 'species'; tab: string; name: string; notes: string; sketch: string; species: string; })[]; }) | (v.InferOutput<typeof JournalRowSchema>))[]; loot: (({ id: string; gear: 'purse.coins'; finds: null; marks: null; charted: boolean; chime: string; economy?: v.InferOutput<typeof LootEconomySchema>; }) | ({ id: string; economy: v.InferOutput<typeof BarterEconomySchema>; }))[]; };
|
|
109
|
+
|
|
110
|
+
/** A species keeps its old fields, with optional explicit creature layers. */
|
|
111
|
+
interface SpeciesRow extends CreatureLayers { id: string; kind: string; label: string; aggressive: boolean; lockable: boolean; dims: { bodyY: number; bodyHalfLen: number; bodyRadius: number; headRadius: number; legLen: number; feet: ([number, number])[]; halfWidth: number; }; flight?: { altitude: number; above?: 'ground' | 'world'; climbRate: number; diveRate: number; lockRange?: number; bank?: number; }; variants: ({ id: string; label: string; rarity: 'common' | 'uncommon' | 'rare' | 'legendary'; weight: number; scale: [number, number]; hp: number; mods: { speed: number; chargeDist: number; damageTaken: number; chargeDamage: number; relentless: boolean; }; })[]; }
|
|
112
|
+
|
|
113
|
+
export { rowRules, RowsSchema, parseRows };
|
|
114
|
+
export type { StrikeRowsInput, StrikeRows, ShardRowsInput, ShardRows, SpeciesRow };
|
|
@@ -58,6 +58,8 @@ import type { LookSky, LookSkyInput } from './look-sky.ts';
|
|
|
58
58
|
import { LookBloomSchema, LookLightSchema, LookToneSchema } from './look-light.ts';
|
|
59
59
|
import { LookVignetteSchema } from './look-render.ts';
|
|
60
60
|
import type { LookBloom, LookBloomInput, LookLight, LookLightInput, LookTone } from './look-light.ts';
|
|
61
|
+
import { LookDetailsSchema, detailLayerRules } from './look-details.ts';
|
|
62
|
+
import type { LookDetail } from './look-details.ts';
|
|
61
63
|
import type { LookVignette } from './look-render.ts';
|
|
62
64
|
|
|
63
65
|
const channel = v.pipe(finite, v.minValue(0), v.maxValue(1));
|
|
@@ -78,7 +80,7 @@ const rawSchema: v.GenericSchema<ShardfileRowsInput, ShardfileRows> & { readonly
|
|
|
78
80
|
map: v.exactOptional(MapSchema),
|
|
79
81
|
requires: RequiresSchema,
|
|
80
82
|
budgets: BudgetsSchema,
|
|
81
|
-
look: v.strictObject({ families: names, materials: v.optional(MaterialsSchema, {}), familyLooks: v.optional(FamilyLooksSchema, {}), curves: v.exactOptional(MaterialCurvesSchema), samplers: v.exactOptional(TextureSamplersSchema), post: v.exactOptional(PostStackSchema), sky: v.exactOptional(LookSkySchema), light: v.exactOptional(LookLightSchema), grade: lookGrade, clock: v.literal('engine'), day: v.optional(day), dayOverride: v.nullable(channel), keys: v.pipe(v.array(key), v.maxLength(64)) }),
|
|
83
|
+
look: v.strictObject({ families: names, materials: v.optional(MaterialsSchema, {}), familyLooks: v.optional(FamilyLooksSchema, {}), curves: v.exactOptional(MaterialCurvesSchema), samplers: v.exactOptional(TextureSamplersSchema), post: v.exactOptional(PostStackSchema), sky: v.exactOptional(LookSkySchema), light: v.exactOptional(LookLightSchema), details: v.exactOptional(LookDetailsSchema), grade: lookGrade, clock: v.literal('engine'), day: v.optional(day), dayOverride: v.nullable(channel), keys: v.pipe(v.array(key), v.maxLength(64)) }),
|
|
82
84
|
sim: SimSchema,
|
|
83
85
|
state: StateSchema,
|
|
84
86
|
migrations: v.optional(MigrationsSchema, []),
|
|
@@ -189,6 +191,7 @@ export interface Shardfile {
|
|
|
189
191
|
post?: v.InferOutput<typeof PostStackSchema>;
|
|
190
192
|
sky?: LookSky;
|
|
191
193
|
light?: LookLight;
|
|
194
|
+
details?: LookDetail[];
|
|
192
195
|
grade: { exposure: number; saturation: number; contrast: number; lut: string | null; bloom?: LookBloom; tone?: LookTone; vignette?: LookVignette };
|
|
193
196
|
clock: 'engine';
|
|
194
197
|
day?: v.InferOutput<typeof day> | undefined;
|
|
@@ -304,6 +307,7 @@ export function shardfileRules(s: Shardfile): string[] {
|
|
|
304
307
|
try { validatePropsReferences(s.props, s); } catch { errors.push('declared prop references'); }
|
|
305
308
|
if (!materialExists(s.look.materials, s.props.family)) errors.push('declared prop material');
|
|
306
309
|
}
|
|
310
|
+
errors.push(...detailLayerRules(s.look.details, s.look.materials, s.files));
|
|
307
311
|
if (Math.abs(s.spawn.x) > CHUNK_HALF || Math.abs(s.spawn.z) > CHUNK_HALF || s.spawn.y < -CELL_BELOW || s.spawn.y > CELL_ABOVE) errors.push('player spawn in cell');
|
|
308
312
|
const conditions = new Set(s.hooks.conditions.map((row) => row.id)), scenes = new Set(s.hooks.scenes.map((row) => row.id));
|
|
309
313
|
const inputConditions = [...(s.plumbing?.input.flatMap(row => [row.when, row.hiddenWhen]) ?? []), ...s.items.contexts.map(row => row.extra?.hiddenWhen)];
|
|
@@ -397,6 +401,7 @@ interface ShardfileRowsInputFields {
|
|
|
397
401
|
post?: v.InferInput<typeof PostStackSchema>;
|
|
398
402
|
sky?: LookSkyInput;
|
|
399
403
|
light?: LookLightInput;
|
|
404
|
+
details?: LookDetail[];
|
|
400
405
|
grade: {
|
|
401
406
|
exposure: v.InferInput<typeof finite>;
|
|
402
407
|
saturation: v.InferInput<typeof finite>;
|
|
@@ -474,6 +479,7 @@ interface ShardfileRowsFields {
|
|
|
474
479
|
post?: v.InferOutput<typeof PostStackSchema>;
|
|
475
480
|
sky?: LookSky;
|
|
476
481
|
light?: LookLight;
|
|
482
|
+
details?: LookDetail[];
|
|
477
483
|
grade: {
|
|
478
484
|
exposure: v.InferOutput<typeof finite>;
|
|
479
485
|
saturation: v.InferOutput<typeof finite>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Validator output rows retain the format's mutable public data types.
|
|
2
|
+
/* oxlint-disable typescript/prefer-readonly-parameter-types */
|
|
3
|
+
import { parse, safeParse } from 'valibot'
|
|
4
|
+
|
|
5
|
+
import { parseSkinBindings, parseSkinRows } from './skinData.ts'
|
|
6
|
+
import {
|
|
7
|
+
SKIN_LOOK_RECIPE,
|
|
8
|
+
SKIN_CLIPS_RECIPE,
|
|
9
|
+
SkinLookParametersSchema,
|
|
10
|
+
SkinClipParametersSchema,
|
|
11
|
+
ShardSkinFileSchema,
|
|
12
|
+
} from './skins.ts'
|
|
13
|
+
|
|
14
|
+
import type { ShardRows } from './rows.ts'
|
|
15
|
+
import type { SkinBinding, SkinRow } from './skinData.ts'
|
|
16
|
+
import type { SkinLookParameters } from './skins.ts'
|
|
17
|
+
|
|
18
|
+
/** Parse the exact skin look and animation parameters. */
|
|
19
|
+
function skinLookParameters(
|
|
20
|
+
row: ShardRows['looks'][number],
|
|
21
|
+
): SkinLookParameters {
|
|
22
|
+
if (row.recipe !== SKIN_LOOK_RECIPE) {
|
|
23
|
+
throw new Error(`look ${row.id}: not a skin look`)
|
|
24
|
+
}
|
|
25
|
+
if (row.animation.recipe !== SKIN_CLIPS_RECIPE) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`look ${row.id}: a skin look animates with ${SKIN_CLIPS_RECIPE}`,
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
const look = safeParse(SkinLookParametersSchema, row.parameters)
|
|
31
|
+
const clips = safeParse(SkinClipParametersSchema, row.animation.parameters)
|
|
32
|
+
if (!look.success || !clips.success) {
|
|
33
|
+
throw new Error(`look ${row.id}: invalid skin parameters`)
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
skin: look.output.skin,
|
|
37
|
+
attackSpan: clips.output.attackSpan,
|
|
38
|
+
...(look.output.variants === undefined
|
|
39
|
+
? {}
|
|
40
|
+
: { variants: look.output.variants }),
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Decode the rig row and binding together. */
|
|
45
|
+
function parseSkinFile(bytes: Uint8Array): {
|
|
46
|
+
row: SkinRow
|
|
47
|
+
binding: SkinBinding
|
|
48
|
+
} {
|
|
49
|
+
const file = parse(
|
|
50
|
+
ShardSkinFileSchema,
|
|
51
|
+
JSON.parse(new TextDecoder().decode(bytes)),
|
|
52
|
+
)
|
|
53
|
+
const [row] = parseSkinRows([file.row])
|
|
54
|
+
if (row === undefined) {
|
|
55
|
+
throw new Error('skin file: no row')
|
|
56
|
+
}
|
|
57
|
+
const [binding] = parseSkinBindings([file.binding], [row])
|
|
58
|
+
if (binding === undefined) {
|
|
59
|
+
throw new Error('skin file: no binding')
|
|
60
|
+
}
|
|
61
|
+
return { row, binding }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { parseSkinFile, skinLookParameters }
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Validator output rows deliberately retain the format's mutable public data types.
|
|
2
|
+
/* oxlint-disable typescript/prefer-readonly-parameter-types */
|
|
3
|
+
// The portable library targets ES2022; sort only new arrays.
|
|
4
|
+
/* oxlint-disable unicorn/no-array-sort */
|
|
5
|
+
import { parse } from 'valibot'
|
|
6
|
+
|
|
7
|
+
import { parseSkinFile, skinLookParameters } from './skin-file.ts'
|
|
8
|
+
import { SkinVariantsSchema } from './skins.ts'
|
|
9
|
+
import { validationSourceBytes } from './validationReceipt.ts'
|
|
10
|
+
|
|
11
|
+
import type { ShardRows } from './rows.ts'
|
|
12
|
+
import type { Shardfile } from './shardSchema.ts'
|
|
13
|
+
import type { SkinBinding, SkinRow } from './skinData.ts'
|
|
14
|
+
|
|
15
|
+
interface ResolvedSkinFile {
|
|
16
|
+
readonly hash: string
|
|
17
|
+
readonly row: SkinRow
|
|
18
|
+
readonly binding: SkinBinding
|
|
19
|
+
}
|
|
20
|
+
interface SkinLookFiles {
|
|
21
|
+
readonly skin: ResolvedSkinFile
|
|
22
|
+
readonly variants?: ReadonlyMap<string, ResolvedSkinFile>
|
|
23
|
+
}
|
|
24
|
+
type Source = Pick<Shardfile, 'rows' | 'files'>
|
|
25
|
+
type Look = ShardRows['looks'][number]
|
|
26
|
+
const FIRST = 0
|
|
27
|
+
const SINGLE = 1
|
|
28
|
+
|
|
29
|
+
function skinContract({ row, binding }: ResolvedSkinFile): string {
|
|
30
|
+
return new TextDecoder().decode(
|
|
31
|
+
validationSourceBytes({
|
|
32
|
+
family: row.family,
|
|
33
|
+
skeleton: row.rig.skeleton,
|
|
34
|
+
joints: [...(row.rig.joints[FIRST] ?? [])].sort(),
|
|
35
|
+
clips: [...row.rig.clips].sort(),
|
|
36
|
+
sockets: [...row.rig.sockets].sort(),
|
|
37
|
+
material: binding.material,
|
|
38
|
+
materials: binding.materials ?? {},
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readSkinFile(
|
|
44
|
+
files: ReadonlyMap<string, Source['files'][number]>,
|
|
45
|
+
assets: ReadonlyMap<string, Uint8Array>,
|
|
46
|
+
hash: string,
|
|
47
|
+
): ResolvedSkinFile {
|
|
48
|
+
const file = files.get(hash)
|
|
49
|
+
const bytes = assets.get(hash)
|
|
50
|
+
if (file?.kind !== 'json' || bytes === undefined) {
|
|
51
|
+
throw new Error('skin file missing')
|
|
52
|
+
}
|
|
53
|
+
const skin = { hash, ...parseSkinFile(bytes) }
|
|
54
|
+
if (
|
|
55
|
+
file.dependencies.length !== SINGLE ||
|
|
56
|
+
file.dependencies[FIRST] !== skin.row.file ||
|
|
57
|
+
files.get(skin.row.file)?.kind !== 'glb'
|
|
58
|
+
) {
|
|
59
|
+
throw new Error('skin rig dependency mismatch')
|
|
60
|
+
}
|
|
61
|
+
return skin
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function skinReader(
|
|
65
|
+
source: Source,
|
|
66
|
+
assets: ReadonlyMap<string, Uint8Array>,
|
|
67
|
+
): (hash: string) => ResolvedSkinFile {
|
|
68
|
+
const files = new Map(
|
|
69
|
+
source.files.map((file): [string, Source['files'][number]] => [
|
|
70
|
+
file.hash,
|
|
71
|
+
file,
|
|
72
|
+
]),
|
|
73
|
+
)
|
|
74
|
+
const parsed = new Map<string, ResolvedSkinFile>()
|
|
75
|
+
return (hash: string): ResolvedSkinFile => {
|
|
76
|
+
const known = parsed.get(hash)
|
|
77
|
+
if (known !== undefined) {
|
|
78
|
+
return known
|
|
79
|
+
}
|
|
80
|
+
const skin = readSkinFile(files, assets, hash)
|
|
81
|
+
parsed.set(hash, skin)
|
|
82
|
+
return skin
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function checkCoverage(
|
|
87
|
+
skins: Readonly<Record<string, string>>,
|
|
88
|
+
ids: readonly string[],
|
|
89
|
+
): void {
|
|
90
|
+
const expected = new Set(ids)
|
|
91
|
+
const keys = Object.keys(skins)
|
|
92
|
+
if (
|
|
93
|
+
keys.length !== expected.size ||
|
|
94
|
+
keys.some((key): boolean => !expected.has(key))
|
|
95
|
+
) {
|
|
96
|
+
throw new Error('variant table must name every species variant exactly')
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function checkDependencies(
|
|
101
|
+
skins: Readonly<Record<string, string>>,
|
|
102
|
+
actual: readonly string[],
|
|
103
|
+
defaultHash: string,
|
|
104
|
+
): void {
|
|
105
|
+
const dependencies = [...new Set(Object.values(skins))].sort()
|
|
106
|
+
if (
|
|
107
|
+
dependencies.length !== actual.length ||
|
|
108
|
+
[...actual]
|
|
109
|
+
.sort()
|
|
110
|
+
.some((hash, index): boolean => hash !== dependencies[index])
|
|
111
|
+
) {
|
|
112
|
+
throw new Error('variant table dependencies differ')
|
|
113
|
+
}
|
|
114
|
+
if (!dependencies.includes(defaultHash)) {
|
|
115
|
+
throw new Error('variant table must include its default skin')
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function variantTable(
|
|
120
|
+
source: Source,
|
|
121
|
+
look: Look,
|
|
122
|
+
{
|
|
123
|
+
assets,
|
|
124
|
+
parameters,
|
|
125
|
+
}: {
|
|
126
|
+
assets: ReadonlyMap<string, Uint8Array>
|
|
127
|
+
parameters: { variants: string; skin: string }
|
|
128
|
+
},
|
|
129
|
+
): Readonly<Record<string, string>> {
|
|
130
|
+
const table = source.files.find(
|
|
131
|
+
(file): boolean => file.hash === parameters.variants,
|
|
132
|
+
)
|
|
133
|
+
const bytes = assets.get(parameters.variants)
|
|
134
|
+
if (table?.kind !== 'json' || bytes === undefined) {
|
|
135
|
+
throw new Error(`skin look ${look.id}: variant table missing`)
|
|
136
|
+
}
|
|
137
|
+
const variants = parse(
|
|
138
|
+
SkinVariantsSchema,
|
|
139
|
+
JSON.parse(new TextDecoder().decode(bytes)),
|
|
140
|
+
)
|
|
141
|
+
const species = source.rows.species.find(
|
|
142
|
+
(row): boolean => row.id === look.species,
|
|
143
|
+
)
|
|
144
|
+
checkCoverage(
|
|
145
|
+
variants.skins,
|
|
146
|
+
(species?.variants ?? []).map((row): string => row.id),
|
|
147
|
+
)
|
|
148
|
+
checkDependencies(variants.skins, table.dependencies, parameters.skin)
|
|
149
|
+
return variants.skins
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function resolveVariants(
|
|
153
|
+
table: Readonly<Record<string, string>>,
|
|
154
|
+
skin: ResolvedSkinFile,
|
|
155
|
+
read: (hash: string) => ResolvedSkinFile,
|
|
156
|
+
): ReadonlyMap<string, ResolvedSkinFile> {
|
|
157
|
+
const contract = skinContract(skin)
|
|
158
|
+
const variants = new Map<string, ResolvedSkinFile>()
|
|
159
|
+
for (const [variant, hash] of Object.entries(table)) {
|
|
160
|
+
const value = read(hash)
|
|
161
|
+
if (skinContract(value) !== contract) {
|
|
162
|
+
throw new Error(`incompatible variant rig or material: ${variant}`)
|
|
163
|
+
}
|
|
164
|
+
variants.set(variant, value)
|
|
165
|
+
}
|
|
166
|
+
return variants
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Resolve exact variant coverage and compatible rig/material declarations once at admission/load. */
|
|
170
|
+
function resolveSkinLookFiles(
|
|
171
|
+
source: Source,
|
|
172
|
+
look: Look,
|
|
173
|
+
assets: ReadonlyMap<string, Uint8Array>,
|
|
174
|
+
): SkinLookFiles {
|
|
175
|
+
const parameters = skinLookParameters(look)
|
|
176
|
+
const read = skinReader(source, assets)
|
|
177
|
+
const skin = read(parameters.skin)
|
|
178
|
+
if (parameters.variants === undefined) {
|
|
179
|
+
return { skin }
|
|
180
|
+
}
|
|
181
|
+
const table = variantTable(source, look, {
|
|
182
|
+
assets,
|
|
183
|
+
parameters: { variants: parameters.variants, skin: parameters.skin },
|
|
184
|
+
})
|
|
185
|
+
const variants = resolveVariants(table, skin, read)
|
|
186
|
+
return { skin, variants }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { resolveSkinLookFiles }
|
|
190
|
+
export type { ResolvedSkinFile, SkinLookFiles }
|