/*
 * NAME: tml_roomcheck()
 * VERSION: 1.0b
 * DESCRIPTION: 
 *   This is a utility for builders to help figure out problems in thier 
 * areas and for implementors and others to define other problems as they
 * exist.  This utility will check for rooms with no names, rooms with
 * names that are not bright white (TML requirement), rooms that are unlinked,
 * rooms that are private without SAFE (hiding rooms), rooms without descriptions,
 * and rooms with the keyword "delete" in them.  This is used extensively by
 * my builder staff to keep the areas clean. I have removed some specific 
 * TML stuff but have left others in the code.  My webpage has all the other
 * required functionality for it. Changing and adding to the code should be 
 * simple.
 * 
 * PROGRAMMER: The Mage
 * GAME: The Mage's Lair
 * WEBSITE: www.mageslair.net
 * DEPENDECIES: tml_mprintf()
 *
 * LICENSE: Use of this code is with the DiKU original Licence and 
 * you cannot charge for its use. Credit in the code should be maintained
 * and if the code is released public, credit is due to the authors of
 * the code.  Notification of code use is not required but is appriciated
 * to the website above. Porting to other codebases is allowed with notification
 * to the authors.  The License shall be continue through all updates and upgrades
 * of this function.  This is intended to prevent people from charging for the
 * code directly.
 *
 * WARRENTY: There is no warrenty, implied or expressed for the code contained.
 *
 * NOTE: Please enjoy the use.  BUG fixes should be directed to the PORTED coders
 * unless they cannot be located.  Enjoy the Code and More will be forthcomming.
 *
 */

/* add to interp.h */
DECLARE_DO_FUN( do_roomcheck );

/* add to interp.c */
{ "roomcheck",	do_roomcheck,	POS_RESTING,	IM,  LOG_NORMAL, 1 },

/* add to merc.h */
#define MAX_EXITS 6 /* (or how many your mud has*/

/* in struct room_index_data */
bool linked;


void display_roomcheck_syntax(CHAR_DATA *ch)
{
  send_to_char("SYNTAX:\n",ch);
  send_to_char("roomcheck <command>\n\r",ch);
  send_to_char("    names   - Check rooms for lack of names.\n\r",ch);
  send_to_char("    white   - Check Room names for white.\n\r",ch);
  send_to_char("    link    - Check Room's for links.\n\r",ch);
  send_to_char("    private - Check Room for Private w/o SAFE.\n\r",ch);
  send_to_char("    descr   - Check Room for BLANK descriptions.\n\r",ch);
  send_to_char("    delete  - Check DELETED status on rooms.\n\r",ch);
}

void do_roomcheck( CHAR_DATA *ch, char *argument )
{
  char buf[MAX_INPUT_LENGTH];
  BUFFER *buffer;
  char arg1[MIL];
  int first = TRUE;
  OBJ_INDEX_DATA *pObjIndex;
  AREA_DATA *area;
  ROOM_INDEX_DATA *room, *room2;
  int number = 0, max_found = 100, vnum=0, tvnum=0, exit = 0;

  argument = one_argument( argument, arg1 );

  if (!str_cmp(arg1,"\0")) {
    display_roomcheck_syntax(ch);
    return;
  }

  buffer = new_buf();
  for ( area = area_first; area; area = area->next )
    {
      for ( vnum = area->min_vnum; vnum <= area->max_vnum; vnum++ )
	{
	  if ( !( room = get_room_index( vnum ) ) )
	    continue;
	  if (!strcmp(arg1,"names")) {
	    strip_color(buf,room->name);
	    if ( !strstr( "asadafa",buf ) )
	      continue;

	    ++number; /*count it if we found a match */
 
	    mprintf( sizeof(buf), buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  } else if (!strcmp(arg1,"white")) {
	    if ( strstr( room->name,"{W" ) )
	      continue;

	    ++number; /*count it if we found a match */
 
	    mprintf( sizeof(buf),buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  } else if (!strcmp(arg1,"private")) {
	    if ( !IS_SET(room->room_flags, ROOM_PRIVATE) )
	      continue;
	    if ( IS_SET(room->room_flags, ROOM_SAFE) )
	      continue;

	    ++number; /*count it if we found a match */
 
	    mprintf( sizeof(buf),buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  } else if (!strcmp(arg1,"delete")) {
	    if (!is_name("delete",room->name))
	      continue;
	    
	    ++number; /*count it if we found a match */
 
	    mprintf( sizeof(buf),buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  } else if (!strcmp(arg1,"descr")) {
	    if (room->description[0] != '\0') 
	      continue;

	    ++number; /*count it if we found a match */
	    mprintf( sizeof(buf),buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  } else if (!strcmp(arg1, "link")) {

	    if (!first) {
	      /* Clear the Linked Flag */
	      for(tvnum=0; tvnum<=top_vnum_room; tvnum++) 
		{
		  if( (room2 = get_room_index(tvnum)) == NULL)
		    continue;
		  room2->linked = FALSE;
		}

	      /* Find all the rooms each room is linked and set the flag */
	      for(tvnum=0; tvnum<=top_vnum_room; tvnum++) 
		{
      
		  if( (room2 = get_room_index(tvnum)) == NULL)
		    continue;
		  for(exit=0; exit<MAX_EXITS; exit++) {
		    if( room2->exit[exit]) {
		      room2->linked = TRUE;
		      room2->exit[exit]->u1.to_room->linked = TRUE;
		    }
		  }
		}
	      /* Find all the portal endings from the objects */
	      for (tvnum = 0; tvnum < top_obj_index; tvnum++)
		if ((pObjIndex = get_obj_index(tvnum)) != NULL)
		  {
		    if (pObjIndex->item_type != ITEM_PORTAL)
		      continue;
		    room2 = get_room_index(pObjIndex->value[3]);
		    if (room2)
		      room2->linked = TRUE;
		  }
	 
	    
	      /* Find all the pet storage rooms */
	      for(tvnum=0; tvnum<=top_vnum_room; tvnum++) 
		{
      
		  if( (room2 = get_room_index(tvnum)) == NULL)
		    continue;

		  if ( !IS_SET(room2->room_flags, ROOM_PET_SHOP) )
		    continue;
      
		  if( (room2 = get_room_index(tvnum+1)) == NULL)
		    continue;
      
		  room2->linked = TRUE;
		}
	      first = FALSE;
	    }

	    if (room->linked)
	      continue;
	    ++number; /*count it if we found a match */
	    mprintf( sizeof(buf),buf, "%3d) [%5d] %s (%s)\n\r",
		     number, vnum, room->name, area->name );
	    add_buf( buffer, buf );

	    if ( number >= max_found )
	      break;
	  
	  }else {
	    display_roomcheck_syntax(ch);
	    return;
	  }
	  if ( number >= max_found )
	    break;
	}
    }
  if ( !number )
    send_to_char( "No matching criteria.\n\r", ch );
  else
    page_to_char(buf_string(buffer),ch);
    
  free_buf(buffer);

}
