/* ************************************************************************
*  file: act.comm.c , Implementation of commands.  Part of Copper DikuMUD *
*  Usage : Communication.                                                 *
*  Copyright (C) 1990, 1991 - see 'license.doc' for complete information. *
***************************************************************************/

/* $Id: $ */

#include <stdio.h>
#include <strings.h>

#include "structs.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "handler.h"
#include "db.h"
#include "spells.h"

/* extern variables */

extern struct char_data *character_list;
extern struct room_data *world;
extern struct zone_data *zone_table;
extern struct descriptor_data *descriptor_list;

/* This updates values for everybody in the character list which */
/* control their ability to "speak" without overloading the system */
void update_ovl(void)
{
	struct char_data *ch;

	for(ch=character_list;ch;ch=ch->next) {
		if(ch->specials.ovl_count > 0) {
			if(++(ch->specials.ovl_timer) > OVL_PULSE) {
				ch->specials.ovl_timer = 0;
				ch->specials.ovl_count -=
					(isdweeb(ch) ? OVL_DWEEB_LIMIT :
							OVL_NORMAL_LIMIT);
			}
		}
	}
}

bool is_overload(struct char_data *ch)
{
	if(++ch->specials.ovl_count > (isdweeb(ch) ? OVL_DWEEB_LIMIT :
							OVL_NORMAL_LIMIT)) {
		send_to_char("SHUT UP ALREADY!!! Your actions lose meaning at this speed!\n\r",ch);
		/* So that nobody will be locked out for too long */
		ch->specials.ovl_count=MIN(ch->specials.ovl_count,
						2*OVL_NORMAL_LIMIT);
		return(TRUE);
	}
	return(FALSE);
}

bool is_silent(struct char_data *ch,bool showit)
{
	if(IS_SET(zone_table[world[ch->in_room].zone].flags,ZONE_SILENT) &&
			!IS_TRUSTED(ch)){
		if(showit)
			send_to_char("The world is deathly silent, and no noise forms...\n\r",ch);
		return(TRUE);
	}
	return(FALSE);
}

bool can_talk(struct char_data *ch)
{
	if(IS_HUMANOID(ch) || GET_CLASS(ch)==CLASS_DRAGON ||
			GET_CLASS(ch)==CLASS_GIANT || GET_CLASS(ch)==CLASS_DEMON
			|| GET_CLASS(ch)==CLASS_OTHER)
		return(TRUE);
	act("$n tries to communicate something but can't!",TRUE,ch,0,0,TO_ROOM);
	act("Who said you could talk?",FALSE,ch,0,0,TO_CHAR);
	return(FALSE);
}

void do_say(struct char_data *ch, char *argument, int cmd)
{
	int i;
	static char buf[MAX_STRING_LENGTH];

	for (i = 0; *(argument + i) == ' '; i++);

	if (!*(argument + i))
		send_to_char("Yes, but WHAT do you want to say?\n\r", ch);
	else if(!is_overload(ch)&&!is_silent(ch,TRUE)&&can_talk(ch))
	{
		sprintf(buf,"$n says '%s'", argument + i);
		act(buf,FALSE,ch,0,0,TO_ROOM);
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf,"You say, '%s'\n\r",argument + i);
			send_to_char(buf,ch);
		} else
			send_to_char("Ok.\n\r", ch);
	}
}


/* Changed to only shout to people in your zone */
/* Changed it back. I like global shouts. Changed so only level 2 or above */
/* can use. -Swiftest */

void do_shout(struct char_data *ch, char *argument, int cmd)
{
	static char buf1[MAX_STRING_LENGTH];
	struct descriptor_data *i;


	if (!IS_NPC(ch) && (IS_SET(ch->specials.act, PLR_NOSHOUT) ||
			    IS_SET(ch->specials.act, PLR_ISDWEEB) ||
			    IS_SET(ch->specials.act, PLR_MRTLNOSHOUT)))
	{
		send_to_char("You can't shout!!\n\r", ch);
		return;
	}

	if (!IS_NPC(ch) && GET_EXP(ch)<1500) {
		send_to_char("Due to misuse, you need to have at least 1,500 exp to shout.\n\r",ch);
		return;
	}

	for (; *argument == ' '; argument++);

	if (!(*argument))
		send_to_char("Shout? Yes! Fine! Shout we must, but WHAT??\n\r", ch);
	else if(!is_overload(ch)&&!is_silent(ch,TRUE)&&can_talk(ch))
	{
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf1,"You shout, '%s'\n\r",argument);
			send_to_char(buf1,ch);
		} else
			send_to_char("Ok.\n\r", ch);
		sprintf(buf1, "$n shouts '%s'", argument);

    	for (i = descriptor_list; i; i = i->next)
      	if (i->character != ch && !i->connected &&
	    !is_silent(i->character,FALSE) &&
            !IS_SET(i->character->specials.act, PLR_MRTLNOSHOUT) &&
            !IS_SET(i->character->specials.act, PLR_NOSHOUT))
			act(buf1, 0, ch, 0, i->character, TO_VICT);
	}
	
	if(ch->desc)
		ch->desc->last_input[0]='\0';
}

