Skip to content

Instantly share code, notes, and snippets.

@Tzeentchful
Created May 16, 2014 03:44
Show Gist options
  • Save Tzeentchful/a0bcc48cda65df73eac1 to your computer and use it in GitHub Desktop.
Save Tzeentchful/a0bcc48cda65df73eac1 to your computer and use it in GitHub Desktop.
Minecraft map rendering
public void a(World world, Entity entity, WorldMap worldmap) {
if (world.worldProvider.dimension == worldmap.map && entity instanceof EntityHuman) {
int scale = 1 << worldmap.scale;
int centerX = worldmap.centerX;
int centerZ = worldmap.centerZ;
int relX = MathHelper.floor(entity.locX - (double) centerX) / scale + 64;
int relZ = MathHelper.floor(entity.locZ - (double) centerZ) / scale + 64;
int blocksPP = 128 / scale; // blocks per pixel
if (world.worldProvider.g) { // if nether
blocksPP /= 2;
}
WorldMapHumanTracker worldmaphumantracker = worldmap.a((EntityHuman) entity);
++worldmaphumantracker.d;
for (int currX = relX - blocksPP + 1; currX < relX + blocksPP; ++currX) { //Loop X
if ((currX & 15) == (worldmaphumantracker.d & 15)) { // 15 block radius?
int l1 = 255; // Something to do with flagging shadow updates
int i2 = 0; // Something to do with flagging shadow updates
double lastBlockHeight = 0.0D; // Used for calculating shadows
for (int currZ = relZ - blocksPP - 1; currZ < relZ + blocksPP; ++currZ) { //Loop Z
if (currX >= 0 && currZ >= -1 && currX < 128 && currZ < 128) {
int k2 = currX - relX;
int l2 = currZ - relZ;
boolean flag = k2 * k2 + l2 * l2 > (blocksPP - 2) * (blocksPP - 2);
int chunkX = (centerX / scale + currX - 64) * scale;
int chunkZ = (centerZ / scale + currZ - 64) * scale;
HashMultiset<MaterialMapColor> blockCountInPixel = HashMultiset.create();
Chunk chunk = world.getChunkAtWorldCoords(chunkX, chunkZ);
if (!chunk.isEmpty()) {
int chunkLocalX = chunkX & 15;
int chunkLocalZ = chunkZ & 15;
int liquidDepth = 0;
double blockHeight = 0.0D;
int currPixelX;
/* calculate colour from all blocks for pixel */
if (world.worldProvider.g) { // if nether
currPixelX = chunkX + chunkZ * 231871;
currPixelX = currPixelX * currPixelX * 31287121 + currPixelX * 11;
if ((currPixelX >> 20 & 1) == 0) {
blockCountInPixel.add(Blocks.DIRT.f(0), 10);
} else {
blockCountInPixel.add(Blocks.STONE.f(0), 100);
}
blockHeight = 100.0D;
} else { // over world
for (currPixelX = 0; currPixelX < scale; ++currPixelX) {
for (int currPixelZ = 0; currPixelZ < scale; ++currPixelZ) {
int currCunkY = chunk.b(currPixelX + chunkLocalX, currPixelZ + chunkLocalZ) + 1;
Block block = Blocks.AIR;
int blockData = 0;
if (currCunkY > 1) {
do {
--currCunkY;
block = chunk.getType(currPixelX + chunkLocalX, currCunkY, currPixelZ + chunkLocalZ);
blockData = chunk.getData(currPixelX + chunkLocalX, currCunkY, currPixelZ + chunkLocalZ);
} while (block.f(blockData) == MaterialMapColor.b && currCunkY > 0);
if (currCunkY > 0 && block.getMaterial().isLiquid()) {
int liquidY = currCunkY - 1;
Block currBlock;
do {
currBlock = chunk.getType(currPixelX + chunkLocalX, liquidY--, currPixelZ + chunkLocalZ);
++liquidDepth;
} while (liquidY > 0 && currBlock.getMaterial().isLiquid());
}
}
blockHeight += (double) currCunkY / (double) (scale * scale);
blockCountInPixel.add(block.f(blockData));
}
}
}
liquidDepth /= scale * scale;
double d2 = (blockHeight - lastBlockHeight) * 4.0D / (double) (scale + 4) + ((double) (currX + currZ & 1) - 0.5D) * 0.4D;
byte colourHue = 1;
if (d2 > 0.6D) {
colourHue = 2;
}
if (d2 < -0.6D) {
colourHue = 0;
}
MaterialMapColor materialmapcolor = (MaterialMapColor) Iterables.getFirst(Multisets.copyHighestCountFirst(blockCountInPixel), MaterialMapColor.b);
/* water color based on depth */
if (materialmapcolor == MaterialMapColor.n) { // if water
d2 = (double) liquidDepth * 0.1D + (double) (currX + currZ & 1) * 0.2D;
colourHue = 1;
if (d2 < 0.5D) {
colourHue = 2;
}
if (d2 > 0.9D) {
colourHue = 0;
}
}
lastBlockHeight = blockHeight;
if (currZ >= 0 && k2 * k2 + l2 * l2 < blocksPP * blocksPP && (!flag || (currX + currZ & 1) != 0)) {
byte currColour = worldmap.colors[currX + currZ * 128];
byte newColour = (byte) (materialmapcolor.M * 4 + colourHue);
if (currColour != newColour) {
if (l1 > currZ) {
l1 = currZ;
}
if (i2 < currZ) {
i2 = currZ;
}
worldmap.colors[currX + currZ * 128] = newColour;
}
}
}
}
}
if (l1 <= i2) { // if adjacent block need shadow update
worldmap.flagDirty(currX, l1, i2);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment