/
bit.c
184 lines (164 loc) · 5.79 KB
/
bit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************************************************************************
* 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 <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "tables.h"
struct flag_stat_type {
const struct flag_type *structure;
bool stat;
};
/*****************************************************************************
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.
****************************************************************************/
const struct flag_stat_type flag_stat_table[] =
{
/* { structure stat }, */
{area_flags, FALSE},
{sex_flags, TRUE},
{exit_flags, FALSE},
{door_resets, TRUE},
{room_flags, FALSE},
{sector_flags, TRUE},
{type_flags, TRUE},
{extra_flags, FALSE},
{wear_flags, FALSE},
{act_flags, FALSE},
{affect_flags, FALSE},
{apply_flags, TRUE},
{wear_loc_flags, TRUE},
{wear_loc_strings, TRUE},
{container_flags, FALSE},
/* ROM specific flags: */
{form_flags, FALSE},
{part_flags, FALSE},
{ac_type, TRUE},
{size_flags, TRUE},
{position_flags, TRUE},
{off_flags, FALSE},
{imm_flags, FALSE},
{res_flags, FALSE},
{vuln_flags, FALSE},
{weapon_class, TRUE},
{weapon_type2, FALSE},
{apply_types, TRUE},
{shield_class, FALSE},
{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
&& flag_stat_table[flag].stat)
return TRUE;
}
return FALSE;
}
/*
* This function is Russ Taylor's creation. Thanks Russ!
* All code copyright (C) Russ Taylor, permission to use and/or distribute
* has NOT been granted. Use only in this OLC package has been granted.
*/
/*****************************************************************************
Name: flag_lookup2( flag, table )
Purpose: Returns the value of a single, settable flag from the table.
Called by: flag_value and flag_string.
Note: This function is local and used only in bit.c.
****************************************************************************/
int flag_lookup2(const char *name, const struct flag_type *flag_table)
{
int flag;
for (flag = 0; flag_table[flag].name != NULL; flag++) {
if (!str_cmp(name, flag_table[flag].name)
&& flag_table[flag].settable)
return flag_table[flag].bit;
}
return NO_FLAG;
}
/*****************************************************************************
Name: flag_value( table, flag )
Purpose: Returns the value of the flags entered. Multi-flags accepted.
Called by: olc.c and olc_act.c.
****************************************************************************/
int flag_value(const struct flag_type *flag_table, char *argument)
{
char word[MAX_INPUT_LENGTH];
int bit;
int marked = 0;
bool found = FALSE;
if (is_stat(flag_table)) {
one_argument(argument, word);
if ((bit = flag_lookup2(word, flag_table)) != NO_FLAG)
return bit;
else
return NO_FLAG;
}
/*
* Accept multiple flags.
*/
for (;;) {
argument = one_argument(argument, word);
if (word[0] == '\0')
break;
if ((bit = flag_lookup2(word, flag_table)) != NO_FLAG) {
SET_BIT(marked, 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.
****************************************************************************/
char *flag_string(const struct flag_type *flag_table, int bits)
{
static char buf[512];
int flag;
buf[0] = '\0';
for (flag = 0; flag_table[flag].name != NULL; flag++) {
if (!is_stat(flag_table) && IS_SET(bits, flag_table[flag].bit)) {
strcat(buf, " ");
strcat(buf, flag_table[flag].name);
} else if (flag_table[flag].bit == bits) {
strcat(buf, " ");
strcat(buf, flag_table[flag].name);
break;
}
}
return (buf[0] != '\0') ? buf + 1 : "none";
}