clients can choose team and faction ... client triggered slot change is prepared

This commit is contained in:
Titus Tscharntke 2010-04-28 23:59:37 +00:00
parent a750d4eda9
commit 50341e9db6
11 changed files with 168 additions and 7 deletions

View File

@ -137,7 +137,7 @@ void GraphicListBox::setSelectedItemIndex(int index){
void GraphicListBox::setEditable(bool editable){
graphButton1.setEditable(editable);
graphButton2.setEditable(editable);
editable=true;
GraphicComponent::setEditable(editable);
}
void GraphicListBox::setSelectedItem(string item){

View File

@ -51,8 +51,8 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM
NetworkManager &networkManager= NetworkManager::getInstance();
Config &config = Config::getInstance();
needToSetChangedGameSettings = false;
lastSetChangedGameSettings = time(NULL);;
lastSetChangedGameSettings = time(NULL);
currentFactionName="";
currentMap="";
@ -116,6 +116,8 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM
listBoxTeams[i].init(600, 550-i*30, 60);
listBoxTeams[i].setEditable(false);
labelNetStatus[i].init(700, 550-i*30, 60);
grabSlotButton[i].init(700, 550-i*30, 30);
grabSlotButton[i].setText(">");
}
labelControl.init(200, 600, GraphicListBox::defW, GraphicListBox::defH, true);
@ -214,6 +216,28 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){
}
else
{
for(int i=0; i<GameConstants::maxPlayers; ++i)
{
if(listBoxFactions[i].getEditable()){
if(listBoxFactions[i].mouseClick(x, y)){
ClientInterface* clientInterface= NetworkManager::getInstance().getClientInterface();
if(clientInterface->isConnected()){
clientInterface->setGameSettingsReceived(false);
clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),clientInterface->getGameSettings()->getThisFactionIndex(),-1,listBoxTeams[i].getSelectedItemIndex());
}
}
}
if(listBoxTeams[i].getEditable()){
if(listBoxTeams[i].mouseClick(x, y)){
if(clientInterface->isConnected()){
clientInterface->setGameSettingsReceived(false);
clientInterface->sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem(),clientInterface->getGameSettings()->getThisFactionIndex(),-1,listBoxTeams[i].getSelectedItemIndex());
}
}
}
}
// for(int i=0; i<mapInfo.players; ++i)
// {
// //ensure thet only 1 human player is present
@ -306,7 +330,10 @@ void MenuStateConnectedGame::render(){
renderer.renderListBox(&listBoxTeams[i]);
//renderer.renderLabel(&labelNetStatus[i]);
if((listBoxControls[i].getSelectedItemIndex()==ctNetwork) ||
if((listBoxControls[i].getSelectedItemIndex()==ctNetwork) && (labelNetStatus[i].getText()=="???")){
renderer.renderButton(&grabSlotButton[i]);
}
else if((listBoxControls[i].getSelectedItemIndex()==ctNetwork) ||
(listBoxControls[i].getSelectedItemIndex()==ctHuman)){
renderer.renderLabel(&labelNetStatus[i]);
}
@ -476,6 +503,8 @@ void MenuStateConnectedGame::update()
// Control
for(int i=0; i<GameConstants::maxPlayers; ++i){
listBoxControls[i].setSelectedItemIndex(ctClosed);
listBoxFactions[i].setEditable(false);
listBoxTeams[i].setEditable(false);
}
for(int i=0; i<gameSettings->getFactionCount(); ++i){
@ -491,6 +520,8 @@ void MenuStateConnectedGame::update()
if(gameSettings->getFactionControl(i) == ctNetwork && gameSettings->getThisFactionIndex() == i){
// set my current slot to ctHuman
listBoxControls[slot].setSelectedItemIndex(ctHuman);
listBoxFactions[slot].setEditable(true);
listBoxTeams[slot].setEditable(true);
}
settingsReceivedFromServer=true;

View File

@ -49,6 +49,7 @@ private:
GraphicListBox listBoxFactions[GameConstants::maxPlayers];
GraphicListBox listBoxTeams[GameConstants::maxPlayers];
GraphicLabel labelNetStatus[GameConstants::maxPlayers];
GraphicButton grabSlotButton[GameConstants::maxPlayers];
MapInfo mapInfo;
bool needToSetChangedGameSettings;
@ -61,7 +62,7 @@ private:
string currentMap;
bool settingsReceivedFromServer;
public:
MenuStateConnectedGame(Program *program, MainMenu *mainMenu, bool openNetworkSlots= false);

View File

@ -523,6 +523,26 @@ void MenuStateCustomGame::update()
needToSetChangedGameSettings = false;
}
SwitchSetupRequest** switchSetupRequests=serverInterface->getSwitchSetupRequests();
for(int i= 0; i<mapInfo.players; ++i)
{
if(switchSetupRequests[i]!=NULL)
{
if(listBoxControls[i].getSelectedItemIndex() == ctNetwork)
{
//printf("switchSetupRequests[i]->getSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str());
//printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam());
if(switchSetupRequests[i]->getSelectedFactionName()!=""){
listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName());
}
if(switchSetupRequests[i]->getToTeam()!=-1)
listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam());
}
delete switchSetupRequests[i];
switchSetupRequests[i]=NULL;
}
}
if(difftime(time(NULL),lastSetChangedGameSettings) >= 2)
{
GameSettings gameSettings;

View File

@ -687,6 +687,15 @@ void ClientInterface::stopServerDiscovery() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam)
{
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//printf("string-cuf-tof-team= %s-%d-%d-%d\n",selectedFactionName.c_str(),currentFactionIndex,toFactionIndex,toTeam);
SwitchSetupRequest message=SwitchSetupRequest(selectedFactionName, currentFactionIndex, toFactionIndex,toTeam);
sendMessage(&message);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
/*
bool ClientInterface::getFogOfWar()
{

View File

@ -72,6 +72,7 @@ public:
bool getLaunchGame() const {return launchGame;}
bool getIntroDone() const {return introDone;}
bool getGameSettingsReceived() const {return gameSettingsReceived;}
void setGameSettingsReceived(bool value) {gameSettingsReceived=value;}
int getPlayerIndex() const {return playerIndex;}
//const GameSettings *getGameSettings() {return &gameSettings;}
@ -80,6 +81,8 @@ public:
void discoverServers(DiscoveredServersInterface *cb);
void stopServerDiscovery();
void sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex, int8 toTeam);
private:
void waitForMessage();

View File

@ -314,6 +314,22 @@ void ConnectionSlot::update(bool checkForNewClients)
}
break;
case nmtSwitchSetupRequest:
{
SwitchSetupRequest switchSetupRequest;
if(receiveMessage(&switchSetupRequest))
{
if(serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()]==NULL)
{
serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()]= new SwitchSetupRequest();
}
*(serverInterface->getSwitchSetupRequests()[switchSetupRequest.getCurrentFactionIndex()])=switchSetupRequest;
}
break;
}
default:
{
//throw runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType));

View File

@ -449,4 +449,42 @@ void NetworkMessageSynchNetworkGameDataFileGet::send(Socket* socket) const
}
// =====================================================
// class NetworkMessageSynchNetworkGameDataFileGet
// =====================================================
SwitchSetupRequest::SwitchSetupRequest()
{
data.messageType= nmtSwitchSetupRequest;
data.selectedFactionName="";
data.currentFactionIndex=-1;
data.toFactionIndex=-1;
data.toTeam = -1;
}
SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam)
{
data.messageType= nmtSwitchSetupRequest;
data.selectedFactionName=selectedFactionName;
data.currentFactionIndex=currentFactionIndex;
data.toFactionIndex=toFactionIndex;
data.toTeam = toTeam;
}
bool SwitchSetupRequest::receive(Socket* socket)
{
return NetworkMessage::receive(socket, &data, sizeof(data));
}
void SwitchSetupRequest::send(Socket* socket) const
{
assert(data.messageType==nmtSwitchSetupRequest);
NetworkMessage::send(socket, &data, sizeof(data));
}
}}//end namespace

View File

@ -38,7 +38,7 @@ enum NetworkMessageType{
nmtSynchNetworkGameDataFileCRCCheck,
nmtSynchNetworkGameDataFileGet,
nmtBroadCastSetup,
nmtSwitchSetupRequest,
nmtCount
};
@ -412,6 +412,43 @@ public:
};
// =====================================================
// class SwitchSetupRequest
//
// Message sent from the server to the client
// when the client connects and vice versa
// =====================================================
class SwitchSetupRequest: public NetworkMessage{
private:
static const int maxStringSize= 256;
private:
struct Data{
int8 messageType;
NetworkString<maxStringSize> selectedFactionName; //wanted faction name
int8 currentFactionIndex;
int8 toFactionIndex;
int8 toTeam;
};
private:
Data data;
public:
SwitchSetupRequest();
SwitchSetupRequest( string selectedFactionName, int8 currentFactionIndex, int8 toFactionIndex,int8 toTeam);
string getSelectedFactionName() const {return data.selectedFactionName.getString();}
int getCurrentFactionIndex() const {return data.currentFactionIndex;}
int getToFactionIndex() const {return data.toFactionIndex;}
int getToTeam() const {return data.toTeam;}
virtual bool receive(Socket* socket);
virtual void send(Socket* socket) const;
};
}}//end namespace
#endif

View File

@ -40,6 +40,7 @@ ServerInterface::ServerInterface(){
for(int i= 0; i<GameConstants::maxPlayers; ++i){
slots[i]= NULL;
switchSetupRequests[i]= NULL;
}
serverSocket.setBlock(false);
serverSocket.bind(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str()));
@ -50,6 +51,9 @@ ServerInterface::~ServerInterface(){
for(int i= 0; i<GameConstants::maxPlayers; ++i){
delete slots[i];
slots[i]=NULL;
delete switchSetupRequests[i];
switchSetupRequests[i]=NULL;
}
close();
@ -217,7 +221,7 @@ void ServerInterface::update()
chatTeamIndex= networkMessageText.getTeamIndex();
break;
}
}
}
}
}
}

View File

@ -43,6 +43,7 @@ private:
ServerSocket serverSocket;
bool gameHasBeenInitiated;
int gameSettingsUpdateCount;
SwitchSetupRequest* switchSetupRequests[GameConstants::maxPlayers];
public:
ServerInterface();
@ -66,6 +67,7 @@ public:
virtual string getNetworkStatus() const;
ServerSocket* getServerSocket() {return &serverSocket;}
SwitchSetupRequest** getSwitchSetupRequests() {return &switchSetupRequests[0];}
void addSlot(int playerIndex);
void removeSlot(int playerIndex);
ConnectionSlot* getSlot(int playerIndex);