From 9aee736978d6c283e9a9b83fdbf5789a40503e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 00:02:13 +0200 Subject: [PATCH 1/6] Display failure messages --- Makefile | 2 +- bomber.h | 2 +- draw.c | 24 +++++++ draw.h | 2 + game.c | 63 +++++++---------- game.h | 1 - menu.c | 205 ++++++++++++++++++++++++++---------------------------- menu.h | 2 - network.c | 18 ++++- network.h | 9 +-- 10 files changed, 173 insertions(+), 155 deletions(-) diff --git a/Makefile b/Makefile index 61d1ee1..6bd384f 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ list.o: list.c bomber.h list.h utils.h menu.o: menu.c announce.h bomber.h draw.h game.h gfx.h list.h menu.h network.h sound.h utils.h -network.o: network.c announce.h bomber.h game.h menu.h network.h utils.h +network.o: network.c announce.h bomber.h draw.h game.h menu.h network.h utils.h sound.o: sound.c sound.h diff --git a/bomber.h b/bomber.h index b3cb963..8d1ccb4 100644 --- a/bomber.h +++ b/bomber.h @@ -204,7 +204,7 @@ typedef struct bonustile { #define MAXTHINGS 500 #define MAXSETS 8 -#define MAXSPRITES 128 +#define MAXSPRITES 256 #define MAXDAMAGES 512 #define MAXBOMBSDETONATED 32 diff --git a/draw.c b/draw.c index 5963e77..c6439e1 100644 --- a/draw.c +++ b/draw.c @@ -446,3 +446,27 @@ void loadgfx() { free(gs); bigscrprintf("Done loading graphics\n"); } + +void failure(char *str,...) { + char output[256]; + va_list ap; + int len; + long now; + + va_start(ap, str); + + len = vsnprintf(output, sizeof(output), str, ap); + if (len >= 256) len = 255; /* truncated string */ + clear(); + drawbigstring((IXSIZE - len*bigfontxsize) / 2, (IYSIZE-bigfontysize) / 2, output); + copyup(); + + now = longtime(); + while (!exitflag && longtime()-now < 3) { + scaninput(); + if (anydown()) { + takedown(); + return; + } + } +} diff --git a/draw.h b/draw.h index ecc8e93..89eb65e 100644 --- a/draw.h +++ b/draw.h @@ -21,6 +21,8 @@ int screentoarrayy(int y); int arraytoscreenx(int x); int arraytoscreeny(int y); +void failure(char *str,...); + extern int bigfontxsize,bigfontysize,bigfontyspace; /* On screen array variables */ diff --git a/game.c b/game.c index 18e4115..e331c4c 100644 --- a/game.c +++ b/game.c @@ -23,8 +23,6 @@ static int detonatetake=0; static list activeflames; static list activeplayers; -#define REGISTERLEN (1+4+4+4+16+1) - figure walking[MAXSETS][NUMWALKFRAMES]; solid background,backgroundoriginal; @@ -32,7 +30,6 @@ solid background,backgroundoriginal; unsigned char field[32][32]; void *info[32][32]; -int gamemode = 0; char exitflag = 0; static int framecount = 0; @@ -53,7 +50,7 @@ TILE_NONE,160 static GameOptions gameoptions; -static const unsigned char playerpositions[] = { /* color, x, y */ +static const unsigned char playerpositions[MAXNETNODES*3] = { /* color, x, y */ 2,0,0, 3,14,10, 4,14,0, @@ -94,7 +91,7 @@ static void initplayers(void) { const unsigned char *p; int c,x,y; - if(!network) { + if(NETWORK_NONE == network) { initplayer(2,0,0,-1); return; } @@ -173,33 +170,6 @@ static void initgame() { copyup(); } - -void run_single_player(void) { - int code; - network=0; - - firstzero(); - do { - initgame(); - while(!(code=iterate())) ++framecount; - } while (code != CODE_QUIT); - - gamemode=0; -} - -void run_network_game(void) { - int code; - - firstzero(); - do { - initgame(); - while(!(code=iterate())) ++framecount; - } while (code != CODE_QUIT); - - network = 0; - gamemode = 0; -} - static void addflame(player *owner,int px,int py) { flame *fl,*fl2; @@ -863,7 +833,7 @@ static int getaction(void) { return what; } -int iterate(void) { +static int iterate(void) { int i; static int deathcount=0; @@ -875,7 +845,7 @@ int iterate(void) { gfxunlock(); myaction=getaction(); - if(!network && myaction==ACT_QUIT) return CODE_QUIT; + if(NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; i=networktraffic(); if(i<0) gountil=mycount+1; @@ -884,7 +854,7 @@ int iterate(void) { while(mycount=ACTIONHIST) // too far behind return CODE_QUIT; @@ -928,7 +898,7 @@ int iterate(void) { deadplayers++; pl=pl->next; } - if (deadplayers > 0 && (!i || (network && i==1))) { + if (deadplayers > 0 && (!i || (NETWORK_NONE != network && i==1))) { ++deathcount; if(deathcount==25) return CODE_ALLDEAD; @@ -941,3 +911,24 @@ int iterate(void) { void set_game_options(GameOptions *options) { gameoptions = *options; } + +void run_single_player(void) { + int code; + network = NETWORK_NONE; + + firstzero(); + do { + initgame(); + while(!(code=iterate()) && !exitflag) ++framecount; + } while (code != CODE_QUIT && !exitflag); +} + +void run_network_game(void) { + int code; + + firstzero(); + do { + initgame(); + while (!(code=iterate()) && !exitflag) ++framecount; + } while (code != CODE_QUIT && !exitflag); +} diff --git a/game.h b/game.h index f66409d..084feb5 100644 --- a/game.h +++ b/game.h @@ -21,7 +21,6 @@ void run_network_game(void); void set_game_options(GameOptions *options); extern char playername[16]; -extern int gamemode; extern solid background,backgroundoriginal; diff --git a/menu.c b/menu.c index 2675245..4bb5bcc 100644 --- a/menu.c +++ b/menu.c @@ -118,7 +118,8 @@ static int domenu(menuname whichmenu, int (*pause)(void)) { } } } - return 0; + menudelta = 0; + return menuexit; } static void menustart() { @@ -172,84 +173,13 @@ static void addexit(char *item,...) { /* game menues */ -static void drawjoinscreen(void) { -int i; -char name[17]; -char temp[64]; - -#define JX (IXSIZE/3) -#define JY (IYSIZE/4) - - clear(); - centerbig(20,"JOIN NETWORK GAME"); - drawbigstring(JX,JY,"SLOT NAME"); - for(i=0;i _REJECT_LAST) { + failure("Couldn't connect"); + } else { + failure("Couldn't connect: %s", reject_reason_str[mesg[9]]); + } return 0; default: break; } } + failure("Could not connect - Timeout"); return 0; } diff --git a/network.h b/network.h index 3fa5628..7ac98dd 100644 --- a/network.h +++ b/network.h @@ -5,13 +5,15 @@ #include #include +#define MAXNETNODES 8 + struct netnode { struct sockaddr_in netname; char name[16]; char used; }; -extern struct netnode netnodes[64]; +extern struct netnode netnodes[MAXNETNODES]; void getsocket(void); void freesocket(void); @@ -27,11 +29,6 @@ int begin_network_game(); void send_invites(); void cancel_network_game(); -#define MAXNETNODES 10 - -extern struct netnode netnodes[64]; - -extern int udpsocket; extern const unsigned char gameversion[4]; typedef enum { NETWORK_NONE = 0, NETWORK_MASTER, NETWORK_SLAVE } network_type; From 36a55544f958ecd35906663d0e54835a47d79947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 21:14:18 +0200 Subject: [PATCH 2/6] Remove usage of some temporary vars --- game.c | 3 ++- network.c | 56 ++++++++++++++++--------------------------------------- 2 files changed, 18 insertions(+), 41 deletions(-) diff --git a/game.c b/game.c index e331c4c..22702a2 100644 --- a/game.c +++ b/game.c @@ -109,7 +109,8 @@ static void firstzero(void) { gountil=mycount=mydatacount=0; memset(latestactions,0,sizeof(latestactions)); memset(latestcounts,0,sizeof(latestcounts)); - actionput=actioncount=0; + memset(actionblock,0,sizeof(actionblock)); + actioncount = 0; } static void initgame() { diff --git a/network.c b/network.c index c458599..88196a2 100644 --- a/network.c +++ b/network.c @@ -27,8 +27,7 @@ int mydatacount; int myslot; network_type network = NETWORK_NONE; -static unsigned char hist[ACTIONHIST][MAXNETNODES]; -int actionput,actioncount; +int actioncount; unsigned char actionblock[ACTIONHIST*MAXNETNODES]; int myaction; @@ -53,7 +52,7 @@ enum network_packet_types { /* slave -> master packets */ PKT_MYDATA, /* 4 bytes unique #,4 bytes frame #, 1 byte data */ /* 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 }; @@ -254,56 +253,35 @@ static int isvalidmsg_from_master() { /* Handling game actions */ static void addactions(void) { - memmove(hist[actionput],actions,MAXNETNODES); - ++actionput; - if(actionput==ACTIONHIST) - actionput=0; + memmove(actionblock+MAXNETNODES, actionblock, (ACTIONHIST-1)*MAXNETNODES); + memcpy(actionblock, actions, MAXNETNODES); ++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) { unsigned char msg[512]; msg[0] = PKT_STEP; - write_unique(msg+1); - writeuint32(msg+5, actioncount); - memcpy(msg+9,actionblock,MAXNETNODES*ACTIONHIST); - putmsg(&netnodes[which].netname,msg,MAXNETNODES*ACTIONHIST+9); + write_unique(msg + 1); + writeuint32(msg + 5, actioncount); + memcpy(msg + 9, actionblock, MAXNETNODES*ACTIONHIST); + putmsg(&netnodes[which].netname, msg, MAXNETNODES*ACTIONHIST + 9); } static void sendmine(int frame) { unsigned char msg[64]; msg[0] = PKT_MYDATA; - write_unique(msg+1); - writeuint32(msg+5, frame); - msg[9]=myaction; - putmsg(&mastername,msg,10); + write_unique(msg + 1); + writeuint32(msg + 5, frame); + msg[9] = myaction; + putmsg(&mastername, msg, 10); } int networktraffic(void) { int i; int length; int whosent; - unsigned char newactions[MAXNETNODES]; long now; long count; @@ -311,8 +289,8 @@ int networktraffic(void) { case NETWORK_NONE: return -1; case NETWORK_MASTER: - memcpy(newactions,latestactions,MAXNETNODES); - newactions[0]=myaction; + memcpy(actions,latestactions,MAXNETNODES); + actions[0]=myaction; now=gtime(); for(;;) { if(gtime()-now>15) break; @@ -327,18 +305,16 @@ int networktraffic(void) { count=readuint32(mesg+5); if(count>latestcounts[whosent]) { latestcounts[whosent]=count; - newactions[whosent]=mesg[9]; + actions[whosent]=mesg[9]; } } if(myaction==ACT_QUIT) { for(i=1;i Date: Mon, 10 Aug 2009 21:44:41 +0200 Subject: [PATCH 3/6] More fixes for network code (slaves don't block anymore) --- game.c | 25 +++++++------ network.c | 106 +++++++++++++++++++++++++++--------------------------- 2 files changed, 68 insertions(+), 63 deletions(-) diff --git a/game.c b/game.c index 22702a2..ac0cbd1 100644 --- a/game.c +++ b/game.c @@ -836,7 +836,7 @@ static int getaction(void) { static int iterate(void) { int i; - static int deathcount=0; + static int deathcount = 0; mypause(); scaninput(); @@ -845,9 +845,9 @@ static int iterate(void) { clearspritelist(); gfxunlock(); - myaction=getaction(); - if(NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; - i=networktraffic(); + myaction = getaction(); + if (NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; + i = networktraffic(); if(i<0) gountil=mycount+1; else @@ -855,12 +855,12 @@ static int iterate(void) { while(mycount=ACTIONHIST) // too far behind - return CODE_QUIT; - memcpy(actions,actionblock+i*MAXNETNODES,MAXNETNODES); - if(actions[myslot]==ACT_QUIT) return CODE_QUIT; + if (NETWORK_NONE != network) { + i = gountil - mycount; + if(i >= ACTIONHIST) // too far behind + goto leave_game; + memcpy(actions, actionblock+i*MAXNETNODES, MAXNETNODES); + if (actions[myslot] == ACT_QUIT) return CODE_QUIT; } processbombs(); dodetonations(); @@ -907,6 +907,11 @@ static int iterate(void) { deathcount=0; } return CODE_CONT; + +leave_game: /* client disconnect/timeout: send ACT_QUIT to master */ + myaction = ACT_QUIT; + networktraffic(); + return CODE_QUIT; } void set_game_options(GameOptions *options) { diff --git a/network.c b/network.c index 88196a2..6248d2c 100644 --- a/network.c +++ b/network.c @@ -291,66 +291,66 @@ int networktraffic(void) { case NETWORK_MASTER: memcpy(actions,latestactions,MAXNETNODES); actions[0]=myaction; - now=gtime(); - for(;;) { - if(gtime()-now>15) break; - length=getmsg(5); - if(length>0 && *mesg!=PKT_MYDATA) fprintf(stderr, "Strange packet %d\n", (int) *mesg); -// check for unexpected old packets... -// for example JOIN on frame 0, respond with BEGIN if player already in game -// respond with uninvite INVITE on JOIN from others - if(length<10) continue; - whosent = isvalidmsg_from_slave(); - if(whosent<=0) continue; - count=readuint32(mesg+5); - if(count>latestcounts[whosent]) { - latestcounts[whosent]=count; - actions[whosent]=mesg[9]; + if (myaction == ACT_QUIT) { + for (i = 1; i < MAXNETNODES; ++i) { + if (netnodes[i].used) + actions[i]=ACT_QUIT; + } + } else { + now=gtime(); + for(;;) { + if(gtime()-now>15) break; + length=getmsg(5); + if(length>0 && *mesg!=PKT_MYDATA) fprintf(stderr, "Strange packet %d\n", (int) *mesg); + /* check for unexpected old packets... + * for example JOIN on frame 0, respond with BEGIN if player already in game + * respond with uninvite INVITE on JOIN from others + */ + if (length < 10) continue; + whosent = isvalidmsg_from_slave(); + if (whosent <= 0) continue; + count = readuint32(mesg+5); + if (count > latestcounts[whosent]) { + latestcounts[whosent] = count; + actions[whosent] = mesg[9]; + } } } - if(myaction==ACT_QUIT) { - for(i=1;i=1) - { - lastsent=gtime(); - sendmine(mydatacount); - } - */ - if(latest>=0 && gtime()-now>3) break; - length=getmsg(3); - if(length==MAXNETNODES*ACTIONHIST+9 && - sender.sin_addr.s_addr==mastername.sin_addr.s_addr && - sender.sin_port==mastername.sin_port) { - i=readuint32(mesg+5); - if(i= 0 && cur - now > 3) break; + if (exitflag || cur - now > 30) break; + + length = getmsg(count >= 0 ? 3 : 20); + + if (MAXNETNODES*ACTIONHIST+9 != length) continue; + if (!isvalidmsg_from_master()) continue; + + i = readuint32(mesg+5); + if (i < actioncount) continue; + count = actioncount = i; + memcpy(actionblock,mesg+9,MAXNETNODES*ACTIONHIST); } + return actioncount; } return -1; } From 8b614a87280a5044d7b42bf0a51ad3eb56318e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 22:00:27 +0200 Subject: [PATCH 4/6] Restrict players to 10 bombs, use enum for tile types --- bomber.h | 25 ++++++++++++++++++------- game.c | 10 +++++----- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bomber.h b/bomber.h index 8d1ccb4..b526e67 100644 --- a/bomber.h +++ b/bomber.h @@ -166,13 +166,24 @@ typedef struct bonustile { int type; } bonustile; -#define TILE_NONE -1 -#define TILE_BOMB 5 -#define TILE_FLAME 2 -#define TILE_GOLDFLAME 7 -#define TILE_CONTROL 9 -#define TILE_SKATES 4 -#define TILE_TURTLE 14 +enum tile_types { + TILE_NONE = -1, + TILE_DISEASE = 0, + TILE_KICK = 1, + TILE_FLAME = 2, + TILE_PUNCH = 3, + TILE_SKATES = 4, + TILE_BOMB = 5, + TILE_SPOOGE = 6, + TILE_GOLDFLAME = 7, + TILE_BAD_DISEASE = 8, + TILE_TRIGGER = 9, + TILE_RANDOM = 10, + TILE_JELLY = 11, + TILE_GRAB = 12, + TILE_RANDOM2 = 13, + TILE_TURTLE = 14 +}; #define ACT_INVALID 0x88 diff --git a/game.c b/game.c index ac0cbd1..c6bbb07 100644 --- a/game.c +++ b/game.c @@ -41,7 +41,7 @@ static int bonustotal; static const int bonuschances[]= { TILE_BOMB,20, TILE_FLAME,20, -TILE_CONTROL,2, +TILE_TRIGGER,2, TILE_GOLDFLAME,2, TILE_SKATES,20, TILE_TURTLE,5, @@ -685,10 +685,10 @@ static void applybonus(player *pl,bonustile *bonus) { maxflame=arraynumx>arraynumy ? arraynumx : arraynumy; type=bonus->type; deletebonus(bonus); - switch(type) - { + switch(type) { case TILE_BOMB: - ++(pl->bombsavailable); + if (pl->bombsavailable < 9) + ++(pl->bombsavailable); break; case TILE_FLAME: if(pl->flamelengthflamelength=maxflame; break; - case TILE_CONTROL: + case TILE_TRIGGER: pl->flags|=FLG_CONTROL; break; case TILE_SKATES: From 1b3b3b3eb8626be88f0f176d216a47b677e1fee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 22:06:54 +0200 Subject: [PATCH 5/6] Remove global var "gountil" --- game.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/game.c b/game.c index c6bbb07..0a28b5a 100644 --- a/game.c +++ b/game.c @@ -35,7 +35,7 @@ static int framecount = 0; char playername[16]; -static int gountil, mycount; +static int mycount; static int bonustotal; static const int bonuschances[]= { @@ -106,7 +106,7 @@ static void initplayers(void) { } static void firstzero(void) { - gountil=mycount=mydatacount=0; + mycount = mydatacount = 0; memset(latestactions,0,sizeof(latestactions)); memset(latestcounts,0,sizeof(latestcounts)); memset(actionblock,0,sizeof(actionblock)); @@ -836,6 +836,7 @@ static int getaction(void) { static int iterate(void) { int i; + int gountil; /* destination tick */ static int deathcount = 0; mypause(); @@ -847,13 +848,13 @@ static int iterate(void) { myaction = getaction(); if (NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT; - i = networktraffic(); - if(i<0) - gountil=mycount+1; - else - gountil=i; + if (NETWORK_NONE == network) { + gountil = mycount + 1; /* single step in singly player mode */ + } else { + gountil = networktraffic(); /* as master single step, as slave as many as we can do */ + } - while(mycountnext) { if(!(pl->flags & FLG_DEAD)) ++i; else deadplayers++; - pl=pl->next; } if (deadplayers > 0 && (!i || (NETWORK_NONE != network && i==1))) { ++deathcount; From f00652cac76bd21598a3423c62b04b2cf35486cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Mon, 10 Aug 2009 22:42:54 +0200 Subject: [PATCH 6/6] Network protocl 1.0.6 --- network.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network.c b/network.c index 6248d2c..894d4bf 100644 --- a/network.c +++ b/network.c @@ -10,7 +10,7 @@ #define MAXMSG 4096 int udpsocket; -const unsigned char gameversion[4]={0xda,0x01,0x00,0x05}; +const unsigned char gameversion[4]={0xda,0x01,0x00,0x06}; struct netnode netnodes[MAXNETNODES];