1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/* -*- C++ -*- */
/*
GAV - Gpl Arcade Volleyball
Copyright (C) 2002
GAV team (http://sourceforge.net/projects/gav/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef NONET
#include "NetServer.h"
#include "Player.h"
using namespace std;
int NetServer::StartServer(int port) {
/* open the socket */
mySock = SDLNet_UDP_Open((Uint16)port);
if(!mySock) {
fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError());
return -1;
}
return 0;
}
int NetServer::ComputePlayerID(char id) {
char tm ,pl;
tm = (id & NET_TEAM_LEFT)?0:1;
pl = id & 0x3F;
return (2 * pl + tm);
}
int NetServer::WaitClients(int nclients) {
IPaddress * ipa;
//char nleft = 0;
//char nright = 0;
char * id;
int i;
bool inserted = false;
char pl;
_nclients = nclients;
while ( !inserted ) { // (nright + nleft) != nclients) {
inserted = false;
if (SDLNet_UDP_Recv(mySock, packetRegister) != 0) {
ipa = (IPaddress*)malloc(sizeof(IPaddress));
memcpy(ipa, &(packetRegister->address), sizeof(IPaddress));
id = &(((net_register_t*)(packetRegister->data))->id);
if (*id & NET_TEAM_LEFT) {
for (i = 0; (i < configuration.left_nplayers) && !inserted; i++)
if ((configuration.left_players[i] == PLAYER_COMPUTER) &&
!_players[2*i]) {
inserted = true;
pl = (char)i;
}
if (inserted) {
*id |= pl;
//nleft++;
} else
continue;
} else if (*id & NET_TEAM_RIGHT) {
for (i = 0; (i < configuration.right_nplayers) && !inserted; i++)
if ((configuration.right_players[i] == PLAYER_COMPUTER) &&
!_players[2*i+1]) {
inserted = true;
pl = (char)i;
}
if (inserted) {
*id |= pl;
//nright++;
} else
continue;
}
_players[ComputePlayerID(*id)] = 1;
clientIP.push_back(ipa);
/* send the ID back to client */
((net_register_t*)(packetRegister->data))->nplayers_l =
configuration.left_nplayers;
((net_register_t*)(packetRegister->data))->nplayers_r =
configuration.right_nplayers;
((net_register_t*)(packetRegister->data))->bgBig =
configuration.bgBig;
((net_register_t*)(packetRegister->data))->winning_score =
configuration.winning_score;
SDLNet_UDP_Send(mySock, -1, packetRegister);
} else SDL_Delay(500);
}
return (nclients-1);
}
int NetServer::SendSnapshot(Team *tleft, Team *tright, Ball * ball) {
unsigned int i;
net_game_snapshot_t * snap = (net_game_snapshot_t *)(packetSnap->data);
std::vector<Player *> plv;
snap->scorel = tleft->getScore();
snap->scorer = tright->getScore();
/* fill the left team informations */
plv = tleft->players();
for (i = 0; i < plv.size(); i++) {
(snap->teaml)[i].x = plv[i]->x();
(snap->teaml)[i].y = plv[i]->y();
(snap->teaml)[i].frame = plv[i]->state();
}
/* fill the right team informations */
plv = tright->players();
for (i = 0; i < plv.size(); i++) {
(snap->teamr)[i].x = plv[i]->x();
(snap->teamr)[i].y = plv[i]->y();
(snap->teamr)[i].frame = plv[i]->state();
}
/* fill the ball informations */
(snap->ball).x = ball->x();
(snap->ball).y = ball->y();
(snap->ball).frame = ball->frame();
/* send the snapshot to all clients */
for (i = 0; i < clientIP.size(); i++) {
packetSnap->address = *(clientIP[i]);
SDLNet_UDP_Send(mySock, -1, packetSnap);
}
return 0;
}
int NetServer::ReceiveCommand(int * player, char * cmd) {
if (SDLNet_UDP_Recv(mySock, packetCmd) != 0) {
net_command_t * c = (net_command_t*)(packetCmd->data);
*player = ComputePlayerID(c->id);
*cmd = c->command;
return 0;
}
return -1;
}
int NetServer::isRemote(int pl) {
return _players[pl];
}
#endif
|