Merge remote-tracking branch 'eressea/master'

This commit is contained in:
TomBraun 2014-07-05 08:58:22 +02:00
commit 37be445fdf
9 changed files with 144 additions and 15 deletions

View File

@ -1074,6 +1074,7 @@ function test_lighthouse()
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
local b = building.create(r, "lighthouse") local b = building.create(r, "lighthouse")
b.size = 100 b.size = 100
b.working = true
u.building = b u.building = b
u:set_skill("perception", 9) u:set_skill("perception", 9)
u:add_item("money", 1000) u:add_item("money", 1000)

View File

@ -56,6 +56,23 @@ static int tolua_building_get_objects(lua_State * L)
return 1; return 1;
} }
static int tolua_building_set_working(lua_State * L)
{
building *self = (building *) tolua_tousertype(L, 1, 0);
bool flag = !!lua_toboolean(L, 2);
if (flag) self->flags |= BLD_WORKING;
else self->flags &= ~BLD_WORKING;
return 1;
}
static int tolua_building_get_working(lua_State * L)
{
building *self = (building *) tolua_tousertype(L, 1, 0);
bool flag = (self->flags&BLD_WORKING)!=0;
lua_pushboolean(L, flag);
return 1;
}
static int tolua_building_get_region(lua_State * L) static int tolua_building_get_region(lua_State * L)
{ {
building *self = (building *) tolua_tousertype(L, 1, 0); building *self = (building *) tolua_tousertype(L, 1, 0);
@ -243,6 +260,7 @@ void tolua_building_open(lua_State * L)
.def_readwrite("size", &building::size) .def_readwrite("size", &building::size)
#endif #endif
tolua_variable(L, TOLUA_CAST "objects", tolua_building_get_objects, 0); tolua_variable(L, TOLUA_CAST "objects", tolua_building_get_objects, 0);
tolua_variable(L, TOLUA_CAST "working", tolua_building_get_working, tolua_building_set_working);
} }
tolua_endmodule(L); tolua_endmodule(L);

View File

