Rename all "liquid" mentions to "fluid"
This commit is contained in:
+44
-44
@@ -231,39 +231,39 @@ function overlaps_solid(min: number[], max: number[], is_solid: BlockCollision)
|
||||
return false;
|
||||
}
|
||||
|
||||
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in liquid
|
||||
export function in_water(body: Body, is_liquid: (x: number, y: number, z: number) => boolean): boolean {
|
||||
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in fluid
|
||||
export function in_water(body: Body, is_fluid: (x: number, y: number, z: number) => boolean): boolean {
|
||||
const x = Math.floor(body.x);
|
||||
const z = Math.floor(body.z);
|
||||
return is_liquid(x, Math.floor(body.y), z) || is_liquid(x, Math.floor(body.y + body.height / 2), z);
|
||||
return is_fluid(x, Math.floor(body.y), z) || is_fluid(x, Math.floor(body.y + body.height / 2), z);
|
||||
}
|
||||
|
||||
// its eyes are in liquid, so it can't breathe
|
||||
// its eyes are in fluid, so it can't breathe
|
||||
export function eyes_in_water(
|
||||
body: Body,
|
||||
eye_height: number,
|
||||
is_liquid: (x: number, y: number, z: number) => boolean,
|
||||
is_fluid: (x: number, y: number, z: number) => boolean,
|
||||
): boolean {
|
||||
return is_liquid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
|
||||
return is_fluid(Math.floor(body.x), Math.floor(body.y + eye_height), Math.floor(body.z));
|
||||
}
|
||||
|
||||
// a liquid in a cell, for liquids pushing things along
|
||||
export interface LiquidInCell {
|
||||
// which liquid, its block's id. a liquid's flow only goes by cells of the same one
|
||||
// a fluid in a cell, for fluids pushing things along
|
||||
export interface FluidInCell {
|
||||
// which fluid, its block's id. a fluid's flow only goes by cells of the same one
|
||||
id: string;
|
||||
// its surface, 0 to 1 (see liquid_height), not counting more of it above
|
||||
// its surface, 0 to 1 (see fluid_height), not counting more of it above
|
||||
height: number;
|
||||
}
|
||||
|
||||
// the liquid in a cell, undefined for none
|
||||
export type LiquidAt = (x: number, y: number, z: number) => LiquidInCell | undefined;
|
||||
// the fluid in a cell, undefined for none
|
||||
export type FluidAt = (x: number, y: number, z: number) => FluidInCell | undefined;
|
||||
|
||||
const HORIZONTAL = [[1, 0], [-1, 0], [0, 1], [0, -1]];
|
||||
// blocks per second added each tick, minecraft's water 0.014 blocks per tick per tick
|
||||
const LIQUID_PUSH = 0.014 * 20;
|
||||
// moving slower than this sideways, a push is at least LIQUID_MIN_PUSH, so things still drift off flat-ish water
|
||||
const LIQUID_STILL_SPEED = 0.003 * 20;
|
||||
const LIQUID_MIN_PUSH = 0.0045 * 20;
|
||||
const FLUID_PUSH = 0.014 * 20;
|
||||
// moving slower than this sideways, a push is at least FLUID_MIN_PUSH, so things still drift off flat-ish water
|
||||
const FLUID_STILL_SPEED = 0.003 * 20;
|
||||
const FLUID_MIN_PUSH = 0.0045 * 20;
|
||||
|
||||
function blocks_motion(collision: boolean | Shape) {
|
||||
return shape_of(collision).length > 0;
|
||||
@@ -274,29 +274,29 @@ function normalized(x: number, y: number, z: number): [number, number, number] {
|
||||
return length < 1e-5 ? [0, 0, 0] : [x / length, y / length, z / length];
|
||||
}
|
||||
|
||||
// which way the liquid at x y z flows, a length 1 vector or nothing, like minecraft's FlowingFluid.getFlow: towards
|
||||
// which way the fluid at x y z flows, a length 1 vector or nothing, like minecraft's FlowingFluid.getFlow: towards
|
||||
// lower cells of it beside it, and towards drops where it pours over an edge. falling down a wall it goes down too
|
||||
export function liquid_flow(
|
||||
export function fluid_flow(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
liquid_at: LiquidAt,
|
||||
fluid_at: FluidAt,
|
||||
is_solid: BlockCollision,
|
||||
): [number, number, number] {
|
||||
const here = liquid_at(x, y, z);
|
||||
const here = fluid_at(x, y, z);
|
||||
if (!here) return [0, 0, 0];
|
||||
let fx = 0;
|
||||
let fz = 0;
|
||||
for (const [dx, dz] of HORIZONTAL) {
|
||||
const neighbor = liquid_at(x + dx, y, z + dz);
|
||||
// another liquid doesn't change this one's flow
|
||||
const neighbor = fluid_at(x + dx, y, z + dz);
|
||||
// another fluid doesn't change this one's flow
|
||||
if (neighbor && neighbor.id !== here.id) continue;
|
||||
let drop = 0;
|
||||
if (neighbor) {
|
||||
drop = here.height - neighbor.height;
|
||||
} else if (!blocks_motion(is_solid(x + dx, y, z + dz))) {
|
||||
// an edge it pours over, onto more of itself
|
||||
const below = liquid_at(x + dx, y - 1, z + dz);
|
||||
const below = fluid_at(x + dx, y - 1, z + dz);
|
||||
if (below?.id === here.id) drop = here.height - (below.height - 1);
|
||||
}
|
||||
fx += dx * drop;
|
||||
@@ -306,7 +306,7 @@ export function liquid_flow(
|
||||
|
||||
// falling: flowing, not a source, with more of it above. a deep pool of sources isn't
|
||||
let fy = 0;
|
||||
if (here.height < 1 && liquid_at(x, y + 1, z)?.id === here.id) {
|
||||
if (here.height < 1 && fluid_at(x, y + 1, z)?.id === here.id) {
|
||||
const against_wall = HORIZONTAL.some(([dx, dz]) =>
|
||||
blocks_motion(is_solid(x + dx, y, z + dz)) || blocks_motion(is_solid(x + dx, y + 1, z + dz))
|
||||
);
|
||||
@@ -315,10 +315,10 @@ export function liquid_flow(
|
||||
return normalized(fx, fy, fz);
|
||||
}
|
||||
|
||||
// liquid the body is in pushes it the way it flows, like minecraft's Entity.updateFluidHeightAndDoFluidPushing: the
|
||||
// fluid the body is in pushes it the way it flows, like minecraft's Entity.updateFluidHeightAndDoFluidPushing: the
|
||||
// flow of every cell of it the box reaches, averaged, weaker where it's shallow. players get that average as it is,
|
||||
// other things are pushed at full strength whichever way it points. changes the body's velocity
|
||||
export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockCollision, is_player: boolean) {
|
||||
export function push_by_fluid(body: Body, fluid_at: FluidAt, is_solid: BlockCollision, is_player: boolean) {
|
||||
const half = body.width / 2 - 0.001;
|
||||
const min_y = body.y + 0.001;
|
||||
const max_y = body.y + body.height - 0.001;
|
||||
@@ -330,12 +330,12 @@ export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockC
|
||||
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
|
||||
for (let y = Math.floor(min_y); y <= Math.floor(max_y); y++) {
|
||||
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
|
||||
const liquid = liquid_at(x, y, z);
|
||||
if (!liquid) continue;
|
||||
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
|
||||
const fluid = fluid_at(x, y, z);
|
||||
if (!fluid) continue;
|
||||
const surface = y + (fluid_at(x, y + 1, z)?.id === fluid.id ? 1 : fluid.height);
|
||||
if (surface < min_y) continue;
|
||||
depth = Math.max(depth, surface - min_y);
|
||||
let [fx, fy, fz] = liquid_flow(x, y, z, liquid_at, is_solid);
|
||||
let [fx, fy, fz] = fluid_flow(x, y, z, fluid_at, is_solid);
|
||||
// minecraft's, which goes by the deepest cell so far
|
||||
if (depth < 0.4) {
|
||||
fx *= depth;
|
||||
@@ -352,31 +352,31 @@ export function push_by_liquid(body: Body, liquid_at: LiquidAt, is_solid: BlockC
|
||||
if (Math.hypot(px, py, pz) === 0) return;
|
||||
|
||||
[px, py, pz] = is_player ? [px / count, py / count, pz / count] : normalized(px, py, pz);
|
||||
px *= LIQUID_PUSH;
|
||||
py *= LIQUID_PUSH;
|
||||
pz *= LIQUID_PUSH;
|
||||
px *= FLUID_PUSH;
|
||||
py *= FLUID_PUSH;
|
||||
pz *= FLUID_PUSH;
|
||||
if (
|
||||
Math.abs(body.vx) < LIQUID_STILL_SPEED && Math.abs(body.vz) < LIQUID_STILL_SPEED &&
|
||||
Math.hypot(px, py, pz) < LIQUID_MIN_PUSH
|
||||
Math.abs(body.vx) < FLUID_STILL_SPEED && Math.abs(body.vz) < FLUID_STILL_SPEED &&
|
||||
Math.hypot(px, py, pz) < FLUID_MIN_PUSH
|
||||
) {
|
||||
[px, py, pz] = normalized(px, py, pz).map((n) => n * LIQUID_MIN_PUSH);
|
||||
[px, py, pz] = normalized(px, py, pz).map((n) => n * FLUID_MIN_PUSH);
|
||||
}
|
||||
body.vx += px;
|
||||
body.vy += py;
|
||||
body.vz += pz;
|
||||
}
|
||||
|
||||
// how far the body's feet are under the surface of liquid it's in, 0 for none, like minecraft's getFluidHeight
|
||||
export function liquid_depth(body: Body, liquid_at: LiquidAt): number {
|
||||
// how far the body's feet are under the surface of fluid it's in, 0 for none, like minecraft's getFluidHeight
|
||||
export function fluid_depth(body: Body, fluid_at: FluidAt): number {
|
||||
const half = body.width / 2 - 0.001;
|
||||
const min_y = body.y + 0.001;
|
||||
let depth = 0;
|
||||
for (let x = Math.floor(body.x - half); x <= Math.floor(body.x + half); x++) {
|
||||
for (let y = Math.floor(min_y); y <= Math.floor(body.y + body.height - 0.001); y++) {
|
||||
for (let z = Math.floor(body.z - half); z <= Math.floor(body.z + half); z++) {
|
||||
const liquid = liquid_at(x, y, z);
|
||||
if (!liquid) continue;
|
||||
const surface = y + (liquid_at(x, y + 1, z)?.id === liquid.id ? 1 : liquid.height);
|
||||
const fluid = fluid_at(x, y, z);
|
||||
if (!fluid) continue;
|
||||
const surface = y + (fluid_at(x, y + 1, z)?.id === fluid.id ? 1 : fluid.height);
|
||||
depth = Math.max(depth, surface - min_y);
|
||||
}
|
||||
}
|
||||
@@ -392,7 +392,7 @@ export const physics = {
|
||||
box_is_free,
|
||||
in_water,
|
||||
eyes_in_water,
|
||||
liquid_flow,
|
||||
push_by_liquid,
|
||||
liquid_depth,
|
||||
fluid_flow,
|
||||
push_by_fluid,
|
||||
fluid_depth,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user