Re: Checking to see how meny mobs are in a zone and in a room

From: Johan Eenfeldt (d95joe@csd.uu.se)
Date: 04/10/96


On Tue, 9 Apr 1996, Ben Leibig wrote:

> So do you know of a  better way to do it?  I would like hard code.

Hrm... Not trying to flame you or anything, but you would learn a LOT more
by trying to code it yourself - and ask if you get stuck.

But to return to the question, what do you think?

The code above will, when the real_room() is removed, do what you want.
But it will go through each room in the entire world and check if it
belongs to the zone you want to check. That does seem a little wasteful...

If the world array is sorted as in the standard mud the rooms of a zone
are all together. Once you have reached the end of the zone you're not
really interested in the rest.

Or "hard code":

int num_mob_zone(int mvnum, int zone)
{
  int count = 0;
  int i;

  for (i=0; i <= top_of_world; i++)
    if(world[i].zone == zone) {
      for (; world[i].zone == zone && i <= top_of_world; i++)
        count += num_mob_room(mvnum, i);
      break;
    }

  return count;
}

If you really want to optimize the code, and have a quite large world as
well as a bit of time on your hand, you might to use binary search to find
the beginning of the zone to search too.

Or "hard code":

int num_mob_zone(int mvnum, int zone)
{
  int count = 0;
  int bot, top, i = 0;

  /* If the zone to search is not the first in the world.
  ** Everything becomes easier if we don't have to thing about this later.
  */
  if ( world[0]->zone != zone ) {
    bot = 0;
    top = top_of_world;

    for (;;) {
      i = (bot + top) >> 1;

      /* Found the beginning of the zone */
      if ((world + i)->zone == zone && (world + i - 1)->zone < zone)
        break;
      /* Zone didn't exist */
      if (bot >= top)
        return 0;
      /* Zone beginning is either in the first or the second half */
      if ((world + i)->zone >= zone)
        top = i - 1;
      else
        bot = i + 1;
    }
  }

  for (; i <= top_of_world; i++)
    if(world[i].zone == zone) {
      for (; world[i].zone == zone && i <= top_of_world; i++)
        count += num_mob_room(mvnum, i);
      break;
    }

  return count;
}

But this is probably a little overkill... ;-)

[All code written of the top of my head - there might be bugs]

/ Johan Eenfeldt
  Student, Computer Science Department @ the University of Uppsala
  d95joe@csd.uu.se



This archive was generated by hypermail 2b30 : 12/18/00 PST