#include <stdio.h>
#include "config.h"
#include "structs.h"
#include "functions.h"
#include "utils.h"
#include "command.h"
#include "db.h"
#include "weather.h"

/* EXTERNAL VARIABLES ETC */
extern struct descriptor_data *desc_list;

extern char buf[SMALL_BUFSIZE];
extern char buf1[SMALL_BUFSIZE];
extern char buf2[SMALL_BUFSIZE];

void  do_who(struct char_data *ch)
{
   char c_invis=0;
   int   i;
   struct descriptor_data *who, *next_who;
   char	tmp[40+1];

   if(ch->LEVEL >= WIZARD || BTST(ch->SPECIALS,SEE_INVIS))
      c_invis=ch->LEVEL;

	for(who=desc_list;who;who=next_who) 
	{
		next_who=who->next_in_list;
   	if((who->character->INVIS <= c_invis || who == ch->desc) && who->connected >= CON_LINKLESS) {
     		if(who->character->LEVEL < WIZARD) 	{
        		sprintf(buf,"[\033[36m%d %s]\t",who->character->LEVEL,class[who->character->CLASS].abbr);
        	} else {
        		for(i=0;plevel[i].level != IMP;i++) {
            	if(who->character->LEVEL < plevel[i+1].level)
              		break;
            }
            sprintf(buf,"[%s]\t",plevel[i].abbr);
         }	
			sprintf(tmp,"\033[1m\033[33m%s\033[0m",who->NAME);
			strcat(buf,tmp);
			if(who->connected > CON_PLYNG) 
			{
				sprintf(buf1,"%s \033[36m%s\033[0m (Private: %d)",buf,who->TITLE,who->connected - CON_PRIVATE);
			} 
			else 
			{
				sprintf(buf1,"%s \033[36m%s\033[0m (\033[35m%s\033[0m)",buf,who->TITLE,show_pos[who->character->POSITION].shrt);
			}
         get_flags(buf1,who->character);
         send_2_output(buf1,ch->desc);
      }
   }
   return;
}

void  do_look(struct char_data *ch, char *arg)
{
   struct extra_desc *xdesc, *xdesc_next;
   struct object_data *obj, *obj_next;
   struct exit_data *exit, *exit_next;
   struct char_data *who;
   struct room_data *room;
   int    room_num,one_arg=0;


	/* Am I out of the created Universe or, short of a light? */
	if(!ch->in_room) {
		if(ch->LEVEL >= GOD) {
        	sprintf(buf,"\033[1m\033[33mNowhere\033[0m [x=%d,y=%d,z=%d][room=%d][zone=%d]\n\r",
				GET_X(ch),GET_Y(ch),GET_Z(ch),ROOM_NUM(ch),GET_ZONE(ch));
			send_2_output(buf,ch->desc);
		} else {
			send_2_output("Nowhere\n\r",ch->desc);
		}
		return;
	} else if((ch->LEVEL < WIZARD) && ((!ch->in_room->LIGHT && BTST(ch->in_room->flags,IS_DARK)) || 
      	(BTST(ch->in_room->flags,OUTSIDE) && TICKS > 8))) {
      send_2_output("It is too dark to see.\n\r",ch->desc);
      return;
   }

   if(!get_arg(buf,arg,1)) {
      if(ch->LEVEL >= IMP) {
      	sprintf(buf,"\033[1m\033[33m%s\033[0m [x=%d,y=%d,z=%d][room=%d][zone=%d]\n\r",
				ch->in_room->name,GET_X(ch),GET_Y(ch),GET_Z(ch),ROOM_NUM(ch),GET_ZONE(ch));
      } else {
      	sprintf(buf,"\033[1m\033[33m%s\033[0m (%s)\n\r", ch->in_room->name,DAY_NIGHT);
      }
      send_2_output(buf,ch->desc);
      if(!(*arg == '+' && BTST(ch->PREFS,BRIEF))) {
         sprintf(buf,"\033[36m%s\033[0m\n\r",ch->in_room->description);
         send_2_output(buf,ch->desc);
      }
      show_people(ch->in_room,ch);
      for(obj=ch->in_room->objects;obj;obj=obj_next) {
         obj_next=obj->next_in_room;
         sprintf(buf,"%s\n",obj->still_desc);
         send_2_output(buf,ch->desc);
      }
      if(ch->LEVEL < IMMORT && BTST(ch->in_room->flags,DEATH_TRAP)) {
         change_status(ch, CON_CLOSE);
		}
   } else if(!get_target(buf1,arg,1)) {
      send_2_output("Look at what??\n\r",ch->desc);
      return;
   } else if(!str_cmp(buf,"in")) {
      /* We want to look in a container. Nice and simple. */
      ;
   } else { //// CRTH - look at an object.
		if(!(who = get_ch_in_room(ch, buf1))) {
			// We are loooking at an object in the room!!
			for(obj=CHAR_ROOM->objects;obj;obj=obj_next)
			{
				obj_next = obj->next_in_room;
				if(obj_cmp(buf1,obj))
				{
					if(obj->exam_desc != NULL)
						sprintf(buf,"%s\n\r",obj->exam_desc);
					else
						sprintf(buf,"You see nothing unusual.\n\r");
					send_2_output(buf,ch->desc);
					break;
				}
			}
			if(!obj)
				send_2_output("Look at what??\n\r",ch->desc);
		} else {
			look_at(ch,who);
		} 
	} 
}


