From: Ari Johnson Date: Thu, 22 Feb 2007 23:59:22 +0000 (+0000) Subject: namegrab() and namegraball() X-Git-Tag: 0.73~126 X-Git-Url: https://git.theari.com/?a=commitdiff_plain;h=3d96099571d11c401b97ecb1bab0924d29e2ec81;p=cobramush.git namegrab() and namegraball() --- diff --git a/game/txt/hlp/cobra_func.hlp b/game/txt/hlp/cobra_func.hlp index 645843c..dd5e4c5 100644 --- a/game/txt/hlp/cobra_func.hlp +++ b/game/txt/hlp/cobra_func.hlp @@ -2507,6 +2507,24 @@ for an object named "Test", preferring a thing over other types. that it will continue to do this for the time being. Don't rely on it. Related functions: FULLNAME(), ACCNAME(), INAME() +& NAMEGRAB() +& NAMEGRABALL() + namegrab(,) + namegraball(,) + + The namegrab() function, when given a list of dbrefs and a name, returns + the first dbref in the list that would match as if you were + checking num() or locate(). An exact match has priority over non-exact matches. + + namegraball() returns all dbrefs whose names would be matched by. + + eg: @@(#0: Room Zero. #1: One. #2: Master Room) + > th namegrab(#0 #1 #2,room) + #0 + > th namegrab(#0 #1 #2,master room) + #2 + > th namegraball(#0 #1 #2,room) + #0 #2 & NAND() nand(, ,...) diff --git a/src/function.c b/src/function.c index e85bc47..8f65e4c 100644 --- a/src/function.c +++ b/src/function.c @@ -487,6 +487,8 @@ FUNTAB flist[] = { {"MUNGE", fun_munge, 3, 5, FN_REG}, {"MWHO", fun_lwho, 0, 0, FN_REG}, {"NAME", fun_name, 0, 2, FN_REG}, + {"NAMEGRAB", fun_namegrab, 2, 3, FN_REG}, + {"NAMEGRABALL", fun_namegraball, 2, 3, FN_REG}, {"NAND", fun_nand, 1, INT_MAX, FN_REG}, {"NATTR", fun_nattr, 1, 1, FN_REG}, {"NCON", fun_dbwalker, 1, 1, FN_REG}, diff --git a/src/funlist.c b/src/funlist.c index d3e3c60..18dbbe1 100644 --- a/src/funlist.c +++ b/src/funlist.c @@ -1933,6 +1933,112 @@ FUNCTION(fun_grab) } while (s); } +/* ARGSUSED */ +FUNCTION(fun_namegraball) +{ + /* Given a list of dbrefs and a string, it matches the + * name of the dbrefs against the string. + * grabnameall(#1 #2 #3,god) -> #1 + */ + + char *r, *s, sep; + dbref victim; + dbref absolute; + int first = 1; + + if (!delim_check(buff, bp, nargs, args, 3, &sep)) + return; + + absolute = parse_dbref(args[1]); + if (!RealGoodObject(absolute)) + absolute = NOTHING; + + if (*args[1]) { + s = trim_space_sep(args[0], sep); + do { + r = split_token(&s, sep); + victim = parse_dbref(r); + if (!RealGoodObject(victim)) + continue; /* Don't bother with garbage */ + if (!(string_match(Name(victim), args[1]) || (absolute == victim))) + continue; + if (!can_interact(victim, executor, INTERACT_MATCH)) + continue; + /* It matches, and is interact-able */ + if (!first) + safe_chr(sep, buff, bp); + safe_str(r, buff, bp); + first = 0; + } while (s); + } else { + /* Pull out all good objects (those that _have_ names) */ + s = trim_space_sep(args[0], sep); + do { + r = split_token(&s, sep); + victim = parse_dbref(r); + if (!RealGoodObject(victim)) + continue; /* Don't bother with garbage */ + if (!can_interact(victim, executor, INTERACT_MATCH)) + continue; + /* It's real, and is interact-able */ + if (!first) + safe_chr(sep, buff, bp); + safe_str(r, buff, bp); + first = 0; + } while (s); + } +} + +/* ARGSUSED */ +FUNCTION(fun_namegrab) +{ + /* Given a list of dbrefs and a string, it matches the + * name of the dbrefs against the string. + */ + + char *r, *s, sep; + dbref victim; + dbref absolute; + char *exact_res, *res; + + exact_res = res = NULL; + + if (!delim_check(buff, bp, nargs, args, 3, &sep)) + return; + + absolute = parse_dbref(args[1]); + if (!RealGoodObject(absolute)) + absolute = NOTHING; + + /* Walk the wordstring, until we find the word we want. */ + s = trim_space_sep(args[0], sep); + do { + r = split_token(&s, sep); + victim = parse_dbref(r); + if (!RealGoodObject(victim)) + continue; /* Don't bother with garbage */ + /* Dbref match has top priority */ + if ((absolute == victim) && can_interact(victim, executor, INTERACT_MATCH)) { + safe_str(r, buff, bp); + return; + } + /* Exact match has second priority */ + if (!exact_res && !strcasecmp(Name(victim), args[1]) && + can_interact(victim, executor, INTERACT_MATCH)) { + exact_res = r; + } + /* Non-exact match. */ + if (!res && string_match(Name(victim), args[1]) && + can_interact(victim, executor, INTERACT_MATCH)) { + res = r; + } + } while (s); + if (exact_res) + safe_str(exact_res, buff, bp); + else if (res) + safe_str(res, buff, bp); +} + /* ARGSUSED */ FUNCTION(fun_match) { diff --git a/win32/funs.h b/win32/funs.h index 46cdd0c..f3ae546 100644 --- a/win32/funs.h +++ b/win32/funs.h @@ -210,6 +210,8 @@ FUNCTION_PROTO(fun_mudname); FUNCTION_PROTO(fun_mul); FUNCTION_PROTO(fun_munge); FUNCTION_PROTO(fun_name); +FUNCTION_PROTO(fun_namegrab); +FUNCTION_PROTO(fun_namegraball); FUNCTION_PROTO(fun_nand); FUNCTION_PROTO(fun_nattr); FUNCTION_PROTO(fun_nearby);