Skip to content

Commit

Permalink
Make lookup_artifact() and lookup_ego_item() return structs (thanks P…
Browse files Browse the repository at this point in the history
…owerWyrm)
  • Loading branch information
NickMcConnell committed Jan 9, 2017
1 parent ec75c80 commit ed713e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
22 changes: 17 additions & 5 deletions src/load.c
Expand Up @@ -124,11 +124,19 @@ static struct object *rd_item(void)

rd_string(buf, sizeof(buf));
if (buf[0]) {
obj->artifact = &a_info[lookup_artifact_name(buf)];
obj->artifact = lookup_artifact_name(buf);
if (!obj->artifact) {
note(format("Couldn't find artifact %s!", buf));
return NULL;
}
}
rd_string(buf, sizeof(buf));
if (buf[0]) {
obj->ego = &e_info[lookup_ego_item(buf, obj->tval, obj->sval)];
obj->ego = lookup_ego_item(buf, obj->tval, obj->sval);
if (!obj->ego) {
note(format("Couldn't find ego item %s!", buf));
return NULL;
}
}
rd_byte(&effect);

Expand Down Expand Up @@ -1514,7 +1522,7 @@ int rd_history(void)
s32b turnno;
s16b dlev, clev;
bitflag type[HIST_SIZE];
byte aidx = 0;
struct artifact *art;
char name[80];
char text[80];

Expand All @@ -1525,11 +1533,15 @@ int rd_history(void)
rd_s16b(&clev);
rd_string(name, sizeof(name));
if (name[0]) {
aidx = lookup_artifact_name(name);
art = lookup_artifact_name(name);
}
rd_string(text, sizeof(text));
if (!art) {
note(format("Couldn't find artifact %s!", name));
continue;
}

history_add_full(player, type, aidx, dlev, clev, turnno, text);
history_add_full(player, type, art->aidx, dlev, clev, turnno, text);
}

return 0;
Expand Down
12 changes: 4 additions & 8 deletions src/mon-init.c
Expand Up @@ -1329,15 +1329,13 @@ static enum parser_error parse_monster_drop(struct parser *p) {
static enum parser_error parse_monster_drop_artifact(struct parser *p) {
struct monster_race *r = parser_priv(p);
struct monster_drop *d;
int art;
struct artifact *a;

if (!r)
return PARSE_ERROR_MISSING_RECORD_HEADER;
art = lookup_artifact_name(parser_getstr(p, "name"));
if (art < 0)
a = lookup_artifact_name(parser_getstr(p, "name"));
if (a == NULL)
return PARSE_ERROR_NO_ARTIFACT_NAME;
a = &a_info[art];

d = mem_zalloc(sizeof *d);
d->artifact = a;
Expand Down Expand Up @@ -2076,15 +2074,13 @@ static enum parser_error parse_lore_drop(struct parser *p) {
static enum parser_error parse_lore_drop_artifact(struct parser *p) {
struct monster_lore *l = parser_priv(p);
struct monster_drop *d;
int art;
struct artifact *a;

if (!l)
return PARSE_ERROR_MISSING_RECORD_HEADER;
art = lookup_artifact_name(parser_getstr(p, "name"));
if (art < 0)
a = lookup_artifact_name(parser_getstr(p, "name"));
if (a == NULL)
return PARSE_ERROR_NO_ARTIFACT_NAME;
a = &a_info[art];

d = mem_zalloc(sizeof *d);
d->artifact = a;
Expand Down
16 changes: 7 additions & 9 deletions src/obj-util.c
Expand Up @@ -357,18 +357,18 @@ struct object_kind *objkind_byid(int kidx) {
/**
* Return the a_idx of the artifact with the given name
*/
int lookup_artifact_name(const char *name)
struct artifact *lookup_artifact_name(const char *name)
{
int i;
int a_idx = -1;

/* Look for it */
for (i = 1; i < z_info->a_max; i++) {
for (i = 0; i < z_info->a_max; i++) {
struct artifact *art = &a_info[i];

/* Test for equality */
if (art->name && streq(name, art->name))
return i;
return art;

/* Test for close matches */
if (strlen(name) >= 3 && art->name && my_stristr(art->name, name)
Expand All @@ -377,7 +377,7 @@ int lookup_artifact_name(const char *name)
}

/* Return our best match */
return a_idx;
return a_idx > 0 ? &a_info[a_idx] : NULL;
}

/**
Expand All @@ -386,10 +386,9 @@ int lookup_artifact_name(const char *name)
* \param sval object sval
* \return eidx of the ego item type
*/
int lookup_ego_item(const char *name, int tval, int sval)
struct ego_item *lookup_ego_item(const char *name, int tval, int sval)
{
int i;
int eidx = -1;

/* Look for it */
for (i = 0; i < z_info->e_max; i++) {
Expand All @@ -404,14 +403,13 @@ int lookup_ego_item(const char *name, int tval, int sval)
while (poss_item) {
struct object_kind *kind = lookup_kind(tval, sval);
if (kind->kidx == poss_item->kidx) {
return i;
return ego;
}
poss_item = poss_item->next;
}
}

/* Return our best match */
return eidx;
return NULL;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/obj-util.h
Expand Up @@ -34,8 +34,8 @@ bool is_unknown(const struct object *obj);
unsigned check_for_inscrip(const struct object *obj, const char *inscrip);
struct object_kind *lookup_kind(int tval, int sval);
struct object_kind *objkind_byid(int kidx);
int lookup_artifact_name(const char *name);
int lookup_ego_item(const char *name, int tval, int sval);
struct artifact *lookup_artifact_name(const char *name);
struct ego_item *lookup_ego_item(const char *name, int tval, int sval);
int lookup_sval(int tval, const char *name);
void object_short_name(char *buf, size_t max, const char *name);
int compare_items(const struct object *o1, const struct object *o2);
Expand Down

0 comments on commit ed713e9

Please sign in to comment.