@ -769,7 +769,7 @@ build_building(unit * u, const building_type * btype, int id, int want, order *
/* build a new building */ /* build a new building */
b = new_building(btype, r, lang); b = new_building(btype, r, lang);
b->type = btype; b->type = btype;
fset(b, BLD_MAINTAINED); fset(b, BLD_MAINTAINED|BLD_WORKING);
/* Die Einheit befindet sich automatisch im Inneren der neuen Burg. */ /* Die Einheit befindet sich automatisch im Inneren der neuen Burg. */
if (u->number && leave(u, false)) { if (u->number && leave(u, false)) {

View File

@ -486,7 +486,6 @@ building *new_building(const struct building_type * btype, region * r,
init_lighthouse = true; init_lighthouse = true;
} }
b->flags = BLD_WORKING | BLD_MAINTAINED;
b->no = newcontainerid(); b->no = newcontainerid();
bhash(b); bhash(b);

View File

@ -80,6 +80,63 @@ static void json_requirements(cJSON *json, requirement **matp) {
*matp = mat; *matp = mat;
} }
static void json_maintenance_i(cJSON *json, maintenance *mt) {
cJSON *child;
for (child = json->child; child; child = child->next) {
switch (child->type) {
case cJSON_Number:
if (strcmp(child->string, "amount") == 0) {
mt->number = child->valueint;
}
else {
log_error_n("maintenance contains unknown attribute %s", child->string);
}
break;
case cJSON_String:
if (strcmp(child->string, "type") == 0) {
mt->rtype = rt_get_or_create(child->valuestring);
}
else {
log_error_n("maintenance contains unknown attribute %s", child->string);
}
break;
case cJSON_Array:
if (strcmp(child->string, "flags") == 0) {
const char * flags[] = { "variable", "required", 0 };
mt->flags = json_flags(child, flags);
}
else {
log_error_n("maintenance contains unknown attribute %s", child->string);
}
default:
log_error_n("maintenance contains unknown attribute %s", child->string);
}
}
}
static void json_maintenance(cJSON *json, maintenance **mtp) {
cJSON *child;
maintenance *mt;
int i, size = 1;
if (json->type == cJSON_Array) {
size = cJSON_GetArraySize(json);
}
else if (json->type != cJSON_Object) {
log_error_n("maintenance is not a json object or array (%d)", json->type);
return;
}
*mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1);
if (json->type == cJSON_Array) {
for (i = 0, child = json->child; child; child = child->next, ++i) {
if (child->type == cJSON_Object) {
json_maintenance_i(child, mt+i);
}
}
}
json_maintenance_i(json, mt);
}
static void json_construction(cJSON *json, construction **consp) { static void json_construction(cJSON *json, construction **consp) {
cJSON *child; cJSON *child;
if (json->type==cJSON_Array) { if (json->type==cJSON_Array) {
@ -162,11 +219,17 @@ static void json_building(cJSON *json, building_type *bt) {
if (strcmp(child->string, "construction") == 0) { if (strcmp(child->string, "construction") == 0) {
json_construction(child, &bt->construction); json_construction(child, &bt->construction);
} }
else if (strcmp(child->string, "maintenance") == 0) {
json_maintenance(child, &bt->maintenance);
}
break; break;
case cJSON_Object: case cJSON_Object:
if (strcmp(child->string, "construction")==0) { if (strcmp(child->string, "construction")==0) {
json_construction(child, &bt->construction); json_construction(child, &bt->construction);
} }
else if (strcmp(child->string, "maintenance") == 0) {
json_maintenance(child, &bt->maintenance);
}
break; break;
case cJSON_String: case cJSON_String:
if (strcmp(child->string, "name")==0) { if (strcmp(child->string, "name")==0) {

View File

@ -200,7 +200,11 @@ static void test_spells(CuTest * tc)
static void test_buildings(CuTest * tc) static void test_buildings(CuTest * tc)
{ {
const char * data = "{\"buildings\": { \"house\" : { " const char * data = "{\"buildings\": { "
"\"house\" : { "
"\"maintenance\" : "
"{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }"
","
"\"construction\" : {" "\"construction\" : {"
"\"maxsize\" : 20," "\"maxsize\" : 20,"
"\"reqsize\" : 10," "\"reqsize\" : 10,"
@ -208,7 +212,13 @@ static void test_buildings(CuTest * tc)
"\"materials\" : {" "\"materials\" : {"
"\"stone\" : 2," "\"stone\" : 2,"
"\"iron\" : 1" "\"iron\" : 1"
"}}}}}"; "}}},"
"\"shed\" : {"
"\"maintenance\" : ["
"{ \"type\" : \"iron\", \"amount\" : 1 },"
"{ \"type\" : \"stone\", \"amount\" : 2 }"
"]}"
"}}";
cJSON *json = cJSON_Parse(data); cJSON *json = cJSON_Parse(data);
const building_type *bt; const building_type *bt;
@ -220,8 +230,24 @@ static void test_buildings(CuTest * tc)
json_config(json); json_config(json);
CuAssertPtrNotNull(tc, buildingtypes); CuAssertPtrNotNull(tc, buildingtypes);
bt = bt_find("shed");
CuAssertPtrNotNull(tc, bt);
CuAssertPtrNotNull(tc, bt->maintenance);
CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype);
CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->maintenance[1].rtype);
CuAssertIntEquals(tc, 1, bt->maintenance[0].number);
CuAssertIntEquals(tc, 2, bt->maintenance[1].number);
CuAssertIntEquals(tc, 0, bt->maintenance[2].number);
bt = bt_find("house"); bt = bt_find("house");
CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt);
CuAssertPtrNotNull(tc, bt->maintenance);
CuAssertIntEquals(tc, 1, bt->maintenance[0].number);
CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype);
CuAssertIntEquals(tc, MTF_VARIABLE|MTF_VITAL, bt->maintenance[0].flags);
CuAssertIntEquals(tc, 0, bt->maintenance[1].number);
CuAssertPtrNotNull(tc, bt->construction); CuAssertPtrNotNull(tc, bt->construction);
CuAssertPtrNotNull(tc, bt->construction->materials); CuAssertPtrNotNull(tc, bt->construction->materials);
CuAssertIntEquals(tc, 2, bt->construction->materials[0].number); CuAssertIntEquals(tc, 2, bt->construction->materials[0].number);

View File

@ -43,6 +43,7 @@ static void test_ship_allowed_with_harbor(CuTest * tc)
ship * sh; ship * sh;
terrain_type * ttype; terrain_type * ttype;
building_type * btype; building_type * btype;
building * b;
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
@ -53,7 +54,8 @@ static void test_ship_allowed_with_harbor(CuTest * tc)
r = test_create_region(0, 0, ttype); r = test_create_region(0, 0, ttype);
sh = test_create_ship(0, 0); sh = test_create_ship(0, 0);
test_create_building(r, btype); b = test_create_building(r, btype);
b->flags |= BLD_WORKING;
CuAssertIntEquals(tc, SA_HARBOUR, check_ship_allowed(sh, r)); CuAssertIntEquals(tc, SA_HARBOUR, check_ship_allowed(sh, r));
} }
@ -73,9 +75,9 @@ static void test_building_type_exists(CuTest * tc)
b = new_building(btype, r, default_locale); b = new_building(btype, r, default_locale);
CuAssertPtrNotNull(tc, b); CuAssertPtrNotNull(tc, b);
CuAssertTrue(tc, !buildingtype_exists(r, NULL, true)); CuAssertTrue(tc, !buildingtype_exists(r, NULL, false));
CuAssertTrue(tc, buildingtype_exists(r, btype, true)); CuAssertTrue(tc, buildingtype_exists(r, btype, false));
CuAssertTrue(tc, !buildingtype_exists(r, btype2, true)); CuAssertTrue(tc, !buildingtype_exists(r, btype2, false));
} }
CuSuite *get_move_suite(void) CuSuite *get_move_suite(void)

View File

@ -3,8 +3,8 @@ require "tests.settings"
require "tests.config" require "tests.config"
require "tests.locale" require "tests.locale"
require "tests.regions" require "tests.regions"
require "tests.ships"
require "tests.study" require "tests.study"
require "tests.movement"
require "tests.castles" require "tests.castles"
require "tests.spells" require "tests.spells"
require "tests.movement"
require "tests.ships"

View File

@ -22,7 +22,7 @@ function setup()
} }
}, },
"buildings" : { "buildings" : {
"harbour" : {} "harbour" : { "maintenance" : { "type" : "money", "amount" : 250, "flags" : [ "required" ] } }
}, },
"terrains" : { "terrains" : {
"ocean": { "flags" : [ "sea", "sail" ] }, "ocean": { "flags" : [ "sea", "sail" ] },
@ -90,7 +90,27 @@ function test_sail_to_forbidden_shore()
assert_equal(ocean, u.region) assert_equal(ocean, u.region)
end end
function test_sail_into_harbour() function test_sail_into_good_harbour()
local ocean = region.create(1, 0, "ocean")
local shore = region.create(0, 0, "glacier")
local f = faction.create("noreply@eressea.de", "human", "de")
local b = building.create(shore, "harbour")
local u
u = unit.create(f, shore, 1)
u:add_item("money", 250) -- building maintenance fee
u.building = b
u = unit.create(f, ocean, 1)
u.ship = ship.create(ocean, "boat")
u:set_skill("sailing", 10)
u:add_order("NACH W")
process_orders()
assert_equal(shore, u.region, "working harbour should let the ship land")
end
function test_sail_into_bad_harbour()
local ocean = region.create(1, 0, "ocean") local ocean = region.create(1, 0, "ocean")
local shore = region.create(0, 0, "glacier") local shore = region.create(0, 0, "glacier")
local f = faction.create("noreply@eressea.de", "human", "de") local f = faction.create("noreply@eressea.de", "human", "de")
@ -103,5 +123,5 @@ function test_sail_into_harbour()
assert_not_nil(b) assert_not_nil(b)
process_orders() process_orders()
assert_equal(shore, u.region) assert_equal(ocean, u.region, "harbour without owner should stop ship")
end end