/
rogue25b1/
rogue25b1/space/planets/
rogue25b1/space/prototypes/
rogue25b1/space/ships/
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments 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          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/

/***************************************************************************
*       ROM 2.4 is copyright 1993-1995 Russ Taylor                         *
*       ROM has been brought to you by the ROM consortium                  *
*           Russ Taylor (rtaylor@pacinfo.com)                              *
*           Gabrielle Taylor (gtaylor@pacinfo.com)                         *
*           Brian Moore (rom@rom.efn.org)                                  *
*       By using this code, you have agreed to follow the terms of the     *
*       ROM license, in the file Rom24/doc/rom.license                     *
***************************************************************************/

/****************************************************************************
*  Automated Quest code written by Vassago of MOONGATE, moongate.ams.com    *
*  4000. Copyright (c) 1996 Ryan Addams, All Rights Reserved. Use of this   *
*  code is allowed provided you add a credit line to the effect of:         *
*  "Quest Code (c) 1996 Ryan Addams" to your logon screen with the rest     *
*  of the standard diku/rom credits. If you use this or a modified version  *
*  of this code, let me know via email: moongate@moongate.ams.com. Further  *
*  updates will be posted to the rom mailing list. If you'd like to get     *
*  the latest version of quest.c, please send a request to the above add-   *
*  ress. Quest Code v2.03. Please do not remove this notice from this file. *
****************************************************************************/

#include "merc.h"
#include "interp.h"

void do_tell_quest( CHAR_DATA *ch, CHAR_DATA *victim, char *argument);

/* Object vnums for Quest Rewards */
#define QUEST_ITEM1	1001
#define QUEST_ITEM2	1001
#define QUEST_ITEM3	1001
#define QUEST_ITEM4	1001
#define QUEST_ITEM5	1001

/* Object vnums for Quest Tokens (objects to be fetched) */
#define QUEST_OBJQUEST1	8
#define QUEST_OBJQUEST2 8
#define QUEST_OBJQUEST3 8
#define QUEST_OBJQUEST4 8
#define QUEST_OBJQUEST5 8

/* Local functions */
void generate_quest     args( (CHAR_DATA *ch, CHAR_DATA *questman) );
void quest_update       args( (void) );
bool quest_level_diff   args( (int clevel, int mlevel) );
bool chance             args( (int num) );
ROOM_INDEX_DATA         *find_location(CHAR_DATA *ch, char *arg);


bool chance(int num) {
    if (number_range(1,100) <= num)
	return TRUE;
    return FALSE;
}

const struct {
    char *	name;
    UInt32	cost;
    SInt32	vnum;
    bool	buyable;
} quest_item_table[] = {
    {	"rose",		50,	OBJ_VNUM_ROSE,	TRUE	},
    {	"\n",		0,	-1,		FALSE	}
};

