merge order_id branch

This commit is contained in:
Enno Rehling 2017-11-26 15:20:38 +01:00
commit c124702ffe
59 changed files with 1001 additions and 463 deletions

View File

@ -12,6 +12,7 @@ include(MSVC)
endif (MSVC) endif (MSVC)
find_package (SQLite3) find_package (SQLite3)
find_package (BerkeleyDB)
find_package (Curses) find_package (Curses)
find_package (LibXml2) find_package (LibXml2)
find_package (ToLua REQUIRED) find_package (ToLua REQUIRED)

View File

@ -0,0 +1,50 @@
# -*- cmake -*-
# - Find BerkeleyDB
# Find the BerkeleyDB includes and library
# This module defines
# DB_INCLUDE_DIR, where to find db.h, etc.
# DB_LIBRARIES, the libraries needed to use BerkeleyDB.
# DB_FOUND, If false, do not try to use BerkeleyDB.
# also defined, but not for general use are
# DB_LIBRARY, where to find the BerkeleyDB library.
FIND_PATH(DB_INCLUDE_DIR db.h
/usr/local/include/db4
/usr/local/include
/usr/include/db4
/usr/include
)
SET(DB_NAMES ${DB_NAMES} db)
FIND_LIBRARY(DB_LIBRARY
NAMES ${DB_NAMES}
PATHS /usr/lib /usr/local/lib
)
IF (DB_LIBRARY AND DB_INCLUDE_DIR)
SET(DB_LIBRARIES ${DB_LIBRARY})
SET(DB_FOUND "YES")
ELSE (DB_LIBRARY AND DB_INCLUDE_DIR)
SET(DB_FOUND "NO")
ENDIF (DB_LIBRARY AND DB_INCLUDE_DIR)
IF (DB_FOUND)
IF (NOT DB_FIND_QUIETLY)
MESSAGE(STATUS "Found BerkeleyDB: ${DB_LIBRARIES}")
ENDIF (NOT DB_FIND_QUIETLY)
ELSE (DB_FOUND)
IF (DB_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find BerkeleyDB library")
ENDIF (DB_FIND_REQUIRED)
ENDIF (DB_FOUND)
# Deprecated declarations.
SET (NATIVE_DB_INCLUDE_PATH ${DB_INCLUDE_DIR} )
GET_FILENAME_COMPONENT (NATIVE_DB_LIB_PATH ${DB_LIBRARY} PATH)
MARK_AS_ADVANCED(
DB_LIBRARY
DB_INCLUDE_DIR
)

View File

@ -47,7 +47,8 @@ function test_give()
assert_not_equal(5, u2:get_item("money")) assert_not_equal(5, u2:get_item("money"))
end 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("MACHE TEMP 123 'Herpderp'")
u:add_order("// this comment will be copied") u:add_order("// this comment will be copied")
u:add_order("ENDE") u:add_order("ENDE")

View File

@ -26,7 +26,7 @@ ENDIF()
IF (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang") IF (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
# SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wno-sign-conversion") # SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wno-sign-conversion")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") # SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89")
ELSEIF(MSVC) ELSEIF(MSVC)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /WX /MP") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /WX /MP")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG set(CMAKE_EXE_LINKER_FLAGS_DEBUG
@ -134,6 +134,7 @@ set (ERESSEA_SRC
${TRIGGERS_SRC} ${TRIGGERS_SRC}
${ATTRIBUTES_SRC} ${ATTRIBUTES_SRC}
${KERNEL_SRC} ${KERNEL_SRC}
${DB_SRC}
${UTIL_SRC} ${UTIL_SRC}
) )
@ -265,8 +266,19 @@ add_test(server test_eressea)
install(TARGETS eressea DESTINATION "bin") install(TARGETS eressea DESTINATION "bin")
if (DB_FOUND)
include_directories (${DB_INCLUDE_DIR})
target_link_libraries(convert ${DB_LIBRARIES})
target_link_libraries(eressea ${DB_LIBRARIES})
target_link_libraries(test_eressea ${DB_LIBRARIES})
add_definitions(-DUSE_DB)
endif(DB_FOUND)
if (SQLITE3_FOUND) if (SQLITE3_FOUND)
include_directories (${SQLITE3_INCLUDE_DIR})
target_link_libraries(eressea ${SQLITE3_LIBRARIES}) target_link_libraries(eressea ${SQLITE3_LIBRARIES})
target_link_libraries(convert ${SQLITE3_LIBRARIES})
target_link_libraries(test_eressea ${SQLITE3_LIBRARIES})
add_definitions(-DUSE_SQLITE) add_definitions(-DUSE_SQLITE)
endif(SQLITE3_FOUND) endif(SQLITE3_FOUND)

View File

@ -3842,7 +3842,7 @@ static bool start_battle(region * r, battle ** bp)
/* Ende Fehlerbehandlung Angreifer */ /* Ende Fehlerbehandlung Angreifer */
init_order(ord); init_order_depr(ord);
/* attackierte Einheit ermitteln */ /* attackierte Einheit ermitteln */
getunit(r, u->faction, &u2); getunit(r, u->faction, &u2);

View File

@ -13,7 +13,7 @@ static int tolua_order_get_token(lua_State *L) {
order *ord = (order *)tolua_tousertype(L, 1, 0); order *ord = (order *)tolua_tousertype(L, 1, 0);
int n = (int)tolua_tonumber(L, 2, 0); int n = (int)tolua_tonumber(L, 2, 0);
const char * str = 0; const char * str = 0;
init_order(ord); init_order_depr(ord);
while (n-->0) { while (n-->0) {
str = getstrtoken(); str = getstrtoken();
if (!str) { if (!str) {

View File

@ -763,6 +763,22 @@ static int tolua_unit_get_spells(lua_State * L)
return tolua_selist_push(L, "spellbook", "spell_entry", slist); 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) static int tolua_unit_get_orders(lua_State * L)
{ {
unit *self = (unit *)tolua_tousertype(L, 1, 0); unit *self = (unit *)tolua_tousertype(L, 1, 0);
@ -778,7 +794,7 @@ static int tolua_unit_get_orders(lua_State * L)
return 1; return 1;
} }
#endif
static int tolua_unit_get_curse(lua_State *L) { static int tolua_unit_get_curse(lua_State *L) {
unit *self = (unit *)tolua_tousertype(L, 1, 0); unit *self = (unit *)tolua_tousertype(L, 1, 0);
const char *name = tolua_tostring(L, 2, 0); const char *name = tolua_tostring(L, 2, 0);
@ -960,95 +976,96 @@ void tolua_unit_open(lua_State * L)
NULL); NULL);
tolua_beginmodule(L, TOLUA_CAST "event"); tolua_beginmodule(L, TOLUA_CAST "event");
{ {
tolua_function(L, TOLUA_CAST "get_type", &tolua_event_gettype); 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", tolua_event_get);
} }
tolua_endmodule(L); tolua_endmodule(L);
tolua_cclass(L, TOLUA_CAST "unit", TOLUA_CAST "unit", TOLUA_CAST "", NULL); tolua_cclass(L, TOLUA_CAST "unit", TOLUA_CAST "unit", TOLUA_CAST "", NULL);
tolua_beginmodule(L, TOLUA_CAST "unit"); tolua_beginmodule(L, TOLUA_CAST "unit");
{ {
tolua_function(L, TOLUA_CAST "__tostring", &tolua_unit_tostring); tolua_function(L, TOLUA_CAST "__tostring", tolua_unit_tostring);
tolua_function(L, TOLUA_CAST "create", &tolua_unit_create); tolua_function(L, TOLUA_CAST "create", tolua_unit_create);
tolua_function(L, TOLUA_CAST "destroy", &tolua_unit_destroy); 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_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_unit_set_faction);
tolua_variable(L, TOLUA_CAST "id", tolua_unit_get_id, tolua_unit_set_id); 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 "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 "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 "hp", tolua_unit_get_hp, tolua_unit_set_hp);
tolua_variable(L, TOLUA_CAST "status", &tolua_unit_get_status, tolua_variable(L, TOLUA_CAST "status", tolua_unit_get_status,
tolua_unit_set_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_unit_set_familiar);
tolua_variable(L, TOLUA_CAST "weight", &tolua_unit_get_weight, 0); 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 "capacity", tolua_unit_get_capacity, 0);
tolua_function(L, TOLUA_CAST "add_order", &tolua_unit_add_order); tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order);
tolua_function(L, TOLUA_CAST "clear_orders", &tolua_unit_clear_orders); tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders);
tolua_variable(L, TOLUA_CAST "orders", &tolua_unit_get_orders, 0); #ifdef TOLUA_ORDERS_CLOSURE
tolua_variable(L, TOLUA_CAST "orders", tolua_unit_get_orders, 0);
tolua_function(L, TOLUA_CAST "get_curse", &tolua_unit_get_curse); #endif
tolua_function(L, TOLUA_CAST "has_attrib", &tolua_unit_has_attrib); 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: */ /* key-attributes for named flags: */
tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag); tolua_function(L, TOLUA_CAST "set_flag", tolua_unit_set_flag);
tolua_function(L, TOLUA_CAST "get_flag", &tolua_unit_get_flag); tolua_function(L, TOLUA_CAST "get_flag", tolua_unit_get_flag);
tolua_variable(L, TOLUA_CAST "guard", &tolua_unit_get_guard, tolua_variable(L, TOLUA_CAST "guard", tolua_unit_get_guard,
&tolua_unit_set_guard); tolua_unit_set_guard);
tolua_variable(L, TOLUA_CAST "flags", &tolua_unit_get_flags, tolua_variable(L, TOLUA_CAST "flags", tolua_unit_get_flags,
&tolua_unit_set_flags); tolua_unit_set_flags);
tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age, tolua_variable(L, TOLUA_CAST "age", tolua_unit_get_age,
tolua_unit_set_age); tolua_unit_set_age);
/* items: */ /* items: */
tolua_function(L, TOLUA_CAST "get_item", &tolua_unit_get_item); tolua_function(L, TOLUA_CAST "get_item", tolua_unit_get_item);
tolua_function(L, TOLUA_CAST "add_item", &tolua_unit_add_item); tolua_function(L, TOLUA_CAST "add_item", tolua_unit_add_item);
tolua_variable(L, TOLUA_CAST "items", &tolua_unit_get_items, 0); 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 "get_pooled", tolua_unit_get_pooled);
tolua_function(L, TOLUA_CAST "use_pooled", &tolua_unit_use_pooled); tolua_function(L, TOLUA_CAST "use_pooled", tolua_unit_use_pooled);
/* effects */ /* effects */
tolua_function(L, TOLUA_CAST "get_potion", &tolua_unit_get_effect); tolua_function(L, TOLUA_CAST "get_potion", tolua_unit_get_effect);
/* skills: */ /* skills: */
tolua_function(L, TOLUA_CAST "get_skill", &tolua_unit_getskill); 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 "eff_skill", tolua_unit_effskill);
tolua_function(L, TOLUA_CAST "set_skill", &tolua_unit_setskill); 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: */ /* 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_variable(L, TOLUA_CAST "race_name", tolua_unit_get_racename,
&tolua_unit_set_racename); tolua_unit_set_racename);
tolua_function(L, TOLUA_CAST "add_spell", &tolua_unit_addspell); tolua_function(L, TOLUA_CAST "add_spell", tolua_unit_addspell);
tolua_variable(L, TOLUA_CAST "spells", &tolua_unit_get_spells, 0); tolua_variable(L, TOLUA_CAST "spells", tolua_unit_get_spells, 0);
tolua_function(L, TOLUA_CAST "cast_spell", &tolua_unit_castspell); 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_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_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_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_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_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_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_unit_set_race);
tolua_variable(L, TOLUA_CAST "hp_max", &tolua_unit_get_hpmax, 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_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); tolua_endmodule(L);
} }

View File

@ -114,20 +114,6 @@ int log_lua_error(lua_State * L)
return 1; 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) static int tolua_selist_iter(lua_State * L)
{ {
selist **qlp = (selist **)lua_touserdata(L, lua_upvalueindex(1)); selist **qlp = (selist **)lua_touserdata(L, lua_upvalueindex(1));

View File

@ -22,7 +22,6 @@ extern "C" {
int tolua_sqlite_open(struct lua_State *L); int tolua_sqlite_open(struct lua_State *L);
int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d);
int tolua_itemlist_next(struct lua_State *L); 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, int tolua_selist_push(struct lua_State *L, const char *list_type,
const char *elem_type, struct selist *list); const char *elem_type, struct selist *list);

View File

@ -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) 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; skill_t sk = (skill_t)var.i;
UNUSED_ARG(userdata); UNUSED_ARG(userdata);
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,
report->locale))); f->locale)));
else else
strcpy(buffer, "\"\""); strcpy(buffer, "\"\"");
return 0; 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) static int cr_order(variant var, char *buffer, const void *userdata)
{ {
order *ord = (order *)var.v; order *ord = (order *)var.v;
UNUSED_ARG(userdata); const faction *f = (const faction *)userdata;
if (ord != NULL) { if (ord != NULL) {
char cmd[ORDERSIZE]; char cmd[ORDERSIZE];
char *wp = buffer; char *wp = buffer;
const char *rp; const char *rp;
get_command(ord, cmd, sizeof(cmd)); get_command(ord, f->locale, cmd, sizeof(cmd));
*wp++ = '\"'; *wp++ = '\"';
for (rp = cmd; *rp;) { 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); 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; const char *str;
char ebuf[1025]; char ebuf[1025];
char obuf[1024]; char obuf[1024];
write_order(ord, obuf, sizeof(obuf)); write_order(ord, lang, obuf, sizeof(obuf));
str = escape_string(obuf, ebuf, sizeof(ebuf)); str = escape_string(obuf, ebuf, sizeof(ebuf));
if (str == ebuf) { if (str == ebuf) {
ebuf[1024] = 0; ebuf[1024] = 0;
@ -779,6 +780,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
const faction *fother; const faction *fother;
const char *prefix; const char *prefix;
bool allied; bool allied;
const struct locale *lang = f->locale;
assert(u && u->number); assert(u && u->number);
assert(u->region == r); /* TODO: if this holds true, then why did we pass in r? */ 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, "EINHEIT %d\n", u->no);
stream_printf(out, "\"%s\";Name\n", unit_getname(u)); stream_printf(out, "\"%s\";Name\n", unit_getname(u));
str = u_description(u, f->locale); str = u_description(u, lang);
if (str) { if (str) {
stream_printf(out, "\"%s\";Beschr\n", 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); prefix = raceprefix(u);
if (prefix) { if (prefix) {
prefix = mkname("prefix", 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))); prefix)));
} }
stream_printf(out, "%d;Anzahl\n", u->number); stream_printf(out, "%d;Anzahl\n", u->number);
@ -844,18 +846,18 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) {
const char *zRace = rc_name_s(u_race(u), NAME_PLURAL); const char *zRace = rc_name_s(u_race(u), NAME_PLURAL);
stream_printf(out, "\"%s\";wahrerTyp\n", stream_printf(out, "\"%s\";wahrerTyp\n",
translate(zRace, LOC(f->locale, zRace))); translate(zRace, LOC(lang, zRace)));
} }
} }
else { else {
const race *irace = u_irace(u); const race *irace = u_irace(u);
const char *zRace = rc_name_s(irace, NAME_PLURAL); const char *zRace = rc_name_s(irace, NAME_PLURAL);
stream_printf(out, "\"%s\";Typ\n", 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)) { if (u->faction == f && irace != u_race(u)) {
zRace = rc_name_s(u_race(u), NAME_PLURAL); zRace = rc_name_s(u_race(u), NAME_PLURAL);
stream_printf(out, "\"%s\";wahrerTyp\n", stream_printf(out, "\"%s\";wahrerTyp\n",
translate(zRace, LOC(f->locale, zRace))); translate(zRace, LOC(lang, zRace)));
} }
} }
@ -923,7 +925,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
for (ord = u->old_orders; ord; ord = ord->next) { for (ord = u->old_orders; ord; ord = ord->next) {
/* this new order will replace the old defaults */ /* this new order will replace the old defaults */
if (is_persistent(ord)) { if (is_persistent(ord)) {
stream_order(out, ord); stream_order(out, ord, lang);
} }
} }
for (ord = u->orders; ord; ord = ord->next) { for (ord = u->orders; ord; ord = ord->next) {
@ -931,7 +933,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
if (u->old_orders && is_repeated(kwd)) if (u->old_orders && is_repeated(kwd))
continue; /* unit has defaults */ continue; /* unit has defaults */
if (is_persistent(ord)) { if (is_persistent(ord)) {
stream_order(out, ord); stream_order(out, ord, lang);
} }
} }
@ -947,7 +949,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, stream_printf(out, "%d %d;%s\n", u->number * level_days(sv->level), esk,
translate(mkname("skill", skillnames[sk]), skillname(sk, translate(mkname("skill", skillnames[sk]), skillname(sk,
f->locale))); lang)));
} }
} }
@ -961,8 +963,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
const spell *sp = mage->combatspells[i].sp; const spell *sp = mage->combatspells[i].sp;
if (sp) { if (sp) {
const char *name = const char *name =
translate(mkname("spell", sp->sname), spell_name(sp, translate(mkname("spell", sp->sname), spell_name(sp, lang));
f->locale));
stream_printf(out, "KAMPFZAUBER %d\n", i); stream_printf(out, "KAMPFZAUBER %d\n", i);
stream_printf(out, "\"%s\";name\n", name); stream_printf(out, "\"%s\";name\n", name);
stream_printf(out, "%d;level\n", mage->combatspells[i].level); stream_printf(out, "%d;level\n", mage->combatspells[i].level);
@ -997,7 +998,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f,
pr = 1; pr = 1;
stream_printf(out, "GEGENSTAENDE\n"); 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); cr_output_curses(out, f, u, TYP_UNIT);

View File

@ -441,7 +441,7 @@ static void recruit(unit * u, struct order *ord, request ** recruitorders)
const char *str; const char *str;
int n; int n;
init_order(ord); init_order_depr(ord);
n = getint(); n = getint();
if (n <= 0) { if (n <= 0) {
syntax_error(u, ord); syntax_error(u, ord);
@ -598,7 +598,7 @@ int give_control_cmd(unit * u, order * ord)
unit *u2; unit *u2;
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
getunit(r, u->faction, &u2); getunit(r, u->faction, &u2);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
@ -657,7 +657,7 @@ static int forget_cmd(unit * u, order * ord)
return 0; return 0;
} }
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
sk = get_skill(s, u->faction->locale); sk = get_skill(s, u->faction->locale);
@ -1303,7 +1303,7 @@ int make_cmd(unit * u, struct order *ord)
char ibuf[16]; char ibuf[16];
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_MAKE); assert(kwd == K_MAKE);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
@ -1577,7 +1577,7 @@ static void buy(unit * u, request ** buyorders, struct order *ord)
/* Im Augenblick kann man nur 1 Produkt kaufen. expandbuying ist aber /* Im Augenblick kann man nur 1 Produkt kaufen. expandbuying ist aber
* schon daf<EFBFBD>r ausger<EFBFBD>stet, mehrere Produkte zu kaufen. */ * schon daf<EFBFBD>r ausger<EFBFBD>stet, mehrere Produkte zu kaufen. */
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_BUY); assert(kwd == K_BUY);
n = getint(); n = getint();
if (n <= 0) { if (n <= 0) {
@ -1884,7 +1884,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord)
/* sellorders sind KEIN array, weil f<>r alle items DIE SELBE resource /* sellorders sind KEIN array, weil f<>r alle items DIE SELBE resource
* (das geld der region) aufgebraucht wird. */ * (das geld der region) aufgebraucht wird. */
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_SELL); assert(kwd == K_SELL);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
@ -2275,7 +2275,7 @@ static void breed_cmd(unit * u, struct order *ord)
} }
/* z<>chte [<anzahl>] <parameter> */ /* z<>chte [<anzahl>] <parameter> */
(void)init_order(ord); (void)init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
m = s ? atoip(s) : 0; m = s ? atoip(s) : 0;
@ -2342,7 +2342,7 @@ static void research_cmd(unit * u, struct order *ord)
region *r = u->region; region *r = u->region;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_RESEARCH); assert(kwd == K_RESEARCH);
if (effskill(u, SK_HERBALISM, 0) < 7) { if (effskill(u, SK_HERBALISM, 0) < 7) {
@ -2418,7 +2418,7 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders)
message * msg; message * msg;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_STEAL); assert(kwd == K_STEAL);
assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH));
@ -2556,7 +2556,7 @@ void entertain_cmd(unit * u, struct order *ord)
static int entertainperlevel = 0; static int entertainperlevel = 0;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_ENTERTAIN); assert(kwd == K_ENTERTAIN);
if (!entertainbase) { if (!entertainbase) {
const char *str = config_get("entertain.base"); const char *str = config_get("entertain.base");
@ -2766,7 +2766,7 @@ void tax_cmd(unit * u, struct order *ord, request ** taxorders)
taxperlevel = config_get_int("taxing.perlevel", 0); taxperlevel = config_get_int("taxing.perlevel", 0);
} }
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_TAX); assert(kwd == K_TAX);
if (!humanoidrace(u_race(u)) && !is_monsters(u->faction)) { if (!humanoidrace(u_race(u)) && !is_monsters(u->faction)) {
@ -2835,7 +2835,7 @@ void loot_cmd(unit * u, struct order *ord, request ** lootorders)
request *o; request *o;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_LOOT); assert(kwd == K_LOOT);
if (config_get_int("rules.enable_loot", 0) == 0 && !is_monsters(u->faction)) { if (config_get_int("rules.enable_loot", 0) == 0 && !is_monsters(u->faction)) {

5
src/eressea.c Executable file → Normal file
View File

@ -27,6 +27,7 @@
#include <kernel/equipment.h> #include <kernel/equipment.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/xmlreader.h> #include <kernel/xmlreader.h>
#include <kernel/database.h>
#include <modules/gmcmd.h> #include <modules/gmcmd.h>
#include <modules/xmas.h> #include <modules/xmas.h>
#include <items/xerewards.h> #include <items/xerewards.h>
@ -36,6 +37,8 @@
#include <util/message.h> #include <util/message.h>
#include <races/races.h> #include <races/races.h>
#include <stdlib.h>
void game_done(void) void game_done(void)
{ {
#ifdef CLEANUP_CODE #ifdef CLEANUP_CODE
@ -57,10 +60,12 @@ void game_done(void)
free_special_directions(); free_special_directions();
free_locales(); free_locales();
kernel_done(); kernel_done();
dblib_close();
} }
void game_init(void) void game_init(void)
{ {
dblib_open();
kernel_init(); kernel_init();
register_triggers(); register_triggers();
register_xmas(); register_xmas();

View File

@ -634,7 +634,7 @@ void give_cmd(unit * u, order * ord)
message *msg; message *msg;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_GIVE); assert(kwd == K_GIVE);
err = getunit(r, u->faction, &u2); err = getunit(r, u->faction, &u2);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));

View File

@ -47,7 +47,7 @@ static int
use_studypotion(struct unit *u, const struct item_type *itype, int amount, use_studypotion(struct unit *u, const struct item_type *itype, int amount,
struct order *ord) struct order *ord)
{ {
if (u->thisorder && init_order(u->thisorder) == K_STUDY) { if (u->thisorder && init_order(u->thisorder, u->faction->locale) == K_STUDY) {
char token[128]; char token[128];
skill_t sk = NOSKILL; skill_t sk = NOSKILL;
skill *sv = 0; skill *sv = 0;

View File

@ -11,12 +11,14 @@ command.test.c
config.test.c config.test.c
# connection.test.c # connection.test.c
curse.test.c curse.test.c
database.test.c
equipment.test.c equipment.test.c
faction.test.c faction.test.c
group.test.c group.test.c
item.test.c item.test.c
messages.test.c messages.test.c
order.test.c order.test.c
orderdb.test.c
# pathfinder.test.c # pathfinder.test.c
plane.test.c plane.test.c
pool.test.c pool.test.c
@ -34,7 +36,18 @@ jsonconf.test.c
# xmlreader.test.c # xmlreader.test.c
) )
SET(_DBFILES db/critbit.c)
IF(SQLITE3_FOUND)
SET(_DBFILES db/sqlite.c)
ENDIF(SQLITE3_FOUND)
IF(DB_FOUND)
SET(_DBFILES db/berkeley.c)
ENDIF(DB_FOUND)
SET(_FILES SET(_FILES
${_DBFILES}
alliance.c alliance.c
ally.c ally.c
build.c build.c
@ -44,12 +57,14 @@ command.c
config.c config.c
connection.c connection.c
curse.c curse.c
database.c
equipment.c equipment.c
faction.c faction.c
group.c group.c
item.c item.c
messages.c messages.c
order.c order.c
orderdb.c
pathfinder.c pathfinder.c
plane.c plane.c
pool.c pool.c

View File

@ -172,7 +172,7 @@ static void perform_kick(void)
if (al && alliance_get_leader(al) == ta->u->faction) { if (al && alliance_get_leader(al) == ta->u->faction) {
faction *f; faction *f;
init_order(ta->ord); init_order_depr(ta->ord);
skip_token(); skip_token();
f = getfaction(); f = getfaction();
if (f && f_get_alliance(f) == al) { if (f && f_get_alliance(f) == al) {
@ -193,7 +193,7 @@ static void perform_new(void)
int id; int id;
faction *f = ta->u->faction; faction *f = ta->u->faction;
init_order(ta->ord); init_order_depr(ta->ord);
skip_token(); skip_token();
id = getid(); id = getid();
@ -228,7 +228,7 @@ static void perform_transfer(void)
if (al && alliance_get_leader(al) == ta->u->faction) { if (al && alliance_get_leader(al) == ta->u->faction) {
faction *f; faction *f;
init_order(ta->ord); init_order_depr(ta->ord);
skip_token(); skip_token();
f = getfaction(); f = getfaction();
if (f && f_get_alliance(f) == al) { if (f && f_get_alliance(f) == al) {
@ -265,7 +265,7 @@ static void perform_join(void)
faction *fj = ta->u->faction; faction *fj = ta->u->faction;
int aid; int aid;
init_order(ta->ord); init_order_depr(ta->ord);
skip_token(); skip_token();
aid = getid(); aid = getid();
if (aid) { if (aid) {
@ -277,7 +277,7 @@ static void perform_join(void)
faction *fi = ti->u->faction; faction *fi = ti->u->faction;
if (fi && f_get_alliance(fi) == al) { if (fi && f_get_alliance(fi) == al) {
int fid; int fid;
init_order(ti->ord); init_order_depr(ti->ord);
skip_token(); skip_token();
fid = getid(); fid = getid();
if (fid == fj->no) { if (fid == fj->no) {

View File

@ -161,7 +161,7 @@ int destroy_cmd(unit * u, struct order *ord)
return 52; return 52;
} }
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
if (s && *s) { if (s && *s) {

View File

@ -14,6 +14,7 @@
#include <kernel/config.h> #include <kernel/config.h>
#include "command.h" #include "command.h"
#include <kernel/faction.h>
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/unit.h> #include <kernel/unit.h>
@ -104,10 +105,10 @@ static int do_command_i(const struct tnode *keys, struct unit *u, struct order *
void do_command(const struct tnode *keys, struct unit *u, struct order *ord) void do_command(const struct tnode *keys, struct unit *u, struct order *ord)
{ {
init_order(ord); init_order_depr(ord);
if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) { if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) {
char cmd[ORDERSIZE]; 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); log_warning("%s failed command '%s'\n", unitname(u), cmd);
} }
} }

View File

@ -32,7 +32,7 @@ static void test_command(CuTest * tc) {
struct locale * loc; struct locale * loc;
unit *u; unit *u;
test_cleanup(); test_setup();
loc = test_create_locale(); loc = test_create_locale();
st = stree_create(); st = stree_create();
CuAssertPtrNotNull(tc, st); CuAssertPtrNotNull(tc, st);

View File

@ -32,28 +32,28 @@ static void test_read_unitid(CuTest *tc) {
a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ a->data.i = atoi36("42"); /* this unit is also TEMP 42 */
ord = create_order(K_GIVE, lang, "TEMP 42"); ord = create_order(K_GIVE, lang, "TEMP 42");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, u->no, read_unitid(u->faction, u->region)); CuAssertIntEquals(tc, u->no, read_unitid(u->faction, u->region));
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, "8"); ord = create_order(K_GIVE, lang, "8");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, 8, read_unitid(u->faction, u->region)); CuAssertIntEquals(tc, 8, read_unitid(u->faction, u->region));
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, ""); ord = create_order(K_GIVE, lang, "");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region));
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, "TEMP"); ord = create_order(K_GIVE, lang, "TEMP");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region));
free_order(ord); free_order(ord);
/* bug https://bugs.eressea.de/view.php?id=1685 */ /* bug https://bugs.eressea.de/view.php?id=1685 */
ord = create_order(K_GIVE, lang, "##"); ord = create_order(K_GIVE, lang, "##");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region));
free_order(ord); free_order(ord);
@ -78,42 +78,42 @@ static void test_getunit(CuTest *tc) {
r = test_create_region(1, 0, t_plain); r = test_create_region(1, 0, t_plain);
ord = create_order(K_GIVE, lang, itoa36(u->no)); ord = create_order(K_GIVE, lang, itoa36(u->no));
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, u, u2); CuAssertPtrEquals(tc, u, u2);
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_NOTFOUND, getunit(r, u->faction, &u2)); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(r, u->faction, &u2));
CuAssertPtrEquals(tc, NULL, u2); CuAssertPtrEquals(tc, NULL, u2);
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, itoa36(u->no + 1)); ord = create_order(K_GIVE, lang, itoa36(u->no + 1));
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, NULL, u2); CuAssertPtrEquals(tc, NULL, u2);
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, "0"); ord = create_order(K_GIVE, lang, "0");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_PEASANTS, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_PEASANTS, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, NULL, u2); CuAssertPtrEquals(tc, NULL, u2);
free_order(ord); free_order(ord);
/* bug https://bugs.eressea.de/view.php?id=1685 */ /* bug https://bugs.eressea.de/view.php?id=1685 */
ord = create_order(K_GIVE, lang, "TEMP ##"); ord = create_order(K_GIVE, lang, "TEMP ##");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, NULL, u2); CuAssertPtrEquals(tc, NULL, u2);
free_order(ord); free_order(ord);
/* bug https://bugs.eressea.de/view.php?id=1685 */ /* bug https://bugs.eressea.de/view.php?id=1685 */
ord = create_order(K_GIVE, lang, "##"); ord = create_order(K_GIVE, lang, "##");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, NULL, u2); CuAssertPtrEquals(tc, NULL, u2);
free_order(ord); free_order(ord);
ord = create_order(K_GIVE, lang, "TEMP 42"); ord = create_order(K_GIVE, lang, "TEMP 42");
init_order(ord); init_order_depr(ord);
CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2));
CuAssertPtrEquals(tc, u, u2); CuAssertPtrEquals(tc, u, u2);
free_order(ord); free_order(ord);

