Hi, I'm writing mmorpg game and I've got a problem. I spent a lot of time looking for some tips, but I found nothing.
If there is one player in the game, it's okay. But if there are more players, they move in sequence in the order in which they moved. Each client sends the position of the player in normal function, and receives it in function on another thread. If I try to make a second thread on the server (only to receive position), the client has about 10 fps (normally 60).
Client - main.cpp
while(1) {
...
if(ev.type == ALLEGRO_EVENT_TIMER){ // 60 fps
...
/*** LOGIC ***/
player->update(key);
_beginthread(player_updatePos, 0, player);
}
Client - player.cpp
// - send pos
void CPlayer::update(CKeyboard key) {
++timeToSend;
if(timeToSend >= 1/* && (oldPos.x != pos.x || oldPos.y != pos.y)*/) { // [ 20 = 1/3 sec with 60 FPS ]
timeToSend = 0;
// send position
char wiad[45] = "";
char buf[10];
itoa(pos.x, buf, 10);
char buf2[10];
itoa(pos.y, buf2, 10);
char buf4[20];
itoa(nick.size(), buf4, 10);
char* buf3 = new char[nick.size()];
for(int i = 0; i < nick.size(); ++i)
buf3[i] = nick[i];
sprintf(wiad, "POS%s%s=%sx%s", buf4, buf3, buf, buf2);
sendToServerUnseq(wiad, 1);
}
}
// - receive pos
void __cdecl player_updatePos(void* arg) {
//void player_updatePos(CPlayer* player) {
CPlayer* player = static_cast<CPlayer*>(arg);
if(event.type == ENET_EVENT_TYPE_RECEIVE) {
if(receive(event.packet->data, "POS")) {
string mes = getPacket(event.packet->data);
...
if(nickInnego != player->nick)
{
bool finded = false;
for(int i = 0; i < v_otherPlayers.size(); ++i) {
if(v_otherPlayers[i].nick == nickInnego) {
v_otherPlayers[i].pos.x = atoi(posX.c_str());
v_otherPlayers[i].pos.y = atoi(posY.c_str());
v_otherPlayers[i].timeFromLastReceive = 0;
finded = true;
break;
}
}
if(!finded) { // new player
v_otherPlayers.push_back(COtherPlayer(nickInnego));
}
}
else
player->oldPos = player->pos;
}
}
_endthread();
}
Client - function to send position
void sendToServerUnseq(char* message, int channelID) {
ENetPacket *p = enet_packet_create((char*)message, strlen(message)+1, ENET_PACKET_FLAG_UNSEQUENCED);
enet_peer_send(peer, channelID, p);
enet_host_flush(client);
serviceResult = enet_host_service(client, &event, 100);
}
Server - main.cpp (it's only sends that, he got from the client)
while(1)
{
serviceResult = enet_host_service(server, &event, 1); //1000 = 1 sec
case ENET_EVENT_TYPE_RECEIVE:
{
if(receive(event.packet->data, "POS")) {
char mess[event.packet->dataLength];
for(int i = 0; i < event.packet->dataLength; ++i)
mess[i] = event.packet->data[i];
ENetPacket *p = enet_packet_create(mess, strlen(mess)+1, ENET_PACKET_FLAG_UNSEQUENCED);
enet_host_broadcast(server, 1, p);
enet_host_flush(server);
}
}
}
What is wrong? Thanks for any help! :)