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