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