From f1be8caa46b4a59d6d55c41ce396f4e8483593ff Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 3 Feb 2018 14:16:01 +0100 Subject: [PATCH] more skill size savings. --- src/kernel/save.c | 21 +++++++++++++++++---- src/kernel/ship.test.c | 3 ++- src/kernel/skills.h | 8 ++++---- src/kernel/unit.c | 1 + src/study.c | 1 + 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/kernel/save.c b/src/kernel/save.c index 607d5c5a8..3843efb12 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -324,20 +324,29 @@ static void writeorder(gamedata *data, const struct order *ord, static void read_skill(gamedata *data, skill *sv) { int val; READ_INT(data->store, &val); + assert(val < MAXSKILLS); sv->id = (skill_t)val; if (sv->id != NOSKILL) { READ_INT(data->store, &val); + assert(val < CHAR_MAX); sv->old = sv->level = val; READ_INT(data->store, &val); + assert(val < CHAR_MAX); sv->weeks = val; } } +static int skill_cmp(const void *a, const void *b) { + const skill * sa = (const skill *)a; + const skill * sb = (const skill *)b; + return sa->id - sb->id; +} + static void read_skills(gamedata *data, unit *u) { if (data->version < SKILLSORT_VERSION) { skill skills[MAXSKILLS], *sv = skills; - size_t sz; + u->skill_size = 0; for (;;) { read_skill(data, sv); @@ -347,9 +356,13 @@ static void read_skills(gamedata *data, unit *u) ++u->skill_size; } } - sz = u->skill_size * sizeof(skill); - u->skills = malloc(sz); - memcpy(u->skills, skills, sz); + if (u->skill_size > 0) { + size_t sz = u->skill_size * sizeof(skill); + + qsort(skills, u->skill_size, sizeof(skill), skill_cmp); + u->skills = malloc(sz); + memcpy(u->skills, skills, sz); + } } else { int i; diff --git a/src/kernel/ship.test.c b/src/kernel/ship.test.c index 69766276c..aa4dc5755 100644 --- a/src/kernel/ship.test.c +++ b/src/kernel/ship.test.c @@ -619,7 +619,8 @@ static void test_shipspeed_max_range(CuTest *tc) { CuAssertIntEquals_Msg(tc, "skill bonus from movement.shipspeed.skillbonus", 3, shipspeed(sh, cap)); set_level(cap, SK_SAILING, stype->cptskill + 15); - set_level(crew, SK_SAILING, (stype->sumskill - stype->cptskill) * 15); + scale_number(crew, 15); + set_level(crew, SK_SAILING, stype->sumskill - stype->cptskill); CuAssertIntEquals_Msg(tc, "skill-bonus cannot exceed max_range", 4, shipspeed(sh, cap)); test_teardown(); } diff --git a/src/kernel/skills.h b/src/kernel/skills.h index 7b7cc3f04..649fa92cf 100644 --- a/src/kernel/skills.h +++ b/src/kernel/skills.h @@ -26,10 +26,10 @@ extern "C" { #endif typedef struct skill { - skill_t id : 16; - int level : 16; - int weeks : 16; - int old : 16; + skill_t id : 8; + int level : 8; + int weeks : 8; + int old : 8; } skill; typedef int(*skillmod_fun) (const struct unit *, const struct region *, diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 40e67ebae..367ea4d97 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -795,6 +795,7 @@ void set_level(unit * u, skill_t sk, int value) skill *sv = u->skills; assert(sk != SK_MAGIC || !u->faction || u->number == 1 || fval(u->faction, FFL_NPC)); + assert(value <= CHAR_MAX && value >= CHAR_MIN); if (!skill_enabled(sk)) return; diff --git a/src/study.c b/src/study.c index f446e34ab..09f228f45 100644 --- a/src/study.c +++ b/src/study.c @@ -828,6 +828,7 @@ void learn_skill(unit *u, skill_t sk, int days) { int leveldays = STUDYDAYS * u->number; int weeks = 0; + assert(sk >= 0 && sk < MAXSKILLS); if (inject_learn_fun) { inject_learn_fun(u, sk, days); return;