Monsters opening doors

This commit is contained in:
2026-11-21 15:16:07 -03:00
parent 2563ec3f69
commit 94463b3cea
6 changed files with 164 additions and 71 deletions
+28 -2
View File
@@ -1,5 +1,6 @@
// the coilhead: it goes after players like any monster, but can't move while one of them is looking at it
import type { MobRef, Player, Position, ServerContext } from "cubi/server";
import type { MobRef, Player, Position, ScriptGoal, ServerContext } from "cubi/server";
import { DOOR, type DoorParams, set_door_open } from "./shared.ts";
// how far away a player still freezes it
const SEEN_RANGE = 48;
@@ -7,7 +8,9 @@ const SEEN_RANGE = 48;
// way, about what fits on the screen
const VIEW_COS = 0.5;
// how much faster than its movement_speed it runs at its target
const CHASE_SPEED = 1.25;
const CHASE_SPEED = 2;
// how long it stands at a closed door on its way before opening it, in ticks
const OPEN_DOOR_TICKS = 20 * 20;
export function register_coilhead(ctx: ServerContext) {
const builtin = ctx.entities.goals;
@@ -27,11 +30,34 @@ export function register_coilhead(ctx: ServerContext) {
goals.add(1, chase);
goals.add(2, stroll);
goals.add(3, look_around);
goals.add(4, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
goals.add_target(0, hurt_by);
goals.add_target(1, nearest_player);
});
}
// opens a door on the mob's way after it's stood at it for ticks. without flags, so the goal going after its target
// keeps its way and the mob stays at the door. the door's in the mobs_path_through tag, so its way goes through it
export function opens_doors(ctx: ServerContext, mob: MobRef, ticks: number): ScriptGoal {
let waited = 0;
const door = () => {
const blocked = mob.blocked_by;
return blocked && DOOR in ctx.blocks.components(blocked.id) ? blocked : undefined;
};
return {
can_use: () => door() !== undefined,
start: () => waited = 0,
tick: () => {
// a goal that started this tick, like being seen, can stop the mob's way and with it what blocks it
const blocked = door();
if (!blocked) return;
const { x, y, z, id } = blocked;
if (++waited !== ticks) return;
set_door_open(ctx, x, y, z, 1, ctx.blocks.components(id)[DOOR] as DoorParams);
},
};
}
// whether the player is looking its way with nothing in between, at its head or its middle
function sees(ctx: ServerContext, player: Player, mob: MobRef) {
if (player.health <= 0) return false;