Remember price quotes that have been seen for types of item

These are displayed in discoveries, and a new 'price_quotes' option
allows them to be displayed for un-IDed objects in other contexts
too (the idea is that you turn on the option while identifying
objects and off for general play).

Invalidates existing save files.
This commit is contained in:
Alex Smith
2026-03-18 21:08:22 +00:00
parent 9877e52025
commit 4fae45220e
14 changed files with 124 additions and 12 deletions

View File

@@ -688,16 +688,20 @@ disco_typename(int otyp)
return result;
}
/* append typename(dis) to buf[], possibly truncating in the process */
/* append typename(dis) to buf[], possibly truncating in the process;
also append price quote information if it fits */
staticfn void
disco_append_typename(char *buf, int dis)
{
unsigned len = (unsigned) strlen(buf);
size_t len = strlen(buf);
char *p, *typnm = disco_typename(dis);
size_t typnm_len = strlen(typnm);
char *eos;
if (len + (unsigned) strlen(typnm) < BUFSZ) {
if (len + typnm_len < BUFSZ) {
/* ordinary */
Strcat(buf, typnm);
eos = buf + len + typnm_len;
} else if ((p = strrchr(typnm, '(')) != 0
&& p > typnm && p[-1] == ' ' && strchr(p, ')') != 0) {
/* typename() returned "really long user-applied name (actual type)"
@@ -706,10 +710,14 @@ disco_append_typename(char *buf, int dis)
--p; /* back up to space in front of open paren */
(void) strncat(buf, typnm, BUFSZ - 1 - (len + (unsigned) strlen(p)));
Strcat(buf, p);
eos = buf + strlen(buf);
} else {
/* unexpected; just truncate from end of typename */
(void) strncat(buf, typnm, BUFSZ - 1 - len);
eos = buf + strlen(buf);
}
append_price_quote(buf, &eos, dis);
}
/* minor fixup for Book of the Dead needed in more than one place */
@@ -1125,6 +1133,7 @@ rename_disco(void)
anything any;
menu_item *selected = 0;
int clr = NO_COLOR;
char buf[BUFSZ];
any = cg.zeroany;
tmpwin = create_nhwindow(NHW_MENU);
@@ -1158,9 +1167,10 @@ rename_disco(void)
prev_class = oclass;
}
any.a_int = dis;
*buf = '\0';
disco_append_typename(buf, dis);
add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0,
ATR_NONE, clr,
disco_typename(dis), MENU_ITEMFLAGS_NONE);
ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE);
}
}
if (ct == 0) {

View File

@@ -1659,6 +1659,8 @@ doname_base(
Sprintf(pricebuf, "%ld %s", quotedprice, currency(quotedprice));
ConcatF2(bp, 0, " (%s, %s)",
obj->unpaid ? "unpaid" : "contents", pricebuf);
record_price_quote(obj->otyp, quotedprice / obj->quan, TRUE);
} else if (with_price) { /* on floor or in container on floor */
int nochrg = 0;
long price = get_cost_of_shop_item(obj, &nochrg);
@@ -1672,6 +1674,11 @@ doname_base(
} else if (nochrg > 0) {
Concat(bp, 0, " (no charge)");
}
if (price)
record_price_quote(obj->otyp, price / obj->quan, TRUE);
} else if (iflags.pricequotes && !objects[obj->otyp].oc_name_known) {
append_price_quote(bp, &bp_eos, obj->otyp);
}
if (!strncmp(prefix, "a ", 2)) {

View File

@@ -5334,6 +5334,7 @@ optfn_boolean(
disp.botl = TRUE;
break;
case opt_fixinv:
case opt_price_quotes:
case opt_sortpack:
case opt_implicit_uncursed:
case opt_wizweight:

View File

@@ -433,6 +433,65 @@ setpaid(struct monst *shkp)
}
}
/* Remembers that a shopkeeper has quoted a particular price for a
particular type of object. */
void
record_price_quote(int otyp, unsigned long price, boolean buyprice) {
struct objclass *oc = &objects[otyp];
if (buyprice) {
if (price > oc->oc_buy_maxseen) oc->oc_buy_maxseen = price;
if (price < oc->oc_buy_minseen) oc->oc_buy_minseen = price;
} else {
if (price > oc->oc_sell_maxseen) oc->oc_sell_maxseen = price;
if (price < oc->oc_sell_minseen) oc->oc_sell_minseen = price;
}
}
/* Appends price-quote information to the given buffer, updating the
given end of string position. *eos mut be buf + strlen(buf). If the
update would make bug longer than BUFSZ, instead does nothing. */
void
append_price_quote(char *buf, char **eos, int otyp) {
char buf2[BUFSZ];
char *eos2 = buf2;
const char *sep = "";
size_t len = *eos - buf;
size_t len2;
if (objects[otyp].oc_sell_minseen > objects[otyp].oc_sell_maxseen &&
objects[otyp].oc_buy_minseen > objects[otyp].oc_buy_maxseen)
return;
eos2 += sprintf(eos2, " {");
if (objects[otyp].oc_buy_minseen < objects[otyp].oc_buy_maxseen) {
eos2 += sprintf(eos2, "buy %lu-%lu",
objects[otyp].oc_buy_minseen,
objects[otyp].oc_buy_maxseen);
sep = " ";
} else if (objects[otyp].oc_buy_minseen == objects[otyp].oc_buy_maxseen) {
eos2 += sprintf(eos2, "buy %lu",
objects[otyp].oc_buy_minseen);
sep = " ";
}
if (objects[otyp].oc_sell_minseen < objects[otyp].oc_sell_maxseen) {
eos2 += sprintf(eos2, "%ssell %lu-%lu", sep,
objects[otyp].oc_sell_minseen,
objects[otyp].oc_sell_maxseen);
} else if (objects[otyp].oc_sell_minseen == objects[otyp].oc_sell_maxseen) {
eos2 += sprintf(eos2, "%ssell %lu", sep,
objects[otyp].oc_sell_minseen);
}
eos2 += sprintf(eos2, "}");
len2 = eos2 - buf2;
if (len2 < BUFSZ - len - 1) {
Strcpy(*eos, buf2);
*eos += len2;
}
}
staticfn long
addupbill(struct monst *shkp)
{
@@ -3285,7 +3344,7 @@ add_one_tobill(
bp->bo_id = obj->o_id;
bp->bquan = obj->quan;
if (dummy) { /* a dummy object must be inserted into */
bp->useup = TRUE; /* the gb.billobjs chain here. crucial for */
bp->useup = TRUE; /* the gb.billobjs chain here. crucial for */
add_to_billobjs(obj); /* eating floorfood in shop. see eat.c */
} else
bp->useup = FALSE;
@@ -3300,6 +3359,7 @@ add_one_tobill(
}
eshkp->billct++;
obj->unpaid = 1;
record_price_quote(obj->otyp, bp->price, TRUE);
}
staticfn void
@@ -3492,6 +3552,9 @@ addtobill(
if (!Deaf && !muteshk(shkp) && !silent) {
char buf[BUFSZ];
/* no need to update price quotes here; it was done by
add_one_tobill above */
if (!ltmp) {
pline("%s has no interest in %s.", Shknam(shkp), the(xname(obj)));
return;
@@ -3991,6 +4054,7 @@ sellobj(
pline("%s cannot pay you at present.", Shknam(shkp));
Sprintf(qbuf, "Will you accept %ld %s in credit for ", tmpcr,
currency(tmpcr));
record_price_quote(obj->otyp, tmpcr / obj->quan, FALSE);
c = ynaq(safe_qbuf(qbuf, qbuf, "?", obj, doname, thesimpleoname,
(obj->quan == 1L) ? "that" : "those"));
if (c == 'a') {
@@ -4086,6 +4150,7 @@ sellobj(
: and_its_contents)
: "",
one ? "it" : "them");
record_price_quote(obj->otyp, offer / obj->quan, FALSE);
(void) safe_qbuf(qbuf, qbuf, qsfx, obj, xname, simpleonames,
one ? "that" : "those");
} else