Engine api
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// what engine access mods get as engine.server, in server scripts. see docs/modding/misc/engine.md
|
||||
import { GameServer } from "./game_server.ts";
|
||||
import { ServerWorld } from "./world.ts";
|
||||
import { ServerPlayer } from "./player.ts";
|
||||
import { ModRuntime } from "./mod_runtime.ts";
|
||||
import { Menu, PlayerMenu } from "./menu.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { Entity } from "./entity/entity.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { Mob } from "./entity/mob.ts";
|
||||
import { ItemEntity } from "./entity/item_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import {
|
||||
FloatGoal,
|
||||
Goal,
|
||||
GoalSelector,
|
||||
HurtByTargetGoal,
|
||||
LookAtPlayerGoal,
|
||||
MeleeAttackGoal,
|
||||
NearestAttackablePlayerGoal,
|
||||
PanicGoal,
|
||||
RandomLookAroundGoal,
|
||||
RandomStrollGoal,
|
||||
} from "./entity/goals.ts";
|
||||
|
||||
// built when it's exposed, not when this module loads, so import cycles can't leave anything undefined
|
||||
export function server_engine() {
|
||||
return {
|
||||
GameServer,
|
||||
ServerWorld,
|
||||
ServerPlayer,
|
||||
ModRuntime,
|
||||
Menu,
|
||||
PlayerMenu,
|
||||
NaturalSpawner,
|
||||
Entity,
|
||||
LivingEntity,
|
||||
Mob,
|
||||
ItemEntity,
|
||||
EntityManager,
|
||||
Goal,
|
||||
GoalSelector,
|
||||
FloatGoal,
|
||||
RandomStrollGoal,
|
||||
PanicGoal,
|
||||
LookAtPlayerGoal,
|
||||
RandomLookAroundGoal,
|
||||
MeleeAttackGoal,
|
||||
NearestAttackablePlayerGoal,
|
||||
HurtByTargetGoal,
|
||||
};
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import type { BlockRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import type { Shape } from "$/common/block_shapes.ts";
|
||||
import { type Collisions, eyes_in_water, in_water, move_body } from "$/common/physics.ts";
|
||||
import { type Collisions, physics } from "$/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
|
||||
@@ -247,19 +247,19 @@ export abstract class Entity {
|
||||
}
|
||||
|
||||
update_in_water() {
|
||||
this.in_water = in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
this.in_water = physics.in_water(this, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// its eyes are in water, so it can't breathe
|
||||
get eyes_in_water() {
|
||||
return eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_liquid(x, y, z));
|
||||
}
|
||||
|
||||
// falls and moves by its velocity for one tick (see move_body), and counts how far it fell. returns what it hit
|
||||
move(): Collisions {
|
||||
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
||||
const from_y = this.y;
|
||||
const collisions = move_body(
|
||||
const collisions = physics.move_body(
|
||||
this,
|
||||
this.no_gravity ? 0 : this.gravity,
|
||||
is_solid,
|
||||
|
||||
@@ -9,6 +9,9 @@ import { ModRuntime } from "./mod_runtime.ts";
|
||||
import { register_entity_type } from "./entity/entity_types.ts";
|
||||
import { mod_entity_factory } from "./entity/mod_entity.ts";
|
||||
import { set_effect_types } from "./entity/effects.ts";
|
||||
import { clear_hooks, expose_engine } from "$/common/hooks.ts";
|
||||
import { common_engine } from "$/common/engine_modules.ts";
|
||||
import { server_engine } from "./engine_modules.ts";
|
||||
|
||||
export interface ServerModSource {
|
||||
listing: ModListing;
|
||||
@@ -25,13 +28,19 @@ export async function start_game(
|
||||
default_seed: string,
|
||||
mods: ServerModSource[],
|
||||
): Promise<GameServer> {
|
||||
// for engine access mods. the last game's hooks go first, a realm only runs one game at a time
|
||||
clear_hooks();
|
||||
expose_engine({ common: common_engine(), server: server_engine() });
|
||||
|
||||
const recipes = register_mod_data(mods.map((mod) => ({ id: mod.listing.id, data: mod.data })));
|
||||
for (const mod of mods) {
|
||||
for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json));
|
||||
}
|
||||
|
||||
const worldgen_scripts = mods.flatMap((mod) =>
|
||||
mod.worldgen_url ? [{ mod: mod.listing.id, url: mod.worldgen_url }] : []
|
||||
mod.worldgen_url
|
||||
? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }]
|
||||
: []
|
||||
);
|
||||
const worldgen = await load_worldgen(worldgen_scripts, recipes.ores);
|
||||
|
||||
@@ -48,15 +57,11 @@ export async function start_game(
|
||||
// every mod's shared rules first, so server scripts can count on any mod's being there
|
||||
await runtime.load_shared_scripts(
|
||||
game,
|
||||
mods.flatMap((mod) =>
|
||||
mod.shared_url ? [{ mod: mod.listing.id, version: mod.listing.version, url: mod.shared_url }] : []
|
||||
),
|
||||
mods.flatMap((mod) => mod.shared_url ? [{ ...script_of(mod), url: mod.shared_url }] : []),
|
||||
);
|
||||
await runtime.load_scripts(
|
||||
game,
|
||||
mods.flatMap((mod) =>
|
||||
mod.server_url ? [{ mod: mod.listing.id, version: mod.listing.version, url: mod.server_url }] : []
|
||||
),
|
||||
mods.flatMap((mod) => mod.server_url ? [{ ...script_of(mod), url: mod.server_url }] : []),
|
||||
);
|
||||
runtime.finish_setup();
|
||||
runtime.after.server_start.emit({});
|
||||
@@ -64,6 +69,10 @@ export async function start_game(
|
||||
return game;
|
||||
}
|
||||
|
||||
function script_of(mod: ServerModSource) {
|
||||
return { mod: mod.listing.id, version: mod.listing.version, engine_access: mod.listing.engine_access ?? false };
|
||||
}
|
||||
|
||||
export function data_url(code: string) {
|
||||
const bytes = new TextEncoder().encode(code);
|
||||
let binary = "";
|
||||
|
||||
+62
-45
@@ -7,6 +7,7 @@ import { covers_face } from "$/common/block_shapes.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import type { SharedContext } from "$/common/mod_api/shared.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import { loading_mod } from "$/common/hooks.ts";
|
||||
import type {
|
||||
BlockComponent,
|
||||
BlockRef,
|
||||
@@ -87,6 +88,14 @@ export class Signal<T> {
|
||||
// the mod whose code is running right now, for apis shared between mods that need to know who called
|
||||
let running_mod = "";
|
||||
|
||||
// a mod's script, imported from url. engine_access lets it import bworld/engine
|
||||
interface ScriptSource {
|
||||
mod: string;
|
||||
version: string;
|
||||
url: string;
|
||||
engine_access: boolean;
|
||||
}
|
||||
|
||||
export function run_guarded<T>(mod: string, what: string, fn: () => T): T | undefined {
|
||||
const previous = running_mod;
|
||||
running_mod = mod;
|
||||
@@ -213,56 +222,64 @@ export class ModRuntime {
|
||||
|
||||
// imports each server script and calls its setup, in load order
|
||||
// shared scripts, the block rules clients run too. their world is the server's
|
||||
async load_shared_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
async load_shared_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url } of scripts) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
}
|
||||
const { world } = this.#context(mod, version);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
world: {
|
||||
get_block: world.get_block,
|
||||
get_state: world.get_state,
|
||||
set_state: world.set_state,
|
||||
is_face_sturdy: world.is_face_sturdy,
|
||||
},
|
||||
components: {
|
||||
register_block: (id, rules) => {
|
||||
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
this.rules.register(mod, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(ctx);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url));
|
||||
}
|
||||
}
|
||||
|
||||
async load_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
async #load_shared_script(mod: string, version: string, url: string) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
}
|
||||
const { world } = this.#context(mod, version);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
world: {
|
||||
get_block: world.get_block,
|
||||
get_state: world.get_state,
|
||||
set_state: world.set_state,
|
||||
is_face_sturdy: world.is_face_sturdy,
|
||||
},
|
||||
components: {
|
||||
register_block: (id, rules) => {
|
||||
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
this.rules.register(mod, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(ctx);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
|
||||
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url } of scripts) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the server script doesn't export a setup function");
|
||||
}
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(this.#context(mod, version));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url));
|
||||
}
|
||||
}
|
||||
|
||||
async #load_script(mod: string, version: string, url: string) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the server script doesn't export a setup function");
|
||||
}
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(this.#context(mod, version));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,7 @@ export async function load_bmods(dir: string, engine_textures: Iterable<string>)
|
||||
sha256,
|
||||
file: `mods/${sha256}${BMOD_EXTENSION}`,
|
||||
size: client_bytes.length,
|
||||
...(bmod.manifest.engine_access ? { engine_access: true } : {}),
|
||||
},
|
||||
};
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user