Move effects into the modding api
This commit is contained in:
+6
-2
@@ -5,6 +5,7 @@ import type { ModelJson } from "./block_models.ts";
|
||||
import { MAX_TEXTURE_SIZE } from "./constants.ts";
|
||||
import {
|
||||
type BlockJson,
|
||||
type EffectJson,
|
||||
type EntityJson,
|
||||
type EntityModelJson,
|
||||
ID_PATTERN,
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
validate_block,
|
||||
validate_effect,
|
||||
validate_entity,
|
||||
validate_entity_model,
|
||||
validate_item,
|
||||
@@ -57,7 +59,7 @@ export class BmodError extends Error {
|
||||
|
||||
// the zip's layout:
|
||||
// manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip
|
||||
// data.json every block, model, item, recipe, ore, entity, entity model and tag
|
||||
// data.json every block, model, item, recipe, ore, entity, entity model, effect and tag
|
||||
// scripts/<side>.js
|
||||
// textures/<name>.png the texture <mod id>:<name>
|
||||
// credits.md
|
||||
@@ -149,10 +151,12 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
|
||||
validate_entity_model,
|
||||
problems,
|
||||
) as EntityModelJson[],
|
||||
effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[],
|
||||
tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[],
|
||||
};
|
||||
// a mod only registers ids in its own namespace
|
||||
for (const { id } of [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models]) {
|
||||
const registered = [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models];
|
||||
for (const { id } of [...registered, ...data.effects]) {
|
||||
if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,9 +58,30 @@ export interface ItemComponent<P = any> {
|
||||
on_use?(item: ItemStack, params: P, player: Player): void;
|
||||
}
|
||||
|
||||
// P is the component's params from the effect json. see docs/modding/entities/effects.md
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface EffectComponent<P = any> {
|
||||
// whether on_tick runs this tick, like neoforge's shouldApplyEffectTickThisTick. ticks is what's left of the
|
||||
// effect, or how long it's been on for ones that last forever. every tick when not set
|
||||
should_tick?(ticks: number, amplifier: number, params: P): boolean;
|
||||
// like neoforge's applyEffectTick. returning false takes the effect off. instant effects run it once, when added
|
||||
on_tick?(entity: LivingRef, amplifier: number, params: P): boolean | void;
|
||||
}
|
||||
|
||||
// a player or a mob, what effects work on
|
||||
export interface LivingRef {
|
||||
readonly id: string;
|
||||
readonly health: number;
|
||||
readonly max_health: number;
|
||||
// cause is for events and the death message, like "magic"
|
||||
damage(amount: number, cause?: string): void;
|
||||
heal(amount: number): void;
|
||||
}
|
||||
|
||||
export interface ComponentRegistry {
|
||||
register_block<P>(id: Id, component: BlockComponent<P>): void;
|
||||
register_item<P>(id: Id, component: ItemComponent<P>): void;
|
||||
register_effect<P>(id: Id, component: EffectComponent<P>): void;
|
||||
}
|
||||
|
||||
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
|
||||
@@ -241,7 +262,7 @@ export interface Player {
|
||||
add_modifier(attribute: Id, modifier: AttributeModifier): void;
|
||||
// whether it had it
|
||||
remove_modifier(attribute: Id, id: string): boolean;
|
||||
// status effects, like minecraft's: "bworld:regeneration", "bworld:speed"... duration in ticks, -1 for forever.
|
||||
// status effects, any mod's, like the base game's "bworld:regeneration". duration in ticks, -1 for forever.
|
||||
// adding one they have merges them like minecraft: the stronger wins, and a weaker longer one waits under it
|
||||
add_effect(id: Id, duration: number, options?: EffectOptions): void;
|
||||
// the one they have of that type, undefined when they don't
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import {
|
||||
type BlockJson,
|
||||
choice_list,
|
||||
type EffectJson,
|
||||
type EntityJson,
|
||||
type EntityModelJson,
|
||||
type ItemJson,
|
||||
@@ -37,6 +38,7 @@ export interface LoadedMod {
|
||||
spawns: { file: string; json: SpawnJson }[];
|
||||
entities: { file: string; json: EntityJson }[];
|
||||
entity_models: { file: string; json: EntityModelJson }[];
|
||||
effects: { file: string; json: EffectJson }[];
|
||||
tags: { file: string; json: TagJson }[];
|
||||
textures: string[];
|
||||
// absolute paths by texture id
|
||||
@@ -79,6 +81,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
const entity_jsons = new Map<string, EntityJson>();
|
||||
const entity_model_owners = new Map<string, string[]>();
|
||||
const entity_model_jsons = new Map<string, EntityModelJson>();
|
||||
const effect_owners = new Map<string, string[]>();
|
||||
const tag_owners = new Map<TagRegistry, Set<string>>();
|
||||
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
||||
|
||||
@@ -99,6 +102,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
add(entity_model_owners, json.id, mod.id);
|
||||
entity_model_jsons.set(json.id, json);
|
||||
}
|
||||
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
|
||||
for (const { json } of mod.tags) {
|
||||
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
|
||||
tag_owners.get(json.registry)!.add(json.id);
|
||||
@@ -233,6 +237,11 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (json.model !== undefined) uses(file, json.model, "entity model");
|
||||
uses(file, json.texture, "texture");
|
||||
}
|
||||
for (const { file, json } of mod.effects) {
|
||||
if ((effect_owners.get(json.id)?.length ?? 0) > 1) {
|
||||
errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`);
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.spawns) {
|
||||
check_namespace(file, json.entity);
|
||||
const entity = entity_jsons.get(json.entity);
|
||||
|
||||
@@ -132,6 +132,22 @@ export interface EntityJson {
|
||||
arcane_shielding?: number;
|
||||
}
|
||||
|
||||
// a status effect, like neoforge's MobEffect. what it does over time comes from components scripts register, see
|
||||
// docs/modding/entities/effects.md
|
||||
export interface EffectJson {
|
||||
id: string;
|
||||
// like minecraft's MobEffectCategory, "neutral" when not set
|
||||
category?: EffectCategory;
|
||||
// happens once when added instead of lasting, like instant health. its components' on_tick runs once
|
||||
instant?: boolean;
|
||||
// attribute modifiers while it's on, for level 1. each level adds that much again, like minecraft's speed
|
||||
modifiers?: { attribute: string; amount: number; operation: EffectModifierOperation }[];
|
||||
components?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type EffectCategory = "beneficial" | "harmful" | "neutral";
|
||||
export type EffectModifierOperation = "add_value" | "add_multiplied_base" | "add_multiplied_total";
|
||||
|
||||
type Vec3 = [number, number, number];
|
||||
|
||||
// a bedrock entity model (models/<name>.geo.json) with only what the engine uses. positions and sizes are in
|
||||
@@ -786,6 +802,41 @@ export function validate_entity(json: unknown): Problems {
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_effect(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["effect must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
if (json.category !== undefined && !["beneficial", "harmful", "neutral"].includes(json.category as string)) {
|
||||
problems.push('category must be "beneficial", "harmful" or "neutral"');
|
||||
}
|
||||
if (json.instant !== undefined && typeof json.instant !== "boolean") problems.push("instant must be true or false");
|
||||
if (json.modifiers !== undefined) {
|
||||
if (!Array.isArray(json.modifiers)) {
|
||||
problems.push("modifiers must be a list");
|
||||
} else {
|
||||
json.modifiers.forEach((modifier, i) => {
|
||||
const field = `modifiers[${i}]`;
|
||||
if (!is_object(modifier)) {
|
||||
problems.push(`${field} must be { attribute, amount, operation }`);
|
||||
return;
|
||||
}
|
||||
problems.push(...validate_id(modifier.attribute, `${field}.attribute`));
|
||||
if (typeof modifier.amount !== "number" || !Number.isFinite(modifier.amount)) {
|
||||
problems.push(`${field}.amount must be a number`);
|
||||
}
|
||||
if (
|
||||
!["add_value", "add_multiplied_base", "add_multiplied_total"].includes(modifier.operation as string)
|
||||
) {
|
||||
problems.push(
|
||||
`${field}.operation must be "add_value", "add_multiplied_base" or "add_multiplied_total"`,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (json.components !== undefined && !is_object(json.components)) problems.push("components must be an object");
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function validate_entity_model(json: unknown): Problems {
|
||||
if (!is_object(json)) return ["entity model must be an object"];
|
||||
const problems = validate_id(json.id, "id");
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
block_from_json,
|
||||
BlockJson,
|
||||
choice_ids,
|
||||
EffectJson,
|
||||
EntityJson,
|
||||
EntityModelJson,
|
||||
grid_recipe_from_json,
|
||||
@@ -34,6 +35,8 @@ export interface ModData {
|
||||
spawns: SpawnJson[];
|
||||
entities: EntityJson[];
|
||||
entity_models: EntityModelJson[];
|
||||
// status effects, see EffectJson
|
||||
effects: EffectJson[];
|
||||
// tag files, see common/tags.ts
|
||||
tags: TagJson[];
|
||||
}
|
||||
@@ -75,7 +78,7 @@ export function is_registered(registry: TagRegistry, id: string): boolean {
|
||||
return (registry === "blocks" && id === "bworld:air") || EverythingRegistry.get(registry, id) !== undefined;
|
||||
}
|
||||
|
||||
// registers every mod's blocks, items, entities, tags and recipes, in the order given (dependencies first). tags load
|
||||
// registers every mod's blocks, items, entities, effects, tags and recipes, in the order given (dependencies first). tags load
|
||||
// once every mod's content has, since a tag can hold anything, and recipes after them, since they can use tags
|
||||
export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook {
|
||||
const recipes = new RecipeBook();
|
||||
@@ -101,6 +104,9 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
for (const json of data.entities) {
|
||||
EverythingRegistry.register<EntityJson>("entities", json.id, json);
|
||||
}
|
||||
for (const json of data.effects) {
|
||||
EverythingRegistry.register<EffectJson>("effects", json.id, json);
|
||||
}
|
||||
} catch (e) {
|
||||
throw new ModLoadError(id, (e as Error).message);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,8 @@ export function setup(ctx: ServerContext) {
|
||||
}
|
||||
```
|
||||
|
||||
Items have components the same way, and so do [status effects](../entities/effects.md#effect-components).
|
||||
|
||||
Components can only be registered during `setup`, and only in the mod's own namespace. Once every mod's `setup` has run,
|
||||
a block or item that lists a component nobody registered stops the server from starting, naming the block and the
|
||||
component.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Registries
|
||||
|
||||
Every block, item, block model, entity, entity model and texture has an id, and is registered under it once, while mods
|
||||
load. After that the game refers to things only by their id.
|
||||
Every block, item, block model, entity, entity model, status effect and texture has an id, and is registered under it
|
||||
once, while mods load. After that the game refers to things only by their id.
|
||||
|
||||
## Identifiers
|
||||
|
||||
@@ -32,8 +32,9 @@ The server and every client load mods the same way, in the same order:
|
||||
|
||||
1. **Order.** Dependencies come before the mods that need them, and otherwise mods load alphabetically. Mods that depend
|
||||
on each other in a circle are an error. The server sends its order to clients, and they follow it.
|
||||
2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models and entities. Then
|
||||
every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which can use tags.
|
||||
2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models, entities and effects.
|
||||
Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which can use
|
||||
tags.
|
||||
3. **Scripts.** Server scripts' `setup` runs on the server, and client scripts' on each client. Worldgen scripts'
|
||||
`setup` runs on the server before it makes chunks.
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# Status Effects
|
||||
|
||||
Status effects change a player or a mob for a while, like speed or poison. They work like NeoForge's `MobEffect`s: an
|
||||
effect is defined once, and adding it to someone gives them an instance of it with a duration and a level.
|
||||
|
||||
Like blocks and items, an effect is data with components. Its JSON in the mod's `effects/` folder says what it is and
|
||||
which attributes it changes. Components, registered by the server script, say what it does over time. Even the base
|
||||
game's effects are defined this way, in `mods/bworld/effects`.
|
||||
|
||||
## Defining an Effect
|
||||
|
||||
`effects/swiftness.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "copper_tools:swiftness",
|
||||
"category": "beneficial",
|
||||
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.1, "operation": "add_multiplied_total" }]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Required | What it does |
|
||||
| ------------ | -------- | ---------------------------------------------------------------------------------------------- |
|
||||
| `id` | yes | The effect's id, in the mod's [namespace](../concepts/registries.md#identifiers). |
|
||||
| `category` | no | `"beneficial"`, `"harmful"` or `"neutral"` (the default), like NeoForge's `MobEffectCategory`. |
|
||||
| `instant` | no | `true` makes it happen once when added instead of lasting, like `bworld:instant_health`. |
|
||||
| `modifiers` | no | [Attribute modifiers](players.md#attributes) while it's on. Each level adds `amount` again. |
|
||||
| `components` | no | What it does over time, see below. |
|
||||
|
||||
The modifiers' `operation` is `"add_value"`, `"add_multiplied_base"` or `"add_multiplied_total"`. They come off when the
|
||||
effect ends.
|
||||
|
||||
## Effect Components
|
||||
|
||||
An effect that does something each tick lists components, with their parameters:
|
||||
|
||||
```json
|
||||
"components": {
|
||||
"copper_tools:rusting": { "every": 40 }
|
||||
}
|
||||
```
|
||||
|
||||
The server script registers them during `setup`, in the mod's own namespace:
|
||||
|
||||
```ts
|
||||
import type { ServerContext } from "bworld/server";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
ctx.components.register_effect<{ every: number }>("copper_tools:rusting", {
|
||||
should_tick: (ticks, amplifier, params) => ticks % params.every === 0,
|
||||
on_tick(entity, amplifier) {
|
||||
entity.damage(amplifier + 1, "rust");
|
||||
},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
| Handler | Called when | Returns |
|
||||
| --------------------------------------- | ---------------------------------------------------------------------------------- | ---------------------------- |
|
||||
| `should_tick(ticks, amplifier, params)` | Every tick the effect is on, to ask whether `on_tick` runs. Every tick without it. | `true` to run `on_tick` |
|
||||
| `on_tick(entity, amplifier, params)` | When `should_tick` says so. Instant effects run it once, when added. | `false` takes the effect off |
|
||||
|
||||
`ticks` is how many ticks the effect has left, or how long it's been on for one that lasts forever. `amplifier` is the
|
||||
level minus 1. `entity` is the player or mob that has it:
|
||||
|
||||
```ts
|
||||
interface LivingRef {
|
||||
readonly id: string;
|
||||
readonly health: number;
|
||||
readonly max_health: number;
|
||||
damage(amount: number, cause?: string): void; // cause is for events and the death message
|
||||
heal(amount: number): void;
|
||||
}
|
||||
```
|
||||
|
||||
Like block components, an effect that lists a component nobody registered stops the server from starting.
|
||||
|
||||
## Using Effects
|
||||
|
||||
Any mod's effects are added to players by id, see [Status Effects](players.md#status-effects):
|
||||
|
||||
```ts
|
||||
player.add_effect("copper_tools:swiftness", 200, { amplifier: 1 });
|
||||
```
|
||||
|
||||
## The Base Game's Effects
|
||||
|
||||
| Effect | What it does |
|
||||
| ------------------------ | --------------------------------------------------------------- |
|
||||
| `bworld:speed` | 20% faster a level. |
|
||||
| `bworld:slowness` | 15% slower a level. |
|
||||
| `bworld:regeneration` | Heals 1 every 50 ticks, twice as often each level. |
|
||||
| `bworld:poison` | Hurts 1 every 25 ticks, twice as often each level. Never kills. |
|
||||
| `bworld:instant_health` | Heals 4 at once, doubling each level. |
|
||||
| `bworld:resistance` | Takes 20% less damage a level. |
|
||||
| `bworld:water_breathing` | Doesn't run out of air under water. |
|
||||
|
||||
Resistance and water breathing have no components: like in Minecraft, the engine checks for them where damage and air
|
||||
are worked out.
|
||||
@@ -103,8 +103,8 @@ sends players their `attack_speed` whenever it changes so the bar matches.
|
||||
|
||||
## Status Effects
|
||||
|
||||
Players have some of Minecraft's status effects: `bworld:speed`, `bworld:slowness`, `bworld:regeneration`,
|
||||
`bworld:poison`, `bworld:instant_health`, `bworld:resistance` and `bworld:water_breathing`.
|
||||
Players can have any mod's [status effects](effects.md), like the base game's `bworld:speed`, `bworld:regeneration` or
|
||||
`bworld:water_breathing`.
|
||||
|
||||
```ts
|
||||
player.add_effect("bworld:regeneration", 100, { amplifier: 1 }); // regeneration II for 5 seconds
|
||||
|
||||
@@ -16,6 +16,7 @@ mods/
|
||||
models/*.json # block models
|
||||
models/*.geo.json # entity models
|
||||
entities/*.json
|
||||
effects/*.json # status effects, see entities/effects.md
|
||||
recipes/*.json
|
||||
tags/<registry>/<namespace>/*.json # tags, see resources/tags.md
|
||||
worldgen/ores.json
|
||||
@@ -36,8 +37,8 @@ Data folders can have subfolders. Every data file holds one definition, wrapped
|
||||
}
|
||||
```
|
||||
|
||||
The key is `block`, `item`, `model`, `entity`, `recipe` or `tag`, depending on the folder. File names don't matter, the
|
||||
`id` inside does, except for [tags](../resources/tags.md), whose id is their path.
|
||||
The key is `block`, `item`, `model`, `entity`, `effect`, `recipe` or `tag`, depending on the folder. File names don't
|
||||
matter, the `id` inside does, except for [tags](../resources/tags.md), whose id is their path.
|
||||
|
||||
## `manifest.json`
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:instant_health",
|
||||
"category": "beneficial",
|
||||
"instant": true,
|
||||
"components": { "bworld:heal": { "amount": 4 } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:poison",
|
||||
"category": "harmful",
|
||||
"components": { "bworld:poison": { "every": 25 } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:regeneration",
|
||||
"category": "beneficial",
|
||||
"components": { "bworld:heal_over_time": { "every": 50 } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:resistance",
|
||||
"category": "beneficial"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:slowness",
|
||||
"category": "harmful",
|
||||
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": -0.15, "operation": "add_multiplied_total" }]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:speed",
|
||||
"category": "beneficial",
|
||||
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.2, "operation": "add_multiplied_total" }]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"effect": {
|
||||
"id": "bworld:water_breathing",
|
||||
"category": "beneficial"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// what the base game's effects do over time, see mods/bworld/effects. speed and slowness are only attribute
|
||||
// modifiers, and resistance and water breathing work where damage and air happen, like minecraft's
|
||||
import type { ServerContext } from "bworld/server";
|
||||
|
||||
// every n ticks, n halving with each level: minecraft's regeneration and poison
|
||||
function every(ticks: number, amplifier: number, n: number) {
|
||||
const interval = n >> amplifier;
|
||||
return interval > 0 ? ticks % interval === 0 : true;
|
||||
}
|
||||
|
||||
export function register_effects(ctx: ServerContext) {
|
||||
ctx.components.register_effect<{ every: number }>("bworld:heal_over_time", {
|
||||
should_tick: (ticks, amplifier, params) => every(ticks, amplifier, params.every),
|
||||
on_tick(entity) {
|
||||
if (entity.health < entity.max_health) entity.heal(1);
|
||||
},
|
||||
});
|
||||
ctx.components.register_effect<{ every: number }>("bworld:poison", {
|
||||
should_tick: (ticks, amplifier, params) => every(ticks, amplifier, params.every),
|
||||
on_tick(entity) {
|
||||
// never kills
|
||||
if (entity.health > 1) entity.damage(1, "magic");
|
||||
},
|
||||
});
|
||||
// instant: amount for level 1, doubling with each level
|
||||
ctx.components.register_effect<{ amount: number }>("bworld:heal", {
|
||||
on_tick(entity, amplifier, params) {
|
||||
entity.heal(Math.max(params.amount << amplifier, 0));
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// the base game's block and item behavior, written against the same api as any other mod
|
||||
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
|
||||
import { register_curios } from "./curios.ts";
|
||||
import { register_effects } from "./effects.ts";
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
register_hoeable(ctx);
|
||||
@@ -10,6 +11,7 @@ export function setup(ctx: ServerContext) {
|
||||
register_farming(ctx);
|
||||
register_fishing(ctx);
|
||||
register_curios(ctx);
|
||||
register_effects(ctx);
|
||||
}
|
||||
|
||||
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
||||
|
||||
@@ -1,70 +1,31 @@
|
||||
// status effects on living entities, like minecraft's MobEffect and MobEffectInstance. an effect is a type plus
|
||||
// how strong it is and for how long. a weaker one that lasts longer waits hidden under the stronger one, and takes
|
||||
// over when that runs out
|
||||
import { ATTRIBUTES, type AttributeType, type ModifierOperation } from "./attributes.ts";
|
||||
import { DAMAGE, finite } from "./entity.ts";
|
||||
import type { EffectCategory } from "$/common/mod_data.ts";
|
||||
import type { AttributeType, ModifierOperation } from "./attributes.ts";
|
||||
import { finite } from "./entity.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// a kind of effect: a mod's effect json with the components it lists, see ModRuntime.finish_setup. the base game's
|
||||
// are in mods/bworld/effects
|
||||
export interface EffectType {
|
||||
id: string;
|
||||
beneficial: boolean;
|
||||
category: EffectCategory;
|
||||
// happens once when added instead of lasting, like instant health
|
||||
instant?: boolean;
|
||||
instant: boolean;
|
||||
// while it's on. amount is for level 1 and goes up with each level, like minecraft
|
||||
modifiers?: { attribute: AttributeType; amount: number; operation: ModifierOperation }[];
|
||||
// whether on_tick runs this tick, from the ticks left (or its age for infinite ones)
|
||||
should_tick?(duration: number, amplifier: number): boolean;
|
||||
// returning false takes the effect off
|
||||
on_tick?(entity: LivingEntity, amplifier: number): boolean | void;
|
||||
apply_instant?(entity: LivingEntity, amplifier: number): void;
|
||||
on_added?(entity: LivingEntity, amplifier: number): void;
|
||||
on_removed?(entity: LivingEntity, amplifier: number): void;
|
||||
modifiers: { attribute: AttributeType; amount: number; operation: ModifierOperation }[];
|
||||
// each tick it's on, and once for instant ones, with ticks undefined. ticks is what's left, or how long it's been
|
||||
// on for ones that last forever. returning false takes the effect off
|
||||
on_tick?(entity: LivingEntity, ticks: number | undefined, amplifier: number): boolean | void;
|
||||
}
|
||||
|
||||
// every n ticks, n halving with each level: minecraft's regeneration and poison
|
||||
const every = (ticks: number) => (duration: number, amplifier: number) => {
|
||||
const interval = ticks >> amplifier;
|
||||
return interval > 0 ? duration % interval === 0 : true;
|
||||
};
|
||||
const registry = new Map<string, EffectType>();
|
||||
|
||||
function effect(id: string, beneficial: boolean, rest: Omit<EffectType, "id" | "beneficial"> = {}): EffectType {
|
||||
return { id: `bworld:${id}`, beneficial, ...rest };
|
||||
}
|
||||
|
||||
export const EFFECTS = {
|
||||
speed: effect("speed", true, {
|
||||
modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: 0.2, operation: "add_multiplied_total" }],
|
||||
}),
|
||||
slowness: effect("slowness", false, {
|
||||
modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: -0.15, operation: "add_multiplied_total" }],
|
||||
}),
|
||||
regeneration: effect("regeneration", true, {
|
||||
should_tick: every(50),
|
||||
on_tick: (entity) => {
|
||||
if (entity.health < entity.max_health) entity.heal(1);
|
||||
},
|
||||
}),
|
||||
poison: effect("poison", false, {
|
||||
should_tick: every(25),
|
||||
on_tick: (entity) => {
|
||||
// never kills
|
||||
if (entity.health > 1) entity.hurt(1, DAMAGE.magic);
|
||||
},
|
||||
}),
|
||||
instant_health: effect("instant_health", true, {
|
||||
instant: true,
|
||||
apply_instant: (entity, amplifier) => entity.heal(Math.max(4 << amplifier, 0)),
|
||||
}),
|
||||
// these do their thing where it happens: LivingEntity's damage and air
|
||||
resistance: effect("resistance", true),
|
||||
water_breathing: effect("water_breathing", true),
|
||||
} satisfies Record<string, EffectType>;
|
||||
|
||||
const registry = new Map<string, EffectType>(Object.values(EFFECTS).map((type) => [type.id, type]));
|
||||
|
||||
// adds an effect type, or replaces one with the same id
|
||||
export function register_effect(type: EffectType) {
|
||||
registry.set(type.id, type);
|
||||
// every effect mods define, replacing the ones before
|
||||
export function set_effect_types(types: EffectType[]) {
|
||||
registry.clear();
|
||||
for (const type of types) registry.set(type.id, type);
|
||||
}
|
||||
|
||||
export function effect_type(id: string): EffectType | undefined {
|
||||
@@ -181,9 +142,7 @@ export class EffectInstance {
|
||||
tick(entity: LivingEntity): "ended" | "changed" | undefined {
|
||||
if (!this.has_time_left) return "ended";
|
||||
const count = this.infinite ? entity.age : this.duration;
|
||||
if (this.type.should_tick?.(count, this.amplifier) && this.type.on_tick?.(entity, this.amplifier) === false) {
|
||||
return "ended";
|
||||
}
|
||||
if (this.type.on_tick?.(entity, count, this.amplifier) === false) return "ended";
|
||||
this.#count_down();
|
||||
if (this.duration === 0 && this.hidden) {
|
||||
this.#take_details(this.hidden);
|
||||
|
||||
@@ -37,7 +37,7 @@ export interface DamageSource {
|
||||
// who did it, which knockback pushes away from
|
||||
attacker?: Attacker;
|
||||
by_player?: boolean;
|
||||
// fire resistance stops it
|
||||
// it's fire, like burning
|
||||
fire?: boolean;
|
||||
// even invulnerable entities are hurt, like falling out of the world
|
||||
bypasses_invulnerability?: boolean;
|
||||
@@ -52,6 +52,12 @@ export const DAMAGE = {
|
||||
magic: { cause: "magic", type: "arcane" },
|
||||
} satisfies Record<string, DamageSource>;
|
||||
|
||||
// a cause from a script, like "magic": the engine's source for it, or physical damage under that name
|
||||
export function damage_source(cause: string): DamageSource {
|
||||
const known: Record<string, DamageSource> = DAMAGE;
|
||||
return known[cause] ?? { cause, type: "physical" };
|
||||
}
|
||||
|
||||
// what entities can see and do in the world, the entity manager is one
|
||||
export interface EntityLevel {
|
||||
readonly current_tick: number;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import { type ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityData } from "$/common/protocol.ts";
|
||||
import { AttributeMap, ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
||||
import { EffectInstance, EFFECTS, type EffectType, type SavedEffect } from "./effects.ts";
|
||||
import { EffectInstance, type SavedEffect } from "./effects.ts";
|
||||
import { DAMAGE, type DamageSource, Entity, finite, type SavedEntity } from "./entity.ts";
|
||||
|
||||
export const EQUIPMENT_SLOTS = ["head", "chest", "legs", "feet", "mainhand", "offhand", "body", "saddle"] as const;
|
||||
@@ -28,6 +28,9 @@ const SWIM_UP = 0.04;
|
||||
// a jump can't start more often than this while jump is held
|
||||
const JUMP_DELAY = 10;
|
||||
const KNOCKBACK = 0.4;
|
||||
// the base game's effects that work where what they change happens, like minecraft's: in damage and air
|
||||
const RESISTANCE = "bworld:resistance";
|
||||
const WATER_BREATHING = "bworld:water_breathing";
|
||||
|
||||
export interface SavedLivingEntity extends SavedEntity {
|
||||
health?: number;
|
||||
@@ -135,7 +138,7 @@ export abstract class LivingEntity extends Entity {
|
||||
override base_tick() {
|
||||
super.base_tick();
|
||||
if (this.alive) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(EFFECTS.water_breathing)) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(WATER_BREATHING)) {
|
||||
this.air -= 1;
|
||||
if (this.air <= -20) {
|
||||
this.air = 0;
|
||||
@@ -314,7 +317,7 @@ export abstract class LivingEntity extends Entity {
|
||||
return;
|
||||
}
|
||||
|
||||
const resistance = this.get_effect(EFFECTS.resistance);
|
||||
const resistance = this.get_effect(RESISTANCE);
|
||||
if (resistance && !source.bypasses_invulnerability) {
|
||||
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
|
||||
}
|
||||
@@ -387,12 +390,12 @@ export abstract class LivingEntity extends Entity {
|
||||
|
||||
// effects
|
||||
|
||||
has_effect(type: EffectType) {
|
||||
return this.effects.has(type.id);
|
||||
has_effect(id: string) {
|
||||
return this.effects.has(id);
|
||||
}
|
||||
|
||||
get_effect(type: EffectType): EffectInstance | undefined {
|
||||
return this.effects.get(type.id);
|
||||
get_effect(id: string): EffectInstance | undefined {
|
||||
return this.effects.get(id);
|
||||
}
|
||||
|
||||
// whether it's affected by that effect at all, like undead and poison
|
||||
@@ -404,14 +407,13 @@ export abstract class LivingEntity extends Entity {
|
||||
add_effect(effect: EffectInstance): boolean {
|
||||
if (!this.alive || !this.can_be_affected(effect)) return false;
|
||||
if (effect.type.instant) {
|
||||
effect.type.apply_instant?.(this, effect.amplifier);
|
||||
effect.type.on_tick?.(this, undefined, effect.amplifier);
|
||||
return true;
|
||||
}
|
||||
const current = this.effects.get(effect.type.id);
|
||||
if (!current) {
|
||||
this.effects.set(effect.type.id, effect);
|
||||
this.#apply_modifiers(effect);
|
||||
effect.type.on_added?.(this, effect.amplifier);
|
||||
return true;
|
||||
}
|
||||
const amplifier = current.amplifier;
|
||||
@@ -420,16 +422,16 @@ export abstract class LivingEntity extends Entity {
|
||||
return changed;
|
||||
}
|
||||
|
||||
remove_effect(type: EffectType) {
|
||||
const effect = this.effects.get(type.id);
|
||||
remove_effect(id: string) {
|
||||
const effect = this.effects.get(id);
|
||||
if (!effect) return false;
|
||||
this.effects.delete(type.id);
|
||||
this.effects.delete(id);
|
||||
this.#effect_ended(effect);
|
||||
return true;
|
||||
}
|
||||
|
||||
clear_effects() {
|
||||
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type);
|
||||
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type.id);
|
||||
}
|
||||
|
||||
tick_effects() {
|
||||
@@ -439,7 +441,7 @@ export abstract class LivingEntity extends Entity {
|
||||
if (this.effects.get(effect.type.id) !== effect) continue;
|
||||
const result = effect.tick(this);
|
||||
if (result === "ended") {
|
||||
this.remove_effect(effect.type);
|
||||
this.remove_effect(effect.type.id);
|
||||
} else if (result === "changed") {
|
||||
this.#apply_modifiers(effect);
|
||||
}
|
||||
@@ -447,7 +449,7 @@ export abstract class LivingEntity extends Entity {
|
||||
}
|
||||
|
||||
#apply_modifiers(effect: EffectInstance) {
|
||||
for (const { attribute, amount, operation } of effect.type.modifiers ?? []) {
|
||||
for (const { attribute, amount, operation } of effect.type.modifiers) {
|
||||
this.attributes.get(attribute)?.add_modifier({
|
||||
id: effect.type.id,
|
||||
amount: amount * (effect.amplifier + 1),
|
||||
@@ -457,10 +459,9 @@ export abstract class LivingEntity extends Entity {
|
||||
}
|
||||
|
||||
#effect_ended(effect: EffectInstance) {
|
||||
for (const { attribute } of effect.type.modifiers ?? []) {
|
||||
for (const { attribute } of effect.type.modifiers) {
|
||||
this.attributes.get(attribute)?.remove_modifier(effect.type.id);
|
||||
}
|
||||
effect.type.on_removed?.(this, effect.amplifier);
|
||||
// back inside what the attributes allow now
|
||||
this.health = this.#health;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import { DAMAGE, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { ATTRIBUTES } from "./entity/attributes.ts";
|
||||
|
||||
// how far from a player's eyes a block can be changed, a bit more than the client's reach
|
||||
@@ -1320,8 +1320,7 @@ export class GameServer {
|
||||
|
||||
// hurts a player, killing them at 0. cause is for mods and the death message, like "fall"
|
||||
damage_player(player: ServerPlayer, amount: number, cause: string) {
|
||||
const known: Record<string, DamageSource> = DAMAGE;
|
||||
player.hurt(amount, known[cause] ?? { cause, type: "physical" });
|
||||
player.hurt(amount, damage_source(cause));
|
||||
}
|
||||
|
||||
// everything that hurts a player ends up here, see ServerPlayer.hurt. mods can change or cancel it first
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { WorldSave } from "./saves.ts";
|
||||
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";
|
||||
|
||||
export interface ServerModSource {
|
||||
listing: ModListing;
|
||||
@@ -34,6 +35,8 @@ export async function start_game(
|
||||
const worldgen = await load_worldgen(worldgen_scripts, recipes.ores);
|
||||
|
||||
const runtime = new ModRuntime();
|
||||
// before the world loads, its mobs can have effects
|
||||
set_effect_types(runtime.effect_types());
|
||||
const game = new GameServer(host, save, default_seed, {
|
||||
listings: mods.map((mod) => mod.listing),
|
||||
recipes,
|
||||
|
||||
@@ -11,10 +11,12 @@ import type {
|
||||
CommandRegistry,
|
||||
CommandSource,
|
||||
Container,
|
||||
EffectComponent,
|
||||
EntityRef,
|
||||
EventSignal,
|
||||
ItemComponent,
|
||||
ItemStack,
|
||||
LivingRef,
|
||||
MenuBuilder,
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
@@ -45,7 +47,9 @@ 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 type { Entity } from "./entity/entity.ts";
|
||||
import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "$/common/mod_data.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
|
||||
@@ -108,6 +112,7 @@ interface Timer {
|
||||
export class ModRuntime {
|
||||
block_components = new Map<string, { mod: string; component: BlockComponent }>();
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
// every command, the engine's and mods', see common/commands.ts
|
||||
commands = new CommandDispatcher<ServerCommandSource>();
|
||||
// who registered each command name: "engine" or a mod's id
|
||||
@@ -218,10 +223,66 @@ export class ModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks and items use must exist
|
||||
// every effect mods define, from their json. they're made before scripts run, since the world loads first and
|
||||
// its mobs can have effects, so their components are looked up as they tick
|
||||
effect_types(): EffectType[] {
|
||||
return EverythingRegistry.entries<EffectJson>("effects").map(([id, json]) => {
|
||||
const modifiers = (json.modifiers ?? []).map(({ attribute, amount, operation }) => {
|
||||
const type = attribute_type(attribute);
|
||||
if (!type) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} changes ${attribute}, which isn't an attribute`,
|
||||
);
|
||||
}
|
||||
return { attribute: type, amount, operation };
|
||||
});
|
||||
const components = Object.entries(json.components ?? {});
|
||||
const on_tick = (entity: LivingEntity, ticks: number | undefined, amplifier: number) => {
|
||||
let keep = true;
|
||||
for (const [component_id, params] of components) {
|
||||
const { mod, component } = this.effect_components.get(component_id)!;
|
||||
const due = ticks === undefined || !component.should_tick ||
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} should_tick`,
|
||||
() => component.should_tick!(ticks, amplifier, params),
|
||||
);
|
||||
if (!due || !component.on_tick) continue;
|
||||
const result = run_guarded(
|
||||
mod,
|
||||
`${component_id} on_tick`,
|
||||
() => component.on_tick!(living_ref(entity), amplifier, params),
|
||||
);
|
||||
if (result === false) keep = false;
|
||||
}
|
||||
return keep;
|
||||
};
|
||||
return {
|
||||
id,
|
||||
category: json.category ?? "neutral",
|
||||
instant: json.instant === true,
|
||||
modifiers,
|
||||
on_tick: components.length > 0 ? on_tick : undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks, items and effects use must exist
|
||||
finish_setup() {
|
||||
this.#setting_up = false;
|
||||
|
||||
for (const [id, effect] of EverythingRegistry.entries<EffectJson>("effects")) {
|
||||
for (const component of Object.keys(effect.components ?? {})) {
|
||||
if (!this.effect_components.has(component)) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} uses component ${component}, which nothing registered`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
|
||||
for (const component of Object.keys(block.components ?? {})) {
|
||||
if (!this.block_components.has(component)) {
|
||||
@@ -416,6 +477,14 @@ export class ModRuntime {
|
||||
}
|
||||
this.item_components.set(id, { mod, component: component as ItemComponent });
|
||||
},
|
||||
register_effect: (id, component) => {
|
||||
setup_only("components");
|
||||
own(id, "component");
|
||||
if (this.effect_components.has(id)) {
|
||||
throw new ModLoadError(mod, `component ${id} is registered twice`);
|
||||
}
|
||||
this.effect_components.set(id, { mod, component: component as EffectComponent });
|
||||
},
|
||||
},
|
||||
commands: {
|
||||
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
|
||||
@@ -998,10 +1067,10 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
player.add_effect(new EffectInstance(type, duration, { ...options, amplifier }));
|
||||
},
|
||||
effect(id) {
|
||||
const effect = player.get_effect(known_effect(id));
|
||||
const effect = player.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id)),
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
};
|
||||
}
|
||||
@@ -1018,6 +1087,26 @@ function known_attribute(id: string): AttributeType {
|
||||
return type;
|
||||
}
|
||||
|
||||
function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
get health() {
|
||||
return entity.health;
|
||||
},
|
||||
get max_health() {
|
||||
return entity.max_health;
|
||||
},
|
||||
damage(amount, cause = "script") {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`damage must be a number`);
|
||||
entity.hurt(amount, damage_source(String(cause)));
|
||||
},
|
||||
heal(amount) {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`heal must be a number`);
|
||||
entity.heal(amount);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function entity_ref(entity: Entity): EntityRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
|
||||
@@ -103,6 +103,7 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
spawns: entries(bmod.data.spawns),
|
||||
entities: entries(bmod.data.entities),
|
||||
entity_models: entries(bmod.data.entity_models),
|
||||
effects: entries(bmod.data.effects),
|
||||
tags: entries(bmod.data.tags),
|
||||
textures: [...bmod.textures.keys()],
|
||||
texture_files: new Map(),
|
||||
|
||||
@@ -249,6 +249,7 @@ Deno.test("a mod with only data packs and reads", () => {
|
||||
spawns: [],
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
tags: [],
|
||||
},
|
||||
scripts: {},
|
||||
|
||||
@@ -4,7 +4,6 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "$/common/protocol.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
import type { WorldSave } from "$/server/game/saves.ts";
|
||||
import { EFFECTS } from "$/server/game/entity/effects.ts";
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
type Msg = any;
|
||||
@@ -163,7 +162,7 @@ Deno.test("the giant chestplate gives regeneration III after 10 seconds without
|
||||
const alice = t.game.players()[0];
|
||||
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
|
||||
t.game.damage_player(alice, 2, "test");
|
||||
const regeneration = () => alice.get_effect(EFFECTS.regeneration);
|
||||
const regeneration = () => alice.get_effect("bworld:regeneration");
|
||||
|
||||
for (let i = 0; i < 199; i++) t.game.tick();
|
||||
assertEquals(regeneration(), undefined);
|
||||
|
||||
+12
-12
@@ -1,7 +1,7 @@
|
||||
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { Attribute, ATTRIBUTES } from "$/server/game/entity/attributes.ts";
|
||||
import { EffectInstance, EFFECTS } from "$/server/game/entity/effects.ts";
|
||||
import { effect_type, EffectInstance } from "$/server/game/entity/effects.ts";
|
||||
import { DAMAGE } from "$/server/game/entity/entity.ts";
|
||||
import { register_entity_type } from "$/server/game/entity/entity_types.ts";
|
||||
import { Goal, GoalSelector, RandomStrollGoal } from "$/server/game/entity/goals.ts";
|
||||
@@ -73,9 +73,9 @@ Deno.test("a stronger, shorter effect hides a weaker one, which takes over when
|
||||
const mob = spawn(0.5, 100, 0.5);
|
||||
const base_speed = mob.attribute(ATTRIBUTES.movement_speed);
|
||||
|
||||
mob.add_effect(new EffectInstance(EFFECTS.speed, 100));
|
||||
mob.add_effect(new EffectInstance(EFFECTS.speed, 10, { amplifier: 2 }));
|
||||
const effect = mob.get_effect(EFFECTS.speed)!;
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:speed")!, 100));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:speed")!, 10, { amplifier: 2 }));
|
||||
const effect = mob.get_effect("bworld:speed")!;
|
||||
assertEquals([effect.amplifier, effect.duration, effect.hidden?.amplifier], [2, 10, 0]);
|
||||
assertAlmostEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed * 1.6);
|
||||
|
||||
@@ -84,7 +84,7 @@ Deno.test("a stronger, shorter effect hides a weaker one, which takes over when
|
||||
assertAlmostEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed * 1.2);
|
||||
|
||||
for (let i = 0; i < 90; i++) mob.tick_effects();
|
||||
assertEquals(mob.has_effect(EFFECTS.speed), false);
|
||||
assertEquals(mob.has_effect("bworld:speed"), false);
|
||||
assertEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed);
|
||||
});
|
||||
|
||||
@@ -93,16 +93,16 @@ Deno.test("effects: regeneration heals and poison never kills", async () => {
|
||||
const mob = spawn(0.5, 100, 0.5);
|
||||
|
||||
mob.health = 2;
|
||||
mob.add_effect(new EffectInstance(EFFECTS.poison, 200, { amplifier: 3 }));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:poison")!, 200, { amplifier: 3 }));
|
||||
for (let i = 0; i < 200; i++) mob.tick_effects();
|
||||
assertEquals(mob.health, 1);
|
||||
|
||||
mob.add_effect(new EffectInstance(EFFECTS.regeneration, 100, { amplifier: 1 }));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:regeneration")!, 100, { amplifier: 1 }));
|
||||
for (let i = 0; i < 100; i++) mob.tick_effects();
|
||||
// every 25 ticks at level 2
|
||||
assertEquals(mob.health, 5);
|
||||
|
||||
mob.add_effect(new EffectInstance(EFFECTS.instant_health, 1));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:instant_health")!, 1));
|
||||
assertEquals(mob.health, 9);
|
||||
});
|
||||
|
||||
@@ -321,7 +321,7 @@ Deno.test("mobs drown after their air runs out, unless they breathe water", asyn
|
||||
|
||||
const breather = spawn(3.5, 100, 3.5);
|
||||
breather.no_ai = true;
|
||||
breather.add_effect(new EffectInstance(EFFECTS.water_breathing, -1));
|
||||
breather.add_effect(new EffectInstance(effect_type("bworld:water_breathing")!, -1));
|
||||
ticks(340);
|
||||
assertEquals(breather.health, 10);
|
||||
});
|
||||
@@ -448,8 +448,8 @@ Deno.test("entities are saved with everything about them, riders on what they ri
|
||||
mob.add_tag("test");
|
||||
mob.data.mood = "happy";
|
||||
mob.health = 6;
|
||||
mob.add_effect(new EffectInstance(EFFECTS.speed, 100));
|
||||
mob.add_effect(new EffectInstance(EFFECTS.speed, 10, { amplifier: 1 }));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:speed")!, 100));
|
||||
mob.add_effect(new EffectInstance(effect_type("bworld:speed")!, 10, { amplifier: 1 }));
|
||||
mob.attributes.get(ATTRIBUTES.physical_shielding)!.base = 4;
|
||||
mob.set_item_slot("mainhand", new ItemStack("bworld:stick"));
|
||||
mob.restrict_to(1, 2, 3, 8);
|
||||
@@ -471,7 +471,7 @@ Deno.test("entities are saved with everything about them, riders on what they ri
|
||||
assertEquals([...horse.tags], ["test"]);
|
||||
assertEquals(horse.data, { mood: "happy" });
|
||||
assertEquals(horse.health, 6);
|
||||
const speed = horse.get_effect(EFFECTS.speed)!;
|
||||
const speed = horse.get_effect("bworld:speed")!;
|
||||
assertEquals([speed.amplifier, speed.duration, speed.hidden?.amplifier, speed.hidden?.duration], [1, 9, 0, 99]);
|
||||
assertAlmostEquals(horse.attribute(ATTRIBUTES.movement_speed), 0.25 * 1.4);
|
||||
assertEquals(horse.attribute(ATTRIBUTES.physical_shielding), 4);
|
||||
|
||||
@@ -35,6 +35,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
|
||||
spawns: mod.spawns.map((s) => s.json),
|
||||
entities: mod.entities.map((e) => e.json),
|
||||
entity_models: mod.entity_models.map((m) => m.json),
|
||||
effects: mod.effects.map((e) => e.json),
|
||||
tags: mod.tags.map((t) => t.json),
|
||||
},
|
||||
server_url: url(scripts.server),
|
||||
|
||||
@@ -205,6 +205,7 @@ Deno.test("tags survive packing into a .bmod", () => {
|
||||
spawns: [],
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
tags: [tag],
|
||||
};
|
||||
const manifest = { format_version: 1, id: "x", name: "X", version: "1.0.0" };
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type SpawnJson,
|
||||
type TagJson,
|
||||
validate_block,
|
||||
validate_effect,
|
||||
validate_entity,
|
||||
validate_item,
|
||||
validate_manifest,
|
||||
@@ -75,6 +76,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
|
||||
spawns: [],
|
||||
entities: [],
|
||||
entity_models: [],
|
||||
effects: [],
|
||||
tags: [],
|
||||
textures: [],
|
||||
texture_files: new Map(),
|
||||
@@ -126,6 +128,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
|
||||
load_data("items", "item", validate_item, mod.items);
|
||||
load_data("recipes", "recipe", validate_recipe, mod.recipes);
|
||||
load_data("entities", "entity", validate_entity, mod.entities);
|
||||
load_data("effects", "effect", validate_effect, mod.effects);
|
||||
|
||||
for (const file of walk(`${dir}/models`, ".geo.json")) {
|
||||
const path = `models/${file}`;
|
||||
|
||||
@@ -34,6 +34,7 @@ export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>>
|
||||
spawns: mod.spawns.map((s) => s.json),
|
||||
entities: mod.entities.map((e) => e.json),
|
||||
entity_models: mod.entity_models.map((m) => m.json),
|
||||
effects: mod.effects.map((e) => e.json),
|
||||
tags: mod.tags.map((t) => t.json),
|
||||
},
|
||||
scripts,
|
||||
|
||||
Reference in New Issue
Block a user