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