Skip to content

Instantly share code, notes, and snippets.

@rmmh
Last active January 10, 2019 05:30
Show Gist options
  • Save rmmh/2e041ca775fd32cdb921b4a60b4a00e2 to your computer and use it in GitHub Desktop.
Save rmmh/2e041ca775fd32cdb921b4a60b4a00e2 to your computer and use it in GitHub Desktop.
Source for part of the ancient Minecraft FastRender mod. Pick the closest 3 chunks to render without doing a full sort.
public boolean a(ei camera, boolean firstRun)
{
hz cmp = new hz(camera);
bz[] toAdd = new bz[3]; //TODO: throughout: bz should be something like "Chunk"
ArrayList<bz> nearChunks = null;
int pendingChunkSize = this.m.size();
int pendingChunkRemoved = 0;
for (int i = 0; i < pendingChunkSize; ++i) {
bz chunk = (bz)this.m.get(i);
if (!firstRun) {
if (chunk.a(camera) > 1024.0F) {
int index;
// is this chunk in the closest 3?
for (index = 0; index < 3; index++) {
if (toAdd[index] != null && cmp.a(toAdd[index], chunk) == 1)
break;
}
index--;
// if it is, place it and shift the others
if (index > 0) {
int x = index;
while (--x != 0)
toAdd[x - 1] = toAdd[x];
toAdd[index] = chunk;
}
continue;
}
}
else if (!chunk.o)
continue;
// chunk is very close -- always render
if (nearChunks == null)
nearChunks = new ArrayList<bz>();
pendingChunkRemoved++;
nearChunks.add(chunk);
this.m.set(i, null);
}
// if there are nearby chunks that need to be prepared for rendering,
// sort them and then process them
if (nearChunks != null) {
if (nearChunks.size() > 1)
Collections.sort(nearChunks, new hz(camera));
for (int i = nearChunks.size() - 1; i >= 0; i--) {
bz chunk = nearChunks.get(i);
chunk.a();
chunk.u = false;
}
}
// render the nearest 3 chunks (farther than 1024 units away)
int secondaryRemoved = 0;
for (int i = 2; i >= 0; i--) {
bz chunk = toAdd[i];
if (chunk != null) {
if (!chunk.o && i != 2) { // escape early if chunks aren't ready
toAdd[i] = null;
toAdd[0] = null;
break;
}
toAdd[i].a();
toAdd[i].u = false;
secondaryRemoved++;
}
}
// compact by removing nulls
int cursor = 0;
int target = 0;
int arraySize = this.m.size();
while (cursor != arraySize) {
bz chunk = (bz)this.m.get(cursor);
if (chunk != null) {
if (chunk == toAdd[0] || chunk == toAdd[1] || chunk == toAdd[2]) {
; // this chunk was rendered and should be removed
} else {
if (target != cursor)
this.m.set(target, chunk);
target++;
}
}
cursor++;
}
// trim
while (--cursor >= target) {
this.m.remove(cursor);
}
return pendingChunkSize == pendingChunkRemoved + secondaryRemoved;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment