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
+77 -2
View File
@@ -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;