Remove usage of some temporary vars

This commit is contained in:
Stefan Bühler 2009-08-10 21:14:18 +02:00
parent 9aee736978
commit 36a55544f9
2 changed files with 18 additions and 41 deletions

3
game.c
View File

@ -109,7 +109,8 @@ static void firstzero(void) {
gountil=mycount=mydatacount=0; gountil=mycount=mydatacount=0;
memset(latestactions,0,sizeof(latestactions)); memset(latestactions,0,sizeof(latestactions));
memset(latestcounts,0,sizeof(latestcounts)); memset(latestcounts,0,sizeof(latestcounts));
actionput=actioncount=0; memset(actionblock,0,sizeof(actionblock));
actioncount = 0;
} }
static void initgame() { static void initgame() {

View File

@ -27,8 +27,7 @@ int mydatacount;
int myslot; int myslot;
network_type network = NETWORK_NONE; network_type network = NETWORK_NONE;
static unsigned char hist[ACTIONHIST][MAXNETNODES]; int actioncount;
int actionput,actioncount;
unsigned char actionblock[ACTIONHIST*MAXNETNODES]; unsigned char actionblock[ACTIONHIST*MAXNETNODES];
int myaction; int myaction;
@ -53,7 +52,7 @@ enum network_packet_types {
/* slave -> master packets */ /* slave -> master packets */
PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */ PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */
/* master -> slave packets */ /* master -> slave packets */
PKT_STEP, /* 4 bytes unique #, 4 bytes frame #, 8 bytes ACT_* */ PKT_STEP, /* 4 bytes unique #, 4 bytes frame #, history x MAXNETNODES bytes ACT_* */
PKT_INVALID = 0xff PKT_INVALID = 0xff
}; };
@ -254,56 +253,35 @@ static int isvalidmsg_from_master() {
/* Handling game actions */ /* Handling game actions */
static void addactions(void) { static void addactions(void) {
memmove(hist[actionput],actions,MAXNETNODES); memmove(actionblock+MAXNETNODES, actionblock, (ACTIONHIST-1)*MAXNETNODES);
++actionput; memcpy(actionblock, actions, MAXNETNODES);
if(actionput==ACTIONHIST)
actionput=0;
++actioncount; ++actioncount;
} }
static void buildactions(void) {
unsigned char *p;
int i,j;
p=actionblock;
i=0;
while(i<20) {
if(actioncount-i>0) {
j=actionput-i-1;
if(j<0) j+=ACTIONHIST;
memmove(p,hist[j],MAXNETNODES);
} else {
memset(p,0,MAXNETNODES);
}
p+=MAXNETNODES;
++i;
}
}
static void sendactions(int which) { static void sendactions(int which) {
unsigned char msg[512]; unsigned char msg[512];
msg[0] = PKT_STEP; msg[0] = PKT_STEP;
write_unique(msg+1); write_unique(msg + 1);
writeuint32(msg+5, actioncount); writeuint32(msg + 5, actioncount);
memcpy(msg+9,actionblock,MAXNETNODES*ACTIONHIST); memcpy(msg + 9, actionblock, MAXNETNODES*ACTIONHIST);
putmsg(&netnodes[which].netname,msg,MAXNETNODES*ACTIONHIST+9); putmsg(&netnodes[which].netname, msg, MAXNETNODES*ACTIONHIST + 9);
} }
static void sendmine(int frame) { static void sendmine(int frame) {
unsigned char msg[64]; unsigned char msg[64];
msg[0] = PKT_MYDATA; msg[0] = PKT_MYDATA;
write_unique(msg+1); write_unique(msg + 1);
writeuint32(msg+5, frame); writeuint32(msg + 5, frame);
msg[9]=myaction; msg[9] = myaction;
putmsg(&mastername,msg,10); putmsg(&mastername, msg, 10);
} }
int networktraffic(void) { int networktraffic(void) {
int i; int i;
int length; int length;
int whosent; int whosent;
unsigned char newactions[MAXNETNODES];
long now; long now;
long count; long count;
@ -311,8 +289,8 @@ int networktraffic(void) {
case NETWORK_NONE: case NETWORK_NONE:
return -1; return -1;
case NETWORK_MASTER: case NETWORK_MASTER:
memcpy(newactions,latestactions,MAXNETNODES); memcpy(actions,latestactions,MAXNETNODES);
newactions[0]=myaction; actions[0]=myaction;
now=gtime(); now=gtime();
for(;;) { for(;;) {
if(gtime()-now>15) break; if(gtime()-now>15) break;
@ -327,18 +305,16 @@ int networktraffic(void) {
count=readuint32(mesg+5); count=readuint32(mesg+5);
if(count>latestcounts[whosent]) { if(count>latestcounts[whosent]) {
latestcounts[whosent]=count; latestcounts[whosent]=count;
newactions[whosent]=mesg[9]; actions[whosent]=mesg[9];
} }
} }
if(myaction==ACT_QUIT) { if(myaction==ACT_QUIT) {
for(i=1;i<MAXNETNODES;++i) for(i=1;i<MAXNETNODES;++i)
if(netnodes[i].used) if(netnodes[i].used)
newactions[i]=ACT_QUIT; actions[i]=ACT_QUIT;
} }
memmove(actions,newactions,sizeof(actions));
addactions(); addactions();
buildactions();
for(i=1;i<MAXNETNODES;++i) for(i=1;i<MAXNETNODES;++i)
if(netnodes[i].used) if(netnodes[i].used)
sendactions(i); sendactions(i);