findparam improvements!

removed findbuildingtype from findparam
separate isparam() function that I can optimize later
This commit is contained in:
Enno Rehling 2012-05-16 12:06:44 -07:00
parent 02ab10d723
commit 7b6a719a62
9 changed files with 87 additions and 80 deletions

View File

@ -1530,12 +1530,14 @@ static int display_cmd(unit * u, struct order *ord)
{
building *b = u->building;
char **s = NULL;
const char *str;
region *r = u->region;
init_tokens(ord);
skip_token();
switch (getparam(u->faction->locale)) {
str = getstrtoken();
switch (findparam_ex(str, u->faction->locale)) {
case P_BUILDING:
case P_GEBAEUDE:
if (!b) {
@ -1695,14 +1697,17 @@ static int name_cmd(unit * u, struct order *ord)
char **s = NULL;
param_t p;
boolean foreign = false;
const char *str;
init_tokens(ord);
skip_token();
p = getparam(u->faction->locale);
str = getstrtoken();
p = findparam_ex(str, u->faction->locale);
if (p == P_FOREIGN) {
str = getstrtoken();
foreign = true;
p = getparam(u->faction->locale);
p = findparam_ex(str, u->faction->locale);
}
switch (p) {
@ -1951,7 +1956,7 @@ static int mail_cmd(unit * u, struct order *ord)
do {
cont = 0;
switch (findparam(s, u->faction->locale)) {
switch (findparam_ex(s, u->faction->locale)) {
case P_REGION:
/* können alle Einheiten in der Region sehen */
s = getstrtoken();
@ -2991,7 +2996,7 @@ static int renumber_cmd(unit * u, order * ord)
init_tokens(ord);
skip_token();
s = getstrtoken();
switch (findparam(s, u->faction->locale)) {
switch (findparam_ex(s, u->faction->locale)) {
case P_FACTION:
s = getstrtoken();
@ -3374,7 +3379,7 @@ static void new_units(void)
if (get_keyword(makeord) == K_MAKE) {
init_tokens(makeord);
skip_token();
if (getparam(u->faction->locale) == P_TEMP) {
if (isparam(getstrtoken(), u->faction->locale, P_TEMP)) {
const char *token;
char *name = NULL;
int alias;

View File

@ -416,13 +416,13 @@ int teach_cmd(unit * u, struct order *ord)
token = getstrtoken();
/* Beginne die Fehlermeldung */
if (findparam(token, u->faction->locale) != P_TEMP) {
uid = token;
} else {
if (isparam(token, u->faction->locale, P_TEMP)) {
token = getstrtoken();
sprintf(tbuf, "%s %s", LOC(u->faction->locale,
parameters[P_TEMP]), token);
uid = tbuf;
} else {
uid = token;
}
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "unitnotfound_id",
"id", uid));

View File

@ -1341,10 +1341,12 @@ void do_misc(region * r, boolean lasttry)
param_t p;
int id;
unit *ulast = NULL;
const char * s;
init_tokens(ord);
skip_token();
p = getparam(u->faction->locale);
s = getstrtoken();
p = findparam_ex(s, u->faction->locale);
id = getid();
switch (p) {

View File

@ -1494,25 +1494,44 @@ keyword_t findkeyword(const char *s, const struct locale * lang)
param_t findparam(const char *s, const struct locale * lang)
{
void **tokens = get_translations(lang, UT_PARAMS);
int i;
param_t result = NOPARAM;
const void * match;
char buffer[64];
char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), s);
critbit_tree *cb = (critbit_tree *)*tokens;
if (str && cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int));
result = (param_t)i;
} else {
if (str) {
int i;
const void * match;
void **tokens = get_translations(lang, UT_PARAMS);
critbit_tree *cb = (critbit_tree *)*tokens;
if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int));
result = (param_t)i;
}
}
return result;
}
param_t findparam_ex(const char *s, const struct locale * lang)
{
param_t result = findparam(s, lang);
if (result==NOPARAM) {
const building_type *btype = findbuildingtype(s, lang);
if (btype != NULL)
return (param_t) P_GEBAEUDE;
return P_GEBAEUDE;
}
return (result == P_BUILDING) ? P_GEBAEUDE : result;
}
int isparam(const char *s, const struct locale * lang, param_t param)
{
if (s[0]>'@') {
param_t p = (param==P_GEBAEUDE) ? findparam_ex(s, lang) : findparam(s, lang);
return p==param;
}
return 0;
}
param_t getparam(const struct locale * lang)
{
return findparam(getstrtoken(), lang);
@ -1588,18 +1607,17 @@ static int read_newunitid(const faction * f, const region * r)
int read_unitid(const faction * f, const region * r)
{
const char *s = getstrtoken();
param_t param;
/* Da s nun nur einen string enthaelt, suchen wir ihn direkt in der
* paramliste. machen wir das nicht, dann wird getnewunit in s nach der
* nummer suchen, doch dort steht bei temp-units nur "temp" drinnen! */
param = findparam(s, f->locale);
if (param == P_TEMP) {
if (!s || *s == 0) {
return -1;
}
if (isparam(s, f->locale, P_TEMP)) {
return read_newunitid(f, r);
}
if (!s || *s == 0)
return -1;
return atoi36((const char *)s);
}

View File

@ -179,8 +179,10 @@ extern "C" {
extern keyword_t findkeyword(const char *s, const struct locale *lang);
extern param_t findparam(const char *s, const struct locale *lang);
extern param_t getparam(const struct locale *lang);
param_t findparam(const char *s, const struct locale *lang);
param_t findparam_ex(const char *s, const struct locale * lang);
int isparam(const char *s, const struct locale * lang, param_t param);
param_t getparam(const struct locale *lang);
extern int getid(void);
#define unitid(x) itoa36((x)->no)

View File

@ -1922,7 +1922,7 @@ addparam_unit(const char *const param[], spllprm ** spobjp, const unit * u,
sppobj_t otype = SPP_UNIT;
*spobjp = NULL;
if (findparam(param[0], u->faction->locale) == P_TEMP) {
if (isparam(param[0], u->faction->locale, P_TEMP)) {
if (param[1] == NULL) {
/* Fehler: Ziel vergessen */
cmistake(u, ord, 203, MSG_MAGIC);
@ -1981,7 +1981,6 @@ static spellparameter *add_spellparameter(region * target_r, unit * u,
while (!fail && *c && i < size && param[i] != NULL) {
spllprm *spobj = NULL;
param_t pword;
int j = -1;
switch (*c) {
case '?':
@ -2028,8 +2027,7 @@ static spellparameter *add_spellparameter(region * target_r, unit * u,
break;
case 'k':
++c;
pword = findparam(param[i++], u->faction->locale);
switch (pword) {
switch (findparam_ex(param[i++], u->faction->locale)) {
case P_REGION:
spobj = (spllprm *)malloc(sizeof(spllprm));
spobj->flag = 0;

View File

@ -373,12 +373,12 @@ order *parse_order(const char *s, const struct locale * lang)
* \return true if the order is long
* \sa is_exclusive(), is_repeated(), is_persistent()
*/
boolean is_repeated(const order * ord)
int is_repeated(const order * ord)
{
keyword_t kwd = ORD_KEYWORD(ord);
const struct locale *lang = ORD_LOCALE(ord);
const char * s;
param_t param;
int result = 0;
switch (kwd) {
case K_CAST:
@ -399,7 +399,8 @@ boolean is_repeated(const order * ord)
case K_BREED:
case K_PIRACY:
case K_PLANT:
return true;
result = 1;
break;
case K_FOLLOW:
/* FOLLOW is only a long order if we are following a ship. */
@ -407,13 +408,8 @@ boolean is_repeated(const order * ord)
init_tokens(ord);
skip_token();
s = getstrtoken();
if (s[0]>'9') {
param = findparam(s, lang);
parser_popstate();
if (param == P_SHIP) {
return true;
}
}
result = isparam(s, lang, P_SHIP);
parser_popstate();
break;
case K_MAKE:
@ -426,16 +422,13 @@ boolean is_repeated(const order * ord)
init_tokens(ord); /* initialize token-parser */
skip_token();
s = getstrtoken();
if (s[0]>'9') {
param = findparam(s, lang);
parser_popstate();
return param != P_TEMP;
}
result = !isparam(s, lang, P_TEMP);
parser_popstate();
break;
default:
return false;
result = 0;
}
return false;
return result;
}
/**
@ -446,11 +439,11 @@ boolean is_repeated(const order * ord)
* \return true if the order is long
* \sa is_exclusive(), is_repeated(), is_persistent()
*/
boolean is_exclusive(const order * ord)
int is_exclusive(const order * ord)
{
keyword_t kwd = ORD_KEYWORD(ord);
const struct locale *lang = ORD_LOCALE(ord);
param_t param;
int result = 0;
switch (kwd) {
case K_MOVE:
@ -471,18 +464,16 @@ boolean is_exclusive(const order * ord)
case K_BREED:
case K_PIRACY:
case K_PLANT:
return true;
result = 1;
break;
case K_FOLLOW:
/* FOLLOW is only a long order if we are following a ship. */
parser_pushstate();
init_tokens(ord);
skip_token();
param = getparam(lang);
result = isparam(getstrtoken(), lang, P_SHIP);
parser_popstate();
if (param == P_SHIP)
return true;
break;
case K_MAKE:
@ -494,16 +485,13 @@ boolean is_exclusive(const order * ord)
parser_pushstate();
init_tokens(ord); /* initialize token-parser */
skip_token();
param = getparam(lang);
result = !isparam(getstrtoken(), lang, P_TEMP);
parser_popstate();
if (param != P_TEMP)
return true;
break;
default:
return false;
result = 0;
}
return false;
return result;
}
/**
@ -514,11 +502,11 @@ boolean is_exclusive(const order * ord)
* \return true if the order is long
* \sa is_exclusive(), is_repeated(), is_persistent()
*/
boolean is_long(const order * ord)
int is_long(const order * ord)
{
keyword_t kwd = ORD_KEYWORD(ord);
const struct locale *lang = ORD_LOCALE(ord);
param_t param;
int result = 0;
switch (kwd) {
case K_CAST:
@ -548,11 +536,8 @@ boolean is_long(const order * ord)
parser_pushstate();
init_tokens(ord);
skip_token();
param = getparam(lang);
result = isparam(getstrtoken(), lang, P_SHIP);
parser_popstate();
if (param == P_SHIP)
return true;
break;
case K_MAKE:
@ -564,16 +549,13 @@ boolean is_long(const order * ord)
parser_pushstate();
init_tokens(ord); /* initialize token-parser */
skip_token();
param = getparam(lang);
result = !isparam(getstrtoken(), lang, P_TEMP);
parser_popstate();
if (param != P_TEMP)
return true;
break;
default:
return false;
result = 0;
}
return false;
return result;
}
/**
@ -585,10 +567,10 @@ boolean is_long(const order * ord)
* \return true if the order is persistent
* \sa is_exclusive(), is_repeated(), is_persistent()
*/
boolean is_persistent(const order * ord)
int is_persistent(const order * ord)
{
keyword_t kwd = ORD_KEYWORD(ord);
boolean persist = ord->_persistent != 0;
int persist = ord->_persistent != 0;
switch (kwd) {
case K_MOVE:
case K_WEREWOLF:

View File

@ -51,10 +51,10 @@ extern "C" {
extern keyword_t get_keyword(const order * ord);
extern void set_order(order ** destp, order * src);
extern char *getcommand(const order * ord);
extern boolean is_persistent(const order * ord);
extern boolean is_exclusive(const order * ord);
extern boolean is_repeated(const order * ord);
extern boolean is_long(const order * ord);
extern int is_persistent(const order * ord);
extern int is_exclusive(const order * ord);
extern int is_repeated(const order * ord);
extern int is_long(const order * ord);
extern char *write_order(const order * ord, char *buffer, size_t size);

View File

@ -291,7 +291,7 @@ static unit *unitorders(FILE * F, int enc, struct faction *f)
if (s == NULL)
break;
if (s[0]) {
if (s[0] && s[0]!='@') {
const char *stok = s;
stok = parse_token(&stok);
@ -436,7 +436,7 @@ int readorders(const char *filename)
b = getbuf(F, enc_gamedata);
if (!b)
break;
p = igetparam(b, lang);
p = (b[0]=='@') ? NOPARAM : igetparam(b, lang);
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
&& p != P_GAMENAME);
break;