Merge branch 'master' into develop

This commit is contained in:
Enno Rehling 2017-08-16 21:50:40 +02:00
commit 3ea06b23e8
2 changed files with 17 additions and 31 deletions

View File

@ -30,6 +30,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
static void a_writekeys(const attrib *a, const void *o, storage *store) { static void a_writekeys(const attrib *a, const void *o, storage *store) {
int i, *keys = (int *)a->data.v; int i, *keys = (int *)a->data.v;
assert(keys[0] < 4096 && keys[0]>0);
WRITE_INT(store, keys[0]); WRITE_INT(store, keys[0]);
for (i = 0; i < keys[0]; ++i) { for (i = 0; i < keys[0]; ++i) {
WRITE_INT(store, keys[i * 2 + 1]); WRITE_INT(store, keys[i * 2 + 1]);
@ -60,6 +61,9 @@ static int a_readkeys(attrib * a, void *owner, gamedata *data) {
static int a_readkey(attrib *a, void *owner, struct gamedata *data) { static int a_readkey(attrib *a, void *owner, struct gamedata *data) {
int res = a_readint(a, owner, data); int res = a_readint(a, owner, data);
if (data->version>=KEYVAL_VERSION) {
return AT_READ_FAIL;
}
return (res != AT_READ_FAIL) ? AT_READ_DEPR : res; return (res != AT_READ_FAIL) ? AT_READ_DEPR : res;
} }
@ -78,35 +82,12 @@ attrib_type at_keys = {
}; };
static void a_upgradekeys(attrib **alist, attrib *abegin) { static void a_upgradekeys(attrib **alist, attrib *abegin) {
int n = 0, *keys = 0; attrib *a, *ak;
int i = 0, val[8];
attrib *a, *ak = a_find(*alist, &at_keys); ak = a_find(*alist, &at_keys);
if (ak) { if (ak) alist = &ak;
keys = (int *)ak->data.v;
if (keys) n = keys[0];
}
for (a = abegin; a && a->type == abegin->type; a = a->next) { for (a = abegin; a && a->type == abegin->type; a = a->next) {
val[i * 2] = a->data.i; key_set(alist, a->data.i, 1);
val[i * 2 + 1] = 1;
if (++i == 4) {
keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1));
memcpy(keys + 2 * n + 1, val, sizeof(val));
n += i;
i = 0;
}
}
if (i > 0) {
keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1));
memcpy(keys + 2 * n + 1, val, sizeof(int)*i*2);
if (!ak) {
ak = a_add(alist, a_new(&at_keys));
}
}
if (ak) {
ak->data.v = keys;
if (keys) {
keys[0] = n + i;
}
} }
} }
@ -115,7 +96,7 @@ attrib_type at_key = {
NULL, NULL,
NULL, NULL,
NULL, NULL,
a_writeint, NULL,
a_readkey, a_readkey,
a_upgradekeys a_upgradekeys
}; };
@ -133,9 +114,10 @@ void key_set(attrib ** alist, int key, int val)
if (keys) { if (keys) {
n = keys[0]; n = keys[0];
} }
/* TODO: too many allocations, unsorted array */
keys = realloc(keys, sizeof(int) *(2 * n + 3)); keys = realloc(keys, sizeof(int) *(2 * n + 3));
/* TODO: does insertion sort pay off here? prob. not. */
keys[0] = n + 1; keys[0] = n + 1;
assert(keys[0] < 4096 && keys[0]>=0);
keys[2 * n + 1] = key; keys[2 * n + 1] = key;
keys[2 * n + 2] = val; keys[2 * n + 2] = val;
a->data.v = keys; a->data.v = keys;
@ -150,6 +132,7 @@ void key_unset(attrib ** alist, int key)
int i, *keys = (int *)a->data.v; int i, *keys = (int *)a->data.v;
if (keys) { if (keys) {
int n = keys[0]; int n = keys[0];
assert(keys[0] < 4096 && keys[0]>0);
for (i = 0; i != n; ++i) { for (i = 0; i != n; ++i) {
if (keys[2 * i + 1] == key) { if (keys[2 * i + 1] == key) {
memmove(keys + 2 * i + 1, keys + 2 * n - 1, 2 * sizeof(int)); memmove(keys + 2 * i + 1, keys + 2 * n - 1, 2 * sizeof(int));
@ -168,6 +151,7 @@ int key_get(attrib *alist, int key) {
if (a) { if (a) {
int i, *keys = (int *)a->data.v; int i, *keys = (int *)a->data.v;
if (keys) { if (keys) {
/* TODO: binary search this! */
for (i = 0; i != keys[0]; ++i) { for (i = 0; i != keys[0]; ++i) {
if (keys[i*2+1] == key) { if (keys[i*2+1] == key) {
return keys[i * 2 + 2]; return keys[i * 2 + 2];

View File

@ -499,11 +499,12 @@ static void test_modify_production(CuTest *tc) {
CuAssertIntEquals(tc, 11, get_item(u, itype)); CuAssertIntEquals(tc, 11, get_item(u, itype));
CuAssertIntEquals(tc, 290, region_getresource(u->region, rtype)); /* used 10 stones to make 10 stones */ CuAssertIntEquals(tc, 290, region_getresource(u->region, rtype)); /* used 10 stones to make 10 stones */
rtype->modifiers = calloc(2, sizeof(resource_mod)); rtype->modifiers = calloc(3, sizeof(resource_mod));
rtype->modifiers[0].type = RMT_PROD_SAVE; rtype->modifiers[0].type = RMT_PROD_SAVE;
rtype->modifiers[0].race = u->_race; rtype->modifiers[0].race = u->_race;
rtype->modifiers[0].value.sa[0] = (short)(0.5+100*d); rtype->modifiers[0].value.sa[0] = (short)(0.5+100*d);
rtype->modifiers[0].value.sa[1] = 100; rtype->modifiers[0].value.sa[1] = 100;
rtype->modifiers[1].type = RMT_END;
make_item(u, itype, 10); make_item(u, itype, 10);
split_allocations(u->region); split_allocations(u->region);
CuAssertIntEquals(tc, 21, get_item(u, itype)); CuAssertIntEquals(tc, 21, get_item(u, itype));
@ -541,6 +542,7 @@ static void test_modify_production(CuTest *tc) {
rtype->modifiers[1].type = RMT_PROD_REQUIRE; rtype->modifiers[1].type = RMT_PROD_REQUIRE;
rtype->modifiers[1].race = u_race(u); rtype->modifiers[1].race = u_race(u);
rtype->modifiers[1].btype = NULL; rtype->modifiers[1].btype = NULL;
rtype->modifiers[2].type = RMT_END;
test_clear_messages(u->faction); test_clear_messages(u->faction);
make_item(u, itype, 10); make_item(u, itype, 10);