SmaugWizard/Backup/
SmaugWizard/Backup/L/
SmaugWizard/Boards/
SmaugWizard/Building/
SmaugWizard/Corpses/
SmaugWizard/Councils/
SmaugWizard/Deity/
SmaugWizard/Gods/
SmaugWizard/MudProgs/
SmaugWizard/Player/L/
SmaugWizard/Src/
SmaugWizard/Src/res/
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |				*
 * -----------------------------------------------------------|   \\._.//	*
 * SmaugWiz (C) 1998 by Russ Pillsbury (Windows NT version)   |   (0...0)	*
 * -----------------------------------------------------------|    ).:.(	*
 * SMAUG (C) 1994, 1995, 1996 by Derek Snider                 |    {o o}	*
 * -----------------------------------------------------------|   / ' ' \	*
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |~'~.VxvxV.~'~*
 * Scryn, Swordbearer, Rennard, Tricops, and Gorog.           |				*
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik Staerfeldt, Tom Madsen, and Katja Nyboe.    *
 ****************************************************************************/


#include	"stdafx.h"
#include	"smaug.h"
#include	"skill.h"
#include	"objects.h"
#include	"rooms.h"
#include	"area.h"
#include	"SmaugWizDoc.h"
#include	"descriptor.h"
#include	"character.h"
#include	"sysdata.h"
#include	"Exits.h"

const	short	movement_loss	[SECT_MAX]	=
{
    1, 2, 2, 3, 4, 6, 4, 1, 6, 10, 6, 5, 7, 4
};

char *	const	dir_name	[]		=
{
    "north", "east", "south", "west", "up", "down",
    "northeast", "northwest", "southeast", "southwest", "somewhere"
};

const	int	trap_door	[]		=
{
    TRAP_N, TRAP_E, TRAP_S, TRAP_W, TRAP_U, TRAP_D,
    TRAP_NE, TRAP_NW, TRAP_SE, TRAP_SW
};


const	short	rev_dir		[]		=
{
    2, 3, 0, 1, 5, 4, 9, 8, 7, 6, 10
};


CRoomIndexData * vroom_hash [MAX_VROOMS];


CRoomIndexData *GetVRoomFromHash (int hash)
{
	ASSERT (hash >= 0 && hash < MAX_VROOMS);
	return vroom_hash [hash];
}

// Local functions.
BOOL	has_key (CCharacter *ch, int key);


char *	const		sect_names[SECT_MAX][2] =
{
    { "In a room","inside"	},	{ "In a city",	"cities"	},
    { "In a field","fields"	},	{ "In a forest","forests"	},
    { "hill",	"hills"		},	{ "On a mountain","mountains"	},
    { "In the water","waters"	},	{ "In rough water","waters"	},
    { "Underwater", "underwaters" },	{ "In the air",	"air"		},
    { "In a desert","deserts"	},	{ "Somewhere",	"unknown"	},
    { "ocean floor", "ocean floor" },	{ "underground", "underground"	}
};


const	int		sent_total[SECT_MAX]		=
{
    3, 5, 4, 4, 1, 1, 1, 1, 1, 2, 2, 25, 1, 1
};

char *	const		room_sents[SECT_MAX][25]	=
{
    {
	"rough hewn walls of granite with the occasional spider crawling around",
	"signs of a recent battle from the bloodstains on the floor",
	"a damp musty odour not unlike rotting vegetation"
    },

    {
	"the occasional stray digging through some garbage",
	"merchants trying to lure customers to their tents",
	"some street people putting on an interesting display of talent",
	"an argument between a customer and a merchant about the price of an item",
	"several shady figures talking down a dark alleyway"
    },

    {
	"sparce patches of brush and shrubs",
	"a small cluster of trees far off in the distance",
	"grassy fields as far as the eye can see",
	"a wide variety of weeds and wildflowers"
    },

    {
	"tall, dark evergreens prevent you from seeing very far",
	"many huge oak trees that look several hundred years old",
	"a solitary lonely weeping willow",
	"a patch of bright white birch trees slender and tall"
    },

    {
	"rolling hills lightly speckled with violet wildflowers"
    },

    {
	"the rocky mountain pass offers many hiding places"
    },

    {
	"the water is smooth as glass"
    },

    {
	"rough waves splash about angrily"
    },

    {
	"a small school of fish"
    },

    {
	"the land far below",
	"a misty haze of clouds"
    },

    {
	"sand as far as the eye can see",
	"an oasis far in the distance"
    },

    {
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",	"nothing unusual",	"nothing unusual",
	"nothing unusual",
    },

    { 
        "rocks and coral which litter the ocean floor."
    },

    {
	"a lengthy tunnel of rock."
    }

};

char *grab_word (char *argument, char *arg_first)
{
    char cEnd;
    short count;

    count = 0;

    while (isspace (*argument))
	argument++;

    cEnd = ' ';
    if (*argument == '\'' || *argument == '"')
	cEnd = *argument++;

    while (*argument != '\0' || ++count >= 255)
    {
	if (*argument == cEnd)
	{
	    argument++;
	    break;
	}
	*arg_first++ = *argument++;
    }
    *arg_first = '\0';

    while (isspace (*argument))
	argument++;

    return argument;
}

char *wordwrap (char *txt, short wrap)
{
    static char buf[MAX_STRING_LENGTH];
    char *bufp;

    buf[0] = '\0';
    bufp = buf;
    if (txt != NULL)
    {
        char line[MAX_STRING_LENGTH];
        char temp[MAX_STRING_LENGTH];
        char *ptr, *p;
        int ln, x;

	++bufp;
        line[0] = '\0';
        ptr = txt;
        while (*ptr)
        {
	  ptr = grab_word (ptr, temp);
          ln = strlen (line);  x = strlen (temp);
          if ((ln + x + 1) < wrap)
          {
	    if (line[ln-1] == '.')
              strcat (line, "  ");
	    else
              strcat (line, " ");
            strcat (line, temp);
            p = strchr (line, '\n');
            if (!p)
              p = strchr (line, '\r');
            if (p)
            {
                strcat (buf, line);
                line[0] = '\0';
            }
          }
          else
          {
            strcat (line, "\r\n");
            strcat (buf, line);
            strcpy (line, temp);
          }
        }
        if (line[0] != '\0')
            strcat (buf, line);
    }
    return bufp;
}


void decorate_room (CRoomIndexData *room)
{
    char buf[MAX_STRING_LENGTH];
    char buf2[MAX_STRING_LENGTH];
    int nRand;
    int iRand, len;
    int previous[8];
    int sector = room->sector_type;
    char *pre, *post;

    room->SetName (sect_names [sector][0]);
    buf [0] = '\0';
    nRand = number_range (1, UMIN (8, sent_total [sector]));

    for (iRand = 0; iRand < nRand; iRand++)
	previous[iRand] = -1;

    for (iRand = 0; iRand < nRand; iRand++)
    {
	while (previous[iRand] == -1)
	{
	    int x, z;

	    x = number_range (0, sent_total[sector]-1);

	    for (z = 0; z < iRand; z++)
		if (previous[z] == x)
		  break;

	    if (z < iRand)
		  continue;

	    previous[iRand] = x;

	    len = strlen (buf);
	    if (len == 0)
	    {
	      switch (number_range (1, 2 * (iRand == nRand -1) ? 1 : 2))
	      {
		case 1:	pre = "You notice ";	post = ".";	 break;
		case 2: pre = "You see ";	post = ".";	 break;
		case 3: pre = "You see ";	post = ", and "; break;
		case 4: pre = "You notice ";	post = ", and "; break;
	      }
	      sprintf (buf2, "%s%s%s", pre, room_sents[sector][x], post);
	    }
	    else
	    if (iRand != nRand -1)
	    {
	      if (buf[len-1] == '.')
	      switch (number_range (0, 3))
	      {
		case 0:	pre = "you notice ";	post = ".";	 break;
		case 1: pre = "you see ";	post = ", and "; break;
		case 2: pre = "you see ";	post = ".";	 break;
		case 3: pre = "over yonder ";	post = ", and "; break;
	      }
	      else
	      switch (number_range (0, 3))
	      {
		case 0:	pre = ""; post = ".";			break;
		case 1:	pre = ""; post = " not too far away.";	break;
		case 2: pre = ""; post = ", and ";		break;
		case 3: pre = ""; post = " nearby.";		break;
	      }
	      sprintf (buf2, "%s%s%s", pre, room_sents[sector][x], post);
	    }
	    else
		sprintf (buf2, "%s.", room_sents[sector][x]);
	    if (len > 5 && buf[len-1] == '.')
	    {
		strcat (buf, "  ");
		buf2[0] = UPPER (buf2[0]);
	    }
	    else
	    if (len == 0)
	        buf2[0] = UPPER (buf2[0]);
	    strcat (buf, buf2);
	}
    }
    sprintf (buf2, "%s\n\r", wordwrap (buf, 78));
    room->SetDescription (buf2);
}


// Remove any unused virtual rooms				-Thoric
void clear_vrooms ()
{
	int				hash;
	CRoomIndexData	*room, *room_next, *prev;

	for (hash = 0; hash < 64; hash++) {
		while (vroom_hash [hash]
		  && ! vroom_hash [hash]->first_person
		  && vroom_hash [hash]->IsEmpty ()) {
			room = vroom_hash [hash];
			vroom_hash [hash] = room->GetNext ();
			room->Clean ();
			delete room;
			--top_vroom;
		}
		prev = NULL;
		for (room = vroom_hash [hash]; room; room = room_next) {
			room_next = room->GetNext ();
			if (! room->first_person && room->IsEmpty ()) {
				if (prev)
					prev->SetNext (room_next);
				room->Clean ();
				delete room;
				--top_vroom;
			}
			if (room)
				prev = room;
		}
	}
}


char *ReverseExit (short vdir)
{
	switch (vdir) {
		default: return "somewhere";
		case 0:  return "the south";
		case 1:  return "the west";
		case 2:  return "the north";
		case 3:  return "the east";
		case 4:  return "below";
		case 5:  return "above";
		case 6:  return "the southwest";
		case 7:  return "the southeast";
		case 8:  return "the northwest";
		case 9:  return "the northeast";
	}

	return "<???>";
}


// Function to get the equivelant exit of DIR 0-MAXDIR out of linked list.
// Made to allow old-style diku-merc exit functions to work.	-Thoric
CExitData *get_exit (CRoomIndexData *room, short dir,
					 BOOL bDisabled /* = FALSE */)
{
	CExitData	*pExit;

	if (! room) {
		bug ("Get_exit: NULL room");
		return NULL;
	}

	for (pExit = room->first_exit; pExit; pExit = pExit->GetNext ())
		if (pExit->vdir == dir && (bDisabled || ! pExit->IsDisabled ()))
			return pExit;

	return NULL;
}


// Function to get an exit, leading the the specified room
CExitData *get_exit_to (CRoomIndexData *room, short dir, int vnum,
						BOOL bDisabled /* = FALSE */)
{
	CExitData	*pExit;

	if (! room) {
		bug ("Get_exit: NULL room");
		return NULL;
	}

	for (pExit = room->first_exit; pExit; pExit = pExit->GetNext ())
		if (pExit->vdir == dir && pExit->vnum == vnum
			&& (bDisabled || ! pExit->IsDisabled ()))
				return pExit;

	return NULL;
}


// Function to get the nth exit of a room			-Thoric
CExitData *get_exit_num (CRoomIndexData *room, short count)
{
	CExitData	*pExit;

	if (! room) {
		bug ("Get_exit: NULL room");
		return NULL;
	}

	int		i = 0;
	for (pExit = room->first_exit; pExit; pExit = pExit->GetNext ())
		if (++i == count)
			return pExit->IsDisabled () ? NULL : pExit;

	return NULL;
}


/*
 * Modify movement due to encumbrance				-Thoric
 */
short encumbrance (CCharacter *ch, short move)
{
    int cur, max;

    max = can_carry_w (ch);
    cur = ch->GetCarryWeight ();
    if (cur >= max)
      return move * 4;
    else
    if (cur >= max * 0.95)
      return (short) (move * 3.5);
    else
    if (cur >= max * 0.90)
      return move * 3;
    else
    if (cur >= max * 0.85)
      return (short) (move * 2.5);
    else
    if (cur >= max * 0.80)
      return move * 2;
    else
    if (cur >= max * 0.75)
      return (short) (move * 1.5);
    else
      return move;
}


// Check to see if a character can fall down, checks for looping   -Thoric
BOOL will_fall (CCharacter *ch, int fall)
{
	if (ch->GetInRoom ()->HasNoFloor () && ch->CanGo (DIR_DOWN)
	  && (! ch->IsFlying () || (ch->mount && ! ch->mount->IsFlying ()))) {
		if (fall > 80) {
			bug ("Falling (in a loop?) more than 80 rooms: vnum %d",
				ch->GetInRoom ()->vnum);
			ch->RemoveFromRoom ();
			ch->SendToRoom (RoomTable.GetRoom (SysData.m_RoomTemple));
			fall = 0;
			return TRUE;
		}
		set_char_color (AT_FALLING, ch);
		ch->SendText ("You're falling down...\n\r");
		move_char (ch, get_exit (ch->GetInRoom (), DIR_DOWN), ++fall);
		return TRUE;
	}
	return FALSE;
}


// create a 'virtual' room					-Thoric
CRoomIndexData *generate_exit (CRoomIndexData *in_room, CExitData **pexit)
{
	CExitData		*xit, *bxit;
	CExitData		*orig_exit = (CExitData *) *pexit;
	CRoomIndexData	*room, *backroom;
	int				brvnum;
	int				serial;
	int				roomnum;
	int				distance = -1;
	int				vdir = orig_exit->vdir;
	short			hash;
	BOOL			found = FALSE;

	if (in_room->vnum > 2097152000) {	// room is virtual (was 32767)
		serial = in_room->vnum;
		roomnum = in_room->tele_vnum;
		if ((serial & 2097152000) == orig_exit->vnum) {	// was 65535
			brvnum = serial >> 16;
			--roomnum;
			distance = roomnum;
		} else {
			brvnum = serial & 2097152000;	// was 65535
			++roomnum;
			distance = orig_exit->distance - 1;
		}
		backroom = RoomTable.GetRoom (brvnum);
	} else {
		int r1 = in_room->vnum;
		int r2 = orig_exit->vnum;

		brvnum = r1;
		backroom = in_room;
		serial = (UMAX (r1, r2) << 16) | UMIN (r1, r2);
		distance = orig_exit->distance - 1;
		roomnum = r1 < r2 ? 1 : distance;
	}
	hash = serial % 64;

	for (room = vroom_hash[hash]; room; room = room->GetNext ())
		if (room->vnum == serial && room->tele_vnum == roomnum) {
			found = TRUE;
			break;
		}

	if (!found) {
		room = new CRoomIndexData (serial);
		room->SetArea (in_room->GetArea ());
		room->tele_vnum = roomnum;
		room->sector_type = in_room->sector_type;
		room->SetRoomFlags (in_room->GetRoomFlags ());
		decorate_room (room);
		room->SetNext (vroom_hash [hash]);
		vroom_hash [hash] = room;
		++top_vroom;
	}
	if (!found || (xit=get_exit (room, vdir))==NULL) {
		xit = make_exit (room, orig_exit->GetToRoom (), vdir);
		xit->keyword = STRALLOC ("");
		xit->description = STRALLOC ("");
		xit->key = -1;
		xit->distance = distance;
	}
	if (!found) {
		bxit = make_exit (room, backroom, rev_dir[vdir]);
		bxit->keyword = STRALLOC ("");
		bxit->description = STRALLOC ("");
		bxit->key = -1;
		if ((serial & 2097152000) != orig_exit->vnum)	// was 65535
			bxit->distance = roomnum;
		else {
			CExitData *tmp = get_exit (backroom, vdir);
			int fulldist = tmp->distance;

			bxit->distance = fulldist - distance;
		}
	}
	pexit = (CExitData **) xit;
	return room;
}


ch_ret move_char (CCharacter *ch, CExitData *pexit, int fall)
{
    CRoomIndexData *in_room;
    CRoomIndexData *to_room;
    CRoomIndexData *from_room;
    char buf[MAX_STRING_LENGTH];
    char *txt;
    char *dtxt;
    ch_ret retcode;
    short door, distance;
    BOOL drunk = FALSE;
    BOOL brief = FALSE;

    if (!ch->IsNpc ())
      if (ch->IsDrunk (2) && (ch->GetPosition () != POS_SHOVE)
	&& (ch->GetPosition () != POS_DRAG))
	drunk = TRUE;

    if (drunk && !fall)
    {
      door = number_door ();
      pexit = get_exit (ch->GetInRoom (), door);
    }

#ifdef DEBUG
    if (pexit)
    {
	sprintf (buf, "move_char: %s to door %d", ch->GetName (), pexit->vdir);
	gpDoc->LogString (buf);
    }
#endif

    retcode = rNONE;
    txt = NULL;

    if (ch->IsNpc () && ch->IsAction (ACT_MOUNTED))
      return retcode;

    in_room = ch->GetInRoom ();
    from_room = in_room;
    if (!pexit || (to_room = pexit->GetToRoom ()) == NULL)
    {
	if (drunk)
	  ch->SendText ("You hit a wall in your drunken state.\n\r");
	 else
	  ch->SendText ("Alas, you cannot go that way.\n\r");
	return rNONE;
    }

    door = pexit->vdir;
    distance = pexit->distance;

	// Exit is only a "window", there is no way to travel in that direction
	// unless it's a door with a window in it		-Thoric
	if (pexit->IsWindow () && ! pexit->IsDoor ()) {
		ch->SendText ("Alas, you cannot go that way.\n\r");
		return rNONE;
	}

	if (pexit->IsPortal () && ch->IsNpc ()) {
		act (AT_PLAIN, "Mobs can't use portals.", ch, NULL, NULL, TO_CHAR);
		return rNONE;
	}

	if (pexit->IsNoMob () && ch->IsNpc ()) {
	act (AT_PLAIN, "Mobs can't enter there.", ch, NULL, NULL, TO_CHAR);
	return rNONE;
	}

	if (pexit->IsClosed () && (! ch->CanPass () || pexit->IsNoPass ())) {
		if (! pexit->IsSecret () && ! pexit->IsDig ()) {
			if (drunk) {
				act (AT_PLAIN, "$n runs into the $d in $s drunken state.",
					ch, NULL, pexit->keyword, TO_ROOM);
				act (AT_PLAIN, "You run into the $d in your drunken state.",
					ch, NULL, pexit->keyword, TO_CHAR); 
			}
			else
				act (AT_PLAIN, "The $d is closed.",
					ch, NULL, pexit->keyword, TO_CHAR);
		} else {
			if (drunk)
				ch->SendText ("You hit a wall in your drunken state.\n\r");
			else
				ch->SendText ("Alas, you cannot go that way.\n\r");
		}
		return rNONE;
	}

    /*
     * Crazy virtual room idea, created upon demand.		-Thoric
     */
    if (distance > 1)
	if ((to_room=generate_exit (in_room, &pexit)) == NULL)
	    ch->SendText ("Alas, you cannot go that way.\n\r");

    if (!fall
    &&   ch->IsCharmed ()
    &&   ch->GetMaster ()
    &&   in_room == ch->GetMaster ()->GetInRoom ())
    {
	ch->SendText ("What?  And leave your beloved master?\n\r");
	return rNONE;
    }

    if (room_is_private (to_room))
    {
	ch->SendText ("That room is private right now.\n\r");
	return rNONE;
    }

	if (ch->IsMortal () && !ch->IsNpc ()
	  && ch->GetInRoom ()->GetArea () != to_room->GetArea ()) {
		if (ch->GetLevel () < to_room->GetArea ()->low_hard_range) {
			set_char_color (AT_TELL, ch);
			switch (to_room->GetArea ()->low_hard_range - ch->GetLevel ()) {
			  case 1:
				ch->SendText ("A voice in your mind says, 'You are "
					"nearly ready to go that way...'\n\r");
				break;
			  case 2:
				ch->SendText ("A voice in your mind says, 'Soon you shall "
					"be ready to travel down this path... soon.'\n\r");
				break;
			  case 3:
			  ch->SendText ("A voice in your mind says, 'You are not "
				"ready to go down that path... yet.'.\n\r");
				break;
			  default:
				ch->SendText ("A voice in your mind says, 'You are not "
					"ready to go down that path.'.\n\r");
			}
			return rNONE;
		}
		else if (ch->GetLevel () > to_room->GetArea ()->hi_hard_range) {
			set_char_color (AT_TELL, ch);
			ch->SendText ("A voice in your mind says, 'There is nothing "
				"more for you down that path.'\n\r");
			return rNONE;
		}          
	}

    if (!fall && !ch->IsNpc ())
    {
	/*int iClass;*/
	int move;
/* Pretty sure we don't need to check for guilds anymore now that we have
   the new dh. -- Narn
*/
/*
	for (iClass = 0; iClass < MAX_CLASS; iClass++)
	{
	    if (iClass != ch->GetClass ()
	    &&   to_room->vnum == class_table[iClass]->guild)
	    {
		ch->SendText ("You aren't allowed in there.\n\r");
		return rNONE;
	    }
	}
*/

/* Prevent deadlies from entering a nopkill-flagged area from a 
   non-flagged area, but allow them to move around if already
   inside a nopkill area. - Blodkai
*/

        if ((to_room->GetArea ()->IsNoPkill ()
          && ! ch->GetInRoom ()->GetArea ()->IsNoPkill ())
          && (ch->IsPkiller () && ch->IsMortal ()))
        {
	    set_char_color (AT_MAGIC, ch);
            ch->SendText ("\n\rA godly force forbids deadly characters from entering that area...\n\r");
            return rNONE;
        }

	if (in_room->sector_type == SECT_AIR
	  || to_room->sector_type == SECT_AIR || pexit->IsFly ()) {
		if (ch->mount && ! ch->mount->IsFlying ()) {
			ch->SendText ("Your mount can't fly.\n\r");
			return rNONE;
		}
		if (! ch->mount && ! ch->IsFlying ()) {
			ch->SendText ("You'd need to fly to go there.\n\r");
			return rNONE;
		}
	}

	if (in_room->sector_type == SECT_WATER_NOSWIM
	||   to_room->sector_type == SECT_WATER_NOSWIM)
	{
	    CObjData *obj;
	    BOOL found;

	    found = FALSE;
	    if (ch->mount)
	    {
		if (ch->mount->IsFlying ()
		|| ch->mount->IsFloating ())
		  found = TRUE;
	    }
	    else
	    if (ch->IsFlying ()
	    || ch->IsFloating ())
		found = TRUE;

		// Look for a boat.
		if (! found) {
			POSITION	pos = ch->GetHeadCarryPos ();
			while (obj = ch->GetNextCarrying (pos)) {
				if (obj->item_type == ITEM_BOAT) {
					found = TRUE;
					if (drunk)
						txt = "paddles unevenly";
					else
						txt = "paddles";
					break;
				}
			}
		}

	    if (! found) {
			ch->SendText ("You'd need a boat to go there.\n\r");
			return rNONE;
	    }
	}

	if (pexit->IsClimb ()) {
	    BOOL found;

	    found = FALSE;
	    if (ch->mount && ch->mount->IsFlying ())
	      found = TRUE;
	    else
	    if (ch->IsFlying ())
	      found = TRUE;

	    if (!found && !ch->mount)
	    {
		if ((!ch->IsNpc () && number_percent () > ch->GetPcData ()->learned[gsn_climb])
		||      drunk || ch->GetMentalState () < -90)
		{
		   ch->SendText ("You start to climb... but lose your grip and fall!\n\r");
		   learn_from_failure (ch, gsn_climb);
		   if (pexit->vdir == DIR_DOWN)
		   {
			retcode = move_char (ch, pexit, 1);
			return retcode;
		   }
		   set_char_color (AT_HURT, ch);
		   ch->SendText ("OUCH! You hit the ground!\n\r");
		   WAIT_STATE (ch, 20);
		   retcode = damage (ch, ch, (pexit->vdir == DIR_UP ? 10 : 5),
					TYPE_UNDEFINED);
		   return retcode;
		}
		found = TRUE;
		learn_from_success (ch, gsn_climb);
		WAIT_STATE (ch, SkillTable.GetBeats (gsn_climb));
		txt = "climbs";
	    }

	    if (!found)
	    {
		ch->SendText ("You can't climb.\n\r");
		return rNONE;
	    }
	}

	if (ch->mount)
	{
	  switch (ch->mount->GetPosition ())
	  {
	    case POS_DEAD:
            ch->SendText ("Your mount is dead!\n\r");
	    return rNONE;
            break;
        
            case POS_MORTAL:
            case POS_INCAP:
            ch->SendText ("Your mount is hurt far too badly to move.\n\r");
	    return rNONE;
            break;
            
            case POS_STUNNED:
            ch->SendText ("Your mount is too stunned to do that.\n\r");
     	    return rNONE;
            break;
       
            case POS_SLEEPING:
            ch->SendText ("Your mount is sleeping.\n\r");
	    return rNONE;
            break;
         
            case POS_RESTING:
            ch->SendText ("Your mount is resting.\n\r");
	    return rNONE;
            break;
        
            case POS_SITTING:
            ch->SendText ("Your mount is sitting down.\n\r");
	    return rNONE;
            break;

	    default:
	    break;
  	  }

	  if (!ch->mount->IsFlying ()
	  &&   !ch->mount->IsFloating ())
	    move = movement_loss[UMIN (SECT_MAX-1, in_room->sector_type)];
	  else
	    move = 1;
	  if (ch->mount->GetMove () < move)
	  {
	    ch->SendText ("Your mount is too exhausted.\n\r");
	    return rNONE;
	  }
	}
	else
	{
	  if (!ch->IsFlying ()
	  &&   !ch->IsFloating ())
	    move = encumbrance (ch, movement_loss[UMIN (SECT_MAX-1, in_room->sector_type)]);
	  else
	    move = 1;
	  if (ch->GetMove () < move)
	  {
	    ch->SendText ("You are too exhausted.\n\r");
	    return rNONE;
	  }
	}

	WAIT_STATE (ch, move);
	if (ch->mount)
	  ch->mount->AddMove (-move);
	else
	  ch->AddMove (-move);
    }

    /*
     * Check if player can fit in the room
     */
    if (to_room->tunnel > 0)
    {
	CCharacter *ctmp;
	int count = ch->mount ? 1 : 0;
	
	for (ctmp = to_room->first_person; ctmp; ctmp = ctmp->GetNextInRoom ())
	  if (++count >= to_room->tunnel)
	  {
		if (ch->mount && count == to_room->tunnel)
		  ch->SendText ("There is no room for both you and your mount in there.\n\r");
		else
		  ch->SendText ("There is no room for you in there.\n\r");
		return rNONE;
	  }
    }

    // check for traps on exit - later
    if (! ch->IsSneaking () && (ch->IsNpc () || ! ch->IsWizInvis ())) {
		if (fall)
			txt = "falls";
		else if (! txt) {
			if (ch->mount) {
				if (ch->mount->IsFloating ())
					txt = "floats";
				else if (ch->mount->IsFlying ())
					txt = "flys";
				else txt = "rides";
			} else {
				if (ch->IsFloating ()) {
					if (drunk)
						txt = "floats unsteadily";
					else txt = "floats";
				}
				else if (ch->IsFlying ()) {
					if (drunk)
						txt = "flys shakily";
					else txt = "flys";
				}
				else if (ch->GetPosition () == POS_SHOVE)
					txt = "is shoved";
				else if (ch->GetPosition () == POS_DRAG)
					txt = "is dragged";
				else {
					if (drunk)
						txt = "stumbles drunkenly";
					else txt = "leaves";
				}
			}
		}
		if (ch->mount) {
			sprintf (buf, "$n %s %s upon $N.", txt, dir_name[door]);
			act (AT_ACTION, buf, ch, NULL, ch->mount, TO_NOTVICT);
		} else {
			sprintf (buf, "$n %s $T.", txt);
			act (AT_ACTION, buf, ch, NULL, dir_name[door], TO_ROOM);
		}
	}

    rprog_leave_trigger (ch);
    if (char_died (ch))
      return global_retcode;

    ch->RemoveFromRoom ();
    if (ch->mount)
    {
      rprog_leave_trigger (ch->mount);
      if (char_died (ch))
        return global_retcode;
      if (ch->mount)
      {
        ch->mount->RemoveFromRoom ();
        ch->mount->SendToRoom (to_room);
      }
    }


    ch->SendToRoom (to_room);
    if (!ch->IsSneaking ()
    && (ch->IsNpc () || !ch->IsWizInvis ()))
    {
      if (fall)
        txt = "falls";
      else
      if (ch->mount)
      {
	if (ch->mount->IsFloating ())
	  txt = "floats in";
	else
	if (ch->mount->IsFlying ())
	  txt = "flys in";
	else
	  txt = "rides in";
      }
      else
      {
	if (ch->IsFloating ())
	{
	  if (drunk)
	    txt = "floats in unsteadily";
	   else
	    txt = "floats in";
	}
	else
	if (ch->IsFlying ())
	{
	  if (drunk)
	    txt = "flys in shakily";
	   else
	    txt = "flys in";
	}
	else
	if (ch->GetPosition () == POS_SHOVE)
          txt = "is shoved in";
	else
	if (ch->GetPosition () == POS_DRAG)
	  txt = "is dragged in";
  	else
	{
	  if (drunk)
	    txt = "stumbles drunkenly in";
	   else
	    txt = "arrives";
	}
      }
      switch (door)
      {
      default: dtxt = "somewhere";	break;
      case 0:  dtxt = "the south";	break;
      case 1:  dtxt = "the west";	break;
      case 2:  dtxt = "the north";	break;
      case 3:  dtxt = "the east";	break;
      case 4:  dtxt = "below";		break;
      case 5:  dtxt = "above";		break;
      case 6:  dtxt = "the south-west";	break;
      case 7:  dtxt = "the south-east";	break;
      case 8:  dtxt = "the north-west";	break;
      case 9:  dtxt = "the north-east";	break;
      }
      if (ch->mount)
      {
	sprintf (buf, "$n %s from %s upon $N.", txt, dtxt);
	act (AT_ACTION, buf, ch, NULL, ch->mount, TO_ROOM);
      }
      else
      {
	sprintf (buf, "$n %s from %s.", txt, dtxt);
	act (AT_ACTION, buf, ch, NULL, NULL, TO_ROOM);
      }
    }

    if (ch->IsMortal ()
    &&  !ch->IsNpc ()
    &&  ch->GetInRoom ()->GetArea () != to_room->GetArea ())
    {
	if (ch->GetLevel () < to_room->GetArea ()->low_soft_range)
	{
	   set_char_color (AT_MAGIC, ch);
	   ch->SendText ("You feel uncomfortable being in this strange land...\n\r");
	}
	else
	if (ch->GetLevel () > to_room->GetArea ()->hi_soft_range)
	{
	   set_char_color (AT_MAGIC, ch);
	   ch->SendText ("You feel there is not much to gain visiting this place...\n\r");
	}
    }

    /* Make sure everyone sees the room description of death traps. */
    if (ch->GetInRoom ()->IsDeathRoom () && ch->IsMortal ())
    {
      if (ch->IsAction (PLR_BRIEF))
        brief = TRUE;
      ch->ClrActBit (PLR_BRIEF);
    }

    do_look (ch, "auto");
    if (brief) 
      ch->SetActBit (PLR_BRIEF);

    /*
     * Put good-old EQ-munching death traps back in!		-Thoric
     */
    if (ch->GetInRoom ()->IsDeathRoom () && ch->IsMortal ())
    {
       act (AT_DEAD, "$n falls prey to a terrible death!", ch, NULL, NULL, TO_ROOM);
       set_char_color (AT_DEAD, ch);
       ch->SendText ("Oopsie... you're dead!\n\r");
       sprintf (buf, "%s hit a DEATH TRAP in room %d!",
		     ch->GetName (), ch->GetInRoom ()->vnum);
       gpDoc->LogString (buf, LOG_PLAYER);
       to_channel (buf, CHANNEL_MONITOR, "Monitor", LEVEL_IMMORTAL);
       extract_char (ch, FALSE);
       return rCHAR_DIED;
    }

    /* BIG ugly looping problem here when the character is mptransed back
       to the starting room.  To avoid this, check how many chars are in 
       the room at the start and stop processing followers after doing
       the right number of them.  -- Narn
    */
    if (!fall)
    {
      CCharacter *fch;
      CCharacter *nextinroom;
      int chars = 0, count = 0;

      for (fch = from_room->first_person; fch; fch = fch->GetNextInRoom ())
        chars++;

      for (fch = from_room->first_person; fch && (count < chars); fch = nextinroom)
      {
	nextinroom = fch->GetNextInRoom ();
        count++;
	if (fch != ch		/* loop room bug fix here by Thoric */
	&& fch->GetMaster () == ch
	&& fch->GetPosition () == POS_STANDING)
	{
	act (AT_ACTION, "You follow $N.", fch, NULL, ch, TO_CHAR);
	    move_char (fch, pexit, 0);
	}
      }
    }

    if (! ch->GetInRoom ()->IsEmpty ())
      retcode = check_room_for_traps (ch, TRAP_ENTER_ROOM);
    if (retcode != rNONE)
      return retcode;

    if (char_died (ch))
      return retcode;

    mprog_entry_trigger (ch);
    if (char_died (ch))
      return retcode;

    rprog_enter_trigger (ch);
    if (char_died (ch))
       return retcode;

    mprog_greet_trigger (ch);
    if (char_died (ch))
       return retcode;

    oprog_greet_trigger (ch);
    if (char_died (ch))
       return retcode;

    if (!will_fall (ch, fall)
    &&   fall > 0)
    {
	if (!ch->IsFloating ()
	|| (ch->mount && !ch->mount->IsFloating ()))
	{
	  set_char_color (AT_HURT, ch);
	  ch->SendText ("OUCH! You hit the ground!\n\r");
	  WAIT_STATE (ch, 20);
	  retcode = damage (ch, ch, 20 * fall, TYPE_UNDEFINED);
	}
	else
	{
	  set_char_color (AT_MAGIC, ch);
	  ch->SendText ("You lightly float down to the ground.\n\r");
	}
    }
    return retcode;
}


void do_north (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_NORTH), 0);
    return;
}