/* The main quest function */
ACMD(do_quest) {
    CHAR_DATA *qm;
    OBJ_DATA *obj=NULL, *obj_next;
    OBJ_INDEX_DATA *questinfoobj;
    MOB_INDEX_DATA *questinfo;
    char buf[MAX_STRING_LENGTH];
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];

    argument = one_argument(argument, arg1);
    argument = one_argument(argument, arg2);

    if (!*arg1) {
	ch->Send("Quest what? Type 'help quest' for more info.\n\r");
        return;
    }

    if (!strcmp(arg1, "info")) {
	if (IS_SET(ch->act, PLR_QUESTOR)) {
	    if (ch->questmob == -1 && ch->questgiver->short_descr != NULL) {
		ch->Send("Your quest is ALMOST complete!\n\r"
			"Get back to %s before your time runs out!\n\r",
				ch->questgiver->short_descr);
            } else if (ch->questobj > 0) {
		if ((questinfoobj = get_obj_index(ch->questobj)) != NULL)
			ch->Send("You are on a quest to recover %s!\n\r", questinfoobj->name);
		else
			ch->Send("You aren't currently on a quest.\n\r");
		return;
	    } else if (ch->questmob > 0) {
		if ((questinfo = get_mob_index(ch->questmob)) != NULL)
			ch->Send("You are on a quest to slay the dreaded %s!\n\r", questinfo->short_descr);
		else
			ch->Send("You aren't currently on a quest.\n\r");
		return;
	    }
	} else
		ch->Send("You aren't currently on a quest.\n\r");
	return;
    }

    if (!strcmp(arg1, "points")) {
	ch->Send("You have %d quest points.\n\r", ch->questpoints);
	return;
    }

    if (!strcmp(arg1, "time")) {
	if (!IS_SET(ch->act, PLR_QUESTOR)) {
	    ch->Send("You aren't currently on a quest.\n\r");
	    if (ch->nextquest > 1)
		ch->Send("There are %d minutes remaining until you can go on another quest.\n\r", ch->nextquest);
            else if (ch->nextquest == 1)
		ch->Send("There is less than a minute remaining until you can go on another quest.\n\r");
	} else if (ch->countdown > 0)
	    ch->Send("Time left for current quest: %d\n\r", ch->countdown);
	return;
    }

    for (qm = ch->in_room->people; qm != NULL; qm = qm->next_in_room) {
	if (!IS_NPC(qm))
		continue;
	if (qm->spec_fun == spec_lookup("spec_questmaster"))
		break;
    }

    if (qm == NULL || qm->spec_fun != spec_lookup("spec_questmaster")) {
	send_to_char("You can't do that here.\n\r", ch);
	return;
    }

    if (qm->fighting != NULL) {
	send_to_char("Wait until the fighting stops.\n\r",ch);
	return;
    }

    ch->questgiver = qm;

    if (!strcmp(arg1, "list"))
    {
        act( "$n asks $N for a list of quest items.", ch, NULL, qm, TO_ROOM);
        act ("You ask $N for a list of quest items.",ch, NULL, qm, TO_CHAR);
        sprintf(buf, "Current Quest Items available for Purchase:\n\r"
		"1qp............a red rose!\n\r"
		"500qp..........350,000 gold pieces\n\r"
		"500qp..........30 Practices\n\r"
		"To buy an item, type 'QUEST BUY <item>'.\n\r");
        send_to_char(buf, ch);
        return;
    }

    else if (!strcmp(arg1, "buy"))
    {
        if (arg2[0] == '\0')
        {
            send_to_char("To buy an item, type 'QUEST BUY <item>'.\n\r",ch);
            return;
        }
        if (is_name(arg2, "rose"))
        {
            if (ch->questpoints >= 1)
            {
                ch->questpoints -= 1;
                obj = create_object(get_obj_index(QUEST_ITEM1),ch->level);
            }
            else
            {
		sprintf(buf, "Sorry, %s, but you don't have enough quest points for that.",ch->name);
                do_tell_quest(ch,qm,buf);
                return;
            }
        }
        else if (is_name(arg2, "practices pracs prac practice"))
        {
            if (ch->questpoints >= 500)
            {
                ch->questpoints -= 500;
                ch->practice += 30;
                act( "$N gives 30 practices to $n.", ch, NULL, qm, TO_ROOM );
                act( "$N gives you 30 practices.",   ch, NULL, qm, TO_CHAR );
                return;
            }
            else
            {
                sprintf(buf, "Sorry, %s, but you don't have enough quest points for that.",ch->name);
                do_tell_quest(ch,qm,buf);
                return;
            }
        }
        else if (is_name(arg2, "gold gp"))
        {
            if (ch->questpoints >= 500)
            {
                ch->questpoints -= 500;
                ch->gold += 350000;
                act( "$N gives 350,000 gold pieces to $n.", ch, NULL, qm, TO_ROOM );
                act( "$N has 350,000 in gold transfered from $s Swiss account to your balance.",ch, NULL, qm, TO_CHAR );
                return;
            }
            else
            {
                sprintf(buf, "Sorry, %s, but you don't have enough quest points for that.",ch->name);
                do_tell_quest(ch,qm,buf);
                return;
            }
        }
        else
        {            sprintf(buf, "I don't have that item, %s.",ch->name);
            do_tell_quest(ch,qm, buf);
        }
        if (obj != NULL)
        {
            act( "$N gives $p to $n.", ch, obj, qm, TO_ROOM );
            act( "$N gives you $p.",   ch, obj, qm, TO_CHAR );
            obj_to_char(obj, ch);
        }
        return;
    }
    else if (!strcmp(arg1, "request"))
    {
        act( "$n asks $N for a quest.", ch, NULL, qm, TO_ROOM);
        act ("You ask $N for a quest.",ch, NULL, qm, TO_CHAR);
        if (IS_SET(ch->act, PLR_QUESTOR))
        {
            sprintf(buf, "But you're already on a quest!");
            do_tell_quest(ch,qm, buf);
            return;
        }
        if (ch->nextquest > 0)
        {
            sprintf(buf, "You're very brave, %s, but let someone else have a chance.",ch->name);
            do_tell_quest(ch,qm, buf);
            sprintf(buf, "Come back later.");
            do_tell_quest(ch,qm, buf);
            return;
        }

        sprintf(buf, "Thank you, brave %s!",ch->name);
        do_tell_quest(ch,qm, buf);
        ch->questmob = 0;
        ch->questobj = 0;

        generate_quest(ch, qm);

        if (ch->questmob > 0 || ch->questobj > 0)
        {
            ch->countdown = number_range(10,30);
            SET_BIT(ch->act, PLR_QUESTOR);
            sprintf(buf, "You have %d minutes to complete this quest.",ch->countdown);
            do_tell_quest(ch,qm, buf);
            sprintf(buf, "May the gods go with you!");
            do_tell_quest(ch,qm, buf);
        }
        return;
    }
    else if (!strcmp(arg1, "complete"))
    {
        act( "$n informs $N $e has completed $s quest.", ch, NULL, qm, TO_ROOM);
        act ("You inform $N you have completed $s quest.",ch, NULL, qm, TO_CHAR);
        if (ch->questgiver != qm)
        {
            sprintf(buf, "I never sent you on a quest! Perhaps you're thinking of someone else.");
            do_tell_quest(ch,qm,buf);
            return;
        }

        if (IS_SET(ch->act, PLR_QUESTOR))
        {
            if (ch->questmob == -1 && ch->countdown > 0)
            {
                int reward, pointreward, pracreward;

                reward = number_range(20,150);
                pointreward = number_range(25,75);

                sprintf(buf, "Congratulations on completing your quest!");
                do_tell_quest(ch,qm,buf);
                sprintf(buf,"As a reward, I am giving you %d quest points, and %d gold.",pointreward,reward);
                do_tell_quest(ch,qm,buf);
                if (chance(15))
                {
                    pracreward = number_range(1,6);
                    sprintf(buf, "You gain %d practices!\n\r",pracreward);
                    send_to_char(buf, ch);
                    ch->practice += pracreward;
                }

                REMOVE_BIT(ch->act, PLR_QUESTOR);
                ch->questgiver = NULL;
                ch->countdown = 0;
                ch->questmob = 0;
                ch->questobj = 0;
                ch->nextquest = 10;
                ch->gold += reward;
                ch->questpoints += pointreward;

                return;
            }
            else if (ch->questobj > 0 && ch->countdown > 0)
            {
                bool obj_found = FALSE;

                for (obj = ch->carrying; obj != NULL; obj= obj_next)
                {
                    obj_next = obj->next_content;

                    if (obj != NULL && obj->pIndexData->vnum == ch->questobj)
                    {
                        obj_found = TRUE;
                        break;
                    }
                }
                if (obj_found == TRUE)
                {
                    int reward, pointreward, pracreward;

                    reward = number_range(20,150);
                    pointreward = number_range(25,75);

                    act("You hand $p to $N.",ch, obj, qm, TO_CHAR);
                    act("$n hands $p to $N.",ch, obj, qm, TO_ROOM);

                    sprintf(buf, "Congratulations on completing your quest!");
                    do_tell_quest(ch,qm,buf);
                    sprintf(buf,"As a reward, I am giving you %d quest points, and %d gold.",pointreward,reward);
                    do_tell_quest(ch,qm,buf);
                    if (chance(15))
                    {
                        pracreward = number_range(1,6);
                        sprintf(buf, "You gain %d practices!\n\r",pracreward);
                        send_to_char(buf, ch);
                        ch->practice += pracreward;
                    }

                    REMOVE_BIT(ch->act, PLR_QUESTOR);
                    ch->questgiver = NULL;
                    ch->countdown = 0;
                    ch->questmob = 0;
                    ch->questobj = 0;
                    ch->nextquest = 10;
                    ch->gold += reward;
                    ch->questpoints += pointreward;
                    extract_obj(obj);
                    return;
                }
                else
                {
                    sprintf(buf, "You haven't completed the quest yet, but there is still time!");
                    do_tell_quest(ch,qm, buf);
                    return;
                }
                return;
            }
            else if ((ch->questmob > 0 || ch->questobj > 0) && ch->countdown >0)
            {
                sprintf(buf, "You haven't completed the quest yet, but there is still time!");
                do_tell_quest(ch,qm, buf);
                return;
            }
        }
        if (ch->nextquest > 0)
            sprintf(buf,"But you didn't complete your quest in time!");
        else sprintf(buf, "You have to REQUEST a quest first, %s.",ch->name);
        do_tell_quest(ch,qm, buf);
        return;
    }

    send_to_char("QUEST commands: POINTS INFO TIME REQUEST COMPLETE LIST BUY.\n\r",ch);
    send_to_char("For more information, type 'HELP QUEST'.\n\r",ch);
    return;
}

void generate_quest(CHAR_DATA *ch, CHAR_DATA *questman)
{
    CHAR_DATA *victim;
    ROOM_INDEX_DATA *room;
    OBJ_DATA *questitem;
    char buf [MAX_STRING_LENGTH];

    /*  Randomly selects a mob from the world mob list. If you don't
        want a mob to be selected, make sure it is immune to summon.
        Or, you could add a new mob flag called ACT_NOQUEST. The mob
        is selected for both mob and obj quests, even tho in the obj
        quest the mob is not used. This is done to assure the level
        of difficulty for the area isn't too great for the player. */

    for (victim = char_list; victim != NULL; victim = victim->next)
    {
        if (!IS_NPC(victim)) continue;

        if (quest_level_diff(ch->level, victim->level) == TRUE
            && !IS_SET(victim->imm_flags, IMM_SUMMON)
            && victim->pIndexData != NULL
            && victim->pIndexData->pShop == NULL
            && !IS_SET(victim->act, ACT_PET)
            && !IS_SET(victim->affected_by, AFF_CHARM)
            && chance(15)) break;
    }

    if ( victim == NULL  )
    {
        do_tell_quest(ch,questman, "I'm sorry, but I don't have any quests for you at this time.");
        do_tell_quest(ch,questman, "Try again later.");
        ch->nextquest = 2;
        return;
    }

    if ( ( room = find_location( ch, victim->name ) ) == NULL )
    {
        sprintf(buf, "I'm sorry, but I don't have any quests for you at this time.");
        do_tell_quest(ch,questman, buf);
        sprintf(buf, "Try again later.");
        do_tell_quest(ch,questman, buf);
        ch->nextquest = 2;
        return;
    }

    /*  40% chance it will send the player on a 'recover item' quest. */

    if (chance(40))
    {
        int objvnum = 0;

        switch(number_range(0,4))
        {
            case 0:
            objvnum = QUEST_OBJQUEST1;
            break;

            case 1:
            objvnum = QUEST_OBJQUEST2;
            break;

            case 2:
            objvnum = QUEST_OBJQUEST3;
            break;

            case 3:
            objvnum = QUEST_OBJQUEST4;
            break;

            case 4:
            objvnum = QUEST_OBJQUEST5;
            break;
        }

        questitem = create_object( get_obj_index(objvnum), ch->level );
        obj_to_room(questitem, room);
        ch->questobj = questitem->pIndexData->vnum;

        sprintf(buf, "Vile pilferers have stolen %s from the royal treasury!",questitem->short_descr);
        do_tell_quest(ch,questman, buf);
        do_tell_quest(ch,questman, "My court wizardess, with her magic mirror, has pinpointed its location.");

        /* I changed my area names so that they have just the name of the area
           and none of the level stuff. You may want to comment these next two
           lines. - Vassago */

        sprintf(buf, "Look in the general area of %s for %s!",room->area->name, room->name);
        do_tell_quest(ch,questman, buf);
        return;
    }

    /* Quest to kill a mob */

    else
    {
    switch(number_range(0,1))
    {
        case 0:
        sprintf(buf, "An enemy of mine, %s, is making vile threats against the crown.",victim->short_descr);
        do_tell_quest(ch,questman, buf);
        sprintf(buf, "This threat must be eliminated!");
        do_tell_quest(ch,questman, buf);
        break;

        case 1:
        sprintf(buf, "Rune's most heinous criminal, %s, has escaped from the dungeon!",victim->short_descr);
        do_tell_quest(ch,questman, buf);
        sprintf(buf, "Since the escape, %s has murdered %d civillians!",victim->short_descr, number_range(2,20));
        do_tell_quest(ch,questman, buf);
        do_tell_quest(ch,questman,"The penalty for this crime is death, and you are to deliver the sentence!");
        break;
    }

    if (room->name != NULL)
    {
        sprintf(buf, "Seek %s out somewhere in the vicinity of %s!",victim->short_descr,room->name);
        do_tell_quest(ch,questman, buf);

        /* I changed my area names so that they have just the name of the area
           and none of the level stuff. You may want to comment these next two
           lines. - Vassago */

        sprintf(buf, "That location is in the general area of %s.",room->area->name);
        do_tell_quest(ch,questman, buf);
    }
    ch->questmob = victim->pIndexData->vnum;
    }
    return;
}