38
src/kernel/database.c Normal file
View File

@ -0,0 +1,38 @@
#include <platform.h>
#include "database.h"
#include <platform.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
#include "db/driver.h"
order_data *dblib_load_order(int id)
{
if (id > 0) {
return db_driver_order_load(id);
}
return NULL;
}
int dblib_save_order(order_data *od)
{
if (od->_str) {
return db_driver_order_save(od);
}
return 0;
}
void dblib_open(void)
{
db_driver_open();
}
void dblib_close(void)
{
db_driver_close();
}

21
src/kernel/database.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef H_DATABASE
#define H_DATABASE
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
struct order_data;
void dblib_open(void);
void dblib_close(void);
struct order_data *dblib_load_order(int id);
int dblib_save_order(struct order_data *od);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,40 @@
#include <platform.h>
#include <kernel/config.h>
#include "database.h"
#include "orderdb.h"
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_save_load_order(CuTest *tc) {
order_data *od;
int id;
const char * s = "GIB enno 1 Hodor";
test_setup();
odata_create(&od, strlen(s) + 1, s);
CuAssertTrue(tc, od->_refcount >= 1);
id = odata_save(od);
odata_release(od);
CuAssertTrue(tc, id != 0);
od = odata_load(id);
CuAssertPtrNotNull(tc, od);
CuAssertTrue(tc, od->_refcount >= 1);
CuAssertStrEquals(tc, s, od->_str);
odata_release(od);
test_cleanup();
}
CuSuite *get_db_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_save_load_order);
return suite;
}

