Make server control knockback

This commit is contained in:
2026-09-30 10:54:09 -03:00
parent 4fbcca6851
commit dc70ada7c4
8 changed files with 99 additions and 53 deletions
+3
View File
@@ -64,6 +64,8 @@ export class LocalPlayer extends Player {
// blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag
jump_force = 9.23;
// the last teleport from the server, sent back with every move so the server knows which moves came after it
teleport_id = 0;
#ticks_since_sent = 0;
// counts down every tick, letting go of jump clears it
#jump_delay = 0;
@@ -207,6 +209,7 @@ export class LocalPlayer extends Player {
z: this.z,
yaw: this.yaw,
pitch: this.pitch,
teleport: this.teleport_id,
});
}
}
+14 -7
View File
@@ -221,14 +221,21 @@ export class ClientPacketListener {
}
break;
}
case "teleport":
client.player.set_position(message.x, message.y, message.z);
break;
case "set_motion":
client.player.vx = message.vx;
client.player.vy = message.vy;
client.player.vz = message.vz;
case "teleport": {
const player = client.player;
player.teleport_id = message.id;
if (message.motion) {
// moved by the server, like knockback: drawn easing there, and keeps going until the next one
player.x = message.x;
player.y = message.y;
player.z = message.z;
[player.vx, player.vy, player.vz] = message.motion;
} else {
player.set_position(message.x, message.y, message.z);
player.vx = player.vy = player.vz = 0;
}
break;
}
case "close_screen":
// closed by the server (the block broke), it already put everything back
client.screens = client.screens.filter((s) => !(s instanceof GuiContainer));
+5 -4
View File
@@ -83,7 +83,8 @@ export type ClientMessage =
| { type: "view_distance"; distance: number }
// answers chunk_batch_finished once the batch is loaded, with how many chunks a tick the client can take
| { type: "chunk_batch_received"; chunks_per_tick: number }
| { type: "move"; x: number; y: number; z: number; yaw: number; pitch: number }
// teleport is the id of the last teleport the client heard of. moves from before the latest one are ignored
| { type: "move"; x: number; y: number; z: number; yaw: number; pitch: number; teleport?: number }
| { type: "break_block"; x: number; y: number; z: number }
// the player started hitting a block, for on_click
| { type: "hit_block"; x: number; y: number; z: number }
@@ -173,9 +174,9 @@ export type ServerMessage =
// it changes. null when it shows nothing any more
| { type: "set_block_item"; x: number; y: number; z: number; item: ItemData | null }
| { type: "chat"; from?: string; text: string }
| { type: "teleport"; x: number; y: number; z: number }
// the player was knocked back: their speed is this now, in blocks per second
| { type: "set_motion"; vx: number; vy: number; vz: number }
// the player is here now. with motion, they're moving that fast (blocks per second) too: the server is moving them,
// like when knocked back, and the client keeps going that way until the next one. id counts up, see move
| { type: "teleport"; id: number; x: number; y: number; z: number; motion?: [number, number, number] }
| { type: "container"; container: ContainerKey; items: (ItemData | null)[] }
| { type: "cursor"; item: ItemData | null }
| { type: "open_screen"; layout: ScreenLayout; properties: Record<string, number> }
+3
View File
@@ -58,6 +58,9 @@ Players follow Minecraft's rules, with 20 health (10 hearts) and 20 hunger:
Players are living entities, like mobs, so damage works the same for both. After being hurt they can't be hurt again for
half a second, unless the new hit is harder, and then only by the difference.
A player's client moves them, except while they're knocked back: then the server moves them until they land, so it works
even when their game isn't running, like in a background tab.
`damage(amount, cause)` hurts them like anything else would. `cause` shows in the `player_damage` and `player_death`
[events](../concepts/events.md) and in the death message. Before events can change the amount or cancel it.
+24 -11
View File
@@ -466,7 +466,16 @@ export class GameServer {
teleport(player: ServerPlayer, x: number, y: number, z: number) {
Object.assign(player, { x, y, z });
player.fall_distance = 0;
this.#send(player, { type: "teleport", x, y, z });
player.stop_knockback();
this.#place(player);
}
// tells their client where they are now (and how fast they're going, while the server moves them), and everyone
// else where they are
#place(player: ServerPlayer, motion?: [number, number, number]) {
const { x, y, z } = player;
player.teleport_id += 1;
this.#send(player, { type: "teleport", id: player.teleport_id, x, y, z, ...(motion && { motion }) });
this.#update_view(player);
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw: player.yaw, pitch: player.pitch }, player);
}
@@ -709,6 +718,11 @@ export class GameServer {
if (![x, y, z, yaw, pitch].every(is_number)) {
return;
}
// the server is moving them, or it's from before the last time it did
const stale = is_number(message.teleport) && message.teleport < player.teleport_id;
if (player.knockback_ticks !== undefined || stale) {
return;
}
this.#on_move(player, x, y, z);
Object.assign(player, { x, y, z, yaw, pitch });
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw, pitch }, player);
@@ -1381,8 +1395,14 @@ export class GameServer {
#tick_vitals() {
for (const player of this.#players.values()) {
// burning, effects, and falling out of the world
// burning, effects, falling out of the world, and being knocked back
const knocked_back = player.knockback_ticks !== undefined;
player.tick();
if (knocked_back && player.alive) {
// the last one hands what's left of the slide to their client
this.#place(player, [player.vx, player.vy, player.vz]);
if (player.knockback_ticks === undefined) player.stop_knockback();
}
player.vitals.tick(
player.health,
(amount) => this.heal_player(player, amount),
@@ -1399,6 +1419,8 @@ export class GameServer {
const ticks = Math.max(1, this.#tick - player.last_move_tick);
player.last_move_tick = this.#tick;
const horizontal = Math.hypot(dx, dz);
// knockback only lifts them off the ground
player.on_ground = this.#on_ground(x, y, z);
// teleports and the first move after joining aren't walking
if (horizontal > TELEPORT_DISTANCE || Math.abs(dy) > TELEPORT_DISTANCE) {
player.fall_distance = 0;
@@ -1421,8 +1443,6 @@ export class GameServer {
if (dy < 0 && !in_liquid) {
player.fall_distance -= dy;
}
// knockback only lifts them off the ground
player.on_ground = this.#on_ground(x, y, z);
if (player.on_ground) {
const damage = Math.ceil(player.fall_distance - SAFE_FALL);
player.fall_distance = 0;
@@ -1530,13 +1550,6 @@ export class GameServer {
}
};
// knockback, their client moves them
const motion = player.take_motion();
if (motion) {
const [vx, vy, vz] = motion;
this.#send(player, { type: "set_motion", vx, vy, vz });
}
// with lore, so it's resent when only the lore changed
const inventory = player.inventory.to_synced_data();
sync("inventory", inventory, () => ({ type: "container", container: "inventory", items: inventory }));
+31 -11
View File
@@ -39,9 +39,13 @@ export interface SavedPlayer {
slim?: boolean;
}
// the most ticks the server moves a knocked back player, in case they never land, like in water
const MAX_KNOCKBACK_TICKS = 20;
// a player is a living entity, like minecraft's Player: they have health, can be hurt and pushed around by mobs and
// other players, and have effects. their client moves them though, so they don't walk, fall or think here. they're
// kept apart from the entity manager's entities, clients hear about players through their own messages
// other players, and have effects. their client moves them, except while they're knocked back: then the server does
// until they land, so it works even when their client isn't running, like in a background tab. they're kept apart
// from the entity manager's entities, clients hear about players through their own messages
export class ServerPlayer extends LivingEntity {
readonly type = "bworld:player";
readonly conn: number;
@@ -55,6 +59,11 @@ export class ServerPlayer extends LivingEntity {
inventory = new Container(INVENTORY_SIZE);
// hunger and saturation, health is the entity's
vitals = new Vitals();
// counts every position the server gave them. their client says which one it's heard of with each move, and moves
// from before the latest are ignored, they'd put them back where they were
teleport_id = 0;
// ticks the server has been moving them for, undefined when their client does
knockback_ticks: number | undefined;
// when the last move came, to tell walking from sprinting
last_move_tick = 0;
crafting = new Container(CRAFTING_SIZE);
@@ -100,12 +109,31 @@ export class ServerPlayer extends LivingEntity {
return true;
}
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down. while
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
override tick() {
this.base_tick();
if (!this.dead) {
this.tick_effects();
}
if (this.knockback_ticks === undefined) return;
this.forward = this.strafe = this.speed = 0;
this.travel();
this.knockback_ticks += 1;
if ((this.on_ground && this.vy <= 0) || this.knockback_ticks >= MAX_KNOCKBACK_TICKS) {
this.knockback_ticks = undefined;
}
}
override knockback(strength: number, dx: number, dz: number) {
super.knockback(strength, dx, dz);
if (this.vx || this.vy || this.vz) this.knockback_ticks ??= 0;
}
// back to their client, where they are now
stop_knockback() {
this.knockback_ticks = undefined;
this.vx = this.vy = this.vz = 0;
}
// everything that hurts a player goes through the game server first, which lets mods change or cancel it
@@ -127,14 +155,6 @@ export class ServerPlayer extends LivingEntity {
this.level.on_death(this, source);
}
// knockback is how fast their client should move them, sent with the next sync and cleared
take_motion(): [number, number, number] | undefined {
if (!this.vx && !this.vy && !this.vz) return undefined;
const motion: [number, number, number] = [this.vx, this.vy, this.vz];
this.vx = this.vy = this.vz = 0;
return motion;
}
get held_item(): ItemStack | undefined {
return this.inventory.get_item(this.selected_slot);
}
-17
View File
@@ -1,17 +0,0 @@
import { test_game } from "../helpers.ts";
Deno.test("kb", async () => {
const t = await test_game("mods");
t.join(1, "alice");
t.join(2, "bob");
for (let x = -8; x <= 8; x++) for (let z = -8; z <= 8; z++) t.game.set_block(x, 99, z, "bworld:stone");
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
t.send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.tick();
t.take(2);
const bob = t.game.players()[1];
console.log("on_ground", bob.on_ground);
t.send(1, { type: "attack_entity", id: bob.id });
console.log("v after hurt", bob.vx, bob.vy, bob.vz, "health", bob.health);
t.game.tick();
console.log(t.take(2).filter((m) => m.type === "set_motion"));
});
+19 -3
View File
@@ -172,11 +172,27 @@ Deno.test("players hit each other, knocking them back and flashing red for every
send(1, { type: "attack_entity", id: bob.id });
assertEquals(bob.health, 19);
ticks(1);
const motion = take(2).find((m) => m.type === "set_motion");
assert(motion && motion.vz > 0, "bob's client is told he's knocked back, away from alice");
assert(take(1).some((m) => m.type === "entity_event" && m.id === bob.id && m.event === "hurt"));
// bob's client sends nothing, like in a background tab, and the server knocks him back anyway
ticks(1);
assert(bob.z > 2.5 && bob.y > 100, "up and away from alice");
const teleport = take(2).find((m) => m.type === "teleport");
assert(teleport.motion[2] > 0, "his client keeps him going between updates");
assert(take(1).some((m) => m.type === "player_move" && m.id === bob.id), "alice sees him move");
const knocked_to = bob.z;
send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0, teleport: teleport.id });
assertEquals(bob.z, knocked_to, "his own moves wait until he lands");
ticks(20);
assertEquals(bob.knockback_ticks, undefined, "landed");
assert(bob.on_ground && bob.z > knocked_to);
const last = take(2).filter((m) => m.type === "teleport").at(-1);
send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0, teleport: last.id - 1 });
assert(bob.z > knocked_to, "a move sent before hearing of the last one is ignored");
send(2, { type: "move", x: 0.5, y: 100, z: 5, yaw: 0, pitch: 0, teleport: last.id });
assertEquals(bob.z, 5, "his client moves him again");
send(1, { type: "attack_entity", id: alice.id });
assertEquals(alice.health, 20, "not yourself");