1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +02:00

add encoder for bspDecal

This commit is contained in:
Robin Appelman 2017-08-16 23:28:50 +02:00
commit 44394b0320
3 changed files with 122 additions and 8 deletions

View file

@ -1,6 +1,6 @@
import {make} from '../Packet/ParserGenerator';
import {ParseBSPDecal} from '../Packet/BSPDecal';
import {EncodeBSPDecal, ParseBSPDecal} from '../Packet/BSPDecal';
import {EncodeClassInfo, ParseClassInfo} from '../Packet/ClassInfo';
import {ParseCmdKeyValues} from '../Packet/CmdKeyValues';
import {EncodeCreateStringTable, ParseCreateStringTable} from '../Packet/CreateStringTable';
@ -48,7 +48,7 @@ export class Packet extends Parser {
17: {parser: ParseParseSounds, encoder: EncodeParseSounds},
18: make('setView', 'index{11}'),
19: make('fixAngle', 'relative{b}x{16}y{16}z{16}'),
21: {parser: ParseBSPDecal, encoder: voidEncoder},
21: {parser: ParseBSPDecal, encoder: EncodeBSPDecal},
23: {parser: ParseUserMessage, encoder: voidEncoder},
24: {parser: ParseEntityMessage, encoder: voidEncoder},
25: {parser: ParseGameEvent, encoder: voidEncoder},

View file

@ -2,12 +2,12 @@ import {BitStream} from 'bit-buffer';
import {BSPDecalPacket} from '../../Data/Packet';
import {Vector} from '../../Data/Vector';
function getCoord(stream) {
const hasInt = !!stream.readBits(1);
const hasFract = !!stream.readBits(1);
export function getCoord(stream: BitStream): number {
const hasInt = stream.readBoolean();
const hasFract = stream.readBoolean();
let value = 0;
if (hasInt || hasFract) {
const sign = !!stream.readBits(1);
const sign = stream.readBoolean();
if (hasInt) {
value += stream.readBits(14) + 1;
}
@ -18,13 +18,32 @@ function getCoord(stream) {
value = -value;
}
}
return value;
}
function getVecCoord(stream): Vector {
export function encodeCoord(value: number, stream: BitStream) {
const abs = Math.abs(value);
const intPart = Math.floor(abs);
const fractPart = abs % 1;
stream.writeBoolean(intPart !== 0);
stream.writeBoolean(fractPart !== 0);
if (intPart || fractPart) {
stream.writeBoolean(value < 0);
if (intPart) {
stream.writeBits(intPart - 1, 14);
}
if (fractPart) {
stream.writeBits(fractPart * 32, 5);
}
}
}
export function getVecCoord(stream: BitStream): Vector {
const hasX = stream.readBoolean();
const hasY = stream.readBoolean();
const hasZ = stream.readBoolean();
return {
x: hasX ? getCoord(stream) : 0,
y: hasY ? getCoord(stream) : 0,
@ -32,16 +51,33 @@ function getVecCoord(stream): Vector {
};
}
export function encodeVecCoord(vector: Vector, stream: BitStream) {
stream.writeBoolean(vector.x !== 0);
stream.writeBoolean(vector.y !== 0);
stream.writeBoolean(vector.z !== 0);
if (vector.x !== 0) {
encodeCoord(vector.x, stream);
}
if (vector.y !== 0) {
encodeCoord(vector.y, stream);
}
if (vector.z !== 0) {
encodeCoord(vector.z, stream);
}
}
export function ParseBSPDecal(stream: BitStream): BSPDecalPacket { // 21: ParseBSPDecal
let modelIndex = 0;
let entIndex = 0;
const position = getVecCoord(stream);
const textureIndex = stream.readBits(9);
if (stream.readBits(1)) {
if (stream.readBoolean()) {
entIndex = stream.readBits(11);
modelIndex = stream.readBits(12);
}
const lowPriority = stream.readBoolean();
return {
packetType: 'bspDecal',
position,
@ -51,3 +87,16 @@ export function ParseBSPDecal(stream: BitStream): BSPDecalPacket { // 21: ParseB
lowPriority,
};
}
export function EncodeBSPDecal(packet: BSPDecalPacket, stream: BitStream) {
encodeVecCoord(packet.position, stream);
stream.writeBits(packet.textureIndex, 9);
if (packet.entIndex || packet.modelIndex) {
stream.writeBoolean(true);
stream.writeBits(packet.entIndex, 11);
stream.writeBits(packet.modelIndex, 12);
} else {
stream.writeBoolean(false);
}
stream.writeBoolean(packet.lowPriority);
}

View file

@ -0,0 +1,65 @@
import {BitStream} from 'bit-buffer';
import {assertEncoder, assertParser, getStream} from './PacketTest';
import {
EncodeBSPDecal, encodeCoord, encodeVecCoord, getCoord, getVecCoord,
ParseBSPDecal
} from '../../../../Parser/Packet/BSPDecal';
const data = [239, 236, 208, 85, 33, 127, 128, 9, 8];
suite('BSPDecal', () => {
test('getCoord', () => {
assertParser(getCoord, getStream([157, 29, 186]), -948, 17);
});
test('getVecCoord', () => {
assertParser(getVecCoord, getStream([239, 236, 208, 85, 33, 127, 128]), {
x: -948,
y: -684,
z: 128
}, 54);
});
test('encodeCoord', () => {
assertEncoder(getCoord, encodeCoord, 5, 17);
assertEncoder(getCoord, encodeCoord, -5, 17);
assertEncoder(getCoord, encodeCoord, 0.09375, 8);
assertEncoder(getCoord, encodeCoord, -6.09375, 22);
assertEncoder(getCoord, encodeCoord, 0, 2);
});
test('encodeVecCoord', () => {
assertEncoder(getVecCoord, encodeVecCoord, {
x: 1,
y: -12,
z: 0.5
}, 45);
assertEncoder(getVecCoord, encodeVecCoord, {
x: 0,
y: 0,
z: 0.5
}, 11);
});
test('Parse bspDecal', () => {
assertParser(ParseBSPDecal, getStream(data), {
packetType: 'bspDecal',
position: {x: -948, y: -684, z: 128},
textureIndex: 38,
entIndex: 0,
modelIndex: 0,
lowPriority: false
}, 65);
});
test('Encode bspDecal', () => {
assertEncoder(ParseBSPDecal, EncodeBSPDecal, {
packetType: 'bspDecal',
position: {x: -948, y: -684, z: 128},
textureIndex: 38,
entIndex: 0,
modelIndex: 0,
lowPriority: false
}, 65);
});
});