I absolutely agree! Obtaining the arrays themselves would be nice for performance. Though the loss of encapsulation is a problem. Think about liquids for example.I guess it could be set up so that the arrays themselves are obtained, which helps for any batch operations (many reads/writes) - although the loss of encapsulation would be a worry.
Code:
public boolean setBlock(int x, int y, int z, Block newBlock, Block oldBlock) {
if (newBlock != oldBlock) {
if (blocks.set(x, y, z, newBlock.getId(), oldBlock.getId())) {
if (!newBlock.isLiquid()) {
setLiquid(x, y, z, new LiquidData());
}
return true;
}
}
return false;
}
But the problem is not only about liquids! In the thread about block dynamics Cervator is talking about general extra-data per block, whose meaning and interpretation would be specific to the type of the block. This makes it necessary that block and extra data are tightly encapsulated. Every block type needs to specify whether it requires extra data and it needs to specify a default value for the extra data. (For debuging purposes the blocks might even specify the exact range of valid extra data values. This would allow to validate the extra data and would certainly make it easier to find bugs. It would also be possible to throw an exception if invalid extra data values are set.)
Generalized for extra data, the above method might look like this:
Code:
public boolean setBlock(int x, int y, int z, Block newBlock, Block oldBlock) {
if (newBlock != oldBlock) {
if (blocks.set(x, y, z, newBlock.getId(), oldBlock.getId())) {
if (newBlock.needsExtraData()) {
setExtra(x, y, z, newBlock.getDefaultExtraData());
} else {
setExtra(x, y, z, 0); // could be omitted... ???
}
return true;
}
}
return false;
}
Code:
public boolean setBlock(int x, int y, int z, Block newBlock, byte extra, Block oldBlock) {
if (newBlock != oldBlock) {
if (blocks.set(x, y, z, newBlock.getId(), oldBlock.getId())) {
setExtra(x, y, z, extra);
return true;
}
}
return false;
}
I was thinking about that too. I would like it to solve the encapsulation problem through views because they would allow lots of cool stuff:Probably some way to provide views which work within the locks.
- Views which allow direct access without encapsulation.
- Views which span multiple chunks.
- Read-Only views! That allows to extend the simple lock into a read/write lock. Good for performance!
- Blocking views: They would block until the chunk is ready/available. Or maybe that could be an integral part of every view. It would simplify the implementation of algorithms which require multiple chunks to be fully ready and available. Very useful for vertical chunks, i guess...
- e.t.c.
I have just read more about protobuf. Its features are very cool! I will implement serialization to raw byte streams. I don't have any experience with protobuf though. What do you say about something like that? Would that work with protobuf?But each tera array could implement methods to serialize/deserialize to a raw byte stream...
Code:
public void serialize(DataOutputStream out) throws IOException {
...
}
public void deserialize(DataInputStream in) throws IOException, ClassNotFoundException {
...
}