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>                   *
***************************************************************************/
/* The following code is based on ILAB OLC by Jason Dinkel */
/* Mobprogram code by Lordrom for Nevermore Mud */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "tables.h"
#include "olc.h"
#include "recycle.h"
#include "interp.h"

#define MPEDIT( fun )           bool fun(CHAR_DATA *ch, const char*argument)

void unlink_mprog (vnum_t pnum)
{
	MPROG_LIST *list, *list_next;
	MOB_INDEX_DATA *pMob;
	vnum_t vnum = 0;
	int nMatch = 0, count, pos = 0;

	for (vnum = 0; nMatch < top_mob_index; vnum++)
	{
		if ((pMob = get_mob_index (vnum)) == NULL)
			continue;

		nMatch++;
		count = -1;
		for (list = pMob->mprogs; list != NULL; list = list_next)
		{
			list_next = list->next;
			count++;

			if (list->vnum != pnum)
				continue;

			if (count == 0)
			{
				REMOVE_BIT (pMob->mprog_flags, pMob->mprogs->trig_type);
				list = pMob->mprogs;
				pMob->mprogs = list->next;
				free_mprog (list);
			}
			else
			{
				while ((list_next = list->next) && (++pos < count))
					list = list_next;

				if (list_next)
				{
					REMOVE_BIT (pMob->mprog_flags, list_next->trig_type);
					list->next = list_next->next;
					free_mprog (list_next);
				}
			}
		}
	}
}

void mpedit (CHAR_DATA * ch, char *argument)
{
	MPROG_CODE *pMcode;
	AREA_DATA *ad;

	EDIT_MPCODE (ch, pMcode);

	if (pMcode)
	{
		ad = get_vnum_area (pMcode->vnum);

		if (ad == NULL)

		{
			edit_done (ch);
			return;
		}

		if (!IS_BUILDER (ch, ad))
		{
			chprintln (ch, "MPEdit: Insufficient security to modify code.");
			edit_done (ch);
			return;
		}
	}

	if (emptystring (argument))
	{
		mpedit_show (ch, argument);
		return;
	}

	if (!str_cmp (argument, "done"))
	{
		edit_done (ch);
		return;
	}

	if (!process_olc_command (ch, argument, mprog_olc_comm_table))
		interpret (ch, argument);

	return;
}

CH_CMD (do_mpedit)
{
	MPROG_CODE *pMcode;
	char command[MAX_INPUT_LENGTH];

	argument = one_argument (argument, command);

	if (is_number (command))
	{
		vnum_t vnum = atol (command);
		AREA_DATA *ad;

		if ((pMcode = get_mprog_index (vnum)) == NULL)
		{
			chprintln (ch, "MPEdit : That vnum does not exist.");
			return;
		}

		ad = get_vnum_area (vnum);

		if (ad == NULL)
		{
			chprint (ch, "MPEdit : vnum_t no asignado a ningun area.\n\r");
			return;
		}

		if (!IS_BUILDER (ch, ad))
		{
			chprintln
				(ch, "MPEdit : Insuficiente seguridad para editar area.");
			return;
		}

		edit_start (ch, pMcode, ED_MPCODE);
		return;
	}

	if (!str_cmp (command, "create"))
	{
		if (argument[0] == '\0')
		{
			chprintln (ch, "Sintaxis : mpedit create [vnum]");
			return;
		}

		mpedit_create (ch, argument);
		return;
	}
	if (!str_cmp (command, "delete"))
	{
		if (argument[0] == '\0')
		{
			chprintln (ch, "Syntax: mpedit delete [vnum]");
			return;
		}
		mpedit_delete (ch, argument);
		return;
	}

	chprintln (ch, "Sintaxis : mpedit [vnum]");
	chprintln (ch, "           mpedit create [vnum]");

	return;
}

