-
Notifications
You must be signed in to change notification settings - Fork 26
/
steven.go
271 lines (236 loc) · 6.16 KB
/
steven.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package steven
import (
"fmt"
"math"
"runtime"
"time"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/thinkofdeath/steven/console"
"github.com/thinkofdeath/steven/format"
"github.com/thinkofdeath/steven/protocol/mojang"
"github.com/thinkofdeath/steven/render"
"github.com/thinkofdeath/steven/resource"
"github.com/thinkofdeath/steven/ui"
)
var (
connected bool
skipLogin bool
stevenBuildVersion string = "dev"
clientUsername = console.NewStringVar("cl_username", "", console.Serializable).Doc(`
cl_username is the username that the client will use to connect
to servers.
`)
clientUUID = console.NewStringVar("cl_uuid", "", console.Serializable).Doc(`
cl_uuid is the uuid of the client. This is unique to a player
unlike their username.
`)
clientAccessToken = console.NewStringVar("auth_token", "", console.Serializable).Doc(`
auth_token is the token used for this session to auth to servers
or relogin to this account.
`)
clientToken = console.NewStringVar("auth_client_token", "", console.Serializable).Doc(`
auth_client_token is a token that stays static between sessions.
Used to identify this client vs others.
`)
)
func init() {
console.NewStringVar("cl_version", stevenBuildVersion).Doc(`
cl_version returns the build information compiled into steven.
Returns dev if not set.
`)
console.NewStringVar("cl_mc_version", resource.ResourcesVersion).Doc(`
cl_mc_version returns the minecraft version steven currently supports.
`)
console.Register("connect %", connect)
}
func stevenVersion() string {
return fmt.Sprintf("%s-%s", resource.ResourcesVersion, stevenBuildVersion)
}
func Main(username, uuid, accessToken string) {
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
console.ExecConf("conf.cfg")
if username != "" {
clientUsername.SetValue(username)
}
if uuid != "" {
clientUUID.SetValue(uuid)
}
if accessToken != "" {
clientAccessToken.SetValue(accessToken)
skipLogin = true
}
initResources()
setUIScale()
startWindow()
}
func getProfile() mojang.Profile {
return mojang.Profile{
Username: clientUsername.Value(),
ID: clientUUID.Value(),
AccessToken: clientAccessToken.Value(),
}
}
func connect(server string) {
setScreen(nil)
connected = true
initClient()
disconnectReason.Value = nil
Client.network.Connect(getProfile(), server)
}
func start() {
render.LoadTextures()
initBlocks()
con.init()
initClient()
fakeGen()
if skipLogin {
setScreen(newServerList())
console.ExecConf("autoexec.cfg")
} else {
setScreen(newLoginScreen())
}
render.Start()
}
func rotate(x, y float64) {
Client.Yaw -= x
Client.Pitch -= y
if Client.Pitch < (math.Pi/2)+0.01 {
Client.Pitch = (math.Pi / 2) + 0.01
}
if Client.Pitch > (math.Pi/2)*3-0.01 {
Client.Pitch = (math.Pi/2)*3 - 0.01
}
}
var maxBuilders = runtime.NumCPU() * 2
var (
ready bool
freeBuilders = maxBuilders
completeBuilders = make(chan buildPos, maxBuilders)
syncChan = make(chan func(), 200)
ticker = time.NewTicker(time.Second / 20)
lastFrame = time.Now()
)
func handleErrors() {
handle:
for {
select {
case err := <-Client.network.Error():
if !connected {
continue
}
connected = false
Client.network.Close()
console.Text("Disconnected: %s", err)
// Reset the ready state to stop packets from being
// sent.
ready = false
if err != errManualDisconnect && disconnectReason.Value == nil {
txt := &format.TextComponent{Text: err.Error()}
txt.Color = format.Red
disconnectReason.Value = txt
}
if Client.entity != nil && Client.entityAdded {
Client.entityAdded = false
Client.entities.container.RemoveEntity(Client.entity)
}
setScreen(newServerList())
default:
break handle
}
}
}
func draw() {
now := time.Now()
diff := now.Sub(lastFrame)
lastFrame = now
delta := float64(diff.Nanoseconds()) / (float64(time.Second) / 60)
handle:
for {
select {
case packet := <-Client.network.Read():
defaultHandler.Handle(packet)
case pos := <-completeBuilders:
freeBuilders++
if c := chunkMap[chunkPosition{pos.X, pos.Z}]; c != nil {
if s := c.Sections[pos.Y]; s != nil {
s.building = false
}
}
case f := <-syncChan:
f()
default:
break handle
}
}
handleErrors()
width, height := window.GetFramebufferSize()
if currentScreen != nil {
currentScreen.tick(delta)
}
if ready && Client != nil {
Client.renderTick(delta)
select {
case <-ticker.C:
tick()
default:
}
} else {
render.Camera.Yaw += 0.005 * delta
if render.Camera.Yaw > math.Pi*2 {
render.Camera.Yaw = 0
}
}
con.tick(delta)
ui.Draw(width, height, delta)
tickAudio()
tickClouds(delta)
render.Draw(width, height, delta)
chunks := sortedChunks()
// Search for 'dirty' chunk sections and start building
// them if we have any builders free. To prevent race conditions
// two flags are used, dirty and building, to allow a second
// build to be requested whilst the chunk is still building
// without either losing the change or having two builds
// for the same section going on at once (where the second
// could finish quicker causing the old version to be
// displayed.
dirtyClean:
for _, c := range chunks {
for _, s := range c.Sections {
if s == nil {
continue
}
if freeBuilders <= 0 {
break dirtyClean
}
if s.dirty && !s.building && s.Buffer.Rendered {
freeBuilders--
s.dirty = false
s.building = true
s.build(completeBuilders)
}
}
}
}
// tick is called 20 times a second (bar any preformance issues).
// Minecraft is built around this fact so we have to follow it
// as well.
func tick() {
Client.tick()
}