Save/Load multi-dimensional byte array

So it has been a while since I have programmed… job,stress, depression, messy code etc tend to get in the way of my development process a hell of a lot.
But that aside, I’m back at it again.

I’ve spent the last week or so re-writing the code for my existing game project so as to make things easier for me to work on.
I’ve also taken a bit more time out to do some more organised documentation and logging of progress which has really helped.

Currently, my goal is to learn how to save game data to a protected file, or save encoded data to a file so that it cannot be altered by other’s without loading the file in the game editor or the game itself.

To begin with, I have a procedurally generated game world using diamond-square algorithm, these procedural worlds will be created when a player creates a custom game with a randomized map generated based on a seed value that is determined by a bunch of slider values and check boxes. I want the procedural world generation to be very extensive, but not right now, I have to get the basics down first.

So at the moment, once I click my little world generation button, it stores a set of numbers (that represent terrain tile types), into a multi-dimensional byte array.

Currently, I have managed to get the engine to create a multi-dimensional array with 530,887,671 tiles in it, and the game runs the same as if there were 500,000 tiles, as the renderer culls out what it can’t see on the immediate viewing area.

In my head, I know what needs to be done, but I just don’t know HOW to do it, what techniques to use.

Without being too specific on map data, I have just been looking at how to save and load data using libGDX and java.

I checked out the libgdx documentation on file handling:

I have come across things such as preferences for things like player stat saving.
http://www.badlogicgames.com/wordpress/?p=1585

I have come across file and data output streams for storing and loading map data.
http://developer.nokia.com/Community/Wiki/How_to_create_2d_tiled_array_binary_file_and_load_it_in_Java_ME

And I have also come across dermetfan’s video where he saves data using JSON and base64.

So basically I know what to do, but just don’t know how to do it, and am looking for direction, or something I can learn from, rather than a direct answer (although I tend to learn from those pretty well too :P).
Either I have been searching the wrong things on youtube and google, or I’m just having a durpey moment (I am not all that intelligent…or at least it feels that way).

Any help would be greatly appreciated. While I wait, I am going to keep exploring the interwebz for information :slight_smile:

Also, for those who don’t know or remember what I was/am working on, here is a youtube video:

nYEB2TrtViE

loading and saving multi dimensional arrays can thought of saving a single dimensional array multiple times. where the single dimensional array is saved as a sequence of tiles.

pesudeo code:


int size=1000;
byte[][][] tileData = new tileData[size][size][size];

// gen tile data here

for (int i = 0 ; i < size;i++)
   for (int j = 0 ; j < size;j++)   
      for (int k = 0 ; k < size;k++)   
          byte tile = tile[i][j][k]
          // write tile to file stream

to reading in the map all you need to is read tile instead of write.

if your tile data is a primitive then I suggest looking at java.io.DataOutputStream. And i suggest using a BufferedOutputStream to help speed up the file writing.

Hmmm…yeah…but it’s the “//write tile to file stream” part that I’m trying to work out…the rest is pretty much common sense.

Reading through the File Handling documentation on the libGdx wiki and actually READING it, seems to be helping clear things up, not exactly scary.

But I still wonder what other people use to store their 2d terrain data, game object data and so on, so forth.

I am not familiar with libGdx but if it provides io streams then the following pure java snippet can be used as a guide:



int size=1000;
byte[][][] tileData = new tileData[size][size][size];

// gen tile data here

File file = new File("tile.data");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);

try
{
   for (int i = 0 ; i < size;i++)
   {
      for (int j = 0 ; j < size;j++)   
      {
             bos.write(tile[i][j]);
      }
   }
}
finally
{
   bos.close();
}

So after a bit of fiddling around over the past 2 days (I haven’t had much time to program despite 2 days off work :frowning: ). I ended up getting saving and loading working on a base level.

I figured I would just share the code for other newbies like myself who may come across this thread.

Bare in mind, there is still a hell of a lot more complexity to what I need to do, but this is one little tiny step forward, and gives me a basis to work from.



// THIS METHOD IS USED FOR SAVING THE MAP DATA TO A FILE.
// (Note: If there is an existing file of the same name of the file we want to write to, we must delete it first, otherwise the data will be appended, which is not a good thing :P)	

public void saveMapData(){

		FileHandle file = Gdx.files.local("savetesting/myMap.txt");
		
		boolean isFileAvailable = Gdx.files.local("savetesting/myMap.txt").exists();
		if(isFileAvailable){
			System.out.println("DELETING FILE");
			file.delete();
		}
	
		try{
			for(int x = 0; x < map.length; x++){
				for(int y = 0; y < map[0].length; y++){
					
					byte[] temp = {map[x][y]}; //I feel as though these 2 lines could be done differently, would it be better if 'temp' was just a byte? If so, I'm not sure how to write a byte value to the file without it being a byte[].
					file.writeBytes(temp, true);

				}
			}
		}catch(Exception e){
			System.out.println("Cannot write to file");
		}
	}

// THIS METHOD IS USED TO LOAD THE MAP DATA
// Note: Right now, it's just reading the data and displaying the byte data as integers within the console, we aren't storing the data into a new map[][] yet, nor are we controlling how the data is to be read into the map[][].
	
	public void loadMapData(){
		FileHandle file = Gdx.files.local("savetesting/myMap.txt");
		byte[] stuff = file.readBytes();
				
		try{
		for (int x = 0; x < stuff.length; x++){
			System.out.println(stuff[x]);
		}
		
		}catch(Exception e){
			System.out.println("There was a problem reading the file");
		}
	}