forked from github/server
smarter processing of MAKE TEMP.
the order gets its own keyword. parsing is hard - composite commands are an anti-pattern. this eliminates a lot of unnecessary mallocs for pushing/poping parser state.
This commit is contained in:
parent
41d42fa248
commit
c79dd33bfb
|
@ -2197,6 +2197,9 @@
|
|||
<string name="make">
|
||||
<text locale="de">MACHEN</text>
|
||||
</string>
|
||||
<string name="maketemp">
|
||||
<text locale="de">MACHE TEMP</text>
|
||||
</string>
|
||||
<string name="move">
|
||||
<text locale="de">NACH</text>
|
||||
</string>
|
||||
|
|
|
@ -1475,6 +1475,9 @@
|
|||
<string name="make">
|
||||
<text locale="en">MAKE</text>
|
||||
</string>
|
||||
<string name="maketemp">
|
||||
<text locale="de">MAKE TEMP</text>
|
||||
</string>
|
||||
<string name="move">
|
||||
<text locale="en">MOVE</text>
|
||||
</string>
|
||||
|
|
|
@ -1817,10 +1817,6 @@ int make_cmd(unit * u, struct order *ord)
|
|||
|
||||
p = findparam(s, u->faction->locale);
|
||||
|
||||
/* MACHE TEMP kann hier schon gar nicht auftauchen, weil diese nicht in
|
||||
* thisorder abgespeichert werden - und auf den ist getstrtoken() beim
|
||||
* aufruf von make geeicht */
|
||||
|
||||
if (p == P_ROAD) {
|
||||
plane *pl = rplane(r);
|
||||
if (pl && fval(pl, PFL_NOBUILD)) {
|
||||
|
@ -1905,12 +1901,15 @@ int make_cmd(unit * u, struct order *ord)
|
|||
else if (btype != NOBUILDING) {
|
||||
plane *pl = rplane(r);
|
||||
if (pl && fval(pl, PFL_NOBUILD)) {
|
||||
cmistake(u, ord, 94, MSG_PRODUCE);
|
||||
cmistake(u, ord, 275, MSG_PRODUCE);
|
||||
}
|
||||
else {
|
||||
else if (btype->construction) {
|
||||
int id = getid();
|
||||
build_building(u, btype, id, m, ord);
|
||||
}
|
||||
else {
|
||||
cmistake(u, ord, 275, MSG_PRODUCE);
|
||||
}
|
||||
}
|
||||
else if (itype != NULL) {
|
||||
create_item(u, itype, m);
|
||||
|
|
|
@ -1571,16 +1571,17 @@ message *msg_unitnotfound(const struct unit * mage, struct order * ord,
|
|||
{
|
||||
/* Einheit nicht gefunden */
|
||||
char tbuf[20];
|
||||
const char *uid;
|
||||
const char *uid = 0;
|
||||
|
||||
if (spobj->typ == SPP_UNIT) {
|
||||
uid = itoa36(spobj->data.i);
|
||||
}
|
||||
else {
|
||||
if (spobj->typ == SPP_TEMP) {
|
||||
sprintf(tbuf, "%s %s", LOC(mage->faction->locale,
|
||||
parameters[P_TEMP]), itoa36(spobj->data.i));
|
||||
uid = tbuf;
|
||||
}
|
||||
else if (spobj->typ == SPP_UNIT) {
|
||||
uid = itoa36(spobj->data.i);
|
||||
}
|
||||
assert(uid);
|
||||
return msg_message("unitnotfound_id",
|
||||
"unit region command id", mage, mage->region, ord, uid);
|
||||
}
|
||||
|
|
|
@ -359,6 +359,12 @@ order *parse_order(const char *s, const struct locale * lang)
|
|||
}
|
||||
sptr = s;
|
||||
kwd = get_keyword(parse_token(&sptr), lang);
|
||||
if (kwd == K_MAKE) {
|
||||
const char *s = parse_token(&sptr);
|
||||
if (isparam(s, lang, P_TEMP)) {
|
||||
kwd = K_MAKETEMP;
|
||||
}
|
||||
}
|
||||
if (kwd != NOKEYWORD) {
|
||||
while (isxspace(*(unsigned char *)sptr)) ++sptr;
|
||||
s = sptr;
|
||||
|
@ -380,7 +386,6 @@ bool is_repeated(const order * ord)
|
|||
{
|
||||
keyword_t kwd = ORD_KEYWORD(ord);
|
||||
const struct locale *lang = ORD_LOCALE(ord);
|
||||
const char * s;
|
||||
int result = 0;
|
||||
|
||||
switch (kwd) {
|
||||
|
@ -402,23 +407,10 @@ bool is_repeated(const order * ord)
|
|||
case K_BREED:
|
||||
case K_PIRACY:
|
||||
case K_PLANT:
|
||||
case K_MAKE:
|
||||
result = 1;
|
||||
break;
|
||||
|
||||
case K_MAKE:
|
||||
/* Falls wir MACHE TEMP haben, ignorieren wir es. Alle anderen
|
||||
* Arten von MACHE zaehlen aber als neue defaults und werden
|
||||
* behandelt wie die anderen (deswegen kein break nach case
|
||||
* K_MAKE) - und in thisorder (der aktuelle 30-Tage Befehl)
|
||||
* abgespeichert). */
|
||||
parser_pushstate();
|
||||
init_tokens(ord); /* initialize token-parser */
|
||||
skip_token();
|
||||
s = getstrtoken();
|
||||
result = !isparam(s, lang, P_TEMP);
|
||||
parser_popstate();
|
||||
// TODO: push/popstate is slow, we can do better.
|
||||
break;
|
||||
default:
|
||||
result = 0;
|
||||
}
|
||||
|
@ -456,21 +448,10 @@ bool is_exclusive(const order * ord)
|
|||
case K_BREED:
|
||||
case K_PIRACY:
|
||||
case K_PLANT:
|
||||
case K_MAKE:
|
||||
result = 1;
|
||||
break;
|
||||
|
||||
case K_MAKE:
|
||||
/* Falls wir MACHE TEMP haben, ignorieren wir es. Alle anderen
|
||||
* Arten von MACHE zaehlen aber als neue defaults und werden
|
||||
* behandelt wie die anderen (deswegen kein break nach case
|
||||
* K_MAKE) - und in thisorder (der aktuelle 30-Tage Befehl)
|
||||
* abgespeichert). */
|
||||
parser_pushstate();
|
||||
init_tokens(ord); /* initialize token-parser */
|
||||
skip_token();
|
||||
result = !isparam(getstrtoken(), lang, P_TEMP);
|
||||
parser_popstate();
|
||||
break;
|
||||
default:
|
||||
result = 0;
|
||||
}
|
||||
|
@ -511,20 +492,9 @@ bool is_long(const order * ord)
|
|||
case K_BREED:
|
||||
case K_PIRACY:
|
||||
case K_PLANT:
|
||||
case K_MAKE:
|
||||
return true;
|
||||
|
||||
case K_MAKE:
|
||||
/* Falls wir MACHE TEMP haben, ignorieren wir es. Alle anderen
|
||||
* Arten von MACHE zaehlen aber als neue defaults und werden
|
||||
* behandelt wie die anderen (deswegen kein break nach case
|
||||
* K_MAKE) - und in thisorder (der aktuelle 30-Tage Befehl)
|
||||
* abgespeichert). */
|
||||
parser_pushstate();
|
||||
init_tokens(ord); /* initialize token-parser */
|
||||
skip_token();
|
||||
result = !isparam(getstrtoken(), lang, P_TEMP);
|
||||
parser_popstate();
|
||||
break;
|
||||
default:
|
||||
result = false;
|
||||
}
|
||||
|
|
|
@ -3594,10 +3594,7 @@ void new_units(void)
|
|||
|
||||
while (*ordp) {
|
||||
order *makeord = *ordp;
|
||||
if (getkeyword(makeord) == K_MAKE) {
|
||||
init_tokens(makeord);
|
||||
skip_token();
|
||||
if (isparam(getstrtoken(), u->faction->locale, P_TEMP)) {
|
||||
if (getkeyword(makeord) == K_MAKETEMP) {
|
||||
const char *token;
|
||||
char *name = NULL;
|
||||
int alias;
|
||||
|
@ -3660,7 +3657,6 @@ void new_units(void)
|
|||
newordersp = &ord->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (*ordp == makeord)
|
||||
ordp = &makeord->next;
|
||||
}
|
||||
|
|
48
src/study.c
48
src/study.c
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
||||
Katja Zedel <katze@felidae.kn-bremen.de
|
||||
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||
Katja Zedel <katze@felidae.kn-bremen.de
|
||||
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
|
@ -70,14 +70,15 @@ magic_t getmagicskill(const struct locale * lang)
|
|||
|
||||
if (tokens && s && s[0]) {
|
||||
if (findtoken(*tokens, s, &token) == E_TOK_SUCCESS) {
|
||||
return (magic_t) token.i;
|
||||
} else {
|
||||
return (magic_t)token.i;
|
||||
}
|
||||
else {
|
||||
char buffer[3];
|
||||
buffer[0] = s[0];
|
||||
buffer[1] = s[1];
|
||||
buffer[2] = '\0';
|
||||
if (findtoken(*tokens, buffer, &token) == E_TOK_SUCCESS) {
|
||||
return (magic_t) token.i;
|
||||
return (magic_t)token.i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -173,7 +174,7 @@ static int study_days(unit * student, skill_t sk)
|
|||
|
||||
static int
|
||||
teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
|
||||
bool report, int *academy)
|
||||
bool report, int *academy)
|
||||
{
|
||||
teaching_info *teach = NULL;
|
||||
attrib *a;
|
||||
|
@ -194,7 +195,7 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
|
|||
n = 30 * student->number;
|
||||
a = a_find(student->attribs, &at_learning);
|
||||
if (a != NULL) {
|
||||
teach = (teaching_info *) a->data.v;
|
||||
teach = (teaching_info *)a->data.v;
|
||||
n -= teach->value;
|
||||
}
|
||||
|
||||
|
@ -207,8 +208,9 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
|
|||
|
||||
if (teach == NULL) {
|
||||
a = a_add(&student->attribs, a_new(&at_learning));
|
||||
teach = (teaching_info *) a->data.v;
|
||||
} else {
|
||||
teach = (teaching_info *)a->data.v;
|
||||
}
|
||||
else {
|
||||
while (teach->teachers[index] && index != MAXTEACHERS)
|
||||
++index;
|
||||
}
|
||||
|
@ -380,7 +382,8 @@ int teach_cmd(unit * u, struct order *ord)
|
|||
student = student->next;
|
||||
}
|
||||
#endif
|
||||
} else
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
char zOrder[4096];
|
||||
|
@ -419,7 +422,8 @@ int teach_cmd(unit * u, struct order *ord)
|
|||
sprintf(tbuf, "%s %s", LOC(u->faction->locale,
|
||||
parameters[P_TEMP]), token);
|
||||
uid = tbuf;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
uid = token;
|
||||
}
|
||||
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "unitnotfound_id",
|
||||
|
@ -584,7 +588,7 @@ int learn_cmd(unit * u, order * ord)
|
|||
p = studycost = study_cost(u, sk);
|
||||
a = a_find(u->attribs, &at_learning);
|
||||
if (a != NULL) {
|
||||
teach = (teaching_info *) a->data.v;
|
||||
teach = (teaching_info *)a->data.v;
|
||||
}
|
||||
|
||||
/* keine kostenpflichtigen Talente fuer Migranten. Vertraute sind
|
||||
|
@ -616,7 +620,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
mtyp = M_GRAY;
|
||||
if (!is_mage(u))
|
||||
create_mage(u, mtyp);
|
||||
} else if (!has_skill(u, SK_MAGIC)) {
|
||||
}
|
||||
else if (!has_skill(u, SK_MAGIC)) {
|
||||
int mmax = skill_limit(u->faction, SK_MAGIC);
|
||||
/* Die Einheit ist noch kein Magier */
|
||||
if (count_skill(u->faction, SK_MAGIC) + u->number > mmax) {
|
||||
|
@ -630,7 +635,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
* ausgegangen, dass das normal gelernt werden soll */
|
||||
if (u->faction->magiegebiet != 0) {
|
||||
mtyp = u->faction->magiegebiet;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* Es wurde kein Magiegebiet angegeben und die Partei
|
||||
* hat noch keins gewaehlt. */
|
||||
mtyp = getmagicskill(u->faction->locale);
|
||||
|
@ -646,7 +652,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
if (u->faction->magiegebiet != 0) {
|
||||
cmistake(u, ord, 179, MSG_MAGIC);
|
||||
return 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* Lernt zum ersten mal Magie und legt damit das
|
||||
* Magiegebiet der Partei fest */
|
||||
u->faction->magiegebiet = mtyp;
|
||||
|
@ -654,7 +661,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
}
|
||||
if (!is_mage(u))
|
||||
create_mage(u, mtyp);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
/* ist schon ein Magier und kein Vertrauter */
|
||||
if (u->faction->magiegebiet == 0) {
|
||||
/* die Partei hat noch kein Magiegebiet gewaehlt. */
|
||||
|
@ -698,7 +706,7 @@ int learn_cmd(unit * u, order * ord)
|
|||
|
||||
if (teach == NULL) {
|
||||
a = a_add(&u->attribs, a_new(&at_learning));
|
||||
teach = (teaching_info *) a->data.v;
|
||||
teach = (teaching_info *)a->data.v;
|
||||
teach->teachers[0] = 0;
|
||||
}
|
||||
if (money > 0) {
|
||||
|
@ -750,7 +758,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
if (days >= u->number * 30) {
|
||||
learn_skill(u, sk, 1.0);
|
||||
days -= u->number * 30;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
double chance = (double)days / u->number / 30;
|
||||
learn_skill(u, sk, chance);
|
||||
days = 0;
|
||||
|
@ -798,7 +807,8 @@ int learn_cmd(unit * u, order * ord)
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if (sk == SK_MAGIC) {
|
||||
}
|
||||
else if (sk == SK_MAGIC) {
|
||||
sc_mage *mage = get_mage(u);
|
||||
if (!mage) {
|
||||
mage = create_mage(u, u->faction->magiegebiet);
|
||||
|
|
Loading…
Reference in New Issue