Giant chesplate

This commit is contained in:
2026-10-01 16:58:30 -03:00
parent a7835261e1
commit 1fc92a7b00
21 changed files with 148 additions and 51 deletions
+2 -1
View File
@@ -56,8 +56,9 @@ const NO_INPUT: MoveInput = { x: 0, z: 0, jump: false, sprint: false, sneak: fal
export class LocalPlayer extends Player {
client: Client;
inventories = new ClientInventories();
// what the server says, see server/game/vitals.ts. out of 20
// what the server says, see server/game/vitals.ts. food out of 20, health out of max_health
health = 20;
max_health = 20;
food = 20;
saturation = 5;
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
+33 -11
View File
@@ -12,11 +12,14 @@ const HOTBAR_HEIGHT = 22;
// each slot is 20 across, the item sits 3 in from its corner and the selection 1 out
const HOTBAR_CELL = 20;
const HOTBAR_ITEM_INSET = 3;
// hearts and hunger: 10 icons each, 8 apart, above the hotbar's top
// hearts and hunger: 10 icons each, 8 apart, above the hotbar's top. more max health stacks more rows of hearts above,
// 10 apart, closer together when there are many, like minecraft
const ICON_SIZE = 9;
const ICON_SPACING = 8;
const ICONS_ABOVE_HOTBAR = 17;
const MAX_VITAL = 20;
const ICONS_PER_ROW = 10;
const HEART_ROW_SPACING = 10;
const MIN_HEART_ROW_SPACING = 3;
// how long the held item's name shows above the hotbar after it changes, and the last part of that it fades out in
const HELD_NAME_MS = 2000;
const HELD_NAME_FADE_MS = 500;
@@ -40,9 +43,10 @@ export class Hud {
render(client: Client) {
this.#render_hotbar(client.player.inventories);
this.#render_vitals(client);
this.#render_held_name(client.player.inventories);
const top = icons_y() - heart_rows_height(client.player.max_health);
this.#render_held_name(client.player.inventories, top);
this.#render_crosshair(client);
client.chat.render(false, icons_y() - HELD_NAME_GAP);
client.chat.render(false, top - HELD_NAME_GAP);
}
#render_hotbar(inventories: ClientInventories) {
@@ -63,7 +67,8 @@ export class Hud {
}
// like minecraft: switching slots, or a different item ending up in the selected one, shows its name for a bit
#render_held_name(inventories: ClientInventories) {
// top is the top of the hearts
#render_held_name(inventories: ClientInventories, top: number) {
const item = inventories.inventory.get_item(inventories.hotbar_selected);
const held = item ? `${inventories.hotbar_selected} ${item.type_id}` : "";
const now = performance.now();
@@ -79,7 +84,7 @@ export class Hud {
const alpha = Math.min(1, (HELD_NAME_MS - shown_for) / HELD_NAME_FADE_MS);
const name = display_name(item.type_id);
const x = (canvas.width - measure_text(name, TEXT_SCALE)) / 2;
const y = icons_y() - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
const y = top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE;
// a shadow so it reads over bright sky and snow
draw_text(name, x + TEXT_SCALE, y + TEXT_SCALE, TEXT_SCALE, [0.15, 0.15, 0.15, alpha]);
draw_text(name, x, y, TEXT_SCALE, [1, 1, 1, alpha]);
@@ -96,14 +101,19 @@ export class Hud {
const food = Math.ceil(player.food);
const saturation = Math.min(player.saturation, player.food);
for (let i = 0; i < MAX_VITAL / 2; i++) {
const heart_x = left + i * ICON_SPACING * GUI_SCALE;
draw_gui_sprite("heart_container", heart_x, y);
const hearts = Math.ceil(player.max_health / 2);
const row_spacing = heart_row_spacing(player.max_health) * GUI_SCALE;
for (let i = 0; i < hearts; i++) {
const heart_x = left + (i % ICONS_PER_ROW) * ICON_SPACING * GUI_SCALE;
const heart_y = y - Math.floor(i / ICONS_PER_ROW) * row_spacing;
draw_gui_sprite("heart_container", heart_x, heart_y);
const heart = icon_fill(health, i);
if (heart) {
draw_gui_sprite(heart === "full" ? "heart_full" : "heart_half", heart_x, y);
draw_gui_sprite(heart === "full" ? "heart_full" : "heart_half", heart_x, heart_y);
}
}
for (let i = 0; i < ICONS_PER_ROW; i++) {
// hunger goes right to left
const food_x = right - (i * ICON_SPACING + ICON_SIZE) * GUI_SCALE;
draw_gui_sprite("food_empty", food_x, y);
@@ -149,7 +159,19 @@ function icons_y() {
return hotbar_position().y - ICONS_ABOVE_HOTBAR * GUI_SCALE;
}
// whether icon i of a 0 to 20 value is full, half or empty
// how far apart rows of hearts are, in gui pixels
function heart_row_spacing(max_health: number) {
const rows = Math.ceil(max_health / 2 / ICONS_PER_ROW);
return Math.max(HEART_ROW_SPACING - (rows - 2), MIN_HEART_ROW_SPACING);
}
// how much higher the rows of hearts past the first go, on the canvas
function heart_rows_height(max_health: number) {
const rows = Math.ceil(max_health / 2 / ICONS_PER_ROW);
return Math.max(0, rows - 1) * heart_row_spacing(max_health) * GUI_SCALE;
}
// whether icon i of a value, 2 points an icon, is full, half or empty
function icon_fill(value: number, i: number): "full" | "half" | undefined {
if (value >= i * 2 + 2) return "full";
if (value === i * 2 + 1) return "half";
+1
View File
@@ -76,6 +76,7 @@ export class ClientPacketListener {
break;
case "vitals":
client.player.health = message.health;
client.player.max_health = message.max_health;
client.player.food = message.food;
client.player.saturation = message.saturation;
break;
+2 -1
View File
@@ -216,7 +216,8 @@ export interface Player {
give_item(id: Id, count?: number, data?: unknown): void;
send_message(text: string): void;
teleport(x: number, y: number, z: number): void;
// health and hunger, like minecraft: 20 each, saturation is never above hunger
// health and hunger, like minecraft: 20 each, saturation is never above hunger. max_health is the
// bworld:max_health attribute
readonly health: number;
readonly max_health: number;
readonly food: number;
+3 -3
View File
@@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 15;
export const PROTOCOL_VERSION = 16;
export interface PlayerInfo {
id: string;
@@ -211,8 +211,8 @@ export type ServerMessage =
| { type: "time"; time: number }
// answers suggest: start is where in the text the suggestions replace from, up to the caret
| { type: "suggestions"; id: number; start: number; suggestions: { text: string; tooltip?: string }[] }
// the player's own health, hunger and saturation, when they change
| { type: "vitals"; health: number; food: number; saturation: number }
// the player's own health, max health, hunger and saturation, when they change
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number }
// the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair
| { type: "attack_speed"; value: number }
// a mod's server script talking to its client script, see ctx.net
+1 -1
View File
@@ -29,7 +29,7 @@ interface Player {
teleport(x: number, y: number, z: number): void;
readonly health: number; // out of max_health
readonly max_health: number; // 20
readonly max_health: number; // 20, or what the bworld:max_health attribute makes it
readonly food: number; // hunger, out of 20
readonly saturation: number; // never above food
+2 -1
View File
@@ -12,7 +12,8 @@ Some of the base game is still built into the engine: recipe types, the inventor
necklaces and belts, for any mod's items. They're in 2 columns left of the player's inventory, there whenever it's open.
Shift clicking a curio in the inventory puts it in the first slot it fits.
Like the original, it adds no items, and a slot type only shows up once some mod puts an item in its tag.
A slot type only shows up once some item is in its tag. The base game's one curio is the giant chestplate, a body curio
that gives 20 more max health while worn, so the body slot is always there.
### Making an Item a Curio
+13
View File
@@ -0,0 +1,13 @@
{
"format_version": 1,
"item": {
"id": "bworld:giant_chestplate",
"texture": "bworld:giant_chestplate",
"max_stack": 1,
"components": {
"bworld:curio_modifiers": [
{ "attribute": "bworld:max_health", "amount": 20, "operation": "add_value" }
]
}
}
}
+4 -6
View File
@@ -37,7 +37,6 @@ export function register_curios(ctx: ServerContext) {
const container = ctx.containers.create(SLOTS.length);
const ids = ctx.storage.get<Record<string, string>>("curios") ?? {};
ctx.storage.set("curios", { ...ids, [player.name]: container.id });
watch(player, container);
return container;
}
@@ -63,20 +62,19 @@ export function register_curios(ctx: ServerContext) {
applied.set(player.id, now);
}
ctx.events.after.player_join.subscribe(({ player }) => {
const curios = saved_curios(player);
if (curios) watch(player, curios);
});
ctx.events.after.player_leave.subscribe(({ player }) => {
stop_watching.get(player.id)?.();
stop_watching.delete(player.id);
applied.delete(player.id);
});
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel.
// this runs as they join, before their health is checked against their max health, so what their curios give them
// comes back first
ctx.menus.extend_player_menu((menu, player) => {
if (active.length === 0) return;
const curios = curios_of(player);
watch(player, curios);
// what's in slot types no item goes in anymore, since the mod that put items in them was removed, goes back
// to them
for (const [index, slot] of SLOTS.entries()) {
@@ -0,0 +1,6 @@
{
"format_version": 1,
"tag": {
"values": ["bworld:giant_chestplate"]
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 388 B

+8
View File
@@ -104,6 +104,12 @@ export abstract class LivingEntity extends Entity {
this.#health = Math.min(this.max_health, Math.max(0, value));
}
// health from a save. it can be over max_health until the next tick, so max_health modifiers given back while
// loading, like worn curios', keep it
load_health(value: number) {
this.#health = Math.max(0, value);
}
get dead() {
return this.#health <= 0;
}
@@ -439,6 +445,8 @@ export abstract class LivingEntity extends Entity {
}
tick_effects() {
// like minecraft, losing max health, like a max_health modifier going away, takes the health over it
if (this.#health > this.max_health) this.#health = this.max_health;
for (const effect of [...this.effects.values()]) {
if (this.effects.get(effect.type.id) !== effect) continue;
const result = effect.tick(this);
+9 -2
View File
@@ -1372,6 +1372,7 @@ export class GameServer {
}
player.vitals.tick(
player.health,
player.max_health,
(amount) => this.heal_player(player, amount),
(amount) => this.damage_player(player, amount, "starve"),
);
@@ -1533,9 +1534,15 @@ export class GameServer {
sync("extra", items, () => ({ type: "container", container: "extra", items }));
}
// like minecraft, losing max health, like a curio's modifier going away, takes the health over it
if (player.health > player.max_health) player.health = player.max_health;
const { food, saturation } = player.vitals;
const health = player.health;
sync("vitals", [health, food, saturation], () => ({ type: "vitals", health, food, saturation }));
const { health, max_health } = player;
sync(
"vitals",
[health, max_health, food, saturation],
() => ({ type: "vitals", health, max_health, food, saturation }),
);
const attack_speed = player.attribute(ATTRIBUTES.attack_speed);
sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed }));
+3 -2
View File
@@ -45,7 +45,6 @@ import { AIR_ID } from "$/common/protocol.ts";
import type { GameServer } from "./game_server.ts";
import type { ServerPlayer } from "./player.ts";
import { Menu, type MenuSlot } from "./menu.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";
@@ -938,7 +937,9 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
get health() {
return player.health;
},
max_health: MAX_HEALTH,
get max_health() {
return player.max_health;
},
get food() {
return player.vitals.food;
},
+1 -1
View File
@@ -213,7 +213,7 @@ export class ServerPlayer extends LivingEntity {
this.selected_slot = saved.selected_slot;
this.inventory.load(saved.inventory);
this.vitals.load(saved.vitals);
this.health = saved.vitals?.health ?? this.max_health;
this.load_health(saved.vitals?.health ?? this.max_health);
this.skin = saved.skin;
this.slim = saved.slim ?? false;
}
+4 -4
View File
@@ -56,9 +56,9 @@ export class Vitals {
return this.food < MAX_FOOD;
}
// one tick, for someone with that much health. heal and hurt go through the game server, which fires events and
// handles dying
tick(health: number, heal: (amount: number) => void, hurt: (amount: number) => void) {
// one tick, for someone with that much health out of max_health. heal and hurt go through the game server, which
// fires events and handles dying
tick(health: number, max_health: number, heal: (amount: number) => void, hurt: (amount: number) => void) {
if (this.exhaustion > EXHAUSTION_PER_POINT) {
this.exhaustion -= EXHAUSTION_PER_POINT;
if (this.saturation > 0) {
@@ -68,7 +68,7 @@ export class Vitals {
}
}
const hurt_some = health < MAX_HEALTH;
const hurt_some = health < max_health;
if (this.saturation > 0 && hurt_some && this.food >= MAX_FOOD) {
if (++this.#timer >= FAST_REGEN_TICKS) {
const amount = Math.min(this.saturation, 6);
+3 -3
View File
@@ -18,14 +18,14 @@ async function setup() {
Deno.test("the power glove and feral claws go in the hands slot, and only work while worn", async () => {
const t = await setup();
// the hands slot is the only curios one, after the player's own menu's
// the hands slot is the second curios one, after the body slot, which are after the player's own menu's
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1 + 4);
// swapped for the claws by hand: the glove's damage goes, the claws' speed comes
t.send(1, { type: "click", menu: 0, slot: 1, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + 1, button: 0 });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1);
assertEquals(t.alice.attribute(ATTRIBUTES.attack_speed), 4 * 1.3);
@@ -35,7 +35,7 @@ Deno.test("worn artifacts work again after rejoining, and say what they do", asy
const t = await setup();
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
const worn = t.take(1).findLast((m: Msg) => m.type === "container" && m.container === "extra").items;
assertEquals(worn[0].lore, "When worn:\n+4 Attack Damage");
assertEquals(worn[1].lore, "When worn:\n+4 Attack Damage");
t.game.on_disconnect(1);
t.join(1, "alice");
+44 -7
View File
@@ -1,6 +1,6 @@
// curios: accessory slots for items in the curios tags, next to the player's inventory, see
// mods/bworld/scripts/curios.ts
import { assertEquals } from "@std/assert";
import { assert, assertEquals } from "@std/assert";
import { PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { copy_dir, test_game } from "./helpers.ts";
import type { WorldSave } from "$/server/game/saves.ts";
@@ -47,9 +47,11 @@ Deno.test("the slot types some item goes in are in 2 columns left of the invento
const t = await setup();
t.game.on_disconnect(1);
const join = t.join(1, "alice");
// the body slot is there because the base game's giant chestplate goes in it
assertEquals(join.player_menu, [
{ x: -45, y: 7, icon: "bworld:empty_curio_slot" },
{ x: -27, y: 7, icon: "bworld:empty_ring_slot" },
{ x: -27, y: 7, icon: "bworld:empty_body_slot" },
{ x: -45, y: 25, icon: "bworld:empty_ring_slot" },
]);
});
@@ -62,11 +64,11 @@ Deno.test("items only go in the slots they fit, and fall out when the player die
const messages = t.take(1);
assertEquals(last_container(messages, "inventory").slice(0, 3), [null, null, null]);
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:coal", count: 1 });
assertEquals(curios_items(messages), [{ id: "jewelry:amulet", count: 1 }, { id: "jewelry:ring", count: 1 }]);
assertEquals(curios_items(messages), [{ id: "jewelry:amulet", count: 1 }, null, { id: "jewelry:ring", count: 1 }]);
// and they come back out with a shift click, into the inventory
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + 1, button: 0, shift: true });
assertEquals(curios_items(t.take(1))[1], null);
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + 2, button: 0, shift: true });
assertEquals(curios_items(t.take(1))[2], null);
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
const player = t.game.players()[0];
@@ -74,7 +76,7 @@ Deno.test("items only go in the slots they fit, and fall out when the player die
const dropped = t.game.item_entities().map((e) => e.item.type_id).sort();
assertEquals(dropped, ["bworld:coal", "jewelry:amulet", "jewelry:ring"]);
t.game.tick();
assertEquals(curios_items(t.take(1)), [null, null]);
assertEquals(curios_items(t.take(1)), [null, null, null]);
});
Deno.test("curios worn when curios was a mod of its own are still worn", async () => {
@@ -89,7 +91,42 @@ Deno.test("curios worn when curios was a mod of its own are still worn", async (
delete level.mod_storage.bworld.curios;
const loaded = await setup({ ...save, level: JSON.stringify(level) });
const [ring] = loaded.game.players()[0].inventory_menu.extra.slice(1);
const [ring] = loaded.game.players()[0].inventory_menu.extra.slice(2);
assertEquals(ring.container.get_item(ring.index)?.to_data(), { id: "jewelry:ring", count: 1 });
assertEquals(loaded.game.mods.storage.curios, undefined);
});
Deno.test("the giant chestplate goes in the body slot and gives 20 more health while worn", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
const alice = t.game.players()[0];
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
// what curios give is worked out each tick, from what changed
t.game.tick();
assertEquals(curios_items(t.take(1))[1], {
id: "bworld:giant_chestplate",
count: 1,
lore: "When worn:\n+20 Max Health",
});
assertEquals(alice.max_health, 40);
// like minecraft, more max health doesn't heal, regenerating fills it
assertEquals(alice.health, 20);
for (let i = 0; i < 20 * 20; i++) t.game.tick();
assert(alice.health > 20, `regenerated to ${alice.health}`);
const vitals = t.take(1).findLast((m: Msg) => m.type === "vitals");
assertEquals(vitals.max_health, 40);
// it's kept over rejoining, the chestplate's health comes back before the health is checked against it
const health = alice.health;
t.game.on_disconnect(1);
t.join(1, "alice");
assertEquals(t.game.players()[0].health, health);
// taking it off takes the health over 20 away
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_EXTRA + 1, button: 0, shift: true });
t.game.tick();
assertEquals(t.game.players()[0].max_health, 20);
assertEquals(t.game.players()[0].health, 20);
});
+1 -1
View File
@@ -28,7 +28,7 @@ Deno.test("the base game loads from mods/bworld", async () => {
// 7 kinds of log, planks and leaves
assertEquals(bworld(EverythingRegistry.entries("blocks")), 42);
// 42 items (25 of them tools) plus the item forms of the 37 blocks that have one
assertEquals(bworld(EverythingRegistry.entries("items")), 79);
assertEquals(bworld(EverythingRegistry.entries("items")), 80);
// bworld's 18 (4 wood tools, the wood watering can, 7 planks, the fishing rod, the smithing table), the cooking pot,
// stove, cutting board and wood knife are farmersdelight's
assertEquals(game.recipes.shaped.length, 22);
+2 -1
View File
@@ -384,13 +384,14 @@ Deno.test("menus: filters, data, buttons, closing, saving, and dropping what's i
assertEquals(menu_data(loaded.take(1)), undefined, "data is only sent again when it changes");
// breaking it closes the menu, runs on_close, and drops what was inside
const containers = loaded.game.containers.size;
loaded.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
assert(loaded.take(1).some((m) => m.type === "close_menu"));
const logged = loaded.game.mods.storage.testmod.log as Record<string, unknown[]>;
assertEquals(logged.closed, [false, false, false]);
const dropped = loaded.game.item_entities().map((e) => e.item.to_data().id).sort();
assertEquals(dropped, ["bworld:coal", "bworld:stone"]);
assertEquals(loaded.game.containers.size, 0);
assertEquals(loaded.game.containers.size, containers - 1, "its container is gone");
Deno.removeSync(dir, { recursive: true });
});
+6 -6
View File
@@ -12,12 +12,12 @@ Deno.test("exhaustion eats saturation first, then hunger", () => {
const vitals = new Vitals();
const noop = () => {};
vitals.add_exhaustion(4.1);
vitals.tick(20, noop, noop);
vitals.tick(20, 20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [20, 4]);
vitals.saturation = 0;
vitals.add_exhaustion(4);
vitals.tick(20, noop, noop);
vitals.tick(20, 20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [19, 0]);
});
@@ -29,7 +29,7 @@ Deno.test("a full stomach heals, fast with saturation and slowly without, and st
// full hunger and saturation: every 10 ticks, up to a whole heart, paid for with exhaustion
const fast = new Vitals();
for (let i = 0; i < 10; i++) fast.tick(10, heal, damage);
for (let i = 0; i < 10; i++) fast.tick(10, 20, heal, damage);
assertAlmostEquals(healed, 5 / 6);
assertEquals(fast.exhaustion, 5);
@@ -38,7 +38,7 @@ Deno.test("a full stomach heals, fast with saturation and slowly without, and st
const slow = new Vitals();
slow.food = 18;
slow.saturation = 0;
for (let i = 0; i < 80; i++) slow.tick(10, heal, damage);
for (let i = 0; i < 80; i++) slow.tick(10, 20, heal, damage);
assertEquals(healed, 1);
// starving: 1 every 4 seconds, never below 1 health
@@ -46,9 +46,9 @@ Deno.test("a full stomach heals, fast with saturation and slowly without, and st
starving.food = 0;
starving.saturation = 0;
let health = 2;
for (let i = 0; i < 80; i++) starving.tick(health, heal, (amount) => health -= amount);
for (let i = 0; i < 80; i++) starving.tick(health, 20, heal, (amount) => health -= amount);
assertEquals(health, 1);
for (let i = 0; i < 400; i++) starving.tick(health, heal, (amount) => health -= amount);
for (let i = 0; i < 400; i++) starving.tick(health, 20, heal, (amount) => health -= amount);
assertEquals(health, 1);
assertEquals(hurt, 0);
});