Re: Newbie question--next_in_room

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 09/19/97

On Fri, 19 Sep 1997, Justin wrote:

-+>   for (vict = world[ch->in_room].people; vict; vict = next_v) {
-+>        next_v = vict->next_in_room;
-+>        if (vict && (vict->in_room == ch->in_room)) {
-+>         /* put your spell on vict here */
-+Now, isn't this going to go through each and every char/npc in room except
-+the caster?  I need it to just hit _one_ character/npc, at random, and
-+then stop.  Like two mages fighting, one has previously cast "reflect
-+electricity", and the other casts "call lightning".  It's going to bounce
-+off the first mage's shield and possibly hit something else in the room,
-+but not nesseccarily.  Will this work?  Looks like I need to add some
-+checks for ch=vict...

Actually, this hits the caster, too.  But if you just want it to
pick one person at random:

void get_vict_room_rand(struct char_data *ch, struct char_data *def)
  struct char_data *vict, *next_v;

  for (vict=world[ch->in_room]; vict; vict = next_v) {
    next_v = vict->next_in_room;
    if (vict != ch && !random(0,3)) /* 25% percent chance of being chosen */

  if (!vict) /* we didn't choose anyone in the room */
    vict = def; /* reflect it back at the unfortunate caster */
  return (vict);

So, now, let's say in our reflect code, we have 'caster' as the caster
of the spell; and 'target' as the targeted caster (who has reflect cast on
them); and 'victim' is the random chosen person.  We then call,

  vict = get_char_room_rand(target, caster);

It looks through the list of people *once* and if no-one is chosen, it
automatically defaults to the 'caster' (second argument passed).  No
check is made in the function for the 'def' (second argument) to be
NULL, but since all it does is return the value, you can do that check
in the actual code.

I don't suggest looping until you find someone, because there's always
the possibility of everyone getting really lucky, or a bad random number
generation problem causing either a lot of lag, or an infinite loop.
So, just go through once, and if we don't have anyone else to reflect it
back at; blast the caster...:) [btw, the caster might get blasted a bit
too much with this current setup, so you might want to think about
changing how probable it is for a random target to be selected; at
present, a person has a 25% chance of being chosen]

Daniel Koepke -:- -:-  [Shadowlord/Nether]

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

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