scramble() neu geschrieben

Noch einen Sicherheitscheck in die NMR-Routine
Öfter mal log_error statt printf benutzen
This commit is contained in:
Enno Rehling 2004-01-04 18:14:20 +00:00
parent eaa0985501
commit a9d118f3ab
6 changed files with 32 additions and 103 deletions

View File

@ -120,65 +120,18 @@ income(const unit * u)
return 20 * u->number; return 20 * u->number;
} }
/* ------------------------------------------------------------- */
/* ------------------------------------------------------------- */
static struct scramble {
int index;
int rnd;
} * vec;
static int
scramblecmp(const void *p1, const void *p2)
{
return ((struct scramble *)p1)->rnd - ((struct scramble *)p2)->rnd;
}
static void static void
scramble(void *data, int n, size_t width) scramble(void *data, int n, size_t width)
{ {
int i; int j;
static int vecsize = 0; char temp[64];
void *buffer = NULL, *temp = NULL; assert(width<=sizeof(temp));
for (j=0;j!=n;++j) {
if (n > vecsize) { int k = rand() % n;
vecsize = n; memcpy(temp, (char*)data+j*width, width);
vec = (struct scramble *) realloc(vec, vecsize * sizeof(struct scramble)); memcpy((char*)data+j*width, (char*)data+k*width, width);
memcpy((char*)data+k*width, temp, width);
} }
for (i = 0; i != n; i++) {
vec[i].rnd = rand();
vec[i].index = i;
}
qsort(vec, n, sizeof(struct scramble), scramblecmp);
for (i = 0; i != n; i++) {
/* in vec[k].index steht, wohin der block k soll */
/* src soll nach target geschoben werden. dafür wird target gemerkt,
* src verschoben, und target zum neuen src. ende, wenn k wieder i ist */
if (vec[i].index!=i) {
char * src = ((char*)data)+width*i;
int k = i;
int dest = vec[k].index;
if (temp==NULL) {
temp = malloc(width);
}
buffer = temp;
do {
char * target = ((char*)data)+width*dest;
memmove(buffer, target, width);
memmove(target, src, width);
k = dest; /* wo das gerettete target hin soll */
dest = vec[dest].index;
vec[k].index = k; /* dest ist an der richtigen stelle */
/* swap buffer and src. misuse target as intermediate var. */
target = buffer;
buffer = src;
src = target;
} while (vec[i].index!=i);
}
}
if (buffer!=NULL) free(temp);
} }
#if 0 #if 0
#define MAX 6 #define MAX 6
@ -3395,10 +3348,6 @@ produce(void)
assert(rpeasants(r) >= 0); assert(rpeasants(r) >= 0);
} }
if (vec) {
free(vec);
vec = 0;
}
} }
void void

View File

@ -3699,7 +3699,7 @@ count_migrants (const faction * f)
u = u->nextF; u = u->nextF;
} }
if (f->num_migrants != n) if (f->num_migrants != n)
puts("FEHLER: Anzahl Migranten falsch"); log_error(("Anzahl Migranten für (%s) ist falsch: %n statt %n.\n", factionid(f), f->num_migrants, n));
#endif #endif
return f->num_migrants; return f->num_migrants;
} }

View File

