1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-03 16:44:12 +02:00

only apply updates for entities from the current packet

This commit is contained in:
Robin Appelman 2017-02-13 18:55:26 +01:00
commit 7cb74d93de
8 changed files with 46 additions and 50 deletions

View file

@ -1,4 +1,4 @@
import {Entity} from "./Entity";
import {PacketEntity} from "./PacketEntity";
import {ServerClass} from "./ServerClass";
import {SendTable} from "./SendTable";
import {StringTable} from "./StringTable";
@ -22,7 +22,7 @@ export class Match {
rounds: any[];
startTick: number;
intervalPerTick: number;
entities: (Entity|null)[];
packetEntities: (PacketEntity|null)[];
stringTables: StringTable[];
serverClasses: ServerClass[];
sendTables: SendTable[];
@ -41,11 +41,11 @@ export class Match {
this.rounds = [];
this.startTick = 0;
this.intervalPerTick = 0;
this.entities = [];
this.packetEntities = [];
this.stringTables = [];
this.sendTables = [];
this.serverClasses = [];
this.entities = [];
this.packetEntities = [];
this.instanceBaselines = [[], []];
this.staticBaseLines = [];
this.eventDefinitions = {};
@ -147,7 +147,7 @@ export class Match {
return this.users[userId];
}
getUserInfoForEntity(entity: Entity): UserInfo {
getUserInfoForEntity(entity: PacketEntity): UserInfo {
for (const id of Object.keys(this.users)) {
const user = this.users[id];
if (user && user.entityId === entity.entityIndex) {

View file

@ -1,7 +1,7 @@
import {StringTable} from "./StringTable";
import {Vector} from "./Vector";
import {GameEvent} from "./GameEvent";
import {Entity} from "./Entity";
import {PacketEntity} from "./PacketEntity";
export interface StringTablePacket {
packetType: 'stringTable';
@ -46,6 +46,7 @@ export interface GameEventListPacket {
export interface PacketEntitiesPacket {
packetType: 'packetEntities';
entities: PacketEntity[];
}
export interface ParseSoundsPacket {
@ -62,7 +63,7 @@ export interface SetConVarPacket {
export interface TempEntitiesPacket {
packetType: 'tempEntities';
entities: Entity[];
entities: PacketEntity[];
}
export interface SayText2Packet {

View file

@ -2,7 +2,7 @@ import {ServerClass} from "./ServerClass";
import {SendTable} from "./SendTable";
import {SendProp} from "./SendProp";
import {SendPropDefinition} from "./SendPropDefinition";
export class Entity {
export class PacketEntity {
serverClass: ServerClass;
sendTable: SendTable;
entityIndex: number;

View file

@ -1,18 +1,16 @@
import {PacketEntitiesPacket} from "../Data/Packet";
import {Match} from "../Data/Match";
import {Entity} from "../Data/Entity";
import {PacketEntity} from "../Data/PacketEntity";
import {Vector} from "../Data/Vector";
import {Player} from "../Data/Player";
export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match) {
for (const entity of match.entities) {
if (entity) {
for (const entity of packet.entities) {
handleEntity(entity, match);
}
}
}
function handleEntity(entity: Entity, match: Match) {
function handleEntity(entity: PacketEntity, match: Match) {
switch (entity.serverClass.name) {
case 'CWorld':
match.world.boundaryMin = <Vector>entity.getProperty('DT_WORLD', 'm_WorldMins').value;

View file

@ -1,14 +1,11 @@
import {Entity} from "../Data/Entity";
import {PacketEntity} from "../Data/PacketEntity";
import {BitStream} from "bit-buffer";
import {SendProp} from "../Data/SendProp";
import {SendPropParser} from "./SendPropParser";
import {readUBitVar} from "./readBitVar";
import {SendTable} from "../Data/SendTable";
export function applyEntityUpdate(entity: Entity, stream: BitStream, sendTable?: SendTable): Entity {
if (!sendTable) {
sendTable = entity.sendTable;
}
export function applyEntityUpdate(entity: PacketEntity, sendTable: SendTable, stream: BitStream): PacketEntity {
let index = -1;
const allProps = sendTable.flattenedProps;
let lastProps:SendProp[] = [];

View file

@ -1,4 +1,4 @@
import {Entity} from '../../Data/Entity';
import {PacketEntity} from '../../Data/PacketEntity';
import {SendProp} from '../../Data/SendProp';
import {PacketEntitiesPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
@ -30,7 +30,7 @@ function readPVSType(stream: BitStream): PVS {
return pvs;
}
function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLine: number): Entity {
function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLine: number): PacketEntity {
// https://github.com/PazerOP/DemoLib/blob/5f9467650f942a4a70f9ec689eadcd3e0a051956/TF2Net/NetMessages/NetPacketEntitiesMessage.cs#L198
const serverClass = match.serverClasses[stream.readBits(match.classBits)];
const sendTable = match.getSendTable(serverClass.dataTable);
@ -39,10 +39,7 @@ function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLin
throw new Error('Unknown SendTable for serverclass');
}
// if (match.entities[entityId]) {
// console.log('overwriting entity');
// }
const entity = new Entity(serverClass, sendTable, entityId, serialNumber);
const entity = new PacketEntity(serverClass, sendTable, entityId, serialNumber);
const decodedBaseLine = match.instanceBaselines[baseLine][entityId];
if (decodedBaseLine) {
@ -56,7 +53,7 @@ function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLin
const staticBaseLine = match.staticBaseLines[serverClass.id];
if (staticBaseLine) {
staticBaseLine.index = 0;
applyEntityUpdate(entity, staticBaseLine, sendTable);
applyEntityUpdate(entity, sendTable, staticBaseLine);
if (staticBaseLine.bitsLeft > 7) {
// console.log(staticBaseLine.length, staticBaseLine.index);
// throw new Error('Unexpected data left at the end of staticBaseline, ' + staticBaseLine.bitsLeft + ' bits left');
@ -68,7 +65,7 @@ function readEnterPVS(stream: BitStream, entityId: number, match: Match, baseLin
function readLeavePVS(match, entityId, shouldDelete) {
if (shouldDelete) {
match.entities[entityId] = null;
match.packetEntities[entityId] = null;
}
}
@ -97,14 +94,15 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
}
}
const receivedEntities: PacketEntity[] = [];
for (let i = 0; i < updatedEntries; i++) {
const diff = readUBitVar(stream);
entityId += 1 + diff;
const pvs = readPVSType(stream);
if (pvs === PVS.ENTER) {
const entity = readEnterPVS(stream, entityId, match, baseLine);
applyEntityUpdate(entity, stream);
match.entities[entityId] = entity;
applyEntityUpdate(entity, entity.sendTable, stream);
match.packetEntities[entityId] = entity;
if (updatedBaseLine) {
const newBaseLine: SendProp[] = [];
@ -112,16 +110,18 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
match.instanceBaselines[baseLine][entityId] = newBaseLine;
}
entity.inPVS = true;
receivedEntities.push(entity);
} else if (pvs === PVS.PRESERVE) {
const entity = match.entities[entityId];
const entity = match.packetEntities[entityId];
if (entity) {
applyEntityUpdate(entity, stream);
applyEntityUpdate(entity, entity.sendTable, stream);
receivedEntities.push(entity);
} else {
console.log(entityId, match.entities.length);
console.log(entityId, match.packetEntities.length);
throw new Error("unknown entity");
}
} else {
const entity = match.entities[entityId];
const entity = match.packetEntities[entityId];
if (entity) {
entity.inPVS = false;
}
@ -132,12 +132,13 @@ export function PacketEntities(stream: BitStream, match: Match): PacketEntitiesP
if (isDelta) {
while (stream.readBoolean()) {
const ent = stream.readBits(11);
match.entities[ent] = null;
match.packetEntities[ent] = null;
}
}
stream.index = end;
return {
packetType: 'packetEntities'
packetType: 'packetEntities',
entities: receivedEntities
};
}

View file

@ -1,8 +1,6 @@
import {Packet} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {GameEventDefinitionMap} from "../../Data/GameEvent";
import {Match} from "../../Data/Match";
import {Entity} from "../../Data/Entity";
export type Parser = (stream: BitStream, match?: Match) => Packet;
export type PacketParserMap = {[id: number]: Parser};

View file

@ -1,7 +1,7 @@
import {TempEntitiesPacket} from "../../Data/Packet";
import {BitStream} from 'bit-buffer';
import {Match} from "../../Data/Match";
import {Entity} from "../../Data/Entity";
import {PacketEntity} from "../../Data/PacketEntity";
import {applyEntityUpdate} from "../EntityDecoder";
export function TempEntities(stream: BitStream, match: Match): TempEntitiesPacket { // 10: classInfo
@ -9,22 +9,23 @@ export function TempEntities(stream: BitStream, match: Match): TempEntitiesPacke
const length = readVarInt(stream);
const end = stream.index + length;
let entity: Entity|null = null;
let entities: Entity[] = [];
let entity: PacketEntity|null = null;
let entities: PacketEntity[] = [];
for (let i = 0; i < entityCount; i++) {
const delay = (stream.readBoolean()) ? stream.readUint8() / 100 : 0; //unused it seems
if (stream.readBoolean()) {
const classId = stream.readBits(match.classBits);
const serverClass = match.serverClasses[classId - 1]; // no clue why the -1 but it works
// maybe because world (id=0) can never be tem
const serverClass = match.serverClasses[classId - 1];
// no clue why the -1 but it works
// maybe because world (id=0) can never be temp
// but it's not like the -1 saves any space
const sendTable = match.getSendTable(serverClass.dataTable);
entity = new Entity(serverClass, sendTable, 0, 0);
applyEntityUpdate(entity, stream);
entity = new PacketEntity(serverClass, sendTable, 0, 0);
applyEntityUpdate(entity, sendTable, stream);
entities.push(entity);
} else {
if (entity) {
applyEntityUpdate(entity, stream);
applyEntityUpdate(entity, entity.sendTable, stream);
} else {
throw new Error("no entity set to update");
}