74
src/kernel/db/berkeley.c Normal file
View File

@ -0,0 +1,74 @@
#include <sys/types.h>
#include <db.h>
#include <platform.h>
#include "driver.h"
#include <kernel/config.h>
#include <kernel/orderdb.h>
#include <assert.h>
#include <string.h>
static DB *g_dbp;
void db_driver_open(void)
{
int ret;
u_int32_t flags = DB_CREATE;
const char * dbname;
dbname = config_get("game.dbname");
ret = db_create(&g_dbp, NULL, 0);
assert(ret==0);
ret = g_dbp->open(g_dbp, NULL, dbname, NULL, DB_RECNO, flags, 0);
assert(ret==0);
}
void db_driver_close(void)
{
int ret;
ret = g_dbp->close(g_dbp, 0);
assert(ret==0);
}
int db_driver_order_save(struct order_data *od)
{
int ret;
DBT key, data;
db_recno_t recno;
assert(od && od->_str);
key.data = &recno;
key.size = sizeof(recno);
key.flags = DB_DBT_USERMEM;
data.data = (void *)od->_str;
data.size = strlen(od->_str) + 1;
data.flags = DB_DBT_USERMEM;
ret = g_dbp->put(g_dbp, NULL, &key, &data, DB_APPEND);
assert(ret == 0);
return (int)recno;
}
struct order_data *db_driver_order_load(int id)
{
int ret;
order_data *od = NULL;
DBT key, data;
db_recno_t recno;
assert(id>0);
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
recno = (db_recno_t)id;
key.data = &recno;
key.size = sizeof(recno);
key.flags = DB_DBT_USERMEM;
ret = g_dbp->get(g_dbp, NULL, &key, &data, 0);
if (ret == 0) {
odata_create(&od, data.size, data.data);
}
return od;
}

67
src/kernel/db/critbit.c Normal file
View File

@ -0,0 +1,67 @@
#include <platform.h>
#include "driver.h"
#include <kernel/orderdb.h>
#include <util/log.h>
#include <critbit.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
static critbit_tree cb_orders = { 0 };
static int auto_id = -1;
struct cb_entry {
int id;
order_data *data;
};
order_data *db_driver_order_load(int id)
{
void * match;
assert(id>0);
if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) {
struct cb_entry *ent = (struct cb_entry *)match;
order_data * od = ent->data;
++od->_refcount;
return od;
}
return NULL;
}
int db_driver_order_save(order_data *od)
{
struct cb_entry ent;
assert(od && od->_str);
++od->_refcount;
ent.id = ++auto_id;
ent.data = od;
cb_insert(&cb_orders, &ent, sizeof(ent));
return ent.id;
}
static int free_data_cb(const void *match, const void *key, size_t keylen,
void *udata)
{
struct cb_entry * ent = (struct cb_entry *)match;
order_data *od = ent->data;
odata_release(od);
return 0;
}
void db_driver_open(void)
{
assert(auto_id == -1);
auto_id = 0;
}
void db_driver_close(void)
{
cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL);
cb_clear(&cb_orders);
auto_id = -1;
}

8
src/kernel/db/driver.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
struct order_data;
void db_driver_open(void);
void db_driver_close(void);
int db_driver_order_save(struct order_data *od);
struct order_data *db_driver_order_load(int id);

123
src/kernel/db/sqlite.c Normal file
View File

