followup to PR #744 (really this time)

Like the commit message for #743, the followup to it was misIDed
as #744.  This one is for #744.  There's no need to check a name
against all the "<class> of typename" unless it contains " of ".
This commit is contained in:
PatR
2022-04-24 01:01:00 -07:00
parent e45ccca3ff
commit f886e0d6ce

View File

@@ -3268,40 +3268,44 @@ get_table_objclass(lua_State *L)
static int
find_objtype(lua_State *L, const char *s)
{
if (s) {
if (s && *s) {
int i;
const char *objname;
char class = 0;
/* In objects.h, some item classes are defined without prefixes (such
* as "scroll of ") in their names, making some names (such as
* "teleportation") ambiguous. Get the object class if it is
* specified, and only return an object of the matching class. */
struct {
/* In objects.h, some item classes are defined without prefixes
(such as "scroll of ") in their names, making some names (such
as "teleportation") ambiguous. Get the object class if it is
specified, and only return an object of the matching class. */
static struct objclasspfx {
const char *prefix;
char class;
} class_prefixes[] = {
{"ring of ", RING_CLASS},
{"potion of ", POTION_CLASS},
{"scroll of ", SCROLL_CLASS},
{"spellbook of ", SPBOOK_CLASS},
{"wand of ", WAND_CLASS},
{NULL, 0}
{ "ring of ", RING_CLASS },
{ "potion of ", POTION_CLASS },
{ "scroll of ", SCROLL_CLASS },
{ "spellbook of ", SPBOOK_CLASS },
{ "wand of ", WAND_CLASS },
{ NULL, 0 }
};
for (i = 0; class_prefixes[i].prefix; i++) {
const char *p = class_prefixes[i].prefix;
if (!strncmpi(s, p, strlen(p))) {
class = class_prefixes[i].class;
s = s + strlen(p);
break;
if (strstri(s, " of ")) {
for (i = 0; class_prefixes[i].prefix; i++) {
const char *p = class_prefixes[i].prefix;
if (!strncmpi(s, p, strlen(p))) {
class = class_prefixes[i].class;
s = s + strlen(p);
break;
}
}
}
/* find by object name */
for (i = 0; i < NUM_OBJECTS; i++) {
objname = OBJ_NAME(objects[i]);
if ((!class || class == objects[i].oc_class) &&
objname && !strcmpi(s, objname))
if ((!class || class == objects[i].oc_class)
&& objname && !strcmpi(s, objname))
return i;
}