How to load a specific object from a SER-file - java

I'm saving my game world using serialization. The world is basically a big ArrayList containing each area map object. The number of area maps is fixed, but they vary greatly in size (between 5-30 mB each). So I don't want to have the entire world loaded in memory during runtime, only the specific area maps that the player's characters are located in.
Is it possible to load specific area maps without loading the entire world (or in other words: loading a specific object without deserializing the whole SER-file)? Likewise I need to save specific area maps (when all characters have left the area). Note that the size (in data) of the area map could have changed significantly, since the player could potentially move hundreds of objects from one area to another.
The point of this question is that I want to avoid storing each map in it's own SER-file. That would be A LOT of files, which will become even more with each save the player has. So my goal is to pack it up nicely in a single save-file.

What you could do is create a numbering system for each of your area maps. Afterwards, store an array containing these area maps in a serializable. Unload the serializable to obtain an array, and store it in some form of variable. You could then use your numbering system to select the map which you would want to use.
So to answer your question, I do not think it is possible, because the way a serializable is deserialized is it goes through all the serialized fields one by one. You cannot select something from the middle of a SER-file to deserialize.
Happy Programming :)

Related

Storing object information in java

I am making a small game in Java which involves guessing what the specified attribute belongs to (For example, what country does the city of Bogota belong to?).
My initial idea was to create a custom object which had the name as the first attribute and then the other attributes of the object (like shape, size, color) as the rest of the attributes. Then you would have a parent array containing these objects and to find the specific attribute of the object you would be guessing, it would pick a random number and select that number object from the array.
I am at the point where I am lost in how I should make this array. If I have 100 objects, it doesn't seem efficient to me to say, create an ArrayList of custom objects, then define each object individually with their attributes, then do list.add(object) 100 times.
Surely it does not make sense to create and store every object each time my main loop is run? Would there be a more effective way of putting each object into an array? Would there be a way to store each object into a file and my program retrieves the object and its information from a different file when I need to call it? I'm pretty new to this so maybe it doesn't make much sense. Any help would be greatly appreciated.

Options for storing huge tile map

I am creating a pseudo-turn-based online strategy browser game where many people play in the same world over a long period(months). For this, I want to have a map of 64000x64000 tiles = 4 billion tiles. I need about 6-10 bytes of data per tile, making up a total of around 30GB of data for storing the map.
Each tile should have properties such as type(water, grass, desert, mountain), resource(wood, cows, gold) and playerBuilt(road, building)
The client will only ever need access to about 100x100 tiles at the same time.
I have handled the map on client side under control. The problem that I'm faced with is how to store, retrieve and modify information from this map on the server side.
Required functionality:
Create, store, and modify 64000x64000 tilemap.
Show 100x100 part of the map to the client.
Make modifications on the map such as roads, buildings, and depleted resources.
What I have considered so far:
Procedural generation: Procedurally generating whichever part of the map is needed on the fly. Making sure that given the same seed, it always generates the same map. The main problem I have with this is that there will be modifications to the map during the game. Note: Less than 1% of the tiles would be modified during the game and it could be possible to store modifications with coordinates in an outside array. Loading them on top of the procedural generation.
Databases: Generating the map at the start of the game and storing it in a database. A friend advised me against this for such a huge tile map and told me that I'd probably want to store it in memory instead.
Keeping it all in memory on the server side: Keeping it in memory in a data structure. Seems like a nice way to do it if the map was smaller but for 4 billion tiles that would be a lot to keep in memory.
I was planning on using java+mysql for back-end for this project. I'm still in early phases and open to change technology if needed.
My question is: Which of the three approaches above seem viable and/or are there other ways to do it which I have not considered?
Depends on:
how much RAM you got (or player/users got)
Is most of the tile map empty (sparse) ? Opposite is dense.
Is there a default terrain (like empty or water ?)
If sparse, use a hashmap instead of a 2D array.
If dense it will be much more challenging and you may need to use a database or some special data structures + cache.
You may detect hot zones and keep them in memory for a while, dead zones (no players there, no activity ...) can be stored in the database and read on demand.
You may also load data in several passes: first just the terrain, then other objects... each layer could be stored in a different way. For example the terrain could be perlin noise generated + another layer which can be modified.

Data structure to use for platformer game map

