[I6] Random Moves

How can I have an NPC move to a random direction? I could switch the valid exits of the current location, but I would rather like to have an array of the possible exits of the current location and have the NPC chose one at random so that he can move across the whole map. Something like
exit_list=possible_exits();
npc->move(exit_list(random(sizeof(exit_list))));
Know what I mean? Any help appreciated.

May I address this again? My NPC has a daemon where he prints out some random stuff, and I want him to move at random occasionally. How do I find out which exits his current location has?

I haven’t tested this, but something like this:

objectloop (dirobj in Compass) {
  dir = dirobj.door_dir;
  dest = curroom.dir;
  if (dest ofclass Object && dest has door) {
    dest = dest.door_to();
  }
  if (dest ofclass Object) {
    ! ... dest is the room dir-ward of curroom.
  }
} 

You can use this loop to build an array of valid exit destinations, and then randomly select among them.

Hm. That gave me some inspiration. Not having any clues about arrays, I tried it the very hard way:

[ random_move npc oldroom newroom counter dirs i diro; oldroom=parent(npc); if(oldroom.n_to) {diro=diro+oldroom.n_obj; dirs=dirs+oldroom.n_to; counter++;} if(oldroom.w_to) {diro=diro+oldroom.w_obj; dirs=dirs+oldroom.w_to; counter++;} if(oldroom.s_to) {diro=diro+oldroom.s_obj; dirs=dirs+oldroom.s_to; counter++;} if(oldroom.e_to) {diro=diro+oldroom.e_obj; dirs=dirs+oldroom.e_to; counter++;} if(counter){ i=random(counter)-1; newroom=diro-->i; if(player in oldroom) print (name)npc," walks off to the ",dirs-->i,".^"; if(player in newroom) print (name)npc," heads in from the ",dirs-->i,".^"; move npc to newroom; } ];

Ignore the spaghetti look and the lack of validation checks please. :wink: Works except one basic thing: When newroom is allocated, it’s not an object (debugs as “Routine 6907”). How can I get diro to be an array of objects? oldroom.x_to is the room I want, it’s just not properly added to diro which I want to be an array.

Under the hood everything in I6 is represented as a number. So, rather than the bits of code like dirs=dirs+oldroom.n_to; adding elements to dynamic arrays, they are actually summing up numeric encodings. Whenever this sum contains more than one term, there’s a good chance that you end up with a number that isn’t the encoding of anything sensible, and it will debug as gibberish like ``Routine 6907.’’

What you probably want to do is declare the arrays outside of the routine, for instance, Array dirs --> 4; ! an array with space for four elements, numbered zero to three and then use a statement like dirs-->counter=oldroom.n_to; to store to a particular element.

Chapeau! Finally it works… Thanks a lot!