1stMUD4.0/bin/
1stMUD4.0/doc/MPDocs/
1stMUD4.0/player/
1stMUD4.0/win32/
1stMUD4.0/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-2003 by Ryan Jennings              *
*            http://1stmud.dlmud.com/  <r-jenn@shaw.ca>                   *
***************************************************************************/
/***************************************************************************
 *  File: bit.c                                                            *
 *                                                                         *
 *  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.                                                  *
 *                                                                         *
 *  This code was written by Jason Dinkel and inspired by Russ Taylor,     *
 *  and has been used here for OLC - OLC would not be what it is without   *
 *  all the previous coders who released their source code.                *
 *                                                                         *
 ***************************************************************************/
/*
 The code below uses a table lookup system that is based on suggestions
 from Russ Taylor.  There are many routines in handler.c that would benefit
 with the use of tables.  You may consider simplifying your code base by
 implementing a system like below with such functions. -Jason Dinkel
 */

#include "merc.h"
#include "tables.h"
#include "lookup.h"

/*****************************************************************************
 Name:		flag_stat_table
 Purpose:	This table catagorizes the tables following the lookup
 		functions below into stats and flags.  Flags can be toggled
 		but stats can only be assigned.  Update this table when a
 		new set of flags is installed.
 ****************************************************************************/
#define STNM(flag)  #flag, flag

const struct flag_stat_type flag_stat_table[] = {
/*  {	structure		stat	}, */
	{STNM(area_flags), FALSE},
	{STNM(sex_flags), TRUE},
	{STNM(exit_flags), FALSE},
	{STNM(door_resets), TRUE},
	{STNM(room_flags), FALSE},
	{STNM(sector_flags), TRUE},
	{STNM(type_flags), TRUE},
	{STNM(extra_flags), FALSE},
	{STNM(wear_flags), FALSE},
	{STNM(act_flags), FALSE},
	{STNM(plr_flags), FALSE},
	{STNM(comm_flags), FALSE},
	{STNM(affect_flags), FALSE},
	{STNM(apply_flags), TRUE},
	{STNM(wear_loc_flags), TRUE},
	{STNM(wear_loc_strings), TRUE},
	{STNM(container_flags), FALSE},
	{STNM(mprog_flags), FALSE},
	{STNM(rprog_flags), FALSE},
	{STNM(oprog_flags), FALSE},
	{STNM(info_flags), FALSE},
/* ROM specific flags: */

	{STNM(form_flags), FALSE},
	{STNM(part_flags), FALSE},
	{STNM(ac_type), TRUE},
	{STNM(size_flags), TRUE},
	{STNM(position_flags), TRUE},
	{STNM(off_flags), FALSE},
	{STNM(imm_flags), FALSE},
	{STNM(res_flags), FALSE},
	{STNM(vuln_flags), FALSE},
	{STNM(weapon_class), TRUE},
	{STNM(weapon_type2), FALSE},
	{STNM(apply_types), TRUE},
	{STNM(desc_flags), FALSE},
	{STNM(log_flags), TRUE},
	{STNM(target_flags), TRUE},
	{STNM(chan_types), TRUE},
	{0, 0}
};

/*****************************************************************************
 Name:		is_stat( table )
 Purpose:	Returns TRUE if the table is a stat table and FALSE if flag.
 Called by:	flag_value and flag_string.
 Note:		This function is local and used only in bit.c.
 ****************************************************************************/
bool is_stat(const struct flag_type *flag_table)
{
	int flag;

	for (flag = 0; flag_stat_table[flag].structure; flag++)
		if (flag_stat_table[flag].structure == flag_table)
			return flag_stat_table[flag].stat;

	logf("flag_table[0] == '%s': not in flag_stat_table", flag_table[0].name);
	return FALSE;
}

/*****************************************************************************
 Name:		flag_value( table, flag )
 Purpose:	Returns the value of the flags entered.  Multi-flags accepted.
 Called by:	olc.c and olc_act.c.
 ****************************************************************************/
flag_t flag_value(const struct flag_type * flag_table, const char *argument)
{
	const struct flag_type *f;
	flag_t marked;
	bool found = FALSE;

	if (is_stat(flag_table))
	{
		if ((f = flag_lookup(argument, flag_table)) == NULL)
			return NO_FLAG;
		return f->bit;
	}

	marked = 0;

	/*
	 * Accept multiple flags.
	 */
	for (;;)
	{
		char word[MAX_INPUT_LENGTH];
		argument = one_argument(argument, word);

		if (IS_NULLSTR(word))
			break;

		if ((f = flag_lookup(word, flag_table)) != NULL)
		{
			SET_BIT(marked, f->bit);
			found = TRUE;
		}
	}

	if (found)
		return marked;
	else
		return NO_FLAG;
}

/*****************************************************************************
 Name:		flag_string( table, flags/stat )
 Purpose:	Returns string with name(s) of the flags or stat entered.
 Called by:	act_olc.c, olc.c, and olc_save.c.
 ****************************************************************************/
#define	NBUFS 3
#define	BUFSZ 512

const char *flag_string(const struct flag_type *flag_table, flag_t bits)
{
	static char buf[NBUFS][BUFSZ];
	static int cnt = 0;
	int flag;
	bool stat;

	cnt = (cnt + 1) % NBUFS;
	buf[cnt][0] = '\0';

	stat = is_stat(flag_table);
	for (flag = 0; flag_table[flag].name != NULL; flag++)
	{
		if (!stat)
		{
			if (IS_SET(bits, flag_table[flag].bit))
			{
				strncat(buf[cnt], " ", BUFSZ);
				strncat(buf[cnt], flag_table[flag].name, BUFSZ);
			}
		}
		else if (flag_table[flag].bit == bits)
		{
			strncat(buf[cnt], " ", BUFSZ);
			strncat(buf[cnt], flag_table[flag].name, BUFSZ);
			break;
		}
	}
	return (buf[cnt][0] != '\0') ? buf[cnt] + 1 : stat ? "unknown" : "none";
}