forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.c
More file actions
275 lines (229 loc) · 6.46 KB
/
database.c
File metadata and controls
275 lines (229 loc) · 6.46 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* database.c:
*
* This file contains the code which handles the database of objects which
* have been used, and what the real names of the items are.
*
* Note: pack_index was first used to keep certain errors from happening.
* various fixes seem to have worked so it is no longer needed, but
* it is nice to have when dumping the table... with that said,
* we do not bother to keep the pack_index reference back to the
* inventory accurate later on. only new items get it set.
*
*/
# include <curses.h>
# include <string.h>
# include "types.h"
# include "globals.h"
# define TABLESIZE 101
# define NOTFOUND (-1)
struct {
int used;
int pack_index;
stuff item_type;
char fakename[NAMSIZ];
char realname[NAMSIZ];
} dbase[TABLESIZE];
int datalen = 0;
/* static declarations */
static int findfake (char *string, stuff item_type);
#if 0 /* unused code */
static char *realname (char *codename);
#endif
/*
* findfake: find the fakename database entry for 'string'
* and of item_type (both must match exactly).
*/
static int
findfake (char *string, stuff item_type)
{
int i;
for (i = 0; i < datalen; i++)
if (streq (dbase[i].fakename, string) &&
(dbase[i].item_type == item_type))
return (i);
else {
if (dbase[i].pack_index != -1) {
if (inven[dbase[i].pack_index].type != dbase[i].item_type)
dbase[i].pack_index = -1;
else if (!(streq (inven[dbase[i].pack_index].str, dbase[i].fakename) ||
streq (inven[dbase[i].pack_index].str, dbase[i].realname)))
dbase[i].pack_index = -1;
}
}
return (NOTFOUND);
}
/*
* findentry: find the database entry for 'string'
*/
int
findentry (char *string)
{
int i;
for (i = 0; i < datalen; i++)
if (streq (dbase[i].fakename, string) ||
(*dbase[i].realname && streq (dbase[i].realname, string)))
return (i);
return (NOTFOUND);
}
/*
* findentry_getfakename: find the database entry for 'string' and item_type,
* item_type must match, but the string can match either the fakename or
* the realname. returns pointer to the fakename or "".
*/
char *
findentry_getfakename (char *string, stuff item_type)
{
int i;
for (i = 0; i < datalen; i++)
if ((dbase[i].item_type == item_type) &&
(streq (dbase[i].fakename, string) ||
(*dbase[i].realname && streq (dbase[i].realname, string))))
return (dbase[i].fakename);
return ("");
}
/*
* findentry_getrealname: find the database entry for 'string' and item_type,
* item_type must match, but the string can match either the fakename or
* the realname. returns pointer to the realname or "".
*/
char *
findentry_getrealname (char *string, stuff item_type)
{
int i;
for (i = 0; i < datalen; i++)
if ((dbase[i].item_type == item_type) &&
(streq (dbase[i].fakename, string) ||
(*dbase[i].realname && streq (dbase[i].realname, string))))
return (dbase[i].realname);
return ("");
}
/*
* addobj: Add item to dbase.
*/
void
addobj (char *codename, int pack_index, stuff item_type)
{
if (findfake (codename, item_type) == NOTFOUND) {
dbase[datalen].pack_index = pack_index;
dbase[datalen].item_type = item_type;
memset (dbase[datalen].fakename, '\0', NAMSIZ);
strncpy (dbase[datalen].fakename, codename, NAMSIZ-1);
memset (dbase[datalen].realname, '\0', NAMSIZ);
datalen++;
}
}
/*
* useobj: Indicate that we have used (i.e. read, quaffed, or zapped) an
* object with name 'string'.
*/
void
useobj (char *string)
{
int i = findentry (string);
if (i != NOTFOUND) {
dbase[i].used = TRUE;
}
}
/*
* infername: Note that we now think that the object named 'codename' is
* really named 'name' (e.g. scroll 'google plex' is really a scroll of
* light).
*/
void
infername (char *codename, char *name, stuff item_type)
{
int i;
i = findfake (codename, item_type);
if (i == NOTFOUND) {
dbase[datalen].item_type = item_type;
memset (dbase[datalen].fakename, '\0', NAMSIZ);
strncpy (dbase[datalen].fakename, codename, NAMSIZ-1);
memset (dbase[datalen].realname, '\0', NAMSIZ);
strncpy (dbase[datalen].realname, name, NAMSIZ-1);
datalen++;
}
else {
if (*dbase[i].realname && strcmp (dbase[i].realname, name))
dwait (D_ERROR, "Inconsistent inference, infername: dbase[i].realname '%s', name '%s'",
dbase[i].realname, name);
else {
memset (dbase[i].realname, '\0', NAMSIZ);
strncpy (dbase[i].realname, name, NAMSIZ-1);
}
}
}
/*
* used: Return true if we have marked 'codename' as used.
*/
int
used (char *codename)
{
int i;
for (i = 0; i < datalen; i++)
if (streq (dbase[i].fakename, codename))
return (dbase[i].used);
return FALSE;
}
/*
* know: Return true if we know what the fake name for 'name' is.
*/
int
know (char *name)
{
int i;
for (i = 0; i < datalen; i++)
if (*dbase[i].realname && streq (dbase[i].realname, name))
return (TRUE);
return (FALSE);
}
#if 0 /* unused code */
/*
* realname: Returns the real name of an object named 'codename'.
*/
static char *
realname (char *codename)
{
int i;
for (i = 0; i < datalen; i++)
if (*dbase[i].realname && streq (dbase[i].fakename, codename))
return (dbase[i].realname);
return ("");
}
#endif
/*
* dumpdatabase: Debugging, dump the database on the screen.
*/
void
dumpdatabase (void)
{
int i;
for (i = 0; i < datalen; i++) {
at (i+1, 0);
printw ("%02d %c|%01d|%01d %-32s %02d '%s'",
i, (dbase[i].pack_index != -1) ? LETTER(dbase[i].pack_index) : ' ', dbase[i].item_type, dbase[i].used,
dbase[i].realname, i, dbase[i].fakename);
}
}