Help with "Spreadsheet"

I am making a 3D game. For this game, I need an efficient way to store and access block data. I want to be able to - find any block at specified z coord, - get all blocks of same type, - get average coord for block in chunk, stuff like that. I have an idea for this, but I'm not sure if it would work for primitive types. As the title suggests, I thought of a spreadsheet. Separate columns (or lists) of each variable, and each row makes up an Object. A cell should automatically update when the row's value is changed, and the row's value should be changed when the cell is changed. I know this is easy for non-primitive types, but I am getting confused on primitives. And, if the variable is set in one of them instead of updated, it wouldn't propogate to the other and the link would be broken. (sorry if I am not using right terms, new to java). I think reflection can do this, but I have no clue how to do it. Please help?
41 Replies
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @The Typhothanian! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
The Typhothanian
The TyphothanianOP11mo ago
Also, it needs to be usable for more than blocks pls
dan1st
dan1st11mo ago
Do you want to load all at once?
The Typhothanian
The TyphothanianOP11mo ago
What are you referring to by "load"?
dan1st
dan1st11mo ago
all your blocks
The Typhothanian
The TyphothanianOP11mo ago
Like, go through them all at once?
dan1st
dan1st11mo ago
Do you have all blocks in memory?
The Typhothanian
The TyphothanianOP11mo ago
No, using 32^3 chunks I would have a spreadsheet for each chunk
dan1st
dan1st11mo ago
ok I wouldn't use spreadsheets first of all but a custom format
The Typhothanian
The TyphothanianOP11mo ago
And I would be using these spreadsheets for other stuff Okay What would be a good solution?
dan1st
dan1st11mo ago
you could e.g. use one file per chunk or store N chunks in a file together for simplicity, assume one chunk per file
The Typhothanian
The TyphothanianOP11mo ago
No, I have a system for storing in a file, I want a system for storing in memory
dan1st
dan1st11mo ago
oh just use an array of block objects?
The Typhothanian
The TyphothanianOP11mo ago
That's why I thought of a sheet
dan1st
dan1st11mo ago
what do you mean with spreadsheet?
The Typhothanian
The TyphothanianOP11mo ago
Well, looking for all blocks of specific type, for example would take a while Give me a minute I am working on smthn else rn
dan1st
dan1st11mo ago
btw reflection doesn't help with the issues you mentioned (propagating) but setters or similar can help Are your coordinates integers or floating point values? i.e. can you have a block at coordinates like 1.25
The Typhothanian
The TyphothanianOP11mo ago
ints
The Typhothanian
The TyphothanianOP11mo ago
So, here's an example
No description
dan1st
dan1st11mo ago
I assume you often need to be able to find blocks by their coordinates?
The Typhothanian
The TyphothanianOP11mo ago
yes
dan1st
dan1st11mo ago
Why do you want to use spreadsheets?
The Typhothanian
The TyphothanianOP11mo ago
Each row would form a Block instance, and I would need to be able to find any blocks at x=1, etc.
dan1st
dan1st11mo ago
just use a 3D array
The Typhothanian
The TyphothanianOP11mo ago
Like I said, I would have to iterate through up to 3072 blocks, that's inefficient on large scales And the spreadsheet thing is just an analogy
dan1st
dan1st11mo ago
What is the fraction of empty vs filled blocks?
The Typhothanian
The TyphothanianOP11mo ago
Any
dan1st
dan1st11mo ago
how sparse are your blocks typically?
The Typhothanian
The TyphothanianOP11mo ago
Underground chunks would be full, air chunks would be empty Density decreases as y increases
dan1st
dan1st11mo ago
What's the fraction of underground blocks vs air? oh so it's complete chunks that are empty typically?
The Typhothanian
The TyphothanianOP11mo ago
something like 2 air for 1 underground Yeah, they can either be completely full or completely empty So, I think arrays would be inneficient for air chunks
dan1st
dan1st11mo ago
so like 1/3 is blocks and 1/3 is airs? Then arrays are likely the best choice for storing and you can store copies for efficient access of other things
The Typhothanian
The TyphothanianOP11mo ago
hmm
dan1st
dan1st11mo ago
then only use arrays for non air chunks
The Typhothanian
The TyphothanianOP11mo ago
But what if I wanted to set all grass blocks in a chunk to stone? without iterating through all 3072
dan1st
dan1st11mo ago
that's what I meant with copies
The Typhothanian
The TyphothanianOP11mo ago
Ok, I'll just go the easy way and encapsulate primitives in a wrapper class
dan1st
dan1st11mo ago
public enum BlockType{
GRASS, STONE
}
public record Coordinate(int x, int y, int z){}
public class Chunk{
private BlockType[][][] blocks;//TODO initialize
private Map<BlockType, Set<Coordinate>> blocksByType = new EnumMap<>(BlockType.class);
public void setBlock(Coordinate coord, BlockType type){
Set<Coordinate> oldBlockTypeCoords = blocksByType.get(type)
if(oldBlockCoords!=null){
oldCoordBlocks.remove(coord);
}
blocksByType.computeIfAbsent(type, t-> new HashSet<>()).add(coord);
blocks[coord.x()][coord.y()][coord.z()] = type;
}
}
public enum BlockType{
GRASS, STONE
}
public record Coordinate(int x, int y, int z){}
public class Chunk{
private BlockType[][][] blocks;//TODO initialize
private Map<BlockType, Set<Coordinate>> blocksByType = new EnumMap<>(BlockType.class);
public void setBlock(Coordinate coord, BlockType type){
Set<Coordinate> oldBlockTypeCoords = blocksByType.get(type)
if(oldBlockCoords!=null){
oldCoordBlocks.remove(coord);
}
blocksByType.computeIfAbsent(type, t-> new HashSet<>()).add(coord);
blocks[coord.x()][coord.y()][coord.z()] = type;
}
}
The Typhothanian
The TyphothanianOP11mo ago
Ok thank you
JavaBot
JavaBot11mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot11mo ago
Post Closed
This post has been closed by <@801145088830210129>.
Want results from more Discord servers?
Add your server