diff --git a/scripts/tests/orders.lua b/scripts/tests/orders.lua index af0919640..63abaed00 100644 --- a/scripts/tests/orders.lua +++ b/scripts/tests/orders.lua @@ -47,7 +47,8 @@ function test_give() assert_not_equal(5, u2:get_item("money")) end -function test_make_temp() +function disable_test_make_temp() + -- disabled because of TOLUA_ORDERS_CLOSURE u:add_order("MACHE TEMP 123 'Herpderp'") u:add_order("// this comment will be copied") u:add_order("ENDE") diff --git a/src/bind_unit.c b/src/bind_unit.c index 1e1aee00e..00c2b9e09 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -763,6 +763,22 @@ static int tolua_unit_get_spells(lua_State * L) return tolua_selist_push(L, "spellbook", "spell_entry", slist); } +#ifdef TOLUA_ORDERS_CLOSURE +/* TODO: this requires that the locale for write_order is included in the closure */ +static int tolua_orderlist_next(lua_State * L) +{ + order **order_ptr = (order **)lua_touserdata(L, lua_upvalueindex(1)); + order *ord = *order_ptr; + if (ord != NULL) { + char cmd[8192]; + write_order(ord, cmd, sizeof(cmd)); + tolua_pushstring(L, cmd); + *order_ptr = ord->next; + return 1; + } + return 0; +} + static int tolua_unit_get_orders(lua_State * L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); @@ -778,7 +794,7 @@ static int tolua_unit_get_orders(lua_State * L) return 1; } - +#endif static int tolua_unit_get_curse(lua_State *L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); @@ -960,95 +976,96 @@ void tolua_unit_open(lua_State * L) NULL); tolua_beginmodule(L, TOLUA_CAST "event"); { - tolua_function(L, TOLUA_CAST "get_type", &tolua_event_gettype); - tolua_function(L, TOLUA_CAST "get", &tolua_event_get); + tolua_function(L, TOLUA_CAST "get_type", tolua_event_gettype); + tolua_function(L, TOLUA_CAST "get", tolua_event_get); } tolua_endmodule(L); tolua_cclass(L, TOLUA_CAST "unit", TOLUA_CAST "unit", TOLUA_CAST "", NULL); tolua_beginmodule(L, TOLUA_CAST "unit"); { - tolua_function(L, TOLUA_CAST "__tostring", &tolua_unit_tostring); - tolua_function(L, TOLUA_CAST "create", &tolua_unit_create); - tolua_function(L, TOLUA_CAST "destroy", &tolua_unit_destroy); + tolua_function(L, TOLUA_CAST "__tostring", tolua_unit_tostring); + tolua_function(L, TOLUA_CAST "create", tolua_unit_create); + tolua_function(L, TOLUA_CAST "destroy", tolua_unit_destroy); - tolua_variable(L, TOLUA_CAST "name", &tolua_unit_get_name, + tolua_variable(L, TOLUA_CAST "name", tolua_unit_get_name, tolua_unit_set_name); - tolua_variable(L, TOLUA_CAST "faction", &tolua_unit_get_faction, + tolua_variable(L, TOLUA_CAST "faction", tolua_unit_get_faction, tolua_unit_set_faction); tolua_variable(L, TOLUA_CAST "id", tolua_unit_get_id, tolua_unit_set_id); tolua_variable(L, TOLUA_CAST "group", tolua_unit_get_group, tolua_unit_set_group); tolua_variable(L, TOLUA_CAST "info", tolua_unit_get_info, tolua_unit_set_info); - tolua_variable(L, TOLUA_CAST "hp", &tolua_unit_get_hp, tolua_unit_set_hp); - tolua_variable(L, TOLUA_CAST "status", &tolua_unit_get_status, + tolua_variable(L, TOLUA_CAST "hp", tolua_unit_get_hp, tolua_unit_set_hp); + tolua_variable(L, TOLUA_CAST "status", tolua_unit_get_status, tolua_unit_set_status); - tolua_variable(L, TOLUA_CAST "familiar", &tolua_unit_get_familiar, + tolua_variable(L, TOLUA_CAST "familiar", tolua_unit_get_familiar, tolua_unit_set_familiar); - tolua_variable(L, TOLUA_CAST "weight", &tolua_unit_get_weight, 0); - tolua_variable(L, TOLUA_CAST "capacity", &tolua_unit_get_capacity, 0); + tolua_variable(L, TOLUA_CAST "weight", tolua_unit_get_weight, 0); + tolua_variable(L, TOLUA_CAST "capacity", tolua_unit_get_capacity, 0); - tolua_function(L, TOLUA_CAST "add_order", &tolua_unit_add_order); - tolua_function(L, TOLUA_CAST "clear_orders", &tolua_unit_clear_orders); - tolua_variable(L, TOLUA_CAST "orders", &tolua_unit_get_orders, 0); - - tolua_function(L, TOLUA_CAST "get_curse", &tolua_unit_get_curse); - tolua_function(L, TOLUA_CAST "has_attrib", &tolua_unit_has_attrib); + tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order); + tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders); +#ifdef TOLUA_ORDERS_CLOSURE + tolua_variable(L, TOLUA_CAST "orders", tolua_unit_get_orders, 0); +#endif + tolua_function(L, TOLUA_CAST "get_curse", tolua_unit_get_curse); + tolua_function(L, TOLUA_CAST "has_attrib", tolua_unit_has_attrib); /* key-attributes for named flags: */ - tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag); - tolua_function(L, TOLUA_CAST "get_flag", &tolua_unit_get_flag); - tolua_variable(L, TOLUA_CAST "guard", &tolua_unit_get_guard, - &tolua_unit_set_guard); - tolua_variable(L, TOLUA_CAST "flags", &tolua_unit_get_flags, - &tolua_unit_set_flags); - tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age, + tolua_function(L, TOLUA_CAST "set_flag", tolua_unit_set_flag); + tolua_function(L, TOLUA_CAST "get_flag", tolua_unit_get_flag); + tolua_variable(L, TOLUA_CAST "guard", tolua_unit_get_guard, + tolua_unit_set_guard); + tolua_variable(L, TOLUA_CAST "flags", tolua_unit_get_flags, + tolua_unit_set_flags); + tolua_variable(L, TOLUA_CAST "age", tolua_unit_get_age, tolua_unit_set_age); /* items: */ - tolua_function(L, TOLUA_CAST "get_item", &tolua_unit_get_item); - tolua_function(L, TOLUA_CAST "add_item", &tolua_unit_add_item); - tolua_variable(L, TOLUA_CAST "items", &tolua_unit_get_items, 0); - tolua_function(L, TOLUA_CAST "get_pooled", &tolua_unit_get_pooled); - tolua_function(L, TOLUA_CAST "use_pooled", &tolua_unit_use_pooled); + tolua_function(L, TOLUA_CAST "get_item", tolua_unit_get_item); + tolua_function(L, TOLUA_CAST "add_item", tolua_unit_add_item); + tolua_variable(L, TOLUA_CAST "items", tolua_unit_get_items, 0); + tolua_function(L, TOLUA_CAST "get_pooled", tolua_unit_get_pooled); + tolua_function(L, TOLUA_CAST "use_pooled", tolua_unit_use_pooled); /* effects */ - tolua_function(L, TOLUA_CAST "get_potion", &tolua_unit_get_effect); + tolua_function(L, TOLUA_CAST "get_potion", tolua_unit_get_effect); /* skills: */ - tolua_function(L, TOLUA_CAST "get_skill", &tolua_unit_getskill); - tolua_function(L, TOLUA_CAST "eff_skill", &tolua_unit_effskill); - tolua_function(L, TOLUA_CAST "set_skill", &tolua_unit_setskill); + tolua_function(L, TOLUA_CAST "get_skill", tolua_unit_getskill); + tolua_function(L, TOLUA_CAST "eff_skill", tolua_unit_effskill); + tolua_function(L, TOLUA_CAST "set_skill", tolua_unit_setskill); - tolua_function(L, TOLUA_CAST "add_notice", &tolua_unit_addnotice); + tolua_function(L, TOLUA_CAST "add_notice", tolua_unit_addnotice); /* npc logic: */ - tolua_function(L, TOLUA_CAST "add_handler", &tolua_unit_addhandler); + tolua_function(L, TOLUA_CAST "add_handler", tolua_unit_addhandler); - tolua_variable(L, TOLUA_CAST "race_name", &tolua_unit_get_racename, - &tolua_unit_set_racename); - tolua_function(L, TOLUA_CAST "add_spell", &tolua_unit_addspell); - tolua_variable(L, TOLUA_CAST "spells", &tolua_unit_get_spells, 0); - tolua_function(L, TOLUA_CAST "cast_spell", &tolua_unit_castspell); + tolua_variable(L, TOLUA_CAST "race_name", tolua_unit_get_racename, + tolua_unit_set_racename); + tolua_function(L, TOLUA_CAST "add_spell", tolua_unit_addspell); + tolua_variable(L, TOLUA_CAST "spells", tolua_unit_get_spells, 0); + tolua_function(L, TOLUA_CAST "cast_spell", tolua_unit_castspell); - tolua_variable(L, TOLUA_CAST "magic", &tolua_unit_get_magic, + tolua_variable(L, TOLUA_CAST "magic", tolua_unit_get_magic, tolua_unit_set_magic); - tolua_variable(L, TOLUA_CAST "aura", &tolua_unit_get_aura, + tolua_variable(L, TOLUA_CAST "aura", tolua_unit_get_aura, tolua_unit_set_aura); - tolua_variable(L, TOLUA_CAST "building", &tolua_unit_get_building, + tolua_variable(L, TOLUA_CAST "building", tolua_unit_get_building, tolua_unit_set_building); - tolua_variable(L, TOLUA_CAST "ship", &tolua_unit_get_ship, + tolua_variable(L, TOLUA_CAST "ship", tolua_unit_get_ship, tolua_unit_set_ship); - tolua_variable(L, TOLUA_CAST "region", &tolua_unit_get_region, + tolua_variable(L, TOLUA_CAST "region", tolua_unit_get_region, tolua_unit_set_region); - tolua_variable(L, TOLUA_CAST "number", &tolua_unit_get_number, + tolua_variable(L, TOLUA_CAST "number", tolua_unit_get_number, tolua_unit_set_number); - tolua_variable(L, TOLUA_CAST "race", &tolua_unit_get_race, + tolua_variable(L, TOLUA_CAST "race", tolua_unit_get_race, tolua_unit_set_race); - tolua_variable(L, TOLUA_CAST "hp_max", &tolua_unit_get_hpmax, 0); - tolua_variable(L, TOLUA_CAST "aura_max", &tolua_unit_get_auramax, 0); + tolua_variable(L, TOLUA_CAST "hp_max", tolua_unit_get_hpmax, 0); + tolua_variable(L, TOLUA_CAST "aura_max", tolua_unit_get_auramax, 0); - tolua_function(L, TOLUA_CAST "show", &tolua_bufunit); + tolua_function(L, TOLUA_CAST "show", tolua_bufunit); } tolua_endmodule(L); } diff --git a/src/bindings.c b/src/bindings.c index 33d609ee0..7a24a3d07 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -114,20 +114,6 @@ int log_lua_error(lua_State * L) return 1; } -int tolua_orderlist_next(lua_State * L) -{ - order **order_ptr = (order **)lua_touserdata(L, lua_upvalueindex(1)); - order *ord = *order_ptr; - if (ord != NULL) { - char cmd[8192]; - write_order(ord, cmd, sizeof(cmd)); - tolua_pushstring(L, cmd); - *order_ptr = ord->next; - return 1; - } - return 0; -} - static int tolua_selist_iter(lua_State * L) { selist **qlp = (selist **)lua_touserdata(L, lua_upvalueindex(1)); diff --git a/src/bindings.h b/src/bindings.h index 5cb64a038..b5ebf916b 100755 --- a/src/bindings.h +++ b/src/bindings.h @@ -22,7 +22,6 @@ extern "C" { int tolua_sqlite_open(struct lua_State *L); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_itemlist_next(struct lua_State *L); - int tolua_orderlist_next(struct lua_State *L); int tolua_selist_push(struct lua_State *L, const char *list_type, const char *elem_type, struct selist *list); diff --git a/src/creport.c b/src/creport.c index c26d28963..74e32505c 100644 --- a/src/creport.c +++ b/src/creport.c @@ -376,13 +376,13 @@ static int cr_alliance(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 *f = (const faction *)userdata; skill_t sk = (skill_t)var.i; UNUSED_ARG(userdata); if (sk != NOSKILL) sprintf(buffer, "\"%s\"", translate(mkname("skill", skillnames[sk]), skillname(sk, - report->locale))); + f->locale))); else strcpy(buffer, "\"\""); return 0; @@ -391,13 +391,14 @@ static int cr_skill(variant var, char *buffer, const void *userdata) static int cr_order(variant var, char *buffer, const void *userdata) { order *ord = (order *)var.v; - UNUSED_ARG(userdata); + const faction *f = (const faction *)userdata; + if (ord != NULL) { char cmd[ORDERSIZE]; char *wp = buffer; const char *rp; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, f->locale, cmd, sizeof(cmd)); *wp++ = '\"'; for (rp = cmd; *rp;) { @@ -722,11 +723,11 @@ static void cr_output_ship_compat(FILE *F, const ship *sh, const unit *u, cr_output_ship(&strm, sh, u, fcaptain, f, r); } -static int stream_order(stream *out, const struct order *ord) { +static int stream_order(stream *out, const struct order *ord, const struct locale *lang) { const char *str; char ebuf[1025]; char obuf[1024]; - write_order(ord, obuf, sizeof(obuf)); + write_order(ord, lang, obuf, sizeof(obuf)); str = escape_string(obuf, ebuf, sizeof(ebuf)); if (str == ebuf) { ebuf[1024] = 0; @@ -779,6 +780,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, const faction *fother; const char *prefix; bool allied; + const struct locale *lang = f->locale; assert(u && u->number); assert(u->region == r); /* TODO: if this holds true, then why did we pass in r? */ @@ -787,7 +789,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, stream_printf(out, "EINHEIT %d\n", u->no); stream_printf(out, "\"%s\";Name\n", unit_getname(u)); - str = u_description(u, f->locale); + str = u_description(u, lang); if (str) { stream_printf(out, "\"%s\";Beschr\n", str); } @@ -833,7 +835,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, prefix = raceprefix(u); if (prefix) { prefix = mkname("prefix", prefix); - stream_printf(out, "\"%s\";typprefix\n", translate(prefix, LOC(f->locale, + stream_printf(out, "\"%s\";typprefix\n", translate(prefix, LOC(lang, prefix))); } stream_printf(out, "%d;Anzahl\n", u->number); @@ -844,20 +846,20 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { const char *zRace = rc_name_s(u_race(u), NAME_PLURAL); stream_printf(out, "\"%s\";wahrerTyp\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); } } else { const race *irace = u_irace(u); const char *zRace = rc_name_s(irace, NAME_PLURAL); stream_printf(out, "\"%s\";Typ\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); if (u->faction == f && irace != u_race(u)) { assert(skill_enabled(SK_STEALTH) || !"we're resetting this on load, so.. ircase should never be used"); zRace = rc_name_s(u_race(u), NAME_PLURAL); stream_printf(out, "\"%s\";wahrerTyp\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); } } @@ -925,7 +927,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, for (ord = u->old_orders; ord; ord = ord->next) { /* this new order will replace the old defaults */ if (is_persistent(ord)) { - stream_order(out, ord); + stream_order(out, ord, lang); } } for (ord = u->orders; ord; ord = ord->next) { @@ -933,7 +935,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (u->old_orders && is_repeated(kwd)) continue; /* unit has defaults */ if (is_persistent(ord)) { - stream_order(out, ord); + stream_order(out, ord, lang); } } @@ -949,7 +951,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, } stream_printf(out, "%d %d;%s\n", u->number * level_days(sv->level), esk, translate(mkname("skill", skillnames[sk]), skillname(sk, - f->locale))); + lang))); } } @@ -963,8 +965,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, const spell *sp = mage->combatspells[i].sp; if (sp) { const char *name = - translate(mkname("spell", sp->sname), spell_name(sp, - f->locale)); + translate(mkname("spell", sp->sname), spell_name(sp, lang)); stream_printf(out, "KAMPFZAUBER %d\n", i); stream_printf(out, "\"%s\";name\n", name); stream_printf(out, "%d;level\n", mage->combatspells[i].level); @@ -999,7 +1000,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, pr = 1; stream_printf(out, "GEGENSTAENDE\n"); } - stream_printf(out, "%d;%s\n", in, translate(ic, LOC(f->locale, ic))); + stream_printf(out, "%d;%s\n", in, translate(ic, LOC(lang, ic))); } cr_output_curses(out, f, u, TYP_UNIT); diff --git a/src/kernel/command.c b/src/kernel/command.c index 912f21f7d..a6a036fe9 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -14,6 +14,7 @@ #include #include "command.h" +#include #include #include @@ -107,7 +108,7 @@ void do_command(const struct tnode *keys, struct unit *u, struct order *ord) init_order(ord); if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) { char cmd[ORDERSIZE]; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, u->faction->locale, cmd, sizeof(cmd)); log_warning("%s failed command '%s'\n", unitname(u), cmd); } } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index cb79cafc4..5408f8fb3 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -637,7 +637,7 @@ static void test_infinitive_from_config(CuTest *tc) { CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); ord = create_order(K_STUDY, lang, ""); - CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + CuAssertStrEquals(tc, "LERNE", get_command(ord, lang, buffer, sizeof(buffer))); free_order(ord); cJSON_Delete(json); test_cleanup(); diff --git a/src/kernel/order.c b/src/kernel/order.c index bbb211cb3..154b3f451 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -31,13 +31,11 @@ #include # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) -# define OD_LOCALE(odata) ((odata) ? (odata)->lang : NULL) # define OD_STRING(odata) ((odata) ? (odata)->_str : NULL) typedef struct order_data { const char *_str; int _refcount; - const struct locale *lang; } order_data; #include @@ -115,7 +113,7 @@ keyword_t getkeyword(const order * ord) * This is the inverse function to the parse_order command. Note that * keywords are expanded to their full length. */ -char* get_command(const order *ord, char *sbuffer, size_t size) { +char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) { char *bufp = sbuffer; order_data *od; const char * text; @@ -144,7 +142,6 @@ char* get_command(const order *ord, char *sbuffer, size_t size) { od = load_data(ord->id); text = OD_STRING(od); if (kwd != NOKEYWORD) { - const struct locale *lang = OD_LOCALE(od); if (size > 0) { const char *str = (const char *)LOC(lang, keyword(kwd)); assert(str); @@ -217,13 +214,12 @@ void free_orders(order ** olist) } } -static char *mkdata(order_data **pdata, size_t len, const struct locale *lang, const char *str) +static char *mkdata(order_data **pdata, size_t len, const char *str) { order_data *data; char *result; data = malloc(sizeof(order_data) + len + 1); result = (char *)(data + 1); - data->lang = lang; data->_refcount = 0; data->_str = (len > 0) ? result : NULL; if (str) strcpy(result, str); @@ -231,7 +227,7 @@ static char *mkdata(order_data **pdata, size_t len, const struct locale *lang, c return result; } -static order_data *create_data(keyword_t kwd, const char *sptr, const struct locale *lang) +static order_data *create_data(keyword_t kwd, const char *sptr) { const char *s = sptr; order_data *data; @@ -241,17 +237,17 @@ static order_data *create_data(keyword_t kwd, const char *sptr, const struct loc /* orders with no parameter, only one order_data per order required */ if (kwd != NOKEYWORD && *sptr == 0) { - mkdata(&data, 0, lang, NULL); + mkdata(&data, 0, NULL); data->_refcount = 1; return data; } - mkdata(&data, s ? strlen(s) : 0, lang, s); + mkdata(&data, s ? strlen(s) : 0, s); data->_refcount = 1; return data; } static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, - bool noerror, const struct locale *lang) + bool noerror) { order_data *od; @@ -279,7 +275,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p while (isspace(*(unsigned char *)sptr)) ++sptr; - od = create_data(kwd, sptr, lang); + od = create_data(kwd, sptr); ord->id = add_data(od); release_data(od); @@ -341,7 +337,7 @@ order *create_order(keyword_t kwd, const struct locale * lang, zBuffer[0] = 0; } ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, zBuffer, false, false, lang); + return create_order_i(ord, kwd, zBuffer, false, false); } order *parse_order(const char *s, const struct locale * lang) @@ -373,7 +369,7 @@ order *parse_order(const char *s, const struct locale * lang) } if (kwd != NOKEYWORD) { order *ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, sptr, persistent, noerror, lang); + return create_order_i(ord, kwd, sptr, persistent, noerror); } } return NULL; @@ -529,7 +525,7 @@ bool is_silent(const order * ord) return (ord->command & CMD_QUIET) != 0; } -char *write_order(const order * ord, char *buffer, size_t size) +char *write_order(const order * ord, const struct locale *lang, char *buffer, size_t size) { if (ord == 0) { buffer[0] = 0; @@ -544,7 +540,7 @@ char *write_order(const order * ord, char *buffer, size_t size) release_data(od); } else { - get_command(ord, buffer, size); + get_command(ord, lang, buffer, size); } } return buffer; diff --git a/src/kernel/order.h b/src/kernel/order.h index f26174ee4..916055379 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -21,6 +21,8 @@ extern "C" { #endif + struct locale; + /* Encapsulation of an order * * This structure contains one order given by a unit. These used to be @@ -57,14 +59,16 @@ extern "C" { /* access functions for orders */ keyword_t getkeyword(const order * ord); void set_order(order ** destp, order * src); - char* get_command(const order *ord, char *buffer, size_t size); + char* get_command(const order *ord, const struct locale *lang, + char *buffer, size_t size); bool is_persistent(const order * ord); bool is_silent(const order * ord); bool is_exclusive(const order * ord); bool is_repeated(keyword_t kwd); bool is_long(keyword_t kwd); - char *write_order(const order * ord, char *buffer, size_t size); + char *write_order(const order * ord, const struct locale *lang, + char *buffer, size_t size); keyword_t init_order(const struct order *ord); void close_orders(void); diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index e1656efc9..675cd90cf 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -20,7 +20,7 @@ static void test_create_order(CuTest *tc) { ord = create_order(K_MOVE, lang, "NORTH"); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); - CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MOVE, init_order(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); @@ -40,7 +40,7 @@ static void test_parse_order(CuTest *tc) { CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MOVE, ord->command); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); - CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MOVE, init_order(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); @@ -98,7 +98,7 @@ static void test_parse_make(CuTest *tc) { ord = parse_order("M hurrdurr", lang); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MAKE, getkeyword(ord)); - CuAssertStrEquals(tc, "MAKE hurrdurr", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKE hurrdurr", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKE, init_order(ord)); CuAssertStrEquals(tc, "hurrdurr", getstrtoken()); @@ -121,7 +121,7 @@ static void test_parse_make_temp(CuTest *tc) { ord = parse_order("M T herp", lang); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); - CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); @@ -144,7 +144,7 @@ static void test_parse_maketemp(CuTest *tc) { ord = parse_order("MAKET herp", lang); CuAssertPtrNotNull(tc, ord); - CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); @@ -213,13 +213,13 @@ static void test_get_command(CuTest *tc) { test_setup(); lang = test_create_locale(); ord = create_order(K_MAKE, lang, "iron"); - CuAssertStrEquals(tc, "make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command |= CMD_QUIET; - CuAssertStrEquals(tc, "!make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "!make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command |= CMD_PERSIST; - CuAssertStrEquals(tc, "!@make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "!@make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command = K_MAKE | CMD_PERSIST; - CuAssertStrEquals(tc, "@make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "@make iron", get_command(ord, lang, buf, sizeof(buf))); free_order(ord); test_cleanup(); } diff --git a/src/kernel/save.c b/src/kernel/save.c index aa09a27be..133472d4f 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -379,7 +379,7 @@ static void writeorder(gamedata *data, const struct order *ord, const struct locale *lang) { char obuf[1024]; - write_order(ord, obuf, sizeof(obuf)); + write_order(ord, lang, obuf, sizeof(obuf)); if (obuf[0]) WRITE_STR(data->store, obuf); } diff --git a/src/keyword.test.c b/src/keyword.test.c index 75c31904a..68858367a 100644 --- a/src/keyword.test.c +++ b/src/keyword.test.c @@ -36,7 +36,7 @@ static void test_infinitive(CuTest *tc) { CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); ord = create_order(K_STUDY, lang, ""); - CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + CuAssertStrEquals(tc, "LERNE", get_command(ord, lang, buffer, sizeof(buffer))); free_order(ord); test_cleanup(); } diff --git a/src/laws.c b/src/laws.c index 6b27e7816..df6c70b89 100644 --- a/src/laws.c +++ b/src/laws.c @@ -968,7 +968,7 @@ int quit_cmd(unit * u, struct order *ord) } else { char buffer[64]; - write_order(ord, buffer, sizeof(buffer)); + write_order(ord, f->locale, buffer, sizeof(buffer)); cmistake(u, ord, 86, MSG_EVENT); log_warning("QUIT with illegal password for faction %s: %s\n", itoa36(f->no), buffer); } diff --git a/src/monsters.test.c b/src/monsters.test.c index 51f122b87..e43ed65cc 100644 --- a/src/monsters.test.c +++ b/src/monsters.test.c @@ -31,12 +31,12 @@ extern void plan_monsters(struct faction *f); extern int monster_attacks(unit * monster, bool rich_only); -static order *find_order(const char *expected, const unit *unit) +static order *find_order(const char *expected, const unit *u) { char cmd[32]; order *ord; - for (ord = unit->orders; ord; ord = ord->next) { - if (strcmp(expected, get_command(ord, cmd, sizeof(cmd))) == 0) { + for (ord = u->orders; ord; ord = ord->next) { + if (strcmp(expected, get_command(ord, u->faction->locale, cmd, sizeof(cmd))) == 0) { return ord; } } diff --git a/src/report.c b/src/report.c index 311cd98b2..3b2dd1d9f 100644 --- a/src/report.c +++ b/src/report.c @@ -1389,7 +1389,8 @@ static int report_template(const char *filename, report_context * ctx, const char *bom) { const resource_type *rsilver = get_resourcetype(R_SILVER); - faction *f = ctx->f; + const faction *f = ctx->f; + const struct locale *lang = f->locale; region *r; FILE *F = fopen(filename, "w"); stream strm = { 0 }, *out = &strm; @@ -1408,11 +1409,11 @@ report_template(const char *filename, report_context * ctx, const char *bom) } newline(out); - rps_nowrap(out, LOC(f->locale, "nr_template")); + rps_nowrap(out, LOC(lang, "nr_template")); newline(out); newline(out); - sprintf(buf, "%s %s \"password\"", LOC(f->locale, parameters[P_FACTION]), itoa36(f->no)); + sprintf(buf, "%s %s \"password\"", LOC(lang, parameters[P_FACTION]), itoa36(f->no)); rps_nowrap(out, buf); newline(out); newline(out); @@ -1439,12 +1440,12 @@ report_template(const char *filename, report_context * ctx, const char *bom) adjust_coordinates(f, &nx, &ny, pl); newline(out); if (pl && pl->id != 0) { - sprintf(buf, "%s %d,%d,%d ; %s", LOC(f->locale, - parameters[P_REGION]), nx, ny, pl->id, rname(r, f->locale)); + sprintf(buf, "%s %d,%d,%d ; %s", LOC(lang, + parameters[P_REGION]), nx, ny, pl->id, rname(r, lang)); } else { - sprintf(buf, "%s %d,%d ; %s", LOC(f->locale, parameters[P_REGION]), - nx, ny, rname(r, f->locale)); + sprintf(buf, "%s %d,%d ; %s", LOC(lang, parameters[P_REGION]), + nx, ny, rname(r, lang)); } rps_nowrap(out, buf); newline(out); @@ -1505,7 +1506,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) for (ord = u->old_orders; ord; ord = ord->next) { /* this new order will replace the old defaults */ strcpy(buf, " "); - write_order(ord, buf + 2, sizeof(buf) - 2); + write_order(ord, lang, buf + 2, sizeof(buf) - 2); rps_nowrap(out, buf); newline(out); } @@ -1515,7 +1516,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) continue; /* unit has defaults */ if (is_persistent(ord)) { strcpy(buf, " "); - write_order(ord, buf + 2, sizeof(buf) - 2); + write_order(ord, lang, buf + 2, sizeof(buf) - 2); rps_nowrap(out, buf); newline(out); } @@ -1527,7 +1528,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) } } newline(out); - strlcpy(buf, LOC(f->locale, parameters[P_NEXT]), sizeof(buf)); + strlcpy(buf, LOC(lang, parameters[P_NEXT]), sizeof(buf)); rps_nowrap(out, buf); newline(out); fstream_done(&strm); diff --git a/src/reports.c b/src/reports.c index e6021134a..265c2ab90 100644 --- a/src/reports.c +++ b/src/reports.c @@ -266,14 +266,14 @@ report_item(const unit * owner, const item * i, const faction * viewer, } #define ORDERS_IN_NR 1 -static size_t buforder(char *buffer, size_t size, const order * ord, int mode) +static size_t buforder(char *buffer, size_t size, const order * ord, const struct locale *lang, int mode) { char *bufp = buffer; bufp = STRLCPY(bufp, ", \"", size); if (mode < ORDERS_IN_NR) { char cmd[ORDERSIZE]; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, lang, cmd, sizeof(cmd)); bufp = STRLCPY(bufp, cmd, size); } else { @@ -498,6 +498,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, char *bufp = buf; int result = 0; item results[MAX_INVENTORY]; + const struct locale *lang = f->locale; assert(f); bufp = STRLCPY(bufp, unitname(u), size); @@ -514,7 +515,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (getarnt) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); + bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size); } else if (u->attribs) { faction *otherf = get_otherfaction(u); @@ -527,7 +528,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, else { if (getarnt) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); + bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size); } else { if (u->attribs && alliedunit(u, f, HELP_FSTEALTH)) { @@ -562,7 +563,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, bufp = STRLCPY(bufp, pzTmp, size); if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { bufp = STRLCPY(bufp, " (", size); - bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size); + bufp = STRLCPY(bufp, racename(lang, u, u_race(u)), size); if (size > 1) { strcpy(bufp++, ")"); --size; @@ -571,10 +572,10 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } else { const race *irace = u_irace(u); - bufp = STRLCPY(bufp, racename(f->locale, u, irace), size); + bufp = STRLCPY(bufp, racename(lang, u, irace), size); if (u->faction == f && irace != u_race(u)) { bufp = STRLCPY(bufp, " (", size); - bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size); + bufp = STRLCPY(bufp, racename(lang, u, u_race(u)), size); if (size > 1) { strcpy(bufp++, ")"); --size; @@ -584,15 +585,15 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "hero"), size); + bufp = STRLCPY(bufp, LOC(lang, "hero"), size); } /* status */ if (u->number && (u->faction == f || isbattle)) { const char *c = hp_status(u); - c = c ? LOC(f->locale, c) : 0; + c = c ? LOC(lang, c) : 0; bufp = STRLCPY(bufp, ", ", size); - bufp += report_status(u, f->locale, bufp, size); + bufp += report_status(u, lang, bufp, size); if (c || fval(u, UFL_HUNGER)) { bufp = STRLCPY(bufp, " (", size); if (c) { @@ -602,7 +603,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (c) { bufp = STRLCPY(bufp, ", ", size); } - bufp = STRLCPY(bufp, LOC(f->locale, "unit_hungers"), size); + bufp = STRLCPY(bufp, LOC(lang, "unit_hungers"), size); } if (size > 1) { strcpy(bufp++, ")"); @@ -612,7 +613,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (is_guard(u)) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "unit_guards"), size); + bufp = STRLCPY(bufp, LOC(lang, "unit_guards"), size); } if ((b = usiege(u)) != NULL) { @@ -624,7 +625,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (u->faction == f) { skill *sv; for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { - size_t bytes = spskill(bufp, size, f->locale, u, sv, &dh, 1); + size_t bytes = spskill(bufp, size, lang, u, sv, &dh, 1); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -651,7 +652,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, bufp = STRLCPY(bufp, ", ", size); if (!dh) { - result = snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_inventory")); + result = snprintf(bufp, size, "%s: ", LOC(lang, "nr_inventory")); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); dh = 1; @@ -681,7 +682,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (sbe->level <= maxlevel) { int result = 0; if (!header) { - result = snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); + result = snprintf(bufp, size, ", %s: ", LOC(lang, "nr_spells")); header = 1; } else { @@ -691,7 +692,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, WARN_STATIC_BUFFER(); } /* TODO: no need to deref the spellref here (spref->name is good) */ - bufp = STRLCPY(bufp, spell_name(sbe->sp, f->locale), size); + bufp = STRLCPY(bufp, spell_name(sbe->sp, lang), size); } } @@ -701,7 +702,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (i != MAXCOMBATSPELLS) { int result = - snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_combatspells")); + snprintf(bufp, size, ", %s: ", LOC(lang, "nr_combatspells")); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); @@ -717,7 +718,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, sp = get_combatspell(u, i); if (sp) { int sl = get_combatspelllevel(u, i); - bufp = STRLCPY(bufp, spell_name(sp, u->faction->locale), size); + bufp = STRLCPY(bufp, spell_name(sp, lang), size); if (sl > 0) { result = snprintf(bufp, size, " (%d)", sl); if (wrptr(&bufp, &size, result) != 0) @@ -725,7 +726,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } } else { - bufp = STRLCPY(bufp, LOC(f->locale, "nr_nospells"), size); + bufp = STRLCPY(bufp, LOC(lang, "nr_nospells"), size); } } } @@ -737,7 +738,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, keyword_t kwd = getkeyword(ord); if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { - int result = (int)buforder(bufp, size, ord, printed++); + int result = (int)buforder(bufp, size, ord, u->faction->locale, printed++); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); } @@ -750,7 +751,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, keyword_t kwd = getkeyword(ord); if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { - int result = (int)buforder(bufp, size, ord, printed++); + int result = (int)buforder(bufp, size, ord, lang, printed++); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); } @@ -762,7 +763,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } i = 0; - str = u_description(u, f->locale); + str = u_description(u, lang); if (str) { bufp = STRLCPY(bufp, "; ", size); bufp = STRLCPY(bufp, str, size); @@ -1980,13 +1981,15 @@ static void eval_race(struct opstack **stack, const void *userdata) static void eval_order(struct opstack **stack, const void *userdata) { /* order -> string */ + const faction *f = (const faction *)userdata; const struct order *ord = (const struct order *)opop(stack).v; char buf[4096]; size_t len; variant var; + const struct locale *lang = f ? f->locale : default_locale; UNUSED_ARG(userdata); - write_order(ord, buf, sizeof(buf)); + write_order(ord, lang, buf, sizeof(buf)); len = strlen(buf); var.v = strcpy(balloc(len + 1), buf); opush(stack, var); @@ -1994,8 +1997,8 @@ static void eval_order(struct opstack **stack, const void *userdata) static void eval_resources(struct opstack **stack, const void *userdata) { /* order -> string */ - const faction *report = (const faction *)userdata; - const struct locale *lang = report ? report->locale : default_locale; + const faction *f = (const faction *)userdata; + const struct locale *lang = f ? f->locale : default_locale; const struct resource *res = (const struct resource *)opop(stack).v; char buf[1024]; /* but we only use about half of this */ size_t size = sizeof(buf) - 1;