View Single Post
Old 05-21-2003, 08:54 AM   #4
jornel
Member
 
Join Date: Sep 2002
Location: Canada
Posts: 73
jornel is on a distinguished road
If you're going the TTL route for your tracks,  you can save oodles of memory by only using dynamically assigned tracks as players move around your world.

Something like this (I don't remember the memory macro names off the top of my head, but you will).

[code]
void playerleft(player, room, direction)
{
struct track *p,*track,tracknext;

 /* first clean out expired or trampled over tracks */
 for(p=NULL,track=room->firsttrack;track;track=tracknext)
 {
    tracknext=track->next;
    if(track->dir = direction || ttl_expired(track))
    {/* these tracks should go */
       if(p)
         p->next=tracknext;
       else
         room->firsttrack=tracknext;
       STRFREE(track->playername);
       DISPOSE(track);
    }
    else
    {/* these tracks don't disappear yet.  keep them. */
      p=track;
    }
 }

 /* fill out new track struct for current player */
 track=ALLOCATE(struct track);
 track->dir = direction;
 track->playername = STRALLOC(player->name);
 track->ttl = make_ttl( TRACK_TTL );

/* add fresh tracks to room */
 track->next=room->firsttrack;
 room->firsttrack=track;
}

[/quote]

Example: Joe just left west.

We scan the list of current tracks in the room, looking for any that went out the west exit.  Dispose those since Joe would have trampled over them.  We also dispose any which have  expired TTL.

We allocate a new track structure.  Fill it out with whatever info you want, plus TTL

Finally add Joe's tracks to the room.  We are done.


This algorithm doesn't need a mostly empty 4*8*10 array per room, just a hook (firsttrack) to hang the first set of tracks in the room.

Rooms that don't get visited often may wind up with a lot of expired tracks that never get cleared unless someone visits the room.  

A nice low-CPU lazy way to clean these up costs you a single integer:  next_vnum_to_clean.

Once per update:

[code]
for(i=0;i<MAX_ROOMCLEANING_PER_UPDATE;i++)
{
if (next_vnum_to_clean IS a valid room number)
{
   scan list of all tracks in the room.
   dispose those that have expired TTL.
}
if(++next_vnum_to_clean>=MAX_ROOM_VNUM)
  next_vnum_to_clean = 0; /* or 1. it matters little */
}

[/quote]

This will guarantee you will not have tracks older than

oldest track age = ( MAX_ROOM_VNUM / MAX_ROOMCLEANING_PER_UPDATE ) * update_period

and it only costs the CPU checking at most MAX_ROOM_CLEANING_PER_UPDATE rooms per update.


Hope this helps.
jornel is offline   Reply With Quote