2023-11-09 21:59:45 +00:00
|
|
|
import { Endian, IReader, IWriter, createWriter } from "bufferstuff";
|
2023-11-09 16:30:40 +00:00
|
|
|
import AABB from "../AABB";
|
2023-08-20 02:10:49 +01:00
|
|
|
import { Chunk } from "../Chunk";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { MetadataEntry, MetadataWriter } from "../MetadataWriter";
|
2023-11-02 08:31:43 +00:00
|
|
|
import { Rotation } from "../Rotation";
|
2023-11-08 15:45:25 +00:00
|
|
|
import { Vec2 } from "../Vec2";
|
|
|
|
import Vec3 from "../Vec3";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { World } from "../World";
|
2023-11-09 16:30:40 +00:00
|
|
|
import { Block } from "../blocks/Block";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { MetadataFieldType } from "../enums/MetadataFieldType";
|
2023-11-02 08:31:43 +00:00
|
|
|
import { PacketEntityLook } from "../packets/EntityLook";
|
|
|
|
import { PacketEntityLookRelativeMove } from "../packets/EntityLookRelativeMove";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { PacketEntityMetadata } from "../packets/EntityMetadata";
|
2023-11-02 08:31:43 +00:00
|
|
|
import { PacketEntityRelativeMove } from "../packets/EntityRelativeMove";
|
|
|
|
import { PacketEntityTeleport } from "../packets/EntityTeleport";
|
2023-11-05 10:58:47 +00:00
|
|
|
import { PacketEntityVelocity } from "../packets/EntityVelocity";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { IEntity } from "./IEntity";
|
2023-12-18 01:23:52 +00:00
|
|
|
import { Player } from "./Player";
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
export class Entity implements IEntity {
|
|
|
|
public static nextEntityId:number = 0;
|
|
|
|
|
|
|
|
public entityId:number;
|
|
|
|
|
2023-11-08 15:45:25 +00:00
|
|
|
public entitySize:Vec2;
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public world:World;
|
2023-11-02 08:31:43 +00:00
|
|
|
|
|
|
|
public position:Vec3;
|
|
|
|
public lastPosition:Vec3;
|
|
|
|
public absPosition:Vec3;
|
|
|
|
public lastAbsPosition:Vec3;
|
2023-11-05 00:55:23 +00:00
|
|
|
public motion:Vec3;
|
2023-11-02 08:31:43 +00:00
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
private positionBeforeMove:Vec3;
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
public rotation:Rotation;
|
|
|
|
public lastRotation:Rotation;
|
|
|
|
public absRotation:Rotation;
|
|
|
|
public lastAbsRotation:Rotation;
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
public health:number;
|
2023-11-02 08:31:43 +00:00
|
|
|
public wasHurt:boolean;
|
|
|
|
public isDead:boolean;
|
2023-04-13 23:52:13 +01:00
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public fire:number;
|
2023-11-05 00:55:23 +00:00
|
|
|
public fallDistance:number;
|
|
|
|
|
|
|
|
public onGround:boolean;
|
2023-04-10 21:52:30 +01:00
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
public chunk:Chunk;
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public crouching:boolean;
|
|
|
|
private lastCrouchState:boolean;
|
|
|
|
private lastFireState:boolean;
|
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
public entityAABB:AABB;
|
|
|
|
|
2023-11-09 21:59:45 +00:00
|
|
|
public readonly isPlayer:boolean;
|
2023-09-04 23:42:38 +01:00
|
|
|
private queuedChunkUpdate:boolean;
|
|
|
|
|
2023-12-06 00:04:57 +00:00
|
|
|
public markedForDisposal:boolean = false;
|
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
public constructor(world:World, isPlayer:boolean = false) {
|
2023-04-08 20:52:47 +01:00
|
|
|
this.entityId = Entity.nextEntityId++;
|
2023-11-08 15:45:25 +00:00
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
this.isPlayer = isPlayer;
|
|
|
|
|
2023-11-08 15:45:25 +00:00
|
|
|
this.entitySize = new Vec2(0.6, 1.8);
|
2023-11-09 16:30:40 +00:00
|
|
|
this.entityAABB = new AABB(-this.entitySize.x / 2, 0, -this.entitySize.x / 2, this.entitySize.x / 2, this.entitySize.y, this.entitySize.x / 2);
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
this.fire = this.fallDistance = 0;
|
|
|
|
this.onGround = false;
|
2023-04-10 21:52:30 +01:00
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
this.world = world;
|
2023-11-02 08:31:43 +00:00
|
|
|
|
|
|
|
this.position = new Vec3();
|
|
|
|
this.lastPosition = new Vec3();
|
|
|
|
this.absPosition = new Vec3();
|
|
|
|
this.lastAbsPosition = new Vec3();
|
2023-11-05 00:55:23 +00:00
|
|
|
this.motion = new Vec3();
|
2023-11-02 08:31:43 +00:00
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
this.positionBeforeMove = new Vec3();
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
this.rotation = new Rotation();
|
|
|
|
this.lastRotation = new Rotation();
|
|
|
|
this.absRotation = new Rotation();
|
|
|
|
this.lastAbsRotation = new Rotation();
|
|
|
|
|
2023-09-04 23:42:38 +01:00
|
|
|
this.crouching = this.lastCrouchState = this.lastFireState = this.queuedChunkUpdate = false;
|
2023-04-13 23:52:13 +01:00
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
this.chunk = world.getChunk(this.position.x >> 4, this.position.z >> 4);
|
2023-08-20 02:10:49 +01:00
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
this.health = 20;
|
2023-11-02 08:31:43 +00:00
|
|
|
this.wasHurt = false;
|
|
|
|
this.isDead = false;
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:04:11 +00:00
|
|
|
public fromSave(reader:IReader) {
|
|
|
|
this.position.set(reader.readDouble(), reader.readDouble(), reader.readDouble());
|
|
|
|
this.motion.set(reader.readFloat(), reader.readFloat(), reader.readFloat());
|
|
|
|
this.rotation.set(reader.readFloat(), reader.readFloat());
|
|
|
|
this.fire = reader.readShort();
|
|
|
|
this.fallDistance = reader.readFloat();
|
|
|
|
this.health = reader.readByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
public toSave(writer:IWriter) {
|
|
|
|
writer.writeDouble(this.position.x).writeDouble(this.position.y).writeDouble(this.position.z) // Position
|
2023-11-09 21:59:45 +00:00
|
|
|
.writeFloat(this.motion.x).writeFloat(this.motion.y).writeFloat(this.motion.z) // Motion
|
2023-11-09 17:04:11 +00:00
|
|
|
.writeFloat(this.rotation.x).writeFloat(this.rotation.y) // Rotation
|
|
|
|
.writeShort(this.fire)
|
|
|
|
.writeFloat(this.fallDistance)
|
|
|
|
.writeByte(this.health);
|
|
|
|
}
|
|
|
|
|
2023-12-18 01:23:52 +00:00
|
|
|
collidesWithPlayer(aabb:AABB) {
|
|
|
|
let collidedWith:Player | undefined;
|
|
|
|
this.world.players.forEach(player => {
|
|
|
|
if (this.entityAABB.intersects(player.entityAABB)) {
|
|
|
|
collidedWith = player;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return collidedWith;
|
|
|
|
}
|
|
|
|
|
2023-08-20 01:17:27 +01:00
|
|
|
sendToNearby(buffer:Buffer) {
|
|
|
|
this.world.sendToNearbyClients(this, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendToAllNearby(buffer:Buffer) {
|
2023-11-05 00:55:23 +00:00
|
|
|
this.world.sendToNearbyAllNearbyClients(this, buffer);
|
2023-08-20 01:17:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
updateMetadata() {
|
|
|
|
const crouchStateChanged = this.crouching !== this.lastCrouchState;
|
|
|
|
const fireStateChanged = this.fire > 0 !== this.lastFireState;
|
|
|
|
if (crouchStateChanged || fireStateChanged) {
|
|
|
|
const metadata = new MetadataWriter();
|
|
|
|
// Flags:
|
|
|
|
// 1 = On Fire
|
|
|
|
// 2 = Player crouched
|
|
|
|
// 4 = Player on mount?
|
2023-11-02 08:31:43 +00:00
|
|
|
metadata.addMetadataEntry(0, new MetadataEntry(MetadataFieldType.Byte, Number(this.fire > 0) + Number(this.crouching) * 2));
|
2023-04-10 21:52:30 +01:00
|
|
|
|
2023-08-20 01:17:27 +01:00
|
|
|
this.sendToNearby(new PacketEntityMetadata(this.entityId, metadata.writeBuffer()).writeData());
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
this.lastCrouchState = this.crouching;
|
|
|
|
this.lastFireState = this.fire > 0;
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
distanceTo(entity:IEntity) {
|
2023-11-02 08:31:43 +00:00
|
|
|
const dX = entity.position.x - this.position.x,
|
|
|
|
dY = entity.position.y - this.position.y,
|
|
|
|
dZ = entity.position.z - this.position.z;
|
2023-04-10 14:42:14 +01:00
|
|
|
|
|
|
|
return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2) + Math.pow(dZ, 2));
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
damageFrom(damage:number, entity?:IEntity) {
|
2023-08-20 01:17:27 +01:00
|
|
|
if (this.health <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
if (entity === undefined) {
|
|
|
|
this.health -= damage;
|
|
|
|
}
|
2023-11-02 08:31:43 +00:00
|
|
|
|
|
|
|
this.wasHurt = true;
|
2023-04-13 23:52:13 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
updateEntityChunk() {
|
2023-11-02 08:31:43 +00:00
|
|
|
const bitX = this.position.x >> 4;
|
|
|
|
const bitZ = this.position.z >> 4;
|
|
|
|
if (bitX != this.lastPosition.x >> 4 || bitZ != this.lastPosition.z >> 4 || this.queuedChunkUpdate) {
|
2023-09-04 23:42:38 +01:00
|
|
|
if (this.world.chunkExists(bitX, bitZ)) {
|
|
|
|
this.chunk = this.world.getChunk(bitX, bitZ);
|
|
|
|
this.queuedChunkUpdate = false;
|
|
|
|
} else {
|
|
|
|
this.queuedChunkUpdate = true;
|
|
|
|
}
|
2023-08-20 02:10:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
private constrainRot(rot:number) {
|
|
|
|
return Math.min(Math.max(rot, -128), 127);
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendPositionUpdate() {
|
|
|
|
this.absPosition.set(Math.floor(this.position.x * 32), Math.floor(this.position.y * 32), Math.floor(this.position.z * 32));
|
|
|
|
|
2023-11-05 10:58:01 +00:00
|
|
|
// This code *does* work, and it works well. But this is absolutely TERRIBLE!
|
|
|
|
// There is definitely a better way to do this.
|
2023-11-02 08:31:43 +00:00
|
|
|
this.absRotation.set(
|
|
|
|
this.constrainRot(Math.floor(((this.rotation.yaw - 180 >= 0 ? this.rotation.yaw - 180 : (this.rotation.yaw - 180) % 360 + 360) % 360 / 360) * 256) - 128), // Yaw
|
|
|
|
this.constrainRot(Math.floor((this.rotation.pitch % 360 * 256) / 360)) // Pitch
|
|
|
|
);
|
|
|
|
const diffX = this.absPosition.x - this.lastAbsPosition.x;
|
|
|
|
const diffY = this.absPosition.y - this.lastAbsPosition.y;
|
|
|
|
const diffZ = this.absPosition.z - this.lastAbsPosition.z;
|
|
|
|
const diffYaw = this.absRotation.yaw - this.lastAbsRotation.yaw;
|
|
|
|
const diffPitch = this.absRotation.pitch - this.lastAbsRotation.pitch;
|
|
|
|
|
|
|
|
const doRelativeMove = Math.abs(diffX) >= 4 || Math.abs(diffY) >= 4 || Math.abs(diffZ) >= 4;
|
|
|
|
const doLook = Math.abs(diffYaw) >= 4 || Math.abs(diffPitch) >= 4;
|
|
|
|
if (Math.abs(diffX) > 128 || Math.abs(diffY) > 128 || Math.abs(diffZ) > 128) {
|
2023-11-05 10:58:01 +00:00
|
|
|
this.sendToNearby(new PacketEntityTeleport(this.entityId, this.absPosition.x, this.absPosition.y, this.absPosition.z, this.absRotation.yaw, this.absRotation.pitch).writeData());
|
2023-11-02 08:31:43 +00:00
|
|
|
} else if (doRelativeMove && doLook) {
|
2023-11-05 10:58:01 +00:00
|
|
|
this.sendToNearby(new PacketEntityLookRelativeMove(this.entityId, diffX, diffY, diffZ, this.absRotation.yaw, this.absRotation.pitch).writeData());
|
2023-11-02 08:31:43 +00:00
|
|
|
} else if (doRelativeMove) {
|
2023-11-05 10:58:01 +00:00
|
|
|
this.sendToNearby(new PacketEntityRelativeMove(this.entityId, diffX, diffY, diffZ).writeData());
|
2023-11-02 08:31:43 +00:00
|
|
|
} else if (doLook) {
|
2023-11-05 10:58:01 +00:00
|
|
|
this.sendToNearby(new PacketEntityLook(this.entityId, this.absRotation.yaw, this.absRotation.pitch).writeData());
|
2023-11-02 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 10:58:47 +00:00
|
|
|
if (!this.motion.isZero) {
|
|
|
|
this.sendToNearby(new PacketEntityVelocity(this.entityId, this.motion.x, this.motion.y, this.motion.z).writeData());
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
if (doRelativeMove) {
|
|
|
|
this.lastAbsPosition.set(this.absPosition);
|
|
|
|
}
|
|
|
|
if (doLook) {
|
|
|
|
this.lastAbsRotation.set(this.absRotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
fall(distance:number) {
|
|
|
|
// TODO: Entity falling mount transfer
|
|
|
|
}
|
|
|
|
|
2023-12-06 00:04:57 +00:00
|
|
|
kill() {
|
|
|
|
this.health = 0;
|
|
|
|
this.markedForDisposal = true;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
updateFalling(distance:number) {
|
|
|
|
if (this.onGround) {
|
2023-11-05 13:30:17 +00:00
|
|
|
if (this.fallDistance > 0) {
|
2023-11-05 00:55:23 +00:00
|
|
|
this.fall(this.fallDistance);
|
|
|
|
this.fallDistance = 0;
|
|
|
|
}
|
|
|
|
} else if (distance < 0) {
|
|
|
|
this.fallDistance -= distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
moveEntity(motionX:number, motionY:number, motionZ:number) {
|
|
|
|
this.positionBeforeMove.set(this.position);
|
|
|
|
const blockId = this.chunk.getBlockId(Math.floor(this.positionBeforeMove.x) & 0xf, Math.floor(this.positionBeforeMove.y), Math.floor(this.positionBeforeMove.z) & 0xf);
|
|
|
|
const blockUnderEntity = blockId > 0 ? Block.blocks[blockId] : null;
|
|
|
|
|
|
|
|
this.position.add(motionX, motionY, motionZ);
|
|
|
|
|
|
|
|
this.entityAABB.move(this.position);
|
|
|
|
if (blockUnderEntity !== null) {
|
|
|
|
const blockBoundingBox = blockUnderEntity.getBoundingBox(Math.floor(this.positionBeforeMove.x), Math.floor(this.positionBeforeMove.y), Math.floor(this.positionBeforeMove.z));
|
2023-11-09 21:59:45 +00:00
|
|
|
// TODO: Handle X and Z collisions.
|
2023-11-09 16:30:40 +00:00
|
|
|
if (this.entityAABB.intersects(blockBoundingBox)) {
|
2023-11-09 21:59:45 +00:00
|
|
|
const inersectionY = this.entityAABB.intersectionY(blockBoundingBox);
|
2023-12-06 00:04:57 +00:00
|
|
|
this.position.add(0, inersectionY, 0);
|
2023-11-09 16:30:40 +00:00
|
|
|
this.motion.y = 0;
|
|
|
|
this.onGround = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
onTick() {
|
2023-04-10 21:52:30 +01:00
|
|
|
this.updateMetadata();
|
2023-08-20 02:10:49 +01:00
|
|
|
this.updateEntityChunk();
|
2023-11-05 00:55:23 +00:00
|
|
|
this.updateFalling(this.motion.y);
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
if (this.fire > 0) {
|
|
|
|
if (this.fire % 20 === 0) {
|
2023-04-13 23:52:13 +01:00
|
|
|
this.damageFrom(1);
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.fire--;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
if (!this.isDead && this.health <= 0) {
|
|
|
|
this.isDead = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.wasHurt) {
|
|
|
|
this.wasHurt = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendPositionUpdate();
|
|
|
|
|
|
|
|
this.lastPosition.set(this.position);
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|