void do_yell(struct char_data *ch, char *argument, int cmd)
{
	static char buf1[MAX_STRING_LENGTH];
	struct descriptor_data *i;


	if (!IS_NPC(ch) && (IS_SET(ch->specials.act, PLR_NOSHOUT) ||
	    IS_SET(ch->specials.act, PLR_ISDWEEB)))
	{
		send_to_char("You can't yell!!\n\r", ch);
		return;
	}

	if (!IS_NPC(ch) && GET_EXP(ch)<1500) {
		send_to_char("Due to misuse, you need to have at least 1,500 exp to yell.\n\r",ch);
		return;
	}

	for (; *argument == ' '; argument++);

	if (!(*argument))
		send_to_char("So, what do you want to yell, bud?\n\r", ch);
	else if(!is_overload(ch)&&!is_silent(ch,TRUE)&&can_talk(ch))
	{
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf1,"You yell, '%s'\n\r",argument);
			send_to_char(buf1,ch);
		} else
			send_to_char("Ok.\n\r", ch);
		sprintf(buf1, "$n yells '%s'", argument);

    	for (i = descriptor_list; i; i = i->next)
 	    if (i->character != ch && !i->connected &&
		!IS_SET(i->character->specials.act, PLR_NOSHOUT) && 
		(world[i->character->in_room].zone == 
		world[ch->in_room].zone) &&
		(!(world[ch->in_room].room_flags & INDOORS) ||
		i->character->in_room==ch->in_room))
				act(buf1, 0, ch, 0, i->character, TO_VICT);
	}
}

void do_tell(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *vict, *k;
	struct follow_type *f;
	char name[MAX_INPUT_LENGTH], message[MAX_STRING_LENGTH],
		buf[MAX_STRING_LENGTH];

	half_chop(argument,name,message);

	if(!*name || !*message)
		send_to_char("Who do you wish to tell what??\n\r", ch);
	else if(is_overload(ch) || is_silent(ch,TRUE) || !can_talk(ch))
		return;
	else if(!(strcmp(name,"group"))) {
		if(!IS_AFFECTED(ch,AFF_GROUP))
			send_to_char("You are not grouped!\n\r",ch);
		else {
			sprintf(buf,"%s tells the group '%s'\n\r",
		 (IS_NPC(ch) ? ch->player.short_descr : GET_NAME(ch)), message);
			k = (ch->master) ? ch->master : ch;
			if(IS_AFFECTED(k,AFF_GROUP) && !is_silent(k,FALSE) &&
					k!=ch && GET_POS(k)>POSITION_SLEEPING)
				send_to_char(buf,k);
			for(f = k->followers ; f ; f = f->next) {
				if(IS_AFFECTED(f->follower,AFF_GROUP) &&
				!is_silent(f->follower,FALSE) &&
				f->follower!=ch &&
				GET_POS(f->follower)>POSITION_SLEEPING)
					send_to_char(buf,f->follower);
			}
			if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
				sprintf(buf,"You tell the group, '%s'\n\r",message);
				send_to_char(buf,ch);
			} else
				send_to_char("Ok.\n\r",ch);
		}
	} else if (!(vict = get_char_vis(ch, name)))
		send_to_char("No-one by that name here..\n\r", ch);
	else if (ch == vict)
		send_to_char("You try to tell yourself something.\n\r", ch);
	else if (((GET_POS(vict) == POSITION_SLEEPING) ||
                 (IS_SET(vict->specials.act, PLR_NOTELL)))&&
		 (GET_LEVEL(ch)<LV_DEMIGOD && !IS_TRUSTED(ch))||
		 (is_silent(vict,FALSE)) || (!IS_NPC(vict) && !vict->desc)) {
		act("$E can't hear you.",FALSE,ch,0,vict,TO_CHAR);
	} else {
		sprintf(buf,"%s tells you '%s'\n\r",
		  (IS_NPC(ch) ? ch->player.short_descr : GET_NAME(ch)),message);
		send_to_char(buf, vict);
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf,"You tell %s, '%s'\n\r",fname(GET_NAME(vict)),message);
			send_to_char(buf,ch);
		} else
			send_to_char("Ok.\n\r", ch);
	}
}



