1stMUD/corefiles/
1stMUD/gods/
1stMUD/player/
1stMUD/win32/
1stMUD/win32/ROM/
/**************************************************************************
*  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
*  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
*                                                                         *
*  Merc Diku Mud improvements copyright (C) 1992, 1993 by Michael         *
*  Chastain, Michael Quan, and Mitchell Tse.                              *
*                                                                         *
*  In order to use any part of this Merc Diku Mud, you must comply with   *
*  both the original Diku license in 'license.doc' as well the Merc       *
*  license in 'license.txt'.  In particular, you may not remove either of *
*  these copyright notices.                                               *
*                                                                         *
*  Much time and thought has gone into this software and you are          *
*  benefiting.  We hope that you share your changes too.  What goes       *
*  around, comes around.                                                  *
***************************************************************************
*       ROM 2.4 is copyright 1993-1998 Russ Taylor                        *
*       ROM has been brought to you by the ROM consortium                 *
*           Russ Taylor (rtaylor@hypercube.org)                           *
*           Gabrielle Taylor (gtaylor@hypercube.org)                      *
*           Brian Moore (zump@rom.org)                                    *
*       By using this code, you have agreed to follow the terms of the    *
*       ROM license, in the file Rom24/doc/rom.license                    *
***************************************************************************
*       1stMUD ROM Derivative (c) 2001-2002 by Ryan Jennings              *
*            http://1stmud.dlmud.com/  <r-jenn@shaw.ca>                   *
***************************************************************************/
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "merc.h"

CH_CMD(do_btalk)
{
	if (!ch || IS_NPC(ch))
		return;

	public_ch(ch, argument,
			  "{W{{" CTAG(_BTALK1) "Buddy{W}" CTAG(_BTALK2), COMM_NOBUDDY,
			  spec_buddy_flag, LAST_BTALK);
	return;
}

void buddy_offline args((CHAR_DATA * ch, char *arg));

