- removing cruft

- fixing print_fighters() since list is not ordered by status anymore
This commit is contained in:
Enno Rehling 2006-04-15 22:10:57 +00:00
parent 573d283e92
commit 34fce9b8a7
4 changed files with 61 additions and 213 deletions

View File

@ -404,15 +404,14 @@ hpflee(int status)
return 0.0; return 0.0;
} }
int static int
get_unitrow(const fighter * af) get_row(const side * s, int row)
{ {
boolean counted[MAXSIDES]; boolean counted[MAXSIDES];
int enemyfront = 0; int enemyfront = 0;
int line, result; int line, result;
int retreat = 0; int retreat = 0;
int size[NUMROWS]; int size[NUMROWS];
int row = statusrow(af->status);
int front = 0; int front = 0;
memset(counted, 0, sizeof(counted)); memset(counted, 0, sizeof(counted));
@ -420,17 +419,19 @@ get_unitrow(const fighter * af)
for (line=FIRST_ROW;line!=NUMROWS;++line) { for (line=FIRST_ROW;line!=NUMROWS;++line) {
int si; int si;
/* how many enemies are there in the first row? */ /* how many enemies are there in the first row? */
for (si=0;af->side->enemies[si];++si) { for (si=0;s->enemies[si];++si) {
side *s = af->side->enemies[si]; side *se = s->enemies[si];
if (s->size[line]>0) { if (se->size[line]>0) {
int ai; int ai;
enemyfront += s->size[line]; /* - s->nonblockers[line] (nicht, weil angreifer) */ enemyfront += se->size[line]; /* - s->nonblockers[line] (nicht, weil angreifer) */
for (ai=0;s->enemies[ai];++ai) { for (ai=0;se->enemies[ai];++ai) {
side * ally = s->enemies[ai]; side * ally = se->enemies[ai];
if (!counted[ally->index] && !enemy(ally, af->side)) { if (!counted[ally->index] && !enemy(ally, s)) {
int i; int i;
counted[ally->index] = true; counted[ally->index] = true;
for (i=0;i!=NUMROWS;++i) size[i] += ally->size[i] - ally->nonblockers[i]; for (i=0;i!=NUMROWS;++i) {
size[i] += ally->size[i] - ally->nonblockers[i];
}
} }
} }
} }
@ -453,17 +454,11 @@ get_unitrow(const fighter * af)
return result; return result;
} }
#if 0 int
static int get_unitrow(const fighter * af)
sort_fighterrow(fighter ** elem1, fighter ** elem2)
{ {
int a, b; return get_row(af->side, statusrow(af->status));
a = get_unitrow(*elem1);
b = get_unitrow(*elem2);
return (a < b) ? -1 : ((a == b) ? 0 : 1);
} }
#endif
static void static void
reportcasualties(battle * b, fighter * fig, int dead) reportcasualties(battle * b, fighter * fig, int dead)
@ -1320,7 +1315,7 @@ select_opponent(battle * b, troop at, int minrow, int maxrow)
* cvector *fgs=fighters(b,af,FIGHT_ROW,BEHIND_ROW, FS_HELP|FS_ENEMY); * cvector *fgs=fighters(b,af,FIGHT_ROW,BEHIND_ROW, FS_HELP|FS_ENEMY);
* fighter *fig; * fighter *fig;
* *
* Optional: Verwirbeln. Vorsicht: Aufwendig! * Optional: Verwirbeln. Vorsicht: Aufwändig!
* v_scramble(fgs->begin, fgs->end); * v_scramble(fgs->begin, fgs->end);
* *
* for (fig = fgs->begin; fig != fgs->end; ++fig) { * for (fig = fgs->begin; fig != fgs->end; ++fig) {
@ -2654,24 +2649,26 @@ battle_punit(unit * u, battle * b)
} }
static void static void
print_fighters(battle * b, fighter * fighters) print_fighters(battle * b, const side * s)
{ {
fighter *df; fighter *df;
int lastrow = -1; int row;
for (df=fighters; df; df=df->next) { for (row=1;row!=NUMROWS;++row) {
int unitrow = get_row(s, row);
for (df=s->fighters; df; df=df->next) {
unit *du = df->unit; unit *du = df->unit;
int thisrow = statusrow(df->unit->status);
int row = get_unitrow(df); if (row == thisrow) {
message * m = msg_message("battle::row_header", "row", unitrow);
if (row != lastrow) {
message * m = msg_message("battle::row_header", "row", row);
message_all(b, m); message_all(b, m);
msg_release(m); msg_release(m);
lastrow = row;
} }
battle_punit(du, b); battle_punit(du, b);
} }
}
} }
boolean boolean
@ -2794,7 +2791,7 @@ print_stats(battle * b)
} }
battledebug(buf); battledebug(buf);
} }
print_fighters(b, s->fighters); print_fighters(b, s);
} }
battlerecord(b, " "); battlerecord(b, " ");
@ -3787,65 +3784,6 @@ battle_stats(FILE * F, battle * b)
} }
} }
/** randomly shuffle an array of fighters.
* for correctness, see Donald E. Knuth, The Art of Computer Programming
*/
#if 0
static void
shuffle_fighters(fighter ** fighters, int n)
{
int i;
for (i=0;i!=n;++i) {
int j = i + (rng_int() % (n-i));
fighter * fig = fighters[i];
fighters[i] = fighters[j];
fighters[j] = fig;
}
}
static void
sort_fighters(fighter ** fighters, int * rows, int N)
{
unsigned int n = N, i = n/2, parent, child;
int tr;
fighter * tf;
for (;;) {
if (i > 0) {
i--;
tr = rows[i];
tf = fighters[i];
} else {
n--;
if (n == 0) return;
tr = rows[n];
tf = fighters[n];
rows[n] = rows[0];
fighters[n] = fighters[0];
}
parent = i;
child = i*2 + 1;
while (child < n) {
if (child + 1 < n && rows[child + 1] > rows[child]) {
child++;
}
if (rows[child] > tr) {
rows[parent] = rows[child];
fighters[parent] = fighters[child];
parent = child;
child = parent*2 + 1;
} else {
break;
}
}
rows[parent] = tr;
fighters[parent] = tf;
}
}
#endif
/** execute one round of attacks /** execute one round of attacks
* fig->fighting is used to determine who attacks, not fig->alive, since * fig->fighting is used to determine who attacks, not fig->alive, since
* the latter may be influenced by attacks that already took place. * the latter may be influenced by attacks that already took place.
@ -4033,35 +3971,6 @@ do_battle(void)
/* PRECOMBATSPELLS */ /* PRECOMBATSPELLS */
do_combatmagic(b, DO_PRECOMBATSPELL); do_combatmagic(b, DO_PRECOMBATSPELL);
/* Nun erstellen wir eine Liste von allen Kämpfern, die wir
* dann scramblen. Zuerst werden sie wild gemischt, und dann wird (stabil)
* nach der Kampfreihe sortiert */
/* das ist wahrscheinlich alles nicht wichtig, sondern uralt */
#if 0
b->fighters = malloc(sizeof(fighter*)*b->nfighters);
for (i=0, s=b->sides; s; s=s->next) {
fighter * fig;
for (fig=s->fighters; fig; fig=fig->next) {
b->fighters[i++] = fig;
}
}
assert(i<=b->nfighters);
shuffle_fighters(b->fighters, b->nfighters);
#endif
/*
rows = malloc(sizeof(int)*b->nfighters);
for (i=0;i!=b->nfighters;++i) {
rows[i] = get_unitrow(b->fighters[i]);
}
sort_fighters(b->fighters, rows, b->nfighters);
free(rows);
for (s = b->sides; s; s = s->next) {
v_sort(s->fighters.begin, s->fighters.end, (v_sort_fun) sort_fighterrow);
}
*/
print_stats(b); /* gibt die Kampfaufstellung aus */ print_stats(b); /* gibt die Kampfaufstellung aus */
printf("%s (%d, %d) : ", rname(r, NULL), r->x, r->y); printf("%s (%d, %d) : ", rname(r, NULL), r->x, r->y);

