diff --git a/src/bind_building.c b/src/bind_building.c index 93576ed3b..d51122b0a 100644 --- a/src/bind_building.c +++ b/src/bind_building.c @@ -183,16 +183,20 @@ static int tolua_building_set_owner(lua_State * L) static int tolua_building_create(lua_State * L) { - region *r = (region *)tolua_tousertype(L, 1, 0); - const char *bname = tolua_tostring(L, 2, 0); + region *r = (region *)tolua_tousertype(L, 1, NULL); + const char *bname = tolua_tostring(L, 2, NULL); + int size = (int)tolua_tonumber(L, 3, 1); if (!r) { log_error("building.create expects a region as argument 1"); } else if (!bname) { log_error("building.create expects a name as argument 2"); + } + else if (size <= 0) { + log_error("building.create expects a size > 0"); } else { const building_type *btype = bt_find(bname); if (btype) { - building *b = new_building(btype, r, default_locale); + building *b = new_building(btype, r, default_locale, size); tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); return 1; } diff --git a/src/kernel/build.c b/src/kernel/build.c index 3eb9870de..76cbe9f34 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -827,8 +827,7 @@ build_building(unit * u, const building_type * btype, int id, int want, order * b->size += built; } else { /* build a new building */ - b = new_building(btype, r, lang); - b->size = built; + b = new_building(btype, r, lang, built); b->type = btype; fset(b, BLD_MAINTAINED); diff --git a/src/kernel/building.c b/src/kernel/building.c index a6167f880..c8c19cc9d 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -369,7 +369,7 @@ building *building_create(int id) } building *new_building(const struct building_type * btype, region * r, - const struct locale * lang) + const struct locale * lang, int size) { building **bptr = &r->buildings; int id = newcontainerid(); @@ -377,6 +377,7 @@ building *new_building(const struct building_type * btype, region * r, const char *bname; char buffer[32]; + assert(size > 0); b->type = btype; b->region = r; while (*bptr) @@ -396,6 +397,7 @@ building *new_building(const struct building_type * btype, region * r, assert(bname); snprintf(buffer, sizeof(buffer), "%s %s", bname, itoa36(b->no)); b->name = str_strdup(bname); + b->size = size; return b; } diff --git a/src/kernel/building.h b/src/kernel/building.h index 2471bbfdc..74128a036 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -111,7 +111,7 @@ extern "C" { int buildingcapacity(const struct building *b); struct building *building_create(int id); struct building *new_building(const struct building_type *typ, - struct region *r, const struct locale *lang); + struct region *r, const struct locale *lang, int size); int build_building(struct unit *u, const struct building_type *typ, int id, int size, struct order *ord); bool building_finished(const struct building *b); diff --git a/src/laws.test.c b/src/laws.test.c index 822f179e7..e830eb95f 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -61,7 +61,7 @@ static void test_rename_building(CuTest * tc) test_create_locale(); btype = test_create_buildingtype("castle"); r = test_create_region(0, 0, NULL); - b = new_building(btype, r, default_locale); + b = new_building(btype, r, default_locale, 1); f = test_create_faction(NULL); u = test_create_unit(f, r); u_set_building(u, b); @@ -84,7 +84,7 @@ static void test_rename_building_twice(CuTest * tc) test_create_locale(); btype = test_create_buildingtype("castle"); r = test_create_region(0, 0, NULL); - b = new_building(btype, r, default_locale); + b = new_building(btype, r, default_locale, 1); f = test_create_faction(NULL); u = test_create_unit(f, r); u_set_building(u, b); diff --git a/src/races/races.c b/src/races/races.c index 7c3e65fc4..a782ba0ce 100644 --- a/src/races/races.c +++ b/src/races/races.c @@ -42,8 +42,7 @@ void equip_newunits(struct unit *u) if (u->building == NULL) { const building_type *btype = bt_find("castle"); if (btype != NULL) { - building *b = new_building(btype, r, u->faction->locale); - b->size = 10; + building *b = new_building(btype, r, u->faction->locale, 10); u_set_building(u, b); building_set_owner(u); } diff --git a/src/spells.c b/src/spells.c index 518819cdd..fbd4d71d5 100644 --- a/src/spells.c +++ b/src/spells.c @@ -4415,7 +4415,7 @@ int sp_icastle(castorder * co) const building_type *type; region *r = co_get_region(co); unit *mage = co_get_caster(co); - int cast_level = co->level; + int size, cast_level = co->level; double power = co->force; spellparameter *pa = co->par; const char *bname; @@ -4431,18 +4431,17 @@ int sp_icastle(castorder * co) type = bt_find("castle"); } - b = new_building(bt_illusion, r, mage->faction->locale); - /* Groesse festlegen. */ if (type == bt_illusion) { - b->size = (rng_int() % (int)((power * power) + 1) * 10); + size = (rng_int() % (int)((power * power) + 1) * 10); } else if (type->maxsize > 0) { - b->size = type->maxsize; + size = type->maxsize; } else { - b->size = ((rng_int() % (int)(power)) + 1) * 5; + size = ((rng_int() % (int)(power)) + 1) * 5; } + b = new_building(bt_illusion, r, mage->faction->locale, size); bname = LOC(mage->faction->locale, buildingtype(type, b, 0)); building_setname(b, bname); diff --git a/src/tests.c b/src/tests.c index 727803e2d..699c093d7 100644 --- a/src/tests.c +++ b/src/tests.c @@ -335,8 +335,7 @@ building * test_create_building(region * r, const building_type * btype) bt_castle->flags |= BTF_FORTIFICATION; btype = bt_castle; } - b = new_building(btype, r, default_locale); - b->size = btype->maxsize > 0 ? btype->maxsize : 1; + b = new_building(btype, r, default_locale, (btype->maxsize > 0) ? btype->maxsize : 1); return b; } diff --git a/src/wormhole.c b/src/wormhole.c index 22f2e3d8b..f4815f158 100644 --- a/src/wormhole.c +++ b/src/wormhole.c @@ -116,13 +116,13 @@ static attrib_type at_wormhole = { static void make_wormhole(const building_type * bt_wormhole, region * r1, region * r2) { - building *b1 = new_building(bt_wormhole, r1, default_locale); - building *b2 = new_building(bt_wormhole, r2, default_locale); + int size = bt_wormhole->maxcapacity * bt_wormhole->capacity; + building *b1 = new_building(bt_wormhole, r1, default_locale, size); + building *b2 = new_building(bt_wormhole, r2, default_locale, size); attrib *a1 = a_add(&b1->attribs, a_new(&at_wormhole)); attrib *a2 = a_add(&b2->attribs, a_new(&at_wormhole)); a1->data.v = b2->region; a2->data.v = b1->region; - b1->size = b2->size = bt_wormhole->maxcapacity * bt_wormhole->capacity; ADDMSG(&r1->msgs, msg_message("wormhole_appear", "region", r1)); ADDMSG(&r2->msgs, msg_message("wormhole_appear", "region", r2)); }