1 /* SCCS Id: @(#)mkobj.c 3.4 2002/10/07 */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
8 STATIC_DCL void FDECL(mkbox_cnts,(struct obj *));
9 STATIC_DCL void FDECL(obj_timer_checks,(struct obj *, XCHAR_P, XCHAR_P, int));
11 STATIC_DCL void FDECL(container_weight, (struct obj *));
12 STATIC_DCL struct obj *FDECL(save_mtraits, (struct obj *, struct monst *));
14 STATIC_DCL const char *FDECL(where_name, (int));
15 STATIC_DCL void FDECL(check_contained, (struct obj *,const char *));
19 extern struct obj *thrownobj; /* defined in dothrow.c */
21 /*#define DEBUG_EFFECTS*/ /* show some messages for debugging */
24 int iprob; /* probability of an item type */
25 char iclass; /* item class */
30 const struct icp mkobjprobs[] = {
44 const struct icp boxiprobs[] = {
57 const struct icp rogueprobs[] = {
68 const struct icp hellprobs[] = {
82 mkobj_at(let, x, y, artif)
89 otmp = mkobj(let, artif);
90 place_object(otmp, x, y);
95 mksobj_at(otyp, x, y, init, artif)
101 otmp = mksobj(otyp, init, artif);
102 place_object(otmp, x, y);
111 int tprob, i, prob = rnd(1000);
113 if(oclass == RANDOM_CLASS) {
114 const struct icp *iprobs =
116 (Is_rogue_level(&u.uz)) ?
117 (const struct icp *)rogueprobs :
119 Inhell ? (const struct icp *)hellprobs :
120 (const struct icp *)mkobjprobs;
122 for(tprob = rnd(100);
123 (tprob -= iprobs->iprob) > 0;
125 oclass = iprobs->iclass;
128 i = bases[(int)oclass];
129 while((prob -= objects[i].oc_prob) > 0) i++;
131 if(objects[i].oc_class != oclass || !OBJ_NAME(objects[i]))
132 panic("probtype error, oclass=%d i=%d", (int) oclass, i);
134 return(mksobj(i, TRUE, artif));
142 register struct obj *otmp;
144 box->cobj = (struct obj *) 0;
147 case ICE_BOX: n = 20; break;
148 case CHEST: n = 5; break;
149 case LARGE_BOX: n = 3; break;
152 /* initial inventory: sack starts out empty */
153 if (moves <= 1 && !in_mklev) { n = 0; break; }
155 case BAG_OF_HOLDING: n = 1; break;
156 default: n = 0; break;
159 for (n = rn2(n+1); n > 0; n--) {
160 if (box->otyp == ICE_BOX) {
161 if (!(otmp = mksobj(CORPSE, TRUE, TRUE))) continue;
162 /* Note: setting age to 0 is correct. Age has a different
163 * from usual meaning for objects stored in ice boxes. -KAA
167 (void) stop_timer(ROT_CORPSE, (genericptr_t)otmp);
168 (void) stop_timer(REVIVE_MON, (genericptr_t)otmp);
172 const struct icp *iprobs = boxiprobs;
174 for (tprob = rnd(100); (tprob -= iprobs->iprob) > 0; iprobs++)
176 if (!(otmp = mkobj(iprobs->iclass, TRUE))) continue;
178 /* handle a couple of special cases */
179 if (otmp->oclass == COIN_CLASS) {
180 /* 2.5 x level's usual amount; weight adjusted below */
181 otmp->quan = (long)(rnd(level_difficulty()+2) * rnd(75));
182 otmp->owt = weight(otmp);
183 } else while (otmp->otyp == ROCK) {
184 otmp->otyp = rnd_class(DILITHIUM_CRYSTAL, LOADSTONE);
185 if (otmp->quan > 2L) otmp->quan = 1L;
186 otmp->owt = weight(otmp);
188 if (box->otyp == BAG_OF_HOLDING) {
192 otmp->owt = weight(otmp);
193 } else while (otmp->otyp == WAN_CANCELLATION)
194 otmp->otyp = rnd_class(WAN_LIGHT, WAN_LIGHTNING);
197 (void) add_to_container(box, otmp);
202 rndmonnum() /* select a random, common monster type */
204 register struct permonst *ptr;
207 /* Plan A: get a level-appropriate common monster */
209 if (ptr) return(monsndx(ptr));
211 /* Plan B: get any common monster */
213 i = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
215 } while((ptr->geno & G_NOGEN) || (!Inhell && (ptr->geno & G_HELL)));
221 * Split obj so that it gets size gets reduced by num. The quantity num is
222 * put in the object structure delivered by this call. The returned object
223 * has its wornmask cleared and is positioned just following the original
224 * in the nobj chain (and nexthere chain when on the floor).
233 if (obj->cobj || num <= 0L || obj->quan <= num)
234 panic("splitobj"); /* can't split containers */
235 otmp = newobj(obj->oxlth + obj->onamelth);
236 *otmp = *obj; /* copies whole structure */
237 otmp->o_id = flags.ident++;
238 if (!otmp->o_id) otmp->o_id = flags.ident++; /* ident overflowed */
239 otmp->timed = 0; /* not timed, yet */
240 otmp->lamplit = 0; /* ditto */
241 otmp->owornmask = 0L; /* new object isn't worn */
243 obj->owt = weight(obj);
245 otmp->owt = weight(otmp); /* -= obj->owt ? */
247 /* Only set nexthere when on the floor, nexthere is also used */
248 /* as a back pointer to the container object when contained. */
249 if (obj->where == OBJ_FLOOR)
250 obj->nexthere = otmp;
252 (void)memcpy((genericptr_t)otmp->oextra, (genericptr_t)obj->oextra,
255 (void)strncpy(ONAME(otmp), ONAME(obj), (int)obj->onamelth);
256 if (obj->unpaid) splitbill(obj,otmp);
257 if (obj->timed) obj_split_timers(obj, otmp);
258 if (obj_sheds_light(obj)) obj_split_light_source(obj, otmp);
263 * Insert otmp right after obj in whatever chain(s) it is on. Then extract
264 * obj from the chain(s). This function does a literal swap. It is up to
265 * the caller to provide a valid context for the swap. When done, obj will
266 * still exist, but not on any chain.
268 * Note: Don't use use obj_extract_self() -- we are doing an in-place swap,
269 * not actually moving something.
272 replace_object(obj, otmp)
276 otmp->where = obj->where;
277 switch (obj->where) {
282 otmp->nobj = obj->nobj;
284 extract_nobj(obj, &invent);
287 otmp->nobj = obj->nobj;
288 otmp->ocontainer = obj->ocontainer;
290 extract_nobj(obj, &obj->ocontainer->cobj);
293 otmp->nobj = obj->nobj;
294 otmp->ocarry = obj->ocarry;
296 extract_nobj(obj, &obj->ocarry->minvent);
299 otmp->nobj = obj->nobj;
300 otmp->nexthere = obj->nexthere;
304 obj->nexthere = otmp;
305 extract_nobj(obj, &fobj);
306 extract_nexthere(obj, &level.objects[obj->ox][obj->oy]);
309 panic("replace_object: obj position");
315 * Create a dummy duplicate to put on shop bill. The duplicate exists
316 * only in the billobjs chain. This function is used when a shop object
317 * is being altered, and a copy of the original is needed for billing
318 * purposes. For example, when eating, where an interruption will yield
319 * an object which is different from what it started out as; the "I x"
320 * command needs to display the original object.
322 * The caller is responsible for checking otmp->unpaid and
323 * costly_spot(u.ux, u.uy). This function will make otmp no charge.
325 * Note that check_unpaid_usage() should be used instead for partial
326 * usage of an object.
329 bill_dummy_object(otmp)
330 register struct obj *otmp;
332 register struct obj *dummy;
335 subfrombill(otmp, shop_keeper(*u.ushops));
336 dummy = newobj(otmp->oxlth + otmp->onamelth);
338 dummy->where = OBJ_FREE;
339 dummy->o_id = flags.ident++;
340 if (!dummy->o_id) dummy->o_id = flags.ident++; /* ident overflowed */
343 (void)memcpy((genericptr_t)dummy->oextra,
344 (genericptr_t)otmp->oextra, otmp->oxlth);
346 (void)strncpy(ONAME(dummy), ONAME(otmp), (int)otmp->onamelth);
347 if (Is_candle(dummy)) dummy->lamplit = 0;
348 addtobill(dummy, FALSE, TRUE, TRUE);
357 static const char dknowns[] = {
358 WAND_CLASS, RING_CLASS, POTION_CLASS, SCROLL_CLASS,
359 GEM_CLASS, SPBOOK_CLASS, WEAPON_CLASS, TOOL_CLASS, 0
363 mksobj(otyp, init, artif)
370 char let = objects[otyp].oc_class;
374 otmp->age = monstermoves;
375 otmp->o_id = flags.ident++;
376 if (!otmp->o_id) otmp->o_id = flags.ident++; /* ident overflowed */
380 otmp->where = OBJ_FREE;
381 otmp->dknown = index(dknowns, let) ? 0 : 1;
382 if ((otmp->otyp >= ELVEN_SHIELD && otmp->otyp <= ORCISH_SHIELD) ||
383 otmp->otyp == SHIELD_OF_REFLECTION)
385 if (!objects[otmp->otyp].oc_uses_known)
387 #ifdef INVISIBLE_OBJECTS
388 otmp->oinvis = !rn2(1250);
390 if (init) switch (let) {
392 otmp->quan = is_multigen(otmp) ? (long) rn1(6,6) : 1L;
395 otmp->blessed = rn2(2);
396 } else if(!rn2(10)) {
399 } else blessorcurse(otmp, 10);
400 if (is_poisonable(otmp) && !rn2(100))
403 if (artif && !rn2(20))
404 otmp = mk_artifact(otmp, (aligntyp)A_NONE);
410 /* possibly overridden by mkcorpstat() */
412 do otmp->corpsenm = undead_to_corpse(rndmonnum());
413 while ((mvitals[otmp->corpsenm].mvflags & G_NOCORPSE) && (--tryct > 0));
415 /* perhaps rndmonnum() only wants to make G_NOCORPSE monsters on
416 this level; let's create an adventurer's corpse instead, then */
417 otmp->corpsenm = PM_HUMAN;
419 /* timer set below */
422 otmp->corpsenm = NON_PM; /* generic egg */
423 if (!rn2(3)) for (tryct = 200; tryct > 0; --tryct) {
424 mndx = can_be_hatched(rndmonnum());
425 if (mndx != NON_PM && !dead_species(mndx, TRUE)) {
426 otmp->corpsenm = mndx; /* typed egg */
427 attach_egg_hatch_timeout(otmp);
433 otmp->corpsenm = NON_PM; /* empty (so far) */
435 otmp->spe = 1; /* spinach */
436 else for (tryct = 200; tryct > 0; --tryct) {
437 mndx = undead_to_corpse(rndmonnum());
438 if (mons[mndx].cnutrit &&
439 !(mvitals[mndx].mvflags & G_NOCORPSE)) {
440 otmp->corpsenm = mndx;
444 blessorcurse(otmp, 10);
447 otmp->spe = current_fruit;
450 otmp->quan = (long) rnd(2);
453 if (otmp->otyp == CORPSE || otmp->otyp == MEAT_RING ||
454 otmp->otyp == KELP_FROND) break;
455 /* fall into next case */
458 if (otmp->otyp == LOADSTONE) curse(otmp);
459 else if (otmp->otyp == ROCK) otmp->quan = (long) rn1(6,6);
460 else if (otmp->otyp != LUCKSTONE && !rn2(6)) otmp->quan = 2L;
461 else otmp->quan = 1L;
466 case WAX_CANDLE: otmp->spe = 1;
467 otmp->age = 20L * /* 400 or 200 */
468 (long)objects[otmp->otyp].oc_cost;
471 (long)(rn2(2) ? rn2(7) : 0);
472 blessorcurse(otmp, 5);
475 case OIL_LAMP: otmp->spe = 1;
476 otmp->age = (long) rn1(500,1000);
478 blessorcurse(otmp, 5);
480 case MAGIC_LAMP: otmp->spe = 1;
482 blessorcurse(otmp, 2);
485 case LARGE_BOX: otmp->olocked = !!(rn2(5));
486 otmp->otrapped = !(rn2(10));
490 case BAG_OF_HOLDING: mkbox_cnts(otmp);
493 case EXPENSIVE_CAMERA:
496 case MAGIC_MARKER: otmp->spe = rn1(70,30);
498 case CAN_OF_GREASE: otmp->spe = rnd(25);
499 blessorcurse(otmp, 10);
501 case CRYSTAL_BALL: otmp->spe = rnd(5);
502 blessorcurse(otmp, 2);
505 case BAG_OF_TRICKS: otmp->spe = rnd(20);
507 case FIGURINE: { int tryct2 = 0;
509 otmp->corpsenm = rndmonnum();
510 while(is_human(&mons[otmp->corpsenm])
512 blessorcurse(otmp, 4);
515 case BELL_OF_OPENING: otmp->spe = 3;
521 case DRUM_OF_EARTHQUAKE:
522 otmp->spe = rn1(5,4);
527 if (otmp->otyp == AMULET_OF_YENDOR) flags.made_amulet = TRUE;
528 if(rn2(10) && (otmp->otyp == AMULET_OF_STRANGULATION ||
529 otmp->otyp == AMULET_OF_CHANGE ||
530 otmp->otyp == AMULET_OF_RESTFUL_SLEEP)) {
532 } else blessorcurse(otmp, 10);
538 if (otmp->otyp == POT_OIL)
539 otmp->age = MAX_OIL_IN_FLASK; /* amount of oil */
543 if (otmp->otyp != SCR_MAIL)
545 blessorcurse(otmp, 4);
548 blessorcurse(otmp, 17);
551 if(rn2(10) && (otmp->otyp == FUMBLE_BOOTS ||
552 otmp->otyp == LEVITATION_BOOTS ||
553 otmp->otyp == HELM_OF_OPPOSITE_ALIGNMENT ||
554 otmp->otyp == GAUNTLETS_OF_FUMBLING ||
558 } else if(!rn2(10)) {
559 otmp->blessed = rn2(2);
561 } else blessorcurse(otmp, 10);
562 if (artif && !rn2(40))
563 otmp = mk_artifact(otmp, (aligntyp)A_NONE);
564 /* simulate lacquered armor for samurai */
565 if (Role_if(PM_SAMURAI) && otmp->otyp == SPLINT_MAIL &&
566 (moves <= 1 || In_quest(&u.uz))) {
568 /* optimizer bitfield bug */
569 otmp->oerodeproof = 1;
572 otmp->oerodeproof = otmp->rknown = 1;
577 if(otmp->otyp == WAN_WISHING) otmp->spe = rnd(3); else
579 (objects[otmp->otyp].oc_dir == NODIR) ? 11 : 4);
580 blessorcurse(otmp, 17);
581 otmp->recharged = 0; /* used to control recharging */
584 if(objects[otmp->otyp].oc_charged) {
585 blessorcurse(otmp, 3);
587 if(rn2(10) && bcsign(otmp))
588 otmp->spe = bcsign(otmp) * rne(3);
589 else otmp->spe = rn2(2) ? rne(3) : -rne(3);
591 /* make useless +0 rings much less common */
592 if (otmp->spe == 0) otmp->spe = rn2(4) - rn2(3);
593 /* negative rings are usually cursed */
594 if (otmp->spe < 0 && rn2(5)) curse(otmp);
595 } else if(rn2(10) && (otmp->otyp == RIN_TELEPORTATION ||
596 otmp->otyp == RIN_POLYMORPH ||
597 otmp->otyp == RIN_AGGRAVATE_MONSTER ||
598 otmp->otyp == RIN_HUNGER || !rn2(9))) {
603 switch (otmp->otyp) {
605 /* possibly overridden by mkcorpstat() */
606 otmp->corpsenm = rndmonnum();
607 if (!verysmall(&mons[otmp->corpsenm]) &&
608 rn2(level_difficulty()/2 + 10) > 10)
609 (void) add_to_container(otmp,
610 mkobj(SPBOOK_CLASS,FALSE));
614 break; /* do nothing */
616 impossible("impossible mkobj %d, sym '%c'.", otmp->otyp,
617 objects[otmp->otyp].oc_class);
618 return (struct obj *)0;
621 /* Some things must get done (timers) even if init = 0 */
622 switch (otmp->otyp) {
624 start_corpse_timeout(otmp);
628 /* unique objects may have an associated artifact entry */
629 if (objects[otyp].oc_unique && !otmp->oartifact)
630 otmp = mk_artifact(otmp, (aligntyp)A_NONE);
631 otmp->owt = weight(otmp);
636 * Start a corpse decay or revive timer.
637 * This takes the age of the corpse into consideration as of 3.4.0.
640 start_corpse_timeout(body)
643 long when; /* rot away when this old */
644 long corpse_age; /* age of corpse */
648 #define TAINT_AGE (50L) /* age when corpses go bad */
649 #define TROLL_REVIVE_CHANCE 37 /* 1/37 chance for 50 turns ~ 75% chance */
650 #define ROT_AGE (250L) /* age when corpses rot away */
652 /* lizards and lichen don't rot or revive */
653 if (body->corpsenm == PM_LIZARD || body->corpsenm == PM_LICHEN) return;
655 action = ROT_CORPSE; /* default action: rot away */
656 rot_adjust = in_mklev ? 25 : 10; /* give some variation */
657 corpse_age = monstermoves - body->age;
658 if (corpse_age > ROT_AGE)
661 when = ROT_AGE - corpse_age;
662 when += (long)(rnz(rot_adjust) - rot_adjust);
664 if (is_rider(&mons[body->corpsenm])) {
666 * Riders always revive. They have a 1/3 chance per turn
667 * of reviving after 12 turns. Always revive by 500.
670 for (when = 12L; when < 500L; when++)
673 } else if (mons[body->corpsenm].mlet == S_TROLL && !body->norevive) {
675 for (age = 2; age <= TAINT_AGE; age++)
676 if (!rn2(TROLL_REVIVE_CHANCE)) { /* troll revives */
683 if (body->norevive) body->norevive = 0;
684 (void) start_timer(when, TIMER_OBJECT, action, (genericptr_t)body);
689 register struct obj *otmp;
692 if (otmp->oclass == COIN_CLASS) return;
696 if (carried(otmp) && confers_luck(otmp))
698 else if (otmp->otyp == BAG_OF_HOLDING)
699 otmp->owt = weight(otmp);
700 else if (otmp->otyp == FIGURINE && otmp->timed)
701 (void) stop_timer(FIG_TRANSFORM, (genericptr_t) otmp);
707 register struct obj *otmp;
710 if (carried(otmp) && confers_luck(otmp))
712 else if (otmp->otyp == BAG_OF_HOLDING)
713 otmp->owt = weight(otmp);
718 register struct obj *otmp;
721 if (otmp->oclass == COIN_CLASS) return;
725 /* welded two-handed weapon interferes with some armor removal */
726 if (otmp == uwep && bimanual(uwep)) reset_remarm();
727 /* rules at top of wield.c state that twoweapon cannot be done
728 with cursed alternate weapon */
729 if (otmp == uswapwep && u.twoweap)
731 /* some cursed items need immediate updating */
732 if (carried(otmp) && confers_luck(otmp))
734 else if (otmp->otyp == BAG_OF_HOLDING)
735 otmp->owt = weight(otmp);
736 else if (otmp->otyp == FIGURINE) {
737 if (otmp->corpsenm != NON_PM
738 && !dead_species(otmp->corpsenm,TRUE)
739 && (carried(otmp) || mcarried(otmp)))
740 attach_fig_transform_timeout(otmp);
747 register struct obj *otmp;
750 if (carried(otmp) && confers_luck(otmp))
752 else if (otmp->otyp == BAG_OF_HOLDING)
753 otmp->owt = weight(otmp);
754 else if (otmp->otyp == FIGURINE && otmp->timed)
755 (void) stop_timer(FIG_TRANSFORM, (genericptr_t) otmp);
763 blessorcurse(otmp, chance)
764 register struct obj *otmp;
767 if(otmp->blessed || otmp->cursed) return;
784 register struct obj *otmp;
786 return(!!otmp->blessed - !!otmp->cursed);
793 * Calculate the weight of the given object. This will recursively follow
794 * and calculate the weight of any containers.
796 * Note: It is possible to end up with an incorrect weight if some part
797 * of the code messes with a contained object and doesn't update the
798 * container's weight.
802 register struct obj *obj;
804 int wt = objects[obj->otyp].oc_weight;
806 if (obj->otyp == LARGE_BOX && obj->spe == 1) /* Schroedinger's Cat */
807 wt += mons[PM_HOUSECAT].cwt;
808 if (Is_container(obj) || obj->otyp == STATUE) {
809 struct obj *contents;
810 register int cwt = 0;
812 if (obj->otyp == STATUE && obj->corpsenm >= LOW_PM)
813 wt = (int)obj->quan *
814 ((int)mons[obj->corpsenm].cwt * 3 / 2);
816 for(contents=obj->cobj; contents; contents=contents->nobj)
817 cwt += weight(contents);
819 * The weight of bags of holding is calculated as the weight
820 * of the bag plus the weight of the bag's contents modified
823 * Bag status Weight of contents
824 * ---------- ------------------
829 * The macro DELTA_CWT in pickup.c also implements these
832 * Note: The above checks are performed in the given order.
833 * this means that if an object is both blessed and
834 * cursed (not supposed to happen), it will be treated
837 if (obj->otyp == BAG_OF_HOLDING)
838 cwt = obj->cursed ? (cwt * 2) :
839 (1 + (cwt / (obj->blessed ? 4 : 2)));
843 if (obj->otyp == CORPSE && obj->corpsenm >= LOW_PM) {
844 long long_wt = obj->quan * (long) mons[obj->corpsenm].cwt;
846 wt = (long_wt > LARGEST_INT) ? LARGEST_INT : (int)long_wt;
847 if (obj->oeaten) wt = eaten_stat(wt, obj);
849 } else if (obj->oclass == FOOD_CLASS && obj->oeaten) {
850 return eaten_stat((int)obj->quan * wt, obj);
851 } else if (obj->oclass == COIN_CLASS)
852 return (int)((obj->quan + 50L) / 100L);
853 else if (obj->otyp == HEAVY_IRON_BALL && obj->owt != 0)
854 return((int)(obj->owt)); /* kludge for "very" heavy iron ball */
855 return(wt ? wt*(int)obj->quan : ((int)obj->quan + 1)>>1);
858 static int treefruits[] = {APPLE,ORANGE,PEAR,BANANA,EUCALYPTUS_LEAF};
861 rnd_treefruit_at(x,y)
864 return mksobj_at(treefruits[rn2(SIZE(treefruits))], x, y, TRUE, FALSE);
874 register struct obj *gold = g_at(x,y);
877 amount = (long)(1 + rnd(level_difficulty()+2) * rnd(30));
879 gold->quan += amount;
881 gold = mksobj_at(GOLD_PIECE, x, y, TRUE, FALSE);
884 gold->owt = weight(gold);
891 /* return TRUE if the corpse has special timing */
892 #define special_corpse(num) (((num) == PM_LIZARD) \
893 || ((num) == PM_LICHEN) \
894 || (is_rider(&mons[num])) \
895 || (mons[num].mlet == S_TROLL))
898 * OEXTRA note: Passing mtmp causes mtraits to be saved
899 * even if ptr passed as well, but ptr is always used for
900 * the corpse type (corpsenm). That allows the corpse type
901 * to be different from the original monster,
902 * i.e. vampire -> human corpse
903 * yet still allow restoration of the original monster upon
907 mkcorpstat(objtype, mtmp, ptr, x, y, init)
908 int objtype; /* CORPSE or STATUE */
910 struct permonst *ptr;
914 register struct obj *otmp;
916 if (objtype != CORPSE && objtype != STATUE)
917 impossible("making corpstat type %d", objtype);
918 if (x == 0 && y == 0) { /* special case - random placement */
919 otmp = mksobj(objtype, init, FALSE);
920 if (otmp) rloco(otmp);
922 otmp = mksobj_at(objtype, x, y, init, FALSE);
927 if (!ptr) ptr = mtmp->data;
928 /* save_mtraits frees original data pointed to by otmp */
929 otmp2 = save_mtraits(otmp, mtmp);
930 if (otmp2) otmp = otmp2;
932 /* use the corpse or statue produced by mksobj() as-is
933 unless `ptr' is non-null */
935 int old_corpsenm = otmp->corpsenm;
937 otmp->corpsenm = monsndx(ptr);
938 otmp->owt = weight(otmp);
939 if (otmp->otyp == CORPSE &&
940 (special_corpse(old_corpsenm) ||
941 special_corpse(otmp->corpsenm))) {
942 obj_stop_timers(otmp);
943 start_corpse_timeout(otmp);
951 * Attach a monster id to an object, to provide
952 * a lasting association between the two.
955 obj_attach_mid(obj, mid)
962 if (!mid || !obj) return (struct obj *)0;
964 namelth = obj->onamelth ? strlen(ONAME(obj)) + 1 : 0;
966 otmp = realloc_obj(obj, lth, (genericptr_t) &mid, namelth, ONAME(obj));
969 otmp->oxlth = sizeof(mid);
970 (void) memcpy((genericptr_t)otmp->oextra, (genericptr_t)&mid,
973 if (otmp && otmp->oxlth) otmp->oattached = OATTACHED_M_ID; /* mark it */
978 save_mtraits(obj, mtmp)
985 lth = sizeof(struct monst) + mtmp->mxlth + mtmp->mnamelth;
986 namelth = obj->onamelth ? strlen(ONAME(obj)) + 1 : 0;
987 otmp = realloc_obj(obj, lth, (genericptr_t) mtmp, namelth, ONAME(obj));
988 if (otmp && otmp->oxlth) {
989 struct monst *mtmp2 = (struct monst *)otmp->oextra;
990 if (mtmp->data) mtmp2->mnum = monsndx(mtmp->data);
991 /* invalidate pointers */
992 /* m_id is needed to know if this is a revived quest leader */
993 /* but m_id must be cleared when loading bones */
994 mtmp2->nmon = (struct monst *)0;
995 mtmp2->data = (struct permonst *)0;
996 mtmp2->minvent = (struct obj *)0;
997 otmp->oattached = OATTACHED_MONST; /* mark it */
1002 /* returns a pointer to a new monst structure based on
1003 * the one contained within the obj.
1006 get_mtraits(obj, copyof)
1010 struct monst *mtmp = (struct monst *)0;
1011 struct monst *mnew = (struct monst *)0;
1013 if (obj->oxlth && obj->oattached == OATTACHED_MONST)
1014 mtmp = (struct monst *)obj->oextra;
1017 int lth = mtmp->mxlth + mtmp->mnamelth;
1018 mnew = newmonst(lth);
1019 lth += sizeof(struct monst);
1020 (void) memcpy((genericptr_t)mnew,
1021 (genericptr_t)mtmp, lth);
1023 /* Never insert this returned pointer into mon chains! */
1033 /* make an object named after someone listed in the scoreboard file */
1035 mk_tt_object(objtype, x, y)
1036 int objtype; /* CORPSE or STATUE */
1039 register struct obj *otmp, *otmp2;
1040 boolean initialize_it;
1042 /* player statues never contain books */
1043 initialize_it = (objtype != STATUE);
1044 if ((otmp = mksobj_at(objtype, x, y, initialize_it, FALSE)) != 0) {
1045 /* tt_oname will return null if the scoreboard is empty */
1046 if ((otmp2 = tt_oname(otmp)) != 0) otmp = otmp2;
1051 /* make a new corpse or statue, uninitialized if a statue (i.e. no books) */
1053 mk_named_object(objtype, ptr, x, y, nm)
1054 int objtype; /* CORPSE or STATUE */
1055 struct permonst *ptr;
1061 otmp = mkcorpstat(objtype, (struct monst *)0, ptr,
1062 x, y, (boolean)(objtype != STATUE));
1064 otmp = oname(otmp, nm);
1070 register struct obj *otmp;
1072 int otyp = otmp->otyp;
1073 int omat = objects[otyp].oc_material;
1075 if (objects[otyp].oc_oprop == FIRE_RES || otyp == WAN_FIRE)
1078 return((boolean)((omat <= WOOD && omat != LIQUID) || omat == PLASTIC));
1083 register struct obj *otmp;
1085 int otyp = otmp->otyp;
1087 return((boolean)(objects[otyp].oc_material <= WOOD &&
1088 objects[otyp].oc_material != LIQUID));
1095 * These routines maintain the single-linked lists headed in level.objects[][]
1096 * and threaded through the nexthere fields in the object-instance structure.
1099 /* put the object at the given location */
1101 place_object(otmp, x, y)
1102 register struct obj *otmp;
1105 register struct obj *otmp2 = level.objects[x][y];
1107 if (otmp->where != OBJ_FREE)
1108 panic("place_object: obj not free");
1110 obj_no_longer_held(otmp);
1111 if (otmp->otyp == BOULDER) block_point(x,y); /* vision */
1113 /* obj goes under boulders */
1114 if (otmp2 && (otmp2->otyp == BOULDER)) {
1115 otmp->nexthere = otmp2->nexthere;
1116 otmp2->nexthere = otmp;
1118 otmp->nexthere = otmp2;
1119 level.objects[x][y] = otmp;
1122 /* set the new object's location */
1126 otmp->where = OBJ_FLOOR;
1128 /* add to floor chain */
1131 if (otmp->timed) obj_timer_checks(otmp, x, y, 0);
1134 #define ON_ICE(a) ((a)->recharged)
1135 #define ROT_ICE_ADJUSTMENT 2 /* rotting on ice takes 2 times as long */
1137 /* If ice was affecting any objects correct that now
1138 * Also used for starting ice effects too. [zap.c]
1141 obj_ice_effects(x, y, do_buried)
1147 for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere) {
1148 if (otmp->timed) obj_timer_checks(otmp, x, y, 0);
1151 for (otmp = level.buriedobjlist; otmp; otmp = otmp->nobj) {
1152 if (otmp->ox == x && otmp->oy == y) {
1153 if (otmp->timed) obj_timer_checks(otmp, x, y, 0);
1160 * Returns an obj->age for a corpse object on ice, that would be the
1161 * actual obj->age if the corpse had just been lifted from the ice.
1162 * This is useful when just using obj->age in a check or calculation because
1163 * rot timers pertaining to the object don't have to be stopped and
1167 peek_at_iced_corpse_age(otmp)
1170 long age, retval = otmp->age;
1172 if (otmp->otyp == CORPSE && ON_ICE(otmp)) {
1173 /* Adjust the age; must be same as obj_timer_checks() for off ice*/
1174 age = monstermoves - otmp->age;
1175 retval = otmp->age + (age / ROT_ICE_ADJUSTMENT);
1176 #ifdef DEBUG_EFFECTS
1177 pline_The("%s age has ice modifications:otmp->age = %ld, returning %ld.",
1178 s_suffix(doname(otmp)),otmp->age, retval);
1179 pline("Effective age of corpse: %ld.",
1180 monstermoves - retval);
1187 obj_timer_checks(otmp, x, y, force)
1190 int force; /* 0 = no force so do checks, <0 = force off, >0 force on */
1193 short action = ROT_CORPSE;
1194 boolean restart_timer = FALSE;
1195 boolean on_floor = (otmp->where == OBJ_FLOOR);
1196 boolean buried = (otmp->where == OBJ_BURIED);
1198 /* Check for corpses just placed on or in ice */
1199 if (otmp->otyp == CORPSE && (on_floor || buried) && is_ice(x,y)) {
1200 tleft = stop_timer(action, (genericptr_t)otmp);
1202 action = REVIVE_MON;
1203 tleft = stop_timer(action, (genericptr_t)otmp);
1208 tleft = tleft - monstermoves;
1209 /* mark the corpse as being on ice */
1211 #ifdef DEBUG_EFFECTS
1212 pline("%s is now on ice at %d,%d.", The(xname(otmp)),x,y);
1214 /* Adjust the time remaining */
1215 tleft *= ROT_ICE_ADJUSTMENT;
1216 restart_timer = TRUE;
1217 /* Adjust the age; must be same as in obj_ice_age() */
1218 age = monstermoves - otmp->age;
1219 otmp->age = monstermoves - (age * ROT_ICE_ADJUSTMENT);
1222 /* Check for corpses coming off ice */
1223 else if ((force < 0) ||
1224 (otmp->otyp == CORPSE && ON_ICE(otmp) &&
1225 ((on_floor && !is_ice(x,y)) || !on_floor))) {
1226 tleft = stop_timer(action, (genericptr_t)otmp);
1228 action = REVIVE_MON;
1229 tleft = stop_timer(action, (genericptr_t)otmp);
1234 tleft = tleft - monstermoves;
1236 #ifdef DEBUG_EFFECTS
1237 pline("%s is no longer on ice at %d,%d.", The(xname(otmp)),x,y);
1239 /* Adjust the remaining time */
1240 tleft /= ROT_ICE_ADJUSTMENT;
1241 restart_timer = TRUE;
1242 /* Adjust the age */
1243 age = monstermoves - otmp->age;
1244 otmp->age = otmp->age + (age / ROT_ICE_ADJUSTMENT);
1247 /* now re-start the timer with the appropriate modifications */
1249 (void) start_timer(tleft, TIMER_OBJECT, action, (genericptr_t)otmp);
1253 #undef ROT_ICE_ADJUSTMENT
1257 register struct obj *otmp;
1262 if (otmp->where != OBJ_FLOOR)
1263 panic("remove_object: obj not on floor");
1264 if (otmp->otyp == BOULDER) unblock_point(x,y); /* vision */
1265 extract_nexthere(otmp, &level.objects[x][y]);
1266 extract_nobj(otmp, &fobj);
1267 if (otmp->timed) obj_timer_checks(otmp,x,y,0);
1270 /* throw away all of a monster's inventory */
1272 discard_minvent(mtmp)
1277 while ((otmp = mtmp->minvent) != 0) {
1278 obj_extract_self(otmp);
1279 obfree(otmp, (struct obj *)0); /* dealloc_obj() isn't sufficient */
1284 * Free obj from whatever list it is on in preperation of deleting it or
1285 * moving it elsewhere. This will perform all high-level consequences
1286 * involved with removing the item. E.g. if the object is in the hero's
1287 * inventory and confers heat resistance, the hero will lose it.
1290 * OBJ_FREE not on any list
1291 * OBJ_FLOOR fobj, level.locations[][] chains (use remove_object)
1292 * OBJ_CONTAINED cobj chain of container object
1293 * OBJ_INVENT hero's invent chain (use freeinv)
1294 * OBJ_MINVENT monster's invent chain
1295 * OBJ_MIGRATING migrating chain
1296 * OBJ_BURIED level.buriedobjs chain
1297 * OBJ_ONBILL on billobjs chain
1300 obj_extract_self(obj)
1303 switch (obj->where) {
1310 extract_nobj(obj, &obj->ocontainer->cobj);
1311 container_weight(obj->ocontainer);
1317 extract_nobj(obj, &obj->ocarry->minvent);
1320 extract_nobj(obj, &migrating_objs);
1323 extract_nobj(obj, &level.buriedobjlist);
1326 extract_nobj(obj, &billobjs);
1329 panic("obj_extract_self");
1335 /* Extract the given object from the chain, following nobj chain. */
1337 extract_nobj(obj, head_ptr)
1338 struct obj *obj, **head_ptr;
1340 struct obj *curr, *prev;
1343 for (prev = (struct obj *) 0; curr; prev = curr, curr = curr->nobj) {
1346 prev->nobj = curr->nobj;
1348 *head_ptr = curr->nobj;
1352 if (!curr) panic("extract_nobj: object lost");
1353 obj->where = OBJ_FREE;
1358 * Extract the given object from the chain, following nexthere chain.
1360 * This does not set obj->where, this function is expected to be called
1361 * in tandem with extract_nobj, which does set it.
1364 extract_nexthere(obj, head_ptr)
1365 struct obj *obj, **head_ptr;
1367 struct obj *curr, *prev;
1370 for (prev = (struct obj *) 0; curr; prev = curr, curr = curr->nexthere) {
1373 prev->nexthere = curr->nexthere;
1375 *head_ptr = curr->nexthere;
1379 if (!curr) panic("extract_nexthere: object lost");
1384 * Add obj to mon's inventory. If obj is able to merge with something already
1385 * in the inventory, then the passed obj is deleted and 1 is returned.
1386 * Otherwise 0 is returned.
1389 add_to_minv(mon, obj)
1395 if (obj->where != OBJ_FREE)
1396 panic("add_to_minv: obj not free");
1398 /* merge if possible */
1399 for (otmp = mon->minvent; otmp; otmp = otmp->nobj)
1400 if (merged(&otmp, &obj))
1401 return 1; /* obj merged and then free'd */
1402 /* else insert; don't bother forcing it to end of chain */
1403 obj->where = OBJ_MINVENT;
1405 obj->nobj = mon->minvent;
1407 return 0; /* obj on mon's inventory chain */
1411 * Add obj to container, make sure obj is "free". Returns (merged) obj.
1412 * The input obj may be deleted in the process.
1415 add_to_container(container, obj)
1416 struct obj *container, *obj;
1420 if (obj->where != OBJ_FREE)
1421 panic("add_to_container: obj not free");
1422 if (container->where != OBJ_INVENT && container->where != OBJ_MINVENT)
1423 obj_no_longer_held(obj);
1425 /* merge if possible */
1426 for (otmp = container->cobj; otmp; otmp = otmp->nobj)
1427 if (merged(&otmp, &obj)) return (otmp);
1429 obj->where = OBJ_CONTAINED;
1430 obj->ocontainer = container;
1431 obj->nobj = container->cobj;
1432 container->cobj = obj;
1437 add_to_migration(obj)
1440 if (obj->where != OBJ_FREE)
1441 panic("add_to_migration: obj not free");
1443 obj->where = OBJ_MIGRATING;
1444 obj->nobj = migrating_objs;
1445 migrating_objs = obj;
1452 if (obj->where != OBJ_FREE)
1453 panic("add_to_buried: obj not free");
1455 obj->where = OBJ_BURIED;
1456 obj->nobj = level.buriedobjlist;
1457 level.buriedobjlist = obj;
1460 /* Recalculate the weight of this container and all of _its_ containers. */
1462 container_weight(container)
1463 struct obj *container;
1465 container->owt = weight(container);
1466 if (container->where == OBJ_CONTAINED)
1467 container_weight(container->ocontainer);
1469 else if (container->where == OBJ_INVENT)
1470 recalculate load delay here ???
1475 * Deallocate the object. _All_ objects should be run through here for
1476 * them to be deallocated.
1482 if (obj->where != OBJ_FREE)
1483 panic("dealloc_obj: obj not free");
1485 /* free up any timers attached to the object */
1487 obj_stop_timers(obj);
1490 * Free up any light sources attached to the object.
1492 * We may want to just call del_light_source() without any
1493 * checks (requires a code change there). Otherwise this
1494 * list must track all objects that can have a light source
1495 * attached to it (and also requires lamplit to be set).
1497 if (obj_sheds_light(obj))
1498 del_light_source(LS_OBJECT, (genericptr_t) obj);
1500 if (obj == thrownobj) thrownobj = (struct obj*)0;
1502 free((genericptr_t) obj);
1506 /* Check all object lists for consistency. */
1514 char obj_address[20], mon_address[20]; /* room for formatted pointers */
1516 mesg = "fobj sanity";
1517 for (obj = fobj; obj; obj = obj->nobj) {
1518 if (obj->where != OBJ_FLOOR) {
1519 pline("%s obj %s %s@(%d,%d): %s\n", mesg,
1520 fmt_ptr((genericptr_t)obj, obj_address),
1521 where_name(obj->where),
1522 obj->ox, obj->oy, doname(obj));
1524 check_contained(obj, mesg);
1527 mesg = "location sanity";
1528 for (x = 0; x < COLNO; x++)
1529 for (y = 0; y < ROWNO; y++)
1530 for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
1531 if (obj->where != OBJ_FLOOR) {
1532 pline("%s obj %s %s@(%d,%d): %s\n", mesg,
1533 fmt_ptr((genericptr_t)obj, obj_address),
1534 where_name(obj->where),
1535 obj->ox, obj->oy, doname(obj));
1538 mesg = "invent sanity";
1539 for (obj = invent; obj; obj = obj->nobj) {
1540 if (obj->where != OBJ_INVENT) {
1541 pline("%s obj %s %s: %s\n", mesg,
1542 fmt_ptr((genericptr_t)obj, obj_address),
1543 where_name(obj->where), doname(obj));
1545 check_contained(obj, mesg);
1548 mesg = "migrating sanity";
1549 for (obj = migrating_objs; obj; obj = obj->nobj) {
1550 if (obj->where != OBJ_MIGRATING) {
1551 pline("%s obj %s %s: %s\n", mesg,
1552 fmt_ptr((genericptr_t)obj, obj_address),
1553 where_name(obj->where), doname(obj));
1555 check_contained(obj, mesg);
1558 mesg = "buried sanity";
1559 for (obj = level.buriedobjlist; obj; obj = obj->nobj) {
1560 if (obj->where != OBJ_BURIED) {
1561 pline("%s obj %s %s: %s\n", mesg,
1562 fmt_ptr((genericptr_t)obj, obj_address),
1563 where_name(obj->where), doname(obj));
1565 check_contained(obj, mesg);
1568 mesg = "bill sanity";
1569 for (obj = billobjs; obj; obj = obj->nobj) {
1570 if (obj->where != OBJ_ONBILL) {
1571 pline("%s obj %s %s: %s\n", mesg,
1572 fmt_ptr((genericptr_t)obj, obj_address),
1573 where_name(obj->where), doname(obj));
1575 /* shouldn't be a full container on the bill */
1577 pline("%s obj %s contains %s! %s\n", mesg,
1578 fmt_ptr((genericptr_t)obj, obj_address),
1579 something, doname(obj));
1583 mesg = "minvent sanity";
1584 for (mon = fmon; mon; mon = mon->nmon)
1585 for (obj = mon->minvent; obj; obj = obj->nobj) {
1586 if (obj->where != OBJ_MINVENT) {
1587 pline("%s obj %s %s: %s\n", mesg,
1588 fmt_ptr((genericptr_t)obj, obj_address),
1589 where_name(obj->where), doname(obj));
1591 if (obj->ocarry != mon) {
1592 pline("%s obj %s (%s) not held by mon %s (%s)\n", mesg,
1593 fmt_ptr((genericptr_t)obj, obj_address),
1595 fmt_ptr((genericptr_t)mon, mon_address),
1598 check_contained(obj, mesg);
1602 /* This must stay consistent with the defines in obj.h. */
1603 static const char *obj_state_names[NOBJ_STATES] = {
1604 "free", "floor", "contained", "invent",
1605 "minvent", "migrating", "buried", "onbill"
1608 STATIC_OVL const char *
1612 return (where<0 || where>=NOBJ_STATES) ? "unknown" : obj_state_names[where];
1615 /* obj sanity check: check objs contained by container */
1617 check_contained(container, mesg)
1618 struct obj *container;
1622 char obj1_address[20], obj2_address[20];
1624 for (obj = container->cobj; obj; obj = obj->nobj) {
1625 if (obj->where != OBJ_CONTAINED)
1626 pline("contained %s obj %s: %s\n", mesg,
1627 fmt_ptr((genericptr_t)obj, obj1_address),
1628 where_name(obj->where));
1629 else if (obj->ocontainer != container)
1630 pline("%s obj %s not in container %s\n", mesg,
1631 fmt_ptr((genericptr_t)obj, obj1_address),
1632 fmt_ptr((genericptr_t)container, obj2_address));