forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkobj.c
More file actions
3585 lines (3295 loc) · 122 KB
/
mkobj.c
File metadata and controls
3585 lines (3295 loc) · 122 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.7 mkobj.c $NHDT-Date: 1654881236 2022/06/10 17:13:56 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.237 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Derek S. Ray, 2015. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
static boolean may_generate_eroded(struct obj *);
static void mkobj_erosions(struct obj *);
static void mkbox_cnts(struct obj *);
static unsigned nextoid(struct obj *, struct obj *);
static void mksobj_init(struct obj *, boolean);
static int item_on_ice(struct obj *);
static void shrinking_glob_gone(struct obj *);
static void obj_timer_checks(struct obj *, coordxy, coordxy, int);
static void container_weight(struct obj *);
static struct obj *save_mtraits(struct obj *, struct monst *);
static void objlist_sanity(struct obj *, int, const char *);
static void shop_obj_sanity(struct obj *, const char *);
static void mon_obj_sanity(struct monst *, const char *);
static void insane_obj_bits(struct obj *, struct monst *);
static boolean nomerge_exception(struct obj *);
static const char *where_name(struct obj *);
static void insane_object(struct obj *, const char *, const char *,
struct monst *);
static void check_contained(struct obj *, const char *);
static void check_glob(struct obj *, const char *);
static void sanity_check_worn(struct obj *);
struct icp {
int iprob; /* probability of an item type */
char iclass; /* item class */
};
static const struct icp mkobjprobs[] = { { 10, WEAPON_CLASS },
{ 10, ARMOR_CLASS },
{ 20, FOOD_CLASS },
{ 8, TOOL_CLASS },
{ 8, GEM_CLASS },
{ 16, POTION_CLASS },
{ 16, SCROLL_CLASS },
{ 4, SPBOOK_CLASS },
{ 4, WAND_CLASS },
{ 3, RING_CLASS },
{ 1, AMULET_CLASS } };
static const struct icp boxiprobs[] = { { 18, GEM_CLASS },
{ 15, FOOD_CLASS },
{ 18, POTION_CLASS },
{ 18, SCROLL_CLASS },
{ 12, SPBOOK_CLASS },
{ 7, COIN_CLASS },
{ 6, WAND_CLASS },
{ 5, RING_CLASS },
{ 1, AMULET_CLASS } };
static const struct icp rogueprobs[] = { { 12, WEAPON_CLASS },
{ 12, ARMOR_CLASS },
{ 22, FOOD_CLASS },
{ 22, POTION_CLASS },
{ 22, SCROLL_CLASS },
{ 5, WAND_CLASS },
{ 5, RING_CLASS } };
static const struct icp hellprobs[] = { { 20, WEAPON_CLASS },
{ 20, ARMOR_CLASS },
{ 16, FOOD_CLASS },
{ 12, TOOL_CLASS },
{ 10, GEM_CLASS },
{ 1, POTION_CLASS },
{ 1, SCROLL_CLASS },
{ 8, WAND_CLASS },
{ 8, RING_CLASS },
{ 4, AMULET_CLASS } };
static const struct oextra zerooextra = DUMMY;
static void
init_oextra(struct oextra *oex)
{
*oex = zerooextra;
}
struct oextra *
newoextra(void)
{
struct oextra *oextra;
oextra = (struct oextra *) alloc(sizeof (struct oextra));
init_oextra(oextra);
return oextra;
}
void
dealloc_oextra(struct obj *o)
{
struct oextra *x = o->oextra;
if (x) {
if (x->oname)
free((genericptr_t) x->oname), x->oname = 0;
if (x->omonst)
free_omonst(o); /* note: pass 'o' rather than 'x' */
if (x->omailcmd)
free((genericptr_t) x->omailcmd), x->omailcmd = 0;
free((genericptr_t) x);
o->oextra = (struct oextra *) 0;
}
}
void
newomonst(struct obj *otmp)
{
if (!otmp->oextra)
otmp->oextra = newoextra();
if (!OMONST(otmp)) {
struct monst *m = newmonst();
*m = cg.zeromonst;
OMONST(otmp) = m;
}
}
void
free_omonst(struct obj *otmp)
{
if (otmp->oextra) {
struct monst *m = OMONST(otmp);
if (m) {
if (m->mextra)
dealloc_mextra(m);
free((genericptr_t) m);
OMONST(otmp) = (struct monst *) 0;
}
}
}
void
newomid(struct obj *otmp)
{
if (!otmp->oextra)
otmp->oextra = newoextra();
OMID(otmp) = 0;
}
void
free_omid(struct obj *otmp)
{
OMID(otmp) = 0;
}
void
new_omailcmd(struct obj *otmp, const char *response_cmd)
{
if (!otmp->oextra)
otmp->oextra = newoextra();
if (OMAILCMD(otmp))
free_omailcmd(otmp);
OMAILCMD(otmp) = dupstr(response_cmd);
}
void
free_omailcmd(struct obj *otmp)
{
if (otmp->oextra && OMAILCMD(otmp)) {
free((genericptr_t) OMAILCMD(otmp));
OMAILCMD(otmp) = (char *) 0;
}
}
/* can object be generated eroded? */
static boolean
may_generate_eroded(struct obj *otmp)
{
/* initial hero inventory */
if (gm.moves <= 1 && !gi.in_mklev)
return FALSE;
/* already erodeproof or cannot be eroded */
if (otmp->oerodeproof || !erosion_matters(otmp) || !is_damageable(otmp))
return FALSE;
/* part of a monster's body and produced when it dies */
if (otmp->otyp == WORM_TOOTH || otmp->otyp == UNICORN_HORN)
return FALSE;
/* artifacts cannot be generated eroded */
if (otmp->oartifact)
return FALSE;
return TRUE;
}
/* random chance of applying erosions/grease to object */
static void
mkobj_erosions(struct obj *otmp)
{
if (may_generate_eroded(otmp)) {
/* A small fraction of non-artifact items will generate eroded or
* possibly erodeproof. An item that generates eroded will never be
* erodeproof, and vice versa. */
if (!rn2(100)) {
otmp->oerodeproof = 1;
} else {
if (!rn2(80) && (is_flammable(otmp) || is_rustprone(otmp))) {
do {
otmp->oeroded++;
} while (otmp->oeroded < 3 && !rn2(9));
}
if (!rn2(80) && (is_rottable(otmp) || is_corrodeable(otmp))) {
do {
otmp->oeroded2++;
} while (otmp->oeroded2 < 3 && !rn2(9));
}
}
/* and an extremely small fraction of the time, erodable items
* will generate greased */
if (!rn2(1000))
otmp->greased = 1;
}
}
struct obj *
mkobj_at(char let, coordxy x, coordxy y, boolean artif)
{
struct obj *otmp;
otmp = mkobj(let, artif);
place_object(otmp, x, y);
return otmp;
}
struct obj *
mksobj_at(int otyp, coordxy x, coordxy y, boolean init, boolean artif)
{
struct obj *otmp;
otmp = mksobj(otyp, init, artif);
place_object(otmp, x, y);
return otmp;
}
struct obj *
mksobj_migr_to_species(
int otyp,
unsigned int mflags2,
boolean init,
boolean artif)
{
struct obj *otmp;
otmp = mksobj(otyp, init, artif);
add_to_migration(otmp);
otmp->owornmask = (long) MIGR_TO_SPECIES;
otmp->migr_species = mflags2;
return otmp;
}
/* mkobj(): select a type of item from a class, use mksobj() to create it;
result is always non-Null */
struct obj *
mkobj(int oclass, boolean artif)
{
int tprob, i, prob;
if (oclass == RANDOM_CLASS) {
const struct icp *iprobs = Is_rogue_level(&u.uz)
? (const struct icp *) rogueprobs
: Inhell ? (const struct icp *) hellprobs
: (const struct icp *) mkobjprobs;
for (tprob = rnd(100); (tprob -= iprobs->iprob) > 0; iprobs++)
continue;
oclass = iprobs->iclass;
}
if (oclass == SPBOOK_no_NOVEL) {
i = rnd_class(gb.bases[SPBOOK_CLASS], SPE_BLANK_PAPER);
oclass = SPBOOK_CLASS; /* for sanity check below */
} else {
prob = rnd(go.oclass_prob_totals[oclass]);
i = gb.bases[oclass];
while ((prob -= objects[i].oc_prob) > 0)
++i;
}
if (objects[i].oc_class != oclass || !OBJ_NAME(objects[i])) {
impossible("probtype error, oclass=%d i=%d", (int) oclass, i);
i = gb.bases[oclass];
}
return mksobj(i, TRUE, artif);
}
static void
mkbox_cnts(struct obj *box)
{
register int n;
register struct obj *otmp;
box->cobj = (struct obj *) 0;
switch (box->otyp) {
case ICE_BOX:
n = 20;
break;
case CHEST:
n = box->olocked ? 7 : 5;
break;
case LARGE_BOX:
n = box->olocked ? 5 : 3;
break;
case SACK:
case OILSKIN_SACK:
/* initial inventory: sack starts out empty */
if (gm.moves <= 1 && !gi.in_mklev) {
n = 0;
break;
}
/*FALLTHRU*/
case BAG_OF_HOLDING:
n = 1;
break;
default:
n = 0;
break;
}
for (n = rn2(n + 1); n > 0; n--) {
if (box->otyp == ICE_BOX) {
otmp = mksobj(CORPSE, TRUE, FALSE);
/* Note: setting age to 0 is correct. Age has a different
* from usual meaning for objects stored in ice boxes. -KAA
*/
otmp->age = 0L;
if (otmp->timed) {
(void) stop_timer(ROT_CORPSE, obj_to_any(otmp));
(void) stop_timer(REVIVE_MON, obj_to_any(otmp));
(void) stop_timer(SHRINK_GLOB, obj_to_any(otmp));
}
} else {
register int tprob;
const struct icp *iprobs = boxiprobs;
for (tprob = rnd(100); (tprob -= iprobs->iprob) > 0; iprobs++)
;
if (!(otmp = mkobj(iprobs->iclass, FALSE)))
continue;
/* handle a couple of special cases */
if (otmp->oclass == COIN_CLASS) {
/* 2.5 x level's usual amount; weight adjusted below */
otmp->quan = (long) (rnd(level_difficulty() + 2) * rnd(75));
otmp->owt = weight(otmp);
} else
while (otmp->otyp == ROCK) {
otmp->otyp = rnd_class(DILITHIUM_CRYSTAL, LOADSTONE);
if (otmp->quan > 2L)
otmp->quan = 1L;
otmp->owt = weight(otmp);
}
if (box->otyp == BAG_OF_HOLDING) {
if (Is_mbag(otmp)) {
otmp->otyp = SACK;
otmp->spe = 0;
otmp->owt = weight(otmp);
} else
while (otmp->otyp == WAN_CANCELLATION)
otmp->otyp = rnd_class(WAN_LIGHT, WAN_LIGHTNING);
}
}
(void) add_to_container(box, otmp);
}
}
/* select a random, common monster type */
int
rndmonnum(void)
{
return rndmonnum_adj(0, 0);
}
/* select a random, common monster type, with adjusted difficulty */
int
rndmonnum_adj(int minadj, int maxadj)
{
register struct permonst *ptr;
register int i;
unsigned short excludeflags;
/* Plan A: get a level-appropriate common monster */
ptr = rndmonst_adj(minadj, maxadj);
if (ptr)
return monsndx(ptr);
/* Plan B: get any common monster */
excludeflags = G_UNIQ | G_NOGEN | (Inhell ? G_NOHELL : G_HELL);
do {
i = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
ptr = &mons[i];
} while ((ptr->geno & excludeflags) != 0);
return i;
}
void
copy_oextra(struct obj *obj2, struct obj *obj1)
{
if (!obj2 || !obj1 || !obj1->oextra)
return;
if (!obj2->oextra)
obj2->oextra = newoextra();
if (has_oname(obj1))
oname(obj2, ONAME(obj1), ONAME_SKIP_INVUPD);
if (has_omonst(obj1)) {
if (!OMONST(obj2))
newomonst(obj2);
(void) memcpy((genericptr_t) OMONST(obj2),
(genericptr_t) OMONST(obj1), sizeof (struct monst));
OMONST(obj2)->mextra = (struct mextra *) 0;
OMONST(obj2)->nmon = (struct monst *) 0;
#if 0
OMONST(obj2)->m_id = next_ident();
#endif
if (OMONST(obj1)->mextra)
copy_mextra(OMONST(obj2), OMONST(obj1));
}
if (has_omailcmd(obj1)) {
new_omailcmd(obj2, OMAILCMD(obj1));
}
if (has_omid(obj1)) {
if (!OMID(obj2))
newomid(obj2);
OMID(obj2) = OMID(obj1);
}
}
/*
* Split obj so that it gets size gets reduced by num. The quantity num is
* put in the object structure delivered by this call. The returned object
* has its wornmask cleared and is positioned just following the original
* in the nobj chain (and nexthere chain when on the floor).
*/
struct obj *
splitobj(struct obj *obj, long num)
{
struct obj *otmp;
if (obj->cobj || num <= 0L || obj->quan <= num)
panic("splitobj"); /* can't split containers */
otmp = newobj();
*otmp = *obj; /* copies whole structure */
otmp->oextra = (struct oextra *) 0;
otmp->o_id = nextoid(obj, otmp);
otmp->timed = 0; /* not timed, yet */
otmp->lamplit = 0; /* ditto */
otmp->owornmask = 0L; /* new object isn't worn */
obj->quan -= num;
obj->owt = weight(obj);
otmp->quan = num;
otmp->owt = weight(otmp); /* -= obj->owt ? */
otmp->lua_ref_cnt = 0;
otmp->pickup_prev = 0;
gc.context.objsplit.parent_oid = obj->o_id;
gc.context.objsplit.child_oid = otmp->o_id;
obj->nobj = otmp;
/* Only set nexthere when on the floor; nexthere is also used
as a back pointer to the container object when contained.
For either case, otmp's nexthere pointer is already pointing
at the right thing. */
if (obj->where == OBJ_FLOOR)
obj->nexthere = otmp; /* insert into chain: obj -> otmp -> next */
/* lua isn't tracking the split off portion even if it happens to
be tracking the original */
if (otmp->where == OBJ_LUAFREE)
otmp->where = OBJ_FREE;
if (obj->unpaid)
splitbill(obj, otmp);
copy_oextra(otmp, obj);
if (has_omid(otmp))
free_omid(otmp); /* only one association with m_id */
if (obj->timed)
obj_split_timers(obj, otmp);
if (obj_sheds_light(obj))
obj_split_light_source(obj, otmp);
return otmp;
}
/* return the value of context.ident and then increment it to be ready for
its next use; used to be simple += 1 so that every value from 1 to N got
used but now has a random increase that skips half of potential values */
unsigned
next_ident(void)
{
unsigned res = gc.context.ident;
/* +rnd(2): originally just +1; changed to rnd() to avoid potential
exploit of player using #adjust to split an object stack in a manner
that makes most recent ident%2 known; since #adjust takes no time,
no intervening activity like random creation of a new monster will
take place before next user command; with former +1, o_id%2 of the
next object to be created was knowable and player could make a wish
under controlled circumstances for an item that is affected by the
low bits of its obj->o_id [particularly helm of opposite alignment] */
gc.context.ident += rnd(2); /* ready for next new object or monster */
/* if ident has wrapped to 0, force it to be non-zero; if/when it
ever wraps past 0 (unlikely, but possible on a configuration which
uses 16-bit 'int'), just live with that and hope no o_id conflicts
between objects or m_id conflicts between monsters arise */
if (!gc.context.ident)
gc.context.ident = rnd(2);
return res;
}
/* when splitting a stack that has o_id-based shop prices, pick an
o_id value for the new stack that will maintain the same price */
static unsigned
nextoid(struct obj *oldobj, struct obj *newobj)
{
int olddif, newdif, trylimit = 256; /* limit of 4 suffices at present */
unsigned oid = gc.context.ident - 1; /* loop increment will reverse -1 */
olddif = oid_price_adjustment(oldobj, oldobj->o_id);
do {
++oid;
if (!oid) /* avoid using 0 (in case value wrapped) */
++oid;
newdif = oid_price_adjustment(newobj, oid);
} while (newdif != olddif && --trylimit >= 0);
gc.context.ident = oid; /* update 'last ident used' */
(void) next_ident(); /* increment context.ident for next use */
return oid; /* caller will use this ident */
}
/* try to find the stack obj was split from, then merge them back together;
returns the combined object if unsplit is successful, null otherwise */
struct obj *
unsplitobj(struct obj *obj)
{
unsigned target_oid = 0;
struct obj *oparent = 0, *ochild = 0, *list = 0;
/*
* We don't operate on floor objects (we're following o->nobj rather
* than o->nexthere), on free objects (don't know which list to use when
* looking for obj's parent or child), on bill objects (too complicated,
* not needed), or on buried or migrating objects (not needed).
* [This could be improved, but at present additional generality isn't
* necessary.]
*/
switch (obj->where) {
case OBJ_FREE:
case OBJ_FLOOR:
case OBJ_ONBILL:
case OBJ_MIGRATING:
case OBJ_BURIED:
default:
return (struct obj *) 0;
case OBJ_INVENT:
list = gi.invent;
break;
case OBJ_MINVENT:
list = obj->ocarry->minvent;
break;
case OBJ_CONTAINED:
list = obj->ocontainer->cobj;
break;
}
/* first try the expected case; obj is split from another stack */
if (obj->o_id == gc.context.objsplit.child_oid) {
/* parent probably precedes child and will require list traversal */
ochild = obj;
target_oid = gc.context.objsplit.parent_oid;
if (obj->nobj && obj->nobj->o_id == target_oid)
oparent = obj->nobj;
} else if (obj->o_id == gc.context.objsplit.parent_oid) {
/* alternate scenario: another stack was split from obj;
child probably follows parent and will be found here */
oparent = obj;
target_oid = gc.context.objsplit.child_oid;
if (obj->nobj && obj->nobj->o_id == target_oid)
ochild = obj->nobj;
}
/* if we have only half the split, scan obj's list to find other half */
if (ochild && !oparent) {
/* expected case */
for (obj = list; obj; obj = obj->nobj)
if (obj->o_id == target_oid) {
oparent = obj;
break;
}
} else if (oparent && !ochild) {
/* alternate scenario */
for (obj = list; obj; obj = obj->nobj)
if (obj->o_id == target_oid) {
ochild = obj;
break;
}
}
/* if we have both parent and child, try to merge them;
if successful, return the combined stack, otherwise return null */
return (oparent && ochild && merged(&oparent, &ochild)) ? oparent : 0;
}
/* reset splitobj()/unsplitobj() context */
void
clear_splitobjs(void)
{
gc.context.objsplit.parent_oid = gc.context.objsplit.child_oid = 0;
}
/*
* Insert otmp right after obj in whatever chain(s) it is on. Then extract
* obj from the chain(s). This function does a literal swap. It is up to
* the caller to provide a valid context for the swap. When done, obj will
* still exist, but not on any chain.
*
* Note: Don't use use obj_extract_self() -- we are doing an in-place swap,
* not actually moving something.
*/
void
replace_object(struct obj *obj, struct obj *otmp)
{
otmp->where = obj->where;
switch (obj->where) {
case OBJ_FREE:
/* do nothing */
break;
case OBJ_INVENT:
otmp->nobj = obj->nobj;
obj->nobj = otmp;
extract_nobj(obj, &gi.invent);
break;
case OBJ_CONTAINED:
otmp->nobj = obj->nobj;
otmp->ocontainer = obj->ocontainer;
obj->nobj = otmp;
extract_nobj(obj, &obj->ocontainer->cobj);
break;
case OBJ_MINVENT:
otmp->nobj = obj->nobj;
otmp->ocarry = obj->ocarry;
obj->nobj = otmp;
extract_nobj(obj, &obj->ocarry->minvent);
break;
case OBJ_FLOOR:
otmp->nobj = obj->nobj;
otmp->nexthere = obj->nexthere;
otmp->ox = obj->ox;
otmp->oy = obj->oy;
obj->nobj = otmp;
obj->nexthere = otmp;
extract_nobj(obj, &fobj);
extract_nexthere(obj, &gl.level.objects[obj->ox][obj->oy]);
break;
default:
panic("replace_object: obj position");
break;
}
}
/* is 'obj' inside a container whose contents aren't known?
if so, return the outermost container meeting that criterium */
struct obj *
unknwn_contnr_contents(struct obj *obj)
{
struct obj *result = 0, *parent;
while (obj->where == OBJ_CONTAINED) {
parent = obj->ocontainer;
if (!parent->cknown)
result = parent;
obj = parent;
}
return result;
}
/*
* Create a dummy duplicate to put on shop bill. The duplicate exists
* only in the billobjs chain. This function is used when a shop object
* is being altered, and a copy of the original is needed for billing
* purposes. For example, when eating, where an interruption will yield
* an object which is different from what it started out as; the "I x"
* command needs to display the original object.
*
* The caller is responsible for checking otmp->unpaid and
* costly_spot(u.ux, u.uy). This function will make otmp no charge.
*
* Note that check_unpaid_usage() should be used instead for partial
* usage of an object.
*/
void
bill_dummy_object(struct obj *otmp)
{
register struct obj *dummy;
long cost = 0L;
if (otmp->unpaid) {
cost = unpaid_cost(otmp, FALSE);
subfrombill(otmp, shop_keeper(*u.ushops));
}
dummy = newobj();
*dummy = *otmp;
dummy->oextra = (struct oextra *) 0;
dummy->where = OBJ_FREE;
dummy->o_id = nextoid(otmp, dummy);
dummy->timed = 0;
copy_oextra(dummy, otmp);
if (has_omid(dummy))
free_omid(dummy); /* only one association with m_id*/
if (Is_candle(dummy))
dummy->lamplit = 0;
dummy->owornmask = 0L; /* dummy object is not worn */
addtobill(dummy, FALSE, TRUE, TRUE);
if (cost)
alter_cost(dummy, -cost);
/* no_charge is only valid for some locations */
otmp->no_charge = (otmp->where == OBJ_FLOOR
|| otmp->where == OBJ_CONTAINED) ? 1 : 0;
otmp->unpaid = 0;
return;
}
/* alteration types; must match COST_xxx macros in hack.h */
static const char *const alteration_verbs[] = {
"cancel", "drain", "uncharge", "unbless", "uncurse", "disenchant",
"degrade", "dilute", "erase", "burn", "neutralize", "destroy", "splatter",
"bite", "open", "break the lock on", "rust", "rot", "tarnish"
};
/* possibly bill for an object which the player has just modified */
void
costly_alteration(struct obj *obj, int alter_type)
{
coordxy ox, oy;
char objroom;
boolean learn_bknown;
const char *those, *them;
struct monst *shkp = 0;
if (alter_type < 0 || alter_type >= SIZE(alteration_verbs)) {
impossible("invalid alteration type (%d)", alter_type);
alter_type = 0;
}
ox = oy = 0; /* lint suppression */
objroom = '\0'; /* ditto */
if (carried(obj) || obj->where == OBJ_FREE) {
/* OBJ_FREE catches obj_no_longer_held()'s transformation
of crysknife back into worm tooth; the object has been
removed from inventory but not necessarily placed at
its new location yet--the unpaid flag will still be set
if this item is owned by a shop */
if (!obj->unpaid)
return;
} else {
/* this get_obj_location shouldn't fail, but if it does,
use hero's location */
if (!get_obj_location(obj, &ox, &oy, CONTAINED_TOO))
ox = u.ux, oy = u.uy;
if (!costly_spot(ox, oy))
return;
objroom = *in_rooms(ox, oy, SHOPBASE);
/* if no shop cares about it, we're done */
if (!billable(&shkp, obj, objroom, FALSE))
return;
}
if (obj->quan == 1L)
those = "that", them = "it";
else
those = "those", them = "them";
/* when shopkeeper describes the object as being uncursed or unblessed
hero will know that it is now uncursed; will also make the feedback
from `I x' after bill_dummy_object() be more specific for this item */
learn_bknown = (alter_type == COST_UNCURS || alter_type == COST_UNBLSS);
switch (obj->where) {
case OBJ_FREE: /* obj_no_longer_held() */
case OBJ_INVENT:
if (learn_bknown)
set_bknown(obj, 1);
if (shkp) {
SetVoice(shkp, 0, 80, 0);
}
verbalize("You %s %s %s, you pay for %s!",
alteration_verbs[alter_type], those, simpleonames(obj),
them);
bill_dummy_object(obj);
break;
case OBJ_FLOOR:
if (learn_bknown)
obj->bknown = 1; /* ok to bypass set_bknown() here */
if (costly_spot(u.ux, u.uy) && objroom == *u.ushops) {
if (shkp) {
SetVoice(shkp, 0, 80, 0);
}
verbalize("You %s %s, you pay for %s!",
alteration_verbs[alter_type], those, them);
bill_dummy_object(obj);
} else {
(void) stolen_value(obj, ox, oy, FALSE, FALSE);
}
break;
}
}
static const char dknowns[] = { WAND_CLASS, RING_CLASS, POTION_CLASS,
SCROLL_CLASS, GEM_CLASS, SPBOOK_CLASS,
WEAPON_CLASS, TOOL_CLASS, VENOM_CLASS, 0 };
/* set obj->dknown to 0 for most types of objects, to 1 otherwise;
split off from unknow_object() */
void
clear_dknown(struct obj *obj)
{
obj->dknown = strchr(dknowns, obj->oclass) ? 0 : 1;
if ((obj->otyp >= ELVEN_SHIELD && obj->otyp <= ORCISH_SHIELD)
|| obj->otyp == SHIELD_OF_REFLECTION
|| objects[obj->otyp].oc_merge)
obj->dknown = 0;
/* globs always have dknown flag set (to maximize merging) but for new
object, globby flag won't be set yet so isn't available to check */
if (Is_pudding(obj))
obj->dknown = 1;
}
/* some init for a brand new object, or partial re-init when hero loses
potentially known info about an object (called when an unseen monster
picks up or uses it); moved from invent.c to here for access to dknowns */
void
unknow_object(struct obj *obj)
{
clear_dknown(obj); /* obj->dknown = 0; */
obj->bknown = obj->rknown = 0;
obj->cknown = obj->lknown = 0;
/* for an existing object, awareness of charges or enchantment has
gone poof... [object types which don't use the known flag have
it set True for some reason] */
obj->known = objects[obj->otyp].oc_uses_known ? 0 : 1;
}
/* do some initialization to a newly created object.
object otyp must be set. */
static void
mksobj_init(struct obj *otmp, boolean artif)
{
int mndx, tryct;
char let = objects[otmp->otyp].oc_class;
switch (let) {
case WEAPON_CLASS:
otmp->quan = is_multigen(otmp) ? (long) rn1(6, 6) : 1L;
if (!rn2(11)) {
otmp->spe = rne(3);
otmp->blessed = rn2(2);
} else if (!rn2(10)) {
curse(otmp);
otmp->spe = -rne(3);
} else
blessorcurse(otmp, 10);
if (is_poisonable(otmp) && !rn2(100))
otmp->opoisoned = 1;
if (artif && !rn2(20 + (10 * nartifact_exist())))
otmp = mk_artifact(otmp, (aligntyp) A_NONE);
break;
case FOOD_CLASS:
otmp->oeaten = 0;
switch (otmp->otyp) {
case CORPSE:
/* possibly overridden by mkcorpstat() */
tryct = 50;
do
otmp->corpsenm = undead_to_corpse(rndmonnum());
while ((gm.mvitals[otmp->corpsenm].mvflags & G_NOCORPSE)
&& (--tryct > 0));
if (tryct == 0) {
/* perhaps rndmonnum() only wants to make G_NOCORPSE
monsters on this gl.level; create an adventurer's
corpse instead, then */
otmp->corpsenm = PM_HUMAN;
}
/* timer set below */
break;
case EGG:
otmp->corpsenm = NON_PM; /* generic egg */
if (!rn2(3))
for (tryct = 200; tryct > 0; --tryct) {
mndx = can_be_hatched(rndmonnum());
if (mndx != NON_PM && !dead_species(mndx, TRUE)) {
otmp->corpsenm = mndx; /* typed egg */
break;
}
}
/* timer set below */
break;
case TIN:
otmp->corpsenm = NON_PM; /* empty (so far) */
if (!rn2(6))
set_tin_variety(otmp, SPINACH_TIN);
else
for (tryct = 200; tryct > 0; --tryct) {
mndx = undead_to_corpse(rndmonnum());
if (mons[mndx].cnutrit
&& !(gm.mvitals[mndx].mvflags & G_NOCORPSE)) {
otmp->corpsenm = mndx;
set_tin_variety(otmp, RANDOM_TIN);
break;
}
}
blessorcurse(otmp, 10);
break;
case SLIME_MOLD:
otmp->spe = gc.context.current_fruit;
flags.made_fruit = TRUE;
break;
case KELP_FROND:
otmp->quan = (long) rnd(2);
break;
case CANDY_BAR:
/* set otmp->spe */
assign_candy_wrapper(otmp);
break;
default:
break;
}
if (Is_pudding(otmp)) {
otmp->globby = 1;
/* for emphasis; glob quantity is always 1 and weight varies
when other globs coalesce with it or this one shrinks */
otmp->quan = 1L;
/* 3.7: globs in 3.6.x left owt as 0 and let weight() fix
that up during 'obj->owt = weight(obj)' below, but now
we initialize glob->owt explicitly so weight() doesn't
need to perform any fix up and returns glob->owt as-is */
otmp->owt = objects[otmp->otyp].oc_weight;
otmp->known = otmp->dknown = 1;
otmp->corpsenm = PM_GRAY_OOZE
+ (otmp->otyp - GLOB_OF_GRAY_OOZE);
start_glob_timeout(otmp, 0L);
} else {
if (otmp->otyp != CORPSE && otmp->otyp != MEAT_RING
&& otmp->otyp != KELP_FROND && !rn2(6)) {
otmp->quan = 2L;
}
}
break;
case GEM_CLASS:
otmp->corpsenm = 0; /* LOADSTONE hack */
if (otmp->otyp == LOADSTONE)
curse(otmp);
else if (otmp->otyp == ROCK)
otmp->quan = (long) rn1(6, 6);
else if (otmp->otyp != LUCKSTONE && !rn2(6))
otmp->quan = 2L;
else
otmp->quan = 1L;
break;
case TOOL_CLASS:
switch (otmp->otyp) {
case TALLOW_CANDLE:
case WAX_CANDLE:
otmp->spe = 1;
otmp->age = 20L * /* 400 or 200 */
(long) objects[otmp->otyp].oc_cost;
otmp->lamplit = 0;
otmp->quan = 1L + (long) (rn2(2) ? rn2(7) : 0);
blessorcurse(otmp, 5);
break;
case BRASS_LANTERN:
case OIL_LAMP:
otmp->spe = 1;
otmp->age = (long) rn1(500, 1000);
otmp->lamplit = 0;
blessorcurse(otmp, 5);
break;
case MAGIC_LAMP:
otmp->spe = 1;
otmp->lamplit = 0;
blessorcurse(otmp, 2);
break;
case CHEST:
case LARGE_BOX:
otmp->olocked = !!(rn2(5));
otmp->otrapped = !(rn2(10));
/*FALLTHRU*/
case ICE_BOX:
case SACK:
case OILSKIN_SACK:
case BAG_OF_HOLDING:
mkbox_cnts(otmp);
break;
case EXPENSIVE_CAMERA: