diff --git a/mods/dangerous_business/entities/coilhead.json b/mods/dangerous_business/entities/coilhead.json index 0732a6e..7704b8f 100644 --- a/mods/dangerous_business/entities/coilhead.json +++ b/mods/dangerous_business/entities/coilhead.json @@ -6,7 +6,7 @@ "texture": "dangerous_business:coilhead", "behavior": "dangerous_business:coilhead", "width": 0.8, - "height": 2.5, + "height": 1.9, "max_health": 40, "movement_speed": 0.23, "attack_damage": 5, diff --git a/mods/dangerous_business/models/coilhead.geo.json b/mods/dangerous_business/models/coilhead.geo.json index 3b6776a..8dec05d 100644 --- a/mods/dangerous_business/models/coilhead.geo.json +++ b/mods/dangerous_business/models/coilhead.geo.json @@ -3,7 +3,7 @@ "minecraft:geometry": [ { "description": { - "identifier": "geometry.unknown", + "identifier": "geometry.CoilHead", "texture_width": 128, "texture_height": 128, "visible_bounds_width": 3, @@ -12,77 +12,191 @@ }, "bones": [ { - "name": "root", + "name": "coilhead", "pivot": [0, 0, 0] }, { - "name": "Torso", - "parent": "root", - "pivot": [0.5, 17, 0], + "name": "body", + "parent": "coilhead", + "pivot": [0, 0, 0], "cubes": [ - {"origin": [-4.5, 17, -3.5], "size": [9, 14, 5], "uv": [0, 0]}, - {"origin": [5.5, 20, -5.499], "size": [0, 1, 1], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [1, 1]}, - {"origin": [10.5, 23, -5.499], "size": [0, 1, 1], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [1, 1]}, - {"origin": [-1.5, 20.05952, -5.5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [-0.5, 26.05952, -5.5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [-0.5, 24.05952, 1.5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [-2.5, 19.05952, 1.5], "size": [0, 1, 2], "uv": [0, 0]} + {"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": [-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": [-4.5, 33.3, 1.3], "size": [9, 2, 6], "uv": [26, 19]} ] }, { - "name": "RightArm", - "parent": "Torso", - "pivot": [-5, 28, -1.5], + "name": "left_leg", + "parent": "body", + "pivot": [3, 20, 4.7], + "rotation": [7.5, 0, 0], "cubes": [ - {"origin": [-8.5, 20, -3], "size": [4, 10, 4], "uv": [0, 34]}, - {"origin": [-5.5, 28.05952, -5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [-5.5, 28.05952, 1], "size": [0, 1, 2], "uv": [0, 0]} + {"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, 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]} ] }, { - "name": "LeftArm", - "parent": "Torso", - "pivot": [5, 28.66032, -0.66633], + "name": "nail2", + "parent": "left_leg", + "pivot": [3.8, 6, 1.7], + "rotation": [64.46949, 11.31255, -5.35254], "cubes": [ - {"origin": [4.5, 22, -3], "size": [4, 8, 4], "uv": [40, 38]}, - {"origin": [6.5, 27.05952, -4.5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [8.5, 27, -10.499], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]} + {"origin": [2.8, 6, 0.7], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [2.3, 8, 0.2], "size": [2, 1, 2], "uv": [0, 0]} ] }, { - "name": "Neck", - "parent": "Torso", - "pivot": [-1.5, 31, 0], + "name": "nail3", + "parent": "left_leg", + "pivot": [4.5, 14.7, 1.9], + "rotation": [74.30716, -16.88547, 4.6653], "cubes": [ - {"origin": [-2, 30.75, -3], "size": [4, 9, 4], "uv": [16, 38]} + {"origin": [3.5, 14.7, 0.9], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [3, 16.7, 0.4], "size": [2, 1, 2], "uv": [0, 0]} ] }, { - "name": "Head", - "parent": "Neck", - "pivot": [0, 39.75, -1], + "name": "right_leg", + "parent": "body", + "pivot": [-3, 20, 4.7], + "rotation": [7.5, 0, 0], "cubes": [ - {"origin": [-3.5, 39.75, -4.5], "size": [7, 8, 7], "uv": [0, 19]} + {"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, 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]} ] }, { - "name": "RightLeg", - "parent": "root", - "pivot": [-1, 16, -0.833], + "name": "nail4", + "parent": "right_leg", + "pivot": [-3.2, 3.2, 1.8], + "rotation": [75.65143, 69.40933, -13.46639], "cubes": [ - {"origin": [-4, 0, -3], "size": [4, 17, 4], "uv": [28, 21]}, - {"origin": [-1.5, 10.05952, -5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [8.5, 10, 4], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]} + {"origin": [-4.2, 3.2, 0.8], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-4.7, 5.2, 0.3], "size": [2, 1, 2], "uv": [0, 0]} ] }, { - "name": "LeftLeg", - "parent": "root", - "pivot": [0.167, 16, -0.833], + "name": "nail5", + "parent": "right_leg", + "pivot": [-1.8, 15, 1.5], + "rotation": [63.60782, 18.05776, -8.74472], "cubes": [ - {"origin": [0, 0, -3], "size": [4, 17, 4], "uv": [28, 0]}, - {"origin": [2.5, 8.05952, -5], "size": [0, 1, 2], "uv": [0, 0]}, - {"origin": [8.5, 12, -6], "size": [0, 1, 2], "pivot": [4.5, 17.5, -4.499], "rotation": [0, -90, 0], "uv": [0, 0]} + {"origin": [-2.8, 15, 0.5], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-3.3, 17, 0], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail6", + "parent": "right_leg", + "pivot": [-4.1, 19.5, 3.7], + "rotation": [29.03199, 61.09544, -57.62441], + "cubes": [ + {"origin": [-5.1, 19.5, 2.7], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-5.6, 21.5, 2.2], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "spring", + "parent": "body", + "pivot": [0, 35.3, 4.5], + "cubes": [ + {"origin": [1.5, 35.3, 1.8], "size": [1, 1, 5], "uv": [52, 33]}, + {"origin": [-2.4, 36.2, 1.6], "size": [1, 1, 5], "uv": [51, 44]}, + {"origin": [-2.7, 35.8, 5.7], "size": [5, 1, 1], "uv": [56, 4]}, + {"origin": [-2.7, 36.6, 1.7], "size": [5, 1, 1], "uv": [56, 2]}, + {"origin": [-2.7, 38.6, 1.7], "size": [5, 1, 1], "uv": [0, 56]}, + {"origin": [1.5, 37.3, 1.8], "size": [1, 1, 5], "uv": [50, 18]}, + {"origin": [-2.7, 37.8, 5.7], "size": [5, 1, 1], "uv": [56, 0]}, + {"origin": [-2.4, 38.2, 1.6], "size": [1, 1, 5], "uv": [50, 50]}, + {"origin": [-2.7, 40.6, 1.7], "size": [5, 1, 1], "uv": [53, 39]}, + {"origin": [1.5, 39.3, 1.8], "size": [1, 1, 5], "uv": [31, 49]}, + {"origin": [-2.7, 39.8, 5.7], "size": [5, 1, 1], "uv": [54, 12]}, + {"origin": [-2.4, 40.2, 1.6], "size": [1, 1, 5], "uv": [43, 49]}, + {"origin": [-2.7, 42.6, 1.7], "size": [5, 1, 1], "uv": [30, 6]}, + {"origin": [1.5, 41.3, 1.8], "size": [1, 1, 5], "uv": [24, 48]}, + {"origin": [-2.7, 41.8, 5.7], "size": [5, 1, 1], "uv": [53, 29]}, + {"origin": [-2.4, 42.2, 1.6], "size": [1, 1, 5], "uv": [49, 1]}, + {"origin": [-2.7, 44.6, 1.7], "size": [5, 1, 1], "uv": [22, 27]}, + {"origin": [1.5, 43.3, 1.8], "size": [1, 1, 5], "uv": [38, 6]}, + {"origin": [-2.7, 43.8, 5.7], "size": [5, 1, 1], "uv": [22, 29]}, + {"origin": [-2.4, 44.2, 1.6], "size": [1, 1, 5], "uv": [46, 27]} + ] + }, + { + "name": "head", + "parent": "spring", + "pivot": [0, 45.5, 2.2], + "cubes": [ + {"origin": [-3.2, 45.4, 1.3], "size": [6, 6, 6], "uv": [28, 27]} + ] + }, + { + "name": "nail7", + "parent": "body", + "pivot": [-0.6, 22.4, 2.2], + "rotation": [82.32005, 12.39135, -1.6575], + "cubes": [ + {"origin": [-1.6, 22.4, 1.2], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-2.1, 24.4, 0.7], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail8", + "parent": "body", + "pivot": [3.9, 31.6, 1.6], + "rotation": [97.7614, -14.8687, -2.00307], + "cubes": [ + {"origin": [2.9, 31.6, 0.6], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [2.4, 33.6, 0.1], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail10", + "parent": "body", + "pivot": [0.9, 31.6, 7.4], + "rotation": [-97.7614, 14.8687, -2.00307], + "cubes": [ + {"origin": [-0.1, 31.6, 7.4], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-0.6, 33.6, 6.9], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail11", + "parent": "body", + "pivot": [-1.5, 22.5, 6.2], + "rotation": [-97.7614, 14.8687, -2.00307], + "cubes": [ + {"origin": [-2.5, 22.5, 6.2], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-3, 24.5, 5.7], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail9", + "parent": "body", + "pivot": [-3.4, 33.5, 1.2], + "rotation": [98.44236, 27.24513, 3.88714], + "cubes": [ + {"origin": [-4.4, 33.5, 0.2], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-4.9, 35.5, -0.3], "size": [2, 1, 2], "uv": [0, 0]} + ] + }, + { + "name": "nail", + "parent": "body", + "pivot": [-4, 33.5, 6.1], + "rotation": [10, 0, -30], + "cubes": [ + {"origin": [-5, 33.5, 5.1], "size": [1, 2, 1], "uv": [0, 3]}, + {"origin": [-5.5, 35.5, 4.6], "size": [2, 1, 2], "uv": [0, 0]} ] } ] diff --git a/mods/dangerous_business/scripts/coilhead.ts b/mods/dangerous_business/scripts/coilhead.ts new file mode 100644 index 0000000..d49c3fb --- /dev/null +++ b/mods/dangerous_business/scripts/coilhead.ts @@ -0,0 +1,52 @@ +// 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"; + +// how far away a player still freezes it +const SEEN_RANGE = 48; +// how far from where a player looks it can be and still be seen, as the cosine of the angle: 0.5 is 60 degrees each +// 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; + +export function register_coilhead(ctx: ServerContext) { + const builtin = ctx.entities.goals; + const chase = builtin.melee_attack({ speed: CHASE_SPEED }); + const stroll = builtin.random_stroll({ speed: 0.6 }); + const look_around = builtin.random_look_around(); + const hurt_by = builtin.hurt_by_target(); + const nearest_player = builtin.nearest_attackable_player(); + + ctx.entities.register_behavior("dangerous_business:coilhead", (mob, goals) => { + // takes every control from the goals after it, so it doesn't walk, turn, jump or hit while seen + goals.add(0, { + flags: ["move", "look", "jump"], + can_use: () => ctx.players.all().some((player) => sees(ctx, player, mob)), + start: () => mob.stop(), + }); + goals.add(1, chase); + goals.add(2, stroll); + goals.add(3, look_around); + goals.add_target(0, hurt_by); + goals.add_target(1, nearest_player); + }); +} + +// 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; + const eye = player.eye_position; + const look = player.look_direction; + const head = mob.eye_position; + const feet = mob.position; + const middle = { x: feet.x, y: (feet.y + head.y) / 2, z: feet.z }; + return [head, middle].some((point: Position) => { + const dx = point.x - eye.x; + const dy = point.y - eye.y; + const dz = point.z - eye.z; + const distance = Math.hypot(dx, dy, dz); + if (distance > SEEN_RANGE) return false; + if (distance > 0 && (dx * look.x + dy * look.y + dz * look.z) / distance < VIEW_COS) return false; + return ctx.world.raycast(eye, point) === undefined; + }); +} diff --git a/mods/dangerous_business/scripts/main.ts b/mods/dangerous_business/scripts/main.ts index 7652910..676ca9d 100644 --- a/mods/dangerous_business/scripts/main.ts +++ b/mods/dangerous_business/scripts/main.ts @@ -2,6 +2,7 @@ import type { ClientContext, Color } from "cubi/client"; import type { ServerContext } from "cubi/server"; import { DOOR, FACILITY_DOOR } from "./shared.ts"; import { Alea } from "@paulaboks/rng"; +import { register_coilhead } from "./coilhead.ts"; import { DOORWAY, MAIN_TILE, type Marker, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.ts"; export { shared } from "./shared.ts"; @@ -13,6 +14,7 @@ export function server(ctx: ServerContext) { register_door(ctx); register_death(ctx); register_commands(ctx); + register_coilhead(ctx); } export function client(ctx: ClientContext) { diff --git a/mods/dangerous_business/textures/coilhead.png b/mods/dangerous_business/textures/coilhead.png new file mode 100644 index 0000000..495842e Binary files /dev/null and b/mods/dangerous_business/textures/coilhead.png differ diff --git a/tests/dangerous_business_test.ts b/tests/dangerous_business_test.ts index a2f6b61..8cc363e 100644 --- a/tests/dangerous_business_test.ts +++ b/tests/dangerous_business_test.ts @@ -3,10 +3,11 @@ // dangerous business's placement components: slabs and other blocks that go in the top or bottom half, blocks that // join the same block beside them, blocks that pick some of their looks when placed, and pipes that stand up or lie -// down. and the death block -import { assert, assertEquals, assertNotEquals } from "@std/assert"; +// down. the death block, and the coilhead +import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert"; import { ItemStack } from "@/common/inventory.ts"; import { get_state_value } from "@/common/utils.ts"; +import { create_entity } from "@/server/game/entity/entity_types.ts"; import { copy_dir, test_game } from "./helpers.ts"; const HALF = "dangerous_business:concrete_wall_slab"; @@ -15,6 +16,7 @@ const RANDOM_LOOK = "dangerous_business:water_puddle"; const FLOOR = "dangerous_business:concrete_floor"; const DEATH = "dangerous_business:death"; const PIPE = "dangerous_business:double_steel_pipes"; +const COILHEAD = "dangerous_business:coilhead"; // 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 // its own, which tests playing mods/ leave out @@ -123,3 +125,35 @@ Deno.test("the death block takes away an item falling into it", async () => { for (let i = 0; i < 40 && !item.removed; i++) t.game.tick(); assert(item.removed); }); + +// a coilhead 6 blocks north of alice, right where she's looking +function spawn_coilhead(t: Awaited>["t"]) { + const coilhead = create_entity(t.game.entities, COILHEAD)!; + coilhead.set_position(0.5, 100, -2.5); + t.game.entities.add_entity(coilhead); + return coilhead; +} + +Deno.test("a coilhead can't move while a player looks at it, and comes for them once they look away", async () => { + const { t } = await setup(); + const coilhead = spawn_coilhead(t); + for (let i = 0; i < 100; i++) t.game.tick(); + assertAlmostEquals(coilhead.x, 0.5, 1e-6); + assertAlmostEquals(coilhead.z, -2.5, 1e-6); + + // looking south, with it behind her + t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 }); + for (let i = 0; i < 100; i++) t.game.tick(); + assert(coilhead.z > 0, `it only got to z ${coilhead.z}`); +}); + +Deno.test("a player can't stop a coilhead through a wall", async () => { + const { t } = await setup(); + for (let x = -3; x <= 3; x++) { + for (let y = 100; y < 103; y++) t.game.set_block(x, y, 0, FLOOR); + } + const coilhead = spawn_coilhead(t); + for (let i = 0; i < 100; i++) t.game.tick(); + // up against the wall, still on its way to her + assert(coilhead.z > -1.5, `it only got to z ${coilhead.z}`); +});