Predict interactions with blocks
This commit is contained in:
+41
-11
@@ -4,7 +4,7 @@ import type { Client } from "./client.ts";
|
||||
import type { BlockHitResult } from "./level/client_level.ts";
|
||||
import type { Container } from "$/common/inventory.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { box_overlaps_block, can_place, placement_target, waterlog_placement } from "$/common/utils.ts";
|
||||
import { box_overlaps_block, can_place, liquid_block, placement_target, waterlog_placement } from "$/common/utils.ts";
|
||||
import { block_rules, predict_neighbors } from "./mods.ts";
|
||||
import { collision_shape_at } from "$/common/block_shapes.ts";
|
||||
import type { EntityJson } from "$/common/mod_data.ts";
|
||||
@@ -47,9 +47,11 @@ export class MultiPlayerGameMode {
|
||||
|
||||
if (this.destroy_progress >= this.destroy_time) {
|
||||
// show it right away, the server decides drops and corrects us if it disagrees
|
||||
this.client.level.break_block(hit.x, hit.y, hit.z);
|
||||
predict_neighbors(hit.x, hit.y, hit.z);
|
||||
this.client.connection.send({ type: "break_block", x: hit.x, y: hit.y, z: hit.z });
|
||||
this.#predict((seq) => {
|
||||
this.client.level.break_block(hit.x, hit.y, hit.z);
|
||||
predict_neighbors(hit.x, hit.y, hit.z);
|
||||
this.client.connection.send({ type: "break_block", x: hit.x, y: hit.y, z: hit.z, seq });
|
||||
});
|
||||
this.destroy_progress = 0;
|
||||
this.destroy_time = 0;
|
||||
}
|
||||
@@ -63,26 +65,42 @@ export class MultiPlayerGameMode {
|
||||
|
||||
// right click on a block
|
||||
use_item_on(hit: BlockHitResult) {
|
||||
const level = this.client.level;
|
||||
// the server checks that the block isn't going inside the player with where it is now, like this does
|
||||
this.client.player.send_position_now();
|
||||
this.client.connection.send({ type: "use_block", x: hit.x, y: hit.y, z: hit.z, face: hit.face, hit: hit.hit });
|
||||
this.#predict((seq) => {
|
||||
const { x, y, z, face } = hit;
|
||||
this.client.connection.send({ type: "use_block", x, y, z, face, hit: hit.hit, seq });
|
||||
this.#guess_use(hit);
|
||||
});
|
||||
}
|
||||
|
||||
// what using a block will do, shown right away
|
||||
#guess_use(hit: BlockHitResult) {
|
||||
const level = this.client.level;
|
||||
|
||||
// the shared rules' on_interact, like a trapdoor opening, shows right away. the server runs them too and corrects
|
||||
// this if it disagrees. when they do something nothing is placed, like on the server
|
||||
// an item aimed at liquids, like a bucket, uses the liquid in a waterlogged block instead, like on the server
|
||||
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
|
||||
const held = this.held_item();
|
||||
const value = level.get_block_value(hit.x, hit.y, hit.z);
|
||||
const used = (held?.targets_liquids && liquid_block(value, block_info)) || block_info;
|
||||
const interaction = { face: hit.face, hit: hit.hit };
|
||||
if (block_rules.interact(used, hit.x, hit.y, hit.z, interaction, this.client.player.yaw)) return;
|
||||
|
||||
// guess that it places the held block, unless the block does something when used. the same rules as the server:
|
||||
// into the clicked block when it's replaceable, and only where place_in and place_on allow
|
||||
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
|
||||
const target = placement_target(hit.x, hit.y, hit.z, FACE_OFFSETS[hit.face], block_info);
|
||||
const current = level.get_block(target.x, target.y, target.z);
|
||||
const below = level.get_block(target.x, target.y - 1, target.z);
|
||||
const below_id = EverythingRegistry.get_by_id<BlockRegistry>("blocks", below)?.id ?? AIR_ID;
|
||||
const held = this.held_item();
|
||||
// items with components might do something else on the server, like on_use
|
||||
const place_id = held?.components ? undefined : held?.block_id;
|
||||
const place_block = place_id !== undefined
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", place_id)
|
||||
: undefined;
|
||||
// with the states the server will give it, see BlockRules.placement_value, and holding the liquid it goes into
|
||||
const value = place_block &&
|
||||
const place_value = place_block &&
|
||||
waterlog_placement(
|
||||
block_rules.placement_value(place_block, this.client.player.yaw, {
|
||||
...target,
|
||||
@@ -98,9 +116,9 @@ export class MultiPlayerGameMode {
|
||||
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
|
||||
level.get_block_value(target.x, target.y, target.z),
|
||||
below_id,
|
||||
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, value!));
|
||||
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, place_value!));
|
||||
if (!block_info.interactive && place_id && placeable) {
|
||||
level.add_block({ ...target, id: place_id }, value! >>> 16);
|
||||
level.add_block({ ...target, id: place_id }, place_value! >>> 16);
|
||||
predict_neighbors(target.x, target.y, target.z);
|
||||
const slot = this.#held_slot();
|
||||
slot.amount = slot.amount! - 1;
|
||||
@@ -151,6 +169,18 @@ export class MultiPlayerGameMode {
|
||||
return false;
|
||||
}
|
||||
|
||||
// runs guess with the blocks it changes kept track of, like minecraft's startPrediction. what the server says about
|
||||
// them waits until it answers the message guess sends with seq, see client/level/block_predictions.ts
|
||||
#predict(guess: (seq: number) => void) {
|
||||
const predictions = this.client.level.predictions;
|
||||
const seq = predictions.start();
|
||||
try {
|
||||
guess(seq);
|
||||
} finally {
|
||||
predictions.end();
|
||||
}
|
||||
}
|
||||
|
||||
#held_slot() {
|
||||
const inventories = this.client.player.inventories;
|
||||
return inventories.inventory.get_slot(inventories.hotbar_selected);
|
||||
|
||||
Reference in New Issue
Block a user