Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / MFC

Building Multiplayer Capability into Your Game in One Hour

Rate me:
Please Sign up or sign in to vote.
4.31/5 (11 votes)
7 Oct 20054 min read 93.7K   1.1K   43  
Want to build multiplayer games but have no idea where to start or no time to develop solid networking code? Read on...
/*
	Author: Gabriyel Wong C.K. (gabriyel@gmail.com)
	Multiplayer networking code (client) for integration into any game engine.
	Code is adopted and modified from Dave Andrew's tutorial on integrating 
	Raknet and Irrlicht

	Copyright (C) 2005 Gabriyel Wong.

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library 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
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <PacketEnumerations.h>
#include <RakNetworkFactory.h>
#include <NetworkTypes.h>
#include <RakClientInterface.h>
#include <BitStream.h>

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <iostream>

#include "MyPlayer.h"

const unsigned char PACKET_ID_LINE = 100;

class ClientConnection
{
public:
    ClientConnection(char * serverIP, char * portString)
    : client(NULL)
    {
        client = RakNetworkFactory::GetRakClientInterface();    
        client->Connect(serverIP, atoi(portString), 0, 0, 0);
		Player myself;
		players.push_back( myself );	// 1st player (self), subsequent players are created on the fly
    }
    
    ~ClientConnection()
    {
        client->Disconnect(300);
        RakNetworkFactory::DestroyRakClientInterface(client);
    }

	// Updates all entities in own world, data coming in from external
    void updateOwnWorld(Player* incomingPlayer)
    {
		// Loop through all players including ownself and update accordingly
		for (int i=0; i<players.size(); i++)
		{
			int diff = (int) (incomingPlayer->ID - players[i].ID);
			if (!diff)
			{
				// Copy all information
				players[i].position[0] = incomingPlayer->position[0];
				players[i].position[1] = incomingPlayer->position[1];
				players[i].position[2] = incomingPlayer->position[2];
				players[i].orientation[0] = incomingPlayer->orientation[0];
				players[i].orientation[1] = incomingPlayer->orientation[1];
				players[i].orientation[2] = incomingPlayer->orientation[2];
				players[i].speed = incomingPlayer->speed;
				players[i].missiles = incomingPlayer->missiles;
				players[i].health = incomingPlayer->health;
			}
		}
    }
    
	// Updates rest of the external world about own changes
    void updateWorld(Player p)
    {
        RakNet::BitStream dataStream;

        dataStream.Write(PACKET_ID_LINE);
        dataStream.Write((float)(p.position[0]));
		dataStream.Write((float)(p.position[1]));
		dataStream.Write((float)(p.position[2]));
		dataStream.Write((float)(p.orientation[0]));
		dataStream.Write((float)(p.orientation[1]));
		dataStream.Write((float)(p.orientation[2]));
		dataStream.Write((int)(p.missiles));
        dataStream.Write((int)(p.speed));
        dataStream.Write((int)(p.health));
     
        client->Send(&dataStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0);
    }

	void ListenForPackets()
    {
        Packet * p = client->Receive();
        
        if(p != NULL) 
		{
            HandlePacket(p);      
            client->DeallocatePacket(p);
        }
    }
    
	// Handle incoming packet
    void HandlePacket(Packet * p)
    {
        RakNet::BitStream dataStream((const char*)p->data, p->length, false);
        unsigned char packetID;
    
        dataStream.Read(packetID);
    
        switch(packetID) 
		{
        case PACKET_ID_LINE:
			Player inPlayer;
                    
			// Order is important (reference server code as well)
			dataStream.Read((inPlayer.ID));
			dataStream.Read((inPlayer.position[0]));
			dataStream.Read((inPlayer.position[1]));
			dataStream.Read((inPlayer.position[2]));
			dataStream.Read((inPlayer.orientation[0]));
			dataStream.Read((inPlayer.orientation[1]));
			dataStream.Read((inPlayer.orientation[2]));
			dataStream.Read((inPlayer.missiles));
			dataStream.Read((inPlayer.speed));		
			dataStream.Read((inPlayer.health));
	
			// If you're not passing "uiversal data" (i.e. passing a single type of data 
			// which is universally shared) and the number of players are dynamically
			// changing, you'd need a manager to keep track of new and dropped out players.
			// I'm using a vector as a simple container and some manipulation for that.
			if (_isNewPlayer(inPlayer.ID))
			{
				std::cout << "New player " << inPlayer.ID << "just joined!" << std::endl;
				// Create new player and include in world
				Player newplayer;
				newplayer.ID = inPlayer.ID;
				newplayer.position[0] = inPlayer.position[0];
				newplayer.position[1] = inPlayer.position[1];
				newplayer.position[2] = inPlayer.position[2];
				newplayer.orientation[0] = inPlayer.orientation[0];
				newplayer.orientation[1] = inPlayer.orientation[1];
				newplayer.orientation[2] = inPlayer.orientation[2];
				newplayer.missiles = inPlayer.missiles;
				newplayer.speed = inPlayer.speed;
				newplayer.health = inPlayer.health;
				players.push_back( newplayer );
			}
			else
			{
				// Look for the specific player and update accordingly
				if (!updateOwnWorld(inPlayer))
				{
					std::cout << "Error updating Player " << inPlayer.ID << "'s information!" << std::endl;
					getchar();
				}
			}
       
			break;
        }
    }
    
	int getPlayerCount() { return players.size(); }

private:

	bool _isNewPlayer(double playerID)
	{
		int count=0;
		
		for (int i=0; i<players.size(); i++)
		{
			int diff = playerID-players[i].ID;
			if ( !diff )	// If similar, x-y=0;
			{
				count++;
			}
		}

		if (count==0)	// is new
		{
            return true;
		}

		return false;
	}

	bool updateOwnWorld(Player p)
	{
		int count = 0;
		for (int i=0; i<players.size(); i++)
		{
			int diff = p.ID-players[i].ID;
			if ( !diff )	// If similar, x-y=0;
			{
				// Update the information	
				players[i].position[0] = p.position[0];
				players[i].position[1] = p.position[1];
				players[i].position[2] = p.position[2];
				players[i].orientation[0] = p.orientation[0];
				players[i].orientation[1] = p.orientation[1];
				players[i].orientation[2] = p.orientation[2];
				players[i].missiles = p.missiles;
				players[i].speed = p.speed;
				players[i].health = p.health;
				return true;	// Assuming no identical ID
			}
			else
			{
				count++;
			}
		}
		if (count == players.size())
		{
			return false;
		}
	}

    RakClientInterface * client;
};

int main(int argc, char** argv)
{
	// Change this to non-compile dependent
    ClientConnection myConnection("127.0.0.1", "10000");

	while(1) 
	{
        Sleep(100);
		myConnection.ListenForPackets();	// includes updating of changes from world
		std::cout << "Total number of players: " << myConnection.getPlayerCount() << std::endl;
        
		if(kbhit())
		{
			char c=getchar();
			if (c==' ')
			{
                // players[0] is always ownself
				players[0].position[0]-=0.1f;
				players[0].position[1]-=0.2f;
				players[0].position[2]-=0.3f;
				players[0].orientation[0]+=0.1f;
				players[0].orientation[1]+=0.1f;
				players[0].orientation[2]+=0.1f;
				players[0].health++;
				players[0].missiles++;
				players[0].speed--;

				myConnection.updateWorld(players[0]);	// includes updating world of own changes
			}
		}
		
		// Get an account of all world items
		for (int j=0; j<players.size(); j++)
		{
			printf("Player[%u] position[0]: %f\n", players[j].ID, players[j].position[0]);
			printf("Player[%u] position[1]: %f\n", players[j].ID, players[j].position[1]);
			printf("Player[%u] position[2]: %f\n", players[j].ID, players[j].position[2]);
			printf("Player[%u] orientation[0]: %f\n", players[j].ID, players[j].orientation[0]);
			printf("Player[%u] orientation[1]: %f\n", players[j].ID, players[j].orientation[1]);
			printf("Player[%u] orientation[2]: %f\n", players[j].ID, players[j].orientation[2]);
			printf("Player[%u] missiles: %d\n", players[j].ID, players[j].missiles);
			printf("Player[%u] speed: %d\n", players[j].ID, players[j].speed);
			printf("Player[%u] health: %d\n", players[j].ID, players[j].health);
		}
    }

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions