/* This snippet has been ported over from a Smaug 1.4a codebase. Oridinal Snippet by GenmaC. 
While his/her snippet calls for 'do_study', I've changed the command to 'do_research' so 
that it will work together with the already 'do_study' command that is in play. Credit 
goes to GenmaC for the original code and Raven Nightshade of Depths of Chaos Mud for the 
porting to LoP format.

I have tested all three ITEMS; wands, scrolls, and staves, and have not discovered
any issues. Compiling was clean. */

*/

/* in db.c, add into your short gsn listings*/

short gsn_research;

/* in db.c, add into your ASSIGN_GSN listings */

ASSIGN_GSN( gsn_research, "research" );

/* in mud.h, add into your extern short listings */

extern short gsn_research; /* Research Skill */

/*

Be sure to create a skill called, 'research' with the 'do_research' code.

Be warned that this skill can unbalance things if you leave a lot of powerful spell
scrolls around for lower level players - however, it will give them the spell at a
very low percentage, so it won't be too powerful if used properly.

*/

/* in act_obj.c, ADD at the bottom of file: */

/*
	do_research - research skill. 
*/

CMDF ( do_research )
{
    char arg[MIL];
    OBJ_DATA *obj;
    int sn = 0;

    one_argument( argument, arg );

    if ( arg[0] == '\0' )
    {
	send_to_char( "Research what?\n\r", ch );
	return;
    }

    if ( ( obj = get_obj_carry( ch, arg ) ) == NULL )
    {
	send_to_char( "You do not have that item.\n\r", ch );
	return;
    }

    if ( obj->item_type != ITEM_STAFF && obj->item_type != ITEM_WAND &&
	obj->item_type != ITEM_SCROLL )
    {
	send_to_char( "You can only research scrolls, wands, and staves.\n\r", ch );
	return;
    }

	act( AT_PLAIN, "$n researches $p.", ch, obj, NULL, TO_NOTVICT );
	act( AT_PLAIN, "You research $p.", ch, obj, NULL, TO_CHAR );

    if (ch->level < obj->level) /* if character level lower than object level, fail */
	{
	send_to_char("You cannot glean any knowledge from it.\n\r",ch);
	act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	separate_obj( obj );
	extract_obj( obj );
	}

    if (obj->item_type == ITEM_STAFF)
	{
	sn = obj->value[3];
	if ( sn < 0 || sn >= MAX_SKILL || skill_table[sn]->spell_fun == 0 )
	    {
	    bug( "Do_research: bad sn %d.", sn );
	    return;
	    }
	if ( !can_use_skill(ch, number_percent(), gsn_research ) ) /* see if player suceeds */
		{
       set_char_color ( AT_MAGIC, ch );
       send_to_char("You cannot glean any knowledge from it.\n\r", ch);
       learn_from_failure( ch, gsn_research );
       act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	   act( AT_MAGIC, "$n accidently destroys a $p!", ch, obj, NULL, TO_NOTVICT);
	   separate_obj( obj );
	   extract_obj( obj );
       return;
		}
	if ( ch->pcdata->learned[sn])
	    {
	    send_to_char("You already know that spell!\n\r",ch);
	    return;
	    }
	ch->pcdata->learned[sn] = 1;
	act(AT_PLAIN, "You have learned the art of $t!",
	ch,skill_table[sn]->name,NULL,TO_CHAR);
	act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	separate_obj( obj );
	extract_obj( obj );
	return;
	}

    if (obj->item_type == ITEM_WAND)
	{
	sn = obj->value[3];
	if ( sn < 0 || sn >= MAX_SKILL || skill_table[sn]->spell_fun == 0 )
	    {
	    bug( "Do_research: bad sn %d.", sn );
	    return;
	    }
	if ( !can_use_skill(ch, number_percent(), gsn_research ) )
		{
       set_char_color ( AT_MAGIC, ch );
       send_to_char("You cannot glean any knowledge from it.\n\r", ch);
       learn_from_failure( ch, gsn_research );
       act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	   act( AT_MAGIC, "$n accidently destroys a $p!", ch, obj, NULL, TO_NOTVICT);
	   separate_obj( obj );
	   extract_obj( obj );
       return;
		}
	if ( ch->pcdata->learned[sn])
	    {
	    send_to_char("You already know that spell!\n\r",ch);
	    return;
	    }
	ch->pcdata->learned[sn] = 1;
	act(AT_PLAIN, "You have learned the art of $t!",
	ch,skill_table[sn]->name,NULL,TO_CHAR);
	act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	separate_obj( obj );
	extract_obj( obj );
	return;
	}

    if (obj->item_type == ITEM_SCROLL)
	{
	sn = obj->value[1];
	if ( sn < 0 || sn >= MAX_SKILL || skill_table[sn]->spell_fun == 0 )
	    {
	    bug( "Do_research: bad sn %d.", sn );
	    return;
	    }
	if ( !can_use_skill(ch, number_percent(), gsn_research ) )
		{
       set_char_color ( AT_MAGIC, ch );
       send_to_char("You cannot glean any knowledge from it.\n\r", ch);
       learn_from_failure( ch, gsn_research );
       act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	   act( AT_MAGIC, "$n accidently destroys a $p!", ch, obj, NULL, TO_NOTVICT);
	   separate_obj( obj );
	   extract_obj( obj );
       return;
		}
	if ( ch->pcdata->learned[sn])
	    {
	    send_to_char("You already know that spell!\n\r",ch);
	    return;
	    }
	ch->pcdata->learned[sn] = 1;
	act(AT_PLAIN, "You have learned the art of $t!",
	ch,skill_table[sn]->name,NULL,TO_CHAR);
	act( AT_PLAIN, "$p burns brightly and is gone.", ch, obj, NULL, TO_CHAR );
	separate_obj( obj );
	extract_obj( obj );
	return;
	}

}