]> git.lizzy.rs Git - mt.git/blob - tocltcmds.go
7c287491593c0fd7ca38120f86672b07eba483d8
[mt.git] / tocltcmds.go
1 package mt
2
3 import (
4         "fmt"
5         "image/color"
6         "io"
7         "math"
8
9         "github.com/Minetest-j45/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         Style uint32
433 }
434
435 type HUDID uint32
436
437 // ToCltHUDAdd tells the client to add a HUD.
438 type ToCltAddHUD struct {
439         ID HUDID
440         HUD
441 }
442
443 type HUDType uint8
444
445 const (
446         ImgHUD HUDType = iota
447         TextHUD
448         StatbarHUD
449         InvHUD
450         WaypointHUD
451         ImgWaypointHUD
452 )
453
454 //go:generate stringer -type HUDType
455
456 // ToCltRmHUD tells the client to remove a HUD.
457 type ToCltRmHUD struct {
458         ID HUDID
459 }
460
461 // ToCltChangeHUD tells the client to change a field in a HUD.
462 type ToCltChangeHUD struct {
463         ID HUDID
464
465         Field HUDField
466
467         //mt:assert %s.Field < hudMax
468
469         //mt:if %s.Field == HUDPos
470         Pos [2]float32
471         //mt:end
472
473         //mt:if %s.Field == HUDName
474         Name string
475         //mt:end
476
477         //mt:if %s.Field == HUDScale
478         Scale [2]float32
479         //mt:end
480
481         //mt:if %s.Field == HUDText
482         Text string
483         //mt:end
484
485         //mt:if %s.Field == HUDNumber
486         Number uint32
487         //mt:end
488
489         //mt:if %s.Field == HUDItem
490         Item uint32
491         //mt:end
492
493         //mt:if %s.Field == HUDDir
494         Dir uint32
495         //mt:end
496
497         //mt:if %s.Field == HUDAlign
498         Align [2]float32
499         //mt:end
500
501         //mt:if %s.Field == HUDOffset
502         Offset [2]float32
503         //mt:end
504
505         //mt:if %s.Field == HUDWorldPos
506         WorldPos Pos
507         //mt:end
508
509         //mt:if %s.Field == HUDSize
510         Size [2]int32
511         //mt:end
512
513         //mt:if %s.Field == HUDZIndex
514         ZIndex int32
515         //mt:end
516
517         //mt:if %s.Field == HUDText2
518         Text2 string
519         //mt:end
520
521         //mt:if %s.Field == HUDStyle
522         Style HUDStyleFlags
523         //mt:end
524 }
525
526 type HUDField uint8
527
528 const (
529         HUDPos HUDField = iota
530         HUDName
531         HUDScale
532         HUDText
533         HUDNumber
534         HUDItem
535         HUDDir
536         HUDAlign
537         HUDOffset
538         HUDWorldPos
539         HUDSize
540         HUDZIndex
541         HUDText2
542         HUDStyle
543         hudMax
544 )
545
546 //go:generate stringer -trimprefix HUD -type HUDField
547
548 type HUDStyleFlags uint32
549
550 const (
551         StyleBold HUDStyleFlags = 1 << iota
552         StyleItalic
553         StyleMono
554 )
555
556 //go:generate stringer -trimprefix Style -type HUDStyleFlags
557
558 // ToCltHUDFlags tells the client to update its HUD flags.
559 type ToCltHUDFlags struct {
560         // &^= Mask
561         // |= Flags
562         Flags, Mask HUDFlags
563 }
564
565 type HUDFlags uint32
566
567 const (
568         ShowHotbar HUDFlags = 1 << iota
569         ShowHealthBar
570         ShowCrosshair
571         ShowWieldedItem
572         ShowBreathBar
573         ShowMinimap
574         ShowRadarMinimap
575 )
576
577 // ToCltSetHotbarParam tells the client to set a hotbar parameter.
578 type ToCltSetHotbarParam struct {
579         Param HotbarParam
580
581         //mt:if %s.Param == HotbarSize
582         //mt:const uint16(4) // Size of Size field.
583         Size int32
584         //mt:end
585
586         //mt:if %s.Param != HotbarSize
587         Img Texture
588         //mt:end
589 }
590
591 type HotbarParam uint16
592
593 const (
594         HotbarSize HotbarParam = 1 + iota
595         HotbarImg
596         HotbarSelImg
597 )
598
599 //go:generate stringer -trimprefix Hotbar -type HotbarParam
600
601 // ToCltBreath tells the client how much breath it has.
602 type ToCltBreath struct {
603         Breath uint16
604 }
605
606 // ToCltSkyParams tells the client how to render the sky.
607 type ToCltSkyParams struct {
608         BgColor     color.NRGBA
609         Type        string
610         Clouds      bool
611         SunFogTint  color.NRGBA
612         MoonFogTint color.NRGBA
613         FogTintType string
614
615         //mt:if %s.Type == "skybox"
616         Textures []Texture
617         //mt:end
618
619         //mt:if %s.Type == "regular"
620         DaySky, DayHorizon,
621         DawnSky, DawnHorizon,
622         NightSky, NightHorizon,
623         Indoor color.NRGBA
624         //mt:end
625 }
626
627 // ToCltOverrideDayNightRatio overrides the client's day-night ratio
628 type ToCltOverrideDayNightRatio struct {
629         Override bool
630         Ratio    uint16
631 }
632
633 // ToCltLocalPlayerAnim tells the client how to animate the player.
634 type ToCltLocalPlayerAnim struct {
635         Idle, Walk, Dig, WalkDig [2]int32
636         Speed                    float32
637 }
638
639 // ToCltEyeOffset tells the client where to position the camera
640 // relative to the player.
641 type ToCltEyeOffset struct {
642         First, Third Vec
643 }
644
645 // ToCltDelParticleSpawner tells the client to delete a particle spawner.
646 type ToCltDelParticleSpawner struct {
647         ID ParticleSpawnerID
648 }
649
650 // ToCltCloudParams tells the client how to render the clouds.
651 type ToCltCloudParams struct {
652         Density      float32
653         DiffuseColor color.NRGBA
654         AmbientColor color.NRGBA
655         Height       float32
656         Thickness    float32
657         Speed        [2]float32
658 }
659
660 // ToCltFadeSound tells the client to fade a sound.
661 type ToCltFadeSound struct {
662         ID   SoundID
663         Step float32
664         Gain float32
665 }
666
667 // ToCltUpdatePlayerList informs the client of players leaving or joining.
668 type ToCltUpdatePlayerList struct {
669         Type    PlayerListUpdateType
670         Players []string
671 }
672
673 type PlayerListUpdateType uint8
674
675 const (
676         InitPlayers   PlayerListUpdateType = iota // init
677         AddPlayers                                // add
678         RemovePlayers                             // remove
679 )
680
681 //go:generate stringer -linecomment -type PlayerListUpdateType
682
683 // ToCltModChanMsg tells the client it has been sent a message on a mod channel.
684 type ToCltModChanMsg struct {
685         Channel string
686         Sender  string
687         Msg     string
688 }
689
690 // ToCltModChanMsg tells the client it has received a signal on a mod channel.
691 type ToCltModChanSig struct {
692         Signal  ModChanSig
693         Channel string
694 }
695
696 type ModChanSig uint8
697
698 const (
699         JoinOK ModChanSig = iota
700         JoinFail
701         LeaveOK
702         LeaveFail
703         NotRegistered
704         SetState
705 )
706
707 //go:generate stringer -type ModChanSig
708
709 // ToCltModChanMsg is sent when node metadata near the client changes.
710 type ToCltNodeMetasChanged struct {
711         //mt:lenhdr 32
712         Changed map[[3]int16]*NodeMeta
713         //mt:end
714 }
715
716 // ToCltSunParams tells the client how to render the sun.
717 type ToCltSunParams struct {
718         Visible bool
719         Texture
720         ToneMap Texture
721         Rise    Texture
722         Rising  bool
723         Size    float32
724 }
725
726 // ToCltMoonParams tells the client how to render the moon.
727 type ToCltMoonParams struct {
728         Visible bool
729         Texture
730         ToneMap Texture
731         Size    float32
732 }
733
734 // ToCltStarParams tells the client how to render the stars.
735 type ToCltStarParams struct {
736         Visible bool
737         Count   uint32
738         Color   color.NRGBA
739         Size    float32
740 }
741
742 type ToCltSRPBytesSaltB struct {
743         Salt, B []byte
744 }
745
746 // ToCltFormspecPrepend tells the client to prepend a string to all formspecs.
747 type ToCltFormspecPrepend struct {
748         Prepend string
749 }
750
751 // ToCltMinimapModes tells the client the set of available minimap modes.
752 type ToCltMinimapModes struct {
753         Current uint16
754         Modes   []MinimapMode
755 }
756
757 var _ serializer = (*ToCltMinimapModes)(nil)
758
759 func (cmd *ToCltMinimapModes) serialize(w io.Writer) {
760         buf := make([]byte, 4)
761         if len(cmd.Modes) > math.MaxUint16 {
762                 chk(ErrTooLong)
763         }
764         be.PutUint16(buf[0:2], uint16(len(cmd.Modes)))
765         be.PutUint16(buf[2:4], cmd.Current)
766         _, err := w.Write(buf)
767         chk(err)
768         for i := range cmd.Modes {
769                 chk(serialize(w, &cmd.Modes[i]))
770         }
771 }
772
773 var _ deserializer = (*ToCltMinimapModes)(nil)
774
775 func (cmd *ToCltMinimapModes) deserialize(r io.Reader) {
776         buf := make([]byte, 4)
777         _, err := io.ReadFull(r, buf)
778         chk(err)
779         cmd.Modes = make([]MinimapMode, be.Uint16(buf[0:2]))
780         cmd.Current = be.Uint16(buf[2:4])
781         for i := range cmd.Modes {
782                 chk(deserialize(r, &cmd.Modes[i]))
783         }
784 }
785
786 type ToCltDisco struct{}
787
788 func (*ToCltDisco) cmd()                         {}
789 func (*ToCltDisco) toCltCmdNo() uint16           { return 0xffff }
790 func (*ToCltDisco) DefaultPktInfo() rudp.PktInfo { return rudp.PktInfo{} }