saving a game

Posted By: Joozey

saving a game - 08/02/07 15:46

I want to save a game, but I can not do this with save_game. save_game can not be used among other instances of the game, and I can't load a saved file when I editted the code. So what's the best way to make a save file? I suppose I have to write into a text file then. But this is very hard to do.

The case is that I have an editor where you can place entities, but ofcourse I want to save your home cooked level and load it another time when you play it. Is there really no other way than writing to a text file?

Thanx in advance.
Posted By: Uhrwerk

Re: saving a game - 08/02/07 18:52

I'm currently doin it that way. It's very easy if you have your data structured. You don't rely here on text files. You can save your data in any format.
Posted By: Joozey

Re: saving a game - 08/02/07 19:27

Hi Uhrwerk, thanx for replying.

Rely in what way exactly?
what do you mean 'any format'? its just text that can be opened in a notepad so it can be read by everyone.

The difficult thing is that I can't just place the pointer wherever I want, or search for a word in the file and place the pointer there, I have to load the whole thing and then unwrap it all with clip and trunc strings. It always get very messy.

Greetz.
Posted By: Uhrwerk

Re: saving a game - 08/02/07 19:50

It's correct that you always open files in textmode. However, the data you write can be arbitrary. If you want to save a number between 0 and 255 you can use file_asc_write and nobody will be able to read that. If you want to store an integer, you can use file_asc_write four times and shift the bits during use of file_asc_write.

What do you mean by "you can't place the pointer whereever you want"? If you are refering to the file pointer you can manipulate it with file_seek. From time to time file_find comes even in very handy.

If you'd give me a more detailed description of what you want to save I could throw my fifty cents in. I'm still note sure why you want to clip and trunc strings...
Posted By: Joozey

Re: saving a game - 08/02/07 20:14

aight here it comes:

I have a game called gravball (as visible in the showcase 1 thread). In the game you have an editor. In the editor you can compose your own level with several objects such as planets, stars, launch platforms, exitgates, electrons etc etc.

If we exit the editor, you will get a question if you want to save the level you made. You press 'yes' and the saveGame() function will be called. So that's where Im stuck at.

The gameSave will search throughout all the entities searching for objects with the flag my.saveable set to on:

Code:

define SAVEABLE, FLAG2;
[...]

function setEnergyDot(&pos) {
if (!me) {return;}

you = ent_create ("electron1.tga", vector (pos[0], pos[1], pos[2]), energyDot);

[...]
you.NAME = "energyDot";
you.SAVEABLE = ON;
you.FACING = ON;
you.LIGHT = ON;
[...]

}



So now that we have found an entity out of the few that have to be saved, we have to store it. So far my idea was to put it in a text file like this:

Code:

function saveGame(name) {
filehandle = file_open_write(name);

you = ent_next (null);

while (you) {

if (you.saveable == off) {
you = ent_next (you);
continue;
}

//name
file_str_write (filehandle, "ent(");
file_str_write (filehandle, you.NAME);

//position
file_str_write (filehandle, ",");
file_var_write (filehandle, you.x);
file_str_write (filehandle, ",");
file_var_write (filehandle, you.y);
file_str_write (filehandle, ")");

wait(1);
}

file_close (filehandle);
}



This will probably result in a textfile with the content:

ENT(energyDot,50,22)

Now the file is full of those saved entities, which would supposedly look like this:

ENT(energyDot,50,22)ENT(energyDot,55,28)ENT(planet,10,72)ENT(star,500,632)ENT(asteroid,150,433)ENT(launchpad,214,311)...etc...etc...

But to load this, we need to get the whole string out of the file again, search for the word "ENT(", clip the string from there, search for the closing ")", truncate until there, and you have the name, x and y position basically retrieved. Some more truncating and clipping and you're done. But this is alot of work, and not such a great method to use. So I was hoping it could be done in a different way.
Posted By: Uhrwerk