int   show_people(struct room_data *room, struct char_data *ch)
{
   struct char_data *who, *who_next;
   int    count=0;

   for(who=room->people;who;who=who_next) {
      who_next=who->next_in_room;
      if(who != ch && see_char_chk(ch,who)) {
			if(who->STATE == STAT_OK)
			{
				if(who->l_name)
   	      	sprintf(buf,"%s%s ",who->l_name,show_pos[who->POSITION].txt);
				else
         		sprintf(buf,"%s%s ",who->name,show_pos[who->POSITION].txt);
			}
			else
			{
				if(who->l_name)
   	      	sprintf(buf,"%s%s %s",who->l_name,show_pos[who->POSITION].txt,show_stat[who->STATE].shrt);
				else
         		sprintf(buf,"%s%s %s",who->name,show_pos[who->POSITION].txt,show_stat[who->STATE].shrt);
			}
         get_flags(buf,who);
         send_2_output(buf,ch->desc);
         count++;
      }
   }
   return(count);
}

void  look_at(struct char_data *ch, struct char_data *pl)
{
   int   c,count=0;

   /* What's his state of health? */
   c = (((pl->HIT * 100) / pl->MAX_HIT) / 13);
   /* Does he have a description? */
   if(pl->description) {
   	sprintf(buf,"%s\n\r%s %s\n\r",pl->description,pl->name,health[c]);
	} else {
   	sprintf(buf,"%s %s\n\r",pl->name,health[c]);
   }
	send_2_output(buf,ch->desc);

   /* Show the user what the other person is using */
   sprintf(buf,"%s is using: \n\r",pl->name);
   for(c=0;c<MAX_WEAR;c++) {
      if(pl->wearing[c]) {
         send_2_output(buf,ch->desc);
         sprintf(buf,"%s %s\n\r",pl->wearing[c]->name,worn[c].where);
         count++;
      }
   }
   if(count) {
      send_2_output(buf,ch->desc);
      sprintf(buf,"%d item(s).\n\r",count);
      send_2_output(buf,ch->desc);
   }

   /* Let the room know about the event */
   if(ch == pl) {
      sprintf(buf,"looks at %s self",sex[ch->SEX].b);
      send_2_room(ch,IGNR_INVIS,buf);
   } else {
      sprintf(buf,"looks at %s",pl->name);
      send_2_xcept(ch,pl,IGNR_INVIS,buf);
      send_2_char(ch,pl,IGNR_INVIS,"looks at you");
   }
   return;
}


