-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.c
468 lines (447 loc) · 9.53 KB
/
pack.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Routines to deal with the pack
*
* @(#)pack.c 9.0 (rdk) 7/17/84
*
* Super-Rogue
* Copyright (C) 1984 Robert D. Kindelberger
* All rights reserved.
*
* Based on "Rogue: Exploring the Dungeons of Doom"
* Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*/
#include <ctype.h>
#include <curses.h>
#include <stdbool.h>
#include <stddef.h>
#include "rogue.h"
#include "rogue.ext"
/*
* add_pack:
* Pick up an object and add it to the pack. If the argument
* is non-null use it as the linked_list pointer instead of
* getting it off the ground.
*/
bool add_pack(struct linked_list *item,bool silent)
{
struct linked_list *ip, *lp;
struct object *obj, *op;
bool from_floor;
char delchar;
if (player.t_room == NULL)
delchar = PASSAGE;
else
delchar = FLOOR;
if (item == NULL) {
from_floor = TRUE;
if ((item = find_obj(hero.y, hero.x)) == NULL) {
mpos = 0;
msg("That object must have been an illusion.");
mvaddch(hero.y, hero.x, delchar);
return FALSE;
}
/*
* Check for scare monster scrolls
*/
obj = OBJPTR(item);
if (obj->o_type == SCROLL && obj->o_which == S_SCARE) {
if (o_on(obj,ISFOUND)) {
msg("The scroll turns to dust as you pick it up.");
detach(lvl_obj, item);
discard(item);
mvaddch(hero.y, hero.x, delchar);
return FALSE;
}
}
}
else
from_floor = FALSE;
obj = OBJPTR(item);
/*
* See if this guy can carry any more weight
*/
if (itemweight(obj) + him->s_pack > him->s_carry) {
msg("You can't carry that %s.", obj->o_typname);
return FALSE;
}
/*
* Check if there is room
*/
if (packvol + obj->o_vol > V_PACK) {
msg("That %s won't fit in your pack.", obj->o_typname);
return FALSE;
}
if (from_floor) {
detach(lvl_obj, item);
mvaddch(hero.y, hero.x, delchar);
}
item->l_prev = NULL;
item->l_next = NULL;
setoflg(obj, ISFOUND);
/*
* start looking thru pack to find the start of items
* with the same type.
*/
lp = pack;
for (ip = pack; ip != NULL; ip = next(ip)) {
op = OBJPTR(ip);
/*
* If we find a matching type then quit.
*/
if (op->o_type == obj->o_type)
break;
if (next(ip) != NULL)
lp = next(lp); /* update "previous" entry */
}
/*
* If the pack was empty, just stick the item in it.
*/
if (pack == NULL) {
pack = item;
item->l_prev = NULL;
}
/*
* If we looked thru the pack, but could not find an
* item of the same type, then stick it at the end,
* unless it was food, then put it in front.
*/
else if (ip == NULL) {
if (obj->o_type == FOOD) { /* insert food at front */
item->l_next = pack;
pack->l_prev = item;
pack = item;
item->l_prev = NULL;
}
else { /* insert other stuff at back */
lp->l_next = item;
item->l_prev = lp;
}
}
/*
* Here, we found at least one item of the same type.
* Look thru these items to see if there is one of the
* same group. If so, increment the count and throw the
* new item away. If not, stick it at the end of the
* items with the same type. Also keep all similar
* objects near each other, like all identify scrolls, etc.
*/
else {
struct linked_list **save;
while (ip != NULL && op->o_type == obj->o_type) {
if (op->o_group == obj->o_group) {
if (op->o_flags == obj->o_flags) {
op->o_count++;
discard(item);
item = ip;
goto picked_up;
}
else {
goto around;
}
}
if (op->o_which == obj->o_which) {
if (obj->o_type == FOOD)
ip = next(ip);
break;
}
around:
ip = next(ip);
if (ip != NULL) {
op = OBJPTR(ip);
lp = next(lp);
}
}
/*
* If inserting into last of group at end of pack,
* just tack on the end.
*/
if (ip == NULL) {
lp->l_next = item;
item->l_prev = lp;
}
/*
* Insert into the last of a group of objects
* not at the end of the pack.
*/
else {
save = &((ip->l_prev)->l_next);
item->l_next = ip;
item->l_prev = ip->l_prev;
ip->l_prev = item;
*save = item;
}
}
picked_up:
obj = OBJPTR(item);
if (!silent)
msg("%s (%c)",inv_name(obj,FALSE),pack_char(obj));
if (obj->o_type == AMULET)
amulet = TRUE;
updpack(); /* new pack weight & volume */
return TRUE;
}
/*
* inventory:
* Show what items are in a specific list
*/
bool inventory(struct linked_list *list,int type)
{
struct linked_list *pc;
struct object *obj;
char ch;
int cnt;
if (list == NULL) { /* empty list */
msg(type == 0 ? "Empty handed." : "Nothing appropriate.");
return FALSE;
}
else if (next(list) == NULL) { /* only 1 item in list */
obj = OBJPTR(list);
msg("a) %s", inv_name(obj, FALSE));
return TRUE;
}
cnt = 0;
wclear(hw);
for (ch = 'a', pc = list; pc != NULL; pc = next(pc), ch = npch(ch)) {
obj = OBJPTR(pc);
wprintw(hw,"%c) %s\n\r",ch,inv_name(obj, FALSE));
if (++cnt > LINES - 2 && next(pc) != NULL) {
dbotline(hw, morestr);
cnt = 0;
wclear(hw);
}
}
dbotline(hw,spacemsg);
restscr(cw);
return TRUE;
}
/*
* pick_up:
* Add something to characters pack.
*/
void pick_up(char ch)
{
nochange = FALSE;
switch(ch) {
case GOLD:
money();
when ARMOR:
case POTION:
case FOOD:
case WEAPON:
case SCROLL:
case AMULET:
case RING:
case STICK:
add_pack(NULL, FALSE);
otherwise:
msg("That item is ethereal !!!");
}
}
/*
* picky_inven:
* Allow player to inventory a single item
*/
int picky_inven()
{
struct linked_list *item;
char ch, mch;
if (pack == NULL)
msg("You aren't carrying anything.");
else if (next(pack) == NULL)
msg("a) %s", inv_name(OBJPTR(pack), FALSE));
else {
msg("Item: ");
mpos = 0;
if ((mch = readchar()) == ESCAPE) {
msg("");
return 0;
}
for (ch='a',item=pack; item != NULL; item=next(item),ch=npch(ch))
if (ch == mch) {
msg("%c) %s",ch,inv_name(OBJPTR(item), FALSE));
return 0;
}
if (ch == 'A')
ch = 'z';
else
ch -= 1;
msg("Range is 'a' to '%c'", ch);
}
return 0;
}
/*
* get_item:
* pick something out of a pack for a purpose
*/
struct linked_list *get_item(char *purpose,int type)
{
struct linked_list *obj, *pit, *savepit;
struct object *pob;
int ch, och, anr, cnt;
if (pack == NULL) {
msg("You aren't carrying anything.");
return NULL;
}
if (type != WEAPON && (type != 0 || next(pack) == NULL)) {
/*
* see if we have any of the type requested
*/
pit = pack;
anr = 0;
for (ch = 'a'; pit != NULL; pit = next(pit), ch = npch(ch)) {
pob = OBJPTR(pit);
if (type == pob->o_type || type == 0) {
++anr;
savepit = pit; /* save in case of only 1 */
}
}
if (anr == 0) {
msg("Nothing to %s",purpose);
after = FALSE;
return NULL;
}
else if (anr == 1) { /* only found one of 'em */
do {
struct object *opb;
opb = OBJPTR(savepit);
msg("%s what (* for the item)?",purpose);
och = readchar();
if (och == '*') {
mpos = 0;
msg("%c) %s",pack_char(opb),inv_name(opb,FALSE));
continue;
}
if (och == ESCAPE) {
msg("");
after = FALSE;
return NULL;
}
if (isalpha(och) && och != pack_char(opb)) {
mpos = 0;
msg("You can't %s that !!", purpose);
after = FALSE;
return NULL;
}
} while(!isalpha(och));
mpos = 0;
return savepit; /* return this item */
}
}
for (;;) {
msg("%s what? (* for list): ",purpose);
ch = readchar();
mpos = 0;
if (ch == ESCAPE) { /* abort if escape hit */
after = FALSE;
msg(""); /* clear display */
return NULL;
}
if (ch == '*') {
wclear(hw);
pit = pack; /* point to pack */
cnt = 0;
for (ch='a'; pit != NULL; pit=next(pit), ch=npch(ch)) {
pob = OBJPTR(pit);
if (type == 0 || type == pob->o_type) {
wprintw(hw,"%c) %s\n\r",ch,inv_name(pob,FALSE));
if (++cnt > LINES - 2 && next(pit) != NULL) {
cnt = 0;
dbotline(hw, morestr);
wclear(hw);
}
}
}
wmove(hw, LINES - 1,0);
wprintw(hw,"%s what? ",purpose);
draw(hw); /* write screen */
anr = FALSE;
do {
ch = readchar();
if (isalpha(ch) || ch == ESCAPE)
anr = TRUE;
} while(!anr); /* do till we got it right */
restscr(cw); /* redraw orig screen */
if (ch == ESCAPE) {
after = FALSE;
msg(""); /* clear top line */
return NULL; /* all done if abort */
}
/* ch has item to get from pack */
}
for (obj=pack,och='a';obj!=NULL;obj=next(obj),och=npch(och))
if (ch == och)
break;
if (obj == NULL) {
if (och == 'A')
och = 'z';
else
och -= 1;
msg("Please specify a letter between 'a' and '%c'",och);
continue;
}
else
return obj;
}
}
/*
* pack_char:
* Get the character of a particular item in the pack
*/
char pack_char(struct object *obj)
{
struct linked_list *item;
char c;
c = 'a';
for (item = pack; item != NULL; item = next(item))
if (OBJPTR(item) == obj)
return c;
else
c = npch(c);
return '%';
}
/*
* idenpack:
* Identify all the items in the pack
*/
void idenpack()
{
struct linked_list *pc;
for (pc = pack ; pc != NULL ; pc = next(pc))
whatis(pc);
}
/*
* del_pack:
* Take something out of the hero's pack
*/
void del_pack(struct linked_list *what)
{
struct object *op;
op = OBJPTR(what);
cur_null(op); /* check for current stuff */
if (op->o_count > 1) {
op->o_count--;
}
else {
detach(pack,what);
discard(what);
}
updpack();
}
/*
* cur_null:
* This updates cur_weapon etc for dropping things
*/
void cur_null(struct object *op)
{
if (op == cur_weapon)
cur_weapon = NULL;
else if (op == cur_armor)
cur_armor = NULL;
else if (op == cur_ring[LEFT])
cur_ring[LEFT] = NULL;
else if (op == cur_ring[RIGHT])
cur_ring[RIGHT] = NULL;
}