Fix stats handling

This commit is contained in:
Stefan Bühler 2009-08-19 13:16:28 +02:00
parent 3da47935a4
commit 41b98c0c01
4 changed files with 103 additions and 56 deletions

View File

@ -65,7 +65,7 @@ struct listhead {
}; };
typedef struct player { typedef struct player {
listhead list; listhead list, list_all_players;
int xpos,ypos; int xpos,ypos;
int flags; int flags;
int abilities; int abilities;
@ -81,6 +81,7 @@ typedef struct player {
int color; int color;
int controller; int controller;
int fixx,fixy; int fixx,fixy;
int kills, deaths;
figure *figure; figure *figure;
} player; } player;
@ -210,7 +211,7 @@ enum tile_types {
#define MAXSPRITES 256 #define MAXSPRITES 256
#define MAXDAMAGES 512 #define MAXDAMAGES 512
extern char exitflag; extern volatile char exitflag;
extern uchar needwhole; extern uchar needwhole;
extern figure walking[MAXSETS][60]; extern figure walking[MAXSETS][60];

119
game.c
View File

@ -18,6 +18,7 @@ static listhead activegeneric;
static listhead detonatebombs; static listhead detonatebombs;
static listhead activeflames; static listhead activeflames;
static listhead activeplayers; static listhead activeplayers;
static listhead allplayers;
figure walking[MAXSETS][NUMWALKFRAMES]; figure walking[MAXSETS][NUMWALKFRAMES];
solid background, backgroundoriginal; solid background, backgroundoriginal;
@ -26,7 +27,7 @@ solid background, backgroundoriginal;
unsigned char field[32][32]; unsigned char field[32][32];
void *info[32][32]; void *info[32][32];
char exitflag = 0; volatile char exitflag = 0;
static int framecount = 0; static int framecount = 0;
char playername[16]; char playername[16];
@ -46,8 +47,6 @@ TILE_NONE,160
static GameOptions gameoptions; static GameOptions gameoptions;
static int host_kills, host_deaths;
static const unsigned char playerpositions[MAXNETNODES*3] = { /* color, x, y */ static const unsigned char playerpositions[MAXNETNODES*3] = { /* color, x, y */
2,0,0, 2,0,0,
3,14,10, 3,14,10,
@ -59,28 +58,41 @@ static const unsigned char playerpositions[MAXNETNODES*3] = { /* color, x, y */
8,14,4, 8,14,4,
}; };
static void resetplayer(player *pl, int color, int x, int y) {
pl->speed=SPEEDSTART;
pl->flags=0;
pl->flamelength=gameoptions.flames+1;
pl->bombsavailable=gameoptions.bombs+1;
pl->color=color;
pl->xpos=arraytoscreenx(x);
pl->ypos=arraytoscreeny(y);
field[y][x]=FIELD_EMPTY;
if(x) field[y][x-1]=FIELD_EMPTY;
if(y) field[y-1][x]=FIELD_EMPTY;
if(x<arraynumx-1) field[y][x+1]=FIELD_EMPTY;
if(y<arraynumy-1) field[y+1][x]=FIELD_EMPTY;
addtail(&activeplayers, pl);
}
static void initplayer(int color,int x,int y,int controller) { static void initplayer(int color,int x,int y,int controller) {
player *pl; player *pl;
pl = allocentry(player); pl = allocentry(player);
if(!pl) if(!pl)
nomem("Couldn't get player structure (allocentry())"); nomem("Couldn't get player structure (allocentry())");
addtail(&activeplayers,pl);
pl->xpos=arraytoscreenx(x); list_add_tail(&allplayers, &pl->list_all_players);
pl->ypos=arraytoscreeny(y);
pl->color=color; pl->controller=controller;
pl->speed=SPEEDSTART;
pl->flags=0;
pl->fixx=-4; pl->fixx=-4;
pl->fixy=-40; pl->fixy=-40;
pl->flamelength=gameoptions.flames+1; pl->kills = pl->deaths = 0;
pl->bombsavailable=gameoptions.bombs+1;
pl->controller=controller; resetplayer(pl, color, x, y);
field[y][x]=FIELD_EMPTY;
if(x) field[y][x-1]=FIELD_EMPTY;
if(y) field[y-1][x]=FIELD_EMPTY;
if(x<arraynumx-1) field[y][x+1]=FIELD_EMPTY;
if(y<arraynumy-1) field[y+1][x]=FIELD_EMPTY;
} }
static void initplayers(void) { static void initplayers(void) {
@ -102,22 +114,36 @@ static void initplayers(void) {
} }
} }
static void resetstats(void) { static void resetplayers(void) {
int i; const unsigned char *p;
int c,x,y;
player *pl;
host_deaths = host_kills = 0; p=playerpositions;
for (i = 0; i < MAXNETNODES; i++) { list_for_each_entry(pl, &allplayers, list_all_players) {
netnodes[i].deaths = netnodes[i].kills = 0; c=*p++;
x=*p++;
y=*p++;
resetplayer(pl, c, x, y);
} }
} }
static void firstzero(void) { static void firstzero(void) {
alloc_things();
list_init_head(&activebombs);
list_init_head(&detonatebombs);
list_init_head(&activeflames);
list_init_head(&activedecays);
list_init_head(&activebonus);
list_init_head(&activeplayers);
list_init_head(&activegeneric);
list_init_head(&allplayers);
mycount = mydatacount = 0; mycount = mydatacount = 0;
memset(latestactions,0,sizeof(latestactions)); memset(latestactions,0,sizeof(latestactions));
memset(latestcounts,0,sizeof(latestcounts)); memset(latestcounts,0,sizeof(latestcounts));
memset(actionblock,0,sizeof(actionblock)); memset(actionblock,0,sizeof(actionblock));
actioncount = 0; actioncount = 0;
resetstats();
} }
static void initgame() { static void initgame() {
@ -130,14 +156,14 @@ static void initgame() {
if (network != NETWORK_SLAVE) if (network != NETWORK_SLAVE)
set_game_options(&configopts); set_game_options(&configopts);
gameframe=0; gameframe=0;
allocthings();
list_init_head(&activebombs); things_list_clear(&activebombs);
list_init_head(&detonatebombs); things_list_clear(&detonatebombs);
list_init_head(&activeflames); things_list_clear(&activeflames);
list_init_head(&activedecays); things_list_clear(&activedecays);
list_init_head(&activebonus); things_list_clear(&activebonus);
list_init_head(&activeplayers); list_init_head(&activeplayers);
list_init_head(&activegeneric); things_list_clear(&activegeneric);
p=bonuschances; p=bonuschances;
bonustotal=0; bonustotal=0;
@ -160,7 +186,7 @@ static void initgame() {
solidcopyany(&backgroundoriginal,&background,0,0,IXSIZE,IYSIZE); solidcopyany(&backgroundoriginal,&background,0,0,IXSIZE,IYSIZE);
initplayers(); resetplayers();
for(j=0;j<arraynumy;++j) { for(j=0;j<arraynumy;++j) {
y=arraystarty+j*arrayspacey; y=arraystarty+j*arrayspacey;
@ -532,11 +558,7 @@ static void adddeath(player *pl) {
} }
static void killplayer(player *pl) { static void killplayer(player *pl) {
if (pl->controller == -1) { pl->deaths++;
host_deaths++;
} else {
netnodes[pl->controller].deaths++;
}
pl->flags|=FLG_DEAD; pl->flags|=FLG_DEAD;
playsound(2); playsound(2);
adddeath(pl); adddeath(pl);
@ -561,23 +583,19 @@ static void drawgenerics(void) {
} }
static void drawstats(void) { static void drawstats(void) {
int d, k, p = 0, i; player *pl;
int p = 0;
const char *n; const char *n;
char buf[16]; char buf[16];
solidcopy(&background, 0, 0, IXSIZE, arraystarty); solidcopy(&background, 0, 0, IXSIZE, arraystarty);
for (i = -1; i < MAXNETNODES; i++) { list_for_each_entry(pl, &allplayers, list_all_players) {
if (i >= 0) { if (pl->controller >= 0) {
if (!netnodes[i].used) continue; n = netnodes[pl->controller].name;
k = netnodes[i].kills;
d = netnodes[i].deaths;
n = netnodes[i].name;
} else { } else {
k = host_kills;
d = host_deaths;
n = playername; n = playername;
} }
snprintf(buf, sizeof(buf), "%-8.8s %2i/%2i", n, k % 100, d % 100); snprintf(buf, sizeof(buf), "%-8.8s %2i/%2i", n, pl->kills % 100, pl->deaths % 100);
drawstring(11 + (p/2) * (15 * fontxsize + 7), 11 + (p%2) * (fontysize+2), buf); drawstring(11 + (p/2) * (15 * fontxsize + 7), 11 + (p%2) * (fontysize+2), buf);
p++; p++;
} }
@ -754,6 +772,7 @@ static void doplayer(player *pl) {
if(what==ACT_QUIT) { if(what==ACT_QUIT) {
killplayer(pl); killplayer(pl);
list_del(&pl->list_all_players);
return; return;
} }
@ -761,6 +780,12 @@ static void doplayer(player *pl) {
playsound((myrand()&1) ? 1 : 5); playsound((myrand()&1) ? 1 : 5);
applybonus(pl,info[py][px]); applybonus(pl,info[py][px]);
} else if(there==FIELD_FLAME) { } else if(there==FIELD_FLAME) {
flame *fl = info[py][px];
if (fl->owner == pl) {
pl->kills--;
} else {
fl->owner->kills++;
}
killplayer(pl); killplayer(pl);
return; return;
} }
@ -935,6 +960,7 @@ void run_single_player(void) {
network = NETWORK_NONE; network = NETWORK_NONE;
firstzero(); firstzero();
initplayers();
do { do {
initgame(); initgame();
while(!(code=iterate()) && !exitflag) ++framecount; while(!(code=iterate()) && !exitflag) ++framecount;
@ -945,6 +971,7 @@ void run_network_game(void) {
int code; int code;
firstzero(); firstzero();
initplayers();
do { do {
initgame(); initgame();
while (!(code=iterate()) && !exitflag) ++framecount; while (!(code=iterate()) && !exitflag) ++framecount;

32
list.c
View File

@ -6,6 +6,7 @@
typedef union genericlistitem genericlistitem; typedef union genericlistitem genericlistitem;
union genericlistitem { union genericlistitem {
genericlistitem *next; genericlistitem *next;
listhead list;
bomb b; bomb b;
flame f; flame f;
brickdecay bc; brickdecay bc;
@ -18,21 +19,26 @@ union genericlistitem {
static genericlistitem *things=0; static genericlistitem *things=0;
static genericlistitem *free_things; static genericlistitem *free_things;
void allocthings(void) { static int count_used = 0;
static const int num = MAXTHINGS;
void alloc_things(void) {
int i; int i;
const int num = MAXTHINGS;
if (!things) { if (!things) {
things = calloc(sizeof(genericlistitem), num); things = calloc(sizeof(genericlistitem), MAXTHINGS);
} else { } else {
memset(things, 0, sizeof(genericlistitem)*num); memset(things, 0, sizeof(genericlistitem)*MAXTHINGS);
} }
if(!things) nomem("Trying to allocate thing memory"); if (!things) nomem("Trying to allocate thing memory");
for(i=0;i<num-1;++i) {
things[i].next = &things[i+1]; for (i=0; i < MAXTHINGS - 1; ++i) {
things[i].next = &things[i + 1];
} }
things[i].next = 0; things[i].next = 0;
free_things = things; free_things = things;
count_used = 0;
} }
void *_allocentry(size_t size) { void *_allocentry(size_t size) {
@ -41,6 +47,7 @@ void *_allocentry(size_t size) {
if (size > sizeof(genericlistitem)) return 0; if (size > sizeof(genericlistitem)) return 0;
if (free_things) { if (free_things) {
count_used++;
free_things = free_things->next; free_things = free_things->next;
memset(entry, 0, sizeof(genericlistitem)); memset(entry, 0, sizeof(genericlistitem));
} }
@ -51,6 +58,7 @@ void freeentry(void *ptr) {
genericlistitem *entry = ptr; genericlistitem *entry = ptr;
entry->next = free_things; entry->next = free_things;
free_things = entry; free_things = entry;
count_used--;
} }
void list_add_tail(listhead *head, listhead *entry) { void list_add_tail(listhead *head, listhead *entry) {
@ -70,3 +78,13 @@ void list_init_head(listhead *head) {
head->next = head; head->next = head;
head->prev = head; head->prev = head;
} }
void things_list_clear(listhead *head) {
genericlistitem *entry;
while (head->next != head) {
entry = container_of(head->next, typeof(*entry), list);
list_del(head->next);
freeentry(entry);
}
}

3
list.h
View File

@ -15,7 +15,7 @@
*/ */
#define list_entry(ptr, type, member) container_of(ptr, type, member) #define list_entry(ptr, type, member) container_of(ptr, type, member)
void allocthings(void); void alloc_things(void);
void *_allocentry(size_t size); void *_allocentry(size_t size);
#define allocentry(type) (type*) _allocentry(sizeof(type)) #define allocentry(type) (type*) _allocentry(sizeof(type))
void freeentry(void *ptr); void freeentry(void *ptr);
@ -29,6 +29,7 @@ void list_del(listhead *entry);
void list_init_head(listhead *head); void list_init_head(listhead *head);
void things_list_clear(listhead *head); /* listhead member must be the first member */
/** /**
* list_for_each_entry - iterate over list of given type * list_for_each_entry - iterate over list of given type