Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

bit.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  File: bit.c                                                            *
00003  *                                                                         *
00004  *  Much time and thought has gone into this software and you are          *
00005  *  benefitting.  We hope that you share your changes too.  What goes      *
00006  *  around, comes around.                                                  *
00007  *                                                                         *
00008  *  This code was written by Jason Dinkel and inspired by Russ Taylor,     *
00009  *  and has been used here for OLC - OLC would not be what it is without   *
00010  *  all the previous coders who released their source code.                *
00011  *                                                                         *
00012  ***************************************************************************/
00013 /*
00014  The code below uses a table lookup system that is based on suggestions
00015  from Russ Taylor.  There are many routines in handler.c that would benefit
00016  with the use of tables.  You may consider simplifying your code base by
00017  implementing a system like below with such functions. -Jason Dinkel
00018  */
00019 
00020 
00021 
00022 #if defined(macintosh)
00023 #include <types.h>
00024 #else
00025 #include <sys/types.h>
00026 #endif
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <time.h>
00031 #include "merc.h"
00032 #include "tables.h"
00033 #include "lookup.h"
00034 
00035 
00036 struct flag_stat_type {
00037     const struct flag_type *structure;
00038     bool stat;
00039 };
00040 
00041 
00042 
00043 /*****************************************************************************
00044  Name:        flag_stat_table
00045  Purpose:    This table catagorizes the tables following the lookup
00046          functions below into stats and flags.  Flags can be toggled
00047          but stats can only be assigned.  Update this table when a
00048          new set of flags is installed.
00049  ****************************************************************************/
00050 const struct flag_stat_type flag_stat_table[] = {
00051 /*  {    structure        stat    }, */
00052     {area_flags, FALSE},
00053     {sex_flags, TRUE},
00054     {exit_flags, FALSE},
00055     {door_resets, TRUE},
00056     {room_flags, FALSE},
00057     {sector_flags, TRUE},
00058     {type_flags, TRUE},
00059     {extra_flags, FALSE},
00060     {wear_flags, FALSE},
00061     {act_flags, FALSE},
00062     {affect_flags, FALSE},
00063     {apply_flags, TRUE},
00064     {wear_loc_flags, TRUE},
00065     {wear_loc_strings, TRUE},
00066     {container_flags, FALSE},
00067 
00068 /* ROM specific flags: */
00069 
00070     {form_flags, FALSE},
00071     {part_flags, FALSE},
00072     {ac_type, TRUE},
00073     {size_flags, TRUE},
00074     {position_flags, TRUE},
00075     {off_flags, FALSE},
00076     {imm_flags, FALSE},
00077     {res_flags, FALSE},
00078     {vuln_flags, FALSE},
00079     {weapon_class, TRUE},
00080     {weapon_type2, FALSE},
00081     {apply_types, TRUE},
00082     {0, 0}
00083 };
00084 
00085 
00086 
00087 /*****************************************************************************
00088  Name:        is_stat( table )
00089  Purpose:    Returns TRUE if the table is a stat table and FALSE if flag.
00090  Called by:    flag_value and flag_string.
00091  Note:        This function is local and used only in bit.c.
00092  ****************************************************************************/
00093 bool is_stat (const struct flag_type *flag_table)
00094 {
00095     int flag;
00096 
00097     for (flag = 0; flag_stat_table[flag].structure; flag++)
00098     {
00099         if (flag_stat_table[flag].structure == flag_table
00100             && flag_stat_table[flag].stat)
00101             return TRUE;
00102     }
00103     return FALSE;
00104 }
00105 
00106 /*****************************************************************************
00107  Name:        flag_value( table, flag )
00108  Purpose:    Returns the value of the flags entered.  Multi-flags accepted.
00109  Called by:    olc.c and olc_act.c.
00110  ****************************************************************************/
00111 int flag_value (const struct flag_type *flag_table, char *argument)
00112 {
00113     char word[MAX_INPUT_LENGTH];
00114     int bit;
00115     int marked = 0;
00116     bool found = FALSE;
00117 
00118     if (is_stat (flag_table))
00119         return flag_lookup (argument, flag_table);
00120 
00121     /*
00122      * Accept multiple flags.
00123      */
00124     for (;;)
00125     {
00126         argument = one_argument (argument, word);
00127 
00128         if (word[0] == '\0')
00129             break;
00130 
00131         if ((bit = flag_lookup (word, flag_table)) != NO_FLAG)
00132         {
00133             SET_BIT (marked, bit);
00134             found = TRUE;
00135         }
00136     }
00137 
00138     if (found)
00139         return marked;
00140     else
00141         return NO_FLAG;
00142 }
00143 
00144 
00145 
00146 /*****************************************************************************
00147  Name:        flag_string( table, flags/stat )
00148  Purpose:    Returns string with name(s) of the flags or stat entered.
00149  Called by:    act_olc.c, olc.c, and olc_save.c.
00150  ****************************************************************************/
00151 char *flag_string (const struct flag_type *flag_table, int bits)
00152 {
00153     static char buf[2][512];
00154     static int cnt = 0;
00155     int flag;
00156 
00157     if (++cnt > 1)
00158         cnt = 0;
00159 
00160     buf[cnt][0] = '\0';
00161 
00162     for (flag = 0; flag_table[flag].name != NULL; flag++)
00163     {
00164         if (!is_stat (flag_table) && IS_SET (bits, flag_table[flag].bit))
00165         {
00166             strcat (buf[cnt], " ");
00167             strcat (buf[cnt], flag_table[flag].name);
00168         }
00169         else if (flag_table[flag].bit == bits)
00170         {
00171             strcat (buf[cnt], " ");
00172             strcat (buf[cnt], flag_table[flag].name);
00173             break;
00174         }
00175     }
00176     return (buf[cnt][0] != '\0') ? buf[cnt] + 1 : "none";
00177 }

Generated on Thu Jan 13 21:48:09 2005 for Beyond the Shadows by  doxygen 1.4.0