You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bomberclone/src/multiwait.c

450 lines
11 KiB

/* $Id: multiwait.c,v 1.49 2004/09/27 19:20:20 patty21 Exp $
multiwait.c - this manages only the network screen where
everyone have to select it's players and where even the basic chat is inside
*/
#include "bomberclone.h"
#include "network.h"
#include "packets.h"
#include "gfx.h"
#include "chat.h"
#include "menu.h"
#include "single.h"
#include "keyb.h"
extern int UpdateRects_nr;
extern int blitdb_nr;
static int mw_num_readyplayers; // number of players who are ready
static int mw_num_players_x; // number of players in the x
static int mw_num_players_y; // number of players in the y
static int mw_num_players;
_point mw_pos[MAX_PLAYERS+1];
// uncomment to sort by points
// #define SORT_BY_POINTS 1
#define MW_PLAYERSCR_X 310
#define MW_PLAYERSCR_Y 50
#define MW_TITLE_Y 50
/* returns number of active players in a team */
int mw_team_is_used (int t_nr) {
int i,j=0;
for(i=0;i<MAX_PLAYERS;i++)
if ((teams[t_nr].players[i]!=NULL) && (PS_IS_used(teams[t_nr].players[i]->state))) { j++;}
d_printf("Team: %s : %d players\n",teams[t_nr].name,j);
return j;
}
/* Calcs the position of all items on the screen */
int mw_calc_screen() {
int i,j,k=0,n=0,cy;
if (bman.updatestatusbar) {
if (bman.gametype==GT_team) {
/* team mode */
for(i=0;i<MAX_TEAMS;i++){
j=mw_team_is_used(i);
if (j>k) k=j;
if (j>0) n++;
}
mw_num_players_x=2;
mw_num_players_y=floorf(n/2)+n%2;
mw_num_players=n;
for(i=0;i<mw_num_players;i++) {
mw_pos[i].x=gfx.res.x/2/mw_num_players_x+(i%mw_num_players_x)*gfx.res.x/2-MW_PLAYERSCR_X/2;
mw_pos[i].y=ceil(i/2)*(55+k*15);
d_printf("mw_team_Position: x=%d, y=%d\n ",mw_pos[i].x,mw_pos[i].y);
}
cy=mw_num_players_y*(55+k*15)+20;
mw_pos[MAX_PLAYERS].x=8; // Pos of unassigned Players
mw_pos[MAX_PLAYERS].y=cy-15;
}
else
{
mw_num_players=0;
for (i=0;i<MAX_PLAYERS;i++) {
if (PS_IS_used (players[i].state)) mw_num_players++;
}
if (mw_num_players*2>MAX_PLAYERS) {
mw_num_players_x=2;
mw_num_players_y=floorf(mw_num_players/2)+mw_num_players%2;
}
else
{
mw_num_players_x=1;
mw_num_players_y=mw_num_players;
}
for(i=0;i<mw_num_players;i++) {
mw_pos[i].x=gfx.res.x/2/mw_num_players_x+(i%mw_num_players_x)*gfx.res.x/2-MW_PLAYERSCR_X/2;
mw_pos[i].y=floorf(i/mw_num_players_x)*MW_PLAYERSCR_Y;
d_printf("mw_Position: x=%d, y=%d\n ",mw_pos[i].x,mw_pos[i].y);
}
cy=(mw_num_players_y)*MW_PLAYERSCR_Y+15;
}
draw_logo ();
d_printf ("mw_num_players_x : %d, mw_num_players_y : %d mw_players:%d\n", mw_num_players_x, mw_num_players_y,mw_num_players);
chat_show (8, cy, gfx.res.x - 16, gfx.res.y - (8 + cy));
chat.active = 1;
bman.updatestatusbar=0;
return 1;
}
return 0;
}
/* this will load some graphics and so other stuff */
void
mw_init ()
{
draw_logo ();
menu_displaytext ("Please Wait", "Loading GFX Data");
network_loop ();
mw_num_readyplayers = 0;
bman.updatestatusbar = 1;
gfx_blitdraw ();
mw_num_players=-1;
};
/* free all grafics */
void mw_shutdown () {
chat_show (-1, -1, -1, -1);
gfx_blitdraw ();
draw_logo ();
SDL_Flip (gfx.screen);
};
/*
* check if the players are ready
*/
void mw_check_players () {
int i;
mw_num_readyplayers = 0;
if (GT_MP_PTPS) /* only the master can start the game */
return;
for (i = 0; i < MAX_PLAYERS; i++) {
if (players[i].gfx_nr >= 0 && players[i].gfx_nr < MAX_PLAYERS)
mw_num_readyplayers++;
}
}
/*
* draw only the given player
*/
void mw_draw_player (int p_nr, int pos) {
_player *player;
_point p;
char text[255];
SDL_Rect src;
SDL_Rect dest;
player=&players[p_nr];
p.x = mw_pos[pos].x;
p.y = mw_pos[pos].y;
if (PS_IS_used (player->state)) {
/*
* player is used
*/
if (player->gfx_nr >= 0 && player->gfx_nr < gfx.player_gfx_count) {
/* player is used and selected a gfx */
dest.x = p.x;
src.x = 0;
src.y = 0;
if (p_nr==bman.p_nr) {
/* player 1 */
dest.y = p.y;
src.w = dest.w = gfx.players[player->gfx_nr].menu_image->w;
src.h = dest.h = gfx.players[player->gfx_nr].menu_image->h;
gfx_blit (gfx.players[player->gfx_nr].menu_image, &src, gfx.screen, &dest, 10001);
} else if (p_nr==bman.p2_nr) {
/* player 2 */
dest.x=p.x+MW_PLAYERSCR_X-64;
dest.y = p.y;
src.w = dest.w = gfx.players[player->gfx_nr].menu_image->w;
src.h = dest.h = gfx.players[player->gfx_nr].menu_image->h;
gfx_blit (gfx.players[player->gfx_nr].menu_image, &src, gfx.screen, &dest, 10001);
} else {
/* net-player */
dest.x=p.x+35;
dest.y = p.y + 15;
src.w = dest.w = gfx.players[player->gfx_nr].small_image->w;
src.h = dest.h = gfx.players[player->gfx_nr].small_image->h;
gfx_blit (gfx.players[player->gfx_nr].small_image, &src, gfx.screen, &dest, 10001);
}
}
font_gfxdraw (p.x + GFX_IMGSIZE, p.y, player->name, 1, COLOR_yellow, 10002);
sprintf (text,"Victorys:%2d(%d)", player->wins, player->points);
font_gfxdraw (p.x + GFX_IMGSIZE, p.y + font[2].size.y - 4, text, 0, COLOR_white, 10002);
if (debug && PS_IS_used (player->state)) {
sprintf (text, "%s:%s [%d]", player->net.addr.host, player->net.addr.port, player->state);
if (player->net.flags & NETF_firewall)
sprintf (text,"%s FW", text);
font_gfxdraw (p.x + GFX_IMGSIZE, p.y + font[0].size.y + font[2].size.y - 4, text, 0, COLOR_white, 10002);
}
}
else {
/*
* player is free
*/
font_gfxdraw (p.x + GFX_IMGSIZE, p.y, "No Player" , 0, COLOR_gray, 10002);
}
};
/*
* draw all player informations
*/
void mw_draw_all_player () {
int i,j,h;
int n[MAX_PLAYERS];
for(i=0;i<MAX_PLAYERS;i++) n[i]=i;
for(i=0;i<MAX_PLAYERS;i++)
for(j=i;j<MAX_PLAYERS;j++)
#ifdef SORT_BY_POINTS
if (players[i].points>players[j].points) {
#else
if (players[i].wins>players[j].wins) {
#endif
h=n[i];n[i]=n[j];n[j]=h;
}
j=0;
for (i = 0; i < MAX_PLAYERS; i++) {
if (PS_IS_used (players[n[i]].state)) {
mw_draw_player (n[i], j);
j++;
}
}
gfx_blitdraw();
};
/*
* draw only the given team
*/
void mw_draw_team (int t_nr, int pos) {
_player *player;
_team *team;
_point p;
int i,j=0;
char text[255];
SDL_Rect src;
SDL_Rect dest;
team=&teams[t_nr];
p.x = mw_pos[pos].x;
p.y = mw_pos[pos].y;
/* Find if there is any active player in that team */
if (mw_team_is_used(t_nr)>0) {
/*
* team is used
*/
font_gfxdraw (p.x + GFX_IMGSIZE, p.y, team->name, 1, COLOR_yellow, 10002);
sprintf (text,"Victorys:%2d(%d)", team->wins, team->points);
font_gfxdraw (p.x + GFX_IMGSIZE, p.y + font[2].size.y - 4, text, 0, COLOR_white, 10002);
j=1;
for(i=0;i<MAX_PLAYERS;i++) {
if ((team->players[i]!=NULL) && (PS_IS_used(team->players[i]->state))) {
player=team->players[i];
if (player->gfx_nr >= 0 && player->gfx_nr < gfx.player_gfx_count) {
/* player is used and selected a gfx */
dest.x = p.x;
src.x = 0;
src.y = 0;
dest.x=p.x+35+20;
dest.y = p.y + 30+j*15;
src.w = dest.w = gfx.players[player->gfx_nr].small_image->w;
src.h = dest.h = gfx.players[player->gfx_nr].small_image->h;
gfx_blit (gfx.players[player->gfx_nr].small_image, &src, gfx.screen, &dest, 10001);
}
font_gfxdraw (p.x + GFX_IMGSIZE+20, p.y + 30+j*15, team->players[i]->name, 0, COLOR_white, 10002);
j++;
}
}
}
else {
/*
* team is emty
*/
font_gfxdraw (p.x + GFX_IMGSIZE, p.y, "No TEAM" , 0, COLOR_gray, 10002);
}
};
/*
Draw players which are not assigned to any team
*/
void mw_draw_unassigned_players () {
int i,
n=0;
char text[255];
sprintf(text,"Unassigned Players: ");
for (i=0;i<MAX_PLAYERS;i++) {
if ((PS_IS_used (players[i].state))&&(players[i].team_nr<0)) {
sprintf(text,"%s %s ",text,players[i].name);
n++;
}
}
d_printf("mw_draw_unassigned: %d players\n",n);
if (n) font_gfxdraw (mw_pos[MAX_PLAYERS].x,mw_pos[MAX_PLAYERS].y, text, 0, COLOR_gray, 10002);
}
/*
Draw all teams
*/
void mw_draw_all_teams () {
int i,j,h;
int n[MAX_TEAMS];
for(i=0;i<MAX_TEAMS;i++) n[i]=i;
for(i=0;i<MAX_TEAMS;i++)
for(j=i;j<MAX_TEAMS;j++)
#ifdef SORT_BY_POINTS
if (teams[i].points>teams[j].points) {
#else
if (teams[i].wins>teams[j].wins) {
#endif
h=n[i];n[i]=n[j];n[j]=h;
}
j=0;
for (i = 0; i < MAX_TEAMS; i++) {
if (mw_team_is_used(n[i])) {
mw_draw_team (n[i], j);
j++;
}
}
mw_draw_unassigned_players();
gfx_blitdraw();
};
/*
* check if one key is pressed
*/
void mw_keys_loop () {
if (GT_MP_PTPM && mw_num_readyplayers >= 2 && keyb_gamekeys.state[BCK_pause] && !keyb_gamekeys.old[BCK_pause]) {
/* min 2 players are ready for the game and
* the start key has pressed */
bman.state = GS_ready;
bman.updatestatusbar = 1; // force an update
}
if (keyb_gamekeys.state[BCK_fullscreen] && !keyb_gamekeys.old[BCK_fullscreen]) {
/* Switch Fullscreen */
SDL_WM_ToggleFullScreen(gfx.screen);
gfx.fullscreen = !gfx.fullscreen;
bman.updatestatusbar = 1; // force an update
bman.updatestatusbar = 1;
}
if (keyb_gamekeys.state[BCK_esc] && !keyb_gamekeys.old[BCK_esc]) {
/* ESCAPE key was pressed */
bman.state = GS_startup;
}
if ((keyb_gamekeys.state[BCPK_drop] && !keyb_gamekeys.old[BCPK_drop])
|| ((!IS_LPLAYER2) && keyb_gamekeys.state[BCPK_max + BCPK_drop] && !keyb_gamekeys.old[BCPK_max + BCPK_drop])) {
/* player 1 want to select a new gfx */
playermenu_selgfx (bman.p_nr);
net_change_playerid (bman.p_nr, 1);
bman.updatestatusbar = 1;
keyb_loop (NULL); // to reload the current keys
}
if (IS_LPLAYER2 && keyb_gamekeys.state[BCPK_max + BCPK_drop] && !keyb_gamekeys.old[BCPK_max + BCPK_drop]) {
/* player 2 want to select a new gfx */
playermenu_selgfx (bman.p2_nr);
net_change_playerid (bman.p2_nr, 1);
bman.updatestatusbar = 1;
keyb_loop (NULL); // to reload the current keys
}
if (keyb_gamekeys.state[BCK_mapmenu] && !keyb_gamekeys.old[BCK_mapmenu] && GT_MP_PTPM) {
/* mapmenu */
mapmenu ();
bman.updatestatusbar = 1;
keyb_loop (NULL); // to reload the current keys
}
if (keyb_gamekeys.state[BCK_playermenu] && !keyb_gamekeys.old[BCK_playermenu]) {
/* playermenu */
playermenu ();
bman.updatestatusbar = 1;
keyb_loop (NULL); // to reload the current keys
}
};
/* the loop itself */
void wait_for_players () {
SDL_Event event;
mw_init ();
mw_calc_screen();
if (bman.gametype==GT_team) mw_draw_all_teams(); else mw_draw_all_player ();
do {
if (SDL_PollEvent (&event) != 0)
switch (event.type) {
case (SDL_QUIT):
bman.state = GS_quit;
break;
}
/*
* check some little things and do the network stuff
*/
mw_check_players ();
network_loop ();
/*
* the drawing stuff
*/
if (mw_calc_screen()) {
if (bman.gametype==GT_team) mw_draw_all_teams(); else mw_draw_all_player ();
}
gfx_blitdraw ();
/*
* input handling
*/
keyb_loop (&event);
mw_keys_loop ();
chat_loop (&event);
chat.active = 1;
s_calctimesync ();
} while (bman.state == GS_wait && bman.sock != -1);
mw_shutdown ();
};