Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c6be61dd0 | ||
|
|
9bf6b98953 |
@@ -3,7 +3,7 @@
|
||||
|
||||
// turning and moving the parts of models: entity models' bones, the player's parts and what's worn on them. no
|
||||
// drawing here, so tests can use it
|
||||
import type { EntityBoneJson, EntityModelJson } from "@/common/mod_data.ts";
|
||||
import type { EntityBoneJson, EntityCubeJson, EntityModelJson } from "@/common/mod_data.ts";
|
||||
|
||||
export type Vec3 = [number, number, number];
|
||||
|
||||
@@ -17,11 +17,12 @@ export function model_pivot(bone: EntityBoneJson): Vec3 {
|
||||
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
|
||||
}
|
||||
|
||||
// its own rotation around its pivot. mirroring x turns x and y rotations the other way
|
||||
export function bone_matrix(bone: EntityBoneJson): Matrix {
|
||||
if (!bone.rotation) return IDENTITY;
|
||||
const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180);
|
||||
return rotation_around(model_pivot(bone), -rx, -ry, rz);
|
||||
// a bone's or a cube's own rotation around its pivot. mirroring x turns x and y rotations the other way
|
||||
export function own_matrix(part: EntityBoneJson | EntityCubeJson): Matrix {
|
||||
if (!part.rotation) return IDENTITY;
|
||||
const [rx, ry, rz] = part.rotation.map((degrees) => degrees * Math.PI / 180);
|
||||
const [px, py, pz] = part.pivot ?? [0, 0, 0];
|
||||
return rotation_around([-px, py, pz], -rx, -ry, rz);
|
||||
}
|
||||
|
||||
// every bone's matrix: its own rotation, then pose's for it (like a head looking or a limb swinging), then its
|
||||
@@ -29,7 +30,7 @@ export function bone_matrix(bone: EntityBoneJson): Matrix {
|
||||
export function bone_matrices(model: EntityModelJson, pose: (bone: EntityBoneJson) => Matrix): Map<string, Matrix> {
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = multiply(pose(bone), bone_matrix(bone));
|
||||
let matrix = multiply(pose(bone), own_matrix(bone));
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import type { Texture } from "@/client/renderer/mod.ts";
|
||||
import type { SpriteRegion } from "@/common/constants.ts";
|
||||
import type { EntityBoneJson, EntityModelJson } from "@/common/mod_data.ts";
|
||||
import { apply, bone_matrices, type Matrix, type Vec3 } from "./model_math.ts";
|
||||
import { apply, bone_matrices, type Matrix, multiply, own_matrix, type Vec3 } from "./model_math.ts";
|
||||
import { push_box, type VertexSink } from "./player_renderer.ts";
|
||||
|
||||
// pose is each bone's turn before its parent's, like a head looking or a limb swinging. to_world takes the model's
|
||||
@@ -28,9 +28,9 @@ export function push_model(
|
||||
const matrices = bone_matrices(model, pose);
|
||||
for (const bone of model.bones) {
|
||||
if (only && !only(bone)) continue;
|
||||
const matrix = matrices.get(bone.name)!;
|
||||
const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz));
|
||||
for (const cube of bone.cubes) {
|
||||
const matrix = multiply(matrices.get(bone.name)!, own_matrix(cube));
|
||||
const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz));
|
||||
const inflate = cube.inflate ?? 0;
|
||||
const [ox, oy, oz] = cube.origin;
|
||||
const [w, h, d] = cube.size;
|
||||
|
||||
@@ -576,6 +576,9 @@ export interface ItemApi {
|
||||
fluid_capacity(id: Id): number | undefined;
|
||||
// the tool type it is, like "pickaxe" or "shovel", from the item's "tool"
|
||||
tool(id: Id): string | undefined;
|
||||
// takes durability off a tool, kept in its data's damage, like ItemStack.hurtAndBreak. true when that used it up.
|
||||
// does nothing to items that aren't tools
|
||||
damage(item: ItemStack, amount: number): boolean;
|
||||
// what one of it sells for with this data, or undefined if it can't be sold: the item's "base_sell_value", and a full
|
||||
// fluid container's fluid on top, rounded up. see docs/modding/resources/fluids.md
|
||||
sell_value(id: Id, data?: unknown): number | undefined;
|
||||
|
||||
+12
-2
@@ -501,6 +501,10 @@ const entity_cube = z.object({
|
||||
uv: z.tuple([z.number(), z.number()], "must be [u, v], per face uvs aren't supported"),
|
||||
// grows the box this many pixels each way without moving its texture
|
||||
inflate: z.number().optional(),
|
||||
// what rotation turns the box around, inside its bone
|
||||
pivot: vec3.optional(),
|
||||
// degrees around x, y then z
|
||||
rotation: vec3.optional(),
|
||||
});
|
||||
|
||||
export type EntityCubeJson = z.infer<typeof entity_cube>;
|
||||
@@ -932,8 +936,14 @@ export function entity_model_from_geo(json: unknown, id: string): { model?: Enti
|
||||
...(rotation === undefined ? {} : { rotation }),
|
||||
cubes: cubes.map((cube) => {
|
||||
if (!is_object(cube)) return cube;
|
||||
const { origin, size, uv, inflate } = cube;
|
||||
return { origin, size, uv, ...(inflate === undefined ? {} : { inflate }) };
|
||||
const { origin, size, uv, inflate, pivot, rotation } = cube;
|
||||
return {
|
||||
origin,
|
||||
size,
|
||||
uv,
|
||||
...(inflate === undefined ? {} : { inflate }),
|
||||
...(rotation === undefined ? {} : { pivot: pivot ?? [0, 0, 0], rotation }),
|
||||
};
|
||||
}),
|
||||
};
|
||||
}),
|
||||
|
||||
@@ -193,7 +193,7 @@ get the id `<mod id>:<name>`. From the file's first geometry, bworld uses:
|
||||
|
||||
- the description's `texture_width` and `texture_height`,
|
||||
- each bone's `name`, `parent`, `pivot` and `rotation`,
|
||||
- each cube's `origin`, `size`, `uv` and `inflate`.
|
||||
- each cube's `origin`, `size`, `uv`, `inflate`, `pivot` and `rotation`.
|
||||
|
||||
Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to
|
||||
a block with its feet at 0, and shows the way Blockbench shows it.
|
||||
|
||||
@@ -212,6 +212,6 @@ blowing them along does it every tick. Their client moves them, so it's sent the
|
||||
|
||||
## Death
|
||||
|
||||
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
|
||||
[Tools](../items/tools.md#durability)). They respawn at spawn right away with full health and hunger, and `player_death`
|
||||
fires.
|
||||
When a player dies, `player_death` fires while they're still where they died, then they respawn at spawn right away with
|
||||
full health and hunger. The engine leaves what they carried alone: the base game's `player_death` handler drops it all
|
||||
where they died, and their tools lose durability (see [Tools](../items/tools.md#durability)).
|
||||
|
||||
@@ -249,6 +249,7 @@ ctx.items.max_stack("bworld:wood_pickaxe"); // 1
|
||||
ctx.items.max_stack(id, data); // the same for a stack with this data, fewer for a full fluid container
|
||||
ctx.items.fluid_capacity("bworld:bucket"); // how much a fluid container holds full, or undefined
|
||||
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
|
||||
ctx.items.damage(item, 1); // takes durability off a tool stack, true when that used it up
|
||||
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
|
||||
ctx.items.sell_value(id, data); // the same for a stack with this data, with a full fluid container's fluid on top
|
||||
|
||||
|
||||
@@ -51,3 +51,6 @@ dropping.
|
||||
|
||||
The damage a tool has taken is kept in its stack's `data.damage`, and a worn tool shows a bar under it in its slot,
|
||||
going from green to red as it wears down. Upgrading a tool at the smithing table keeps its data, damage included.
|
||||
|
||||
A server script wears a tool down with `ctx.items.damage(item, amount)`, which adds to the stack's `data.damage` and
|
||||
returns true when that used it up. It does nothing to items that aren't tools. The base game's death drops use it.
|
||||
|
||||
@@ -39,6 +39,22 @@ export function setup(ctx: ServerContext) {
|
||||
register_stove(ctx);
|
||||
register_cooking_pot(ctx);
|
||||
register_cutting_board(ctx);
|
||||
register_death_drops(ctx);
|
||||
}
|
||||
|
||||
// everything a player carried falls out where they died. tools lose 1 durability first, and ones that run out break
|
||||
// instead of dropping
|
||||
function register_death_drops(ctx: ServerContext) {
|
||||
ctx.events.after.player_death.subscribe(({ player }) => {
|
||||
const { x, y, z } = player.position;
|
||||
for (let i = 0; i < player.inventory.size; i++) {
|
||||
const item = player.inventory.get(i);
|
||||
if (!item) continue;
|
||||
player.inventory.set(i, undefined);
|
||||
if (ctx.items.damage(item, 1)) continue;
|
||||
ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
||||
|
||||
@@ -1969,20 +1969,11 @@ export class GameServer {
|
||||
player.heal(amount);
|
||||
}
|
||||
|
||||
// like minecraft: everything they carried falls out where they died, and they start over at spawn. tools lose 1
|
||||
// durability first, and ones that run out break instead of dropping
|
||||
// they start over at spawn. what happens to what they carried is up to mods' player_death, which runs while
|
||||
// they're still where they died
|
||||
#kill(player: ServerPlayer, source: DamageSource) {
|
||||
const cause = source.cause;
|
||||
const { x, y, z } = player;
|
||||
this.close_menu(player);
|
||||
for (let i = 0; i < player.inventory.size; i++) {
|
||||
const item = player.inventory.get_item(i);
|
||||
if (item) {
|
||||
player.inventory.set_item(i, undefined);
|
||||
if (item.damage(1)) continue;
|
||||
this.pop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), item);
|
||||
}
|
||||
}
|
||||
this.mods.after.player_death.emit({ player: this.mods.player(player), cause });
|
||||
this.#broadcast({ type: "chat", text: death_message(player.name, source) });
|
||||
player.vitals.reset();
|
||||
|
||||
@@ -1002,6 +1002,12 @@ export class ModRuntime {
|
||||
if (!item) throw new Error(`unknown item ${id}`);
|
||||
return item.tool?.type;
|
||||
},
|
||||
damage: (item, amount) => {
|
||||
const stack = EngineItemStack.from_data(item);
|
||||
const used_up = stack.damage(amount);
|
||||
item.data = stack.data;
|
||||
return used_up;
|
||||
},
|
||||
sell_value: shared_items_api(mod, this.fluid_rules).sell_value,
|
||||
components: (id) => {
|
||||
const item = EverythingRegistry.get<ItemRegistry>("items", id);
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
||||
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
|
||||
import { entity_model_from_geo, validate_entity } from "@/common/mod_data.ts";
|
||||
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
||||
import { entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts";
|
||||
import type { Texture } from "@/client/renderer/mod.ts";
|
||||
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
||||
import { push_model } from "@/client/rendering/model_renderer.ts";
|
||||
import { Mob } from "@/server/game/entity/mob.ts";
|
||||
import { create_entity } from "@/server/game/entity/entity_types.ts";
|
||||
import { container_items, test_game } from "./helpers.ts";
|
||||
@@ -39,6 +42,35 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
|
||||
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
|
||||
});
|
||||
|
||||
Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => {
|
||||
const pivot = [1, 2, 3];
|
||||
const rotation = [30, -45, 60];
|
||||
const cube = { origin: [0, 0, 0], size: [2, 3, 4], uv: [0, 0] };
|
||||
const geometry = {
|
||||
description: { texture_width: 16, texture_height: 16 },
|
||||
bones: [
|
||||
{ name: "turned_bone", pivot, rotation, cubes: [cube] },
|
||||
{ name: "turned_cube", pivot: [0, 0, 0], cubes: [{ ...cube, pivot, rotation }] },
|
||||
{ name: "still", pivot: [0, 0, 0], cubes: [cube] },
|
||||
],
|
||||
};
|
||||
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
|
||||
assertEquals(problems, []);
|
||||
const points = (name: string) => {
|
||||
const out: number[] = [];
|
||||
const atlas = { width: 16, height: 16 } as Texture;
|
||||
const region = { x: 0, y: 0, width: 16, height: 16 };
|
||||
const only = (bone: EntityBoneJson) => bone.name === name;
|
||||
push_model(model!, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z) => {
|
||||
out.push(x, y, z);
|
||||
});
|
||||
return out;
|
||||
};
|
||||
const turned = points("turned_cube");
|
||||
points("turned_bone").forEach((n, i) => assertAlmostEquals(turned[i], n, 1e-9));
|
||||
assertNotEquals(turned, points("still"));
|
||||
});
|
||||
|
||||
Deno.test("an entity needs a texture and a size", () => {
|
||||
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0.5, height: 1 }), []);
|
||||
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0, height: 1, max_health: -1 }), [
|
||||
|
||||
Reference in New Issue
Block a user