Skip to content

Commit

Permalink
#16 Server immediately broadcasts received 'move' commands to reduce …
Browse files Browse the repository at this point in the history
…latency and perceived stutter. Client also doesn't send it's own ships to the new location but instead waits for confirmation from the server. That way both clients should a) nearly always see the same and b) the ships dont jump backwards so much.
  • Loading branch information
nitzel committed Apr 20, 2019
1 parent e8e0917 commit c0af352
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int client(const CConfiguration& config) {
size_t size = 0;
void* data = game->sendSelectedGetData(party, vp, vr, formation, size);
if (data != nullptr) {
game->sendShips(party, data);
// game->sendShips(party, data); // todo see Issue#16 improve/leave/remove
ENetPacket* packet = enet_packet_create(data, size, ENET_PACKET_FLAG_RELIABLE, PTYPE_SHIPS_MOVE);
enet_peer_send(peer, 1, packet);
free(data);
Expand Down
24 changes: 20 additions & 4 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ int server(const CConfiguration &config) {
if (mouseR.y > screen.h - 80) dy = mouseR.y - (screen.h - 80);
view.x += dx / 80.f * 20;
view.y += dy / 80.f * 20;
if (view.x < 0) view.x = 0;
if (view.y < 0) view.y = 0;
if (view.x > game.mMap.w - screen.w) view.x = game.mMap.w - screen.w;
if (view.y > game.mMap.h - screen.h) view.y = game.mMap.h - screen.h;
if (dx != 0 || dy != 0) {
if (view.x < 0) view.x = 0;
if (view.y < 0) view.y = 0;
if (view.x > game.mMap.w - screen.w) view.x = game.mMap.w - screen.w;
if (view.y > game.mMap.h - screen.h) view.y = game.mMap.h - screen.h;
updateMouseOnMapPosition(mouseR, view);
}
}
// clear screen
glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -142,6 +145,7 @@ int server(const CConfiguration &config) {
game.clearChanged();
}

bool hasReceivedMove = false;
while (enet_host_service(host, &event, 0) > 0)
{
switch (event.type)
Expand Down Expand Up @@ -199,6 +203,7 @@ int server(const CConfiguration &config) {
printf("received shipmove event\n");
Party commandingParty = ((ClientData*)event.peer->data)->party;
game.sendShips(commandingParty, enet_packet_data(event.packet));
hasReceivedMove = true; // issue #16 ?
} break;
case PTYPE_PLANET_ACTION:
{
Expand Down Expand Up @@ -242,6 +247,17 @@ int server(const CConfiguration &config) {
default:;
}
}
if (hasReceivedMove) { // immediately broadcast effects of received move commands to clients issue#16 - can/should probably be improved
timeToBroadcast = 0.1; /// 2x per sec
size_t size;
void* d = game.packUpdateData(size, glfwGetTime());
ENetPacket* packet = enet_packet_create(d, size, 0, PTYPE_UPDATE);// ENET_PACKET_FLAG_RELIABLE

enet_host_broadcast(host, 1, packet);
enet_host_flush(host);
free(d);
game.clearChanged();
}
}

glfwDestroyWindow(info.window);
Expand Down

0 comments on commit c0af352

Please sign in to comment.