/* decode one input frame and update player controllers to reflect this */ void joynet_game_logic(JOYNET_GAME * gp) { if(gp->input_buffer->frames > 0) { switch(gp->type) { case JOYNET_GAME_TYPE_MOUSE: { joynet_serialize(gp->serial_data, &gp->input_buffer->data[gp->input_buffer->read_pos]); if(gp->controller_axes > 0) { joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_x); } if(gp->controller_axes > 1) { joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_y); } if(gp->controller_axes > 2) { joynet_getw(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_z); } if(gp->controller_buttons > 0) { joynet_getc(gp->serial_data, &gp->player[gp->current_player]->controller->mouse_b); } gp->input_buffer->read_pos += joynet_get_serial_size(gp->serial_data); if(gp->input_buffer->read_pos >= gp->max_buffer_frames * gp->input_buffer->frame_size) { gp->input_buffer->read_pos = 0; } break; } case JOYNET_GAME_TYPE_CONTROLLERS: { unsigned char axis_data; int i, j; for(i = 0; i < gp->players; i++) { if(gp->player[i]->controller) { joynet_serialize(gp->serial_data, &gp->input_buffer->data[gp->input_buffer->read_pos]); if(gp->controller_axes > 0) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player[i]->controller->axis[0] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_axes > 1) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player[i]->controller->axis[1] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_axes > 2) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player[i]->controller->axis[2] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_buttons > 0) { joynet_getc(gp->serial_data, &gp->player[i]->controller->bits[0]); for(j = 0; j < 8; j++) { gp->player[i]->controller->button[j] = ((gp->player[i]->controller->bits[0] >> j) & 1); } // printf("bits = %d\n", gp->player[i]->controller->bits[0]); } if(gp->controller_buttons > 8) { joynet_getc(gp->serial_data, &gp->player[i]->controller->bits[1]); for(j = 0; j < 8; j++) { gp->player[i]->controller->button[j + 8] = ((gp->player[i]->controller->bits[1] >> j) & 1); } } gp->input_buffer->read_pos += joynet_get_serial_size(gp->serial_data); if(gp->input_buffer->read_pos >= gp->max_buffer_frames * gp->input_buffer->frame_size) { gp->input_buffer->read_pos = 0; } } } break; }
/* if we have new input send INPUT message * send NO_INPUT message when no new input has been received * call this once per tick */ void joynet_update_game_server(JOYNET_SERVER * sp, JOYNET_GAME * gp) { ENetPacket * pp; char data[1024]; int i, j; if(gp->state == JOYNET_GAME_STATE_PLAYING) { switch(gp->type) { case JOYNET_GAME_TYPE_MOUSE: { if(gp->input_buffer->frames) { /* pull the next input frame from the buffer and send it to everyone */ joynet_read_input_buffer_frame(joynet_current_server_game->input_buffer, data); for(i = 0; i < sp->max_clients; i++) { if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1)) { pp = joynet_build_packet(JOYNET_GAME_MESSAGE_INPUT, data, joynet_current_server_game->input_buffer->frame_size); enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp); } } /* decode the input so we have a copy of the current inputs on the server */ joynet_serialize(gp->serial_data, data); if(gp->controller_axes > 0) { joynet_getw(gp->serial_data, &gp->player_mouse[0]->x); } if(gp->controller_axes > 1) { joynet_getw(gp->serial_data, &gp->player_mouse[0]->y); } if(gp->controller_axes > 2) { joynet_getw(gp->serial_data, &gp->player_mouse[0]->z); } if(gp->controller_buttons > 0) { joynet_getc(gp->serial_data, &gp->player_mouse[0]->b); } } /* if we have no buffered input, send NO_INPUT message */ else { for(i = 0; i < sp->max_clients; i++) { if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1)) { pp = joynet_create_packet(JOYNET_GAME_MESSAGE_NO_INPUT, NULL); enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp); } } } break; } case JOYNET_GAME_TYPE_CONTROLLERS: { /* read next input frame and send to everyone */ if(joynet_current_server_game->input_buffer->frames) { joynet_read_input_buffer_frame(joynet_current_server_game->input_buffer, data); for(i = 0; i < sp->max_clients; i++) { if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1)) { pp = joynet_build_packet(JOYNET_GAME_MESSAGE_INPUT, data, joynet_current_server_game->input_buffer->frame_size); enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp); } } } /* send NO_INPUT message if we don't have anything in the buffer */ else { for(i = 0; i < sp->max_clients; i++) { if(sp->client[i]->peer && (sp->client[i]->playing || sp->client[i]->spectating == 1)) { pp = joynet_create_packet(JOYNET_GAME_MESSAGE_NO_INPUT, NULL); enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_GAME, pp); } } } unsigned char axis_data; joynet_serialize(gp->serial_data, data); for(i = 0; i < gp->players; i++) { if(gp->player[i]->playing) { if(gp->controller_axes > 0) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player_controller[i]->axis[0] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_axes > 1) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player_controller[i]->axis[1] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_axes > 2) { joynet_getc(gp->serial_data, (char *)(&axis_data)); gp->player_controller[i]->axis[2] = (float)axis_data / 127.5 - 1.0; } if(gp->controller_buttons > 0) { joynet_getc(gp->serial_data, &gp->player_controller[i]->bits[0]); for(j = 0; j < 8; j++) { gp->player_controller[i]->button[j] = ((gp->player_controller[i]->bits[0] >> j) & 1); } } if(gp->controller_buttons > 8) { joynet_getc(gp->serial_data, &gp->player_controller[i]->bits[1]); for(j = 0; j < 8; j++) { gp->player_controller[i]->button[j + 8] = ((gp->player_controller[i]->bits[1] >> j) & 1); } } } } break; } }