Fluid data
This commit is contained in:
+100
-13
@@ -4,28 +4,43 @@
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { FluidJson } from "$/common/mod_data.ts";
|
||||
import type { ModListing } from "$/common/mod_loader.ts";
|
||||
import type { FluidApi } from "$/common/mod_api/shared.ts";
|
||||
import { read_bmod } from "$/common/bmod.ts";
|
||||
import { register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { fluid_rules, load_client_mods } from "$/client/mods.ts";
|
||||
import { load_and_check } from "$/tools/check_mods.ts";
|
||||
import { pack_mod } from "$/tools/pack_mod.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
// a mod with only a manifest and the given fluids/ files, by file name
|
||||
function fluid_mod(fluids: Record<string, unknown>) {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" });
|
||||
Deno.mkdirSync(`${dir}/testfluids/fluids`, { recursive: true });
|
||||
Deno.writeTextFileSync(
|
||||
`${dir}/testfluids/manifest.json`,
|
||||
JSON.stringify({ format_version: 1, id: "testfluids", name: "Test Fluids", version: "1.0.0" }),
|
||||
);
|
||||
for (const [file, fluid] of Object.entries(fluids)) {
|
||||
Deno.writeTextFileSync(`${dir}/testfluids/fluids/${file}`, JSON.stringify({ format_version: 1, fluid }));
|
||||
// testfluids: a manifest, the given fluids/ files by file name, and maybe a shared script and an item for it to read
|
||||
function fluid_mod(dir: string, fluids: Record<string, unknown>, shared_script?: string) {
|
||||
const mod = `${dir}/testfluids`;
|
||||
const write = (path: string, json: unknown) => {
|
||||
Deno.mkdirSync(`${mod}/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
|
||||
Deno.writeTextFileSync(`${mod}/${path}`, typeof json === "string" ? json : JSON.stringify(json));
|
||||
};
|
||||
write("manifest.json", {
|
||||
format_version: 1,
|
||||
id: "testfluids",
|
||||
name: "Test Fluids",
|
||||
version: "1.0.0",
|
||||
...(shared_script && { scripts: { shared: "scripts/shared.ts" } }),
|
||||
});
|
||||
for (const [file, fluid] of Object.entries(fluids)) write(`fluids/${file}`, { format_version: 1, fluid });
|
||||
if (shared_script) {
|
||||
write("scripts/shared.ts", shared_script);
|
||||
write("items/maple.json", {
|
||||
format_version: 1,
|
||||
item: { id: "testfluids:maple", texture: "engine:missing", name: "Sugar Maple" },
|
||||
});
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
Deno.test("a mod's fluids/ file registers a fluid, packed into its .bmod", async () => {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" });
|
||||
const syrup = { id: "testfluids:syrup", name: "Maple Syrup", color: "#a0522d" };
|
||||
const dir = fluid_mod({ "syrup.json": syrup });
|
||||
fluid_mod(dir, { "syrup.json": syrup });
|
||||
const mods = load_and_check(dir);
|
||||
assertEquals(mods[0].report.errors, []);
|
||||
const bmod = read_bmod(await pack_mod(mods[0]), "testfluids.bmod");
|
||||
@@ -36,8 +51,80 @@ Deno.test("a mod's fluids/ file registers a fluid, packed into its .bmod", async
|
||||
});
|
||||
|
||||
Deno.test("a fluid with a bad color fails loading", () => {
|
||||
const dir = fluid_mod({ "syrup.json": { id: "testfluids:syrup", color: "brown" } });
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" });
|
||||
fluid_mod(dir, { "syrup.json": { id: "testfluids:syrup", color: "brown" } });
|
||||
const errors = load_and_check(dir)[0].report.errors;
|
||||
assert(errors.some((e) => e.startsWith("fluids/syrup.json:") && e.includes("color")), errors.join("\n"));
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
// syrup is named after the tree in its data and has the color its data says, sap has no rules
|
||||
const RULES = `
|
||||
import type { SharedContext } from "bworld/shared";
|
||||
export function setup(ctx: SharedContext) {
|
||||
ctx.fluids.register("testfluids:syrup", {
|
||||
name: (data: any) => data?.tree && ctx.items.name(data.tree) + " Syrup",
|
||||
color: (data: any) => data?.color,
|
||||
});
|
||||
}
|
||||
`;
|
||||
const FLUIDS = {
|
||||
"syrup.json": { id: "testfluids:syrup", name: "Syrup", color: "#a0522d" },
|
||||
"sap.json": { id: "testfluids:tree_sap", color: "#e8d9a0" },
|
||||
};
|
||||
|
||||
// what the server's and a client's ctx.fluids say about the fluids above
|
||||
async function both_sides(ask: (fluids: FluidApi) => unknown) {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
fluid_mod(dir, FLUIDS, RULES);
|
||||
const { game } = await test_game(dir);
|
||||
const server = ask(game.mods.fluid_rules.api());
|
||||
|
||||
const mod = load_and_check(dir).find((m) => m.id === "testfluids")!;
|
||||
const bmod = read_bmod(await pack_mod(mod), "testfluids.bmod");
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
const listing: ModListing = { id: "testfluids", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 };
|
||||
EverythingRegistry.clear();
|
||||
await load_client_mods([listing], [bmod]);
|
||||
return { server, client: ask(fluid_rules.api()) };
|
||||
}
|
||||
|
||||
Deno.test("a fluid's rules name and color it by its data, the same on the server and clients", async () => {
|
||||
const { server, client } = await both_sides((fluids) => [
|
||||
[
|
||||
fluids.name({ id: "testfluids:syrup", data: { tree: "testfluids:maple" } }),
|
||||
fluids.color({
|
||||
id: "testfluids:syrup",
|
||||
data: { color: "#c87533" },
|
||||
}),
|
||||
],
|
||||
// the rules say nothing, or something that isn't a color, so the json's are used
|
||||
[fluids.name({ id: "testfluids:syrup" }), fluids.color({ id: "testfluids:syrup", data: { color: "brown" } })],
|
||||
// no rules, and no name in its json
|
||||
[
|
||||
fluids.name({ id: "testfluids:tree_sap" }),
|
||||
fluids.color({ id: "testfluids:tree_sap", data: { color: "#000000" } }),
|
||||
],
|
||||
]);
|
||||
assertEquals(server, [["Sugar Maple Syrup", "#c87533"], ["Syrup", "#a0522d"], ["Tree Sap", "#e8d9a0"]]);
|
||||
assertEquals(client, server);
|
||||
});
|
||||
|
||||
Deno.test("fluids are the same when their ids and data are, whatever order the data's keys are in", async () => {
|
||||
const { server, client } = await both_sides((fluids) => {
|
||||
const syrup = (data?: unknown) => ({ id: "testfluids:syrup", data });
|
||||
return [
|
||||
fluids.same(
|
||||
syrup({ tree: "testfluids:maple", grade: "a" }),
|
||||
syrup({ grade: "a", tree: "testfluids:maple" }),
|
||||
),
|
||||
fluids.same(syrup({}), syrup()),
|
||||
fluids.same(syrup({ tree: "testfluids:maple" }), syrup({ tree: "testfluids:birch" })),
|
||||
fluids.same(syrup({ tree: "testfluids:maple" }), syrup()),
|
||||
fluids.same(syrup(), { id: "testfluids:tree_sap" }),
|
||||
];
|
||||
});
|
||||
assertEquals(server, [true, true, false, false, false]);
|
||||
assertEquals(client, server);
|
||||
});
|
||||
|
||||
+4
-1
@@ -16,7 +16,7 @@ import type { SavedChunk, SavedChunks, WorldSave } from "$/server/game/saves.ts"
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { get_state_value, set_state_value, shared_items_api } from "$/common/utils.ts";
|
||||
import type { Face, SharedWorld } from "$/common/mod_api/shared.ts";
|
||||
import { setup as setup_shared } from "$/mods/bworld/scripts/shared.ts";
|
||||
|
||||
@@ -259,6 +259,9 @@ export function client_of(game: GameServer, area: Area) {
|
||||
components: (id) => structuredClone(info(id)!.components ?? {}),
|
||||
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
|
||||
},
|
||||
items: shared_items_api("bworld"),
|
||||
// only its blocks are looked at here
|
||||
fluids: { register() {} },
|
||||
world,
|
||||
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user