Bleached InuYasha Galaxy
http://biyg.org/forums/

Code I've done
http://biyg.org/forums/viewtopic.php?f=6&t=10
Page 1 of 1

Author:  Zeno [ November 10th, 2004, 3:01 pm ]
Post subject:  Code I've done

Here's some code I've done, although it's not used with this MUD.

These functions handle reading, storing, and writing to a game data file. It stores global variables, such as how many times the game has been run.

In the main header file:
Define the game data file:
Code:
#define GAME_DATA_FILE     "../game.dat"             /* Stores global variables            */

Define the struct:
Code:
typedef struct  game_data     GAME_DATA;

Allow it to be used outside this header file:
Code:
extern  struct  game_data     gamedat;

The actual struct:
Code:
struct  game_data
{
  sh_int          gamestate;
  int             defaulthp;
  int             battlenum;
  sh_int          samechars;
  sh_int          joining;
};

Define functions used to load/save/check the file:
Code:
void  fread_gamedata           ( GAME_DATA *sys, FILE *fp );
bool  load_gamedata            ( GAME_DATA *sys );
void  save_gamedata            ( GAME_DATA game );


In save.c:
Read the game data file:
Code:
void fread_gamedata( GAME_DATA *sys, FILE *fp )
{
    char *word;
    bool done = FALSE, found;

  word = fread_word(fp);
  while (!done)
  {
    found = FALSE;
    switch (word[0])
    {
      case 'B':
        IREAD( "BattleNum", sys->battlenum );
        break;
      case 'D':
        IREAD( "DefaultHP", sys->defaulthp );
        break;
      case 'E':
        if (compares(word, "EOF")) {done = TRUE; found = TRUE; break;}
        break;
      case 'G':
        IREAD( "GameState", sys->gamestate );
        break;
      case 'J':
        IREAD( "Joining", sys->joining );
        break;
      case 'S':
        IREAD( "SameChars", sys->samechars );
        break;
      default:
         bug("Fread_gamedata: default case.", word );
         return;
    }
    if (!found)
    {
      bug("Fread_gamedata: unexpected '%s' in game_data.", word );
    }

    if (!done) word = fread_word(fp);
  }

}

Load the game data file:
Code:
bool load_gamedata( GAME_DATA *sys )
{
    FILE *fp;
    bool found;

    found = FALSE;

    if ( ( fp = fopen( GAME_DATA_FILE, "r" ) ) != NULL )
    {
        fread_gamedata( sys, fp );
        fclose( fp );
    }

    return found;
}

Save the game data file:
Code:
void save_gamedata( GAME_DATA game )
{
    FILE *fp;

    if ( ( fp = fopen( GAME_DATA_FILE, "w" ) ) == NULL )
    {
        bug( "save_gamedata: fopen" );
        perror( GAME_DATA_FILE );
    }
    else
    {
        fprintf( fp, "GameState    %d\n", game.gamestate );
        fprintf( fp, "DefaultHP    %d\n", game.defaulthp );
        fprintf( fp, "BattleNum    %d\n", game.battlenum );
        fprintf( fp, "SameChars    %d\n", game.samechars );
        fprintf( fp, "Joining      %d\n", game.joining   );
        fprintf( fp, "EOF" );

    }
    fclose( fp );
    return;
}


In socket.c:
Ref the struct to use:
Code:
GAME_DATA               gamedat;


Finally, load the damn data:
Code:
load_gamedata(&gamedat);


And done.

Author:  Drin-sama [ November 11th, 2004, 1:24 pm ]
Post subject: 

I'm Sure. If I wasn't a idiot when it comes to lang, Hehe Wish i could write that

Author:  Guest [ December 22nd, 2004, 8:37 pm ]
Post subject: 

:D :) :( :o :shock: :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:

Author:  Reeve [ June 7th, 2005, 4:33 am ]
Post subject: 

What was that last post for?

Author:  Bodean [ July 22nd, 2005, 4:03 pm ]
Post subject: 

Code:
void save_gamedata( GAME_DATA game )
{
    FILE *fp;

    if ( ( fp = fopen( GAME_DATA_FILE, "w" ) ) == NULL )
    {
        bug( "save_gamedata: fopen" );
        perror( GAME_DATA_FILE );
    }
    else
    {
        fprintf( fp, "GameState    %d\n", game.gamestate );
        fprintf( fp, "DefaultHP    %d\n", game.defaulthp );
        fprintf( fp, "BattleNum    %d\n", game.battlenum );
        fprintf( fp, "SameChars    %d\n", game.samechars );
        fprintf( fp, "Joining      %d\n", game.joining   );
        fprintf( fp, "EOF" );

    }
    fclose( fp );
    return;
}

The fclose(fp); should be inside of the else statement or you should return after the perror :) otherwise its trying to close a null file. just thought i would point that out :)

Author:  Zeno [ July 22nd, 2005, 7:59 pm ]
Post subject: 

Hm interesting. I think I was following how SocketMUD did a file write... wonder if they have it like that too.

Author:  Bodean [ July 22nd, 2005, 8:11 pm ]
Post subject: 

While I havent looked at SocketMUD in awhile I do recall it having quite alot of little issues that needed fixed, at least the one I looked at did.

Author:  Alexander [ August 5th, 2005, 12:58 pm ]
Post subject: 

Omfg... It's like... You Coders make your own language or something oO; Im amazed at all this... I mean really. Not one of those symbols... Heck words, letters, phrases ect did I ever understand. Idk how is it you guys do it, or where you learn this stuff... But damn, my confidence in alot of things just droped drasticaly. But in any case, I guess it's a 'born with thing' or something o.o Anyways, just wanted you to know how stupid I felt is all.

Author:  Dragrath [ August 13th, 2005, 1:57 pm ]
Post subject: 

Wooow alot of code,I will never understand it

Author:  Reikan [ August 26th, 2005, 3:25 am ]
Post subject: 

Pssh, that's easy. An idiot could see that those squiggly means that the squirrels are readying their artillery. Morons.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/