Remove duplicate code
This commit is contained in:
+17
-30
@@ -303,14 +303,28 @@ export class MeleeAttackGoal extends Goal {
|
||||
}
|
||||
}
|
||||
|
||||
// goes after the nearest player it can see within follow_range
|
||||
export class NearestAttackablePlayerGoal extends Goal {
|
||||
// picks the mob's target, and keeps it while it's alive and within follow_range
|
||||
abstract class TargetGoal extends Goal {
|
||||
override flags: GoalFlag[] = ["target"];
|
||||
|
||||
constructor(readonly mob: Mob) {
|
||||
super();
|
||||
}
|
||||
|
||||
override can_continue_to_use() {
|
||||
const target = this.mob.target;
|
||||
const mob = this.mob;
|
||||
return target !== undefined && is_alive(target) &&
|
||||
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.mob.target = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// goes after the nearest player it can see within follow_range
|
||||
export class NearestAttackablePlayerGoal extends TargetGoal {
|
||||
#find() {
|
||||
const mob = this.mob;
|
||||
return mob.level.nearest_player(mob.x, mob.y, mob.z, mob.attribute(ATTRIBUTES.follow_range));
|
||||
@@ -320,52 +334,25 @@ export class NearestAttackablePlayerGoal extends Goal {
|
||||
return this.mob.random() < 0.1 && this.#find() !== undefined;
|
||||
}
|
||||
|
||||
override can_continue_to_use() {
|
||||
const target = this.mob.target;
|
||||
const mob = this.mob;
|
||||
return target !== undefined && is_alive(target) &&
|
||||
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
|
||||
}
|
||||
|
||||
override start() {
|
||||
this.mob.target = this.#find();
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.mob.target = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// goes after whoever hurt it last, like minecraft's HurtByTargetGoal, for as long as they're within follow_range
|
||||
export class HurtByTargetGoal extends Goal {
|
||||
override flags: GoalFlag[] = ["target"];
|
||||
export class HurtByTargetGoal extends TargetGoal {
|
||||
// the Mob.times_hurt_by_attacker it last went after someone for, so each hit only starts it once
|
||||
#handled = 0;
|
||||
|
||||
constructor(readonly mob: Mob) {
|
||||
super();
|
||||
}
|
||||
|
||||
can_use() {
|
||||
const attacker = this.mob.last_attacker;
|
||||
return this.mob.times_hurt_by_attacker !== this.#handled && attacker !== undefined && is_alive(attacker);
|
||||
}
|
||||
|
||||
override can_continue_to_use() {
|
||||
const target = this.mob.target;
|
||||
const mob = this.mob;
|
||||
return target !== undefined && is_alive(target) &&
|
||||
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
|
||||
}
|
||||
|
||||
override start() {
|
||||
this.#handled = this.mob.times_hurt_by_attacker;
|
||||
this.mob.target = this.mob.last_attacker;
|
||||
}
|
||||
|
||||
override stop() {
|
||||
this.mob.target = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function is_alive(target: LivingEntity) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ItemEntity } from "./item_entity.ts";
|
||||
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
|
||||
import type { EntityData } from "$/common/protocol.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { wrap_angle } from "$/common/utils.ts";
|
||||
|
||||
// minecraft's despawning distances: gone at once past the first, maybe after a while past the second
|
||||
const DESPAWN_DISTANCE = 128;
|
||||
@@ -401,14 +402,6 @@ export class JumpControl {
|
||||
}
|
||||
}
|
||||
|
||||
// -pi to pi
|
||||
export function wrap_angle(angle: number) {
|
||||
angle %= Math.PI * 2;
|
||||
if (angle >= Math.PI) angle -= Math.PI * 2;
|
||||
if (angle < -Math.PI) angle += Math.PI * 2;
|
||||
return angle;
|
||||
}
|
||||
|
||||
// from one angle towards another the short way round, at most max
|
||||
export function approach_angle(from: number, to: number, max: number) {
|
||||
const difference = Math.max(-max, Math.min(max, wrap_angle(to - from)));
|
||||
|
||||
+36
-60
@@ -134,7 +134,7 @@ export interface SavedLevel {
|
||||
seed: string;
|
||||
// world time in ticks
|
||||
time: number;
|
||||
tiles: SavedTile[];
|
||||
tiles: Tile[];
|
||||
// every ctx.containers container, by id
|
||||
containers: Record<string, (ItemData | null)[]>;
|
||||
players: Record<string, SavedPlayer>;
|
||||
@@ -144,19 +144,6 @@ export interface SavedLevel {
|
||||
mod_storage: Record<string, Record<string, unknown>>;
|
||||
}
|
||||
|
||||
interface SavedTile {
|
||||
id: string;
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// what it shows, see ServerWorld.set_block_item in the mod api
|
||||
item?: ItemData;
|
||||
// false when its components turned ticking off for it
|
||||
ticking?: false;
|
||||
}
|
||||
|
||||
// the loaded mods, see load_mods.ts
|
||||
export interface GameMods {
|
||||
listings: ModListing[];
|
||||
@@ -270,17 +257,7 @@ export class GameServer {
|
||||
container.load(items);
|
||||
this.containers.set(id, container);
|
||||
}
|
||||
for (const tile of saved?.tiles ?? []) {
|
||||
this.world.add_tile({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
ticking: tile.ticking,
|
||||
});
|
||||
}
|
||||
for (const tile of saved?.tiles ?? []) this.world.add_tile({ ...tile });
|
||||
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
|
||||
this.#saved_players = saved?.players ?? {};
|
||||
// a new world is saved straight away
|
||||
@@ -430,15 +407,7 @@ export class GameServer {
|
||||
version: 5,
|
||||
seed: this.world.seed,
|
||||
time: this.world.time,
|
||||
tiles: [...this.world.tiles.values()].map((tile) => ({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
ticking: tile.ticking,
|
||||
})),
|
||||
tiles: [...this.world.tiles.values()].map((tile) => ({ ...tile })),
|
||||
containers: Object.fromEntries(
|
||||
[...this.containers].map(([id, container]) => [id, container.to_data()]),
|
||||
),
|
||||
@@ -1187,19 +1156,8 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
const place = {
|
||||
player: player_api,
|
||||
block: { id: held.block_id, x: tx, y: ty, z: tz },
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before_place = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before_place);
|
||||
if (before_place.cancel) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
const place = this.#place_event(player, { id: held.block_id, x: tx, y: ty, z: tz }, face, hit);
|
||||
if (!place) return;
|
||||
|
||||
this.set_block(tx, ty, tz, held.block_id, player, state);
|
||||
held_slot.amount = held_slot.amount! - 1;
|
||||
@@ -1210,20 +1168,13 @@ export class GameServer {
|
||||
// so a bucket empties like it does placing water anywhere else
|
||||
#waterlog(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const player_api = this.mods.player(player);
|
||||
const place = {
|
||||
player: player_api,
|
||||
block: { id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
|
||||
const place = this.#place_event(
|
||||
player,
|
||||
{ id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before_place = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before_place);
|
||||
if (before_place.cancel) {
|
||||
this.#correct(player, x, y, z);
|
||||
return;
|
||||
}
|
||||
hit,
|
||||
);
|
||||
if (!place) return;
|
||||
const info = this.world.get_block_info(x, y, z)!;
|
||||
this.set_block_state(
|
||||
x,
|
||||
@@ -1235,6 +1186,31 @@ export class GameServer {
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
// the event for a player placing a block, after mods had the chance to cancel it. undefined when one did, and the
|
||||
// player is told what's really there
|
||||
#place_event(
|
||||
player: ServerPlayer,
|
||||
block: { id: string; x: number; y: number; z: number },
|
||||
face: Faces,
|
||||
hit: number[],
|
||||
) {
|
||||
const player_api = this.mods.player(player);
|
||||
const place = {
|
||||
player: player_api,
|
||||
block,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before);
|
||||
if (before.cancel) {
|
||||
this.#correct(player, block.x, block.y, block.z);
|
||||
return undefined;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
// 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) =>
|
||||
|
||||
@@ -7,6 +7,7 @@ import { join } from "@std/path";
|
||||
import { type Bmod, BMOD_EXTENSION, client_copy, read_bmod, write_bmod } from "$/common/bmod.ts";
|
||||
import { check_references, load_order, type LoadedMod } from "$/common/mod_check.ts";
|
||||
import type { ModListing } from "$/common/mod_loader.ts";
|
||||
import { sha256 } from "$/common/utils.ts";
|
||||
|
||||
export interface ServerMod {
|
||||
bmod: Bmod;
|
||||
@@ -74,7 +75,7 @@ export async function load_bmods(dir: string, engine_textures: Iterable<string>)
|
||||
return await Promise.all(ordered.map(async (mod) => {
|
||||
const bmod = by_id.get(mod.id)!.bmod;
|
||||
const client_bytes = write_bmod(client_copy(bmod));
|
||||
const sha256 = await hex_sha256(client_bytes);
|
||||
const hash = await sha256(client_bytes);
|
||||
return {
|
||||
bmod,
|
||||
client_bytes,
|
||||
@@ -82,8 +83,8 @@ export async function load_bmods(dir: string, engine_textures: Iterable<string>)
|
||||
id: bmod.manifest.id,
|
||||
name: bmod.manifest.name,
|
||||
version: bmod.manifest.version,
|
||||
sha256,
|
||||
file: `mods/${sha256}${BMOD_EXTENSION}`,
|
||||
sha256: hash,
|
||||
file: `mods/${hash}${BMOD_EXTENSION}`,
|
||||
size: client_bytes.length,
|
||||
...(bmod.manifest.engine_access ? { engine_access: true } : {}),
|
||||
},
|
||||
@@ -121,8 +122,3 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
texture_json: new Map(bmod.texture_json),
|
||||
};
|
||||
}
|
||||
|
||||
async function hex_sha256(bytes: Uint8Array<ArrayBuffer>) {
|
||||
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
||||
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user