/
Cool3/bin/
/***********************************************************************\
*				  CMDEDIT				*
*	CMDEDIT is by Jouster@COOLmud.  He can be reached at		*
* dan@betterbox.net.  This file falls under the GPL and the licenses	*
* of Diku, Merc, ROM, RoT, and any licenses that may apply to any	*
* portions of any file that this program may modify.			*
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
*	If you don't mind, I'd appreciate an email if you use this	*
* file in your code.  Thanks.						*
*						dan@betterbox.net	*
\***********************************************************************/

/***********************************************************************\
*			     VERSION HISTORY				*
* v0.1a--original development						*
* v1.0---internal release only						*
* v1.1---MuTarna Release						*
\***********************************************************************/
#define CMDEDIT_VERSION "1.1"
#define CMDEDIT_VERSION_OUTPUT "CMDEDIT " ## CMDEDIT_VERSION

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

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

extern const struct flag_type position_flags[];

char * pos_lookup( sh_int position );

void do_cmdedit( CHAR_DATA *ch, char * argument )
{
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char arg3[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    bool found=FALSE;
    int cur_cmd;

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

    if ( !is_name( ch->name, IMPSTRING ) &&
	 ( arg1[0] == '\0' ||
	   arg2[0] == '\0' ||
	   str_cmp( arg2, "info" ) ||
	   arg3[0] != '\0' ) )
    {
	send_to_char( "You are only authorized for the following syntax:\n\r"
			"Syntax: cmdedit <command> info\n\r", ch );
	return;
    }

    if ( !is_name( ch->name, IMPSTRING ) && str_cmp( arg2, "info" ) )
    {
	send_to_char( "You aren't authorized to use this command.\n\r", ch );
	return;
    }

    if ( ( arg3[0] == '\0' && str_cmp( arg2, "info" ) ) ||
	( arg2[0] == '\0' || arg1[0] == '\0' ) )
    {
	send_to_char(	"Syntax:\n\r"
			"  cmdedit <command> <field> <value>\n\r"
			"  Field being one of:\n\r"
			"    name position level tier log show\n\r"
			"    info\n\r"
			"  Position being one of:\n\r"
			"    dead mortal incapacitated stunned\n\r"
			"    sleeping resting sitting fighting\n\r",
			ch );
	return;
    }

    if ( !str_cmp( arg2, "log" ) && 
	 !is_name( arg3, "always never sometimes" ) )
    {
	send_to_char(	"Syntax:\n\r"
			"  cmdedit <command> log <type>\n\r"
			"  Type being one of:\n\r"
			"    always never sometimes\n\r",
			ch );
	return;
    }

    if ( !str_cmp( arg2, "show" ) && 
	 !is_name( arg3, "yes no" ) )
    {
	send_to_char(	"Syntax:\n\r"
			"  cmdedit <command> show <bool>\n\r"
			"  Bool being one of:\n\r"
			"    yes no\n\r",
			ch );
	return;
    }

    if ( !str_cmp( arg2, "position" ) &&
	 !is_name( arg3, "dead mortal incapacitated stunned "
			 "sleeping resting sitting fighting" ) )
    {
	send_to_char(	"Syntax:\n\r"
			"  cmdedit <command> position <min_pos>\n\r"
			"  Position being one of:\n\r"
			"    dead mortal incapacitated stunned\n\r"
			"    sleeping resting sitting fighting\n\r",
			ch );
	return;
    }

    for ( cur_cmd = 0; cmd_table[cur_cmd].name[0] != '\0'; cur_cmd++ )
	if ( !str_cmp( cmd_table[cur_cmd].name, arg1 ) )
	{
	    found=TRUE;
	    break;
	}

    /*
     * heh... this took me a while.  If we use !cur_cmd, then we can't
     * change the first command in the table, since it's #0 and triggers
     * the check.  --Jouster
     */
    if ( !found )
    {
	sprintf( buf, "Command \"%s\" not found.\n\r", arg1 );
	send_to_char( buf, ch );
	return;
    }

    if ( !str_cmp( arg2, "name" ) )
    {
	register int chk_cmd;

	for ( chk_cmd = 0; cmd_table[chk_cmd].name[0] != '\0'; chk_cmd++ )
	{
	    if ( !str_cmp( arg3, cmd_table[chk_cmd].name ) )
	    {
		sprintf( buf, "\"%s\" already exists in the table (%d)\n\r", arg3, chk_cmd );
		send_to_char( buf, ch );
		return;
	    }
	}

	cmd_table[cur_cmd].name = str_dup( arg3 );   

	sprintf( buf, "Command %d's name changed to %s.\n\r", cur_cmd, arg3 );
	return;
    }

    if ( !str_cmp( arg2, "info" ) )
    {
	sprintf( buf,	"---------------%s---------------\n\r"
			"Information for \"%s\", slot %d.\n\r"
			"Level: %-4d Tier: %d\n\r"
			"Logtype: {%s{x  Show: {%s{x\n\r"
			"Position: %s\n\r",
			CMDEDIT_VERSION_OUTPUT,
			cmd_table[cur_cmd].name, cur_cmd,
			cmd_table[cur_cmd].level,
			cmd_table[cur_cmd].tier,
			( cmd_table[cur_cmd].log ?
			( cmd_table[cur_cmd].log == 1 ? "GA" : "RN" ) : "YS" ),
			( cmd_table[cur_cmd].show ? "GY" : "RN" ),
			pos_lookup( cmd_table[cur_cmd].position ) );
	send_to_char( buf, ch );
	return;
    }

    if ( !str_cmp( arg2, "log" ) )
    {
	/* LOG_ALWAYS is default if it slips by the earlier check. */
	cmd_table[cur_cmd].log = ( str_cmp( arg3, "sometimes" ) ?
				( !str_cmp( arg3, "never" ) ? LOG_NEVER :
				LOG_ALWAYS ) : LOG_NORMAL );
	send_to_char( "Logtype changed.\n\r", ch );
	return;
    }

    if ( !str_cmp( arg2, "show" ) )
    {
	cmd_table[cur_cmd].show = ( !str_cmp( arg3, "yes" ) ? TRUE : FALSE );
	sprintf( buf,	"%s will %s be shown on command lists.\n\r",
			capitalize( cmd_table[cur_cmd].name ),
			( cmd_table[cur_cmd].show ? "now" : "no longer" ) );
	send_to_char( buf, ch );
	return;
    }

    if ( !str_cmp( arg2, "tier" ) )
    {
	if ( atoi( arg3 ) < 1 || atoi( arg3 ) > 2 )
	{
	    sprintf( buf, "Syntax:\n\r"
			"  cmdedit <command> tier <tier>\n\r"
			"  Where tier is either 1 or 2.\n\r" );
	    send_to_char( buf, ch );
	}

	cmd_table[cur_cmd].tier = atoi( arg3 );

	send_to_char( "Command tier changed.\n\r", ch );
	return;
    }

    if ( !str_cmp( arg2, "level" ) )
    {
	if ( !is_number( arg3 ) ||
		atoi(arg3) > (MAX_LEVEL+1) ||
		atoi(arg3) < 0 )
	{
	    sprintf( buf, "Syntax:\n\r"
			"  cmdedit <command> level <level>\n\r"
			"  Where level is from %d to %d (%d is disabled)\n\r",
			1, MAX_LEVEL, MAX_LEVEL+1 );
	    send_to_char( buf, ch );
	}

	cmd_table[cur_cmd].level = atoi( arg3 );
	if ( cmd_table[cur_cmd].level == 1 )
	    cmd_table[cur_cmd].level = 0;
		/*
		 * If anyone can use the command, level is traditionally
		 * stored as 0.  We'll set it to that, in case someone
		 * wants to expand this function to actually REWRITE
		 * the command table.
		 */

	send_to_char( "Level changed.\n\r", ch );
	return;
    }

    if ( !str_cmp( arg2, "position" ) )
    {
	int possetto=0;

	switch( arg3[0] )
	{
	    case 'd': case 'D':
		possetto = POS_DEAD;
		break;
	    case 'f': case 'F':
		possetto = POS_FIGHTING;
		break;
	    case 'i': case 'I':
		possetto = POS_INCAP;
		break;
	    case 'm': case 'M':
		possetto = POS_MORTAL;
		break;
	    case 'r': case 'R':
		possetto = POS_RESTING;
		break;
	    case 's': case 'S':
		switch( arg3[1] )
		{
		    case 'i': case 'I':
			possetto = POS_SITTING;
			break;
		    case 'l': case 'L':
			possetto = POS_SLEEPING;
			break;
		    case 't': case 'T':
			possetto = POS_STUNNED;
			break;
		}
	    break;
	}
	cmd_table[cur_cmd].position = possetto;
	send_to_char( "New position requirement set.\n\r", ch );
	return;
    }

    do_cmdedit( ch, "" ); /* Syntax message */
    return;
}

char * pos_lookup( sh_int position )
{
    register int cur_pos;
    bool found=FALSE;

    for( cur_pos = 0; position_flags[cur_pos].name[0] != '\0'; cur_pos++ )
    {
	if ( position_flags[cur_pos].bit == position )
	{
	    found=TRUE;
	    break;
	}
    }

    if ( !found )
	return "<bad lookup>";

    return position_flags[cur_pos].name;
}



/*
struct  cmd_type
{
    char * const        name;
    DO_FUN *            do_fun;
    sh_int              position;
    sh_int              level;
    sh_int              tier;
    sh_int              log;
    sh_int              show;
};
*/