Predict interactions with blocks
This commit is contained in:
+22
-1
@@ -1,7 +1,7 @@
|
||||
// the block rules shared scripts register (see BlockRules in mod_api/shared.ts), run the same way on the server and on
|
||||
// clients: what states a placed block gets, and how blocks change with the blocks beside them. each side has its own
|
||||
// BlockRules, with its own world: the server's is the real one, a client's is what it shows and guesses ahead with
|
||||
import type { BlockPlacement, BlockRules as Rules, SharedWorld } from "./mod_api/shared.ts";
|
||||
import type { BlockInteraction, BlockPlacement, BlockRules as Rules, SharedWorld } from "./mod_api/shared.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
|
||||
import { default_block_value, set_state_value } from "./utils.ts";
|
||||
|
||||
@@ -75,6 +75,27 @@ export class BlockRules {
|
||||
return value;
|
||||
}
|
||||
|
||||
// a player right clicked the block at x y z: its rules' on_interact, every one of them. returns whether any did
|
||||
// something. what they change goes through the side's world, so on a client it's a guess the server corrects
|
||||
interact(
|
||||
block: BlockRegistry,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
interaction: Omit<BlockInteraction, "facing">,
|
||||
yaw: number,
|
||||
): boolean {
|
||||
const at = { x, y, z, id: block.id };
|
||||
const full: BlockInteraction = { ...interaction, facing: horizontal_facing(yaw) };
|
||||
let handled = false;
|
||||
for (const { mod, id, rules, params } of this.#of(block)) {
|
||||
if (!rules.on_interact) continue;
|
||||
handled = this.run(mod, `${id} on_interact`, () => rules.on_interact!(at, params, full)) === true ||
|
||||
handled;
|
||||
}
|
||||
return handled;
|
||||
}
|
||||
|
||||
// tells the blocks beside x y z it changed, so they can change with it like a fence joining it: their rules'
|
||||
// on_neighbor_change. the world's set_state calls this again for what they change, until nothing changes
|
||||
notify_neighbors(get_block: SharedWorld["get_block"], x: number, y: number, z: number) {
|
||||
|
||||
@@ -97,6 +97,14 @@ export interface BlockPlacement extends Position {
|
||||
readonly facing: "north" | "east" | "south" | "west";
|
||||
}
|
||||
|
||||
// a player right clicking face of a block, at hit (0 to 1 on each axis of the block), like minecraft's BlockHitResult.
|
||||
// facing is the way the player looks, along the ground
|
||||
export interface BlockInteraction {
|
||||
readonly face: Face;
|
||||
readonly hit: readonly [number, number, number];
|
||||
readonly facing: "north" | "east" | "south" | "west";
|
||||
}
|
||||
|
||||
// P is the component's params from the block json
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface BlockRules<P = any> {
|
||||
@@ -106,4 +114,8 @@ export interface BlockRules<P = any> {
|
||||
// a block next to it (on any of the 6 sides) was placed, broken or changed its states, like minecraft's
|
||||
// updateShape. neighbor is where, and what's there now
|
||||
on_neighbor_change?(block: SharedBlock, params: P, neighbor: SharedBlock): void;
|
||||
// a player right clicks it, like minecraft's useWithoutItem: changes only its states and its neighbors', which
|
||||
// the clicking client shows right away instead of waiting for the server. return true if it did something, then
|
||||
// nothing is placed. the server runs the server script's on_interact too
|
||||
on_interact?(block: SharedBlock, params: P, interaction: BlockInteraction): boolean | void;
|
||||
}
|
||||
|
||||
+8
-4
@@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air";
|
||||
|
||||
// bump when a client and server of different versions can't play together.
|
||||
// the server rejects a different version before the client downloads anything
|
||||
export const PROTOCOL_VERSION = 19;
|
||||
export const PROTOCOL_VERSION = 20;
|
||||
|
||||
export interface PlayerInfo {
|
||||
id: string;
|
||||
@@ -101,7 +101,8 @@ export type ClientMessage =
|
||||
teleport?: number;
|
||||
swimming?: boolean;
|
||||
}
|
||||
| { type: "break_block"; x: number; y: number; z: number }
|
||||
// seq numbers what this client guessed for it, the server answers with block_ack. see client/level/block_predictions.ts
|
||||
| { type: "break_block"; x: number; y: number; z: number; seq?: number }
|
||||
// the player started hitting a block, for on_click
|
||||
| { type: "hit_block"; x: number; y: number; z: number }
|
||||
// left click on an entity, by id
|
||||
@@ -111,8 +112,8 @@ export type ClientMessage =
|
||||
// right click without looking at a block, for items' on_use
|
||||
| { type: "use_item" }
|
||||
// right click on a block: interact with it, or place the held block against `face`. hit is where on the block it
|
||||
// clicked, 0 to 1 on each axis, like minecraft's click location
|
||||
| { type: "use_block"; x: number; y: number; z: number; face: Faces; hit?: [number, number, number] }
|
||||
// clicked, 0 to 1 on each axis, like minecraft's click location. seq is like break_block's
|
||||
| { type: "use_block"; x: number; y: number; z: number; face: Faces; hit?: [number, number, number]; seq?: number }
|
||||
| { type: "select_slot"; slot: number }
|
||||
// a slot of the open menu, or of menu 0 when none is. shift moves the stack to the other part of the menu instead,
|
||||
// like minecraft's quick move. clicks for a menu that isn't open any more are ignored
|
||||
@@ -208,6 +209,9 @@ export type ServerMessage =
|
||||
// to the players who have the chunk, including the one who caused it, which corrects anything their client
|
||||
// predicted wrong
|
||||
| { type: "set_block"; x: number; y: number; z: number; id: string; state?: number }
|
||||
// the break_block or use_block with this seq is handled, and so is every one before it. the set_blocks it caused
|
||||
// came first, and what they said about blocks this client guessed shows now
|
||||
| { type: "block_ack"; seq: number }
|
||||
// the item a block shows, see BlockRegistry.item_display. to the players who have the chunk, after it and whenever
|
||||
// it changes. null when it shows nothing any more
|
||||
| { type: "set_block_item"; x: number; y: number; z: number; item: ItemData | null }
|
||||
|
||||
Reference in New Issue
Block a user