43de689e4af4f5f962ed2e0dd483feb401918af5
[oweals/minetest.git] / src / clientserver.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CLIENTSERVER_HEADER
21 #define CLIENTSERVER_HEADER
22
23 #include "utility.h"
24
25 /*
26         changes by PROTOCOL_VERSION:
27
28         PROTOCOL_VERSION 3:
29                 Base for writing changes here
30         PROTOCOL_VERSION 4:
31                 Add TOCLIENT_TEXTURES
32                 Add TOCLIENT_TOOLDEF
33                 Add TOCLIENT_NODEDEF
34                 Add TOCLIENT_CRAFTITEMDEF
35                 Add TOSERVER_INTERACT
36                 Obsolete TOSERVER_CLICK_ACTIVEOBJECT
37                 Obsolete TOSERVER_GROUND_ACTION
38         PROTOCOL_VERSION 5:
39                 Make players to be handled mostly as ActiveObjects
40         PROTOCOL_VERSION 6:
41                 Only non-cached textures are sent
42 */
43
44 #define PROTOCOL_VERSION 6
45
46 #define PROTOCOL_ID 0x4f457403
47
48 #define PASSWORD_SIZE 28       // Maximum password length. Allows for
49                                // base64-encoded SHA-1 (27+\0).
50
51 #define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_."
52
53 enum ToClientCommand
54 {
55         TOCLIENT_INIT = 0x10,
56         /*
57                 Server's reply to TOSERVER_INIT.
58                 Sent second after connected.
59
60                 [0] u16 TOSERVER_INIT
61                 [2] u8 deployed version
62                 [3] v3s16 player's position + v3f(0,BS/2,0) floatToInt'd 
63                 [12] u64 map seed (new as of 2011-02-27)
64
65                 NOTE: The position in here is deprecated; position is
66                       explicitly sent afterwards
67         */
68
69         TOCLIENT_BLOCKDATA = 0x20, //TODO: Multiple blocks
70         TOCLIENT_ADDNODE = 0x21,
71         TOCLIENT_REMOVENODE = 0x22,
72         
73         TOCLIENT_PLAYERPOS = 0x23, // Obsolete
74         /*
75                 [0] u16 command
76                 // Followed by an arbitary number of these:
77                 // Number is determined from packet length.
78                 [N] u16 peer_id
79                 [N+2] v3s32 position*100
80                 [N+2+12] v3s32 speed*100
81                 [N+2+12+12] s32 pitch*100
82                 [N+2+12+12+4] s32 yaw*100
83         */
84
85         TOCLIENT_PLAYERINFO = 0x24, // Obsolete
86         /*
87                 [0] u16 command
88                 // Followed by an arbitary number of these:
89                 // Number is determined from packet length.
90                 [N] u16 peer_id
91                 [N] char[20] name
92         */
93         
94         TOCLIENT_OPT_BLOCK_NOT_FOUND = 0x25, // Obsolete
95
96         TOCLIENT_SECTORMETA = 0x26, // Obsolete
97         /*
98                 [0] u16 command
99                 [2] u8 sector count
100                 [3...] v2s16 pos + sector metadata
101         */
102
103         TOCLIENT_INVENTORY = 0x27,
104         /*
105                 [0] u16 command
106                 [2] serialized inventory
107         */
108         
109         TOCLIENT_OBJECTDATA = 0x28, // Obsolete
110         /*
111                 Sent as unreliable.
112
113                 u16 command
114                 u16 number of player positions
115                 for each player:
116                         u16 peer_id
117                         v3s32 position*100
118                         v3s32 speed*100
119                         s32 pitch*100
120                         s32 yaw*100
121                 u16 count of blocks
122                 for each block:
123                         v3s16 blockpos
124                         block objects
125         */
126
127         TOCLIENT_TIME_OF_DAY = 0x29,
128         /*
129                 u16 command
130                 u16 time (0-23999)
131         */
132
133         // (oops, there is some gap here)
134
135         TOCLIENT_CHAT_MESSAGE = 0x30,
136         /*
137                 u16 command
138                 u16 length
139                 wstring message
140         */
141
142         TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD = 0x31,
143         /*
144                 u16 command
145                 u16 count of removed objects
146                 for all removed objects {
147                         u16 id
148                 }
149                 u16 count of added objects
150                 for all added objects {
151                         u16 id
152                         u8 type
153                         u32 initialization data length
154                         string initialization data
155                 }
156         */
157         
158         TOCLIENT_ACTIVE_OBJECT_MESSAGES = 0x32,
159         /*
160                 u16 command
161                 for all objects
162                 {
163                         u16 id
164                         u16 message length
165                         string message
166                 }
167         */
168
169         TOCLIENT_HP = 0x33,
170         /*
171                 u16 command
172                 u8 hp
173         */
174
175         TOCLIENT_MOVE_PLAYER = 0x34,
176         /*
177                 u16 command
178                 v3f1000 player position
179                 f1000 player pitch
180                 f1000 player yaw
181         */
182
183         TOCLIENT_ACCESS_DENIED = 0x35,
184         /*
185                 u16 command
186                 u16 reason_length
187                 wstring reason
188         */
189
190         TOCLIENT_PLAYERITEM = 0x36,
191         /*
192                 u16 command
193                 u16 count of player items
194                 for all player items {
195                         u16 peer id
196                         u16 length of serialized item
197                         string serialized item
198                 }
199         */
200
201         TOCLIENT_DEATHSCREEN = 0x37,
202         /*
203                 u16 command
204                 u8 bool set camera point target
205                 v3f1000 camera point target (to point the death cause or whatever)
206         */
207
208         TOCLIENT_TEXTURES = 0x38,
209         /*
210                 u16 command
211                 u16 total number of texture bunches
212                 u16 index of this bunch
213                 u32 number of textures in this bunch
214                 for each texture {
215                         u16 length of name
216                         string name
217                         u32 length of data
218                         data
219                 }
220         */
221         
222         TOCLIENT_TOOLDEF = 0x39,
223         /*
224                 u16 command
225                 u32 length of the next item
226                 serialized ToolDefManager
227         */
228         
229         TOCLIENT_NODEDEF = 0x3a,
230         /*
231                 u16 command
232                 u32 length of the next item
233                 serialized NodeDefManager
234         */
235         
236         TOCLIENT_CRAFTITEMDEF = 0x3b,
237         /*
238                 u16 command
239                 u32 length of the next item
240                 serialized CraftiItemDefManager
241         */
242
243         TOCLIENT_ANNOUNCE_TEXTURES = 0x3c,
244
245         /*
246                 u16 command
247                 u32 number of textures
248                 for each texture {
249                         u16 length of name
250                         string name
251                         u16 length of sha1_digest
252                         string sha1_digest
253                 }
254         */
255 };
256
257 enum ToServerCommand
258 {
259         TOSERVER_INIT=0x10,
260         /*
261                 Sent first after connected.
262
263                 [0] u16 TOSERVER_INIT
264                 [2] u8 SER_FMT_VER_HIGHEST
265                 [3] u8[20] player_name
266                 [23] u8[28] password (new in some version)
267                 [51] u16 client network protocol version (new in some version)
268         */
269
270         TOSERVER_INIT2 = 0x11,
271         /*
272                 Sent as an ACK for TOCLIENT_INIT.
273                 After this, the server can send data.
274
275                 [0] u16 TOSERVER_INIT2
276         */
277
278         TOSERVER_GETBLOCK=0x20, // Obsolete
279         TOSERVER_ADDNODE = 0x21, // Obsolete
280         TOSERVER_REMOVENODE = 0x22, // Obsolete
281
282         TOSERVER_PLAYERPOS = 0x23,
283         /*
284                 [0] u16 command
285                 [2] v3s32 position*100
286                 [2+12] v3s32 speed*100
287                 [2+12+12] s32 pitch*100
288                 [2+12+12+4] s32 yaw*100
289         */
290
291         TOSERVER_GOTBLOCKS = 0x24,
292         /*
293                 [0] u16 command
294                 [2] u8 count
295                 [3] v3s16 pos_0
296                 [3+6] v3s16 pos_1
297                 ...
298         */
299
300         TOSERVER_DELETEDBLOCKS = 0x25,
301         /*
302                 [0] u16 command
303                 [2] u8 count
304                 [3] v3s16 pos_0
305                 [3+6] v3s16 pos_1
306                 ...
307         */
308
309         TOSERVER_ADDNODE_FROM_INVENTORY = 0x26, // Obsolete
310         /*
311                 [0] u16 command
312                 [2] v3s16 pos
313                 [8] u16 i
314         */
315
316         TOSERVER_CLICK_OBJECT = 0x27, // Obsolete
317         /*
318                 length: 13
319                 [0] u16 command
320                 [2] u8 button (0=left, 1=right)
321                 [3] v3s16 blockpos
322                 [9] s16 id
323                 [11] u16 item
324         */
325
326         TOSERVER_GROUND_ACTION = 0x28, // Obsolete
327         /*
328                 length: 17
329                 [0] u16 command
330                 [2] u8 action
331                 [3] v3s16 nodepos_undersurface
332                 [9] v3s16 nodepos_abovesurface
333                 [15] u16 item
334                 actions:
335                 0: start digging (from undersurface)
336                 1: place block (to abovesurface)
337                 2: stop digging (all parameters ignored)
338                 3: digging completed
339         */
340         
341         TOSERVER_RELEASE = 0x29, // Obsolete
342
343         // (oops, there is some gap here)
344
345         TOSERVER_SIGNTEXT = 0x30, // Old signs, obsolete
346         /*
347                 u16 command
348                 v3s16 blockpos
349                 s16 id
350                 u16 textlen
351                 textdata
352         */
353
354         TOSERVER_INVENTORY_ACTION = 0x31,
355         /*
356                 See InventoryAction in inventory.h
357         */
358
359         TOSERVER_CHAT_MESSAGE = 0x32,
360         /*
361                 u16 command
362                 u16 length
363                 wstring message
364         */
365
366         TOSERVER_SIGNNODETEXT = 0x33,
367         /*
368                 u16 command
369                 v3s16 p
370                 u16 textlen
371                 textdata
372         */
373
374         TOSERVER_CLICK_ACTIVEOBJECT = 0x34, // Obsolete
375         /*
376                 length: 7
377                 [0] u16 command
378                 [2] u8 button (0=left, 1=right)
379                 [3] u16 id
380                 [5] u16 item
381         */
382         
383         TOSERVER_DAMAGE = 0x35,
384         /*
385                 u16 command
386                 u8 amount
387         */
388
389         TOSERVER_PASSWORD=0x36,
390         /*
391                 Sent to change password.
392
393                 [0] u16 TOSERVER_PASSWORD
394                 [2] u8[28] old password
395                 [30] u8[28] new password
396         */
397
398         TOSERVER_PLAYERITEM=0x37,
399         /*
400                 Sent to change selected item.
401
402                 [0] u16 TOSERVER_PLAYERITEM
403                 [2] u16 item
404         */
405         
406         TOSERVER_RESPAWN=0x38,
407         /*
408                 u16 TOSERVER_RESPAWN
409         */
410
411         TOSERVER_INTERACT = 0x39,
412         /*
413                 [0] u16 command
414                 [2] u8 action
415                 [3] u16 item
416                 [5] u32 length of the next item
417                 [9] serialized PointedThing
418                 actions:
419                 0: start digging (from undersurface) or use
420                 1: stop digging (all parameters ignored)
421                 2: digging completed
422                 3: place block or item (to abovesurface)
423                 4: use item
424
425                 (Obsoletes TOSERVER_GROUND_ACTION and TOSERVER_CLICK_ACTIVEOBJECT.)
426         */
427         
428         TOSERVER_REQUEST_TEXTURES = 0x40,
429
430         /*
431                         u16 command
432                         u16 number of textures requested
433                         for each texture {
434                                 u16 length of name
435                                 string name
436                         }
437          */
438
439 };
440
441 inline SharedBuffer<u8> makePacket_TOCLIENT_TIME_OF_DAY(u16 time)
442 {
443         SharedBuffer<u8> data(2+2);
444         writeU16(&data[0], TOCLIENT_TIME_OF_DAY);
445         writeU16(&data[2], time);
446         return data;
447 }
448
449 #endif
450