MPEDIT (mpedit_create)
{
	MPROG_CODE *pMcode;
	vnum_t value = atol (argument);
	AREA_DATA *ad;

	if (IS_NULLSTR (argument) || value < 1)
	{
		chprintln (ch, "Sintaxis : mpedit create [vnum]");
		return FALSE;
	}

	ad = get_vnum_area (value);

	if (ad == NULL)
	{
		chprintln (ch, "MPEdit : vnum_t no asignado a ningun area.");
		return FALSE;
	}

	if (!IS_BUILDER (ch, ad))
	{
		chprintln (ch,
				   "MPEdit : Insuficiente seguridad para crear MobProgs.");
		return FALSE;
	}

	if (get_mprog_index (value))
	{
		chprintln (ch, "MPEdit: Code vnum already exists.");
		return FALSE;
	}

	pMcode = new_mpcode ();
	pMcode->vnum = value;
	pMcode->next = mprog_list;
	mprog_list = pMcode;
	edit_start (ch, pMcode, ED_MPCODE);
	chprintln (ch, "MobProgram Code Created.");

	return TRUE;
}

MPEDIT (mpedit_show)
{
	MPROG_CODE *pMcode;
	char buf[MAX_STRING_LENGTH];

	EDIT_MPCODE (ch, pMcode);

	sprintf (buf, "Vnum:       [%ld]\n\r" "Code:\n\r%s\n\r", pMcode->vnum,
			 pMcode->code);
	chprint (ch, buf);

	return FALSE;
}

MPEDIT (mpedit_list)
{
	int count = 1;
	MPROG_CODE *mprg;
	char buf[MAX_STRING_LENGTH];
	BUFFER *buffer;
	bool fAll = !str_cmp (argument, "all");
	char blah;
	AREA_DATA *ad;

	buffer = new_buf ();

	for (mprg = mprog_list; mprg != NULL; mprg = mprg->next)
		if (fAll ||
			ENTRE (ch->in_room->area->min_vnum, mprg->vnum,
				   ch->in_room->area->max_vnum))
		{
			ad = get_vnum_area (mprg->vnum);

			if (ad == NULL)
				blah = '?';
			else if (IS_BUILDER (ch, ad))
				blah = '*';
			else
				blah = ' ';

			sprintf (buf, "[%3d] (%c) %5ld\n\r", count, blah, mprg->vnum);
			add_buf (buffer, buf);

			count++;
		}

	if (count == 1)
	{
		if (fAll)
			add_buf (buffer, "No existen MobPrograms.\n\r");
		else
			add_buf (buffer, "No existen MobPrograms en esta area.\n\r");
	}

	page_to_char (buf_string (buffer), ch);
	free_buf (buffer);

	return FALSE;
}

MPEDIT (mpedit_delete)
{
	MPROG_CODE *curr, *prev;
	vnum_t value = atol (argument);
	AREA_DATA *ad;

	if (IS_NULLSTR (argument) || value < 1)
	{
		chprintln (ch, "Syntax : mpedit create [vnum]");
		return FALSE;
	}

	if (get_mprog_index (value) == NULL)
	{
		chprintln (ch, "MPEdit : Mob program not found.");
		return FALSE;
	}

	ad = get_vnum_area (value);
	if (ad == NULL)
	{
		chprintln (ch, "MPEdit : Mob program not assigned to an area.");
		return FALSE;
	}

	if (!IS_BUILDER (ch, ad))
	{
		chprintln (ch, "MPEdit : Insufficient security to create MobProgs.");
		return FALSE;
	}

	unlink_mprog (value);
	prev = NULL;
	for (curr = mprog_list; curr != NULL; prev = curr, curr = curr->next)
	{
		if (curr->vnum != value)
			continue;
		if (prev == NULL)
			mprog_list = mprog_list->next;
		else
			prev->next = curr->next;

		free_mpcode (curr);
	}
	save_area (ad);
	chprintln (ch, "MobProgram Code Deleted.");

	return TRUE;
}