1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
package proxy
import (
"errors"
"image/color"
"net"
"github.com/HimbeerserverDE/mt"
)
var (
ErrNoServerConn = errors.New("no server connection")
ErrNoSuchServer = errors.New("inexistent server")
ErrNewMediaPool = errors.New("media pool unknown to client")
)
// Hop connects the ClientConn to the specified upstream server
// or the first working fallback server, saving the player's last server
// unless `ForceDefaultSrv` is enabled.
// If all attempts fail the client stays connected to the current server
// with the potential for inconsistent state.
// At the moment the ClientConn is NOT fixed if an error occurs
// so the player may have to reconnect.
func (cc *ClientConn) Hop(serverName string) (err error) {
defer func() {
if err == nil && !Conf().ForceDefaultSrv {
err = authIface.SetLastSrv(cc.Name(), serverName)
}
}()
err = cc.HopRaw(serverName)
return
}
// HopGroup connects the ClientConn to the specified server group
// or the first working fallback server, saving the player's last server
// unless `ForceDefaultSrv` is enabled.
// See the documentation on `Server.Groups` in `doc/config.md`
// for details on how a specific game server is selected from the group name.
// If all attempts fail the client stays connected to the current server
// with the potential for inconsistent state.
// At the moment the ClientConn is NOT fixed if an error occurs
// so the player may have to reconnect.
func (cc *ClientConn) HopGroup(groupName string) error {
choice, ok := Conf().RandomGroupServer(groupName)
if !ok {
return ErrNoSuchServer
}
return cc.Hop(choice)
}
// HopRaw connects the ClientConn to the specified upstream server.
// At the moment the ClientConn is NOT fixed if an error occurs
// so the player may have to reconnect.
//
// This method ignores fallback servers and doesn't save the player's
// last server.
// You may use the `Hop` wrapper for these purposes.
func (cc *ClientConn) HopRaw(serverName string) error {
cc.hopMu.Lock()
defer cc.hopMu.Unlock()
cc.Log("<->", "hop", serverName)
if cc.server() == nil {
return ErrNoServerConn
}
newSrv, ok := Conf().Servers[serverName]
if !ok {
return ErrNoSuchServer
}
if _, ok := cc.denyPools[newSrv.MediaPool]; ok {
return ErrNewMediaPool
}
if newSrv.poolAdded.After(cc.created) {
return ErrNewMediaPool
}
// This needs to be done before the ServerConn is closed
// so the clientConn isn't closed by the packet handler
cc.server().mu.Lock()
cc.server().clt = nil
cc.server().mu.Unlock()
cc.server().Close()
// Player CAO is a good indicator for full client initialization.
if cc.hasPlayerCAO() {
// Reset the client to its initial state
for _, inv := range cc.server().detachedInvs {
cc.SendCmd(&mt.ToCltDetachedInv{
Name: inv,
Keep: false,
})
}
var aoRm []mt.AOID
for ao := range cc.server().aos {
aoRm = append(aoRm, ao)
}
cc.SendCmd(&mt.ToCltAORmAdd{Remove: aoRm})
for spawner := range cc.server().particleSpawners {
cc.SendCmd(&mt.ToCltDelParticleSpawner{ID: spawner})
}
for sound := range cc.server().sounds {
cc.SendCmd(&mt.ToCltStopSound{ID: sound})
}
for hud := range cc.server().huds {
cc.SendCmd(&mt.ToCltRmHUD{ID: hud})
}
// Static parameters
cc.SendCmd(&mt.ToCltBreath{Breath: 10})
cc.SendCmd(&mt.ToCltCloudParams{})
cc.SendCmd(&mt.ToCltEyeOffset{})
cc.SendCmd(&mt.ToCltFOV{})
cc.SendCmd(&mt.ToCltFormspecPrepend{})
cc.SendCmd(&mt.ToCltHP{})
cc.SendCmd(&mt.ToCltHUDFlags{Mask: ^mt.HUDFlags(0), Flags: ^mt.HUDFlags(0)})
cc.SendCmd(&mt.ToCltLocalPlayerAnim{})
cc.SendCmd(&mt.ToCltMinimapModes{
Modes: mt.DefaultMinimap,
})
cc.SendCmd(&mt.ToCltMoonParams{
Visible: true,
Texture: "moon.png",
ToneMap: "moon_tonemap.png",
Size: 1,
})
cc.SendCmd(&mt.ToCltMovement{})
cc.SendCmd(&mt.ToCltOverrideDayNightRatio{})
cc.SendCmd(&mt.ToCltPrivs{})
for i := mt.HotbarParam(mt.HotbarSize); i <= mt.HotbarSelImg; i++ {
cc.SendCmd(&mt.ToCltSetHotbarParam{Param: i})
}
cc.SendCmd(&mt.ToCltSkyParams{
BgColor: color.NRGBA{255, 255, 255, 255},
Type: "regular",
Clouds: true,
SunFogTint: color.NRGBA{244, 125, 29, 255},
MoonFogTint: color.NRGBA{128, 153, 204, 255},
FogTintType: "default",
DayHorizon: color.NRGBA{144, 211, 246, 255},
DawnHorizon: color.NRGBA{186, 193, 240, 255},
NightHorizon: color.NRGBA{64, 144, 255, 255},
DaySky: color.NRGBA{97, 181, 245, 255},
DawnSky: color.NRGBA{180, 186, 250, 255},
NightSky: color.NRGBA{0, 107, 255, 255},
Indoor: color.NRGBA{100, 100, 100, 255},
BodyOrbitTilt: -1024,
FogDistance: -1,
})
cc.SendCmd(&mt.ToCltStarParams{
Visible: true,
Count: 1000,
Color: color.NRGBA{105, 235, 235, 255},
Size: 1,
})
cc.SendCmd(&mt.ToCltSunParams{
Visible: true,
Texture: "sun.png",
ToneMap: "sun_tonemap.png",
Rise: "sunrisebg.png",
Rising: true,
Size: 1,
})
var players []string
for player := range cc.server().playerList {
players = append(players, player)
}
cc.SendCmd(&mt.ToCltUpdatePlayerList{
Type: mt.RemovePlayers,
Players: players,
})
cc.SendCmd(&mt.ToCltLighting{Saturation: 1})
}
cc.mu.Lock()
cc.srv = nil
cc.mu.Unlock()
addr, err := net.ResolveUDPAddr("udp", newSrv.Addr)
if err != nil {
return err
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return err
}
connect(conn, serverName, cc)
for ch := range cc.modChs {
cc.server().SendCmd(&mt.ToSrvJoinModChan{Channel: ch})
}
if cc.cltInfo != nil { // May not be initialized yet if this is an early fallback.
cc.server().SendCmd(cc.cltInfo)
}
return nil
}
|