void do_whisper(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *vict;
	char name[MAX_INPUT_LENGTH], message[MAX_STRING_LENGTH],
		buf[MAX_STRING_LENGTH];

	half_chop(argument,name,message);

	if(!*name || !*message)
		send_to_char("Who do you want to whisper to.. and what??\n\r", ch);
	else if (!(vict = get_char_room_vis(ch, name)))
		send_to_char("No-one by that name here..\n\r", ch);
	else if(is_overload(ch) || is_silent(ch,TRUE) || !can_talk(ch)) {
		return;
	} else if (vict == ch) {
		act("$n whispers quietly to $mself.",FALSE,ch,0,0,TO_ROOM);
		send_to_char(
			"You can't seem to get your mouth close enough to your ear...\n\r",
			 ch);
	} else {
		sprintf(buf,"$n whispers to you, '%s'",message);
		act(buf, FALSE, ch, 0, vict, TO_VICT);
		send_to_char("Ok.\n\r", ch);
		act("$n whispers something to $N.", FALSE, ch, 0, vict, TO_NOTVICT);
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf,"You whisper to %s, '%s'\n\r",fname(GET_NAME(vict)),message);
			send_to_char(buf,ch);
		} else
			send_to_char("Ok.\n\r",ch);
	}
}


void do_ask(struct char_data *ch, char *argument, int cmd)
{
	struct char_data *vict;
	char name[MAX_INPUT_LENGTH], message[MAX_STRING_LENGTH],
		buf[MAX_STRING_LENGTH];

	half_chop(argument,name,message);

	if(!*name || !*message)
		send_to_char("Who do you want to ask something.. and what??\n\r", ch);
	else if (!(vict = get_char_room_vis(ch, name)))
		send_to_char("No-one by that name here..\n\r", ch);
	else if (vict == ch)
	{
		act("$n quietly asks $mself a question.",FALSE,ch,0,0,TO_ROOM);
		send_to_char("You think about it for a while...\n\r", ch);
	}
	else if(!is_overload(ch)&&!is_silent(ch,TRUE) && can_talk(ch))
	{
		sprintf(buf,"$n asks you '%s'",message);
		act(buf, FALSE, ch, 0, vict, TO_VICT);
		act("$n asks $N a question.",FALSE,ch,0,vict,TO_NOTVICT);
		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf,"You ask %s, '%s'\n\r",fname(GET_NAME(vict)),message);
			send_to_char(buf,ch);
		} else
			send_to_char("Ok.\n\r",ch);
	}
}



#define MAX_NOTE_LENGTH 1000      /* arbitrary */

