make it impossible to create buildings with size 0

This commit is contained in:
Enno Rehling 2020-07-17 18:27:46 +02:00
parent 2d39641e3f
commit a5b39962fd
9 changed files with 24 additions and 22 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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));
}