change how message types are created (more prep work for expat)

This commit is contained in:
Enno Rehling 2018-05-18 19:58:49 +02:00
parent f8b8a5284c
commit 19b3b5b35a
30 changed files with 250 additions and 220 deletions

View File

@ -519,7 +519,7 @@ static void report_crtypes(FILE * F, const struct locale *lang)
fputc('\"', F);
fputs(str_escape(nrt_string(kmt->mtype, lang), buffer, sizeof(buffer)), F);
fputs("\";text\n", F);
fprintf(F, "\"%s\";section\n", nrt_section(kmt->mtype));
fprintf(F, "\"%s\";section\n", kmt->mtype->section);
}
while (mtypehash[i]) {
kmt = mtypehash[i];

View File

@ -13,7 +13,7 @@ static void test_add_donation(CuTest *tc) {
region *r;
test_setup();
mt_register(mt_new_va("donation", "from:faction", "to:faction", "amount:int", MT_NEW_END));
mt_create_va(mt_new("donation", NULL), "from:faction", "to:faction", "amount:int", MT_NEW_END);
r = test_create_region(0, 0, NULL);
f1 = test_create_faction(NULL);
f2 = test_create_faction(NULL);

View File

@ -143,10 +143,10 @@ static struct unit *create_recruiter(void) {
static void setup_production(void) {
init_resources();
mt_register(mt_new_va("produce", "unit:unit", "region:region", "amount:int", "wanted:int", "resource:resource", MT_NEW_END));
mt_register(mt_new_va("income", "unit:unit", "region:region", "amount:int", "wanted:int", "mode:int", MT_NEW_END));
mt_register(mt_new_va("buy", "unit:unit", "money:int", MT_NEW_END));
mt_register(mt_new_va("buyamount", "unit:unit", "amount:int", "resource:resource", MT_NEW_END));
mt_create_va(mt_new("produce", NULL), "unit:unit", "region:region", "amount:int", "wanted:int", "resource:resource", MT_NEW_END);
mt_create_va(mt_new("income", NULL), "unit:unit", "region:region", "amount:int", "wanted:int", "mode:int", MT_NEW_END);
mt_create_va(mt_new("buy", NULL), "unit:unit", "money:int", MT_NEW_END);
mt_create_va(mt_new("buyamount", NULL), "unit:unit", "amount:int", "resource:resource", MT_NEW_END);
}
static void test_heroes_dont_recruit(CuTest * tc) {
@ -378,11 +378,11 @@ static void test_tax_cmd(CuTest *tc) {
}
static void setup_economy(void) {
mt_register(mt_new_va("recruit", "unit:unit", "region:region", "amount:int", "want:int", MT_NEW_END));
mt_register(mt_new_va("maintenance", "unit:unit", "building:building", MT_NEW_END));
mt_register(mt_new_va("maintenancefail", "unit:unit", "building:building", MT_NEW_END));
mt_register(mt_new_va("maintenance_nowork", "building:building", MT_NEW_END));
mt_register(mt_new_va("maintenance_noowner", "building:building", MT_NEW_END));
mt_create_va(mt_new("recruit", NULL), "unit:unit", "region:region", "amount:int", "want:int", MT_NEW_END);
mt_create_va(mt_new("maintenance", NULL), "unit:unit", "building:building", MT_NEW_END);
mt_create_va(mt_new("maintenancefail", NULL), "unit:unit", "building:building", MT_NEW_END);
mt_create_va(mt_new("maintenance_nowork", NULL), "building:building", MT_NEW_END);
mt_create_va(mt_new("maintenance_noowner", NULL), "building:building", MT_NEW_END);
}
/**

View File

@ -54,24 +54,24 @@ static void setup_give(struct give *env) {
}
/* success messages: */
mt_register(mt_new_va("receive_person", "unit:unit", "target:unit", "amount:int", MT_NEW_END));
mt_register(mt_new_va("give_person", "unit:unit", "target:unit", "amount:int", MT_NEW_END));
mt_register(mt_new_va("give_person_peasants", "unit:unit", "amount:int", MT_NEW_END));
mt_register(mt_new_va("give_person_ocean", "unit:unit", "amount:int", MT_NEW_END));
mt_register(mt_new_va("receive", "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END));
mt_register(mt_new_va("give", "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END));
mt_register(mt_new_va("give_peasants", "unit:unit", "resource:resource", "amount:int", MT_NEW_END));
mt_create_va(mt_new("receive_person", NULL), "unit:unit", "target:unit", "amount:int", MT_NEW_END);
mt_create_va(mt_new("give_person", NULL), "unit:unit", "target:unit", "amount:int", MT_NEW_END);
mt_create_va(mt_new("give_person_peasants", NULL), "unit:unit", "amount:int", MT_NEW_END);
mt_create_va(mt_new("give_person_ocean", NULL), "unit:unit", "amount:int", MT_NEW_END);
mt_create_va(mt_new("receive", NULL), "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END);
mt_create_va(mt_new("give", NULL), "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END);
mt_create_va(mt_new("give_peasants", NULL), "unit:unit", "resource:resource", "amount:int", MT_NEW_END);
/* error messages: */
mt_register(mt_new_va("too_many_units_in_faction", "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END));
mt_register(mt_new_va("too_many_units_in_alliance", "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END));
mt_register(mt_new_va("feedback_no_contact", "unit:unit", "region:region", "command:order", "target:unit", MT_NEW_END));
mt_register(mt_new_va("feedback_give_forbidden", "unit:unit", "region:region", "command:order", MT_NEW_END));
mt_register(mt_new_va("peasants_give_invalid", "unit:unit", "region:region", "command:order", MT_NEW_END));
mt_register(mt_new_va("giverestriction", "unit:unit", "region:region", "command:order", "turns:int", MT_NEW_END));
mt_register(mt_new_va("error_unit_size", "unit:unit", "region:region", "command:order", "maxsize:int", MT_NEW_END));
mt_register(mt_new_va("nogive_reserved", "unit:unit", "region:region", "command:order", "resource:resource", "reservation:int", MT_NEW_END));
mt_register(mt_new_va("race_notake", "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END));
mt_register(mt_new_va("race_noregroup", "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END));
mt_create_va(mt_new("too_many_units_in_faction", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END);
mt_create_va(mt_new("too_many_units_in_alliance", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END);
mt_create_va(mt_new("feedback_no_contact", NULL), "unit:unit", "region:region", "command:order", "target:unit", MT_NEW_END);
mt_create_va(mt_new("feedback_give_forbidden", NULL), "unit:unit", "region:region", "command:order", MT_NEW_END);
mt_create_va(mt_new("peasants_give_invalid", NULL), "unit:unit", "region:region", "command:order", MT_NEW_END);
mt_create_va(mt_new("giverestriction", NULL), "unit:unit", "region:region", "command:order", "turns:int", MT_NEW_END);
mt_create_va(mt_new("error_unit_size", NULL), "unit:unit", "region:region", "command:order", "maxsize:int", MT_NEW_END);
mt_create_va(mt_new("nogive_reserved", NULL), "unit:unit", "region:region", "command:order", "resource:resource", "reservation:int", MT_NEW_END);
mt_create_va(mt_new("race_notake", NULL), "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END);
mt_create_va(mt_new("race_noregroup", NULL), "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END);
}
static void test_give_unit(CuTest * tc) {

View File

@ -253,7 +253,7 @@ static void test_build_destroy_road(CuTest *tc)
message *m;
test_setup();
mt_register(mt_new_va("destroy_road", "unit:unit", "from:region", "to:region", MT_NEW_END));
mt_create_va(mt_new("destroy_road", NULL), "unit:unit", "from:region", "to:region", MT_NEW_END);
r2 = test_create_region(1, 0, 0);
r = test_create_region(0, 0, NULL);
rsetroad(r, D_EAST, 100);

View File

@ -82,8 +82,8 @@ struct message *msg_feedback(const struct unit *u, struct order *ord,
if (!mtype) {
log_warning("trying to create message of unknown type \"%s\"\n", name);
if (!mt_find("missing_feedback")) {
mt_register(mt_new_va("missing_feedback", "unit:unit",
"region:region", "command:order", "name:string", MT_NEW_END));
mt_create_va(mt_new("missing_feedback", NULL), "unit:unit",
"region:region", "command:order", "name:string", MT_NEW_END);
}
return msg_message("missing_feedback", "name unit region command",
name, u, u->region, ord);
@ -153,7 +153,7 @@ static message *missing_message(const char *name) {
log_warning("trying to create undefined message of type \"%s\"\n", name);
if (strcmp(name, "missing_message") != 0) {
if (!mt_find("missing_message")) {
mt_register(mt_new_va("missing_message", "name:string", MT_NEW_END));
mt_create_va(mt_new("missing_message", NULL), "name:string", MT_NEW_END);
}
return msg_message("missing_message", "name", name);
}

View File

@ -22,10 +22,10 @@ void test_missing_message(CuTest *tc) {
void test_message(CuTest *tc) {
message *msg;
message_type *mtype = mt_new("custom", NULL);
message_type *mtype;
test_setup();
mt_register(mtype);
mtype = mt_create(mt_new("custom", NULL), NULL);
CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom"));
CuAssertIntEquals(tc, 0, mtype->nparameters);
CuAssertPtrEquals(tc, NULL, (void *)mtype->pnames);
@ -47,11 +47,11 @@ void test_message(CuTest *tc) {
static void test_merge_split(CuTest *tc) {
message_list *mlist = 0, *append = 0;
struct mlist **split; /* TODO: why is this a double asterisk? */
message_type *mtype = mt_new("custom", NULL);
message_type *mtype;
message *msg;
test_setup();
mt_register(mtype);
mtype = mt_create(mt_new("custom", NULL), NULL);
add_message(&mlist, msg = msg_message(mtype->name, ""));
msg_release(msg);
add_message(&append, msg = msg_message(mtype->name, ""));

View File

@ -258,7 +258,7 @@ static void test_force_leave_buildings(CuTest *tc) {
building * b;
test_setup();
mt_register(mt_new_va("force_leave_building", "unit:unit", "owner:unit", "building:building", MT_NEW_END));
mt_create_va(mt_new("force_leave_building", NULL), "unit:unit", "owner:unit", "building:building", MT_NEW_END);
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
u1 = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(u1->faction, r);
@ -288,7 +288,7 @@ static void test_force_leave_ships(CuTest *tc) {
ship *sh;
test_setup();
mt_register(mt_new_va("force_leave_ship", "unit:unit", "owner:unit", "ship:ship", MT_NEW_END));
mt_create_va(mt_new("force_leave_ship", NULL), "unit:unit", "owner:unit", "ship:ship", MT_NEW_END);
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
u1 = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(test_create_faction(NULL), r);
@ -876,7 +876,7 @@ static void test_luck_message(CuTest *tc) {
attrib *a;
test_setup();
mt_register(mt_new_va("peasantluck_success", "births:int", MT_NEW_END));
mt_create_va(mt_new("peasantluck_success", NULL), "births:int", MT_NEW_END);
setup_terrains(tc);
r = test_create_region(0, 0, NULL);
rsetpeasants(r, 1);
@ -901,8 +901,8 @@ static unit * setup_name_cmd(void) {
faction *f;
test_setup();
mt_register(mt_new_va("renamed_building_seen", "renamer:unit", "region:region", "building:building", MT_NEW_END));
mt_register(mt_new_va("renamed_building_notseen", "region:region", "building:building", MT_NEW_END));
mt_create_va(mt_new("renamed_building_seen", NULL), "renamer:unit", "region:region", "building:building", MT_NEW_END);
mt_create_va(mt_new("renamed_building_notseen", NULL), "region:region", "building:building", MT_NEW_END);
f = test_create_faction(NULL);
return test_create_unit(f, test_create_region(0, 0, NULL));
}
@ -1301,9 +1301,9 @@ static void test_ally_cmd(CuTest *tc) {
static void test_nmr_warnings(CuTest *tc) {
faction *f1, *f2;
test_setup();
mt_register(mt_new_va("nmr_warning", MT_NEW_END));
mt_register(mt_new_va("nmr_warning_final", MT_NEW_END));
mt_register(mt_new_va("warn_dropout", "faction:faction", "turn:int", MT_NEW_END));
mt_create_va(mt_new("nmr_warning", NULL), MT_NEW_END);
mt_create_va(mt_new("nmr_warning_final", NULL), MT_NEW_END);
mt_create_va(mt_new("warn_dropout", NULL), "faction:faction", "turn:int", MT_NEW_END);
config_set("nmr.timeout", "3");
f1 = test_create_faction(NULL);
f2 = test_create_faction(NULL);
@ -1325,9 +1325,9 @@ static unit * setup_mail_cmd(void) {
faction *f;
test_setup();
mt_register(mt_new_va("regionmessage", "region:region", "sender:unit", "string:string", MT_NEW_END));
mt_register(mt_new_va("unitmessage", "region:region", "sender:unit", "string:string", "unit:unit", MT_NEW_END));
mt_register(mt_new_va("mail_result", "message:string", "unit:unit", MT_NEW_END));
mt_create_va(mt_new("regionmessage", NULL), "region:region", "sender:unit", "string:string", MT_NEW_END);
mt_create_va(mt_new("unitmessage", NULL), "region:region", "sender:unit", "string:string", "unit:unit", MT_NEW_END);
mt_create_va(mt_new("mail_result", NULL), "message:string", "unit:unit", MT_NEW_END);
f = test_create_faction(NULL);
return test_create_unit(f, test_create_region(0, 0, NULL));
}
@ -1444,7 +1444,7 @@ static void test_show_without_item(CuTest *tc)
struct locale *loc;
test_setup();
mt_register(mt_new_va("displayitem", "weight:int", "item:resource", "description:string", MT_NEW_END));
mt_create_va(mt_new("displayitem", NULL), "weight:int", "item:resource", "description:string", MT_NEW_END);
loc = get_or_create_locale("de");
locale_setstring(loc, parameters[P_ANY], "ALLE");
@ -1489,7 +1489,7 @@ static void test_show_race(CuTest *tc) {
test_setup();
mt_register(mt_new_va("msg_event", "string:string", MT_NEW_END));
mt_create_va(mt_new("msg_event", NULL), "string:string", MT_NEW_END);
test_create_race("human");
rc = test_create_race("elf");
@ -1529,8 +1529,8 @@ static void test_show_both(CuTest *tc) {
message * msg;
test_setup();
mt_register(mt_new_va("msg_event", "string:string", MT_NEW_END));
mt_register(mt_new_va("displayitem", "weight:int", "item:resource", "description:string", MT_NEW_END));
mt_create_va(mt_new("msg_event", NULL), "string:string", MT_NEW_END);
mt_create_va(mt_new("displayitem", NULL), "weight:int", "item:resource", "description:string", MT_NEW_END);
rc = test_create_race("elf");
test_create_itemtype("elvenhorse");

View File

@ -33,7 +33,7 @@ static void test_market_curse(CuTest * tc)
building_type *btype;
test_setup();
mt_register(mt_new_va("buyamount", "unit:unit", "amount:int", "resource:resource", MT_NEW_END));
mt_create_va(mt_new("buyamount", NULL), "unit:unit", "amount:int", "resource:resource", MT_NEW_END);
htype = test_create_itemtype("herb");
htype->flags |= ITF_HERB;

View File

@ -51,7 +51,8 @@ static void create_monsters(unit **up, unit **um) {
region *r;
faction *fp, *fm;
mt_register(mt_new_va("dragon_growl", "dragon:unit", "number:int", "target:region", "growl:string", MT_NEW_END));
mt_create_va(mt_new("dragon_growl", NULL),
"dragon:unit", "number:int", "target:region", "growl:string", MT_NEW_END);
test_create_horse();
default_locale = test_create_locale();
fp = test_create_faction(NULL);
@ -223,8 +224,6 @@ static void test_dragon_moves(CuTest * tc)
plan_monsters(m->faction);
CuAssertPtrNotNull(tc, find_order("move east", m));
mt_register(mt_new_va("dragon_growl", "dragon:unit", "number:int", "target:region", "growl:string", MT_NEW_END));
random_growl(m, findregion(1, 0), 3);
msg = test_get_last_message(r->msgs);

View File

@ -28,8 +28,10 @@
#include <assert.h>
static void setup_move(void) {
mt_register(mt_new_va("travel", "unit:unit", "start:region", "end:region", "mode:int", "regions:regions", MT_NEW_END));
mt_register(mt_new_va("moveblocked", "unit:unit", "direction:int", MT_NEW_END));
mt_create_va(mt_new("travel", NULL),
"unit:unit", "start:region", "end:region", "mode:int", "regions:regions", MT_NEW_END);
mt_create_va(mt_new("moveblocked", NULL),
"unit:unit", "direction:int", MT_NEW_END);
}
static void test_ship_not_allowed_in_coast(CuTest * tc)
@ -285,9 +287,12 @@ void setup_drift (struct drift_fixture *fix) {
u_set_ship(fix->u, fix->sh = test_create_ship(fix->u->region, fix->st_boat));
assert(fix->sh);
mt_register(mt_new_va("ship_drift", "ship:ship", "dir:int", MT_NEW_END));
mt_register(mt_new_va("shipsink", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("massive_overload", "ship:ship", MT_NEW_END));
mt_create_va(mt_new("ship_drift", NULL),
"ship:ship", "dir:int", MT_NEW_END);
mt_create_va(mt_new("shipsink", NULL),
"ship:ship", MT_NEW_END);
mt_create_va(mt_new("massive_overload", NULL),
"ship:ship", MT_NEW_END);
}
static void test_ship_no_overload(CuTest *tc) {
@ -482,7 +487,8 @@ static void test_follow_ship_msg(CuTest * tc) {
td->dir = D_NORTHWEST;
td->age = 2;
mt_register(mt_new_va("error18", "unit:unit", "region:region", "command:order", MT_NEW_END));
mt_create_va(mt_new("error18", NULL),
"unit:unit", "region:region", "command:order", MT_NEW_END);
init_order_depr(ord);
getstrtoken();

View File

@ -64,7 +64,7 @@ static void test_faction_password_bad(CuTest *tc) {
const char *orders[] = { "ERESSEA 1 password", NULL };
test_setup();
mt_register(mt_new_va("wrongpasswd", "password:string", MT_NEW_END));
mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END);
f = test_create_faction(NULL);
renumber_faction(f, 1);

View File

@ -30,12 +30,18 @@ static void setup_piracy(void) {
st_boat = test_create_shiptype("boat");
st_boat->cargo = 1000;
mt_register(mt_new_va("piratenovictim", "ship:ship", "unit:unit", "region:region", MT_NEW_END));
mt_register(mt_new_va("piratesawvictim", "ship:ship", "unit:unit", "region:region", "dir:int", MT_NEW_END));
mt_register(mt_new_va("shipsail", "ship:ship", "from:region", "to:region", MT_NEW_END));
mt_register(mt_new_va("shipfly", "ship:ship", "from:region", "to:region", MT_NEW_END));
mt_register(mt_new_va("shipnoshore", "ship:ship", "region:region", MT_NEW_END));
mt_register(mt_new_va("travel", "unit:unit", "start:region", "end:region", "mode:int", "regions:regions", MT_NEW_END));
mt_create_va(mt_new("piratenovictim", NULL),
"ship:ship", "unit:unit", "region:region", MT_NEW_END);
mt_create_va(mt_new("piratesawvictim", NULL),
"ship:ship", "unit:unit", "region:region", "dir:int", MT_NEW_END);
mt_create_va(mt_new("shipsail", NULL),
"ship:ship", "from:region", "to:region", MT_NEW_END);
mt_create_va(mt_new("shipfly", NULL),
"ship:ship", "from:region", "to:region", MT_NEW_END);
mt_create_va(mt_new("shipnoshore", NULL),
"ship:ship", "region:region", MT_NEW_END);
mt_create_va(mt_new("travel", NULL),
"unit:unit", "start:region", "end:region", "mode:int", "regions:regions", MT_NEW_END);
}
static void setup_pirate(unit **pirate, int p_r_flags, int p_rc_flags, const char *p_shiptype,

View File

@ -38,7 +38,7 @@ static void test_renumber_faction_duplicate(CuTest *tc) {
const struct locale *lang;
test_setup_ex(tc);
mt_register(mt_new_va("renumber_inuse", "id:int", MT_NEW_END));
mt_create_va(mt_new("renumber_inuse", NULL), "id:int", MT_NEW_END);
f2 = test_create_faction(NULL);
u = test_create_unit(f = test_create_faction(NULL), test_create_region(0, 0, NULL));
no = f->no;

View File

@ -714,16 +714,17 @@ static void
rp_messages(struct stream *out, message_list * msgs, faction * viewer, int indent,
bool categorized)
{
nrsection *section;
if (!msgs)
int i;
if (!msgs) {
return;
for (section = sections; section; section = section->next) {
}
for (i = 0; i != MAXSECTIONS && sections[i]; ++i) {
const char * section = sections[i];
int k = 0;
struct mlist *m = msgs->begin;
while (m) {
/* messagetype * mt = m->type; */
if (!categorized || strcmp(nrt_section(m->msg->type), section->name) == 0) {
if (!categorized || strcmp(m->msg->type->section, section) == 0) {
char lbuf[8192];
if (!k && categorized) {
@ -731,14 +732,14 @@ rp_messages(struct stream *out, message_list * msgs, faction * viewer, int inden
char cat_identifier[24];
newline(out);
sprintf(cat_identifier, "section_%s", section->name);
sprintf(cat_identifier, "section_%s", section);
section_title = LOC(viewer->locale, cat_identifier);
if (section_title) {
centre(out, section_title, true);
newline(out);
}
else {
log_error("no title defined for section %s in locale %s", section->name, locale_name(viewer->locale));
log_error("no title defined for section %s in locale %s", section, locale_name(viewer->locale));
}
k = 1;
}

View File

@ -130,8 +130,10 @@ static void test_view_reality(CuTest *tc) {
test_setup();
setup_spells();
mt_register(mt_new_va("spell_astral_only", "unit:unit", "region:region", "command:order", MT_NEW_END));
mt_register(mt_new_va("viewreality_effect", "unit:unit", MT_NEW_END));
mt_create_va(mt_new("spell_astral_only", NULL),
"unit:unit", "region:region", "command:order", MT_NEW_END);
mt_create_va(mt_new("viewreality_effect", NULL),
"unit:unit", MT_NEW_END);
r = test_create_region(0, 0, NULL);
ra = test_create_region(real2tp(r->x), real2tp(r->y), NULL);
ra->_plane = get_astralplane();

View File

@ -29,7 +29,8 @@ static void test_flyingship(CuTest * tc)
ship *sh1, *sh2;
test_setup();
mt_register(mt_new_va("flying_ship_result", "mage:unit", "ship:ship", MT_NEW_END));
mt_create_va(mt_new("flying_ship_result", NULL),
"mage:unit", "ship:ship", MT_NEW_END);
par.param = &par_data_ptr;
par_data.typ = SPP_SHIP;

View File

@ -29,7 +29,8 @@ static void test_magicresistance_unit(CuTest *tc) {
curse *c;
test_setup();
mt_register(mt_new_va("magicresistance_unit", "unit:unit", "id:int", MT_NEW_END));
mt_create_va(mt_new("magicresistance_unit", NULL),
"unit:unit", "id:int", MT_NEW_END);
r = test_create_plain(0, 0);
f1 = test_create_faction(NULL);
u1 = test_create_unit(f1, r);
@ -57,7 +58,8 @@ static void test_magicresistance_building(CuTest *tc) {
curse *c;
test_setup();
mt_register(mt_new_va("magicresistance_building", "building:building", "id:int", MT_NEW_END));
mt_create_va(mt_new("magicresistance_building", NULL),
"building:building", "id:int", MT_NEW_END);
r = test_create_plain(0, 0);
f1 = test_create_faction(NULL);
u1 = test_create_unit(f1, r);

View File

@ -33,19 +33,32 @@ typedef struct {
} spy_fixture;
static void setup_spy(spy_fixture *fix) {
mt_register(mt_new_va("spyreport", "spy:unit", "target:unit", "status:int", MT_NEW_END));
mt_register(mt_new_va("spyreport_mage", "spy:unit", "target:unit", "type:int", MT_NEW_END));
mt_register(mt_new_va("spyreport_faction", "spy:unit", "target:unit", "faction:faction", MT_NEW_END));
mt_register(mt_new_va("spyreport_skills", "spy:unit", "target:unit", "skills:string", MT_NEW_END));
mt_register(mt_new_va("spyreport_items", "spy:unit", "target:unit", "items:items", MT_NEW_END));
mt_register(mt_new_va("destroy_ship_0", "unit:unit", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("destroy_ship_1", "unit:unit", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("destroy_ship_2", "unit:unit", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("destroy_ship_3", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("destroy_ship_4", "ship:ship", MT_NEW_END));
mt_register(mt_new_va("sink_msg", "ship:ship", "region:region", MT_NEW_END));
mt_register(mt_new_va("sink_lost_msg", "unit:unit", "region:region", "dead:int", MT_NEW_END));
mt_register(mt_new_va("sink_saved_msg", "unit:unit", "region:region", MT_NEW_END));
mt_create_va(mt_new("spyreport", NULL),
"spy:unit", "target:unit", "status:int", MT_NEW_END);
mt_create_va(mt_new("spyreport_mage", NULL),
"spy:unit", "target:unit", "type:int", MT_NEW_END);
mt_create_va(mt_new("spyreport_faction", NULL),
"spy:unit", "target:unit", "faction:faction", MT_NEW_END);
mt_create_va(mt_new("spyreport_skills", NULL),
"spy:unit", "target:unit", "skills:string", MT_NEW_END);
mt_create_va(mt_new("spyreport_items", NULL),
"spy:unit", "target:unit", "items:items", MT_NEW_END);
mt_create_va(mt_new("destroy_ship_0", NULL),
"unit:unit", "ship:ship", MT_NEW_END);
mt_create_va(mt_new("destroy_ship_1", NULL),
"unit:unit", "ship:ship", MT_NEW_END);
mt_create_va(mt_new("destroy_ship_2", NULL),
"unit:unit", "ship:ship", MT_NEW_END);
mt_create_va(mt_new("destroy_ship_3", NULL),
"ship:ship", MT_NEW_END);
mt_create_va(mt_new("destroy_ship_4", NULL),
"ship:ship", MT_NEW_END);
mt_create_va(mt_new("sink_msg", NULL),
"ship:ship", "region:region", MT_NEW_END);
mt_create_va(mt_new("sink_lost_msg", NULL),
"unit:unit", "region:region", "dead:int", MT_NEW_END);
mt_create_va(mt_new("sink_saved_msg", NULL),
"unit:unit", "region:region", MT_NEW_END);
if (fix) {
fix->r = test_create_region(0, 0, NULL);

View File

@ -58,9 +58,12 @@ typedef struct {
} study_fixture;
static void setup_study(void) {
mt_register(mt_new_va("studycost", "unit:unit", "region:region", "cost:int", "skill:int", MT_NEW_END));
mt_register(mt_new_va("teach_teacher", "teacher:unit", "student:unit", "skill:int", "level:int", MT_NEW_END));
mt_register(mt_new_va("teach_student", "teacher:unit", "student:unit", "skill:int", MT_NEW_END));
mt_create_va(mt_new("studycost", NULL),
"unit:unit", "region:region", "cost:int", "skill:int", MT_NEW_END);
mt_create_va(mt_new("teach_teacher", NULL),
"teacher:unit", "student:unit", "skill:int", "level:int", MT_NEW_END);
mt_create_va(mt_new("teach_student", NULL),
"teacher:unit", "student:unit", "skill:int", MT_NEW_END);
}
static void setup_locale(struct locale *lang) {
@ -218,7 +221,8 @@ static void test_academy_building(CuTest *tc) {
message * msg;
test_setup();
mt_register(mt_new_va("teach_asgood", "unit:unit", "region:region", "command:order", "student:unit", MT_NEW_END));
mt_create_va(mt_new("teach_asgood", NULL),
"unit:unit", "region:region", "command:order", "student:unit", MT_NEW_END);
random_source_inject_constant(0.0);
init_resources();
@ -636,8 +640,10 @@ static void test_teach_message(CuTest *tc) {
teaching_info *teach;
test_setup();
mt_register(mt_new_va("teach_teacher", "teacher:unit", "student:unit", "skill:int", "level:int", MT_NEW_END));
mt_register(mt_new_va("teach_student", "teacher:unit", "student:unit", "skill:int", MT_NEW_END));
mt_create_va(mt_new("teach_teacher", NULL),
"teacher:unit", "student:unit", "skill:int", "level:int", MT_NEW_END);
mt_create_va(mt_new("teach_student", NULL),
"teacher:unit", "student:unit", "skill:int", MT_NEW_END);
init_resources();
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL));
scale_number(u, 20);

View File

@ -246,9 +246,12 @@ static void test_reset(void) {
}
random_source_reset();
mt_register(mt_new_va("changepasswd", "value:string", MT_NEW_END));
mt_register(mt_new_va("starvation", "unit:unit", "region:region", "dead:int", "live:int", MT_NEW_END));
mt_register(mt_new_va("malnourish", "unit:unit", "region:region", MT_NEW_END));
mt_create_va(mt_new("changepasswd", NULL),
"value:string", MT_NEW_END);
mt_create_va(mt_new("starvation", NULL),
"unit:unit", "region:region", "dead:int", "live:int", MT_NEW_END);
mt_create_va(mt_new("malnourish", NULL),
"unit:unit", "region:region", MT_NEW_END);
if (errno) {
int error = errno;

View File

@ -12,7 +12,8 @@
#include <CuTest.h>
static void shock_setup(void) {
mt_register(mt_new_va("shock", "mage:unit", "reason:string", MT_NEW_END));
mt_create_va(mt_new("shock", NULL),
"mage:unit", "reason:string", MT_NEW_END);
}
static void test_shock(CuTest *tc) {

View File

@ -60,26 +60,34 @@ arg_type *find_argtype(const char *name)
return NULL;
}
message_type *mt_new(const char *name, const char *args[])
static unsigned int mt_id(const message_type * mtype)
{
unsigned int key = 0;
size_t i = strlen(mtype->name);
while (i > 0) {
/* TODO: why not use str_hash? */
key = (mtype->name[--i] + key * 37);
}
return key % 0x7FFFFFFF;
}
#define MT_MAXHASH 1021
static selist *messagetypes[MT_MAXHASH];
message_type *mt_create(message_type * mtype, const char *args[])
{
int nparameters = 0;
message_type *mtype;
unsigned int hash = str_hash(mtype->name) % MT_MAXHASH;
selist **qlp = messagetypes + hash;
assert(name != NULL);
if (name == NULL) {
log_error("Trying to create message_type with name=0x0\n");
return NULL;
}
if (args != NULL) {
/* count the number of parameters */
while (args[nparameters]) ++nparameters;
}
mtype = (message_type *)malloc(sizeof(message_type));
mtype->key = 0;
mtype->name = str_strdup(name);
mtype->nparameters = nparameters;
if (nparameters > 0) {
int i;
mtype->nparameters = nparameters;
mtype->pnames = (char **)malloc(sizeof(char *) * nparameters);
mtype->types = (arg_type **)malloc(sizeof(arg_type *) * nparameters);
for (i = 0; args[i]; ++i) {
@ -103,20 +111,38 @@ message_type *mt_new(const char *name, const char *args[])
}
}
}
else {
mtype->pnames = NULL;
mtype->types = NULL;
if (selist_set_insert(qlp, mtype, NULL)) {
mtype->key = mt_id(mtype);
}
return mtype;
}
message_type *mt_new_va(const char *name, ...)
message_type *mt_new(const char *name, const char *section)
{
message_type *mtype;
assert(name != NULL);
if (name == NULL) {
log_error("Trying to create message_type with name=0x0\n");
return NULL;
}
mtype = (message_type *)malloc(sizeof(message_type));
mtype->key = 0;
mtype->name = str_strdup(name);
mtype->section = section;
mtype->nparameters = 0;
mtype->pnames = NULL;
mtype->types = NULL;
return mtype;
}
message_type *mt_create_va(message_type *mtype, ...)
{
const char *args[16];
int i = 0;
va_list marker;
va_start(marker, name);
va_start(marker, mtype);
for (;;) {
const char *c = va_arg(marker, const char *);
args[i++] = c;
@ -124,8 +150,8 @@ message_type *mt_new_va(const char *name, ...)
break;
}
va_end(marker);
args[i] = 0;
return mt_new(name, args);
args[i] = NULL;
return mt_create(mtype, args);
}
static variant copy_arg(const arg_type * atype, variant data)
@ -165,9 +191,6 @@ message *msg_create(const struct message_type *mtype, variant args[])
return msg;
}
#define MT_MAXHASH 1021
static selist *messagetypes[MT_MAXHASH];
static void mt_free(void *val) {
message_type *mtype = (message_type *)val;
int i;
@ -205,29 +228,6 @@ const message_type *mt_find(const char *name)
return 0;
}
static unsigned int mt_id(const message_type * mtype)
{
unsigned int key = 0;
size_t i = strlen(mtype->name);
while (i > 0) {
/* TODO: why not use str_hash? */
key = (mtype->name[--i] + key * 37);
}
return key % 0x7FFFFFFF;
}
const message_type *mt_register(message_type * type)
{
unsigned int hash = str_hash(type->name) % MT_MAXHASH;
selist **qlp = messagetypes + hash;
if (selist_set_insert(qlp, type, NULL)) {
type->key = mt_id(type);
}
return type;
}
void msg_free(message * msg)
{
int i;

View File

@ -29,6 +29,7 @@ extern "C" {
typedef struct message_type {
unsigned int key;
char *name;
const char *section;
int nparameters;
char **pnames;
struct arg_type ** types;
@ -43,8 +44,6 @@ extern "C" {
void message_done(void);
void mt_clear(void);
struct message_type *mt_new(const char *name, const char *args[]);
struct message_type *mt_new_va(const char *name, ...);
#define MT_NEW_END ((const char *)0)
/* mt_new("simple_sentence", "subject:string", "predicate:string",
* "object:string", "lang:locale", MT_NEW_END); */
@ -59,8 +58,10 @@ extern "C" {
const char *mt_name(const struct message_type *mtype);
struct message_type *mt_new(const char *name, const char *section);
/** message_type registry (optional): **/
const struct message_type *mt_register(struct message_type *);
struct message_type *mt_create(struct message_type *, const char *args[]);
struct message_type *mt_create_va(struct message_type *, ...);
const struct message_type *mt_find(const char *);
void register_argtype(const char *name, void(*free_arg) (variant),

View File

@ -8,8 +8,7 @@ static void test_mt_new(CuTest *tc)
{
message_type *mt;
test_setup();
mt = mt_new_va("test", "name:string", "number:int", MT_NEW_END);
mt_register(mt);
mt = mt_create_va(mt_new("test", NULL), "name:string", "number:int", MT_NEW_END);
CuAssertPtrNotNull(tc, mt);
CuAssertStrEquals(tc, "test", mt->name);
CuAssertIntEquals(tc, 2, mt->nparameters);

View File

@ -30,7 +30,6 @@ typedef struct nrmessage_type {
const struct message_type *mtype;
char *vars;
struct nrmessage_type *next;
const char *section;
} nrmessage_type;
#define NRT_MAXHASH 1021
@ -66,41 +65,44 @@ static nrmessage_type *nrt_find(const struct message_type * mtype)
return found;
}
nrsection *sections;
char *sections[MAXSECTIONS];
const nrsection *section_find(const char *name)
const char *section_find(const char *name)
{
nrsection **mcp = &sections;
if (name == NULL)
int i;
if (name == NULL) {
return NULL;
for (; *mcp; mcp = &(*mcp)->next) {
nrsection *mc = *mcp;
if (!strcmp(mc->name, name))
break;
}
return *mcp;
for (i = 0; i != MAXSECTIONS && sections[i]; ++i) {
if (strcmp(sections[i], name) == 0) {
return sections[i];
}
}
return NULL;
}
const nrsection *section_add(const char *name)
{
nrsection **mcp = &sections;
if (name == NULL)
const char *section_add(const char *name) {
int i;
if (name == NULL) {
return NULL;
for (; *mcp; mcp = &(*mcp)->next) {
nrsection *mc = *mcp;
if (!strcmp(mc->name, name))
break;
}
if (!*mcp) {
nrsection *mc = calloc(sizeof(nrsection), 1);
mc->name = str_strdup(name);
*mcp = mc;
for (i = 0; i != MAXSECTIONS && sections[i]; ++i) {
if (strcmp(sections[i], name) == 0) {
return sections[i];
}
return *mcp;
}
assert(i < MAXSECTIONS);
assert(sections[i] == NULL);
if (i + 1 < MAXSECTIONS) {
sections[i + 1] = NULL;
}
return sections[i] = str_strdup(name);
}
void
nrt_register(const struct message_type *mtype, const char *section)
nrt_register(const struct message_type *mtype)
{
unsigned int hash = mtype->key % NRT_MAXHASH;
nrmessage_type *nrt = nrtypes[hash];
@ -118,15 +120,6 @@ nrt_register(const struct message_type *mtype, const char *section)
nrt = malloc(sizeof(nrmessage_type));
nrt->mtype = mtype;
nrt->next = nrtypes[hash];
if (section) {
const nrsection *s = section_find(section);
if (s == NULL) {
s = section_add(section);
}
nrt->section = s->name;
}
else
nrt->section = NULL;
nrtypes[hash] = nrt;
*c = '\0';
for (i = 0; i != mtype->nparameters; ++i) {
@ -159,12 +152,6 @@ size_t size, const void *userdata)
return 0;
}
const char *nrt_section(const struct message_type * mtype)
{
nrmessage_type *nrt = nrt_find(mtype);
return nrt ? nrt->section : NULL;
}
void free_nrmesssages(void) {
int i;
for (i = 0; i != NRT_MAXHASH; ++i) {

View File

@ -25,19 +25,14 @@ extern "C" {
struct message_type;
struct nrmessage_type;
typedef struct nrsection {
char *name;
struct nrsection *next;
} nrsection;
extern nrsection *sections;
#define MAXSECTIONS 8
extern char *sections[MAXSECTIONS];
void free_nrmesssages(void);
void nrt_register(const struct message_type *mtype, const char *section);
void nrt_register(const struct message_type *mtype);
const char *nrt_string(const struct message_type *mtype,
const struct locale *lang);
const char *nrt_section(const struct message_type *mtype);
size_t nr_render(const struct message *msg, const struct locale *lang,
char *buffer, size_t size, const void *userdata);

View File

@ -20,7 +20,8 @@ static void test_volcano_update(CuTest *tc) {
const struct terrain_type *t_volcano, *t_active;
test_setup();
mt_register(mt_new_va("volcanostopsmoke", "region:region", MT_NEW_END));
mt_create_va(mt_new("volcanostopsmoke", NULL),
"region:region", MT_NEW_END);
t_volcano = test_create_terrain("volcano", LAND_REGION);
t_active = test_create_terrain("activevolcano", LAND_REGION);
r = test_create_region(0, 0, t_active);
@ -42,9 +43,12 @@ static void test_volcano_outbreak(CuTest *tc) {
const struct terrain_type *t_volcano, *t_active;
test_setup();
mt_register(mt_new_va("volcanooutbreak", "regionv:region", "regionn:region", MT_NEW_END));
mt_register(mt_new_va("volcanooutbreaknn", "region:region", MT_NEW_END));
mt_register(mt_new_va("volcano_dead", "unit:unit", "region:region", "dead:int", MT_NEW_END));
mt_create_va(mt_new("volcanooutbreak", NULL),
"regionv:region", "regionn:region", MT_NEW_END);
mt_create_va(mt_new("volcanooutbreaknn", NULL),
"region:region", MT_NEW_END);
mt_create_va(mt_new("volcano_dead", NULL),
"unit:unit", "region:region", "dead:int", MT_NEW_END);
t_volcano = test_create_terrain("volcano", LAND_REGION);
t_active = test_create_terrain("activevolcano", LAND_REGION);
r = test_create_region(0, 0, t_active);

View File

@ -19,10 +19,14 @@ void sort_wormhole_regions(selist *rlist, region **match, int count);
void make_wormholes(region **match, int count, const building_type *bt_wormhole);
static void setup_wormholes(void) {
mt_register(mt_new_va("wormhole_appear", "region:region", MT_NEW_END));
mt_register(mt_new_va("wormhole_dissolve", "region:region", MT_NEW_END));
mt_register(mt_new_va("wormhole_exit", "unit:unit", "region:region", MT_NEW_END));
mt_register(mt_new_va("wormhole_requirements", "unit:unit", "region:region", MT_NEW_END));
mt_create_va(mt_new("wormhole_appear", NULL),
"region:region", MT_NEW_END);
mt_create_va(mt_new("wormhole_dissolve", NULL),
"region:region", MT_NEW_END);
mt_create_va(mt_new("wormhole_exit", NULL),
"unit:unit", "region:region", MT_NEW_END);
mt_create_va(mt_new("wormhole_requirements", NULL),
"unit:unit", "region:region", MT_NEW_END);
}
static void test_make_wormholes(CuTest *tc) {

View File

@ -1304,18 +1304,28 @@ static int parse_messages(xmlDocPtr doc)
xmlXPathFreeObject(result);
/* add the messagetype */
propSection = xmlGetProp(node, BAD_CAST "section");
if (propSection == NULL) {
propSection = BAD_CAST default_section;
}
propValue = xmlGetProp(node, BAD_CAST "name");
mtype = mt_find((const char *)propValue);
if (mtype == NULL) {
mtype = mt_register(mt_new((const char *)propValue, (const char **)argv));
mtype = mt_create(mt_new((const char *)propValue, (const char *)propSection), (const char **)argv);
}
else {
assert(argv != NULL || !"cannot redefine arguments of message now");
}
xmlFree(propValue);
/* register the type for the CR */
if (propSection != BAD_CAST default_section) {
xmlFree(propSection);
}
/* register the type for CR and NR */
crt_register(mtype);
nrt_register(mtype);
/* let's clean up the mess */
if (argv != NULL) {
@ -1323,16 +1333,6 @@ static int parse_messages(xmlDocPtr doc)
free(argv[k]);
free(argv);
}
propSection = xmlGetProp(node, BAD_CAST "section");
if (propSection == NULL) {
propSection = BAD_CAST default_section;
}
nrt_register(mtype, (const char *)propSection);
if (propSection != BAD_CAST default_section) {
xmlFree(propSection);
}
}
xmlXPathFreeObject(messages);