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;
}
/* ------------------------------------------------------------- */
/* ------------------------------------------------------------- */
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
scramble(void *data, int n, size_t width)
{
int i;
static int vecsize = 0;
void *buffer = NULL, *temp = NULL;
if (n > vecsize) {
vecsize = n;
vec = (struct scramble *) realloc(vec, vecsize * sizeof(struct scramble));
int j;
char temp[64];
assert(width<=sizeof(temp));
for (j=0;j!=n;++j) {
int k = rand() % n;
memcpy(temp, (char*)data+j*width, width);
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
#define MAX 6
@ -3395,10 +3348,6 @@ produce(void)
assert(rpeasants(r) >= 0);
}
if (vec) {
free(vec);
vec = 0;
}
}
void

View File

@ -3699,7 +3699,7 @@ count_migrants (const faction * f)
u = u->nextF;
}
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
return f->num_migrants;
}

View File

@ -2400,7 +2400,7 @@ openbatch(void)
if (f->email) {
sprintf(buf, "%s/mailit", reportpath());
if ((BAT = fopen(buf, "w")) == NULL)
puts("* Fehler: mailit konnte nicht geöffnet werden!");
log_error(("mailit konnte nicht geöffnet werden!\n"));
else
fprintf(BAT,
"#!/bin/sh\n"
@ -3666,7 +3666,7 @@ report_summary(summary * s, summary * o, boolean full)
fprintf(F, "\n\n");
for (i = 0; i != NMRTimeout()+1; ++i) {
for (i = 0; i <= NMRTimeout(); ++i) {
nmrs[i] = 0;
}
@ -3674,11 +3674,17 @@ report_summary(summary * s, summary * o, boolean full)
if (f->age <= 1 && turn - f->lastorders == 1) {
newplayers++;
} 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()) {
fprintf(F, "+ NMRs:\t\t %d\n", nmrs[i]);
} else {
@ -3723,10 +3729,6 @@ report_summary(summary * s, summary * o, boolean full)
fclose(F);
if (full) {
#ifdef PLAYER_CSV
FILE *F;
region * r;
#endif
printf("Schreibe Liste der Adressen (adressen)...\n");
writeadresses();
writenewssubscriptions();
@ -3735,28 +3737,6 @@ report_summary(summary * s, summary * o, boolean full)
printf("writing date & turn\n");
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();
}
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.");
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;
}
@ -395,7 +395,7 @@ msg_message(const char * name, const char* sig, ...)
memset(args, 0, sizeof(args));
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;
}
@ -435,7 +435,7 @@ new_message(struct faction * receiver, const char* sig, ...)
mtype = mt_find(buffer);
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;
}

View File

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

View File

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