-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.c
534 lines (437 loc) · 9.45 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Routines to deal with the pack
*
* @(#)pack.c 4.40 (Berkeley) 02/05/99
*
* Rogue: Exploring the Dungeons of Doom
* Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*/
#include <string.h>
#include <curses.h>
#include <ctype.h>
#include "rogue.h"
/*
* 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 gettting
* it off the ground.
*/
void
add_pack(THING *obj, bool silent)
{
THING *op, *lp;
bool from_floor;
from_floor = FALSE;
if (obj == NULL) {
if ((obj = find_obj(hero.y, hero.x)) == NULL)
return;
from_floor = TRUE;
}
/*
* Check for and deal with scare monster scrolls
*/
if (obj->o_type == SCROLL && obj->o_which == S_SCARE)
if (obj->o_flags & ISFOUND) {
detach(lvl_obj, obj);
mvaddch(hero.y, hero.x, floor_ch());
chat(hero.y, hero.x) = (proom->r_flags & ISGONE) ? PASSAGE : FLOOR;
discard(obj);
msg("the scroll turns to dust as you pick it up");
return;
}
if (pack == NULL) {
pack = obj;
obj->o_packch = pack_char();
inpack++;
}
else {
lp = NULL;
for (op = pack; op != NULL; op = next(op)) {
if (op->o_type != obj->o_type)
lp = op;
else {
while (op->o_type == obj->o_type && op->o_which != obj->o_which) {
lp = op;
if (next(op) == NULL)
break;
else
op = next(op);
}
if (op->o_type == obj->o_type && op->o_which == obj->o_which) {
if (ISMULT(op->o_type)) {
if (!pack_room(from_floor, obj))
return;
op->o_count++;
dump_it:
discard(obj);
obj = op;
lp = NULL;
goto out;
}
else if (obj->o_group) {
lp = op;
while (op->o_type == obj->o_type
&& op->o_which == obj->o_which
&& op->o_group != obj->o_group) {
lp = op;
if (next(op) == NULL)
break;
else
op = next(op);
}
if (op->o_type == obj->o_type
&& op->o_which == obj->o_which
&& op->o_group == obj->o_group) {
op->o_count += obj->o_count;
inpack--;
if (!pack_room(from_floor, obj))
return;
goto dump_it;
}
}
else
lp = op;
}
out:
break;
}
}
if (lp != NULL) {
if (!pack_room(from_floor, obj))
return;
else {
obj->o_packch = pack_char();
next(obj) = next(lp);
prev(obj) = lp;
if (next(lp) != NULL)
prev(next(lp)) = obj;
next(lp) = obj;
}
}
}
obj->o_flags |= ISFOUND;
/*
* If this was the object of something's desire, that monster will
* get mad and run at the hero.
*/
for (op = mlist; op != NULL; op = next(op))
if (op->t_dest == &obj->o_pos)
op->t_dest = &hero;
if (obj->o_type == AMULET)
amulet = TRUE;
/*
* Notify the user
*/
if (!silent) {
if (!terse)
addmsg("you now have ");
msg("%s (%c)", inv_name(obj, !terse), obj->o_packch);
}
}
/*
* pack_room:
* See if there's room in the pack. If not, print out an
* appropriate message
*/
bool
pack_room(bool from_floor, THING *obj)
{
if (++inpack > MAXPACK) {
if (!terse)
addmsg("there's ");
addmsg("no room");
if (!terse)
addmsg(" in your pack");
endmsg();
if (from_floor)
move_msg(obj);
inpack = MAXPACK;
return FALSE;
}
if (from_floor) {
detach(lvl_obj, obj);
mvaddch(hero.y, hero.x, floor_ch());
chat(hero.y, hero.x) = (proom->r_flags & ISGONE) ? PASSAGE : FLOOR;
}
return TRUE;
}
/*
* leave_pack:
* take an item out of the pack
*/
THING *
leave_pack(THING *obj, bool newobj, bool all)
{
THING *nobj;
inpack--;
nobj = obj;
if (obj->o_count > 1 && !all) {
last_pick = obj;
obj->o_count--;
if (obj->o_group)
inpack++;
if (newobj) {
nobj = new_item();
*nobj = *obj;
next(nobj) = NULL;
prev(nobj) = NULL;
nobj->o_count = 1;
}
}
else {
last_pick = NULL;
pack_used[obj->o_packch - 'a'] = FALSE;
detach(pack, obj);
}
return nobj;
}
/*
* pack_char:
* Return the next unused pack character.
*/
char
pack_char()
{
bool *bp;
for (bp = pack_used; *bp; bp++)
continue;
*bp = TRUE;
return (char)((int)(bp - pack_used) + 'a');
}
/*
* inventory:
* List what is in the pack. Return TRUE if there is something of
* the given type.
*/
bool
inventory(THING *list, int type)
{
static char inv_temp[MAXSTR];
n_objs = 0;
for (; list != NULL; list = next(list)) {
if (type && type != list->o_type && !(type == CALLABLE &&
list->o_type != FOOD && list->o_type != AMULET) &&
!(type == R_OR_S && (list->o_type == RING || list->o_type == STICK)))
continue;
n_objs++;
#ifdef MASTER
if (!list->o_packch)
strcpy(inv_temp, "%s");
else
#endif
sprintf(inv_temp, "%c) %%s", list->o_packch);
msg_esc = TRUE;
if (add_line(inv_temp, inv_name(list, FALSE)) == ESCAPE) {
msg_esc = FALSE;
msg("");
return TRUE;
}
msg_esc = FALSE;
}
if (n_objs == 0) {
if (terse)
msg(type == 0 ? "empty handed" :
"nothing appropriate");
else
msg(type == 0 ? "you are empty handed" :
"you don't have anything appropriate");
return FALSE;
}
end_line();
return TRUE;
}
/*
* pick_up:
* Add something to characters pack.
*/
void
pick_up(char ch)
{
THING *obj, *tmpl;
if (on(player, ISLEVIT))
return;
obj = find_obj(hero.y, hero.x);
if (move_on)
move_msg(obj);
else
switch (ch) {
case GOLD:
if (obj == NULL)
return;
money(obj->o_goldval);
proom->r_goldval = 0;
for (tmpl = mlist; tmpl != NULL; tmpl = next(tmpl)) {
if (tmpl->t_dest == &obj->o_pos) {
tmpl->t_dest = &hero;
}
}
detach(lvl_obj, obj);
discard(obj);
break;
default:
#ifdef MASTER
debug("Where did you pick a '%s' up???", unctrl(ch));
#endif
case ARMOR:
case POTION:
case FOOD:
case WEAPON:
case SCROLL:
case AMULET:
case RING:
case STICK:
add_pack((THING *) NULL, FALSE);
break;
}
}
/*
* move_msg:
* Print out the message if you are just moving onto an object
*/
void
move_msg(THING *obj)
{
if (!terse)
addmsg("you ");
msg("moved onto %s", inv_name(obj, TRUE));
}
/*
* picky_inven:
* Allow player to inventory a single item
*/
void
picky_inven()
{
THING *obj;
char mch;
if (pack == NULL)
msg("you aren't carrying anything");
else if (next(pack) == NULL)
msg("a) %s", inv_name(pack, FALSE));
else {
msg(terse ? "item: " : "which item do you wish to inventory: ");
mpos = 0;
if ((mch = readchar()) == ESCAPE) {
msg("");
return;
}
for (obj = pack; obj != NULL; obj = next(obj))
if (mch == obj->o_packch) {
msg("%c) %s", mch, inv_name(obj, FALSE));
return;
}
msg("'%s' not in pack", unctrl(mch));
}
}
/*
* get_item:
* Pick something out of a pack for a purpose
*/
THING *
get_item(char *purpose, int type)
{
THING *obj;
char ch;
if (pack == NULL)
msg("you aren't carrying anything");
else if (again)
if (last_pick)
return last_pick;
else
msg("you ran out");
else {
for (;;) {
if (!terse)
addmsg("which object do you want to ");
addmsg(purpose);
if (terse)
addmsg(" what");
msg("? (* for list): ");
ch = readchar();
mpos = 0;
/*
* Give the poor player a chance to abort the command
*/
if (ch == ESCAPE) {
reset_last();
after = FALSE;
msg("");
return NULL;
}
n_objs = 1; /* normal case: person types one char */
if (ch == '*') {
mpos = 0;
if (inventory(pack, type) == 0) {
after = FALSE;
return NULL;
}
continue;
}
for (obj = pack; obj != NULL; obj = next(obj))
if (obj->o_packch == ch)
break;
if (obj == NULL) {
msg("'%s' is not a valid item",unctrl(ch));
continue;
}
else
return obj;
}
}
return NULL;
}
/*
* money:
* Add or subtract gold from the pack
*/
void
money(int value)
{
purse += value;
mvaddch(hero.y, hero.x, floor_ch());
chat(hero.y, hero.x) = (proom->r_flags & ISGONE) ? PASSAGE : FLOOR;
if (value > 0) {
if (!terse)
addmsg("you found ");
msg("%d gold pieces", value);
}
}
/*
* floor_ch:
* Return the appropriate floor character for her room
*/
char
floor_ch()
{
if (proom->r_flags & ISGONE)
return PASSAGE;
return (show_floor() ? FLOOR : ' ');
}
/*
* floor_at:
* Return the character at hero's position, taking see_floor
* into account
*/
char
floor_at()
{
char ch;
ch = chat(hero.y, hero.x);
if (ch == FLOOR)
ch = floor_ch();
return ch;
}
/*
* reset_last:
* Reset the last command when the current one is aborted
*/
void
reset_last()
{
last_comm = l_last_comm;
last_dir = l_last_dir;
last_pick = l_last_pick;
}