Skip to content

Commit

Permalink
patch: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCxFF committed Nov 11, 2022
1 parent f118d96 commit bd17605
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
22 changes: 20 additions & 2 deletions src/Coder/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
// TODO(ABC):
// Code a compressor (The previous was not OOP and used lz4js code)
// If this code gets out of bounds (only happens if you play around with dev too much), the server crashes [better than out of bounds r/w].
const OUTPUT_BUFFER = new Uint8Array(65536);
//
// TEMP FIX - 2022/11/11:
// OUTPUT_BUFFER now gets resized if running out of space for the packet
let OUTPUT_BUFFER = new Uint8Array(65536);

const convo = new ArrayBuffer(4);
const u8 = new Uint8Array(convo);
Expand All @@ -40,7 +43,22 @@ const endianSwap = (num: number) =>
((num >> 24) & 0xff);

export default class Writer {
private at: number = 0;
private _at: number = 0;

private get at() {
return this._at;
}

private set at(v) {
this._at = v;

if (OUTPUT_BUFFER.byteLength <= this._at + 5) {
const newBuffer = new Uint8Array(OUTPUT_BUFFER.byteLength + (OUTPUT_BUFFER.byteLength >> 1));
newBuffer.set(OUTPUT_BUFFER, 0);

OUTPUT_BUFFER = newBuffer;
}
}

public u8(val: number) {
OUTPUT_BUFFER[this.at++] = val;
Expand Down
49 changes: 32 additions & 17 deletions src/Native/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ export default class Camera extends CameraEntity {

/** Updates the camera's current view. */
private updateView(tick: number) {
const w = this.client.write().u8(ClientBound.Update).vu(tick);

const deletes: { id: number, hash: number, noDelete?: boolean }[] = [];
const updates: Entity[] = [];
const creations: Entity[] = [];
Expand Down Expand Up @@ -222,13 +220,8 @@ export default class Camera extends CameraEntity {
}
}

// Now compile
w.vu(deletes.length);
for (let i = 0; i < deletes.length; ++i) {
w.entid(deletes[i]);
if (!deletes[i].noDelete) this.removeFromView(deletes[i].id);
}

// Next pick updates
const entities = this.game.entities;
for (const id of this.game.entities.otherEntities) {
if (this.view.findIndex(r => r.id === id) === -1) {
Expand Down Expand Up @@ -269,16 +262,38 @@ export default class Camera extends CameraEntity {
}
}

// Arrays of entities
w.vu(creations.length + updates.length);
for (let i = 0; i < updates.length; ++i) {
this.compileUpdate(w, updates[i]);
}
for (let i = 0; i < creations.length; ++i) {
this.compileCreation(w, creations[i]);
}
// UPDATE 2022/11/11 - Send out only chunks of 64 updates per packet to prevent intense packet size
// - WHEN COMPRESSION IS ADDED, UNDO
const CHUNK_SIZE = 64;
let entitiesSent = 0;
while (entitiesSent < creations.length + updates.length) {
// Prep for compilation
const w = this.client.write().u8(ClientBound.Update).vu(tick);
// If the first packet of tick, compile deletions
if (entitiesSent === 0) {
w.vu(deletes.length);
for (let i = 0; i < deletes.length; ++i) {
w.entid(deletes[i]);
if (!deletes[i].noDelete) this.removeFromView(deletes[i].id);
}
} else w.vu(0); // or assume they've already been completed

w.send();
const lastChunk = entitiesSent;
const sizeOfChunk = creations.length + updates.length - lastChunk < CHUNK_SIZE ? creations.length + updates.length - lastChunk : CHUNK_SIZE;

// Chunk out the arrays of entities
w.vu(sizeOfChunk);
while (entitiesSent < updates.length && entitiesSent - lastChunk < sizeOfChunk) {
this.compileUpdate(w, updates[entitiesSent]);
entitiesSent += 1;
}
while (entitiesSent - updates.length < creations.length && entitiesSent - lastChunk < sizeOfChunk) {
this.compileCreation(w, creations[entitiesSent - updates.length]);
entitiesSent += 1;
}

w.send();
}
}

/** Entity creation compiler function... Run! */
Expand Down

0 comments on commit bd17605

Please sign in to comment.