/
rogue25b1/
rogue25b1/space/planets/
rogue25b1/space/prototypes/
rogue25b1/space/ships/
/***************************************************************************\
[*]    ___    ____   ____   __   __  ____ [*]   ROGUE: ROM With Attitude  [*]
[*]   /#/ )  /#/  ) /#/  ) /#/  /#/ /#/   [*]    All rights reserved      [*]
[*]  /#/ <  /#/  / /#/ _  /#/  /#/ /#/--  [*]   Copyright(C) 2000-2001    [*]
[*] /#/   \(#(__/ (#(__/ (#(__/#/ (#(___  [*] Kenneth Conley (Mendanbar)  [*]
[*]  Expression of Digital Creativity..   [*]  scmud@mad.scientist.com    [*]
[-]---------------------------------------+-+-----------------------------[-]
[*] File: pueblo.cpp                                                      [*]
[*] Usage: Sound/Music Funtions for MSP and Pueblo                        [*]
\***************************************************************************/

#include "merc.h"
#include "db.h"
#include "interp.h"
#include "recycle.h"
#include "tables.h"
#include "pueblo.h"

ACMD(do_pmusic) {
    int i, x;
    char arg[MAX_STRING_LENGTH];

    argument = one_argument(argument, arg);

    if (!DESC_FLAGGED(ch->desc, DESC_PUEBLO | DESC_MSPON)) {
	ch->Send("You do not have Pueblo or MSP enabled.\n\r");
	return;
    }

    if (!arg || !*arg) {
	ch->Send("The following pre-set music is availible...\n\r");

	for (i = 0; i < maxPSong; i++)
	    ch->Send(" %3d. %-25s  `y%s`n\n\r", i+1, pmusic_table[i].name,
		IS_STAFF(ch) ? pmusic_table[i].fname : "");

	ch->Send("\n\rUsage: pmusic <number>\n\r");
    } else if (!isdigit(*arg)) {
	ch->Send("Usage: pmusic <number>\n\r"
		"Type 'pmusic' without an argument for a list of songs.\n\r");
    } else {
	if ((i = (atoi(arg)-1)) < 0)
		ch->Send("The number cannot be negative.\n\r");
        else {
	    if (i >= maxPSong)
		ch->Send("The range for the pmusic number is 1-%d.\n\r", maxPSong);
	    else {
		ch->Send("Activating Music: %s, Infinite Loop...\n\r", pmusic_table[i].name);
		music_to_char(ch, pmusic_table[i].fname);
	    }
	}
    }
}

ACMD(do_pstop) {
    if (IS_NPC(ch))
	return;

    if (!DESC_FLAGGED(ch->desc, DESC_PUEBLO | DESC_MSPON)) {
	ch->Send("You do not have Pueblo or MSP enabled.\n\r");
	return;
    }

    send_to_char("Deactivating Music: Please Standby...\n\r",ch);
    if (DESC_FLAGGED(ch->desc, DESC_PUEBLO))
	ch->Send("</xch_mudtext><img xch_mode=html>"
		"<img xch_sound=stop>""<br><img xch_mode=text>");
    else if (DESC_FLAGGED(ch->desc, DESC_MSPON))
	ch->Send("!!MUSIC(Off)\r\n");
    return;
}

ACMD(do_volume) {
    SInt16 value = 0;
    char arg[MAX_INPUT_LENGTH];

    argument = one_argument(argument, arg);

    if (IS_NPC(ch)) {
	send_to_char("Not on NPCs.\n\r",ch);
	return;
    }
    if (!DESC_FLAGGED(ch->desc, DESC_PUEBLO | DESC_MSPON)) {
	send_to_char("You do not need to set your volume.\n\r",ch);
	return;
    }
    if (arg[0] == '\0') {
	ch->Send("Syntax: volume <0-100>\n\r"
		"Your current volume level is %d%%.\r\n", ch->pcdata->volume);
	return;
    }
    if ((atoi(arg) > 100) || (atoi(arg) < 1)) {
	send_to_char("Value must be within 0-100\n\r",ch);
	return;
    }
    value = atoi(arg);
    ch->pcdata->volume = value;
    ch->Send("Pueblo Volume set to %d.\n\r", value);
    sound(PUEBLO_V_TEST, ch, 0, 0, TO_CHAR, POS_DEAD);
    return;
}

void image_to_char(CHAR_DATA *ch, const char *image) {
    if (!ch || !image)
	return;

    if (DESC_FLAGGED(ch->desc, DESC_PUEBLO)) {
	ch->Send("</xch_mudtext><img xch_mode=html>");
	ch->Send("<img src=\"%s%s\">", PUEBLO_DIR, image);
	ch->Send("<br><img xch_mode=text>");
    }
}

