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