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.
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.
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;
}
The above method is from the chunk class. Every time a block is changed, it is necessary to check, whether the new block type is liquid or solid. If the new block is solid it is necessary to clear the liquid data.
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;
}
And there might be an additional setBlock() method which allows to pass extra data:
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;
}
The extra data could be larger than just one byte if necessary. Maybe using a 16-bit value would be desireable...
Probably some way to provide views which work within the locks.
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:
- 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.
But each tera array could implement methods to serialize/deserialize to a raw byte stream...
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?
Code:
public void serialize(DataOutputStream out) throws IOException {
...
}
public void deserialize(DataInputStream in) throws IOException, ClassNotFoundException {
...
}
Well, its late again in switzerland... Good night!
