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
+41 -6
View File
@@ -24,6 +24,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_states,
box_overlaps_block,
can_place,
can_stand_on,
@@ -36,7 +37,9 @@ import {
} from "$/common/utils.ts";
import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
import { EXHAUSTION } from "./vitals.ts";
import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import type { LootContext } from "$/common/mod_api/server.ts";
import type { EntityJson } from "$/common/mod_data.ts";
import {
argument,
choice,
@@ -228,6 +231,18 @@ export class GameServer {
on_entity_death: (entity, source) => {
if (entity instanceof ServerPlayer) this.#kill(entity, source);
},
entity_loot: (entity, source) => {
const table = EverythingRegistry.get<EntityJson>("entities", entity.type)?.loot_table;
if (!table) return [];
const killer = source.attacker instanceof ServerPlayer ? source.attacker : undefined;
return this.roll_loot(table, {
position: { x: entity.x, y: entity.y, z: entity.z },
entity: entity_ref(entity),
killed_by_player: source.by_player === true,
player: killer && this.mods.player(killer),
tool: killer && this.mods.player(killer).held_item,
});
},
});
this.mods.storage = upgrade_mod_storage(saved?.mod_storage ?? {});
if (saved) {
@@ -610,6 +625,17 @@ export class GameServer {
}
}
// what a loot table gives, ready to drop. a table that can't be rolled is logged and gives nothing, so a broken
// table never stops a block breaking or a mob dying
roll_loot(table: string, context: LootContext): ItemStack[] {
try {
return this.mods.loot.roll(table, context).map(new_stack);
} catch (e) {
console.error(`Couldn't roll loot table ${table}:`, e);
return [];
}
}
// breaks a block the way breaking it by hand does: its components' on_break, then what it drops. without the
// right tool of a high enough tier (or without a player) blocks that need one drop nothing
destroy_block(x: number, y: number, z: number, player?: ServerPlayer) {
@@ -620,14 +646,23 @@ export class GameServer {
const held = EverythingRegistry.get<ItemRegistry>("items", player?.held_item?.type_id ?? "");
const tool = held?.tool;
const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0);
const drops = info.drop_table && (!info.requires_tool || right_tool);
// a waterlogged block leaves its liquid behind, a source
const drops = !info.requires_tool || right_tool;
const value = this.world.get_block_value(x, y, z);
// rolled while it's still there, for its states
const loot = drops && info.loot_table
? this.roll_loot(info.loot_table, {
position: { x: x + 0.5, y: y + 0.5, z: z + 0.5 },
block: { id: info.id, x, y, z, states: block_states(value, info) },
player: player && this.mods.player(player),
tool: player && this.mods.player(player).held_item,
})
: drops && info.drop_table
? [new ItemStack(info.drop_table)]
: [];
// a waterlogged block leaves its liquid behind, a source
const liquid = is_waterlogged(value, info) ? liquid_block(value, info) : undefined;
this.set_block(x, y, z, liquid?.id ?? AIR_ID, player);
if (drops) {
this.pop_item(x, y, z, new ItemStack(info.drop_table!));
}
for (const stack of loot) this.pop_item(x, y, z, stack);
}
get_or_create_tile(x: number, y: number, z: number): Tile {