Created
January 20, 2011 23:40
-
-
Save anonymous/788949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ChunkOrder.java | |
By Marglyph, 20 January 2011 | |
Released under the terms of the MIT License as per the text found here: | |
http://opensource.org/licenses/mit-license.php | |
Given the integer (x, z) coordinates of a Minecraft player (accessed by | |
pressing F3 in-game, but leave out the decimal part), this will calculate the | |
order in which surrounding chunks (a chunk is 16 by 16) will be selected in | |
the mob spawning algorithm, as of Minecraft Beta 1.2_01. | |
*/ | |
import java.util.HashSet; | |
import java.util.Iterator; | |
class ChunkOrder { | |
public static void main(String[] args) { | |
/* Validate arguments */ | |
if(args.length != 2) { | |
usage(); | |
System.exit(1); | |
} | |
int x = 0; | |
int z = 0; | |
try { | |
x = Integer.parseInt(args[0]); | |
z = Integer.parseInt(args[1]); | |
} catch (NumberFormatException e) { | |
usage(); | |
System.exit(1); | |
} | |
/* Mimic the game's use of HashSet */ | |
HashSet<CoordPair> set = new HashSet<CoordPair>(); | |
int r = 8; // The "radius" (it's a square) in chunks around the player | |
int chunkX = x / 16; | |
int chunkZ = z / 16; | |
/* Add all chunks around the player to the hashset */ | |
for(int i = -r; i <= r; ++i) { | |
for(int j = -r; j <= r; ++j) { | |
CoordPair coord = new CoordPair(chunkX + i, chunkZ + j); | |
set.add(coord); | |
} | |
} | |
/* Figure out which chunks will be accessed in what order */ | |
int[][] traversalOrder = new int[r * 2 + 1][r * 2 + 1]; | |
int hashIndex = 0; | |
for(CoordPair coord : set) { | |
traversalOrder[coord.x + r - chunkX][coord.z + r - chunkZ] = hashIndex; | |
++hashIndex; | |
} | |
/* Start showing information */ | |
System.out.println("Player at chunk " + Integer.toString(chunkX) + ", " + Integer.toString(chunkZ)); | |
System.out.println("Chunk traversal order (player in center):"); | |
printCenterMarker(r, "N"); | |
for(int k = 0; k < (r * 2 + 1); ++k) { | |
/* Include center marker on left side; */ | |
System.out.print(k == r ? "W " : " "); | |
/* Display a row of aligned numbers */ | |
for(int l = (r * 2); l >= 0; --l) { | |
System.out.print(String.format("%1$3d ", traversalOrder[k][l] + 1)); // 1-indexed for human readers | |
} | |
/* Include center marker on right side */ | |
System.out.println(k == r ? "E" : ""); | |
} | |
printCenterMarker(r, "S"); | |
} | |
/* Print comand-line usage hint */ | |
private static void usage() { | |
System.out.println("Usage: java ChunkOrder <x> <z> (where x and z are player's integer coordinates)"); | |
} | |
/* Print a mark in the center column of output */ | |
private static void printCenterMarker(int r, String label) { | |
int numSpaces = 2 + (r * 4) + 2 - label.length(); // Ick, magic numbers | |
for(int i = 0; i < numSpaces; ++i) System.out.print(" "); | |
System.out.println(label); | |
} | |
/* Mimic the game's coordinate pair class, particularly its hash */ | |
private static final class CoordPair { | |
public int x; | |
public int z; | |
public CoordPair(int x, int z) { | |
this.x = x; | |
this.z = z; | |
} | |
public int hashCode() { | |
return this.x << 8 | this.z; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment