From 2b2d58e904d06b819df5044911b996c6ae5c5e08 Mon Sep 17 00:00:00 2001 From: gmueller Date: Mon, 10 Jan 2011 23:02:38 +0100 Subject: [PATCH] fix team, point sync --- src/game.cpp | 8 ++++---- src/main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index f0dfd3b..fb439dd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -74,9 +74,9 @@ void game_reset_point(point_t *point) { void game_setup(game_t *game, int master, void(*explosion_callback)(double x, double y, double z)) { size_t i; + for (i = 0; i < GAME_TEAM_COUNT; i++) + game_setup_team(&game->team[i], i); if (master) { - for (i = 0; i < GAME_TEAM_COUNT; i++) - game_setup_team(&game->team[i], i); for (i = 0; i < GAME_PLAYER_COUNT; i++) game_setup_player(&game->player[i]); @@ -84,8 +84,8 @@ void game_setup(game_t *game, int master, void(*explosion_callback)(double x, for (i = 0; i < GAME_POINT_COUNT; i++) game_setup_point(&game->point[i]); } else { - for (i = 0; i < GAME_TEAM_COUNT; i++) - game_reset_team(&game->team[i], i); +// for (i = 0; i < GAME_TEAM_COUNT; i++) +// game_reset_team(&game->team[i], i); for (i = 0; i < GAME_PLAYER_COUNT; i++) game_reset_player(&game->player[i]); diff --git a/src/main.cpp b/src/main.cpp index 7595bfb..4e7daf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,8 +24,8 @@ void key_callback(int key, int state) { } void setup_light() { - GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; - GLfloat LightDiffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; + GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; + GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat LightPosition[] = { 1.0f, 1.0f, 1.0f, 0.0f }; // setup directional light @@ -178,7 +178,8 @@ void setup_network(const char *remote) { #define MESSAGE_PLAYER_ACCELERATE 4 #define MESSAGE_BOMB_DROP 5 #define MESSAGE_BOMB_UPDATE 6 -#define MESSAGE_POINT_UPDATE 6 +#define MESSAGE_POINT_UPDATE 7 +#define MESSAGE_TEAM_UPDATE 8 typedef struct message_t { uint16_t msg_id; @@ -206,6 +207,7 @@ typedef struct player_update_message_t { unsigned int session; double x, y, z; double vx, vy, vz; + uint16_t points; } player_update_message_t; typedef struct player_accelerate_message_t { @@ -236,6 +238,13 @@ typedef struct point_update_mesage_t { double x, y, z; } point_update_meesage_t; +struct team_update_message_t { + uint16_t msg_id; + uint16_t team_id; + uint16_t points; + uint16_t wins; +}; + void send_player_updates(game_t *game) { size_t i; for (i = 0; i < GAME_PLAYER_COUNT; i++) { @@ -250,6 +259,16 @@ void send_player_updates(game_t *game) { msg.vx = game->player[i].vx; msg.vy = game->player[i].vy; msg.vz = game->player[i].vz; + msg.points = game->player[i].points; + ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); + enet_host_broadcast(host, 0, packet); + } + for (i = 0; i < GAME_TEAM_COUNT; i++) { + team_update_message_t msg; + msg.msg_id = MESSAGE_TEAM_UPDATE; + msg.team_id = game->team[i].id; + msg.points = game->team[i].points; + msg.wins = game->team[i].wins; ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); enet_host_broadcast(host, 0, packet); } @@ -286,6 +305,7 @@ void dispatch_message(enet_uint8 *data, size_t length, game_t *game) { player->vx = um->vx; player->vy = um->vy; player->vz = um->vz; + player->points = um->points; break; } case MESSAGE_POINT_UPDATE: { @@ -324,6 +344,15 @@ void dispatch_message(enet_uint8 *data, size_t length, game_t *game) { } break; } + case MESSAGE_TEAM_UPDATE: { + team_update_message_t *m = (team_update_message_t *) data; + team_t *team = game_team(game, m->team_id); + if (team == NULL) + return; + team->points = m->points; + team->wins = m->wins; + break; + } }; } void service_network(game_t *game) { @@ -644,6 +673,13 @@ void Application::update() { camZ = 1000.0; } + service_network(&game); + if (server && player_update_schudule.next(time)) { + send_player_updates(&game); + } + + game_update(&game, dt); + double rx = cos(phi) * cos(theta); double ry = sin(theta); double rz = sin(phi) * cos(theta); @@ -673,6 +709,7 @@ void Application::update() { drop_bomb(&game, rx, ry, rz, 5.0); } } + service_network(&game); prepareFrame(rx, ry, rz); setup_light(); @@ -696,7 +733,6 @@ void Application::update() { glDisable(GL_CULL_FACE); explosion.update(dt * 1000.0, camX, camY, camZ); explosion.render(); - oglf_begin(&font, width, height); if (local_player) { std::stringstream sstr; @@ -721,12 +757,6 @@ void Application::update() { // Check if the ESC key was pressed or the window was closed running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED); - service_network(&game); - if (server && player_update_schudule.next(time)) { - send_player_updates(&game); - } - - game_update(&game, dt); last_time = time;