void music_to_char(CHAR_DATA *ch, const char *sound) {
    if (!ch || !sound)
	return;

    if (DESC_FLAGGED(ch->desc, DESC_PUEBLO)) {
	ch->Send("</xch_mudtext><img xch_mode=html>"
		"<img xch_sound=loop xch_volume=%d src=\"%s%s\">"
		"<br><img xch_mode=text>",
		ch->pcdata->volume, PUEBLO_DIR, sound);
    } else if (DESC_FLAGGED(ch->desc, DESC_MSPON))
	ch->Send("!!MUSIC(%s V=%d L=-1 U=%s)\r\n", sound, ch->pcdata->volume, PUEBLO_DIR);
}

ACMD(do_sound) {
    char arg[MIL];
    CHAR_DATA *vict;
    argument = one_argument(argument, arg);

    if (IS_NPC(ch))
	return;

    if (!arg[0] || !argument[0]) {
	send_to_char("Usage: sound <char/room/all> <filename>\r\n", ch);
	return;
    }

    if (str_infix(".wav", argument) && str_infix(".mid", argument))
	strcat(argument, ".wav");

    if (!str_cmp(arg, "room")) {
	ch->Send("Sending %s to room.\r\n", argument);
	sound(argument, ch, 0, 0, TO_ROOM | TO_CHAR, POS_DEAD);
    } else if (!str_cmp(arg, "all")) {
	ch->Send("Sending %s to all.\r\n", argument);
	sound(argument, ch, 0, 0, TO_ALL | TO_CHAR, POS_DEAD);
    } else if ((vict = get_char_world(ch, arg)) != NULL && !IS_NPC(ch)) {
	ch->Send("Sending %s to %s.\r\n", argument, vict->name);
	sound(argument, ch, 0, vict, TO_VICT, POS_DEAD);
    } else
	send_to_char("Usage: sound <char/room/all> <filename>\r\n", ch);
}

#define SENDOK(ch)	\
		((ch)->desc && ((ch)->position >= min_pos) && !IS_NPC(ch))

void perform_sound(char *sound, CHAR_DATA *ch) {
    char buf[MAX_STRING_LENGTH];

    if (!*sound || !ch)
	return;

    if (ch->desc) {
	if (DESC_FLAGGED(ch->desc, DESC_PUEBLO)) {
	    sprintf(buf, "</xch_mudtext><img xch_mode=html>");
	    sprintf(buf + strlen(buf), "<img xch_sound=play xch_volume=%d src=\"%s%s\">",
	    ch->pcdata->volume, PUEBLO_DIR, sound);
	    strcat(buf, "<br><img xch_mode=text>");
	} else if (DESC_FLAGGED(ch->desc, DESC_MSPON)) {
	    sprintf(buf, "!!SOUND(%s V=%d U=%s)\r\n", sound, ch->pcdata->volume, PUEBLO_DIR);
	} else
	    return; // Don't write anything to buffer if nothing to write.
	write_to_buffer(ch->desc, buf, 0);
    }
    return;
}

void sound(	char *str, CHAR_DATA *ch, OBJ_DATA *obj,
		void *vict_obj, int type, int min_pos)
{
    CHAR_DATA *to;
    DESCRIPTOR_DATA *d;
    ROOM_INDEX_DATA *room;

    if (!*str)
	return;

    if (str_infix(".wav", str) && str_infix(".mid", str))
	strcat(str, ".wav"); // assume sound file.

    if (IS_SET(type, TO_CHAR)) {
	if (ch && SENDOK(ch))
	    perform_sound(str, ch);
    }

    if (IS_SET(type, TO_VICT)) {
	if ((to = (CHAR_DATA *)vict_obj) && SENDOK(to))
	    perform_sound(str, to);
    }

    if (IS_SET(type, TO_ALL | TO_ZONE)) {
	for (d = descriptor_list; d; d = d->next) {
	    if (d->character && SENDOK(d->character) && (d->character != ch) &&
	    ((IS_SET(type, TO_ALL) || (d->character->in_room->area == ch->in_room->area))))
		perform_sound(str, d->character);
	}
    }

    if (IS_SET(type, TO_ROOM | TO_NOTVICT)) {
	if (ch && IN_ROOM(ch) != NULL)
	    room = IN_ROOM(ch);
	else if (obj && IN_ROOM(obj) != NULL)
	    room = IN_ROOM(obj);
	else {
	    mudlogf(BRF, LVL_STAFF, TRUE, "SYSERR: no valid target to sound()");
	    return;
	}

	for (to = room->people; to; to = to->next_in_room) {
	    if (SENDOK(to) && (to != ch) && (IS_SET(type, TO_ROOM) || (to != vict_obj)))
		perform_sound(str, to);
	}
    }
}