tighter packing

This commit is contained in:
Robin Appelman 2020-10-10 01:12:04 +02:00
commit 84187ebb6a
4 changed files with 1895 additions and 1618 deletions

View file

@ -1,7 +1,7 @@
import {FlatState, XY} from '../pkg/tf_demos_viewer';
import {FlatState, XY} from '../pkg/index';
export async function parseDemo(bytes: Uint8Array): Promise<ParsedDemo> {
let m = await import("../pkg/tf_demos_viewer.js");
let m = await import("../pkg/index.js");
const state = m.parse_demo(bytes);
let playerCount = state.player_count;
@ -112,15 +112,16 @@ export class ParsedDemo {
}
}
const PACK_SIZE = 8;
const PACK_SIZE = 7;
function unpackPlayer(bytes: Uint8Array, base: number, world: WorldBoundaries): PlayerState {
const x = unpack_f32(bytes[base] + (bytes[base + 1] << 8), world.boundary_min.x, world.boundary_max.x);
const y = unpack_f32(bytes[base + 2] + (bytes[base + 3] << 8), world.boundary_min.y, world.boundary_max.y);
let health = bytes[base + 4] + (bytes[base + 5] << 8);
const team_class_health = bytes[base + 4] + (bytes[base + 5] << 8);
const angle = unpack_angle(bytes[base + 6]);
const team = (bytes[base + 7] >> 4) as Team;
const playerClass = (bytes[base + 7] & 15) as Class;
const health = team_class_health & 1013;
const team = (team_class_health >> 14) as Team;
const playerClass = ((team_class_health >> 10) & 15) as Class;
return {
position: {x, y},

3396
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -79,7 +79,7 @@ pub struct PlayerState {
impl PlayerState {
const PACKET_SIZE: usize = 8;
pub fn pack(&self, world: &World) -> [u8; 8] {
pub fn pack(&self, world: &World) -> [u8; 7] {
// for the purpose of viewing the demo in the browser we dont really need high accuracy for
// position or angle, so we save a bunch of space by truncating those down to half the number
// of bits
@ -90,21 +90,25 @@ impl PlayerState {
let x = pack_f32(self.position.x, world.boundary_min.x, world.boundary_max.x).to_le_bytes();
let y = pack_f32(self.position.y, world.boundary_min.y, world.boundary_max.y).to_le_bytes();
let team_and_class = ((self.team as u8) << 4) + self.class as u8;
let health_bytes = self.health.to_le_bytes();
// 2 bits for team
// 4 bits for class
// 10 bits for health
let team_class_health =
((self.team as u16) << 14) + ((self.class as u16) << 10) + self.health;
let combined_bytes = team_class_health.to_le_bytes();
[
x[0],
x[1],
y[0],
y[1],
health_bytes[0],
health_bytes[1],
combined_bytes[0],
combined_bytes[1],
self.angle.0,
team_and_class,
]
}
#[allow(dead_code)]
pub fn unpack(bytes: [u8; 8], world: &World) -> Self {
fn unpack_f32(val: u16, min: f32, max: f32) -> f32 {
let ratio = val as f32 / (u16::max_value() as f32);

View file

@ -31,9 +31,11 @@ module.exports = {
],
},
plugins: [
new CopyPlugin([
path.resolve(__dirname, "static")
]),
new CopyPlugin({
patterns: [
path.resolve(__dirname, "static")
]
}),
new WasmPackPlugin({
crateDirectory: __dirname,