optimize sizeof(skill)

This commit is contained in:
Enno Rehling 2018-02-03 12:57:43 +01:00
parent 096861fc62
commit 5cdd79804a
2 changed files with 28 additions and 21 deletions

View file

@ -321,23 +321,35 @@ static void writeorder(gamedata *data, const struct order *ord,
WRITE_STR(data->store, obuf); WRITE_STR(data->store, obuf);
} }
static void read_skill(gamedata *data, skill *sv) {
int val;
READ_INT(data->store, &val);
sv->id = (skill_t)val;
if (sv->id != NOSKILL) {
READ_INT(data->store, &val);
sv->old = sv->level = val;
READ_INT(data->store, &val);
sv->weeks = val;
}
}
static void read_skills(gamedata *data, unit *u) static void read_skills(gamedata *data, unit *u)
{ {
if (data->version < SKILLSORT_VERSION) { if (data->version < SKILLSORT_VERSION) {
skill skills[MAXSKILLS], *sv = skills;
size_t sz;
u->skill_size = 0;
for (;;) { for (;;) {
int n = NOSKILL, level, weeks; read_skill(data, sv);
skill_t sk; if (sv->id == NOSKILL) break;
READ_INT(data->store, &n); if (sv->level > 0) {
sk = (skill_t)n; ++sv;
if (sk == NOSKILL) break; ++u->skill_size;
READ_INT(data->store, &level);
READ_INT(data->store, &weeks);
if (level) {
skill *sv = add_skill(u, sk);
sv->level = sv->old = (unsigned char)level;
sv->weeks = (unsigned char)weeks;
} }
} }
sz = u->skill_size * sizeof(skill);
u->skills = malloc(sz);
memcpy(u->skills, skills, sz);
} }
else { else {
int i; int i;
@ -345,12 +357,7 @@ static void read_skills(gamedata *data, unit *u)
u->skills = malloc(sizeof(skill)*u->skill_size); u->skills = malloc(sizeof(skill)*u->skill_size);
for (i = 0; i != u->skill_size; ++i) { for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i; skill *sv = u->skills + i;
int val; read_skill(data, sv);
READ_INT(data->store, &val);
sv->id = (skill_t)val;
READ_INT(data->store, &sv->level);
sv->old = sv->level;
READ_INT(data->store, &sv->weeks);
} }
} }
} }

View file

@ -26,10 +26,10 @@ extern "C" {
#endif #endif
typedef struct skill { typedef struct skill {
skill_t id; skill_t id : 16;
int level; int level : 16;
int weeks; int weeks : 16;
int old; int old : 16;
} skill; } skill;
typedef int(*skillmod_fun) (const struct unit *, const struct region *, typedef int(*skillmod_fun) (const struct unit *, const struct region *,