named-fruit manipulation

Add some new routines for dealing with fruit.  I had hoped they would
let the existing fruit handling be simplified quite a bit, but the
improvement wasn't great.  However, they're also groundwork for fixing
an old bug.
This commit is contained in:
PatR
2017-07-03 18:57:50 -07:00
parent fa2fbb64c8
commit ca84c8e0ff
5 changed files with 187 additions and 65 deletions

View File

@@ -2341,32 +2341,32 @@ boolean tinitial, tfrom_file;
return;
if (!initial) {
struct fruit *f;
int fnum = 0;
num = 0;
for (f = ffruit; f; f = f->nextf) {
if (!strcmp(op, f->fname))
break;
num++;
}
if (!flags.made_fruit) {
for (forig = ffruit; forig; forig = forig->nextf) {
if (!strcmp(pl_fruit, forig->fname)) {
break;
}
/* count number of named fruits; if 'op' is found among them,
then the count doesn't matter because we won't be adding it */
f = fruit_from_name(op, FALSE, &fnum);
if (!f) {
if (!flags.made_fruit)
forig = fruit_from_name(pl_fruit, FALSE, (int *) 0);
if (!forig && fnum >= 100) {
pline("Doing that so many times isn't very fruitful.");
return;
}
}
if (!forig && num >= 100) {
pline("Doing that so many times isn't very fruitful.");
return;
}
}
goodfruit:
nmcpy(pl_fruit, op, PL_FSIZ);
sanitize_name(pl_fruit);
/* OBJ_NAME(objects[SLIME_MOLD]) won't work after initialization */
/* OBJ_NAME(objects[SLIME_MOLD]) won't work for this after
initialization; it gets changed to generic "fruit" */
if (!*pl_fruit)
nmcpy(pl_fruit, "slime mold", PL_FSIZ);
if (!initial) {
/* if 'forig' is nonNull, we replace it rather than add
a new fruit; it can only be nonNull if no fruits have
been created since the previous name was put in place */
(void) fruitadd(pl_fruit, forig);
pline("Fruit is now \"%s\".", pl_fruit);
}
@@ -5574,7 +5574,6 @@ struct fruit *replace_fruit;
/* disallow naming after other foods (since it'd be impossible
* to tell the difference)
*/
for (i = bases[FOOD_CLASS]; objects[i].oc_class == FOOD_CLASS; i++) {
if (!strcmp(OBJ_NAME(objects[i]), pl_fruit)) {
found = TRUE;
@@ -5584,15 +5583,15 @@ struct fruit *replace_fruit;
{
char *c;
c = pl_fruit;
for (c = pl_fruit; *c >= '0' && *c <= '9'; c++)
;
continue;
if (isspace((uchar) *c) || *c == 0)
numeric = TRUE;
}
if (found || numeric || !strncmp(str, "cursed ", 7)
|| !strncmp(str, "uncursed ", 9) || !strncmp(str, "blessed ", 8)
if (found || numeric
|| !strncmp(str, "cursed ", 7)
|| !strncmp(str, "uncursed ", 9)
|| !strncmp(str, "blessed ", 8)
|| !strncmp(str, "partly eaten ", 13)
|| (!strncmp(str, "tin of ", 7)
&& (!strcmp(str + 7, "spinach")
@@ -5615,42 +5614,39 @@ struct fruit *replace_fruit;
*/
flags.made_fruit = FALSE;
if (replace_fruit) {
for (f = ffruit; f; f = f->nextf) {
if (f == replace_fruit) {
copynchars(f->fname, str, PL_FSIZ - 1);
goto nonew;
}
}
/* replace_fruit is already part of the fruit chain;
update it in place rather than looking it up again */
f = replace_fruit;
copynchars(f->fname, str, PL_FSIZ - 1);
goto nonew;
}
} else {
/* not user_supplied, so assumed to be from bones */
copynchars(altname, str, PL_FSIZ - 1);
sanitize_name(altname);
flags.made_fruit = TRUE; /* for safety. Any fruit name added from a
bones level should exist anyway. */
* bones level should exist anyway. */
}
for (f = ffruit; f; f = f->nextf) {
if (f->fid > highest_fruit_id)
highest_fruit_id = f->fid;
if (!strncmp(str, f->fname, PL_FSIZ - 1)
|| (*altname && !strcmp(altname, f->fname)))
goto nonew;
}
/* if adding another fruit would overflow spe, use a random
fruit instead... we've got a lot to choose from.
f = fruit_from_name(*altname ? altname : str, FALSE, &highest_fruit_id);
if (f)
goto nonew;
/* Maximum number of named fruits is 127, even if obj->spe can
handle bigger values. If adding another fruit would overflow,
use a random fruit instead... we've got a lot to choose from.
current_fruit remains as is. */
if (highest_fruit_id >= 127)
return rnd(127);
f = newfruit();
(void) memset((genericptr_t)f, 0, sizeof(struct fruit));
(void) memset((genericptr_t) f, 0, sizeof (struct fruit));
copynchars(f->fname, *altname ? altname : str, PL_FSIZ - 1);
f->fid = ++highest_fruit_id;
/* we used to go out of our way to add it at the end of the list,
but the order is arbitrary so use simpler insertion at start */
f->nextf = ffruit;
ffruit = f;
nonew:
nonew:
if (user_specified)
context.current_fruit = f->fid;
return f->fid;