@ -0,0 +1,123 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/orderdb.h>
#include <util/log.h>
#include "driver.h"
#include <sqlite3.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
static sqlite3 *g_db;
static sqlite3_stmt * g_stmt_insert;
static sqlite3_stmt * g_stmt_select;
static int g_order_batchsize;
static int g_order_tx_size;
order_data *db_driver_order_load(int id)
{
order_data * od = NULL;
int err;
if (g_order_tx_size > 0) {
g_order_tx_size = 0;
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
err = sqlite3_reset(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_bind_int(g_stmt_select, 1, id);
assert(err == SQLITE_OK);
do {
err = sqlite3_step(g_stmt_select);
if (err == SQLITE_ROW) {
const unsigned char *text;
int bytes;
bytes = sqlite3_column_bytes(g_stmt_select, 0);
assert(bytes > 0);
text = sqlite3_column_text(g_stmt_select, 0);
odata_create(&od, 1+(size_t)bytes, (const char *)text);
return od;
}
} while (err == SQLITE_ROW);
assert(err == SQLITE_DONE);
return NULL;
}
int db_driver_order_save(order_data *od)
{
int err;
sqlite3_int64 id;
assert(od && od->_str);
if (g_order_batchsize > 0) {
if (g_order_tx_size == 0) {
err = sqlite3_exec(g_db, "BEGIN TRANSACTION", NULL, NULL, NULL);
assert(err == SQLITE_OK);
}
}
err = sqlite3_reset(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_bind_text(g_stmt_insert, 1, od->_str, -1, SQLITE_STATIC);
assert(err == SQLITE_OK);
err = sqlite3_step(g_stmt_insert);
assert(err == SQLITE_DONE);
id = sqlite3_last_insert_rowid(g_db);
assert(id <= INT_MAX);
if (g_order_batchsize > 0) {
if (++g_order_tx_size >= g_order_batchsize) {
err = sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);
assert(err == SQLITE_OK);
g_order_tx_size = 0;
}
}
return (int)id;
}
void db_driver_open(void)
{
int err;
const char *dbname;
g_order_batchsize = config_get_int("game.dbbatch", 100);
dbname = config_get("game.dbname");
if (!dbname) {
dbname = "";
}
err = sqlite3_open(dbname, &g_db);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA journal_mode=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "PRAGMA synchronous=OFF", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_exec(g_db, "CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, data TEXT NOT NULL)", NULL, NULL, NULL);
assert(err == SQLITE_OK);
err = sqlite3_prepare_v2(g_db, "INSERT INTO orders (data) VALUES (?)", -1, &g_stmt_insert, NULL);
assert(err == SQLITE_OK);
err = sqlite3_prepare_v2(g_db, "SELECT data FROM orders WHERE id = ?", -1, &g_stmt_select, NULL);
assert(err == SQLITE_OK);
}
void db_driver_close(void)
{
int err;
err = sqlite3_finalize(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_finalize(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_close(g_db);
assert(err == SQLITE_OK);
}

View File

@ -637,7 +637,7 @@ static void test_infinitive_from_config(CuTest *tc) {
CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang));
ord = create_order(K_STUDY, 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); free_order(ord);
cJSON_Delete(json); cJSON_Delete(json);
test_cleanup(); test_cleanup();

View File

@ -14,6 +14,7 @@
#include <kernel/config.h> #include <kernel/config.h>
#include "order.h" #include "order.h"
#include "orderdb.h"
#include "skill.h" #include "skill.h"
#include "keyword.h" #include "keyword.h"
@ -31,31 +32,7 @@
#include <string.h> #include <string.h>
# define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF)
# define ORD_LOCALE(ord) locale_array[(ord)->data->_lindex]->lang # define OD_STRING(odata) ((odata) ? (odata)->_str : NULL)
# define ORD_STRING(ord) (ord)->data->_str
typedef struct locale_data {
struct order_data *short_orders;
struct order_data *study_orders[MAXSKILLS];
const struct locale *lang;
} locale_data;
static struct locale_data *locale_array[MAXLOCALES];
typedef struct order_data {
const char *_str;
int _refcount;
int _lindex;
} order_data;
static void release_data(order_data * data)
{
if (data) {
if (--data->_refcount == 0) {
free(data);
}
}
}
void replace_order(order ** dlist, order * orig, const order * src) void replace_order(order ** dlist, order * orig, const order * src)
{ {
@ -64,7 +41,7 @@ void replace_order(order ** dlist, order * orig, const order * src)
assert(dlist); assert(dlist);
while (*dlist != NULL) { while (*dlist != NULL) {
order *dst = *dlist; order *dst = *dlist;
if (dst->data == orig->data) { if (dst->id == orig->id) {
order *cpy = copy_order(src); order *cpy = copy_order(src);
*dlist = cpy; *dlist = cpy;
cpy->next = dst->next; cpy->next = dst->next;
@ -87,9 +64,10 @@ 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.
*/ */
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; char *bufp = sbuffer;
const char *text = ORD_STRING(ord); order_data *od = NULL;
const char * text;
keyword_t kwd = ORD_KEYWORD(ord); keyword_t kwd = ORD_KEYWORD(ord);
int bytes; int bytes;
@ -111,8 +89,16 @@ char* get_command(const order *ord, char *sbuffer, size_t size) {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
if (ord->id < 0) {
skill_t sk = (skill_t)(100+ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS);
text = skillname(sk, lang);
} else {
od = odata_load(ord->id);
text = OD_STRING(od);
}
if (kwd != NOKEYWORD) { if (kwd != NOKEYWORD) {
const struct locale *lang = ORD_LOCALE(ord);
if (size > 0) { if (size > 0) {
const char *str = (const char *)LOC(lang, keyword(kwd)); const char *str = (const char *)LOC(lang, keyword(kwd));
assert(str); assert(str);
@ -142,6 +128,9 @@ char* get_command(const order *ord, char *sbuffer, size_t size) {
} }
} }
} }
if (od) {
odata_release(od);
}
if (size > 0) *bufp = 0; if (size > 0) *bufp = 0;
return sbuffer; return sbuffer;
} }
@ -150,8 +139,6 @@ void free_order(order * ord)
{ {
if (ord != NULL) { if (ord != NULL) {
assert(ord->next == 0); assert(ord->next == 0);
release_data(ord->data);
free(ord); free(ord);
} }
} }
@ -162,8 +149,7 @@ order *copy_order(const order * src)
order *ord = (order *)malloc(sizeof(order)); order *ord = (order *)malloc(sizeof(order));
ord->next = NULL; ord->next = NULL;
ord->command = src->command; ord->command = src->command;
ord->data = src->data; ord->id = src->id;
++ord->data->_refcount;
return ord; return ord;
} }
return NULL; return NULL;
@ -187,123 +173,46 @@ void free_orders(order ** olist)
} }
} }
static char *mkdata(order_data **pdata, size_t len, int lindex, const char *str) static int create_data(keyword_t kwd, const char *s,
const struct locale *lang)
{ {
order_data *data; order_data *data;
char *result; int id;
data = malloc(sizeof(order_data) + len + 1);
result = (char *)(data + 1);
data->_lindex = lindex;
data->_refcount = 0;
data->_str = 0;
data->_str = (len > 0) ? result : 0;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
return result;
}
static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) assert(kwd!=NOKEYWORD);
{
const char *s = sptr;
order_data *data;
const struct locale *lang = locale_array[lindex]->lang;
if (kwd != NOKEYWORD) if (!s || *s == 0) {
s = (*sptr) ? sptr : NULL; return 0;
}
/* learning, only one order_data per skill required */ if (kwd==K_STUDY) {
if (kwd == K_STUDY) { const char * sptr = s;
skill_t sk = get_skill(parse_token_depr(&sptr), lang); skill_t sk = get_skill(parse_token_depr(&sptr), lang);
switch (sk) { if (sk != SK_MAGIC && sk != NOSKILL) {
case NOSKILL: /* fehler */ return ((int)sk)-100;
break;
case SK_MAGIC: /* kann parameter haben */
if (*sptr != 0)
break;
default: /* nur skill als Parameter, keine extras */
data = locale_array[lindex]->study_orders[sk];
if (data == NULL) {
const char *skname = skillname(sk, lang);
const char *spc = strchr(skname, ' ');
size_t len = strlen(skname);
char *dst = mkdata(&data, len + (spc ? 3 : 0), lindex, spc ? 0 : skname);
locale_array[lindex]->study_orders[sk] = data;
if (spc) {
dst[0] = '\"';
memcpy(dst + 1, skname, len);
dst[len + 1] = '\"';
dst[len + 2] = '\0';
}
data->_refcount = 1;
}
++data->_refcount;
return data;
}
}
/* orders with no parameter, only one order_data per order required */
else if (kwd != NOKEYWORD && *sptr == 0) {
data = locale_array[lindex]->short_orders;
if (data == NULL) {
mkdata(&data, 0, lindex, 0);
data->_refcount = 1;
locale_array[lindex]->short_orders = data;
}
++data->_refcount;
return data;
}
mkdata(&data, s ? strlen(s) : 0, lindex, s);
data->_refcount = 1;
return data;
}
static void clear_localedata(int lindex) {
int i;
release_data(locale_array[lindex]->short_orders);
locale_array[lindex]->short_orders = NULL;
for (i = 0; i != MAXSKILLS; ++i) {
release_data(locale_array[lindex]->study_orders[i]);
locale_array[lindex]->study_orders[i] = 0;
}
locale_array[lindex]->lang = 0;
}
void close_orders(void) {
int i;
for (i = 0; i != MAXLOCALES; ++i) {
if (locale_array[i]){
clear_localedata(i);
free(locale_array[i]);
locale_array[i] = 0;
} }
} }
/* TODO: between mkdata and odata_release, this object is very
* short-lived. */
odata_create(&data, strlen(s), s);
id = odata_save(data);
odata_release(data);
return id;
} }
static void create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, static void create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent,
bool noerror, const struct locale *lang) bool noerror, const struct locale *lang)
{ {
int lindex;
assert(ord); assert(ord);
assert(kwd != NOKEYWORD && !keyword_disabled(kwd)); assert(kwd != NOKEYWORD && !keyword_disabled(kwd));
lindex = locale_index(lang);
assert(lindex < MAXLOCALES);
if (!locale_array[lindex]) {
locale_array[lindex] = (locale_data *)calloc(1, sizeof(locale_data));
}
else if (locale_array[lindex]->lang != lang) {
clear_localedata(lindex);
}
locale_array[lindex]->lang = lang;
ord->command = (int)kwd; ord->command = (int)kwd;
if (persistent) ord->command |= CMD_PERSIST; if (persistent) ord->command |= CMD_PERSIST;
if (noerror) ord->command |= CMD_QUIET; if (noerror) ord->command |= CMD_QUIET;
ord->next = NULL; ord->next = NULL;
while (isspace(*(unsigned char *)sptr)) ++sptr; while (isspace(*(unsigned char *)sptr)) ++sptr;
ord->data = create_data(kwd, sptr, lindex);
ord->id = create_data(kwd, sptr, lang);
} }
order *create_order(keyword_t kwd, const struct locale * lang, order *create_order(keyword_t kwd, const struct locale * lang,
@ -551,7 +460,7 @@ bool is_silent(const order * ord)
return (ord->command & CMD_QUIET) != 0; 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) { if (ord == 0) {
buffer[0] = 0; buffer[0] = 0;
@ -559,12 +468,14 @@ char *write_order(const order * ord, char *buffer, size_t size)
else { else {
keyword_t kwd = ORD_KEYWORD(ord); keyword_t kwd = ORD_KEYWORD(ord);
if (kwd == NOKEYWORD) { if (kwd == NOKEYWORD) {
const char *text = ORD_STRING(ord); order_data *od = odata_load(ord->id);
const char *text = OD_STRING(od);
if (text) strlcpy(buffer, (const char *)text, size); if (text) strlcpy(buffer, (const char *)text, size);
else buffer[0] = 0; else buffer[0] = 0;
odata_release(od);
} }
else { else {
get_command(ord, buffer, size); get_command(ord, lang, buffer, size);
} }
} }
return buffer; return buffer;
@ -577,9 +488,54 @@ void push_order(order ** ordp, order * ord)
*ordp = ord; *ordp = ord;
} }
keyword_t init_order(const struct order *ord) static order_data *parser_od;
keyword_t init_order(const struct order *ord, const struct locale *lang)
{ {
assert(ord && ord->data); if (!ord) {
init_tokens_str(ord->data->_str); odata_release(parser_od);
return ORD_KEYWORD(ord); parser_od = NULL;
return NOKEYWORD;
}
else {
keyword_t kwd = ORD_KEYWORD(ord);
if (parser_od) {
/* TODO: warning */
odata_release(parser_od);
parser_od = NULL;
}
if (ord->id < 0) {
skill_t sk = (skill_t)(100 + ord->id);
assert(sk < MAXSKILLS);
assert(lang);
assert(kwd == K_STUDY);
init_tokens_str(skillname(sk, lang));
}
else {
const char *str;
parser_od = odata_load(ord->id);
if (parser_od) {
odata_addref(parser_od);
}
str = OD_STRING(parser_od);
init_tokens_ex(str, parser_od, (void(*)(void *))odata_release);
}
return kwd;
}
} }
keyword_t init_order_depr(const struct order *ord)
{
if (ord) {
keyword_t kwd = ORD_KEYWORD(ord);
assert(kwd != K_STUDY);
}
return init_order(ord, NULL);
}
void close_orders(void) {
if (parser_od) {
(void)init_order(NULL, NULL);
}
}

View File

@ -21,6 +21,8 @@
extern "C" { extern "C" {
#endif #endif
struct locale;
/* Encapsulation of an order /* Encapsulation of an order
* *
* This structure contains one order given by a unit. These used to be * This structure contains one order given by a unit. These used to be
@ -30,8 +32,6 @@ extern "C" {
* implemented yet) saving approx. 50% of all string-related memory. * implemented yet) saving approx. 50% of all string-related memory.
*/ */
struct order_data;
#define CMD_QUIET 0x010000 #define CMD_QUIET 0x010000
#define CMD_PERSIST 0x020000 #define CMD_PERSIST 0x020000
#define CMD_DEFAULT 0x040000 #define CMD_DEFAULT 0x040000
@ -39,7 +39,7 @@ extern "C" {
typedef struct order { typedef struct order {
struct order *next; struct order *next;
/* do not access this data: */ /* do not access this data: */
struct order_data *data; int id;
int command; int command;
} order; } order;
@ -59,15 +59,18 @@ extern "C" {
/* access functions for orders */ /* access functions for orders */
keyword_t getkeyword(const order * ord); keyword_t getkeyword(const order * ord);
void set_order(order ** destp, order * src); 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_persistent(const order * ord);
bool is_silent(const order * ord); bool is_silent(const order * ord);
bool is_exclusive(const order * ord); bool is_exclusive(const order * ord);
bool is_repeated(keyword_t kwd); bool is_repeated(keyword_t kwd);
bool is_long(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,
keyword_t init_order(const struct order *ord); char *buffer, size_t size);
keyword_t init_order_depr(const struct order *ord);
keyword_t init_order(const struct order *ord, const struct locale *lang);
void close_orders(void); void close_orders(void);

View File

@ -2,6 +2,8 @@
#include <kernel/config.h> #include <kernel/config.h>
#include "order.h" #include "order.h"
#include <kernel/skills.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/language.h> #include <util/language.h>
@ -20,9 +22,9 @@ static void test_create_order(CuTest *tc) {
ord = create_order(K_MOVE, lang, "NORTH"); ord = create_order(K_MOVE, lang, "NORTH");
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertIntEquals(tc, K_MOVE, getkeyword(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)); CuAssertIntEquals(tc, K_MOVE, init_order_depr(ord));
CuAssertStrEquals(tc, "NORTH", getstrtoken()); CuAssertStrEquals(tc, "NORTH", getstrtoken());
free_order(ord); free_order(ord);
test_cleanup(); test_cleanup();
@ -40,43 +42,43 @@ static void test_parse_order(CuTest *tc) {
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertIntEquals(tc, K_MOVE, ord->command); CuAssertIntEquals(tc, K_MOVE, ord->command);
CuAssertIntEquals(tc, K_MOVE, getkeyword(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)); CuAssertIntEquals(tc, K_MOVE, init_order_depr(ord));
CuAssertStrEquals(tc, "NORTH", getstrtoken()); CuAssertStrEquals(tc, "NORTH", getstrtoken());
free_order(ord); free_order(ord);
ord = parse_order("!MOVE NORTH", lang); ord = parse_order("!MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data); CuAssertTrue(tc, ord->id > 0);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_QUIET, ord->command); CuAssertIntEquals(tc, K_MOVE | CMD_QUIET, ord->command);
free_order(ord); free_order(ord);
ord = parse_order("@MOVE NORTH", lang); ord = parse_order("@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data); CuAssertTrue(tc, ord->id > 0);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST, ord->command); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST, ord->command);
free_order(ord); free_order(ord);
ord = parse_order("!@MOVE NORTH", lang); ord = parse_order("!@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data); CuAssertTrue(tc, ord->id > 0);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord); free_order(ord);
ord = parse_order("@!MOVE NORTH", lang); ord = parse_order("@!MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data); CuAssertTrue(tc, ord->id > 0);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord); free_order(ord);
ord = parse_order(" !@MOVE NORTH", lang); ord = parse_order(" !@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data); CuAssertTrue(tc, ord->id > 0);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord); free_order(ord);
@ -98,9 +100,9 @@ static void test_parse_make(CuTest *tc) {
ord = parse_order("M hurrdurr", lang); ord = parse_order("M hurrdurr", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertIntEquals(tc, K_MAKE, getkeyword(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)); CuAssertIntEquals(tc, K_MAKE, init_order_depr(ord));
CuAssertStrEquals(tc, "hurrdurr", getstrtoken()); CuAssertStrEquals(tc, "hurrdurr", getstrtoken());
free_order(ord); free_order(ord);
test_cleanup(); test_cleanup();
@ -121,9 +123,9 @@ static void test_parse_make_temp(CuTest *tc) {
ord = parse_order("M T herp", lang); ord = parse_order("M T herp", lang);
CuAssertPtrNotNull(tc, ord); CuAssertPtrNotNull(tc, ord);
CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(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)); CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord));
CuAssertStrEquals(tc, "herp", getstrtoken()); CuAssertStrEquals(tc, "herp", getstrtoken());
free_order(ord); free_order(ord);
test_cleanup(); test_cleanup();
@ -144,9 +146,9 @@ static void test_parse_maketemp(CuTest *tc) {
ord = parse_order("MAKET herp", lang); ord = parse_order("MAKET herp", lang);
CuAssertPtrNotNull(tc, ord); 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, getkeyword(ord));
CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord));
CuAssertStrEquals(tc, "herp", getstrtoken()); CuAssertStrEquals(tc, "herp", getstrtoken());
free_order(ord); free_order(ord);
test_cleanup(); test_cleanup();
@ -160,7 +162,7 @@ static void test_init_order(CuTest *tc) {
lang = get_or_create_locale("en"); lang = get_or_create_locale("en");
ord = create_order(K_MAKETEMP, lang, "hurr durr"); ord = create_order(K_MAKETEMP, lang, "hurr durr");
CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord));
CuAssertStrEquals(tc, "hurr", getstrtoken()); CuAssertStrEquals(tc, "hurr", getstrtoken());
CuAssertStrEquals(tc, "durr", getstrtoken()); CuAssertStrEquals(tc, "durr", getstrtoken());
free_order(ord); free_order(ord);
@ -213,13 +215,13 @@ static void test_get_command(CuTest *tc) {
test_setup(); test_setup();
lang = test_create_locale(); lang = test_create_locale();
ord = create_order(K_MAKE, lang, "iron"); 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; 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; 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; 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); free_order(ord);
test_cleanup(); test_cleanup();
} }
@ -292,10 +294,48 @@ static void test_is_silent(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_study_orders(CuTest *tc) {
order *ord;
struct locale *lang;
const char *s;
char token[16];
test_setup();
lang = test_create_locale();
ord = create_order(K_STUDY, lang, skillname(SK_CROSSBOW, lang));
CuAssertIntEquals(tc, K_STUDY, getkeyword(ord));
CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang));
s = gettoken(token, sizeof(token));
CuAssertStrEquals(tc, skillname(SK_CROSSBOW, lang), s);
CuAssertPtrEquals(tc, NULL, (void *)getstrtoken());
free_order(ord);
ord = create_order(K_STUDY, lang, skillname(SK_MAGIC, lang));
CuAssertIntEquals(tc, K_STUDY, getkeyword(ord));
CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang));
s = gettoken(token, sizeof(token));
CuAssertStrEquals(tc, skillname(SK_MAGIC, lang), s);
CuAssertPtrEquals(tc, NULL, (void *)getstrtoken());
free_order(ord);
ord = create_order(K_STUDY, lang, "%s 100", skillname(SK_MAGIC, lang));
CuAssertIntEquals(tc, K_STUDY, getkeyword(ord));
CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang));
s = gettoken(token, sizeof(token));
CuAssertStrEquals(tc, skillname(SK_MAGIC, lang), s);
CuAssertIntEquals(tc, 100, getint());
free_order(ord);
test_cleanup();
}
CuSuite *get_order_suite(void) CuSuite *get_order_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_create_order); SUITE_ADD_TEST(suite, test_create_order);
SUITE_ADD_TEST(suite, test_study_orders);
SUITE_ADD_TEST(suite, test_parse_order); SUITE_ADD_TEST(suite, test_parse_order);
SUITE_ADD_TEST(suite, test_parse_make); SUITE_ADD_TEST(suite, test_parse_make);
SUITE_ADD_TEST(suite, test_parse_make_temp); SUITE_ADD_TEST(suite, test_parse_make_temp);

47
src/kernel/orderdb.c Normal file
View File

@ -0,0 +1,47 @@
#include <platform.h>
#include "database.h"
#include "orderdb.h"
#include <util/log.h>
#include <critbit.h>
#include <stdlib.h>
#include <string.h>
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
}
void odata_release(order_data * od)
{
if (od) {
if (--od->_refcount == 0) {
free(od);
}
}
}
void odata_addref(order_data *od)
{
++od->_refcount;
}
order_data *odata_load(int id)
{
return dblib_load_order(id);
}
int odata_save(order_data *od)
{
return dblib_save_order(od);
}

