Hey, please help me debug this, cause I'm stuck xP.
The problem occurs when receiving new UDP packet.
Server
#include "MikeNet.h"
#include <iostream>
//TCP codes
#define CODE_NEW_PLAYER 1
#define CODE_PLAYER_LEFT 2
#define CODE_PLAYER_ASSIGN 3
//UDP codes
#define CODE_PLAYER_MOVED 1
int main()
{
int iJoined;
int iLeft;
int iUdpPackets;
unsigned short LocalPort = 6789;
char * LocalIP = "";
int MaxClients = 50;
int MaxOperations = 1;
int ErrorMode = ERRORMODE_ENABLE_MESSAGE_BOX;
int UdpMode = UDPMODE_PER_CLIENT_PER_OPERATION;
int UdpRecvSize = 1024;
int TcpRecvSize = 16;
int TcpStoreSize = 16;
int NoThreads = 0;
float playerX;
float playerY;
float playerZ;
float angleX;
float angleY;
float angleZ;
std::cout << "Starting server on 127.0.0.1:6789..." << std::endl;
mnStartServer(LocalIP, LocalPort, MaxClients, MaxOperations, ErrorMode, UdpMode, UdpRecvSize, TcpRecvSize, TcpStoreSize, NoThreads);
std::cout << "Server started." << std::endl;
std::cout << "Waiting for connections..." << std::endl;
while(true)
{
iJoined = mnClientJoined();
iLeft = mnClientLeft();
if( iJoined > 0 )
{
std::cout << "Received connection request." << std::endl;
std::cout << "Player info: " << std::endl;
std::cout << "ID: " << iJoined << std::endl;
for( int n = 1; n <= 30; n++ )
{
if( mnClientConnected(n) == 1 )
{
mnAddInt(CODE_NEW_PLAYER);
mnAddInt(n);
mnSendTCP(iJoined, false, false);
}
}
mnAddInt(CODE_NEW_PLAYER);
mnAddInt(iJoined);
mnSendTCPAll( false, false, iJoined );
mnAddInt(CODE_PLAYER_ASSIGN);
mnAddInt(iJoined);
mnSendTCP(iJoined, false, false);
}
for( int iClient = 1;iClient <= MaxClients;iClient++ )
{
for( int iOperation = 0;iOperation < MaxOperations; iOperation++ )
{
iUdpPackets = mnRecvUDP( iClient, iOperation );
if( iUdpPackets > 0 )
{
playerX = mnGetFloat();
playerY = mnGetFloat();
playerZ = mnGetFloat();
angleX = mnGetFloat();
angleY = mnGetFloat();
angleZ = mnGetFloat();
mnAddInt( CODE_PLAYER_MOVED );
mnAddInt( iClient );
mnAddFloat( playerX );
mnAddFloat( playerY );
mnAddFloat( playerZ );
mnAddFloat( angleX );
mnAddFloat( angleY );
mnAddFloat( angleZ );
mnSendUDPAll( false, false, iClient );
}
}
}
}
}
Client
main.cpp
#include "DarkGDK.h"
#include "MikeNet.h"
#include "Player.h"
//TCP codes
#define CODE_NEW_PLAYER 1
#define CODE_PLAYER_LEFT 2
#define CODE_PLAYER_ASSIGN 3
//UDP codes
#define CODE_PLAYER_MOVED 1
void DarkGDK()
{
Player bubble(101);
int iCode;
int iPlayer;
int iResult = 0;
int iTcpPackets;
int iUdpPackets;
float posX;
float PosY;
float PosZ;
float angleX;
float angleY;
float angleZ;
dbSyncOn();
dbSyncRate(60);
dbPrint("Connecting to server...");
iResult = mnConnect("127.0.0.1",
6789,
4,
"",
NULL,
ERRORMODE_ENABLE_MESSAGE_BOX,
1024,
1024,
0);
if( iResult == 1)
dbPrint("Connected.");
else
dbPrint("Unable to connect. The server might be down.");
while(LoopSDK())
{
if ( iResult != 1 )
dbText(0, 0, "Offline mode.");
iTcpPackets = mnRecvTCP(NULL);
iUdpPackets = mnRecvUDP( NULL, 0 );
if( iUdpPackets == 1 )
{
iCode = mnGetInt();
iPlayer = mnGetInt();
if( iCode == CODE_PLAYER_MOVED )
{
//Receive data
posX = mnGetFloat();
PosY = mnGetFloat();
PosZ = mnGetFloat();
angleX = mnGetFloat();
angleY = mnGetFloat();
angleZ = mnGetFloat();
//Position the cube
dbPositionObject( iPlayer, posX, PosY, PosZ );
//RotateObject
dbRotateObject( iPlayer, angleX, angleY, angleZ );
}
}
if( iTcpPackets == 1)
{
iCode = mnGetInt();
iPlayer = mnGetInt();
if( iCode == CODE_NEW_PLAYER )
{
dbMakeObjectCube( iPlayer, 1 );
}
if( iCode == CODE_PLAYER_ASSIGN )
{
dbHideObject( iPlayer );
}
}
mnAddFloat( dbObjectPositionX(101) );
mnAddFloat( dbObjectPositionY(101) );
mnAddFloat( dbObjectPositionZ(101) );
mnAddFloat( dbObjectAngleX(101) );
mnAddFloat( dbObjectAngleY(101) );
mnAddFloat( dbObjectAngleZ(101) );
mnSendUDP( NULL, false, false );
bubble.OnUpdate();
dbSync();
}
}
Player.h
class Player
{
public:
Player(int id);
~Player(){};
int items[50];
void OnUpdate();
private:
int playerId;
int health;
};
Player::Player(int id)
{
playerId = id;
dbMakeObjectCube(playerId, 1);
}
void Player::OnUpdate()
{
//Controls
if ( dbKeyState(17) == 1 )
dbMoveObject( playerId, 0.1 );
if ( dbKeyState(31) == 1 )
dbMoveObject( playerId, -0.1 );
if ( dbKeyState(32) == 1 )
dbYRotateObject( playerId, dbObjectAngleY( playerId ) + 5 );
if ( dbKeyState(30) == 1 )
dbYRotateObject( playerId, dbObjectAngleY( playerId ) -5 );
//Position camera
dbPositionCamera(dbObjectPositionX( playerId ),
dbObjectPositionY( playerId ),
dbObjectPositionZ( playerId ) - 5 );
}
Thanks