factor out some function to make future debugging easier.

This commit is contained in:
Enno Rehling 2017-02-14 21:01:22 +01:00
parent 4ce415be50
commit 7947101899

View file

@ -428,6 +428,76 @@ int roqf_factor(void)
return value; return value;
} }
static int use_materials(unit *u, const construction *type, int n, int completed) {
if (type->materials) {
int c;
for (c = 0; type->materials[c].number; c++) {
const struct resource_type *rtype = type->materials[c].rtype;
int prebuilt =
required(completed, type->reqsize, type->materials[c].number);
int need =
required(completed + n, type->reqsize, type->materials[c].number);
int multi = 1;
int canuse = 100; /* normalization */
if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse);
}
if (canuse < 0) {
return canuse; /* pass errors to caller */
}
canuse = matmod(type->attribs, u, rtype, canuse);
assert(canuse % 100 == 0
|| !"only constant multipliers are implemented in build()");
multi = canuse / 100;
if (canuse < 0) {
return canuse; /* pass errors to caller */
}
use_pooled(u, rtype, GET_DEFAULT,
(need - prebuilt + multi - 1) / multi);
}
}
return 0;
}
static int count_materials(unit *u, const construction *type, int n, int completed)
{
if (type->materials) {
int c;
for (c = 0; n > 0 && type->materials[c].number; c++) {
const struct resource_type *rtype = type->materials[c].rtype;
int need, prebuilt;
int canuse = get_pooled(u, rtype, GET_DEFAULT, INT_MAX);
if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse);
}
if (canuse < 0)
return canuse; /* pass errors to caller */
canuse = matmod(type->attribs, u, rtype, canuse);
if (type->reqsize > 1) {
prebuilt =
required(completed, type->reqsize, type->materials[c].number);
for (; n;) {
need =
required(completed + n, type->reqsize, type->materials[c].number);
if (need - prebuilt <= canuse)
break;
--n; /* TODO: optimieren? */
}
}
else {
int maxn = canuse / type->materials[c].number;
if (maxn < n) {
n = maxn;
}
}
}
}
return n;
}
/** Use up resources for building an object. /** Use up resources for building an object.
* Build up to 'size' points of 'type', where 'completed' * Build up to 'size' points of 'type', where 'completed'
* of the first object have already been finished. return the * of the first object have already been finished. return the
@ -492,7 +562,7 @@ int build(unit * u, const construction * ctype, int completed, int want)
} }
} }
for (; want > 0 && skills > 0;) { for (; want > 0 && skills > 0;) {
int c, n; int err, n;
/* skip over everything that's already been done: /* skip over everything that's already been done:
* type->improvement==NULL means no more improvements, but no size limits * type->improvement==NULL means no more improvements, but no size limits
@ -559,67 +629,16 @@ int build(unit * u, const construction * ctype, int completed, int want)
} }
} }
if (type->materials) n = count_materials(u, type, n, completed);
for (c = 0; n > 0 && type->materials[c].number; c++) {
const struct resource_type *rtype = type->materials[c].rtype;
int need, prebuilt;
int canuse = get_pooled(u, rtype, GET_DEFAULT, INT_MAX);
if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse);
}
if (canuse < 0)
return canuse; /* pass errors to caller */
canuse = matmod(type->attribs, u, rtype, canuse);
if (type->reqsize > 1) {
prebuilt =
required(completed, type->reqsize, type->materials[c].number);
for (; n;) {
need =
required(completed + n, type->reqsize, type->materials[c].number);
if (need - prebuilt <= canuse)
break;
--n; /* TODO: optimieren? */
}
}
else {
int maxn = canuse / type->materials[c].number;
if (maxn < n)
n = maxn;
}
}
if (n <= 0) { if (n <= 0) {
if (made == 0) if (made == 0)
return ENOMATERIALS; return ENOMATERIALS;
else else
break; break;
} }
if (type->materials) { err = use_materials(u, type, n, completed);
for (c = 0; type->materials[c].number; c++) { if (err < 0) {
const struct resource_type *rtype = type->materials[c].rtype; return err;
int prebuilt =
required(completed, type->reqsize, type->materials[c].number);
int need =
required(completed + n, type->reqsize, type->materials[c].number);
int multi = 1;
int canuse = 100; /* normalization */
if (building_is_active(u->building) && inside_building(u)) {
canuse = matmod(u->building->type->attribs, u, rtype, canuse);
}
if (canuse < 0)
return canuse; /* pass errors to caller */
canuse = matmod(type->attribs, u, rtype, canuse);
assert(canuse % 100 == 0
|| !"only constant multipliers are implemented in build()");
multi = canuse / 100;
if (canuse < 0)
return canuse; /* pass errors to caller */
use_pooled(u, rtype, GET_DEFAULT,
(need - prebuilt + multi - 1) / multi);
}
} }
made += n; made += n;
skills -= n * type->minskill; skills -= n * type->minskill;