void do_east (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_EAST), 0);
    return;
}


void do_south (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_SOUTH), 0);
    return;
}


void do_west (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_WEST), 0);
    return;
}


void do_up (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_UP), 0);
    return;
}


void do_down (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_DOWN), 0);
    return;
}

void do_northeast (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_NORTHEAST), 0);
    return;
}

void do_northwest (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_NORTHWEST), 0);
    return;
}

void do_southeast (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_SOUTHEAST), 0);
    return;
}

void do_southwest (CCharacter *ch, char *argument)
{
    move_char (ch, get_exit (ch->GetInRoom (), DIR_SOUTHWEST), 0);
    return;
}


CExitData *find_door (CCharacter *ch, char *arg, BOOL quiet)
{
	CExitData	*pExit = NULL;

	if (arg == NULL || !str_cmp (arg,""))
		return NULL;

	int	door = get_door (arg);

	if (door < 0) {
		pExit = ch->GetInRoom ()->first_exit;
		for ( ; pExit; pExit = pExit->GetNext ()) {
			if ((quiet || pExit->IsDoor ()) && pExit->keyword
				&& nifty_is_name (arg, pExit->keyword)
				&& ! pExit->IsDisabled ())
					return pExit;
		}
		if (!quiet)
			act (AT_PLAIN, "You see no $T here.", ch, NULL, arg, TO_CHAR);
		return NULL;
	}

	if ((pExit = get_exit (ch->GetInRoom (), door)) == NULL) {
		if (! quiet)
			act (AT_PLAIN, "You see no $T here.", ch, NULL, arg, TO_CHAR);
		return NULL;
	}

	if (quiet)
		return pExit;

	if (pExit->IsSecret ()) {
		act (AT_PLAIN, "You see no $T here.", ch, NULL, arg, TO_CHAR);
		return NULL;
	}

	if (! pExit->IsDoor ()) {
		ch->SendText ("You can't do that.\n\r");
		return NULL;
	}

	return pExit;
}


