Bleached InuYasha Galaxy

Forums for the MUD (biyg.org 1801)
It is currently March 28th, 2024, 6:42 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Code I've done
PostPosted: November 10th, 2004, 3:01 pm 
Offline
MUD Owner
User avatar

Joined: November 7th, 2004, 7:16 pm
Posts: 982
Location: Saratoga, NY
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.


Top
 Profile  
 
 Post subject:
PostPosted: November 11th, 2004, 1:24 pm 
Offline
Fellow player
User avatar

Joined: November 7th, 2004, 7:47 pm
Posts: 28
Location: Lousiana
I'm Sure. If I wasn't a idiot when it comes to lang, Hehe Wish i could write that


Top
 Profile  
 
 Post subject:
PostPosted: December 22nd, 2004, 8:37 pm 
:D :) :( :o :shock: :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:


Top
  
 
 Post subject:
PostPosted: June 7th, 2005, 4:33 am 
Offline
Fellow player

Joined: April 28th, 2005, 8:38 pm
Posts: 37
Location: MI
What was that last post for?

_________________
"Almighty don't like no body TVL


Top
 Profile  
 
 Post subject:
PostPosted: July 22nd, 2005, 4:03 pm 
Offline
Neo adventurer

Joined: July 22nd, 2005, 3:46 pm
Posts: 4
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 :)


Top
 Profile  
 
 Post subject:
PostPosted: July 22nd, 2005, 7:59 pm 
Offline
MUD Owner
User avatar

Joined: November 7th, 2004, 7:16 pm
Posts: 982
Location: Saratoga, NY
Hm interesting. I think I was following how SocketMUD did a file write... wonder if they have it like that too.

_________________
-Zeno McDohl


Top
 Profile  
 
 Post subject:
PostPosted: July 22nd, 2005, 8:11 pm 
Offline
Neo adventurer

Joined: July 22nd, 2005, 3:46 pm
Posts: 4
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.


Top
 Profile  
 
 Post subject:
PostPosted: August 5th, 2005, 12:58 pm 
Offline
Fellow player
User avatar

Joined: April 13th, 2005, 3:50 pm
Posts: 455
Location: Alvin, TX
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.

_________________
"Being right means you betray life. After all, common sense is dead."
-Alexander Leon


Top
 Profile  
 
 Post subject:
PostPosted: August 13th, 2005, 1:57 pm 
Offline
Neo adventurer

Joined: August 6th, 2005, 5:18 pm
Posts: 5
Wooow alot of code,I will never understand it


Top
 Profile  
 
 Post subject:
PostPosted: August 26th, 2005, 3:25 am 
Offline
Fellow player
User avatar

Joined: August 25th, 2005, 2:14 am
Posts: 42
Location: Veneta, OR
Pssh, that's easy. An idiot could see that those squiggly means that the squirrels are readying their artillery. Morons.

_________________
http://mudconnector.com/mud-bin/vote_ra ... sha+Galaxy
http://www.themudslide.com/affiliate_en ... l?a_id=117
http://mostpopularsites.net/muds/votein.php?ID=120


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group