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
+4 -2
View File
@@ -2,7 +2,7 @@
// numbers are minecraft's, so movement_speed is its per tick value, see LivingEntity.travel
export type ModifierOperation = "add_value" | "add_multiplied_base" | "add_multiplied_total";
const OPERATIONS: ModifierOperation[] = ["add_value", "add_multiplied_base", "add_multiplied_total"];
export const MODIFIER_OPERATIONS: ModifierOperation[] = ["add_value", "add_multiplied_base", "add_multiplied_total"];
export interface AttributeModifier {
// unique on the attribute, so it can be replaced or taken off, like "bworld:effect.speed"
@@ -31,6 +31,8 @@ export const ATTRIBUTES = {
movement_speed: attribute("movement_speed", 0.7, 0, 1024),
attack_damage: attribute("attack_damage", 2, 0, 2048),
attack_knockback: attribute("attack_knockback", 0, 0, 5),
// full strength attacks a second, see ServerPlayer.attack_strength
attack_speed: attribute("attack_speed", 4, 0, 1024),
// how far mobs notice players from, in blocks
follow_range: attribute("follow_range", 32, 0, 2048),
// how fast a jump starts, blocks per tick
@@ -167,7 +169,7 @@ export class AttributeMap {
for (const modifier of Array.isArray(entry.modifiers) ? entry.modifiers : []) {
if (
typeof modifier?.id === "string" && typeof modifier.amount === "number" &&
OPERATIONS.includes(modifier.operation)
MODIFIER_OPERATIONS.includes(modifier.operation)
) {
attribute.add_modifier(modifier);
}
+16 -1
View File
@@ -463,6 +463,11 @@ export class GameServer {
this.#send(player, { type: "chat", text });
}
// a mod's server script to its client script, see ctx.net
send_mod_message(player: ServerPlayer, channel: string, data: unknown) {
this.#send(player, { type: "mod", channel, data });
}
teleport(player: ServerPlayer, x: number, y: number, z: number) {
Object.assign(player, { x, y, z });
player.fall_distance = 0;
@@ -765,6 +770,7 @@ export class GameServer {
break;
case "select_slot":
if (is_int(message.slot) && message.slot >= 0 && message.slot < 9) {
if (message.slot !== player.selected_slot) player.attack_strength_ticker = 0;
player.selected_slot = message.slot;
}
break;
@@ -783,6 +789,11 @@ export class GameServer {
case "suggest":
this.#suggest(player, message.id, message.text);
break;
case "mod":
if (typeof message.channel === "string") {
this.mods.receive(player, message.channel, message.data);
}
break;
}
}
@@ -838,7 +849,11 @@ export class GameServer {
return;
}
const source = { type: "physical", cause: "player_attack", attacker: player, by_player: true } as const;
if (entity.hurt(player.attribute(ATTRIBUTES.attack_damage), source)) {
// like minecraft, attacking again before the attack speed allows hits weaker
const strength = player.attack_strength();
player.attack_strength_ticker = 0;
const damage = player.attribute(ATTRIBUTES.attack_damage) * (0.2 + strength * strength * 0.8);
if (entity.hurt(damage, source)) {
player.vitals.add_exhaustion(EXHAUSTION.attack);
}
}
+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,
+10
View File
@@ -66,6 +66,8 @@ export class ServerPlayer extends LivingEntity {
knockback_ticks: number | undefined;
// when the last move came, to tell walking from sprinting
last_move_tick = 0;
// ticks since they last attacked or changed their held item, like minecraft's attackStrengthTicker
attack_strength_ticker = 0;
crafting = new Container(CRAFTING_SIZE);
cursor: Cursor = { item: undefined };
selected_slot = 0;
@@ -94,6 +96,7 @@ export class ServerPlayer extends LivingEntity {
...super.default_attributes(),
// a fist does half a heart, like minecraft
[ATTRIBUTES.attack_damage, 1],
[ATTRIBUTES.attack_speed],
[ATTRIBUTES.physical_shielding],
[ATTRIBUTES.arcane_shielding],
];
@@ -113,6 +116,7 @@ export class ServerPlayer extends LivingEntity {
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
override tick() {
this.base_tick();
this.attack_strength_ticker += 1;
if (!this.dead) {
this.tick_effects();
}
@@ -155,6 +159,12 @@ export class ServerPlayer extends LivingEntity {
this.level.on_death(this, source);
}
// like minecraft's getAttackStrengthScale: 0 right after attacking, filling up to 1 over 20 / attack_speed ticks
attack_strength(): number {
const delay = 20 / this.attribute(ATTRIBUTES.attack_speed);
return Math.min(1, Math.max(0, (this.attack_strength_ticker + 0.5) / delay));
}
get held_item(): ItemStack | undefined {
return this.inventory.get_item(this.selected_slot);
}