b7e64d2951ff722de34ddeac326d8845c6cd84a3
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "util/serialize.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_MEDIA
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         PROTOCOL_VERSION 7:
43                 Add TOCLIENT_ITEMDEF
44                 Obsolete TOCLIENT_TOOLDEF
45                 Obsolete TOCLIENT_CRAFTITEMDEF
46                 Compress the contents of TOCLIENT_ITEMDEF and TOCLIENT_NODEDEF
47         PROTOCOL_VERSION 8:
48                 Digging based on item groups
49                 Many things
50         PROTOCOL_VERSION 9:
51                 ContentFeatures and NodeDefManager use a different serialization
52                     format; better for future version cross-compatibility
53                 Many things
54         PROTOCOL_VERSION 10:
55                 TOCLIENT_PRIVILEGES
56                 Version raised to force 'fly' and 'fast' privileges into effect.
57                 Node metadata change (came in later; somewhat incompatible)
58         PROTOCOL_VERSION 11:
59                 TileDef in ContentFeatures
60                 Nodebox drawtype
61                 (some dev snapshot)
62                 TOCLIENT_INVENTORY_FORMSPEC
63                 (0.4.0, 0.4.1)
64         PROTOCOL_VERSION 12:
65                 TOSERVER_INVENTORY_FIELDS
66                 16-bit node ids
67                 TOCLIENT_DETACHED_INVENTORY
68         PROTOCOL_VERSION 13:
69                 InventoryList field "Width" (deserialization fails with old versions)
70         PROTOCOL_VERSION 14:
71                 Added transfer of player pressed keys to the server
72                 Added new messages for mesh and bone animation, as well as attachments
73                 GENERIC_CMD_SET_ANIMATION
74                 GENERIC_CMD_SET_BONE_POSITION
75                 GENERIC_CMD_SET_ATTACHMENT
76 */
77
78 // Server always only supports one version
79 #define SERVER_PROTOCOL_VERSION 14
80
81 // Client can support older versions too
82 #define CLIENT_PROTOCOL_VERSION_MIN 13
83 #define CLIENT_PROTOCOL_VERSION_MAX SERVER_PROTOCOL_VERSION
84
85 // Constant that differentiates the protocol from random data and other protocols
86 #define PROTOCOL_ID 0x4f457403
87
88 #define PASSWORD_SIZE 28       // Maximum password length. Allows for
89                                // base64-encoded SHA-1 (27+\0).
90
91 #define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_."
92
93 enum ToClientCommand
94 {
95         TOCLIENT_INIT = 0x10,
96         /*
97                 Server's reply to TOSERVER_INIT.
98                 Sent second after connected.
99
100                 [0] u16 TOSERVER_INIT
101                 [2] u8 deployed version
102                 [3] v3s16 player's position + v3f(0,BS/2,0) floatToInt'd 
103                 [12] u64 map seed (new as of 2011-02-27)
104
105                 NOTE: The position in here is deprecated; position is
106                       explicitly sent afterwards
107         */
108
109         TOCLIENT_BLOCKDATA = 0x20, //TODO: Multiple blocks
110         TOCLIENT_ADDNODE = 0x21,
111         TOCLIENT_REMOVENODE = 0x22,
112         
113         TOCLIENT_PLAYERPOS = 0x23, // Obsolete
114         /*
115                 [0] u16 command
116                 // Followed by an arbitary number of these:
117                 // Number is determined from packet length.
118                 [N] u16 peer_id
119                 [N+2] v3s32 position*100
120                 [N+2+12] v3s32 speed*100
121                 [N+2+12+12] s32 pitch*100
122                 [N+2+12+12+4] s32 yaw*100
123         */
124
125         TOCLIENT_PLAYERINFO = 0x24, // Obsolete
126         /*
127                 [0] u16 command
128                 // Followed by an arbitary number of these:
129                 // Number is determined from packet length.
130                 [N] u16 peer_id
131                 [N] char[20] name
132         */
133         
134         TOCLIENT_OPT_BLOCK_NOT_FOUND = 0x25, // Obsolete
135
136         TOCLIENT_SECTORMETA = 0x26, // Obsolete
137         /*
138                 [0] u16 command
139                 [2] u8 sector count
140                 [3...] v2s16 pos + sector metadata
141         */
142
143         TOCLIENT_INVENTORY = 0x27,
144         /*
145                 [0] u16 command
146                 [2] serialized inventory
147         */
148         
149         TOCLIENT_OBJECTDATA = 0x28, // Obsolete
150         /*
151                 Sent as unreliable.
152
153                 u16 command
154                 u16 number of player positions
155                 for each player:
156                         u16 peer_id
157                         v3s32 position*100
158                         v3s32 speed*100
159                         s32 pitch*100
160                         s32 yaw*100
161                 u16 count of blocks
162                 for each block:
163                         v3s16 blockpos
164                         block objects
165         */
166
167         TOCLIENT_TIME_OF_DAY = 0x29,
168         /*
169                 u16 command
170                 u16 time (0-23999)
171                 Added in a later version:
172                 f1000 time_speed
173         */
174
175         // (oops, there is some gap here)
176
177         TOCLIENT_CHAT_MESSAGE = 0x30,
178         /*
179                 u16 command
180                 u16 length
181                 wstring message
182         */
183
184         TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD = 0x31,
185         /*
186                 u16 command
187                 u16 count of removed objects
188                 for all removed objects {
189                         u16 id
190                 }
191                 u16 count of added objects
192                 for all added objects {
193                         u16 id
194                         u8 type
195                         u32 initialization data length
196                         string initialization data
197                 }
198         */
199         
200         TOCLIENT_ACTIVE_OBJECT_MESSAGES = 0x32,
201         /*
202                 u16 command
203                 for all objects
204                 {
205                         u16 id
206                         u16 message length
207                         string message
208                 }
209         */
210
211         TOCLIENT_HP = 0x33,
212         /*
213                 u16 command
214                 u8 hp
215         */
216
217         TOCLIENT_MOVE_PLAYER = 0x34,
218         /*
219                 u16 command
220                 v3f1000 player position
221                 f1000 player pitch
222                 f1000 player yaw
223         */
224
225         TOCLIENT_ACCESS_DENIED = 0x35,
226         /*
227                 u16 command
228                 u16 reason_length
229                 wstring reason
230         */
231
232         TOCLIENT_PLAYERITEM = 0x36, // Obsolete
233         /*
234                 u16 command
235                 u16 count of player items
236                 for all player items {
237                         u16 peer id
238                         u16 length of serialized item
239                         string serialized item
240                 }
241         */
242
243         TOCLIENT_DEATHSCREEN = 0x37,
244         /*
245                 u16 command
246                 u8 bool set camera point target
247                 v3f1000 camera point target (to point the death cause or whatever)
248         */
249
250         TOCLIENT_MEDIA = 0x38,
251         /*
252                 u16 command
253                 u16 total number of texture bunches
254                 u16 index of this bunch
255                 u32 number of files in this bunch
256                 for each file {
257                         u16 length of name
258                         string name
259                         u32 length of data
260                         data
261                 }
262         */
263         
264         TOCLIENT_TOOLDEF = 0x39,
265         /*
266                 u16 command
267                 u32 length of the next item
268                 serialized ToolDefManager
269         */
270         
271         TOCLIENT_NODEDEF = 0x3a,
272         /*
273                 u16 command
274                 u32 length of the next item
275                 serialized NodeDefManager
276         */
277         
278         TOCLIENT_CRAFTITEMDEF = 0x3b,
279         /*
280                 u16 command
281                 u32 length of the next item
282                 serialized CraftiItemDefManager
283         */
284
285         TOCLIENT_ANNOUNCE_MEDIA = 0x3c,
286
287         /*
288                 u16 command
289                 u32 number of files
290                 for each texture {
291                         u16 length of name
292                         string name
293                         u16 length of sha1_digest
294                         string sha1_digest
295                 }
296         */
297
298         TOCLIENT_ITEMDEF = 0x3d,
299         /*
300                 u16 command
301                 u32 length of next item
302                 serialized ItemDefManager
303         */
304         
305         TOCLIENT_PLAY_SOUND = 0x3f,
306         /*
307                 u16 command
308                 s32 sound_id
309                 u16 len
310                 u8[len] sound name
311                 s32 gain*1000
312                 u8 type (0=local, 1=positional, 2=object)
313                 s32[3] pos_nodes*10000
314                 u16 object_id
315                 u8 loop (bool)
316         */
317
318         TOCLIENT_STOP_SOUND = 0x40,
319         /*
320                 u16 command
321                 s32 sound_id
322         */
323
324         TOCLIENT_PRIVILEGES = 0x41,
325         /*
326                 u16 command
327                 u16 number of privileges
328                 for each privilege
329                         u16 len
330                         u8[len] privilege
331         */
332
333         TOCLIENT_INVENTORY_FORMSPEC = 0x42,
334         /*
335                 u16 command
336                 u32 len
337                 u8[len] formspec
338         */
339
340         TOCLIENT_DETACHED_INVENTORY = 0x43,
341         /*
342                 [0] u16 command
343                 u16 len
344                 u8[len] name
345                 [2] serialized inventory
346         */
347 };
348
349 enum ToServerCommand
350 {
351         TOSERVER_INIT=0x10,
352         /*
353                 Sent first after connected.
354
355                 [0] u16 TOSERVER_INIT
356                 [2] u8 SER_FMT_VER_HIGHEST
357                 [3] u8[20] player_name
358                 [23] u8[28] password (new in some version)
359                 [51] u16 minimum supported network protocol version (added sometime)
360                 [53] u16 maximum supported network protocol version (added later than the previous one)
361         */
362
363         TOSERVER_INIT2 = 0x11,
364         /*
365                 Sent as an ACK for TOCLIENT_INIT.
366                 After this, the server can send data.
367
368                 [0] u16 TOSERVER_INIT2
369         */
370
371         TOSERVER_GETBLOCK=0x20, // Obsolete
372         TOSERVER_ADDNODE = 0x21, // Obsolete
373         TOSERVER_REMOVENODE = 0x22, // Obsolete
374
375         TOSERVER_PLAYERPOS = 0x23,
376         /*
377                 [0] u16 command
378                 [2] v3s32 position*100
379                 [2+12] v3s32 speed*100
380                 [2+12+12] s32 pitch*100
381                 [2+12+12+4] s32 yaw*100
382                 [2+12+12+4+4] u32 keyPressed
383         */
384
385         TOSERVER_GOTBLOCKS = 0x24,
386         /*
387                 [0] u16 command
388                 [2] u8 count
389                 [3] v3s16 pos_0
390                 [3+6] v3s16 pos_1
391                 ...
392         */
393
394         TOSERVER_DELETEDBLOCKS = 0x25,
395         /*
396                 [0] u16 command
397                 [2] u8 count
398                 [3] v3s16 pos_0
399                 [3+6] v3s16 pos_1
400                 ...
401         */
402
403         TOSERVER_ADDNODE_FROM_INVENTORY = 0x26, // Obsolete
404         /*
405                 [0] u16 command
406                 [2] v3s16 pos
407                 [8] u16 i
408         */
409
410         TOSERVER_CLICK_OBJECT = 0x27, // Obsolete
411         /*
412                 length: 13
413                 [0] u16 command
414                 [2] u8 button (0=left, 1=right)
415                 [3] v3s16 blockpos
416                 [9] s16 id
417                 [11] u16 item
418         */
419
420         TOSERVER_GROUND_ACTION = 0x28, // Obsolete
421         /*
422                 length: 17
423                 [0] u16 command
424                 [2] u8 action
425                 [3] v3s16 nodepos_undersurface
426                 [9] v3s16 nodepos_abovesurface
427                 [15] u16 item
428                 actions:
429                 0: start digging (from undersurface)
430                 1: place block (to abovesurface)
431                 2: stop digging (all parameters ignored)
432                 3: digging completed
433         */
434         
435         TOSERVER_RELEASE = 0x29, // Obsolete
436
437         // (oops, there is some gap here)
438
439         TOSERVER_SIGNTEXT = 0x30, // Old signs, obsolete
440         /*
441                 u16 command
442                 v3s16 blockpos
443                 s16 id
444                 u16 textlen
445                 textdata
446         */
447
448         TOSERVER_INVENTORY_ACTION = 0x31,
449         /*
450                 See InventoryAction in inventory.h
451         */
452
453         TOSERVER_CHAT_MESSAGE = 0x32,
454         /*
455                 u16 command
456                 u16 length
457                 wstring message
458         */
459
460         TOSERVER_SIGNNODETEXT = 0x33, // obsolete
461         /*
462                 u16 command
463                 v3s16 p
464                 u16 textlen
465                 textdata
466         */
467
468         TOSERVER_CLICK_ACTIVEOBJECT = 0x34, // Obsolete
469         /*
470                 length: 7
471                 [0] u16 command
472                 [2] u8 button (0=left, 1=right)
473                 [3] u16 id
474                 [5] u16 item
475         */
476         
477         TOSERVER_DAMAGE = 0x35,
478         /*
479                 u16 command
480                 u8 amount
481         */
482
483         TOSERVER_PASSWORD=0x36,
484         /*
485                 Sent to change password.
486
487                 [0] u16 TOSERVER_PASSWORD
488                 [2] u8[28] old password
489                 [30] u8[28] new password
490         */
491
492         TOSERVER_PLAYERITEM=0x37,
493         /*
494                 Sent to change selected item.
495
496                 [0] u16 TOSERVER_PLAYERITEM
497                 [2] u16 item
498         */
499         
500         TOSERVER_RESPAWN=0x38,
501         /*
502                 u16 TOSERVER_RESPAWN
503         */
504
505         TOSERVER_INTERACT = 0x39,
506         /*
507                 [0] u16 command
508                 [2] u8 action
509                 [3] u16 item
510                 [5] u32 length of the next item
511                 [9] serialized PointedThing
512                 actions:
513                 0: start digging (from undersurface) or use
514                 1: stop digging (all parameters ignored)
515                 2: digging completed
516                 3: place block or item (to abovesurface)
517                 4: use item
518
519                 (Obsoletes TOSERVER_GROUND_ACTION and TOSERVER_CLICK_ACTIVEOBJECT.)
520         */
521         
522         TOSERVER_REMOVED_SOUNDS = 0x3a,
523         /*
524                 u16 command
525                 u16 len
526                 s32[len] sound_id
527         */
528
529         TOSERVER_NODEMETA_FIELDS = 0x3b,
530         /*
531                 u16 command
532                 v3s16 p
533                 u16 len
534                 u8[len] form name (reserved for future use)
535                 u16 number of fields
536                 for each field:
537                         u16 len
538                         u8[len] field name
539                         u32 len
540                         u8[len] field value
541         */
542
543         TOSERVER_INVENTORY_FIELDS = 0x3c,
544         /*
545                 u16 command
546                 u16 len
547                 u8[len] form name (reserved for future use)
548                 u16 number of fields
549                 for each field:
550                         u16 len
551                         u8[len] field name
552                         u32 len
553                         u8[len] field value
554         */
555
556         TOSERVER_REQUEST_MEDIA = 0x40,
557         /*
558                 u16 command
559                 u16 number of files requested
560                 for each file {
561                         u16 length of name
562                         string name
563                 }
564          */
565
566 };
567
568 inline SharedBuffer<u8> makePacket_TOCLIENT_TIME_OF_DAY(u16 time, float time_speed)
569 {
570         SharedBuffer<u8> data(2+2+4);
571         writeU16(&data[0], TOCLIENT_TIME_OF_DAY);
572         writeU16(&data[2], time);
573         writeF1000(&data[4], time_speed);
574         return data;
575 }
576
577 #endif
578