25
src/kernel/orderdb.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef H_ORDERDB
#define H_ORDERDB
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct order_data {
const char *_str;
int _refcount;
} order_data;
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);
void odata_addref(order_data *od);
order_data *odata_load(int id);
int odata_save(order_data *od);
#ifdef __cplusplus
}
#endif
#endif

28
src/kernel/orderdb.test.c Normal file
View File

@ -0,0 +1,28 @@
#include <platform.h>
#include <kernel/config.h>
#include "orderdb.h"
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_orderdb(CuTest *tc) {
order_data *od = NULL;
const char * s = "GIB enno 1 Hodor";
odata_create(&od, strlen(s) + 1, s);
CuAssertPtrNotNull(tc, od);
CuAssertStrEquals(tc, s, od->_str);
CuAssertTrue(tc, od->_refcount >= 1);
odata_release(od);
}
CuSuite *get_orderdb_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_orderdb);
return suite;
}

View File

@ -9,7 +9,7 @@ static void test_plane(CuTest *tc) {
struct region *r; struct region *r;
plane *pl; plane *pl;
test_cleanup(); test_setup();
r = test_create_region(0, 0, 0); r = test_create_region(0, 0, 0);
CuAssertPtrEquals(tc, 0, findplane(0, 0)); CuAssertPtrEquals(tc, 0, findplane(0, 0));
CuAssertPtrEquals(tc, 0, getplane(r)); CuAssertPtrEquals(tc, 0, getplane(r));
@ -38,13 +38,14 @@ static void test_plane(CuTest *tc) {
CuAssertIntEquals(tc, 60, plane_center_y(pl)); CuAssertIntEquals(tc, 60, plane_center_y(pl));
CuAssertIntEquals(tc, 5, plane_width(pl)); CuAssertIntEquals(tc, 5, plane_width(pl));
CuAssertIntEquals(tc, 41, plane_height(pl)); CuAssertIntEquals(tc, 41, plane_height(pl));
test_cleanup();
} }
static void test_origin(CuTest *tc) { static void test_origin(CuTest *tc) {
struct faction *f; struct faction *f;
int x, y; int x, y;
test_cleanup(); test_setup();
f = test_create_faction(0); f = test_create_faction(0);
x = 0; x = 0;
y = 0; y = 0;

View File

@ -379,7 +379,7 @@ static void writeorder(gamedata *data, const struct order *ord,
const struct locale *lang) const struct locale *lang)
{ {
char obuf[1024]; char obuf[1024];
write_order(ord, obuf, sizeof(obuf)); write_order(ord, lang, obuf, sizeof(obuf));
if (obuf[0]) if (obuf[0])
WRITE_STR(data->store, obuf); WRITE_STR(data->store, obuf);
} }

View File

@ -1733,38 +1733,22 @@ int unit_max_hp(const unit * u)
void scale_number(unit * u, int n) void scale_number(unit * u, int n)
{ {
const attrib *a; if (n == u->number) {
int remain;
if (n == u->number)
return; return;
if (n && u->number > 0) {
int full;
remain = ((u->hp % u->number) * (n % u->number)) % u->number;
full = u->hp / u->number; /* wieviel kriegt jede person mindestens */
u->hp = full * n + (u->hp - full * u->number) * n / u->number;
assert(u->hp >= 0);
if ((rng_int() % u->number) < remain)
++u->hp; /* Nachkommastellen */
}
else {
remain = 0;
u->hp = 0;
} }
if (u->number > 0) { if (u->number > 0) {
for (a = a_find(u->attribs, &at_effect); a && a->type == &at_effect; if (n>0) {
a = a->next) { const attrib *a = a_find(u->attribs, &at_effect);
effect_data *data = (effect_data *)a->data.v;
int snew = data->value / u->number * n; u->hp = (long long)u->hp * n / u->number;
if (n) {
remain = data->value - snew / n * u->number; for (; a && a->type == &at_effect; a = a->next) {
snew += remain * n / u->number; effect_data *data = (effect_data *)a->data.v;
remain = (remain * n) % u->number; data->value = (long long)data->value * n / u->number;
if ((rng_int() % u->number) < remain)
++snew; /* Nachkommastellen */
} }
data->value = snew; }
else {
u->hp = 0;
} }
} }
if (u->number == 0 || n == 0) { if (u->number == 0 || n == 0) {

View File

@ -112,7 +112,7 @@ extern "C" {
int flags; int flags;
struct attrib *attribs; struct attrib *attribs;
status_t status; status_t status;
int n; /* helper temporariy variable, used in econmy, enno: attribut? */ int n; /* helper temporary variable, used in economy, enno: attribut? */
int wants; /* enno: attribut? */ int wants; /* enno: attribut? */
} unit; } unit;

View File

@ -106,13 +106,22 @@ static void test_scale_number(CuTest *tc) {
ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1); ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
change_effect(u, ptype, 1); change_effect(u, ptype, 1);
u->hp = 35;
CuAssertIntEquals(tc, 1, u->number); CuAssertIntEquals(tc, 1, u->number);
CuAssertIntEquals(tc, 20, u->hp); CuAssertIntEquals(tc, 35, u->hp);
CuAssertIntEquals(tc, 1, get_effect(u, ptype)); CuAssertIntEquals(tc, 1, get_effect(u, ptype));
scale_number(u, 2); scale_number(u, 2);
CuAssertIntEquals(tc, 2, u->number); CuAssertIntEquals(tc, 2, u->number);
CuAssertIntEquals(tc, 40, u->hp); CuAssertIntEquals(tc, 35 * u->number, u->hp);
CuAssertIntEquals(tc, 2, get_effect(u, ptype)); CuAssertIntEquals(tc, u->number, get_effect(u, ptype));
scale_number(u, 8237);
CuAssertIntEquals(tc, 8237, u->number);
CuAssertIntEquals(tc, 35 * u->number, u->hp);
CuAssertIntEquals(tc, u->number, get_effect(u, ptype));
scale_number(u, 8100);
CuAssertIntEquals(tc, 8100, u->number);
CuAssertIntEquals(tc, 35 * u->number, u->hp);
CuAssertIntEquals(tc, u->number, get_effect(u, ptype));
set_level(u, SK_ALCHEMY, 1); set_level(u, SK_ALCHEMY, 1);
scale_number(u, 0); scale_number(u, 0);
CuAssertIntEquals(tc, 0, get_level(u, SK_ALCHEMY)); CuAssertIntEquals(tc, 0, get_level(u, SK_ALCHEMY));

View File

@ -36,7 +36,7 @@ static void test_infinitive(CuTest *tc) {
CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang));
ord = create_order(K_STUDY, 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); free_order(ord);
test_cleanup(); test_cleanup();
} }

View File

@ -921,7 +921,7 @@ int contact_cmd(unit * u, order * ord)
unit *u2; unit *u2;
int n; int n;
init_order(ord); init_order_depr(ord);
n = read_unitid(u->faction, u->region); n = read_unitid(u->faction, u->region);
u2 = findunit(n); u2 = findunit(n);
@ -971,7 +971,7 @@ int quit_cmd(unit * u, struct order *ord)
const char *passwd; const char *passwd;
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_QUIT); assert(kwd == K_QUIT);
passwd = gettoken(token, sizeof(token)); passwd = gettoken(token, sizeof(token));
if (checkpasswd(f, (const char *)passwd)) { if (checkpasswd(f, (const char *)passwd)) {
@ -979,7 +979,7 @@ int quit_cmd(unit * u, struct order *ord)
} }
else { else {
char buffer[64]; char buffer[64];
write_order(ord, buffer, sizeof(buffer)); write_order(ord, f->locale, buffer, sizeof(buffer));
cmistake(u, ord, 86, MSG_EVENT); cmistake(u, ord, 86, MSG_EVENT);
log_warning("QUIT with illegal password for faction %s: %s\n", itoa36(f->no), buffer); log_warning("QUIT with illegal password for faction %s: %s\n", itoa36(f->no), buffer);
} }
@ -1157,7 +1157,7 @@ void do_enter(struct region *r, bool is_final_attempt)
unit *ulast = NULL; unit *ulast = NULL;
const char * s; const char * s;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
p = findparam_ex(s, u->faction->locale); p = findparam_ex(s, u->faction->locale);
id = getid(); id = getid();
@ -1317,7 +1317,7 @@ int ally_cmd(unit * u, struct order *ord)
int keyword, not_kw; int keyword, not_kw;
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
f = getfaction(); f = getfaction();
if (f == NULL || is_monsters(f)) { if (f == NULL || is_monsters(f)) {
@ -1484,7 +1484,7 @@ int prefix_cmd(unit * u, struct order *ord)
for (in = pnames; in->lang != lang; in = in->next); for (in = pnames; in->lang != lang; in = in->next);
} }
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
if (!s || !*s) { if (!s || !*s) {
@ -1528,7 +1528,7 @@ int display_cmd(unit * u, struct order *ord)
const char *str; const char *str;
region *r = u->region; region *r = u->region;
init_order(ord); init_order_depr(ord);
str = gettoken(token, sizeof(token)); str = gettoken(token, sizeof(token));
switch (findparam_ex(str, u->faction->locale)) { switch (findparam_ex(str, u->faction->locale)) {
@ -1701,7 +1701,7 @@ int name_cmd(struct unit *u, struct order *ord)
bool foreign = false; bool foreign = false;
const char *str; const char *str;
init_order(ord); init_order_depr(ord);
str = gettoken(token, sizeof(token)); str = gettoken(token, sizeof(token));
p = findparam_ex(str, u->faction->locale); p = findparam_ex(str, u->faction->locale);
@ -1961,7 +1961,7 @@ int mail_cmd(unit * u, struct order *ord)
const char *s; const char *s;
int n, cont; int n, cont;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
/* Falls kein Parameter, ist das eine Einheitsnummer; /* Falls kein Parameter, ist das eine Einheitsnummer;
@ -2118,7 +2118,7 @@ int banner_cmd(unit * u, struct order *ord)
const char * s; const char * s;
free(u->faction->banner); free(u->faction->banner);
init_order(ord); init_order_depr(ord);
s = getstrtoken(); s = getstrtoken();
u->faction->banner = s ? strdup(s) : 0; u->faction->banner = s ? strdup(s) : 0;
add_message(&u->faction->msgs, msg_message("changebanner", "value", add_message(&u->faction->msgs, msg_message("changebanner", "value",
@ -2131,7 +2131,7 @@ int email_cmd(unit * u, struct order *ord)
{ {
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
s = getstrtoken(); s = getstrtoken();
if (!s || !s[0]) { if (!s || !s[0]) {
@ -2157,7 +2157,7 @@ int password_cmd(unit * u, struct order *ord)
const char *s; const char *s;
bool pwok = true; bool pwok = true;
init_order(ord); init_order_depr(ord);
s = gettoken(pwbuf, sizeof(pwbuf)); s = gettoken(pwbuf, sizeof(pwbuf));
if (!s || !*s) { if (!s || !*s) {
@ -2190,7 +2190,7 @@ int send_cmd(unit * u, struct order *ord)
const char *s; const char *s;
int option; int option;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
option = findoption(s, u->faction->locale); option = findoption(s, u->faction->locale);
@ -2525,7 +2525,7 @@ int group_cmd(unit * u, struct order *ord)
{ {
keyword_t kwd; keyword_t kwd;
kwd = init_order(ord); kwd = init_order_depr(ord);
assert(kwd == K_GROUP); assert(kwd == K_GROUP);
join_group(u, getstrtoken()); join_group(u, getstrtoken());
return 0; return 0;
@ -2535,7 +2535,7 @@ int origin_cmd(unit * u, struct order *ord)
{ {
short px, py; short px, py;
init_order(ord); init_order_depr(ord);
px = (short)getint(); px = (short)getint();
py = (short)getint(); py = (short)getint();
@ -2547,7 +2547,7 @@ int origin_cmd(unit * u, struct order *ord)
int guard_off_cmd(unit * u, struct order *ord) int guard_off_cmd(unit * u, struct order *ord)
{ {
assert(getkeyword(ord) == K_GUARD); assert(getkeyword(ord) == K_GUARD);
init_order(ord); init_order_depr(ord);
if (getparam(u->faction->locale) == P_NOT) { if (getparam(u->faction->locale) == P_NOT) {
setguard(u, false); setguard(u, false);
@ -2561,7 +2561,7 @@ int reshow_cmd(unit * u, struct order *ord)
const char *s; const char *s;
param_t p = NOPARAM; param_t p = NOPARAM;
init_order(ord); init_order_depr(ord);
s = gettoken(lbuf, sizeof(lbuf)); s = gettoken(lbuf, sizeof(lbuf));
if (s && isparam(s, u->faction->locale, P_ANY)) { if (s && isparam(s, u->faction->locale, P_ANY)) {
@ -2578,7 +2578,7 @@ int status_cmd(unit * u, struct order *ord)
char token[128]; char token[128];
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
switch (findparam(s, u->faction->locale)) { switch (findparam(s, u->faction->locale)) {
case P_NOT: case P_NOT:
@ -2626,7 +2626,7 @@ int combatspell_cmd(unit * u, struct order *ord)
int level = 0; int level = 0;
spell *sp = 0; spell *sp = 0;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
/* KAMPFZAUBER [NICHT] löscht alle gesetzten Kampfzauber */ /* KAMPFZAUBER [NICHT] löscht alle gesetzten Kampfzauber */
@ -2671,7 +2671,7 @@ int guard_on_cmd(unit * u, struct order *ord)
assert(u); assert(u);
assert(u->faction); assert(u->faction);
init_order(ord); init_order_depr(ord);
/* GUARD NOT is handled in goard_off_cmd earlier in the turn */ /* GUARD NOT is handled in goard_off_cmd earlier in the turn */
if (getparam(u->faction->locale) == P_NOT) { if (getparam(u->faction->locale) == P_NOT) {
@ -2752,7 +2752,7 @@ void restack_units(void)
int id; int id;
unit *v; unit *v;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
p = findparam(s, u->faction->locale); p = findparam(s, u->faction->locale);
id = getid(); id = getid();
@ -3062,7 +3062,7 @@ void maketemp_cmd(unit *u, order **olist)
unit *u2; unit *u2;
order **ordp, **oinsert; order **ordp, **oinsert;
#ifndef NDEBUG #ifndef NDEBUG
keyword_t kwd = init_order(makeord); keyword_t kwd = init_order_depr(makeord);
assert(kwd == K_MAKETEMP); assert(kwd == K_MAKETEMP);
#endif #endif
alias = getid(); alias = getid();
@ -3360,7 +3360,7 @@ void defaultorders(void)
char lbuf[8192]; char lbuf[8192];
order *new_order = 0; order *new_order = 0;
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
s = gettoken(lbuf, sizeof(lbuf)); s = gettoken(lbuf, sizeof(lbuf));
if (s) { if (s) {
new_order = parse_order(s, u->faction->locale); new_order = parse_order(s, u->faction->locale);
@ -3476,7 +3476,7 @@ int use_cmd(unit * u, struct order *ord)
int n, err = ENOITEM; int n, err = ENOITEM;
const item_type *itype; const item_type *itype;
init_order(ord); init_order_depr(ord);
t = gettoken(token, sizeof(token)); t = gettoken(token, sizeof(token));
if (!t) { if (!t) {
@ -3531,7 +3531,7 @@ int pay_cmd(unit * u, struct order *ord)
param_t p; param_t p;
int id; int id;
init_order(ord); init_order_depr(ord);
p = getparam(u->faction->locale); p = getparam(u->faction->locale);
id = getid(); id = getid();
if (p == P_NOT) { if (p == P_NOT) {
@ -3585,7 +3585,7 @@ static int reserve_i(unit * u, struct order *ord, int flags)
const item_type *itype; const item_type *itype;
const char *s; const char *s;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
count = s ? atoip(s) : 0; count = s ? atoip(s) : 0;
para = findparam(s, u->faction->locale); para = findparam(s, u->faction->locale);
@ -3628,7 +3628,7 @@ int claim_cmd(unit * u, struct order *ord)
int n = 1; int n = 1;
const item_type *itype = 0; const item_type *itype = 0;
init_order(ord); init_order_depr(ord);
t = gettoken(token, sizeof(token)); t = gettoken(token, sizeof(token));
if (t) { if (t) {
@ -3922,7 +3922,7 @@ int siege_cmd(unit * u, order * ord)
resource_type *rt_catapultammo = NULL; resource_type *rt_catapultammo = NULL;
resource_type *rt_catapult = NULL; resource_type *rt_catapult = NULL;
init_order(ord); init_order_depr(ord);
b = getbuilding(r); b = getbuilding(r);
if (!b) { if (!b) {

View File

@ -1049,7 +1049,7 @@ static void test_long_order_normal(CuTest *tc) {
fset(u, UFL_LONGACTION); fset(u, UFL_LONGACTION);
unit_addorder(u, ord = create_order(K_MOVE, u->faction->locale, 0)); unit_addorder(u, ord = create_order(K_MOVE, u->faction->locale, 0));
update_long_order(u); update_long_order(u);
CuAssertPtrEquals(tc, ord->data, u->thisorder->data); CuAssertIntEquals(tc, ord->id, u->thisorder->id);
CuAssertIntEquals(tc, 0, fval(u, UFL_MOVED)); CuAssertIntEquals(tc, 0, fval(u, UFL_MOVED));
CuAssertIntEquals(tc, 0, fval(u, UFL_LONGACTION)); CuAssertIntEquals(tc, 0, fval(u, UFL_LONGACTION));
CuAssertPtrNotNull(tc, u->orders); CuAssertPtrNotNull(tc, u->orders);

View File

@ -2514,7 +2514,7 @@ static castorder *cast_cmd(unit * u, order * ord)
} }
level = effskill(u, SK_MAGIC, 0); level = effskill(u, SK_MAGIC, 0);
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
param = findparam(s, u->faction->locale); param = findparam(s, u->faction->locale);
/* für Syntax ' STUFE x REGION y z ' */ /* für Syntax ' STUFE x REGION y z ' */

View File

@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/log.h> #include <util/log.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/version.h> #include <kernel/version.h>
#include <kernel/save.h> #include <kernel/save.h>
#include <util/filereader.h> #include <util/filereader.h>
@ -83,6 +84,8 @@ static const char * valid_keys[] = {
"game.mailcmd", "game.mailcmd",
"game.era", "game.era",
"game.sender", "game.sender",
"game.dbname",
"game.dbbatch",
"editor.color", "editor.color",
"editor.codepage", "editor.codepage",
"editor.population.", "editor.population.",

View File

@ -31,12 +31,12 @@
extern void plan_monsters(struct faction *f); extern void plan_monsters(struct faction *f);
extern int monster_attacks(unit * monster, bool rich_only); 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]; char cmd[32];
order *ord; order *ord;
for (ord = unit->orders; ord; ord = ord->next) { for (ord = u->orders; ord; ord = ord->next) {
if (strcmp(expected, get_command(ord, cmd, sizeof(cmd))) == 0) { if (strcmp(expected, get_command(ord, u->faction->locale, cmd, sizeof(cmd))) == 0) {
return ord; return ord;
} }
} }

View File

@ -1067,7 +1067,7 @@ static void cycle_route(order * ord, unit * u, int gereist)
return; return;
tail[0] = '\0'; tail[0] = '\0';
init_order(ord); init_order_depr(ord);
neworder[0] = 0; neworder[0] = 0;
for (cm = 0;; ++cm) { for (cm = 0;; ++cm) {
@ -1144,7 +1144,7 @@ static bool transport(unit * ut, unit * u)
for (ord = ut->orders; ord; ord = ord->next) { for (ord = ut->orders; ord; ord = ord->next) {
if (getkeyword(ord) == K_TRANSPORT) { if (getkeyword(ord) == K_TRANSPORT) {
unit *u2; unit *u2;
init_order(ord); init_order_depr(ord);
getunit(ut->region, ut->faction, &u2); getunit(ut->region, ut->faction, &u2);
if (u2 == u) { if (u2 == u) {
return true; return true;
@ -1178,7 +1178,7 @@ static void init_transportation(void)
&& !fval(u, UFL_NOTMOVING) && !LongHunger(u)) { && !fval(u, UFL_NOTMOVING) && !LongHunger(u)) {
unit *ut = 0; unit *ut = 0;
init_order(u->thisorder); init_order_depr(u->thisorder);
if (getunit(r, u->faction, &ut) != GET_UNIT) { if (getunit(r, u->faction, &ut) != GET_UNIT) {
ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder,
"feedback_unit_not_found", "")); "feedback_unit_not_found", ""));
@ -1207,7 +1207,7 @@ static void init_transportation(void)
for (ord = u->orders; ord; ord = ord->next) { for (ord = u->orders; ord; ord = ord->next) {
if (getkeyword(ord) == K_TRANSPORT) { if (getkeyword(ord) == K_TRANSPORT) {
init_order(ord); init_order_depr(ord);
for (;;) { for (;;) {
unit *ut = 0; unit *ut = 0;
@ -1218,7 +1218,7 @@ static void init_transportation(void)
can_move(ut) && !fval(ut, UFL_NOTMOVING) && can_move(ut) && !fval(ut, UFL_NOTMOVING) &&
!LongHunger(ut)) { !LongHunger(ut)) {
unit *u2; unit *u2;
init_order(ut->thisorder); init_order_depr(ut->thisorder);
getunit(r, ut->faction, &u2); getunit(r, ut->faction, &u2);
if (u2 == u) { if (u2 == u) {
w += weight(ut); w += weight(ut);
@ -2062,7 +2062,7 @@ static const region_list *travel_i(unit * u, const region_list * route_begin,
if (getkeyword(ord) != K_TRANSPORT) if (getkeyword(ord) != K_TRANSPORT)
continue; continue;
init_order(ord); init_order_depr(ord);
if (getunit(r, u->faction, &ut) == GET_UNIT) { if (getunit(r, u->faction, &ut) == GET_UNIT) {
if (getkeyword(ut->thisorder) == K_DRIVE) { if (getkeyword(ut->thisorder) == K_DRIVE) {
if (ut->building && !can_leave(ut)) { if (ut->building && !can_leave(ut)) {
@ -2077,7 +2077,7 @@ static const region_list *travel_i(unit * u, const region_list * route_begin,
if (!fval(ut, UFL_NOTMOVING) && !LongHunger(ut)) { if (!fval(ut, UFL_NOTMOVING) && !LongHunger(ut)) {
unit *u2; unit *u2;
init_order(ut->thisorder); init_order_depr(ut->thisorder);
getunit(u->region, ut->faction, &u2); getunit(u->region, ut->faction, &u2);
if (u2 == u) { if (u2 == u) {
const region_list *route_to = const region_list *route_to =
@ -2363,7 +2363,7 @@ static void move_hunters(void)
if (getkeyword(ord) == K_FOLLOW) { if (getkeyword(ord) == K_FOLLOW) {
param_t p; param_t p;
init_order(ord); init_order_depr(ord);
p = getparam(u->faction->locale); p = getparam(u->faction->locale);
if (p != P_SHIP) { if (p != P_SHIP) {
if (p != P_UNIT) { if (p != P_UNIT) {
@ -2485,13 +2485,13 @@ void movement(void)
else { else {
if (ships) { if (ships) {
if (u->ship && ship_owner(u->ship) == u) { if (u->ship && ship_owner(u->ship) == u) {
init_order(u->thisorder); init_order_depr(u->thisorder);
move_cmd(u, u->thisorder); move_cmd(u, u->thisorder);
} }
} }
else { else {
if (!u->ship || ship_owner(u->ship) != u) { if (!u->ship || ship_owner(u->ship) != u) {
init_order(u->thisorder); init_order_depr(u->thisorder);
move_cmd(u, u->thisorder); move_cmd(u, u->thisorder);
} }
} }
@ -2548,7 +2548,7 @@ void follow_unit(unit * u)
if (getkeyword(ord) == K_FOLLOW) { if (getkeyword(ord) == K_FOLLOW) {
int id; int id;
param_t p; param_t p;
init_order(ord); init_order_depr(ord);
p = getparam(lang); p = getparam(lang);
if (p == P_UNIT) { if (p == P_UNIT) {
id = read_unitid(u->faction, r); id = read_unitid(u->faction, r);

View File

@ -462,7 +462,7 @@ static void test_follow_ship_msg(CuTest * tc) {
mt_register(mt_new_va("error18", "unit:unit", "region:region", "command:order", 0)); mt_register(mt_new_va("error18", "unit:unit", "region:region", "command:order", 0));
init_order(ord); init_order_depr(ord);
getstrtoken(); getstrtoken();
follow_ship(u, ord); follow_ship(u, ord);

View File

@ -86,7 +86,7 @@ int *parse_ids(const order *ord) {
const char *s; const char *s;
int *il = NULL; int *il = NULL;
init_order(ord); init_order_depr(ord);
s = getstrtoken(); s = getstrtoken();
if (s != NULL && *s) { if (s != NULL && *s) {
il = intlist_init(); il = intlist_init();
@ -211,7 +211,7 @@ void piracy_cmd(unit * u)
ord = create_order(K_MOVE, u->faction->locale, "%s", LOC(u->faction->locale, directions[target_dir])); ord = create_order(K_MOVE, u->faction->locale, "%s", LOC(u->faction->locale, directions[target_dir]));
/* Bewegung ausführen */ /* Bewegung ausführen */
init_order(ord); init_order_depr(ord);
move_cmd(u, ord); move_cmd(u, ord);
free_order(ord); free_order(ord);
} }

View File

@ -80,7 +80,7 @@ int renumber_cmd(unit * u, order * ord)
int i = 0; int i = 0;
faction *f = u->faction; faction *f = u->faction;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
switch (findparam_ex(s, u->faction->locale)) { switch (findparam_ex(s, u->faction->locale)) {

View File

@ -1389,7 +1389,8 @@ static int
report_template(const char *filename, report_context * ctx, const char *bom) report_template(const char *filename, report_context * ctx, const char *bom)
{ {
const resource_type *rsilver = get_resourcetype(R_SILVER); 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; region *r;
FILE *F = fopen(filename, "w"); FILE *F = fopen(filename, "w");
stream strm = { 0 }, *out = &strm; stream strm = { 0 }, *out = &strm;
@ -1408,11 +1409,11 @@ report_template(const char *filename, report_context * ctx, const char *bom)
} }
newline(out); newline(out);
rps_nowrap(out, LOC(f->locale, "nr_template")); rps_nowrap(out, LOC(lang, "nr_template"));
newline(out); newline(out);
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); rps_nowrap(out, buf);
newline(out); newline(out);
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); adjust_coordinates(f, &nx, &ny, pl);
newline(out); newline(out);
if (pl && pl->id != 0) { if (pl && pl->id != 0) {
sprintf(buf, "%s %d,%d,%d ; %s", LOC(f->locale, sprintf(buf, "%s %d,%d,%d ; %s", LOC(lang,
parameters[P_REGION]), nx, ny, pl->id, rname(r, f->locale)); parameters[P_REGION]), nx, ny, pl->id, rname(r, lang));
} }
else { else {
sprintf(buf, "%s %d,%d ; %s", LOC(f->locale, parameters[P_REGION]), sprintf(buf, "%s %d,%d ; %s", LOC(lang, parameters[P_REGION]),
nx, ny, rname(r, f->locale)); nx, ny, rname(r, lang));
} }
rps_nowrap(out, buf); rps_nowrap(out, buf);
newline(out); 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) { for (ord = u->old_orders; ord; ord = ord->next) {
/* this new order will replace the old defaults */ /* this new order will replace the old defaults */
strcpy(buf, " "); strcpy(buf, " ");
write_order(ord, buf + 2, sizeof(buf) - 2); write_order(ord, lang, buf + 2, sizeof(buf) - 2);
rps_nowrap(out, buf); rps_nowrap(out, buf);
newline(out); newline(out);
} }
@ -1515,7 +1516,7 @@ report_template(const char *filename, report_context * ctx, const char *bom)
continue; /* unit has defaults */ continue; /* unit has defaults */
if (is_persistent(ord)) { if (is_persistent(ord)) {
strcpy(buf, " "); strcpy(buf, " ");
write_order(ord, buf + 2, sizeof(buf) - 2); write_order(ord, lang, buf + 2, sizeof(buf) - 2);
rps_nowrap(out, buf); rps_nowrap(out, buf);
newline(out); newline(out);
} }
@ -1527,7 +1528,7 @@ report_template(const char *filename, report_context * ctx, const char *bom)
} }
} }
newline(out); 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); rps_nowrap(out, buf);
newline(out); newline(out);
fstream_done(&strm); fstream_done(&strm);

View File

@ -266,14 +266,14 @@ report_item(const unit * owner, const item * i, const faction * viewer,
} }
#define ORDERS_IN_NR 1 #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; char *bufp = buffer;
bufp = STRLCPY(bufp, ", \"", size); bufp = STRLCPY(bufp, ", \"", size);
if (mode < ORDERS_IN_NR) { if (mode < ORDERS_IN_NR) {
char cmd[ORDERSIZE]; char cmd[ORDERSIZE];
get_command(ord, cmd, sizeof(cmd)); get_command(ord, lang, cmd, sizeof(cmd));
bufp = STRLCPY(bufp, cmd, size); bufp = STRLCPY(bufp, cmd, size);
} }
else { else {
@ -498,6 +498,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
char *bufp = buf; char *bufp = buf;
int result = 0; int result = 0;
item results[MAX_INVENTORY]; item results[MAX_INVENTORY];
const struct locale *lang = f->locale;
assert(f); assert(f);
bufp = STRLCPY(bufp, unitname(u), size); 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) { if (getarnt) {
bufp = STRLCPY(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size);
} }
else if (u->attribs) { else if (u->attribs) {
faction *otherf = get_otherfaction(u); faction *otherf = get_otherfaction(u);
@ -527,7 +528,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
else { else {
if (getarnt) { if (getarnt) {
bufp = STRLCPY(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size);
} }
else { else {
if (u->attribs && alliedunit(u, f, HELP_FSTEALTH)) { 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); bufp = STRLCPY(bufp, pzTmp, size);
if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) {
bufp = STRLCPY(bufp, " (", size); 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) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
--size; --size;
@ -571,10 +572,10 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
} }
else { else {
const race *irace = u_irace(u); 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)) { if (u->faction == f && irace != u_race(u)) {
bufp = STRLCPY(bufp, " (", size); 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) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
--size; --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))) { if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) {
bufp = STRLCPY(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
bufp = STRLCPY(bufp, LOC(f->locale, "hero"), size); bufp = STRLCPY(bufp, LOC(lang, "hero"), size);
} }
/* status */ /* status */
if (u->number && (u->faction == f || isbattle)) { if (u->number && (u->faction == f || isbattle)) {
const char *c = hp_status(u); const char *c = hp_status(u);
c = c ? LOC(f->locale, c) : 0; c = c ? LOC(lang, c) : 0;
bufp = STRLCPY(bufp, ", ", size); 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)) { if (c || fval(u, UFL_HUNGER)) {
bufp = STRLCPY(bufp, " (", size); bufp = STRLCPY(bufp, " (", size);
if (c) { if (c) {
@ -602,7 +603,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
if (c) { if (c) {
bufp = STRLCPY(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
} }
bufp = STRLCPY(bufp, LOC(f->locale, "unit_hungers"), size); bufp = STRLCPY(bufp, LOC(lang, "unit_hungers"), size);
} }
if (size > 1) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
@ -612,7 +613,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
} }
if (is_guard(u)) { if (is_guard(u)) {
bufp = STRLCPY(bufp, ", ", size); 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) { 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) { if (u->faction == f) {
skill *sv; skill *sv;
for (sv = u->skills; sv != u->skills + u->skill_size; ++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); assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0) if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
@ -651,7 +652,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
bufp = STRLCPY(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (!dh) { 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) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
dh = 1; dh = 1;
@ -681,7 +682,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
if (sbe->level <= maxlevel) { if (sbe->level <= maxlevel) {
int result = 0; int result = 0;
if (!header) { if (!header) {
result = snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); result = snprintf(bufp, size, ", %s: ", LOC(lang, "nr_spells"));
header = 1; header = 1;
} }
else { else {
@ -691,7 +692,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
/* TODO: no need to deref the spellref here (spref->name is good) */ /* 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) { if (i != MAXCOMBATSPELLS) {
int result = 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) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); 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); sp = get_combatspell(u, i);
if (sp) { if (sp) {
int sl = get_combatspelllevel(u, i); 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) { if (sl > 0) {
result = snprintf(bufp, size, " (%d)", sl); result = snprintf(bufp, size, " (%d)", sl);
if (wrptr(&bufp, &size, result) != 0) if (wrptr(&bufp, &size, result) != 0)
@ -725,7 +726,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
} }
} }
else { 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); keyword_t kwd = getkeyword(ord);
if (is_repeated(kwd)) { if (is_repeated(kwd)) {
if (printed < ORDERS_IN_NR) { 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) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); 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); keyword_t kwd = getkeyword(ord);
if (is_repeated(kwd)) { if (is_repeated(kwd)) {
if (printed < ORDERS_IN_NR) { 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) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
@ -762,7 +763,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode,
} }
i = 0; i = 0;
str = u_description(u, f->locale); str = u_description(u, lang);
if (str) { if (str) {
bufp = STRLCPY(bufp, "; ", size); bufp = STRLCPY(bufp, "; ", size);
bufp = STRLCPY(bufp, str, size); bufp = STRLCPY(bufp, str, size);
@ -1984,13 +1985,15 @@ static void eval_race(struct opstack **stack, const void *userdata)
static void eval_order(struct opstack **stack, const void *userdata) static void eval_order(struct opstack **stack, const void *userdata)
{ /* order -> string */ { /* order -> string */
const faction *f = (const faction *)userdata;
const struct order *ord = (const struct order *)opop(stack).v; const struct order *ord = (const struct order *)opop(stack).v;
char buf[4096]; char buf[4096];
size_t len; size_t len;
variant var; variant var;
const struct locale *lang = f ? f->locale : default_locale;
UNUSED_ARG(userdata); UNUSED_ARG(userdata);
write_order(ord, buf, sizeof(buf)); write_order(ord, lang, buf, sizeof(buf));
len = strlen(buf); len = strlen(buf);
var.v = strcpy(balloc(len + 1), buf); var.v = strcpy(balloc(len + 1), buf);
opush(stack, var); opush(stack, var);
@ -1998,8 +2001,8 @@ static void eval_order(struct opstack **stack, const void *userdata)
static void eval_resources(struct opstack **stack, const void *userdata) static void eval_resources(struct opstack **stack, const void *userdata)
{ /* order -> string */ { /* order -> string */
const faction *report = (const faction *)userdata; const faction *f = (const faction *)userdata;
const struct locale *lang = report ? report->locale : default_locale; const struct locale *lang = f ? f->locale : default_locale;
const struct resource *res = (const struct resource *)opop(stack).v; const struct resource *res = (const struct resource *)opop(stack).v;
char buf[1024]; /* but we only use about half of this */ char buf[1024]; /* but we only use about half of this */
size_t size = sizeof(buf) - 1; size_t size = sizeof(buf) - 1;

View File

@ -126,7 +126,7 @@ int spy_cmd(unit * u, struct order *ord)
double spychance, observechance; double spychance, observechance;
region *r = u->region; region *r = u->region;
init_order(ord); init_order_depr(ord);
getunit(r, u->faction, &target); getunit(r, u->faction, &target);
if (!target) { if (!target) {
@ -219,7 +219,7 @@ int setstealth_cmd(unit * u, struct order *ord)
const char *s; const char *s;
int level; int level;
init_order(ord); init_order_depr(ord);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
/* Tarne ohne Parameter: Setzt maximale Tarnung */ /* Tarne ohne Parameter: Setzt maximale Tarnung */
@ -494,10 +494,10 @@ int sabotage_cmd(unit * u, struct order *ord)
assert(u); assert(u);
assert(ord); assert(ord);
init_order(ord); init_order_depr(ord);
s = getstrtoken(); s = getstrtoken();
p = findparam(s, u->faction->locale); p = findparam(s, u->faction->locale);
init_order_depr(NULL);
switch (p) { switch (p) {
case P_SHIP: case P_SHIP:

View File

@ -23,48 +23,6 @@ faction *get_faction_by_id(int uid)
return NULL; return NULL;
} }
/*
typedef struct stmt_cache {
sqlite3 *db;
sqlite3_stmt *stmt;
const char *sql;
int inuse;
} stmt_cache;
#define MAX_STMT_CACHE 64
static stmt_cache cache[MAX_STMT_CACHE];
static int cache_insert;
static sqlite3_stmt *stmt_cache_get(sqlite3 * db, const char *sql)
{
int i;
sqlite3_stmt *stmt;
for (i = 0; i != MAX_STMT_CACHE && cache[i].db; ++i) {
if (cache[i].sql == sql && cache[i].db == db) {
cache[i].inuse = 1;
stmt = cache[i].stmt;
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
return stmt;
}
}
if (i == MAX_STMT_CACHE) {
while (cache[cache_insert].inuse) {
cache[cache_insert].inuse = 0;
cache_insert = (cache_insert + 1) & (MAX_STMT_CACHE - 1);
}
i = cache_insert;
stmt = cache[i].stmt;
sqlite3_finalize(stmt);
}
cache[i].inuse = 1;
cache[i].db = db;
cache[i].sql = sql;
sqlite3_prepare_v2(db, sql, -1, &cache[i].stmt, NULL);
return cache[i].stmt;
}
*/
typedef struct db_faction { typedef struct db_faction {
int uid; int uid;
int no; int no;

View File

@ -283,7 +283,7 @@ int teach_cmd(unit * teacher, struct order *ord)
count = 0; count = 0;
init_order(ord); init_order_depr(ord);
#if TEACH_ALL #if TEACH_ALL
if (getparam(teacher->faction->locale) == P_ANY) { if (getparam(teacher->faction->locale) == P_ANY) {
@ -304,7 +304,7 @@ int teach_cmd(unit * teacher, struct order *ord)
else if (student->faction == teacher->faction) { else if (student->faction == teacher->faction) {
if (getkeyword(student->thisorder) == K_STUDY) { if (getkeyword(student->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
init_order(student->thisorder); init_order(student->thisorder, student->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(student->faction->locale);
if (sk != NOSKILL && teachskill[0] != NOSKILL) { if (sk != NOSKILL && teachskill[0] != NOSKILL) {
for (t = 0; teachskill[t] != NOSKILL; ++t) { for (t = 0; teachskill[t] != NOSKILL; ++t) {
@ -324,7 +324,7 @@ int teach_cmd(unit * teacher, struct order *ord)
else if (alliedunit(teacher, student->faction, HELP_GUARD)) { else if (alliedunit(teacher, student->faction, HELP_GUARD)) {
if (getkeyword(student->thisorder) == K_STUDY) { if (getkeyword(student->thisorder) == K_STUDY) {
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
init_order(student->thisorder); init_order(student->thisorder, student->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(student->faction->locale);
if (sk != NOSKILL if (sk != NOSKILL
&& effskill_study(teacher, sk, 0) - TEACHDIFFERENCE >= effskill(student, sk, 0)) { && effskill_study(teacher, sk, 0) - TEACHDIFFERENCE >= effskill(student, sk, 0)) {
@ -343,7 +343,7 @@ int teach_cmd(unit * teacher, struct order *ord)
order *new_order; order *new_order;
zOrder[0] = '\0'; zOrder[0] = '\0';
init_order(ord); init_order_depr(ord);
while (!parser_end()) { while (!parser_end()) {
skill_t sk; skill_t sk;
@ -361,7 +361,7 @@ int teach_cmd(unit * teacher, struct order *ord)
const char *token; const char *token;
/* Finde den string, der den Fehler verursacht hat */ /* Finde den string, der den Fehler verursacht hat */
parser_pushstate(); parser_pushstate();
init_order(ord); init_order_depr(ord);
for (j = 0; j != count - 1; ++j) { for (j = 0; j != count - 1; ++j) {
/* skip over the first 'count' units */ /* skip over the first 'count' units */
@ -406,7 +406,7 @@ int teach_cmd(unit * teacher, struct order *ord)
/* Input ist nun von student->thisorder !! */ /* Input ist nun von student->thisorder !! */
parser_pushstate(); parser_pushstate();
init_order(student->thisorder); init_order(student->thisorder, student->faction->locale);
sk = getskill(student->faction->locale); sk = getskill(student->faction->locale);
parser_popstate(); parser_popstate();
@ -448,6 +448,7 @@ int teach_cmd(unit * teacher, struct order *ord)
if (academy_students > 0 && sk_academy!=NOSKILL) { if (academy_students > 0 && sk_academy!=NOSKILL) {
academy_teaching_bonus(teacher, sk_academy, academy_students); academy_teaching_bonus(teacher, sk_academy, academy_students);
} }
init_order_depr(NULL);
return 0; return 0;
} }
@ -556,7 +557,7 @@ int study_cmd(unit * u, order * ord)
return 0; return 0;
} }
init_order(ord); (void)init_order(ord, u->faction->locale);
sk = getskill(u->faction->locale); sk = getskill(u->faction->locale);
if (sk < 0) { if (sk < 0) {
@ -784,7 +785,7 @@ int study_cmd(unit * u, order * ord)
mage = create_mage(u, u->faction->magiegebiet); mage = create_mage(u, u->faction->magiegebiet);
} }
} }
init_order_depr(NULL);
return 0; return 0;
} }

View File

@ -1,6 +1,7 @@
#include <platform.h> #include <platform.h>
#include <eressea.h> #include <eressea.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/database.h>
#include <CuTest.h> #include <CuTest.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -93,6 +94,7 @@ int RunAllTests(int argc, char *argv[])
/* kernel */ /* kernel */
ADD_SUITE(alliance); ADD_SUITE(alliance);
ADD_SUITE(command); ADD_SUITE(command);
ADD_SUITE(db);
ADD_SUITE(plane); ADD_SUITE(plane);
ADD_SUITE(unit); ADD_SUITE(unit);
ADD_SUITE(faction); ADD_SUITE(faction);
@ -132,6 +134,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(monsters); ADD_SUITE(monsters);
ADD_SUITE(move); ADD_SUITE(move);
ADD_SUITE(names); ADD_SUITE(names);
ADD_SUITE(orderdb);
ADD_SUITE(orderfile); ADD_SUITE(orderfile);
ADD_SUITE(otherfaction); ADD_SUITE(otherfaction);
ADD_SUITE(piracy); ADD_SUITE(piracy);

View File

@ -218,6 +218,7 @@ void log_fatal(const char *format, ...)
va_start(args, format); va_start(args, format);
log_write(LOG_CPERROR, NULL, format, args); log_write(LOG_CPERROR, NULL, format, args);
va_end(args); va_end(args);
abort();
} }
void log_error(const char *format, ...) /*-V524 */ void log_error(const char *format, ...) /*-V524 */

View File

@ -16,6 +16,8 @@
typedef struct parser_state { typedef struct parser_state {
const char *current_token; const char *current_token;
struct parser_state *next; struct parser_state *next;
void *data;
void(*dtor)(void *);
} parser_state; } parser_state;
static parser_state *states; static parser_state *states;
@ -50,17 +52,26 @@ static int eatwhitespace_c(const char **str_p)
return ret; return ret;
} }
void init_tokens_str(const char *initstr) void init_tokens_ex(const char *initstr, void *data, void (*dtor)(void *))
{ {
if (states == NULL) { if (states == NULL) {
states = malloc(sizeof(parser_state)); states = calloc(1, sizeof(parser_state));
} }
else if (states->dtor) {
states->dtor(states->data);
}
states->dtor = dtor;
states->data = data;
states->current_token = initstr; states->current_token = initstr;
} }
void init_tokens_str(const char *initstr) {
init_tokens_ex(initstr, NULL, NULL);
}
void parser_pushstate(void) void parser_pushstate(void)
{ {
parser_state *new_state = malloc(sizeof(parser_state)); parser_state *new_state = calloc(1, sizeof(parser_state));
new_state->current_token = NULL; new_state->current_token = NULL;
new_state->next = states; new_state->next = states;
states = new_state; states = new_state;
@ -69,6 +80,9 @@ void parser_pushstate(void)
void parser_popstate(void) void parser_popstate(void)
{ {
parser_state *new_state = states->next; parser_state *new_state = states->next;
if (states->dtor) {
states->dtor(states->data);
}
free(states); free(states);
states = new_state; states = new_state;
} }
@ -254,7 +268,7 @@ unsigned int atoip(const char *s)
int n; int n;
assert(s); assert(s);
n = (s[0] >='0' && s[0]<='9'); n = (s[0] >= '0' && s[0] <= '9');
n = n ? atoi(s) : 0; n = n ? atoi(s) : 0;
if (n < 0) if (n < 0)

View File

@ -18,6 +18,7 @@
extern "C" { extern "C" {
#endif #endif
void init_tokens_ex(const char *initstr, void *data, void(*dtor)(void *));
void init_tokens_str(const char *initstr); /* initialize token parsing */ void init_tokens_str(const char *initstr); /* initialize token parsing */
void skip_token(void); void skip_token(void);
const char *parse_token_depr(const char **str); const char *parse_token_depr(const char **str);

View File

@ -1,6 +1,7 @@
cd c:\users\enno\documents\eressea\git\tests cd c:\users\enno\documents\eressea\git\tests
"C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\eressea.exe -t184 test-turn.lua "C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\eressea.exe -t184 test-turn.lua
REM "C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\test_eressea.exe -t184 test-turn.lua
del /q reports del /q reports
del /q datum htpasswd parteien parteien.full passwd score turn del /q datum htpasswd parteien parteien.full passwd score turn