-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
230 lines (194 loc) · 4.43 KB
/
flags.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
package game
import (
"log"
"time"
"github.com/sauerbraten/timer"
"github.com/sauerbraten/waiter/pkg/geom"
"github.com/sauerbraten/waiter/pkg/protocol"
"github.com/sauerbraten/waiter/pkg/protocol/nmc"
"github.com/sauerbraten/waiter/pkg/protocol/playerstate"
)
type FlagMode interface {
NeedsMapInfo() bool
FlagsInitPacket() []interface{}
}
type flagMode interface {
TeamMode
CanSpawn(*Player) bool
InitFlags([]*flag) bool
TouchFlag(*Player, *flag)
DropFlag(*Player, *flag)
TeamByFlagTeamID(int32) *Team
}
type flag struct {
index int32
team *Team
teamID int32
carrier *Player
version int32
spawnLocation *geom.Vector
dropLocation *geom.Vector
dropTime time.Time
pendingReset *timer.Timer
}
type handlesFlags struct {
s Server
flagMode
flags []*flag
}
var (
_ FlagMode = &handlesFlags{}
_ HasTimers = &handlesFlags{}
)
func handlingFlags(fm flagMode) *handlesFlags {
return &handlesFlags{
flagMode: fm,
}
}
func (m *handlesFlags) NeedsMapInfo() bool {
log.Println("flag init asked:", len(m.flags))
return len(m.flags) == 0
}
func (m *handlesFlags) HandlePacket(p *Player, packetType nmc.ID, pkt *protocol.Packet) bool {
switch packetType {
case nmc.InitFlags:
m.initFlags(pkt)
case nmc.TouchFlag:
m.touchFlag(p, pkt)
case nmc.TryDropFlag:
m.dropAllFlags(p)
default:
return false
}
return true
}
func (m *handlesFlags) initFlags(pkt *protocol.Packet) {
numFlags, ok := pkt.GetInt()
if !ok {
log.Println("could not read number of flags from initflags packet (packet too short):", pkt)
return
}
flags := []*flag{}
for i := int32(0); i < numFlags; i++ {
teamID, ok := pkt.GetInt()
if !ok {
log.Println("could not read flag team from initflags packet (packet too short):", pkt)
return
}
spawnLocation, ok := pkt.GetVector()
if !ok {
log.Println("could not read flag spawn location from initflags packet (packet too short):", pkt)
return
}
spawnLocation = spawnLocation.Mul(1 / geom.DMF)
flags = append(flags, &flag{
index: i,
team: m.TeamByFlagTeamID(teamID),
teamID: teamID,
spawnLocation: spawnLocation,
})
}
if len(m.flags) != 0 {
log.Println("got initflags packet, but flags are already initialized")
return
}
ok = m.InitFlags(flags)
if ok {
m.flags = flags
}
}
func (m *handlesFlags) touchFlag(p *Player, pkt *protocol.Packet) {
if p.State != playerstate.Alive {
return
}
i, ok := pkt.GetInt()
if !ok {
log.Println("could not read flag index from takeflag packet (packet too short):", pkt)
return
}
if i < 0 || len(m.flags) <= int(i) {
log.Printf("flag index %d from takeflag packet out of range [0..%d]", i, len(m.flags))
return
}
f := m.flags[i]
if f.carrier != nil {
return
}
version, ok := pkt.GetInt()
if !ok {
log.Println("could not read flag version from takeflag packet (packet too short):", pkt)
return
}
if f.version != version {
return
}
m.TouchFlag(p, f)
}
func (m *handlesFlags) dropAllFlags(p *Player) {
for _, f := range m.flags {
if f != nil && f.carrier == p {
m.DropFlag(p, f)
}
}
}
func (m *handlesFlags) FlagsInitPacket() []interface{} {
q := []interface{}{}
for _, f := range m.flags {
if f.team == nil {
log.Printf("flag with index '%d' has no team!", f.index)
continue
}
q = append(q, f.team.Score)
}
q = append(q, len(m.flags))
for _, f := range m.flags {
if f == nil || f.team == nil {
continue
}
var carrierCN int32 = -1
if f.carrier != nil {
carrierCN = int32(f.carrier.CN)
}
q = append(q, f.version, 0, carrierCN, 0)
if f.carrier == nil {
dropped := !f.dropTime.IsZero()
q = append(q, dropped)
if dropped {
q = append(q, f.dropLocation.Mul(geom.DMF))
}
}
}
return q
}
func (m *handlesFlags) HandleFrag(actor, victim *Player) {
m.dropAllFlags(victim)
m.flagMode.HandleFrag(actor, victim)
}
func (m *handlesFlags) Pause() {
for _, f := range m.flags {
if f == nil || f.pendingReset == nil || f.pendingReset.TimeLeft() == 0 {
continue
}
f.pendingReset.Pause()
}
}
func (m *handlesFlags) Resume() {
for _, f := range m.flags {
if f == nil || f.pendingReset == nil || f.pendingReset.TimeLeft() == 0 {
continue
}
f.pendingReset.Start()
}
}
func (m *handlesFlags) Leave(p *Player) {
m.dropAllFlags(p)
m.flagMode.Leave(p)
}
func (m *handlesFlags) CleanUp() {
for _, f := range m.flags {
if f == nil || f.pendingReset == nil {
continue
}
f.pendingReset.Stop()
}
}