Make fishing rods not actually target water

This commit is contained in:
2026-10-07 18:00:37 -03:00
parent b128da0631
commit b6b10e35e2
10 changed files with 374 additions and 70 deletions
+47
View File
@@ -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 {
+32
View File
@@ -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