Merge branch 'develop' of github.com:ennorehling/eressea into develop

This commit is contained in:
Enno Rehling 2017-05-06 16:48:46 +02:00
commit 6ed2e5bb26
13 changed files with 79 additions and 110 deletions

View File

@ -27,7 +27,7 @@ function test_laen_needs_mine()
turn_process() turn_process()
assert_equal(0, u:get_item('laen')) assert_equal(0, u:get_item('laen'))
assert_equal(100, r:get_resource('laen')) assert_equal(100, r:get_resource('laen'))
assert_equal(1, f:count_msg_type("error104")) -- requires building assert_equal(1, f:count_msg_type("building_needed")) -- requires building
u.building = building.create(u.region, "mine") u.building = building.create(u.region, "mine")
u.building.working = true u.building.working = true

View File

@ -814,6 +814,7 @@ static struct message * get_modifiers(unit *u, skill_t sk, const resource_type *
int skill = 0; int skill = 0;
int need_race = 0, need_bldg = 0; int need_race = 0, need_bldg = 0;
resource_mod *mod; resource_mod *mod;
const struct building_type *btype_needed = NULL;
if (btype && btype->modifiers) { if (btype && btype->modifiers) {
for (mod = btype->modifiers; mod && mod->type != RMT_END; ++mod) { for (mod = btype->modifiers; mod && mod->type != RMT_END; ++mod) {
@ -838,7 +839,9 @@ static struct message * get_modifiers(unit *u, skill_t sk, const resource_type *
break; break;
case RMT_PROD_REQUIRE: case RMT_PROD_REQUIRE:
if (mod->race) need_race |= 1; if (mod->race) need_race |= 1;
if (mod->btype) need_bldg |= 1; if (mod->btype) {
need_bldg |= 1;
}
break; break;
default: default:
/* is not a production modifier, ignore it */ /* is not a production modifier, ignore it */
@ -848,14 +851,17 @@ static struct message * get_modifiers(unit *u, skill_t sk, const resource_type *
} }
if (mod->type == RMT_PROD_REQUIRE) { if (mod->type == RMT_PROD_REQUIRE) {
if (mod->race) need_race |= 2; if (mod->race) need_race |= 2;
if (mod->btype) need_bldg |= 2; if (mod->btype) {
btype_needed = mod->btype;
need_bldg |= 2;
}
} }
} }
if (need_race == 2) { if (need_race == 2) {
return msg_error(u, u->thisorder, 117); return msg_error(u, u->thisorder, 117);
} }
if (need_bldg == 2) { if (btype_needed && need_bldg == 2) {
return msg_error(u, u->thisorder, 104); return msg_feedback(u, u->thisorder, "building_needed", "building", btype_needed->_name);
} }
*skillp = skill; *skillp = skill;
if (savep) *savep = frac_make(save_n, save_d); if (savep) *savep = frac_make(save_n, save_d);
@ -885,11 +891,6 @@ static void manufacture(unit * u, const item_type * itype, int want)
ADDMSG(&u->faction->msgs, ADDMSG(&u->faction->msgs,
msg_feedback(u, u->thisorder, "skill_needed", "skill", sk)); msg_feedback(u, u->thisorder, "skill_needed", "skill", sk));
return; return;
case EBUILDINGREQ:
ADDMSG(&u->faction->msgs,
msg_feedback(u, u->thisorder, "building_needed", "building",
itype->construction->extra.btype->_name));
return;
case ELOWSKILL: case ELOWSKILL:
ADDMSG(&u->faction->msgs, ADDMSG(&u->faction->msgs,
msg_feedback(u, u->thisorder, "manufacture_skills", msg_feedback(u, u->thisorder, "manufacture_skills",
@ -1236,11 +1237,6 @@ static void create_potion(unit * u, const potion_type * ptype, int want)
/* no skill, or not enough skill points to build */ /* no skill, or not enough skill points to build */
cmistake(u, u->thisorder, 50, MSG_PRODUCE); cmistake(u, u->thisorder, 50, MSG_PRODUCE);
break; break;
case EBUILDINGREQ:
ADDMSG(&u->faction->msgs,
msg_feedback(u, u->thisorder, "building_needed", "building",
ptype->itype->construction->extra.btype->_name));
break;
case ECOMPLETE: case ECOMPLETE:
assert(0); assert(0);
break; break;
@ -2948,8 +2944,8 @@ static void peasant_taxes(region * r)
level = buildingeffsize(b, false); level = buildingeffsize(b, false);
if (level > 0) { if (level > 0) {
double taxfactor = money * level / building_taxes(b); double taxfactor = (double)money * level / building_taxes(b);
double morale = money * region_get_morale(r) / MORALE_TAX_FACTOR; double morale = (double)money * region_get_morale(r) / MORALE_TAX_FACTOR;
if (taxfactor > morale) { if (taxfactor > morale) {
taxfactor = morale; taxfactor = morale;
} }

View File

@ -527,7 +527,7 @@ static void test_modify_production(CuTest *tc) {
test_clear_messages(u->faction); test_clear_messages(u->faction);
make_item(u, itype, 10); make_item(u, itype, 10);
CuAssertIntEquals(tc, 28, get_item(u, itype)); CuAssertIntEquals(tc, 28, get_item(u, itype));
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error104")); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "building_needed"));
rtype->modifiers[0].type = RMT_PROD_REQUIRE; rtype->modifiers[0].type = RMT_PROD_REQUIRE;
rtype->modifiers[0].race = test_create_race("smurf"); rtype->modifiers[0].race = test_create_race("smurf");

View File

@ -513,17 +513,6 @@ int build(unit * u, const construction * ctype, int completed, int want, int ski
return ENOMATERIALS; return ENOMATERIALS;
if (con->improvement == NULL && completed == con->maxsize) if (con->improvement == NULL && completed == con->maxsize)
return ECOMPLETE; return ECOMPLETE;
if (con->type==CONS_ITEM && con->extra.btype) {
building *b;
if (!u->building || u->building->type != con->extra.btype) {
return EBUILDINGREQ;
}
b = inside_building(u);
if (!b || !building_is_active(b)) {
return EBUILDINGREQ;
}
}
if (con->skill != NOSKILL) { if (con->skill != NOSKILL) {
int effsk; int effsk;
int dm = get_effect(u, oldpotiontype[P_DOMORE]); int dm = get_effect(u, oldpotiontype[P_DOMORE]);

View File

@ -52,13 +52,8 @@ extern "C" {
int reqsize; /* size of object using up 1 set of requirement. */ int reqsize; /* size of object using up 1 set of requirement. */
requirement *materials; /* material req'd to build one object */ requirement *materials; /* material req'd to build one object */
union { /* only used by CONS_BUILDING: */
/* CONS_BUILDING: */ char * name; /* building level name */
char * name; /* building level name */
/* CONS_ITEM: */
const struct building_type *btype; /* building required to build item */
} extra;
struct construction *improvement; struct construction *improvement;
/* next level, if upgradable. */ /* next level, if upgradable. */
} construction; } construction;
@ -89,7 +84,6 @@ extern "C" {
#define ENEEDSKILL -2 #define ENEEDSKILL -2
#define ECOMPLETE -3 #define ECOMPLETE -3
#define ENOMATERIALS -4 #define ENOMATERIALS -4
#define EBUILDINGREQ -5
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -71,29 +71,6 @@ static void test_build_requires_materials(CuTest *tc) {
teardown_build(&bf); teardown_build(&bf);
} }
static void test_build_requires_building(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
building_type *btype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, 2);
bf.cons.type = CONS_ITEM;
bf.cons.extra.btype = btype = bt_get_or_create("hodor");
btype->maxcapacity = 1;
btype->capacity = 1;
CuAssertIntEquals_Msg(tc, "must be inside a production building", EBUILDINGREQ, build(u, &bf.cons, 0, 1, 0));
u->building = test_create_building(u->region, btype);
fset(u->building, BLD_MAINTAINED);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1, 0));
btype->maxcapacity = 0;
CuAssertIntEquals_Msg(tc, "cannot build when production building capacity exceeded", EBUILDINGREQ, build(u, &bf.cons, 0, 1, 0));
teardown_build(&bf);
}
static void test_build_failure_missing_skill(CuTest *tc) { static void test_build_failure_missing_skill(CuTest *tc) {
build_fixture bf = { 0 }; build_fixture bf = { 0 };
unit *u; unit *u;
@ -411,7 +388,6 @@ CuSuite *get_build_suite(void)
SUITE_ADD_TEST(suite, test_build_failure_low_skill); SUITE_ADD_TEST(suite, test_build_failure_low_skill);
SUITE_ADD_TEST(suite, test_build_failure_missing_skill); SUITE_ADD_TEST(suite, test_build_failure_missing_skill);
SUITE_ADD_TEST(suite, test_build_requires_materials); SUITE_ADD_TEST(suite, test_build_requires_materials);
SUITE_ADD_TEST(suite, test_build_requires_building);
SUITE_ADD_TEST(suite, test_build_failure_completed); SUITE_ADD_TEST(suite, test_build_failure_completed);
SUITE_ADD_TEST(suite, test_build_with_ring); SUITE_ADD_TEST(suite, test_build_with_ring);
SUITE_ADD_TEST(suite, test_build_with_potion); SUITE_ADD_TEST(suite, test_build_with_potion);

View File

@ -41,11 +41,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/gamedata.h> #include <util/gamedata.h>
#include <util/language.h> #include <util/language.h>
#include <util/log.h> #include <util/log.h>
#include <selist.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <storage.h> #include <storage.h>
#include <selist.h>
#include <critbit.h>
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
@ -62,21 +63,22 @@ typedef struct building_typelist {
} building_typelist; } building_typelist;
selist *buildingtypes = NULL; selist *buildingtypes = NULL;
static critbit_tree cb_bldgtypes;
/* Returns a building type for the (internal) name */ /* Returns a building type for the (internal) name */
static building_type *bt_find_i(const char *name) static building_type *bt_find_i(const char *name)
{ {
selist *ql; const char *match;
int qi; building_type *btype = NULL;
assert(name); match = cb_find_str(&cb_bldgtypes, name);
if (match) {
for (qi = 0, ql = buildingtypes; ql; selist_advance(&ql, &qi, 1)) { cb_get_kv(match, &btype, sizeof(btype));
building_type *btype = (building_type *)selist_get(ql, qi);
if (strcmp(btype->_name, name) == 0)
return btype;
} }
return NULL; else {
log_warning("st_find: could not find ship '%s'\n", name);
}
return btype;
} }
const building_type *bt_find(const char *name) const building_type *bt_find(const char *name)
@ -96,9 +98,15 @@ bool bt_changed(int *cache)
return false; return false;
} }
void bt_register(building_type * type) static void bt_register(building_type * btype)
{ {
selist_push(&buildingtypes, (void *)type); size_t len;
char data[64];
selist_push(&buildingtypes, (void *)btype);
len = cb_new_kv(btype->_name, strlen(btype->_name), &btype, sizeof(btype), data);
assert(len <= sizeof(data));
cb_insert(&cb_bldgtypes, data, len);
++bt_changes; ++bt_changes;
} }
@ -111,6 +119,7 @@ static void free_buildingtype(void *ptr) {
} }
void free_buildingtypes(void) { void free_buildingtypes(void) {
cb_clear(&cb_bldgtypes);
selist_foreach(buildingtypes, free_buildingtype); selist_foreach(buildingtypes, free_buildingtype);
selist_free(buildingtypes); selist_free(buildingtypes);
buildingtypes = 0; buildingtypes = 0;
@ -190,14 +199,14 @@ const char *buildingtype(const building_type * btype, const building * b, int bs
if (btype->name) { if (btype->name) {
return btype->name(btype, b, bsize); return btype->name(btype, b, bsize);
} }
if (btype->construction && btype->construction->extra.name) { if (btype->construction && btype->construction->name) {
if (b) { if (b) {
bsize = adjust_size(b, bsize); bsize = adjust_size(b, bsize);
} }
for (con = btype->construction; con; con = con->improvement) { for (con = btype->construction; con; con = con->improvement) {
bsize -= con->maxsize; bsize -= con->maxsize;
if (!con->improvement || bsize <0) { if (!con->improvement || bsize <0) {
return con->extra.name; return con->name;
} }
} }
} }

View File

@ -83,7 +83,6 @@ extern "C" {
bool bt_changed(int *cache); bool bt_changed(int *cache);
const building_type *bt_find(const char *name); const building_type *bt_find(const char *name);
void free_buildingtypes(void); void free_buildingtypes(void);
void bt_register(struct building_type *type);
int bt_effsize(const struct building_type *btype, int bt_effsize(const struct building_type *btype,
const struct building *b, int bsize); const struct building *b, int bsize);

View File

@ -21,14 +21,13 @@ static void test_register_building(CuTest * tc)
test_cleanup(); test_cleanup();
btype = (building_type *)calloc(sizeof(building_type), 1);
btype->_name = strdup("herp");
CuAssertIntEquals(tc, true, bt_changed(&cache)); CuAssertIntEquals(tc, true, bt_changed(&cache));
CuAssertIntEquals(tc, false, bt_changed(&cache)); CuAssertIntEquals(tc, false, bt_changed(&cache));
bt_register(btype);
CuAssertIntEquals(tc, true, bt_changed(&cache));
CuAssertPtrNotNull(tc, bt_find("herp")); btype = bt_get_or_create("herp");
CuAssertIntEquals(tc, true, bt_changed(&cache));
CuAssertPtrEquals(tc, btype, (void *)bt_find("herp"));
free_buildingtypes(); free_buildingtypes();
CuAssertIntEquals(tc, true, bt_changed(&cache)); CuAssertIntEquals(tc, true, bt_changed(&cache));
test_cleanup(); test_cleanup();
@ -564,7 +563,7 @@ static void test_buildingtype(CuTest *tc) {
btype = test_create_buildingtype("hodor"); btype = test_create_buildingtype("hodor");
CuAssertPtrNotNull(tc, btype->construction); CuAssertPtrNotNull(tc, btype->construction);
CuAssertStrEquals(tc, "hodor", buildingtype(btype, NULL, 1)); CuAssertStrEquals(tc, "hodor", buildingtype(btype, NULL, 1));
btype->construction->extra.name = strdup("castle"); btype->construction->name = strdup("castle");
CuAssertStrEquals(tc, "castle", buildingtype(btype, NULL, 1)); CuAssertStrEquals(tc, "castle", buildingtype(btype, NULL, 1));
btype = bt_get_or_create("portal"); btype = bt_get_or_create("portal");
CuAssertPtrEquals(tc, NULL, btype->construction); CuAssertPtrEquals(tc, NULL, btype->construction);

View File

@ -37,13 +37,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/event.h> #include <util/event.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
#include <util/log.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <selist.h>
#include <util/xml.h> #include <util/xml.h>
#include <attributes/movement.h> #include <attributes/movement.h>
#include <storage.h> #include <storage.h>
#include <selist.h>
#include <critbit.h>
/* libc includes */ /* libc includes */
#include <assert.h> #include <assert.h>
@ -51,7 +53,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
selist *shiptypes = NULL; selist *shiptypes = NULL; /* do not use this list for searching*/
static critbit_tree cb_shiptypes; /* use this trie instead */
static local_names *snames; static local_names *snames;
@ -60,9 +63,7 @@ const ship_type *findshiptype(const char *name, const struct locale *lang)
local_names *sn = snames; local_names *sn = snames;
variant var; variant var;
while (sn) { while (sn && sn->lang != lang) {
if (sn->lang == lang)
break;
sn = sn->next; sn = sn->next;
} }
if (!sn) { if (!sn) {
@ -89,29 +90,42 @@ const ship_type *findshiptype(const char *name, const struct locale *lang)
static ship_type *st_find_i(const char *name) static ship_type *st_find_i(const char *name)
{ {
selist *ql; const char *match;
int qi; ship_type *st = NULL;
for (qi = 0, ql = shiptypes; ql; selist_advance(&ql, &qi, 1)) { match = cb_find_str(&cb_shiptypes, name);
ship_type *stype = (ship_type *)selist_get(ql, qi); if (match) {
if (strcmp(stype->_name, name) == 0) { cb_get_kv(match, &st, sizeof(st));
return stype;
}
} }
return NULL; else {
log_warning("st_find: could not find ship '%s'\n", name);
}
return st;
} }
const ship_type *st_find(const char *name) { const ship_type *st_find(const char *name) {
return st_find_i(name); return st_find_i(name);
} }
static void st_register(ship_type *stype) {
size_t len;
char data[64];
selist_push(&shiptypes, (void *)stype);
len = cb_new_kv(stype->_name, strlen(stype->_name), &stype, sizeof(stype), data);
assert(len <= sizeof(data));
cb_insert(&cb_shiptypes, data, len);
}
ship_type *st_get_or_create(const char * name) { ship_type *st_get_or_create(const char * name) {
ship_type * st = st_find_i(name); ship_type * st = st_find_i(name);
assert(!snames);
if (!st) { if (!st) {
st = (ship_type *)calloc(sizeof(ship_type), 1); st = (ship_type *)calloc(sizeof(ship_type), 1);
st->_name = strdup(name); st->_name = strdup(name);
st->storm = 1.0; st->storm = 1.0;
selist_push(&shiptypes, (void *)st); st_register(st);
} }
return st; return st;
} }
@ -234,8 +248,9 @@ void remove_ship(ship ** slist, ship * sh)
void free_ship(ship * s) void free_ship(ship * s)
{ {
while (s->attribs) while (s->attribs) {
a_remove(&s->attribs, s->attribs); a_remove(&s->attribs, s->attribs);
}
free(s->name); free(s->name);
free(s->display); free(s->display);
free(s); free(s);
@ -250,6 +265,7 @@ static void free_shiptype(void *ptr) {
} }
void free_shiptypes(void) { void free_shiptypes(void) {
cb_clear(&cb_shiptypes);
selist_foreach(shiptypes, free_shiptype); selist_foreach(shiptypes, free_shiptype);
selist_free(shiptypes); selist_free(shiptypes);
shiptypes = 0; shiptypes = 0;

View File

@ -247,17 +247,10 @@ construction ** consPtr, construct_t type)
con->minskill = xml_ivalue(node, "minskill", -1); con->minskill = xml_ivalue(node, "minskill", -1);
con->reqsize = xml_ivalue(node, "reqsize", 1); con->reqsize = xml_ivalue(node, "reqsize", 1);
if (type == CONS_ITEM) { if (type == CONS_BUILDING) {
propValue = xmlGetProp(node, BAD_CAST "building");
if (propValue != NULL) {
con->extra.btype = bt_get_or_create((const char *)propValue);
xmlFree(propValue);
}
}
else if (type == CONS_BUILDING) {
propValue = xmlGetProp(node, BAD_CAST "name"); propValue = xmlGetProp(node, BAD_CAST "name");
if (propValue != NULL) { if (propValue != NULL) {
con->extra.name = strdup((const char *)propValue); con->name = strdup((const char *)propValue);
xmlFree(propValue); xmlFree(propValue);
} }
} }

View File

@ -479,7 +479,7 @@ static void test_illusioncastle(CuTest *tc)
CuAssertPtrEquals(tc, btype, (void *)icastle_type(a)); CuAssertPtrEquals(tc, btype, (void *)icastle_type(a));
CuAssertPtrEquals(tc, bt_icastle, (void *)b->type); CuAssertPtrEquals(tc, bt_icastle, (void *)b->type);
CuAssertStrEquals(tc, "castle", buildingtype(btype, b, b->size)); CuAssertStrEquals(tc, "castle", buildingtype(btype, b, b->size));
btype->construction->extra.name = strdup("site"); btype->construction->name = strdup("site");
CuAssertStrEquals(tc, "site", buildingtype(btype, b, b->size)); CuAssertStrEquals(tc, "site", buildingtype(btype, b, b->size));
test_cleanup(); test_cleanup();
} }

View File

@ -43,9 +43,7 @@ static void test_market_curse(CuTest * tc)
config_set("rules.region_owners", "1"); config_set("rules.region_owners", "1");
btype = (building_type *)calloc(1, sizeof(building_type)); btype = bt_get_or_create("market");
btype->_name = strdup("market");
bt_register(btype);
terrain = get_terrain("plain"); terrain = get_terrain("plain");