Files
bworld/client/game_mode.ts
T

157 lines
6.6 KiB
TypeScript

import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AIR, FACE_OFFSETS, TICK_DELTA } from "$/common/constants.ts";
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 } 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";
import { RemoteEntity } from "./entity/remote_entity.ts";
import { Player } from "./entity/player.ts";
import type { Entity } from "./entity/entity.ts";
// breaking and using blocks against a server, like minecraft's MultiPlayerGameMode. the client times breaking
// and guesses the results so they feel instant, the server decides what really happens
export class MultiPlayerGameMode {
client: Client;
// the block being broken, and how far along it is in seconds out of destroy_time
destroy_pos: { x: number; y: number; z: number } | undefined;
destroy_progress = 0;
destroy_time = 0;
constructor(client: Client) {
this.client = client;
}
// the click that starts breaking, for mods' on_click
start_destroy_block(hit: BlockHitResult) {
this.client.connection.send({ type: "hit_block", x: hit.x, y: hit.y, z: hit.z });
}
// called every tick the attack button is held on a block
continue_destroy_block(hit: BlockHitResult) {
const block_info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", hit.block)!;
const tool = this.held_item()?.tool;
// looking at another block starts it over
const pos = this.destroy_pos;
if (!pos || pos.x !== hit.x || pos.y !== hit.y || pos.z !== hit.z) {
this.destroy_progress = 0;
}
this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z };
this.destroy_time = block_info.toughness ?? 9999;
this.destroy_progress += TICK_DELTA * (tool && tool.type === block_info.tool_to_break ? tool.speed : 1);
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.destroy_progress = 0;
this.destroy_time = 0;
}
}
stop_destroy_block() {
this.destroy_pos = undefined;
this.destroy_progress = 0;
this.destroy_time = 0;
}
// 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 });
// 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
const value = place_block &&
block_rules.placement_value(place_block, this.client.player.yaw, {
...target,
face: hit.face,
hit: hit.hit,
});
const placeable = place_id !== undefined && can_place(
place_block,
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", current),
below_id,
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, value!));
if (!block_info.interactive && place_id && placeable) {
level.add_block({ ...target, id: place_id }, value! >>> 16);
predict_neighbors(target.x, target.y, target.z);
const slot = this.#held_slot();
slot.amount = slot.amount! - 1;
}
}
// q on a slot: throws one, or the whole stack. the server spawns the item, this only takes it out right away
// q on a menu's slot, whose item is in container at index on this client
drop_item(container: Container, index: number, menu: number, slot: number, all: boolean) {
const item = container.get_item(index);
if (!item) {
return;
}
this.client.connection.send({ type: "drop_item", menu, slot, all });
item.amount = all ? 0 : item.amount - 1;
container.set_item(index, item.amount > 0 ? item : undefined);
}
// left click on an entity or another player, the server decides whether it's hurt
attack(entity: Entity) {
this.client.player.send_position_now();
this.client.connection.send({ type: "attack_entity", id: entity.id });
this.client.player.last_attack = performance.now();
}
// left click on nothing: a swing at the air, which uses up the attack cooldown like an attack
swing() {
this.client.connection.send({ type: "swing" });
this.client.player.last_attack = performance.now();
}
// right click on nothing
use_item() {
this.client.connection.send({ type: "use_item" });
}
// whether a mob's or player's collision box overlaps the block, the server doesn't let solid blocks go there. mobs
// are the mod entities that aren't scripted, players are this one and the others
#occupied(x: number, y: number, z: number, block: BlockRegistry, value: number) {
const shape = collision_shape_at(block, value);
for (const entity of this.client.level.entities.values()) {
const json = entity instanceof RemoteEntity
? EverythingRegistry.get<EntityJson>("entities", entity.type)
: undefined;
const blocks = entity instanceof Player || (json !== undefined && json.behavior !== "scripted");
if (blocks && box_overlaps_block(entity, x, y, z, shape)) return true;
}
return false;
}
#held_slot() {
const inventories = this.client.player.inventories;
return inventories.inventory.get_slot(inventories.hotbar_selected);
}
held_item() {
return EverythingRegistry.get<ItemRegistry>("items", this.#held_slot().type_id ?? "");
}
}