/* Level differences to search for. Moongate has 350
   levels, so you will want to tweak these greater or
   less than statements for yourself. - Vassago */

bool quest_level_diff(int clevel, int mlevel)
{
    if (clevel < 9 && mlevel < clevel + 2) return TRUE;
    else if (clevel <= 9 && mlevel < clevel + 3
          && mlevel > clevel - 5) return TRUE;
    else if (clevel <= 14 && mlevel < clevel + 4
          && mlevel > clevel - 5) return TRUE;
    else if (clevel <= 21 && mlevel < clevel + 5
          && mlevel > clevel - 4) return TRUE;
    else if (clevel <= 29 && mlevel < clevel + 6
          && mlevel > clevel - 3) return TRUE;
    else if (clevel <= 37 && mlevel < clevel + 7
          && mlevel > clevel - 2) return TRUE;
    else if (clevel <= 55 && mlevel < clevel + 8
          && mlevel > clevel - 1) return TRUE;
    else if (clevel <= 70 && mlevel < clevel + 9
        && mlevel > clevel - 1) return TRUE;
    else if (clevel <= 81 && mlevel < clevel + 10
        && mlevel > clevel - 1) return TRUE;
    else if (clevel <= 92 && mlevel < clevel + 11
        && mlevel > clevel - 1) return TRUE;
    else if (clevel <= 100 && mlevel < clevel + 12
        && mlevel > clevel - 1) return TRUE;
    else if (clevel > 101) return TRUE; /* Imms can get anything :) */
    else return FALSE;
}

