1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +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,7 +19,7 @@ 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
@ -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);
}
@ -64,7 +65,7 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
// 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 isDelta = stream.readBoolean();
const delta = (isDelta) ? stream.readInt32() : 0;
const baseLine = stream.readBits(1);
const updatedEntries = stream.readBits(11);
@ -74,7 +75,7 @@ export function ParsePacketEntities(stream: BitStream, match: Match, skip: boole
let entityId = -1;
const receivedEntities: PacketEntity[] = [];
const removedEntityIds: number[] = [];
const removedEntityIds: EntityId[] = [];
if (!skip) {
for (let i = 0; i < updatedEntries; i++) {
@ -96,13 +97,11 @@ 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]) {
} else if (match.entityClasses.has(entityId)) {
const packetEntity = getPacketEntityForExisting(entityId, match, pvs);
receivedEntities.push(packetEntity);
}
}
}
if (isDelta) {
while (stream.readBoolean()) {