]> git.lizzy.rs Git - mt.git/blob - tocltcmds.go
fix errors in usage of zstd
[mt.git] / tocltcmds.go
1 package mt
2
3 import (
4         "crypto/sha1"
5         "fmt"
6         "image/color"
7         "io"
8         "math"
9
10         "github.com/anon55555/mt/rudp"
11 )
12
13 type ToCltCmd interface {
14         Cmd
15         toCltCmdNo() uint16
16 }
17
18 //go:generate ./cmdno.sh tocltcmds ToClt toClt uint16 Cmd newToCltCmd
19
20 // ToCltHello is sent as a response to ToSrvInit.
21 // The client responds to ToCltHello by authenticating.
22 type ToCltHello struct {
23         SerializeVer uint8
24         Compression  CompressionModes
25         ProtoVer     uint16
26         AuthMethods
27         Username string
28 }
29
30 // ToCltAcceptAuth is sent after the client successfully authenticates.
31 // The client responds to ToCltAcceptAuth with ToSrvInit2.
32 type ToCltAcceptAuth struct {
33         // The client does the equivalent of
34         //      PlayerPos[1] -= 5
35         // before using PlayerPos.
36         PlayerPos Pos
37
38         MapSeed         uint64
39         SendInterval    float32
40         SudoAuthMethods AuthMethods
41 }
42
43 type ToCltAcceptSudoMode struct {
44         SudoAuthMethods AuthMethods
45         //mt:const [15]byte{}
46 }
47
48 type ToCltDenySudoMode struct{}
49
50 // ToCltKick tells that the client that it has been kicked by the server.
51 type ToCltKick struct {
52         Reason KickReason
53         //mt:assert %s.Reason < maxKickReason
54
55         //mt:if dr := %s.Reason; dr == Custom || dr == Shutdown || dr == Crash
56         Custom string
57         //mt:end
58
59         //mt:if dr := %s.Reason; dr == Shutdown || dr == Crash
60         Reconnect bool
61         //mt:end
62 }
63
64 // A KickReason is the reason a ToCltKick has been sent.
65 type KickReason uint8
66
67 const (
68         WrongPasswd KickReason = iota
69         UnexpectedData
70         SrvIsSingleplayer
71         UnsupportedVer
72         BadNameChars
73         BadName
74         TooManyClts
75         EmptyPasswd
76         AlreadyConnected
77         SrvErr
78         Custom
79         Shutdown
80         Crash
81         maxKickReason
82 )
83
84 func (cmd ToCltKick) String() (msg string) {
85         switch cmd.Reason {
86         case WrongPasswd:
87                 return "wrong password"
88         case UnexpectedData:
89                 return "unexpected data"
90         case SrvIsSingleplayer:
91                 return "server is singleplayer"
92         case UnsupportedVer:
93                 return "unsupported client version"
94         case BadNameChars:
95                 return "disallowed character(s) in player name"
96         case BadName:
97                 return "disallowed player name"
98         case TooManyClts:
99                 return "too many clients"
100         case EmptyPasswd:
101                 return "empty password"
102         case AlreadyConnected:
103                 return "another client is already connected with the same name"
104         case SrvErr:
105                 return "server error"
106         case Custom:
107                 return cmd.Custom
108         case Shutdown:
109                 msg = "server shutdown"
110         case Crash:
111                 msg = "server crash"
112         default:
113                 msg = fmt.Sprintf("KickReason(%d)", cmd.Reason)
114         }
115
116         if cmd.Custom != "" {
117                 msg += ": " + cmd.Custom
118         }
119
120         return
121 }
122
123 // ToCltBlkData tells the client the contents of a nearby MapBlk.
124 type ToCltBlkData struct {
125         Blkpos [3]int16
126         Blk    MapBlk
127 }
128
129 // ToCltAddNode tells the client that a nearby node changed
130 // to something other than air.
131 type ToCltAddNode struct {
132         Pos [3]int16
133         Node
134         KeepMeta bool
135 }
136
137 // ToCltRemoveNode tells the client that a nearby node changed to air.
138 type ToCltRemoveNode struct {
139         Pos [3]int16
140 }
141
142 // ToCltInv updates the client's inventory.
143 type ToCltInv struct {
144         //mt:raw
145         Inv string
146 }
147
148 // ToCltTimeOfDay updates the client's in-game time of day.
149 type ToCltTimeOfDay struct {
150         Time  uint16  // %24000
151         Speed float32 // Speed times faster than real time
152 }
153
154 // ToCltCSMRestrictionFlags tells the client how use of CSMs should be restricted.
155 type ToCltCSMRestrictionFlags struct {
156         Flags CSMRestrictionFlags
157
158         // MapRange is the maximum distance from the player CSMs can read the map
159         // if Flags&LimitMapRange != 0.
160         MapRange uint32
161 }
162
163 type CSMRestrictionFlags uint64
164
165 const (
166         NoCSMs CSMRestrictionFlags = 1 << iota
167         NoChatMsgs
168         NoItemDefs
169         NoNodeDefs
170         LimitMapRange
171         NoPlayerList
172 )
173
174 // ToCltAddPlayerVel tells the client to add Vel to the player's velocity.
175 type ToCltAddPlayerVel struct {
176         Vel Vec
177 }
178
179 // ToCltMediaPush is sent when a media file is dynamically added.
180 type ToCltMediaPush struct {
181         //mt:const uint16(sha1.Size)
182         SHA1        [sha1.Size]byte
183         Filename    string
184         ShouldCache bool
185
186         //mt:len32
187         Data []byte
188 }
189
190 // ToCltChatMsg tells the client that is has received a chat message.
191 type ToCltChatMsg struct {
192         //mt:const uint8(1)
193
194         Type ChatMsgType
195
196         //mt:utf16
197         Sender, Text string
198
199         Timestamp int64 // Unix time.
200 }
201
202 type ChatMsgType uint8
203
204 const (
205         RawMsg      ChatMsgType = iota // raw
206         NormalMsg                      // normal
207         AnnounceMsg                    // announce
208         SysMsg                         // sys
209         maxMsg
210 )
211
212 //go:generate stringer -linecomment -type ChatMsgType
213
214 // ToCltAORmAdd tells the client that AOs have been removed from and/or added to
215 // the AOs that it can see.
216 type ToCltAORmAdd struct {
217         Remove []AOID
218         Add    []AOAdd
219 }
220
221 type AOAdd struct {
222         ID AOID
223         //mt:const genericCAO
224         //mt:lenhdr 32
225         InitData AOInitData
226         //mt:end
227 }
228
229 // ToCltAOMsgs updates the client about nearby AOs.
230 type ToCltAOMsgs struct {
231         //mt:raw
232         Msgs []IDAOMsg
233 }
234
235 // ToCltHP updates the player's HP on the client.
236 type ToCltHP struct {
237         HP uint16
238 }
239
240 // ToCltMovePlayer tells the client that the player has been moved server-side.
241 type ToCltMovePlayer struct {
242         Pos
243         Pitch, Yaw float32
244 }
245
246 type ToCltLegacyKick struct {
247         //mt:utf16
248         Reason string
249 }
250
251 // ToCltFOV tells the client to change its FOV.
252 type ToCltFOV struct {
253         FOV            float32
254         Multiplier     bool
255         TransitionTime float32
256 }
257
258 // ToCltDeathScreen tells the client to show the death screen.
259 type ToCltDeathScreen struct {
260         PointCam bool
261         PointAt  Pos
262 }
263
264 // ToCltMedia responds to a ToSrvMedia packet with the requested media files.
265 type ToCltMedia struct {
266         // N is the total number of ToCltMedia packets.
267         // I is the index of this packet.
268         N, I uint16
269
270         //mt:len32
271         Files []struct {
272                 Name string
273
274                 //mt:len32
275                 Data []byte
276         }
277 }
278
279 // ToCltNodeDefs tells the client the definitions of nodes.
280 type ToCltNodeDefs struct {
281         //mt:lenhdr 32
282         //mt:zlib
283
284         // Version.
285         //mt:const uint8(1)
286
287         // See (de)serialize.fmt.
288         Defs []NodeDef
289
290         //mt:end
291         //mt:end
292 }
293
294 // ToCltAnnounceMedia tells the client what media is available on request.
295 // See ToSrvReqMedia.
296 type ToCltAnnounceMedia struct {
297         Files []struct {
298                 Name       string
299                 Base64SHA1 string
300         }
301         URL string
302 }
303
304 // ToCltItemDefs tells the client the definitions of items.
305 type ToCltItemDefs struct {
306         //mt:lenhdr 32
307         //mt:zlib
308
309         //mt:const uint8(0)
310
311         Defs    []ItemDef
312         Aliases []struct{ Alias, Orig string }
313
314         //mt:end
315         //mt:end
316 }
317
318 // ToCltPlaySound tells the client to play a sound.
319 type ToCltPlaySound struct {
320         ID      SoundID
321         Name    string
322         Gain    float32
323         SrcType SoundSrcType
324         Pos
325         SrcAOID   AOID
326         Loop      bool
327         Fade      float32
328         Pitch     float32
329         Ephemeral bool
330 }
331
332 // ToCltStopSound tells the client to stop playing a sound.
333 type ToCltStopSound struct {
334         ID SoundID
335 }
336
337 // ToCltPrivs tells the client its privs.
338 type ToCltPrivs struct {
339         Privs []string
340 }
341
342 // ToCltInvFormspec tells the client its inventory formspec.
343 type ToCltInvFormspec struct {
344         //mt:len32
345         Formspec string
346 }
347
348 // ToCltDetachedInv updates a detached inventory on the client.
349 type ToCltDetachedInv struct {
350         Name string
351         Keep bool
352         Len  uint16 // deprecated
353
354         //mt:raw
355         Inv string
356 }
357
358 // ToCltShowFormspec tells the client to show a formspec.
359 type ToCltShowFormspec struct {
360         //mt:len32
361         Formspec string
362
363         Formname string
364 }
365
366 // ToCltMovement tells the client how to move.
367 type ToCltMovement struct {
368         DefaultAccel, AirAccel, FastAccel,
369         WalkSpeed, CrouchSpeed, FastSpeed, ClimbSpeed, JumpSpeed,
370         Fluidity, Smoothing, Sink, // liquids
371         Gravity float32
372 }
373
374 // ToCltSpawnParticle tells the client to spawn a particle.
375 type ToCltSpawnParticle struct {
376         Pos, Vel, Acc  [3]float32
377         ExpirationTime float32 // in seconds.
378         Size           float32
379         Collide        bool
380
381         //mt:len32
382         Texture
383
384         Vertical    bool
385         CollisionRm bool
386         AnimParams  TileAnim
387         Glow        uint8
388         AOCollision bool
389         NodeParam0  Content
390         NodeParam2  uint8
391         NodeTile    uint8
392 }
393
394 type ParticleSpawnerID uint32
395
396 // ToCltAddParticleSpawner tells the client to add a particle spawner.
397 type ToCltAddParticleSpawner struct {
398         Amount         uint16
399         Duration       float32
400         Pos, Vel, Acc  [2][3]float32
401         ExpirationTime [2]float32 // in seconds.
402         Size           [2]float32
403         Collide        bool
404
405         //mt:len32
406         Texture
407
408         ID           ParticleSpawnerID
409         Vertical     bool
410         CollisionRm  bool
411         AttachedAOID AOID
412         AnimParams   TileAnim
413         Glow         uint8
414         AOCollision  bool
415         NodeParam0   Content
416         NodeParam2   uint8
417         NodeTile     uint8
418 }
419
420 type HUD struct {
421         Type HUDType
422
423         Pos      [2]float32
424         Name     string
425         Scale    [2]float32
426         Text     string
427         Number   uint32
428         Item     uint32
429         Dir      uint32
430         Align    [2]float32
431         Offset   [2]float32
432         WorldPos Pos
433         Size     [2]int32
434         ZIndex   int16
435         Text2    string
436 }
437
438 type HUDID uint32
439
440 // ToCltHUDAdd tells the client to add a HUD.
441 type ToCltAddHUD struct {
442         ID HUDID
443         HUD
444 }
445
446 type HUDType uint8
447
448 const (
449         ImgHUD HUDType = iota
450         TextHUD
451         StatbarHUD
452         InvHUD
453         WaypointHUD
454         ImgWaypointHUD
455 )
456
457 //go:generate stringer -type HUDType
458
459 // ToCltRmHUD tells the client to remove a HUD.
460 type ToCltRmHUD struct {
461         ID HUDID
462 }
463
464 // ToCltChangeHUD tells the client to change a field in a HUD.
465 type ToCltChangeHUD struct {
466         ID HUDID
467
468         Field HUDField
469
470         //mt:assert %s.Field < hudMax
471
472         //mt:if %s.Field == HUDPos
473         Pos [2]float32
474         //mt:end
475
476         //mt:if %s.Field == HUDName
477         Name string
478         //mt:end
479
480         //mt:if %s.Field == HUDScale
481         Scale [2]float32
482         //mt:end
483
484         //mt:if %s.Field == HUDText
485         Text string
486         //mt:end
487
488         //mt:if %s.Field == HUDNumber
489         Number uint32
490         //mt:end
491
492         //mt:if %s.Field == HUDItem
493         Item uint32
494         //mt:end
495
496         //mt:if %s.Field == HUDDir
497         Dir uint32
498         //mt:end
499
500         //mt:if %s.Field == HUDAlign
501         Align [2]float32
502         //mt:end
503
504         //mt:if %s.Field == HUDOffset
505         Offset [2]float32
506         //mt:end
507
508         //mt:if %s.Field == HUDWorldPos
509         WorldPos Pos
510         //mt:end
511
512         //mt:if %s.Field == HUDSize
513         Size [2]int32
514         //mt:end
515
516         //mt:if %s.Field == HUDZIndex
517         ZIndex int32
518         //mt:end
519
520         //mt:if %s.Field == HUDText2
521         Text2 string
522         //mt:end
523 }
524
525 type HUDField uint8
526
527 const (
528         HUDPos HUDField = iota
529         HUDName
530         HUDScale
531         HUDText
532         HUDNumber
533         HUDItem
534         HUDDir
535         HUDAlign
536         HUDOffset
537         HUDWorldPos
538         HUDSize
539         HUDZIndex
540         HUDText2
541         hudMax
542 )
543
544 //go:generate stringer -trimprefix HUD -type HUDField
545
546 // ToCltHUDFlags tells the client to update its HUD flags.
547 type ToCltHUDFlags struct {
548         // &^= Mask
549         // |= Flags
550         Flags, Mask HUDFlags
551 }
552
553 type HUDFlags uint32
554
555 const (
556         ShowHotbar HUDFlags = 1 << iota
557         ShowHealthBar
558         ShowCrosshair
559         ShowWieldedItem
560         ShowBreathBar
561         ShowMinimap
562         ShowRadarMinimap
563 )
564
565 // ToCltSetHotbarParam tells the client to set a hotbar parameter.
566 type ToCltSetHotbarParam struct {
567         Param HotbarParam
568
569         //mt:if %s.Param == HotbarSize
570         //mt:const uint16(4) // Size of Size field.
571         Size int32
572         //mt:end
573
574         //mt:if %s.Param != HotbarSize
575         Img Texture
576         //mt:end
577 }
578
579 type HotbarParam uint16
580
581 const (
582         HotbarSize HotbarParam = 1 + iota
583         HotbarImg
584         HotbarSelImg
585 )
586
587 //go:generate stringer -trimprefix Hotbar -type HotbarParam
588
589 // ToCltBreath tells the client how much breath it has.
590 type ToCltBreath struct {
591         Breath uint16
592 }
593
594 // ToCltSkyParams tells the client how to render the sky.
595 type ToCltSkyParams struct {
596         BgColor     color.NRGBA
597         Type        string
598         Clouds      bool
599         SunFogTint  color.NRGBA
600         MoonFogTint color.NRGBA
601         FogTintType string
602
603         //mt:if %s.Type == "skybox"
604         Textures []Texture
605         //mt:end
606
607         //mt:if %s.Type == "regular"
608         DaySky, DayHorizon,
609         DawnSky, DawnHorizon,
610         NightSky, NightHorizon,
611         Indoor color.NRGBA
612         //mt:end
613 }
614
615 // ToCltOverrideDayNightRatio overrides the client's day-night ratio
616 type ToCltOverrideDayNightRatio struct {
617         Override bool
618         Ratio    uint16
619 }
620
621 // ToCltLocalPlayerAnim tells the client how to animate the player.
622 type ToCltLocalPlayerAnim struct {
623         Idle, Walk, Dig, WalkDig [2]int32
624         Speed                    float32
625 }
626
627 // ToCltEyeOffset tells the client where to position the camera
628 // relative to the player.
629 type ToCltEyeOffset struct {
630         First, Third Vec
631 }
632
633 // ToCltDelParticleSpawner tells the client to delete a particle spawner.
634 type ToCltDelParticleSpawner struct {
635         ID ParticleSpawnerID
636 }
637
638 // ToCltCloudParams tells the client how to render the clouds.
639 type ToCltCloudParams struct {
640         Density      float32
641         DiffuseColor color.NRGBA
642         AmbientColor color.NRGBA
643         Height       float32
644         Thickness    float32
645         Speed        [2]float32
646 }
647
648 // ToCltFadeSound tells the client to fade a sound.
649 type ToCltFadeSound struct {
650         ID   SoundID
651         Step float32
652         Gain float32
653 }
654
655 // ToCltUpdatePlayerList informs the client of players leaving or joining.
656 type ToCltUpdatePlayerList struct {
657         Type    PlayerListUpdateType
658         Players []string
659 }
660
661 type PlayerListUpdateType uint8
662
663 const (
664         InitPlayers   PlayerListUpdateType = iota // init
665         AddPlayers                                // add
666         RemovePlayers                             // remove
667 )
668
669 //go:generate stringer -linecomment -type PlayerListUpdateType
670
671 // ToCltModChanMsg tells the client it has been sent a message on a mod channel.
672 type ToCltModChanMsg struct {
673         Channel string
674         Sender  string
675         Msg     string
676 }
677
678 // ToCltModChanMsg tells the client it has received a signal on a mod channel.
679 type ToCltModChanSig struct {
680         Signal  ModChanSig
681         Channel string
682 }
683
684 type ModChanSig uint8
685
686 const (
687         JoinOK ModChanSig = iota
688         JoinFail
689         LeaveOK
690         LeaveFail
691         NotRegistered
692         SetState
693 )
694
695 //go:generate stringer -type ModChanSig
696
697 // ToCltModChanMsg is sent when node metadata near the client changes.
698 type ToCltNodeMetasChanged struct {
699         //mt:lenhdr 32
700         Changed map[[3]int16]*NodeMeta
701         //mt:end
702 }
703
704 // ToCltSunParams tells the client how to render the sun.
705 type ToCltSunParams struct {
706         Visible bool
707         Texture
708         ToneMap Texture
709         Rise    Texture
710         Rising  bool
711         Size    float32
712 }
713
714 // ToCltMoonParams tells the client how to render the moon.
715 type ToCltMoonParams struct {
716         Visible bool
717         Texture
718         ToneMap Texture
719         Size    float32
720 }
721
722 // ToCltStarParams tells the client how to render the stars.
723 type ToCltStarParams struct {
724         Visible bool
725         Count   uint32
726         Color   color.NRGBA
727         Size    float32
728 }
729
730 type ToCltSRPBytesSaltB struct {
731         Salt, B []byte
732 }
733
734 // ToCltFormspecPrepend tells the client to prepend a string to all formspecs.
735 type ToCltFormspecPrepend struct {
736         Prepend string
737 }
738
739 // ToCltMinimapModes tells the client the set of available minimap modes.
740 type ToCltMinimapModes struct {
741         Current uint16
742         Modes   []MinimapMode
743 }
744
745 var _ serializer = (*ToCltMinimapModes)(nil)
746
747 func (cmd *ToCltMinimapModes) serialize(w io.Writer) {
748         buf := make([]byte, 4)
749         if len(cmd.Modes) > math.MaxUint16 {
750                 chk(ErrTooLong)
751         }
752         be.PutUint16(buf[0:2], uint16(len(cmd.Modes)))
753         be.PutUint16(buf[2:4], cmd.Current)
754         _, err := w.Write(buf)
755         chk(err)
756         for i := range cmd.Modes {
757                 chk(serialize(w, &cmd.Modes[i]))
758         }
759 }
760
761 var _ deserializer = (*ToCltMinimapModes)(nil)
762
763 func (cmd *ToCltMinimapModes) deserialize(r io.Reader) {
764         buf := make([]byte, 4)
765         _, err := io.ReadFull(r, buf)
766         chk(err)
767         cmd.Modes = make([]MinimapMode, be.Uint16(buf[0:2]))
768         cmd.Current = be.Uint16(buf[2:4])
769         for i := range cmd.Modes {
770                 chk(deserialize(r, &cmd.Modes[i]))
771         }
772 }
773
774 type ToCltDisco struct{}
775
776 func (*ToCltDisco) cmd()                         {}
777 func (*ToCltDisco) toCltCmdNo() uint16           { return 0xffff }
778 func (*ToCltDisco) DefaultPktInfo() rudp.PktInfo { return rudp.PktInfo{} }