@ -2400,7 +2400,7 @@ openbatch(void)
if (f->email) { if (f->email) {
sprintf(buf, "%s/mailit", reportpath()); sprintf(buf, "%s/mailit", reportpath());
if ((BAT = fopen(buf, "w")) == NULL) if ((BAT = fopen(buf, "w")) == NULL)
puts("* Fehler: mailit konnte nicht geöffnet werden!"); log_error(("mailit konnte nicht geöffnet werden!\n"));
else else
fprintf(BAT, fprintf(BAT,
"#!/bin/sh\n" "#!/bin/sh\n"
@ -3666,7 +3666,7 @@ report_summary(summary * s, summary * o, boolean full)
fprintf(F, "\n\n"); fprintf(F, "\n\n");
for (i = 0; i != NMRTimeout()+1; ++i) { for (i = 0; i <= NMRTimeout(); ++i) {
nmrs[i] = 0; nmrs[i] = 0;
} }
@ -3674,11 +3674,17 @@ report_summary(summary * s, summary * o, boolean full)
if (f->age <= 1 && turn - f->lastorders == 1) { if (f->age <= 1 && turn - f->lastorders == 1) {
newplayers++; newplayers++;
} else if (f->no != MONSTER_FACTION) { } else if (f->no != MONSTER_FACTION) {
nmrs[min(NMRTimeout(),turn-f->lastorders)]++; int nmr = turn-f->lastorders;
if (nmr<0 || nmr>NMRTimeout()) {
log_error(("faction %s has %d NMRS\n", factionid(f), nmr));
nmr = max(0, nmr);
nmr = min(nmr, NMRTimeout());
}
nmrs[nmr]++;
} }
} }
for (i = 0; i != NMRTimeout()+1; ++i) { for (i = 0; i <= NMRTimeout(); ++i) {
if (i == NMRTimeout()) { if (i == NMRTimeout()) {
fprintf(F, "+ NMRs:\t\t %d\n", nmrs[i]); fprintf(F, "+ NMRs:\t\t %d\n", nmrs[i]);
} else { } else {
@ -3723,10 +3729,6 @@ report_summary(summary * s, summary * o, boolean full)
fclose(F); fclose(F);
if (full) { if (full) {
#ifdef PLAYER_CSV
FILE *F;
region * r;
#endif
printf("Schreibe Liste der Adressen (adressen)...\n"); printf("Schreibe Liste der Adressen (adressen)...\n");
writeadresses(); writeadresses();
writenewssubscriptions(); writenewssubscriptions();
@ -3735,28 +3737,6 @@ report_summary(summary * s, summary * o, boolean full)
printf("writing date & turn\n"); printf("writing date & turn\n");
writeturn(); writeturn();
#ifdef PLAYER_CSV
{
strcpy(zText, "%s/players", basepath());
F = cfopen(zText, "w");
}
if (!F) return;
printf("Schreibe Spielerliste (players)...\n");
r = findregion(0, 0);
fputs("id;name;email;info;age;x;y;nmr;score;race;magic;units;people;money", F);
if (r) {
fputs("id;name;email;info;age;x;y;nmr;score;race;magic;units;people;money", F);
for (f=factions;f;f=f->next) {
fprintf(F, "%s;\"%s\";\"%s\";\"%s\";\"%s\";%d;%d;%d;%d;%d;"
"\"%s\";\"%s\";"
"%d;%d;%d\n",
factionid(f), f->name, f->email, f->banner, f->passw, f->age, region_x(r, f), region_y(r, f), turn-f->lastorders, f->score,
f->race->name[0], neue_gebiete[f->magiegebiet],
f->no_units, f->number, f->money);
}
}
fclose(F);
#endif
writemonument(); writemonument();
} }
free(nmrs); free(nmrs);

View File

@ -360,7 +360,7 @@ msg_error(const struct unit * u, const char * cmd, const char * name, const char
assert(cmd!=u->thisorder || !"only use entries from u->orders - memory corruption imminent."); assert(cmd!=u->thisorder || !"only use entries from u->orders - memory corruption imminent.");
if (!mtype) { if (!mtype) {
fprintf(stderr, "trying to create message of unknown type \"%s\"\n", name); log_error(("trying to create message of unknown type \"%s\"\n", name));
return NULL; return NULL;
} }
@ -395,7 +395,7 @@ msg_message(const char * name, const char* sig, ...)
memset(args, 0, sizeof(args)); memset(args, 0, sizeof(args));
if (!mtype) { if (!mtype) {
fprintf(stderr, "trying to create message of unknown type \"%s\"\n", name); log_error(("trying to create message of unknown type \"%s\"\n", name));
return NULL; return NULL;
} }
@ -435,7 +435,7 @@ new_message(struct faction * receiver, const char* sig, ...)
mtype = mt_find(buffer); mtype = mt_find(buffer);
if (!mtype) { if (!mtype) {
fprintf(stderr, "trying to create message of unknown type \"%s\"\n", buffer); log_error(("trying to create message of unknown type \"%s\"\n", buffer));
return NULL; return NULL;
} }

View File

@ -63,7 +63,7 @@ terraform_resources(region * r)
res->type = tdata->rawmaterials[i].type; res->type = tdata->rawmaterials[i].type;
res->amount = (int)(res->base * (1+(res->level-res->startlevel)*(res->divisor/100.0))); res->amount = (int)(res->base * (1+(res->level-res->startlevel)*(res->divisor/100.0)));
#ifdef RESOURCE_FACTOR #ifdef RESOURCE_FACTOR
res->amount = res->amount * RESOURCE_FACTOR; res->amount = (int)(res->amount * RESOURCE_FACTOR);
if (res->amount == 0) res->amount = 1; if (res->amount == 0) res->amount = 1;
#endif #endif
assert(res->amount>0); assert(res->amount>0);

View File

@ -221,9 +221,6 @@
<File <File
RelativePath=".\resolve.h"> RelativePath=".\resolve.h">
</File> </File>
<File
RelativePath=".\script.h">
</File>
<File <File
RelativePath=".\sql.h"> RelativePath=".\sql.h">
</File> </File>
@ -267,6 +264,12 @@
<File <File
RelativePath=".\event.c"> RelativePath=".\event.c">
</File> </File>
<File
RelativePath=".\functions.c">
</File>
<File
RelativePath=".\functions.h">
</File>
<File <File
RelativePath=".\goodies.c"> RelativePath=".\goodies.c">
</File> </File>
@ -294,9 +297,6 @@
<File <File
RelativePath=".\resolve.c"> RelativePath=".\resolve.c">
</File> </File>
<File
RelativePath=".\script.c">
</File>
<File <File
RelativePath=".\sql.c"> RelativePath=".\sql.c">
</File> </File>