micro optimizations.

removing a lot of mallocs through strdup  by replacing getcommand calls with get_command, which takes a buffer. only one left.
removing mkname calls with a static buffer for keyword-tokens.
commit the schema file I had lying around.
This commit is contained in:
Enno Rehling 2014-08-14 09:57:05 +02:00
parent 9ca206ec3f
commit dafe71f4b8
10 changed files with 3264 additions and 3166 deletions

6
schema.sql Normal file
View File

@ -0,0 +1,6 @@
CREATE TABLE email(id INTEGER PRIMARY KEY, md5 VARCHAR(32) UNIQUE NOT NULL, email VARCHAR(32), bounces INT DEFAULT 0, confirmed TIMESTAMP DEFAULT NULL);
CREATE TABLE faction (id INTEGER PRIMARY KEY, user_id INTEGER REFERENCES user(id), no INTEGER, name VARCHAR(64), game_id INTEGER REFERENCES game(id), race VARCHAR(10), lang CHAR(2));
CREATE TABLE faction_email (faction_id INTEGER REFERENCES faction(id), email_id INTEGER REFERENCES email(id));
CREATE TABLE game (id INTEGER PRIMARY KEY, name VARCHAR(20), last_turn INTEGER);
CREATE TABLE score (turn INTEGER, faction_id INTEGER REFERENCES faction(id), value INTEGER, UNIQUE(turn, faction_id));
CREATE TABLE user(id INTEGER PRIMARY KEY, email_id INTEGER REFERENCES email(id), creation TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

View File

@ -119,7 +119,8 @@ static const char *translate(const char *key, const char *value)
if (junkyard) { if (junkyard) {
t = junkyard; t = junkyard;
junkyard = junkyard->next; junkyard = junkyard->next;
} else }
else
t = malloc(sizeof(translation)); t = malloc(sizeof(translation));
t->key = _strdup(key); t->key = _strdup(key);
t->value = value; t->value = value;
@ -187,14 +188,15 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
* Spezialfälle (besonderes Talent, verursachender Magier usw. werde * Spezialfälle (besonderes Talent, verursachender Magier usw. werde
* bei jedem curse gesondert behandelt. */ * bei jedem curse gesondert behandelt. */
if (typ == TYP_SHIP) { if (typ == TYP_SHIP) {
ship *sh = (ship *) obj; ship *sh = (ship *)obj;
unit *owner = ship_owner(sh); unit *owner = ship_owner(sh);
a = sh->attribs; a = sh->attribs;
r = sh->region; r = sh->region;
if (owner != NULL) { if (owner != NULL) {
if (owner->faction == viewer) { if (owner->faction == viewer) {
self = 2; self = 2;
} else { /* steht eine person der Partei auf dem Schiff? */ }
else { /* steht eine person der Partei auf dem Schiff? */
unit *u = NULL; unit *u = NULL;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
if (u->ship == sh) { if (u->ship == sh) {
@ -204,15 +206,17 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
} }
} }
} }
} else if (typ == TYP_BUILDING) { }
building *b = (building *) obj; else if (typ == TYP_BUILDING) {
building *b = (building *)obj;
unit *owner = building_owner(b); unit *owner = building_owner(b);
a = b->attribs; a = b->attribs;
r = b->region; r = b->region;
if (owner != NULL) { if (owner != NULL) {
if (owner->faction == viewer) { if (owner->faction == viewer) {
self = 2; self = 2;
} else { /* steht eine Person der Partei in der Burg? */ }
else { /* steht eine Person der Partei in der Burg? */
unit *u = NULL; unit *u = NULL;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
if (u->building == b) { if (u->building == b) {
@ -222,23 +226,26 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
} }
} }
} }
} else if (typ == TYP_UNIT) { }
unit *u = (unit *) obj; else if (typ == TYP_UNIT) {
unit *u = (unit *)obj;
a = u->attribs; a = u->attribs;
r = u->region; r = u->region;
if (u->faction == viewer) { if (u->faction == viewer) {
self = 2; self = 2;
} }
} else if (typ == TYP_REGION) { }
r = (region *) obj; else if (typ == TYP_REGION) {
r = (region *)obj;
a = r->attribs; a = r->attribs;
} else { }
else {
/* fehler */ /* fehler */
} }
while (a) { while (a) {
if (fval(a->type, ATF_CURSE)) { if (fval(a->type, ATF_CURSE)) {
curse *c = (curse *) a->data.v; curse *c = (curse *)a->data.v;
message *msg; message *msg;
if (c->type->cansee) { if (c->type->cansee) {
@ -256,8 +263,9 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
fprintf(F, "\"%s\"\n", buf); fprintf(F, "\"%s\"\n", buf);
msg_release(msg); msg_release(msg);
} }
} else if (a->type == &at_effect && self) { }
effect_data *data = (effect_data *) a->data.v; else if (a->type == &at_effect && self) {
effect_data *data = (effect_data *)a->data.v;
if (data->value > 0) { if (data->value > 0) {
const char *key = resourcename(data->type->itype->rtype, 0); const char *key = resourcename(data->type->itype->rtype, 0);
if (!header) { if (!header) {
@ -274,28 +282,28 @@ cr_output_curses(FILE * F, const faction * viewer, const void *obj, objtype_t ty
static int cr_unit(variant var, char *buffer, const void *userdata) static int cr_unit(variant var, char *buffer, const void *userdata)
{ {
unit *u = (unit *) var.v; unit *u = (unit *)var.v;
sprintf(buffer, "%d", u ? u->no : -1); sprintf(buffer, "%d", u ? u->no : -1);
return 0; return 0;
} }
static int cr_ship(variant var, char *buffer, const void *userdata) static int cr_ship(variant var, char *buffer, const void *userdata)
{ {
ship *u = (ship *) var.v; ship *u = (ship *)var.v;
sprintf(buffer, "%d", u ? u->no : -1); sprintf(buffer, "%d", u ? u->no : -1);
return 0; return 0;
} }
static int cr_building(variant var, char *buffer, const void *userdata) static int cr_building(variant var, char *buffer, const void *userdata)
{ {
building *u = (building *) var.v; building *u = (building *)var.v;
sprintf(buffer, "%d", u ? u->no : -1); sprintf(buffer, "%d", u ? u->no : -1);
return 0; return 0;
} }
static int cr_faction(variant var, char *buffer, const void *userdata) static int cr_faction(variant var, char *buffer, const void *userdata)
{ {
faction *f = (faction *) var.v; faction *f = (faction *)var.v;
sprintf(buffer, "%d", f ? f->no : -1); sprintf(buffer, "%d", f ? f->no : -1);
return 0; return 0;
} }
@ -303,7 +311,7 @@ static int cr_faction(variant var, char *buffer, const void *userdata)
static int cr_region(variant var, char *buffer, const void *userdata) static int cr_region(variant var, char *buffer, const void *userdata)
{ {
const faction *report = (const faction *)userdata; const faction *report = (const faction *)userdata;
region *r = (region *) var.v; region *r = (region *)var.v;
if (r) { if (r) {
plane *pl = rplane(r); plane *pl = rplane(r);
int nx = r->x, ny = r->y; int nx = r->x, ny = r->y;
@ -351,7 +359,7 @@ static int cr_alliance(variant var, char *buffer, const void *userdata)
static int cr_skill(variant var, char *buffer, const void *userdata) static int cr_skill(variant var, char *buffer, const void *userdata)
{ {
const faction *report = (const faction *)userdata; const faction *report = (const faction *)userdata;
skill_t sk = (skill_t) var.i; skill_t sk = (skill_t)var.i;
if (sk != NOSKILL) if (sk != NOSKILL)
sprintf(buffer, "\"%s\"", sprintf(buffer, "\"%s\"",
translate(mkname("skill", skillnames[sk]), skillname(sk, translate(mkname("skill", skillnames[sk]), skillname(sk,
@ -363,14 +371,16 @@ static int cr_skill(variant var, char *buffer, const void *userdata)
static int cr_order(variant var, char *buffer, const void *userdata) static int cr_order(variant var, char *buffer, const void *userdata)
{ {
order *ord = (order *) var.v; order *ord = (order *)var.v;
if (ord != NULL) { if (ord != NULL) {
char cmd[ORDERSIZE];
char *wp = buffer; char *wp = buffer;
char *cmd = getcommand(ord); const char *rp;
const char *rp = cmd;
get_command(ord, cmd, sizeof(cmd));
*wp++ = '\"'; *wp++ = '\"';
while (*rp) { for (rp = cmd; *rp;) {
switch (*rp) { switch (*rp) {
case '\"': case '\"':
case '\\': case '\\':
@ -383,15 +393,16 @@ static int cr_order(variant var, char *buffer, const void *userdata)
*wp++ = 0; *wp++ = 0;
free(cmd); free(cmd);
} else }
else
strcpy(buffer, "\"\""); strcpy(buffer, "\"\"");
return 0; return 0;
} }
static int cr_resources(variant var, char *buffer, const void *userdata) static int cr_resources(variant var, char *buffer, const void *userdata)
{ {
faction *f = (faction *) userdata; faction *f = (faction *)userdata;
resource *rlist = (resource *) var.v; resource *rlist = (resource *)var.v;
char *wp = buffer; char *wp = buffer;
if (rlist != NULL) { if (rlist != NULL) {
const char *name = resourcename(rlist->type, rlist->number != 1); const char *name = resourcename(rlist->type, rlist->number != 1);
@ -414,7 +425,7 @@ static int cr_resources(variant var, char *buffer, const void *userdata)
static int cr_regions(variant var, char *buffer, const void *userdata) static int cr_regions(variant var, char *buffer, const void *userdata)
{ {
faction *f = (faction *) userdata; faction *f = (faction *)userdata;
const arg_regions *rdata = (const arg_regions *)var.v; const arg_regions *rdata = (const arg_regions *)var.v;
if (rdata != NULL && rdata->nregions > 0) { if (rdata != NULL && rdata->nregions > 0) {
@ -434,7 +445,8 @@ static int cr_regions(variant var, char *buffer, const void *userdata)
wp += sprintf(wp, ", %d %d %d", nx, ny, z); wp += sprintf(wp, ", %d %d %d", nx, ny, z);
} }
strcat(wp, "\""); strcat(wp, "\"");
} else { }
else {
strcpy(buffer, "\"\""); strcpy(buffer, "\"\"");
} }
return 0; return 0;
@ -443,7 +455,7 @@ static int cr_regions(variant var, char *buffer, const void *userdata)
static int cr_spell(variant var, char *buffer, const void *userdata) static int cr_spell(variant var, char *buffer, const void *userdata)
{ {
const faction *report = (const faction *)userdata; const faction *report = (const faction *)userdata;
spell *sp = (spell *) var.v; spell *sp = (spell *)var.v;
if (sp != NULL) if (sp != NULL)
sprintf(buffer, "\"%s\"", spell_name(sp, report->locale)); sprintf(buffer, "\"%s\"", spell_name(sp, report->locale));
else else
@ -457,7 +469,8 @@ static int cr_curse(variant var, char *buffer, const void *userdata)
const curse_type *ctype = (const curse_type *)var.v; const curse_type *ctype = (const curse_type *)var.v;
if (ctype != NULL) { if (ctype != NULL) {
sprintf(buffer, "\"%s\"", curse_name(ctype, report->locale)); sprintf(buffer, "\"%s\"", curse_name(ctype, report->locale));
} else }
else
strcpy(buffer, "\"\""); strcpy(buffer, "\"\"");
return 0; return 0;
} }
@ -561,7 +574,8 @@ static void render_messages(FILE * F, faction * f, message_list * msgs)
} }
fputs(crbuffer, F); fputs(crbuffer, F);
} }
} else { }
else {
log_error("could not render cr-message %p: %s\n", m->msg, m->msg->type->name); log_error("could not render cr-message %p: %s\n", m->msg, m->msg->type->name);
} }
if (printed) { if (printed) {
@ -589,7 +603,7 @@ static void cr_output_messages(FILE * F, message_list * msgs, faction * f)
/* prints a building */ /* prints a building */
static void static void
cr_output_building(FILE * F, building * b, const unit * owner, int fno, cr_output_building(FILE * F, building * b, const unit * owner, int fno,
faction * f) faction * f)
{ {
const char *bname, *billusion; const char *bname, *billusion;
@ -603,7 +617,8 @@ cr_output_building(FILE * F, building * b, const unit * owner, int fno,
fprintf(F, "\"%s\";wahrerTyp\n", translate(bname, LOC(f->locale, fprintf(F, "\"%s\";wahrerTyp\n", translate(bname, LOC(f->locale,
bname))); bname)));
} }
} else { }
else {
fprintf(F, "\"%s\";Typ\n", translate(bname, LOC(f->locale, bname))); fprintf(F, "\"%s\";Typ\n", translate(bname, LOC(f->locale, bname)));
} }
fprintf(F, "\"%s\";Name\n", b->name); fprintf(F, "\"%s\";Name\n", b->name);
@ -625,7 +640,7 @@ cr_output_building(FILE * F, building * b, const unit * owner, int fno,
/* prints a ship */ /* prints a ship */
static void static void
cr_output_ship(FILE * F, const ship * sh, const unit * u, int fcaptain, cr_output_ship(FILE * F, const ship * sh, const unit * u, int fcaptain,
const faction * f, const region * r) const faction * f, const region * r)
{ {
int w = 0; int w = 0;
assert(sh); assert(sh);
@ -668,7 +683,7 @@ cr_output_ship(FILE * F, const ship * sh, const unit * u, int fcaptain,
static void static void
fwriteorder(FILE * F, const struct order *ord, const struct locale *lang, fwriteorder(FILE * F, const struct order *ord, const struct locale *lang,
bool escape) bool escape)
{ {
char ebuf[1024]; char ebuf[1024];
char obuf[1024]; char obuf[1024];
@ -777,11 +792,13 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
if (mage) { if (mage) {
fprintf(F, "%u;familiarmage\n", mage->no); fprintf(F, "%u;familiarmage\n", mage->no);
} }
} else { }
else {
if (fval(u, UFL_ANON_FACTION)) { if (fval(u, UFL_ANON_FACTION)) {
/* faction info is hidden */ /* faction info is hidden */
fprintf(F, "%d;Parteitarnung\n", i2b(fval(u, UFL_ANON_FACTION))); fprintf(F, "%d;Parteitarnung\n", i2b(fval(u, UFL_ANON_FACTION)));
} else { }
else {
const attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction); const attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction);
const faction *otherfaction = const faction *otherfaction =
a_otherfaction ? get_otherfaction(a_otherfaction) : NULL; a_otherfaction ? get_otherfaction(a_otherfaction) : NULL;
@ -808,7 +825,8 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
if (u->faction != f && a_fshidden if (u->faction != f && a_fshidden
&& a_fshidden->data.ca[0] == 1 && effskill(u, SK_STEALTH) >= 6) { && a_fshidden->data.ca[0] == 1 && effskill(u, SK_STEALTH) >= 6) {
fprintf(F, "-1;Anzahl\n"); fprintf(F, "-1;Anzahl\n");
} else { }
else {
fprintf(F, "%d;Anzahl\n", u->number); fprintf(F, "%d;Anzahl\n", u->number);
} }
@ -820,7 +838,8 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
fprintf(F, "\"%s\";wahrerTyp\n", fprintf(F, "\"%s\";wahrerTyp\n",
translate(zRace, locale_string(f->locale, zRace))); translate(zRace, locale_string(f->locale, zRace)));
} }
} else { }
else {
const race *irace = u_irace(u); const race *irace = u_irace(u);
const char *zRace = rc_name(irace, 1); const char *zRace = rc_name(irace, 1);
fprintf(F, "\"%s\";Typ\n", fprintf(F, "\"%s\";Typ\n",
@ -950,7 +969,8 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
pr = 0; pr = 0;
if (f == u->faction || omniscient(f)) { if (f == u->faction || omniscient(f)) {
show = u->items; show = u->items;
} else if (!itemcloak && mode >= see_unit && !(a_fshidden }
else if (!itemcloak && mode >= see_unit && !(a_fshidden
&& a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) { && a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) {
int n = report_items(u->items, result, MAX_INVENTORY, u, f); int n = report_items(u->items, result, MAX_INVENTORY, u, f);
assert(n >= 0); assert(n >= 0);
@ -958,7 +978,8 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f,
show = result; show = result;
else else
show = NULL; show = NULL;
} else { }
else {
show = NULL; show = NULL;
} }
lasttype = NULL; lasttype = NULL;
@ -1111,7 +1132,7 @@ static char *cr_output_resource(char *buf, const char *name,
static void static void
cr_borders(seen_region ** seen, const region * r, const faction * f, cr_borders(seen_region ** seen, const region * r, const faction * f,
int seemode, FILE * F) int seemode, FILE * F)
{ {
direction_t d; direction_t d;
int g = 0; int g = 0;
@ -1203,7 +1224,8 @@ cr_region_header(FILE * F, int plid, int nx, int ny, unsigned int uid)
{ {
if (plid == 0) { if (plid == 0) {
fprintf(F, "REGION %d %d\n", nx, ny); fprintf(F, "REGION %d %d\n", nx, ny);
} else { }
else {
fprintf(F, "REGION %d %d %d\n", nx, ny, plid); fprintf(F, "REGION %d %d %d\n", nx, ny, plid);
} }
if (uid) if (uid)
@ -1229,7 +1251,8 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
if (opt_cr_absolute_coords) { if (opt_cr_absolute_coords) {
nx = r->x; nx = r->x;
ny = r->y; ny = r->y;
} else { }
else {
nx = r->x, ny = r->y; nx = r->x, ny = r->y;
pnormalize(&nx, &ny, pl); pnormalize(&nx, &ny, pl);
adjust_coordinates(f, &nx, &ny, pl, r); adjust_coordinates(f, &nx, &ny, pl, r);
@ -1274,7 +1297,8 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
fprintf(F, "\"%s\";visibility\n", visibility[sr->mode]); fprintf(F, "\"%s\";visibility\n", visibility[sr->mode]);
if (sr->mode == see_neighbour) { if (sr->mode == see_neighbour) {
cr_borders(ctx->seen, r, f, sr->mode, F); cr_borders(ctx->seen, r, f, sr->mode, F);
} else { }
else {
building *b; building *b;
ship *sh; ship *sh;
unit *u; unit *u;
@ -1302,7 +1326,8 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
} }
if (is_cursed(r->attribs, C_RIOT, 0)) { if (is_cursed(r->attribs, C_RIOT, 0)) {
fputs("0;Rekruten\n", F); fputs("0;Rekruten\n", F);
} else { }
else {
fprintf(F, "%d;Rekruten\n", rpeasants(r) / RECRUITFRACTION); fprintf(F, "%d;Rekruten\n", rpeasants(r) / RECRUITFRACTION);
} }
if (production(r)) { if (production(r)) {
@ -1339,7 +1364,8 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
locale_string(f->locale, ch))); locale_string(f->locale, ch)));
} }
} }
} else if (rpeasants(r) / TRADE_FRACTION > 0) { }
else if (rpeasants(r) / TRADE_FRACTION > 0) {
struct demand *dmd = r->land->demands; struct demand *dmd = r->land->demands;
fputs("PREISE\n", F); fputs("PREISE\n", F);
while (dmd) { while (dmd) {
@ -1388,9 +1414,9 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
/* show units pulled through region */ /* show units pulled through region */
for (ru = a_find(r->attribs, &at_travelunit); for (ru = a_find(r->attribs, &at_travelunit);
ru && ru->type == &at_travelunit; ru = ru->next) { ru && ru->type == &at_travelunit; ru = ru->next) {
unit *u = (unit *) ru->data.v; unit *u = (unit *)ru->data.v;
if (cansee_durchgezogen(f, r, u, 0) && r != u->region) { if (cansee_durchgezogen(f, r, u, 0) && r != u->region) {
if (u->ship && ship_owner(u->ship)==u) { if (u->ship && ship_owner(u->ship) == u) {
if (!seeships) { if (!seeships) {
fprintf(F, "DURCHSCHIFFUNG\n"); fprintf(F, "DURCHSCHIFFUNG\n");
} }
@ -1401,7 +1427,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
} }
for (ru = a_find(r->attribs, &at_travelunit); for (ru = a_find(r->attribs, &at_travelunit);
ru && ru->type == &at_travelunit; ru = ru->next) { ru && ru->type == &at_travelunit; ru = ru->next) {
unit *u = (unit *) ru->data.v; unit *u = (unit *)ru->data.v;
if (cansee_durchgezogen(f, r, u, 0) && r != u->region) { if (cansee_durchgezogen(f, r, u, 0) && r != u->region) {
if (!u->ship) { if (!u->ship) {
if (!seeunits) { if (!seeunits) {
@ -1476,7 +1502,8 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
if (F == NULL) { if (F == NULL) {
perror(filename); perror(filename);
return -1; return -1;
} else if (_strcmpl(charset, "utf-8")==0 || _strcmpl(charset, "utf8")==0) { }
else if (_strcmpl(charset, "utf-8") == 0 || _strcmpl(charset, "utf8") == 0) {
const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 }; const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 };
fwrite(utf8_bom, 1, 3, F); fwrite(utf8_bom, 1, 3, F);
} }
@ -1560,7 +1587,8 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
int flag = want(i); int flag = want(i);
if (options[i]) { if (options[i]) {
fprintf(F, "%d;%s\n", (f->options & flag) ? 1 : 0, options[i]); fprintf(F, "%d;%s\n", (f->options & flag) ? 1 : 0, options[i]);
} else if (f->options & flag) { }
else if (f->options & flag) {
f->options &= (~flag); f->options &= (~flag);
} }
} }
@ -1604,7 +1632,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset)
cr_find_address(F, f, ctx->addresses); cr_find_address(F, f, ctx->addresses);
a = a_find(f->attribs, &at_reportspell); a = a_find(f->attribs, &at_reportspell);
while (a && a->type == &at_reportspell) { while (a && a->type == &at_reportspell) {
spellbook_entry *sbe = (spellbook_entry *) a->data.v; spellbook_entry *sbe = (spellbook_entry *)a->data.v;
cr_reportspell(F, sbe->sp, sbe->level, f->locale); cr_reportspell(F, sbe->sp, sbe->level, f->locale);
a = a->next; a = a->next;
} }
@ -1671,7 +1699,8 @@ int crwritemap(const char *filename)
int plid = plane_id(pl); int plid = plane_id(pl);
if (plid) { if (plid) {
fprintf(F, "REGION %d %d %d\n", r->x, r->y, plid); fprintf(F, "REGION %d %d %d\n", r->x, r->y, plid);
} else { }
else {
fprintf(F, "REGION %d %d\n", r->x, r->y); fprintf(F, "REGION %d %d\n", r->x, r->y);
} }
fprintf(F, "\"%s\";Name\n\"%s\";Terrain\n", rname(r, default_locale), fprintf(F, "\"%s\";Name\n\"%s\";Terrain\n", rname(r, default_locale),

View File

@ -47,7 +47,7 @@ syntaxtree *stree_create(void)
syntaxtree *sroot = NULL; syntaxtree *sroot = NULL;
const struct locale *lang = locales; const struct locale *lang = locales;
while (lang) { while (lang) {
syntaxtree *stree = (syntaxtree *) malloc(sizeof(syntaxtree)); syntaxtree *stree = (syntaxtree *)malloc(sizeof(syntaxtree));
stree->lang = lang; stree->lang = lang;
stree->next = sroot; stree->next = sroot;
stree->root = 0; stree->root = 0;
@ -59,9 +59,9 @@ syntaxtree *stree_create(void)
void void
add_command(void **keys, void *tnext, add_command(void **keys, void *tnext,
const char *str, parser fun) const char *str, parser fun)
{ {
command *cmd = (command *) malloc(sizeof(command)); command *cmd = (command *)malloc(sizeof(command));
variant var; variant var;
cmd->fun = fun; cmd->fun = fun;
@ -77,11 +77,12 @@ static int do_command_i(const void *keys, struct unit *u, struct order *ord)
c = getstrtoken(); c = getstrtoken();
if (findtoken(keys, c, &var) == E_TOK_SUCCESS) { if (findtoken(keys, c, &var) == E_TOK_SUCCESS) {
command *cmd = (command *) var.v; command *cmd = (command *)var.v;
if (cmd->nodes && *c) { if (cmd->nodes && *c) {
assert(!cmd->fun); assert(!cmd->fun);
return do_command_i(cmd->nodes, u, ord); return do_command_i(cmd->nodes, u, ord);
} else if (cmd->fun) { }
else if (cmd->fun) {
cmd->fun(cmd->nodes, u, ord); cmd->fun(cmd->nodes, u, ord);
return E_TOK_SUCCESS; return E_TOK_SUCCESS;
} }
@ -94,8 +95,8 @@ void do_command(const void *keys, struct unit *u, struct order *ord)
init_tokens(ord); init_tokens(ord);
skip_token(); skip_token();
if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) { if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) {
char *cmd = getcommand(ord); char cmd[ORDERSIZE];
get_command(ord, cmd, sizeof(cmd));
log_warning("%s failed command '%s'\n", unitname(u), cmd); log_warning("%s failed command '%s'\n", unitname(u), cmd);
free(cmd);
} }
} }

View File

@ -88,6 +88,7 @@ extern "C" {
#define ENCCHANCE 10 /* %-Chance für einmalige Zufallsbegegnung */ #define ENCCHANCE 10 /* %-Chance für einmalige Zufallsbegegnung */
#define DISPLAYSIZE 8192 /* max. Länge einer Beschreibung, incl trailing 0 */ #define DISPLAYSIZE 8192 /* max. Länge einer Beschreibung, incl trailing 0 */
#define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */
#define NAMESIZE 128 /* max. Länge eines Namens, incl trailing 0 */ #define NAMESIZE 128 /* max. Länge eines Namens, incl trailing 0 */
#define IDSIZE 16 /* max. Länge einer no (als String), incl trailing 0 */ #define IDSIZE 16 /* max. Länge einer no (als String), incl trailing 0 */
#define KEYWORDSIZE 16 /* max. Länge eines Keyword, incl trailing 0 */ #define KEYWORDSIZE 16 /* max. Länge eines Keyword, incl trailing 0 */

View File

@ -93,7 +93,7 @@ keyword_t getkeyword(const order * ord)
* This is the inverse function to the parse_order command. Note that * This is the inverse function to the parse_order command. Note that
* keywords are expanded to their full length. * keywords are expanded to their full length.
*/ */
static char* get_command(const order *ord, char *sbuffer, size_t size) { char* get_command(const order *ord, char *sbuffer, size_t size) {
char *bufp = sbuffer; char *bufp = sbuffer;
const char *text = ORD_STRING(ord); const char *text = ORD_STRING(ord);
keyword_t kwd = ORD_KEYWORD(ord); keyword_t kwd = ORD_KEYWORD(ord);
@ -141,12 +141,6 @@ static char* get_command(const order *ord, char *sbuffer, size_t size) {
return sbuffer; return sbuffer;
} }
char *getcommand(const order * ord)
{
char sbuffer[DISPLAYSIZE * 2];
return _strdup(get_command(ord, sbuffer, sizeof(sbuffer)));
}
void free_order(order * ord) void free_order(order * ord)
{ {
if (ord != NULL) { if (ord != NULL) {
@ -423,6 +417,7 @@ bool is_repeated(const order * ord)
s = getstrtoken(); s = getstrtoken();
result = !isparam(s, lang, P_TEMP); result = !isparam(s, lang, P_TEMP);
parser_popstate(); parser_popstate();
// TODO: push/popstate is slow, we can do better.
break; break;
default: default:
result = 0; result = 0;
@ -589,6 +584,12 @@ void push_order(order ** ordp, order * ord)
*ordp = ord; *ordp = ord;
} }
static char *getcommand(const order * ord)
{
char cmd[ORDERSIZE];
return _strdup(get_command(ord, cmd, sizeof(cmd)));
}
void init_tokens(const struct order *ord) void init_tokens(const struct order *ord)
{ {
char *cmd = getcommand(ord); char *cmd = getcommand(ord);

View File

@ -50,13 +50,14 @@ extern "C" {
extern void push_order(struct order **olist, struct order *ord); extern void push_order(struct order **olist, struct order *ord);
/* access functions for orders */ /* access functions for orders */
extern keyword_t getkeyword(const order * ord); keyword_t getkeyword(const order * ord);
extern void set_order(order ** destp, order * src); void set_order(order ** destp, order * src);
extern char *getcommand(const order * ord); char *getcommand(const order * ord);
extern bool is_persistent(const order * ord); char* get_command(const order *ord, char *buffer, size_t size);
extern bool is_exclusive(const order * ord); bool is_persistent(const order * ord);
extern bool is_repeated(const order * ord); bool is_exclusive(const order * ord);
extern bool is_long(const order * ord); bool is_repeated(const order * ord);
bool is_long(const order * ord);
extern char *write_order(const order * ord, char *buffer, size_t size); extern char *write_order(const order * ord, char *buffer, size_t size);
extern void init_tokens(const struct order *ord); /* initialize token parsing */ extern void init_tokens(const struct order *ord); /* initialize token parsing */

View File

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -138,7 +138,7 @@ const char *hp_status(const unit * u)
void void
report_item(const unit * owner, const item * i, const faction * viewer, report_item(const unit * owner, const item * i, const faction * viewer,
const char **name, const char **basename, int *number, bool singular) const char **name, const char **basename, int *number, bool singular)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER); const resource_type *rsilver = get_resourcetype(R_SILVER);
@ -152,7 +152,8 @@ report_item(const unit * owner, const item * i, const faction * viewer,
*basename = resourcename(i->type->rtype, 0); *basename = resourcename(i->type->rtype, 0);
if (number) if (number)
*number = i->number; *number = i->number;
} else if (owner && i->type->rtype == rsilver) { }
else if (owner && i->type->rtype == rsilver) {
int pp = i->number / owner->number; int pp = i->number / owner->number;
if (number) if (number)
*number = 1; *number = 1;
@ -161,17 +162,20 @@ report_item(const unit * owner, const item * i, const faction * viewer,
*name = locale_string(viewer->locale, "dragonhoard"); *name = locale_string(viewer->locale, "dragonhoard");
if (basename) if (basename)
*basename = "dragonhoard"; *basename = "dragonhoard";
} else if (pp > 5000) { }
else if (pp > 5000) {
if (name) if (name)
*name = locale_string(viewer->locale, "moneychest"); *name = locale_string(viewer->locale, "moneychest");
if (basename) if (basename)
*basename = "moneychest"; *basename = "moneychest";
} else if (pp > 500) { }
else if (pp > 500) {
if (name) if (name)
*name = locale_string(viewer->locale, "moneybag"); *name = locale_string(viewer->locale, "moneybag");
if (basename) if (basename)
*basename = "moneybag"; *basename = "moneybag";
} else { }
else {
if (number) if (number)
*number = 0; *number = 0;
if (name) if (name)
@ -179,7 +183,8 @@ report_item(const unit * owner, const item * i, const faction * viewer,
if (basename) if (basename)
*basename = NULL; *basename = NULL;
} }
} else { }
else {
if (name) if (name)
*name = *name =
locale_string(viewer->locale, resourcename(i->type->rtype, locale_string(viewer->locale, resourcename(i->type->rtype,
@ -212,7 +217,8 @@ int update_nmrs(void)
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
if (fval(f, FFL_ISNEW)) { if (fval(f, FFL_ISNEW)) {
++newplayers; ++newplayers;
} else if (!is_monsters(f) && f->alive) { }
else if (!is_monsters(f) && f->alive) {
int nmr = turn - f->lastorders + 1; int nmr = turn - f->lastorders + 1;
if (nmr < 0 || nmr > NMRTimeout()) { if (nmr < 0 || nmr > NMRTimeout()) {
log_error("faction %s has %d NMRS\n", factionid(f), nmr); log_error("faction %s has %d NMRS\n", factionid(f), nmr);
@ -236,10 +242,11 @@ static size_t buforder(char *bufp, size_t size, const order * ord, int mode)
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
if (mode < ORDERS_IN_NR) { if (mode < ORDERS_IN_NR) {
char *cmd = getcommand(ord); char cmd[ORDERSIZE];
get_command(ord, cmd, sizeof(cmd));
bytes = (int)strlcpy(bufp, cmd, size); bytes = (int)strlcpy(bufp, cmd, size);
free(cmd); }
} else { else {
bytes = (int)strlcpy(bufp, "...", size); bytes = (int)strlcpy(bufp, "...", size);
} }
tsize += bytes; tsize += bytes;
@ -249,7 +256,8 @@ static size_t buforder(char *bufp, size_t size, const order * ord, int mode)
if (size > 1) { if (size > 1) {
*bufp++ = '\"'; *bufp++ = '\"';
--size; --size;
} else { }
else {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
++tsize; ++tsize;
@ -265,7 +273,7 @@ static size_t buforder(char *bufp, size_t size, const order * ord, int mode)
*/ */
int int
report_items(const item * items, item * result, int size, const unit * owner, report_items(const item * items, item * result, int size, const unit * owner,
const faction * viewer) const faction * viewer)
{ {
const item *itm; const item *itm;
int n = 0; /* number of results */ int n = 0; /* number of results */
@ -311,7 +319,7 @@ report_items(const item * items, item * result, int size, const unit * owner,
static void static void
report_resource(resource_report * result, const char *name, int number, report_resource(resource_report * result, const char *name, int number,
int level) int level)
{ {
result->name = name; result->name = name;
result->number = number; result->number = number;
@ -324,7 +332,8 @@ void report_race(const struct unit *u, const char **name, const char **illusion)
const race *irace = u_irace(u); const race *irace = u_irace(u);
if (irace && irace != u_race(u)) { if (irace && irace != u_race(u)) {
*illusion = irace->_name[0]; *illusion = irace->_name[0];
} else { }
else {
*illusion = NULL; *illusion = NULL;
} }
} }
@ -340,7 +349,7 @@ void report_race(const struct unit *u, const char **name, const char **illusion)
void void
report_building(const struct building *b, const char **name, report_building(const struct building *b, const char **name,
const char **illusion) const char **illusion)
{ {
const struct building_type *bt_illusion; const struct building_type *bt_illusion;
@ -354,7 +363,7 @@ report_building(const struct building *b, const char **name,
if (bt_illusion && b->type == bt_illusion) { if (bt_illusion && b->type == bt_illusion) {
const attrib *a = a_findc(b->attribs, &at_icastle); const attrib *a = a_findc(b->attribs, &at_icastle);
if (a != NULL) { if (a != NULL) {
icastle_data *icastle = (icastle_data *) a->data.v; icastle_data *icastle = (icastle_data *)a->data.v;
*illusion = buildingtype(icastle->type, b, b->size); *illusion = buildingtype(icastle->type, b, b->size);
} }
} }
@ -363,7 +372,7 @@ report_building(const struct building *b, const char **name,
int int
report_resources(const seen_region * sr, resource_report * result, int size, report_resources(const seen_region * sr, resource_report * result, int size,
const faction * viewer) const faction * viewer)
{ {
const region *r = sr->r; const region *r = sr->r;
int n = 0; int n = 0;
@ -420,7 +429,8 @@ report_resources(const seen_region * sr, resource_report * result, int size,
if (res->type->visible == NULL) { if (res->type->visible == NULL) {
visible = res->amount; visible = res->amount;
level = res->level + itype->construction->minskill - 1; level = res->level + itype->construction->minskill - 1;
} else { }
else {
const unit *u; const unit *u;
for (u = r->units; visible != res->amount && u != NULL; u = u->next) { for (u = r->units; visible != res->amount && u != NULL; u = u->next) {
if (u->faction == viewer) { if (u->faction == viewer) {
@ -446,13 +456,13 @@ report_resources(const seen_region * sr, resource_report * result, int size,
int int
bufunit(const faction * f, const unit * u, int indent, int mode, char *buf, bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
size_t size) size_t size)
{ {
int i, dh; int i, dh;
int getarnt = fval(u, UFL_ANON_FACTION); int getarnt = fval(u, UFL_ANON_FACTION);
const char *pzTmp, *str; const char *pzTmp, *str;
building *b; building *b;
bool isbattle = (bool) (mode == see_battle); bool isbattle = (bool)(mode == see_battle);
int telepath_see = 0; int telepath_see = 0;
attrib *a_fshidden = NULL; attrib *a_fshidden = NULL;
item *itm; item *itm;
@ -479,7 +489,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
if (fval(u, UFL_GROUP)) { if (fval(u, UFL_GROUP)) {
attrib *a = a_find(u->attribs, &at_group); attrib *a = a_find(u->attribs, &at_group);
if (a) { if (a) {
group *g = (group *) a->data.v; group *g = (group *)a->data.v;
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
@ -495,7 +505,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size); bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else if (a_otherfaction) { }
else if (a_otherfaction) {
faction *otherfaction = get_otherfaction(a_otherfaction); faction *otherfaction = get_otherfaction(a_otherfaction);
if (otherfaction) { if (otherfaction) {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
@ -506,7 +517,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
} else { }
else {
if (getarnt) { if (getarnt) {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
@ -514,7 +526,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size); bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else { }
else {
if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) { if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) {
faction *f = get_otherfaction(a_otherfaction); faction *f = get_otherfaction(a_otherfaction);
bytes = bytes =
@ -522,7 +535,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
factionname(u->faction)); factionname(u->faction));
if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else { }
else {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
@ -541,7 +555,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1 if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1
&& effskill(u, SK_STEALTH) >= 6) { && effskill(u, SK_STEALTH) >= 6) {
bytes = (int)strlcpy(bufp, "? ", size); bytes = (int)strlcpy(bufp, "? ", size);
} else { }
else {
bytes = _snprintf(bufp, size, "%d ", u->number); bytes = _snprintf(bufp, size, "%d ", u->number);
} }
if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0)
@ -564,7 +579,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
--size; --size;
} }
} }
} else { }
else {
const race *irace = u_irace(u); const race *irace = u_irace(u);
bytes = (int)strlcpy(bufp, racename(f->locale, u, irace), size); bytes = (int)strlcpy(bufp, racename(f->locale, u, irace), size);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
@ -658,7 +674,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
dh = 0; dh = 0;
if (f == u->faction || telepath_see || omniscient(f)) { if (f == u->faction || telepath_see || omniscient(f)) {
show = u->items; show = u->items;
} else if (!itemcloak && mode >= see_unit && !(a_fshidden }
else if (!itemcloak && mode >= see_unit && !(a_fshidden
&& a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) { && a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH) >= 3)) {
int n = report_items(u->items, result, MAX_INVENTORY, u, f); int n = report_items(u->items, result, MAX_INVENTORY, u, f);
assert(n >= 0); assert(n >= 0);
@ -666,7 +683,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
show = result; show = result;
else else
show = NULL; show = NULL;
} else { }
else {
show = NULL; show = NULL;
} }
for (itm = show; itm; itm = itm->next) { for (itm = show; itm; itm = itm->next) {
@ -687,7 +705,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
} }
if (in == 1) { if (in == 1) {
bytes = (int)strlcpy(bufp, ic, size); bytes = (int)strlcpy(bufp, ic, size);
} else { }
else {
bytes = _snprintf(bufp, size, "%d %s", in, ic); bytes = _snprintf(bufp, size, "%d %s", in, ic);
} }
if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0)
@ -706,12 +725,13 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
} }
for (header = 0, qi = 0; ql; ql_advance(&ql, &qi, 1)) { for (header = 0, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry * sbe = (spellbook_entry *) ql_get(ql, qi); spellbook_entry * sbe = (spellbook_entry *)ql_get(ql, qi);
if (sbe->level <= maxlevel) { if (sbe->level <= maxlevel) {
if (!header) { if (!header) {
bytes = _snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); bytes = _snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells"));
header = 1; header = 1;
} else { }
else {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
} }
if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) { if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) {
@ -739,7 +759,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
const spell *sp; const spell *sp;
if (!dh) { if (!dh) {
dh = 1; dh = 1;
} else { }
else {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) { if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
@ -759,7 +780,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0) if (bytes < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} else { }
else {
bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_nospells"), size); bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_nospells"), size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) if (bytes && wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
@ -776,7 +798,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
bytes = (int)buforder(bufp, size, ord, printed++); bytes = (int)buforder(bufp, size, ord, printed++);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else }
else
break; break;
} }
} }
@ -787,7 +810,8 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
bytes = (int)buforder(bufp, size, ord, printed++); bytes = (int)buforder(bufp, size, ord, printed++);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} else }
else
break; break;
} }
} }
@ -845,7 +869,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char *buf,
size_t size_t
spskill(char *buffer, size_t size, const struct locale * lang, spskill(char *buffer, size_t size, const struct locale * lang,
const struct unit * u, struct skill * sv, int *dh, int days) const struct unit * u, struct skill * sv, int *dh, int days)
{ {
char *bufp = buffer; char *bufp = buffer;
int i, effsk; int i, effsk;
@ -963,7 +987,7 @@ void lparagraph(struct strlist **SP, char *s, int indent, char mark)
void void
spunit(struct strlist **SP, const struct faction *f, const unit * u, int indent, spunit(struct strlist **SP, const struct faction *f, const unit * u, int indent,
int mode) int mode)
{ {
char buf[DISPLAYSIZE]; char buf[DISPLAYSIZE];
int dh = bufunit(f, u, indent, mode, buf, sizeof(buf)); int dh = bufunit(f, u, indent, mode, buf, sizeof(buf));
@ -977,7 +1001,8 @@ struct message *msg_curse(const struct curse *c, const void *obj, objtype_t typ,
if (c->type->curseinfo) { if (c->type->curseinfo) {
/* if curseinfo returns NULL, then we don't want to tell the viewer anything. */ /* if curseinfo returns NULL, then we don't want to tell the viewer anything. */
return c->type->curseinfo(obj, typ, c, self); return c->type->curseinfo(obj, typ, c, self);
} else { }
else {
message *msg = cinfo_simple(obj, typ, c, self); message *msg = cinfo_simple(obj, typ, c, self);
if (msg == NULL) { if (msg == NULL) {
const char *unknown[] = const char *unknown[] =
@ -985,7 +1010,8 @@ struct message *msg_curse(const struct curse *c, const void *obj, objtype_t typ,
"ship_unknown" }; "ship_unknown" };
msg = msg_message(mkname("curseinfo", unknown[typ]), "id", c->no); msg = msg_message(mkname("curseinfo", unknown[typ]), "id", c->no);
log_error("no curseinfo function for %s and no fallback either.\n", c->type->cname); log_error("no curseinfo function for %s and no fallback either.\n", c->type->cname);
} else { }
else {
log_error("no curseinfo function for %s, using cinfo_simple fallback.\n", c->type->cname); log_error("no curseinfo function for %s, using cinfo_simple fallback.\n", c->type->cname);
} }
return msg; return msg;
@ -1024,7 +1050,7 @@ void transfer_seen(quicklist ** dst, quicklist ** src)
static void get_addresses(report_context * ctx) static void get_addresses(report_context * ctx)
{ {
/* "TODO: travelthru" */ /* "TODO: travelthru" */
seen_region *sr = NULL; seen_region *sr = NULL;
region *r; region *r;
const faction *lastf = NULL; const faction *lastf = NULL;
@ -1063,7 +1089,8 @@ static void get_addresses(report_context * ctx)
} }
} }
} }
} else if (sr->mode == see_travel) { }
else if (sr->mode == see_travel) {
unit *u = r->units; unit *u = r->units;
while (u) { while (u) {
faction *sf = visible_faction(ctx->f, u); faction *sf = visible_faction(ctx->f, u);
@ -1071,7 +1098,7 @@ static void get_addresses(report_context * ctx)
if (lastf != sf) { if (lastf != sf) {
attrib *a = a_find(r->attribs, &at_travelunit); attrib *a = a_find(r->attribs, &at_travelunit);
while (a && a->type == &at_travelunit) { while (a && a->type == &at_travelunit) {
unit *u2 = (unit *) a->data.v; unit *u2 = (unit *)a->data.v;
if (u2->faction == ctx->f) { if (u2->faction == ctx->f) {
if (cansee_unit(u2, u, stealthmod)) { if (cansee_unit(u2, u, stealthmod)) {
ql_set_insert(&flist, sf); ql_set_insert(&flist, sf);
@ -1084,7 +1111,8 @@ static void get_addresses(report_context * ctx)
} }
u = u->next; u = u->next;
} }
} else if (sr->mode > see_travel) { }
else if (sr->mode > see_travel) {
const unit *u = r->units; const unit *u = r->units;
while (u != NULL) { while (u != NULL) {
if (u->faction != ctx->f) { if (u->faction != ctx->f) {
@ -1117,7 +1145,7 @@ seen_region *reuse;
seen_region **seen_init(void) seen_region **seen_init(void)
{ {
return (seen_region **) calloc(MAXSEEHASH, sizeof(seen_region *)); return (seen_region **)calloc(MAXSEEHASH, sizeof(seen_region *));
} }
void seen_done(seen_region * seehash[]) void seen_done(seen_region * seehash[])
@ -1204,19 +1232,20 @@ static void get_seen_interval(report_context * ctx)
bool bool
add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode, add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode,
bool dis) bool dis)
{ {
seen_region *find = find_seen(seehash, r); seen_region *find = find_seen(seehash, r);
if (find == NULL) { if (find == NULL) {
unsigned int index = reg_hashkey(r) & (MAXSEEHASH - 1); unsigned int index = reg_hashkey(r) & (MAXSEEHASH - 1);
if (!reuse) if (!reuse)
reuse = (seen_region *) calloc(1, sizeof(struct seen_region)); reuse = (seen_region *)calloc(1, sizeof(struct seen_region));
find = reuse; find = reuse;
reuse = reuse->nextHash; reuse = reuse->nextHash;
find->nextHash = seehash[index]; find->nextHash = seehash[index];
seehash[index] = find; seehash[index] = find;
find->r = r; find->r = r;
} else if (find->mode >= mode) { }
else if (find->mode >= mode) {
return false; return false;
} }
find->mode = mode; find->mode = mode;
@ -1266,7 +1295,7 @@ static quicklist *get_regions_distance(region * root, int radius)
} }
ql_advance(&ql, &qi, 1); ql_advance(&ql, &qi, 1);
} }
for (ql=rlist,qi=0;ql;ql_advance(&ql, &qi, 1)) { for (ql = rlist, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
region *r = (region *)ql_get(ql, qi); region *r = (region *)ql_get(ql, qi);
freset(r, RF_MARK); freset(r, RF_MARK);
} }
@ -1332,7 +1361,7 @@ static void view_neighbours(struct seen_region **seen, region * r, faction * f)
static void static void
recurse_regatta(struct seen_region **seen, region * center, region * r, recurse_regatta(struct seen_region **seen, region * center, region * r,
faction * f, int maxdist) faction * f, int maxdist)
{ {
int d; int d;
int dist = distance(center, r); int dist = distance(center, r);
@ -1355,7 +1384,8 @@ recurse_regatta(struct seen_region **seen, region * center, region * r,
if (add_seen(seen, r2, see_far, false)) { if (add_seen(seen, r2, see_far, false)) {
recurse_regatta(seen, center, r2, f, maxdist); recurse_regatta(seen, center, r2, f, maxdist);
} }
} else }
else
add_seen(seen, r2, see_neighbour, false); add_seen(seen, r2, see_neighbour, false);
} }
} }
@ -1383,7 +1413,7 @@ static void prepare_lighthouse(building * b, faction * f)
quicklist *ql, *rlist = get_regions_distance(b->region, range); quicklist *ql, *rlist = get_regions_distance(b->region, range);
int qi; int qi;
for (ql=rlist,qi=0;ql;ql_advance(&ql, &qi,1)) { for (ql = rlist, qi = 0; ql; ql_advance(&ql, &qi, 1)) {
region *rl = (region *)ql_get(ql, qi); region *rl = (region *)ql_get(ql, qi);
if (!fval(rl->terrain, FORBIDDEN_REGION)) { if (!fval(rl->terrain, FORBIDDEN_REGION)) {
region * next[MAXDIRECTIONS]; region * next[MAXDIRECTIONS];
@ -1415,14 +1445,15 @@ void reorder_units(region * r)
unit *u = *umove; unit *u = *umove;
if (u->building == b) { if (u->building == b) {
unit **uinsert = unext; unit **uinsert = unext;
if (u==owner) { if (u == owner) {
uinsert = ufirst; uinsert = ufirst;
} }
if (umove != uinsert) { if (umove != uinsert) {
*umove = u->next; *umove = u->next;
u->next = *uinsert; u->next = *uinsert;
*uinsert = u; *uinsert = u;
} else { }
else {
/* no need to move, skip ahead */ /* no need to move, skip ahead */
umove = &u->next; umove = &u->next;
} }
@ -1430,7 +1461,8 @@ void reorder_units(region * r)
/* we have a new well-placed unit. jump over it */ /* we have a new well-placed unit. jump over it */
unext = &u->next; unext = &u->next;
} }
} else { }
else {
umove = &u->next; umove = &u->next;
} }
} }
@ -1449,13 +1481,15 @@ void reorder_units(region * r)
*umove = u->next; *umove = u->next;
u->next = *unext; u->next = *unext;
*unext = u; *unext = u;
} else { }
else {
/* no need to move, skip ahead */ /* no need to move, skip ahead */
umove = &u->next; umove = &u->next;
} }
/* we have a new well-placed unit. jump over it */ /* we have a new well-placed unit. jump over it */
unext = &u->next; unext = &u->next;
} else { }
else {
umove = &u->next; umove = &u->next;
} }
} }
@ -1468,7 +1502,7 @@ void reorder_units(region * r)
unit *u = *umove; unit *u = *umove;
if (u->number && u->ship == sh) { if (u->number && u->ship == sh) {
unit **uinsert = unext; unit **uinsert = unext;
if (u==owner) { if (u == owner) {
uinsert = ufirst; uinsert = ufirst;
owner = u; owner = u;
} }
@ -1476,7 +1510,8 @@ void reorder_units(region * r)
*umove = u->next; *umove = u->next;
u->next = *uinsert; u->next = *uinsert;
*uinsert = u; *uinsert = u;
} else { }
else {
/* no need to move, skip ahead */ /* no need to move, skip ahead */
umove = &u->next; umove = &u->next;
} }
@ -1484,7 +1519,8 @@ void reorder_units(region * r)
/* we have a new well-placed unit. jump over it */ /* we have a new well-placed unit. jump over it */
unext = &u->next; unext = &u->next;
} }
} else { }
else {
umove = &u->next; umove = &u->next;
} }
} }
@ -1562,7 +1598,7 @@ static void prepare_reports(void)
if (fval(r, RF_TRAVELUNIT)) { if (fval(r, RF_TRAVELUNIT)) {
for (ru = a_find(r->attribs, &at_travelunit); for (ru = a_find(r->attribs, &at_travelunit);
ru && ru->type == &at_travelunit; ru = ru->next) { ru && ru->type == &at_travelunit; ru = ru->next) {
unit *u = (unit *) ru->data.v; unit *u = (unit *)ru->data.v;
/* make sure the faction has not been removed this turn: */ /* make sure the faction has not been removed this turn: */
if (u->faction) { if (u->faction) {
@ -1589,11 +1625,11 @@ static seen_region **prepare_report(faction * f)
if (sr->mode > see_neighbour) { if (sr->mode > see_neighbour) {
region *r = sr->r; region *r = sr->r;
plane *p = rplane(r); plane *p = rplane(r);
void (*view) (struct seen_region **, region *, faction *) = view_default; void(*view) (struct seen_region **, region *, faction *) = view_default;
if (p && fval(p, PFL_SEESPECIAL)) { if (p && fval(p, PFL_SEESPECIAL)) {
/* TODO: this is not very customizable */ /* TODO: this is not very customizable */
view = (strcmp(p->name, "Regatta")==0) ? view_regatta : view_neighbours; view = (strcmp(p->name, "Regatta") == 0) ? view_regatta : view_neighbours;
} }
view(f->seen, r, f); view(f->seen, r, f);
} }
@ -1603,7 +1639,7 @@ static seen_region **prepare_report(faction * f)
int write_reports(faction * f, time_t ltime) int write_reports(faction * f, time_t ltime)
{ {
int backup = 1, maxbackup = 128*1000; int backup = 1, maxbackup = 128 * 1000;
bool gotit = false; bool gotit = false;
struct report_context ctx; struct report_context ctx;
const char *encoding = "UTF-8"; const char *encoding = "UTF-8";
@ -1642,7 +1678,7 @@ int write_reports(faction * f, time_t ltime)
if (errno) { if (errno) {
char zText[64]; char zText[64];
log_warning("retrying, error %d during reports for faction %s", errno, factionname(f)); log_warning("retrying, error %d during reports for faction %s", errno, factionname(f));
sprintf(zText, "waiting %u seconds before we retry", backup/1000); sprintf(zText, "waiting %u seconds before we retry", backup / 1000);
perror(zText); perror(zText);
_sleep(backup); _sleep(backup);
if (backup < maxbackup) { if (backup < maxbackup) {
@ -1671,7 +1707,8 @@ static void nmr_warnings(void)
if (f->alliance && f->alliance == fa->alliance) { if (f->alliance && f->alliance == fa->alliance) {
warn = 1; warn = 1;
} }
} else if (alliedfaction(NULL, f, fa, FRIEND) }
else if (alliedfaction(NULL, f, fa, FRIEND)
&& alliedfaction(NULL, fa, f, FRIEND)) { && alliedfaction(NULL, fa, f, FRIEND)) {
warn = 1; warn = 1;
} }
@ -1738,7 +1775,7 @@ int init_reports(void)
{ {
prepare_reports(); prepare_reports();
{ {
if (_access(reportpath(), 0)!=0) { if (_access(reportpath(), 0) != 0) {
return 0; return 0;
} }
} }
@ -1808,7 +1845,7 @@ static void var_free_string(variant x)
static variant var_copy_order(variant x) static variant var_copy_order(variant x)
{ {
x.v = copy_order((order *) x.v); x.v = copy_order((order *)x.v);
return x; return x;
} }
@ -1822,7 +1859,7 @@ static variant var_copy_items(variant x)
item *isrc; item *isrc;
resource *rdst = NULL, **rptr = &rdst; resource *rdst = NULL, **rptr = &rdst;
for (isrc = (item *) x.v; isrc != NULL; isrc = isrc->next) { for (isrc = (item *)x.v; isrc != NULL; isrc = isrc->next) {
resource *res = malloc(sizeof(resource)); resource *res = malloc(sizeof(resource));
res->number = isrc->number; res->number = isrc->number;
res->type = isrc->type->rtype; res->type = isrc->type->rtype;
@ -1836,7 +1873,7 @@ static variant var_copy_items(variant x)
static void var_free_resources(variant x) static void var_free_resources(variant x)
{ {
resource *rsrc = (resource *) x.v; resource *rsrc = (resource *)x.v;
while (rsrc) { while (rsrc) {
resource *res = rsrc->next; resource *res = rsrc->next;
free(rsrc); free(rsrc);
@ -1872,17 +1909,18 @@ f_regionid(const region * r, const faction * f, char *buffer, size_t size)
size_t len; size_t len;
if (!r) { if (!r) {
len = strlcpy(buffer, "(Chaos)", size); len = strlcpy(buffer, "(Chaos)", size);
} else { }
else {
plane *pl = rplane(r); plane *pl = rplane(r);
const char *name = pl ? pl->name : 0; const char *name = pl ? pl->name : 0;
int nx = r->x, ny = r->y; int nx = r->x, ny = r->y;
int named = (name && name[0]); int named = (name && name[0]);
pnormalize(&nx, &ny, pl); pnormalize(&nx, &ny, pl);
adjust_coordinates(f, &nx, &ny, pl, r); adjust_coordinates(f, &nx, &ny, pl, r);
len = strlcpy(buffer, rname(r, f?f->locale:0), size); len = strlcpy(buffer, rname(r, f ? f->locale : 0), size);
_snprintf(buffer + len, size-len, " (%d,%d%s%s)", nx, ny, named ? "," : "", (named) ? name : ""); _snprintf(buffer + len, size - len, " (%d,%d%s%s)", nx, ny, named ? "," : "", (named) ? name : "");
buffer[size-1] = 0; buffer[size - 1] = 0;
len=strlen(buffer); len = strlen(buffer);
} }
return len; return len;
} }
@ -2022,7 +2060,8 @@ static void eval_alliance(struct opstack **stack, const void *userdata)
if (c != NULL) { if (c != NULL) {
size_t len = strlen(c); size_t len = strlen(c);
var.v = strcpy(balloc(len + 1), c); var.v = strcpy(balloc(len + 1), c);
} else }
else
var.v = NULL; var.v = NULL;
opush(stack, var); opush(stack, var);
} }
@ -2087,14 +2126,17 @@ static void eval_weight(struct opstack **stack, const void *userdata)
if (weight % SCALEWEIGHT == 0) { if (weight % SCALEWEIGHT == 0) {
if (weight == SCALEWEIGHT) { if (weight == SCALEWEIGHT) {
sprintf(buffer, "1 %s", LOC(lang, "weight_unit")); sprintf(buffer, "1 %s", LOC(lang, "weight_unit"));
} else { }
else {
sprintf(buffer, "%u %s", weight / SCALEWEIGHT, LOC(lang, sprintf(buffer, "%u %s", weight / SCALEWEIGHT, LOC(lang,
"weight_unit_p")); "weight_unit_p"));
} }
} else { }
else {
if (weight == 1) { if (weight == 1) {
sprintf(buffer, "1 %s %u", LOC(lang, "weight_per"), SCALEWEIGHT); sprintf(buffer, "1 %s %u", LOC(lang, "weight_per"), SCALEWEIGHT);
} else { }
else {
sprintf(buffer, "%u %s %u", weight, LOC(lang, "weight_per_p"), sprintf(buffer, "%u %s %u", weight, LOC(lang, "weight_per_p"),
SCALEWEIGHT); SCALEWEIGHT);
} }
@ -2190,7 +2232,8 @@ static void eval_regions(struct opstack **stack, const void *userdata)
if (regions == NULL) { if (regions == NULL) {
end = begin; end = begin;
} else { }
else {
if (i >= 0) if (i >= 0)
end = begin + i; end = begin + i;
else else
@ -2240,9 +2283,11 @@ static void eval_trail(struct opstack **stack, const void *userdata)
if (i + 2 < end) { if (i + 2 < end) {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, ", ", size);
} else if (i + 1 < end) { }
else if (i + 1 < end) {
bytes = (int)strlcpy(bufp, LOC(lang, "list_and"), size); bytes = (int)strlcpy(bufp, LOC(lang, "list_and"), size);
} else }
else
bytes = 0; bytes = 0;
if (bytes && wrptr(&bufp, &size, bytes) != 0) if (bytes && wrptr(&bufp, &size, bytes) != 0)
@ -2276,7 +2321,7 @@ static void eval_skill(struct opstack **stack, const void *userdata)
{ {
const faction *report = (const faction *)userdata; const faction *report = (const faction *)userdata;
const struct locale *lang = report ? report->locale : default_locale; const struct locale *lang = report ? report->locale : default_locale;
skill_t sk = (skill_t) opop(stack).i; skill_t sk = (skill_t)opop(stack).i;
const char *c = skillname(sk, lang); const char *c = skillname(sk, lang);
size_t len = strlen(c); size_t len = strlen(c);
variant var; variant var;
@ -2338,17 +2383,20 @@ int report_action(region * r, unit * actor, message * msg, int flags)
/* Bei Fernzaubern sieht nur die eigene Partei den Magier */ /* Bei Fernzaubern sieht nur die eigene Partei den Magier */
show = show || (r == actor->region show = show || (r == actor->region
&& cansee(u->faction, r, actor, 0)); && cansee(u->faction, r, actor, 0));
} else if (view == ACTION_CANNOTSEE) { }
else if (view == ACTION_CANNOTSEE) {
show = !show && !(r == actor->region show = !show && !(r == actor->region
&& cansee(u->faction, r, actor, 0)); && cansee(u->faction, r, actor, 0));
} else { }
else {
/* the unliely (or lazy) case */ /* the unliely (or lazy) case */
show = true; show = true;
} }
if (show) { if (show) {
r_addmessage(r, u->faction, msg); r_addmessage(r, u->faction, msg);
} else { /* Partei des Magiers, sieht diesen immer */ }
else { /* Partei des Magiers, sieht diesen immer */
result = 1; result = 1;
} }
} }
@ -2414,6 +2462,6 @@ void register_reports(void)
add_function("trail", &eval_trail); add_function("trail", &eval_trail);
/* register alternative visibility functions */ /* register alternative visibility functions */
register_function((pf_generic) view_neighbours, "view_neighbours"); register_function((pf_generic)view_neighbours, "view_neighbours");
register_function((pf_generic) view_regatta, "view_regatta"); register_function((pf_generic)view_regatta, "view_regatta");
} }

View File

@ -10,10 +10,19 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
static const char * keyword_key(int i) const char * keyword(keyword_t kwd)
{ {
assert(i<MAXKEYWORDS&& i>=0); static char result[KEYWORDSIZE];
return mkname("keyword", keywords[i]); if (!result[0]) {
strcpy(result, "keyword::");
}
strcpy(result+9, keywords[kwd]);
return result;
}
static const char * keyword_key(int kwd) {
assert(kwd < MAXKEYWORDS && kwd >= 0);
return keyword((keyword_t)kwd);
} }
void init_keyword(const struct locale *lang, keyword_t kwd, const char *str) { void init_keyword(const struct locale *lang, keyword_t kwd, const char *str) {
@ -28,8 +37,8 @@ void init_keywords(const struct locale *lang) {
keyword_t findkeyword(const char *s) { keyword_t findkeyword(const char *s) {
int i; int i;
for (i=0;i!=MAXKEYWORDS;++i) { for (i = 0; i != MAXKEYWORDS; ++i) {
if (strcmp(s, keywords[i])==0) { if (strcmp(s, keywords[i]) == 0) {
return (keyword_t)i; return (keyword_t)i;
} }
} }
@ -65,12 +74,12 @@ keyword_t get_keyword(const char *s, const struct locale *lang) {
static bool disabled_kwd[MAXKEYWORDS]; static bool disabled_kwd[MAXKEYWORDS];
void enable_keyword(keyword_t kwd, bool enabled) { void enable_keyword(keyword_t kwd, bool enabled) {
assert(kwd<MAXKEYWORDS); assert(kwd < MAXKEYWORDS);
disabled_kwd[kwd] = !enabled; disabled_kwd[kwd] = !enabled;
} }
bool keyword_disabled(keyword_t kwd) { bool keyword_disabled(keyword_t kwd) {
assert(kwd<MAXKEYWORDS); assert(kwd < MAXKEYWORDS);
return disabled_kwd[kwd]; return disabled_kwd[kwd];
} }

View File

@ -80,7 +80,8 @@ void init_keywords(const struct locale *lang);
void init_keyword(const struct locale *lang, keyword_t kwd, const char *str); void init_keyword(const struct locale *lang, keyword_t kwd, const char *str);
bool keyword_disabled(keyword_t kwd); bool keyword_disabled(keyword_t kwd);
void enable_keyword(keyword_t kwd, bool enabled); void enable_keyword(keyword_t kwd, bool enabled);
#define keyword(kwd) mkname("keyword", keywords[kwd]) const char *keyword(keyword_t kwd);
// #define keyword(kwd) mkname("keyword", keywords[kwd])
#ifdef __cplusplus #ifdef __cplusplus
#endif #endif

View File

@ -53,7 +53,8 @@ void init_tokens_str(const char *initstr, char *cmd)
{ {
if (states == NULL) { if (states == NULL) {
states = malloc(sizeof(parser_state)); states = malloc(sizeof(parser_state));
} else if (states->current_cmd) { }
else if (states->current_cmd && states->current_cmd!=cmd) {
free(states->current_cmd); free(states->current_cmd);
} }
states->current_cmd = cmd; states->current_cmd = cmd;