BlueGit

timfa questward

Add sand ground with wet shoreline blend toward liquid water.

Bake sand→sand-wet within the outer 30% facing water (including diagonals). In winter only deep water counts, since shallow water is ice. Soft-terrain blending still applies; darkness stays on the lit atlas path.

timfa Tim Falken · pushed by timfa

Authored 2026-09-03 · 2 months ago

Hash
83d6f12f8238be915870b3735f527d8bcfe7bb6f
Parent
8a83b21b
9 files changed +444 −37
client/src/render/chunkBake.js +98 −32
@@ -1,5 +1,13 @@
1 1 import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2 import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock } from "/shared/definitions/tiles.js";
2 import {
3 GROUND_DEFINITIONS,
4 BLOCK_DEFINITIONS,
5 GroundType,
6 normalizeGround,
7 normalizeBlock,
8 isChunkAtlasBakedBlock,
9 isLiquidWaterGround
10 } from "/shared/definitions/tiles.js";
3 11 import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js";
4 12 import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js";
5 13 import {
@@ -8,6 +16,11 @@ import {
8 16 softTerrainNeighborSample,
9 17 mixSoftTerrainPixel
10 18 } from "/shared/render/softTerrain.js";
19 import {
20 WET_SAND_NEIGHBOR_DIRS,
21 wetSandWetness,
22 mixWetSandPixel
23 } from "/shared/render/wetSand.js";
11 24 import { computeChunkAtlasKeys } from "/shared/render/chunkAtlasKeys.js";
12 25
13 26 /**
@@ -188,10 +201,10 @@ function bakeGroundCanvas ({
188 201 const width = CHUNK_SIZE * SPRITE_SIZE_PX;
189 202 const height = CHUNK_SIZE * SPRITE_SIZE_PX;
190 203 const ground = makeCanvas(width, height);
191 const groundCtx = ground.getContext("2d", { willReadFrequently: softTerrain });
204 const groundCtx = ground.getContext("2d", { willReadFrequently: true });
192 205 groundCtx.imageSmoothingEnabled = false;
193 206 groundCtx.clearRect(0, 0, width, height);
194 const pixelCache = softTerrain ? new Map() : null;
207 const pixelCache = new Map();
195 208 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
196 209 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
197 210
@@ -208,10 +221,11 @@ function bakeGroundCanvas ({
208 221 const groundMeta = groundCatalogMeta(catalog, groundEntry);
209 222 if (!isBakeable(images[groundName], groundMeta))
210 223 { continue; }
224 const wetShore = isWetShoreGround(groundEntry);
211 225 const blendable = softTerrain && isGroundBlendable(groundEntry);
212 if (blendable)
226 if (wetShore || blendable)
213 227 {
214 blitSoftGround(
228 blitBlendedGround(
215 229 groundCtx,
216 230 images,
217 231 catalog,
@@ -223,7 +237,8 @@ function bakeGroundCanvas ({
223 237 tileY,
224 238 season,
225 239 resolveGround,
226 pixelCache
240 pixelCache,
241 { softTerrain: blendable, wetShore }
227 242 );
228 243 }
229 244 else
@@ -380,6 +395,25 @@ function isGroundBlendable (ground)
380 395 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
381 396 }
382 397
398 function isWetShoreGround (ground)
399 {
400 const entry = normalizeGround(ground);
401 return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore;
402 }
403
404 function wetShoreSpriteName (catalog, ground)
405 {
406 const entry = normalizeGround(ground);
407 const def = GROUND_DEFINITIONS[entry.typeId];
408 if (!def?.wetShore)
409 { return null; }
410 const wetState = `${def.key}-wet`;
411 const states = catalog.grounds?.[def.key]?.states ?? [];
412 if (!states.includes(wetState))
413 { return null; }
414 return spriteImageName("ground", def.key, wetState);
415 }
416
383 417 function groundVisualKey (catalog, ground, season)
384 418 {
385 419 const entry = normalizeGround(ground);
@@ -389,7 +423,7 @@ function groundVisualKey (catalog, ground, season)
389 423 return `${entry.typeId}:${state ?? ""}`;
390 424 }
391 425
392 function blitSoftGround (
426 function blitBlendedGround (
393 427 ctx,
394 428 images,
395 429 catalog,
@@ -401,42 +435,68 @@ function blitSoftGround (
401 435 tileY,
402 436 season,
403 437 resolveGround,
404 pixelCache
438 pixelCache,
439 { softTerrain = false, wetShore = false } = {}
405 440 )
406 441 {
407 const selfPixels = spritePixels(images[groundName], pixelCache);
408 if (!selfPixels)
442 const dryPixels = spritePixels(images[groundName], pixelCache);
443 if (!dryPixels)
409 444 {
410 445 blit(ctx, images[groundName], dx, dy);
411 446 return;
412 447 }
413 448
449 const waterDirs = [];
450 if (wetShore)
451 {
452 for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS)
453 {
454 const neighborGround = resolveNeighborGround(
455 tileX + ndx,
456 tileY + ndy,
457 resolveGround
458 );
459 if (neighborGround == null)
460 { continue; }
461 const typeId = normalizeGround(neighborGround).typeId;
462 if (!isLiquidWaterGround(typeId, season))
463 { continue; }
464 waterDirs.push({ ndx, ndy });
465 }
466 }
467
468 const wetName = wetShore && waterDirs.length ? wetShoreSpriteName(catalog, groundEntry) : null;
469 const wetPixels = wetName ? spritePixels(images[wetName], pixelCache) : null;
470
414 471 const selfKey = groundVisualKey(catalog, groundEntry, season);
415 472 const neighborSprites = [];
416 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
473 if (softTerrain)
417 474 {
418 const neighborGround = resolveNeighborGround(
419 tileX + ndx,
420 tileY + ndy,
421 resolveGround
422 );
423 if (neighborGround == null)
424 { continue; }
425 if (groundVisualKey(catalog, neighborGround, season) === selfKey)
426 { continue; }
427 const neighborName = groundSpriteName(catalog, neighborGround, season);
428 const pixels = spritePixels(images[neighborName], pixelCache);
429 if (!pixels)
430 { continue; }
431 neighborSprites.push({
432 ndx,
433 ndy,
434 pixels,
435 neighborBlendable: isGroundBlendable(neighborGround)
436 });
475 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
476 {
477 const neighborGround = resolveNeighborGround(
478 tileX + ndx,
479 tileY + ndy,
480 resolveGround
481 );
482 if (neighborGround == null)
483 { continue; }
484 if (groundVisualKey(catalog, neighborGround, season) === selfKey)
485 { continue; }
486 const neighborName = groundSpriteName(catalog, neighborGround, season);
487 const pixels = spritePixels(images[neighborName], pixelCache);
488 if (!pixels)
489 { continue; }
490 neighborSprites.push({
491 ndx,
492 ndy,
493 pixels,
494 neighborBlendable: isGroundBlendable(neighborGround)
495 });
496 }
437 497 }
438 498
439 if (!neighborSprites.length)
499 if (!waterDirs.length && !neighborSprites.length)
440 500 {
441 501 blit(ctx, images[groundName], dx, dy);
442 502 return;
@@ -448,7 +508,13 @@ function blitSoftGround (
448 508 {
449 509 for (let px = 0; px < SPRITE_SIZE_PX; px++)
450 510 {
451 const self = readPixel(selfPixels, px, py);
511 let self = readPixel(dryPixels, px, py);
512 if (wetPixels && waterDirs.length)
513 {
514 const wetness = wetSandWetness(px, py, waterDirs, SPRITE_SIZE_PX);
515 if (wetness > 0)
516 { self = mixWetSandPixel(self, readPixel(wetPixels, px, py), wetness); }
517 }
452 518 const contribs = [];
453 519 for (const neighbor of neighborSprites)
454 520 {
@@ -1,5 +1,13 @@
1import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
1import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock } from "/shared/definitions/tiles.js";
2import {
3 GROUND_DEFINITIONS,
4 BLOCK_DEFINITIONS,
5 GroundType,
6 normalizeGround,
7 normalizeBlock,
8 isChunkAtlasBakedBlock,
9 isLiquidWaterGround
10} from "/shared/definitions/tiles.js";
3import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js";
11import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js";
4import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js";
12import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js";
5import {
13import {
@@ -8,6 +16,11 @@ import {
8 softTerrainNeighborSample,
16 softTerrainNeighborSample,
9 mixSoftTerrainPixel
17 mixSoftTerrainPixel
10} from "/shared/render/softTerrain.js";
18} from "/shared/render/softTerrain.js";
19import {
20 WET_SAND_NEIGHBOR_DIRS,
21 wetSandWetness,
22 mixWetSandPixel
23} from "/shared/render/wetSand.js";
11import { computeChunkAtlasKeys } from "/shared/render/chunkAtlasKeys.js";
24import { computeChunkAtlasKeys } from "/shared/render/chunkAtlasKeys.js";
12
25
13/**
26/**
@@ -188,10 +201,10 @@ function bakeGroundCanvas ({
188 const width = CHUNK_SIZE * SPRITE_SIZE_PX;
201 const width = CHUNK_SIZE * SPRITE_SIZE_PX;
189 const height = CHUNK_SIZE * SPRITE_SIZE_PX;
202 const height = CHUNK_SIZE * SPRITE_SIZE_PX;
190 const ground = makeCanvas(width, height);
203 const ground = makeCanvas(width, height);
191 const groundCtx = ground.getContext("2d", { willReadFrequently: softTerrain });
204 const groundCtx = ground.getContext("2d", { willReadFrequently: true });
192 groundCtx.imageSmoothingEnabled = false;
205 groundCtx.imageSmoothingEnabled = false;
193 groundCtx.clearRect(0, 0, width, height);
206 groundCtx.clearRect(0, 0, width, height);
194 const pixelCache = softTerrain ? new Map() : null;
207 const pixelCache = new Map();
195 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
208 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
196 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
209 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
197
210
@@ -208,10 +221,11 @@ function bakeGroundCanvas ({
208 const groundMeta = groundCatalogMeta(catalog, groundEntry);
221 const groundMeta = groundCatalogMeta(catalog, groundEntry);
209 if (!isBakeable(images[groundName], groundMeta))
222 if (!isBakeable(images[groundName], groundMeta))
210 { continue; }
223 { continue; }
224 const wetShore = isWetShoreGround(groundEntry);
211 const blendable = softTerrain && isGroundBlendable(groundEntry);
225 const blendable = softTerrain && isGroundBlendable(groundEntry);
212 if (blendable)
226 if (wetShore || blendable)
213 {
227 {
214 blitSoftGround(
228 blitBlendedGround(
215 groundCtx,
229 groundCtx,
216 images,
230 images,
217 catalog,
231 catalog,
@@ -223,7 +237,8 @@ function bakeGroundCanvas ({
223 tileY,
237 tileY,
224 season,
238 season,
225 resolveGround,
239 resolveGround,
226 pixelCache
240 pixelCache,
241 { softTerrain: blendable, wetShore }
227 );
242 );
228 }
243 }
229 else
244 else
@@ -380,6 +395,25 @@ function isGroundBlendable (ground)
380 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
395 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
381}
396}
382
397
398function isWetShoreGround (ground)
399{
400 const entry = normalizeGround(ground);
401 return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore;
402}
403
404function wetShoreSpriteName (catalog, ground)
405{
406 const entry = normalizeGround(ground);
407 const def = GROUND_DEFINITIONS[entry.typeId];
408 if (!def?.wetShore)
409 { return null; }
410 const wetState = `${def.key}-wet`;
411 const states = catalog.grounds?.[def.key]?.states ?? [];
412 if (!states.includes(wetState))
413 { return null; }
414 return spriteImageName("ground", def.key, wetState);
415}
416
383function groundVisualKey (catalog, ground, season)
417function groundVisualKey (catalog, ground, season)
384{
418{
385 const entry = normalizeGround(ground);
419 const entry = normalizeGround(ground);
@@ -389,7 +423,7 @@ function groundVisualKey (catalog, ground, season)
389 return `${entry.typeId}:${state ?? ""}`;
423 return `${entry.typeId}:${state ?? ""}`;
390}
424}
391
425
392function blitSoftGround (
426function blitBlendedGround (
393 ctx,
427 ctx,
394 images,
428 images,
395 catalog,
429 catalog,
@@ -401,42 +435,68 @@ function blitSoftGround (
401 tileY,
435 tileY,
402 season,
436 season,
403 resolveGround,
437 resolveGround,
404 pixelCache
438 pixelCache,
439 { softTerrain = false, wetShore = false } = {}
405)
440)
406{
441{
407 const selfPixels = spritePixels(images[groundName], pixelCache);
442 const dryPixels = spritePixels(images[groundName], pixelCache);
408 if (!selfPixels)
443 if (!dryPixels)
409 {
444 {
410 blit(ctx, images[groundName], dx, dy);
445 blit(ctx, images[groundName], dx, dy);
411 return;
446 return;
412 }
447 }
413
448
449 const waterDirs = [];
450 if (wetShore)
451 {
452 for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS)
453 {
454 const neighborGround = resolveNeighborGround(
455 tileX + ndx,
456 tileY + ndy,
457 resolveGround
458 );
459 if (neighborGround == null)
460 { continue; }
461 const typeId = normalizeGround(neighborGround).typeId;
462 if (!isLiquidWaterGround(typeId, season))
463 { continue; }
464 waterDirs.push({ ndx, ndy });
465 }
466 }
467
468 const wetName = wetShore && waterDirs.length ? wetShoreSpriteName(catalog, groundEntry) : null;
469 const wetPixels = wetName ? spritePixels(images[wetName], pixelCache) : null;
470
414 const selfKey = groundVisualKey(catalog, groundEntry, season);
471 const selfKey = groundVisualKey(catalog, groundEntry, season);
415 const neighborSprites = [];
472 const neighborSprites = [];
416 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
473 if (softTerrain)
417 {
474 {
418 const neighborGround = resolveNeighborGround(
475 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
419 tileX + ndx,
476 {
420 tileY + ndy,
477 const neighborGround = resolveNeighborGround(
421 resolveGround
478 tileX + ndx,
422 );
479 tileY + ndy,
423 if (neighborGround == null)
480 resolveGround
424 { continue; }
481 );
425 if (groundVisualKey(catalog, neighborGround, season) === selfKey)
482 if (neighborGround == null)
426 { continue; }
483 { continue; }
427 const neighborName = groundSpriteName(catalog, neighborGround, season);
484 if (groundVisualKey(catalog, neighborGround, season) === selfKey)
428 const pixels = spritePixels(images[neighborName], pixelCache);
485 { continue; }
429 if (!pixels)
486 const neighborName = groundSpriteName(catalog, neighborGround, season);
430 { continue; }
487 const pixels = spritePixels(images[neighborName], pixelCache);
431 neighborSprites.push({
488 if (!pixels)
432 ndx,
489 { continue; }
433 ndy,
490 neighborSprites.push({
434 pixels,
491 ndx,
435 neighborBlendable: isGroundBlendable(neighborGround)
492 ndy,
436 });
493 pixels,
494 neighborBlendable: isGroundBlendable(neighborGround)
495 });
496 }
437 }
497 }
438
498
439 if (!neighborSprites.length)
499 if (!waterDirs.length && !neighborSprites.length)
440 {
500 {
441 blit(ctx, images[groundName], dx, dy);
501 blit(ctx, images[groundName], dx, dy);
442 return;
502 return;
@@ -448,7 +508,13 @@ function blitSoftGround (
448 {
508 {
449 for (let px = 0; px < SPRITE_SIZE_PX; px++)
509 for (let px = 0; px < SPRITE_SIZE_PX; px++)
450 {
510 {
451 const self = readPixel(selfPixels, px, py);
511 let self = readPixel(dryPixels, px, py);
512 if (wetPixels && waterDirs.length)
513 {
514 const wetness = wetSandWetness(px, py, waterDirs, SPRITE_SIZE_PX);
515 if (wetness > 0)
516 { self = mixWetSandPixel(self, readPixel(wetPixels, px, py), wetness); }
517 }
452 const contribs = [];
518 const contribs = [];
453 for (const neighbor of neighborSprites)
519 for (const neighbor of neighborSprites)
454 {
520 {
client/src/render/worldView.js +2 −1
@@ -3412,7 +3412,8 @@ export class WorldView
3412 3412 {
3413 3413 this.#bakeAndPlaceChunk(snapshot);
3414 3414 this.#relayoutChunkTiles(snapshot);
3415 if (this.softTerrain && refreshSoftNeighbors)
3415 // Soft terrain and wet-sand shorelines both sample cross-chunk neighbors.
3416 if (refreshSoftNeighbors)
3416 3417 { this.#rebuildSoftTerrainNeighborChunks(snapshot); }
3417 3418 }
3418 3419
@@ -3412,7 +3412,8 @@ export class WorldView
3412 {
3412 {
3413 this.#bakeAndPlaceChunk(snapshot);
3413 this.#bakeAndPlaceChunk(snapshot);
3414 this.#relayoutChunkTiles(snapshot);
3414 this.#relayoutChunkTiles(snapshot);
3415 if (this.softTerrain && refreshSoftNeighbors)
3415 // Soft terrain and wet-sand shorelines both sample cross-chunk neighbors.
3416 if (refreshSoftNeighbors)
3416 { this.#rebuildSoftTerrainNeighborChunks(snapshot); }
3417 { this.#rebuildSoftTerrainNeighborChunks(snapshot); }
3417 }
3418 }
3418
3419
server/src/assets/spriteCatalog.js +3 −1
@@ -402,7 +402,9 @@ function scanLayer (layerDir, definitions, layer, urlFolder)
402 402 ? "fishing-spot"
403 403 : def.door && states.includes("pine-s-closed")
404 404 ? "pine-s-closed"
405 : (def.outdoor && preferredSeasonalDefaultState(states)) || states[0],
405 : def.key === "sand" && states.includes("sand")
406 ? "sand"
407 : (def.outdoor && preferredSeasonalDefaultState(states)) || states[0],
406 408 blooms,
407 409 equips,
408 410 equipBlooms,
@@ -402,7 +402,9 @@ function scanLayer (layerDir, definitions, layer, urlFolder)
402 ? "fishing-spot"
402 ? "fishing-spot"
403 : def.door && states.includes("pine-s-closed")
403 : def.door && states.includes("pine-s-closed")
404 ? "pine-s-closed"
404 ? "pine-s-closed"
405 : (def.outdoor && preferredSeasonalDefaultState(states)) || states[0],
405 : def.key === "sand" && states.includes("sand")
406 ? "sand"
407 : (def.outdoor && preferredSeasonalDefaultState(states)) || states[0],
406 blooms,
408 blooms,
407 equips,
409 equips,
408 equipBlooms,
410 equipBlooms,
server/tests/chunk-atlas-keys.test.js +66 −0
@@ -68,3 +68,69 @@ test("soft border fingerprint differs when neighbor ground changes", () =>
68 68 });
69 69 assert.notEqual(flat.groundKey, edgeDirt.groundKey);
70 70 });
71
72 test("wet sand border fingerprint differs for liquid water neighbors", () =>
73 {
74 const catalogSand = {
75 grounds: {
76 sand: { states: ["sand", "sand-wet"] },
77 water: { states: ["summer"] },
78 "deep-water": { states: ["summer"] }
79 },
80 blocks: {}
81 };
82 const snapshot = {
83 chunkX: 1,
84 chunkY: 1,
85 grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })),
86 blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null)
87 };
88 const dry = computeChunkAtlasKeys({
89 snapshot,
90 catalog: catalogSand,
91 season: "summer",
92 softTerrain: false,
93 resolveGround: () => ({ typeId: GroundType.sand })
94 });
95 const besideWater = computeChunkAtlasKeys({
96 snapshot,
97 catalog: catalogSand,
98 season: "summer",
99 softTerrain: false,
100 resolveGround: (tileX, tileY) =>
101 {
102 if (tileX < snapshot.chunkX * CHUNK_SIZE)
103 { return { typeId: GroundType.water }; }
104 return { typeId: GroundType.sand };
105 }
106 });
107 assert.notEqual(dry.groundKey, besideWater.groundKey);
108
109 const winterShallow = computeChunkAtlasKeys({
110 snapshot,
111 catalog: catalogSand,
112 season: "winter",
113 softTerrain: false,
114 resolveGround: (tileX, tileY) =>
115 {
116 if (tileX < snapshot.chunkX * CHUNK_SIZE)
117 { return { typeId: GroundType.water }; }
118 return { typeId: GroundType.sand };
119 }
120 });
121 const winterDeep = computeChunkAtlasKeys({
122 snapshot,
123 catalog: catalogSand,
124 season: "winter",
125 softTerrain: false,
126 resolveGround: (tileX, tileY) =>
127 {
128 if (tileX < snapshot.chunkX * CHUNK_SIZE)
129 { return { typeId: GroundType.deepWater }; }
130 return { typeId: GroundType.sand };
131 }
132 });
133 // Frozen shallow water should not wet the shoreline; deep still does.
134 assert.equal(winterShallow.groundKey.includes("wet:flat"), true);
135 assert.notEqual(winterShallow.groundKey, winterDeep.groundKey);
136 });
@@ -68,3 +68,69 @@ test("soft border fingerprint differs when neighbor ground changes", () =>
68 });
68 });
69 assert.notEqual(flat.groundKey, edgeDirt.groundKey);
69 assert.notEqual(flat.groundKey, edgeDirt.groundKey);
70});
70});
71
72test("wet sand border fingerprint differs for liquid water neighbors", () =>
73{
74 const catalogSand = {
75 grounds: {
76 sand: { states: ["sand", "sand-wet"] },
77 water: { states: ["summer"] },
78 "deep-water": { states: ["summer"] }
79 },
80 blocks: {}
81 };
82 const snapshot = {
83 chunkX: 1,
84 chunkY: 1,
85 grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })),
86 blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null)
87 };
88 const dry = computeChunkAtlasKeys({
89 snapshot,
90 catalog: catalogSand,
91 season: "summer",
92 softTerrain: false,
93 resolveGround: () => ({ typeId: GroundType.sand })
94 });
95 const besideWater = computeChunkAtlasKeys({
96 snapshot,
97 catalog: catalogSand,
98 season: "summer",
99 softTerrain: false,
100 resolveGround: (tileX, tileY) =>
101 {
102 if (tileX < snapshot.chunkX * CHUNK_SIZE)
103 { return { typeId: GroundType.water }; }
104 return { typeId: GroundType.sand };
105 }
106 });
107 assert.notEqual(dry.groundKey, besideWater.groundKey);
108
109 const winterShallow = computeChunkAtlasKeys({
110 snapshot,
111 catalog: catalogSand,
112 season: "winter",
113 softTerrain: false,
114 resolveGround: (tileX, tileY) =>
115 {
116 if (tileX < snapshot.chunkX * CHUNK_SIZE)
117 { return { typeId: GroundType.water }; }
118 return { typeId: GroundType.sand };
119 }
120 });
121 const winterDeep = computeChunkAtlasKeys({
122 snapshot,
123 catalog: catalogSand,
124 season: "winter",
125 softTerrain: false,
126 resolveGround: (tileX, tileY) =>
127 {
128 if (tileX < snapshot.chunkX * CHUNK_SIZE)
129 { return { typeId: GroundType.deepWater }; }
130 return { typeId: GroundType.sand };
131 }
132 });
133 // Frozen shallow water should not wet the shoreline; deep still does.
134 assert.equal(winterShallow.groundKey.includes("wet:flat"), true);
135 assert.notEqual(winterShallow.groundKey, winterDeep.groundKey);
136});
server/tests/softTerrain.test.js +3 −1
@@ -14,7 +14,7 @@ import {
14 14 import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";
15 15 import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";
16 16
17 test("grass, dirt, and path are blendable outdoor grounds", () =>
17 test("grass, dirt, path, and sand are blendable grounds", () =>
18 18 {
19 19 assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);
20 20 assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);
@@ -22,6 +22,8 @@ test("grass, dirt, and path are blendable outdoor grounds", () =>
22 22 assert.equal(GROUND_DEFINITIONS[GroundType.path].blendable, true);
23 23 assert.equal(GROUND_DEFINITIONS[GroundType.path].outdoor, true);
24 24 assert.equal(GROUND_DEFINITIONS[GroundType.path].pathSurface, true);
25 assert.equal(GROUND_DEFINITIONS[GroundType.sand].blendable, true);
26 assert.equal(GROUND_DEFINITIONS[GroundType.sand].wetShore, true);
25 27 assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);
26 28 assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);
27 29 });
@@ -14,7 +14,7 @@ import {
14import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";
14import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";
15import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";
15import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";
16
16
17test("grass, dirt, and path are blendable outdoor grounds", () =>
17test("grass, dirt, path, and sand are blendable grounds", () =>
18{
18{
19 assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);
19 assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);
20 assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);
20 assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);
@@ -22,6 +22,8 @@ test("grass, dirt, and path are blendable outdoor grounds", () =>
22 assert.equal(GROUND_DEFINITIONS[GroundType.path].blendable, true);
22 assert.equal(GROUND_DEFINITIONS[GroundType.path].blendable, true);
23 assert.equal(GROUND_DEFINITIONS[GroundType.path].outdoor, true);
23 assert.equal(GROUND_DEFINITIONS[GroundType.path].outdoor, true);
24 assert.equal(GROUND_DEFINITIONS[GroundType.path].pathSurface, true);
24 assert.equal(GROUND_DEFINITIONS[GroundType.path].pathSurface, true);
25 assert.equal(GROUND_DEFINITIONS[GroundType.sand].blendable, true);
26 assert.equal(GROUND_DEFINITIONS[GroundType.sand].wetShore, true);
25 assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);
27 assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);
26 assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);
28 assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);
27});
29});
server/tests/wetSand.test.js added +77 −0
@@ -0,0 +1,77 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import {
4 wetSandBlendWeight,
5 wetSandEdgeDistance,
6 wetSandWetness,
7 mixWetSandPixel,
8 WET_SAND_BAND_FRACTION,
9 WET_SAND_NEIGHBOR_DIRS
10 } from "../../shared/render/wetSand.js";
11 import {
12 GroundType,
13 GROUND_DEFINITIONS,
14 isLiquidWaterGround
15 } from "../../shared/definitions/tiles.js";
16 import { SeasonId } from "../../shared/world/season.js";
17
18 test("sand is blendable wet-shore ground with year-round sprites", () =>
19 {
20 const sand = GROUND_DEFINITIONS[GroundType.sand];
21 assert.ok(sand);
22 assert.equal(sand.key, "sand");
23 assert.equal(sand.blendable, true);
24 assert.equal(sand.wetShore, true);
25 assert.equal(sand.outdoor, false);
26 assert.equal(sand.passable, true);
27 assert.equal(sand.water, false);
28 });
29
30 test("wet sand band is 30% of the tile", () =>
31 {
32 assert.equal(WET_SAND_BAND_FRACTION, 0.3);
33 assert.equal(WET_SAND_NEIGHBOR_DIRS.length, 8);
34 });
35
36 test("east water: edge fully wet, beyond 30% dry", () =>
37 {
38 const band = WET_SAND_BAND_FRACTION * 16;
39 assert.equal(wetSandBlendWeight(15, 8, 1, 0), 1);
40 assert.ok(wetSandBlendWeight(14, 8, 1, 0) > 0);
41 assert.ok(wetSandBlendWeight(14, 8, 1, 0) < 1);
42 assert.equal(wetSandBlendWeight(15 - Math.ceil(band), 8, 1, 0), 0);
43 assert.equal(wetSandBlendWeight(0, 8, 1, 0), 0);
44 });
45
46 test("southeast water wets the corner fan", () =>
47 {
48 assert.equal(wetSandEdgeDistance(15, 15, 1, 1), 0);
49 assert.equal(wetSandBlendWeight(15, 15, 1, 1), 1);
50 assert.equal(wetSandBlendWeight(15, 8, 1, 1), 0);
51 assert.ok(wetSandBlendWeight(14, 14, 1, 1) > 0);
52 });
53
54 test("wetness takes the max across water directions", () =>
55 {
56 const dirs = [{ ndx: 1, ndy: 0 }, { ndx: 0, ndy: 1 }];
57 assert.equal(wetSandWetness(15, 8, dirs), 1);
58 assert.equal(wetSandWetness(8, 15, dirs), 1);
59 assert.equal(wetSandWetness(0, 0, dirs), 0);
60 });
61
62 test("mixWetSandPixel lerps dry toward wet", () =>
63 {
64 const dry = { r: 0, g: 0, b: 0, a: 255 };
65 const wet = { r: 100, g: 200, b: 50, a: 255 };
66 assert.deepEqual(mixWetSandPixel(dry, wet, 0), dry);
67 assert.deepEqual(mixWetSandPixel(dry, wet, 1), wet);
68 assert.deepEqual(mixWetSandPixel(dry, wet, 0.5), { r: 50, g: 100, b: 25, a: 255 });
69 });
70
71 test("winter wet shore uses only liquid water (deep), not frozen shallows", () =>
72 {
73 assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.summer), true);
74 assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.winter), false);
75 assert.equal(isLiquidWaterGround(GroundType.deepWater, SeasonId.winter), true);
76 assert.equal(isLiquidWaterGround(GroundType.sand, SeasonId.winter), false);
77 });
@@ -0,0 +1,77 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 wetSandBlendWeight,
5 wetSandEdgeDistance,
6 wetSandWetness,
7 mixWetSandPixel,
8 WET_SAND_BAND_FRACTION,
9 WET_SAND_NEIGHBOR_DIRS
10} from "../../shared/render/wetSand.js";
11import {
12 GroundType,
13 GROUND_DEFINITIONS,
14 isLiquidWaterGround
15} from "../../shared/definitions/tiles.js";
16import { SeasonId } from "../../shared/world/season.js";
17
18test("sand is blendable wet-shore ground with year-round sprites", () =>
19{
20 const sand = GROUND_DEFINITIONS[GroundType.sand];
21 assert.ok(sand);
22 assert.equal(sand.key, "sand");
23 assert.equal(sand.blendable, true);
24 assert.equal(sand.wetShore, true);
25 assert.equal(sand.outdoor, false);
26 assert.equal(sand.passable, true);
27 assert.equal(sand.water, false);
28});
29
30test("wet sand band is 30% of the tile", () =>
31{
32 assert.equal(WET_SAND_BAND_FRACTION, 0.3);
33 assert.equal(WET_SAND_NEIGHBOR_DIRS.length, 8);
34});
35
36test("east water: edge fully wet, beyond 30% dry", () =>
37{
38 const band = WET_SAND_BAND_FRACTION * 16;
39 assert.equal(wetSandBlendWeight(15, 8, 1, 0), 1);
40 assert.ok(wetSandBlendWeight(14, 8, 1, 0) > 0);
41 assert.ok(wetSandBlendWeight(14, 8, 1, 0) < 1);
42 assert.equal(wetSandBlendWeight(15 - Math.ceil(band), 8, 1, 0), 0);
43 assert.equal(wetSandBlendWeight(0, 8, 1, 0), 0);
44});
45
46test("southeast water wets the corner fan", () =>
47{
48 assert.equal(wetSandEdgeDistance(15, 15, 1, 1), 0);
49 assert.equal(wetSandBlendWeight(15, 15, 1, 1), 1);
50 assert.equal(wetSandBlendWeight(15, 8, 1, 1), 0);
51 assert.ok(wetSandBlendWeight(14, 14, 1, 1) > 0);
52});
53
54test("wetness takes the max across water directions", () =>
55{
56 const dirs = [{ ndx: 1, ndy: 0 }, { ndx: 0, ndy: 1 }];
57 assert.equal(wetSandWetness(15, 8, dirs), 1);
58 assert.equal(wetSandWetness(8, 15, dirs), 1);
59 assert.equal(wetSandWetness(0, 0, dirs), 0);
60});
61
62test("mixWetSandPixel lerps dry toward wet", () =>
63{
64 const dry = { r: 0, g: 0, b: 0, a: 255 };
65 const wet = { r: 100, g: 200, b: 50, a: 255 };
66 assert.deepEqual(mixWetSandPixel(dry, wet, 0), dry);
67 assert.deepEqual(mixWetSandPixel(dry, wet, 1), wet);
68 assert.deepEqual(mixWetSandPixel(dry, wet, 0.5), { r: 50, g: 100, b: 25, a: 255 });
69});
70
71test("winter wet shore uses only liquid water (deep), not frozen shallows", () =>
72{
73 assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.summer), true);
74 assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.winter), false);
75 assert.equal(isLiquidWaterGround(GroundType.deepWater, SeasonId.winter), true);
76 assert.equal(isLiquidWaterGround(GroundType.sand, SeasonId.winter), false);
77});
shared/definitions/tiles.js +18 −1
@@ -41,7 +41,8 @@ export const GroundType = Object.freeze({
41 41 water: 3,
42 42 deepWater: 4,
43 43 dirt: 5,
44 path: 6
44 path: 6,
45 sand: 7
45 46 });
46 47
47 48 export const BlockType = Object.freeze({
@@ -176,6 +177,22 @@ export const GROUND_DEFINITIONS = Object.freeze({
176 177 blendable: true,
177 178 /** Running here costs half stamina; A* prefers these slightly out of combat. */
178 179 pathSurface: true
180 }),
181 [GroundType.sand]: Object.freeze({
182 id: GroundType.sand,
183 key: "sand",
184 displayName: "Sand",
185 passable: true,
186 wades: false,
187 water: false,
188 frames: 1,
189 framesPerRow: 1,
190 animationSpeed: 0,
191 /** Same look year-round; wet edge is baked toward sand-wet beside liquid water. */
192 outdoor: false,
193 blendable: true,
194 /** Client bakes a wet shoreline toward `sand-wet` (not a placeable state). */
195 wetShore: true
179 196 })
180 197 });
181 198
@@ -41,7 +41,8 @@ export const GroundType = Object.freeze({
41 water: 3,
41 water: 3,
42 deepWater: 4,
42 deepWater: 4,
43 dirt: 5,
43 dirt: 5,
44 path: 6
44 path: 6,
45 sand: 7
45});
46});
46
47
47export const BlockType = Object.freeze({
48export const BlockType = Object.freeze({
@@ -176,6 +177,22 @@ export const GROUND_DEFINITIONS = Object.freeze({
176 blendable: true,
177 blendable: true,
177 /** Running here costs half stamina; A* prefers these slightly out of combat. */
178 /** Running here costs half stamina; A* prefers these slightly out of combat. */
178 pathSurface: true
179 pathSurface: true
180 }),
181 [GroundType.sand]: Object.freeze({
182 id: GroundType.sand,
183 key: "sand",
184 displayName: "Sand",
185 passable: true,
186 wades: false,
187 water: false,
188 frames: 1,
189 framesPerRow: 1,
190 animationSpeed: 0,
191 /** Same look year-round; wet edge is baked toward sand-wet beside liquid water. */
192 outdoor: false,
193 blendable: true,
194 /** Client bakes a wet shoreline toward `sand-wet` (not a placeable state). */
195 wetShore: true
179 })
196 })
180});
197});
181
198
shared/render/chunkAtlasKeys.js +71 −1
@@ -1,8 +1,17 @@
1 1 import { CHUNK_SIZE } from "../protocol/constants.js";
2 import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock } from "../definitions/tiles.js";
2 import {
3 GROUND_DEFINITIONS,
4 BLOCK_DEFINITIONS,
5 GroundType,
6 normalizeGround,
7 normalizeBlock,
8 isChunkAtlasBakedBlock,
9 isLiquidWaterGround
10 } from "../definitions/tiles.js";
3 11 import { bloomImageName } from "../definitions/tileSprites.js";
4 12 import { resolveSeasonalVisualState } from "../world/season.js";
5 13 import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js";
14 import { WET_SAND_NEIGHBOR_DIRS } from "./wetSand.js";
6 15
7 16 export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty";
8 17 export const EMPTY_BLOOM_KEY = "bloom|empty";
@@ -45,6 +54,13 @@ export function computeChunkAtlasKeys ({
45 54 resolveGround
46 55 })}`;
47 56 }
57 groundBody += `|wet:${wetSandBorderFingerprint({
58 snapshot,
59 catalog,
60 season,
61 preview,
62 resolveGround
63 })}`;
48 64 const groundKey = `ground|${season}|${groundBody}`;
49 65
50 66 const blockRowKeys = [];
@@ -208,12 +224,66 @@ function softBorderGroundFingerprint ({
208 224 return parts.length ? parts.join(";") : "flat";
209 225 }
210 226
227 /**
228 * Cross-chunk liquid-water adjacency for wet-shore sand (8 dirs).
229 * Winter ice (shallow water) is excluded via isLiquidWaterGround.
230 */
231 function wetSandBorderFingerprint ({
232 snapshot,
233 season,
234 preview,
235 resolveGround
236 })
237 {
238 if (typeof resolveGround !== "function")
239 { return "none"; }
240 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
241 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
242 const parts = [];
243 for (let localY = 0; localY < CHUNK_SIZE; localY++)
244 {
245 for (let localX = 0; localX < CHUNK_SIZE; localX++)
246 {
247 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
248 if (!onBorder)
249 { continue; }
250 const tileX = baseTileX + localX;
251 const tileY = baseTileY + localY;
252 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
253 if (!isWetShoreGround(groundEntry))
254 { continue; }
255 for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS)
256 {
257 const nx = tileX + ndx;
258 const ny = tileY + ndy;
259 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
260 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
261 { continue; }
262 const neighbor = resolveGround(nx, ny);
263 if (neighbor == null)
264 { continue; }
265 const typeId = normalizeGround(neighbor).typeId;
266 if (!isLiquidWaterGround(typeId, season))
267 { continue; }
268 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${typeId}`);
269 }
270 }
271 }
272 return parts.length ? parts.join(";") : "flat";
273 }
274
211 275 function isGroundBlendable (ground)
212 276 {
213 277 const entry = normalizeGround(ground);
214 278 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
215 279 }
216 280
281 function isWetShoreGround (ground)
282 {
283 const entry = normalizeGround(ground);
284 return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore;
285 }
286
217 287 function groundVisualKey (catalog, ground, season)
218 288 {
219 289 const entry = normalizeGround(ground);
@@ -1,8 +1,17 @@
1import { CHUNK_SIZE } from "../protocol/constants.js";
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock } from "../definitions/tiles.js";
2import {
3 GROUND_DEFINITIONS,
4 BLOCK_DEFINITIONS,
5 GroundType,
6 normalizeGround,
7 normalizeBlock,
8 isChunkAtlasBakedBlock,
9 isLiquidWaterGround
10} from "../definitions/tiles.js";
3import { bloomImageName } from "../definitions/tileSprites.js";
11import { bloomImageName } from "../definitions/tileSprites.js";
4import { resolveSeasonalVisualState } from "../world/season.js";
12import { resolveSeasonalVisualState } from "../world/season.js";
5import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js";
13import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js";
14import { WET_SAND_NEIGHBOR_DIRS } from "./wetSand.js";
6
15
7export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty";
16export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty";
8export const EMPTY_BLOOM_KEY = "bloom|empty";
17export const EMPTY_BLOOM_KEY = "bloom|empty";
@@ -45,6 +54,13 @@ export function computeChunkAtlasKeys ({
45 resolveGround
54 resolveGround
46 })}`;
55 })}`;
47 }
56 }
57 groundBody += `|wet:${wetSandBorderFingerprint({
58 snapshot,
59 catalog,
60 season,
61 preview,
62 resolveGround
63 })}`;
48 const groundKey = `ground|${season}|${groundBody}`;
64 const groundKey = `ground|${season}|${groundBody}`;
49
65
50 const blockRowKeys = [];
66 const blockRowKeys = [];
@@ -208,12 +224,66 @@ function softBorderGroundFingerprint ({
208 return parts.length ? parts.join(";") : "flat";
224 return parts.length ? parts.join(";") : "flat";
209}
225}
210
226
227/**
228 * Cross-chunk liquid-water adjacency for wet-shore sand (8 dirs).
229 * Winter ice (shallow water) is excluded via isLiquidWaterGround.
230 */
231function wetSandBorderFingerprint ({
232 snapshot,
233 season,
234 preview,
235 resolveGround
236})
237{
238 if (typeof resolveGround !== "function")
239 { return "none"; }
240 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
241 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
242 const parts = [];
243 for (let localY = 0; localY < CHUNK_SIZE; localY++)
244 {
245 for (let localX = 0; localX < CHUNK_SIZE; localX++)
246 {
247 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
248 if (!onBorder)
249 { continue; }
250 const tileX = baseTileX + localX;
251 const tileY = baseTileY + localY;
252 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
253 if (!isWetShoreGround(groundEntry))
254 { continue; }
255 for (const { ndx, ndy } of WET_SAND_NEIGHBOR_DIRS)
256 {
257 const nx = tileX + ndx;
258 const ny = tileY + ndy;
259 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
260 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
261 { continue; }
262 const neighbor = resolveGround(nx, ny);
263 if (neighbor == null)
264 { continue; }
265 const typeId = normalizeGround(neighbor).typeId;
266 if (!isLiquidWaterGround(typeId, season))
267 { continue; }
268 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${typeId}`);
269 }
270 }
271 }
272 return parts.length ? parts.join(";") : "flat";
273}
274
211function isGroundBlendable (ground)
275function isGroundBlendable (ground)
212{
276{
213 const entry = normalizeGround(ground);
277 const entry = normalizeGround(ground);
214 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
278 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
215}
279}
216
280
281function isWetShoreGround (ground)
282{
283 const entry = normalizeGround(ground);
284 return !!GROUND_DEFINITIONS[entry.typeId]?.wetShore;
285}
286
217function groundVisualKey (catalog, ground, season)
287function groundVisualKey (catalog, ground, season)
218{
288{
219 const entry = normalizeGround(ground);
289 const entry = normalizeGround(ground);
shared/render/wetSand.js added +106 −0
@@ -0,0 +1,106 @@
1 /**
2 * Wet-sand edge blend toward liquid water.
3 *
4 * Within the outer 30% of a sand tile facing water (cardinal or diagonal),
5 * pixels lerp toward the wet sprite: edge = 100% wet, beyond 30% = dry.
6 * In winter, shallow water is ice — only deep water counts (see isLiquidWaterGround).
7 */
8
9 export const WET_SAND_BAND_FRACTION = 0.3;
10
11 /** All eight neighbors — diagonals wet the corner fan toward water. */
12 export const WET_SAND_NEIGHBOR_DIRS = Object.freeze([
13 Object.freeze({ ndx: -1, ndy: -1 }),
14 Object.freeze({ ndx: 0, ndy: -1 }),
15 Object.freeze({ ndx: 1, ndy: -1 }),
16 Object.freeze({ ndx: -1, ndy: 0 }),
17 Object.freeze({ ndx: 1, ndy: 0 }),
18 Object.freeze({ ndx: -1, ndy: 1 }),
19 Object.freeze({ ndx: 0, ndy: 1 }),
20 Object.freeze({ ndx: 1, ndy: 1 })
21 ]);
22
23 /**
24 * Distance in pixels from the edge(s) facing neighbor (ndx, ndy).
25 * Cardinal: axis distance from that edge.
26 * Diagonal: Chebyshev max so the wet region is a square fan from the corner.
27 * @returns {number|null}
28 */
29 export function wetSandEdgeDistance (px, py, ndx, ndy, size = 16)
30 {
31 const last = size - 1;
32 let edgeX = null;
33 let edgeY = null;
34 if (ndx > 0)
35 { edgeX = last - px; }
36 else if (ndx < 0)
37 { edgeX = px; }
38 if (ndy > 0)
39 { edgeY = last - py; }
40 else if (ndy < 0)
41 { edgeY = py; }
42
43 if (ndx !== 0 && ndy !== 0)
44 {
45 if (edgeX == null || edgeY == null)
46 { return null; }
47 return Math.max(edgeX, edgeY);
48 }
49 if (ndx !== 0)
50 { return edgeX; }
51 if (ndy !== 0)
52 { return edgeY; }
53 return null;
54 }
55
56 /**
57 * Wet blend weight for pixel (px, py) toward water neighbor (ndx, ndy).
58 * 1 at the abutting edge/corner, 0 beyond WET_SAND_BAND_FRACTION of the tile.
59 */
60 export function wetSandBlendWeight (px, py, ndx, ndy, size = 16)
61 {
62 const dist = wetSandEdgeDistance(px, py, ndx, ndy, size);
63 if (dist == null)
64 { return 0; }
65 const band = WET_SAND_BAND_FRACTION * size;
66 if (!(band > 0) || dist >= band)
67 { return 0; }
68 return 1 - dist / band;
69 }
70
71 /**
72 * Max wetness from all liquid-water neighbor directions.
73 * @param {Array<{ ndx: number, ndy: number }>} waterDirs
74 */
75 export function wetSandWetness (px, py, waterDirs, size = 16)
76 {
77 let max = 0;
78 for (const dir of waterDirs ?? [])
79 {
80 const w = wetSandBlendWeight(px, py, dir.ndx, dir.ndy, size);
81 if (w > max)
82 { max = w; }
83 }
84 return max;
85 }
86
87 /**
88 * Lerp dry → wet RGBA by wetness t in [0,1].
89 * @param {{ r: number, g: number, b: number, a: number }} dry
90 * @param {{ r: number, g: number, b: number, a: number }} wet
91 */
92 export function mixWetSandPixel (dry, wet, t)
93 {
94 const w = Math.max(0, Math.min(1, Number(t) || 0));
95 if (w <= 0)
96 { return dry; }
97 if (w >= 1)
98 { return wet; }
99 const keep = 1 - w;
100 return {
101 r: Math.round(dry.r * keep + wet.r * w),
102 g: Math.round(dry.g * keep + wet.g * w),
103 b: Math.round(dry.b * keep + wet.b * w),
104 a: Math.round(dry.a * keep + wet.a * w)
105 };
106 }
@@ -0,0 +1,106 @@
1/**
2 * Wet-sand edge blend toward liquid water.
3 *
4 * Within the outer 30% of a sand tile facing water (cardinal or diagonal),
5 * pixels lerp toward the wet sprite: edge = 100% wet, beyond 30% = dry.
6 * In winter, shallow water is ice — only deep water counts (see isLiquidWaterGround).
7 */
8
9export const WET_SAND_BAND_FRACTION = 0.3;
10
11/** All eight neighbors — diagonals wet the corner fan toward water. */
12export const WET_SAND_NEIGHBOR_DIRS = Object.freeze([
13 Object.freeze({ ndx: -1, ndy: -1 }),
14 Object.freeze({ ndx: 0, ndy: -1 }),
15 Object.freeze({ ndx: 1, ndy: -1 }),
16 Object.freeze({ ndx: -1, ndy: 0 }),
17 Object.freeze({ ndx: 1, ndy: 0 }),
18 Object.freeze({ ndx: -1, ndy: 1 }),
19 Object.freeze({ ndx: 0, ndy: 1 }),
20 Object.freeze({ ndx: 1, ndy: 1 })
21]);
22
23/**
24 * Distance in pixels from the edge(s) facing neighbor (ndx, ndy).
25 * Cardinal: axis distance from that edge.
26 * Diagonal: Chebyshev max so the wet region is a square fan from the corner.
27 * @returns {number|null}
28 */
29export function wetSandEdgeDistance (px, py, ndx, ndy, size = 16)
30{
31 const last = size - 1;
32 let edgeX = null;
33 let edgeY = null;
34 if (ndx > 0)
35 { edgeX = last - px; }
36 else if (ndx < 0)
37 { edgeX = px; }
38 if (ndy > 0)
39 { edgeY = last - py; }
40 else if (ndy < 0)
41 { edgeY = py; }
42
43 if (ndx !== 0 && ndy !== 0)
44 {
45 if (edgeX == null || edgeY == null)
46 { return null; }
47 return Math.max(edgeX, edgeY);
48 }
49 if (ndx !== 0)
50 { return edgeX; }
51 if (ndy !== 0)
52 { return edgeY; }
53 return null;
54}
55
56/**
57 * Wet blend weight for pixel (px, py) toward water neighbor (ndx, ndy).
58 * 1 at the abutting edge/corner, 0 beyond WET_SAND_BAND_FRACTION of the tile.
59 */
60export function wetSandBlendWeight (px, py, ndx, ndy, size = 16)
61{
62 const dist = wetSandEdgeDistance(px, py, ndx, ndy, size);
63 if (dist == null)
64 { return 0; }
65 const band = WET_SAND_BAND_FRACTION * size;
66 if (!(band > 0) || dist >= band)
67 { return 0; }
68 return 1 - dist / band;
69}
70
71/**
72 * Max wetness from all liquid-water neighbor directions.
73 * @param {Array<{ ndx: number, ndy: number }>} waterDirs
74 */
75export function wetSandWetness (px, py, waterDirs, size = 16)
76{
77 let max = 0;
78 for (const dir of waterDirs ?? [])
79 {
80 const w = wetSandBlendWeight(px, py, dir.ndx, dir.ndy, size);
81 if (w > max)
82 { max = w; }
83 }
84 return max;
85}
86
87/**
88 * Lerp dry → wet RGBA by wetness t in [0,1].
89 * @param {{ r: number, g: number, b: number, a: number }} dry
90 * @param {{ r: number, g: number, b: number, a: number }} wet
91 */
92export function mixWetSandPixel (dry, wet, t)
93{
94 const w = Math.max(0, Math.min(1, Number(t) || 0));
95 if (w <= 0)
96 { return dry; }
97 if (w >= 1)
98 { return wet; }
99 const keep = 1 - w;
100 return {
101 r: Math.round(dry.r * keep + wet.r * w),
102 g: Math.round(dry.g * keep + wet.g * w),
103 b: Math.round(dry.b * keep + wet.b * w),
104 a: Math.round(dry.a * keep + wet.a * w)
105 };
106}