void do_write(struct char_data *ch, char *argument, int cmd)
{
	struct obj_data *paper = 0, *pen = 0;
	char papername[MAX_INPUT_LENGTH], penname[MAX_INPUT_LENGTH],
		buf[MAX_STRING_LENGTH];

	argument_interpreter(argument, papername, penname);

	if (!ch->desc)
		return;

	if (!*papername)  /* nothing was delivered */
	{   
		send_to_char(
			"Write? with what? ON what? what are you trying to do??\n\r", ch);
		return;
	}
	if (*penname) /* there were two arguments */
	{
		if (!(paper = get_obj_in_list_vis(ch, papername, ch->carrying)))
		{
			sprintf(buf, "You have no %s.\n\r", papername);
			send_to_char(buf, ch);
			return;
		}
		if (!(pen = get_obj_in_list_vis(ch, penname, ch->carrying)))
		{
			sprintf(buf, "You have no %s.\n\r", papername);
			send_to_char(buf, ch);
			return;
		}
	}
	else  /* there was one arg.let's see what we can find */
	{			
		if (!(paper = get_obj_in_list_vis(ch, papername, ch->carrying)))
		{
			sprintf(buf, "There is no %s in your inventory.\n\r", papername);
			send_to_char(buf, ch);
			return;
		}
		if (paper->obj_flags.type_flag == ITEM_PEN)  /* oops, a pen.. */
		{
			pen = paper;
			paper = 0;
		}
		else if (paper->obj_flags.type_flag != ITEM_NOTE)
		{
			send_to_char("That thing has nothing to do with writing.\n\r", ch);
			return;
		}

		/* one object was found. Now for the other one. */
		if (!ch->equipment[HOLD])
		{
			sprintf(buf, "You can't write with a %s alone.\n\r", papername);
			send_to_char(buf, ch);
			return;
		}
		if (!CAN_SEE_OBJ(ch, ch->equipment[HOLD]))
		{
			send_to_char("The stuff in your hand is invisible! Yeech!!\n\r", ch);
			return;
		}
		
		if (pen)
			paper = ch->equipment[HOLD];
		else
			pen = ch->equipment[HOLD];
	}
			
	/* ok.. now let's see what kind of stuff we've found */
	if (pen->obj_flags.type_flag != ITEM_PEN)
	{
		act("$p is no good for writing with.",FALSE,ch,pen,0,TO_CHAR);
	}
	else if (paper->obj_flags.type_flag != ITEM_NOTE)
	{
		act("You can't write on $p.", FALSE, ch, paper, 0, TO_CHAR);
	}
	else if (paper->action_description)
		send_to_char("There's something written on it already.\n\r", ch);
	else
	{
		/* we can write - hooray! */
				
		send_to_char("Ok.. go ahead and write.. end the note with a @.\n\r",
			ch);
		act("$n begins to jot down a note.", TRUE, ch, 0,0,TO_ROOM);
		ch->desc->str = &paper->action_description;
		ch->desc->max_str = MAX_NOTE_LENGTH;
	}
}

void do_auction(struct char_data *ch, char *argument, int cmd)
{
	static char buf1[MAX_STRING_LENGTH];
	struct descriptor_data *i;

	for (; *argument == ' '; argument++);

	if(strlen(argument)==0) {
		if(IS_SET(ch->specials.act,PLR_AUCTION)) {
			REMOVE_BIT(ch->specials.act,PLR_AUCTION);
			send_to_char("You will not hear the auction channel anymore.\n\r",ch);
		} else {
			SET_BIT(ch->specials.act,PLR_AUCTION);
			send_to_char("You will now hear the auction channel.\n\r",ch);
		}
		return;
	} else if(!strcmp(argument,"on")) {
		SET_BIT(ch->specials.act,PLR_AUCTION);
		send_to_char("You will now hear the auction channel.\n\r",ch);
		return;
	} else if(!strcmp(argument,"off")) {
		REMOVE_BIT(ch->specials.act,PLR_AUCTION);
		send_to_char("You will not hear the auction channel anymore.\n\r",ch);
		return;
	}

	/* Auction messages go here */


	if ((!IS_SET(ch->specials.act, PLR_AUCTION) ||
	    IS_SET(ch->specials.act, PLR_ISDWEEB) ||
	    IS_SET(ch->specials.act, PLR_NOSHOUT)))
	{
		send_to_char("You can't auction!!\n\r", ch);
		return;
	}

	if (!IS_NPC(ch) && GET_EXP(ch)<1500) {
		send_to_char("Due to misuse, you need to have at least 1,500 exp to auction.\n\r",ch);
		return;
	}

	if(!is_overload(ch) && !is_silent(ch,TRUE) && can_talk(ch))
	{
		sprintf(buf1, "AUCTION: [$n] %s", argument);

	    	for (i = descriptor_list; i; i = i->next)
      			if (i->character != ch && !i->connected &&
					!is_silent(ch,FALSE) &&
       	                   IS_SET(i->character->specials.act, PLR_AUCTION) ) {
				act(buf1, 0, ch, 0, i->character, TO_VICT);
			}

		if(IS_SET(ch->specials.act,PLR_MSG_ECHO)) {
			sprintf(buf1,"You auction: %s\n\r",argument);
			send_to_char(buf1,ch);
		} else
			send_to_char("Ok.\n\r",ch);
	}
	return;
}