Display failure messages

This commit is contained in:
Stefan Bühler 2009-08-10 00:02:13 +02:00
parent 94df5eb6dd
commit 9aee736978
10 changed files with 173 additions and 155 deletions

View File

@ -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 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 sound.o: sound.c sound.h

View File

@ -204,7 +204,7 @@ typedef struct bonustile {
#define MAXTHINGS 500 #define MAXTHINGS 500
#define MAXSETS 8 #define MAXSETS 8
#define MAXSPRITES 128 #define MAXSPRITES 256
#define MAXDAMAGES 512 #define MAXDAMAGES 512
#define MAXBOMBSDETONATED 32 #define MAXBOMBSDETONATED 32

24
draw.c
View File

@ -446,3 +446,27 @@ void loadgfx() {
free(gs); free(gs);
bigscrprintf("Done loading graphics\n"); 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;
}
}
}

2
draw.h
View File

@ -21,6 +21,8 @@ int screentoarrayy(int y);
int arraytoscreenx(int x); int arraytoscreenx(int x);
int arraytoscreeny(int y); int arraytoscreeny(int y);
void failure(char *str,...);
extern int bigfontxsize,bigfontysize,bigfontyspace; extern int bigfontxsize,bigfontysize,bigfontyspace;
/* On screen array variables */ /* On screen array variables */

63
game.c
View File