void quest_update(void) {
    DESCRIPTOR_DATA *d;
    CHAR_DATA *ch;

    for (d = descriptor_list; d != NULL; d = d->next) {
	if (d->character != NULL && d->connected == CON_PLAYING) {
	    ch = Original(d);

	    if (ch->nextquest > 0) {
		ch->nextquest--;
		if (ch->nextquest == 0) {
		    ch->Send("You may now quest again.\n\r");
		    return;
		}
	    } else if (IS_SET(ch->act,PLR_QUESTOR)) {
		if (--ch->countdown <= 0) {
		    ch->nextquest = 10;
		    ch->Send("You have run out of time for your quest!\n\r"
			"You may quest again in %d minutes.\n\r", ch->nextquest);
		    REMOVE_BIT(ch->act, PLR_QUESTOR);
		    ch->questgiver = NULL;
		    ch->countdown = 0;
		    ch->questmob = 0;
		}
		if (ch->countdown > 0 && ch->countdown < 6) {
		    ch->Send("Better hurry, you're almost out of time for your quest!\n\r");
		    return;
		}
	    }
	}
    }
    return;
}


void do_tell_quest( CHAR_DATA *ch, CHAR_DATA *victim, char *argument )
{
char buf[MAX_STRING_LENGTH];

	sprintf(buf,"%s tells you '%s'\n\r",
		IS_NPC(victim) ? victim->short_descr : victim->name,argument);
        send_to_char( buf, ch );
        return;
}