improve the skill read/write code.

This commit is contained in:
Enno Rehling 2017-09-15 22:03:54 +02:00
parent 8b4e3b7738
commit 860afe855f
2 changed files with 35 additions and 21 deletions

View File

@ -614,35 +614,48 @@ static void writeorder(gamedata *data, const struct order *ord,
static void read_skills(gamedata *data, unit *u) static void read_skills(gamedata *data, unit *u)
{ {
for (;;) { if (data->version < SKILLSORT_VERSION) {
int n = NOSKILL, level, weeks; for (;;) {
skill_t sk; int n = NOSKILL, level, weeks;
READ_INT(data->store, &n); skill_t sk;
sk = (skill_t)n; READ_INT(data->store, &n);
if (sk == NOSKILL) break; sk = (skill_t)n;
READ_INT(data->store, &level); if (sk == NOSKILL) break;
READ_INT(data->store, &weeks); READ_INT(data->store, &level);
if (level) { READ_INT(data->store, &weeks);
skill *sv = add_skill(u, sk); if (level) {
sv->level = sv->old = (unsigned char)level; skill *sv = add_skill(u, sk);
sv->weeks = (unsigned char)weeks; sv->level = sv->old = (unsigned char)level;
sv->weeks = (unsigned char)weeks;
}
}
}
else {
int i;
READ_INT(data->store, &u->skill_size);
u->skills = malloc(sizeof(skill)*u->skill_size);
for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i;
int val;
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);
} }
} }
} }
static void write_skills(gamedata *data, const unit *u) { static void write_skills(gamedata *data, const unit *u) {
int i; int i;
WRITE_INT(data->store, 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;
assert(sv->weeks <= sv->level * 2 + 1); assert(sv->weeks <= sv->level * 2 + 1);
if (sv->level > 0) { WRITE_INT(data->store, sv->id);
WRITE_INT(data->store, sv->id); WRITE_INT(data->store, sv->level);
WRITE_INT(data->store, sv->level); WRITE_INT(data->store, sv->weeks);
WRITE_INT(data->store, sv->weeks);
}
} }
WRITE_INT(data->store, -1);
WRITE_SECTION(data->store);
} }
unit *read_unit(gamedata *data) unit *read_unit(gamedata *data)

View File

@ -36,11 +36,12 @@
#define NOLANDITEM_VERSION 356 /* land_region has no items */ #define NOLANDITEM_VERSION 356 /* land_region has no items */
#define NORCSPELL_VERSION 357 /* data contains no RC_SPELL units */ #define NORCSPELL_VERSION 357 /* data contains no RC_SPELL units */
#define SORTKEYS_VERSION 358 /* at_keys is sorted */ #define SORTKEYS_VERSION 358 /* at_keys is sorted */
#define FAMILIAR_FIX_VERSION 359 /* at_keys is sorted */ #define FAMILIAR_FIX_VERSION 359 /* familiar links are fixed */
#define SKILLSORT_VERSION 360 /* u->skills is sorted */
/* unfinished: */ /* unfinished: */
#define CRYPT_VERSION 400 /* passwords are encrypted */ #define CRYPT_VERSION 400 /* passwords are encrypted */
#define RELEASE_VERSION FAMILIAR_FIX_VERSION /* current datafile */ #define RELEASE_VERSION SKILLSORT_VERSION /* current datafile */
#define MIN_VERSION INTPAK_VERSION /* minimal datafile we support */ #define MIN_VERSION INTPAK_VERSION /* minimal datafile we support */
#define MAX_VERSION RELEASE_VERSION /* change this if we can need to read the future datafile, and we can do so */ #define MAX_VERSION RELEASE_VERSION /* change this if we can need to read the future datafile, and we can do so */