Better block models !
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { Shape } from "$/common/block_shapes.ts";
|
||||
import { type Collisions, eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
@@ -62,6 +63,8 @@ export function damage_source(cause: string): DamageSource {
|
||||
export interface EntityLevel {
|
||||
readonly current_tick: number;
|
||||
has_collision(x: number, y: number, z: number): boolean;
|
||||
// what it bumps into at a block, see common/physics.ts
|
||||
collision_shape(x: number, y: number, z: number): boolean | Shape;
|
||||
// water, and other blocks you're in instead of on
|
||||
is_liquid(x: number, y: number, z: number): boolean;
|
||||
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined;
|
||||
@@ -254,7 +257,7 @@ export abstract class Entity {
|
||||
|
||||
// falls and moves by its velocity for one tick (see move_body), and counts how far it fell. returns what it hit
|
||||
move(): Collisions {
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.has_collision(x, y, z);
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
||||
const from_y = this.y;
|
||||
const collisions = move_body(
|
||||
this,
|
||||
|
||||
@@ -89,6 +89,10 @@ export class EntityManager implements EntityLevel {
|
||||
return this.#host.world.has_collision(x, y, z);
|
||||
}
|
||||
|
||||
collision_shape(x: number, y: number, z: number) {
|
||||
return this.#host.world.collision_shape(x, y, z);
|
||||
}
|
||||
|
||||
is_liquid(x: number, y: number, z: number) {
|
||||
return is_liquid_block(this.#host.world.get_block_info(x, y, z));
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import type { WorldgenSetup } from "$/common/generation.ts";
|
||||
import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts";
|
||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||
import {
|
||||
block_value,
|
||||
box_overlaps_block,
|
||||
can_place,
|
||||
can_stand_on,
|
||||
@@ -31,6 +32,7 @@ import {
|
||||
placement_target,
|
||||
set_state_value,
|
||||
} from "$/common/utils.ts";
|
||||
import { type Shape, shape_at } from "$/common/block_shapes.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import {
|
||||
@@ -1007,10 +1009,10 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
|
||||
if (
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)?.has_collision &&
|
||||
this.#occupied(tx, ty, tz)
|
||||
) {
|
||||
const state = placed_state(held.block_id, player);
|
||||
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id);
|
||||
const value = state === undefined ? default_block_value(0, placing) : block_value(0, state);
|
||||
if (placing?.has_collision && this.#occupied(tx, ty, tz, shape_at(placing, value))) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
@@ -1028,22 +1030,23 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
this.set_block(tx, ty, tz, held.block_id, player, placed_state(held.block_id, player));
|
||||
this.set_block(tx, ty, tz, held.block_id, player, state);
|
||||
held_slot.amount = held_slot.amount! - 1;
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
// whether a mob's or player's collision box overlaps the block at x, y, z
|
||||
#occupied(x: number, y: number, z: number) {
|
||||
// whether a mob's or player's collision box overlaps a block with that shape at x, y, z
|
||||
#occupied(x: number, y: number, z: number, shape: Shape) {
|
||||
return this.players().some((player) =>
|
||||
box_overlaps_block(
|
||||
{ x: player.x, y: player.y, z: player.z, width: PLAYER_WIDTH, height: player.height },
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
shape,
|
||||
)
|
||||
) || this.entities.entities_near(x + 0.5, y, z + 0.5, 3).some((entity) =>
|
||||
entity instanceof Mob && box_overlaps_block(entity, x, y, z)
|
||||
entity instanceof Mob && box_overlaps_block(entity, x, y, z, shape)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation
|
||||
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
||||
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
|
||||
import { LruMap } from "./lru.ts";
|
||||
import { collision_shape_at, EMPTY_SHAPE, type Shape } from "$/common/block_shapes.ts";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
import { encode_level_chunk } from "$/common/chunk_streaming.ts";
|
||||
import type { ItemData } from "$/common/inventory.ts";
|
||||
@@ -158,6 +159,14 @@ export class ServerWorld {
|
||||
return this.get_block_info(x, y, z)?.has_collision ?? false;
|
||||
}
|
||||
|
||||
// what entities bump into there, see common/physics.ts
|
||||
collision_shape(x: number, y: number, z: number): Shape {
|
||||
const value = this.get_block_value(x, y, z);
|
||||
const nid = value & ID_MASK;
|
||||
const block = nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
return block ? collision_shape_at(block, value) : EMPTY_SHAPE;
|
||||
}
|
||||
|
||||
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined {
|
||||
const nid = this.get_block_nid(x, y, z);
|
||||
return nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
|
||||
Reference in New Issue
Block a user