MCPcopy Create free account
hub / github.com/NetHack/NetHack / make_itemized_bill

Function make_itemized_bill

src/shk.c:1544–1663  ·  view source on GitHub ↗

for itemized purchasing, create an alternate shop bill that hides container contents */

Source from the content-addressed store, hash-verified

1542/* for itemized purchasing, create an alternate shop bill that hides
1543 container contents */
1544staticfn int /* returns number of entries */
1545make_itemized_bill(
1546 struct monst *shkp,
1547 Bill **ibill_p) /* output, augmented bill similar to a 'sortloot array' */
1548{
1549 static Bill zerosbi; /* Null sortbill item */
1550 Bill *ibill;
1551 struct bill_x *bp;
1552 struct obj *otmp;
1553 struct eshk *eshkp = ESHK(shkp);
1554 int i, n, bidx, ebillct = eshkp->billct;
1555 int8 used;
1556 long quan, cost;
1557
1558 /* this overallocates unless there happens to be a used-up portion
1559 and an intact potion for every object on the bill; doing it this
1560 way avoids the need to look up every object on the bill an extra
1561 time; (the +1 for a terminator isn't actually needed) */
1562 n = 2 * ebillct + 1;
1563 ibill = *ibill_p = (Bill *) alloc(n * sizeof *ibill);
1564 for (i = 0; i < n; ++i)
1565 ibill[i] = zerosbi;
1566
1567 n = 0; /* number of entries in ibill[]; won't necessary match ebillct */
1568 for (i = 0; i < ebillct; ++i) {
1569 bp = &eshkp->bill_p[i];
1570 /* find the object on the bill */
1571 otmp = bp_to_obj(bp);
1572 if (!otmp) {
1573 impossible("Can't find shop bill entry for #%d", bp->bo_id);
1574 continue;
1575 }
1576 bidx = i; /* index into bill_p[], except for hero-owner container */
1577
1578 if (otmp->quan == 0L || otmp->where == OBJ_ONBILL) {
1579 /* item is completely used up; restore quantity from when it
1580 was first unpaid; otmp is on billobjs list where it can
1581 only be seen via Ix and itemized billing while paying shk */
1582 otmp->quan = bp->bquan;
1583 bp->useup = TRUE; /* (expected to be set already) */
1584 } else if (otmp->quan < bp->bquan) {
1585 /* item is partly used up; we will create two entries in the
1586 augmented bill: one for the used up part here, another for
1587 the intact part (which might be inside a container if put in
1588 after using part of a stack; used up part isn't) below */
1589 ibill[n].obj = otmp;
1590 ibill[n].quan = bp->bquan - otmp->quan;
1591 ibill[n].cost = bp->price * ibill[n].quan;
1592 ibill[n].bidx = bidx; /* duplicate index into eshkp->bill_p[] */
1593 ibill[n].usedup = PartlyUsedUp; /* for sorting */
1594 ++n; /* intact portion will be a separate entry, next */
1595 }
1596
1597 if (otmp->where == OBJ_ONBILL) {
1598 /* completely used up */
1599 quan = bp->bquan;
1600 cost = bp->price * quan;
1601 used = FullyUsedUp;

Callers 1

dopayFunction · 0.85

Calls 5

bp_to_objFunction · 0.85
unpaid_costFunction · 0.85
qsortFunction · 0.85
allocFunction · 0.70
impossibleFunction · 0.70

Tested by

no test coverage detected