celestar@5642: /* $Id$ */ celestar@5642: celestar@5642: #ifdef ENABLE_NETWORK celestar@5642: celestar@5642: #include "../stdafx.h" celestar@5642: #include "../debug.h" celestar@5642: #include "network_data.h" celestar@5642: #include "../string.h" celestar@5642: #include "network_client.h" celestar@5642: #include "../command.h" celestar@5642: #include "../callback_table.h" celestar@5642: celestar@5642: // Add a command to the local command queue celestar@5642: void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp) celestar@5642: { celestar@5642: CommandPacket* new_cp = malloc(sizeof(*new_cp)); celestar@5642: celestar@5642: *new_cp = *cp; celestar@5642: celestar@5642: if (cs->command_queue == NULL) { celestar@5642: cs->command_queue = new_cp; celestar@5642: } else { celestar@5642: CommandPacket *c = cs->command_queue; celestar@5642: while (c->next != NULL) c = c->next; celestar@5642: c->next = new_cp; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: // Prepare a DoCommand to be send over the network celestar@5642: void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) celestar@5642: { celestar@5642: CommandPacket *c = malloc(sizeof(CommandPacket)); celestar@5642: byte temp_callback; celestar@5642: celestar@5642: c->player = _local_player; celestar@5642: c->next = NULL; celestar@5642: c->tile = tile; celestar@5642: c->p1 = p1; celestar@5642: c->p2 = p2; celestar@5642: c->cmd = cmd; celestar@5642: c->callback = 0; celestar@5642: celestar@5642: temp_callback = 0; celestar@5642: celestar@5642: while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) celestar@5642: temp_callback++; celestar@5642: if (temp_callback == _callback_table_count) { celestar@5642: DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); celestar@5642: temp_callback = 0; /* _callback_table[0] == NULL */ celestar@5642: } celestar@5642: celestar@5642: if (_network_server) { celestar@5642: // We are the server, so set the command to be executed next possible frame celestar@5642: c->frame = _frame_counter_max + 1; celestar@5642: } else { celestar@5642: c->frame = 0; // The client can't tell which frame, so just make it 0 celestar@5642: } celestar@5642: celestar@5642: ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); celestar@5642: celestar@5642: if (_network_server) { celestar@5642: // If we are the server, we queue the command in our 'special' queue. celestar@5642: // In theory, we could execute the command right away, but then the celestar@5642: // client on the server can do everything 1 tick faster than others. celestar@5642: // So to keep the game fair, we delay the command with 1 tick celestar@5642: // which gives about the same speed as most clients. celestar@5642: NetworkClientState *cs; celestar@5642: celestar@5642: // And we queue it for delivery to the clients celestar@5642: FOR_ALL_CLIENTS(cs) { celestar@5642: if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); celestar@5642: } celestar@5642: celestar@5642: // Only the server gets the callback, because clients should not get them celestar@5642: c->callback = temp_callback; celestar@5642: if (_local_command_queue == NULL) { celestar@5642: _local_command_queue = c; celestar@5642: } else { celestar@5642: // Find last packet celestar@5642: CommandPacket *cp = _local_command_queue; celestar@5642: while (cp->next != NULL) cp = cp->next; celestar@5642: cp->next = c; celestar@5642: } celestar@5642: celestar@5642: return; celestar@5642: } celestar@5642: celestar@5642: // Clients send their command to the server and forget all about the packet celestar@5642: c->callback = temp_callback; celestar@5642: SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); celestar@5642: } celestar@5642: celestar@5642: // Execute a DoCommand we received from the network celestar@5642: void NetworkExecuteCommand(CommandPacket *cp) celestar@5642: { celestar@5642: _current_player = cp->player; celestar@5642: _cmd_text = cp->text; celestar@5642: /* cp->callback is unsigned. so we don't need to do lower bounds checking. */ celestar@5642: if (cp->callback > _callback_table_count) { celestar@5642: DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); celestar@5642: cp->callback = 0; celestar@5642: } celestar@5642: DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND); celestar@5642: } celestar@5642: celestar@5642: #endif /* ENABLE_NETWORK */