@ -23,8 +23,6 @@ static int detonatetake=0;
static list activeflames; static list activeflames;
static list activeplayers; static list activeplayers;
#define REGISTERLEN (1+4+4+4+16+1)
figure walking[MAXSETS][NUMWALKFRAMES]; figure walking[MAXSETS][NUMWALKFRAMES];
solid background,backgroundoriginal; solid background,backgroundoriginal;
@ -32,7 +30,6 @@ solid background,backgroundoriginal;
unsigned char field[32][32]; unsigned char field[32][32];
void *info[32][32]; void *info[32][32];
int gamemode = 0;
char exitflag = 0; char exitflag = 0;
static int framecount = 0; static int framecount = 0;
@ -53,7 +50,7 @@ TILE_NONE,160
static GameOptions gameoptions; static GameOptions gameoptions;
static const unsigned char playerpositions[] = { /* 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,
4,14,0, 4,14,0,
@ -94,7 +91,7 @@ static void initplayers(void) {
const unsigned char *p; const unsigned char *p;
int c,x,y; int c,x,y;
if(!network) { if(NETWORK_NONE == network) {
initplayer(2,0,0,-1); initplayer(2,0,0,-1);
return; return;
} }
@ -173,33 +170,6 @@ static void initgame() {
copyup(); 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) { static void addflame(player *owner,int px,int py) {
flame *fl,*fl2; flame *fl,*fl2;
@ -863,7 +833,7 @@ static int getaction(void) {
return what; return what;
} }
int iterate(void) { static int iterate(void) {
int i; int i;
static int deathcount=0; static int deathcount=0;
@ -875,7 +845,7 @@ int iterate(void) {
gfxunlock(); gfxunlock();
myaction=getaction(); myaction=getaction();
if(!network && myaction==ACT_QUIT) return CODE_QUIT; if(NETWORK_NONE == network && myaction==ACT_QUIT) return CODE_QUIT;
i=networktraffic(); i=networktraffic();
if(i<0) if(i<0)
gountil=mycount+1; gountil=mycount+1;
@ -884,7 +854,7 @@ int iterate(void) {
while(mycount<gountil) { while(mycount<gountil) {
++mycount; ++mycount;
if(network) { if(NETWORK_NONE != network) {
i=gountil-mycount; i=gountil-mycount;
if(i>=ACTIONHIST) // too far behind if(i>=ACTIONHIST) // too far behind
return CODE_QUIT; return CODE_QUIT;
@ -928,7 +898,7 @@ int iterate(void) {
deadplayers++; deadplayers++;
pl=pl->next; pl=pl->next;
} }
if (deadplayers > 0 && (!i || (network && i==1))) { if (deadplayers > 0 && (!i || (NETWORK_NONE != network && i==1))) {
++deathcount; ++deathcount;
if(deathcount==25) if(deathcount==25)
return CODE_ALLDEAD; return CODE_ALLDEAD;
@ -941,3 +911,24 @@ int iterate(void) {
void set_game_options(GameOptions *options) { void set_game_options(GameOptions *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);
}

1
game.h
View File

@ -21,7 +21,6 @@ void run_network_game(void);
void set_game_options(GameOptions *options); void set_game_options(GameOptions *options);
extern char playername[16]; extern char playername[16];
extern int gamemode;
extern solid background,backgroundoriginal; extern solid background,backgroundoriginal;

203
menu.c
View File

@ -118,7 +118,8 @@ static int domenu(menuname whichmenu, int (*pause)(void)) {
} }
} }
} }
return 0; menudelta = 0;
return menuexit;
} }
static void menustart() { static void menustart() {
@ -172,84 +173,13 @@ static void addexit(char *item,...) {
/* game menues */ /* 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<MAXNETNODES;++i)
{
if(!netnodes[i].used) continue;
memmove(name,netnodes[i].name,16);
name[16]=0;
sprintf(temp," %d %s",i+1,name);
drawbigstring(JX,JY+(i+1)*bigfontyspace,temp);
}
}
static int tryjoin(int which) {
int res;
if (0 == (res = send_join(&gamelistentries[which].netname, playername)))
return 0;
for ( ;; ) {
switch (res) {
case 1: return 0;
case 2:
drawjoinscreen();
copyup();
break;
case 3: return 1;
default: break;
}
scaninput();
while(anydown()) {
switch(takedown()) {
case 0x1b:
send_quit();
return 0;
}
}
res=scaninvite(200);
}
return 0;
}
static void main_menu(void) {
int sel;
while(!exitflag) {
menustart();
additem("BOMBER MAIN MENU");
additem("EXIT GAME");
additem("START SINGLE PLAYER GAME");
additem("OPTIONS");
// additem("REMAP MOVEMENT KEYS");
additem("START NETWORK GAME");
additem("JOIN NETWORK GAME");
sel=domenu(MENU_MAIN, NULL);
if(!sel) {exitflag=1;break;}
if(sel==1) {gamemode=1;break;}
if(sel==2) {gamemode=2;break;}
if(sel==3) {gamemode=3;break;}
if(sel==4) {gamemode=4;break;}
}
}
char *densities[]={"PACKED","HIGH","MEDIUM","LOW"}; char *densities[]={"PACKED","HIGH","MEDIUM","LOW"};
char *generosities[]={"LOW","MEDIUM","HIGH","RIDICULOUS"}; char *generosities[]={"LOW","MEDIUM","HIGH","RIDICULOUS"};
static void config_menu(void) { static void config_menu(void) {
int sel; int sel;
for(;;) { while (!exitflag) {
menustart(); menustart();
additem("GAME OPTIONS"); additem("GAME OPTIONS");
additem("RETURN TO MAIN MENU"); additem("RETURN TO MAIN MENU");
@ -258,30 +188,29 @@ static void config_menu(void) {
additem("INITIAL FLAME LENGTH: %d",configopts.flames+1); additem("INITIAL FLAME LENGTH: %d",configopts.flames+1);
additem("INITIAL NUMBER OF BOMBS: %d",configopts.bombs+1); additem("INITIAL NUMBER OF BOMBS: %d",configopts.bombs+1);
sel=domenu(MENU_CONFIG, NULL); sel=domenu(MENU_CONFIG, NULL);
if(!sel) {gamemode=0;break;} switch (sel) {
if(sel==1) { case 0:
return;
case 1:
configopts.density+=menudelta; configopts.density+=menudelta;
configopts.density&=3; configopts.density&=3;
} break;
if(sel==2) { case 2:
configopts.generosity+=menudelta; configopts.generosity+=menudelta;
configopts.generosity&=3; configopts.generosity&=3;
} break;
if(sel==3) { case 3:
configopts.flames+=menudelta; configopts.flames+=menudelta;
configopts.flames&=7; configopts.flames&=7;
} break;
if(sel==4) { case 4:
configopts.bombs+=menudelta; configopts.bombs+=menudelta;
configopts.bombs&=7; configopts.bombs&=7;
break;
} }
} }
} }
static void failure(char *str,...) {
gamemode=0;
}
static void draw_host_game(void) { static void draw_host_game(void) {
int i; int i;
char *name; char *name;
@ -319,13 +248,12 @@ static void host_game(void) {
case 0x1b: case 0x1b:
unregistergame(); unregistergame();
cancel_network_game(); cancel_network_game();
gamemode=0;
return; return;
case ' ': case ' ':
case 13: case 13:
unregistergame(); unregistergame();
if (begin_network_game()) { if (begin_network_game()) {
gamemode=5; run_network_game();
return; return;
} }
send_invites(); send_invites();
@ -338,8 +266,58 @@ static void host_game(void) {
send_invites(); send_invites();
draw_host_game(); draw_host_game();
} }
}
gamemode=0; 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 < MAXNETNODES; ++i) {
if(!netnodes[i].used) continue;
memmove(name,netnodes[i].name,16);
name[16]=0;
sprintf(temp," %d %s",i+1,name);
drawbigstring(JX,JY+(i+1)*bigfontyspace,temp);
}
}
static int tryjoin(int which) {
int res;
if (0 == (res = send_join(&gamelistentries[which].netname, playername))) {
return 0;
}
while (!exitflag) {
switch (res) {
case 1:
failure("Game closed");
return 0;
case 2:
drawjoinscreen();
copyup();
break;
case 3: return 1;
default: break;
}
scaninput();
while(anydown()) {
switch(takedown()) {
case 0x1b:
send_quit();
return 0;
}
}
res=scaninvite(200);
}
return 0;
} }
static int join_game_pause(void) { static int join_game_pause(void) {
@ -353,7 +331,6 @@ static void join_game(void) {
int sel = -1; int sel = -1;
if (!searchgames()) { if (!searchgames()) {
gamemode = 0;
return; return;
} }
@ -372,33 +349,49 @@ static void join_game(void) {
addexit("EXIT"); addexit("EXIT");
} }
sel = domenu(MENU_JOIN, join_game_pause); sel = domenu(MENU_JOIN, join_game_pause);
if (menudelta < 0) sel = -1;
} }
stop_search(); stop_search();
if(menuexit == sel || !gamelistsize) { if(menuexit == sel || !gamelistsize) {
gamemode=0;
return; return;
} }
if(!tryjoin(sel)) { if(!tryjoin(sel)) {
gamemode=0;
return; return;
} }
network = NETWORK_SLAVE; run_network_game();
gamemode=5;
} }
void (*modefunctions[])()= {
main_menu,
run_single_player,
config_menu,
host_game,
join_game,
run_network_game,
};
void mainloop(void) { void mainloop(void) {
int sel;
exitflag = 0; exitflag = 0;
while(!exitflag) while(!exitflag) {
modefunctions[gamemode](); menustart();
additem("BOMBER MAIN MENU");
additem("EXIT GAME");
additem("START SINGLE PLAYER GAME");
additem("OPTIONS");
// additem("REMAP MOVEMENT KEYS");
additem("START NETWORK GAME");
additem("JOIN NETWORK GAME");
sel=domenu(MENU_MAIN, NULL);
if (menudelta < 0) sel = -1;
switch (sel) {
case 0:
return;
case 1:
run_single_player();
break;
case 2:
config_menu();
break;
case 3:
host_game();
break;
case 4:
join_game();
break;
}
}
} }

2
menu.h
View File

@ -3,8 +3,6 @@
void mainloop(void); void mainloop(void);
int iterate(void); /* bomber.c */
extern struct GameOptions configopts; extern struct GameOptions configopts;
#endif #endif

View File

@ -5,13 +5,14 @@
#include "menu.h" #include "menu.h"
#include "network.h" #include "network.h"
#include "utils.h" #include "utils.h"
#include "draw.h"
#define MAXMSG 4096 #define MAXMSG 4096
int udpsocket; int udpsocket;
const unsigned char gameversion[4]={0xda,0x01,0x00,0x05}; const unsigned char gameversion[4]={0xda,0x01,0x00,0x05};
struct netnode netnodes[64]; struct netnode netnodes[MAXNETNODES];
static int informsize; static int informsize;
static unsigned char regpacket[64]; static unsigned char regpacket[64];
@ -63,6 +64,13 @@ enum reject_reason {
/* TODO: password? */ /* TODO: password? */
}; };
#define _REJECT_LAST REJECT_VERSION
const char *reject_reason_str[] = {
"Server full",
"Version mismatch"
};
/* all bytes stored MSB first */ /* all bytes stored MSB first */
/* /*
@ -671,6 +679,7 @@ int scaninvite(int msec) {
} }
if (*mesg == PKT_BEGIN) { if (*mesg == PKT_BEGIN) {
network = NETWORK_SLAVE;
return 3; return 3;
} else { } else {
return 2; return 2;
@ -708,12 +717,17 @@ int send_join(struct sockaddr_in *netname, char playername[16]) {
if (!read_inform(&buf,&size)) return 0; if (!read_inform(&buf,&size)) return 0;
return 2; return 2;
case PKT_REJECT: case PKT_REJECT:
/* TODO: print reject message */ if (size < 10 || mesg[9] > _REJECT_LAST) {
failure("Couldn't connect");
} else {
failure("Couldn't connect: %s", reject_reason_str[mesg[9]]);
}
return 0; return 0;
default: default:
break; break;
} }
} }
failure("Could not connect - Timeout");
return 0; return 0;
} }

View File

@ -5,13 +5,15 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#define MAXNETNODES 8
struct netnode { struct netnode {
struct sockaddr_in netname; struct sockaddr_in netname;
char name[16]; char name[16];
char used; char used;
}; };
extern struct netnode netnodes[64]; extern struct netnode netnodes[MAXNETNODES];
void getsocket(void); void getsocket(void);
void freesocket(void); void freesocket(void);
@ -27,11 +29,6 @@ int begin_network_game();
void send_invites(); void send_invites();
void cancel_network_game(); void cancel_network_game();
#define MAXNETNODES 10
extern struct netnode netnodes[64];
extern int udpsocket;
extern const unsigned char gameversion[4]; extern const unsigned char gameversion[4];
typedef enum { NETWORK_NONE = 0, NETWORK_MASTER, NETWORK_SLAVE } network_type; typedef enum { NETWORK_NONE = 0, NETWORK_MASTER, NETWORK_SLAVE } network_type;