-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.c
340 lines (294 loc) · 7.73 KB
/
items.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2018 Easter <[email protected]>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "items.h"
#include "items-desc.h"
#include "rooms.h"
static void assign_name(char **word1, char **word2, char **adj, char **name);
static int in_room(int room_id, char *adj, char *name);
static int in_inv(char *adj, char *name);
/* display items in room */
extern void room_items(int room_id)
{
int i;
for(i = 0; items[i].item_id != -1; i++) {
if (items[i].location == room_id && items[i].hidden == NO)
printf("%s\n",items[i].item_desc_floor);
}
}
/* function for item grammar */
static void assign_name(char **word1, char **word2, char **adj, char **name)
{
if (*word2 == NULL) {
*adj = NULL;
*name = *word1;
} else {
*adj = *word1;
*name = *word2;
}
}
/* inventory stuff */
static char inventory[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/* search for hidden items */
extern void search(int room_id)
{
int i;
int count;
putchar('\n');
if (search_desc())
printf("You carefully search the area. ");
for (count = 0, i = 0; items[i].item_id != -1; i++) {
if (items[i].location == room_id && items[i].hidden == YES) {
if (count > 0)
printf("\nYou also found the ");
else
printf("You found the ");
count++;
if (items[i].item_adj != NULL)
printf("%s ",items[i].item_adj);
printf("%s! ",items[i].item_name);
items[i].hidden = NO;
items[i].location = -1;
inventory[i] = 1;
}
}
if (count == 0)
printf("You didn't find anything.\n");
else
putchar('\n');
}
/* display items in inventory */
extern void list_inv(void)
{
int i;
int count;
printf("\nInventory:\n");
for (count = 0, i = 0; items[i].item_id != -1; i++) {
if (inventory[i] == YES) {
count++;
putchar('\t');
if (items[i].item_adj != NULL)
printf("%s ",items[i].item_adj);
printf("%s\n",items[i].item_name);
}
}
if (count == 0)
printf("\tnothing.\n");
}
/* in inventory */
static int in_room(int room_id, char *adj, char *name)
{
int i;
int item_num;
int count;
/* find out if given name is ambiguous */
for (count = 0, i = 0; items[i].item_id != -1; i++) {
if (strcmp(items[i].item_name,name) == 0 &&
items[i].location == room_id && items[i].hidden == NO) {
if (adj == NULL) {
count++;
item_num = i;
} else if (items[i].item_adj != NULL &&
strcmp(adj,items[i].item_adj) == 0) {
count = 1;
item_num = i;
break;
}
}
}
/* return results */
if (count == 0) {
return -1;
} else if (count > 1) {
return -2;
} else {
return item_num;
}
}
/* pick up item */
extern void take_item(int room_id, char *word1, char *word2)
{
char *adj;
char *name;
int item_num;
putchar('\n');
if (word1 == NULL) {
printf("Take what?\n");
return;
}
/* assign an adjective if possible */
assign_name(&word1, &word2, &adj, &name);
/* find out if given name is ambiguous */
item_num = in_room(room_id, adj, name);
/* if none were found, the item is not present */
/* if only one was found, it's not ambiguous */
if (item_num == -1) {
printf("There is no ");
if (adj != NULL) printf("%s ",adj);
printf("%s here.\n",name);
return;
} else if (item_num == -2) {
printf("Which %s do you want to take?\n",name);
return;
}
/* take item if possible */
if (items[item_num].takeable == YES) {
items[item_num].location = -1;
inventory[item_num] = 1;
printf("You took the ");
if (items[item_num].item_adj != NULL)
printf("%s ",items[item_num].item_adj);
printf("%s.\n",name);
} else {
printf("You cannot take the %s!\n",name);
}
}
extern void take(int item_id)
{
inventory[item_id] = 1;
}
static int in_inv(char *adj, char *name)
{
int i;
int item_num;
int count;
/* find out if given name is ambiguous */
for (count = 0, i = 0; items[i].item_id != -1; i++) {
if (strcmp(items[i].item_name,name) == 0 &&
inventory[i] == YES) {
if (adj == NULL) {
count++;
item_num = i;
} else if (items[i].item_adj != NULL &&
strcmp(adj,items[i].item_adj) == 0) {
count = 1;
item_num = i;
break;
}
}
}
/* return results */
if (count == 0) {
return -1;
} else if (count > 1) {
return -2;
} else {
return item_num;
}
}
extern int unique_item(int room_id, char *adj, char *name)
{
int room;
int inv;
if (name == NULL) return -1;
room = in_room(room_id, adj,name);
inv = in_inv(adj,name);
if ((room >= 0 && inv >= 0) || room == -2 || inv == -2) {
return -2;
} else if (room == -1 && inv == -1) {
return -1;
} else {
if (inv == -1) inv = room;
return inv;
}
}
/* drop item */
extern void drop_item(int room_id, char *word1, char *word2)
{
char *adj = NULL;
char *name = NULL;
int item_num;
putchar('\n');
if (word1 == NULL) {
printf("Drop what?\n");
return;
}
/* assign an adjective if possible */
assign_name(&word1, &word2, &adj, &name);
/* find out if given name is ambiguous */
item_num = in_inv(adj, name);
/* if none were found, the item is not in inventory */
/* if only one was found, it's not ambiguous */
if (item_num == -1) {
printf("You do not have a ");
if (adj != NULL) printf("%s ",adj);
printf("%s.\n",name);
return;
} else if (item_num == -2) {
printf("Which %s do you want to drop?\n",name);
return;
}
/* drop item */
items[item_num].location = room_id;
inventory[item_num] = NO;
printf("You dropped the ");
if (items[item_num].item_adj != NULL)
printf("%s ",items[item_num].item_adj);
printf("%s.\n",name);
}
/* item in inventory? */
extern void look_item(int room_id, char *word1, char *word2)
{
char *adj = NULL;
char *name = NULL;
int item_num;
putchar('\n');
if (word1 == NULL) {
printf("Look at what?\n");
return;
}
/* assign an adjective if possible */
assign_name(&word1, &word2, &adj, &name);
/* in location or inventory? */
item_num = unique_item(room_id, adj, name);
/* is it ambiguous? */
if (item_num == -2) {
printf("Which %s?\n",name);
return;
} else if (item_num == -1) {
printf("There is no ");
if (adj != NULL) printf("%s ",adj);
printf("%s here.\n",name);
return;
}
/* output description */
printf("%s\n",items[item_num].item_desc_exam);
}
extern void break_item(int item_id)
{
inventory[item_id] = 0;
items[item_id].location = -1;
}
extern void create_item(int item_id, int room_id)
{
items[item_id].location = room_id;
}