Jester time
This commit is contained in:
@@ -180,6 +180,12 @@ export interface LivingRef {
|
|||||||
// cause is for events and the death message, like "magic"
|
// cause is for events and the death message, like "magic"
|
||||||
damage(amount: number, cause?: string): void;
|
damage(amount: number, cause?: string): void;
|
||||||
heal(amount: number): void;
|
heal(amount: number): void;
|
||||||
|
// its stats, like Player's: "cubi:movement_speed", "cubi:attack_damage"... a mob's modifiers are saved with it
|
||||||
|
attribute(id: Id): number;
|
||||||
|
// replaces one with the same id on that attribute
|
||||||
|
add_modifier(attribute: Id, modifier: AttributeModifier): void;
|
||||||
|
// whether it had it
|
||||||
|
remove_modifier(attribute: Id, id: string): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ComponentRegistry {
|
export interface ComponentRegistry {
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ interface LivingRef {
|
|||||||
effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
|
effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
|
||||||
damage(amount: number, cause?: string): void; // cause is for events and the death message
|
damage(amount: number, cause?: string): void; // cause is for events and the death message
|
||||||
heal(amount: number): void;
|
heal(amount: number): void;
|
||||||
|
// its attributes and their modifiers, like a player's (see players.md). a mob's modifiers are saved with it
|
||||||
|
attribute(id: Id): number;
|
||||||
|
add_modifier(attribute: Id, modifier: AttributeModifier): void;
|
||||||
|
remove_modifier(attribute: Id, id: string): boolean;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -169,10 +169,19 @@ interface MobRef extends LivingRef {
|
|||||||
look_at(x: number, y: number, z: number): void; // for a couple of ticks, so call it every tick
|
look_at(x: number, y: number, z: number): void; // for a couple of ticks, so call it every tick
|
||||||
jump(): void; // this tick, or swims up in water
|
jump(): void; // this tick, or swims up in water
|
||||||
attack(target: Player | MobRef): void; // swings and hits with its attack_damage
|
attack(target: Player | MobRef): void; // swings and hits with its attack_damage
|
||||||
|
// what clients see of its state, for its animator, see Animators
|
||||||
|
get_client_data<T>(field: string): T | undefined;
|
||||||
|
set_client_data(field: string, value: unknown): void;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Each mob has one `MobRef`, so `mob.target === player` works.
|
Each mob has one `MobRef`, so `mob.target === player` works. Like any living entity, it has attributes that modifiers
|
||||||
|
change, so a mob that speeds up the longer it chases can do it with a modifier on its speed:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
mob.add_modifier("cubi:movement_speed", { id: "my_mod:rush", amount: 0.4, operation: "add_multiplied_total" });
|
||||||
|
mob.remove_modifier("cubi:movement_speed", "my_mod:rush");
|
||||||
|
```
|
||||||
|
|
||||||
## Getting Around
|
## Getting Around
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -4,14 +4,14 @@
|
|||||||
"id": "dangerous_business:jester",
|
"id": "dangerous_business:jester",
|
||||||
"model": "dangerous_business:jester",
|
"model": "dangerous_business:jester",
|
||||||
"texture": "dangerous_business:jester",
|
"texture": "dangerous_business:jester",
|
||||||
"behavior": "dangerous_business:coilhead",
|
"behavior": "dangerous_business:jester",
|
||||||
"width": 0.8,
|
"width": 0.8,
|
||||||
"height": 1.9,
|
"height": 1.9,
|
||||||
"max_health": 40,
|
"max_health": 40,
|
||||||
"movement_speed": 0.23,
|
"movement_speed": 0.23,
|
||||||
"jumps": false,
|
"jumps": false,
|
||||||
"max_fall": 0,
|
"max_fall": 0,
|
||||||
"attack_damage": 5,
|
"attack_damage": 99999,
|
||||||
"physical_shielding": 3
|
"physical_shielding": 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,20 @@
|
|||||||
{ "origin": [-4, 19, 1.3], "size": [8, 5, 6], "uv": [0, 25] },
|
{ "origin": [-4, 19, 1.3], "size": [8, 5, 6], "uv": [0, 25] },
|
||||||
{ "origin": [-4.5, 24, 0.8], "size": [9, 3, 7], "uv": [0, 15] },
|
{ "origin": [-4.5, 24, 0.8], "size": [9, 3, 7], "uv": [0, 15] },
|
||||||
{ "origin": [-5.5, 27, 0.3], "size": [11, 7, 8], "uv": [0, 0] },
|
{ "origin": [-5.5, 27, 0.3], "size": [11, 7, 8], "uv": [0, 0] },
|
||||||
{"origin": [-20.5, 24, 2.3], "size": [8, 3, 3], "pivot": [0, 0, 0], "rotation": [0, 0, 45], "uv": [35, 12]},
|
{
|
||||||
{"origin": [11.5, 24, 2.3], "size": [9, 3, 3], "pivot": [0, 0, 0], "rotation": [0, 0, -45], "uv": [30, 0]},
|
"origin": [-20.5, 24, 2.3],
|
||||||
|
"size": [8, 3, 3],
|
||||||
|
"pivot": [0, 0, 0],
|
||||||
|
"rotation": [0, 0, 45],
|
||||||
|
"uv": [35, 12]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [11.5, 24, 2.3],
|
||||||
|
"size": [9, 3, 3],
|
||||||
|
"pivot": [0, 0, 0],
|
||||||
|
"rotation": [0, 0, -45],
|
||||||
|
"uv": [30, 0]
|
||||||
|
},
|
||||||
{ "origin": [-4.5, 33.3, 1.3], "size": [9, 2, 6], "uv": [26, 19] }
|
{ "origin": [-4.5, 33.3, 1.3], "size": [9, 2, 6], "uv": [26, 19] }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -35,10 +47,37 @@
|
|||||||
"rotation": [7.5, 0, 0],
|
"rotation": [7.5, 0, 0],
|
||||||
"cubes": [
|
"cubes": [
|
||||||
{ "origin": [1.5, 0, -2], "size": [3, 2, 3], "uv": [33, 55] },
|
{ "origin": [1.5, 0, -2], "size": [3, 2, 3], "uv": [33, 55] },
|
||||||
{"origin": [1.5, 0, 0.5], "size": [3, 2, 3], "inflate": -0.1, "pivot": [-2.5, 0, 0], "rotation": [17.5, 0, 0], "uv": [21, 54]},
|
{
|
||||||
{"origin": [1.5, 0.9, 1.5], "size": [3, 7, 3], "pivot": [-2.5, -0.2, 0], "rotation": [5, 0, 0], "uv": [44, 39]},
|
"origin": [1.5, 0, 0.5],
|
||||||
{"origin": [1.5, 7.8, 0.7], "size": [3, 5, 3], "inflate": 0.2, "pivot": [-2.5, -0.2, 0], "rotation": [0, 0, 0], "uv": [12, 48]},
|
"size": [3, 2, 3],
|
||||||
{"origin": [1, 12.5, -3.7], "size": [4, 8, 4], "inflate": 0.2, "pivot": [-2.5, -0.2, 0], "rotation": [-17.5, 0, 0], "uv": [16, 36]}
|
"inflate": -0.1,
|
||||||
|
"pivot": [-2.5, 0, 0],
|
||||||
|
"rotation": [17.5, 0, 0],
|
||||||
|
"uv": [21, 54]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [1.5, 0.9, 1.5],
|
||||||
|
"size": [3, 7, 3],
|
||||||
|
"pivot": [-2.5, -0.2, 0],
|
||||||
|
"rotation": [5, 0, 0],
|
||||||
|
"uv": [44, 39]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [1.5, 7.8, 0.7],
|
||||||
|
"size": [3, 5, 3],
|
||||||
|
"inflate": 0.2,
|
||||||
|
"pivot": [-2.5, -0.2, 0],
|
||||||
|
"rotation": [0, 0, 0],
|
||||||
|
"uv": [12, 48]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [1, 12.5, -3.7],
|
||||||
|
"size": [4, 8, 4],
|
||||||
|
"inflate": 0.2,
|
||||||
|
"pivot": [-2.5, -0.2, 0],
|
||||||
|
"rotation": [-17.5, 0, 0],
|
||||||
|
"uv": [16, 36]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -68,10 +107,37 @@
|
|||||||
"rotation": [7.5, 0, 0],
|
"rotation": [7.5, 0, 0],
|
||||||
"cubes": [
|
"cubes": [
|
||||||
{ "origin": [-4.5, 0, -2], "size": [3, 2, 3], "uv": [53, 24] },
|
{ "origin": [-4.5, 0, -2], "size": [3, 2, 3], "uv": [53, 24] },
|
||||||
{"origin": [-4.5, 0, 0.5], "size": [3, 2, 3], "inflate": -0.1, "pivot": [2.5, 0, 0], "rotation": [17.5, 0, 0], "uv": [50, 7]},
|
{
|
||||||
{"origin": [-4.5, 0.9, 1.5], "size": [3, 7, 3], "pivot": [2.5, -0.2, 0], "rotation": [5, 0, 0], "uv": [32, 39]},
|
"origin": [-4.5, 0, 0.5],
|
||||||
{"origin": [-4.5, 7.8, 0.7], "size": [3, 5, 3], "inflate": 0.2, "pivot": [2.5, -0.2, 0], "rotation": [0, 0, 0], "uv": [0, 48]},
|
"size": [3, 2, 3],
|
||||||
{"origin": [-5, 12.5, -3.7], "size": [4, 8, 4], "inflate": 0.2, "pivot": [2.5, -0.2, 0], "rotation": [-17.5, 0, 0], "uv": [0, 36]}
|
"inflate": -0.1,
|
||||||
|
"pivot": [2.5, 0, 0],
|
||||||
|
"rotation": [17.5, 0, 0],
|
||||||
|
"uv": [50, 7]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [-4.5, 0.9, 1.5],
|
||||||
|
"size": [3, 7, 3],
|
||||||
|
"pivot": [2.5, -0.2, 0],
|
||||||
|
"rotation": [5, 0, 0],
|
||||||
|
"uv": [32, 39]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [-4.5, 7.8, 0.7],
|
||||||
|
"size": [3, 5, 3],
|
||||||
|
"inflate": 0.2,
|
||||||
|
"pivot": [2.5, -0.2, 0],
|
||||||
|
"rotation": [0, 0, 0],
|
||||||
|
"uv": [0, 48]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"origin": [-5, 12.5, -3.7],
|
||||||
|
"size": [4, 8, 4],
|
||||||
|
"inflate": 0.2,
|
||||||
|
"pivot": [2.5, -0.2, 0],
|
||||||
|
"rotation": [-17.5, 0, 0],
|
||||||
|
"uv": [0, 36]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import type { Position, Schematic } from "cubi/server";
|
|||||||
// placed, so a plan that comes out too small can be tried again without touching the world
|
// placed, so a plan that comes out too small can be tried again without touching the world
|
||||||
|
|
||||||
export const DOORWAY = "dangerous_business:doorway";
|
export const DOORWAY = "dangerous_business:doorway";
|
||||||
|
// where the surface is placed, high above the interior. anyone higher than it is outside the facility
|
||||||
|
export const SURFACE_Y = 150;
|
||||||
export const VENT_SPOT = "dangerous_business:vent_spot";
|
export const VENT_SPOT = "dangerous_business:vent_spot";
|
||||||
|
|
||||||
// a marker block in a tile, a doorway or a vent spot, with its facing state: 0 north, 1 east, 2 south, 3 west. in a
|
// a marker block in a tile, a doorway or a vent spot, with its facing state: 0 north, 1 east, 2 south, 3 west. in a
|
||||||
@@ -40,6 +42,10 @@ export interface TileType extends TileRules {
|
|||||||
vent_spots: Marker[];
|
vent_spots: Marker[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function in_facility(position: Position) {
|
||||||
|
return position.y <= SURFACE_Y;
|
||||||
|
}
|
||||||
|
|
||||||
export interface PlacedTile {
|
export interface PlacedTile {
|
||||||
tile: string;
|
tile: string;
|
||||||
// where the schematic's lowest corner goes, before it's turned around it
|
// where the schematic's lowest corner goes, before it's turned around it
|
||||||
|
|||||||
@@ -0,0 +1,236 @@
|
|||||||
|
// the jester: a box on legs that wanders until it sees a player. it follows them for a while, stops to wind its crank,
|
||||||
|
// and pops open, then runs down every player in the facility, faster and faster, until there's nobody left inside
|
||||||
|
import type { MobRef, Player, ScriptGoal, ServerContext } from "cubi/server";
|
||||||
|
import { type AnimatedEntity, AnimationState, type ClientContext, type Clip, type EntityPose } from "cubi/client";
|
||||||
|
import { opens_doors } from "./coilhead.ts";
|
||||||
|
import { in_facility } from "./interior.ts";
|
||||||
|
|
||||||
|
const JESTER = "dangerous_business:jester";
|
||||||
|
|
||||||
|
// its "state" client data, what its animator plays
|
||||||
|
const ROAMING = 1;
|
||||||
|
const CRANKING = 2;
|
||||||
|
const AGGRO = 3;
|
||||||
|
|
||||||
|
// how far away it notices a player it can see
|
||||||
|
const SEE_RANGE = 24;
|
||||||
|
// how close it comes to the player it's following
|
||||||
|
const FOLLOW_DISTANCE = 3;
|
||||||
|
// seconds after it first notices someone before it starts cranking, shorter with more players around
|
||||||
|
const COUNTDOWN_ALONE = [25, 42];
|
||||||
|
const COUNTDOWN_TOGETHER = [13, 21];
|
||||||
|
const CRANK_TICKS = 40 * 20;
|
||||||
|
// how long it stands after popping before it goes
|
||||||
|
const POP_TICKS = 2 * 20;
|
||||||
|
// it chases a fifth faster than it walks, and another fifth every SPEED_UP_TICKS, up to twice as fast
|
||||||
|
const SPEED_STEP = 0.2;
|
||||||
|
const SPEED_UP_TICKS = 120;
|
||||||
|
const MAX_SPEED_STEPS = 8;
|
||||||
|
const CHASE_SPEED = "dangerous_business:jester_chase";
|
||||||
|
// it only waits a moment at a closed door
|
||||||
|
const OPEN_DOOR_TICKS = 20;
|
||||||
|
|
||||||
|
export function register_jester(ctx: ServerContext) {
|
||||||
|
const builtin = ctx.entities.goals;
|
||||||
|
const chase = builtin.melee_attack();
|
||||||
|
const stroll = builtin.random_stroll();
|
||||||
|
const look_around = builtin.random_look_around();
|
||||||
|
|
||||||
|
ctx.entities.register_behavior(JESTER, (mob, goals) => {
|
||||||
|
let state = mob.get_client_data<number>("state") ?? ROAMING;
|
||||||
|
// ticks in its state
|
||||||
|
let ticks = 0;
|
||||||
|
// ticks left until it cranks, from when it first noticed someone
|
||||||
|
let countdown: number | undefined;
|
||||||
|
let followed: Player | undefined;
|
||||||
|
const set_state = (next: number) => {
|
||||||
|
state = next;
|
||||||
|
ticks = 0;
|
||||||
|
mob.set_client_data("state", next);
|
||||||
|
};
|
||||||
|
|
||||||
|
// what it's doing, which the goals after it follow
|
||||||
|
goals.add(0, {
|
||||||
|
can_use: () => true,
|
||||||
|
tick: () => {
|
||||||
|
ticks++;
|
||||||
|
if (state === ROAMING) {
|
||||||
|
if (followed && (followed.health <= 0 || !in_facility(followed.position))) followed = undefined;
|
||||||
|
followed ??= ctx.players.all().find((player) => can_see(ctx, mob, player));
|
||||||
|
if (followed && countdown === undefined) {
|
||||||
|
const [low, high] = ctx.players.all().length > 1 ? COUNTDOWN_TOGETHER : COUNTDOWN_ALONE;
|
||||||
|
countdown = Math.round((low + (high - low) * mob.random()) * 20);
|
||||||
|
}
|
||||||
|
if (countdown !== undefined && --countdown <= 0) {
|
||||||
|
countdown = undefined;
|
||||||
|
followed = undefined;
|
||||||
|
set_state(CRANKING);
|
||||||
|
}
|
||||||
|
} else if (state === CRANKING) {
|
||||||
|
if (ticks >= CRANK_TICKS) set_state(AGGRO);
|
||||||
|
} else {
|
||||||
|
const target = nearest_player(ctx, mob);
|
||||||
|
if (!target) {
|
||||||
|
mob.target = undefined;
|
||||||
|
mob.remove_modifier("cubi:movement_speed", CHASE_SPEED);
|
||||||
|
set_state(ROAMING);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ticks < POP_TICKS) return;
|
||||||
|
mob.target = target;
|
||||||
|
const steps = Math.min(MAX_SPEED_STEPS, 1 + Math.floor((ticks - POP_TICKS) / SPEED_UP_TICKS));
|
||||||
|
mob.add_modifier("cubi:movement_speed", {
|
||||||
|
id: CHASE_SPEED,
|
||||||
|
amount: steps * SPEED_STEP,
|
||||||
|
operation: "add_multiplied_total",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// stands still while it cranks and while it pops
|
||||||
|
goals.add(1, {
|
||||||
|
flags: ["move", "look", "jump"],
|
||||||
|
can_use: () => state === CRANKING || (state === AGGRO && ticks < POP_TICKS),
|
||||||
|
start: () => mob.stop(),
|
||||||
|
});
|
||||||
|
goals.add(2, chase);
|
||||||
|
goals.add(3, follow(mob, () => state === ROAMING ? followed : undefined));
|
||||||
|
goals.add(4, stroll);
|
||||||
|
goals.add(5, look_around);
|
||||||
|
goals.add(6, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// keeps close to who(), looking at them, and stands by them once it's there instead of wandering off. it goes a new
|
||||||
|
// way only once they've moved to another block
|
||||||
|
function follow(mob: MobRef, who: () => Player | undefined): ScriptGoal {
|
||||||
|
let block = "";
|
||||||
|
return {
|
||||||
|
flags: ["move", "look"],
|
||||||
|
can_use: () => who() !== undefined,
|
||||||
|
start: () => block = "",
|
||||||
|
stop: () => mob.stop(),
|
||||||
|
tick: () => {
|
||||||
|
// the goal deciding what the jester does can let them go this tick
|
||||||
|
const player = who();
|
||||||
|
if (!player) return;
|
||||||
|
const { x, y, z } = player.position;
|
||||||
|
mob.look_at(x, y + 1.5, z);
|
||||||
|
const at = mob.position;
|
||||||
|
if (Math.hypot(x - at.x, y - at.y, z - at.z) <= FOLLOW_DISTANCE) {
|
||||||
|
if (block !== "") mob.stop();
|
||||||
|
block = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const now = `${Math.floor(x)},${Math.floor(y)},${Math.floor(z)}`;
|
||||||
|
if (now === block) return;
|
||||||
|
block = now;
|
||||||
|
mob.move_to(x, y, z);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// whether it can see the player's eyes from its own, close enough to notice them, in the facility
|
||||||
|
function can_see(ctx: ServerContext, mob: MobRef, player: Player) {
|
||||||
|
if (player.health <= 0 || !in_facility(player.position)) return false;
|
||||||
|
const from = mob.eye_position;
|
||||||
|
const to = player.eye_position;
|
||||||
|
if (Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z) > SEE_RANGE) return false;
|
||||||
|
return ctx.world.raycast(from, to) === undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the closest living player in the facility, however far
|
||||||
|
function nearest_player(ctx: ServerContext, mob: MobRef) {
|
||||||
|
const at = mob.position;
|
||||||
|
const distance = ({ position: { x, y, z } }: Player) => Math.hypot(x - at.x, y - at.y, z - at.z);
|
||||||
|
const inside = ctx.players.all().filter((player) => player.health > 0 && in_facility(player.position));
|
||||||
|
return inside.sort((a, b) => distance(a) - distance(b))[0] as Player | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// what bedrock's animation controller did: idle and walk while it roams, crank, then pop, then its chase. what its
|
||||||
|
// clips did with expressions is here as code
|
||||||
|
export function register_jester_animator(ctx: ClientContext) {
|
||||||
|
const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`);
|
||||||
|
const idle = clip("idle");
|
||||||
|
const walk = clip("walk");
|
||||||
|
const crank = clip("crank");
|
||||||
|
const pop = clip("pop");
|
||||||
|
const aggro_idle = clip("aggro_idle");
|
||||||
|
const aggro_walk = clip("aggro_walk");
|
||||||
|
const attack = clip("attack");
|
||||||
|
|
||||||
|
ctx.render.register_entity_animator(JESTER, (jester) => {
|
||||||
|
const cranking = new AnimationState();
|
||||||
|
const popped = new AnimationState();
|
||||||
|
const attacking = new AnimationState();
|
||||||
|
return {
|
||||||
|
on_data(field, time) {
|
||||||
|
if (field !== "state") return;
|
||||||
|
const state = jester.get_client_data<number>("state");
|
||||||
|
cranking.animate_when(state === CRANKING, time);
|
||||||
|
popped.animate_when(state === AGGRO, time);
|
||||||
|
},
|
||||||
|
pose(pose, time) {
|
||||||
|
if (cranking.started) {
|
||||||
|
pose.play_state(crank, cranking, time);
|
||||||
|
} else if (popped.started && !popped.finished(pop, time)) {
|
||||||
|
pose.play_state(pop, popped, time);
|
||||||
|
} else if (popped.started) {
|
||||||
|
const swinging = jester.attack_progress(time) > 0;
|
||||||
|
if (swinging && (!attacking.started || attacking.finished(attack, time))) attacking.start(time);
|
||||||
|
if (attacking.started && !attacking.finished(attack, time)) {
|
||||||
|
pose.play_state(attack, attacking, time);
|
||||||
|
} else {
|
||||||
|
// its walk goes half as fast as when it roams
|
||||||
|
walking(pose, jester, time, aggro_walk, 40, (weight) => {
|
||||||
|
pose.play(aggro_idle, time / 20, weight);
|
||||||
|
sway(pose, time, weight, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
walking(pose, jester, time, walk, 20, (weight) => {
|
||||||
|
pose.play(idle, time / 20, weight);
|
||||||
|
sway(pose, time, weight, false);
|
||||||
|
});
|
||||||
|
// both clips hide them, which blended halfway would only shrink them
|
||||||
|
pose.scale("tendrils", 0, 0, 0);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// clip as it walks, a second of it for ticks of walk_position, and standing as strongly as it isn't walking
|
||||||
|
function walking(
|
||||||
|
pose: EntityPose,
|
||||||
|
jester: AnimatedEntity,
|
||||||
|
time: number,
|
||||||
|
clip: Clip,
|
||||||
|
ticks: number,
|
||||||
|
standing: (weight: number) => void,
|
||||||
|
) {
|
||||||
|
const speed = Math.min(jester.walk_speed(time), 1);
|
||||||
|
pose.play(clip, jester.walk_position(time) / ticks, speed);
|
||||||
|
standing(1 - speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the box bobbing and the arm swaying as it stands, and once it's popped the lid, skull, jaw and crank too
|
||||||
|
function sway(pose: EntityPose, time: number, weight: number, popped: boolean) {
|
||||||
|
// bedrock's math.cos and math.sin of life_time (seconds) times degrees a second
|
||||||
|
const seconds = time / 20;
|
||||||
|
const cos = (degrees_a_second: number) => Math.cos(seconds * degrees_a_second * Math.PI / 180) * weight;
|
||||||
|
const sin = (degrees_a_second: number) => Math.sin(seconds * degrees_a_second * Math.PI / 180) * weight;
|
||||||
|
pose.rotate("box", cos(180) * 2.5, 0, 0);
|
||||||
|
pose.rotate("left_arm", (-1.6248 * weight) + cos(90) * 5, -22.22485 * weight, 70.78761 * weight);
|
||||||
|
pose.rotate("left_forearm", cos(180) * 5, 62.5 * weight, 0);
|
||||||
|
if (!popped) {
|
||||||
|
pose.move("box", 0, -weight + cos(180), 0);
|
||||||
|
pose.move("left_arm", 0, 3 * weight - cos(180), 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pose.rotate("tendrils", sin(180) * 2.5, 0, 0);
|
||||||
|
pose.rotate("box_lid", -127.5 * weight + sin(180) * 5, 0, 0);
|
||||||
|
pose.rotate("skull", cos(180) * -2.5, 0, 0);
|
||||||
|
pose.rotate("jaw", 10 * weight + sin(180) * 5, 0, 0);
|
||||||
|
pose.rotate("crank", sin(180) * 2.5, 0, 0);
|
||||||
|
}
|
||||||
@@ -3,7 +3,17 @@ import type { ServerContext } from "cubi/server";
|
|||||||
import { DOOR, FACILITY_DOOR } from "./shared.ts";
|
import { DOOR, FACILITY_DOOR } from "./shared.ts";
|
||||||
import { Alea } from "@paulaboks/rng";
|
import { Alea } from "@paulaboks/rng";
|
||||||
import { register_coilhead, register_coilhead_animator } from "./coilhead.ts";
|
import { register_coilhead, register_coilhead_animator } from "./coilhead.ts";
|
||||||
import { DOORWAY, MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts";
|
import { register_jester, register_jester_animator } from "./jester.ts";
|
||||||
|
import {
|
||||||
|
DOORWAY,
|
||||||
|
MAIN_TILE,
|
||||||
|
type Marker,
|
||||||
|
type PlacedTile,
|
||||||
|
plan_interior,
|
||||||
|
SURFACE_Y,
|
||||||
|
tile_type,
|
||||||
|
TILES,
|
||||||
|
} from "./interior.ts";
|
||||||
|
|
||||||
export { shared } from "./shared.ts";
|
export { shared } from "./shared.ts";
|
||||||
|
|
||||||
@@ -15,11 +25,13 @@ export function server(ctx: ServerContext) {
|
|||||||
register_death(ctx);
|
register_death(ctx);
|
||||||
register_commands(ctx);
|
register_commands(ctx);
|
||||||
register_coilhead(ctx);
|
register_coilhead(ctx);
|
||||||
|
register_jester(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function client(ctx: ClientContext) {
|
export function client(ctx: ClientContext) {
|
||||||
register_hud(ctx);
|
register_hud(ctx);
|
||||||
register_coilhead_animator(ctx);
|
register_coilhead_animator(ctx);
|
||||||
|
register_jester_animator(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
|
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
|
||||||
@@ -142,7 +154,7 @@ function clear_map(ctx: ServerContext) {
|
|||||||
function place_surface(ctx: ServerContext) {
|
function place_surface(ctx: ServerContext) {
|
||||||
ctx.schematics.place(
|
ctx.schematics.place(
|
||||||
"dangerous_business:assurance",
|
"dangerous_business:assurance",
|
||||||
{ position: { x: -80, y: 150, z: -80 } },
|
{ position: { x: -80, y: SURFACE_Y, z: -80 } },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -287,7 +287,9 @@ export class MeleeAttackGoal extends Goal {
|
|||||||
|
|
||||||
override tick() {
|
override tick() {
|
||||||
const mob = this.mob;
|
const mob = this.mob;
|
||||||
const target = mob.target!;
|
// a script's goal ticking before this one can have let it go, it stops next tick
|
||||||
|
const target = mob.target;
|
||||||
|
if (!target) return;
|
||||||
mob.look_control.look_at_entity(target);
|
mob.look_control.look_at_entity(target);
|
||||||
this.#recalculate -= 1;
|
this.#recalculate -= 1;
|
||||||
const block = `${Math.floor(target.x)},${Math.floor(target.y)},${Math.floor(target.z)}`;
|
const block = `${Math.floor(target.x)},${Math.floor(target.y)},${Math.floor(target.z)}`;
|
||||||
|
|||||||
+20
-12
@@ -1681,18 +1681,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
|||||||
}
|
}
|
||||||
player.vitals.add_exhaustion(amount);
|
player.vitals.add_exhaustion(amount);
|
||||||
},
|
},
|
||||||
attribute: (id) => player.attribute(known_attribute(id)),
|
...attributes_api(player),
|
||||||
add_modifier(attribute, modifier) {
|
|
||||||
const type = known_attribute(attribute);
|
|
||||||
const { id, amount, operation } = modifier;
|
|
||||||
if (typeof id !== "string" || typeof amount !== "number" || !Number.isFinite(amount)) {
|
|
||||||
throw new Error("a modifier needs a string id and a number amount");
|
|
||||||
}
|
|
||||||
if (!MODIFIER_OPERATIONS.includes(operation)) throw new Error(`unknown modifier operation ${operation}`);
|
|
||||||
player.attributes.get(type)?.add_modifier({ id, amount, operation });
|
|
||||||
},
|
|
||||||
remove_modifier: (attribute, id) =>
|
|
||||||
player.attributes.get(known_attribute(attribute))?.remove_modifier(id) ?? false,
|
|
||||||
add_effect(id, duration, options = {}) {
|
add_effect(id, duration, options = {}) {
|
||||||
const type = known_effect(id);
|
const type = known_effect(id);
|
||||||
if (!Number.isInteger(duration) || (duration < 1 && duration !== -1)) {
|
if (!Number.isInteger(duration) || (duration < 1 && duration !== -1)) {
|
||||||
@@ -1748,8 +1737,27 @@ function look_direction(yaw: number, pitch: number): Position {
|
|||||||
return { x: -Math.sin(yaw) * cos_pitch, y: Math.sin(pitch), z: -Math.cos(yaw) * cos_pitch };
|
return { x: -Math.sin(yaw) * cos_pitch, y: Math.sin(pitch), z: -Math.cos(yaw) * cos_pitch };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// a player's or a mob's attribute, add_modifier and remove_modifier
|
||||||
|
function attributes_api(entity: LivingEntity): Pick<LivingRef, "attribute" | "add_modifier" | "remove_modifier"> {
|
||||||
|
return {
|
||||||
|
attribute: (id) => entity.attribute(known_attribute(id)),
|
||||||
|
add_modifier(attribute, modifier) {
|
||||||
|
const type = known_attribute(attribute);
|
||||||
|
const { id, amount, operation } = modifier;
|
||||||
|
if (typeof id !== "string" || typeof amount !== "number" || !Number.isFinite(amount)) {
|
||||||
|
throw new Error("a modifier needs a string id and a number amount");
|
||||||
|
}
|
||||||
|
if (!MODIFIER_OPERATIONS.includes(operation)) throw new Error(`unknown modifier operation ${operation}`);
|
||||||
|
entity.attributes.get(type)?.add_modifier({ id, amount, operation });
|
||||||
|
},
|
||||||
|
remove_modifier: (attribute, id) =>
|
||||||
|
entity.attributes.get(known_attribute(attribute))?.remove_modifier(id) ?? false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function living_ref(entity: LivingEntity): LivingRef {
|
export function living_ref(entity: LivingEntity): LivingRef {
|
||||||
return {
|
return {
|
||||||
|
...attributes_api(entity),
|
||||||
id: entity.id,
|
id: entity.id,
|
||||||
get health() {
|
get health() {
|
||||||
return entity.health;
|
return entity.health;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ const FLOOR = "dangerous_business:concrete_floor";
|
|||||||
const DEATH = "dangerous_business:death";
|
const DEATH = "dangerous_business:death";
|
||||||
const PIPE = "dangerous_business:double_steel_pipes";
|
const PIPE = "dangerous_business:double_steel_pipes";
|
||||||
const COILHEAD = "dangerous_business:coilhead";
|
const COILHEAD = "dangerous_business:coilhead";
|
||||||
|
const JESTER = "dangerous_business:jester";
|
||||||
const FACTORY_DOOR = "dangerous_business:factory_door";
|
const FACTORY_DOOR = "dangerous_business:factory_door";
|
||||||
|
|
||||||
// alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of
|
// alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of
|
||||||
@@ -256,6 +257,51 @@ Deno.test("a coilhead seen while it waits at a closed door stops waiting, and do
|
|||||||
assertEquals(coilhead.navigation.blocked_by, undefined);
|
assertEquals(coilhead.navigation.blocked_by, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// a jester 6 blocks north of alice, in a state from its client data like one that was saved
|
||||||
|
function spawn_jester(t: Awaited<ReturnType<typeof setup>>["t"], state?: number) {
|
||||||
|
const jester = create_entity(t.game.entities, JESTER)! as Mob;
|
||||||
|
jester.set_position(0.5, 100, -2.5);
|
||||||
|
if (state !== undefined) jester.client_data = { state };
|
||||||
|
t.game.entities.add_entity(jester);
|
||||||
|
return jester;
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test("a jester follows a player it sees without hurting them, then winds its crank standing still", async () => {
|
||||||
|
const { t } = await setup();
|
||||||
|
const alice = t.game.players()[0];
|
||||||
|
const jester = spawn_jester(t);
|
||||||
|
for (let i = 0; i < 100; i++) t.game.tick();
|
||||||
|
assert(jester.z > -1, `it stayed at z ${jester.z}, away from her`);
|
||||||
|
assertEquals(alice.health, alice.max_health);
|
||||||
|
|
||||||
|
for (let i = 0; i < 2000 && jester.client_data.state !== 2; i++) t.game.tick();
|
||||||
|
assertEquals(jester.client_data.state, 2);
|
||||||
|
const at = [jester.x, jester.z];
|
||||||
|
t.send(1, { type: "move", x: -2.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
|
||||||
|
for (let i = 0; i < 100; i++) t.game.tick();
|
||||||
|
assertEquals([jester.x, jester.z], at);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("a popped jester runs down the nearest player faster and faster, and boxes up once they're out", async () => {
|
||||||
|
const { t } = await setup();
|
||||||
|
const alice = t.game.players()[0];
|
||||||
|
const jester = spawn_jester(t, 3);
|
||||||
|
const speed = () => t.game.mods.mob(jester).attribute("cubi:movement_speed");
|
||||||
|
const walking = speed();
|
||||||
|
for (let i = 0; i < 200 && alice.health === alice.max_health; i++) t.game.tick();
|
||||||
|
assert(alice.health < alice.max_health, "it never got to her");
|
||||||
|
assert(speed() > walking);
|
||||||
|
const chasing = speed();
|
||||||
|
for (let i = 0; i < 300; i++) t.game.tick();
|
||||||
|
assert(speed() > chasing, "it didn't get any faster");
|
||||||
|
|
||||||
|
// up on the surface, out of the facility
|
||||||
|
alice.set_position(0.5, 160, 3.5);
|
||||||
|
t.game.tick();
|
||||||
|
assertEquals(jester.client_data.state, 1);
|
||||||
|
assertEquals(speed(), walking);
|
||||||
|
});
|
||||||
|
|
||||||
// last, since it loads the mod the way a player gets it, in place of the server's
|
// last, since it loads the mod the way a player gets it, in place of the server's
|
||||||
Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => {
|
Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => {
|
||||||
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
|
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
|
||||||
@@ -279,3 +325,27 @@ Deno.test("a coilhead's head springs back and forth when it freezes, and stays s
|
|||||||
assertEquals(head(true, 1200), head(false, 1200));
|
assertEquals(head(true, 1200), head(false, 1200));
|
||||||
Deno.removeSync(dir, { recursive: true });
|
Deno.removeSync(dir, { recursive: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("a jester keeps its tendrils in the box until it pops, and turns its crank while it winds", async () => {
|
||||||
|
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
|
||||||
|
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
|
||||||
|
await load_as_player(dir);
|
||||||
|
const pose = (state: number | undefined, time: number) => {
|
||||||
|
const jester = {
|
||||||
|
id: "e1",
|
||||||
|
type: JESTER,
|
||||||
|
data: state === undefined ? {} : { client_data: { state } },
|
||||||
|
age: 0,
|
||||||
|
yaw: 0,
|
||||||
|
head_yaw: 0,
|
||||||
|
pitch: 0,
|
||||||
|
walk_animation: () => ({ position: 0, speed: 0 }),
|
||||||
|
attack_animation: () => 0,
|
||||||
|
};
|
||||||
|
return animation_for(jester)!.pose(time)!;
|
||||||
|
};
|
||||||
|
assertEquals(pose(undefined, 10).get("tendrils")!.scale, [0, 0, 0]);
|
||||||
|
assertNotEquals(pose(3, 100).get("tendrils")!.scale, [0, 0, 0]);
|
||||||
|
assertNotEquals(pose(2, 5).get("crank")!.rotation, pose(2, 15).get("crank")!.rotation);
|
||||||
|
Deno.removeSync(dir, { recursive: true });
|
||||||
|
});
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
// mobs following paths, see server/game/entity/navigation.ts
|
// mobs following paths, see server/game/entity/navigation.ts
|
||||||
import { assert, assertEquals } from "@std/assert";
|
import { assert, assertEquals } from "@std/assert";
|
||||||
import { test_game, test_mob } from "./helpers.ts";
|
import { test_game, test_mob } from "./helpers.ts";
|
||||||
|
import { MeleeAttackGoal } from "@/server/game/entity/goals.ts";
|
||||||
|
|
||||||
// two rooms on a stone floor at y 99, split by a wall at x 3 with a doorway 2 high at z 4. a goal-less mob in the
|
// two rooms on a stone floor at y 99, split by a wall at x 3 with a doorway 2 high at z 4. a goal-less mob in the
|
||||||
// west room at 0.5 100 0.5, and alice in its corner, so it thinks
|
// west room at 0.5 100 0.5, and alice in its corner, so it thinks
|
||||||
@@ -81,3 +82,15 @@ Deno.test("a script's mob walks its way, and stops when told", async () => {
|
|||||||
assert(Math.hypot(ref.position.x - x, ref.position.z - z) < 0.5);
|
assert(Math.hypot(ref.position.x - x, ref.position.z - z) < 0.5);
|
||||||
assertEquals(ref.blocked_by, undefined);
|
assertEquals(ref.blocked_by, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("a mob going after its target stops when a script lets the target go partway through its thinking", async () => {
|
||||||
|
const { game, mob } = await rooms();
|
||||||
|
const melee = new MeleeAttackGoal(mob);
|
||||||
|
mob.target = game.players()[0];
|
||||||
|
assert(melee.can_use());
|
||||||
|
melee.start();
|
||||||
|
melee.tick();
|
||||||
|
mob.target = undefined;
|
||||||
|
melee.tick();
|
||||||
|
assert(!melee.can_continue_to_use());
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user