-
Notifications
You must be signed in to change notification settings - Fork 21
/
player.go
177 lines (149 loc) · 4.3 KB
/
player.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
package main
import (
"os"
"io"
"log"
"net"
"math"
"bytes"
)
type Player struct {
Entity
game *Game
conn net.Conn
name string
position XYZ
orientation Orientation
currentItem int16
txQueue chan []byte
}
func StartPlayer(game *Game, conn net.Conn, name string) {
player := &Player{
game: game,
conn: conn,
name: name,
position: StartPosition,
orientation: Orientation{0, 0},
txQueue: make(chan []byte, 128),
}
go player.ReceiveLoop()
go player.TransmitLoop()
game.Enqueue(func(game *Game) {
game.AddPlayer(player)
player.postLogin()
})
}
func (player *Player) PacketKeepAlive() {
}
func (player *Player) PacketChatMessage(message string) {
log.Stderrf("PacketChatMessage message=%s", message)
player.game.Enqueue(func(game *Game) { game.SendChatMessage(message) })
}
func (player *Player) PacketFlying(flying bool) {
}
func (player *Player) PacketPlayerPosition(position *XYZ, stance float64, flying bool) {
log.Stderrf("PacketPlayerPosition position=(%.2f, %.2f, %.2f) stance=%.2f flying=%v",
position.x, position.y, position.z, stance, flying)
player.game.Enqueue(func(game *Game) {
var delta = XYZ{position.x - player.position.x,
position.y - player.position.y,
position.z - player.position.z}
distance := math.Sqrt(delta.x*delta.x + delta.y*delta.y + delta.z*delta.z)
if distance > 10 {
log.Stderrf("Discarding player position that is too far removed (%.2f, %.2f, %.2f)",
position.x, position.y, position.z)
return
}
player.position = *position
buf := &bytes.Buffer{}
WriteEntityTeleport(buf, player.EntityID, &player.position, &player.orientation)
game.MulticastPacket(buf.Bytes(), player)
})
}
func (player *Player) PacketPlayerLook(orientation *Orientation, flying bool) {
player.game.Enqueue(func(game *Game) {
// TODO input validation
player.orientation = *orientation
buf := &bytes.Buffer{}
WriteEntityLook(buf, player.EntityID, orientation)
game.MulticastPacket(buf.Bytes(), player)
})
}
func (player *Player) PacketPlayerDigging(status byte, x int32, y byte, z int32, face byte) {
log.Stderrf("PacketPlayerDigging status=%d x=%d y=%d z=%d face=%d",
status, x, y, z, face)
}
func (player *Player) PacketPlayerBlockPlacement(blockItemID int16, x int32, y byte, z int32, direction byte) {
log.Stderrf("PacketPlayerBlockPlacement blockItemID=%d x=%d y=%d z=%d direction=%d",
blockItemID, x, y, z, direction)
}
func (player *Player) PacketHoldingChange(blockItemID int16) {
log.Stderrf("PacketHoldingChange blockItemID=%d", blockItemID)
}
func (player *Player) PacketArmAnimation(forward bool) {
log.Stderrf("PacketArmAnimation forward=%v", forward)
}
func (player *Player) PacketDisconnect(reason string) {
log.Stderrf("PacketDisconnect reason=%s", reason)
player.game.Enqueue(func(game *Game) {
game.RemovePlayer(player)
close(player.txQueue)
player.conn.Close()
})
}
func (player *Player) ReceiveLoop() {
for {
err := ReadPacket(player.conn, player)
if err != nil {
if err != os.EOF {
log.Stderr("ReceiveLoop failed: ", err.String())
}
return
}
}
}
func (player *Player) TransmitLoop() {
for {
bs := <-player.txQueue
if bs == nil {
return // txQueue closed
}
_, err := player.conn.Write(bs)
if err != nil {
if err != os.EOF {
log.Stderr("TransmitLoop failed: ", err.String())
}
return
}
}
}
func (player *Player) sendChunks(writer io.Writer) {
playerX := ChunkCoord(player.position.x / ChunkSizeX)
playerZ := ChunkCoord(player.position.z / ChunkSizeZ)
for z := playerZ - ChunkRadius; z <= playerZ+ChunkRadius; z++ {
for x := playerX - ChunkRadius; x <= playerX+ChunkRadius; x++ {
WritePreChunk(writer, x, z, true)
}
}
for z := playerZ - ChunkRadius; z <= playerZ+ChunkRadius; z++ {
for x := playerX - ChunkRadius; x <= playerX+ChunkRadius; x++ {
chunk := player.game.chunkManager.Get(x, z)
WriteMapChunk(writer, chunk)
}
}
}
func (player *Player) TransmitPacket(packet []byte) {
if packet == nil {
return // skip empty packets
}
player.txQueue <- packet
}
func (player *Player) postLogin() {
buf := &bytes.Buffer{}
WriteSpawnPosition(buf, &player.position)
player.sendChunks(buf)
WritePlayerInventory(buf)
WritePlayerPositionLook(buf, &player.position, &player.orientation,
0, false)
player.TransmitPacket(buf.Bytes())
}