(svn r25652) -Fix: Improve text caret movement for complex scripts.
[openttd/fttd.git] / src / network / network_command.cpp
blob6cf96c9f58c70a576e16af2d8393e2a39dc382b0
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file network_command.cpp Command handling over network connections. */
12 #ifdef ENABLE_NETWORK
14 #include "../stdafx.h"
15 #include "network_admin.h"
16 #include "network_client.h"
17 #include "network_server.h"
18 #include "../command_func.h"
19 #include "../company_func.h"
20 #include "../settings_type.h"
22 /** Table with all the callbacks we'll use for conversion*/
23 static CommandCallback * const _callback_table[] = {
24 /* 0x00 */ NULL,
25 /* 0x01 */ CcBuildPrimaryVehicle,
26 /* 0x02 */ CcBuildAirport,
27 /* 0x03 */ CcBuildBridge,
28 /* 0x04 */ CcBuildCanal,
29 /* 0x05 */ CcBuildDocks,
30 /* 0x06 */ CcFoundTown,
31 /* 0x07 */ CcBuildRoadTunnel,
32 /* 0x08 */ CcBuildRailTunnel,
33 /* 0x09 */ CcBuildWagon,
34 /* 0x0A */ CcRoadDepot,
35 /* 0x0B */ CcRailDepot,
36 /* 0x0C */ CcPlaceSign,
37 /* 0x0D */ CcPlaySound10,
38 /* 0x0E */ CcPlaySound1D,
39 /* 0x0F */ CcPlaySound1E,
40 /* 0x10 */ CcStation,
41 /* 0x11 */ CcTerraform,
42 /* 0x12 */ CcAI,
43 /* 0x13 */ CcCloneVehicle,
44 /* 0x14 */ CcGiveMoney,
45 /* 0x15 */ CcCreateGroup,
46 /* 0x16 */ CcFoundRandomTown,
47 /* 0x17 */ CcRoadStop,
48 /* 0x18 */ CcBuildIndustry,
49 /* 0x19 */ CcStartStopVehicle,
50 /* 0x1A */ CcGame,
51 /* 0x1B */ CcAddVehicleNewGroup,
54 /**
55 * Append a CommandPacket at the end of the queue.
56 * @param p The packet to append to the queue.
57 * @note A new instance of the CommandPacket will be made.
59 void CommandQueue::Append(CommandPacket *p)
61 CommandPacket *add = MallocT<CommandPacket>(1);
62 *add = *p;
63 add->next = NULL;
64 if (this->first == NULL) {
65 this->first = add;
66 } else {
67 this->last->next = add;
69 this->last = add;
70 this->count++;
73 /**
74 * Return the first item in the queue and remove it from the queue.
75 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
76 * @return the first item in the queue.
78 CommandPacket *CommandQueue::Pop(bool ignore_paused)
80 CommandPacket **prev = &this->first;
81 CommandPacket *ret = this->first;
82 CommandPacket *prev_item = NULL;
83 if (ignore_paused && _pause_mode != PM_UNPAUSED) {
84 while (ret != NULL && !IsCommandAllowedWhilePaused(ret->cmd)) {
85 prev_item = ret;
86 prev = &ret->next;
87 ret = ret->next;
90 if (ret != NULL) {
91 if (ret == this->last) this->last = prev_item;
92 *prev = ret->next;
93 this->count--;
95 return ret;
98 /**
99 * Return the first item in the queue, but don't remove it.
100 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
101 * @return the first item in the queue.
103 CommandPacket *CommandQueue::Peek(bool ignore_paused)
105 if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
107 for (CommandPacket *p = this->first; p != NULL; p = p->next) {
108 if (IsCommandAllowedWhilePaused(p->cmd)) return p;
110 return NULL;
113 /** Free everything that is in the queue. */
114 void CommandQueue::Free()
116 CommandPacket *cp;
117 while ((cp = this->Pop()) != NULL) {
118 free(cp);
120 assert(this->count == 0);
123 /** Local queue of packets waiting for handling. */
124 static CommandQueue _local_wait_queue;
125 /** Local queue of packets waiting for execution. */
126 static CommandQueue _local_execution_queue;
129 * Prepare a DoCommand to be send over the network
130 * @param tile The tile to perform a command on (see #CommandProc)
131 * @param p1 Additional data for the command (see #CommandProc)
132 * @param p2 Additional data for the command (see #CommandProc)
133 * @param cmd The command to execute (a CMD_* value)
134 * @param callback A callback function to call after the command is finished
135 * @param text The text to pass
136 * @param company The company that wants to send the command
138 void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
140 assert((cmd & CMD_FLAGS_MASK) == 0);
142 CommandPacket c;
143 c.company = company;
144 c.tile = tile;
145 c.p1 = p1;
146 c.p2 = p2;
147 c.cmd = cmd;
148 c.callback = callback;
150 strecpy(c.text, (text != NULL) ? text : "", lastof(c.text));
152 if (_network_server) {
153 /* If we are the server, we queue the command in our 'special' queue.
154 * In theory, we could execute the command right away, but then the
155 * client on the server can do everything 1 tick faster than others.
156 * So to keep the game fair, we delay the command with 1 tick
157 * which gives about the same speed as most clients.
159 c.frame = _frame_counter_max + 1;
160 c.my_cmd = true;
162 _local_wait_queue.Append(&c);
163 return;
166 c.frame = 0; // The client can't tell which frame, so just make it 0
168 /* Clients send their command to the server and forget all about the packet */
169 MyClient::SendCommand(&c);
173 * Sync our local command queue to the command queue of the given
174 * socket. This is needed for the case where we receive a command
175 * before saving the game for a joining client, but without the
176 * execution of those commands. Not syncing those commands means
177 * that the client will never get them and as such will be in a
178 * desynced state from the time it started with joining.
179 * @param cs The client to sync the queue to.
181 void NetworkSyncCommandQueue(NetworkClientSocket *cs)
183 for (CommandPacket *p = _local_execution_queue.Peek(); p != NULL; p = p->next) {
184 CommandPacket c = *p;
185 c.callback = 0;
186 cs->outgoing_queue.Append(&c);
191 * Execute all commands on the local command queue that ought to be executed this frame.
193 void NetworkExecuteLocalCommandQueue()
195 assert(IsLocalCompany());
197 CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
199 CommandPacket *cp;
200 while ((cp = queue.Peek()) != NULL) {
201 /* The queue is always in order, which means
202 * that the first element will be executed first. */
203 if (_frame_counter < cp->frame) break;
205 if (_frame_counter > cp->frame) {
206 /* If we reach here, it means for whatever reason, we've already executed
207 * past the command we need to execute. */
208 error("[net] Trying to execute a packet in the past!");
211 /* We can execute this command */
212 _current_company = cp->company;
213 cp->cmd |= CMD_NETWORK_COMMAND;
214 DoCommandP(cp, cp->my_cmd);
216 queue.Pop();
217 free(cp);
220 /* Local company may have changed, so we should not restore the old value */
221 _current_company = _local_company;
225 * Free the local command queues.
227 void NetworkFreeLocalCommandQueue()
229 _local_wait_queue.Free();
230 _local_execution_queue.Free();
234 * "Send" a particular CommandPacket to all clients.
235 * @param cp The command that has to be distributed.
236 * @param owner The client that owns the command,
238 static void DistributeCommandPacket(CommandPacket cp, const NetworkClientSocket *owner)
240 CommandCallback *callback = cp.callback;
241 cp.frame = _frame_counter_max + 1;
243 NetworkClientSocket *cs;
244 FOR_ALL_CLIENT_SOCKETS(cs) {
245 if (cs->status >= NetworkClientSocket::STATUS_MAP) {
246 /* Callbacks are only send back to the client who sent them in the
247 * first place. This filters that out. */
248 cp.callback = (cs != owner) ? NULL : callback;
249 cp.my_cmd = (cs == owner);
250 cs->outgoing_queue.Append(&cp);
254 cp.callback = (cs != owner) ? NULL : callback;
255 cp.my_cmd = (cs == owner);
256 _local_execution_queue.Append(&cp);
260 * "Send" a particular CommandQueue to all clients.
261 * @param queue The queue of commands that has to be distributed.
262 * @param owner The client that owns the commands,
264 static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
266 #ifdef DEBUG_DUMP_COMMANDS
267 /* When replaying we do not want this limitation. */
268 int to_go = UINT16_MAX;
269 #else
270 int to_go = _settings_client.network.commands_per_frame;
271 #endif
273 CommandPacket *cp;
274 while (--to_go >= 0 && (cp = queue->Pop(true)) != NULL) {
275 DistributeCommandPacket(*cp, owner);
276 NetworkAdminCmdLogging(owner, cp);
277 free(cp);
281 /** Distribute the commands of ourself and the clients. */
282 void NetworkDistributeCommands()
284 /* First send the server's commands. */
285 DistributeQueue(&_local_wait_queue, NULL);
287 /* Then send the queues of the others. */
288 NetworkClientSocket *cs;
289 FOR_ALL_CLIENT_SOCKETS(cs) {
290 DistributeQueue(&cs->incoming_queue, cs);
295 * Receives a command from the network.
296 * @param p the packet to read from.
297 * @param cp the struct to write the data to.
298 * @return an error message. When NULL there has been no error.
300 const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
302 cp->company = (CompanyID)p->Recv_uint8();
303 cp->cmd = p->Recv_uint32();
304 if (!IsValidCommand(cp->cmd)) return "invalid command";
305 if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "offline only command";
306 if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
308 cp->p1 = p->Recv_uint32();
309 cp->p2 = p->Recv_uint32();
310 cp->tile = p->Recv_uint32();
311 p->Recv_string(cp->text, lengthof(cp->text), (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
313 byte callback = p->Recv_uint8();
314 if (callback >= lengthof(_callback_table)) return "invalid callback";
316 cp->callback = _callback_table[callback];
317 return NULL;
321 * Sends a command over the network.
322 * @param p the packet to send it in.
323 * @param cp the packet to actually send.
325 void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
327 p->Send_uint8 (cp->company);
328 p->Send_uint32(cp->cmd);
329 p->Send_uint32(cp->p1);
330 p->Send_uint32(cp->p2);
331 p->Send_uint32(cp->tile);
332 p->Send_string(cp->text);
334 byte callback = 0;
335 while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
336 callback++;
339 if (callback == lengthof(_callback_table)) {
340 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
341 callback = 0; // _callback_table[0] == NULL
343 p->Send_uint8 (callback);
346 #endif /* ENABLE_NETWORK */