/* ---------------------------------------------------------------------- * * Public Functions -- Outgoing messages * ---------------------------------------------------------------------- */ void network_join_game (void) { MsgBuf msg; MSG_OPCODE(&msg) = MSG_JOIN; MSG_LENGTH(&msg) = 0; net_send_message (sock, &msg); }
void network_new_city (int* originx, int* originy, int random_village) { MsgBuf msg; MSG_OPCODE(&msg) = MSG_NEW_CITY; MSG_LENGTH(&msg) = 0; net_send_message (sock, &msg); }
void network_do_coal_survey (void) { MsgBuf msg; MSG_OPCODE(&msg) = MSG_DO_COAL_SURVEY; MSG_LENGTH(&msg) = 0; net_send_message (sock, &msg); }
void network_unrequest_mappoint_stats (void) { MsgBuf msg; MSG_OPCODE(&msg) = MSG_MPS_END; MSG_LENGTH(&msg) = 0; net_send_message (sock, &msg); }
void network_unrequest_mini_screen (void) { MsgBuf msg; MSG_OPCODE(&msg) = MSG_MINI_SCRN_END; MSG_LENGTH(&msg) = 0; net_send_message (sock, &msg); }
void network_request_mini_screen (int mini_type) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_MINI_SCRN_REQ; MSG_LENGTH(&msg) = 1 * sizeof(Int32); *msgp++ = htonl(mini_type); net_send_message (sock, &msg); }
void server_send_message(state_type *state, event_content_type *message, time_type now) { // Get the server configuration from simulation state SERVER_lp_state_type *pointer = &state->type.server_state; message->origin_object_id = pointer->server_id; net_send_message(message->origin_object_id, message->destination_object_id, message, state->num_clients, state->num_servers, now, state->average_client_to_server_net_delay); if (pointer->configuration.server_verbose) printf("S%d - function Server_ProcessEvent: DELIVER_MESSAGE sent at time %f\n", pointer->server_id, now); }
void network_launch_rocket (int x, int y) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_LAUNCH_ROCKET; MSG_LENGTH(&msg) = 2 * sizeof(Int32); *msgp++ = htonl(x); *msgp++ = htonl(y); net_send_message (sock, &msg); }
void network_request_mappoint_stats (int x, int y) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_MPS_REQ; MSG_LENGTH(&msg) = 2 * sizeof(Int32); *msgp++ = htonl(x); *msgp++ = htonl(y); net_send_message (sock, &msg); }
void network_send_flags (int x, int y) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_SEND_FLAGS; MSG_LENGTH(&msg) = 3 * sizeof(Int32); *msgp++ = htonl(x); *msgp++ = htonl(y); *msgp++ = htonl(MP_INFO(x,y).flags); net_send_message (sock, &msg); }
void network_bulldoze_item (int x, int y) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_BULLDOZE_ITEM; MSG_LENGTH(&msg) = 2 * sizeof(Int32); *msgp++ = htonl(x); *msgp++ = htonl(y); net_send_message (sock, &msg); }
void network_place_item (int x, int y, int selected_type) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_PLACE_ITEM; MSG_LENGTH(&msg) = 3 * sizeof(Int32); *msgp++ = htonl(x); *msgp++ = htonl(y); *msgp++ = htonl(selected_type); net_send_message (sock, &msg); }
void server_send_remote_tx_get(state_type *state, event_content_type *event_content, time_type now) { // Get the server configuration from simulation state SERVER_lp_state_type *pointer = &state->type.server_state; double predicted_delay; event_content->origin_object_id = pointer->server_id; predicted_delay = get_cubist_predicted_remote_get_rtt(state, event_content, now); predicted_delay/=(double)2; net_send_message(event_content->origin_object_id, event_content->destination_object_id, event_content, state->num_clients, state->num_servers, now, predicted_delay); if (pointer->configuration.server_verbose) printf("S%d - function Server_ProcessEvent: DELIVER_MESSAGE sent at time %f\n", pointer->server_id, now); }
void server_send_final_commit_message(state_type *state, event_content_type *event_content, time_type now) { // Get the server configuration from simulation state SERVER_lp_state_type *pointer = &state->type.server_state; double predicted_delay=state->average_server_to_server_net_delay; event_content->origin_object_id = pointer->server_id; //printf("\ninputString: %s",inputString); // predicted_delay = get_cubist_predicted_rtt(state, event_content, now); predicted_delay=0; net_send_message(event_content->origin_object_id, event_content->destination_object_id, event_content, state->num_clients, state->num_servers, now, predicted_delay); if (pointer->configuration.server_verbose) printf("S%d - function Server_ProcessEvent: DELIVER_MESSAGE sent at time %f\n", pointer->server_id, now); }
void network_request_main_screen (void) { MsgBuf msg; Int32* msgp = (Int32*) MSG_BODY(&msg); MSG_OPCODE(&msg) = MSG_MAIN_SCRN_REQ; MSG_LENGTH(&msg) = 4 * sizeof(Int32); *msgp++ = htonl(main_screen_originx); *msgp++ = htonl(main_screen_originy); #if defined (commentout) /* GCS FIX: Need to give server right size of main screen */ *msgp++ = htonl(MAIN_WIN_W / 16); *msgp++ = htonl(MAIN_WIN_H / 16); #endif net_send_message (sock, &msg); }
int main(int argc, char **argv) { int return_status = 1; #define MAIN_SDL_CHECK(expression, error_prefix) { \ if (!(expression)) { \ log_error(error_prefix, SDL_GetError()); \ goto exit; \ } \ } MAIN_SDL_CHECK(SDL_Init(SDL_INIT_VIDEO) == 0, "SDL_Init"); if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { log_error("IMG_Init", IMG_GetError()); goto exit; } MAIN_SDL_CHECK(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"), "SDL_SetHint SDL_HINT_RENDER_SCALE_QUALITY"); startup_info_t info = startup(argc, argv); if (!info.success) goto exit; net_mode_t net_mode = info.net_mode; network = info.network; char wtitle[256]; if (net_mode == NET_SERVER) { snprintf(wtitle, sizeof(wtitle), "NetCheckers - server (%s)", info.port); } else { snprintf(wtitle, sizeof(wtitle), "NetCheckers - client (%s:%s)", info.host, info.port); } window = SDL_CreateWindow( wtitle, #if 1 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, #else (net_mode == NET_SERVER ? 10 : window_width + 20), SDL_WINDOWPOS_CENTERED, #endif window_width, window_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI ); MAIN_SDL_CHECK(window, "SDL_CreateWindow"); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); MAIN_SDL_CHECK(renderer, "SDL_CreateRenderer"); MAIN_SDL_CHECK(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) == 0, "SDL_SetRenderDrawBlendMode SDL_BLENDMODE_BLEND"); char *error = 0; char path[1024]; #define LOAD_TEXTURE_CHECKED(var, file) { \ sprintf(path, "%s/" file, info.assets_path); \ var = load_png_texture(path, &error); \ if (error) { \ log_error("load_png_texture " file, error); \ goto exit; \ } \ } LOAD_TEXTURE_CHECKED(tex.textures.board, "board.png"); LOAD_TEXTURE_CHECKED(tex.textures.red_piece, "piece_red.png"); LOAD_TEXTURE_CHECKED(tex.textures.red_piece_king, "piece_red_king.png"); LOAD_TEXTURE_CHECKED(tex.textures.white_piece, "piece_white.png"); LOAD_TEXTURE_CHECKED(tex.textures.white_piece_king, "piece_white_king.png"); LOAD_TEXTURE_CHECKED(tex.textures.highlight, "highlight.png"); LOAD_TEXTURE_CHECKED(tex.textures.player_turn, "player_turn.png"); LOAD_TEXTURE_CHECKED(tex.textures.opponent_turn, "opponent_turn.png"); LOAD_TEXTURE_CHECKED(tex.textures.victory, "victory.png"); LOAD_TEXTURE_CHECKED(tex.textures.defeat, "defeat.png"); window_resized(window_width, window_height); // Put the pieces on the board #if 0 // Game over testing for (int i = 0; i < 24; i++) { piece_t *piece = pieces + i; piece->color = (i >= 12) ? PIECE_BLACK : PIECE_WHITE; piece->captured = true; } pieces[0].captured = false; pieces[0].king = true; pieces[0].pos = cell_pos(1, 3); board[1][3] = &pieces[0]; pieces[12].captured = false; pieces[12].king = true; pieces[12].pos = cell_pos(5, 3); board[5][3] = &pieces[12]; #else int fill_row = 0; int fill_col = 0; for (int i = 0; i < 12; i++) { piece_t *piece = pieces + i; piece->color = PIECE_WHITE; piece->pos = cell_pos(fill_row, fill_col); board[fill_row][fill_col] = piece; advance_board_row_col(&fill_row, &fill_col); } fill_row = 5; fill_col = 1; for (int i = 12; i < 24; i++) { piece_t *piece = pieces + i; piece->color = PIECE_BLACK; piece->pos = cell_pos(fill_row, fill_col); board[fill_row][fill_col] = piece; advance_board_row_col(&fill_row, &fill_col); } #endif game_over = false; current_turn = PIECE_BLACK; local_color = (net_mode == NET_SERVER) ? PIECE_BLACK : PIECE_WHITE; bool running = true; int last_time = SDL_GetTicks(); while (running) { int current_time = SDL_GetTicks(); int ellapsed_ms = current_time - last_time; last_time = current_time; float delta_time = (float)ellapsed_ms / 1000.0f; SDL_Event event = {0}; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: { running = false; } break; case SDL_WINDOWEVENT: { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { window_resized(event.window.data1, event.window.data2); } } break; case SDL_MOUSEBUTTONDOWN: { #if 1 if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_RIGHT) { message_t net_msg = {0}; net_msg.move_piece = cell_pos(5, 1); net_msg.move_target = cell_pos(4, 0); net_send_message(network, &net_msg); } #endif if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_LEFT) { if (!game_over && !animating_piece && current_turn == local_color) { int click_x = event.button.x * dpi_rate; int click_y = event.button.y * dpi_rate; if (rect_includes(&board_rect, click_x, click_y)) { cell_pos_t clicked_cell = point_to_cell(click_x, click_y); piece_t *clicked_piece = board[clicked_cell.row][clicked_cell.col]; if (clicked_piece && clicked_piece->color == current_turn) { piece_moves_t moves = find_valid_moves(clicked_piece); if (moves.count) { selected_piece = clicked_piece; available_moves = moves; } } else if (selected_piece) { cell_pos_t from_cell = selected_piece->pos; move_result_t res = perform_move(selected_piece, clicked_cell); if (res != MOVE_INVALID) { if (res == MOVE_END_TURN) { selected_piece = 0; } else { available_moves = find_valid_moves(selected_piece); } message_t net_msg = {0}; net_msg.move_piece = from_cell; net_msg.move_target = clicked_cell; if (!net_send_message(network, &net_msg)) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Falha de comunicação", "Falha ao enviar movimento para o adversário.", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } } } } } } } break; } } if (net_get_state(network) != NET_RUNNING) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Conexão Interrompida", "A conexão foi interrompida pelo adversário", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } message_t net_msg; if (net_poll_message(network, &net_msg)) { bool valid_move = false; piece_t *piece = board[net_msg.move_piece.row][net_msg.move_piece.col]; if (piece && current_turn != local_color && piece->color != local_color) { move_result_t res = perform_move(piece, net_msg.move_target); if (res != MOVE_INVALID) valid_move = true; } if (!valid_move) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Movimento inválido", "Seu adversário enviou um movimento inválido.", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } } render(delta_time); } return_status = 0; exit: if (network) net_destroy(network); for (int i = 0; i < ARRAY_SIZE(tex.array); i++) { if (tex.array[i]) SDL_DestroyTexture(tex.array[i]); } if (renderer) SDL_DestroyRenderer(renderer); if (window) SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return return_status; }