forked from RoguelikeRestorationProject/urogue1.03
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbag.c
448 lines (349 loc) · 10.1 KB
/
bag.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
bag.c - functions for dealing with bags
UltraRogue: The Ultimate Adventure in the Dungeons of Doom
Copyright (C) 1986, 1992, 1993, 1995 Herb Chong
All rights reserved.
See the file LICENSE.TXT for full copyright and licensing information.
*/
/*
* new bag functions
*
* This is a simple version of bag.c that uses linked lists to perform the bag
* functions. The bag is just a linked list of objects (struct object) to be
* specific, but most of that is supposed to be hidden from the user, who
* should access the bag only through the functions presented here.
*/
#include <stdlib.h>
#include "rogue.h"
/*
* apply_to_bag
*
* This is the general bag manipulation routine. The bag is subjected to
* selection criteria and those objects which pass are processed by an action
* routine. The two criteria are type and filter function. The filter
* function returns TRUE if the object passes and FALSE otherwise. The filter
* function is passed the object and the user-supplied argument. This gives
* the user plenty of flexibility in determining which items will be
* processed. The action routine is passed the object, the id, and the
* user-supplied argument given to apply_to_bag. Specifying NULL for either
* the type or filter function means that criterion always selects. A NULL
* action routine means no processing is done and the first object which
* passes the filter is returned to the user. The action routine returns TRUE
* if processing should continue or FALSE if the current item should be
* returned to the caller.
*
* Returns NULL if the bag is empty or if nothing qualified.
*
* linked_list *bag_p; // linked list of objects
* int type; // what is its type (ARMOR, ...)
* int (*bff_p)(); // bag filter function
* int (*baf_p)(); // bag action routine
* long user_arg; // user argument for filter, action
*
*/
struct object *
apply_to_bag(struct linked_list *bag_p,
int type,
int (*bff_p)(struct object *obj, bag_arg *user_arg),
int (*baf_p)(struct object *obj, bag_arg *user_arg, int id),
void *user_arg)
{
struct object *bag_obj_p = NULL; /* qualifying object */
struct object *cur_obj_p; /* current object */
bag_arg arg;
arg.varg = user_arg;
if (bag_p == NULL)
return (NULL);
for (; bag_p != NULL; bag_p = next(bag_p))
{
cur_obj_p = OBJPTR(bag_p);
if (type != 0 && type != cur_obj_p->o_type)
continue;
if (bff_p != NULL && !(*bff_p)(cur_obj_p, &arg))
continue;
/*
* At this point, we have an object which qualifies for
* processing
*/
bag_obj_p = cur_obj_p; /* in case the user wants it */
if (baf_p != NULL && (*baf_p)(cur_obj_p, &arg, identifier(bag_obj_p)))
continue;
/*
* We have an object which qualifies, quit now!
*/
break;
}
if (bag_p == NULL)
return (NULL);
return (bag_obj_p);
}
/*
count_bag()
Counts up all bag items which meet the selection criteria
*/
int
count_bag(linked_list *bag_p,
int type,
int (*bff_p)(struct object *obj, bag_arg *junk))
{
int cnt = 0;
apply_to_bag(bag_p, type, bff_p, baf_increment, &cnt);
return(cnt);
}
/*
del_bag()
Removes an object from a bag and throws it away.
*/
void
del_bag(linked_list *bag_p, object *obj_p)
{
pop_bag(&bag_p, obj_p); /* get the thing from the bag */
ur_free(obj_p); /* release the memory */
}
/*
pop_bag()
Removes an item from a bag and returns it to the user. If the item is
not in the bag, return NULL.
*/
struct object *
pop_bag(linked_list **bag_pp, object *obj_p)
{
linked_list *item_p;
for (item_p = *bag_pp; item_p != NULL && OBJPTR(item_p) != obj_p;
item_p = next(item_p));
if (item_p == NULL)
return (NULL);
_detach(bag_pp, item_p);
return (obj_p);
}
/*
push_bag()
stuff another item into the bag
*/
void
push_bag(linked_list **bag_pp, object *obj_p)
{
struct linked_list *item_p = NULL;
struct linked_list *new_p = NULL;
struct linked_list *best_p = NULL;
new_p = new_list();
new_p->data.obj = obj_p; /* attach our object */
identifier(obj_p) = get_ident(obj_p); /* tag this object for */
/* inventory */
/*
* Find a place in the bag - try to match the type, then sort by
* identifier
*/
for (item_p = *bag_pp; item_p != NULL; item_p = next(item_p))
{
if ((OBJPTR(item_p))->o_type == obj_p->o_type)
{
if (best_p == NULL)
best_p = item_p;
else if (identifier((OBJPTR(item_p))) >
identifier((OBJPTR(best_p))) &&
identifier((OBJPTR(item_p))) <
identifier(obj_p))
best_p = item_p;
}
}
_attach_after(bag_pp, best_p, new_p); /* stuff it in the list */
return;
}
/*
scan_bag()
Gets the object from the bag that matches the type and id. The object
is not removed from the bag.
*/
struct object *
scan_bag(linked_list *bag_p, int type, int id)
{
object *obj_p = NULL;
for (; bag_p != NULL; bag_p = next(bag_p))
{
obj_p = OBJPTR(bag_p);
if (obj_p->o_type == type && identifier(obj_p) == id)
break;
}
if (bag_p == NULL)
return(NULL);
return(obj_p);
}
/*
baf_decrement_test()
Assumes the argument is a pointer to int and it just decrements it.
Returns TRUE, except when the count goes to zero.
*/
int
baf_decrement_test(struct object *obj_p, bag_arg *count_p, int id)
{
NOOP(obj_p);
NOOP(id);
if (*count_p->iarg > 0)
return(TRUE);
return(FALSE);
}
/*
baf_identify()
Bag action function to identify an object. This is needed to conform
to bag action routine calling conventions and to put the linked list
structure on top of the object before calling whatis()
*/
int
baf_identify(struct object *obj_p, bag_arg *junk, int id)
{
linked_list l;
linked_list *lp = &l;
NOOP(junk);
NOOP(id);
lp->data.obj = obj_p; /* stuff object in the right place */
whatis(lp);
return(TRUE);
}
/*
baf_increment()
Assumes the argument is a pointer to int and it just increments it and
returns TRUE
*/
int
baf_increment(object *obj_p, bag_arg *count_p, int id)
{
NOOP(obj_p);
NOOP(id);
(*count_p->iarg)++;
return(TRUE);
}
/*
baf_print_item()
Bag action function to print a single item, inventory style.
*/
int
baf_print_item(struct object *obj_p, bag_arg *type, int id)
{
char inv_temp[3 * LINELEN]; /* plenty of space for paranoid programmers */
if (*(char *)type->iarg == 0)
sprintf(inv_temp, "%c%c) %s", obj_p->o_type,
print_letters[id], inv_name(obj_p, LOWERCASE), FALSE);
else
sprintf(inv_temp, "%c) %s", print_letters[id],
inv_name(obj_p, LOWERCASE), FALSE);
add_line(inv_temp);
return(TRUE);
}
/*
bff_group()
This bag filter function checks to see if two items can be combined by
adjusting the count. Grouped items can be combined if the group numbers
match. The only other item that is allowed to have a count is food, and
there an exact match is required.
*/
int
bff_group(struct object *obj_p, bag_arg *arg)
{
struct object *new_obj_p = arg->obj;
if (new_obj_p->o_group > 0 && new_obj_p->o_group == obj_p->o_group)
return(TRUE);
if (new_obj_p->o_type == FOOD &&
obj_p->o_type == new_obj_p->o_type &&
obj_p->o_which == new_obj_p->o_which)
return(TRUE);
return(FALSE);
}
/*
bff_callable
Figures out which items can be callable: current rules are:
potions, scrolls, staffs, and rings.
*/
int
bff_callable(struct object *obj_p, bag_arg *junk)
{
NOOP(junk);
if (obj_p->o_type == POTION || obj_p->o_type == RING ||
obj_p->o_type == STICK || obj_p->o_type == SCROLL)
return(TRUE);
return(FALSE);
}
/*
bff_markable()
Selects which items can be marked. Current rules exclude only gold.
*/
int
bff_markable(struct object *obj_p, bag_arg *junk)
{
NOOP(junk);
if (obj_p->o_type == GOLD)
return(FALSE);
return(TRUE);
}
/*
bffron()
returns TRUE if hero is wearing this ring
*/
int
bffron(object *obj_p, bag_arg *junk)
{
NOOP(junk);
return(cur_ring[LEFT_1] == obj_p || cur_ring[LEFT_2] == obj_p ||
cur_ring[LEFT_3] == obj_p || cur_ring[LEFT_4] == obj_p ||
cur_ring[LEFT_5] ||
cur_ring[RIGHT_1] == obj_p || cur_ring[RIGHT_2] == obj_p ||
cur_ring[RIGHT_3] == obj_p || cur_ring[RIGHT_4] == obj_p ||
cur_ring[RIGHT_5]);
}
/*
bff_zappable()
Selects which items can be zapped. This includes both sticks and
magically enhanced weapons with lightning ability.
*/
int
bff_zappable(struct object *obj_p, bag_arg *junk)
{
NOOP(junk);
if (obj_p->o_type == STICK)
return(TRUE);
if (obj_p->o_type == WEAPON && obj_p->o_flags & ISZAPPED)
return(TRUE);
return (FALSE);
}
/*
baf_curse()
Curse all non-artifact items in the player's pack
*/
int
baf_curse(struct object *obj_p, bag_arg *junk, int id)
{
NOOP(junk);
NOOP(id);
if (obj_p->o_type != ARTIFACT && rnd(8) == 0)
{
obj_p->o_flags |= ISCURSED;
obj_p->o_flags &= ~ISBLESSED;
}
return(TRUE);
}
/*
bafcweapon()
bag action routine to fetch the current weapon
*/
int
bafcweapon(struct object *obj_p, bag_arg *junk, int id)
{
NOOP(junk);
NOOP(id);
if (obj_p == cur_weapon)
return(FALSE); /* found what we wanted - stop and return it */
return(TRUE);
}
/*
bafcarmor()
bag action routine to fetch the current armor
*/
int
bafcarmor(struct object *obj_p, bag_arg *junk, int id)
{
NOOP(junk);
NOOP(id);
if (obj_p == cur_armor)
return(FALSE); /* found what we wanted - stop and return it */
return(TRUE);
}