Scanning

I haven’t posted for a while, but then again, I haven’t been doing a lot of coding recently.

I am working from TBAMud 3.56, and as you may see from the TBA homepage, the current code is on version 3.61.  I’ve done some fairly extensive changes to the code, and I haven’t been using version control (bad coder, BAD!), so updates aren’t exactly easy.

I was perusing the changelog recently and noticed that version 3.61 (or somewhere around there) had added a “scan” command.  Scan is a fairly ubiquitous mud command, and offers the ability for players to look around in adjacent rooms and see what mobs are there.

I didn’t like the old version, so I decided that I would change it.  Feel free to swipe.

Here’s the version copied directly from tba 3.61:

ACMD(do_scan)
{
 int door;
 char buf[MAX_STRING_LENGTH];

 *buf = '';

 if (IS_AFFECTED(ch, AFF_BLIND)) {
 send_to_char(ch, "You can't see a damned thing, you're blind!\r\n");
 return;
 }
 /* may want to add more restrictions here, too */
 send_to_char(ch, "You quickly scan the area.\r\n");
 for (door = 0; door < NUM_OF_DIRS - 2; door++) /* don't scan up/down */
 if (EXIT(ch, door) && EXIT(ch, door)->to_room != NOWHERE &&
 !IS_SET(EXIT(ch, door)->exit_info, EX_CLOSED)
 && !IS_DARK(EXIT(ch, door)->to_room)) {
 if (world[EXIT(ch, door)->to_room].people) {
 list_scanned_chars(world[EXIT(ch, door)->to_room].people, ch, 0, door);
 } else if (_2ND_EXIT(ch, door) && _2ND_EXIT(ch, door)->to_room !=
 NOWHERE && !IS_SET(_2ND_EXIT(ch, door)->exit_info, EX_CLOSED)
 && !IS_DARK(_2ND_EXIT(ch, door)->to_room)) {
 /* check the second room away */
 if (world[_2ND_EXIT(ch, door)->to_room].people) {
 list_scanned_chars(world[_2ND_EXIT(ch, door)->to_room].people, ch, 1, door);
 } else if (_3RD_EXIT(ch, door) && _3RD_EXIT(ch, door)->to_room !=
 NOWHERE && !IS_SET(_3RD_EXIT(ch, door)->exit_info, EX_CLOSED)
 && !IS_DARK(_3RD_EXIT(ch, door)->to_room)) {
 /* check the third room */
 if (world[_3RD_EXIT(ch, door)->to_room].people) {
 list_scanned_chars(world[_3RD_EXIT(ch, door)->to_room].people, ch, 2,
door);
 }

 }
 }
 }                
}

Look complicated?  It’s not terribly complicated.  But, IMO, it’s inelegant.  You’ll also notice that it adds new shortcuts (_2ND_EXIT(ch, door)) which will have to be added to utils.h in order for the code to work properly.  These shortcuts aren’t useful outside of this code snippet, and therefore only serve to reduce coding in this section.  I’d rather have shortcuts that are useful throughout the mud (for example, IN_ROOM(ch) is a great function, it’s used throughout the mud and, saves a lot of typing, and clearly indicates what it does), instead of ones limited to a single function.

The code basically checks a room 1 away, looks for people, then checks the next room, looks for people, and finally checks the third room.  But what if you want a 4th room to be scanned?  Then you have to add a _4th_exit shortcut.

My version follows.  I removed the IF statements to check each successive room and instead added a FOR loop which counts the distance from the character’s room.  So if a coder wants to add a skill that increases the MAXRANGE of scan, they can do so.  Or if you want to change the scan distance to 4 rooms, 5 rooms, etc.

ACMD(do_scan)
{
 int door;
 char buf[MAX_STRING_LENGTH];

 int range;
 int maxrange = 3;

 room_rnum scanned_room = IN_ROOM(ch);
 
 if (IS_AFFECTED(ch, AFF_BLIND)) {
 send_to_char(ch, "You can't see a damned thing, you're blind!\r\n");
 return;
 }

 for (door = 0; door < NUM_OF_DIRS; door++) {
 for (range = 1; range<= maxrange; range++) {
 if (world[scanned_room].dir_option[door] && !IS_SET(world[scanned_room].dir_option[door]->exit_info, EX_CLOSED)) {
 scanned_room = world[scanned_room].dir_option[door]->to_room;
 if (world[scanned_room].people)
 list_scanned_chars(world[scanned_room].people, ch, range - 1, door);
 }                  // end of if
 else
 break;
 }                    // end of range
 scanned_room = IN_ROOM(ch);
 }                      // end of directions
} // end of do_scan

Note that I have nothing against the coders at TBA, but their version looks like a quick and dirty solution.  I have some pretty nasty bits in my code as well (ok, REALLY nasty).  Also, I have a feeling that this won’t be any more efficient, in CPU usage, but it looks neater.

Leave a comment