Make fishing rods not actually target water
This commit is contained in:
@@ -153,6 +153,53 @@ export function clip_shape(
|
||||
return closest;
|
||||
}
|
||||
|
||||
export type ShapeFace = "west" | "east" | "bottom" | "top" | "north" | "south";
|
||||
|
||||
export interface BlockRaycastHit {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
// the side of the block's box it went in by
|
||||
face: ShapeFace;
|
||||
distance: number;
|
||||
}
|
||||
|
||||
// the first block a ray from origin goes into within max_distance, cell by cell, like minecraft's Level.clip.
|
||||
// direction is a unit vector, and shape_of says what each block there is, undefined for nothing to hit. a block the
|
||||
// ray starts inside isn't hit
|
||||
export function raycast_blocks(
|
||||
origin: readonly number[],
|
||||
direction: readonly number[],
|
||||
max_distance: number,
|
||||
shape_of: (x: number, y: number, z: number) => Shape | undefined,
|
||||
): BlockRaycastHit | undefined {
|
||||
const cell = origin.map(Math.floor);
|
||||
const step = direction.map(Math.sign);
|
||||
// how far along the ray it takes to cross a whole cell on each axis, and how far until it crosses the next one
|
||||
const delta = direction.map((d) => d === 0 ? Infinity : Math.abs(1 / d));
|
||||
const next = [0, 1, 2].map((axis) =>
|
||||
direction[axis] === 0
|
||||
? Infinity
|
||||
: (step[axis] > 0 ? cell[axis] + 1 - origin[axis] : origin[axis] - cell[axis]) * delta[axis]
|
||||
);
|
||||
for (;;) {
|
||||
const shape = shape_of(cell[0], cell[1], cell[2]);
|
||||
const hit = shape && clip_shape(shape, cell[0], cell[1], cell[2], [...origin], [...direction], max_distance);
|
||||
if (hit) {
|
||||
const face: ShapeFace = hit.axis === 0
|
||||
? (hit.sign > 0 ? "west" : "east")
|
||||
: hit.axis === 1
|
||||
? (hit.sign > 0 ? "bottom" : "top")
|
||||
: (hit.sign > 0 ? "north" : "south");
|
||||
return { x: cell[0], y: cell[1], z: cell[2], face, distance: hit.distance };
|
||||
}
|
||||
const axis = next[0] < next[1] ? (next[0] < next[2] ? 0 : 2) : (next[1] < next[2] ? 1 : 2);
|
||||
if (next[axis] > max_distance) return undefined;
|
||||
cell[axis] += step[axis];
|
||||
next[axis] += delta[axis];
|
||||
}
|
||||
}
|
||||
|
||||
// whether the shape's boxes cover the whole of one of the block's sides, like minecraft's Block.isFaceFull. checked
|
||||
// pixel by pixel, which is as fine as shapes in pixels get
|
||||
export function covers_face(shape: Shape, face: "west" | "east" | "bottom" | "top" | "north" | "south"): boolean {
|
||||
|
||||
@@ -16,6 +16,7 @@ export interface ServerContext {
|
||||
players: PlayerList;
|
||||
containers: ContainerApi;
|
||||
items: ItemApi;
|
||||
blocks: BlockApi;
|
||||
recipes: RecipeApi;
|
||||
tags: TagApi;
|
||||
menus: MenuRegistry;
|
||||
@@ -215,6 +216,9 @@ export interface ServerWorld {
|
||||
drop_item(x: number, y: number, z: number, item: ItemStack): void;
|
||||
// an entity from entities/ with its feet at x y z. throws for kinds that don't exist
|
||||
spawn_entity(id: Id, x: number, y: number, z: number): EntityRef;
|
||||
// the first block on the line from one point to another, like minecraft's Level.clip. it hits what entities bump
|
||||
// into, so it goes through plants and other blocks without collision. undefined when nothing is in the way
|
||||
raycast(from: Position, to: Position, options?: RaycastOptions): RaycastHit | undefined;
|
||||
is_loaded(x: number, z: number): boolean;
|
||||
readonly seed: string;
|
||||
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
|
||||
@@ -224,6 +228,24 @@ export interface ServerWorld {
|
||||
set_time(time: number): void;
|
||||
}
|
||||
|
||||
export interface RaycastOptions {
|
||||
// like minecraft's ClipContext.Fluid: "none" (the default) goes through liquids, "source_only" hits their sources
|
||||
// and goes through where they flow, "any" hits all of them. a liquid is hit at its surface
|
||||
liquids?: "none" | "source_only" | "any";
|
||||
}
|
||||
|
||||
export interface RaycastHit {
|
||||
// the block it hit, and where that is
|
||||
readonly id: Id;
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
readonly z: number;
|
||||
// the side it went in by
|
||||
readonly face: Face;
|
||||
// the exact point it hit
|
||||
readonly position: Readonly<Position>;
|
||||
}
|
||||
|
||||
// an entity in the world. once it's removed, by a script or anything else, it stays removed and moving it does nothing
|
||||
export interface EntityRef {
|
||||
readonly id: string;
|
||||
@@ -238,6 +260,10 @@ export interface Player {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly position: Readonly<Position>;
|
||||
// where they see from, lower while swimming, and which way they look as a vector 1 long. for aiming, like
|
||||
// ctx.world.raycast(player.eye_position, ...)
|
||||
readonly eye_position: Readonly<Position>;
|
||||
readonly look_direction: Readonly<Position>;
|
||||
readonly inventory: Container; // 36 slots, hotbar is 0-8
|
||||
readonly selected_slot: number;
|
||||
readonly held_item: ItemStack | undefined;
|
||||
@@ -344,6 +370,12 @@ export interface ItemApi {
|
||||
components(id: Id): Record<Id, unknown>;
|
||||
}
|
||||
|
||||
export interface BlockApi {
|
||||
exists(id: Id): boolean;
|
||||
// the components its json lists and their params, a copy, like ItemApi.components
|
||||
components(id: Id): Record<Id, unknown>;
|
||||
}
|
||||
|
||||
export interface RecipeApi {
|
||||
furnace_result(input: Id): { output: ItemStack; cook_time: number } | undefined;
|
||||
fuel_value(item: Id): number; // burn time in ticks, 0 if it isn't fuel
|
||||
|
||||
@@ -164,7 +164,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
|
||||
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
|
||||
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
|
||||
| `bworld:water_source` | none | Refills the watering can. |
|
||||
| `bworld:fishable` | none | Can be fished in with the fishing rod. |
|
||||
| `bworld:fishable` | none | The fishing rod's hook floats on it and fish bite there when it lands in it. |
|
||||
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (its rules are in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
|
||||
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
|
||||
|
||||
|
||||
@@ -158,3 +158,30 @@ still going when the server stops don't carry on after it starts again until som
|
||||
grass, leaves and water, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only
|
||||
the grass block's top. Without it every texture is tinted. In inventories, where there's no biome, the block uses
|
||||
plains' color.
|
||||
|
||||
## Raycasts
|
||||
|
||||
Server scripts find the first block on a line with `ctx.world.raycast`, like Minecraft's `Level.clip`. The fishing rod's
|
||||
hook uses it to see where it lands, and with a player's
|
||||
[`eye_position` and `look_direction`](../entities/players.md#the-player-api) it finds what they're looking at:
|
||||
|
||||
```ts
|
||||
const hit = ctx.world.raycast(from, to, { liquids: "any" });
|
||||
if (hit && "bworld:fishable" in ctx.blocks.components(hit.id)) {
|
||||
// hit.x, hit.y, hit.z is the block, hit.face the side it went in by, hit.position the exact point
|
||||
}
|
||||
```
|
||||
|
||||
It hits what entities bump into, so it goes through plants and other blocks with `"collision": false`. `liquids` is like
|
||||
Minecraft's `ClipContext.Fluid`:
|
||||
|
||||
| `liquids` | Hits |
|
||||
| ------------------ | ------------------------------------------- |
|
||||
| `"none"` (default) | No liquids, it goes through them. |
|
||||
| `"source_only"` | Liquid sources, going through flowing ones. |
|
||||
| `"any"` | Every liquid. |
|
||||
|
||||
A liquid is hit at its surface, which is lower where it flows. It returns `undefined` when nothing is in the way.
|
||||
|
||||
`ctx.blocks.components(id)` gives the components a block's json lists and their params, like `ctx.items.components`, and
|
||||
`ctx.blocks.exists(id)` whether a block exists.
|
||||
|
||||
@@ -20,6 +20,8 @@ interface Player {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly position: { x: number; y: number; z: number }; // their feet
|
||||
readonly eye_position: { x: number; y: number; z: number }; // where they see from, lower while swimming
|
||||
readonly look_direction: { x: number; y: number; z: number }; // which way they look, 1 long
|
||||
readonly inventory: Container; // 36 slots, the hotbar is 0-8
|
||||
readonly selected_slot: number;
|
||||
readonly held_item: ItemStack | undefined;
|
||||
@@ -54,6 +56,17 @@ interface Player {
|
||||
`inventory` is a live [container](../inventories/containers.md), and `held_item` a live stack: changing them changes
|
||||
what the player has.
|
||||
|
||||
`eye_position` and `look_direction` are for aiming. With [`ctx.world.raycast`](../blocks/index.md#raycasts) they find
|
||||
what a player is looking at, like Minecraft's `getPlayerPOVHitResult`:
|
||||
|
||||
```ts
|
||||
const eye = player.eye_position;
|
||||
const look = player.look_direction;
|
||||
const reach = 5;
|
||||
const to = { x: eye.x + look.x * reach, y: eye.y + look.y * reach, z: eye.z + look.z * reach };
|
||||
const hit = ctx.world.raycast(eye, to, { liquids: "source_only" });
|
||||
```
|
||||
|
||||
## Health and Hunger
|
||||
|
||||
Players follow Minecraft's rules, with 20 health (10 hearts) and 20 hunger:
|
||||
|
||||
@@ -6,9 +6,8 @@
|
||||
"data_textures": {
|
||||
"cast": "bworld:fishing_rod_cast"
|
||||
},
|
||||
"targets_liquids": true,
|
||||
"max_stack": 1,
|
||||
"lore": "Right click water to cast, and again when something bites.",
|
||||
"lore": "Right click to cast into water, and again when something bites.",
|
||||
"components": {
|
||||
"bworld:fishing_rod": {
|
||||
"wait": [100, 600],
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
|
||||
import type { Position } from "bworld/shared";
|
||||
import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
|
||||
@@ -622,13 +623,23 @@ function register_buckets(ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
// fishing, like minecraft's: right click water with a rod to cast, wait for a bite, and right click again before it
|
||||
// gets away to reel it in. the hook floats on the water, and goes under when something bites
|
||||
// fishing, like minecraft's: right click with a rod to throw its hook forward. when it lands in a block with
|
||||
// bworld:fishable, like water, it floats there until something bites, and right clicking again before it gets away
|
||||
// reels it in. a hook that lands on anything else lies there until it's reeled in, with nothing on it
|
||||
|
||||
const FISHING_ROD = "bworld:fishing_rod";
|
||||
const FISHING_HOOK = "bworld:fishing_hook";
|
||||
// further than this from where they cast, the line snaps
|
||||
const FISHABLE = "bworld:fishable";
|
||||
// further than this from the hook, the line snaps
|
||||
const LINE_LENGTH = 16;
|
||||
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
|
||||
const THROW_SPEED = 1;
|
||||
const GRAVITY = 0.03;
|
||||
const DRAG = 0.92;
|
||||
// it's reeled in if it's still flying after this many ticks
|
||||
const MAX_FLIGHT = 100;
|
||||
// half the hook's 0.25 block box: the hook's middle flies along the line, its feet are this much under it
|
||||
const HOOK_HALF = 0.125;
|
||||
// the hook's feet under the water's surface, so its red stripe floats right on it (the hook's sprite is 16 pixels
|
||||
// around the middle of its 0.25 block box, the stripe is 1.5 pixels above that)
|
||||
const HOOK_DEPTH = 0.125 + 1.5 / 16;
|
||||
@@ -645,25 +656,25 @@ interface FishingRodParams {
|
||||
}
|
||||
|
||||
interface Cast {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
// the block they cast into, the line comes out when it's gone
|
||||
block: string;
|
||||
// the hotbar slot the rod is in, switching away reels it in
|
||||
slot: number;
|
||||
params: FishingRodParams;
|
||||
hook: EntityRef;
|
||||
// the hook's middle
|
||||
position: Position;
|
||||
// blocks per tick while it's flying, undefined once it has landed
|
||||
velocity?: Position;
|
||||
thrown_at: number;
|
||||
// the fishable block it floats on, once it lands in one. the line comes out when it's gone
|
||||
water?: { x: number; y: number; z: number; block: string };
|
||||
// ticks, when something bites and when it gets away
|
||||
bite_at: number;
|
||||
bite_at?: number;
|
||||
escape_at?: number;
|
||||
}
|
||||
|
||||
function register_fishing(ctx: ServerContext) {
|
||||
// by player id
|
||||
const casts = new Map<string, Cast>();
|
||||
// the water each player right clicked this tick, for their rod's on_use, which doesn't get the block
|
||||
const aimed = new Map<string, { x: number; y: number; z: number; block: string; tick: number }>();
|
||||
|
||||
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
|
||||
|
||||
@@ -677,7 +688,14 @@ function register_fishing(ctx: ServerContext) {
|
||||
return loot.at(-1)?.item;
|
||||
};
|
||||
|
||||
// takes the line out of the water, the rod in the slot looks uncast again
|
||||
const fishable = new Map<string, boolean>();
|
||||
const is_fishable = (id: string) => {
|
||||
let result = fishable.get(id);
|
||||
if (result === undefined) fishable.set(id, result = FISHABLE in ctx.blocks.components(id));
|
||||
return result;
|
||||
};
|
||||
|
||||
// takes the line in, the rod in the slot looks uncast again
|
||||
const stop = (player: Player | undefined, id: string) => {
|
||||
const cast = casts.get(id);
|
||||
casts.delete(id);
|
||||
@@ -686,18 +704,35 @@ function register_fishing(ctx: ServerContext) {
|
||||
if (rod?.id === FISHING_ROD) rod.data = undefined;
|
||||
};
|
||||
|
||||
ctx.components.register_block("bworld:fishable", {
|
||||
on_interact(block, _params, player) {
|
||||
aimed.set(player.id, {
|
||||
x: block.x,
|
||||
y: block.y,
|
||||
z: block.z,
|
||||
block: block.id,
|
||||
tick: ctx.system.current_tick,
|
||||
});
|
||||
return false;
|
||||
},
|
||||
});
|
||||
// moves the hook along its flight for a tick, until it hits something
|
||||
const fly = (cast: Cast, velocity: Position, now: number) => {
|
||||
const from = cast.position;
|
||||
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
|
||||
const hit = ctx.world.raycast(from, to, { liquids: "any" });
|
||||
if (!hit) {
|
||||
cast.position = to;
|
||||
velocity.x *= DRAG;
|
||||
velocity.y = velocity.y * DRAG - GRAVITY;
|
||||
velocity.z *= DRAG;
|
||||
cast.hook.teleport(to.x, to.y - HOOK_HALF, to.z);
|
||||
return;
|
||||
}
|
||||
cast.velocity = undefined;
|
||||
cast.position = { ...hit.position };
|
||||
if (is_fishable(hit.id)) {
|
||||
// it floats on top, even when it went in by a side under the surface
|
||||
let y = hit.y;
|
||||
while (ctx.world.get_block(hit.x, y + 1, hit.z) === hit.id) y++;
|
||||
cast.water = { x: hit.x, y, z: hit.z, block: hit.id };
|
||||
cast.position.y = y + 1;
|
||||
cast.bite_at = now + between(cast.params.wait);
|
||||
} else {
|
||||
cast.hook.teleport(hit.position.x, hit.position.y - HOOK_HALF, hit.position.z);
|
||||
}
|
||||
};
|
||||
|
||||
// fishable blocks are found by their components, see is_fishable
|
||||
ctx.components.register_block(FISHABLE, {});
|
||||
|
||||
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
|
||||
on_use(rod, params, player) {
|
||||
@@ -710,22 +745,16 @@ function register_fishing(ctx: ServerContext) {
|
||||
stop(player, player.id);
|
||||
return;
|
||||
}
|
||||
const water = aimed.get(player.id);
|
||||
aimed.delete(player.id);
|
||||
if (water?.tick !== ctx.system.current_tick) {
|
||||
// cast before the server restarted
|
||||
rod.data = undefined;
|
||||
return;
|
||||
}
|
||||
const eye = player.eye_position;
|
||||
const look = player.look_direction;
|
||||
const position = { x: eye.x + look.x * 0.5, y: eye.y + look.y * 0.5, z: eye.z + look.z * 0.5 };
|
||||
casts.set(player.id, {
|
||||
x: water.x,
|
||||
y: water.y,
|
||||
z: water.z,
|
||||
block: water.block,
|
||||
slot: player.selected_slot,
|
||||
params,
|
||||
hook: ctx.world.spawn_entity(FISHING_HOOK, water.x + 0.5, water.y + 1 - HOOK_DEPTH, water.z + 0.5),
|
||||
bite_at: ctx.system.current_tick + between(params.wait),
|
||||
hook: ctx.world.spawn_entity(FISHING_HOOK, position.x, position.y - HOOK_HALF, position.z),
|
||||
position,
|
||||
velocity: { x: look.x * THROW_SPEED, y: look.y * THROW_SPEED, z: look.z * THROW_SPEED },
|
||||
thrown_at: ctx.system.current_tick,
|
||||
});
|
||||
rod.data = { cast: true };
|
||||
},
|
||||
@@ -735,16 +764,24 @@ function register_fishing(ctx: ServerContext) {
|
||||
const now = ctx.system.current_tick;
|
||||
for (const [id, cast] of casts) {
|
||||
const player = ctx.players.get(id);
|
||||
const { x, y, z } = player?.position ?? cast;
|
||||
const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z);
|
||||
const { x, y, z } = player?.position ?? cast.position;
|
||||
const distance = Math.hypot(x - cast.position.x, y - cast.position.y, z - cast.position.z);
|
||||
const water = cast.water;
|
||||
if (
|
||||
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
|
||||
distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block ||
|
||||
cast.hook.removed
|
||||
distance > LINE_LENGTH || cast.hook.removed ||
|
||||
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
|
||||
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
|
||||
) {
|
||||
stop(player, id);
|
||||
continue;
|
||||
}
|
||||
if (cast.velocity) {
|
||||
fly(cast, cast.velocity, now);
|
||||
}
|
||||
if (!cast.water || cast.bite_at === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (cast.escape_at === undefined && now >= cast.bite_at) {
|
||||
cast.escape_at = now + between(cast.params.bite);
|
||||
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
|
||||
@@ -753,7 +790,7 @@ function register_fishing(ctx: ServerContext) {
|
||||
cast.bite_at = now + between(cast.params.wait);
|
||||
}
|
||||
const bob = cast.escape_at === undefined ? Math.sin(now / 40 * Math.PI * 2) * HOOK_BOB : -HOOK_DIP;
|
||||
cast.hook.teleport(cast.x + 0.5, cast.y + 1 - HOOK_DEPTH + bob, cast.z + 0.5);
|
||||
cast.hook.teleport(cast.position.x, cast.position.y - HOOK_DEPTH + bob, cast.position.z);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
// runs mods' server scripts: builds each one's ServerContext and keeps what they register.
|
||||
// see docs/modding/. parts not built yet throw when used, saying so
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "$/common/everything_registry.ts";
|
||||
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { covers_face } from "$/common/block_shapes.ts";
|
||||
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
|
||||
import { AIR, ID_MASK } from "$/common/constants.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import type { SharedContext } from "$/common/mod_api/shared.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
@@ -647,6 +648,37 @@ export class ModRuntime {
|
||||
game().entities.add_entity(entity);
|
||||
return entity_ref(entity);
|
||||
},
|
||||
raycast: (from, to, options) => {
|
||||
const liquids = options?.liquids ?? "none";
|
||||
if (!["none", "source_only", "any"].includes(liquids)) {
|
||||
throw new Error(`liquids must be "none", "source_only" or "any", not ${liquids}`);
|
||||
}
|
||||
const origin = [from.x, from.y, from.z];
|
||||
const length = Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z);
|
||||
if (!Number.isFinite(length)) throw new Error("raycast needs two points with finite x, y and z");
|
||||
if (length === 0) return undefined;
|
||||
const direction = [(to.x - from.x) / length, (to.y - from.y) / length, (to.z - from.z) / length];
|
||||
const world = game().world;
|
||||
const hit = raycast_blocks(
|
||||
origin,
|
||||
direction,
|
||||
length,
|
||||
(x, y, z) => raycast_shape(world, x, y, z, liquids),
|
||||
);
|
||||
if (!hit) return undefined;
|
||||
return {
|
||||
id: world.get_block_id(hit.x, hit.y, hit.z),
|
||||
x: hit.x,
|
||||
y: hit.y,
|
||||
z: hit.z,
|
||||
face: hit.face,
|
||||
position: {
|
||||
x: from.x + direction[0] * hit.distance,
|
||||
y: from.y + direction[1] * hit.distance,
|
||||
z: from.z + direction[2] * hit.distance,
|
||||
},
|
||||
};
|
||||
},
|
||||
is_loaded: () => true,
|
||||
get seed() {
|
||||
return game().world.seed;
|
||||
@@ -698,6 +730,14 @@ export class ModRuntime {
|
||||
return structuredClone(item.components ?? {});
|
||||
},
|
||||
},
|
||||
blocks: {
|
||||
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
|
||||
components: (id) => {
|
||||
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) throw new Error(`unknown block ${id}`);
|
||||
return structuredClone(block.components ?? {});
|
||||
},
|
||||
},
|
||||
recipes: {
|
||||
furnace_result: (input) => {
|
||||
const recipe = game().recipes.furnace.get(input);
|
||||
@@ -1040,6 +1080,29 @@ function container_api(game: GameServer, id: string, container: EngineContainer)
|
||||
return api;
|
||||
}
|
||||
|
||||
// what ServerWorld.raycast hits in a block: what entities bump into, and liquids up to their surface when it's
|
||||
// looking for them
|
||||
function raycast_shape(
|
||||
world: GameServer["world"],
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
liquids: "none" | "source_only" | "any",
|
||||
): Shape | undefined {
|
||||
const value = world.get_block_value(x, y, z);
|
||||
const nid = value & ID_MASK;
|
||||
if (nid === AIR) return undefined;
|
||||
const block = EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
if (!block) return undefined;
|
||||
if (block.liquid) {
|
||||
const level = get_state_value(value, block, "level") ?? 0;
|
||||
if (liquids === "none" || (liquids === "source_only" && level !== 0)) return undefined;
|
||||
return [[0, 0, 0, 1, liquid_height(level, block.liquid.range), 1]];
|
||||
}
|
||||
const shape = collision_shape_at(block, value);
|
||||
return shape.length > 0 ? shape : undefined;
|
||||
}
|
||||
|
||||
function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
const inventory = container_api(game, `player:${player.id}`, player.inventory);
|
||||
|
||||
@@ -1053,6 +1116,18 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
get position() {
|
||||
return { x: player.x, y: player.y, z: player.z };
|
||||
},
|
||||
get eye_position() {
|
||||
return { x: player.x, y: player.y + player.eye_height, z: player.z };
|
||||
},
|
||||
// the same way the client aims, see ClientLevel.pick
|
||||
get look_direction() {
|
||||
const cos_pitch = Math.cos(player.pitch);
|
||||
return {
|
||||
x: -Math.sin(player.yaw) * cos_pitch,
|
||||
y: Math.sin(player.pitch),
|
||||
z: -Math.cos(player.yaw) * cos_pitch,
|
||||
};
|
||||
},
|
||||
inventory,
|
||||
get selected_slot() {
|
||||
return player.selected_slot;
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
EMPTY_SHAPE,
|
||||
FULL_SHAPE,
|
||||
model_shape,
|
||||
raycast_blocks,
|
||||
type Shape,
|
||||
} from "$/common/block_shapes.ts";
|
||||
import { block_from_json, validate_block } from "$/common/mod_data.ts";
|
||||
@@ -118,3 +119,42 @@ Deno.test("the outline is the edges of the shape's boxes, a little outside them"
|
||||
// edges two boxes share are drawn once
|
||||
assertEquals(shape_edges([...SLAB, ...SLAB]).length, 12);
|
||||
});
|
||||
|
||||
// a world of full blocks at the listed positions, and half height slabs at the others listed
|
||||
function raycast_world(full: number[][], slabs: number[][] = []) {
|
||||
const has = (list: number[][], x: number, y: number, z: number) =>
|
||||
list.some(([bx, by, bz]) => bx === x && by === y && bz === z);
|
||||
return (x: number, y: number, z: number): Shape | undefined =>
|
||||
has(full, x, y, z) ? FULL_SHAPE : has(slabs, x, y, z) ? [[0, 0, 0, 1, 0.5, 1]] : undefined;
|
||||
}
|
||||
|
||||
Deno.test("raycast_blocks finds the first block along the ray, and the side it went in by", () => {
|
||||
const world = raycast_world([[5, 0, 0], [8, 0, 0]]);
|
||||
const east = raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([east.x, east.y, east.z, east.face], [5, 0, 0, "west"]);
|
||||
assertAlmostEquals(east.distance, 4.5);
|
||||
|
||||
const west = raycast_blocks([10.5, 0.5, 0.5], [-1, 0, 0], 20, world)!;
|
||||
assertEquals([west.x, west.face], [8, "east"]);
|
||||
assertAlmostEquals(west.distance, 1.5);
|
||||
|
||||
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 4, world), undefined);
|
||||
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [0, 0, 1], 20, world), undefined);
|
||||
});
|
||||
|
||||
Deno.test("raycast_blocks goes diagonally and down onto the top of a block", () => {
|
||||
// it reaches y 0 at x and z 2.7, on the block's top
|
||||
const world = raycast_world([[2, -1, 2]]);
|
||||
const d = 1 / Math.sqrt(3);
|
||||
const hit = raycast_blocks([0.2, 2.5, 0.2], [d, -d, d], 20, world)!;
|
||||
assertEquals([hit.x, hit.y, hit.z, hit.face], [2, -1, 2, "top"]);
|
||||
assertAlmostEquals(hit.distance, 2.5 * Math.sqrt(3));
|
||||
});
|
||||
|
||||
Deno.test("raycast_blocks goes over the empty half of a slab, and not out of a block it starts in", () => {
|
||||
const world = raycast_world([[0, 0, 0], [4, 0, 0]], [[2, 0, 0]]);
|
||||
const over = raycast_blocks([0.5, 0.75, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([over.x, over.face], [4, "west"]);
|
||||
const into = raycast_blocks([0.5, 0.25, 0.5], [1, 0, 0], 20, world)!;
|
||||
assertEquals([into.x, into.face], [2, "west"]);
|
||||
});
|
||||
|
||||
+57
-23
@@ -1,4 +1,4 @@
|
||||
// fishing: cast into water, wait for a bite, reel it in before it gets away
|
||||
// fishing: throw the hook into water, wait for a bite, reel it in before it gets away
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
@@ -9,51 +9,71 @@ import { test_game } from "./helpers.ts";
|
||||
type Msg = any;
|
||||
|
||||
const ROD = "bworld:fishing_rod";
|
||||
// below where the hook floats, even at the bottom of its bobbing, see the bworld mod's fishing
|
||||
// below where the hook floats on the pond's surface at y 100, even at the bottom of its bobbing, see the bworld mod's
|
||||
// fishing
|
||||
const HOOK_UNDER = 0.3;
|
||||
// looking north and down: a throw lands in the pond. steeper, it lands on the stone in front of the player
|
||||
const AT_POND = -0.5;
|
||||
const AT_FLOOR = -1.3;
|
||||
|
||||
// a player on a stone floor at y 99 with water next to them and a rod in hand
|
||||
// a player on a stone floor at y 99, looking north (yaw 0) at a pond in it from z -3 to -9, with a rod in hand
|
||||
async function setup() {
|
||||
const t = await test_game("mods");
|
||||
t.join(1, "alice");
|
||||
for (let x = -3; x <= 3; x++) {
|
||||
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
|
||||
for (let z = -10; z <= 3; z++) {
|
||||
t.game.set_block(x, 99, z, z <= -3 && z >= -9 ? "bworld:water" : "bworld:stone");
|
||||
if (z <= -3 && z >= -9) t.game.set_block(x, 98, z, "bworld:stone");
|
||||
}
|
||||
}
|
||||
t.game.set_block(2, 100, 0, "bworld:water");
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
||||
t.send(1, { type: "chat", text: `/give ${ROD}` });
|
||||
const inventory = () => t.game.players()[0].inventory.to_data();
|
||||
const slot = inventory().findIndex((i: Msg) => i?.id === ROD);
|
||||
t.send(1, { type: "select_slot", slot });
|
||||
const rod = () => inventory()[slot];
|
||||
const cast = () => t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" });
|
||||
const hooks = () => t.game.entities.all().filter((e) => e.type === "bworld:fishing_hook");
|
||||
const aim = (pitch: number) => t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch });
|
||||
// throws the hook and lets it fly until it lands
|
||||
const cast = (pitch = AT_POND) => {
|
||||
aim(pitch);
|
||||
t.send(1, { type: "use_item" });
|
||||
for (let i = 0; i < 20; i++) t.game.tick();
|
||||
};
|
||||
const reel = () => t.send(1, { type: "use_item" });
|
||||
const fish = () => inventory().filter((i: Msg) => i?.id === "bworld:cod" || i?.id === "bworld:salmon");
|
||||
const hooks = () => t.game.entities.all().filter((e) => e.type === "bworld:fishing_hook");
|
||||
// ticks until something bites, which pulls the hook under the water. false if nothing did in 35 seconds
|
||||
const wait_for_bite = () => {
|
||||
for (let i = 0; i < 700; i++) {
|
||||
t.game.tick();
|
||||
if (hooks()[0]?.y < 101 - HOOK_UNDER) return true;
|
||||
if (hooks()[0]?.y < 100 - HOOK_UNDER) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return { ...t, slot, rod, cast, reel, fish, wait_for_bite, hooks };
|
||||
return { ...t, slot, rod, aim, cast, reel, fish, wait_for_bite, hooks };
|
||||
}
|
||||
|
||||
Deno.test("the hook flies forward from the player and floats where it lands in water", async () => {
|
||||
const t = await setup();
|
||||
t.aim(AT_POND);
|
||||
t.send(1, { type: "use_item" });
|
||||
assertEquals(t.rod()?.data, { cast: true });
|
||||
assertEquals(t.hooks().length, 1);
|
||||
const hook = t.hooks()[0];
|
||||
const start = hook.z;
|
||||
t.game.tick();
|
||||
assert(hook.z < start, `${hook.z} isn't north of ${start}`);
|
||||
for (let i = 0; i < 20; i++) t.game.tick();
|
||||
assert(hook.z <= -2 && hook.z >= -9, String(hook.z));
|
||||
assert(Math.abs(hook.x - 0.5) < 0.01, String(hook.x));
|
||||
assert(hook.y > 100 - HOOK_UNDER && hook.y < 100, String(hook.y));
|
||||
});
|
||||
|
||||
Deno.test("cast into water, reel in a fish once something bites", async () => {
|
||||
const t = await setup();
|
||||
t.cast();
|
||||
assertEquals(t.rod()?.data, { cast: true });
|
||||
// the hook floats on the water, and goes under when something bites
|
||||
assertEquals(t.hooks().length, 1);
|
||||
const hook = t.hooks()[0];
|
||||
assertEquals([hook.x, hook.z], [2.5, 0.5]);
|
||||
const floating = hook.y;
|
||||
assert(floating > 100.7 && floating < 101, String(floating));
|
||||
|
||||
const floating = t.hooks()[0].y;
|
||||
assert(t.wait_for_bite());
|
||||
assert(hook.y < floating - 0.1, String(hook.y));
|
||||
assert(t.hooks()[0].y < floating - 0.1, String(t.hooks()[0].y));
|
||||
t.reel();
|
||||
assertEquals(t.fish().length, 1);
|
||||
assertEquals(t.rod()?.data, undefined);
|
||||
@@ -61,6 +81,14 @@ Deno.test("cast into water, reel in a fish once something bites", async () => {
|
||||
assertEquals(t.hooks().length, 0);
|
||||
});
|
||||
|
||||
Deno.test("right clicking a block throws the hook too", async () => {
|
||||
const t = await setup();
|
||||
t.aim(AT_POND);
|
||||
t.send(1, { type: "use_block", x: 0, y: 99, z: -1, face: "top" });
|
||||
assertEquals(t.rod()?.data, { cast: true });
|
||||
assertEquals(t.hooks().length, 1);
|
||||
});
|
||||
|
||||
Deno.test("reeling in before a bite catches nothing", async () => {
|
||||
const t = await setup();
|
||||
t.cast();
|
||||
@@ -69,12 +97,17 @@ Deno.test("reeling in before a bite catches nothing", async () => {
|
||||
assertEquals(t.rod()?.data, undefined);
|
||||
});
|
||||
|
||||
Deno.test("right clicking air with an uncast rod does nothing", async () => {
|
||||
Deno.test("a hook that lands on stone lies there, and nothing bites", async () => {
|
||||
const t = await setup();
|
||||
t.cast(AT_FLOOR);
|
||||
const hook = t.hooks()[0];
|
||||
assert(hook.z > -3, `${hook.z} is in the pond`);
|
||||
assert(Math.abs(hook.y + 0.125 - 100) < 0.01, String(hook.y));
|
||||
assert(!t.wait_for_bite());
|
||||
assertEquals(t.rod()?.data, { cast: true });
|
||||
t.reel();
|
||||
assertEquals(t.rod()?.data, undefined);
|
||||
for (let i = 0; i < 700; i++) t.game.tick();
|
||||
assertEquals(t.fish().length, 0);
|
||||
assertEquals(t.rod()?.data, undefined);
|
||||
});
|
||||
|
||||
Deno.test("switching away from the rod takes the line out", async () => {
|
||||
@@ -92,7 +125,8 @@ Deno.test("switching away from the rod takes the line out", async () => {
|
||||
Deno.test("the line comes out when the water is gone", async () => {
|
||||
const t = await setup();
|
||||
t.cast();
|
||||
t.game.set_block(2, 100, 0, "bworld:stone");
|
||||
const hook = t.hooks()[0];
|
||||
t.game.set_block(Math.floor(hook.x), 99, Math.floor(hook.z), "bworld:stone");
|
||||
t.game.tick();
|
||||
assertEquals(t.rod()?.data, undefined);
|
||||
t.game.tick();
|
||||
|
||||
Reference in New Issue
Block a user