> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of KJM
> Sent: Wednesday, September 28, 2005 11:23 AM
> To: [EMAIL PROTECTED]
> Cc: [email protected]
> Subject: RE: Question about a function to track stats.
> 
> Ok got it.  It now reads
> 
> void mobstat_update (void)
> {
>      FILE *fp;
>      int vnum;
>      MOB_INDEX_DATA *pMobIndex;
>      CHAR_DATA *mob;
>      int nMatch;
>      char buf[MSL];
> 
>     fclose(fpReserve);
> 
>     fp = fopen("../data/mobstat.txt","w");
> 
>     nMatch = 0;
>     for (vnum = 0; nMatch < top_mob_index; vnum++)
>     if ((pMobIndex = get_mob_index(vnum)) != NULL)
>     {
>       nMatch++;
>       if ( ( pMobIndex = get_mob_index( vnum ) ) != NULL )
>       {

This is a very inefficient way of looping, since get_mob_index has to run
through the hash table for every mob you're looking up (which happens to be
every mob in your mud). This is what you should be doing to save CPU:
        for( iHash = 0; iHash < MAX_KEY_HASH; iHash++ )
        {
                for ( pMobIndex  = mob_index_hash[iHash];
                          pMobIndex != NULL;
                          pMobIndex  = pMobIndex->next )
                {
Especially since you seem to have been calling get_mob_index twice for some
reason.

>        if pMobIndex->kills >= 3 || pMobIndex->killed >= 3)
>        {
>         sprintf(buf,"%5d %5d %5d\n", pMobIndex->vnum, 
> pMobIndex->kills,
> pMobIndex->killed);
>         fprintf( fp, buf );
>        }
>       }
>     }
>      fprintf(fp,"-1");
>      fclose(fp);
>      fpReserve = fopen( NULL_FILE, "r" );
>      return;
> }
> 
> Still doesn't tell me why the stats are not loading onto the 
> index....but a big help in the memory department.
> 

So for the not loading, I'm presuming that /nothing/ is loading /at all/?
One thing I would suggest doing, just for safety's sake is this:

>        vnum = fread_number( fp );
>  
>        if ( vnum == -1 )
>        {
>         fclose( fp );
>         return;
>        }
>  
> 
>        pMobIndex = get_mob_index(vnum);
>        pMobIndex->killed = fread_number(fp);
>        pMobIndex->kills = fread_number(fp);

Replace the above 3 lines with:

       pMobIndex = get_mob_index(vnum);
        if ( pMobIndex == NULL )
        {
                bug( "Invalid VNUM in mobstat.txt!" );
                fread_to_eol(fp);
                continue;
        }
       pMobIndex->killed = fread_number(fp);
       pMobIndex->kills = fread_number(fp);

>       }
>  
>       fclose(fp);
>       fpReserve = fopen( NULL_FILE, "r" );
>       return;
> }

Hope this inches you closer. Also, some good GDB runs would help you out as
well, since your file is only 3 entries long, dropping a break in
load_mobstats() and stepping through it would help solve the problem as
well.

Ammaross Danan
Realms of the Forgotten
www.rotf.net:5000

Reply via email to