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