Skip to content

Instantly share code, notes, and snippets.

@Jire
Created June 7, 2016 23:43
Show Gist options
  • Save Jire/baee865815844634618ff7c9ef77e011 to your computer and use it in GitHub Desktop.
Save Jire/baee865815844634618ff7c9ef77e011 to your computer and use it in GitHub Desktop.
public final void updatePlayers(Buffer b, int size) {
int updateCount = 0;
b.startBitAccess(0);
currentPlane = b.readBits(2);
int tileX = b.readBits(7);
int tileY = b.readBits(7);
if (b.readBits(1) == 1) {
entityUpdateIndices[updateCount++] = LOCALPLAYER_INDEX;
}
localPlayer.setPosition(tileX, tileY);
deadEntityCount = 0;
int count = b.readBits(8);
if (count < npcCount) {
for (int n = count; n < playerCount; n++) {
deadEntityIndices[deadEntityCount++] = playerIndices[n];
players[playerIndices[n]].remove = true;
}
}
playerCount = 0;
for (int n = 0; n < count; n++) {
int index = playerIndices[n];
Player p = players[index];
if (b.readBits(1) == 0) {
playerIndices[playerCount++] = index;
} else {
int type = b.readBits(2);
if (type == 3) {
deadEntityIndices[deadEntityCount++] = index;
p.remove = true;
} else {
playerIndices[playerCount++] = index;
if (type == 0) {
entityUpdateIndices[updateCount++] = index;
} else {
if (type == 2) {
entityUpdateIndices[updateCount++] = index;
}
int dir = b.readBits(3);
if (dir == 0) {
p.moveBy(false, 1, -1);
} else if (dir == 1) {
p.moveBy(false, 1, 0);
} else if (dir == 2) {
p.moveBy(false, 1, 1);
} else if (dir == 3) {
p.moveBy(false, 0, -1);
} else if (dir == 4) {
p.moveBy(false, 0, 1);
} else if (dir == 5) {
p.moveBy(false, -1, -1);
} else if (dir == 6) {
p.moveBy(false, -1, 0);
} else if (dir == 7) {
p.moveBy(false, -1, 1);
}
}
}
}
}
for (;;) {
int index = b.readBits(11);
if (index == 2047 || b.bitPos + 10 >= size * 8) {
break;
}
if (players[index] == null) {
players[index] = new Player();
if (playerBuffers[index] != null) {
players[index].read(playerBuffers[index]);
}
}
playerIndices[playerCount++] = index;
Player p = players[index];
p.remove = false;
int x = b.readBits(5);
if (x > 15) {
x -= 32;
}
int y = b.readBits(5);
if (y > 15) {
y -= 32;
}
p.setPosition(localPlayer.pathX[0] + x, localPlayer.pathY[0] + y);
entityUpdateIndices[updateCount++] = index;
}
b.startByteAccess();
for (int n = 0; n < updateCount; n++) {
int index = entityUpdateIndices[n];
Player p = players[index];
int mask = b.read();
if ((mask & 0x80) == 0x80) {
mask |= b.read() << 8;
}
if ((mask & 0x1) == 1) {
Buffer b1 = new Buffer(new byte[b.read()]);
b.read(b1.data, 0, b1.data.length);
playerBuffers[index] = b1;
p.read(b1);
}
if ((mask & 0x2) == 2) {
int animIndex = b.readUShort();
if (animIndex == 0xFFFF) {
animIndex = -1;
}
if (animIndex == p.primaryAnimIndex) {
p.primaryAnimDelta = 0;
}
int delay = b.read();
if (animIndex == -1 || p.primaryAnimIndex == -1
|| Animation.instance[animIndex].priority > Animation.instance[p.primaryAnimIndex].priority) {
p.primaryAnimIndex = animIndex;
p.primaryAnimFrame = 0;
p.primaryAnimCycle = 0;
p.primaryAnimDelay = delay;
p.primaryAnimDelta = 0;
}
}
if ((mask & 0x4) == 4) {
p.targetEntity = b.readUShort();
if (p.targetEntity == 0xFFFF) {
p.targetEntity = -1;
}
}
if ((mask & 0x8) == 8) {
p.spoken = b.readString();
p.spokenColor = 0;
p.spokenEffect = 0;
p.spokenLife = 150;
addMessage(2, p.name, p.spoken);
}
if ((mask & 0x10) == 0x10) {
p.damageTaken = b.read();
p.damageType = b.read();
p.lastCombatCycle = cycle + 400;
p.health = b.read();
p.maxHealth = b.read();
}
if ((mask & 0x20) == 32) {
p.focusX = b.readUShort();
p.focusY = b.readUShort();
}
if ((mask & 0x40) == 64) {
int info = b.readUShort();
int length = b.read();
long l = StringUtil.toBase37(p.name);
boolean ignore = false;
for (int m = 0; m < ignoreCount; m++) {
if (ignoreNameLong[m] == l) {
ignore = true;
break;
}
}
if (!ignore) {
String s = StringBuffer.read(b, length);
s = Censor.getFiltered(s);
p.spoken = s;
p.spokenColor = info >> 8;
p.spokenEffect = info & 0xff;
p.spokenLife = 150;
addMessage(2, p.name, s);
}
}
if ((mask & 0x100) == 0x100) {
p.spotanimIndex = b.readUShort();
int info = b.readInt();
p.spotanimOffsetY = info >> 16;
p.lastSpotanimCycle = cycle + (info & 0xFFFF);
p.spotanimFrame = 0;
p.spotanimCycle = 0;
if (p.lastSpotanimCycle > cycle) {
p.spotanimFrame = -1;
}
if (p.spotanimIndex == 0xFFFF) {
p.spotanimIndex = -1;
}
}
if ((mask & 0x200) == 0x200) {
p.srcTileX = b.read();
p.srcTileY = b.read();
p.dstTileX = b.read();
p.dstTileY = b.read();
p.firstMoveCycle = b.readUShort() + cycle;
p.lastMoveCycle = b.readUShort() + cycle;
p.faceDirection = b.read();
p.pathStepCount = 0;
p.pathX[0] = p.dstTileX;
p.pathY[0] = p.dstTileY;
}
}
for (int n = 0; n < deadEntityCount; n++) {
int i = deadEntityIndices[n];
if (players[i].remove) {
players[i] = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment