pixelkiln 0.140.0 → 0.141.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/dist/cli.js CHANGED
@@ -3522,6 +3522,20 @@ var AssetSchema = z.object({
3522
3522
  });
3523
3523
  var DEFAULT_HISTORY_LIMIT = 5;
3524
3524
  var HistoryLimitSchema = z.number().int().min(0).max(100);
3525
+ var ParallaxRunSpriteSchema = z.object({
3526
+ asset: z.string().min(1).optional(),
3527
+ /** The style the asset was generated under, when it was generated under more than one. */
3528
+ style: z.string().min(1).optional(),
3529
+ file: z.string().min(1).optional()
3530
+ }).strict().superRefine((sprite, ctx) => {
3531
+ if (sprite.asset === void 0 === (sprite.file === void 0)) {
3532
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: "a run sprite needs exactly one of asset or file" });
3533
+ }
3534
+ if (sprite.style !== void 0 && sprite.asset === void 0) {
3535
+ ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["style"], message: "style only applies to an asset sprite" });
3536
+ }
3537
+ });
3538
+ var RUN_ONLY = ["seed", "step", "gap", "jitter", "width"];
3525
3539
  var ParallaxLayerSchema = z.object({
3526
3540
  /** Names the layer in exports; defaults to the asset id, the file name, or "sky". */
3527
3541
  name: z.string().regex(/^[A-Za-z][A-Za-z0-9_-]*$/, "letters, digits, - and _, starting with a letter").optional(),
@@ -3533,6 +3547,22 @@ var ParallaxLayerSchema = z.object({
3533
3547
  file: z.string().min(1).optional(),
3534
3548
  /** Flat colour bands, top to bottom, with a dithered step between neighbours. */
3535
3549
  sky: z.array(HexColorSchema).min(1).max(64).optional(),
3550
+ /**
3551
+ * Isolated silhouettes laid left to right in an overlapping run, each
3552
+ * flipped at random and never the same one twice in a row, so a long
3553
+ * level never shows the same stretch twice.
3554
+ */
3555
+ run: z.array(ParallaxRunSpriteSchema).min(1).max(32).optional(),
3556
+ /** Seeds a run's layout; the same seed lays the same run. Defaults to one taken from the layer's name. */
3557
+ seed: z.number().int().min(0).max(2 ** 32 - 1).optional(),
3558
+ /** How far across one sprite the next starts, as a fraction of its width; 0.72 by default. */
3559
+ step: z.number().min(0.1).max(2).optional(),
3560
+ /** Up to this many more pixels between sprites, at random; 16 by default. */
3561
+ gap: z.number().int().min(0).max(4096).optional(),
3562
+ /** Moves each sprite's foot up or down by up to this many pixels; 0 by default. */
3563
+ jitter: z.number().int().min(0).max(256).optional(),
3564
+ /** The run's length in pixels; by default as much as the camera sees of the layer. */
3565
+ width: z.number().int().min(1).max(1e5).optional(),
3536
3566
  speed: z.number().min(0).max(4),
3537
3567
  /** Vertical speed; 1, moving with the level, when the set does not climb. */
3538
3568
  speedY: z.number().min(0).max(4).optional(),
@@ -3551,9 +3581,14 @@ var ParallaxLayerSchema = z.object({
3551
3581
  /** Repeat vertically, for an endless climb. */
3552
3582
  repeatY: z.boolean().optional()
3553
3583
  }).strict().superRefine((layer, ctx) => {
3554
- const sources = [layer.asset, layer.file, layer.sky].filter((value) => value !== void 0).length;
3584
+ const sources = [layer.asset, layer.file, layer.sky, layer.run].filter((value) => value !== void 0).length;
3555
3585
  if (sources !== 1) {
3556
- ctx.addIssue({ code: z.ZodIssueCode.custom, message: "a layer needs exactly one of asset, file, or sky" });
3586
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: "a layer needs exactly one of asset, file, sky, or run" });
3587
+ }
3588
+ if (layer.run === void 0) {
3589
+ for (const key3 of RUN_ONLY) {
3590
+ if (layer[key3] !== void 0) ctx.addIssue({ code: z.ZodIssueCode.custom, path: [key3], message: `${key3} only applies to a run layer` });
3591
+ }
3557
3592
  }
3558
3593
  if (layer.style !== void 0 && layer.asset === void 0) {
3559
3594
  ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["style"], message: "style only applies to an asset layer" });
@@ -16587,15 +16622,18 @@ ${issues}`);
16587
16622
  const names = /* @__PURE__ */ new Set();
16588
16623
  set.layers.forEach((layer, index) => {
16589
16624
  const at2 = `parallax.${setId}.layers.${index}`;
16590
- if (layer.asset !== void 0) {
16591
- const asset = parsed.data.assets[layer.asset];
16592
- if (!asset) unknownReferences.push(`${at2}.asset: unknown asset "${layer.asset}"`);
16593
- else if (layer.style !== void 0 && !asset.styles.includes(layer.style)) {
16594
- unknownReferences.push(`${at2}.style: asset "${layer.asset}" is not generated under style "${layer.style}"`);
16595
- } else if (layer.style === void 0 && asset.styles.length > 1) {
16596
- unknownReferences.push(`${at2}.style: asset "${layer.asset}" has ${asset.styles.length} styles; name one`);
16625
+ const checkAsset = (where, ref) => {
16626
+ if (ref.asset === void 0) return;
16627
+ const asset = parsed.data.assets[ref.asset];
16628
+ if (!asset) unknownReferences.push(`${where}.asset: unknown asset "${ref.asset}"`);
16629
+ else if (ref.style !== void 0 && !asset.styles.includes(ref.style)) {
16630
+ unknownReferences.push(`${where}.style: asset "${ref.asset}" is not generated under style "${ref.style}"`);
16631
+ } else if (ref.style === void 0 && asset.styles.length > 1) {
16632
+ unknownReferences.push(`${where}.style: asset "${ref.asset}" has ${asset.styles.length} styles; name one`);
16597
16633
  }
16598
- }
16634
+ };
16635
+ checkAsset(at2, layer);
16636
+ layer.run?.forEach((sprite, n) => checkAsset(`${at2}.run.${n}`, sprite));
16599
16637
  if (layer.name !== void 0) {
16600
16638
  if (names.has(layer.name)) unknownReferences.push(`${at2}.name: "${layer.name}" is used twice`);
16601
16639
  names.add(layer.name);
@@ -35595,6 +35633,11 @@ import { readFile as readFile50 } from "fs/promises";
35595
35633
  import path73 from "path";
35596
35634
  var PARALLAX_FORMATS = ["tiled", "godot", "ldtk"];
35597
35635
  var SEAM_WARN_RATIO = 2;
35636
+ function layerFiles(layer) {
35637
+ if (layer.source.kind === "sky") return [];
35638
+ if (layer.source.kind === "run") return layer.source.sprites.map((sprite) => sprite.path);
35639
+ return [layer.source.path];
35640
+ }
35598
35641
  var BAYER4 = [
35599
35642
  [0, 8, 2, 10],
35600
35643
  [12, 4, 14, 6],
@@ -35656,7 +35699,7 @@ function seamRatio(png) {
35656
35699
  }
35657
35700
  function layerName(layer, index) {
35658
35701
  if (layer.name) return layer.name;
35659
- const base = layer.asset ?? (layer.file ? path73.basename(layer.file, path73.extname(layer.file)) : "sky");
35702
+ const base = layer.asset ?? (layer.file ? path73.basename(layer.file, path73.extname(layer.file)) : layer.run ? "run" : "sky");
35660
35703
  const slug3 = base.replace(/[^A-Za-z0-9_-]+/g, "_").replace(/^[^A-Za-z]+/, "");
35661
35704
  return slug3 || `layer${index + 1}`;
35662
35705
  }
@@ -35682,24 +35725,20 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35682
35725
  if (layer.sky) {
35683
35726
  png = skyBands(viewW, viewH + Math.ceil(climb * speedY), layer.sky);
35684
35727
  source = { kind: "sky", colors: layer.sky };
35685
- } else if (layer.file) {
35686
- const file = path73.resolve(manifestDir, layer.file);
35687
- png = decodePng(await readFile50(file));
35688
- source = { kind: "file", path: file };
35728
+ } else if (layer.run) {
35729
+ const sprites = await Promise.all(layer.run.map((sprite) => readPicture(manifest, manifestDir, lock, setId, name, sprite, warnings)));
35730
+ const seed = layer.seed ?? seedFromName(name);
35731
+ const loop = layer.repeat ?? true;
35732
+ const width = layer.width ?? Math.max(1, viewW + Math.ceil(travel * layer.speed) - (loop ? 0 : layer.x ?? 0));
35733
+ png = layRun(sprites.map((sprite) => sprite.png), width, { seed, step: layer.step, gap: layer.gap, jitter: layer.jitter, loop });
35734
+ source = { kind: "run", seed, sprites: sprites.map((sprite) => sprite.source) };
35689
35735
  } else {
35690
- const assetId = layer.asset;
35691
- const style = layer.style ?? manifest.assets[assetId].styles[0];
35692
- const entry = lock.entries[lockKey(style, assetId)];
35693
- if (!entry) throw new Error(`parallax ${setId}: ${style}/${assetId} has not been generated; run gen first`);
35694
- const output = entry.outputs.find((candidate) => /\.png$/i.test(candidate.path)) ?? entry.outputs[0];
35695
- if (!output) throw new Error(`parallax ${setId}: ${style}/${assetId} has no output in the lockfile`);
35696
- const file = path73.resolve(manifestDir, output.path);
35697
- png = decodePng(await readFile50(file));
35698
- source = { kind: "asset", style, asset: assetId, path: file };
35699
- if (entry.outputs.length > 1) warnings.push(`${name}: ${style}/${assetId} has ${entry.outputs.length} outputs; using ${output.path}`);
35736
+ const picture = await readPicture(manifest, manifestDir, lock, setId, name, layer, warnings);
35737
+ png = picture.png;
35738
+ source = picture.source;
35700
35739
  }
35701
35740
  const repeat = layer.repeat ?? true;
35702
- let bottom = layer.bottom ?? viewH;
35741
+ let bottom = (layer.bottom ?? viewH) + (layer.run ? layer.jitter ?? 0 : 0);
35703
35742
  if (layer.extend) {
35704
35743
  const rows = layer.extend === true ? Math.max(0, viewH - bottom) : layer.extend;
35705
35744
  png = extendDown(png, rows);
@@ -35716,7 +35755,7 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35716
35755
  repeatY: layer.repeatY ?? false,
35717
35756
  source
35718
35757
  };
35719
- if (repeat && source.kind !== "sky") {
35758
+ if (repeat && source.kind !== "sky" && source.kind !== "run") {
35720
35759
  image.seamRatio = seamRatio(png);
35721
35760
  if (travel * layer.speed > 0 && image.seamRatio > SEAM_WARN_RATIO) {
35722
35761
  warnings.push(
@@ -35746,6 +35785,77 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35746
35785
  }
35747
35786
  return { id: setId, view: [viewW, viewH], travel, climb, layers, warnings };
35748
35787
  }
35788
+ async function readPicture(manifest, manifestDir, lock, setId, name, ref, warnings) {
35789
+ if (ref.file) {
35790
+ const file2 = path73.resolve(manifestDir, ref.file);
35791
+ return { png: decodePng(await readFile50(file2)), source: { kind: "file", path: file2 } };
35792
+ }
35793
+ const assetId = ref.asset;
35794
+ const style = ref.style ?? manifest.assets[assetId].styles[0];
35795
+ const entry = lock.entries[lockKey(style, assetId)];
35796
+ if (!entry) throw new Error(`parallax ${setId}: ${style}/${assetId} has not been generated; run gen first`);
35797
+ const output = entry.outputs.find((candidate) => /\.png$/i.test(candidate.path)) ?? entry.outputs[0];
35798
+ if (!output) throw new Error(`parallax ${setId}: ${style}/${assetId} has no output in the lockfile`);
35799
+ const file = path73.resolve(manifestDir, output.path);
35800
+ if (entry.outputs.length > 1) warnings.push(`${name}: ${style}/${assetId} has ${entry.outputs.length} outputs; using ${output.path}`);
35801
+ return { png: decodePng(await readFile50(file)), source: { kind: "asset", style, asset: assetId, path: file } };
35802
+ }
35803
+ function seedFromName(name) {
35804
+ return createHash9("sha256").update(name).digest().readUInt32BE(0);
35805
+ }
35806
+ function seededRandom(seed) {
35807
+ let a = seed >>> 0;
35808
+ return () => {
35809
+ a = a + 1831565813 >>> 0;
35810
+ let t = a;
35811
+ t = Math.imul(t ^ t >>> 15, t | 1);
35812
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
35813
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
35814
+ };
35815
+ }
35816
+ function layRun(sprites, width, options) {
35817
+ const step = options.step ?? 0.72;
35818
+ const gap = options.gap ?? 16;
35819
+ const jitter = options.jitter ?? 0;
35820
+ const random = seededRandom(options.seed);
35821
+ const tallest = Math.max(...sprites.map((sprite) => sprite.height));
35822
+ const height = tallest + 2 * jitter;
35823
+ if (options.loop) {
35824
+ const widest = Math.max(...sprites.map((sprite) => sprite.width));
35825
+ if (width < widest) throw new Error(`a looping run must be at least as wide as its widest sprite (${widest} px); it is ${width}`);
35826
+ }
35827
+ const placed = [];
35828
+ let x = options.loop ? 0 : -Math.floor(random() * 32);
35829
+ let last = -1;
35830
+ while (x < width) {
35831
+ let pick = Math.floor(random() * sprites.length);
35832
+ if (sprites.length > 1 && pick === last) pick = (pick + 1) % sprites.length;
35833
+ last = pick;
35834
+ const sprite = sprites[pick];
35835
+ const flip = random() < 0.5;
35836
+ const shift = jitter ? Math.floor(random() * (2 * jitter + 1)) - jitter : 0;
35837
+ placed.push({ sprite: pick, flip, x, y: height - jitter - sprite.height + shift });
35838
+ x += Math.max(1, Math.floor(sprite.width * step + random() * gap));
35839
+ }
35840
+ const copies = options.loop ? [-width, 0, width] : [0];
35841
+ const stamps = placed.flatMap((p) => copies.map((dx) => ({ ...p, x: p.x + dx }))).sort((a, b) => a.x - b.x);
35842
+ const pixels = Buffer.alloc(width * height * 4);
35843
+ for (const p of stamps) {
35844
+ const sprite = sprites[p.sprite];
35845
+ for (let sy = 0; sy < sprite.height; sy++) {
35846
+ const y = p.y + sy;
35847
+ if (y < 0 || y >= height) continue;
35848
+ for (let sx = 0; sx < sprite.width; sx++) {
35849
+ const tx = p.x + sx;
35850
+ if (tx < 0 || tx >= width) continue;
35851
+ const from = (sy * sprite.width + (p.flip ? sprite.width - 1 - sx : sx)) * 4;
35852
+ if (sprite.pixels[from + 3] === 0) continue;
35853
+ sprite.pixels.copy(pixels, (y * width + tx) * 4, from, from + 4);
35854
+ }
35855
+ }
35856
+ }
35857
+ return { width, height, pixels };
35858
+ }
35749
35859
  function extendDown(png, rows) {
35750
35860
  if (rows <= 0) return png;
35751
35861
  const height = png.height + rows;
@@ -36240,7 +36350,9 @@ async function runParallax(args) {
36240
36350
  await provenanceFile("$manifest", args.manifest),
36241
36351
  await provenanceFile("$lock", args.lock),
36242
36352
  ...await Promise.all(
36243
- set.layers.filter((layer) => layer.source.kind !== "sky").map((layer) => provenanceFile(`layer:${layer.name}`, layer.source.path))
36353
+ set.layers.flatMap(
36354
+ (layer) => layerFiles(layer).map((file, n, all) => provenanceFile(all.length > 1 || layer.source.kind === "run" ? `layer:${layer.name}:${n + 1}` : `layer:${layer.name}`, file))
36355
+ )
36244
36356
  )
36245
36357
  ];
36246
36358
  const describe2 = setOptions(set);
@@ -36297,7 +36409,8 @@ function setOptions(set) {
36297
36409
  y: layer.y,
36298
36410
  repeat: layer.repeat,
36299
36411
  repeatY: layer.repeatY,
36300
- ...layer.source.kind === "sky" ? { sky: layer.source.colors } : {}
36412
+ ...layer.source.kind === "sky" ? { sky: layer.source.colors } : {},
36413
+ ...layer.source.kind === "run" ? { run: { seed: layer.source.seed, sprites: layer.source.sprites.length, width: layer.png.width } } : {}
36301
36414
  }))
36302
36415
  };
36303
36416
  }