Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Implementin blocks in a Minecraft-like, AI-heavy, complex game with huge worlds  (Read 2815 times)
0 Members and 1 Guest are viewing this topic.
Offline Llorn

Junior Newbie





« Posted 2016-03-26 23:31:22 »

I'm developing an AI-driven fantasy world generator(Meaning that the resident NPCs will take care of doing everything after the initial "normal" generation.) with the aim of making it generate the most engaging worlds possible. Basically my own Dwarf Fortress to lord over, gameplay- and complexity- wise. I'm starting out small, though, so for now, it's a 100x100 plate of rock and few NPCs on it. I want to see if they will properly seek out water and food if I make them go thirsty and hungry.

But first, I wanted to ask something about my implementation of the Block class, intended to represent every single block in the game world:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Block {
    int posX, posY, posZ;
    Material material;
    BlockShape shape;
    ArrayList<Object> objects;


    public Block(int posX, int posY, int posZ) {
        this.posX = posX;
        this.posY = posY;
        this.posZ = posZ;
    }
}


The idea is for the blocks to have obviously their coordinates, then the material they're made of(dirst, iron ore, adamantium,...), then their shape(solid, ramp, column, hewn wall,...), and then a list of diverse other objects that may be in the block(NPCs, furniture, projectile, effect of a spell, item, remains, some flora, fluid...). Coordinates stay the same, the other stuff is initialized(and deleted) as needed during world generation/gameplay. So, folks with a more extensive knowledge and experience with running times and data structure(I know, technically, there's no data structure yet, but bear with me) optimization: Do you see any obvious red lights in there; could this cause a huge slowdown or save bloat? There's gonna be a lot of these in the game, and they're going to go through a huge number of AI checks, so I figured out I'd ask beforehand, rather than having to weed out such an elementary feature out of everything later and rework it.
Offline CopyableCougar4
« Reply #1 - Posted 2016-03-26 23:34:34 »

Well for material you can have a list of materials and simply store a reference integer for each block.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
static ArrayList<Material> materials;

public class Block {
    int posX, posY, posZ;
    int materialId;
    BlockShape shape;
    ArrayList<Object> objects;


    public Block(int posX, int posY, int posZ) {
        this.posX = posX;
        this.posY = posY;
        this.posZ = posZ;
    }

// now you get material with materials.get(materialId)
}


Also, if you are only planning on one or two objects per block, then just use a field or two, because using ArrayLists can use a decent amount of memory.

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline ags1

JGO Kernel


Medals: 356
Projects: 7


Make code not war!


« Reply #2 - Posted 2016-03-26 23:52:05 »

How does dividing the world into arbitrary blocks help you? It just seems to introduce another thing to loop over.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Llorn

Junior Newbie





« Reply #3 - Posted 2016-03-27 00:13:44 »

How does dividing the world into arbitrary blocks help you? It just seems to introduce another thing to loop over.

It's the most flexible implementation as far as I know. Generate all the volcanoes and crevices you want, have dwarves and drow and undead and gnomes and whatnot carve cities underground, have the Old Ones build some underwater, have the mages make theirs float, have more architectural styles in the game, have the AI use the local materials for building, make the world dried and have civilizations die leaving their ruins after them... What would you suggest instead?
Offline ags1

JGO Kernel


Medals: 356
Projects: 7


Make code not war!


« Reply #4 - Posted 2016-03-27 00:24:08 »

Only store the entities. Most of those blocks are just cubic meters of air or rock or water, and then you are going to be looping over all of these and then looping over their objects lists to find the actual entities?

Offline Llorn

Junior Newbie





« Reply #5 - Posted 2016-03-27 00:48:48 »

Only store the entities. Most of those blocks are just cubic meters of air or rock or water, and then you are going to be looping over all of these and then looping over their objects lists to find the actual entities?

The entities will be stored in their own list, looping over that will be what will make the game tick, literally. Blocks will only be called when either an entity does something to them, such as move into them or mine them(I don't know what to do about liquids yet, though. Looping through them and checking the pressure and whatnot would murder the FPS), or when the graphics will need to know what to display.

I haven't yet decided on the data structure in which to store the blocks. Something with fast enough access for all functionality required, but not too much bloat due to excessive references.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1327
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2016-03-27 10:39:34 »

I haven't yet decided on the data structure in which to store the blocks. Something with fast enough access for all functionality required, but not too much bloat due to excessive references.
If you can derive X,Y,Z from the location in the block data structure, you can remove these 3 fields from the Block class.

Simple example: currently you store 3 ints = 3x4 bytes = 12 bytes worth of coordinates in each block. For 100x100x100 blocks that's 12MB. If you create a Block[W][H][D], and loop over it, you get access to these coordinates by iteration, where the Block[][][] will 'only' take 4MB. Ofcourse, this is just a trivial example. A Block[][][] has its downsides too, but it all depends on how sparse you expect your space to be. In case of an extremely sparse space, you could fall back to HashMap<Coord3d, Block>. Whichever way you chose, I find it very unlikely that the data structure holding the blocks will not be able to provide you with the coordinates, so you can safely remove them from the Block class.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline ags1

JGO Kernel


Medals: 356
Projects: 7


Make code not war!


« Reply #7 - Posted 2016-03-27 14:03:21 »

I suppose a question would be: how big is the world, and how big are the blocks?

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (145 views)
2017-09-30 02:57:34

theagentd (213 views)
2017-09-26 18:23:31

cybrmynd (294 views)
2017-08-02 12:28:51

cybrmynd (284 views)
2017-08-02 12:19:43

cybrmynd (294 views)
2017-08-02 12:18:09

Sralse (287 views)
2017-07-25 17:13:48

Archive (966 views)
2017-04-27 17:45:51

buddyBro (1092 views)
2017-04-05 03:38:00

CopyableCougar4 (1663 views)
2017-03-24 15:39:42

theagentd (1425 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!