CH_CMD(do_buddy)
{
	CHAR_DATA *fch;
	char arg[MIL];
	int pos;
	bool found = FALSE;

	if (IS_NPC(ch))
	{
		chprintln(ch, "Mobiles don't have buddies =).");
		return;
	}

	argument = one_argument(argument, arg);

	if (IS_NULLSTR(arg))
	{
		if (IS_NULLSTR(ch->pcdata->buddies[0]))
		{
			chprintln(ch, "You have no buddies!");
			return;
		}
		chprintln(ch, "" CTAG(_BTALK1) "Your " CTAG(_BTALK2) "Buddies"
				  CTAG(_BTALK1) ":{x");
		for (pos = 0; pos < MAX_BUDDY; pos++)
		{
			if (IS_NULLSTR(ch->pcdata->buddies[pos]))
				break;
			fch = get_char_world(NULL, ch->pcdata->buddies[pos]);
			chprintlnf(ch, "" CTAG(_BTALK1) "%02d)" CTAG(_BTALK2)
					   " %-12s %s{x", pos + 1,
					   capitalize(ch->pcdata->buddies[pos]), (!fch
															  ||
															  !can_see
															  (ch,
															   fch)) ?
					   "{R(Offline){x" : check_buddy(fch,
													 ch) ==
					   -1 ? "{M(Wannabe){x" : !fch->desc ? "{C(LinkDead){x"
					   : "{G(Online){x");
		}
		return;
	}
	else if (!str_prefix(arg, "clear"))
	{
		for (pos = 0; pos < MAX_BUDDY; pos++)
		{
			if (IS_NULLSTR(ch->pcdata->buddies[pos]))
				break;
			replace_string(ch->pcdata->buddies[pos], "");
		}
		chprintln(ch, "No one is your buddy!");
		return;
	}
	else if (!str_prefix(arg, "wannabe"))
	{
		chprintln(ch, "" CTAG(_BTALK1)
				  "People online who want to be your buddy:{x\n\r");
		for (fch = player_first; fch != NULL; fch = fch->next_player)
		{
			if (check_buddy(fch, ch) != -1 && check_buddy(ch, fch) == -1)
			{
				chprintlnf(ch, "" CTAG(_BTALK1) "- " CTAG(_BTALK2) "%-12s{x",
						   fch->name);
				found = TRUE;
			}
		}
		if (!found)
			chprintln(ch, "" CTAG(_BTALK1) "- " CTAG(_BTALK2) "No one.{x");
		return;
	}
	else
	{
		for (pos = 0; pos < MAX_BUDDY; pos++)
		{
			if (IS_NULLSTR(ch->pcdata->buddies[pos]))
				break;
			if (found)
			{
				replace_string(ch->pcdata->buddies[pos - 1],
							   ch->pcdata->buddies[pos]);
				ch->pcdata->buddies[pos] = NULL;
				continue;
			}
			if (!str_prefix(arg, ch->pcdata->buddies[pos]))
			{
				if ((fch = get_char_world(ch, ch->pcdata->buddies[pos]))
					!= NULL)
				{
					if (check_buddy(fch, ch) != -1)
						chprintlnf(fch, "%s is no longer your buddy.",
								   ch->name);
					else
						chprintlnf(fch, "%s no longer wants to be your buddy.",
								   ch->name);
				}
				chprintlnf(ch, "%s is no longer your buddy.", capitalize(arg));
				replace_string(ch->pcdata->buddies[pos], "");
				found = TRUE;
			}
		}

		if (found)
			return;

		if (pos >= MAX_BUDDY)
		{
			chprintln(ch, "Too many people, remove a name.");
			return;
		}
		if ((fch = get_char_world(ch, arg)) == NULL)
		{
			buddy_offline(ch, arg);
			return;
		}
		if (fch == ch)
		{
			chprintln(ch, "Are you that lonely?");
			return;
		}
		if (IS_NPC(fch))
		{
			chprintln(ch, "Uh, nope.");
			return;
		}

		replace_string(ch->pcdata->buddies[pos], arg);
		if (check_buddy(fch, ch) == -1)
			chprintlnf(ch, "You are now a wannabe buddy of %s.",
					   capitalize(ch->pcdata->buddies[pos]));
		else
			chprintlnf(ch, "You are now buddies with %s.",
					   capitalize(ch->pcdata->buddies[pos]));
		if (check_buddy(fch, ch) == -1)
			chprintlnf(fch, "%s wants to be your buddy. (see 'buddy wannabe')",
					   ch->name);
		else
			chprintlnf(fch, "%s is now your buddy.", ch->name);
		return;
	}
}

void buddy_offline(CHAR_DATA * ch, char *arg)
{
	int pos;
	char buf[MIL];
	FILE *fp;
	bool exists;

	sprintf(buf, "%s%s", PLAYER_DIR, capitalize(arg));
	exists = ((fp = file_open(buf, "r")) != NULL) ? TRUE : FALSE;
	file_close(fp);

	for (pos = 0; pos < MAX_BUDDY; pos++)
	{
		if (IS_NULLSTR(ch->pcdata->buddies[pos]))
			break;

		if (!str_cmp(arg, ch->pcdata->buddies[pos]))
		{
			free_string(ch->pcdata->buddies[pos]);
			ch->pcdata->buddies[pos] = NULL;
			chprintlnf(ch, "You are no longer buddies with %s.", arg);
			return;
		}
	}

	if (pos >= MAX_BUDDY)
	{
		chprintln(ch, "You can't buddy anymore people");
		return;
	}

	if (!exists)
	{
		chprintln(ch, "That character doesn't exist.");
		return;
	}

	replace_string(ch->pcdata->buddies[pos], capitalize(arg));
	chprintlnf(ch, "You are now buddies with %s.", capitalize(arg));
}

int check_buddy(CHAR_DATA * ch, CHAR_DATA * fch)
{
	int pos;

	if (!ch || !fch)
		return -1;

	if (IS_NPC(ch) || IS_NPC(fch))
		return -1;

	for (pos = 0; pos < MAX_BUDDY; pos++)
	{
		if (IS_NULLSTR(ch->pcdata->buddies[pos]))
			break;
		if (is_name(fch->name, ch->pcdata->buddies[pos]))
			return pos;
	}
	return -1;
}