Curios and artifacts !

This commit is contained in:
2026-09-30 15:22:32 -03:00
parent 342b45d593
commit 3af5f6e54b
45 changed files with 729 additions and 43 deletions
+73 -11
View File
@@ -44,6 +44,7 @@ import type { OpenScreen, ServerPlayer } from "./player.ts";
import { MAX_HEALTH } from "./vitals.ts";
import type { Entity } from "./entity/entity.ts";
import { create_entity } from "./entity/entity_types.ts";
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
import type { ScreenLayout } from "$/common/protocol.ts";
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
@@ -138,6 +139,25 @@ export class ModRuntime {
#next_timer = 1;
#setting_up = true;
#players = new WeakMap<ServerPlayer, Player>();
// 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
#net_counts = new WeakMap<ServerPlayer, { second: number; count: number }>();
// a player's client sent a mod message. clients are untrusted: too big or too many are dropped
receive(player: ServerPlayer, channel: string, data: unknown) {
const second = Math.floor(Date.now() / 1000);
let counts = this.#net_counts.get(player);
if (!counts || counts.second !== second) {
counts = { second, count: 0 };
this.#net_counts.set(player, counts);
}
if (++counts.count > MAX_MOD_MESSAGES_PER_SECOND) return;
if ((JSON.stringify(data) ?? "").length > MAX_MOD_MESSAGE_LENGTH) return;
for (const { mod, handler } of this.#net_handlers.get(channel) ?? []) {
run_guarded(mod, `a ${channel} handler`, () => handler(this.player(player), data));
}
}
// the other way, for apis that take a player
#server_players = new WeakMap<Player, ServerPlayer>();
game!: GameServer;
@@ -498,6 +518,11 @@ export class ModRuntime {
if (!item) throw new Error(`unknown item ${id}`);
return item.tool?.type;
},
components: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
return structuredClone(item.components ?? {});
},
},
recipes: {
furnace_result: (input) => {
@@ -537,7 +562,26 @@ export class ModRuntime {
modal_form: not_yet(mod, "ui.modal_form", "see docs/modding_plan.md"),
open_screen: not_yet(mod, "ui.open_screen", "see docs/modding_plan.md"),
},
net: not_yet_object(mod, "net", "see docs/modding_plan.md"),
net: {
on: (channel, handler) => {
own(channel, "channel");
const handlers = this.#net_handlers.get(channel) ?? [];
handlers.push({ mod, handler: handler as (player: Player, data: unknown) => void });
this.#net_handlers.set(channel, handlers);
},
send: (player, channel, data) => {
own(channel, "channel");
const server_player = this.#server_players.get(player);
if (!server_player || !game().players().includes(server_player)) {
throw new Error(`${player.name} isn't online`);
}
game().send_mod_message(server_player, channel, data);
},
broadcast: (channel, data) => {
own(channel, "channel");
for (const player of game().players()) game().send_mod_message(player, channel, data);
},
},
storage: {
get: (key) => structuredClone(this.storage[mod]?.[key]) as never,
set: (key, value) => {
@@ -587,9 +631,13 @@ export class ModRuntime {
const bottom = Math.max(0, ...options.layout.map((s) => s.y + 1), ...bars.map((b) => b.y + 1));
const layout: ScreenLayout = {
rows: options.rows ?? Math.ceil(bottom),
slots: options.layout.map(({ slot, x, y, output_only }) =>
output_only ? { index: slot, x, y, output: true } : { index: slot, x, y }
),
slots: options.layout.map(({ slot, x, y, output_only, icon }) => ({
index: slot,
x,
y,
...(output_only ? { output: true } : {}),
...(icon ? { icon } : {}),
})),
bars: bars.map(({ x, y, value, max, direction, empty_texture, full_texture }) => ({
x,
y,
@@ -673,14 +721,10 @@ function not_yet(mod: string, name: string, why: string) {
};
}
// every method on it throws, saying what isn't built yet
function not_yet_object<T>(mod: string, name: string, where: string): T {
return new Proxy({}, {
get: (_, prop) => not_yet(mod, `${name}.${String(prop)}`, where),
}) as T;
}
const MAX_CONTAINER_SIZE = 256;
// what a client may send on mod channels, see docs/modding/networking/index.md
const MAX_MOD_MESSAGE_LENGTH = 16 * 1024;
const MAX_MOD_MESSAGES_PER_SECOND = 60;
// who ran a command, as the engine sees it: what mods see plus the engine's player
export interface ServerCommandSource extends CommandSource {
@@ -813,9 +857,27 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
}
player.vitals.add_exhaustion(amount);
},
attribute: (id) => player.attribute(known_attribute(id)),
add_modifier(attribute, modifier) {
const type = known_attribute(attribute);
const { id, amount, operation } = modifier;
if (typeof id !== "string" || typeof amount !== "number" || !Number.isFinite(amount)) {
throw new Error("a modifier needs a string id and a number amount");
}
if (!MODIFIER_OPERATIONS.includes(operation)) throw new Error(`unknown modifier operation ${operation}`);
player.attributes.get(type)?.add_modifier({ id, amount, operation });
},
remove_modifier: (attribute, id) =>
player.attributes.get(known_attribute(attribute))?.remove_modifier(id) ?? false,
};
}
function known_attribute(id: string): AttributeType {
const type = attribute_type(id);
if (!type) throw new Error(`unknown attribute ${id}`);
return type;
}
function entity_ref(entity: Entity): EntityRef {
return {
id: entity.id,