From d0022db79aef470550d50a10ffaf3230527de459 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 16 May 2008 22:37:13 +0000 Subject: [PATCH] Data format change reflects that id 0 is now illegal (so we use that instead of -1) --- src/common/attributes/giveitem.c | 8 +- src/common/kernel/building.c | 24 ++-- src/common/kernel/faction.c | 4 +- src/common/kernel/group.c | 2 +- src/common/kernel/magic.c | 12 +- src/common/kernel/save.c | 215 ++----------------------------- src/common/kernel/ship.c | 2 +- src/common/kernel/unit.c | 2 +- src/common/kernel/version.h | 3 +- src/common/spells/spells.c | 1 + src/scripts/run-tests.lua | 6 +- 11 files changed, 48 insertions(+), 231 deletions(-) diff --git a/src/common/attributes/giveitem.c b/src/common/attributes/giveitem.c index 00c3894b2..9fb31fca3 100644 --- a/src/common/attributes/giveitem.c +++ b/src/common/attributes/giveitem.c @@ -59,8 +59,12 @@ a_readgive(attrib * a, struct storage * store) char zText[32]; var.i = store->r_id(store); - gdata->building = findbuilding(var.i); - if (gdata->building==NULL) ur_add(var, (void**)&gdata->building, resolve_building); + if (var.i>0) { + gdata->building = findbuilding(var.i); + if (gdata->building==NULL) ur_add(var, (void**)&gdata->building, resolve_building); + } else { + gdata->building=NULL; + } for (;;) { int i; store->r_tok_buf(store, zText, sizeof(zText)); diff --git a/src/common/kernel/building.c b/src/common/kernel/building.c index 3ab59c35e..d0c5dbea3 100644 --- a/src/common/kernel/building.c +++ b/src/common/kernel/building.c @@ -377,23 +377,23 @@ resolve_building(variant id) { void write_building_reference(const struct building * b, struct storage * store) { - store->w_id(store, b?b->no:-1); + store->w_id(store, b?b->no:0); } int read_building_reference(struct building ** b, struct storage * store) { - variant var; - var.i = store->r_id(store); - if (var.i<=0) { - *b = NULL; - return AT_READ_FAIL; - } - else { - *b = findbuilding(var.i); - if (*b==NULL) ur_add(var, (void**)b, resolve_building); - return AT_READ_OK; - } + variant var; + var.i = store->r_id(store); + if (var.i<=0) { + *b = NULL; + return AT_READ_FAIL; + } + else { + *b = findbuilding(var.i); + if (*b==NULL) ur_add(var, (void**)b, resolve_building); + return AT_READ_OK; + } } void diff --git a/src/common/kernel/faction.c b/src/common/kernel/faction.c index e073677b6..16962261d 100644 --- a/src/common/kernel/faction.c +++ b/src/common/kernel/faction.c @@ -208,7 +208,7 @@ read_faction_reference(faction ** f, struct storage * store) { variant id; id.i = store->r_id(store); - if (id.i<0) { + if (id.i<=0 && (store->version>=NOZEROIDS_VERSION || id.i<0)) { *f = NULL; return AT_READ_FAIL; } @@ -220,7 +220,7 @@ read_faction_reference(faction ** f, struct storage * store) void write_faction_reference(const faction * f, struct storage * store) { - store->w_id(store, f?f->no:-1); + store->w_id(store, f?f->no:0); } void diff --git a/src/common/kernel/group.c b/src/common/kernel/group.c index 6c266c1e7..28a16bbfc 100644 --- a/src/common/kernel/group.c +++ b/src/common/kernel/group.c @@ -191,7 +191,7 @@ write_groups(struct storage * store, group * g) store->w_int(store, a->status); } } - store->w_id(store, -1); + store->w_id(store, 0); a_write(store, g->attribs); store->w_brk(store); g=g->next; diff --git a/src/common/kernel/magic.c b/src/common/kernel/magic.c index 7f58b0b20..7a4b6fc53 100644 --- a/src/common/kernel/magic.c +++ b/src/common/kernel/magic.c @@ -2224,10 +2224,10 @@ resolve_familiar(variant data) static int read_familiar(attrib * a, struct storage * store) { - variant id; + variant id; id.i = store->r_id(store); - ur_add(id, &a->data.v, resolve_familiar); - return AT_READ_OK; + ur_add(id, &a->data.v, resolve_familiar); + return AT_READ_OK; } /* clones */ @@ -2300,10 +2300,10 @@ resolve_clone(variant data) static int read_clone(attrib * a, struct storage * store) { - variant id; + variant id; id.i = store->r_id(store); - ur_add(id, &a->data.v, resolve_clone); - return AT_READ_OK; + ur_add(id, &a->data.v, resolve_clone); + return AT_READ_OK; } /* mages */ diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index 3ea663eb3..02c1facf9 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -241,155 +241,6 @@ fwritestr(FILE * F, const char * str) return nwrite + 2; } -#if 0 -#define store->r_id(store) ri36(F) - -static void -rds(struct storage * store, char **ds) -{ - static char buffer[DISPLAYSIZE + 1]; /*Platz für null-char nicht vergessen!*/ - char *s = &buffer[0]; - int c = getc(F); - while (c != '"') { - if (c == EOF) { - *s = 0; - fprintf(stderr, "Die Datei bricht vorzeitig ab.\n"); - abort(); - } - c = getc(F); - } - - c = getc(F); - - while (c != '"') { - if (c == EOF) { - *s = 0; - fprintf(stderr, "Die Datei bricht vorzeitig ab.\n"); - abort(); - } - if (s - buffer < DISPLAYSIZE) { - *s++ = (char)c; - } - c = getc(F); - } - - c = getc(F); - *s = 0; - if (ds) { - *ds = realloc(*ds, sizeof(char) * (strlen(buffer) + 1)); - strcpy(*ds, buffer); - } -} - - -static void -xrds(struct storage * store, char **ds, int encoding) -{ - static char buffer[DISPLAYSIZE + 1]; /*Platz für null-char nicht vergessen!*/ - int len = store->r_str_buf(store, buffer, sizeof(buffer)); - - if (len>=0) { - if (ds) { - *ds = realloc(*ds, sizeof(char) * (len + 1)); - strcpy(*ds, buffer); - } - } -} - -#define rcf(F) (getc(F)); -void -rsf(struct storage * store, char *s, size_t len) -{ - char * begin = s; - int c; - do { - c = getc(F);; - if (c == EOF) { - puts("Die Datei bricht vorzeitig ab."); - abort(); - } - } while (c != '"'); - - for (;;) { - c = getc(F); - if (c == '"') break; - else if (c == EOF) { - puts("Die Datei bricht vorzeitig ab."); - abort(); - } - if (s-begin<(int)len-1) - *s++ = (char)c; - } - *s = 0; -} - -static void -rs(struct storage * store, char *s) -{ - boolean apos = false; - int c = getc(F); - while (isspace(c)) c = getc(F); - if (c=='"') { - apos = true; - c = getc(F); - } - for (;;) { - if (c=='"') { - c = getc(F); - break; - } else if (!apos && isspace(c)) break; - *s++ = (char)c; - c = getc(F); - } - *s = 0; -} - -#define store->r_str_buf(store, buf, size) store->r_tok_buf(store, buf) /* should check size but doesn't */ - -static int -ri(struct storage * store) -{ - int i = 0, vz = 1; - - int c = getc(F); - while (!xisdigit(c)) { - if (c == EOF) { - puts("Die Datei bricht vorzeitig ab."); - abort(); - } - c = getc(F); - } - - while (xisdigit(c)) { - if (c == '-') - vz = -1; - else - i = 10 * i + (c - '0'); - c = getc(F); - } - - return i * vz; -} - -static int -ri36(struct storage * store) -{ - char buf[16]; - int i = 0; - int c = getc(F); - while (!isalnum(c)) c = getc(F); - while (isalnum(c)) { - if (i+1enemies = NULL; for (;;) { int fno = store->r_id(store); - if (fno<0) break; + if (fno<=0) break; else { faction_list * flist = malloc(sizeof(faction_list)); flist->next = f->enemies; @@ -414,7 +265,7 @@ write_enemies(struct storage * store, const faction_list * flist) while (flist) { write_faction_reference(flist->data, store); } - store->w_id(store, -1); + store->w_id(store, 0); } #endif @@ -735,47 +586,6 @@ read_alliances(struct storage * store) } } -#if 0 -#define wc(F, c) putc(c, F); -#define wnl(F) putc('\n', F); -#define whs(F, s) fputs(s, F); putc(' ', F) - -size_t -wsn(struct storage * store, const char *s) -{ - size_t n = 0; - if (!s) - return 0; - while (*s) { - wc(F, *s++); - ++n; - } - return n; -} - -size_t -ws(struct storage * store, const char *s) -{ - size_t n; - fputc('"', F); - n = wsn(F, s); - fputs("\" ", F); - return n+3; -} - -static int -wi(struct storage * store, int n) -{ - return fprintf(F, "%d ", n); -} - -static int -wi36(struct storage * store, int n) -{ - return fprintf(F, "%s ", itoa36(n)); -} -#endif - void write_alliances(struct storage * store) { @@ -850,6 +660,7 @@ readunit(struct storage * store) int number, n, p; order ** orderp; char obuf[1024]; + faction * f; n = store->r_id(store); u = findunit(n); @@ -865,11 +676,11 @@ readunit(struct storage * store) u->skill_size = 0; u_setfaction(u, NULL); } - { - int n = store->r_id(store); - faction * f = findfaction(n); - if (f!=u->faction) u_setfaction(u, f); - } + + n = store->r_id(store); + f = findfaction(n); + if (f!=u->faction) u_setfaction(u, f); + u->name = store->r_str(store); if (lomem) { store->r_str_buf(store, NULL, 0); @@ -934,10 +745,10 @@ readunit(struct storage * store) set_number(u, number); n = store->r_id(store); - if (n>=0) u->building = findbuilding(n); + if (n>0) u->building = findbuilding(n); n = store->r_id(store); - if (n>=0) u->ship = findship(n); + if (n>0) u->ship = findship(n); if (store->version <= 73) { if (store->r_int(store)) { @@ -1370,7 +1181,7 @@ readfaction(struct storage * store) if (alliances!=NULL) { int allianceid = store->r_id(store); - if (allianceid>=0) f->alliance = findalliance(allianceid); + if (allianceid>0) f->alliance = findalliance(allianceid); if (f->alliance) { faction_list * flist = malloc(sizeof(faction_list)); flist->data = f; @@ -1493,7 +1304,7 @@ writefaction(struct storage * store, const faction * f) store->w_int(store, f->subscription); if (alliances!=NULL) { if (f->alliance) store->w_id(store, f->alliance->id); - else store->w_id(store, -1); + else store->w_id(store, 0); } store->w_str(store, (const char *)f->name); @@ -1540,7 +1351,7 @@ writefaction(struct storage * store, const faction * f) store->w_int(store, sf->status); } } - store->w_id(store, -1); + store->w_id(store, 0); store->w_brk(store); write_groups(store, f->groups); #ifdef ENEMIES diff --git a/src/common/kernel/ship.c b/src/common/kernel/ship.c index cd59a06af..5bfc514c5 100644 --- a/src/common/kernel/ship.c +++ b/src/common/kernel/ship.c @@ -283,7 +283,7 @@ shipowner(const ship * sh) void write_ship_reference(const struct ship * sh, struct storage * store) { - store->w_id(store, sh?sh->no:-1); + store->w_id(store, sh?sh->no:0); } void diff --git a/src/common/kernel/unit.c b/src/common/kernel/unit.c index 9a2c6f815..00014990c 100644 --- a/src/common/kernel/unit.c +++ b/src/common/kernel/unit.c @@ -597,7 +597,7 @@ free_units(void) void write_unit_reference(const unit * u, struct storage * store) { - store->w_id(store, u?u->no:-1); + store->w_id(store, u?u->no:0); } void * diff --git a/src/common/kernel/version.h b/src/common/kernel/version.h index b783ef15e..0a53de7de 100644 --- a/src/common/kernel/version.h +++ b/src/common/kernel/version.h @@ -55,6 +55,7 @@ #define UID_VERSION 327 /* regions have a unique id */ #define STORAGE_VERSION 328 /* with storage.h, some things are stored smarter (ids as base36, fractions as float) */ #define INTPAK_VERSION 329 /* in binary, ints can get packed */ +#define NOZEROIDS_VERSION 330 /* zero is not a valid ID for anything (including factions) */ #define MIN_VERSION CURSETYPE_VERSION /* minimal datafile we support */ -#define RELEASE_VERSION INTPAK_VERSION /* current datafile */ +#define RELEASE_VERSION NOZEROIDS_VERSION /* current datafile */ diff --git a/src/common/spells/spells.c b/src/common/spells/spells.c index 3489fe466..ca69e69a0 100644 --- a/src/common/spells/spells.c +++ b/src/common/spells/spells.c @@ -3399,6 +3399,7 @@ sp_chaossuction(castorder *co) return 0; } + /* TODO: implement with a building */ create_special_direction(r, rt, 2, "vortex_desc", "vortex"); create_special_direction(rt, r, 2, "vortex_desc", "vortex"); new_border(&bt_chaosgate, r, rt); diff --git a/src/scripts/run-tests.lua b/src/scripts/run-tests.lua index 8f9dc4396..9832ef57d 100644 --- a/src/scripts/run-tests.lua +++ b/src/scripts/run-tests.lua @@ -12,7 +12,7 @@ end test_locales() local now = os.clock() -read_game("567.dat", "binary") +read_game("571.new", "binary") --read_game("566.txt", "text") --write_game("566.txt", "text") local elapsed = os.clock() - now @@ -23,6 +23,6 @@ print(elapsed) f = get_faction(0) --renumber(f, 666) ---write_game("566.dat", "binary") +-- write_game("571.new", "binary") ---io.stdin:read("*line") +io.stdin:read("*line")