Loot tables

This commit is contained in:
2026-10-08 00:54:45 -03:00
parent 757ee74bc7
commit ec2b53c02a
45 changed files with 1733 additions and 150 deletions
+54 -3
View File
@@ -2,7 +2,8 @@
// see docs/modding/. parts not built yet throw when used, saying so
import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from "$/common/everything_registry.ts";
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
import { get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
import { LootTables } from "./loot.ts";
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
import { AIR, ID_MASK } from "$/common/constants.ts";
import { BlockRules } from "$/common/block_rules.ts";
@@ -22,6 +23,7 @@ import type {
ItemComponent,
ItemStack,
LivingRef,
LootContext,
MenuBuilder,
MenuHandle,
MenuType,
@@ -163,6 +165,8 @@ export class ModRuntime {
#next_timer = 1;
#setting_up = true;
#players = new WeakMap<ServerPlayer, Player>();
// loot tables with what mods registered for them, see ctx.loot
readonly loot = new LootTables();
// mod channels, see ctx.net: who listens on each channel
#net_handlers = new Map<string, { mod: string; handler: (player: Player, data: unknown) => void }[]>();
// mod messages each player sent this second, to drop floods
@@ -344,6 +348,8 @@ export class ModRuntime {
}
}
this.loot.check();
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
for (const component of Object.keys(block.components ?? {})) {
if (!this.block_components.has(component) && !this.rules.has(component)) {
@@ -506,6 +512,19 @@ export class ModRuntime {
};
}
// a script's loot context with its block's states read from the world, when it didn't give them
#loot_context(context: LootContext = {}): LootContext {
const block = context.block;
if (!block || block.states) return context;
const world = this.game.world;
const info = world.get_block_info(block.x, block.y, block.z);
if (info?.id !== block.id) return context;
return {
...context,
block: { ...block, states: block_states(world.get_block_value(block.x, block.y, block.z), info) },
};
}
#context(mod: string, version: string): ServerContext {
const setup_only = (what: string) => {
if (!this.#setting_up) {
@@ -759,6 +778,38 @@ export class ModRuntime {
list: (type) => structuredClone(game().recipes.by_type.get(type) ?? []),
},
tags: tag_api(),
loot: {
exists: (id) => this.loot.exists(id),
roll: (id, context) => this.loot.roll(id, this.#loot_context(context)),
fill: (container, id, context) => {
const items = this.loot.roll(id, this.#loot_context(context));
// empty slots in a random order, one item each, like minecraft's chests
const empty = [...Array(container.size).keys()].filter((slot) => !container.get(slot));
for (let i = empty.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[empty[i], empty[j]] = [empty[j], empty[i]];
}
const left: ItemStack[] = [];
for (const item of items) {
const slot = empty.pop();
if (slot === undefined) left.push(item);
else container.set(slot, item);
}
return left;
},
register_condition: (id, condition) => {
setup_only("loot conditions");
this.loot.register_condition(mod, id, condition);
},
register_function: (id, fn) => {
setup_only("loot functions");
this.loot.register_function(mod, id, fn);
},
modify: (table, fn) => {
setup_only("loot modifiers");
this.loot.modify(mod, table, fn);
},
},
containers: {
create: (size) => {
if (!Number.isInteger(size) || size < 1 || size > MAX_CONTAINER_SIZE) {
@@ -1014,7 +1065,7 @@ export function item_api(stack: EngineItemStack): ItemStack {
};
}
function new_stack(item: ItemStack): EngineItemStack {
export function new_stack(item: ItemStack): EngineItemStack {
if (!EverythingRegistry.get("items", item.id)) throw new Error(`unknown item ${item.id}`);
const stack = new EngineItemStack(item.id, item.count);
if (item.data !== undefined) stack.data = structuredClone(item.data);
@@ -1254,7 +1305,7 @@ function living_ref(entity: LivingEntity): LivingRef {
};
}
function entity_ref(entity: Entity): EntityRef {
export function entity_ref(entity: Entity): EntityRef {
return {
id: entity.id,
type: entity.type,