I'm writing a 2D platformer game using Java for Android.
Currently I have all game entities (except for the avatar, stored in an array of array of "Entity"
Entity[][]
Whenever I need to check something - such as what I'm going to draw on screen or for collision detection - I simply grab a small radius of items around the avatar and do whatever using a system of inheritance and polymorphism.
The problem is that this means I can only put one entity in a particular grid coordinate. This used to be quite okay for the most part - but now I have moving items (such as enemies or moving blocks) - which when they collide, end up deleting one another basically they get overwritten.
So what data structure should I use? I was thinking of something like
ArrayList<Entity>[][]
But that's going to be very expensive, and a waste of memory since duplicate items are the exception not the rule.
I was also considering separating the moving items into their own ArrayList, and looping through all of them, but that's an ugly solution.
So any ideas on what I could use? I want something which is pretty fast, but not too memory intensive.
A two-dimensional array for storing entities wastes memory (most blocks won't be occupied) and doesn't allow overlapping entities, as you noticed. With few entities, a simple ArrayList (no visible arrays involved) is probably going to be fully adequate.
If you have lots of entities, you could consider a quad-tree data structure or some other spatial indexing structure (check http://en.wikipedia.org/wiki/Spatial_index).
As a simpler and more "traditional" approach, how about keeping a two-layer view of your entities-- an ArrayList of all active (visible) ones, and an ArrayList of all inactive (too-far-away-to-care) ones? If performance matters, you can disable collision detection on the latter and perform little or no AI/maintenance on them.
What about you keep the entities as:
Entity[][]
And assuming you look-up entities, like:
Entity e = entities[i][j]
But keeping you enemies in a:
Map<String,List<Entity>> map
Where the key is the String made by concatenating i and j with a random middle character (e.g. "1.15" if i==1 and j==15). Now you keep the fast look-up of entities and also have fast look-up (albeit not as fast as an array) of enemies without the excess empty space.

Best way to represent (store ) area of a region

Well I am developing a simulation where agents in tribes (groups) can compete for how much of the area they own in a map of a fixed size.(The map is 2D map). They compete through fighting with each other in groups and the winning group gets the owned by the other group.This simulation is written in java.
The main issue I am trying to get some ideas towards is how should I store how much of the map each group owns.At first I though of just using an instance of Dimension where it then each time a group of agents wins over an area it adds it to the Dimension.The issue however is that areas owned can be in any position in the map with gaps in-between areas as it can be seen in the picture below.(Sorry for the poor picture,was trying to draw the problem using gimp)
NOTE: The rectangles of different colours represent the areas owned by the agents whereas the circles in violet are the agents themselves.
Now another idea was to have an ArrayList of Dimension types holding all the area owned of the agents of specific tribe.
But I am thinking whether there is a better way to do this.
There's a one-to-many (technically a one-to-zero-or-many) relationship between each tribe and how many regions are owned by that tribe. You have 3 basic choices for representing any one-to-many relationship.
1. You can place a reference in each child to the parent
2. You can manage a list of children in each parent
3. You can manage a list of parent-child pairings separately
The question I'd ask yourself is what results are you trying to derive from your simulation? Start from there and work backwards. For example, if your goal is a report by tribe of what is owned, then it may be most efficient to manage a list of regions inside each tribe object. If your goal is a report of regions showing who owns each, then it may be simpler to reference the owning tribe inside the object of each region. It may also be reasonable to manage in both places if you're supporting multiple views.
The third option, managing the relationships outside of the objects themselves, is most useful for times when many-to-many relationships might also be supported, like allowing players to be members of multiple tribes.
if i can suggest you something, when i was working while ago on some graph related stuff, in one of papers i found idea how to convert graph to a map, sorry i cant remember title of that paper
but what i remember, is they were using voronoi diagram,
in this case you can store your map data as set of objects which holds coordinates of action performed by agent and team id
you can also introduces sales vlume, which might be used for resolving conflicts for ovrlaping area (90% sales in region belongs to A, so 90% region will be given to them as well)

Optimal data structures for a tile-based RPG In java

The game is tile-based, but the tiles are really only for terrain and path-finding purposes. Sprite movement is free-form (ie, the player can be half way through a tile).
The maps in this game are very large. At normal zoom tiles are 32*32 pixels, and maps sizes can be up 2000x2000 or larger (4 million tiles!). Currently, a map is an array of tiles, and the tile object looks like this:
public class Tile {
public byte groundType;
public byte featureType;
public ArrayList<Sprite> entities;
public Tile () {
groundType = -1;
featureType = -1;
entities = null;
}
}
Where groundType is the texture, and featureType is a map object that takes up an entire tile (such as a tree, or large rock). These types of features are quite common so I have opted to make them their own variable rather than store them in entities, which is a list of objects on the tile (items, creatures, etc). Entities are saved to a tile for performance reasons.
The problem I am having is that if entities is not initialized to null, Java runs out of heap space. But setting it to null and only initializing when something moves into the tile seems to me a bad solution. If a creature were moving across otherwise empty tiles, lists would constantly need to be initialized and set back to null. Is this not poor memory management? What would be a better solution?
Have a single structure (start with an ArrayList) containing all of
your sprites.
If you're running a game loop and cycling through the sprites list,
say, once very 30-50 seconds and there are up to, say, 200 sprites,
you shouldn't have a performance hit from this structure per se.
Later on, for other purposes such as collision detection, you may
well need to revise the structure of just a single ArrayList. I would suggest
starting with the simple, noddyish solution to get your game logic sorted out, then optimise as necessary.
For your tiles, if space is a concern, then rather than having a special "Tile" object, consider packing the
information for each tile into a single byte, short or int if not
actually much specific information per tile is required. Remember
that every Java object you create has some overhead (for the sake of
argument, let's say in the order of 24-32 bytes per object depending
on VM and 32 vs 64 bit processor). An array of 4 million bytes is
"only" 4MB, 4 million ints "only" 16MB.
Another solution for your tile data, if packing a tile's specification into a single primitive isn't practical, is to declare a large ByteBuffer, with each tile's data stored at index (say) tileNo * 16 if each tile needs 16 bytes of data.
You could consider not actually storing all of the tiles in memory. Whether this is appropriate will depend on your game. I would say that 2000x2000 is still within the realm that you could sensibly keep the whole data in memory if each individual tile does not need much data.
If you're thinking the last couple of points defeat the whole point of an object-oriented language, then yes you're right. So you need to weigh up at what point you opt for the "extreme" solution to save heap space, or whether you can "get away with" using more memory for the sake of a better programming paradigm. Having an object per tile might use (say) in the order of a few hundred megabytes. In some environments that will be ridiculous. In others where several gigabytes are available, it might be entirely reasonable.

Categories

Resources