void toggle_bexit_flag (CExitData *pExit, int flag)
{
	CExitData	*pExitRev;

	pExit->Toggle (flag);

	if ((pExitRev = pExit->rexit) != NULL && pExitRev != pExit)
		pExitRev->Toggle (flag);
}


void set_bexit_flag (CExitData *pExit, int flag)
{
	CExitData	*pExitRev;

	pExit->SetBit (flag);

	if ((pExitRev = pExit->rexit) != NULL && pExitRev != pExit)
		pExitRev->SetBit (flag);
}


void remove_bexit_flag (CExitData *pExit, int flag)
{
	CExitData	*pExitRev;

	pExit->ClrBit (flag);

	if ((pExitRev = pExit->rexit) != NULL && pExitRev != pExit)
		pExitRev->ClrBit (flag);
}


void do_open (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CObjData *obj;
    CExitData *pexit;
    int door;

    one_argument (argument, arg);

    if (arg[0] == '\0')
    {
	ch->SendText ("Open what?\n\r");
	return;
    }

    if ((pexit = find_door (ch, arg, TRUE)) != NULL) {
		// 'open door'
		CExitData	*pExitRev;

		if (! pexit->IsDoor ()) {
			ch->SendText ("You can't do that.\n\r");
			return;
		}
		if (! pexit->IsClosed ()) {
			ch->SendText ("It's already open.\n\r");
			return;
		}
		if (pexit->IsLocked ()) {
			ch->SendText ("It's locked.\n\r");
			return;
		}

		if (! pexit->IsSecret ()
		  || (pexit->keyword && nifty_is_name (arg, pexit->keyword))) {
			act (AT_ACTION, "$n opens the $d.", ch, NULL,
				pexit->keyword, TO_ROOM);
			act (AT_ACTION, "You open the $d.", ch, NULL,
				pexit->keyword, TO_CHAR);
			if ((pExitRev = pexit->rexit) != NULL
			  && pExitRev->GetToRoom () == ch->GetInRoom ()) {
				CCharacter *rch = pexit->GetToRoom ()->first_person;

				for ( ; rch; rch = rch->GetNextInRoom ())
					act (AT_ACTION, "The $d opens.", rch, NULL,
						pExitRev->keyword, TO_CHAR);
			}
			remove_bexit_flag (pexit, EX_CLOSED);
			if ((door = pexit->vdir) >= 0 && door < 10)
				check_room_for_traps (ch, trap_door [door]);
			return;
		}
	}

    if ((obj = get_obj_here (ch, arg)) != NULL)
    {
	/* 'open object' */
	if (obj->item_type != ITEM_CONTAINER)
	{ 
          ch->SendColorf ("%s is not a container.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 
	if (!IS_SET (obj->value[1], CONT_CLOSED))
	{ 
          ch->SendColorf ("%s is already open.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 
	if (!IS_SET (obj->value[1], CONT_CLOSEABLE))
	{ 
          ch->SendColorf ("%s cannot be opened or closed.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 
	if (IS_SET (obj->value[1], CONT_LOCKED))
	{ 
          ch->SendColorf ("%s is locked.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 

	REMOVE_BIT (obj->value[1], CONT_CLOSED);
	act (AT_ACTION, "You open $p.", ch, obj, NULL, TO_CHAR);
	act (AT_ACTION, "$n opens $p.", ch, obj, NULL, TO_ROOM);
	check_for_trap (ch, obj, TRAP_OPEN);
	return;
    }

    ch->SendTextf ("You see no %s here.\n\r", arg);
    return;
}



void do_close (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CObjData *obj;
    CExitData *pexit;
    int door;

    one_argument (argument, arg);

    if (arg[0] == '\0')
    {
	ch->SendText ("Close what?\n\r");
	return;
    }

	if ((pexit = find_door (ch, arg, TRUE)) != NULL) {
		// 'close door'
		CExitData	*pExitRev;

		if (! pexit->IsDoor ()) {
			ch->SendText ("You can't do that.\n\r");
			return;
		}
		if (pexit->IsClosed ()) {
			ch->SendText ("It's already closed.\n\r");
			return;
		}

		act (AT_ACTION, "$n closes the $d.", ch, NULL, pexit->keyword, TO_ROOM);
		act (AT_ACTION, "You close the $d.", ch, NULL, pexit->keyword, TO_CHAR);

		// close the other side
		if ((pExitRev = pexit->rexit) != NULL
		  && pExitRev->GetToRoom () == ch->GetInRoom ()) {

			pExitRev->SetClosed ();
			CCharacter	*rch = pexit->GetToRoom ()->first_person;
			for ( ; rch; rch = rch->GetNextInRoom ())
				act (AT_ACTION, "The $d closes.", rch, NULL,
					pExitRev->keyword, TO_CHAR);
		}
		set_bexit_flag (pexit, EX_CLOSED);
		if ((door = pexit->vdir) >= 0 && door < 10)
			check_room_for_traps (ch, trap_door[door]);
		return;
	}

    if ((obj = get_obj_here (ch, arg)) != NULL)
    {
	/* 'close object' */
	if (obj->item_type != ITEM_CONTAINER)
	{ 
          ch->SendColorf ("%s is not a container.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 
	if (IS_SET (obj->value[1], CONT_CLOSED))
	{ 
          ch->SendColorf ("%s is already closed.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 
	if (!IS_SET (obj->value[1], CONT_CLOSEABLE))
        { 
          ch->SendColorf ("%s cannot be opened or closed.\n\r",
			capitalize (obj->GetShortDescr ())); 
          return;
        } 

	SET_BIT (obj->value[1], CONT_CLOSED);
	act (AT_ACTION, "You close $p.", ch, obj, NULL, TO_CHAR);
	act (AT_ACTION, "$n closes $p.", ch, obj, NULL, TO_ROOM);
	check_for_trap (ch, obj, TRAP_CLOSE);
	return;
    }

    ch->SendTextf ("You see no %s here.\n\r", arg);
    return;
}


BOOL has_key (CCharacter *ch, int key)
{
	CObjData	*obj;
	POSITION	pos = ch->GetHeadCarryPos ();

	while (obj = ch->GetNextCarrying (pos))
		if (obj->pIndexData->vnum == key || obj->value [0] == key)
			return TRUE;

	return FALSE;
}


void do_lock (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CObjData *obj;
    CExitData *pexit;

    one_argument (argument, arg);

    if (arg[0] == '\0')
    {
	ch->SendText ("Lock what?\n\r");
	return;
    }

	if ((pexit = find_door (ch, arg, TRUE)) != NULL) {
		// 'lock door'

		if (! pexit->IsDoor ()) {
			ch->SendText ("You can't do that.\n\r");
			return;
		}
		if (! pexit->IsClosed ()) {
			ch->SendText ("It's not closed.\n\r");
			return;
		}
		if (pexit->key < 0) {
			ch->SendText ("It can't be locked.\n\r");
			return;
		}
		if (! has_key (ch, pexit->key)) {
			ch->SendText ("You lack the key.\n\r");
			return;
		}
		if (pexit->IsLocked ()) {
			ch->SendText ("It's already locked.\n\r");
			return;
		}

		if (! pexit->IsSecret ()
		  || (pexit->keyword && nifty_is_name (arg, pexit->keyword))) {
			ch->SendText ("*Click*\n\r");
			act (AT_ACTION, "$n locks the $d.", ch, NULL, pexit->keyword, TO_ROOM);
			set_bexit_flag (pexit, EX_LOCKED);
			return;
		}
	}

    if ((obj = get_obj_here (ch, arg)) != NULL)
    {
	/* 'lock object' */
	if (obj->item_type != ITEM_CONTAINER)
	    { ch->SendText ("That's not a container.\n\r"); return; }
	if (!IS_SET (obj->value[1], CONT_CLOSED))
	    { ch->SendText ("It's not closed.\n\r"); return; }
	if (obj->value[2] < 0)
	    { ch->SendText ("It can't be locked.\n\r"); return; }
	if (!has_key (ch, obj->value[2]))
	    { ch->SendText ("You lack the key.\n\r"); return; }
	if (IS_SET (obj->value[1], CONT_LOCKED))
	    { ch->SendText ("It's already locked.\n\r"); return; }

	SET_BIT (obj->value[1], CONT_LOCKED);
	ch->SendText ("*Click*\n\r");
	act (AT_ACTION, "$n locks $p.", ch, obj, NULL, TO_ROOM);
	return;
    }

    ch->SendTextf ("You see no %s here.\n\r", arg);
    return;
}



void do_unlock (CCharacter *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];
    CObjData *obj;
    CExitData *pexit;

    one_argument (argument, arg);

    if (arg[0] == '\0')
    {
	ch->SendText ("Unlock what?\n\r");
	return;
    }

	if ((pexit = find_door (ch, arg, TRUE)) != NULL) {
		// 'unlock door'

		if (! pexit->IsDoor ()) {
			ch->SendText ("You can't do that.\n\r");
			return;
		}
		if (! pexit->IsClosed ()) {
			ch->SendText ("It's not closed.\n\r");
			return;
		}
		if (pexit->key < 0) {
			ch->SendText ("It can't be unlocked.\n\r");
			return;
		}
		if (! has_key (ch, pexit->key)) {
			ch->SendText ("You lack the key.\n\r");
			return;
		}
		if (! pexit->IsLocked ()) {
			ch->SendText ("It's already unlocked.\n\r");
			return;
		}

		if (! pexit->IsSecret ()
		  || (pexit->keyword && nifty_is_name (arg, pexit->keyword))) {
			ch->SendText ("*Click*\n\r");
			act (AT_ACTION, "$n unlocks the $d.", ch, NULL, pexit->keyword, TO_ROOM);
			remove_bexit_flag (pexit, EX_LOCKED);
			return;   
		}
	}

    if ((obj = get_obj_here (ch, arg)) != NULL)
    {
	/* 'unlock object' */
	if (obj->item_type != ITEM_CONTAINER)
	    { ch->SendText ("That's not a container.\n\r"); return; }
	if (!IS_SET (obj->value[1], CONT_CLOSED))
	    { ch->SendText ("It's not closed.\n\r"); return; }
	if (obj->value[2] < 0)
	    { ch->SendText ("It can't be unlocked.\n\r"); return; }
	if (!has_key (ch, obj->value[2]))
	    { ch->SendText ("You lack the key.\n\r"); return; }
	if (!IS_SET (obj->value[1], CONT_LOCKED))
	    { ch->SendText ("It's already unlocked.\n\r"); return; }

	REMOVE_BIT (obj->value[1], CONT_LOCKED);
	ch->SendText ("*Click*\n\r");
	act (AT_ACTION, "$n unlocks $p.", ch, obj, NULL, TO_ROOM);
	return;
    }

    ch->SendTextf ("You see no %s here.\n\r", arg);
    return;
}

void do_bashdoor (CCharacter *ch, char *argument)
{
	CCharacter *gch;
	CExitData *pexit;
	char       arg [ MAX_INPUT_LENGTH ];

	if (!ch->IsNpc ()
	&& ch->GetLevel () < SkillTable.GetClassLevel (gsn_bashdoor, ch->GetClass ()))
	{
	    ch->SendText ("You're not enough of a warrior to bash doors!\n\r");
	    return;
	}

	one_argument (argument, arg);

	if (arg[0] == '\0')
	{
	    ch->SendText ("Bash what?\n\r");
	    return;
	}

	if (ch->GetFightData ())
	{
	    ch->SendText ("You can't break off your fight.\n\r");
	    return;
	}

	if (pexit = find_door (ch, arg, FALSE)) {
		CRoomIndexData	*to_room;
		CExitData		*pExitRev;
		int				chance;
		char			*keyword;

		if (! pexit->IsClosed ()) {
			ch->SendText ("Calm down.  It is already open.\n\r");
			return;
		}

		WAIT_STATE (ch, SkillTable.GetBeats (gsn_bashdoor));

		if (pexit->IsSecret ())
			keyword = "wall";
		else
			keyword = pexit->keyword;
		if (! ch->IsNpc ())
			chance = ch->GetPcData ()->learned[gsn_bashdoor] / 2;
		else
			chance = 90;
		if (pexit->IsLocked ())
			chance /= 3;

		if (! pexit->IsBashProof () && ch->GetMove () >= 15
		  && number_percent ()
		  < (chance + 4 * (ch->GetCurrentStrength () - 19))) {
			pexit->ClrClosed ();
			pexit->ClrLocked ();
			pexit->SetBashed ();

			act (AT_SKILL, "Crash!  You bashed open the $d!", ch, NULL,
				keyword, TO_CHAR);
			act (AT_SKILL, "$n bashes open the $d!", ch, NULL,
				keyword, TO_ROOM);
			learn_from_success (ch, gsn_bashdoor);

			if ((to_room = pexit->GetToRoom ()) != NULL
			  && (pExitRev = pexit->rexit) != NULL
			  && pExitRev->GetToRoom () == ch->GetInRoom ()) {

				pExitRev->ClrClosed ();
				pExitRev->ClrLocked ();
				pExitRev->SetBashed ();

				CCharacter *rch = to_room->first_person;
				for ( ; rch; rch = rch->GetNextInRoom ()) {
					act (AT_SKILL, "The $d crashes open!",
						rch, NULL, pExitRev->keyword, TO_CHAR);
				}
			}
			damage (ch, ch, (ch->GetMaxHp () / 20), gsn_bashdoor);

		} else {
			act (AT_SKILL,
				"WHAAAAM!!!  You bash against the $d, but it doesn't budge.",
				ch, NULL, keyword, TO_CHAR);
			act (AT_SKILL,
				"WHAAAAM!!!  $n bashes against the $d, but it holds strong.",
				ch, NULL, keyword, TO_ROOM);
			damage (ch, ch, (ch->GetMaxHp () / 20) + 10, gsn_bashdoor);
			learn_from_failure (ch, gsn_bashdoor);
		}
	} else {
	    act (AT_SKILL, "WHAAAAM!!!  You bash against the wall, but it doesn't budge.",
		ch, NULL, NULL, TO_CHAR);
	    act (AT_SKILL, "WHAAAAM!!!  $n bashes against the wall, but it holds strong.",
		ch, NULL, NULL, TO_ROOM);
	    damage (ch, ch, (ch->GetMaxHp () / 20) + 10, gsn_bashdoor);
	    learn_from_failure (ch, gsn_bashdoor);
	}
	if (!char_died (ch))
	   for (gch = ch->GetInRoom ()->first_person; gch; gch = gch->GetNextInRoom ())
	   {
		 if (gch->IsAwake ()
		 && !gch->GetFightData ()
		 && (gch->IsNpc () && ! gch->IsCharmed ())
		 && (ch->GetLevel () - gch->GetLevel () <= 4)
		 && number_bits (2) == 0)
		 multi_hit (gch, ch, TYPE_UNDEFINED);
	   }

        return;
}


void do_stand (CCharacter *ch, char *argument)
{
	switch (ch->GetPosition ()) {
	  case POS_SLEEPING:
		if (ch->IsSleeping ()) {
			ch->SendText ("You can't seem to wake up!\n\r");
			return;
		}

		ch->SendText ("You wake and climb quickly to your feet.\n\r");
		act (AT_ACTION, "$n arises from $s slumber.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_STANDING);
		break;

	  case POS_RESTING:
		ch->SendText ("You gather yourself and stand up.\n\r");
		act (AT_ACTION, "$n rises from $s rest.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_STANDING);
		break;

	  case POS_SITTING:
		ch->SendText ("You move quickly to your feet.\n\r");
		act (AT_ACTION, "$n rises up.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_STANDING);
		break;

	  case POS_STANDING:
		ch->SendText ("You are already standing.\n\r");
		break;

	  case POS_FIGHTING:
	  case POS_EVASIVE:
	  case POS_DEFENSIVE:
	  case POS_AGGRESSIVE:
	  case POS_BERSERK:
		ch->SendText ("You are already fighting!\n\r");
		break;
	}
}


void do_sit (CCharacter *ch, char *argument)
{
	switch (ch->GetPosition ()) {
	  case POS_SLEEPING:
		if (ch->IsSleeping ()) {
			ch->SendText ("You can't seem to wake up!\n\r");
			return;
		}

		ch->SendText ("You wake and sit up.\n\r");
		act (AT_ACTION, "$n wakes and sits up.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SITTING);
		break;

	  case POS_RESTING:
		ch->SendText ("You stop resting and sit up.\n\r");
		act (AT_ACTION, "$n stops resting and sits up.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SITTING);
		break;

	  case POS_STANDING:
		ch->SendText ("You sit down.\n\r");
		act (AT_ACTION, "$n sits down.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SITTING);
		break;

	  case POS_SITTING:
		ch->SendText ("You are already sitting.\n\r");
		return;

	  case POS_FIGHTING:
	  case POS_EVASIVE:
	  case POS_DEFENSIVE:
	  case POS_AGGRESSIVE:
	  case POS_BERSERK:
		ch->SendText ("You are busy fighting!\n\r");
		return;

	  case POS_MOUNTED:
		ch->SendText ("You are already sitting - on your mount.\n\r");
		return;
	}
}


void do_rest (CCharacter *ch, char *argument)
{
	switch (ch->GetPosition ()) {
	  case POS_SLEEPING:
		if (ch->IsSleeping ()) {
			ch->SendText ("You can't seem to wake up!\n\r");
			return;
		}

		ch->SendText ("You rouse from your slumber.\n\r");
		act (AT_ACTION, "$n rouses from $s slumber.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_RESTING);
		break;

	  case POS_RESTING:
		ch->SendText ("You are already resting.\n\r");
		return;

	  case POS_STANDING:
		ch->SendText ("You sprawl out haphazardly.\n\r");
		act (AT_ACTION, "$n sprawls out haphazardly.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_RESTING);
		break;

	  case POS_SITTING:
		ch->SendText ("You lie back and sprawl out to rest.\n\r");
		act (AT_ACTION, "$n lies back and sprawls out to rest.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_RESTING);
		break;

	  case POS_FIGHTING:
	  case POS_EVASIVE:
	  case POS_DEFENSIVE:
	  case POS_AGGRESSIVE:
	  case POS_BERSERK:
		ch->SendText ("You are busy fighting!\n\r");
		return;

	  case POS_MOUNTED:
		ch->SendText ("You'd better dismount first.\n\r");
		return;
	}

	rprog_rest_trigger (ch);
}


void do_sleep (CCharacter *ch, char *argument)
{
	switch (ch->GetPosition ()) {
	  case POS_SLEEPING:
		ch->SendText ("You are already sleeping.\n\r");
		return;

	  case POS_RESTING:
		if (ch->GetMentalState () > 30
		  && (number_percent ()+10) < ch->GetMentalState ()) {
			ch->SendText ("You just can't seem to calm yourself down "
				"enough to sleep.\n\r");
			act (AT_ACTION, "$n closes $s eyes for a few moments, but "
				"just can't seem to go to sleep.", ch, NULL, NULL, TO_ROOM);
			return;
		}
		ch->SendText ("You close your eyes and drift into slumber.\n\r");
		act (AT_ACTION, "$n closes $s eyes and drifts into a deep slumber.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SLEEPING);
		break;

	  case POS_SITTING:
		if (ch->GetMentalState () > 30
		  && (number_percent ()+5) < ch->GetMentalState ()) {
			ch->SendText ("You just can't seem to calm yourself down "
				"enough to sleep.\n\r");
			act (AT_ACTION, "$n closes $s eyes for a few moments, but just "
				"can't seem to go to sleep.", ch, NULL, NULL, TO_ROOM);
			return;
		}
		ch->SendText ("You slump over and fall dead asleep.\n\r");
		act (AT_ACTION, "$n nods off and slowly slumps over, dead asleep.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SLEEPING);
		break;

	  case POS_STANDING: 
		if (ch->GetMentalState () > 30
		  && number_percent () < ch->GetMentalState ()) {
			ch->SendText ("You just can't seem to calm yourself down "
				"enough to sleep.\n\r");
			act (AT_ACTION, "$n closes $s eyes for a few moments, but just "
				"can't seem to go to sleep.", ch, NULL, NULL, TO_ROOM);
			return;
		}
		ch->SendText ("You collapse into a deep sleep.\n\r");
		act (AT_ACTION, "$n collapses into a deep sleep.", ch, NULL, NULL, TO_ROOM);
		ch->SetPosition (POS_SLEEPING);
		break;

	  case POS_FIGHTING:
	  case POS_EVASIVE:
	  case POS_DEFENSIVE:
	  case POS_AGGRESSIVE:
	  case POS_BERSERK:
		ch->SendText ("You are busy fighting!\n\r");
		return;

	  case POS_MOUNTED:
		ch->SendText ("You really should dismount first.\n\r");
		return;
	}

	rprog_sleep_trigger (ch);
}


void do_wake (CCharacter *ch, char *argument)
{
	char		arg [MAX_INPUT_LENGTH];
	CCharacter	*victim;

	one_argument (argument, arg);
	if (arg [0] == '\0') {
		do_stand (ch, argument);
		return;
	}

	if (! ch->IsAwake ()) {
		ch->SendText ("You are asleep yourself!\n\r");
		return;
	}

	if ((victim = get_char_room (ch, arg)) == NULL) {
		ch->SendText ("They aren't here.\n\r");
		return;
	}

	if (victim->IsAwake ()) {
		act (AT_PLAIN, "$N is already awake.", ch, NULL, victim, TO_CHAR);
		return;
	}

	if (victim->IsSleeping () || victim->GetPosition () < POS_SLEEPING) {
		act (AT_PLAIN, "You can't seem to wake $M!",  ch, NULL, victim, TO_CHAR);
		return;
	}

	act (AT_ACTION, "You wake $M.", ch, NULL, victim, TO_CHAR);
	victim->SetPosition (POS_STANDING);
	act (AT_ACTION, "$n wakes you.", ch, NULL, victim, TO_VICT);
}


// teleport a character to another room
void teleportch (CCharacter *ch, CRoomIndexData *room, BOOL show)
{
	char	buf [MAX_STRING_LENGTH];

	if (room_is_private (room))
		return;
	act (AT_ACTION, "$n disappears suddenly!", ch, NULL, NULL, TO_ROOM);
	ch->RemoveFromRoom ();
	ch->SendToRoom (room);
	act (AT_ACTION, "$n arrives suddenly!", ch, NULL, NULL, TO_ROOM);

	if (show)
		do_look (ch, "auto");

	if (ch->GetInRoom ()->IsDeathRoom () && ch->IsMortal ()) {
		act (AT_DEAD, "$n falls prey to a terrible death!", ch, NULL, NULL, TO_ROOM);
		set_char_color (AT_DEAD, ch);
		ch->SendText ("Oopsie... you're dead!\n\r");
		sprintf (buf, "%s hit a DEATH TRAP in room %d!",
			ch->GetName (), ch->GetInRoom ()->vnum);
		gpDoc->LogString (buf, LOG_PLAYER);
		to_channel (buf, CHANNEL_MONITOR, "Monitor", LEVEL_IMMORTAL);
		extract_char (ch, FALSE);
	}
}


void teleport (CCharacter *ch, short room, int flags)
{
	CCharacter	*nch, *nch_next;
	BOOL		show;

	CRoomIndexData	*pDestRoom = RoomTable.GetRoom (room);
	if (! pDestRoom) {
		bug ("teleport: bad room vnum %d", room);
		return;
	}

	if (IS_SET (flags, TELE_SHOWDESC))
		show = TRUE;
	else show = FALSE;

	if (!IS_SET (flags, TELE_TRANSALL)) {
		teleportch (ch, pDestRoom, show);
		return;
	}

	// teleport everybody in the room
	for (nch = ch->GetInRoom ()->first_person; nch; nch = nch_next) {
		nch_next = nch->GetNextInRoom ();
		teleportch (nch, pDestRoom, show);
	}

	// teleport the objects on the ground too
	if (IS_SET (flags, TELE_TRANSALLPLUS)) {
		CObjData	*obj;
		POSITION	pos = ch->GetInRoom ()->GetHeadContentPos ();

		while (obj = ch->GetInRoom ()->GetNextContent (pos)) {
			obj_from_room (obj);
			obj_to_room (obj, pDestRoom);
		}
	}
}


// "Climb" in a certain direction.				-Thoric
void do_climb (CCharacter *ch, char *argument)
{
	CExitData	*pExit;
	BOOL		found;

	found = FALSE;
	if (argument [0] == '\0') {
		pExit = ch->GetInRoom ()->first_exit;
		for ( ; pExit; pExit = pExit->GetNext ())
			if (pExit->CanClimb () && ! pExit->IsDisabled ()) {
				move_char (ch, pExit, 0);
				return;
			}
		ch->SendText ("You cannot climb here.\n\r");
		return;
	}

	if ((pExit = find_door (ch, argument, TRUE)) != NULL
	  && pExit->CanClimb ()) {
		move_char (ch, pExit, 0);
		return;
	}
	ch->SendText ("You cannot climb there.\n\r");
}


// "enter" something (moves through an exit)			-Thoric
void do_enter (CCharacter *ch, char *argument)
{
	CExitData	*pExit;
	BOOL		found;

	found = FALSE;
	if (argument [0] == '\0') {
		pExit = ch->GetInRoom ()->first_exit;
		for ( ; pExit; pExit = pExit->GetNext ())
			if (pExit->CanEnter () && ! pExit->IsDisabled ()) {
				move_char (ch, pExit, 0);
				return;
			}

		if (ch->GetInRoom ()->sector_type != SECT_INSIDE
		  && ch->IsOutside ()) {
			pExit = ch->GetInRoom ()->first_exit;
			for ( ; pExit; pExit = pExit->GetNext ())
				if (! pExit->IsDisabled () && pExit->GetToRoom ()
				  && (pExit->GetToRoom ()->sector_type == SECT_INSIDE
				  || pExit->GetToRoom ()->IsIndoors ())) {
					move_char (ch, pExit, 0);
					return;
				}
		}

		ch->SendText ("You cannot find an entrance here.\n\r");
		return;
	}

	if ((pExit = find_door (ch, argument, TRUE)) != NULL
	  && pExit->CanEnter ()) {
		move_char (ch, pExit, 0);
		return;
	}
	ch->SendText ("You cannot enter that.\n\r");
}


// Leave through an exit.					-Thoric
void do_leave (CCharacter *ch, char *argument)
{
	CExitData	*pExit;
	BOOL		found;

	found = FALSE;
	if (argument [0] == '\0') {
		pExit = ch->GetInRoom ()->first_exit;
		for ( ; pExit; pExit = pExit->GetNext ())
			if (! pExit->IsDisabled () && pExit->CanLeave ()) {
				move_char (ch, pExit, 0);
				return;
			}

		if (ch->GetInRoom ()->sector_type == SECT_INSIDE || ch->IsIndoors ()){
			pExit = ch->GetInRoom ()->first_exit;
		for ( ; pExit; pExit = pExit->GetNext ())
			if (! pExit->IsDisabled () && pExit->GetToRoom ()
			  && pExit->GetToRoom ()->sector_type != SECT_INSIDE
			  && ! pExit->GetToRoom ()->IsIndoors ()) {
				move_char (ch, pExit, 0);
				return;
			}
		}

		ch->SendText ("You cannot find an exit here.\n\r");
		return;
	}

	if ((pExit = find_door (ch, argument, TRUE)) != NULL
	  && pExit->CanLeave ()) {
		move_char (ch, pExit, 0);
		return;
	}
	ch->SendText ("You cannot leave that way.\n\r");
}