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