28 } |
28 } |
29 |
29 |
30 // Prepare a DoCommand to be send over the network |
30 // Prepare a DoCommand to be send over the network |
31 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
31 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
32 { |
32 { |
33 CommandPacket *c = MallocT<CommandPacket>(1); |
33 CommandPacket c; |
34 byte temp_callback; |
|
35 |
34 |
36 c->player = _local_player; |
35 c.player = _local_player; |
37 c->next = NULL; |
36 c.next = NULL; |
38 c->tile = tile; |
37 c.tile = tile; |
39 c->p1 = p1; |
38 c.p1 = p1; |
40 c->p2 = p2; |
39 c.p2 = p2; |
41 c->cmd = cmd; |
40 c.cmd = cmd; |
42 c->callback = 0; |
|
43 |
41 |
44 temp_callback = 0; |
42 c.callback = 0; |
45 |
43 while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) { |
46 while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) |
44 c.callback++; |
47 temp_callback++; |
|
48 if (temp_callback == _callback_table_count) { |
|
49 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); |
|
50 temp_callback = 0; /* _callback_table[0] == NULL */ |
|
51 } |
45 } |
52 |
46 |
53 if (_network_server) { |
47 if (c.callback == _callback_table_count) { |
54 // We are the server, so set the command to be executed next possible frame |
48 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); |
55 c->frame = _frame_counter_max + 1; |
49 c.callback = 0; // _callback_table[0] == NULL |
56 } else { |
|
57 c->frame = 0; // The client can't tell which frame, so just make it 0 |
|
58 } |
50 } |
59 |
51 |
60 ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); |
52 ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text)); |
61 |
53 |
62 if (_network_server) { |
54 if (_network_server) { |
63 // If we are the server, we queue the command in our 'special' queue. |
55 /* If we are the server, we queue the command in our 'special' queue. |
64 // In theory, we could execute the command right away, but then the |
56 * In theory, we could execute the command right away, but then the |
65 // client on the server can do everything 1 tick faster than others. |
57 * client on the server can do everything 1 tick faster than others. |
66 // So to keep the game fair, we delay the command with 1 tick |
58 * So to keep the game fair, we delay the command with 1 tick |
67 // which gives about the same speed as most clients. |
59 * which gives about the same speed as most clients. |
68 NetworkTCPSocketHandler *cs; |
60 */ |
|
61 c.frame = _frame_counter_max + 1; |
69 |
62 |
70 // And we queue it for delivery to the clients |
63 CommandPacket *new_cp = MallocT<CommandPacket>(1); |
71 FOR_ALL_CLIENTS(cs) { |
64 *new_cp = c; |
72 if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); |
65 if (_local_command_queue == NULL) { |
|
66 _local_command_queue = new_cp; |
|
67 } else { |
|
68 /* Find last packet */ |
|
69 CommandPacket *cp = _local_command_queue; |
|
70 while (cp->next != NULL) cp = cp->next; |
|
71 cp->next = new_cp; |
73 } |
72 } |
74 |
73 |
75 // Only the server gets the callback, because clients should not get them |
74 /* Only the local client (in this case, the server) gets the callback */ |
76 c->callback = temp_callback; |
75 c.callback = 0; |
77 if (_local_command_queue == NULL) { |
76 /* And we queue it for delivery to the clients */ |
78 _local_command_queue = c; |
77 NetworkTCPSocketHandler *cs; |
79 } else { |
78 FOR_ALL_CLIENTS(cs) { |
80 // Find last packet |
79 if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, &c); |
81 CommandPacket *cp = _local_command_queue; |
|
82 while (cp->next != NULL) cp = cp->next; |
|
83 cp->next = c; |
|
84 } |
80 } |
85 |
|
86 return; |
81 return; |
87 } |
82 } |
88 |
83 |
89 // Clients send their command to the server and forget all about the packet |
84 c.frame = 0; // The client can't tell which frame, so just make it 0 |
90 c->callback = temp_callback; |
85 |
91 SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); |
86 /* Clients send their command to the server and forget all about the packet */ |
|
87 SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c); |
92 } |
88 } |
93 |
89 |
94 // Execute a DoCommand we received from the network |
90 // Execute a DoCommand we received from the network |
95 void NetworkExecuteCommand(CommandPacket *cp) |
91 void NetworkExecuteCommand(CommandPacket *cp) |
96 { |
92 { |