Re: saving a game - 08/02/07 21:10

Your save game file only consists of these objects? Then it's pretty easy. You can do it this way:

Code:

function saveEntity(fileHandle, ent)
{
you = ent;
file_str_write(fileHandle,your.name);
file_str_write(fileHandle,delimit_str);
file_var_write(fileHandle,your.x);
file_var_write(fileHandle,your.y);
}



Now the loading function is the same, only backwards. You don't need to parse through any strings. The loop you are using with ent_next() is still the same as in your code.
Posted By: Joozey

Re: saving a game - 08/03/07 00:29

ok way more clever solution than I had in mind.
And hell yeah, it's working

Code:

exitgate,313.477 ,438.477 ,
planet,29.297 ,-423.828 ,
planet,-228.516 ,240.234 ,
star,-258.789 ,-419.922 ,
star,329.102 ,-151.367 ,
star,-136.719 ,424.805 ,
launchpad,124.023 ,412.109 ,
launchpad,-113.281 ,137.695 ,
launchpad,5.859 ,-97.656 ,
launchpad,166.992 ,-263.672 ,
energyDot,-153.320 ,-51.758 ,
energyDot,14.648 ,285.156 ,
energyDot,65.430 ,128.906 ,
energyDot,214.844 ,53.711 ,
energyDot,213.867 ,300.781 ,



Theres no really need to encrypt this file but I'd like to know how encrypting is possible... and that with the use of 4 times file_asc_write and bit shifting is a little beyond my mind. I'd appreciate it if you could explain in more details.

Either way thanx alot for helping me out
Greetz
Posted By: Uhrwerk

Re: saving a game - 08/03/07 01:11

If you want to encrypt your content that's not really easy. The easy way is x-oring (which prevents a user from directly seeing the content, but is in no way secure), the hard way would be DES for example. Btw. I think the advantages of text files are underestimated. This way you can do quick changes to the files, easily import and export the data and it is really usefull for debugging purposes. And I am talking out of experience here. (You don't need to write the delimit_str between the vars, just at the end of the string.)


Writing a var to a file, that cannot be directly read could be acchieved this way:
Code:

function fileVarWrite(fileHandle, variable)
{
file_asc_write(fileHandle,(variable >> 14) & 255);
file_asc_write(fileHandle,(variable >> 6) & 255);
file_asc_write(fileHandle,(variable << 2) & 255);
file_asc_write(fileHandle,(variable >> 10) & 255);
}
function fileVarRead(fileHandle)
{
var ret;
ret = 0;
ret = ret | (file_asc_read(fileHandle) << 14);
ret = ret | (file_asc_read(fileHandle) << 6);
ret = ret | (file_asc_read(fileHandle) >> 2);
ret = ret | (file_asc_read(fileHandle) >> 10);
return(ret);
}


I know this can be coded much more efficient. I just wrote it this way to make it more demonstrative. Maybe it works exactly this way, maybe you have to do minor adjustments. I have not tested it, just proven it correct... Using this method every var needs exactly 32 bit, the same consumption as in random access memory and the cipher cannot be read directly. I hope this helps...
Posted By: Joozey

Re: saving a game - 08/03/07 01:42

Thanx alot . With encrypting I just ment not direct readable, not entirely lock it down and prevent it from being opened at all , but in any case I do know now what to search for when I need to entirely encrypt the content .

This example makes it somewhat more clear now, Im gonna look into this, seems not really hard to understand once I figured what a single | and & does I never had to use those so I never gotten into these characters.
Greetz
Posted By: Uhrwerk

Re: saving a game - 08/03/07 14:57

& is the and operator. For example

10011100 &
01001101 =
00001100

i.e. Both bits of the two operands have to be set so that the resulting bit is also set.

| is the or operator. For Example

10011100 |
01001101 =
11011101

i.e. If any or both bits of the two operands is set, then the resulting bit is set too.
© 2024 lite-C Forums