View File

@ -58,9 +58,6 @@ extern "C" {
bfaction * factions; bfaction * factions;
int nfactions; int nfactions;
int nfighters; int nfighters;
#if 0
struct fighter ** fighters;
#endif
struct side * sides; struct side * sides;
int nsides; int nsides;
cvector meffects; cvector meffects;

View File

@ -68,34 +68,6 @@ cv_pushback(cvector * cv, void *u)
*(cv->end++) = u; *(cv->end++) = u;
} }
void
v_sort(void **begin, void **end, int (__cdecl * compare) (const void *, const void *))
{
qsort(begin, end - begin, sizeof(void *), compare);
}
void **
v_find(void **begin, void **end, void *item)
{
void **it;
for (it = begin; it != end; ++it)
if (*it == item)
return it;
return it;
}
void **
v_findx(void **begin, void **end, void *item, int (__cdecl * cmp) (void *, void *))
{
void **it;
for (it = begin; it != end; ++it)
if (cmp(*it, item))
return it;
return it;
}
int int
__cv_scramblecmp(const void *p1, const void *p2) __cv_scramblecmp(const void *p1, const void *p2)
{ {
@ -104,27 +76,21 @@ __cv_scramblecmp(const void *p1, const void *p2)
#define addptr(p,i) ((void *)(((char *)p) + i)) #define addptr(p,i) ((void *)(((char *)p) + i))
void /** randomly shuffle an array
* for correctness, see Donald E. Knuth, The Art of Computer Programming
*/
static void
__cv_scramble(void *v1, size_t n, size_t width) __cv_scramble(void *v1, size_t n, size_t width)
{ {
size_t i; size_t i;
static size_t s = 0; void * temp = malloc(width);
static void *v = 0; for (i=0;i!=n;++i) {
size_t j = i + (rng_int() % (n-i));
if (n * (width + 4) > s) { memcpy(temp, addptr(v1, i*width), width);
s = n * (width + 4); memcpy(addptr(v1, i*width), addptr(v1, j*width), width);
v = (void *) realloc(v, s); memcpy(addptr(v1, j*width), temp, width);
} }
for (i = 0; i != n; i++) { free(temp);
*(long *) addptr(v, i * (width + 4)) = rng_int();
memcpy(addptr(v, i * (width + 4) + 4), addptr(v1, i * width), width);
}
qsort(v, n, width + 4, __cv_scramblecmp);
for (i = 0; i != n; i++)
memcpy(addptr(v1, i * width), addptr(v, i * (width + 4) + 4), width);
} }
void void
@ -132,23 +98,3 @@ v_scramble(void **begin, void **end)
{ {
__cv_scramble(begin, end - begin, sizeof(void *)); __cv_scramble(begin, end - begin, sizeof(void *));
} }
void
cv_mergeunique(cvector * c, const cvector * a, const cvector * b, int (__cdecl * keyfun) (const void *))
{
void **ai, **bi;
assert(cv_size(c) == 0);
for (ai = a->begin, bi = b->begin; ai != a->end || bi != b->end;) {
int ak = (ai == a->end) ? INT_MAX : keyfun(*ai);
int bk = (bi == b->end) ? INT_MAX : keyfun(*bi);
int nk = (ak < bk) ? ak : bk;
cv_pushback(c, (ak == nk) ? (*ai) : (*bi));
while (ai != a->end && keyfun(*ai) == nk)
++ai;
while (bi != b->end && keyfun(*bi) == nk)
++bi;
}
}

View File

@ -40,11 +40,7 @@ cvector *cv_kill(cvector * cv);
size_t cv_size(cvector * cv); size_t cv_size(cvector * cv);
void cv_reserve(cvector * cv, size_t size); void cv_reserve(cvector * cv, size_t size);
void cv_pushback(cvector * cv, void *u); void cv_pushback(cvector * cv, void *u);
void v_sort(void **begin, void **end, int (__cdecl * compare) (const void *, const void *));
extern void **v_find(void **begin, void **end, void *);
extern void **v_findx(void **begin, void **end, void *, int (__cdecl * cmp) (void *, void *));
void v_scramble(void **begin, void **end); void v_scramble(void **begin, void **end);
void cv_mergeunique(cvector * c, const cvector * a, const cvector * b, int (__cdecl * keyfun) (const void *));
#define cv_remove(c, i) { void** x = v_find((c)->begin, (c)->end, (i)); if (x) { *x = *(c)->end; (c)->end--; } } #define cv_remove(c, i) { void** x = v_find((c)->begin, (c)->end, (i)); if (x) { *x = *(c)->end; (c)->end--; } }
#define cv_foreach(item, vector) \ #define cv_foreach(item, vector) \