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

use map for entity classes

This commit is contained in:
Robin Appelman 2017-09-02 15:42:38 +02:00
commit d5173b4548
4 changed files with 32 additions and 34 deletions

View file

@ -31,7 +31,7 @@ export class Match {
public eventDefinitions: Map<number, GameEventDefinition>;
public world: World;
public playerEntityMap: Map<EntityId, Player>;
public entityClasses: {[entityId: string]: ServerClass};
public entityClasses: Map<EntityId, ServerClass> = new Map();
public sendTableMap: {[name: string]: SendTable};
public baseLineCache: {[serverClass: string]: PacketEntity};
public weaponMap: {[entityId: string]: Weapon};
@ -63,7 +63,6 @@ export class Match {
boundaryMin: {x: 0, y: 0, z: 0},
boundaryMax: {x: 0, y: 0, z: 0},
};
this.entityClasses = {};
this.sendTableMap = {};
this.baseLineCache = {};
this.weaponMap = {};

View file

@ -13,7 +13,7 @@ export type EntityId = number;
export class PacketEntity {
public serverClass: ServerClass;
public entityIndex: number;
public entityIndex: EntityId;
public props: SendProp[];
public inPVS: boolean;
public pvs: PVS;

View file

@ -10,7 +10,7 @@ import {TeamNumber} from '../Data/Team';
export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match) {
for (const removedEntityId of packet.removedEntities) {
delete match.entityClasses[removedEntityId];
match.entityClasses.delete(removedEntityId);
}
for (const entity of packet.entities) {
@ -21,10 +21,10 @@ export function handlePacketEntities(packet: PacketEntitiesPacket, match: Match)
function saveEntity(packetEntity: PacketEntity, match: Match) {
if (packetEntity.pvs === PVS.DELETE) {
delete match.entityClasses[packetEntity.entityIndex];
match.entityClasses.delete(packetEntity.entityIndex);
}
match.entityClasses[packetEntity.entityIndex] = packetEntity.serverClass;
match.entityClasses.set(packetEntity.entityIndex, packetEntity.serverClass);
}
function handleEntity(entity: PacketEntity, match: Match) {

View file

@ -1,7 +1,7 @@
import {BitStream} from 'bit-buffer';
import {Match} from '../../Data/Match';
import {PacketEntitiesPacket} from '../../Data/Packet';
import {PacketEntity, PVS} from '../../Data/PacketEntity';
import {EntityId, PacketEntity, PVS} from '../../Data/PacketEntity';
import {SendProp} from '../../Data/SendProp';
import {applyEntityUpdate} from '../EntityDecoder';
import {readUBitVar} from '../readBitVar';
@ -19,18 +19,18 @@ function readPVSType(stream: BitStream): PVS {
return pvsMap[pvs];
}
function readEnterPVS(stream: BitStream, entityId: number, match: Match): PacketEntity {
function readEnterPVS(stream: BitStream, entityId: EntityId, match: Match): PacketEntity {
// https://github.com/PazerOP/DemoLib/blob/5f9467650f942a4a70f9ec689eadcd3e0a051956/TF2Net/NetMessages/NetPacketEntitiesMessage.cs#L198
const serverClass = match.serverClasses[stream.readBits(match.classBits)];
const serial = stream.readBits(10); // unused serial number
const serial = stream.readBits(10); // unused serial number
if (match.baseLineCache[serverClass.id]) {
const result = match.baseLineCache[serverClass.id].clone();
result.entityIndex = entityId;
const result = match.baseLineCache[serverClass.id].clone();
result.entityIndex = entityId;
result.serialNumber = serial;
return result;
} else {
const entity = new PacketEntity(serverClass, entityId, PVS.ENTER);
const entity = new PacketEntity(serverClass, entityId, PVS.ENTER);
const sendTable = match.getSendTable(serverClass.dataTable);
if (!sendTable) {
throw new Error('Unknown SendTable for serverclass');
@ -41,8 +41,8 @@ function readEnterPVS(stream: BitStream, entityId: number, match: Match): Packet
applyEntityUpdate(entity, sendTable, staticBaseLine);
match.baseLineCache[serverClass.id] = entity.clone();
// 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');
// console.log(staticBaseLine.length, staticBaseLine.index);
// throw new Error('Unexpected data left at the end of staticBaseline, ' + staticBaseLine.bitsLeft + ' bits left');
// }
}
entity.serialNumber = serial;
@ -50,11 +50,12 @@ function readEnterPVS(stream: BitStream, entityId: number, match: Match): Packet
}
}
function getPacketEntityForExisting(entityId: number, match: Match, pvs: PVS) {
if (!match.entityClasses[entityId]) {
function getPacketEntityForExisting(entityId: EntityId, match: Match, pvs: PVS) {
const serverClass = match.entityClasses.get(entityId);
if (!serverClass) {
throw new Error(`"unknown entity ${entityId} for ${PVS[pvs]}(${pvs})`);
}
const serverClass = match.entityClasses[entityId];
return new PacketEntity(serverClass, entityId, pvs);
}
@ -63,24 +64,24 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
// https://github.com/StatsHelix/demoinfo/blob/3d28ea917c3d44d987b98bb8f976f1a3fcc19821/DemoInfo/DP/Handler/PacketEntitesHandler.cs
// https://github.com/StatsHelix/demoinfo/blob/3d28ea917c3d44d987b98bb8f976f1a3fcc19821/DemoInfo/DP/Entity.cs
// https://github.com/PazerOP/DemoLib/blob/5f9467650f942a4a70f9ec689eadcd3e0a051956/TF2Net/NetMessages/NetPacketEntitiesMessage.cs
const maxEntries = stream.readBits(11);
const isDelta = !!stream.readBits(1);
const delta = (isDelta) ? stream.readInt32() : 0;
const baseLine = stream.readBits(1);
const updatedEntries = stream.readBits(11);
const length = stream.readBits(20);
const maxEntries = stream.readBits(11);
const isDelta = stream.readBoolean();
const delta = (isDelta) ? stream.readInt32() : 0;
const baseLine = stream.readBits(1);
const updatedEntries = stream.readBits(11);
const length = stream.readBits(20);
const updatedBaseLine = stream.readBoolean();
const end = stream.index + length;
let entityId = -1;
const end = stream.index + length;
let entityId = -1;
const receivedEntities: PacketEntity[] = [];
const removedEntityIds: number[] = [];
const removedEntityIds: EntityId[] = [];
if (!skip) {
for (let i = 0; i < updatedEntries; i++) {
const diff = readUBitVar(stream);
entityId += 1 + diff;
const pvs = readPVSType(stream);
const pvs = readPVSType(stream);
if (pvs === PVS.ENTER) {
const packetEntity = readEnterPVS(stream, entityId, match);
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
@ -96,11 +97,9 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
applyEntityUpdate(packetEntity, match.getSendTable(packetEntity.serverClass.dataTable), stream);
receivedEntities.push(packetEntity);
} else {
if (match.entityClasses[entityId]) {
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
receivedEntities.push(packetEntity);
}
} else if (match.entityClasses.has(entityId)) {
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
receivedEntities.push(packetEntity);
}
}
@ -113,8 +112,8 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
stream.index = end;
return {
packetType: 'packetEntities',
entities: receivedEntities,
packetType: 'packetEntities',
entities: receivedEntities,
removedEntities: removedEntityIds,
maxEntries,
isDelta,