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